Example #1
0
 /**
  * @param Request $request
  * @return bool
  * @since 1.0
  */
 public static function is_request_signature_valid(Request $request)
 {
     $public_key = $request->headers('X-Auth');
     $private_key = Arr::get(Kohana::$config->load('hapi.keys'), $public_key);
     if (!$public_key or !$private_key) {
         return FALSE;
     }
     $provided_request_signature = $request->headers('X-Auth-Hash');
     $expected_request_signature = self::calculate_hmac($request, $private_key);
     return $expected_request_signature === $provided_request_signature;
 }
Example #2
0
 public static function check_cache(Request $request, Response $response, $etag = NULL)
 {
     if ($etag == NULL) {
         $etag = $response->generate_etag();
     }
     $response->headers("etag", $etag);
     if ($response->headers("cache-control")) {
         $response->headers("cache-control", $response->headers("cache-control") . ", must-revalidate");
     } else {
         $response->headers("cache-control", "must-revalidate");
     }
     if ($request->headers("if-none-match") and (string) $request->headers("if-none-match") === $etag) {
         throw HTTP_Exception::factory(304)->headers("etag", $etag);
     }
     return $response;
 }
Example #3
0
 /**
  * Sends the HTTP message [Request] to a remote server and processes
  * the response.
  *
  * @param   Request   $request  request to send
  * @param   Response  $request  response to send
  * @return  Response
  */
 public function _send_message(Request $request, Response $response)
 {
     $http_method_mapping = array(HTTP_Request::GET => HTTPRequest::METH_GET, HTTP_Request::HEAD => HTTPRequest::METH_HEAD, HTTP_Request::POST => HTTPRequest::METH_POST, HTTP_Request::PUT => HTTPRequest::METH_PUT, HTTP_Request::DELETE => HTTPRequest::METH_DELETE, HTTP_Request::OPTIONS => HTTPRequest::METH_OPTIONS, HTTP_Request::TRACE => HTTPRequest::METH_TRACE, HTTP_Request::CONNECT => HTTPRequest::METH_CONNECT);
     // Create an http request object
     $http_request = new HTTPRequest($request->uri(), $http_method_mapping[$request->method()]);
     if ($this->_options) {
         // Set custom options
         $http_request->setOptions($this->_options);
     }
     // Set headers
     $http_request->setHeaders($request->headers()->getArrayCopy());
     // Set cookies
     $http_request->setCookies($request->cookie());
     // Set query data (?foo=bar&bar=foo)
     $http_request->setQueryData($request->query());
     // Set the body
     if ($request->method() == HTTP_Request::PUT) {
         $http_request->addPutData($request->body());
     } else {
         $http_request->setBody($request->body());
     }
     try {
         $http_request->send();
     } catch (HTTPRequestException $e) {
         throw new Request_Exception($e->getMessage());
     } catch (HTTPMalformedHeaderException $e) {
         throw new Request_Exception($e->getMessage());
     } catch (HTTPEncodingException $e) {
         throw new Request_Exception($e->getMessage());
     }
     // Build the response
     $response->status($http_request->getResponseCode())->headers($http_request->getResponseHeader())->cookie($http_request->getResponseCookies())->body($http_request->getResponseBody());
     return $response;
 }
