Exemple #1
0
 /**
  * index
  *
  * @param \dirp\http\request $req
  * @param \dirp\http\response $res
  */
 public function index(\dirp\http\request $req, \dirp\http\response $res)
 {
     $path = file::safe_path($req->get('dir', '/'));
     $files = file::from_directory($path);
     $relative = file::to_relative_path($path);
     // most likely tried to list a file. a file is
     // not a directory, silly.
     if ($files === false) {
         $res->redirect($req->get_base_uri());
     }
     $ev = event::fire('indexlist', array('files' => $files, 'body' => null, 'relative' => $relative, 'path' => $path));
     // prepare the breadcrumb:
     $root = $req->get_base_uri() . '?dir=';
     $parts = explode('/', $relative);
     $crumbs = array();
     foreach ($parts as $i => $crumb) {
         $crumbs[$crumb] = $root . '/' . rawurlencode(implode('/', array_slice($parts, 0, $i + 1)));
     }
     $this->master()->title = 'viewing: /' . $relative;
     $this->master()->css[] = \dirp\app::asset('css/index/index.css');
     $this->master()->body = $this->view('list')->render(array('files' => $ev->files, 'relative' => $relative, 'crumbs' => $crumbs, 'root' => $req->get_base_uri() . '/', 'filesroot' => \dirp\app::cfg()->files_uri, 'body_override' => $ev->body, 'files_count' => 0));
 }
Exemple #2
0
 /**
  * dispatch
  * dispatches execution to a handler based on
  * a request uri
  *
  * pattern modifications based on those by Joe Topjian,
  * in the GluePHP micro-framework: http://gluephp.com/
  *
  * @param \dirp\http\request $request
  * @param \dirp\http\response
  * @throws \dirp\exception\routing if no routes are defined
  * @throws \dirp\exception\routing if a method is assigned but not existant
  * @return mixed
  */
 public static function dispatch(\dirp\http\request $request, \dirp\http\response $response)
 {
     if (!static::$_routes) {
         return false;
         // boy i sure hope this is handled somewhere
     }
     $routes = static::$_routes;
     $path = $request->get_uri();
     foreach ($routes as $pair) {
         $pattern = $pair[0];
         $handler = $pair[1];
         $pattern = str_replace('/', '\\/', $pattern);
         $pattern = '^' . $pattern . '\\/?$';
         if (preg_match("/{$pattern}/i", $path, $matches)) {
             if (!method_exists($handler[0], $handler[1])) {
                 throw new \dirp\exception\routing('that method (' . $handler[1] . ') doesn\'t exist!');
             }
             // make matches available through the request
             $request->set_params($matches);
             // route passing/halting:
             try {
                 // check for a 'before' handler for this controller.
                 if (method_exists($handler[0], 'before')) {
                     call_user_func(array($handler[0], 'before'), $request, $response);
                 }
                 if ($ret = call_user_func(array($handler[0], $handler[1]), $request, $response)) {
                     return $ret;
                 }
                 return $handler[0];
             } catch (\dirp\exception\pass $e) {
                 continue;
             } catch (\dirp\exception\halt $e) {
                 return $e->getMessage();
             }
         }
     }
     throw new \dirp\exception\routing('no matches, four oh four!');
 }