source: github/program/include/rcube_json_output.php @ 2eb7943

HEADcourier-fixdev-browser-capabilitiespdorelease-0.6release-0.7release-0.8
Last change on this file since 2eb7943 was 2eb7943, checked in by alecpl <alec@…>, 3 years ago
  • code cleanup (mostly identation fixes)
  • Property mode set to 100644
File size: 6.6 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 $type = 'js';
40    public $ajax_call = true;
41   
42   
43    /**
44     * Constructor
45     */
46    public function __construct($task)
47    {
48        $this->config = rcmail::get_instance()->config;
49    }
50
51
52    /**
53     * Set environment variable
54     *
55     * @param string Property name
56     * @param mixed Property value
57     */
58    public function set_env($name, $value)
59    {
60        $this->env[$name] = $value;
61    }
62
63
64    /**
65     * Issue command to set page title
66     *
67     * @param string New page title
68     */
69    public function set_pagetitle($title)
70    {
71        $name = $this->config->get('product_name');
72        $this->command('set_pagetitle', empty($name) ? $title : $name.' :: '.$title);
73    }
74
75
76    /**
77     * @ignore
78     */
79    function set_charset($charset)
80    {
81        // ignore: $this->charset = $charset;
82    }
83
84
85    /**
86     * Get charset for output
87     *
88     * @return string Output charset
89     */
90    function get_charset()
91    {
92        return $this->charset;
93    }
94
95
96    /**
97     * Register a template object handler
98     *
99     * @param  string Object name
100     * @param  string Function name to call
101     * @return void
102     */
103    public function add_handler($obj, $func)
104    {
105        // ignore
106    }
107
108
109    /**
110     * Register a list of template object handlers
111     *
112     * @param  array Hash array with object=>handler pairs
113     * @return void
114     */
115    public function add_handlers($arr)
116    {
117        // ignore
118    }
119
120
121    /**
122     * Call a client method
123     *
124     * @param string Method to call
125     * @param ... Additional arguments
126     */
127    public function command()
128    {
129        $cmd = func_get_args();
130       
131        if (strpos($cmd[0], 'plugin.') === 0)
132          $this->callbacks[] = $cmd;
133        else
134          $this->commands[] = $cmd;
135    }
136   
137   
138    /**
139     * Add a localized label to the client environment
140     */
141    public function add_label()
142    {
143        $args = func_get_args();
144        if (count($args) == 1 && is_array($args[0]))
145            $args = $args[0];
146       
147        foreach ($args as $name) {
148            $this->texts[$name] = rcube_label($name);
149        }
150    }
151
152
153    /**
154     * Invoke display_message command
155     *
156     * @param string Message to display
157     * @param string Message type [notice|confirm|error]
158     * @param array Key-value pairs to be replaced in localized text
159     * @param boolean Override last set message
160     * @uses self::command()
161     */
162    public function show_message($message, $type='notice', $vars=null, $override=true)
163    {
164        if ($override || !$this->message) {
165            $this->message = $message;
166            $this->command(
167                'display_message',
168                rcube_label(array('name' => $message, 'vars' => $vars)),
169                $type
170            );
171        }
172    }
173
174
175    /**
176     * Delete all stored env variables and commands
177     */
178    public function reset()
179    {
180        $this->env = array();
181        $this->texts = array();
182        $this->commands = array();
183    }
184
185
186    /**
187     * Redirect to a certain url
188     *
189     * @param mixed Either a string with the action or url parameters as key-value pairs
190     * @see rcmail::url()
191     */
192    public function redirect($p = array(), $delay = 1)
193    {
194        $location = rcmail::get_instance()->url($p);
195        $this->remote_response("window.setTimeout(\"location.href='{$location}'\", $delay);");
196        exit;
197    }
198   
199   
200    /**
201     * Send an AJAX response to the client.
202     */
203    public function send()
204    {
205        $this->remote_response();
206        exit;
207    }
208   
209   
210    /**
211     * Send an AJAX response with executable JS code
212     *
213     * @param  string  Additional JS code
214     * @param  boolean True if output buffer should be flushed
215     * @return void
216     * @deprecated
217     */
218    public function remote_response($add='')
219    {
220        static $s_header_sent = false;
221
222        if (!$s_header_sent) {
223            $s_header_sent = true;
224            send_nocacheing_headers();
225            header('Content-Type: text/plain; charset=' . $this->get_charset());
226        }
227
228        // unset default env vars
229        unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
230
231        $rcmail = rcmail::get_instance();
232        $response = array('action' => $rcmail->action, 'unlock' => (bool)$_REQUEST['_unlock']);
233       
234        if (!empty($this->env))
235            $response['env'] = $this->env;
236         
237        if (!empty($this->texts))
238            $response['texts'] = $this->texts;
239
240        // send function calls
241        $response['exec'] = $this->get_js_commands() . $add;
242       
243        if (!empty($this->callbacks))
244            $response['callbacks'] = $this->callbacks;
245
246        echo json_serialize($response);
247    }
248
249
250    /**
251     * Return executable javascript code for all registered commands
252     *
253     * @return string $out
254     */
255    private function get_js_commands()
256    {
257        $out = '';
258
259        foreach ($this->commands as $i => $args) {
260            $method = array_shift($args);
261            foreach ($args as $i => $arg) {
262                $args[$i] = json_serialize($arg);
263            }
264
265            $out .= sprintf(
266                "this.%s(%s);\n",
267                preg_replace('/^parent\./', '', $method),
268                implode(',', $args)
269            );
270        }
271
272        return $out;
273    }
274}
Note: See TracBrowser for help on using the repository browser.