Example #4
0
 /**
  * Sends the HTTP message [Request] to a remote server and processes
  * the response.
  *
  * @param   Request   request to send
  * @return  Response
  * @uses    [PHP cURL](http://php.net/manual/en/book.curl.php)
  */
 public function _send_message(Request $request)
 {
     // Calculate stream mode
     $mode = $request->method() === HTTP_Request::GET ? 'r' : 'r+';
     // Process cookies
     if ($cookies = $request->cookie()) {
         $request->headers('cookie', http_build_query($cookies, NULL, '; '));
     }
     // Get the message body
     $body = $request->body();
     if (is_resource($body)) {
         $body = stream_get_contents($body);
     }
     // Set the content length
     $request->headers('content-length', (string) strlen($body));
     list($protocol) = explode('/', $request->protocol());
     // Create the context
     $options = array(strtolower($protocol) => array('method' => $request->method(), 'header' => (string) $request->headers(), 'content' => $body));
     // Create the context stream
     $context = stream_context_create($options);
     stream_context_set_option($context, $this->_options);
     $uri = $request->uri();
     if ($query = $request->query()) {
         $uri .= '?' . http_build_query($query, NULL, '&');
     }
     $stream = fopen($uri, $mode, FALSE, $context);
     $meta_data = stream_get_meta_data($stream);
     // Get the HTTP response code
     $http_response = array_shift($meta_data['wrapper_data']);
     if (preg_match_all('/(\\w+\\/\\d\\.\\d) (\\d{3})/', $http_response, $matches) !== FALSE) {
         $protocol = $matches[1][0];
         $status = (int) $matches[2][0];
     } else {
         $protocol = NULL;
         $status = NULL;
     }
     // Create a response
     $response = $request->create_response();
     $response_header = $response->headers();
     // Process headers
     array_map(array($response_header, 'parse_header_string'), array(), $meta_data['wrapper_data']);
     $response->status($status)->protocol($protocol)->body(stream_get_contents($stream));
     // Close the stream after use
     fclose($stream);
     return $response;
 }
Example #5
0
 /**
  * Sends the HTTP message [Request] to a remote server and processes
  * the response.
  *
  * @param   Request   request to send
  * @return  Response
  */
 public function _send_message(Request $request)
 {
     // Response headers
     $response_headers = array();
     // Set the request method
     $options[CURLOPT_CUSTOMREQUEST] = $request->method();
     // Set the request body. This is perfectly legal in CURL even
     // if using a request other than POST. PUT does support this method
     // and DOES NOT require writing data to disk before putting it, if
     // reading the PHP docs you may have got that impression. SdF
     $options[CURLOPT_POSTFIELDS] = $request->body();
     // Process headers
     if ($headers = $request->headers()) {
         $http_headers = array();
         foreach ($headers as $key => $value) {
             $http_headers[] = $key . ': ' . $value;
         }
         $options[CURLOPT_HTTPHEADER] = $http_headers;
     }
     // Process cookies
     if ($cookies = $request->cookie()) {
         $options[CURLOPT_COOKIE] = http_build_query($cookies, NULL, '; ');
     }
     // Create response
     $response = $request->create_response();
     $response_header = $response->headers();
     // Implement the standard parsing parameters
     $options[CURLOPT_HEADERFUNCTION] = array($response_header, 'parse_header_string');
     $this->_options[CURLOPT_RETURNTRANSFER] = TRUE;
     $this->_options[CURLOPT_HEADER] = FALSE;
     // Apply any additional options set to
     $options += $this->_options;
     $uri = $request->uri();
     if ($query = $request->query()) {
         $uri .= '?' . http_build_query($query, NULL, '&');
     }
     // Open a new remote connection
     $curl = curl_init($uri);
     // Set connection options
     if (!curl_setopt_array($curl, $options)) {
         throw new Request_Exception('Failed to set CURL options, check CURL documentation: :url', array(':url' => 'http://php.net/curl_setopt_array'));
     }
     // Get the response body
     $body = curl_exec($curl);
     // Get the response information
     $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     if ($body === FALSE) {
         $error = curl_error($curl);
     }
     // Close the connection
     curl_close($curl);
     if (isset($error)) {
         throw new Request_Exception('Error fetching remote :url [ status :code ] :error', array(':url' => $request->url(), ':code' => $code, ':error' => $error));
     }
     $response->status($code)->body($body);
     return $response;
 }
Example #6
0
 /**
  * Checks the browser cache to see the response needs to be returned,
  * execution will halt and a 304 Not Modified will be sent if the
  * browser cache is up to date.
  *
  * @param  Request   $request   Request
  * @param  Response  $response  Response
  * @param  string    $etag      Resource ETag
  * @throws HTTP_Exception_304
  * @return Response
  */
 public static function check_cache(Request $request, Response $response, $etag = NULL)
 {
     // Generate an etag if necessary
     if ($etag == NULL) {
         $etag = $response->generate_etag();
     }
     // Set the ETag header
     $response->headers('etag', $etag);
     // Add the Cache-Control header if it is not already set
     // This allows etags to be used with max-age, etc
     if ($response->headers('cache-control')) {
         $response->headers('cache-control', $response->headers('cache-control') . ', must-revalidate');
     } else {
         $response->headers('cache-control', 'must-revalidate');
     }
     // Check if we have a matching etag
     if ($request->headers('if-none-match') and (string) $request->headers('if-none-match') === $etag) {
         // No need to send data again
         throw HTTP_Exception::factory(304)->headers('etag', $etag);
     }
     return $response;
 }
