Plugin.class.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * Manage plugin système trhought php hooks
  4. * @author valentin carruesco
  5. * @category Core
  6. * @license MIT
  7. */
  8. class Plugin{
  9. public $id,$name,$author,$link,$licence,$folder,$description,$version,$state,$type,$require,$firms,$color,$icon;
  10. function __construct(){
  11. $this->state = false;
  12. }
  13. public function toArray(){
  14. return array(
  15. 'id' => $this->id,
  16. 'name' => $this->name,
  17. 'author' => $this->author,
  18. 'link' => $this->link,
  19. 'licence' => $this->licence,
  20. 'folder' => $this->folder,
  21. 'description' => $this->description,
  22. 'version' => $this->version,
  23. 'state' => $this->state,
  24. 'type' => $this->type,
  25. 'require' => $this->require,
  26. 'firms' => $this->firms,
  27. 'color' => $this->color,
  28. 'icon' => $this->icon
  29. );
  30. }
  31. public static function exist($id){
  32. foreach(self::getAll() as $plugin)
  33. if($plugin->id==$id) return true;
  34. return false;
  35. }
  36. public static function addHook($hookName, $functionName){
  37. $GLOBALS['hooks'][$hookName][] = $functionName;
  38. }
  39. public static function callHook($hookName, $hookArguments = array()) {
  40. if(!isset($GLOBALS['hooks'][$hookName])) return;
  41. foreach($GLOBALS['hooks'][$hookName] as $functionName)
  42. call_user_func_array($functionName, $hookArguments);
  43. }
  44. public static function includeAll(){
  45. global $myFirm;
  46. $validFirm = is_object($myFirm) && is_numeric($myFirm->id) && $myFirm->id!=-1;
  47. foreach(self::getAll() as $plugin) {
  48. if(!$plugin->state) continue;
  49. if($validFirm && !in_array($myFirm->id, $plugin->firms)) continue;
  50. $main = $plugin->path().SLASH.$plugin->folder.'.plugin.php';
  51. if(file_exists($main)) require_once($main);
  52. }
  53. }
  54. public static function getAll($enableOnly = false,$includeCore = false){
  55. $plugins = array();
  56. foreach(glob(__ROOT__.PLUGIN_PATH.'*'.SLASH.'app.json') as $file){
  57. $plugin = self::parseManifest($file);
  58. if($enableOnly && !$plugin->state) continue;
  59. $plugins[] = $plugin;
  60. }
  61. if($includeCore) $plugins[] = self::parseManifest('.');
  62. usort($plugins, function($a, $b){
  63. if ($a->name == $b->name)
  64. $result = 0;
  65. if($a->name < $b->name){
  66. $result = -1;
  67. } else{
  68. $result = 1;
  69. }
  70. return $result;
  71. });
  72. return $plugins;
  73. }
  74. public static function getById($id){
  75. $plugin = false;
  76. foreach(self::getAll() as $onePlugin)
  77. if($onePlugin->id==$id) $plugin = $onePlugin;
  78. return $plugin;
  79. }
  80. public static function parseManifest($file){
  81. //si on cible le coeur
  82. if($file=='.' || basename(dirname($file)) == 'class'){
  83. $plugin = new Plugin();
  84. $plugin->name = 'Coeur logiciel';
  85. $plugin->id = 'fr.core.core';
  86. $plugin->folder = '.';
  87. $plugin->state = true;
  88. $plugin->firms = array(0);
  89. $plugin->require = array();
  90. $plugin->author = 'god';
  91. $plugin->url = '';
  92. $plugin->licence = 'copyright';
  93. $plugin->description = 'Coeur de l\'application';
  94. $plugin->version = '1.0.0';
  95. $plugin->core = true;
  96. $plugin->color ='#222222';
  97. $plugin->icon = 'fas fa-bullseye';
  98. //si c'est un plugin classique
  99. }else{
  100. $enabled = self::states();
  101. $plugin = new self();
  102. $manifest = json_decode(file_get_contents($file),true);
  103. if(!$manifest) return $plugin;
  104. if(!isset($manifest['id']) || !isset($manifest['name']) ) return;
  105. $plugin->name = $manifest['name'];
  106. $plugin->id = $manifest['id'];
  107. $plugin->folder = basename(dirname($file));
  108. if(isset($enabled[$plugin->id])){
  109. $plugin->state = true;
  110. $plugin->firms = $enabled[$plugin->id];
  111. }
  112. if(isset($manifest['author'])) $plugin->author = $manifest['author'];
  113. if(isset($manifest['url'])) $plugin->url = $manifest['url'];
  114. if(isset($manifest['licence'])) $plugin->licence = $manifest['licence'];
  115. if(isset($manifest['description'])) $plugin->description = $manifest['description'];
  116. if(isset($manifest['version'])) $plugin->version = $manifest['version'];
  117. if(isset($manifest['core'])) $plugin->core = $manifest['core'];
  118. if(isset($manifest['color'])) $plugin->color = $manifest['color'];
  119. if(isset($manifest['icon'])) $plugin->icon = $manifest['icon'];
  120. if(isset($manifest['require'])) $plugin->require = isset($manifest['require'])? $manifest['require']: array();
  121. }
  122. return $plugin;
  123. }
  124. public static function state($id,$state){
  125. $enabled = self::states();
  126. $plugin = self::getById($id);
  127. $main = $plugin->path().SLASH.$plugin->folder.'.plugin.php';
  128. if(file_exists($main))
  129. require_once($main);
  130. if($state==0){
  131. unset($enabled[$plugin->id]);
  132. Plugin::callHook('uninstall',array($plugin->id));
  133. } else {
  134. if(!isset($enabled[$plugin->id]))
  135. $enabled[$plugin->id] = array();
  136. Plugin::callHook('install',array($plugin->id));
  137. }
  138. self::states($enabled);
  139. }
  140. public static function states($states = false){
  141. $enabledFile = File::core().SLASH.'plugin.json';
  142. if(!file_exists($enabledFile)) touch($enabledFile);
  143. if(!is_array($states)){
  144. $enabled = json_decode(file_get_contents($enabledFile),true);
  145. return !is_array($enabled)?array():$enabled;
  146. }
  147. file_put_contents($enabledFile,json_encode($states));
  148. }
  149. public static function is_active($id){
  150. $enabled = self::states();
  151. return isset($enabled[$id]);
  152. }
  153. public function path(){
  154. if($this->folder=='.') return __ROOT__.SLASH.'class';
  155. return __ROOT__.PLUGIN_PATH.$this->folder;
  156. }
  157. public static function folder($path){
  158. return str_replace(__ROOT__,'',$path).SLASH;
  159. }
  160. public static function url(){
  161. $bt = debug_backtrace();
  162. return ROOT_URL.'/'.str_replace(SLASH, '/', PLUGIN_PATH).basename(dirname($bt[0]['file']));
  163. }
  164. public static function addCss($css,$options = array('forcePath'=>false)) {
  165. if(!$options['forcePath']){
  166. $bt = debug_backtrace();
  167. if(substr($css, 0,4) != 'http') $css = str_replace(array(__ROOT__,'\\'),array('','/'),dirname($bt[0]['file'])).$css;
  168. }
  169. $GLOBALS['hooks']['css_files'][] = $css;
  170. }
  171. public static function callCss($root = null,$cacheVersion = ''){
  172. if(!isset($GLOBALS['hooks']['css_files'])) return '';
  173. $stream = '';
  174. foreach($GLOBALS['hooks']['css_files'] as $css_file){
  175. $url = $css_file;
  176. if(isset($root) && substr($css_file,0, 4) != 'http') $url = $root.'/'.$url;
  177. $stream .='<link rel="stylesheet" href="'.$url.'?v='.$cacheVersion.'">'."\n";
  178. }
  179. return $stream;
  180. }
  181. public static function addJs($js){
  182. global $_;
  183. $bt = debug_backtrace();
  184. if(substr($js, 0,4) != 'http') $js = str_replace(array(__ROOT__,'\\'),array('','/'),dirname($bt[0]['file'])).$js;
  185. $GLOBALS['hooks']['js_files'][] = $js;
  186. }
  187. public static function callJs($root = null,$cacheVersion = ''){
  188. if(!isset($GLOBALS['hooks']['js_files'])) return '';
  189. $stream = '';
  190. foreach($GLOBALS['hooks']['js_files'] as $js_file){
  191. $url = $js_file;
  192. if(isset($root) && substr($js_file,0, 4) != 'http') $url = $root.'/'.$url;
  193. $stream .='<script type="text/javascript" src="'.$url.'?v='.$cacheVersion.'"></script>'."\n";
  194. }
  195. return $stream;
  196. }
  197. /*
  198. * Aide à l'inclusion de classes de plugin, ex:
  199. * require_once('..'.SLASH.'plugin'.SLASH.'issue'.SLASH.'Event.class.php');
  200. * devient :
  201. * Plugin::need('issue/Event');
  202. * Il est possible de faires des inclusions mulitples, ex :
  203. * Plugin::need('issue/Event, client/Client, client/ClientContact');
  204. */
  205. public static function need($selectors){
  206. foreach(explode(',',$selectors) as $selector){
  207. list($plugin,$class) = explode('/',trim($selector));
  208. require_once(__ROOT__.PLUGIN_PATH.$plugin.SLASH.$class.'.class.php');
  209. }
  210. }
  211. }