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