2010-06-01 14:38:41 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-03-06 08:32:51 +01:00
|
|
|
* EGroupware API: JSON - Contains functions and classes for doing JSON requests.
|
2010-06-01 14:38:41 +02:00
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
2010-06-16 08:44:19 +02:00
|
|
|
* @package api
|
2010-06-01 14:38:41 +02:00
|
|
|
* @subpackage ajax
|
2010-06-01 20:54:31 +02:00
|
|
|
* @author Andreas Stoeckel <as@stylite.de>
|
2010-06-01 16:20:55 +02:00
|
|
|
* @version $Id$
|
2010-06-01 14:38:41 +02:00
|
|
|
*/
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
2010-06-15 16:05:56 +02:00
|
|
|
* Class handling JSON requests to the server
|
2010-06-01 20:54:31 +02:00
|
|
|
*/
|
2010-06-01 14:38:41 +02:00
|
|
|
class egw_json_request
|
|
|
|
{
|
2011-06-11 23:27:15 +02:00
|
|
|
|
|
|
|
private static $_hadJSONRequest = false;
|
2011-08-26 11:34:18 +02:00
|
|
|
|
2014-02-22 16:51:14 +01:00
|
|
|
/**
|
|
|
|
* Check if JSON request running or (re)set JSON request flag
|
|
|
|
*
|
|
|
|
* Can be used to:
|
|
|
|
* - detect regular JSON request:
|
|
|
|
* egw_json_request::isJSONRequest()
|
|
|
|
* - switch regular JSON response handling off, which would send arbitrary output via response method "html".
|
|
|
|
* Neccessary if json.php is used to send arbitrary JSON data eg. nodes for foldertree!
|
|
|
|
* egw_json_request::isJSONRequest(false)
|
|
|
|
*
|
|
|
|
* @param boolean $set=null
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function isJSONRequest($set=null)
|
2011-06-11 23:27:15 +02:00
|
|
|
{
|
2014-02-22 16:51:14 +01:00
|
|
|
$ret = self::$_hadJSONRequest;
|
|
|
|
if (isset($set)) self::$_hadJSONRequest = $set;
|
|
|
|
return $ret;
|
2011-06-11 23:27:15 +02:00
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Parses the raw input data supplied with the input_data parameter and calls the menuaction
|
|
|
|
* passing all parameters supplied in the request to it.
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2011-09-13 19:27:02 +02:00
|
|
|
* Also handle queued requests (menuaction == 'home.queue') containing multiple requests
|
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @param string menuaction to call
|
|
|
|
* @param string $input_data is the RAW input data as it was received from the client
|
|
|
|
*/
|
2010-06-01 16:20:55 +02:00
|
|
|
public function parseRequest($menuaction, $input_data)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2011-06-11 23:27:15 +02:00
|
|
|
// Remember that we currently are in a JSON request - e.g. used in the redirect code
|
|
|
|
self::$_hadJSONRequest = true;
|
2011-08-26 11:34:18 +02:00
|
|
|
|
2011-09-13 19:27:02 +02:00
|
|
|
if (get_magic_quotes_gpc()) $input_data = stripslashes($input_data);
|
|
|
|
|
|
|
|
$json_data = json_decode($input_data,true);
|
2014-02-17 10:44:13 +01:00
|
|
|
if (is_array($json_data) && isset($json_data['request']) && isset($json_data['request']['parameters']) && is_array($json_data['request']['parameters']))
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2014-02-17 10:44:13 +01:00
|
|
|
//error_log(__METHOD__.__LINE__.array2string($json_data['request']).function_backtrace());
|
2011-09-13 19:27:02 +02:00
|
|
|
$parameters =& $json_data['request']['parameters'];
|
|
|
|
}
|
2010-06-01 16:20:55 +02:00
|
|
|
else
|
|
|
|
{
|
2011-09-13 19:27:02 +02:00
|
|
|
$parameters = array();
|
|
|
|
}
|
|
|
|
// do we have a single request or an array of queued requests
|
|
|
|
if ($menuaction == 'home.queue')
|
|
|
|
{
|
|
|
|
$responses = array();
|
|
|
|
$response = egw_json_response::get();
|
|
|
|
foreach($parameters as $uid => $data)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2011-09-13 19:27:02 +02:00
|
|
|
//error_log("$uid: menuaction=$data[menuaction], parameters=".array2string($data['parameters']));
|
2011-09-14 16:22:17 +02:00
|
|
|
$this->handleRequest($data['menuaction'], (array)$data['parameters']);
|
2011-09-13 19:27:02 +02:00
|
|
|
$responses[$uid] = $response->initResponseArray();
|
|
|
|
//error_log("responses[$uid]=".array2string($responses[$uid]));
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
2011-09-13 19:27:02 +02:00
|
|
|
$response->data($responses); // send all responses as data
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->handleRequest($menuaction, $parameters);
|
2010-06-11 13:41:12 +02:00
|
|
|
}
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Request handler
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @param string $menuaction
|
|
|
|
* @param array $parameters
|
|
|
|
*/
|
|
|
|
public function handleRequest($menuaction, array $parameters)
|
2010-06-01 16:20:55 +02:00
|
|
|
{
|
|
|
|
if (strpos($menuaction,'::') !== false && strpos($menuaction,'.') === false) // static method name app_something::method
|
|
|
|
{
|
|
|
|
@list($className,$functionName,$handler) = explode('::',$menuaction);
|
|
|
|
list($appName) = explode('_',$className);
|
2014-05-21 18:48:36 +02:00
|
|
|
|
|
|
|
// Check for a real static method, avoid instanciation if it is
|
|
|
|
$m = new ReflectionMethod($menuaction);
|
|
|
|
if($m->isStatic())
|
|
|
|
{
|
|
|
|
$ajaxClass = $className;
|
|
|
|
}
|
2010-06-01 16:20:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
@list($appName, $className, $functionName, $handler) = explode('.',$menuaction);
|
|
|
|
}
|
2010-06-02 20:11:15 +02:00
|
|
|
//error_log("json.php: appName=$appName, className=$className, functionName=$functionName, handler=$handler");
|
2010-06-01 16:20:55 +02:00
|
|
|
|
|
|
|
switch($handler)
|
|
|
|
{
|
2010-06-09 15:46:35 +02:00
|
|
|
case '/etemplate/process_exec':
|
2010-06-10 12:33:00 +02:00
|
|
|
$_GET['menuaction'] = $appName.'.'.$className.'.'.$functionName;
|
2010-06-01 16:20:55 +02:00
|
|
|
$appName = $className = 'etemplate';
|
|
|
|
$functionName = 'process_exec';
|
|
|
|
$menuaction = 'etemplate.etemplate.process_exec';
|
|
|
|
|
2010-06-09 15:46:35 +02:00
|
|
|
$parameters = array(
|
2010-06-10 12:33:00 +02:00
|
|
|
$parameters[0]['etemplate_exec_id'],
|
|
|
|
$parameters[0]['submit_button'],
|
|
|
|
$parameters[0],
|
2010-06-01 16:20:55 +02:00
|
|
|
'xajaxResponse',
|
|
|
|
);
|
|
|
|
//error_log("xajax_doXMLHTTP() /etemplate/process_exec handler: arg0='$menuaction', menuaction='$_GET[menuaction]'");
|
2010-06-09 15:46:35 +02:00
|
|
|
break;
|
2010-06-01 16:20:55 +02:00
|
|
|
case 'etemplate': // eg. ajax code in an eTemplate widget
|
|
|
|
$menuaction = ($appName = 'etemplate').'.'.$className.'.'.$functionName;
|
|
|
|
break;
|
2013-11-15 17:28:24 +01:00
|
|
|
case 'template': // calling current template / framework object
|
2010-06-01 16:20:55 +02:00
|
|
|
$menuaction = $appName.'.'.$className.'.'.$functionName;
|
2013-11-15 17:28:24 +01:00
|
|
|
$className = get_class($GLOBALS['egw']->framework);
|
2010-06-01 16:20:55 +02:00
|
|
|
list($template) = explode('_', $className);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(substr($className,0,4) != 'ajax' && substr($className,-4) != 'ajax' &&
|
|
|
|
$menuaction != 'etemplate.etemplate.process_exec' && substr($functionName,0,4) != 'ajax' ||
|
2011-07-26 15:49:53 +02:00
|
|
|
!preg_match('/^[A-Za-z0-9_-]+(\.[A-Za-z0-9_]+\.|::)[A-Za-z0-9_]+$/',$menuaction))
|
2010-06-01 16:20:55 +02:00
|
|
|
{
|
|
|
|
// stopped for security reasons
|
|
|
|
error_log($_SERVER['PHP_SELF']. ' stopped for security reason. '.$menuaction.' is not valid. class- or function-name must start with ajax!!!');
|
|
|
|
// send message also to the user
|
|
|
|
throw new Exception($_SERVER['PHP_SELF']. ' stopped for security reason. '.$menuaction.' is not valid. class- or function-name must start with ajax!!!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($template))
|
|
|
|
{
|
2013-11-15 17:28:24 +01:00
|
|
|
$ajaxClass = $GLOBALS['egw']->framework;
|
2010-06-01 16:20:55 +02:00
|
|
|
}
|
2014-05-21 18:48:36 +02:00
|
|
|
else if (!$ajaxClass)
|
2010-06-01 16:20:55 +02:00
|
|
|
{
|
2010-06-15 16:05:56 +02:00
|
|
|
$ajaxClass = CreateObject($appName.'.'.$className);
|
2010-06-01 16:20:55 +02:00
|
|
|
}
|
2010-06-15 16:05:56 +02:00
|
|
|
|
2011-08-26 11:34:18 +02:00
|
|
|
// for Ajax: no need to load the "standard" javascript files,
|
2010-06-24 13:33:51 +02:00
|
|
|
// they are already loaded, in fact jquery has a problem if loaded twice
|
|
|
|
egw_framework::js_files(array());
|
|
|
|
|
2010-06-01 16:20:55 +02:00
|
|
|
$parameters = translation::convert($parameters, 'utf-8');
|
|
|
|
|
|
|
|
call_user_func_array(array($ajaxClass, $functionName), $parameters);
|
|
|
|
}
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Class used to send ajax responses
|
|
|
|
*/
|
2010-06-01 14:38:41 +02:00
|
|
|
class egw_json_response
|
|
|
|
{
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
2010-06-15 16:05:56 +02:00
|
|
|
* A response can only contain one generic data part.
|
2010-06-01 20:54:31 +02:00
|
|
|
* This variable is used to store, whether a data part had already been added to the response.
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @var boolean
|
|
|
|
*/
|
2010-06-01 14:38:41 +02:00
|
|
|
private $hasData = false;
|
|
|
|
|
2010-06-30 11:13:17 +02:00
|
|
|
/**
|
|
|
|
* Array containing all beforeSendData callbacks
|
|
|
|
*/
|
|
|
|
protected $beforeSendDataProcs = array();
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
2010-06-15 16:05:56 +02:00
|
|
|
* Holds the actual response data which is then encoded to JSON
|
2010-06-01 20:54:31 +02:00
|
|
|
* once the "getJSON" function is called
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
2010-06-01 14:38:41 +02:00
|
|
|
protected $responseArray = array();
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Holding instance of class for singelton egw_json_response::get()
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @var egw_json_response
|
|
|
|
*/
|
2010-06-01 16:20:55 +02:00
|
|
|
private static $response = null;
|
|
|
|
|
2013-08-25 14:42:55 +02:00
|
|
|
/**
|
|
|
|
* Force use of singleton: $response = egw_json_response::get();
|
|
|
|
*/
|
|
|
|
private function __construct()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Singelton for class
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @return egw_json_response
|
|
|
|
*/
|
2010-06-01 16:20:55 +02:00
|
|
|
public static function get()
|
|
|
|
{
|
|
|
|
if (!isset(self::$response))
|
|
|
|
{
|
|
|
|
self::$response = new egw_json_response();
|
2012-03-06 08:32:51 +01:00
|
|
|
self::sendHeader();
|
2010-06-01 16:20:55 +02:00
|
|
|
}
|
|
|
|
return self::$response;
|
|
|
|
}
|
2011-08-26 11:34:18 +02:00
|
|
|
|
2011-06-11 23:27:15 +02:00
|
|
|
public static function isJSONResponse()
|
|
|
|
{
|
|
|
|
return isset(self::$response);
|
|
|
|
}
|
2010-06-01 16:20:55 +02:00
|
|
|
|
2012-03-06 08:32:51 +01:00
|
|
|
/**
|
|
|
|
* Do we have a JSON response to send back
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function haveJSONResponse()
|
|
|
|
{
|
2014-02-21 16:38:38 +01:00
|
|
|
return $this->responseArray || $this->beforeSendDataProcs;
|
2012-03-06 08:32:51 +01:00
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Private function used to send the HTTP header of the JSON response
|
|
|
|
*/
|
2010-06-01 16:20:55 +02:00
|
|
|
private function sendHeader()
|
|
|
|
{
|
2014-02-21 16:38:38 +01:00
|
|
|
$file = $line = null;
|
2012-03-06 08:32:51 +01:00
|
|
|
if (headers_sent($file, $line))
|
|
|
|
{
|
|
|
|
error_log(__METHOD__."() header already sent by $file line $line: ".function_backtrace());
|
|
|
|
}
|
|
|
|
else
|
2014-02-21 16:38:38 +01:00
|
|
|
{
|
|
|
|
//Send the character encoding header
|
|
|
|
header('content-type: application/json; charset='.translation::charset());
|
|
|
|
}
|
2010-06-01 16:20:55 +02:00
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Private function which is used to send the result via HTTP
|
|
|
|
*/
|
2014-02-21 16:38:38 +01:00
|
|
|
public static function sendResult()
|
2010-06-01 16:20:55 +02:00
|
|
|
{
|
2010-06-23 10:33:23 +02:00
|
|
|
$inst = self::get();
|
2010-06-16 08:44:19 +02:00
|
|
|
|
2010-06-30 11:13:17 +02:00
|
|
|
//Call each attached before send data proc
|
|
|
|
foreach ($inst->beforeSendDataProcs as $proc)
|
2012-03-06 08:32:51 +01:00
|
|
|
{
|
2010-06-30 11:13:17 +02:00
|
|
|
call_user_func_array($proc['proc'], $proc['params']);
|
2012-03-06 08:32:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if application made some direct output
|
|
|
|
if (($output = ob_get_clean()))
|
|
|
|
{
|
|
|
|
if (!$inst->haveJSONResponse())
|
|
|
|
{
|
2014-02-22 16:51:14 +01:00
|
|
|
error_log(__METHOD__."() adding output with inst->addGeneric('html', '$output')");
|
2012-03-06 10:50:43 +01:00
|
|
|
$inst->addGeneric('html', $output);
|
2012-03-06 08:32:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$inst->alert('Application echoed something', $output);
|
|
|
|
}
|
|
|
|
}
|
2010-06-30 11:13:17 +02:00
|
|
|
|
2013-08-01 14:28:28 +02:00
|
|
|
echo $inst->getJSON();
|
|
|
|
$inst->initResponseArray();
|
2010-06-01 16:20:55 +02:00
|
|
|
}
|
2010-06-15 16:05:56 +02:00
|
|
|
|
2014-07-17 19:26:00 +02:00
|
|
|
/**
|
|
|
|
* Return json response data, after running beforeSendDataProcs
|
|
|
|
*
|
|
|
|
* Used to send json response with etemplate data in GET request
|
|
|
|
*
|
|
|
|
* @return array responseArray
|
|
|
|
*/
|
|
|
|
public static function returnResult()
|
|
|
|
{
|
|
|
|
$inst = self::get();
|
|
|
|
|
|
|
|
//Call each attached before send data proc
|
|
|
|
foreach ($inst->beforeSendDataProcs as $proc)
|
|
|
|
{
|
|
|
|
call_user_func_array($proc['proc'], $proc['params']);
|
|
|
|
}
|
|
|
|
return $inst->initResponseArray();
|
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* xAjax compatibility function
|
|
|
|
*/
|
|
|
|
public function printOutput()
|
|
|
|
{
|
2014-02-21 16:38:38 +01:00
|
|
|
// do nothing, as output is triggered by egw::__destruct()
|
2010-06-01 20:54:31 +02:00
|
|
|
}
|
2010-06-01 16:20:55 +02:00
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Adds any type of data to the response array
|
|
|
|
*/
|
2010-06-01 14:38:41 +02:00
|
|
|
protected function addGeneric($key, $data)
|
|
|
|
{
|
2010-06-23 10:33:23 +02:00
|
|
|
self::get()->responseArray[] = array(
|
2010-06-01 20:54:31 +02:00
|
|
|
'type' => $key,
|
|
|
|
'data' => $data,
|
|
|
|
);
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
|
2011-09-13 19:27:02 +02:00
|
|
|
/**
|
|
|
|
* Init responseArray
|
|
|
|
*
|
|
|
|
* @param array $arr
|
|
|
|
* @return array previous content
|
|
|
|
*/
|
|
|
|
public function initResponseArray()
|
|
|
|
{
|
|
|
|
$return = $this->responseArray;
|
2014-02-21 16:38:38 +01:00
|
|
|
$this->responseArray = $this->beforeSendDataProcs = array();
|
2011-09-13 19:27:02 +02:00
|
|
|
$this->hasData = false;
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
2010-06-15 16:05:56 +02:00
|
|
|
* Adds a "data" response to the json response.
|
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* This function may only be called once for a single JSON response object.
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @param object|array|string $data can be of any data type and will be added JSON Encoded to your response.
|
|
|
|
*/
|
2010-06-01 16:20:55 +02:00
|
|
|
public function data($data)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
|
|
|
/* Only allow adding the data response once */
|
2010-06-23 10:33:23 +02:00
|
|
|
$inst = self::get();
|
|
|
|
if (!$inst->hasData)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2010-06-23 10:33:23 +02:00
|
|
|
$inst->addGeneric('data', $data);
|
|
|
|
$inst->hasData = true;
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Adding more than one data response to a JSON response is not allowed.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
2010-06-15 16:05:56 +02:00
|
|
|
* Adds an "alert" to the response which can be handeled on the client side.
|
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* The default implementation simply displays the text supplied here with the JavaScript function "alert".
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @param string $message contains the actual message being sent to the client.
|
|
|
|
* @param string $details (optional) can be used to inform the user on the client side about additional details about the error. This might be information how the error can be resolved/why it was raised or simply some debug data.
|
|
|
|
*/
|
2010-06-01 16:20:55 +02:00
|
|
|
public function alert($message, $details = '')
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
|
|
|
if (is_string($message) && is_string($details))
|
|
|
|
{
|
|
|
|
$this->addGeneric('alert', array(
|
|
|
|
"message" => $message,
|
|
|
|
"details" => $details));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameters supplied.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Allows to add a generic java script to the response which will be executed upon the request gets received.
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @deprecated
|
|
|
|
* @param string $script the script code which should be executed upon receiving
|
2010-06-15 16:05:56 +02:00
|
|
|
*/
|
2010-06-01 16:20:55 +02:00
|
|
|
public function script($script)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
|
|
|
if (is_string($script))
|
|
|
|
{
|
|
|
|
$this->addGeneric('script', $script);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameters supplied.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-09 16:41:40 +02:00
|
|
|
/**
|
2011-09-13 19:27:02 +02:00
|
|
|
* Allows to call a global javascript function with given parameters: window[$func].apply(window, $parameters)
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2011-09-13 19:27:02 +02:00
|
|
|
* @param string $func name of the global (window) javascript function to call
|
|
|
|
* @param array $parameters=array()
|
|
|
|
*/
|
|
|
|
public function apply($function,array $parameters=array())
|
|
|
|
{
|
|
|
|
if (is_string($function))
|
|
|
|
{
|
|
|
|
$this->addGeneric('apply', array(
|
|
|
|
'func' => $function,
|
|
|
|
'parms' => $parameters,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameters supplied.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows to call a global javascript function with given parameters: window[$func].call(window[, $param1[, ...]])
|
|
|
|
*
|
|
|
|
* @param string $func name of the global (window) javascript function to call
|
|
|
|
* @param mixed $parameters variable number of parameters
|
|
|
|
*/
|
|
|
|
public function call($function)
|
|
|
|
{
|
|
|
|
$parameters = func_get_args();
|
|
|
|
array_shift($parameters); // shift off $function
|
|
|
|
|
|
|
|
if (is_string($function))
|
|
|
|
{
|
|
|
|
$this->addGeneric('apply', array(
|
|
|
|
'func' => $function,
|
|
|
|
'parms' => $parameters,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameters supplied.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows to call a jquery function on a selector with given parameters: $j($selector).$func($parmeters)
|
|
|
|
*
|
|
|
|
* @param string $selector jquery selector
|
|
|
|
* @param string $method name of the jquery to call
|
|
|
|
* @param array $parameters=array()
|
2010-06-15 16:05:56 +02:00
|
|
|
*/
|
2010-06-10 14:24:50 +02:00
|
|
|
public function jquery($selector,$method,array $parameters=array())
|
2010-06-09 16:41:40 +02:00
|
|
|
{
|
|
|
|
if (is_string($selector) && is_string($method))
|
|
|
|
{
|
2010-06-10 14:24:50 +02:00
|
|
|
$this->addGeneric('jquery', array(
|
2010-06-09 16:41:40 +02:00
|
|
|
'select' => $selector,
|
|
|
|
'func' => $method,
|
2010-06-10 14:24:50 +02:00
|
|
|
'parms' => $parameters,
|
2010-06-09 16:41:40 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameters supplied.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 14:03:13 +02:00
|
|
|
public function generic($type, array $parameters = array())
|
|
|
|
{
|
|
|
|
if (is_string($type))
|
|
|
|
{
|
|
|
|
$this->addGeneric($type, $parameters);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameters supplied.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Adds an html assign to the response, which is excecuted upon the request is received.
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @param string $id id of dom element to modify
|
|
|
|
* @param string $key attribute name of dom element which should be modified
|
|
|
|
* @param string $value the value which should be assigned to the given attribute
|
|
|
|
*/
|
2010-06-01 16:20:55 +02:00
|
|
|
public function assign($id, $key, $value)
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
2010-07-21 15:16:36 +02:00
|
|
|
if (is_string($id) && is_string($key) && (is_string($value) || is_numeric($value) || is_null($value)))
|
2010-06-01 14:38:41 +02:00
|
|
|
{
|
|
|
|
$this->addGeneric('assign', array(
|
2010-06-01 20:54:31 +02:00
|
|
|
'id' => $id,
|
|
|
|
'key' => $key,
|
|
|
|
'value' => $value,
|
|
|
|
));
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Exception("Invalid parameters supplied");
|
|
|
|
}
|
|
|
|
}
|
2010-06-15 16:05:56 +02:00
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Redirect to given url
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @param string $url
|
2010-06-18 10:36:33 +02:00
|
|
|
* @param boolean $global specifies whether to redirect the whole framework
|
2014-01-27 17:28:39 +01:00
|
|
|
* @param string $app=null default current app from flags
|
2010-06-18 10:36:33 +02:00
|
|
|
* or only the current application
|
2010-06-01 20:54:31 +02:00
|
|
|
*/
|
2014-01-27 17:28:39 +01:00
|
|
|
public function redirect($url, $global = false, $app=null)
|
2010-06-01 20:54:31 +02:00
|
|
|
{
|
2010-06-11 10:08:07 +02:00
|
|
|
if (is_string($url) && is_bool($global))
|
|
|
|
{
|
|
|
|
//self::script("location.href = '$url';");
|
|
|
|
$this->addGeneric('redirect', array(
|
|
|
|
'url' => $url,
|
|
|
|
'global' => $global,
|
2014-01-27 17:28:39 +01:00
|
|
|
'app' => $app ? $app : $GLOBALS['egw_info']['flags']['currentapp'],
|
2010-06-11 10:08:07 +02:00
|
|
|
));
|
|
|
|
}
|
2010-06-01 20:54:31 +02:00
|
|
|
}
|
2010-06-01 14:38:41 +02:00
|
|
|
|
2010-06-21 10:39:21 +02:00
|
|
|
/**
|
|
|
|
* Displays an error message on the client
|
|
|
|
*/
|
|
|
|
public function error($msg)
|
|
|
|
{
|
|
|
|
if (is_string($msg))
|
|
|
|
{
|
|
|
|
$this->addGeneric('error', $msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-18 10:36:33 +02:00
|
|
|
/**
|
|
|
|
* Includes the given CSS file. Every url can only be included once.
|
|
|
|
*
|
|
|
|
* @param string $url specifies the url to the css file to include
|
|
|
|
*/
|
|
|
|
public function includeCSS($url)
|
|
|
|
{
|
|
|
|
if (is_string($url))
|
|
|
|
{
|
|
|
|
$this->addGeneric('css', $url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Includes the given JS file. Every url can only be included once.
|
|
|
|
*
|
|
|
|
* @param string $url specifies the url to the css file to include
|
|
|
|
*/
|
|
|
|
public function includeScript($url)
|
|
|
|
{
|
|
|
|
if (is_string($url))
|
|
|
|
{
|
2010-06-23 10:33:23 +02:00
|
|
|
self::get()->addGeneric('js', $url);
|
2010-06-18 10:36:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
|
|
|
* Returns the actual JSON code generated by calling the above "add" function.
|
2010-06-15 16:05:56 +02:00
|
|
|
*
|
2010-06-01 20:54:31 +02:00
|
|
|
* @return string
|
2010-06-15 16:05:56 +02:00
|
|
|
*/
|
2010-06-01 14:38:41 +02:00
|
|
|
public function getJSON()
|
|
|
|
{
|
2010-06-23 10:33:23 +02:00
|
|
|
$inst = self::get();
|
|
|
|
|
2010-06-01 14:38:41 +02:00
|
|
|
/* Wrap the result array into a parent "response" Object */
|
2010-06-23 10:33:23 +02:00
|
|
|
$res = array('response' => $inst->responseArray);
|
2010-06-15 16:05:56 +02:00
|
|
|
|
2010-06-02 20:11:15 +02:00
|
|
|
return json_encode($res); //PHP5.3+, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP);
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
2010-06-01 16:20:55 +02:00
|
|
|
|
2013-08-01 14:28:28 +02:00
|
|
|
public function json_encode($var)
|
|
|
|
{
|
|
|
|
$ret = json_encode($var);
|
|
|
|
|
|
|
|
if (($err = json_last_error()))
|
|
|
|
{
|
|
|
|
static $json_err2str = array(
|
|
|
|
JSON_ERROR_NONE => 'No errors',
|
|
|
|
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
|
|
|
|
JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch',
|
|
|
|
JSON_ERROR_CTRL_CHAR => 'Unexpected control character found',
|
|
|
|
JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON',
|
|
|
|
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded',
|
|
|
|
);
|
|
|
|
error_log(__METHOD__.'('.array2string($var).') json_last_error()='.$err.'='.$json_err2str[$err]);
|
|
|
|
}
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2010-06-30 11:13:17 +02:00
|
|
|
/**
|
|
|
|
* Function which can be used to add an event listener callback function to
|
|
|
|
* the "beforeSendData" callback. This callback might be used to add a response
|
|
|
|
* which always has to be added after all other responses.
|
|
|
|
* @param callback Callback function or method which should be called before the response gets sent
|
|
|
|
* @param mixed n Optional parameters which get passed to the callback function.
|
|
|
|
*/
|
|
|
|
public function addBeforeSendDataCallback($proc)
|
|
|
|
{
|
|
|
|
//Get the current instance
|
|
|
|
$inst = self::get();
|
|
|
|
|
|
|
|
//Get all parameters passed to the function and delete the first one
|
|
|
|
$params = func_get_args();
|
|
|
|
array_shift($params);
|
|
|
|
|
|
|
|
$inst->beforeSendDataProcs[] = array(
|
|
|
|
'proc' => $proc,
|
|
|
|
'params' => $params
|
|
|
|
);
|
|
|
|
}
|
2010-06-01 14:38:41 +02:00
|
|
|
}
|
2010-06-09 15:46:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated legacy xajax wrapper functions for the new egw_json interface
|
|
|
|
*/
|
2013-08-25 17:14:19 +02:00
|
|
|
class xajaxResponse
|
2010-06-09 15:46:35 +02:00
|
|
|
{
|
2013-08-25 17:14:19 +02:00
|
|
|
public function __call($name, $args)
|
2010-06-09 15:46:35 +02:00
|
|
|
{
|
2013-08-25 17:14:19 +02:00
|
|
|
if (substr($name, 0, 3) == 'add')
|
|
|
|
{
|
|
|
|
$name = substr($name, 3);
|
|
|
|
$name[0] = strtolower($name[0]);
|
|
|
|
}
|
|
|
|
return call_user_func_array(array(egw_json_response::get(), $name), $args);
|
2010-06-09 15:46:35 +02:00
|
|
|
}
|
|
|
|
|
2010-06-11 13:41:12 +02:00
|
|
|
public function addScriptCall($func)
|
|
|
|
{
|
|
|
|
$args = func_get_args();
|
|
|
|
$func = array_shift($args);
|
|
|
|
|
2013-08-25 17:14:19 +02:00
|
|
|
return call_user_func(array(egw_json_response::get(), 'apply'), $func, $args);
|
2010-06-11 13:41:12 +02:00
|
|
|
}
|
|
|
|
|
2010-06-09 15:46:35 +02:00
|
|
|
public function getXML()
|
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
2010-06-10 08:59:54 +02:00
|
|
|
}
|