Exemple #1
0
 /**
  * 每个请求层,最终被调用的方法
  *
  * @return mixed
  */
 public function handle()
 {
     /** @var Request $request */
     $request =& $this->flow->contexts['request'];
     // 如果当前请求是初始请求,那么对其进行额外处理
     // 如果当前请求是CLI,那么直接当做为初始化请求
     if (PHP_SAPI == 'cli' || $request->isInitial()) {
         if (!empty($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && 'https' === $_SERVER['HTTP_X_FORWARDED_PROTO'] && in_array($_SERVER['REMOTE_ADDR'], Request::$trustedProxies)) {
             $request->secure = true;
         }
         if (isset($_SERVER['REQUEST_METHOD'])) {
             $method = $_SERVER['REQUEST_METHOD'];
         } else {
             $method = Http::GET;
         }
         if (isset($_SERVER['HTTP_REFERER'])) {
             $referrer = $_SERVER['HTTP_REFERER'];
         }
         if (isset($_SERVER['HTTP_USER_AGENT'])) {
             Request::$userAgent = $_SERVER['HTTP_USER_AGENT'];
         }
         if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
             $requestedWith = $_SERVER['HTTP_X_REQUESTED_WITH'];
         }
         Request::$clientIp = Request::getClientIP();
         if ($method !== Http::GET) {
             $body = file_get_contents('php://input');
         }
         $cookies = [];
         if ($cookieKeys = array_keys($_COOKIE)) {
             foreach ($cookieKeys as $key) {
                 $cookies[$key] = Cookie::get($key);
             }
         }
         if (isset($_SERVER['SERVER_PROTOCOL'])) {
             $request->protocol = $_SERVER['SERVER_PROTOCOL'];
         }
         $request->query($_GET)->post($_POST);
         if (isset($method)) {
             $request->method = $method;
         }
         if (isset($referrer)) {
             $request->referrer = $referrer;
         }
         if (isset($requestedWith)) {
             $request->requestedWith = $requestedWith;
         }
         if (isset($body)) {
             $request->body = $body;
         }
         if (isset($cookies)) {
             $request->cookie($cookies);
         }
     }
 }
Exemple #2
0
 /**
  * @return bool
  */
 protected function _destroy()
 {
     // Destroy the current session
     session_destroy();
     // Did destruction work?
     $status = !Base::getHttp()->sessionID();
     if ($status) {
         // Make sure the session cannot be restarted
         Cookie::delete($this->_name);
     }
     return $status;
 }
Exemple #3
0
 /**
  * 设置一个cookie
  *
  *     Cookie::set('theme', 'red');
  *
  * @param  string $name    cookie名
  * @param  string $value   cookie值
  * @param  int    $expired 生效时间(秒数)
  * @return bool
  */
 public static function set($name, $value, $expired = null)
 {
     // 处理过期时间
     if (null === $expired) {
         $expired = Cookie::$expiration;
     }
     if ($expired !== 0) {
         $expired += time();
     }
     // Add the salt to the cookie value
     $value = Cookie::salt($name, $value) . '~' . $value;
     return Base::getHttp()->setCookie($name, $value, $expired, Cookie::$path, Cookie::$domain, Cookie::$secure, Cookie::$httpOnly);
 }
Exemple #4
0
 /**
  * @return  bool
  */
 protected function _destroy()
 {
     return Cookie::delete($this->_name);
 }
Exemple #5
0
 /**
  * Completely destroy the current session.
  *     $success = $session->destroy();
  *
  * @return  boolean
  */
 public function destroy()
 {
     // Destroy the current session
     @session_destroy();
     // Did destruction work?
     $status = !session_id();
     if ($status) {
         // Make sure the session cannot be restarted
         Cookie::delete($this->_name);
     }
     return $status;
 }
Exemple #6
0
 /**
  * 发送header信息
  *
  * @param  array $headers
  * @param  bool  $replace
  * @return $this
  */
 protected function _sendHeadersToPhp(array $headers, $replace)
 {
     if (Base::getHttp()->headersSent()) {
         return $this;
     }
     foreach ($headers as $key => $line) {
         Base::getLog()->debug(__METHOD__ . ' send headers to php', ['key' => $key, 'value' => $line]);
         if ($key == 'Set-Cookie' && is_array($line)) {
             Base::getLog()->debug(__METHOD__ . ' set cookie headers', $line);
             foreach ($line as $name => $value) {
                 Cookie::set($name, $value['value'], $value['expiration']);
             }
             continue;
         }
         Base::getHttp()->header($line, $replace);
     }
     return $this;
 }