Controller.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @author Open Mind
  4. * @date 09/04/2018
  5. * @time 16:15
  6. */
  7. namespace OpenMind\Web;
  8. use DB\SQL;
  9. use OpenMind\System\User;
  10. class Controller
  11. {
  12. /** @var SQL $dbHandle OM-Link Database */
  13. protected $dbHandle;
  14. private $timeLoadStart = null;
  15. /**
  16. * Sanitized POST request, useful if sanitized POST is need
  17. * @var array $sanitizedPostRequest
  18. */
  19. private $sanitizedPostRequest;
  20. public function __construct($f3)
  21. {
  22. $this->timeLoadStart = round(microtime(true) * 1000);
  23. $this->sanitizedPostRequest = $f3->clean($f3->get('POST'));
  24. $this->dbHandle = $f3->get('DB');
  25. /** @var User $loggedUser */
  26. $loggedUser = $f3->exists('SESSION.user') ? User::Restore() : null;
  27. $f3->set('USER', $loggedUser);
  28. // Check if user has permissions to navigate in current url
  29. $currentAlias = $f3->get('ALIAS');
  30. // If user is not logged and page require a permission, redirect to '/'
  31. if (!$loggedUser && $currentAlias) {
  32. $f3->reroute('/');
  33. return;
  34. }
  35. if (
  36. $loggedUser && // user is logged in?
  37. $currentAlias && // the page require a permission?
  38. !$loggedUser->hasPermission($currentAlias) // The user has the permission to go next step?
  39. ) {
  40. $f3->reroute('/homemenu');
  41. return;
  42. }
  43. return;
  44. }
  45. public function __destruct()
  46. {
  47. global $f3, $frameworkUtils;
  48. if ($f3->get('DEBUG') > 2) {
  49. $timeEnd = round(microtime(true) * 1000);
  50. $totalMS = $timeEnd - $this->timeLoadStart;
  51. $className = get_called_class();
  52. $frameworkUtils->getLogger()->write("Page $className loaded in {$totalMS}ms", 'd/m/Y H:i:s');
  53. }
  54. }
  55. /**
  56. * Return the sanitized POST parameters
  57. *
  58. * @return mixed
  59. */
  60. protected function getSanitizedPostRequest()
  61. {
  62. return $this->sanitizedPostRequest;
  63. }
  64. /**
  65. * Use setTimeout to request a change url
  66. *
  67. * @param string $url to change to
  68. * @param int $msDelay
  69. */
  70. protected function setRedirect(string $url, int $msDelay)
  71. {
  72. global $f3;
  73. $f3->set('REQ_REDIRECT', $url);
  74. $f3->set('REQ_REDIRECT_MS', $msDelay);
  75. }
  76. }