Author: Nick Sagona, III (nick@popphp.org)
Esempio n. 1
0
 public function testRoute()
 {
     $r = new Router(array('/' => 'Pop\\Mvc\\Controller'));
     $r->route(new Project(new Config(array())));
     $this->assertInstanceOf('Pop\\Mvc\\Controller', $r->controller());
     $this->assertInstanceOf('Pop\\Project\\Project', $r->project());
 }
Esempio n. 2
0
 /**
  * Run the project.
  *
  * @return void
  */
 public function run()
 {
     // If router exists, then route the project to the appropriate controller
     if (null !== $this->router) {
         $this->start = time();
         if (isset($_SERVER['REQUEST_METHOD'])) {
             $session = '[' . $_SERVER['REQUEST_METHOD'] . ']';
             if (isset($_SERVER['REMOTE_ADDR'])) {
                 $session .= ' ' . $_SERVER['REMOTE_ADDR'];
                 if (isset($_SERVER['SERVER_PORT'])) {
                     $session .= ':' . $_SERVER['SERVER_PORT'];
                 }
                 if (isset($_SERVER['HTTP_USER_AGENT'])) {
                     $session .= ' ' . $_SERVER['HTTP_USER_AGENT'];
                 }
             }
         } else {
             $session = '[CLI]';
         }
         $this->log($session, time());
         if (null !== $this->events->get('route.pre')) {
             $this->log('[Event] Pre-Route', time(), \Pop\Log\Logger::NOTICE);
         }
         // Trigger any pre-route events, route, then trigger any post-route events
         $this->events->trigger('route.pre', array('router' => $this->router));
         // If still alive after 'route.pre'
         if ($this->events->alive()) {
             $this->log('Route Start', time());
             $this->router->route($this);
             // If still alive after 'route'
             if ($this->events->alive()) {
                 if (null !== $this->events->get('route.post')) {
                     $this->log('[Event] Post-Route', time(), \Pop\Log\Logger::NOTICE);
                 }
                 $this->events->trigger('route.post', array('router' => $this->router));
                 // If still alive after 'route.post' and if a controller was properly
                 // routed and created, then dispatch it
                 if ($this->events->alive() && null !== $this->router->controller()) {
                     // Trigger any pre-dispatch events
                     if (null !== $this->events->get('dispatch.pre')) {
                         $this->log('[Event] Pre-Dispatch', time(), \Pop\Log\Logger::NOTICE);
                     }
                     $this->events->trigger('dispatch.pre', array('router' => $this->router));
                     // If still alive after 'dispatch.pre'
                     if ($this->events->alive()) {
                         // Get the action and dispatch it
                         $action = $this->router->getAction();
                         // Dispatch the found action, the error action or trigger the dispatch error events
                         if (null !== $action && method_exists($this->router->controller(), $action)) {
                             $this->router->controller()->dispatch($action);
                         } else {
                             if (method_exists($this->router->controller(), $this->router->controller()->getErrorAction())) {
                                 $this->router->controller()->dispatch($this->router->controller()->getErrorAction());
                             } else {
                                 if (null !== $this->events->get('dispatch.error')) {
                                     $this->log('[Event] Dispatch Error', time(), \Pop\Log\Logger::ERR);
                                 }
                                 $this->events->trigger('dispatch.error', array('router' => $this->router));
                             }
                         }
                         // If still alive after 'dispatch'
                         if ($this->events->alive()) {
                             // Trigger any post-dispatch events
                             if (null !== $this->events->get('dispatch.post')) {
                                 $this->log('[Event] Post-Dispatch', time(), \Pop\Log\Logger::NOTICE);
                             }
                             $this->events->trigger('dispatch.post', array('router' => $this->router));
                         }
                     }
                 }
             }
         }
         $this->log('Route End', time());
     }
 }
Esempio n. 3
0
 /**
  * Event-based auth check
  *
  * @param  \Pop\Mvc\Router $router
  * @return mixed
  */
 public static function auth($router)
 {
     $sess = Session::getInstance();
     $site = Sites::getSite();
     $basePath = $site->base_path;
     $resource = $router->getControllerClass();
     $permission = $router->getAction();
     $isFrontController = substr_count($resource, '\\') == 2;
     // Check for the resource and permission
     if (!$isFrontController && $resource != 'Phire\\Controller\\Phire\\Install\\IndexController') {
         if (null === $router->project()->getService('acl')->getResource($resource)) {
             if ($resource != 'Phire\\Controller\\Phire\\IndexController') {
                 $router->project()->getService('acl')->addResource($resource);
             } else {
                 $resource = null;
                 $permission = null;
             }
         }
         if (null !== $permission && null !== $resource && !method_exists($resource, $permission)) {
             $permission = 'error';
         }
         if ($router->controller()->getRequest()->getPath(0) == 'index' || $router->controller()->getRequest()->getPath(0) == 'add') {
             $permId = $router->controller()->getRequest()->getPath(1);
             if (null !== $permId && is_numeric($permId)) {
                 $permission .= '_' . $permId;
             }
         }
         // Get the user URI
         $uri = APP_URI == '' || strtolower($router->project()->getService('acl')->getType()->type) == 'user' ? APP_URI : '/' . strtolower($router->project()->getService('acl')->getType()->type);
         // If reset password flag is set
         if (isset($sess->reset_pwd) && $_SERVER['REQUEST_URI'] != $basePath . $uri . '/profile' && $_SERVER['REQUEST_URI'] != $basePath . $uri . '/login' && $_SERVER['REQUEST_URI'] != $basePath . $uri . '/logout') {
             \Pop\Http\Response::redirect($basePath . $uri . '/profile');
             return \Pop\Event\Manager::KILL;
             // If not logged in for unsubscribe and required, redirect to the system login
         } else {
             if ($_SERVER['REQUEST_URI'] == $basePath . $uri . '/unsubscribe' && $router->project()->getService('acl')->getType()->unsubscribe_login && !$router->project()->getService('acl')->isAuth($resource, $permission)) {
                 \Pop\Http\Response::redirect($basePath . $uri . '/login');
                 return \Pop\Event\Manager::KILL;
                 // Else, if not logged in or allowed, redirect to the system login
             } else {
                 if ($_SERVER['REQUEST_URI'] != $basePath . $uri . '/login' && $_SERVER['REQUEST_URI'] != $basePath . $uri . '/register' && $_SERVER['REQUEST_URI'] != $basePath . $uri . '/forgot' && $_SERVER['REQUEST_URI'] != $basePath . $uri . '/unsubscribe' && substr($_SERVER['REQUEST_URI'], 0, strlen($basePath . $uri . '/json')) != $basePath . $uri . '/json' && strpos($_SERVER['REQUEST_URI'], $basePath . $uri . '/verify') === false && !$router->project()->getService('acl')->isAuth($resource, $permission)) {
                     \Pop\Http\Response::redirect($basePath . $uri . '/login');
                     return \Pop\Event\Manager::KILL;
                     // Else, if logged in and allowed, and a system access URI, redirect back to the system
                 } else {
                     if (($_SERVER['REQUEST_URI'] == $basePath . $uri . '/login' || $_SERVER['REQUEST_URI'] == $basePath . $uri . '/register' || $_SERVER['REQUEST_URI'] == $basePath . $uri . '/forgot') && $router->project()->getService('acl')->isAuth($resource, $permission)) {
                         \Pop\Http\Response::redirect($basePath . ($uri == '' ? '/' : $uri));
                         return \Pop\Event\Manager::KILL;
                     }
                 }
             }
         }
     }
 }