Example #1
0
 /**
  * Method to show the content.
  *
  * @return mixed
  * @throws \Exception If not supported request method or bad controller
  */
 public function render()
 {
     if (Sapi::isCli() && Config::get('environment') == 'production') {
         $suffix = 'CliAction';
         $action = $this->request->fromCli()->get('action', 'index');
     } else {
         $suffix = 'Action';
         if ($this->request->getMethod() != 'GET' && $this->request->getMethod() != 'POST') {
             $redirectUrl = new Value($this->env->REDIRECT_URL);
             $redirectUrl = $redirectUrl->deleteLeading('/')->deleteTrailing('/')->explode('/');
             $action = isset($redirectUrl[1]) ? $redirectUrl[1] : 'index';
         } else {
             $bag = sprintf('from%s', ucfirst(strtolower($this->request->getMethod())));
             $action = $this->request->{$bag}()->get('action', 'index');
         }
         if (Config::get('app.routeable') === true && $this->router instanceof \Pimf\Router) {
             $target = $this->router->find();
             if ($target instanceof \Pimf\Route\Target) {
                 $action = $target->getAction();
                 Request::$getData = new Param(array_merge($target->getParams(), Request::$getData->getAll()));
             }
         }
     }
     $action = strtolower($action) . $suffix;
     if (method_exists($this, 'init')) {
         call_user_func(array($this, 'init'));
     }
     if (!method_exists($this, $action)) {
         throw new Bomb("no action '{$action}' defined at controller " . get_class($this));
     }
     return call_user_func(array($this, $action));
 }
 /**
  * @param Request $request
  * @param string  $repositoryPath
  * @param string  $prefix
  *
  * @throws Resolver\Exception
  */
 public function __construct(Request $request, $repositoryPath = '/Controller', $prefix = 'Pimf\\')
 {
     $conf = Registry::get('conf');
     $controllerName = $request->fromGet()->get('controller');
     if ($conf['app']['routeable'] === true) {
         $target = Registry::get('router')->find();
         if ($target instanceof \Pimf\Route\Target) {
             $controllerName = $target->getController();
         }
     }
     if (Sapi::isCli() && $conf['environment'] == 'production') {
         $controllerName = $request->fromCli()->get('controller');
     }
     if (!$controllerName) {
         $controllerName = $conf['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 found at the repository path; ' . $this->controllerPath);
     }
 }
 /**
  * Method to show the content.
  *
  * @return mixed
  * @throws \Exception If not supported request method or bad controller
  */
 public function render()
 {
     $conf = Registry::get('conf');
     if (Sapi::isCli() && $conf['environment'] == 'production') {
         $suffix = 'CliAction';
         $action = $this->request->fromCli()->get('action', 'index');
     } else {
         $suffix = 'Action';
         $bag = 'from' . ucfirst(strtolower($this->response->getMethod()));
         $action = $this->request->{$bag}()->get('action', 'index');
         if ($conf['app']['routeable'] === true) {
             $target = Registry::get('router')->find();
             if ($target instanceof \Pimf\Route\Target) {
                 $action = $target->getAction();
                 Request::$getData = new Param((array) Request::stripSlashesIfMagicQuotes(array_merge($target->getParams(), Request::$getData->getAll())));
             }
         }
     }
     $action = strtolower($action) . $suffix;
     if (method_exists($this, 'init')) {
         call_user_func(array($this, 'init'));
     }
     if (!method_exists($this, $action)) {
         throw new Bomb("no action '{$action}' defined at controller " . get_class($this));
     }
     return call_user_func(array($this, $action));
 }
 /**
  * @param Environment   $env
  * @param Logger        $logger
  * @param EntityManager $em
  *
  * @return \Pimf\Controller\Base
  * @throws \Pimf\Resolver\Exception If no controller specified or no controller found at the repository.
  */
 public function process(Environment $env, $em, Logger $logger)
 {
     $path = str_replace($this->repositoryPath, '', $this->controllerPath);
     $name = str_replace('/', $this->controllerClass, $path);
     $controller = str_replace('.php', '', $name);
     if (!class_exists($controller)) {
         throw new Bomb('can not load class "' . $controller . '" from the repository');
     }
     return new $controller($this->request, new Response($this->request->getMethod()), $logger, $em, $this->router, $env);
 }
 /**
  * Strip slashes from string or array
  *
  * @param $rawData
  *
  * @return array|string
  */
 public static function stripSlashes($rawData)
 {
     return is_array($rawData) ? array_map(function ($value) {
         return \Pimf\Request::stripSlashes($value);
     }, $rawData) : stripslashes($rawData);
 }