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

Last change on this file since 3274 was 3274, checked in by thomasb, 3 years ago

Fix loading of plugin configs: user prefs will always survive (#1486368)

  • Property svn:keywords set to Id
File size: 7.8 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 Core
26 */
27abstract 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 (is_file($fpath) && !$rcmail->config->load_from_file($fpath)) {
63      raise_error(array('code' => 527, 'type' => 'php',
64        'file' => __FILE__, 'line' => __LINE__,
65        'message' => "Failed to load config from $fpath"), true, false);
66      return false;
67    }
68   
69    return true;
70  }
71
72  /**
73   * Register a callback function for a specific (server-side) hook
74   *
75   * @param string Hook name
76   * @param mixed Callback function as string or array with object reference and method name
77   */
78  public function add_hook($hook, $callback)
79  {
80    $this->api->register_hook($hook, $callback);
81  }
82 
83  /**
84   * Load localized texts from the plugins dir
85   *
86   * @param string Directory to search in
87   * @param mixed Make texts also available on the client (array with list or true for all)
88   */
89  public function add_texts($dir, $add2client = false)
90  {
91    $domain = $this->ID;
92   
93    $lang = $_SESSION['language'];
94    $locdir = slashify(realpath(slashify($this->home) . $dir));
95    $texts = array();
96   
97    foreach (array('en_US', $lang) as $lng) {
98      @include($locdir . $lng . '.inc');
99      $texts = (array)$labels + (array)$messages + (array)$texts;
100    }
101
102    // prepend domain to text keys and add to the application texts repository
103    if (!empty($texts)) {
104      $add = array();
105      foreach ($texts as $key => $value)
106        $add[$domain.'.'.$key] = $value;
107
108      $rcmail = rcmail::get_instance();
109      $rcmail->load_language($lang, $add);
110     
111      // add labels to client
112      if ($add2client) {
113        $js_labels = is_array($add2client) ? array_map(array($this, 'label_map_callback'), $add2client) : array_keys($add);
114        $rcmail->output->add_label($js_labels);
115      }
116    }
117  }
118 
119  /**
120   * Wrapper for rcmail::gettext() adding the plugin ID as domain
121   *
122   * @return string Localized text
123   * @see rcmail::gettext()
124   */
125  public function gettext($p)
126  {
127    return rcmail::get_instance()->gettext($p, $this->ID);
128  }
129
130  /**
131   * Register this plugin to be responsible for a specific task
132   *
133   * @param string Task name (only characters [a-z0-9_.-] are allowed)
134   */
135  public function register_task($task)
136  {
137    if ($task != asciiwords($task)) {
138      raise_error(array('code' => 526, 'type' => 'php',
139        'file' => __FILE__, 'line' => __LINE__,
140        'message' => "Invalid task name: $task. Only characters [a-z0-9_.-] are allowed"), true, false);
141    }
142    else if (in_array(rcmail::$main_tasks, $task)) {
143      raise_error(array('code' => 526, 'type' => 'php',
144        'file' => __FILE__, 'line' => __LINE__,
145        'message' => "Cannot register taks $task; already taken by another plugin or the application itself"), true, false);
146    }
147    else {
148      rcmail::$main_tasks[] = $task;
149    }
150  }
151
152  /**
153    * Register a handler for a specific client-request action
154    *
155    * The callback will be executed upon a request like /?_task=mail&_action=plugin.myaction
156    *
157    * @param string Action name (should be unique)
158    * @param mixed Callback function as string or array with object reference and method name
159   */
160  public function register_action($action, $callback)
161  {
162    $this->api->register_action($action, $this->ID, $callback);
163  }
164
165  /**
166   * Register a handler function for a template object
167   *
168   * When parsing a template for display, tags like <roundcube:object name="plugin.myobject" />
169   * will be replaced by the return value if the registered callback function.
170   *
171   * @param string Object name (should be unique and start with 'plugin.')
172   * @param mixed Callback function as string or array with object reference and method name
173   */
174  public function register_handler($name, $callback)
175  {
176    $this->api->register_handler($name, $this->ID, $callback);
177  }
178
179  /**
180   * Make this javascipt file available on the client
181   *
182   * @param string File path; absolute or relative to the plugin directory
183   */
184  public function include_script($fn)
185  {
186    $this->api->include_script($this->resource_url($fn));
187  }
188
189  /**
190   * Make this stylesheet available on the client
191   *
192   * @param string File path; absolute or relative to the plugin directory
193   */
194  public function include_stylesheet($fn)
195  {
196    $this->api->include_stylesheet($this->resource_url($fn));
197  }
198 
199  /**
200   * Append a button to a certain container
201   *
202   * @param array Hash array with named parameters (as used in skin templates)
203   * @param string Container name where the buttons should be added to
204   * @see rcube_remplate::button()
205   */
206  public function add_button($p, $container)
207  {
208    if ($this->api->output->type == 'html') {
209      // fix relative paths
210      foreach (array('imagepas', 'imageact', 'imagesel') as $key)
211        if ($p[$key])
212          $p[$key] = $this->api->url . $this->resource_url($p[$key]);
213     
214      $this->api->add_content($this->api->output->button($p), $container);
215    }
216  }
217 
218  /**
219   * Generate an absolute URL to the given resource within the current
220   * plugin directory
221   *
222   * @param string The file name
223   * @return string Absolute URL to the given resource
224   */
225  public function url($fn)
226  {
227      return $this->api->url . $this->resource_url($fn);
228  }
229
230  /**
231   * Make the given file name link into the plugin directory
232   */
233  private function resource_url($fn)
234  {
235    if ($fn[0] != '/' && !preg_match('|^https?://|i', $fn))
236      return $this->ID . '/' . $fn;
237    else
238      return $fn;
239  }
240 
241  /**
242   * Provide path to the currently selected skin folder within the plugin directory
243   * with a fallback to the default skin folder.
244   *
245   * @return string Skin path relative to plugins directory
246   */
247  protected function local_skin_path()
248  {
249      $skin_path = 'skins/'.$this->api->output->config['skin'];
250      if (!is_dir(realpath(slashify($this->home) . $skin_path)))
251        $skin_path = 'skins/default';
252    return $skin_path;
253  }
254
255  /**
256   * Callback function for array_map
257   */
258  private function label_map_callback($key)
259  {
260    return $this->ID.'.'.$key;
261  }
262
263
264}
265
Note: See TracBrowser for help on using the repository browser.