pluginhost.php 9.2 KB

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