source: github/program/include/rcube_plugin.php @ 59041fd

HEADcourier-fixdev-browser-capabilitiespdo
Last change on this file since 59041fd was 59041fd, checked in by Aleksander Machniak <alec@…>, 13 months ago

Use similar language as a fallback for plugin localization (#1488401)
Don't load en_US localization more than once

  • Property mode set to 100644
File size: 9.9 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    $rcube = rcube::get_instance();
114    if (is_file($fpath) && !$rcube->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    $lang   = $_SESSION['language'];
157    $langs  = array_unique(array('en_US', $lang));
158    $locdir = slashify(realpath(slashify($this->home) . $dir));
159    $texts  = array();
160
161    // Language aliases used to find localization in similar lang, see below
162    $aliases = array(
163        'de_CH' => 'de_DE',
164        'es_AR' => 'es_ES',
165        'fa_AF' => 'fa_IR',
166        'nl_BE' => 'nl_NL',
167        'pt_BR' => 'pt_PT',
168        'zh_CN' => 'zh_TW',
169    );
170
171    // use buffering to handle empty lines/spaces after closing PHP tag
172    ob_start();
173
174    foreach ($langs as $lng) {
175      $fpath = $locdir . $lng . '.inc';
176      if (is_file($fpath) && is_readable($fpath)) {
177        include $fpath;
178        $texts = (array)$labels + (array)$messages + (array)$texts;
179      }
180      else if ($lng != 'en_US') {
181        // Find localization in similar language (#1488401)
182        $alias = null;
183        if (!empty($aliases[$lng])) {
184          $alias = $aliases[$lng];
185        }
186        else if ($key = array_search($lng, $aliases)) {
187          $alias = $key;
188        }
189
190        if (!empty($alias)) {
191          $fpath = $locdir . $alias . '.inc';
192          if (is_file($fpath) && is_readable($fpath)) {
193            include $fpath;
194            $texts = (array)$labels + (array)$messages + (array)$texts;
195          }
196        }
197      }
198    }
199
200    ob_end_clean();
201
202    // prepend domain to text keys and add to the application texts repository
203    if (!empty($texts)) {
204      $add = array();
205      foreach ($texts as $key => $value)
206        $add[$domain.'.'.$key] = $value;
207
208      $rcmail = rcmail::get_instance();
209      $rcmail->load_language($lang, $add);
210
211      // add labels to client
212      if ($add2client) {
213        $js_labels = is_array($add2client) ? array_map(array($this, 'label_map_callback'), $add2client) : array_keys($add);
214        $rcmail->output->add_label($js_labels);
215      }
216    }
217  }
218
219  /**
220   * Wrapper for rcmail::gettext() adding the plugin ID as domain
221   *
222   * @param string $p Message identifier
223   * @return string Localized text
224   * @see rcmail::gettext()
225   */
226  public function gettext($p)
227  {
228    return rcube::get_instance()->gettext($p, $this->ID);
229  }
230
231  /**
232   * Register this plugin to be responsible for a specific task
233   *
234   * @param string $task Task name (only characters [a-z0-9_.-] are allowed)
235   */
236  public function register_task($task)
237  {
238    if ($this->api->register_task($task, $this->ID))
239      $this->mytask = $task;
240  }
241
242  /**
243    * Register a handler for a specific client-request action
244    *
245    * The callback will be executed upon a request like /?_task=mail&_action=plugin.myaction
246    *
247    * @param string $action  Action name (should be unique)
248    * @param mixed $callback Callback function as string or array with object reference and method name
249   */
250  public function register_action($action, $callback)
251  {
252    $this->api->register_action($action, $this->ID, $callback, $this->mytask);
253  }
254
255  /**
256   * Register a handler function for a template object
257   *
258   * When parsing a template for display, tags like <roundcube:object name="plugin.myobject" />
259   * will be replaced by the return value if the registered callback function.
260   *
261   * @param string $name Object name (should be unique and start with 'plugin.')
262   * @param mixed  $callback Callback function as string or array with object reference and method name
263   */
264  public function register_handler($name, $callback)
265  {
266    $this->api->register_handler($name, $this->ID, $callback);
267  }
268
269  /**
270   * Make this javascipt file available on the client
271   *
272   * @param string $fn File path; absolute or relative to the plugin directory
273   */
274  public function include_script($fn)
275  {
276    $this->api->include_script($this->resource_url($fn));
277  }
278
279  /**
280   * Make this stylesheet available on the client
281   *
282   * @param string $fn File path; absolute or relative to the plugin directory
283   */
284  public function include_stylesheet($fn)
285  {
286    $this->api->include_stylesheet($this->resource_url($fn));
287  }
288
289  /**
290   * Append a button to a certain container
291   *
292   * @param array $p Hash array with named parameters (as used in skin templates)
293   * @param string $container Container name where the buttons should be added to
294   * @see rcube_remplate::button()
295   */
296  public function add_button($p, $container)
297  {
298    if ($this->api->output->type == 'html') {
299      // fix relative paths
300      foreach (array('imagepas', 'imageact', 'imagesel') as $key)
301        if ($p[$key])
302          $p[$key] = $this->api->url . $this->resource_url($p[$key]);
303
304      $this->api->add_content($this->api->output->button($p), $container);
305    }
306  }
307
308  /**
309   * Generate an absolute URL to the given resource within the current
310   * plugin directory
311   *
312   * @param string $fn The file name
313   * @return string Absolute URL to the given resource
314   */
315  public function url($fn)
316  {
317      return $this->api->url . $this->resource_url($fn);
318  }
319
320  /**
321   * Make the given file name link into the plugin directory
322   *
323   * @param string $fn Filename
324   */
325  private function resource_url($fn)
326  {
327    if ($fn[0] != '/' && !preg_match('|^https?://|i', $fn))
328      return $this->ID . '/' . $fn;
329    else
330      return $fn;
331  }
332
333  /**
334   * Provide path to the currently selected skin folder within the plugin directory
335   * with a fallback to the default skin folder.
336   *
337   * @return string Skin path relative to plugins directory
338   */
339  public function local_skin_path()
340  {
341    $rcmail = rcube::get_instance();
342    $skin_path = 'skins/' . $rcmail->config->get('skin');
343    if (!is_dir(realpath(slashify($this->home) . $skin_path)))
344      $skin_path = 'skins/default';
345    return $skin_path;
346  }
347
348  /**
349   * Callback function for array_map
350   *
351   * @param string $key Array key.
352   * @return string
353   */
354  private function label_map_callback($key)
355  {
356    return $this->ID.'.'.$key;
357  }
358
359}
Note: See TracBrowser for help on using the repository browser.