pluginhost.php 9.6 KB

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