| @@ -1,27 +1,31 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | class FeedWordPressHTTPAuthenticator { |
| 3 | 3 | var $args = array(); |
| 4 | - | |
| 4 | + | |
| 5 | + function FeedWordPressHTTPAuthenticator () { | |
| 6 | + self::__construct(); | |
| 7 | + } | |
| 8 | + | |
| 5 | 9 | function __construct () { |
| 6 | - add_filter('use_curl_transport', array($this, 'digest_do_it'), 2, 1000); | |
| 10 | + add_filter('use_curl_transport', array(&$this, 'digest_do_it'), 2, 1000); | |
| 7 | 11 | foreach (array('fsockopen', 'fopen', 'streams', 'http_extension') as $transport) : |
| 8 | - add_filter("use_{$transport}_transport", array($this, 'digest_dont'), 2, 1000); | |
| 12 | + add_filter("use_{$transport}_transport", array(&$this, 'digest_dont'), 2, 1000); | |
| 9 | 13 | 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); | |
| 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); | |
| 13 | 17 | } /* FeedWordPressHTTPAuthenticator::__construct () */ |
| 14 | 18 | |
| 15 | 19 | function methods_available () { |
| 16 | 20 | $methods = array('-' => 'None'); |
| 17 | - | |
| 21 | + | |
| 18 | 22 | if ($this->have_curl(array('authentication' => 'digest'))) : |
| 19 | 23 | $methods = array_merge(array( |
| 20 | 24 | 'digest' => 'Digest', |
| 21 | 25 | ), $methods); |
| 22 | 26 | endif; |
| 23 | - | |
| 27 | + | |
| 24 | 28 | if ( |
| 25 | 29 | $this->have_curl(array('authentication' => 'basic')) |
| 26 | 30 | or $this->have_streams(array('authentication' => 'basic')) |
| 27 | 31 | ) : |
| @@ -28,9 +32,9 @@ | ||
| 28 | 32 | $methods = array_merge(array( |
| 29 | 33 | 'basic' => 'Basic', |
| 30 | 34 | ), $methods); |
| 31 | 35 | endif; |
| 32 | - | |
| 36 | + | |
| 33 | 37 | return $methods; |
| 34 | 38 | } |
| 35 | 39 | |
| 36 | 40 | function pre_http_request ($pre, $args, $url) { |
| @@ -38,78 +42,74 @@ | ||
| 38 | 42 | 'authentication' => NULL, |
| 39 | 43 | 'username' => NULL, |
| 40 | 44 | 'password' => NULL, |
| 41 | 45 | )); |
| 42 | - | |
| 46 | + | |
| 43 | 47 | // 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. | |
| 48 | + if (!is_null($this->args['authentication'])) : | |
| 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; | |
| 60 | 55 | 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']; | |
| 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; | |
| 70 | 82 | 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); | |
| 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; | |
| 94 | 88 | endif; |
| 95 | - elseif (is_callable('WP_Http', '_getTransport')) : | |
| 96 | - $transports = WP_Http::_getTransport($args); // absolutely deprecated & removed. (gwyneth 20230920) | |
| 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 | - | |
| 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 | + endif; | |
| 112 | 112 | return $pre; |
| 113 | 113 | } /* FeedWordPressHTTPAuthenticator::pre_http_request () */ |
| 114 | 114 | |
| 115 | 115 | function have_curl ($args, $url = NULL) { |
| @@ -114,18 +114,18 @@ | ||
| 114 | 114 | |
| 115 | 115 | function have_curl ($args, $url = NULL) { |
| 116 | 116 | return WP_Http_Curl::test($args); |
| 117 | 117 | } |
| 118 | - | |
| 118 | + | |
| 119 | 119 | function have_streams ($args, $url = NULL) { |
| 120 | 120 | return WP_Http_Streams::test($args); |
| 121 | 121 | } |
| 122 | - | |
| 122 | + | |
| 123 | 123 | function need_curl ($args) { |
| 124 | 124 | $args = wp_parse_args($args, array( |
| 125 | 125 | 'authentication' => NULL, |
| 126 | 126 | )); |
| 127 | - | |
| 127 | + | |
| 128 | 128 | switch ($args['authentication']) : |
| 129 | 129 | case 'digest' : |
| 130 | 130 | $use = true; |
| 131 | 131 | break; |
| @@ -133,17 +133,17 @@ | ||
| 133 | 133 | $use = false; |
| 134 | 134 | endswitch; |
| 135 | 135 | return $use; |
| 136 | 136 | } /* FeedWordPressHTTPAuthenticator::need_curl () */ |
| 137 | - | |
| 137 | + | |
| 138 | 138 | function digest_do_it ($use, $args) { |
| 139 | 139 | return $this->if_curl($use, $args, true); |
| 140 | 140 | } /* FeedWordPerssHTTPAuthenticator::digest_do_it () */ |
| 141 | - | |
| 141 | + | |
| 142 | 142 | function digest_dont ($use, $args) { |
| 143 | 143 | return $this->if_curl($use, $args, false); |
| 144 | 144 | } /* FeedWordPressHTTPAuthenticator::digest_dont () */ |
| 145 | - | |
| 145 | + | |
| 146 | 146 | function if_curl ($use, $args, $what) { |
| 147 | 147 | if ($this->need_curl($args)) : |
| 148 | 148 | $use = $what; |
| 149 | 149 | endif; |
| @@ -148,22 +148,22 @@ | ||
| 148 | 148 | $use = $what; |
| 149 | 149 | endif; |
| 150 | 150 | return $use; |
| 151 | 151 | } /* FeedWordPressHTTPAuthenticator::if_curl () */ |
| 152 | - | |
| 153 | - function set_auth_options ($handle, $r, $url) { | |
| 152 | + | |
| 153 | + function set_auth_options (&$handle) { | |
| 154 | 154 | if ('digest'==$this->args['authentication']) : |
| 155 | 155 | curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
| 156 | 156 | endif; |
| 157 | - | |
| 158 | - if ( !is_null($this->args['username'])) : | |
| 157 | + | |
| 158 | + if (!is_null($this->args['username'])) : | |
| 159 | 159 | $userPass = $this->args['username']; |
| 160 | - if ( !is_null($this->args['password'])) : | |
| 160 | + if (!is_null($this->args['password'])) : | |
| 161 | 161 | $userPass .= ':'.$this->args['password']; |
| 162 | 162 | endif; |
| 163 | - | |
| 163 | + | |
| 164 | 164 | curl_setopt($handle, CURLOPT_USERPWD, $userPass); |
| 165 | 165 | endif; |
| 166 | 166 | |
| 167 | 167 | } /* FeedWordPressHTTPAuthenticator::set_auth_options() */ |
| 168 | - | |
| 168 | + | |
| 169 | 169 | } /* class FeedWordPressHTTPAuthenticator */ |