source: github/program/include/rcube_json_output.php @ 1151581

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 1151581 was 1151581, checked in by alecpl <alec@…>, 3 years ago
  • Fix setting headers for IE in AJAX responses
  • Property mode set to 100644
File size: 6.7 KB
Line 
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$
20
21*/
22
23
24/**
25 * View class to produce JSON responses
26 *
27 * @package View
28 */
29class rcube_json_output
30{
31    private $config;
32    private $charset = RCMAIL_CHARSET;
33    private $env = array();
34    private $texts = array();
35    private $commands = array();
36    private $callbacks = array();
37    private $message = null;
38
39    public $browser;
40    public $type = 'js';
41    public $ajax_call = true;
42
43
44    /**
45     * Constructor
46     */
47    public function __construct($task=null)
48    {
49        $this->config  = rcmail::get_instance()->config;
50        $this->browser = new rcube_browser();
51    }
52
53
54    /**
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    }
64
65
66    /**
67     * Issue command to set page title
68     *
69     * @param string New page title
70     */
71    public function set_pagetitle($title)
72    {
73        $name = $this->config->get('product_name');
74        $this->command('set_pagetitle', empty($name) ? $title : $name.' :: '.$title);
75    }
76
77
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
110
111    /**
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    }
121
122
123    /**
124     * Call a client method
125     *
126     * @param string Method to call
127     * @param ... Additional arguments
128     */
129    public function command()
130    {
131        $cmd = func_get_args();
132       
133        if (strpos($cmd[0], 'plugin.') === 0)
134          $this->callbacks[] = $cmd;
135        else
136          $this->commands[] = $cmd;
137    }
138   
139   
140    /**
141     * Add a localized label to the client environment
142     */
143    public function add_label()
144    {
145        $args = func_get_args();
146        if (count($args) == 1 && is_array($args[0]))
147            $args = $args[0];
148       
149        foreach ($args as $name) {
150            $this->texts[$name] = rcube_label($name);
151        }
152    }
153
154
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
161     * @param boolean Override last set message
162     * @uses self::command()
163     */
164    public function show_message($message, $type='notice', $vars=null, $override=true)
165    {
166        if ($override || !$this->message) {
167            $this->message = $message;
168            $this->command(
169                'display_message',
170                rcube_label(array('name' => $message, 'vars' => $vars)),
171                $type
172            );
173        }
174    }
175
176
177    /**
178     * Delete all stored env variables and commands
179     */
180    public function reset()
181    {
182        $this->env = array();
183        $this->texts = array();
184        $this->commands = array();
185    }
186
187
188    /**
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     */
194    public function redirect($p = array(), $delay = 1)
195    {
196        $location = rcmail::get_instance()->url($p);
197        $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);");
198        exit;
199    }
200   
201   
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     */
220    public function remote_response($add='')
221    {
222        static $s_header_sent = false;
223
224        if (!$s_header_sent) {
225            $s_header_sent = true;
226            send_nocacheing_headers();
227            header('Content-Type: text/plain; charset=' . $this->get_charset());
228        }
229
230        // unset default env vars
231        unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
232
233        $rcmail = rcmail::get_instance();
234        $response = array('action' => $rcmail->action, 'unlock' => (bool)$_REQUEST['_unlock']);
235       
236        if (!empty($this->env))
237            $response['env'] = $this->env;
238         
239        if (!empty($this->texts))
240            $response['texts'] = $this->texts;
241
242        // send function calls
243        $response['exec'] = $this->get_js_commands() . $add;
244       
245        if (!empty($this->callbacks))
246            $response['callbacks'] = $this->callbacks;
247
248        echo json_serialize($response);
249    }
250
251
252    /**
253     * Return executable javascript code for all registered commands
254     *
255     * @return string $out
256     */
257    private function get_js_commands()
258    {
259        $out = '';
260
261        foreach ($this->commands as $i => $args) {
262            $method = array_shift($args);
263            foreach ($args as $i => $arg) {
264                $args[$i] = json_serialize($arg);
265            }
266
267            $out .= sprintf(
268                "this.%s(%s);\n",
269                preg_replace('/^parent\./', '', $method),
270                implode(',', $args)
271            );
272        }
273
274        return $out;
275    }
276}
Note: See TracBrowser for help on using the repository browser.