PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/pluginhost.php

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