Response.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. Lib::loadClass('Request');
  3. Lib::loadClass('DBG');
  4. /**
  5. * @example: Response::sendJson($data);
  6. * @example: Response::sendPlainText($data);
  7. * @example: Response::sendJsonExit($data);
  8. * @example: Response::sendPlainTextExit($data);
  9. * @example: Response::sendTryCatchJson(array($this, 'methodNameReponseCallback'), $args);
  10. * @example: Response::sendTryCatchJson(array($this, 'methodNameReponseCallback'), $args = 'JSON_FROM_REQUEST_BODY');// try to read json from request body
  11. */
  12. class Response {
  13. public static function correctFilename($filename, $extension = null) {
  14. if (strlen($extension) && (!preg_match("/\.{$extension}$/i", $filename))) $filename .= ".{$extension}";
  15. return preg_replace('/[^[:alnum:].-]/', '_', $filename);
  16. }
  17. public static function sendJson($data) {
  18. header("Content-type: application/json");
  19. echo json_encode($data);
  20. }
  21. public static function sendPlainText($data) {
  22. header("Content-type: text/plain");
  23. if (!is_scalar($data)) throw new Exception("Wrong data type - only scalar values allowed");
  24. echo $data;
  25. }
  26. public static function sendCsv($data, $filename = null) {
  27. header("Content-Type: text/csv");
  28. if (strlen($filename)) header('Content-Disposition: attachment; filename=' . self::correctFilename($filename, 'csv') . ';');
  29. header("Content-Transfer-Encoding: binary");
  30. header("Content-Length: ".strlen($data));
  31. echo $data;
  32. }
  33. public static function sendJsonExit($data) {
  34. self::sendJson($data);
  35. exit;
  36. }
  37. public static function sendPlainTextExit($data) {
  38. self::sendPlainText($data);
  39. exit;
  40. }
  41. public static function sendCsvExit($data, $fileName = null) {
  42. self::sendCsvExit($data, $fileName);
  43. exit;
  44. }
  45. public static function sendTryCatchJson($callback, $args = array()) {// @param callback must return $response object to send as json or throw exception
  46. $response = array();
  47. try {
  48. if (!is_callable($callback)) throw new Exception("#1L" . __LINE__ . " Callback is not callable");
  49. if ('JSON_FROM_REQUEST_BODY' == $args) $args = Request::getRequestJson();
  50. $response = call_user_func($callback, $args);
  51. } catch (AlertDangerException $e) {
  52. Http::sendHeaderByCode(200);
  53. $response['type'] = 'error';
  54. $response['msg'] = $e->getMessage();
  55. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  56. $response['body'] = $e->getBody();
  57. DBG::log($e);
  58. Response::sendJsonExit($response);
  59. } catch (AlertSuccessException $e) {
  60. Http::sendHeaderByCode(200);
  61. $response['type'] = 'success';
  62. $response['msg'] = $e->getMessage();
  63. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  64. $response['body'] = $e->getBody();
  65. DBG::log($e);
  66. Response::sendJsonExit($response);
  67. } catch (AlertInfoException $e) {
  68. Http::sendHeaderByCode(200);
  69. $response['type'] = 'info';
  70. $response['msg'] = $e->getMessage();
  71. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  72. $response['body'] = $e->getBody();
  73. DBG::log($e);
  74. Response::sendJsonExit($response);
  75. } catch (AlertWarningException $e) {
  76. Http::sendHeaderByCode(200);
  77. $response['type'] = 'warning';
  78. $response['msg'] = $e->getMessage();
  79. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  80. $response['body'] = $e->getBody();
  81. DBG::log($e);
  82. Response::sendJsonExit($response);
  83. } catch (HttpException $e) {
  84. Http::sendHeaderByCode($e->getCode());
  85. $response['type'] = 'error';
  86. $response['msg'] = $e->getMessage();
  87. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  88. DBG::log($e);
  89. Response::sendJsonExit($response);
  90. } catch (Exception $e) {
  91. Http::sendHeaderByCode(500);
  92. $response['type'] = 'error';
  93. $response['msg'] = $e->getMessage();
  94. $response['code'] = "#" . $e->getCode() . "L" . $e->getLine();
  95. DBG::log($e);
  96. Response::sendJsonExit($response);
  97. }
  98. Http::sendHeaderByCode(200);
  99. Response::sendJsonExit($response);
  100. }
  101. static function sendRedirect($url) {
  102. if (!headers_sent()) {
  103. header("HTTP/1.1 303 See Other");
  104. header("Location: {$url}");
  105. } else {
  106. echo'<script type="text/javascript">'."window.location.href='{$url}';".'</script>';
  107. echo "\n".'<noscript>';
  108. echo "\n".'<meta http-equiv="refresh" content="0;url='.$url.'" />';
  109. echo "\n".'</noscript>';
  110. echo'<p>'.'<a href="'.$url.'">'."dalej".'</a>'.'</p>';
  111. }
  112. exit;
  113. }
  114. }