pluginhost.php 8.5 KB

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