4 +-----------------------------------------------------------------------+
5 | program/include/rcube_json_output.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2010, Roundcube Dev. - Switzerland |
9 | Licensed under the GNU GPL |
12 | Class to handle HTML page output using a skin template. |
13 | Extends rcube_html_page class from rcube_shared.inc |
15 +-----------------------------------------------------------------------+
16 | Author: Thomas Bruederli <roundcube@gmail.com> |
17 +-----------------------------------------------------------------------+
19 $Id: rcube_json_output.php 4139 2010-10-26 13:20:34Z alec $
25 * View class to produce JSON responses
29 class rcube_json_output
32 * Stores configuration object.
37 private $charset = RCMAIL_CHARSET;
38 private $texts = array();
39 private $commands = array();
40 private $callbacks = array();
41 private $message = null;
44 public $env = array();
46 public $ajax_call = true;
52 public function __construct($task=null)
54 $this->config = rcmail::get_instance()->config;
55 $this->browser = new rcube_browser();
60 * Set environment variable
62 * @param string $name Property name
63 * @param mixed $value Property value
65 public function set_env($name, $value)
67 $this->env[$name] = $value;
72 * Issue command to set page title
74 * @param string $title New page title
76 public function set_pagetitle($title)
78 $name = $this->config->get('product_name');
79 $this->command('set_pagetitle', empty($name) ? $title : $name.' :: '.$title);
86 function set_charset($charset)
88 // ignore: $this->charset = $charset;
93 * Get charset for output
95 * @return string Output charset
97 function get_charset()
99 return $this->charset;
104 * Register a template object handler
106 * @param string $obj Object name
107 * @param string $func Function name to call
110 public function add_handler($obj, $func)
117 * Register a list of template object handlers
119 * @param array $arr Hash array with object=>handler pairs
122 public function add_handlers($arr)
129 * Call a client method
131 * @param string Method to call
132 * @param ... Additional arguments
134 public function command()
136 $cmd = func_get_args();
138 if (strpos($cmd[0], 'plugin.') === 0)
139 $this->callbacks[] = $cmd;
141 $this->commands[] = $cmd;
146 * Add a localized label to the client environment
148 public function add_label()
150 $args = func_get_args();
151 if (count($args) == 1 && is_array($args[0]))
154 foreach ($args as $name) {
155 $this->texts[$name] = rcube_label($name);
161 * Invoke display_message command
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
167 * @uses self::command()
169 public function show_message($message, $type='notice', $vars=null, $override=true)
171 if ($override || !$this->message) {
172 $this->message = $message;
175 rcube_label(array('name' => $message, 'vars' => $vars)),
183 * Delete all stored env variables and commands
185 public function reset()
187 $this->env = array();
188 $this->texts = array();
189 $this->commands = array();
194 * Redirect to a certain url
196 * @param mixed $p Either a string with the action or url parameters as key-value pairs
197 * @param int $delay Delay in seconds
200 public function redirect($p = array(), $delay = 1)
202 $location = rcmail::get_instance()->url($p);
203 $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);");
209 * Send an AJAX response to the client.
211 public function send()
213 $this->remote_response();
219 * Send an AJAX response with executable JS code
221 * @param string $add Additional JS code
222 * @param boolean True if output buffer should be flushed
226 public function remote_response($add='')
228 static $s_header_sent = false;
230 if (!$s_header_sent) {
231 $s_header_sent = true;
232 send_nocacheing_headers();
233 header('Content-Type: text/plain; charset=' . $this->get_charset());
236 // unset default env vars
237 unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
239 $rcmail = rcmail::get_instance();
240 $response['action'] = $rcmail->action;
242 if ($unlock = get_input_value('_unlock', RCUBE_INPUT_GPC)) {
243 $response['unlock'] = $unlock;
246 if (!empty($this->env))
247 $response['env'] = $this->env;
249 if (!empty($this->texts))
250 $response['texts'] = $this->texts;
252 // send function calls
253 $response['exec'] = $this->get_js_commands() . $add;
255 if (!empty($this->callbacks))
256 $response['callbacks'] = $this->callbacks;
258 echo json_serialize($response);
263 * Return executable javascript code for all registered commands
265 * @return string $out
267 private function get_js_commands()
271 foreach ($this->commands as $i => $args) {
272 $method = array_shift($args);
273 foreach ($args as $i => $arg) {
274 $args[$i] = json_serialize($arg);
279 preg_replace('/^parent\./', '', $method),