source: subversion/trunk/roundcubemail/program/include/rcube_plugin.php @ 6073

Last change on this file since 6073 was 6073, checked in by alec, 13 months ago
  • Merge devel-framework branch, resolved conflicts
  • Property svn:keywords set to Id
File size: 9.0 KB
Line 
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, The Roundcube Dev Team                       |
9 |                                                                       |
10 | Licensed under the GNU General Public License version 3 or            |
11 | any later version with exceptions for skins & plugins.                |
12 | See the README file for a full license statement.                     |
13 |                                                                       |
14 | PURPOSE:                                                              |
15 |  Abstract plugins interface/class                                     |
16 |  All plugins need to extend this class                                |
17 +-----------------------------------------------------------------------+
18 | Author: Thomas Bruederli <roundcube@gmail.com>                        |
19 +-----------------------------------------------------------------------+
20
21 $Id$
22
23*/
24
25/**
26 * Plugin interface class
27 *
28 * @package PluginAPI
29 */
30abstract class rcube_plugin
31{
32  /**
33   * Class name of the plugin instance
34   *
35   * @var string
36   */
37  public $ID;
38
39  /**
40   * Instance of Plugin API
41   *
42   * @var rcube_plugin_api
43   */
44  public $api;
45
46  /**
47   * Regular expression defining task(s) to bind with
48   *
49   * @var string
50   */
51  public $task;
52
53  /**
54   * Disables plugin in AJAX requests
55   *
56   * @var boolean
57   */
58  public $noajax = false;
59
60  /**
61   * Disables plugin in framed mode
62   *
63   * @var boolean
64   */
65  public $noframe = false;
66
67  protected $home;
68  protected $urlbase;
69  private $mytask;
70
71
72  /**
73   * Default constructor.
74   *
75   * @param rcube_plugin_api $api Plugin API
76   */
77  public function __construct($api)
78  {
79    $this->ID = get_class($this);
80    $this->api = $api;
81    $this->home = $api->dir . $this->ID;
82    $this->urlbase = $api->url . $this->ID . '/';
83  }
84
85  /**
86   * Initialization method, needs to be implemented by the plugin itself
87   */
88  abstract function init();
89
90
91  /**
92   * Attempt to load the given plugin which is required for the current plugin
93   *
94   * @param string Plugin name
95   * @return boolean True on success, false on failure
96   */
97  public function require_plugin($plugin_name)
98  {
99    return $this->api->load_plugin($plugin_name);
100  }
101
102
103  /**
104   * Load local config file from plugins directory.
105   * The loaded values are patched over the global configuration.
106   *
107   * @param string $fname Config file name relative to the plugin's folder
108   * @return boolean True on success, false on failure
109   */
110  public function load_config($fname = 'config.inc.php')
111  {
112    $fpath = $this->home.'/'.$fname;
113    $rcmail = rcube::get_instance();
114    if (is_file($fpath) && !$rcmail->config->load_from_file($fpath)) {
115      rcube::raise_error(array(
116        'code' => 527, 'type' => 'php',
117        'file' => __FILE__, 'line' => __LINE__,
118        'message' => "Failed to load config from $fpath"), true, false);
119      return false;
120    }
121
122    return true;
123  }
124
125  /**
126   * Register a callback function for a specific (server-side) hook
127   *
128   * @param string $hook Hook name
129   * @param mixed  $callback Callback function as string or array with object reference and method name
130   */
131  public function add_hook($hook, $callback)
132  {
133    $this->api->register_hook($hook, $callback);
134  }
135
136  /**
137   * Unregister a callback function for a specific (server-side) hook.
138   *
139   * @param string $hook Hook name
140   * @param mixed  $callback Callback function as string or array with object reference and method name
141   */
142  public function remove_hook($hook, $callback)
143  {
144    $this->api->unregister_hook($hook, $callback);
145  }
146
147  /**
148   * Load localized texts from the plugins dir
149   *
150   * @param string $dir Directory to search in
151   * @param mixed  $add2client Make texts also available on the client (array with list or true for all)
152   */
153  public function add_texts($dir, $add2client = false)
154  {
155    $domain = $this->ID;
156
157    $lang = $_SESSION['language'];
158    $locdir = slashify(realpath(slashify($this->home) . $dir));
159    $texts = array();
160
161    // use buffering to handle empty lines/spaces after closing PHP tag
162    ob_start();
163
164    foreach (array('en_US', $lang) as $lng) {
165      $fpath = $locdir . $lng . '.inc';
166      if (is_file($fpath) && is_readable($fpath)) {
167        include($fpath);
168        $texts = (array)$labels + (array)$messages + (array)$texts;
169      }
170    }
171
172    ob_end_clean();
173
174    // prepend domain to text keys and add to the application texts repository
175    if (!empty($texts)) {
176      $add = array();
177      foreach ($texts as $key => $value)
178        $add[$domain.'.'.$key] = $value;
179
180      $rcmail = rcube::get_instance();
181      $rcmail->load_language($lang, $add);
182
183      // add labels to client
184      if ($add2client) {
185        $js_labels = is_array($add2client) ? array_map(array($this, 'label_map_callback'), $add2client) : array_keys($add);
186        $rcmail->output->add_label($js_labels);
187      }
188    }
189  }
190
191  /**
192   * Wrapper for rcmail::gettext() adding the plugin ID as domain
193   *
194   * @param string $p Message identifier
195   * @return string Localized text
196   * @see rcmail::gettext()
197   */
198  public function gettext($p)
199  {
200    return rcube::get_instance()->gettext($p, $this->ID);
201  }
202
203  /**
204   * Register this plugin to be responsible for a specific task
205   *
206   * @param string $task Task name (only characters [a-z0-9_.-] are allowed)
207   */
208  public function register_task($task)
209  {
210    if ($this->api->register_task($task, $this->ID))
211      $this->mytask = $task;
212  }
213
214  /**
215    * Register a handler for a specific client-request action
216    *
217    * The callback will be executed upon a request like /?_task=mail&_action=plugin.myaction
218    *
219    * @param string $action  Action name (should be unique)
220    * @param mixed $callback Callback function as string or array with object reference and method name
221   */
222  public function register_action($action, $callback)
223  {
224    $this->api->register_action($action, $this->ID, $callback, $this->mytask);
225  }
226
227  /**
228   * Register a handler function for a template object
229   *
230   * When parsing a template for display, tags like <roundcube:object name="plugin.myobject" />
231   * will be replaced by the return value if the registered callback function.
232   *
233   * @param string $name Object name (should be unique and start with 'plugin.')
234   * @param mixed  $callback Callback function as string or array with object reference and method name
235   */
236  public function register_handler($name, $callback)
237  {
238    $this->api->register_handler($name, $this->ID, $callback);
239  }
240
241  /**
242   * Make this javascipt file available on the client
243   *
244   * @param string $fn File path; absolute or relative to the plugin directory
245   */
246  public function include_script($fn)
247  {
248    $this->api->include_script($this->resource_url($fn));
249  }
250
251  /**
252   * Make this stylesheet available on the client
253   *
254   * @param string $fn File path; absolute or relative to the plugin directory
255   */
256  public function include_stylesheet($fn)
257  {
258    $this->api->include_stylesheet($this->resource_url($fn));
259  }
260
261  /**
262   * Append a button to a certain container
263   *
264   * @param array $p Hash array with named parameters (as used in skin templates)
265   * @param string $container Container name where the buttons should be added to
266   * @see rcube_remplate::button()
267   */
268  public function add_button($p, $container)
269  {
270    if ($this->api->output->type == 'html') {
271      // fix relative paths
272      foreach (array('imagepas', 'imageact', 'imagesel') as $key)
273        if ($p[$key])
274          $p[$key] = $this->api->url . $this->resource_url($p[$key]);
275
276      $this->api->add_content($this->api->output->button($p), $container);
277    }
278  }
279
280  /**
281   * Generate an absolute URL to the given resource within the current
282   * plugin directory
283   *
284   * @param string $fn The file name
285   * @return string Absolute URL to the given resource
286   */
287  public function url($fn)
288  {
289      return $this->api->url . $this->resource_url($fn);
290  }
291
292  /**
293   * Make the given file name link into the plugin directory
294   *
295   * @param string $fn Filename
296   */
297  private function resource_url($fn)
298  {
299    if ($fn[0] != '/' && !preg_match('|^https?://|i', $fn))
300      return $this->ID . '/' . $fn;
301    else
302      return $fn;
303  }
304
305  /**
306   * Provide path to the currently selected skin folder within the plugin directory
307   * with a fallback to the default skin folder.
308   *
309   * @return string Skin path relative to plugins directory
310   */
311  public function local_skin_path()
312  {
313    $rcmail = rcube::get_instance();
314    $skin_path = 'skins/' . $rcmail->config->get('skin');
315    if (!is_dir(realpath(slashify($this->home) . $skin_path)))
316      $skin_path = 'skins/default';
317    return $skin_path;
318  }
319
320  /**
321   * Callback function for array_map
322   *
323   * @param string $key Array key.
324   * @return string
325   */
326  private function label_map_callback($key)
327  {
328    return $this->ID.'.'.$key;
329  }
330
331}
Note: See TracBrowser for help on using the repository browser.