4 +-----------------------------------------------------------------------+
5 | program/include/rcube_plugin.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2009, The Roundcube Dev Team |
9 | Licensed under the GNU GPL |
12 | Abstract plugins interface/class |
13 | All plugins need to extend this class |
14 +-----------------------------------------------------------------------+
15 | Author: Thomas Bruederli <roundcube@gmail.com> |
16 +-----------------------------------------------------------------------+
18 $Id: rcube_plugin.php 5168 2011-09-05 11:08:48Z alec $
23 * Plugin interface class
27 abstract class rcube_plugin
30 * Class name of the plugin instance
37 * Instance of Plugin API
39 * @var rcube_plugin_api
44 * Regular expression defining task(s) to bind with
51 * Disables plugin in AJAX requests
55 public $noajax = false;
58 * Disables plugin in framed mode
62 public $noframe = false;
70 * Default constructor.
72 * @param rcube_plugin_api $api Plugin API
74 public function __construct($api)
76 $this->ID = get_class($this);
78 $this->home = $api->dir . $this->ID;
79 $this->urlbase = $api->url . $this->ID . '/';
83 * Initialization method, needs to be implemented by the plugin itself
85 abstract function init();
89 * Attempt to load the given plugin which is required for the current plugin
91 * @param string Plugin name
92 * @return boolean True on success, false on failure
94 public function require_plugin($plugin_name)
96 return $this->api->load_plugin($plugin_name);
101 * Load local config file from plugins directory.
102 * The loaded values are patched over the global configuration.
104 * @param string $fname Config file name relative to the plugin's folder
105 * @return boolean True on success, false on failure
107 public function load_config($fname = 'config.inc.php')
109 $fpath = $this->home.'/'.$fname;
110 $rcmail = rcmail::get_instance();
111 if (is_file($fpath) && !$rcmail->config->load_from_file($fpath)) {
112 raise_error(array('code' => 527, 'type' => 'php',
113 'file' => __FILE__, 'line' => __LINE__,
114 'message' => "Failed to load config from $fpath"), true, false);
122 * Register a callback function for a specific (server-side) hook
124 * @param string $hook Hook name
125 * @param mixed $callback Callback function as string or array with object reference and method name
127 public function add_hook($hook, $callback)
129 $this->api->register_hook($hook, $callback);
133 * Load localized texts from the plugins dir
135 * @param string $dir Directory to search in
136 * @param mixed $add2client Make texts also available on the client (array with list or true for all)
138 public function add_texts($dir, $add2client = false)
142 $lang = $_SESSION['language'];
143 $locdir = slashify(realpath(slashify($this->home) . $dir));
146 // use buffering to handle empty lines/spaces after closing PHP tag
149 foreach (array('en_US', $lang) as $lng) {
150 $fpath = $locdir . $lng . '.inc';
151 if (is_file($fpath) && is_readable($fpath)) {
153 $texts = (array)$labels + (array)$messages + (array)$texts;
159 // prepend domain to text keys and add to the application texts repository
160 if (!empty($texts)) {
162 foreach ($texts as $key => $value)
163 $add[$domain.'.'.$key] = $value;
165 $rcmail = rcmail::get_instance();
166 $rcmail->load_language($lang, $add);
168 // add labels to client
170 $js_labels = is_array($add2client) ? array_map(array($this, 'label_map_callback'), $add2client) : array_keys($add);
171 $rcmail->output->add_label($js_labels);
177 * Wrapper for rcmail::gettext() adding the plugin ID as domain
179 * @param string $p Message identifier
180 * @return string Localized text
181 * @see rcmail::gettext()
183 public function gettext($p)
185 return rcmail::get_instance()->gettext($p, $this->ID);
189 * Register this plugin to be responsible for a specific task
191 * @param string $task Task name (only characters [a-z0-9_.-] are allowed)
193 public function register_task($task)
195 if ($this->api->register_task($task, $this->ID))
196 $this->mytask = $task;
200 * Register a handler for a specific client-request action
202 * The callback will be executed upon a request like /?_task=mail&_action=plugin.myaction
204 * @param string $action Action name (should be unique)
205 * @param mixed $callback Callback function as string or array with object reference and method name
207 public function register_action($action, $callback)
209 $this->api->register_action($action, $this->ID, $callback, $this->mytask);
213 * Register a handler function for a template object
215 * When parsing a template for display, tags like <roundcube:object name="plugin.myobject" />
216 * will be replaced by the return value if the registered callback function.
218 * @param string $name Object name (should be unique and start with 'plugin.')
219 * @param mixed $callback Callback function as string or array with object reference and method name
221 public function register_handler($name, $callback)
223 $this->api->register_handler($name, $this->ID, $callback);
227 * Make this javascipt file available on the client
229 * @param string $fn File path; absolute or relative to the plugin directory
231 public function include_script($fn)
233 $this->api->include_script($this->resource_url($fn));
237 * Make this stylesheet available on the client
239 * @param string $fn File path; absolute or relative to the plugin directory
241 public function include_stylesheet($fn)
243 $this->api->include_stylesheet($this->resource_url($fn));
247 * Append a button to a certain container
249 * @param array $p Hash array with named parameters (as used in skin templates)
250 * @param string $container Container name where the buttons should be added to
251 * @see rcube_remplate::button()
253 public function add_button($p, $container)
255 if ($this->api->output->type == 'html') {
256 // fix relative paths
257 foreach (array('imagepas', 'imageact', 'imagesel') as $key)
259 $p[$key] = $this->api->url . $this->resource_url($p[$key]);
261 $this->api->add_content($this->api->output->button($p), $container);
266 * Generate an absolute URL to the given resource within the current
269 * @param string $fn The file name
270 * @return string Absolute URL to the given resource
272 public function url($fn)
274 return $this->api->url . $this->resource_url($fn);
278 * Make the given file name link into the plugin directory
280 * @param string $fn Filename
282 private function resource_url($fn)
284 if ($fn[0] != '/' && !preg_match('|^https?://|i', $fn))
285 return $this->ID . '/' . $fn;
291 * Provide path to the currently selected skin folder within the plugin directory
292 * with a fallback to the default skin folder.
294 * @return string Skin path relative to plugins directory
296 public function local_skin_path()
298 $skin_path = 'skins/'.$this->api->config->get('skin');
299 if (!is_dir(realpath(slashify($this->home) . $skin_path)))
300 $skin_path = 'skins/default';
305 * Callback function for array_map
307 * @param string $key Array key.
310 private function label_map_callback($key)
312 return $this->ID.'.'.$key;