Example #1
0
 /**
  * @param wfWAFRequest|null $request
  * @return wfWAFRequest
  */
 public static function createFromGlobals($request = null)
 {
     if ($request === null) {
         if (version_compare(phpversion(), '5.3.0') > 0) {
             $class = get_called_class();
             $request = new $class();
         } else {
             $request = new self();
         }
     }
     $request->setAuth(array());
     $request->setCookies(array());
     $request->setFileNames(array());
     $request->setFiles(array());
     $request->setHeaders(array());
     $request->setHost('');
     $request->setIP('');
     $request->setMethod('');
     $request->setPath('');
     $request->setProtocol('');
     $request->setTimestamp('');
     $request->setURI('');
     $request->setBody(wfWAFUtils::stripMagicQuotes($_POST));
     $request->setQueryString(wfWAFUtils::stripMagicQuotes($_GET));
     $request->setCookies(wfWAFUtils::stripMagicQuotes($_COOKIE));
     $request->setFiles(wfWAFUtils::stripMagicQuotes($_FILES));
     if (!empty($_FILES)) {
         $fileNames = array();
         foreach ($_FILES as $input => $file) {
             $fileNames[$input] = wfWAFUtils::stripMagicQuotes($file['name']);
         }
         $request->setFileNames($fileNames);
     }
     if (is_array($_SERVER)) {
         //All of these depend on $_SERVER being non-null and an array
         $auth = array();
         if (array_key_exists('PHP_AUTH_USER', $_SERVER)) {
             $auth['user'] = wfWAFUtils::stripMagicQuotes($_SERVER['PHP_AUTH_USER']);
         }
         if (array_key_exists('PHP_AUTH_PW', $_SERVER)) {
             $auth['password'] = wfWAFUtils::stripMagicQuotes($_SERVER['PHP_AUTH_PW']);
         }
         $request->setAuth($auth);
         if (array_key_exists('REQUEST_TIME_FLOAT', $_SERVER)) {
             $timestamp = $_SERVER['REQUEST_TIME_FLOAT'];
         } else {
             if (array_key_exists('REQUEST_TIME', $_SERVER)) {
                 $timestamp = $_SERVER['REQUEST_TIME'];
             } else {
                 $timestamp = time();
             }
         }
         $request->setTimestamp($timestamp);
         $headers = array();
         foreach ($_SERVER as $key => $value) {
             if (wfWAFUtils::strpos($key, 'HTTP_') === 0) {
                 $header = wfWAFUtils::substr($key, 5);
                 $header = str_replace(array(' ', '_'), array('', ' '), $header);
                 $header = ucwords(wfWAFUtils::strtolower($header));
                 $header = str_replace(' ', '-', $header);
                 $headers[$header] = wfWAFUtils::stripMagicQuotes($value);
             }
         }
         if (array_key_exists('CONTENT_TYPE', $_SERVER)) {
             $headers['Content-Type'] = wfWAFUtils::stripMagicQuotes($_SERVER['CONTENT_TYPE']);
         }
         if (array_key_exists('CONTENT_LENGTH', $_SERVER)) {
             $headers['Content-Length'] = wfWAFUtils::stripMagicQuotes($_SERVER['CONTENT_LENGTH']);
         }
         $request->setHeaders($headers);
         $host = '';
         if (array_key_exists('Host', $headers)) {
             $host = $headers['Host'];
         } else {
             if (array_key_exists('SERVER_NAME', $_SERVER)) {
                 $host = wfWAFUtils::stripMagicQuotes($_SERVER['SERVER_NAME']);
             }
         }
         $request->setHost($host);
         $request->setMethod(array_key_exists('REQUEST_METHOD', $_SERVER) ? wfWAFUtils::stripMagicQuotes($_SERVER['REQUEST_METHOD']) : 'GET');
         $request->setProtocol(array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
         $request->setUri(array_key_exists('REQUEST_URI', $_SERVER) ? wfWAFUtils::stripMagicQuotes($_SERVER['REQUEST_URI']) : '');
         $uri = parse_url($request->getURI());
         if (is_array($uri) && array_key_exists('path', $uri)) {
             $path = $uri['path'];
         } else {
             $path = $request->getURI();
         }
         $request->setPath($path);
     }
     return $request;
 }