2010-06-01 16:20:55 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2014-02-25 07:51:56 +01:00
|
|
|
* EGroupware - general JSON handler for EGroupware
|
2010-06-01 16:20:55 +02:00
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @subpackage ajax
|
2010-06-01 20:54:31 +02:00
|
|
|
* @author Andreas Stoeckel <as@stylite.de>
|
2010-06-01 16:20:55 +02:00
|
|
|
*/
|
|
|
|
|
2016-04-27 12:34:57 +02:00
|
|
|
use EGroupware\Api;
|
2016-05-01 11:57:48 +02:00
|
|
|
use EGroupware\Api\Egw;
|
2016-03-20 14:02:55 +01:00
|
|
|
use EGroupware\Api\Json;
|
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
/**
|
2016-04-27 12:34:57 +02:00
|
|
|
* callback if the session-check fails, redirects to login.php, if no valid basic auth credentials given
|
2010-06-01 20:54:31 +02:00
|
|
|
*
|
|
|
|
* @param array &$anon_account anon account_info with keys 'login', 'passwd' and optional 'passwd_type'
|
2014-02-25 07:51:56 +01:00
|
|
|
* @return boolean|string true if we allow anon access and anon_account is set, a sessionid or false otherwise
|
2010-06-01 20:54:31 +02:00
|
|
|
*/
|
2014-02-25 07:51:56 +01:00
|
|
|
function login_redirect(&$anon_account)
|
2010-06-01 20:54:31 +02:00
|
|
|
{
|
2016-04-27 12:34:57 +02:00
|
|
|
// allow to make json calls via basic auth
|
|
|
|
if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW']) &&
|
|
|
|
($session_id = Api\Header\Authenticate::autocreate_session_callback($anon_account)))
|
|
|
|
{
|
|
|
|
return $session_id;
|
|
|
|
}
|
2016-05-01 11:57:48 +02:00
|
|
|
Json\Request::isJSONRequest(true); // because Api\Json\Request::parseRequest() is not (yet) called
|
2016-03-20 14:02:55 +01:00
|
|
|
$response = Json\Response::get();
|
2019-10-18 14:17:27 +02:00
|
|
|
$response->apply('framework.callOnLogout');
|
2010-06-11 13:41:12 +02:00
|
|
|
$response->redirect($GLOBALS['egw_info']['server']['webserver_url'].'/login.php?cd=10', true);
|
2010-06-01 20:54:31 +02:00
|
|
|
|
2021-04-01 08:22:27 +02:00
|
|
|
// under PHP 8 the destructor is called to late and the response is not send
|
|
|
|
Json\Response::sendResult();
|
2016-05-01 11:57:48 +02:00
|
|
|
exit();
|
2010-06-01 20:54:31 +02:00
|
|
|
}
|
2010-06-01 16:20:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Exception handler for xajax, return the message (and trace, if enabled) as alert() to the user
|
|
|
|
*
|
|
|
|
* Does NOT return!
|
|
|
|
*
|
2016-02-16 00:17:19 +01:00
|
|
|
* @param Exception|Error $e
|
2010-06-01 16:20:55 +02:00
|
|
|
*/
|
2016-02-16 00:17:19 +01:00
|
|
|
function ajax_exception_handler($e)
|
2010-06-01 16:20:55 +02:00
|
|
|
{
|
2014-10-15 16:55:08 +02:00
|
|
|
// handle redirects without logging
|
2016-10-31 18:30:45 +01:00
|
|
|
if (is_a($e, 'EGroupware\\Api\\Exception\\Redirect'))
|
2014-10-15 16:55:08 +02:00
|
|
|
{
|
2016-05-01 11:57:48 +02:00
|
|
|
Egw::redirect($e->url, $e->app);
|
2014-10-15 16:55:08 +02:00
|
|
|
}
|
2010-06-01 20:54:31 +02:00
|
|
|
// logging all exceptions to the error_log
|
2014-02-25 07:51:56 +01:00
|
|
|
$message = null;
|
2010-06-01 20:54:31 +02:00
|
|
|
if (function_exists('_egw_log_exception'))
|
|
|
|
{
|
|
|
|
_egw_log_exception($e,$message);
|
|
|
|
}
|
2016-03-20 14:02:55 +01:00
|
|
|
$response = Json\Response::get();
|
2010-06-01 20:54:31 +02:00
|
|
|
$message .= ($message ? "\n\n" : '').$e->getMessage();
|
2011-03-14 18:50:22 +01:00
|
|
|
|
2010-06-01 20:54:31 +02:00
|
|
|
// only show trace (incl. function arguments) if explicitly enabled, eg. on a development system
|
|
|
|
if ($GLOBALS['egw_info']['server']['exception_show_trace'])
|
|
|
|
{
|
|
|
|
$message .= "\n\n".$e->getTraceAsString();
|
|
|
|
}
|
2010-06-08 11:34:49 +02:00
|
|
|
$response->alert($message);
|
2010-06-01 20:54:31 +02:00
|
|
|
|
2021-04-01 08:22:27 +02:00
|
|
|
// under PHP 8 the destructor is called to late and the response is not send
|
|
|
|
Json\Response::sendResult();
|
2010-06-01 20:54:31 +02:00
|
|
|
exit;
|
2010-06-01 16:20:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// set our own exception handler, to not get the html from eGW's default one
|
2014-10-30 16:33:00 +01:00
|
|
|
set_exception_handler('ajax_exception_handler');
|
2010-06-01 16:20:55 +02:00
|
|
|
|
2018-11-01 11:59:31 +01:00
|
|
|
try {
|
|
|
|
if (!isset($_GET['menuaction']))
|
|
|
|
{
|
|
|
|
throw new InvalidArgumentException('Missing menuaction GET parameter', 998);
|
|
|
|
}
|
2010-06-01 16:20:55 +02:00
|
|
|
if (strpos($_GET['menuaction'],'::') !== false && strpos($_GET['menuaction'],'.') === false) // static method name app_something::method
|
|
|
|
{
|
|
|
|
@list($className,$functionName,$handler) = explode('::',$_GET['menuaction']);
|
2016-03-19 16:24:36 +01:00
|
|
|
|
|
|
|
if (substr($className, 0, 11) == 'EGroupware\\')
|
|
|
|
{
|
|
|
|
list(,$appName) = explode('\\', strtolower($className));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
list($appName) = explode('_',$className);
|
|
|
|
}
|
2010-06-01 16:20:55 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
@list($appName, $className, $functionName, $handler) = explode('.',$_GET['menuaction']);
|
|
|
|
}
|
2011-03-14 18:50:22 +01:00
|
|
|
//error_log("json.php: appName=$appName, className=$className, functionName=$functionName, handler=$handler");
|
2010-06-01 16:20:55 +02:00
|
|
|
|
|
|
|
$GLOBALS['egw_info'] = array(
|
|
|
|
'flags' => array(
|
|
|
|
'currentapp' => $appName,
|
|
|
|
'noheader' => True,
|
|
|
|
'disable_Template_class' => True,
|
2014-02-25 07:51:56 +01:00
|
|
|
'autocreate_session_callback' => 'login_redirect',
|
2010-06-01 16:20:55 +02:00
|
|
|
'no_exception_handler' => true, // we already installed our own
|
2014-09-10 20:38:48 +02:00
|
|
|
// only log ajax requests which represent former GET requests or submits
|
|
|
|
// cuts down updates to egw_access_log table
|
2019-12-14 12:09:22 +01:00
|
|
|
'no_dla_update' => !preg_match('/(Etemplate::ajax_process_content|\.jdots_framework\.ajax_exec\.template)/', $_GET['menuaction']),
|
2010-06-01 16:20:55 +02:00
|
|
|
)
|
|
|
|
);
|
2012-09-19 18:22:21 +02:00
|
|
|
include_once('./header.inc.php');
|
2010-06-01 16:20:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
//Create a new json handler
|
2016-03-20 14:02:55 +01:00
|
|
|
$json = new Json\Request();
|
2010-06-01 16:20:55 +02:00
|
|
|
|
|
|
|
//Check whether the request data is set
|
2010-06-29 14:58:55 +02:00
|
|
|
if (isset($GLOBALS['egw_unset_vars']['_POST[json_data]']))
|
|
|
|
{
|
2015-01-27 09:57:34 +01:00
|
|
|
$json->isJSONRequest(true); // otherwise exception is not send back to client, as we have not yet called parseRequest()
|
2016-03-20 14:02:55 +01:00
|
|
|
throw new Json\Exception\ScriptTags("JSON Data contains script tags. Aborting...");
|
2010-06-29 14:58:55 +02:00
|
|
|
}
|
2018-08-15 13:10:15 +02:00
|
|
|
// check if we have a real json request
|
|
|
|
if (strpos($_SERVER['CONTENT_TYPE'], 'application/json') === 0)
|
|
|
|
{
|
|
|
|
$json->parseRequest($_GET['menuaction'], file_get_contents('php://input'));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$json->parseRequest($_GET['menuaction'], $_REQUEST['json_data']);
|
|
|
|
}
|
2016-03-20 14:02:55 +01:00
|
|
|
Json\Response::get();
|
2020-11-21 18:55:39 +01:00
|
|
|
|
|
|
|
// run egw destructor now explicit, in case a (notification) email is send via Egw::on_shutdown(),
|
|
|
|
// as stream-wrappers used by Horde Smtp fail when PHP is already in destruction
|
2020-07-26 15:04:22 +02:00
|
|
|
$GLOBALS['egw']->__destruct();
|
2010-06-01 16:20:55 +02:00
|
|
|
}
|
2018-11-01 11:59:31 +01:00
|
|
|
// missing menuaction GET parameter or request:parameters object or unparsable JSON
|
|
|
|
catch (\InvalidArgumentException $e) {
|
|
|
|
if (isset($json)) $json->isJSONRequest(false); // no regular json request processing
|
2010-06-01 16:20:55 +02:00
|
|
|
|
2018-11-01 11:59:31 +01:00
|
|
|
// give a proper HTTP status 400 Bad Request with some JSON payload explaining the problem
|
|
|
|
http_response_code(400);
|
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo json_encode(array('error' => $e->getMessage(), 'errno' => $e->getCode()));
|
|
|
|
}
|
|
|
|
// other exceptions are handled by our ajax_exception_handler sending them back as alerts to client-side
|