View source
<?php
function js_deliver_json($callback_result, array $info = NULL) {
$json = array();
$redirect_codes = array(
301,
302,
303,
307,
);
if ($callback_result === JS_MENU_NOT_FOUND) {
drupal_set_title('Not Found');
$json = js_http_response(404);
}
elseif ($callback_result === JS_MENU_ACCESS_DENIED) {
drupal_set_title('Access Denied');
$json = js_http_response(403);
}
elseif ($callback_result === JS_MENU_METHOD_NOT_ALLOWED) {
drupal_set_title('Method Not Allowed');
drupal_set_message(t('A request was made of a resource using a request method not supported by that resource; for example, using GET on a form which requires data to be presented via POST, or using PUT on a read-only resource. Please contact the site administrator if this problem persists.'), 'error', FALSE);
$json = js_http_response(403);
}
else {
if (is_array($callback_result)) {
if (js_is_renderable($callback_result)) {
js_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$json['content'] = drupal_render($callback_result);
}
else {
$json += $callback_result;
}
}
if (empty($json['response']['code']) || in_array($json['response']['code'], $redirect_codes)) {
$json += js_http_response();
}
}
if (!in_array($json['response']['code'], $redirect_codes)) {
$json['title'] = drupal_get_title();
$json['messages'] = drupal_get_messages();
$js = drupal_add_js();
$settings = array();
if (isset($js['settings']['data'])) {
foreach ($js['settings']['data'] as $setting) {
$settings = drupal_array_merge_deep($settings, $setting);
}
unset($settings['basePath']);
unset($settings['pathPrefix']);
unset($settings['ajaxPageState']);
}
$json['settings'] = $settings;
}
$output = drupal_json_encode($json);
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
$output = gzencode($output, 9);
drupal_add_http_header('Content-Encoding', 'gzip');
drupal_add_http_header('Vary', 'Accept-Encoding');
}
drupal_add_http_header('Content-Type', 'application/json; charset=utf-8');
drupal_add_http_header('Content-Length', strlen($output));
if (function_exists('ajax_set_verification_header')) {
ajax_set_verification_header();
}
print $output;
if (drupal_get_bootstrap_phase() === DRUPAL_BOOTSTRAP_FULL && (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update')) {
module_invoke_all('exit');
}
if (function_exists('drupal_session_commit')) {
drupal_session_commit();
}
exit;
}