Plugin.class.php 5.8 KB

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