Example #7
0
 /**
  * Check Cache
  *
  * Checks the browser cache to see the response needs to be returned
  *
  * @param   string   $etag     Resource ETag [Optional]
  * @param   Request  $request  The request to test against [Optional]
  *
  * @return  Response
  *
  * @throws  Request_Exception
  */
 public function check_cache($etag = NULL, Request $request = NULL)
 {
     if (!$etag) {
         $etag = $this->generate_etag();
     }
     if (!$request) {
         throw new Request_Exception('A Request object must be supplied with an etag for evaluation');
     }
     // Set the ETag header
     $this->_header['etag'] = $etag;
     // Add the Cache-Control header if it is not already set
     // This allows etags to be used with max-age, etc
     if ($this->_header->offsetExists('cache-control')) {
         if (is_array($this->_header['cache-control'])) {
             // @todo
             $this->_header['cache-control'][] = new HTTP_Header_Value('must-revalidate');
         } else {
             $this->_header['cache-control'] = $this->_header['cache-control'] . ', must-revalidate';
         }
     } else {
         $this->_header['cache-control'] = 'must-revalidate';
     }
     if ($request->headers('if-none-match') and (string) $request->headers('if-none-match') === $etag) {
         // No need to send data again
         $this->_status = 304;
         $this->send_headers();
         // Stop execution
         exit;
     }
     return $this;
 }
Example #8
0
 /**
  * Reads and processes the request headers
  *
  * @access public
  * @return
  */
 public static function ProcessHeaders()
 {
     self::$headers = array_change_key_case(apache_request_headers(), CASE_LOWER);
     self::$useragent = isset(self::$headers["user-agent"]) ? self::$headers["user-agent"] : self::UNKNOWN;
     if (!isset(self::$asProtocolVersion)) {
         self::$asProtocolVersion = isset(self::$headers["ms-asprotocolversion"]) ? self::filterEvilInput(self::$headers["ms-asprotocolversion"], self::NUMBERSDOT_ONLY) : ZPush::GetLatestSupportedASVersion();
     }
     //if policykey is not yet set, try to set it from the header
     //the policy key might be set in Request::Initialize from the base64 encoded query
     if (!isset(self::$policykey)) {
         if (isset(self::$headers["x-ms-policykey"])) {
             self::$policykey = (int) self::filterEvilInput(self::$headers["x-ms-policykey"], self::NUMBERS_ONLY);
         } else {
             self::$policykey = 0;
         }
     }
     if (!empty($_SERVER['QUERY_STRING']) && Utils::IsBase64String($_SERVER['QUERY_STRING'])) {
         ZLog::Write(LOGLEVEL_DEBUG, "Using data from base64 encoded query string");
         if (isset(self::$policykey)) {
             self::$headers["x-ms-policykey"] = self::$policykey;
         }
         if (isset(self::$asProtocolVersion)) {
             self::$headers["ms-asprotocolversion"] = self::$asProtocolVersion;
         }
     }
     if (!isset(self::$acceptMultipart) && isset(self::$headers["ms-asacceptmultipart"]) && strtoupper(self::$headers["ms-asacceptmultipart"]) == "T") {
         self::$acceptMultipart = true;
     }
     ZLog::Write(LOGLEVEL_DEBUG, sprintf("Request::ProcessHeaders() ASVersion: %s", self::$asProtocolVersion));
     if (defined('USE_X_FORWARDED_FOR_HEADER') && USE_X_FORWARDED_FOR_HEADER == true && isset(self::$headers["x-forwarded-for"])) {
         $forwardedIP = self::filterEvilInput(self::$headers["x-forwarded-for"], self::NUMBERSDOT_ONLY);
         if ($forwardedIP) {
             self::$remoteAddr = $forwardedIP;
             ZLog::Write(LOGLEVEL_INFO, sprintf("'X-Forwarded-for' indicates remote IP: %s", self::$remoteAddr));
         }
     }
 }
