]> git.wh0rd.org - tt-rss.git/blame - classes/pluginhost.php
update_rss_feed: escape error string immediately
[tt-rss.git] / classes / pluginhost.php
CommitLineData
19c73507
AD
1<?php
2class PluginHost {
3 private $link;
4 private $hooks = array();
5 private $plugins = array();
8dcb2b47 6 private $handlers = array();
73f28fe9 7 private $commands = array();
19c73507
AD
8
9 const HOOK_ARTICLE_BUTTON = 1;
10 const HOOK_ARTICLE_FILTER = 2;
6065f3ad
AD
11 const HOOK_PREFS_TAB = 3;
12 const HOOK_PREFS_SECTION = 4;
6cbe53c9 13 const HOOK_PREFS_TABS = 5;
4412b877 14 const HOOK_FEED_PARSED = 6;
19c73507
AD
15
16 function __construct($link) {
17 $this->link = $link;
18 }
19
20 private function register_plugin($name, $plugin) {
21 //array_push($this->plugins, $plugin);
22 $this->plugins[$name] = $plugin;
23 }
24
25 function get_link() {
26 return $this->link;
27 }
28
29 function get_plugins() {
30 return $this->plugins;
31 }
32
33 function get_plugin($name) {
34 return $this->plugins[$name];
35 }
36
6065f3ad
AD
37 function run_hooks($type, $method, $args) {
38 foreach ($this->get_hooks($type) as $hook) {
39 $hook->$method($args);
40 }
41 }
42
19c73507
AD
43 function add_hook($type, $sender) {
44 if (!is_array($this->hooks[$type])) {
45 $this->hooks[$type] = array();
46 }
47
48 array_push($this->hooks[$type], $sender);
49 }
50
51 function del_hook($type, $sender) {
52 if (is_array($this->hooks[$type])) {
53 $key = array_Search($this->hooks[$type], $sender);
54 if ($key !== FALSE) {
55 unset($this->hooks[$type][$key]);
56 }
57 }
58 }
59
60 function get_hooks($type) {
19b3992b
AD
61 if (isset($this->hooks[$type])) {
62 return $this->hooks[$type];
63 } else {
64 return array();
65 }
19c73507
AD
66 }
67
68 function load($classlist) {
69 $plugins = explode(",", $classlist);
70
71 foreach ($plugins as $class) {
72 $class = trim($class);
8dcb2b47 73 $class_file = strtolower(basename($class));
19c73507
AD
74 $file = dirname(__FILE__)."/../plugins/$class_file/$class_file.php";
75
76 if (file_exists($file)) require_once $file;
77
5a0e0392 78 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
19c73507
AD
79 $plugin = new $class($this);
80
81 $this->register_plugin($class, $plugin);
82 }
83 }
84 }
85
8dcb2b47 86 function add_handler($handler, $method, $sender) {
6cbe53c9 87 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
88 $method = strtolower($method);
89
90 if (!is_array($this->handlers[$handler])) {
91 $this->handlers[$handler] = array();
92 }
93
94 $this->handlers[$handler][$method] = $sender;
95 }
96
97 function del_handler($handler, $method) {
6cbe53c9 98 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
99 $method = strtolower($method);
100
101 unset($this->handlers[$handler][$method]);
102 }
103
104 function lookup_handler($handler, $method) {
6cbe53c9 105 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
106 $method = strtolower($method);
107
108 if (is_array($this->handlers[$handler])) {
6cbe53c9
AD
109 if (isset($this->handlers[$handler]["*"])) {
110 return $this->handlers[$handler]["*"];
111 } else {
112 return $this->handlers[$handler][$method];
113 }
8dcb2b47
AD
114 }
115
116 return false;
117 }
73f28fe9
AD
118
119 function add_command($command, $description, $sender) {
120 $command = "-" . str_replace("-", "_", strtolower($command));
121
122 $this->commands[$command] = array("description" => $description,
123 "class" => $sender);
124 }
125
126 function del_command($command) {
127 $command = "-" . strtolower($command);
128
129 unset($this->commands[$command]);
130 }
131
132 function lookup_command($command) {
133 $command = "-" . strtolower($command);
134
135 if (is_array($this->commands[$command])) {
136 return $this->commands[$command]["class"];
137 } else {
138 return false;
139 }
140
141 return false;
142 }
143
144 function get_commands() {
145 return $this->commands;
146 }
147
148 function run_commands($args) {
149 foreach ($this->get_commands() as $command => $data) {
150 if (in_array($command, $args)) {
151 $command = str_replace("-", "", $command);
152 $data["class"]->$command($args);
153 }
154 }
155 }
156
19c73507
AD
157}
158?>