| 1 |
<?php |
| 2 |
class FeedWordPressHTTPAuthenticator { |
| 3 |
var $args = array(); |
| 4 |
|
| 5 |
function __construct () { |
| 6 |
add_filter('use_curl_transport', array($this, 'digest_do_it'), 2, 1000); |
| 7 |
foreach (array('fsockopen', 'fopen', 'streams', 'http_extension') as $transport) : |
| 8 |
add_filter("use_{$transport}_transport", array($this, 'digest_dont'), 2, 1000); |
| 9 |
endforeach; |
| 10 |
|
| 11 |
add_filter('pre_http_request', array($this, 'pre_http_request'), 10, 3); |
| 12 |
add_action('http_api_curl', array($this, 'set_auth_options'), 1000, 3); |
| 13 |
} /* FeedWordPressHTTPAuthenticator::__construct () */ |
| 14 |
|
| 15 |
function methods_available () { |
| 16 |
$methods = array('-' => 'None'); |
| 17 |
|
| 18 |
if ($this->have_curl(array('authentication' => 'digest'))) : |
| 19 |
$methods = array_merge(array( |
| 20 |
'digest' => 'Digest', |
| 21 |
), $methods); |
| 22 |
endif; |
| 23 |
|
| 24 |
if ( |
| 25 |
$this->have_curl(array('authentication' => 'basic')) |
| 26 |
or $this->have_streams(array('authentication' => 'basic')) |
| 27 |
) : |
| 28 |
$methods = array_merge(array( |
| 29 |
'basic' => 'Basic', |
| 30 |
), $methods); |
| 31 |
endif; |
| 32 |
|
| 33 |
return $methods; |
| 34 |
} |
| 35 |
|
| 36 |
function pre_http_request ($pre, $args, $url) { |
| 37 |
$this->args = wp_parse_args($args, array( |
| 38 |
'authentication' => NULL, |
| 39 |
'username' => NULL, |
| 40 |
'password' => NULL, |
| 41 |
)); |
| 42 |
|
| 43 |
// Ruh roh... |
| 44 |
$auth = $this->args['authentication']; |
| 45 |
if (is_null($auth) or (strlen($auth) == 0)) : |
| 46 |
$this->args['authentication'] = '-'; |
| 47 |
endif; |
| 48 |
|
| 49 |
switch ($this->args['authentication']) : |
| 50 |
case '-' : |
| 51 |
// No HTTP Auth method. Remove this stuff. |
| 52 |
$this->args['authentication'] = NULL; |
| 53 |
$this->args['username'] = NULL; |
| 54 |
$this->args['password'] = NULL; |
| 55 |
break; |
| 56 |
case 'basic' : |
| 57 |
if ($this->have_curl($args, $url)) : |
| 58 |
// Don't need to do anything. http_api_curl hook takes care |
| 59 |
// of it. |
| 60 |
break; |
| 61 |
elseif ($this->have_streams($args, $url)) : |
| 62 |
// curl has a nice native way to jam in the username and |
| 63 |
// passwd but streams and fsockopen do not. So we have to |
| 64 |
// make a recursive call with the credentials in the URL. |
| 65 |
// Wee ha! |
| 66 |
$method = $this->args['authentication']; |
| 67 |
$credentials = $this->args['username']; |
| 68 |
if (!is_null($this->args['password'])) : |
| 69 |
$credentials .= ':'.$args['password']; |
| 70 |
endif; |
| 71 |
|
| 72 |
// Remove these so we don't recurse all the way down |
| 73 |
unset($this->args['authentication']); |
| 74 |
unset($this->args['username']); |
| 75 |
unset($this->args['password']); |
| 76 |
|
| 77 |
$url = preg_replace('!(https?://)!', '$1'.$credentials.'@', $url); |
| 78 |
|
| 79 |
// Subsidiary request |
| 80 |
$pre = wp_remote_request($url, $this->args); |
| 81 |
break; |
| 82 |
endif; |
| 83 |
case 'digest' : |
| 84 |
if ($this->have_curl($args, $url)) : |
| 85 |
// Don't need to do anything. http_api_curl hook takes care |
| 86 |
// of it. |
| 87 |
break; |
| 88 |
endif; |
| 89 |
default : |
| 90 |
if (is_callable('WP_Http', '_get_first_available_transport')) : |
| 91 |
$trans = WP_Http::_get_first_available_transport($args, $url); |
| 92 |
if (!$trans) : |
| 93 |
$trans = WP_Http::_get_first_available_transport(array(), $url); |
| 94 |
endif; |
| 95 |
elseif (is_callable('WP_Http', '_getTransport')) : |
| 96 |
$transports = WP_Http::_getTransport($args); |
| 97 |
$trans = get_class(reset($transports)); |
| 98 |
else : |
| 99 |
$trans = 'HTTP'; |
| 100 |
endif; |
| 101 |
|
| 102 |
$pre = new WP_Error('http_request_failed', |
| 103 |
sprintf( |
| 104 |
__('%s cannot use %s authentication with the %s transport.'), |
| 105 |
__CLASS__, |
| 106 |
$args['authentication'], |
| 107 |
$trans |
| 108 |
) |
| 109 |
); |
| 110 |
endswitch; |
| 111 |
|
| 112 |
return $pre; |
| 113 |
} /* FeedWordPressHTTPAuthenticator::pre_http_request () */ |
| 114 |
|
| 115 |
function have_curl ($args, $url = NULL) { |
| 116 |
return WP_Http_Curl::test($args); |
| 117 |
} |
| 118 |
|
| 119 |
function have_streams ($args, $url = NULL) { |
| 120 |
return WP_Http_Streams::test($args); |
| 121 |
} |
| 122 |
|
| 123 |
function need_curl ($args) { |
| 124 |
$args = wp_parse_args($args, array( |
| 125 |
'authentication' => NULL, |
| 126 |
)); |
| 127 |
|
| 128 |
switch ($args['authentication']) : |
| 129 |
case 'digest' : |
| 130 |
$use = true; |
| 131 |
break; |
| 132 |
default : |
| 133 |
$use = false; |
| 134 |
endswitch; |
| 135 |
return $use; |
| 136 |
} /* FeedWordPressHTTPAuthenticator::need_curl () */ |
| 137 |
|
| 138 |
function digest_do_it ($use, $args) { |
| 139 |
return $this->if_curl($use, $args, true); |
| 140 |
} /* FeedWordPerssHTTPAuthenticator::digest_do_it () */ |
| 141 |
|
| 142 |
function digest_dont ($use, $args) { |
| 143 |
return $this->if_curl($use, $args, false); |
| 144 |
} /* FeedWordPressHTTPAuthenticator::digest_dont () */ |
| 145 |
|
| 146 |
function if_curl ($use, $args, $what) { |
| 147 |
if ($this->need_curl($args)) : |
| 148 |
$use = $what; |
| 149 |
endif; |
| 150 |
return $use; |
| 151 |
} /* FeedWordPressHTTPAuthenticator::if_curl () */ |
| 152 |
|
| 153 |
function set_auth_options ($handle, $r, $url) { |
| 154 |
if ('digest'==$this->args['authentication']) : |
| 155 |
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
| 156 |
endif; |
| 157 |
|
| 158 |
if (!is_null($this->args['username'])) : |
| 159 |
$userPass = $this->args['username']; |
| 160 |
if (!is_null($this->args['password'])) : |
| 161 |
$userPass .= ':'.$this->args['password']; |
| 162 |
endif; |
| 163 |
|
| 164 |
curl_setopt($handle, CURLOPT_USERPWD, $userPass); |
| 165 |
endif; |
| 166 |
|
| 167 |
} /* FeedWordPressHTTPAuthenticator::set_auth_options() */ |
| 168 |
|
| 169 |
} /* class FeedWordPressHTTPAuthenticator */ |
| 170 |
|