Plugin.class.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. return strcmp($a->name, $b->name);
  64. });
  65. return $plugins;
  66. }
  67. public static function getById($id){
  68. $plugin = false;
  69. foreach(self::getAll() as $onePlugin)
  70. if($onePlugin->id==$id) $plugin = $onePlugin;
  71. return $plugin;
  72. }
  73. public static function parseManifest($file){
  74. //Si on cible le coeur
  75. if($file=='.' || basename(dirname($file)) == 'class'){
  76. $plugin = new Plugin();
  77. $plugin->name = 'Coeur logiciel';
  78. $plugin->id = 'fr.core.core';
  79. $plugin->folder = '.';
  80. $plugin->state = true;
  81. $plugin->firms = array(0);
  82. $plugin->require = array();
  83. $plugin->author = 'god';
  84. $plugin->url = '';
  85. $plugin->licence = 'copyright';
  86. $plugin->description = 'Coeur de l\'application';
  87. $plugin->version = '1.0.0';
  88. $plugin->core = true;
  89. $plugin->color ='#222222';
  90. $plugin->icon = 'fas fa-bullseye';
  91. //Si c'est un plugin classique
  92. }else{
  93. $enabled = self::states();
  94. $plugin = new self();
  95. $manifest = json_decode(file_get_contents($file),true);
  96. if(!$manifest) return $plugin;
  97. if(!isset($manifest['id']) || !isset($manifest['name']) ) return;
  98. $plugin->name = $manifest['name'];
  99. $plugin->id = $manifest['id'];
  100. $plugin->folder = basename(dirname($file));
  101. if(isset($enabled[$plugin->id])){
  102. $plugin->state = true;
  103. $plugin->firms = $enabled[$plugin->id];
  104. }
  105. if(isset($manifest['author'])) $plugin->author = $manifest['author'];
  106. if(isset($manifest['url'])) $plugin->url = $manifest['url'];
  107. if(isset($manifest['licence'])) $plugin->licence = $manifest['licence'];
  108. if(isset($manifest['description'])) $plugin->description = $manifest['description'];
  109. if(isset($manifest['version'])) $plugin->version = $manifest['version'];
  110. if(isset($manifest['core'])) $plugin->core = $manifest['core'];
  111. if(isset($manifest['color'])) $plugin->color = $manifest['color'];
  112. if(isset($manifest['icon'])) $plugin->icon = $manifest['icon'];
  113. if(isset($manifest['require'])) $plugin->require = isset($manifest['require']) ? $manifest['require'] : array();
  114. }
  115. return $plugin;
  116. }
  117. public static function state($id,$state){
  118. $enabled = self::states();
  119. $plugin = self::getById($id);
  120. $main = $plugin->path().SLASH.$plugin->folder.'.plugin.php';
  121. if(file_exists($main)) require_once($main);
  122. if($state==0){
  123. unset($enabled[$plugin->id]);
  124. Plugin::callHook('uninstall',array($plugin->id));
  125. } else {
  126. if(!isset($enabled[$plugin->id])) $enabled[$plugin->id] = array();
  127. Plugin::callHook('install',array($plugin->id));
  128. }
  129. self::states($enabled);
  130. }
  131. public static function states($states = false){
  132. $enabledFile = File::core().SLASH.'plugin.json';
  133. if(!file_exists($enabledFile)) touch($enabledFile);
  134. if(!is_array($states)){
  135. $enabled = json_decode(file_get_contents($enabledFile),true);
  136. return !is_array($enabled)?array():$enabled;
  137. }
  138. file_put_contents($enabledFile,json_encode($states));
  139. }
  140. public static function is_active($id){
  141. $enabled = self::states();
  142. return isset($enabled[$id]);
  143. }
  144. public function path(){
  145. if($this->folder=='.') return __ROOT__.SLASH.'class';
  146. return __ROOT__.PLUGIN_PATH.$this->folder;
  147. }
  148. public static function folder($path){
  149. return str_replace(__ROOT__,'',$path).SLASH;
  150. }
  151. public static function url(){
  152. $bt = debug_backtrace();
  153. return ROOT_URL.'/'.str_replace(SLASH, '/', PLUGIN_PATH).basename(dirname($bt[0]['file']));
  154. }
  155. public static function addCss($css,$options = array('forcePath'=>false)) {
  156. if(!$options['forcePath']){
  157. $bt = debug_backtrace();
  158. if(substr($css, 0,4) != 'http') $css = str_replace(array(__ROOT__,'\\'),array('','/'),dirname($bt[0]['file'])).$css;
  159. }
  160. $GLOBALS['hooks']['css_files'][] = $css;
  161. }
  162. public static function callCss($root = null,$cacheVersion = ''){
  163. if(!isset($GLOBALS['hooks']['css_files'])) return '';
  164. $stream = '';
  165. foreach($GLOBALS['hooks']['css_files'] as $css_file){
  166. $url = $css_file;
  167. if(isset($root) && substr($css_file,0, 4) != 'http') $url = $root.'/'.$url;
  168. $stream .='<link rel="stylesheet" href="'.$url.'?v='.$cacheVersion.'">'."\n";
  169. }
  170. return $stream;
  171. }
  172. public static function addJs($js){
  173. global $_;
  174. $bt = debug_backtrace();
  175. if(substr($js, 0,4) != 'http') $js = str_replace(array(__ROOT__,'\\'),array('','/'),dirname($bt[0]['file'])).$js;
  176. $GLOBALS['hooks']['js_files'][] = $js;
  177. }
  178. public static function callJs($root = null,$cacheVersion = ''){
  179. if(!isset($GLOBALS['hooks']['js_files'])) return '';
  180. $stream = '';
  181. foreach($GLOBALS['hooks']['js_files'] as $js_file){
  182. $url = $js_file;
  183. if(isset($root) && substr($js_file,0, 4) != 'http') $url = $root.'/'.$url;
  184. $stream .='<script type="text/javascript" src="'.$url.'?v='.$cacheVersion.'"></script>'."\n";
  185. }
  186. return $stream;
  187. }
  188. /*
  189. * Aide à l'inclusion de classes de plugin, ex:
  190. * require_once('..'.SLASH.'plugin'.SLASH.'issue'.SLASH.'Event.class.php');
  191. * devient :
  192. * Plugin::need('issue/Event');
  193. * Il est possible de faires des inclusions mulitples, ex :
  194. * Plugin::need('issue/Event, client/Client, client/ClientContact');
  195. */
  196. public static function need($selectors){
  197. foreach(explode(',',$selectors) as $selector){
  198. list($plugin,$class) = explode('/',trim($selector));
  199. require_once(__ROOT__.PLUGIN_PATH.$plugin.SLASH.$class.'.class.php');
  200. }
  201. }
  202. }