WebApp.php 5.2 KB

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