4 +-----------------------------------------------------------------------+
5 | program/include/rcube_plugin.php |
7 | This file is part of the RoundCube Webmail client |
8 | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland |
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 +-----------------------------------------------------------------------+
23 * Plugin interface class
27 abstract class rcube_plugin
36 * Default constructor.
38 public function __construct($api)
40 $this->ID = get_class($this);
42 $this->home = $api->dir . DIRECTORY_SEPARATOR . $this->ID;
43 $this->urlbase = $api->url . $this->ID . '/';
47 * Initialization method, needs to be implemented by the plugin itself
49 abstract function init();
52 * Load local config file from plugins directory.
53 * The loaded values are patched over the global configuration.
55 * @param string Config file name relative to the plugin's folder
56 * @return boolean True on success, false on failure
58 public function load_config($fname = 'config.inc.php')
60 $fpath = $this->home.'/'.$fname;
61 $rcmail = rcmail::get_instance();
62 if (!$rcmail->config->load_from_file($fpath)) {
63 raise_error(array('code' => 527, 'type' => 'php', 'message' => "Failed to load config from $fpath"), true, false);
71 * Register a callback function for a specific (server-side) hook
73 * @param string Hook name
74 * @param mixed Callback function as string or array with object reference and method name
76 public function add_hook($hook, $callback)
78 $this->api->register_hook($hook, $callback);
82 * Load localized texts from the plugins dir
84 * @param string Directory to search in
85 * @param mixed Make texts also available on the client (array with list or true for all)
87 public function add_texts($dir, $add2client = false)
91 $lang = $_SESSION['language'];
92 $locdir = slashify(realpath(slashify($this->home) . $dir));
95 foreach (array('en_US', $lang) as $lng) {
96 @include($locdir . $lng . '.inc');
97 $texts = (array)$labels + (array)$messages + (array)$texts;
100 // prepend domain to text keys and add to the application texts repository
101 if (!empty($texts)) {
103 foreach ($texts as $key => $value)
104 $add[$domain.'.'.$key] = $value;
106 $rcmail = rcmail::get_instance();
107 $rcmail->load_language($lang, $add);
109 // add labels to client
111 $js_labels = is_array($add2client) ? array_map(array($this, 'label_map_callback'), $add2client) : array_keys($add);
112 $rcmail->output->add_label($js_labels);
118 * Wrapper for rcmail::gettext() adding the plugin ID as domain
120 * @return string Localized text
121 * @see rcmail::gettext()
123 public function gettext($p)
125 return rcmail::get_instance()->gettext($p, $this->ID);
129 * Register this plugin to be responsible for a specific task
131 * @param string Task name (only characters [a-z0-9_.-] are allowed)
133 public function register_task($task)
135 if ($task != asciiwords($task)) {
136 raise_error(array('code' => 526, 'type' => 'php', 'message' => "Invalid task name: $task. Only characters [a-z0-9_.-] are allowed"), true, false);
138 else if (in_array(rcmail::$main_tasks, $task)) {
139 raise_error(array('code' => 526, 'type' => 'php', 'message' => "Cannot register taks $task; already taken by another plugin or the application itself"), true, false);
142 rcmail::$main_tasks[] = $task;
147 * Register a handler for a specific client-request action
149 * The callback will be executed upon a request like /?_task=mail&_action=plugin.myaction
151 * @param string Action name (should be unique)
152 * @param mixed Callback function as string or array with object reference and method name
154 public function register_action($action, $callback)
156 $this->api->register_action($action, $this->ID, $callback);
160 * Register a handler function for a template object
162 * When parsing a template for display, tags like <roundcube:object name="plugin.myobject" />
163 * will be replaced by the return value if the registered callback function.
165 * @param string Object name (should be unique and start with 'plugin.')
166 * @param mixed Callback function as string or array with object reference and method name
168 public function register_handler($name, $callback)
170 $this->api->register_handler($name, $this->ID, $callback);
174 * Make this javascipt file available on the client
176 * @param string File path; absolute or relative to the plugin directory
178 public function include_script($fn)
180 $this->api->include_script($this->resource_url($fn));
184 * Make this stylesheet available on the client
186 * @param string File path; absolute or relative to the plugin directory
188 public function include_stylesheet($fn)
190 $this->api->include_stylesheet($this->resource_url($fn));
194 * Append a button to a certain container
196 * @param array Hash array with named parameters (as used in skin templates)
197 * @param string Container name where the buttons should be added to
198 * @see rcube_remplate::button()
200 public function add_button($p, $container)
202 if ($this->api->output->type == 'html') {
203 // fix relative paths
204 foreach (array('imagepas', 'imageact', 'imagesel') as $key)
206 $p[$key] = $this->api->url . $this->resource_url($p[$key]);
208 $this->api->add_content($this->api->output->button($p), $container);
213 * Generate an absolute URL to the given resource within the current
216 * @param string The file name
217 * @return string Absolute URL to the given resource
219 public function url($fn)
221 return $this->api->url . $this->resource_url($fn);
225 * Make the given file name link into the plugin directory
227 private function resource_url($fn)
229 if ($fn[0] != '/' && !preg_match('|^https?://|i', $fn))
230 return $this->ID . '/' . $fn;
236 * Callback function for array_map
238 private function label_map_callback($key)
240 return $this->ID.'.'.$key;