4 +-----------------------------------------------------------------------+
5 | program/include/rcube_json_output.php |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2010, The Roundcube Dev Team |
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 5037 2011-08-09 18:40:42Z 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 if ($this->config->get('devel_mode') && !empty($_SESSION['username']))
79 $name = $_SESSION['username'];
81 $name = $this->config->get('product_name');
83 $this->command('set_pagetitle', empty($name) ? $title : $name.' :: '.$title);
90 function set_charset($charset)
92 // ignore: $this->charset = $charset;
97 * Get charset for output
99 * @return string Output charset
101 function get_charset()
103 return $this->charset;
108 * Register a template object handler
110 * @param string $obj Object name
111 * @param string $func Function name to call
114 public function add_handler($obj, $func)
121 * Register a list of template object handlers
123 * @param array $arr Hash array with object=>handler pairs
126 public function add_handlers($arr)
133 * Call a client method
135 * @param string Method to call
136 * @param ... Additional arguments
138 public function command()
140 $cmd = func_get_args();
142 if (strpos($cmd[0], 'plugin.') === 0)
143 $this->callbacks[] = $cmd;
145 $this->commands[] = $cmd;
150 * Add a localized label to the client environment
152 public function add_label()
154 $args = func_get_args();
155 if (count($args) == 1 && is_array($args[0]))
158 foreach ($args as $name) {
159 $this->texts[$name] = rcube_label($name);
165 * Invoke display_message command
167 * @param string $message Message to display
168 * @param string $type Message type [notice|confirm|error]
169 * @param array $vars Key-value pairs to be replaced in localized text
170 * @param boolean $override Override last set message
171 * @param int $timeout Message displaying time in seconds
172 * @uses self::command()
174 public function show_message($message, $type='notice', $vars=null, $override=true, $timeout=0)
176 if ($override || !$this->message) {
177 if (rcube_label_exists($message)) {
179 $vars = array_map('Q', $vars);
180 $msgtext = rcube_label(array('name' => $message, 'vars' => $vars));
185 $this->message = $message;
186 $this->command('display_message', $msgtext, $type, $timeout * 1000);
192 * Delete all stored env variables and commands
194 public function reset()
196 $this->env = array();
197 $this->texts = array();
198 $this->commands = array();
203 * Redirect to a certain url
205 * @param mixed $p Either a string with the action or url parameters as key-value pairs
206 * @param int $delay Delay in seconds
209 public function redirect($p = array(), $delay = 1)
211 $location = rcmail::get_instance()->url($p);
212 $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);");
218 * Send an AJAX response to the client.
220 public function send()
222 $this->remote_response();
228 * Send an AJAX response with executable JS code
230 * @param string $add Additional JS code
231 * @param boolean True if output buffer should be flushed
235 public function remote_response($add='')
237 static $s_header_sent = false;
239 if (!$s_header_sent) {
240 $s_header_sent = true;
241 send_nocacheing_headers();
242 header('Content-Type: text/plain; charset=' . $this->get_charset());
245 // unset default env vars
246 unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
248 $rcmail = rcmail::get_instance();
249 $response['action'] = $rcmail->action;
251 if ($unlock = get_input_value('_unlock', RCUBE_INPUT_GPC)) {
252 $response['unlock'] = $unlock;
255 if (!empty($this->env))
256 $response['env'] = $this->env;
258 if (!empty($this->texts))
259 $response['texts'] = $this->texts;
261 // send function calls
262 $response['exec'] = $this->get_js_commands() . $add;
264 if (!empty($this->callbacks))
265 $response['callbacks'] = $this->callbacks;
267 echo json_serialize($response);
272 * Return executable javascript code for all registered commands
274 * @return string $out
276 private function get_js_commands()
280 foreach ($this->commands as $i => $args) {
281 $method = array_shift($args);
282 foreach ($args as $i => $arg) {
283 $args[$i] = json_serialize($arg);
288 preg_replace('/^parent\./', '', $method),