Plugin.class.php 11 KB

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