| [1291] | 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | +-----------------------------------------------------------------------+ |
|---|
| 5 | | program/include/rcube_json_output.php | |
|---|
| 6 | | | |
|---|
| [3989] | 7 | | This file is part of the Roundcube Webmail client | |
|---|
| 8 | | Copyright (C) 2008-2010, Roundcube Dev. - Switzerland | |
|---|
| [1291] | 9 | | Licensed under the GNU GPL | |
|---|
| 10 | | | |
|---|
| 11 | | PURPOSE: | |
|---|
| 12 | | Class to handle HTML page output using a skin template. | |
|---|
| 13 | | Extends rcube_html_page class from rcube_shared.inc | |
|---|
| 14 | | | |
|---|
| 15 | +-----------------------------------------------------------------------+ |
|---|
| 16 | | Author: Thomas Bruederli <roundcube@gmail.com> | |
|---|
| 17 | +-----------------------------------------------------------------------+ |
|---|
| 18 | |
|---|
| [3211] | 19 | $Id$ |
|---|
| [1291] | 20 | |
|---|
| 21 | */ |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * View class to produce JSON responses |
|---|
| 26 | * |
|---|
| 27 | * @package View |
|---|
| 28 | */ |
|---|
| 29 | class rcube_json_output |
|---|
| 30 | { |
|---|
| [4015] | 31 | /** |
|---|
| 32 | * Stores configuration object. |
|---|
| 33 | * |
|---|
| 34 | * @var rcube_config |
|---|
| 35 | */ |
|---|
| [1291] | 36 | private $config; |
|---|
| [2672] | 37 | private $charset = RCMAIL_CHARSET; |
|---|
| [1291] | 38 | private $texts = array(); |
|---|
| 39 | private $commands = array(); |
|---|
| [2589] | 40 | private $callbacks = array(); |
|---|
| [2441] | 41 | private $message = null; |
|---|
| [1291] | 42 | |
|---|
| [3981] | 43 | public $browser; |
|---|
| [4091] | 44 | public $env = array(); |
|---|
| [1735] | 45 | public $type = 'js'; |
|---|
| [1291] | 46 | public $ajax_call = true; |
|---|
| [3981] | 47 | |
|---|
| 48 | |
|---|
| [1291] | 49 | /** |
|---|
| 50 | * Constructor |
|---|
| 51 | */ |
|---|
| [3981] | 52 | public function __construct($task=null) |
|---|
| [1291] | 53 | { |
|---|
| [3981] | 54 | $this->config = rcmail::get_instance()->config; |
|---|
| 55 | $this->browser = new rcube_browser(); |
|---|
| [1291] | 56 | } |
|---|
| [3553] | 57 | |
|---|
| 58 | |
|---|
| [1291] | 59 | /** |
|---|
| 60 | * Set environment variable |
|---|
| 61 | * |
|---|
| [4015] | 62 | * @param string $name Property name |
|---|
| 63 | * @param mixed $value Property value |
|---|
| [1291] | 64 | */ |
|---|
| 65 | public function set_env($name, $value) |
|---|
| 66 | { |
|---|
| 67 | $this->env[$name] = $value; |
|---|
| 68 | } |
|---|
| [3553] | 69 | |
|---|
| 70 | |
|---|
| [1291] | 71 | /** |
|---|
| [1562] | 72 | * Issue command to set page title |
|---|
| 73 | * |
|---|
| [4015] | 74 | * @param string $title New page title |
|---|
| [1291] | 75 | */ |
|---|
| 76 | public function set_pagetitle($title) |
|---|
| 77 | { |
|---|
| [1562] | 78 | $name = $this->config->get('product_name'); |
|---|
| [2905] | 79 | $this->command('set_pagetitle', empty($name) ? $title : $name.' :: '.$title); |
|---|
| [1291] | 80 | } |
|---|
| 81 | |
|---|
| [3553] | 82 | |
|---|
| [1291] | 83 | /** |
|---|
| 84 | * @ignore |
|---|
| 85 | */ |
|---|
| 86 | function set_charset($charset) |
|---|
| 87 | { |
|---|
| 88 | // ignore: $this->charset = $charset; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Get charset for output |
|---|
| 94 | * |
|---|
| 95 | * @return string Output charset |
|---|
| 96 | */ |
|---|
| 97 | function get_charset() |
|---|
| 98 | { |
|---|
| 99 | return $this->charset; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | /** |
|---|
| 104 | * Register a template object handler |
|---|
| 105 | * |
|---|
| [4015] | 106 | * @param string $obj Object name |
|---|
| 107 | * @param string $func Function name to call |
|---|
| [1291] | 108 | * @return void |
|---|
| 109 | */ |
|---|
| 110 | public function add_handler($obj, $func) |
|---|
| 111 | { |
|---|
| 112 | // ignore |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| [3553] | 115 | |
|---|
| [1291] | 116 | /** |
|---|
| 117 | * Register a list of template object handlers |
|---|
| 118 | * |
|---|
| [4015] | 119 | * @param array $arr Hash array with object=>handler pairs |
|---|
| [1291] | 120 | * @return void |
|---|
| 121 | */ |
|---|
| 122 | public function add_handlers($arr) |
|---|
| 123 | { |
|---|
| 124 | // ignore |
|---|
| 125 | } |
|---|
| [3553] | 126 | |
|---|
| 127 | |
|---|
| [1291] | 128 | /** |
|---|
| 129 | * Call a client method |
|---|
| 130 | * |
|---|
| 131 | * @param string Method to call |
|---|
| 132 | * @param ... Additional arguments |
|---|
| 133 | */ |
|---|
| 134 | public function command() |
|---|
| 135 | { |
|---|
| [2589] | 136 | $cmd = func_get_args(); |
|---|
| [4083] | 137 | |
|---|
| [2589] | 138 | if (strpos($cmd[0], 'plugin.') === 0) |
|---|
| 139 | $this->callbacks[] = $cmd; |
|---|
| 140 | else |
|---|
| 141 | $this->commands[] = $cmd; |
|---|
| [1291] | 142 | } |
|---|
| [4083] | 143 | |
|---|
| 144 | |
|---|
| [1291] | 145 | /** |
|---|
| 146 | * Add a localized label to the client environment |
|---|
| 147 | */ |
|---|
| 148 | public function add_label() |
|---|
| 149 | { |
|---|
| [2429] | 150 | $args = func_get_args(); |
|---|
| 151 | if (count($args) == 1 && is_array($args[0])) |
|---|
| 152 | $args = $args[0]; |
|---|
| [4083] | 153 | |
|---|
| [2429] | 154 | foreach ($args as $name) { |
|---|
| [1297] | 155 | $this->texts[$name] = rcube_label($name); |
|---|
| [1291] | 156 | } |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| [3553] | 159 | |
|---|
| [1291] | 160 | /** |
|---|
| 161 | * Invoke display_message command |
|---|
| 162 | * |
|---|
| [4015] | 163 | * @param string $message Message to display |
|---|
| 164 | * @param string $type Message type [notice|confirm|error] |
|---|
| 165 | * @param array $vars Key-value pairs to be replaced in localized text |
|---|
| 166 | * @param boolean $override Override last set message |
|---|
| [1291] | 167 | * @uses self::command() |
|---|
| 168 | */ |
|---|
| [2441] | 169 | public function show_message($message, $type='notice', $vars=null, $override=true) |
|---|
| [1291] | 170 | { |
|---|
| [2441] | 171 | if ($override || !$this->message) { |
|---|
| 172 | $this->message = $message; |
|---|
| 173 | $this->command( |
|---|
| 174 | 'display_message', |
|---|
| 175 | rcube_label(array('name' => $message, 'vars' => $vars)), |
|---|
| 176 | $type |
|---|
| 177 | ); |
|---|
| 178 | } |
|---|
| [1291] | 179 | } |
|---|
| [3553] | 180 | |
|---|
| 181 | |
|---|
| [1291] | 182 | /** |
|---|
| 183 | * Delete all stored env variables and commands |
|---|
| 184 | */ |
|---|
| [1568] | 185 | public function reset() |
|---|
| [1291] | 186 | { |
|---|
| 187 | $this->env = array(); |
|---|
| 188 | $this->texts = array(); |
|---|
| 189 | $this->commands = array(); |
|---|
| 190 | } |
|---|
| [3553] | 191 | |
|---|
| 192 | |
|---|
| [1562] | 193 | /** |
|---|
| 194 | * Redirect to a certain url |
|---|
| 195 | * |
|---|
| [4015] | 196 | * @param mixed $p Either a string with the action or url parameters as key-value pairs |
|---|
| 197 | * @param int $delay Delay in seconds |
|---|
| [1562] | 198 | * @see rcmail::url() |
|---|
| 199 | */ |
|---|
| [1564] | 200 | public function redirect($p = array(), $delay = 1) |
|---|
| [1562] | 201 | { |
|---|
| 202 | $location = rcmail::get_instance()->url($p); |
|---|
| 203 | $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);"); |
|---|
| 204 | exit; |
|---|
| 205 | } |
|---|
| [4083] | 206 | |
|---|
| 207 | |
|---|
| [1291] | 208 | /** |
|---|
| 209 | * Send an AJAX response to the client. |
|---|
| 210 | */ |
|---|
| 211 | public function send() |
|---|
| 212 | { |
|---|
| 213 | $this->remote_response(); |
|---|
| 214 | exit; |
|---|
| 215 | } |
|---|
| [4083] | 216 | |
|---|
| 217 | |
|---|
| [1291] | 218 | /** |
|---|
| 219 | * Send an AJAX response with executable JS code |
|---|
| 220 | * |
|---|
| [4015] | 221 | * @param string $add Additional JS code |
|---|
| [1291] | 222 | * @param boolean True if output buffer should be flushed |
|---|
| 223 | * @return void |
|---|
| 224 | * @deprecated |
|---|
| 225 | */ |
|---|
| [2401] | 226 | public function remote_response($add='') |
|---|
| [1291] | 227 | { |
|---|
| 228 | static $s_header_sent = false; |
|---|
| 229 | |
|---|
| 230 | if (!$s_header_sent) { |
|---|
| 231 | $s_header_sent = true; |
|---|
| 232 | send_nocacheing_headers(); |
|---|
| [2401] | 233 | header('Content-Type: text/plain; charset=' . $this->get_charset()); |
|---|
| [1291] | 234 | } |
|---|
| 235 | |
|---|
| 236 | // unset default env vars |
|---|
| 237 | unset($this->env['task'], $this->env['action'], $this->env['comm_path']); |
|---|
| 238 | |
|---|
| [2401] | 239 | $rcmail = rcmail::get_instance(); |
|---|
| [4139] | 240 | $response['action'] = $rcmail->action; |
|---|
| [4083] | 241 | |
|---|
| [4139] | 242 | if ($unlock = get_input_value('_unlock', RCUBE_INPUT_GPC)) { |
|---|
| 243 | $response['unlock'] = $unlock; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| [2401] | 246 | if (!empty($this->env)) |
|---|
| [3553] | 247 | $response['env'] = $this->env; |
|---|
| [4083] | 248 | |
|---|
| [2401] | 249 | if (!empty($this->texts)) |
|---|
| [3553] | 250 | $response['texts'] = $this->texts; |
|---|
| [2401] | 251 | |
|---|
| [2589] | 252 | // send function calls |
|---|
| [2401] | 253 | $response['exec'] = $this->get_js_commands() . $add; |
|---|
| [4083] | 254 | |
|---|
| [2589] | 255 | if (!empty($this->callbacks)) |
|---|
| [3553] | 256 | $response['callbacks'] = $this->callbacks; |
|---|
| [1291] | 257 | |
|---|
| [3175] | 258 | echo json_serialize($response); |
|---|
| [1291] | 259 | } |
|---|
| [3553] | 260 | |
|---|
| 261 | |
|---|
| [1291] | 262 | /** |
|---|
| 263 | * Return executable javascript code for all registered commands |
|---|
| 264 | * |
|---|
| 265 | * @return string $out |
|---|
| 266 | */ |
|---|
| 267 | private function get_js_commands() |
|---|
| 268 | { |
|---|
| [1305] | 269 | $out = ''; |
|---|
| [3175] | 270 | |
|---|
| [1291] | 271 | foreach ($this->commands as $i => $args) { |
|---|
| 272 | $method = array_shift($args); |
|---|
| 273 | foreach ($args as $i => $arg) { |
|---|
| [3175] | 274 | $args[$i] = json_serialize($arg); |
|---|
| [1291] | 275 | } |
|---|
| 276 | |
|---|
| 277 | $out .= sprintf( |
|---|
| 278 | "this.%s(%s);\n", |
|---|
| 279 | preg_replace('/^parent\./', '', $method), |
|---|
| 280 | implode(',', $args) |
|---|
| 281 | ); |
|---|
| 282 | } |
|---|
| [1358] | 283 | |
|---|
| [1291] | 284 | return $out; |
|---|
| 285 | } |
|---|
| 286 | } |
|---|