pluginhost.php 11 KB

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