PageRenderTime 26ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/include/class.plugin.php

https://bitbucket.org/icarito/pmc
PHP | 254 lines | 182 code | 30 blank | 42 comment | 13 complexity | c900d677f73a1b7c11d3cfd4a53d5ab0 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. class plugin
  3. {
  4. private $pluginFuncs;
  5. /**
  6. * Constructor
  7. *
  8. * @access protected
  9. */
  10. function __construct()
  11. {
  12. $this->pluginFuncs = array();
  13. }
  14. /**
  15. * Install a plugin to the plugin directory
  16. * This just installs the plugin to the plugin directory, so Collabtive can find it.
  17. *
  18. * @param string $ name Name of the plugin
  19. * @param string $ description Description of what it does
  20. * @return int ID of the installed plugin
  21. */
  22. public function installPlugin($name, $description)
  23. {
  24. $name = mysql_real_escape_string($name);
  25. $description = mysql_real_escape_string($description);
  26. $ins = mysql_query("INSERT INTO plugins (name,description) VALUES ('$name','$description')");
  27. if ($ins)
  28. {
  29. $insid = mysql_insert_id();
  30. return $insid;
  31. }
  32. else
  33. {
  34. return false;
  35. }
  36. }
  37. public function installPluginFromAppStore()
  38. {
  39. }
  40. public function listAppStorePlugins()
  41. {
  42. }
  43. /**
  44. * Remove a plugin and all of its events
  45. *
  46. * @param int $id ID of the plugin
  47. * @return bool
  48. */
  49. public function uninstallPlugin($plugin)
  50. {
  51. $plugin = (int) $plugin;
  52. $del = mysql_query("DELETE FROM plugins WHERE ID = $plugin");
  53. $this->delAllPluginEvents($plugin);
  54. if ($del1 and $del2)
  55. {
  56. return true;
  57. }
  58. else
  59. {
  60. return false;
  61. }
  62. }
  63. /**
  64. * Activate a plugin
  65. *
  66. * @param int $plugin ID of the plugin
  67. * @return bool
  68. */
  69. public function activatePlugin($plugin)
  70. {
  71. $plugin = (int) $plugin;
  72. $upd = mysql_query("UPDATE plugins SET state = 1 WHERE ID = $id");
  73. }
  74. /**
  75. * Deactivate a plugin
  76. *
  77. * @param int $plugin ID of the plugin
  78. * @return bool
  79. */
  80. public function deactivatePlugin($plugin)
  81. {
  82. $plugin = (int) $plugin;
  83. $upd = mysql_query("UPDATE plugins SET state = 0 WHERE ID = $id");
  84. }
  85. /**
  86. * Check if a plugin is installed to the directory
  87. *
  88. * @param string $pluginname Name of the plugin
  89. * @return bool
  90. */
  91. public function isInstalledPlugin($pluginname)
  92. {
  93. $pluginname = mysql_real_escape_string($pluginname);
  94. $sel = mysql_query("SELECT ID FROM plugins WHERE name = '$pluginname'");
  95. $chk = mysql_fetch_row($sel);
  96. $chk = $chk[0];
  97. if ($chk > 0)
  98. {
  99. return true;
  100. }
  101. else
  102. {
  103. return false;
  104. }
  105. }
  106. /**
  107. * Scan the plugindir. Install not installed plugins to the directory and add its events.
  108. *
  109. * @return void $plugins
  110. */
  111. public function scanPlugindir()
  112. {
  113. $dir = scandir(CL_ROOT . "/plugins/");
  114. $plugins = array();
  115. foreach($dir as $folder)
  116. {
  117. if ($folder != "." and $folder != "..")
  118. {
  119. if (!$this->isInstalledPlugin($folder))
  120. {
  121. $file = simplexml_load_file(CL_ROOT . "/plugins/" . $folder . "/config.xml");
  122. $insid = $this->installPlugin($file->name, $file->description);
  123. if ($insid)
  124. {
  125. foreach($file->calls as $call)
  126. {
  127. $this->addPluginEvent($insid, $call->call->signal , $call->call->action , $call->call->name , "");
  128. }
  129. }
  130. }
  131. }
  132. }
  133. return true;
  134. }
  135. public function addPluginEvent($plugin, $signal, $action, $name, $params)
  136. {
  137. $plugin = (int) $plugin;
  138. $signal = mysql_real_escape_string($signal);
  139. $action = mysql_real_escape_string($action);
  140. $name = mysql_real_escape_string($name);
  141. $params = serialize($params);
  142. return $ins = mysql_query("INSERT INTO pluginevents (pid,signal,action,name,params) VALUE ($plugin,'$signal','$action','$name','$params')");
  143. }
  144. public function delPluginEvent($event)
  145. {
  146. $event = (int) $event;
  147. return $del = mysql_query("DELETE FROM pluginevents WHERE ID = $event");
  148. }
  149. public function delAllPluginEvents($plugin)
  150. {
  151. $plugin = (int) $plugin;
  152. $del = mysql_query("DELETE FROM pluginevents WHERE pid = $plugin");
  153. }
  154. public function getPluginEvents($plugin)
  155. {
  156. $plugin = (int) $plugin;
  157. $sel = mysql_query("SELECT * FROM pluginevents WHERE pid = $plugin");
  158. while ($plugins = mysql_fetch_array($sel, MYSQL_ASSOC))
  159. {
  160. $this->registerPluginEvent($plugins["signal"], $plugins["action"] , $plugins["name"] , unserialize($plugins["params"]));
  161. }
  162. return $this->getFunclist();
  163. }
  164. public function getPlugin($id)
  165. {
  166. $id = (int) $id;
  167. $sel = mysql_query("SELECT * FROM plugins WHERE ID = $id");
  168. $plugin = mysql_fetch_array($sel, MYSQL_ASSOC);
  169. $plugin["functions"] = $this->getPluginEvents($plugin["ID"]);
  170. return $plugin;
  171. }
  172. public function getPlugins($state = 1)
  173. {
  174. $state = (int) $state;
  175. $sel = mysql_query("SELECT ID FROM plugins WHERE state = $state");
  176. $plugins = array();
  177. while ($plugin = mysql_fetch_array($sel, MYSQL_ASSOC))
  178. {
  179. array_push($plugins, $this->getPlugin($plugin["ID"]));
  180. }
  181. return $plugins;
  182. }
  183. public function getFunclist()
  184. {
  185. return $this->pluginFuncs;
  186. }
  187. public function addHookData(Smarty $template, $hook, array $data)
  188. {
  189. $template->append($hook, $data);
  190. }
  191. public function callSignalFuncs($signal, $action = "", $objid = 0)
  192. {
  193. if (!$action)
  194. {
  195. $thefunctions = $this->pluginFuncs[$signal];
  196. }
  197. else
  198. {
  199. $thefunctions = $this->pluginFuncs[$signal][$action];
  200. }
  201. if (!empty($thefunctions))
  202. {
  203. foreach($thefunctions as $thefunction)
  204. {
  205. $classname = explode("::", $thefunction["name"]);
  206. $classname = $classname[0];
  207. if (!class_exists($classname, false))
  208. {
  209. include(CL_ROOT . "/plugins/" . $classname . "/class.$classname.php");
  210. }
  211. $thefunction["params"] = array();
  212. array_push($thefunction["params"], $objid);
  213. call_user_func_array($thefunction["name"], $thefunction["params"]);
  214. }
  215. }
  216. }
  217. private function registerPluginEvent($signal, $action, $name, $params)
  218. {
  219. $this->pluginFuncs[$signal][$action][] = array("name" => $name, "params" => $params);
  220. }
  221. private function unregisterPluginEvent()
  222. {
  223. }
  224. }