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