pluginhost.php 8.2 KB

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