コード例 #1
0
ファイル: Server.php プロジェクト: taviroquai/duality
 /**
  * Parses HTTP properties from PHP global environment
  * 
  * @param array $server The global $_SERVER variable
  * @param array $params The global $_REQUEST/$_GET/$_POST variable
  * 
  * @return Request The resulting request instance
  */
 public function getRequestFromGlobals($server, $params)
 {
     if (empty($server['REQUEST_METHOD'])) {
         return false;
     }
     // Filter input
     array_filter($server, function (&$var) {
         $var = filter_var($var, FILTER_UNSAFE_RAW);
     });
     array_filter($params, function (&$var) {
         $var = filter_var($var, FILTER_UNSAFE_RAW);
     });
     // Detect base URL and URI
     $server['SERVER_NAME'] = empty($server['SERVER_NAME']) ? $this->hostname : $server['SERVER_NAME'];
     $server['SCRIPT_NAME'] = empty($server['SCRIPT_NAME']) ? '/index.php' : $server['SCRIPT_NAME'];
     $server['REQUEST_URI'] = empty($server['REQUEST_URI']) ? '/' : $server['REQUEST_URI'];
     $baseUrl = (empty($server['HTTPS']) ? 'http' : 'https') . "://" . $server['SERVER_NAME'] . dirname($server['SCRIPT_NAME']);
     $uri = $server['REQUEST_URI'];
     $uri = str_replace(dirname($server['SCRIPT_NAME']), '', $uri);
     $uri = str_replace(basename($server['SCRIPT_NAME']), '', $uri);
     $uri = '/' . trim($uri, '/');
     // Set base URL and URI strings
     $this->setBaseUrl(new Url($baseUrl));
     $request = new Request(new Url($baseUrl . $uri));
     $request->setMethod($server['REQUEST_METHOD']);
     $request->setContent(file_get_contents('php://input'));
     $request->setTimestamp(empty($server['REQUEST_TIME']) ? time() : $server['REQUEST_TIME']);
     $headers = array('Http-Accept' => empty($server['HTTP_ACCEPT']) ? 'text/html' : $server['HTTP_ACCEPT'], 'Http-Accept-Language' => !empty($server['HTTP_ACCEPT_LANGUAGE']) ? $server['HTTP_ACCEPT_LANGUAGE'] : 'en-US', 'Http-Accept-Charset' => !empty($server['HTTP_ACCEPT_CHARSET']) ? $server['HTTP_ACCEPT_CHARSET'] : !empty($server['HTTP_ACCEPT_ENCODING']) ? $server['HTTP_ACCEPT_ENCODING'] : 'utf-8', 'Http-Host' => empty($server['REMOTE_HOST']) ? empty($server['REMOTE_ADDR']) ? '' : $server['REMOTE_ADDR'] : $server['REMOTE_HOST'], 'Referer' => empty($server['REFERER']) ? '' : $server['REFERER']);
     $request->setHeaders($headers);
     $request->setParams($params);
     if (!empty($server['HTTP_X_REQUESTED_WITH']) && strtolower($server['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
         $request->setAjax(true);
     }
     return $request;
 }