| 1 |
<?php |
| 2 |
class FeedWordPressHTTPAuthenticator { |
| 3 |
var $args = array(); |
| 4 |
|
| 5 |
function FeedWordPressHTTPAuthenticator () { |
| 6 |
self::__construct(); |
| 7 |
} |
| 8 |
|
| 9 |
function __construct () { |
| 10 |
add_filter('use_curl_transport', array(&$this, 'digest_do_it'), 2, 1000); |
| 11 |
foreach (array('fsockopen', 'fopen', 'streams', 'http_extension') as $transport) : |
| 12 |
add_filter("use_{$transport}_transport", array(&$this, 'digest_dont'), 2, 1000); |
| 13 |
endforeach; |
| 14 |
|
| 15 |
add_filter('pre_http_request', array(&$this, 'pre_http_request'), 10, 3); |
| 16 |
add_action('http_api_curl', array(&$this, 'set_auth_options'), 1000, 1); |
| 17 |
} /* FeedWordPerssHTTPAuthenticator::__construct () */ |
| 18 |
|
| 19 |
function need_curl ($args) { |
| 20 |
$args = wp_parse_args($args, array( |
| 21 |
'authentication' => NULL, |
| 22 |
)); |
| 23 |
|
| 24 |
switch ($args['authentication']) : |
| 25 |
case 'digest' : |
| 26 |
$use = true; |
| 27 |
break; |
| 28 |
default : |
| 29 |
$use = false; |
| 30 |
endswitch; |
| 31 |
return $use; |
| 32 |
} /* FeedWordPerssHTTPAuthenticator::need_curl () */ |
| 33 |
|
| 34 |
function digest_do_it ($use, $args) { |
| 35 |
if ($this->need_curl($args)) : |
| 36 |
$use = true; |
| 37 |
endif; |
| 38 |
return $use; |
| 39 |
} /* FeedWordPerssHTTPAuthenticator::digest_do_it () */ |
| 40 |
|
| 41 |
function digest_dont ($use, $args) { |
| 42 |
if ($this->need_curl($args)) : |
| 43 |
$use = false; |
| 44 |
endif; |
| 45 |
return false; |
| 46 |
} /* FeedWordPerssHTTPAuthenticator::digest_dont () */ |
| 47 |
|
| 48 |
function pre_http_request ($pre, $args, $url) { |
| 49 |
$this->args = wp_parse_args($args, array( |
| 50 |
'authentication' => NULL, |
| 51 |
'username' => NULL, |
| 52 |
'password' => NULL, |
| 53 |
)); |
| 54 |
return $pre; |
| 55 |
} |
| 56 |
|
| 57 |
function set_auth_options (&$handle) { |
| 58 |
if ('digest'==$this->args['authentication']) : |
| 59 |
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
| 60 |
endif; |
| 61 |
|
| 62 |
if (!is_null($this->args['username'])) : |
| 63 |
$userPass = $this->args['username']; |
| 64 |
if (!is_null($this->args['password'])) : |
| 65 |
$userPass .= ':'.$this->args['password']; |
| 66 |
endif; |
| 67 |
|
| 68 |
curl_setopt($handle, CURLOPT_USERPWD, $userPass); |
| 69 |
endif; |
| 70 |
|
| 71 |
} /* FeedWordPressHTTPAuthenticator::set_auth_options() */ |
| 72 |
|
| 73 |
} /* class FeedWordPressHTTPAuthenticator */ |
| 74 |
|