You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
272 lines
7.2 KiB
PHTML
272 lines
7.2 KiB
PHTML
17 years ago
|
<?php
|
||
|
|
||
|
/*
|
||
|
+-----------------------------------------------------------------------+
|
||
13 years ago
|
| program/include/rcube_output_json.php |
|
||
17 years ago
|
| |
|
||
14 years ago
|
| This file is part of the Roundcube Webmail client |
|
||
13 years ago
|
| Copyright (C) 2008-2012, The Roundcube Dev Team |
|
||
13 years ago
|
| |
|
||
|
| Licensed under the GNU General Public License version 3 or |
|
||
|
| any later version with exceptions for skins & plugins. |
|
||
|
| See the README file for a full license statement. |
|
||
17 years ago
|
| |
|
||
|
| PURPOSE: |
|
||
13 years ago
|
| Class to handle JSON (AJAX) output |
|
||
17 years ago
|
+-----------------------------------------------------------------------+
|
||
|
| Author: Thomas Bruederli <roundcube@gmail.com> |
|
||
13 years ago
|
| Author: Aleksander Machniak <alec@alec.pl> |
|
||
17 years ago
|
+-----------------------------------------------------------------------+
|
||
|
|
||
15 years ago
|
$Id$
|
||
17 years ago
|
|
||
|
*/
|
||
|
|
||
|
|
||
|
/**
|
||
|
* View class to produce JSON responses
|
||
|
*
|
||
|
* @package View
|
||
|
*/
|
||
13 years ago
|
class rcube_output_json extends rcube_output
|
||
17 years ago
|
{
|
||
13 years ago
|
protected $texts = array();
|
||
|
protected $commands = array();
|
||
|
protected $callbacks = array();
|
||
|
protected $message = null;
|
||
|
|
||
16 years ago
|
public $type = 'js';
|
||
17 years ago
|
public $ajax_call = true;
|
||
14 years ago
|
|
||
|
|
||
17 years ago
|
/**
|
||
|
* Set environment variable
|
||
|
*
|
||
14 years ago
|
* @param string $name Property name
|
||
|
* @param mixed $value Property value
|
||
17 years ago
|
*/
|
||
|
public function set_env($name, $value)
|
||
|
{
|
||
|
$this->env[$name] = $value;
|
||
|
}
|
||
15 years ago
|
|
||
|
|
||
17 years ago
|
/**
|
||
17 years ago
|
* Issue command to set page title
|
||
|
*
|
||
14 years ago
|
* @param string $title New page title
|
||
17 years ago
|
*/
|
||
|
public function set_pagetitle($title)
|
||
|
{
|
||
13 years ago
|
if ($this->config->get('devel_mode') && !empty($_SESSION['username']))
|
||
|
$name = $_SESSION['username'];
|
||
|
else
|
||
|
$name = $this->config->get('product_name');
|
||
|
|
||
15 years ago
|
$this->command('set_pagetitle', empty($name) ? $title : $name.' :: '.$title);
|
||
17 years ago
|
}
|
||
|
|
||
15 years ago
|
|
||
17 years ago
|
/**
|
||
|
* Register a template object handler
|
||
|
*
|
||
14 years ago
|
* @param string $obj Object name
|
||
|
* @param string $func Function name to call
|
||
17 years ago
|
*/
|
||
|
public function add_handler($obj, $func)
|
||
|
{
|
||
|
// ignore
|
||
|
}
|
||
|
|
||
15 years ago
|
|
||
17 years ago
|
/**
|
||
|
* Register a list of template object handlers
|
||
|
*
|
||
14 years ago
|
* @param array $arr Hash array with object=>handler pairs
|
||
17 years ago
|
*/
|
||
|
public function add_handlers($arr)
|
||
|
{
|
||
|
// ignore
|
||
|
}
|
||
15 years ago
|
|
||
|
|
||
17 years ago
|
/**
|
||
|
* Call a client method
|
||
|
*
|
||
|
* @param string Method to call
|
||
|
* @param ... Additional arguments
|
||
|
*/
|
||
|
public function command()
|
||
|
{
|
||
16 years ago
|
$cmd = func_get_args();
|
||
14 years ago
|
|
||
16 years ago
|
if (strpos($cmd[0], 'plugin.') === 0)
|
||
|
$this->callbacks[] = $cmd;
|
||
|
else
|
||
|
$this->commands[] = $cmd;
|
||
17 years ago
|
}
|
||
14 years ago
|
|
||
|
|
||
17 years ago
|
/**
|
||
|
* Add a localized label to the client environment
|
||
|
*/
|
||
|
public function add_label()
|
||
|
{
|
||
16 years ago
|
$args = func_get_args();
|
||
|
if (count($args) == 1 && is_array($args[0]))
|
||
|
$args = $args[0];
|
||
14 years ago
|
|
||
16 years ago
|
foreach ($args as $name) {
|
||
13 years ago
|
$this->texts[$name] = $this->app->gettext($name);
|
||
17 years ago
|
}
|
||
|
}
|
||
15 years ago
|
|
||
17 years ago
|
|
||
|
/**
|
||
|
* Invoke display_message command
|
||
|
*
|
||
14 years ago
|
* @param string $message Message to display
|
||
|
* @param string $type Message type [notice|confirm|error]
|
||
|
* @param array $vars Key-value pairs to be replaced in localized text
|
||
|
* @param boolean $override Override last set message
|
||
14 years ago
|
* @param int $timeout Message displaying time in seconds
|
||
17 years ago
|
* @uses self::command()
|
||
|
*/
|
||
14 years ago
|
public function show_message($message, $type='notice', $vars=null, $override=true, $timeout=0)
|
||
17 years ago
|
{
|
||
16 years ago
|
if ($override || !$this->message) {
|
||
13 years ago
|
if ($this->app->text_exists($message)) {
|
||
|
if (!empty($vars)) {
|
||
|
$vars = array_map(array('rcube_ui', 'Q'), $vars);
|
||
|
}
|
||
|
$msgtext = $this->app->gettext(array('name' => $message, 'vars' => $vars));
|
||
13 years ago
|
}
|
||
|
else
|
||
|
$msgtext = $message;
|
||
|
|
||
16 years ago
|
$this->message = $message;
|
||
14 years ago
|
$this->command('display_message', $msgtext, $type, $timeout * 1000);
|
||
16 years ago
|
}
|
||
17 years ago
|
}
|
||
15 years ago
|
|
||
|
|
||
17 years ago
|
/**
|
||
|
* Delete all stored env variables and commands
|
||
|
*/
|
||
17 years ago
|
public function reset()
|
||
17 years ago
|
{
|
||
13 years ago
|
parent::reset();
|
||
17 years ago
|
$this->texts = array();
|
||
|
$this->commands = array();
|
||
|
}
|
||
15 years ago
|
|
||
|
|
||
17 years ago
|
/**
|
||
|
* Redirect to a certain url
|
||
|
*
|
||
14 years ago
|
* @param mixed $p Either a string with the action or url parameters as key-value pairs
|
||
|
* @param int $delay Delay in seconds
|
||
17 years ago
|
* @see rcmail::url()
|
||
|
*/
|
||
17 years ago
|
public function redirect($p = array(), $delay = 1)
|
||
17 years ago
|
{
|
||
|
$location = rcmail::get_instance()->url($p);
|
||
13 years ago
|
$this->remote_response(sprintf("window.setTimeout(function(){ %s.redirect('%s',true); }, %d);", JS_OBJECT_NAME, $location, $delay));
|
||
17 years ago
|
exit;
|
||
|
}
|
||
14 years ago
|
|
||
|
|
||
17 years ago
|
/**
|
||
|
* Send an AJAX response to the client.
|
||
|
*/
|
||
|
public function send()
|
||
|
{
|
||
|
$this->remote_response();
|
||
|
exit;
|
||
|
}
|
||
14 years ago
|
|
||
|
|
||
13 years ago
|
/**
|
||
|
* Show error page and terminate script execution
|
||
|
*
|
||
|
* @param int $code Error code
|
||
|
* @param string $message Error message
|
||
|
*/
|
||
|
public function raise_error($code, $message)
|
||
|
{
|
||
|
$this->show_message("Application Error ($code): $message", 'error');
|
||
|
$this->remote_response();
|
||
|
exit;
|
||
|
}
|
||
|
|
||
|
|
||
17 years ago
|
/**
|
||
|
* Send an AJAX response with executable JS code
|
||
|
*
|
||
14 years ago
|
* @param string $add Additional JS code
|
||
17 years ago
|
* @param boolean True if output buffer should be flushed
|
||
|
* @return void
|
||
|
* @deprecated
|
||
|
*/
|
||
13 years ago
|
protected function remote_response($add='')
|
||
17 years ago
|
{
|
||
|
static $s_header_sent = false;
|
||
|
|
||
|
if (!$s_header_sent) {
|
||
|
$s_header_sent = true;
|
||
13 years ago
|
$this->nocacheing_headers();
|
||
16 years ago
|
header('Content-Type: text/plain; charset=' . $this->get_charset());
|
||
17 years ago
|
}
|
||
|
|
||
|
// unset default env vars
|
||
|
unset($this->env['task'], $this->env['action'], $this->env['comm_path']);
|
||
|
|
||
16 years ago
|
$rcmail = rcmail::get_instance();
|
||
14 years ago
|
$response['action'] = $rcmail->action;
|
||
|
|
||
13 years ago
|
if ($unlock = rcube_ui::get_input_value('_unlock', rcube_ui::INPUT_GPC)) {
|
||
14 years ago
|
$response['unlock'] = $unlock;
|
||
|
}
|
||
14 years ago
|
|
||
16 years ago
|
if (!empty($this->env))
|
||
15 years ago
|
$response['env'] = $this->env;
|
||
14 years ago
|
|
||
16 years ago
|
if (!empty($this->texts))
|
||
15 years ago
|
$response['texts'] = $this->texts;
|
||
16 years ago
|
|
||
16 years ago
|
// send function calls
|
||
16 years ago
|
$response['exec'] = $this->get_js_commands() . $add;
|
||
14 years ago
|
|
||
16 years ago
|
if (!empty($this->callbacks))
|
||
15 years ago
|
$response['callbacks'] = $this->callbacks;
|
||
17 years ago
|
|
||
13 years ago
|
echo self::json_serialize($response);
|
||
17 years ago
|
}
|
||
15 years ago
|
|
||
|
|
||
17 years ago
|
/**
|
||
|
* Return executable javascript code for all registered commands
|
||
|
*
|
||
|
* @return string $out
|
||
|
*/
|
||
13 years ago
|
protected function get_js_commands()
|
||
17 years ago
|
{
|
||
17 years ago
|
$out = '';
|
||
15 years ago
|
|
||
17 years ago
|
foreach ($this->commands as $i => $args) {
|
||
|
$method = array_shift($args);
|
||
|
foreach ($args as $i => $arg) {
|
||
13 years ago
|
$args[$i] = self::json_serialize($arg);
|
||
17 years ago
|
}
|
||
|
|
||
|
$out .= sprintf(
|
||
|
"this.%s(%s);\n",
|
||
|
preg_replace('/^parent\./', '', $method),
|
||
|
implode(',', $args)
|
||
|
);
|
||
|
}
|
||
17 years ago
|
|
||
17 years ago
|
return $out;
|
||
|
}
|
||
|
}
|