pluginhost.php 10 KB

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