Exemplo n.º 1
0
 public function __construct(Request $request)
 {
     if (strpos($request->getURI(), 'index.php') || strpos($request->getURI(), 'index.html')) {
         header('HTTP/1.0 301 Moved Permanently');
         $replaced_url = str_replace(['index.php/', 'index.php', 'index.html'], ['', '', ''], str_replace('?', '', $request->getURI()));
         header('Location: http://' . $request->getHttpHost() . $replaced_url);
         exit(0);
     }
 }
Exemplo n.º 2
0
 public function passThrouthMiddleWares(Request $request, Response $response, Dispatcher $dispatcher)
 {
     $route = $this->getMatchedRoute();
     if (null == $route) {
         $r = $this->getDI()->get('router');
         $r->handle($request->getURI());
         $route = $r->getMatchedRoute();
         //为什么搜索“装备”会出现找不到路由的问题?估计与字符处理有关系
         if (null == $route) {
             die('url地址无效,找不到对应的路由设置!');
         }
     }
     $pattern = $route->getPattern();
     //对每个路由都进行验证的中间件! @todo 如果是get方式的话,目标对象如何获取呢?当前用户是否拥有该资源?
     foreach ($this->middlewaresForEveryRoute as $validator) {
         $data = null;
         if (preg_match('|.*:.*|', $validator)) {
             //此处设置了可以带中间件参数
             list($validator, $data) = explode(':', $validator);
             $data = $dispatcher->getParam($data);
         }
         /** @var myValidation $validator */
         $validator = new $validator();
         if (!in_array($route->getName(), $validator->excludedRoutes) and !$validator->isValid($data)) {
             $url = $validator->getRedirectedUrl();
             //                    dd($url);
             $response->redirect($url, true);
             return false;
         }
     }
     //@todo 如果是get方式的如何过滤呢?应该如何设置才是正常的呢?例如get方式的search的过滤,单独处理?也许吧?
     if ($this->hasMatchedMiddleWares($pattern) and $request->isPost()) {
         $middleWares = $this->getMiddleWares($pattern);
         foreach ($middleWares as $validator) {
             $data = $request->getPost();
             //                dd($validator);
             if (preg_match('|[^:]+:[^:]+|', $validator)) {
                 list($validator, $data) = explode(':', $validator);
                 $data = $dispatcher->getParam($data);
             }
             if (preg_match('|.*Rules$|', $validator)) {
                 $rules = new $validator();
                 $validator = (new myValidation())->take($rules);
             } else {
                 $validator = new $validator();
             }
             if (!$validator->isValid($data)) {
                 $url = $validator->getRedirectedUrl();
                 //                    dd($url);
                 $response->redirect($url, true);
                 return false;
             }
         }
     }
     return true;
 }
Exemplo n.º 3
0
 public function __construct(Dispatcher $dispatcher, Request $request, Router $router, View $view)
 {
     if ($view->getLayout() == 'admin') {
         return;
     }
     $match_url_entry = $this->matchingUrl($request->getURI());
     if ($match_url_entry) {
         $this->pick($match_url_entry);
     }
 }
Exemplo n.º 4
0
 /**
  * インスタンスを生成
  * Logger constructor.
  * @param null $fileName ファイル名を指定。デフォルトはmain設定ファイルのlog_file_format
  */
 public function __construct($fileName = null)
 {
     if ((bool) $fileName) {
         $logFile = LOGS_PATH . $fileName;
     } else {
         $logFile = LOGS_PATH . APPS_MAIN_CONF['log_file_name_format'];
     }
     $this->logger = new File($logFile);
     $req = new Request();
     $uri = $req->getURI();
     $this->logger->setFormatter(new LogFormatter("[%date%][{$uri}][%type%] %message%"));
 }
Exemplo n.º 5
0
 /**
  * retorna url de proxima pagina ou anterioir dependendo do define passado de parametro self::WAY_*
  * 
  * @param int|self::WAY_* $way
  * @return string
  */
 private function getPageURL($way)
 {
     $parsed = parse_url($_SERVER['REQUEST_URI']);
     if (isset($parsed['query'])) {
         parse_str($parsed['query'], $params);
         unset($params['_url']);
     } else {
         $params = [];
     }
     $params[DefaultParams::PAGE] = $this->currentPage + $way;
     $formated = http_build_query($params);
     $uri = $this->request->getURI();
     return WS_HOST . "{$parsed['path']}?{$formated}";
 }
