PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/pluginhost.php

https://github.com/krihal/Tiny-Tiny-RSS
PHP | 304 lines | 71 code | 14 blank | 219 comment | 6 complexity | 323c1a53aab64217b7509208e1699ae6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-3.0, GPL-2.0
  1. <?php
  2. class PluginHost {
  3. private $link;
  4. private $hooks = array();
  5. private $plugins = array();
  6. private $handlers = array();
  7. private $commands = array();
  8. private $storage = array();
  9. private $owner_uid;
  10. private $debug;
  11. const HOOK_ARTICLE_BUTTON = 1;
  12. const HOOK_ARTICLE_FILTER = 2;
  13. const HOOK_PREFS_TAB = 3;
  14. const HOOK_PREFS_TAB_SECTION = 4;
  15. const HOOK_PREFS_TABS = 5;
  16. const HOOK_FEED_PARSED = 6;
  17. const HOOK_UPDATE_TASK = 7;
  18. const HOOK_AUTH_USER = 8;
  19. const HOOK_HOTKEY_MAP = 9;
  20. const HOOK_RENDER_ARTICLE = 10;
  21. const HOOK_RENDER_ARTICLE_CDM = 11;
  22. const HOOK_FEED_FETCHED = 12;
  23. const HOOK_SANITIZE = 13;
  24. const KIND_ALL = 1;
  25. const KIND_SYSTEM = 2;
  26. const KIND_USER = 3;
  27. function __construct($link) {
  28. $this->link = $link;
  29. $this->storage = $_SESSION["plugin_storage"];
  30. if (!$this->storage) $this->storage = array();
  31. }
  32. private function register_plugin($name, $plugin) {
  33. //array_push($this->plugins, $plugin);
  34. $this->plugins[$name] = $plugin;
  35. }
  36. function get_link() {
  37. return $this->link;
  38. }
  39. function get_plugins() {
  40. return $this->plugins;
  41. }
  42. function get_plugin($name) {
  43. return $this->plugins[$name];
  44. }
  45. function run_hooks($type, $method, $args) {
  46. foreach ($this->get_hooks($type) as $hook) {
  47. $hook->$method($args);
  48. }
  49. }
  50. function add_hook($type, $sender) {
  51. if (!is_array($this->hooks[$type])) {
  52. $this->hooks[$type] = array();
  53. }
  54. array_push($this->hooks[$type], $sender);
  55. }
  56. function del_hook($type, $sender) {
  57. if (is_array($this->hooks[$type])) {
  58. $key = array_Search($this->hooks[$type], $sender);
  59. if ($key !== FALSE) {
  60. unset($this->hooks[$type][$key]);
  61. }
  62. }
  63. }
  64. function get_hooks($type) {
  65. if (isset($this->hooks[$type])) {
  66. return $this->hooks[$type];
  67. } else {
  68. return array();
  69. }
  70. }
  71. function load_all($kind, $owner_uid = false) {
  72. $plugins = array_map("basename", glob("plugins/*"));
  73. $this->load(join(",", $plugins), $kind, $owner_uid);
  74. }
  75. function load($classlist, $kind, $owner_uid = false) {
  76. $plugins = explode(",", $classlist);
  77. $this->owner_uid = (int) $owner_uid;
  78. foreach ($plugins as $class) {
  79. $class = trim($class);
  80. $class_file = strtolower(basename($class));
  81. $file = dirname(__FILE__)."/../plugins/$class_file/init.php";
  82. if (!isset($this->plugins[$class])) {
  83. if (file_exists($file)) require_once $file;
  84. if (class_exists($class) && is_subclass_of($class, "Plugin")) {
  85. $plugin = new $class($this);
  86. switch ($kind) {
  87. case $this::KIND_SYSTEM:
  88. if ($this->is_system($plugin)) {
  89. $plugin->init($this);
  90. $this->register_plugin($class, $plugin);
  91. }
  92. break;
  93. case $this::KIND_USER:
  94. if (!$this->is_system($plugin)) {
  95. $plugin->init($this);
  96. $this->register_plugin($class, $plugin);
  97. }
  98. break;
  99. case $this::KIND_ALL:
  100. $plugin->init($this);
  101. $this->register_plugin($class, $plugin);
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. function is_system($plugin) {
  109. $about = $plugin->about();
  110. return @$about[3];
  111. }
  112. // only system plugins are allowed to modify routing
  113. function add_handler($handler, $method, $sender) {
  114. $handler = str_replace("-", "_", strtolower($handler));
  115. $method = strtolower($method);
  116. if ($this->is_system($sender)) {
  117. if (!is_array($this->handlers[$handler])) {
  118. $this->handlers[$handler] = array();
  119. }
  120. $this->handlers[$handler][$method] = $sender;
  121. }
  122. }
  123. function del_handler($handler, $method) {
  124. $handler = str_replace("-", "_", strtolower($handler));
  125. $method = strtolower($method);
  126. if ($this->is_system($sender)) {
  127. unset($this->handlers[$handler][$method]);
  128. }
  129. }
  130. function lookup_handler($handler, $method) {
  131. $handler = str_replace("-", "_", strtolower($handler));
  132. $method = strtolower($method);
  133. if (is_array($this->handlers[$handler])) {
  134. if (isset($this->handlers[$handler]["*"])) {
  135. return $this->handlers[$handler]["*"];
  136. } else {
  137. return $this->handlers[$handler][$method];
  138. }
  139. }
  140. return false;
  141. }
  142. function add_command($command, $description, $sender) {
  143. $command = "-" . str_replace("-", "_", strtolower($command));
  144. $this->commands[$command] = array("description" => $description,
  145. "class" => $sender);
  146. }
  147. function del_command($command) {
  148. $command = "-" . strtolower($command);
  149. unset($this->commands[$command]);
  150. }
  151. function lookup_command($command) {
  152. $command = "-" . strtolower($command);
  153. if (is_array($this->commands[$command])) {
  154. return $this->commands[$command]["class"];
  155. } else {
  156. return false;
  157. }
  158. return false;
  159. }
  160. function get_commands() {
  161. return $this->commands;
  162. }
  163. function run_commands($args) {
  164. foreach ($this->get_commands() as $command => $data) {
  165. if (in_array($command, $args)) {
  166. $command = str_replace("-", "", $command);
  167. $data["class"]->$command($args);
  168. }
  169. }
  170. }
  171. function load_data($force = false) {
  172. if ($this->owner_uid && (!$_SESSION["plugin_storage"] || $force)) {
  173. $plugin = db_escape_string($plugin);
  174. $result = db_query($this->link, "SELECT name, content FROM ttrss_plugin_storage
  175. WHERE owner_uid = '".$this->owner_uid."'");
  176. while ($line = db_fetch_assoc($result)) {
  177. $this->storage[$line["name"]] = unserialize($line["content"]);
  178. }
  179. $_SESSION["plugin_storage"] = $this->storage;
  180. }
  181. }
  182. private function save_data($plugin) {
  183. if ($this->owner_uid) {
  184. $plugin = db_escape_string($plugin);
  185. db_query($this->link, "BEGIN");
  186. $result = db_query($this->link,"SELECT id FROM ttrss_plugin_storage WHERE
  187. owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
  188. if (!isset($this->storage[$plugin]))
  189. $this->storage[$plugin] = array();
  190. $content = db_escape_string(serialize($this->storage[$plugin]));
  191. if (db_num_rows($result) != 0) {
  192. db_query($this->link, "UPDATE ttrss_plugin_storage SET content = '$content'
  193. WHERE owner_uid= '".$this->owner_uid."' AND name = '$plugin'");
  194. } else {
  195. db_query($this->link, "INSERT INTO ttrss_plugin_storage
  196. (name,owner_uid,content) VALUES
  197. ('$plugin','".$this->owner_uid."','$content')");
  198. }
  199. db_query($this->link, "COMMIT");
  200. }
  201. }
  202. function set($sender, $name, $value, $sync = true) {
  203. $idx = get_class($sender);
  204. if (!isset($this->storage[$idx]))
  205. $this->storage[$idx] = array();
  206. $this->storage[$idx][$name] = $value;
  207. $_SESSION["plugin_storage"] = $this->storage;
  208. if ($sync) $this->save_data(get_class($sender));
  209. }
  210. function get($sender, $name, $default_value = false) {
  211. $idx = get_class($sender);
  212. if (isset($this->storage[$idx][$name])) {
  213. return $this->storage[$idx][$name];
  214. } else {
  215. return $default_value;
  216. }
  217. }
  218. function get_all($sender) {
  219. $idx = get_class($sender);
  220. return $this->storage[$idx];
  221. }
  222. function clear_data($sender) {
  223. if ($this->owner_uid) {
  224. $idx = get_class($sender);
  225. unset($this->storage[$idx]);
  226. db_query($this->link, "DELETE FROM ttrss_plugin_storage WHERE name = '$idx'
  227. AND owner_uid = " . $this->owner_uid);
  228. $_SESSION["plugin_storage"] = $this->storage;
  229. }
  230. }
  231. function set_debug($debug) {
  232. $this->debug = $debug;
  233. }
  234. function get_debug() {
  235. return $this->debug;
  236. }
  237. }
  238. ?>