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