WebApp.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. // LOAD ROUTES
  30. if (is_file(APP_DIR . '/config/routes.php')) {
  31. require APP_DIR . '/config/routes.php';
  32. }
  33. if (defined('USE_HOOKS') && USE_HOOKS) {
  34. HooksManager::applyHook('webapp.boot.after', []);
  35. }
  36. }
  37. public static function createTemplate($tpl_path=null, $variables=[], $layout=null, $templates_dirs=null)
  38. {
  39. return new PhpTemplate($tpl_path, $variables, $layout, $templates_dirs);
  40. }
  41. /*
  42. public static function getUser()
  43. {
  44. return self::$session_user;
  45. }
  46. public static function setUser($user)
  47. {
  48. self::$session_user = $user;
  49. }
  50. */
  51. // deprecated
  52. public static function route()
  53. {
  54. return self::routeUrl();
  55. }
  56. public static function routeUrl()
  57. {
  58. if (! self::$booted) {
  59. self::boot();
  60. }
  61. // routing: parse l'url puis transfert au controller
  62. if (defined('USE_HOOKS') && USE_HOOKS) {
  63. HooksManager::applyHook('webapp.route.before', []);
  64. }
  65. $route = Router::routeByUrl( $_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], false );
  66. if (defined('USE_HOOKS') && USE_HOOKS) {
  67. HooksManager::applyHook('webapp.route.after', [$route]);
  68. }
  69. if ($route) {
  70. //echo "success: route ok";
  71. if (defined('USE_HOOKS') && USE_HOOKS) {
  72. HooksManager::applyHook('webapp.route.success', [$_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'], $route]);
  73. }
  74. exit(0);
  75. } else if ($route === null) {
  76. // route found but callback is not callable
  77. self::error404('Page not Found', 'Warning: route callback is not callable');
  78. exit(1);
  79. } else if ($route === 0) {
  80. // route found but no callback defined
  81. if (defined('USE_HOOKS') && USE_HOOKS) {
  82. HooksManager::applyHook('webapp.route.error', []);
  83. }
  84. self::error404('Page not Found', "Warning: route found but no callback defined");
  85. exit(1);
  86. } else if ($route === false) {
  87. // no matching route
  88. if (defined('USE_HOOKS') && USE_HOOKS) {
  89. HooksManager::applyHook('webapp.route.error', []);
  90. }
  91. self::error404('Page not Found', "Warning: no matching route");
  92. exit(1);
  93. } else {
  94. // other cases
  95. if (defined('USE_HOOKS') && USE_HOOKS) {
  96. HooksManager::applyHook('webapp.route.error', []);
  97. }
  98. self::error404('Page not Found', "Warning: cannot route");
  99. exit(1);
  100. }
  101. }
  102. public static function error($http_status = 500, $meta_title = 'Server Error', $h1 = 'Error 500 - Server Error', $message = 'an error has occured')
  103. {
  104. if (! self::$controller) {
  105. self::$controller = new WebAppController();
  106. }
  107. if (self::$controller && $template = self::$controller->getTemplate()) {
  108. $template->assign('meta_title', $meta_title);
  109. $template->assign('h1', $h1);
  110. $template->assign('p', $message);
  111. $template->assign('http_status', $http_status);
  112. $error_template = 'error.tpl.php';
  113. if (defined('ERROR_TEMPLATE')) {
  114. $error_template = ERROR_TEMPLATE;
  115. }
  116. $template->display($error_template);
  117. } else {
  118. //header("HTTP/1.0 " . $http_status . " " . $meta_title);
  119. $output_html = '';
  120. $output_html .= '<html>' . PHP_EOL;
  121. $output_html .= '<head>' . PHP_EOL;
  122. if (! empty($meta_title)) {
  123. $output_html .= '<title>' . $meta_title . '</title>' . PHP_EOL;
  124. }
  125. $output_html .= '</head>' . PHP_EOL;
  126. $output_html .= '<body>' . PHP_EOL;
  127. if (! empty($h1)) {
  128. $output_html .= '<h1>' . $h1 . '</h1>' . PHP_EOL;
  129. }
  130. if (! empty($message)) {
  131. $output_html .= '<p>' . $message . '</p>' . PHP_EOL;
  132. }
  133. $output_html .= '</body>' . PHP_EOL;
  134. $output_html .= '</html>' . PHP_EOL;
  135. echo $output_html;
  136. }
  137. //exit;
  138. }
  139. public static function error400($title = 'Bad request', $message = '')
  140. {
  141. return self::error(400, $title, $title, $message);
  142. }
  143. public static function error403($title = 'Forbidden', $message = 'You are not allowed')
  144. {
  145. return self::error(403, $title, $title, $message);
  146. }
  147. public static function error404($title = 'Page not Found', $message = "The page you're looking for doesn't exist")
  148. {
  149. return self::error(404, $title, $title, $message);
  150. }
  151. public static function error500($title = 'Internal Server Error', $message = 'An error has occured')
  152. {
  153. return $this->error(500, $title, $title, $message);
  154. }
  155. public static function error503($title = 'Service Unavailable', $message = 'The service is unavailable')
  156. {
  157. return $this->error(503, $title, $title, $message);
  158. }
  159. }