| 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 | * Register a callback function for a specific (server-side) hook |
|---|
| 53 | * |
|---|
| 54 | * @param string Hook name |
|---|
| 55 | * @param mixed Callback function as string or array with object reference and method name |
|---|
| 56 | */ |
|---|
| 57 | public function add_hook($hook, $callback) |
|---|
| 58 | { |
|---|
| 59 | $this->api->register_hook($hook, $callback); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Load localized texts from the plugins dir |
|---|
| 64 | * |
|---|
| 65 | * @param string Directory to search in |
|---|
| 66 | * @param mixed Make texts also available on the client (array with list or true for all) |
|---|
| 67 | */ |
|---|
| 68 | public function add_texts($dir, $add2client = false) |
|---|
| 69 | { |
|---|
| 70 | $domain = $this->ID; |
|---|
| 71 | |
|---|
| 72 | $lang = $_SESSION['language']; |
|---|
| 73 | $locdir = slashify(realpath(slashify($this->home) . $dir)); |
|---|
| 74 | $texts = array(); |
|---|
| 75 | |
|---|
| 76 | foreach (array('en_US', $lang) as $lng) { |
|---|
| 77 | @include($locdir . $lng . '.inc'); |
|---|
| 78 | $texts = (array)$labels + (array)$messages + (array)$texts; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | // prepend domain to text keys and add to the application texts repository |
|---|
| 82 | if (!empty($texts)) { |
|---|
| 83 | $add = array(); |
|---|
| 84 | foreach ($texts as $key => $value) |
|---|
| 85 | $add[$domain.'.'.$key] = $value; |
|---|
| 86 | |
|---|
| 87 | $rcmail = rcmail::get_instance(); |
|---|
| 88 | $rcmail->load_language($lang, $add); |
|---|
| 89 | |
|---|
| 90 | // add labels to client |
|---|
| 91 | if ($add2client) { |
|---|
| 92 | $js_labels = is_array($add2client) ? array_map(array($this, 'label_map_callback'), $add2client) : array_keys($add); |
|---|
| 93 | $rcmail->output->add_label($js_labels); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Wrapper for rcmail::gettext() adding the plugin ID as domain |
|---|
| 100 | * |
|---|
| 101 | * @return string Localized text |
|---|
| 102 | * @see rcmail::gettext() |
|---|
| 103 | */ |
|---|
| 104 | function gettext($p) |
|---|
| 105 | { |
|---|
| 106 | return rcmail::get_instance()->gettext($p, $this->ID); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | /** |
|---|
| 110 | * Register a handler for a specific client-request action |
|---|
| 111 | * |
|---|
| 112 | * The callback will be executed upon a request like /?_task=mail&_action=plugin.myaction |
|---|
| 113 | * |
|---|
| 114 | * @param string Action name (should be unique) |
|---|
| 115 | * @param mixed Callback function as string or array with object reference and method name |
|---|
| 116 | */ |
|---|
| 117 | public function register_action($action, $callback) |
|---|
| 118 | { |
|---|
| 119 | $this->api->register_action($action, $this->ID, $callback); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | /** |
|---|
| 123 | * Register a handler function for a template object |
|---|
| 124 | * |
|---|
| 125 | * When parsing a template for display, tags like <roundcube:object name="plugin.myobject" /> |
|---|
| 126 | * will be replaced by the return value if the registered callback function. |
|---|
| 127 | * |
|---|
| 128 | * @param string Object name (should be unique and start with 'plugin.') |
|---|
| 129 | * @param mixed Callback function as string or array with object reference and method name |
|---|
| 130 | */ |
|---|
| 131 | public function register_handler($name, $callback) |
|---|
| 132 | { |
|---|
| 133 | $this->api->register_handler($name, $this->ID, $callback); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * Make this javascipt file available on the client |
|---|
| 138 | * |
|---|
| 139 | * @param string File path; absolute or relative to the plugin directory |
|---|
| 140 | */ |
|---|
| 141 | public function include_script($fn) |
|---|
| 142 | { |
|---|
| 143 | $this->api->include_script($this->ressource_url($fn)); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | /** |
|---|
| 147 | * Make this stylesheet available on the client |
|---|
| 148 | * |
|---|
| 149 | * @param string File path; absolute or relative to the plugin directory |
|---|
| 150 | */ |
|---|
| 151 | public function include_stylesheet($fn) |
|---|
| 152 | { |
|---|
| 153 | $this->api->include_stylesheet($this->ressource_url($fn)); |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | /** |
|---|
| 157 | * Append a button to a certain container |
|---|
| 158 | * |
|---|
| 159 | * @param array Hash array with named parameters (as used in skin templates) |
|---|
| 160 | * @param string Container name where the buttons should be added to |
|---|
| 161 | * @see rcube_remplate::button() |
|---|
| 162 | */ |
|---|
| 163 | public function add_button($p, $container) |
|---|
| 164 | { |
|---|
| 165 | if ($this->api->output->type == 'html') { |
|---|
| 166 | // fix relative paths |
|---|
| 167 | foreach (array('imagepas', 'imageact', 'imagesel') as $key) |
|---|
| 168 | if ($p[$key]) |
|---|
| 169 | $p[$key] = $this->api->url . $this->ressource_url($p[$key]); |
|---|
| 170 | |
|---|
| 171 | $this->api->add_content($this->api->output->button($p), $container); |
|---|
| 172 | } |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | /** |
|---|
| 176 | * Make the given file name link into the plugin directory |
|---|
| 177 | */ |
|---|
| 178 | private function ressource_url($fn) |
|---|
| 179 | { |
|---|
| 180 | if ($fn[0] != '/' && !eregi('^https?://', $fn)) |
|---|
| 181 | return $this->ID . '/' . $fn; |
|---|
| 182 | else |
|---|
| 183 | return $fn; |
|---|
| 184 | } |
|---|
| 185 | |
|---|
| 186 | /** |
|---|
| 187 | * Callback function for array_map |
|---|
| 188 | */ |
|---|
| 189 | private function label_map_callback($key) |
|---|
| 190 | { |
|---|
| 191 | return $this->ID.'.'.$key; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | } |
|---|
| 196 | |
|---|