Example #9
0
 /**
  * Processes the request, executing the controller action that handles this
  * request, determined by the [Route].
  *
  * 1. Before the controller action is called, the [Controller::before] method
  * will be called.
  * 2. Next the controller action will be called.
  * 3. After the controller action is called, the [Controller::after] method
  * will be called.
  *
  * By default, the output from the controller is captured and returned, and
  * no headers are sent.
  *
  *     $request->execute();
  *
  * @param   Request   $request   A request object
  * @param   Response  $response  A response object
  * @return  Response
  * @throws  Kohana_Exception
  * @uses    [Kohana::$profiling]
  * @uses    [Profiler]
  */
 public function execute_request(Request $request, Response $response)
 {
     if (Kohana::$profiling) {
         // Set the benchmark name
         $benchmark = '"' . $request->uri() . '"';
         if ($request !== Request::$initial and Request::$current) {
             // Add the parent request uri
             $benchmark .= ' « "' . Request::$current->uri() . '"';
         }
         // Start benchmarking
         $benchmark = Profiler::start('Requests', $benchmark);
     }
     // Store the current active request and replace current with new request
     $previous = Request::$current;
     Request::$current = $request;
     // Resolve the POST fields
     if ($post = $request->post()) {
         $request->body(http_build_query($post, NULL, '&'))->headers('content-type', 'application/x-www-form-urlencoded; charset=' . Kohana::$charset);
     }
     $request->headers('content-length', (string) $request->content_length());
     // If Kohana expose, set the user-agent
     if (Kohana::$expose) {
         $request->headers('user-agent', Kohana::version());
     }
     try {
         $response = $this->_send_message($request, $response);
     } catch (Exception $e) {
         // Restore the previous request
         Request::$current = $previous;
         if (isset($benchmark)) {
             // Delete the benchmark, it is invalid
             Profiler::delete($benchmark);
         }
         // Re-throw the exception
         throw $e;
     }
     // Restore the previous request
     Request::$current = $previous;
     if (isset($benchmark)) {
         // Stop the benchmark
         Profiler::stop($benchmark);
     }
     // Return the response
     return $response;
 }
Example #10
0
 /**
  * Tests the setting of headers to the request object
  * 
  * @dataProvider provider_headers_set
  *
  * @param   Request    request object
  * @param   array      header(s) to set to the request object
  * @param   string     expected http header
  * @return  void
  */
 public function test_headers_set(Request $request, $headers, $expected)
 {
     $request->headers($headers);
     $this->assertSame($expected, (string) $request->headers());
 }
Example #11
0
 protected function _send_message(\Request $request, \Response $response)
 {
     $this->headers = $request->headers();
     return $response;
 }
Example #12
0
 /**
  * Caches a [Response] using the supplied [Cache]
  * and the key generated by [Request_Client::_create_cache_key].
  *
  * If not response is supplied, the cache will be checked for an existing
  * one that is available.
  *
  * @param   Request   $request  The request
  * @param   Response  $response Response
  * @return  mixed
  */
 public function cache_response(Request $request, Response $response = NULL)
 {
     if (!$this->_cache instanceof Cache) {
         return FALSE;
     }
     // Check for Pragma: no-cache
     if ($pragma = $request->headers('pragma')) {
         if ($pragma instanceof HTTP_Header_Value and $pragma->key == 'no-cache') {
             return FALSE;
         } elseif (is_array($pragma) and isset($pragma['no-cache'])) {
             return FALSE;
         }
     }
     if (!$response) {
         $response = $this->_cache->get($this->create_cache_key($request));
         return $response !== NULL ? $response : FALSE;
     } else {
         if (($ttl = $this->cache_lifetime($response)) === FALSE) {
             return FALSE;
         }
         return $this->_cache->set($this->create_cache_key($request), $response, $ttl);
     }
 }
