Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

92 lines
2.9 KiB

  1. <?php
  2. namespace Friendica\Module\Special;
  3. use Friendica\Core\L10n;
  4. use Friendica\Core\Logger;
  5. use Friendica\Core\Renderer;
  6. use Friendica\Core\System;
  7. /**
  8. * This special module displays HTTPException when they are thrown in modules.
  9. *
  10. * @package Friendica\Module\Special
  11. */
  12. class HTTPException
  13. {
  14. /**
  15. * Generates the necessary template variables from the caught HTTPException.
  16. *
  17. * Fills in the blanks if title or descriptions aren't provided by the exception.
  18. *
  19. * @param \Friendica\Network\HTTPException $e
  20. * @return array ['$title' => ..., '$description' => ...]
  21. */
  22. private static function getVars(\Friendica\Network\HTTPException $e)
  23. {
  24. $message = $e->getMessage();
  25. $titles = [
  26. 200 => 'OK',
  27. 400 => L10n::t('Bad Request'),
  28. 401 => L10n::t('Unauthorized'),
  29. 403 => L10n::t('Forbidden'),
  30. 404 => L10n::t('Not Found'),
  31. 500 => L10n::t('Internal Server Error'),
  32. 503 => L10n::t('Service Unavailable'),
  33. ];
  34. $title = defaults($titles, $e->getCode(), 'Error ' . $e->getCode());
  35. if (empty($message)) {
  36. // Explanations are taken from https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
  37. $explanation = [
  38. 400 => L10n::t('The server cannot or will not process the request due to an apparent client error.'),
  39. 401 => L10n::t('Authentication is required and has failed or has not yet been provided.'),
  40. 403 => L10n::t('The request was valid, but the server is refusing action. The user might not have the necessary permissions for a resource, or may need an account.'),
  41. 404 => L10n::t('The requested resource could not be found but may be available in the future.'),
  42. 500 => L10n::t('An unexpected condition was encountered and no more specific message is suitable.'),
  43. 503 => L10n::t('The server is currently unavailable (because it is overloaded or down for maintenance). Please try again later.'),
  44. ];
  45. $message = defaults($explanation, $e->getCode(), '');
  46. }
  47. return ['$title' => $title, '$message' => $message, '$back' => L10n::t('Go back')];
  48. }
  49. /**
  50. * Displays a bare message page with no theming at all.
  51. *
  52. * @param \Friendica\Network\HTTPException $e
  53. * @throws \Exception
  54. */
  55. public static function rawContent(\Friendica\Network\HTTPException $e)
  56. {
  57. $content = '';
  58. if ($e->getCode() >= 400) {
  59. $tpl = Renderer::getMarkupTemplate('http_status.tpl');
  60. $content = Renderer::replaceMacros($tpl, self::getVars($e));
  61. }
  62. System::httpExit($e->getCode(), $e->httpdesc, $content);
  63. }
  64. /**
  65. * Returns a content string that can be integrated in the current theme.
  66. *
  67. * @param \Friendica\Network\HTTPException $e
  68. * @return string
  69. * @throws \Exception
  70. */
  71. public static function content(\Friendica\Network\HTTPException $e)
  72. {
  73. header($_SERVER["SERVER_PROTOCOL"] . ' ' . $e->getCode() . ' ' . $e->httpdesc);
  74. $tpl = Renderer::getMarkupTemplate('exception.tpl');
  75. return Renderer::replaceMacros($tpl, self::getVars($e));
  76. }
  77. }