pluginhost.php 10 KB

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