Example #13
0
 /**
  * Reads and processes the request headers
  *
  * @access public
  * @return
  */
 public static function ProcessHeaders()
 {
     self::$headers = array_change_key_case(apache_request_headers(), CASE_LOWER);
     self::$useragent = isset(self::$headers["user-agent"]) ? self::$headers["user-agent"] : self::UNKNOWN;
     if (!isset(self::$asProtocolVersion)) {
         self::$asProtocolVersion = isset(self::$headers["ms-asprotocolversion"]) ? self::filterEvilInput(self::$headers["ms-asprotocolversion"], self::NUMBERSDOT_ONLY) : ZPush::GetLatestSupportedASVersion();
     }
     //if policykey is not yet set, try to set it from the header
     //the policy key might be set in Request::Initialize from the base64 encoded query
     if (!isset(self::$policykey)) {
         if (isset(self::$headers["x-ms-policykey"])) {
             self::$policykey = (int) self::filterEvilInput(self::$headers["x-ms-policykey"], self::NUMBERS_ONLY);
         } else {
             self::$policykey = 0;
         }
     }
     if (!empty($_SERVER['QUERY_STRING']) && Utils::IsBase64String($_SERVER['QUERY_STRING'])) {
         ZLog::Write(LOGLEVEL_DEBUG, "Using data from base64 encoded query string");
         if (isset(self::$policykey)) {
             self::$headers["x-ms-policykey"] = self::$policykey;
         }
         if (isset(self::$asProtocolVersion)) {
             self::$headers["ms-asprotocolversion"] = self::$asProtocolVersion;
         }
     }
     ZLog::Write(LOGLEVEL_DEBUG, sprintf("Request::ProcessHeaders() ASVersion: %s", self::$asProtocolVersion));
 }
Example #14
0
 public static function on_header_location(Request $request, Response $response, Request_Client $client)
 {
     if ($client->follow() and in_array($response->status(), array(201, 301, 302, 303, 307))) {
         switch ($response->status()) {
             default:
             case 301:
             case 307:
                 $follow_method = $request->method();
                 break;
             case 201:
             case 303:
                 $follow_method = Request::GET;
                 break;
             case 302:
                 if ($client->strict_redirect()) {
                     $follow_method = $request->method();
                 } else {
                     $follow_method = Request::GET;
                 }
                 break;
         }
         $orig_headers = $request->headers()->getArrayCopy();
         $follow_header_keys = array_intersect(array_keys($orig_headers), $client->follow_headers());
         $follow_headers = \Arr::extract($orig_headers, $follow_header_keys);
         $follow_request = Request::factory($response->headers("Location"))->method($follow_method)->headers($follow_headers);
         if ($follow_method !== Request::GET) {
             $follow_request->body($request->body());
         }
         return $follow_request;
     }
     return NULL;
 }
