pluginhost.php 9.0 KB

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