pluginhost.php 8.9 KB

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