0
|
1 <?php
|
|
2
|
|
3 /**
|
|
4 +-----------------------------------------------------------------------+
|
|
5 | program/include/rcmail_output_json.php |
|
|
6 | |
|
|
7 | This file is part of the Roundcube Webmail client |
|
|
8 | Copyright (C) 2008-2012, The Roundcube Dev Team |
|
|
9 | |
|
|
10 | Licensed under the GNU General Public License version 3 or |
|
|
11 | any later version with exceptions for skins & plugins. |
|
|
12 | See the README file for a full license statement. |
|
|
13 | |
|
|
14 | PURPOSE: |
|
|
15 | Class to handle JSON (AJAX) output |
|
|
16 +-----------------------------------------------------------------------+
|
|
17 | Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
18 | Author: Aleksander Machniak <alec@alec.pl> |
|
|
19 +-----------------------------------------------------------------------+
|
|
20 */
|
|
21
|
|
22 /**
|
|
23 * View class to produce JSON responses
|
|
24 *
|
|
25 * @package Webmail
|
|
26 * @subpackage View
|
|
27 */
|
|
28 class rcmail_output_json extends rcmail_output
|
|
29 {
|
|
30 protected $texts = array();
|
|
31 protected $commands = array();
|
|
32 protected $callbacks = array();
|
|
33 protected $message = null;
|
|
34
|
|
35 public $type = 'js';
|
|
36 public $ajax_call = true;
|
|
37
|
|
38
|
|
39 /**
|
|
40 * Issue command to set page title
|
|
41 *
|
|
42 * @param string $title New page title
|
|
43 */
|
|
44 public function set_pagetitle($title)
|
|
45 {
|
|
46 if ($this->config->get('devel_mode') && !empty($_SESSION['username'])) {
|
|
47 $name = $_SESSION['username'];
|
|
48 }
|
|
49 else {
|
|
50 $name = $this->config->get('product_name');
|
|
51 }
|
|
52
|
|
53 $this->command('set_pagetitle', empty($name) ? $title : $name . ' :: ' . $title);
|
|
54 }
|
|
55
|
|
56 /**
|
|
57 * Register a template object handler
|
|
58 *
|
|
59 * @param string $obj Object name
|
|
60 * @param string $func Function name to call
|
|
61 */
|
|
62 public function add_handler($obj, $func)
|
|
63 {
|
|
64 // ignore
|
|
65 }
|
|
66
|
|
67 /**
|
|
68 * Register a list of template object handlers
|
|
69 *
|
|
70 * @param array $arr Hash array with object=>handler pairs
|
|
71 */
|
|
72 public function add_handlers($arr)
|
|
73 {
|
|
74 // ignore
|
|
75 }
|
|
76
|
|
77 /**
|
|
78 * Call a client method
|
|
79 *
|
|
80 * @param string Method to call
|
|
81 * @param ... Additional arguments
|
|
82 */
|
|
83 public function command()
|
|
84 {
|
|
85 $cmd = func_get_args();
|
|
86
|
|
87 if (strpos($cmd[0], 'plugin.') === 0) {
|
|
88 $this->callbacks[] = $cmd;
|
|
89 }
|
|
90 else {
|
|
91 $this->commands[] = $cmd;
|
|
92 }
|
|
93 }
|
|
94
|
|
95 /**
|
|
96 * Add a localized label to the client environment
|
|
97 */
|
|
98 public function add_label()
|
|
99 {
|
|
100 $args = func_get_args();
|
|
101 if (count($args) == 1 && is_array($args[0])) {
|
|
102 $args = $args[0];
|
|
103 }
|
|
104
|
|
105 foreach ($args as $name) {
|
|
106 $this->texts[$name] = $this->app->gettext($name);
|
|
107 }
|
|
108 }
|
|
109
|
|
110 /**
|
|
111 * Invoke display_message command
|
|
112 *
|
|
113 * @param string $message Message to display
|
|
114 * @param string $type Message type [notice|confirm|error]
|
|
115 * @param array $vars Key-value pairs to be replaced in localized text
|
|
116 * @param boolean $override Override last set message
|
|
117 * @param int $timeout Message displaying time in seconds
|
|
118 *
|
|
119 * @uses self::command()
|
|
120 */
|
|
121 public function show_message($message, $type='notice', $vars=null, $override=true, $timeout=0)
|
|
122 {
|
|
123 if ($override || !$this->message) {
|
|
124 if ($this->app->text_exists($message)) {
|
|
125 if (!empty($vars)) {
|
|
126 $vars = array_map(array('rcmail', 'Q'), $vars);
|
|
127 }
|
|
128 $msgtext = $this->app->gettext(array('name' => $message, 'vars' => $vars));
|
|
129 }
|
|
130 else
|
|
131 $msgtext = $message;
|
|
132
|
|
133 $this->message = $message;
|
|
134 $this->command('display_message', $msgtext, $type, $timeout * 1000);
|
|
135 }
|
|
136 }
|
|
137
|
|
138 /**
|
|
139 * Delete all stored env variables and commands
|
|
140 */
|
|
141 public function reset()
|
|
142 {
|
|
143 parent::reset();
|
|
144 $this->texts = array();
|
|
145 $this->commands = array();
|
|
146 }
|
|
147
|
|
148 /**
|
|
149 * Redirect to a certain url
|
|
150 *
|
|
151 * @param mixed $p Either a string with the action or url parameters as key-value pairs
|
|
152 * @param int $delay Delay in seconds
|
|
153 *
|
|
154 * @see rcmail::url()
|
|
155 */
|
|
156 public function redirect($p = array(), $delay = 1)
|
|
157 {
|
|
158 $location = $this->app->url($p);
|
|
159 $this->remote_response(sprintf("window.setTimeout(function(){ %s.redirect('%s',true); }, %d);",
|
|
160 self::JS_OBJECT_NAME, $location, $delay));
|
|
161 exit;
|
|
162 }
|
|
163
|
|
164 /**
|
|
165 * Send an AJAX response to the client.
|
|
166 */
|
|
167 public function send()
|
|
168 {
|
|
169 $this->remote_response();
|
|
170 exit;
|
|
171 }
|
|
172
|
|
173 /**
|
|
174 * Show error page and terminate script execution
|
|
175 *
|
|
176 * @param int $code Error code
|
|
177 * @param string $message Error message
|
|
178 */
|
|
179 public function raise_error($code, $message)
|
|
180 {
|
|
181 if ($code == 403) {
|
|
182 header('HTTP/1.1 403 Forbidden');
|
|
183 die("Invalid Request");
|
|
184 }
|
|
185
|
|
186 $this->show_message("Application Error ($code): $message", 'error');
|
|
187 $this->remote_response();
|
|
188 exit;
|
|
189 }
|
|
190
|
|
191 /**
|
|
192 * Send an AJAX response with executable JS code
|
|
193 *
|
|
194 * @param string $add Additional JS code
|
|
195 */
|
|
196 protected function remote_response($add = '')
|
|
197 {
|
|
198 static $s_header_sent = false;
|
|
199
|
|
200 if (!$s_header_sent) {
|
|
201 $s_header_sent = true;
|
|
202 $this->nocacheing_headers();
|
|
203 header('Content-Type: text/plain; charset=' . $this->get_charset());
|
|
204 }
|
|
205
|
|
206 // unset default env vars
|
|
207 unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
|
|
208
|
|
209 $rcmail = rcmail::get_instance();
|
|
210 $response['action'] = $rcmail->action;
|
|
211
|
|
212 if ($unlock = rcube_utils::get_input_value('_unlock', rcube_utils::INPUT_GPC)) {
|
|
213 $response['unlock'] = $unlock;
|
|
214 }
|
|
215
|
|
216 if (!empty($this->env))
|
|
217 $response['env'] = $this->env;
|
|
218
|
|
219 if (!empty($this->texts))
|
|
220 $response['texts'] = $this->texts;
|
|
221
|
|
222 // send function calls
|
|
223 $response['exec'] = $this->get_js_commands() . $add;
|
|
224
|
|
225 if (!empty($this->callbacks))
|
|
226 $response['callbacks'] = $this->callbacks;
|
|
227
|
|
228 // trigger generic hook where plugins can put additional content to the response
|
|
229 $hook = $this->app->plugins->exec_hook("render_response", array('response' => $response));
|
|
230
|
|
231 // save some memory
|
|
232 $response = $hook['response'];
|
|
233 unset($hook['response']);
|
|
234
|
|
235 echo self::json_serialize($response, $this->devel_mode);
|
|
236 }
|
|
237
|
|
238 /**
|
|
239 * Return executable javascript code for all registered commands
|
|
240 */
|
|
241 protected function get_js_commands()
|
|
242 {
|
|
243 $out = '';
|
|
244
|
|
245 foreach ($this->commands as $i => $args) {
|
|
246 $method = array_shift($args);
|
|
247 foreach ($args as $i => $arg) {
|
|
248 $args[$i] = self::json_serialize($arg, $this->devel_mode);
|
|
249 }
|
|
250
|
|
251 $out .= sprintf(
|
|
252 "this.%s(%s);\n",
|
|
253 preg_replace('/^parent\./', '', $method),
|
|
254 implode(',', $args)
|
|
255 );
|
|
256 }
|
|
257
|
|
258 return $out;
|
|
259 }
|
|
260 }
|