diff --git a/library/Requests.php b/library/Requests.php index b24e6d937..6be11bd88 100755 --- a/library/Requests.php +++ b/library/Requests.php @@ -266,6 +266,9 @@ public static function patch($url, $headers, $data = array(), $options = array() * (string|boolean, default: false) * - `auth`: Authentication handler or array of user/password details to use * for Basic authentication + * (Requests_Auth|array|boolean, default: false)* + * - `auth_digest`: Authentication handler or array of user/password details to use + * for Digest authentication* * (Requests_Auth|array|boolean, default: false) * - `proxy`: Proxy details to use for proxy by-passing and authentication * (Requests_Proxy|array|boolean, default: false) @@ -451,6 +454,7 @@ protected static function get_default_options($multirequest = false) { 'type' => self::GET, 'filename' => false, 'auth' => false, + 'auth_digest' => false, 'proxy' => false, 'cookies' => false, 'idn' => true, @@ -491,6 +495,13 @@ protected static function set_defaults(&$url, &$headers, &$data, &$type, &$optio $options['auth']->register($options['hooks']); } + if (is_array($options['auth_digest'])) { + $options['auth_digest'] = new Requests_Auth_Digest($options['auth_digest']); + } + if ($options['auth_digest'] !== false) { + $options['auth_digest']->register($options['hooks']); + } + if (!empty($options['proxy'])) { $options['proxy'] = new Requests_Proxy_HTTP($options['proxy']); } diff --git a/library/Requests/Auth/Digest.php b/library/Requests/Auth/Digest.php new file mode 100644 index 000000000..e8bfc0787 --- /dev/null +++ b/library/Requests/Auth/Digest.php @@ -0,0 +1,82 @@ +user, $this->pass) = $args; + } + } + + /** + * Register the necessary callbacks + * + * @see curl_before_send + * @see fsockopen_header + * @param Requests_Hooks $hooks Hook system + */ + public function register(Requests_Hooks &$hooks) { + $hooks->register('curl.before_send', array(&$this, 'curl_before_send')); + $hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header')); + } + + /** + * Set cURL parameters before the data is sent + * + * @param resource $handle cURL resource + */ + public function curl_before_send(&$handle) { + curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); + curl_setopt($handle, CURLOPT_USERPWD, $this->getAuthString()); + } + + /** + * Add extra headers to the request before sending + * + * @param string $out HTTP header string + */ + public function fsockopen_header(&$out) { + $out .= "Authorization: Digest " . base64_encode($this->getAuthString()) . "\r\n"; + } + + /** + * Get the authentication string (user:pass) + * + * @return string + */ + public function getAuthString() { + return $this->user . ':' . $this->pass; + } +} \ No newline at end of file