Exemplo n.º 6
0
 /**
  * Generate cache key pair (for response header / body) by Host + Uri + Allowed Queries
  * @param Request $request
  * @param array $ignores
  * @return array
  */
 public function generateCacheKeys(Request $request, array $ignores = array())
 {
     list($urlPath) = explode('?', $request->getURI());
     $urlQuery = $request->getQuery();
     //NOTE: remove Phalcon default url rewrite param here
     unset($urlQuery['_url']);
     if ($ignores) {
         foreach ($ignores as $ignoreKey) {
             unset($urlQuery[$ignoreKey]);
         }
     }
     $cacheKeyPrefix = $request->getHttpHost() . $urlPath . json_encode($urlQuery);
     $cacheKeyPrefix = md5($cacheKeyPrefix);
     $bodyKey = $cacheKeyPrefix . '_b';
     $headersKey = $cacheKeyPrefix . '_h';
     $this->cacheHeadersKey = $headersKey;
     $this->cacheBodyKey = $bodyKey;
     return array($headersKey, $bodyKey);
 }
Exemplo n.º 7
0
 /**
  * Makes a request.
  *
  * @param \Symfony\Component\BrowserKit\Request $request
  *
  * @return \Symfony\Component\BrowserKit\Response
  * @throws \RuntimeException
  */
 public function doRequest($request)
 {
     $application = $this->getApplication();
     if (!$application instanceof Application && !$application instanceof MicroApplication) {
         throw new RuntimeException('Unsupported application class.');
     }
     $di = $application->getDI();
     /** @var \Phalcon\Http\Request $phRequest */
     if ($di->has('request')) {
         $phRequest = $di->get('request');
     }
     if (!$phRequest instanceof RequestInterface) {
         $phRequest = new Request();
     }
     $uri = $request->getUri() ?: $phRequest->getURI();
     $pathString = parse_url($uri, PHP_URL_PATH);
     $queryString = parse_url($uri, PHP_URL_QUERY);
     $_SERVER = $request->getServer();
     $_SERVER['REQUEST_METHOD'] = strtoupper($request->getMethod());
     $_SERVER['REQUEST_URI'] = null === $queryString ? $pathString : $pathString . '?' . $queryString;
     $_COOKIE = $request->getCookies();
     $_FILES = $this->remapFiles($request->getFiles());
     $_REQUEST = $this->remapRequestParameters($request->getParameters());
     $_POST = [];
     $_GET = [];
     if ($_SERVER['REQUEST_METHOD'] == 'GET') {
         $_GET = $_REQUEST;
     } else {
         $_POST = $_REQUEST;
     }
     parse_str($queryString, $output);
     foreach ($output as $k => $v) {
         $_GET[$k] = $v;
     }
     $_GET['_url'] = $pathString;
     $_SERVER['QUERY_STRING'] = http_build_query($_GET);
     Di::reset();
     Di::setDefault($di);
     $di['request'] = Stub::construct($phRequest, [], ['getRawBody' => $request->getContent()]);
     $response = $application->handle();
     $headers = $response->getHeaders();
     $status = (int) $headers->get('Status');
     $headersProperty = new ReflectionProperty($headers, '_headers');
     $headersProperty->setAccessible(true);
     $headers = $headersProperty->getValue($headers);
     if (!is_array($headers)) {
         $headers = [];
     }
     $cookiesProperty = new ReflectionProperty($di['cookies'], '_cookies');
     $cookiesProperty->setAccessible(true);
     $cookies = $cookiesProperty->getValue($di['cookies']);
     if (is_array($cookies)) {
         $restoredProperty = new ReflectionProperty('\\Phalcon\\Http\\Cookie', '_restored');
         $restoredProperty->setAccessible(true);
         $valueProperty = new ReflectionProperty('\\Phalcon\\Http\\Cookie', '_value');
         $valueProperty->setAccessible(true);
         foreach ($cookies as $name => $cookie) {
             if (!$restoredProperty->getValue($cookie)) {
                 $clientCookie = new Cookie($name, $valueProperty->getValue($cookie), $cookie->getExpiration(), $cookie->getPath(), $cookie->getDomain(), $cookie->getSecure(), $cookie->getHttpOnly());
                 $headers['Set-Cookie'][] = (string) $clientCookie;
             }
         }
     }
     return new Response($response->getContent(), $status ? $status : 200, $headers);
 }
Exemplo n.º 8
0
 /**
  * Get the URI for the current page.
  *
  * @return mixed
  */
 public function current()
 {
     return $this->getUrl($this->_request->getURI());
 }