/** * Request * ------------------------------------------------- */ function i18nApiResp($data) { global $kgReq; $callback = $kgReq->getVal('callback'); // Allow CORS (to avoid having to use JSON-P with cache busting callback) $kgReq->setHeader('Access-Control-Allow-Origin', '*'); // We don't yet support retrieval of when the localisation was last updated, // so default to unconditionally caching for 5 minutes. $maxAge = 5 * 60; $kgReq->setHeader('Last-Modified', gmdate('D, d M Y H:i:s', time()) . ' GMT'); $kgReq->setHeader('Cache-Control', 'public, max-age=' . intval($maxAge)); $kgReq->setHeader('Expires', gmdate('D, d M Y H:i:s', time() + $maxAge) . ' GMT'); if ($kgReq->tryLastModified(time() - $maxAge)) { exit; } // Serve as JSON or JSON-P if ($callback === null) { $kgReq->setHeader('Content-Type', 'application/json; charset=utf-8'); echo json_encode($data); } else { $kgReq->setHeader('Content-Type', 'text/javascript; charset=utf-8'); // Sanatize callback $callback = kfSanatizeJsCallback($callback); echo $callback . '(' . json_encode($data) . ');'; } exit; }
/** * Request * ------------------------------------------------- */ function i18nApiResp($data) { global $kgReq; $callback = $kgReq->getVal('callback'); // Serve as JSON or JSON-P if ($callback === null) { header('content-type: application/json; charset=utf-8', true); echo json_encode($data); } else { header('content-type: text/javascript; charset=utf-8', true); // Sanatize callback $callback = kfSanatizeJsCallback($callback); echo $callback . '(' . json_encode($data) . ');'; } exit; }
/** * Build API response * * @param string $specialFormat If $format is set to this format this function will not output * anything and return true. This can be used for a GUI front-end. */ function kfApiExport($data = array('krApiExport' => 'Example'), $format = 'dump', $callback = null, $specialFormat = null) { if ($specialFormat !== null && $format === $specialFormat) { return true; } switch ($format) { case 'php': header('Content-Type: application/vnd.php.serialized; charset=utf-8', true); echo serialize($data); die; break; case 'json': case 'jsonp': // Serve as AJAX object object or JSONP callback if ($callback === null) { header('Content-Type: application/json; charset=utf-8', true); echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); die; } else { header('Content-Type: text/javascript; charset=utf-8', true); // Sanatize callback $callback = kfSanatizeJsCallback($callback); echo $callback . '(' . json_encode($data) . ')'; die; } break; case 'dump': // No text/plain due to IE7 mime-type sniff bug causing html parsing header('Content-Type: text/text; charset=utf-8', true); var_dump($data); die; break; default: // HTTP 400 Bad Request http_response_code(400); header('Content-Type: text/plain; charset=utf-8', true); echo 'Invalid format.'; exit; } }