Example #15
0
 /**
  * Execute the request using PHP stream. (not recommended)
  *
  * @param   Request   $request  Request to execute
  * @return  Response
  */
 protected function _native_execute(Request $request)
 {
     // Reset the headers
     Request_Client_External::$_processed_headers = array();
     // Calculate stream mode
     $mode = $request->method() === HTTP_Request::GET ? 'r' : 'r+';
     // Process cookies
     if ($cookies = $request->cookie()) {
         $request->headers('cookie', http_build_query($cookies, NULL, '; '));
     }
     // Create the context
     $options = array($request->protocol() => array('method' => $request->method(), 'header' => (string) $request->headers(), 'content' => $request->body(), 'user-agent' => 'Kohana Framework ' . Kohana::VERSION . ' (' . Kohana::CODENAME . ')'));
     // Create the context stream
     $context = stream_context_create($options);
     stream_context_set_option($context, $this->_options);
     $stream = fopen($request->uri(), $mode, FALSE, $context);
     $meta_data = stream_get_meta_data($stream);
     // Get the HTTP response code
     $http_response = array_shift($meta_data['wrapper_data']);
     if (preg_match_all('/(\\w+\\/\\d\\.\\d) (\\d{3})/', $http_response, $matches) !== FALSE) {
         $protocol = $matches[1][0];
         $status = (int) $matches[2][0];
     } else {
         $protocol = NULL;
         $status = NULL;
     }
     // Process headers
     array_map(array('Request_Client_External', '_parse_headers'), array(), $meta_data['wrapper_data']);
     // Create a response
     $response = $request->create_response();
     $response->status($status)->protocol($protocol)->headers(Request_Client_External::$_processed_headers)->body(stream_get_contents($stream));
     // Close the stream after use
     fclose($stream);
     return $response;
 }
 /**
  * Caches a [Response] using the supplied [Cache]
  * and the key generated by [Request_Client::_create_cache_key].
  *
  * If not response is supplied, the cache will be checked for an existing
  * one that is available.
  *
  * @param   string      $key        the cache key to use
  * @param   Request     $request    the HTTP Request
  * @param   Response    $response   the HTTP Response
  * @return  mixed
  */
 public function cache_response($key, Request $request, Response $response = NULL)
 {
     if (!$this->_cache instanceof Cache) {
         return FALSE;
     }
     // Check for Pragma: no-cache
     if ($pragma = $request->headers('pragma')) {
         if ($pragma == 'no-cache') {
             return FALSE;
         } elseif (is_array($pragma) and in_array('no-cache', $pragma)) {
             return FALSE;
         }
     }
     // If there is no response, lookup an existing cached response
     if ($response === NULL) {
         $response = $this->_cache->get($key);
         if (!$response instanceof Response) {
             return FALSE;
         }
         // Do cache hit arithmetic, using fast arithmetic if available
         if ($this->_cache instanceof Cache_Arithmetic) {
             $hit_count = $this->_cache->increment(HTTP_Cache::CACHE_HIT_KEY . $key);
         } else {
             $hit_count = $this->_cache->get(HTTP_Cache::CACHE_HIT_KEY . $key);
             $this->_cache->set(HTTP_Cache::CACHE_HIT_KEY . $key, ++$hit_count);
         }
         // Update the header to have correct HIT status and count
         $response->headers(HTTP_Cache::CACHE_STATUS_KEY, HTTP_Cache::CACHE_STATUS_HIT)->headers(HTTP_Cache::CACHE_HIT_KEY, $hit_count);
         return $response;
     } else {
         if (($ttl = $this->cache_lifetime($response)) === FALSE) {
             return FALSE;
         }
         $response->headers(HTTP_Cache::CACHE_STATUS_KEY, HTTP_Cache::CACHE_STATUS_SAVED);
         // Set the hit count to zero
         $this->_cache->set(HTTP_Cache::CACHE_HIT_KEY . $key, 0);
         return $this->_cache->set($key, $response, $ttl);
     }
 }
