pluginhost.php 9.1 KB

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