| @@ -1,131 +1,27 @@ | ||
| 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; |
| 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 () */ | |
| 10 | 18 | |
| 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); // 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 | - | |
| 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 | 19 | function need_curl ($args) { |
| 124 | 20 | $args = wp_parse_args($args, array( |
| 125 | 21 | 'authentication' => NULL, |
| 126 | 22 | )); |
| 127 | - | |
| 23 | + | |
| 128 | 24 | switch ($args['authentication']) : |
| 129 | 25 | case 'digest' : |
| 130 | 26 | $use = true; |
| 131 | 27 | break; |
| @@ -132,38 +28,46 @@ | ||
| 132 | 28 | default : |
| 133 | 29 | $use = false; |
| 134 | 30 | endswitch; |
| 135 | 31 | return $use; |
| 136 | - } /* FeedWordPressHTTPAuthenticator::need_curl () */ | |
| 137 | - | |
| 32 | + } /* FeedWordPerssHTTPAuthenticator::need_curl () */ | |
| 33 | + | |
| 138 | 34 | function digest_do_it ($use, $args) { |
| 139 | - return $this->if_curl($use, $args, true); | |
| 35 | + if ($this->need_curl($args)) : | |
| 36 | + $use = true; | |
| 37 | + endif; | |
| 38 | + return $use; | |
| 140 | 39 | } /* FeedWordPerssHTTPAuthenticator::digest_do_it () */ |
| 141 | - | |
| 40 | + | |
| 142 | 41 | 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 | 42 | if ($this->need_curl($args)) : |
| 148 | - $use = $what; | |
| 43 | + $use = false; | |
| 149 | 44 | endif; |
| 150 | - return $use; | |
| 151 | - } /* FeedWordPressHTTPAuthenticator::if_curl () */ | |
| 152 | - | |
| 153 | - function set_auth_options ($handle, $r, $url) { | |
| 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) { | |
| 154 | 58 | if ('digest'==$this->args['authentication']) : |
| 155 | 59 | curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); |
| 156 | 60 | endif; |
| 157 | - | |
| 158 | - if ( !is_null($this->args['username'])) : | |
| 61 | + | |
| 62 | + if (!is_null($this->args['username'])) : | |
| 159 | 63 | $userPass = $this->args['username']; |
| 160 | - if ( !is_null($this->args['password'])) : | |
| 64 | + if (!is_null($this->args['password'])) : | |
| 161 | 65 | $userPass .= ':'.$this->args['password']; |
| 162 | 66 | endif; |
| 163 | - | |
| 67 | + | |
| 164 | 68 | curl_setopt($handle, CURLOPT_USERPWD, $userPass); |
| 165 | 69 | endif; |
| 166 | 70 | |
| 167 | 71 | } /* FeedWordPressHTTPAuthenticator::set_auth_options() */ |
| 168 | - | |
| 72 | + | |
| 169 | 73 | } /* class FeedWordPressHTTPAuthenticator */ |