/**
  * @param Request      $request
  * @param string       $repositoryPath
  * @param string       $prefix
  * @param \Pimf\Router $router
  *
  * @throws \Pimf\Resolver\Exception If no controller found at the repository path
  */
 public function __construct(\Pimf\Request $request, $repositoryPath = '/Controller', $prefix = 'Pimf\\', $router)
 {
     $controllerName = $request->fromGet()->get('controller');
     $this->router = $router;
     if (Config::get('app.routeable') === true) {
         $target = $this->router->find();
         if ($target instanceof \Pimf\Route\Target) {
             $controllerName = $target->getController();
         }
     }
     if (Sapi::isCli() && Config::get('environment') == 'production') {
         $controllerName = $request->fromCli()->get('controller');
     }
     if (!$controllerName) {
         $controllerName = Config::get('app.default_controller');
     }
     $this->repositoryPath = $repositoryPath;
     $this->request = $request;
     $this->controllerClass = $prefix . 'Controller\\';
     $basepath = $this->repositoryPath . '/';
     $controller = ucfirst($controllerName);
     if (Str::isEvilPath($basepath . $controller)) {
         throw new Bomb('directory traversal attack is not funny!');
     }
     $this->controllerPath = $basepath . $controller . '.php';
     if (!file_exists($this->controllerPath)) {
         throw new Bomb('no "' . $controller . '" controller found at the repository path');
     }
 }
Example #2
0
 /**
  * Get a new session ID that isn't assigned to any current session.
  *
  * @return string
  */
 public function id()
 {
     // just return any string since the Cookie storage has no idea.
     if ($this instanceof \Pimf\Session\Storages\Cookie) {
         return Character::random(40);
     }
     // we'll find an random ID here.
     do {
         $session = $this->load($key = Character::random(40));
     } while ($session !== null);
     return $key;
 }
Example #3
0
 /**
  * Create a new cache storage instance.
  *
  * @param string $storage
  *
  * @return CS\Apc|CS\Dba|CS\File|CS\Memcached|CS\Memory|CS\Pdo|CS\Redis|CS\WinCache
  * @throws \RuntimeException
  */
 protected static function factory($storage)
 {
     $cache = Config::get('cache');
     switch ($storage) {
         case 'apc':
             return new CS\Apc($cache['key']);
         case 'file':
             return new CS\File($cache['storage_path']);
         case 'pdo':
             return new CS\Pdo(Pdo\Factory::get($cache['database']), $cache['key']);
         case 'memcached':
             return new CS\Memcached(Memcached::connection(), $cache['key']);
         case 'memory':
             return new CS\Memory();
         case 'redis':
             return new CS\Redis(Redis::database());
         case 'wincache':
             return new CS\WinCache($cache['key']);
         case 'dba':
             return new CS\Dba(Str::ensureTrailing('/', $cache['storage_path']) . $cache['key']);
         default:
             throw new \RuntimeException("Cache storage {$storage} is not supported.");
     }
 }
Example #4
0
 /**
  * @param array $commands
  *
  * @return array
  */
 public static function parse(array $commands)
 {
     $cli = array();
     parse_str(implode('&', array_slice($commands, 1)), $cli);
     $command = current(array_keys((array) $cli, ''));
     if (Str::contains($command, ':')) {
         list($controller, $action) = explode(':', $command);
         $cli['controller'] = $controller;
         $cli['action'] = $action;
     }
     return $cli;
 }
 /**
  * Please bootstrap first, than run the application!
  * Run a application, let application accept a request, route the request,
  * dispatch to controller/action, render response and return response to client finally.
  *
  * @param array $get Array of variables passed to the current script via the URL parameters.
  * @param array $post Array of variables passed to the current script via the HTTP POST method.
  * @param array $cookie Array of variables passed to the current script via HTTP Cookies.
  * @param array $files An associative array FILES of items uploaded to the current script via the HTTP POST method.
  *
  * @return void
  */
 public static function run(array $get, array $post, array $cookie, array $files)
 {
     $cli = array();
     if (Sapi::isCli()) {
         $cli = Cli::parse((array) self::$env->argv);
         if (count($cli) < 1 || isset($cli['list'])) {
             Cli::absorb();
             exit(0);
         }
     }
     $prefix = Str::ensureTrailing('\\', Config::get('app.name'));
     $repository = BASE_PATH . 'app/' . Config::get('app.name') . '/Controller';
     if (isset($cli['controller']) && $cli['controller'] == 'core') {
         $prefix = 'Pimf\\';
         $repository = BASE_PATH . 'pimf-framework/core/Pimf/Controller';
     }
     $request = new Request($get, $post, $cookie, $cli, $files, self::$env);
     $resolver = new Resolver($request, $repository, $prefix, self::$router);
     $sessionized = Sapi::isWeb() && Config::get('session.storage') !== '';
     if ($sessionized) {
         Session::load();
     }
     $pimf = $resolver->process(self::$env, self::$em, self::$logger);
     if ($sessionized) {
         Session::save();
         Cookie::send();
     }
     $pimf->render();
 }
Example #6
0
 /**
  * Load the session for the current request.
  *
  * @param null|string $key
  */
 public function load($key)
 {
     if ($key !== null) {
         $this->session = $this->storage->load($key);
     }
     // If the session doesn't exist or is invalid.
     if (is_null($this->session) || static::expired($this->session)) {
         $this->exists = false;
         $this->session = $this->storage->fresh();
     }
     // A CSRF token is stored in every session to protect
     // the application from cross-site request
     if (!$this->has(Session::CSRF)) {
         $this->put(Session::CSRF, Character::random(40));
     }
 }
Example #7
0
 /**
  * Determine if the current URI matches a given pattern.
  *
  * @param  string $pattern
  *
  * @return bool
  */
 public static function is($pattern)
 {
     return Str::is($pattern, static::current());
 }
Example #8
0
 /**
  * Get cleaner URLs or old-fashioned ยป RFC 3986 URL-query string.
  *
  * @param string $route controller/action
  * @param array  $params
  * @param null   $https
  * @param bool   $asset
  *
  * @return string
  */
 public static function compute($route = '', array $params = array(), $https = null, $asset = false)
 {
     // if your application should work with RFC 3986 URL-query strings
     if (Config::get('app.routeable') === false) {
         list($controller, $action) = explode('/', $route);
         $params = array_merge(compact('controller', 'action'), $params);
         return Str::ensureTrailing('/', self::format($https, $asset)) . '?' . http_build_query($params, null, '&');
     }
     // otherwise PIMF will serve you cleaner URLs
     $slug = implode('/', $params);
     if ($slug != '') {
         $slug = '/' . $slug;
     }
     return self::to($route, $https, $asset) . $slug;
 }