/** * This action is executed before execute any action in the application. * * @param PhalconEvent $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * * @return mixed */ public function beforeDispatch(PhEvent $event, Dispatcher $dispatcher) { $di = $this->getDI(); $cookie = $di->getCookie(); $session = $di->getSession(); $config = $di->getConfig(); $languageCode = ''; if ($di->get('app')->isConsole()) { return; } // Detect language from cookie if ($cookie->has('languageCode')) { $languageCode = $cookie->get('languageCode')->getValue(); } else { // Get default language from language model $languageCode = LanguageModel::findFirst(['default = :isdefault: AND status = :enable:', 'bind' => ['isdefault' => LanguageModel::IS_DEFAULT, 'enable' => LanguageModel::STATUS_ENABLE]])->code; } // Set language code to session if ($session->has('languageCode') && $session->get('languageCode') != $languageCode || !$session->has('languageCode')) { $session->set('languageCode', $languageCode); } $messages = []; $directory = $di->get('registry')->directories->modules . ucfirst($dispatcher->getModuleName()) . '/Lang/' . $languageCode . '/' . strtolower($dispatcher->getControllerName()); $extension = '.php'; if (file_exists($directory . $extension)) { require $directory . $extension; } // add default core lang package require $di->get('registry')->directories->modules . self::DEFAULT_LANG_PACK . '/Lang/' . $languageCode . '/default.php'; $translate = new PhTranslateArray(['content' => array_merge($messages, $default)]); $di->set('lang', $translate); return !$event->isStopped(); }
/** * Before exception is happening. * * @param Event $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * @param PhalconException $exception Exception object. * * @throws \Phalcon\Exception * @return bool */ public function beforeException($event, $dispatcher, $exception) { // Handle 404 exceptions. if ($exception instanceof DispatchException) { $dispatcher->forward(['module' => EngineApplication::SYSTEM_DEFAULT_MODULE, 'namespace' => ucfirst(EngineApplication::SYSTEM_DEFAULT_MODULE) . '\\Controller', 'controller' => 'Error', 'action' => 'show404']); return false; } if (APPLICATION_STAGE == APPLICATION_STAGE_DEVELOPMENT) { throw $exception; } else { EngineException::logException($exception); } // Handle other exceptions. $dispatcher->forward(['module' => EngineApplication::SYSTEM_DEFAULT_MODULE, 'namespace' => ucfirst(EngineApplication::SYSTEM_DEFAULT_MODULE) . '\\Controller', 'controller' => 'Error', 'action' => 'show500']); return $event->isStopped(); }
/** * This event is executed before every route is executed in the dispatcher. * * @param Event $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * * @return bool */ public function beforeExecuteRoute($event, $dispatcher) { // Parse the annotations in the method currently executed. $annotations = $this->annotations->getMethod($dispatcher->getActiveController(), $dispatcher->getActiveMethod()); // Check if the method has an annotation 'Cache'. if ($annotations->has('Cache')) { // The method has the annotation 'Cache'. /** @var \Phalcon\Annotations\Annotation $annotation */ $annotation = $annotations->get('Cache'); // Get the lifetime. $lifetime = $annotation->getNamedArgument('lifetime'); $options = ['lifetime' => $lifetime]; // Check if there is a user defined cache key. if ($annotation->hasNamedArgument('key')) { $options['key'] = $annotation->getNamedArgument('key'); } // Enable the cache for the current method. $this->view->cache($options); } return !$event->isStopped(); }
/** * This action is executed before execute any action in the application * * @param Event $event * @param MvcDispatcher $dispatcher * @param \Exception $exception * @return boolean * @throws \Exception */ public function beforeException(Event $event, MvcDispatcher $dispatcher, $exception) { if ($exception instanceof DispatcherException) { switch ($exception->getCode()) { case Dispatcher::EXCEPTION_INVALID_HANDLER: case Dispatcher::EXCEPTION_CYCLIC_ROUTING: $action = 'show500'; break; case Dispatcher::EXCEPTION_INVALID_PARAMS: $action = 'show400'; break; default: $action = 'show404'; } $dispatcher->forward(['controller' => 'errors', 'action' => $action]); return false; } if (APP_PRODUCTION !== APPLICATION_ENV && $exception instanceof \Exception) { throw $exception; } $dispatcher->forward(['controller' => 'errors', 'action' => 'route500']); return $event->isStopped(); }
/** * This action is executed before execute any action in the application. * * @param PhalconEvent $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * * @return mixed */ public function beforeDispatch(PhEvent $event, Dispatcher $dispatcher) { $me = null; $config = $this->getDI()->get('config'); $cookie = $this->getDI()->get('cookie'); $session = $this->getDI()->get('session'); // check exsited cookie if ($cookie->has('remember-me')) { $rememberMe = $cookie->get('remember-me'); $userId = $rememberMe->getValue(); $myUser = User::findFirst(['id = :id: AND status = :status:', 'bind' => ['id' => $userId, 'status' => User::STATUS_ENABLE]]); if ($myUser) { } $this->session->set('me', $me); $role = $myUser->role; } else { //Get role name from session if ($session->has('me')) { $me = $session->get('me'); $role = $me->role; } else { $role = ROLE_GUEST; } } $current_resource = $dispatcher->getModuleName() . '/' . strtolower($dispatcher->getControllerName()); $current_action = $dispatcher->getActionName(); $acl = $this->getAcl($config); $allowed = $acl->isAllowed($role, $current_resource, $current_action); if ($allowed != PhAcl::ALLOW) { $this->getDI()->getEventsManager()->fire('dispatch:beforeException', $dispatcher, new Dispatcher\Exception()); } return !$event->isStopped(); }
/** * This action is executed before execute any action in the application. * * @param PhalconEvent $event Event object. * @param Dispatcher $dispatcher Dispatcher object. * * @return mixed */ public function beforeDispatch(PhEvent $event, Dispatcher $dispatcher) { $me = null; $config = $this->getDI()->get('config'); $cookie = $this->getDI()->get('cookie'); $session = $this->getDI()->get('session'); // check exsited cookie if ($cookie->has('remember-me')) { $rememberMe = $cookie->get('remember-me'); $userId = $rememberMe->getValue(); $myUser = UserModel::findFirst(['id = :id: AND status = :status:', 'bind' => ['id' => $userId, 'status' => UserModel::STATUS_ENABLE]]); if ($myUser) { $me = new \stdClass(); $me->id = $myUser->id; $me->email = $myUser->email; $me->name = $myUser->name; $me->role = $myUser->role; $me->roleName = $myUser->getRoleName(); $me->avatar = $myUser->avatar; } $this->session->set('me', $me); $role = $myUser->role; } else { //Get role name from session if ($session->has('me')) { $me = $session->get('me'); $role = $me->role; } else { $role = ROLE_GUEST; } } $current_resource = $dispatcher->getModuleName() . '/' . strtolower($dispatcher->getControllerName()); $current_action = $dispatcher->getActionName(); $acl = $this->getAcl($config); $allowed = $acl->isAllowed($role, $current_resource, $current_action); // var_dump($current_resource, $current_action, $allowed);die; if ($allowed === false && $me == null) { echo '<script type="text/javascript">self.location.href = "' . $this->getDI()->get('config')->global->baseUrl . 'login?redirect=' . base64_encode($this->getCurrentUrl()) . '"; </script>'; exit; } elseif ($allowed === false && $me->id > 0) { // khong co quyen + dang nhap roi echo '<script type="text/javascript">self.location.href = "' . $this->getDI()->get('config')->global->baseUrl . 'notfound' . '"; </script>'; exit; } return !$event->isStopped(); }