| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- Lib::loadClass('Request');
- Lib::loadClass('DBG');
- /**
- * @example: Response::sendJson($data);
- * @example: Response::sendPlainText($data);
- * @example: Response::sendJsonExit($data);
- * @example: Response::sendPlainTextExit($data);
- * @example: Response::sendTryCatchJson(array($this, 'methodNameReponseCallback'), $args);
- * @example: Response::sendTryCatchJson(array($this, 'methodNameReponseCallback'), $args = 'JSON_FROM_REQUEST_BODY');// try to read json from request body
- */
- class Response {
- public static function correctFilename($filename, $extension = null) {
- if (strlen($extension) && (!preg_match("/\.{$extension}$/i", $filename))) $filename .= ".{$extension}";
- return preg_replace('/[^[:alnum:].-]/', '_', $filename);
- }
- public static function sendJson($data) {
- header("Content-type: application/json");
- echo json_encode($data);
- }
- public static function sendPlainText($data) {
- header("Content-type: text/plain");
- if (!is_scalar($data)) throw new Exception("Wrong data type - only scalar values allowed");
- echo $data;
- }
- public static function sendCsv($data, $filename = null) {
- header("Content-Type: text/csv");
- if (strlen($filename)) header('Content-Disposition: attachment; filename=' . self::correctFilename($filename, 'csv') . ';');
- header("Content-Transfer-Encoding: binary");
- header("Content-Length: ".strlen($data));
- echo $data;
- }
- public static function sendJsonExit($data) {
- self::sendJson($data);
- exit;
- }
- public static function sendPlainTextExit($data) {
- self::sendPlainText($data);
- exit;
- }
- public static function sendCsvExit($data, $fileName = null) {
- self::sendCsvExit($data, $fileName);
- exit;
- }
- public static function sendTryCatchJson($callback, $args = array()) {// @param callback must return $response object to send as json or throw exception
- $response = array();
- try {
- if (!is_callable($callback)) throw new Exception("#1L" . __LINE__ . " Callback is not callable");
- if ('JSON_FROM_REQUEST_BODY' == $args) $args = Request::getRequestJson();
- $response = call_user_func($callback, $args);
- } catch (AlertDangerException $e) {
- Http::sendHeaderByCode(200);
- $response['type'] = 'error';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- $response['body'] = $e->getBody();
- DBG::log($e);
- Response::sendJsonExit($response);
- } catch (AlertSuccessException $e) {
- Http::sendHeaderByCode(200);
- $response['type'] = 'success';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- $response['body'] = $e->getBody();
- DBG::log($e);
- Response::sendJsonExit($response);
- } catch (AlertInfoException $e) {
- Http::sendHeaderByCode(200);
- $response['type'] = 'info';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- $response['body'] = $e->getBody();
- DBG::log($e);
- Response::sendJsonExit($response);
- } catch (AlertWarningException $e) {
- Http::sendHeaderByCode(200);
- $response['type'] = 'warning';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- $response['body'] = $e->getBody();
- DBG::log($e);
- Response::sendJsonExit($response);
- } catch (HttpException $e) {
- Http::sendHeaderByCode($e->getCode());
- $response['type'] = 'error';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- DBG::log($e);
- Response::sendJsonExit($response);
- } catch (Exception $e) {
- Http::sendHeaderByCode(500);
- $response['type'] = 'error';
- $response['msg'] = $e->getMessage();
- $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
- DBG::log($e);
- Response::sendJsonExit($response);
- }
- Http::sendHeaderByCode(200);
- Response::sendJsonExit($response);
- }
- static function sendRedirect($url) {
- if (!headers_sent()) {
- header("HTTP/1.1 303 See Other");
- header("Location: {$url}");
- } else {
- echo'<script type="text/javascript">'."window.location.href='{$url}';".'</script>';
- echo "\n".'<noscript>';
- echo "\n".'<meta http-equiv="refresh" content="0;url='.$url.'" />';
- echo "\n".'</noscript>';
- echo'<p>'.'<a href="'.$url.'">'."dalej".'</a>'.'</p>';
- }
- exit;
- }
- }
|