]> git.donarmstrong.com Git - roundcube.git/blob - program/include/rcube_plugin.php
Imported Upstream version 0.3
[roundcube.git] / program / include / rcube_plugin.php
1 <?php
2
3 /*
4  +-----------------------------------------------------------------------+
5  | program/include/rcube_plugin.php                                      |
6  |                                                                       |
7  | This file is part of the RoundCube Webmail client                     |
8  | Copyright (C) 2008-2009, RoundCube Dev. - Switzerland                 |
9  | Licensed under the GNU GPL                                            |
10  |                                                                       |
11  | PURPOSE:                                                              |
12  |  Abstract plugins interface/class                                     |
13  |  All plugins need to extend this class                                |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  +-----------------------------------------------------------------------+
17
18  $Id: $
19
20 */
21
22 /**
23  * Plugin interface class
24  *
25  * @package Core
26  */
27 abstract class rcube_plugin
28 {
29   public $ID;
30   public $api;
31   public $task;
32   protected $home;
33   protected $urlbase;
34
35   /**
36    * Default constructor.
37    */
38   public function __construct($api)
39   {
40     $this->ID = get_class($this);
41     $this->api = $api;
42     $this->home = $api->dir . DIRECTORY_SEPARATOR . $this->ID;
43     $this->urlbase = $api->url . $this->ID . '/';
44   }
45   
46   /**
47    * Initialization method, needs to be implemented by the plugin itself
48    */
49   abstract function init();
50   
51   /**
52    * Load local config file from plugins directory.
53    * The loaded values are patched over the global configuration.
54    *
55    * @param string Config file name relative to the plugin's folder
56    * @return boolean True on success, false on failure
57    */
58   public function load_config($fname = 'config.inc.php')
59   {
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);
64       return false;
65     }
66     
67     return true;
68   }
69
70   /**
71    * Register a callback function for a specific (server-side) hook
72    *
73    * @param string Hook name
74    * @param mixed Callback function as string or array with object reference and method name
75    */
76   public function add_hook($hook, $callback)
77   {
78     $this->api->register_hook($hook, $callback);
79   }
80   
81   /**
82    * Load localized texts from the plugins dir
83    *
84    * @param string Directory to search in
85    * @param mixed Make texts also available on the client (array with list or true for all)
86    */
87   public function add_texts($dir, $add2client = false)
88   {
89     $domain = $this->ID;
90     
91     $lang = $_SESSION['language'];
92     $locdir = slashify(realpath(slashify($this->home) . $dir));
93     $texts = array();
94     
95     foreach (array('en_US', $lang) as $lng) {
96       @include($locdir . $lng . '.inc');
97       $texts = (array)$labels + (array)$messages + (array)$texts;
98     }
99
100     // prepend domain to text keys and add to the application texts repository
101     if (!empty($texts)) {
102       $add = array();
103       foreach ($texts as $key => $value)
104         $add[$domain.'.'.$key] = $value;
105
106       $rcmail = rcmail::get_instance();
107       $rcmail->load_language($lang, $add);
108       
109       // add labels to client
110       if ($add2client) {
111         $js_labels = is_array($add2client) ? array_map(array($this, 'label_map_callback'), $add2client) : array_keys($add);
112         $rcmail->output->add_label($js_labels);
113       }
114     }
115   }
116   
117   /**
118    * Wrapper for rcmail::gettext() adding the plugin ID as domain
119    *
120    * @return string Localized text
121    * @see rcmail::gettext()
122    */
123   public function gettext($p)
124   {
125     return rcmail::get_instance()->gettext($p, $this->ID);
126   }
127
128   /**
129    * Register this plugin to be responsible for a specific task
130    *
131    * @param string Task name (only characters [a-z0-9_.-] are allowed)
132    */
133   public function register_task($task)
134   {
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);
137     }
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);
140     }
141     else {
142       rcmail::$main_tasks[] = $task;
143     }
144   }
145
146   /**
147     * Register a handler for a specific client-request action
148     *
149     * The callback will be executed upon a request like /?_task=mail&_action=plugin.myaction
150     *
151     * @param string Action name (should be unique)
152     * @param mixed Callback function as string or array with object reference and method name
153    */
154   public function register_action($action, $callback)
155   {
156     $this->api->register_action($action, $this->ID, $callback);
157   }
158
159   /**
160    * Register a handler function for a template object
161    *
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.
164    *
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
167    */
168   public function register_handler($name, $callback)
169   {
170     $this->api->register_handler($name, $this->ID, $callback);
171   }
172
173   /**
174    * Make this javascipt file available on the client
175    *
176    * @param string File path; absolute or relative to the plugin directory
177    */
178   public function include_script($fn)
179   {
180     $this->api->include_script($this->resource_url($fn));
181   }
182
183   /**
184    * Make this stylesheet available on the client
185    *
186    * @param string File path; absolute or relative to the plugin directory
187    */
188   public function include_stylesheet($fn)
189   {
190     $this->api->include_stylesheet($this->resource_url($fn));
191   }
192   
193   /**
194    * Append a button to a certain container
195    *
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()
199    */
200   public function add_button($p, $container)
201   {
202     if ($this->api->output->type == 'html') {
203       // fix relative paths
204       foreach (array('imagepas', 'imageact', 'imagesel') as $key)
205         if ($p[$key])
206           $p[$key] = $this->api->url . $this->resource_url($p[$key]);
207       
208       $this->api->add_content($this->api->output->button($p), $container);
209     }
210   }
211   
212   /**
213    * Generate an absolute URL to the given resource within the current
214    * plugin directory
215    *
216    * @param string The file name
217    * @return string Absolute URL to the given resource
218    */
219   public function url($fn)
220   {
221       return $this->api->url . $this->resource_url($fn);
222   }
223
224   /**
225    * Make the given file name link into the plugin directory
226    */
227   private function resource_url($fn)
228   {
229     if ($fn[0] != '/' && !preg_match('|^https?://|i', $fn))
230       return $this->ID . '/' . $fn;
231     else
232       return $fn;
233   }
234
235   /**
236    * Callback function for array_map
237    */
238   private function label_map_callback($key)
239   {
240     return $this->ID.'.'.$key;
241   }
242
243
244 }
245