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