WebApp.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace KarmaFW;
  3. use \KarmaFW\Routing\Router;
  4. use \KarmaFW\Routing\Controllers\WebAppController;
  5. use \KarmaFW\Lib\Hooks\HooksManager;
  6. use \KarmaFW\Templates\PhpTemplate;
  7. class WebApp extends App
  8. {
  9. public static $session_user = false; // user connected with a session
  10. public static $controller = null;
  11. public static function boot()
  12. {
  13. parent::boot();
  14. if (defined('USE_HOOKS') && USE_HOOKS) {
  15. HooksManager::applyHook('webapp.boot.before', []);
  16. }
  17. // start session
  18. if (empty(session_id())) {
  19. if (defined('SESSION_NAME') && ! empty(SESSION_NAME)) {
  20. session_name(SESSION_NAME);
  21. }
  22. if (defined('SESSION_DURATION') && is_numeric(SESSION_DURATION)) {
  23. ini_set('session.gc_maxlifetime', SESSION_DURATION);
  24. session_set_cookie_params(SESSION_DURATION);
  25. // Note: si cron est actif, il faut modifier la valeur de session.gc_maxlifetime dans /etc/php/7.3/apache2/php.ini (voir /etc/cron.d/php)
  26. }
  27. session_start();
  28. }
  29. if (defined('USE_HOOKS') && USE_HOOKS) {
  30. HooksManager::applyHook('webapp.boot.after', []);
  31. }
  32. }
  33. public static function createTemplate($tpl_path=null, $variables=[], $layout=null, $templates_dirs=null)
  34. {
  35. return new PhpTemplate($tpl_path, $variables, $layout, $templates_dirs);
  36. }
  37. /*
  38. public static function getUser()
  39. {
  40. return self::$session_user;
  41. }
  42. public static function setUser($user)
  43. {
  44. self::$session_user = $user;
  45. }
  46. */
  47. public static function route()
  48. {
  49. if (! self::$booted) {
  50. self::boot();
  51. }
  52. // routing: parse l'url puis transfert au controller
  53. if (defined('USE_HOOKS') && USE_HOOKS) {
  54. HooksManager::applyHook('app.route.before', []);
  55. }
  56. $route = Router::routeByUrl( $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], false );
  57. if (defined('USE_HOOKS') && USE_HOOKS) {
  58. HooksManager::applyHook('app.route.after', [$route]);
  59. }
  60. if ($route) {
  61. //echo "success: route ok";
  62. if (defined('USE_HOOKS') && USE_HOOKS) {
  63. HooksManager::applyHook('app.route.success', [$_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'], $route]);
  64. }
  65. exit(0);
  66. } else if ($route === null) {
  67. // route found but callback is not callable
  68. self::error404('Page not Found', 'Warning: route callback is not callable');
  69. exit(1);
  70. } else if ($route === 0) {
  71. // route found but no callback defined
  72. if (defined('USE_HOOKS') && USE_HOOKS) {
  73. HooksManager::applyHook('app.route.error', []);
  74. }
  75. self::error404('Page not Found', "Warning: route found but no callback defined");
  76. exit(1);
  77. } else if ($route === false) {
  78. // no matching route
  79. if (defined('USE_HOOKS') && USE_HOOKS) {
  80. HooksManager::applyHook('app.route.error', []);
  81. }
  82. self::error404('Page not Found', "Warning: no matching route");
  83. exit(1);
  84. } else {
  85. // other cases
  86. if (defined('USE_HOOKS') && USE_HOOKS) {
  87. HooksManager::applyHook('app.route.error', []);
  88. }
  89. self::error404('Page not Found', "Warning: cannot route");
  90. exit(1);
  91. }
  92. }
  93. public static function error($http_status = 500, $meta_title = 'Server Error', $h1 = 'Error 500 - Server Error', $message = 'an error has occured', $error_template = 'error.tpl.php')
  94. {
  95. if (! self::$controller) {
  96. self::$controller = new WebAppController();
  97. }
  98. if (self::$controller && $template = self::$controller->getTemplate()) {
  99. $template->assign('meta_title', $meta_title);
  100. $template->assign('h1', $h1);
  101. $template->assign('p', $message);
  102. $template->assign('http_status', $http_status);
  103. $template->display($error_template);
  104. } else {
  105. //header("HTTP/1.0 " . $error_code . " " . $title);
  106. $output_html = '';
  107. $output_html .= '<html>' . PHP_EOL;
  108. $output_html .= '<head>' . PHP_EOL;
  109. if (! empty($meta_title)) {
  110. $output_html .= '<title>' . $meta_title . '</title>' . PHP_EOL;
  111. }
  112. $output_html .= '</head>' . PHP_EOL;
  113. $output_html .= '<body>' . PHP_EOL;
  114. if (! empty($h1)) {
  115. $output_html .= '<h1>' . $h1 . '</h1>' . PHP_EOL;
  116. }
  117. if (! empty($message)) {
  118. $output_html .= '<p>' . $message . '</p>' . PHP_EOL;
  119. }
  120. $output_html .= '</body>' . PHP_EOL;
  121. $output_html .= '</html>' . PHP_EOL;
  122. echo $output_html;
  123. }
  124. exit;
  125. }
  126. public static function error400($title = 'Bad request', $message = '')
  127. {
  128. return self::error(400, $title, $title, $message);
  129. }
  130. public static function error403($title = 'Forbidden', $message = 'You are not allowed')
  131. {
  132. return self::error(403, $title, $title, $message);
  133. }
  134. public static function error404($title = 'Page not Found', $message = "The page you're looking for doesn't exist")
  135. {
  136. return self::error(404, $title, $title, $message);
  137. }
  138. public static function error500($title = 'Internal Server Error', $message = 'An error has occured')
  139. {
  140. return $this->error(500, $title, $title, $message);
  141. }
  142. public static function error503($title = 'Service Unavailable', $message = 'The service is unavailable')
  143. {
  144. return $this->error(503, $title, $title, $message);
  145. }
  146. }