1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
<?php
/**
* Processes an AJAX request and returns a JSON encoded result.
*
* Path Info:
* ----------
* http://example.com/horde/services/ajax.php/APP/ACTION[?OPTIONS]
* - ACTION: (string) The AJAX action identifier.
* - APP: (string) The application name.
* - OPTIONS: Additional URL options
* - jsonhtml: (boolean) On error, return in text/html format instead of
* application/json
*
* Copyright 2010-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL-2). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl LGPL-2
* @package Horde
*/
require_once __DIR__ . '/../lib/Application.php';
list($app, $action) = explode('/', trim(Horde_Util::getPathInfo(), '/'));
if (empty($action)) {
// This is the only case where we really don't return anything, since
// the frontend can be presumed not to make this request on purpose.
// Other missing data cases we return a response of boolean false.
exit;
}
try {
Horde_Registry::appInit($app, array(
'authentication' => 'fallback'
));
} catch (Exception $e) {
// Uncaught exception. Sending backtrace info back via AJAX is just a
// waste of time.
exit;
}
// Open an output buffer to ensure that we catch errors that might break JSON
// encoding.
Horde::startBuffer();
// Token checking occurs in constructor.
$vars = $injector->getInstance('Horde_Variables');
try {
$ajax = $injector->getInstance('Horde_Core_Factory_Ajax')->create($app, $vars, $action, $vars->token);
} catch (Horde_Exception $e) {
/* Treat a token error as a session timeout. */
$response = new Horde_Core_Ajax_Response_HordeCore_SessionTimeout($app);
$response->jsonhtml = $vars->jsonhtml;
$response->sendAndExit();
}
try {
$ajax->doAction();
// Clear the output buffer that we started above, and log any unexpected
// output at a DEBUG level.
if ($out = Horde::endBuffer()) {
Horde::log('Unexpected output when creating AJAX reponse: ' . $out, 'DEBUG');
}
// Send the final result.
$ajax->send();
} catch (Horde_Exception_AuthenticationFailure $e) {
// If we reach this, authentication to Horde was successful, but
// authentication to some underlying backend failed. Best to logout
// immediately, since no way of knowing if error is transient.
$response = new Horde_Core_Ajax_Response_HordeCore_NoAuth($app, $e->getCode());
$response->jsonhtml = $vars->jsonhtml;
$response->sendAndExit();
} catch (Exception $e) {
$notification->push($e->getMessage(), 'horde.error');
$response = new Horde_Core_Ajax_Response_HordeCore();
$response->jsonhtml = $vars->jsonhtml;
$response->sendAndExit();
}
|