| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <?php
- /**
- * @author Open Mind
- * @date 09/04/2018
- * @time 16:15
- */
- namespace OpenMind\Web;
- use DB\SQL;
- use OpenMind\System\User;
- class Controller
- {
- /** @var SQL $dbHandle OM-Link Database */
- protected $dbHandle;
- private $timeLoadStart = null;
- /**
- * Sanitized POST request, useful if sanitized POST is need
- * @var array $sanitizedPostRequest
- */
- private $sanitizedPostRequest;
- public function __construct($f3)
- {
- $this->timeLoadStart = round(microtime(true) * 1000);
- $this->sanitizedPostRequest = $f3->clean($f3->get('POST'));
- $this->dbHandle = $f3->get('DB');
- /** @var User $loggedUser */
- $loggedUser = $f3->exists('SESSION.user') ? User::Restore() : null;
- $f3->set('USER', $loggedUser);
- // Check if user has permissions to navigate in current url
- $currentAlias = $f3->get('ALIAS');
- // If user is not logged and page require a permission, redirect to '/'
- if (!$loggedUser && $currentAlias) {
- $f3->reroute('/');
- return;
- }
- if (
- $loggedUser && // user is logged in?
- $currentAlias && // the page require a permission?
- !$loggedUser->hasPermission($currentAlias) // The user has the permission to go next step?
- ) {
- $f3->reroute('/homemenu');
- return;
- }
- return;
- }
- public function __destruct()
- {
- global $f3, $frameworkUtils;
- if ($f3->get('DEBUG') > 2) {
- $timeEnd = round(microtime(true) * 1000);
- $totalMS = $timeEnd - $this->timeLoadStart;
- $className = get_called_class();
- $frameworkUtils->getLogger()->write("Page $className loaded in {$totalMS}ms", 'd/m/Y H:i:s');
- }
- }
- /**
- * Return the sanitized POST parameters
- *
- * @return mixed
- */
- protected function getSanitizedPostRequest()
- {
- return $this->sanitizedPostRequest;
- }
- /**
- * Use setTimeout to request a change url
- *
- * @param string $url to change to
- * @param int $msDelay
- */
- protected function setRedirect(string $url, int $msDelay)
- {
- global $f3;
- $f3->set('REQ_REDIRECT', $url);
- $f3->set('REQ_REDIRECT_MS', $msDelay);
- }
- }
|