PageRenderTime 36ms CodeModel.GetById 6ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/pluginhost.php

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