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

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