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