/include/class.plugin.php

https://github.com/temperatio/collabtive · PHP · 260 lines · 179 code · 34 blank · 47 comment · 12 complexity · 91fda8c0351b171c92827ca4fd13681b MD5 · raw file

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