Example #17
0
 /**
  * Reads and processes the request headers
  *
  * @access public
  * @return
  */
 public static function ProcessHeaders()
 {
     self::$headers = array_change_key_case(apache_request_headers(), CASE_LOWER);
     self::$useragent = isset(self::$headers["user-agent"]) ? self::$headers["user-agent"] : self::UNKNOWN;
     if (!isset(self::$asProtocolVersion)) {
         self::$asProtocolVersion = isset(self::$headers["ms-asprotocolversion"]) ? self::filterEvilInput(self::$headers["ms-asprotocolversion"], self::NUMBERSDOT_ONLY) : ZPush::GetLatestSupportedASVersion();
     }
     //if policykey is not yet set, try to set it from the header
     //the policy key might be set in Request::Initialize from the base64 encoded query
     if (!isset(self::$policykey)) {
         if (isset(self::$headers["x-ms-policykey"])) {
             self::$policykey = (int) self::filterEvilInput(self::$headers["x-ms-policykey"], self::NUMBERS_ONLY);
         } else {
             self::$policykey = 0;
         }
     }
     if (!empty($_SERVER['QUERY_STRING']) && Utils::IsBase64String($_SERVER['QUERY_STRING'])) {
         ZLog::Write(LOGLEVEL_DEBUG, "Using data from base64 encoded query string");
         if (isset(self::$policykey)) {
             self::$headers["x-ms-policykey"] = self::$policykey;
         }
         if (isset(self::$asProtocolVersion)) {
             self::$headers["ms-asprotocolversion"] = self::$asProtocolVersion;
         }
     }
     if (!isset(self::$acceptMultipart) && isset(self::$headers["ms-asacceptmultipart"]) && strtoupper(self::$headers["ms-asacceptmultipart"]) == "T") {
         self::$acceptMultipart = true;
     }
     ZLog::Write(LOGLEVEL_DEBUG, sprintf("Request::ProcessHeaders() ASVersion: %s", self::$asProtocolVersion));
     if (isset(self::$headers["x-push-plugin"])) {
         list($version, $build, $buildDate) = explode("/", self::$headers["x-push-plugin"]);
         self::$koeVersion = self::filterEvilInput($version, self::NUMBERSDOT_ONLY);
         self::$koeBuild = self::filterEvilInput($build, self::HEX_ONLY);
         self::$koeBuildDate = strtotime(self::filterEvilInput($buildDate, self::ISO8601));
     }
     if (defined('USE_X_FORWARDED_FOR_HEADER') && USE_X_FORWARDED_FOR_HEADER == true && isset(self::$headers["x-forwarded-for"])) {
         $forwardedIP = self::filterEvilInput(self::$headers["x-forwarded-for"], self::NUMBERSDOT_ONLY);
         if ($forwardedIP) {
             ZLog::Write(LOGLEVEL_DEBUG, sprintf("'X-Forwarded-for' indicates remote IP: %s - connect is coming from IP: %s", $forwardedIP, self::$remoteAddr));
             self::$remoteAddr = $forwardedIP;
         }
     }
     // Mobile devices send Authorization header using UTF-8 charset. Outlook sends it using ISO-8859-1 encoding.
     // For the successful authentication the user and password must be UTF-8 encoded. Try to determine which
     // charset was sent by the client and convert it to UTF-8. See https://jira.z-hub.io/browse/ZP-864.
     if (isset($_SERVER['PHP_AUTH_USER'])) {
         $encoding = mb_detect_encoding(self::$authUser, "UTF-8, ISO-8859-1");
         if (!$encoding) {
             $encoding = mb_detect_encoding(self::$authUser, Utils::GetAvailableCharacterEncodings());
             if ($encoding) {
                 ZLog::Write(LOGLEVEL_WARN, sprintf("Request->ProcessHeaders(): mb_detect_encoding detected '%s' charset. This charset is not in the default detect list. Please report it to Z-Push developers.", $encoding));
             } else {
                 ZLog::Write(LOGLEVEL_ERROR, "Request->ProcessHeaders(): mb_detect_encoding failed to detect the Authorization header charset. It's possible that user won't be able to login.");
             }
         }
         if ($encoding && strtolower($encoding) != "utf-8") {
             ZLog::Write(LOGLEVEL_DEBUG, sprintf("Request->ProcessHeaders(): mb_detect_encoding detected '%s' charset. Authorization header will be converted to UTF-8 from it.", $encoding));
             self::$authUser = mb_convert_encoding(self::$authUser, "UTF-8", $encoding);
             self::$authPassword = mb_convert_encoding(self::$authPassword, "UTF-8", $encoding);
         }
     }
 }
Example #18
0
 /**
  * The default handler for following redirects, triggered by the presence of
  * a Location header in the response.
  *
  * The client's follow property must be set TRUE and the HTTP response status
  * one of 201, 301, 302, 303 or 307 for the redirect to be followed.
  *
  * @param Request $request
  * @param Response $response
  * @param Request_Client $client
  */
 public static function on_header_location(Request $request, Response $response, Request_Client $client)
 {
     // Do we need to follow a Location header ?
     if ($client->follow() and in_array($response->status(), array(201, 301, 302, 303, 307))) {
         // Figure out which method to use for the follow request
         switch ($response->status()) {
             default:
             case 301:
             case 307:
                 $follow_method = $request->method();
                 break;
             case 201:
             case 303:
                 $follow_method = Request::GET;
                 break;
             case 302:
                 // Cater for sites with broken HTTP redirect implementations
                 if ($client->strict_redirect()) {
                     $follow_method = $request->method();
                 } else {
                     $follow_method = Request::GET;
                 }
                 break;
         }
         // Prepare the additional request, copying any follow_headers that were present on the original request
         $orig_headers = $request->headers()->getArrayCopy();
         $follow_header_keys = array_intersect(array_keys($orig_headers), $client->follow_headers());
         $follow_headers = \Arr::extract($orig_headers, $follow_header_keys);
         $follow_request = Request::factory($response->headers('Location'))->method($follow_method)->headers($follow_headers);
         if ($follow_method !== Request::GET) {
             $follow_request->body($request->body());
         }
         return $follow_request;
     }
     return NULL;
 }
Example #19
0
 public static function reset()
 {
     self::$headers = null;
     self::$base_uri = null;
     self::$uri = null;
 }