Plugin.class.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. /*
  3. @nom: Plugin
  4. @auteur: Valentin CARRUESCO (valentin.carruesco@sys1.fr)
  5. @description: Classe de gestion des plugins au travers de l'application
  6. */
  7. class Plugin{
  8. const FOLDER = '/plugins';
  9. protected $name,$author,$mail,$link,$licence,$path,$description,$version,$state,$type;
  10. function __construct(){
  11. }
  12. public static function includeAll(){
  13. $pluginFiles = Plugin::getFiles(true);
  14. if(is_array($pluginFiles)) {
  15. foreach($pluginFiles as $pluginFile) {
  16. //Inclusion du coeur de plugin
  17. include $pluginFile;
  18. //Gestion des css du plugin en fonction du thème actif
  19. $cssTheme = glob('../'.dirname($pluginFile).'/*/'.DEFAULT_THEME.'.css');
  20. $cssDefault = glob('../'.dirname($pluginFile).'/*/default.css');
  21. if(isset($cssTheme[0])){
  22. $GLOBALS['hooks']['css_files'][] = Functions::relativePath(str_replace('\\','/',dirname(__FILE__)),str_replace('\\','/',$cssTheme[0]));
  23. }else if(isset($cssDefault[0])){
  24. $GLOBALS['hooks']['css_files'][] = Functions::relativePath(str_replace('\\','/',dirname(__FILE__)),str_replace('\\','/',$cssDefault[0]));
  25. }
  26. }
  27. }
  28. }
  29. private static function getStates(){
  30. $stateFile = dirname(dirname(__FILE__)).Plugin::FOLDER.'/plugins.states.json';
  31. if(!file_exists($stateFile)) touch($stateFile);
  32. return json_decode(file_get_contents($stateFile),true);
  33. }
  34. private static function setStates($states){
  35. $stateFile = dirname(dirname(__FILE__)).Plugin::FOLDER.'/plugins.states.json';
  36. file_put_contents($stateFile,json_encode($states));
  37. }
  38. private static function getObject($pluginFile){
  39. $plugin = new Plugin();
  40. $fileLines = file_get_contents($pluginFile);
  41. if(preg_match("#@author\s(.+)\s\<#", $fileLines, $match))
  42. $plugin->setAuthor(trim($match[1]));
  43. if(preg_match("#@author\s(.+)\s\<([a-z\@\.A-Z\s\-]+)\>#", $fileLines, $match))
  44. $plugin->setMail(strtolower($match[2]));
  45. if(preg_match("#@name\s(.+)[\r\n]#", $fileLines, $match))
  46. $plugin->setName($match[1]);
  47. if(preg_match("#@licence\s(.+)[\r\n]#", $fileLines, $match))
  48. $plugin->setLicence($match[1]);
  49. if(preg_match("#@version\s(.+)[\r\n]#", $fileLines, $match))
  50. $plugin->setVersion($match[1]);
  51. if(preg_match("#@link\s(.+)[\r\n]#", $fileLines, $match))
  52. $plugin->setLink(trim($match[1]));
  53. if(preg_match("#@type\s(.+)[\r\n]#", $fileLines, $match))
  54. $plugin->setType(trim($match[1]));
  55. if(preg_match("#@description\s(.+)[\r\n]#", $fileLines, $match))
  56. $plugin->setDescription(trim($match[1]));
  57. if(Plugin::loadState($pluginFile) || $plugin->getType()=='component'){
  58. $plugin->setState(1);
  59. }else{
  60. $plugin->setState(0);
  61. }
  62. $plugin->setPath($pluginFile);
  63. return $plugin;
  64. }
  65. public static function getAll(){
  66. $pluginFiles = Plugin::getFiles();
  67. $plugins = array();
  68. if(is_array($pluginFiles)) {
  69. foreach($pluginFiles as $pluginFile) {
  70. $plugin = Plugin::getObject($pluginFile);
  71. $plugins[]=$plugin;
  72. }
  73. }
  74. usort($plugins, "Plugin::sortPlugin");
  75. return $plugins;
  76. }
  77. public static function getFiles($onlyActivated=false){
  78. $enabled = $disabled = array();
  79. $files = glob(dirname(dirname(__FILE__)). Plugin::FOLDER .'/*/*.plugin*.php');
  80. $plugins = array();
  81. foreach($files as $file){
  82. $plugins[] = Plugin::getObject($file);
  83. }
  84. usort($plugins, "Plugin::sortPlugin");
  85. foreach($plugins as $plugin){
  86. if($plugin->getState() || $plugin->getType() =='component'){
  87. $enabled [] = $plugin->getPath();
  88. }else{
  89. $disabled [] = $plugin->getPath();
  90. }
  91. }
  92. if(!$onlyActivated)$enabled = array_merge($enabled,$disabled);
  93. return $enabled;
  94. }
  95. public static function addHook($hookName, $functionName) {
  96. $GLOBALS['hooks'][$hookName][] = $functionName;
  97. }
  98. public static function callCss(){
  99. $return='';
  100. if(isset($GLOBALS['hooks']['css_files'])) {
  101. foreach($GLOBALS['hooks']['css_files'] as $css_file) {
  102. $return .='<link href="'.$css_file.'" rel="stylesheet">'."\n";
  103. }
  104. }
  105. return $return;
  106. }
  107. public static function addLink($rel, $link) {
  108. $GLOBALS['hooks']['head_link'][] = array("rel"=>$rel, "link"=>$link);
  109. }
  110. public static function callLink(){
  111. $return='';
  112. if(isset($GLOBALS['hooks']['head_link'])) {
  113. foreach($GLOBALS['hooks']['head_link'] as $head_link) {
  114. $return .='<link href="'.$head_link['link'].'" rel="'.$head_link['rel'].'" />'."\n";
  115. }
  116. }
  117. return $return;
  118. }
  119. public static function path(){
  120. $bt = debug_backtrace();
  121. return Functions::relativePath(str_replace('\\','/',dirname(dirname(__FILE__))),str_replace('\\','/',dirname($bt[0]['file']))).'/';
  122. }
  123. public static function addCss($css,$force = false) {
  124. $bt = debug_backtrace();
  125. $module = isset($_GET['module'])?$_GET['module']:'';
  126. $module = isset($_GET['section']) && $module==''?$_GET['section']:$module;
  127. $module = isset($_GET['block']) && $module==''?$_GET['block']:$module;
  128. $path = Functions::relativePath(str_replace('\\','/',dirname(dirname(__FILE__))),str_replace('\\','/',dirname($bt[0]['file']).$css));
  129. if(strcasecmp(basename(dirname($bt[0]['file'])), $module) == 0 || $force)
  130. $GLOBALS['hooks']['css_files'][] = $path;
  131. }
  132. public static function addJs($js,$force = false){
  133. global $_;
  134. $bt = debug_backtrace();
  135. $module = isset($_GET['module'])?$_GET['module']:'';
  136. $module = isset($_GET['section']) && $module==''?$_GET['section']:$module;
  137. $module = isset($_GET['block']) && $module==''?$_GET['block']:$module;
  138. $path = Functions::relativePath(str_replace('\\','/',dirname(dirname(__FILE__))),str_replace('\\','/',dirname($bt[0]['file']).$js));
  139. if(strcasecmp(basename(dirname($bt[0]['file'])), $module) == 0 || $force)
  140. $GLOBALS['hooks']['js_files'][] = $path;
  141. }
  142. public static function callJs(){
  143. $return='';
  144. if(isset($GLOBALS['hooks']['js_files'])) {
  145. foreach($GLOBALS['hooks']['js_files'] as $js_file) {
  146. $return .='<script type="text/javascript" src="'.$js_file.'"></script>'."\n";
  147. }
  148. }
  149. return $return;
  150. }
  151. public static function callHook($hookName, $hookArguments) {
  152. //echo '<div style="display:inline;background-color:#CC47CB;padding:3px;border:5px solid #9F1A9E;border-radius:5px;color:#ffffff;font-size:15px;">'.$hookName.'</div>';
  153. if(isset($GLOBALS['hooks'][$hookName])) {
  154. foreach($GLOBALS['hooks'][$hookName] as $functionName) {
  155. call_user_func_array($functionName, $hookArguments);
  156. }
  157. }
  158. }
  159. //Définis si un plugin existe et si il est activé ou non
  160. public static function exist($pluginName){
  161. $exist = false;
  162. $file = glob(dirname(dirname(__FILE__)). Plugin::FOLDER .'/'.$pluginName.'/*.plugin*.php');
  163. if(count($file)!=0){
  164. $plugin = Plugin::getObject($file[0]);
  165. if($plugin->getState() || $plugin->getType() =='component'){
  166. $exist = true;
  167. }
  168. }
  169. return $exist;
  170. }
  171. public static function loadState($plugin){
  172. $states = Plugin::getStates();
  173. return (isset($states[$plugin])?$states[$plugin]:false);
  174. }
  175. public static function changeState($plugin,$state){
  176. $states = Plugin::getStates();
  177. $states[$plugin] = $state;
  178. Plugin::setStates($states);
  179. }
  180. public static function enabled($pluginUid){
  181. $plugins = Plugin::getAll();
  182. foreach($plugins as $plugin){
  183. if($plugin->getUid()==$pluginUid){
  184. Plugin::changeState($plugin->getPath(),true);
  185. $install = dirname($plugin->getPath()).'/install';
  186. if(file_exists($install)) {
  187. require_once($install);
  188. }else if (file_exists($install.'.php')){
  189. require_once($install.'.php');
  190. }
  191. }
  192. }
  193. }
  194. public static function disabled($pluginUid){
  195. $plugins = Plugin::getAll();
  196. foreach($plugins as $plugin){
  197. if($plugin->getUid()==$pluginUid){
  198. Plugin::changeState($plugin->getPath(),false);
  199. $uninstall = dirname($plugin->getPath()).'/uninstall';
  200. if(file_exists($uninstall)){
  201. require_once($uninstall);
  202. }else if (file_exists($uninstall.'.php')){
  203. require_once($uninstall.'.php');
  204. }
  205. }
  206. }
  207. }
  208. function getUid(){
  209. $pathInfo = explode('/',$this->getPath());
  210. $count = count($pathInfo);
  211. $name = $pathInfo[$count-1];
  212. return $pathInfo[$count -2].'-'.substr($name,0,strpos($name,'.'));
  213. }
  214. static function sortPlugin($a, $b){
  215. if ($a->getName() == $b->getName())
  216. $result = 0;
  217. if($a->getName() < $b->getName()){
  218. $result = -1;
  219. } else{
  220. $result = 1;
  221. }
  222. if($b->getType() != $a->getType()){
  223. if($a->getType() == 'component'){
  224. $result = -1;
  225. }else{
  226. $result = 1;
  227. }
  228. }
  229. return $result;
  230. }
  231. function getName(){
  232. return $this->name;
  233. }
  234. function setName($name){
  235. $this->name = $name;
  236. }
  237. function setAuthor($author){
  238. $this->author = $author;
  239. }
  240. function getAuthor(){
  241. return $this->author;
  242. }
  243. function getMail(){
  244. return $this->mail;
  245. }
  246. function setMail($mail){
  247. $this->mail = $mail;
  248. }
  249. function getLicence(){
  250. return $this->licence;
  251. }
  252. function setLicence($licence){
  253. $this->licence = $licence;
  254. }
  255. function getPath(){
  256. return $this->path;
  257. }
  258. function setPath($path){
  259. $this->path = $path;
  260. }
  261. function getDescription(){
  262. return $this->description;
  263. }
  264. function setDescription($description){
  265. $this->description = $description;
  266. }
  267. function getLink(){
  268. return $this->link;
  269. }
  270. function setLink($link){
  271. $this->link = $link;
  272. }
  273. function getVersion(){
  274. return $this->version;
  275. }
  276. function setVersion($version){
  277. $this->version = $version;
  278. }
  279. function getState(){
  280. return $this->state;
  281. }
  282. function setState($state){
  283. $this->state = $state;
  284. }
  285. function getType(){
  286. return $this->type;
  287. }
  288. function setType($type){
  289. $this->type = $type;
  290. }
  291. }
  292. ?>