Plugin.class.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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;
  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. foreach(self::getAll() as $plugin):
  28. if(!$plugin->state) continue;
  29. $main = $plugin->path().SLASH.$plugin->folder.'.plugin.php';
  30. if(file_exists($main))
  31. require_once($main);
  32. endforeach;
  33. }
  34. public static function getAll(){
  35. $enabled = self::states();
  36. $plugins = array();
  37. foreach(glob(__ROOT__.PLUGIN_PATH.'*'.SLASH.'app.json') as $file)
  38. $plugins[] = self::parseManifest($file);
  39. usort($plugins, function($a, $b){
  40. if ($a->name == $b->name)
  41. $result = 0;
  42. if($a->name < $b->name){
  43. $result = -1;
  44. } else{
  45. $result = 1;
  46. }
  47. return $result;
  48. });
  49. return $plugins;
  50. }
  51. public static function getById($id){
  52. $plugin = false;
  53. foreach(self::getAll() as $onePlugin)
  54. if($onePlugin->id==$id) $plugin = $onePlugin;
  55. return $plugin;
  56. }
  57. public static function parseManifest($file){
  58. $enabled = self::states();
  59. $plugin = new self();
  60. $manifest = json_decode(file_get_contents($file),true);
  61. if(!$manifest) return $plugin;
  62. if(!isset($manifest['id']) || !isset($manifest['name']) ) continue;
  63. $plugin->name = $manifest['name'];
  64. $plugin->id = $manifest['id'];
  65. $plugin->folder = basename(dirname($file));
  66. if(in_array($plugin->id,$enabled)) $plugin->state = true;
  67. if(isset($manifest['author'])) $plugin->author = $manifest['author'];
  68. if(isset($manifest['url'])) $plugin->url = $manifest['url'];
  69. if(isset($manifest['licence'])) $plugin->licence = $manifest['licence'];
  70. if(isset($manifest['description'])) $plugin->description = $manifest['description'];
  71. if(isset($manifest['version'])) $plugin->version = $manifest['version'];
  72. if(isset($manifest['core'])) $plugin->core = $manifest['core'];
  73. if(isset($manifest['require'])) $plugin->require = $manifest['require'];
  74. return $plugin;
  75. }
  76. public static function state($id,$state){
  77. $enabled = self::states();
  78. $plugin = self::getById($id);
  79. $main = $plugin->path().SLASH.$plugin->folder.'.plugin.php';
  80. if(file_exists($main))
  81. require_once($main);
  82. if($state==0){
  83. $key = array_search($plugin->id, $enabled);
  84. if($key !== false)
  85. unset($enabled[$key]);
  86. plugin::callHook('uninstall',array($plugin->id));
  87. }else{
  88. if(!in_array($plugin->id,$enabled))
  89. $enabled[] = $plugin->id;
  90. plugin::callHook('install',array($plugin->id));
  91. }
  92. self::states($enabled);
  93. }
  94. public static function states($states = false){
  95. $enabledFile = __ROOT__.PLUGIN_PATH.'enabled.json';
  96. if(!file_exists($enabledFile)) touch($enabledFile);
  97. if(!is_array($states)){
  98. $enabled = json_decode(file_get_contents($enabledFile),true);
  99. return !is_array($enabled)?array():$enabled;
  100. }
  101. file_put_contents($enabledFile,json_encode($states));
  102. }
  103. public function path(){
  104. return __ROOT__.PLUGIN_PATH.$this->folder;
  105. }
  106. public static function url(){
  107. $bt = debug_backtrace();
  108. return ROOT_URL.SLASH.PLUGIN_PATH.basename(dirname($bt[0]['file']));
  109. }
  110. public static function addCss($css) {
  111. $bt = debug_backtrace();
  112. $GLOBALS['hooks']['css_files'][] = str_replace(__ROOT__,'',dirname($bt[0]['file'])).$css;
  113. }
  114. public static function callCss(){
  115. if(!isset($GLOBALS['hooks']['css_files'])) return '';
  116. $stream = '';
  117. foreach($GLOBALS['hooks']['css_files'] as $css_file)
  118. $stream .='<link rel="stylesheet" href="'.$css_file.'">'."\n";
  119. return $stream;
  120. }
  121. public static function addJs($js){
  122. global $_;
  123. $bt = debug_backtrace();
  124. $GLOBALS['hooks']['js_files'][] = str_replace(__ROOT__,'',dirname($bt[0]['file'])).$js;
  125. }
  126. public static function callJs(){
  127. if(!isset($GLOBALS['hooks']['js_files'])) return '';
  128. $stream = '';
  129. foreach($GLOBALS['hooks']['js_files'] as $js_file)
  130. $stream .='<script type="text/javascript" src="'.$js_file.'"></script>'."\n";
  131. return $stream;
  132. }
  133. }
  134. ?>