diff options
author | Brion Vibber <brion@pobox.com> | 2009-10-28 15:29:20 -0400 |
---|---|---|
committer | Brion Vibber <brion@pobox.com> | 2009-11-02 09:14:15 -0800 |
commit | 5581143bee602dbd5417f532f2b483e58d0a4269 (patch) | |
tree | ed1da24d03d594895c743c8e0a4bd3c46c42c6c7 /plugins | |
parent | 73b9e531bf7cab8ba6642f9cb85e4b37b48706d7 (diff) |
Rebuilt HTTPClient class as an extension of PEAR HTTP_Request2 package, adding redirect handling and convenience functions.
Caching support will be added in future work after unit tests have been added.
* extlib: add PEAR HTTP_Request2 0.4.1 alpha
* extlib: update PEAR Net_URL2 to 0.3.0 beta for HTTP_Request2 compatibility
* moved direct usage of CURL and file_get_contents to HTTPClient class, excluding external-sourced libraries
* adapted GeonamesPlugin for new HTTPResponse interface
Note some plugins haven't been fully tested yet.
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/BlogspamNetPlugin.php | 15 | ||||
-rw-r--r-- | plugins/GeonamesPlugin.php | 16 | ||||
-rw-r--r-- | plugins/LilUrl/LilUrlPlugin.php | 5 | ||||
-rw-r--r-- | plugins/LinkbackPlugin.php | 21 | ||||
-rw-r--r-- | plugins/SimpleUrl/SimpleUrlPlugin.php | 11 | ||||
-rwxr-xr-x | plugins/TwitterBridge/daemons/synctwitterfriends.php | 4 | ||||
-rwxr-xr-x | plugins/TwitterBridge/daemons/twitterstatusfetcher.php | 43 | ||||
-rw-r--r-- | plugins/TwitterBridge/twitter.php | 2 | ||||
-rw-r--r-- | plugins/TwitterBridge/twitterauthorization.php | 2 | ||||
-rw-r--r-- | plugins/TwitterBridge/twitterbasicauthclient.php | 68 | ||||
-rw-r--r-- | plugins/WikiHashtagsPlugin.php | 15 |
11 files changed, 76 insertions, 126 deletions
diff --git a/plugins/BlogspamNetPlugin.php b/plugins/BlogspamNetPlugin.php index c14569746..51236001a 100644 --- a/plugins/BlogspamNetPlugin.php +++ b/plugins/BlogspamNetPlugin.php @@ -22,6 +22,7 @@ * @category Plugin * @package StatusNet * @author Evan Prodromou <evan@status.net> + * @author Brion Vibber <brion@status.net> * @copyright 2009 StatusNet, Inc. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @link http://status.net/ @@ -69,14 +70,12 @@ class BlogspamNetPlugin extends Plugin { $args = $this->testArgs($notice); common_debug("Blogspamnet args = " . print_r($args, TRUE)); - $request = xmlrpc_encode_request('testComment', array($args)); - $context = stream_context_create(array('http' => array('method' => "POST", - 'header' => - "Content-Type: text/xml\r\n". - "User-Agent: " . $this->userAgent(), - 'content' => $request))); - $file = file_get_contents($this->baseUrl, false, $context); - $response = xmlrpc_decode($file); + $requestBody = xmlrpc_encode_request('testComment', array($args)); + + $request = HTTPClient::start(); + $httpResponse = $request->post($this->baseUrl, array('Content-Type: text/xml'), $requestBody); + + $response = xmlrpc_decode($httpResponse->getBody()); if (xmlrpc_is_fault($response)) { throw new ServerException("$response[faultString] ($response[faultCode])", 500); } else { diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php index 80ef44cc9..e18957c36 100644 --- a/plugins/GeonamesPlugin.php +++ b/plugins/GeonamesPlugin.php @@ -74,8 +74,8 @@ class GeonamesPlugin extends Plugin $result = $client->get('http://ws.geonames.org/search?'.$str); - if ($result->code == "200") { - $rj = json_decode($result->body); + if ($result->isOk()) { + $rj = json_decode($result->getBody()); if (count($rj->geonames) > 0) { $n = $rj->geonames[0]; @@ -121,9 +121,9 @@ class GeonamesPlugin extends Plugin $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str); - if ($result->code == "200") { + if ($result->isOk()) { - $rj = json_decode($result->body); + $rj = json_decode($result->getBody()); if (count($rj->geonames) > 0) { @@ -182,9 +182,9 @@ class GeonamesPlugin extends Plugin $result = $client->get('http://ws.geonames.org/findNearbyPlaceNameJSON?'.$str); - if ($result->code == "200") { + if ($result->isOk()) { - $rj = json_decode($result->body); + $rj = json_decode($result->getBody()); if (count($rj->geonames) > 0) { @@ -249,9 +249,9 @@ class GeonamesPlugin extends Plugin $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str); - if ($result->code == "200") { + if ($result->isOk()) { - $rj = json_decode($result->body); + $rj = json_decode($result->getBody()); if (count($rj->geonames) > 0) { diff --git a/plugins/LilUrl/LilUrlPlugin.php b/plugins/LilUrl/LilUrlPlugin.php index 7665b6c1e..852253b02 100644 --- a/plugins/LilUrl/LilUrlPlugin.php +++ b/plugins/LilUrl/LilUrlPlugin.php @@ -58,7 +58,10 @@ class LilUrl extends ShortUrlApi $y = @simplexml_load_string($response); if (!isset($y->body)) return $url; $x = $y->body->p[0]->a->attributes(); - if (isset($x['href'])) return $x['href']; + if (isset($x['href'])) { + common_log(LOG_INFO, __CLASS__ . ": shortened $url to $x[href]"); + return $x['href']; + } return $url; } } diff --git a/plugins/LinkbackPlugin.php b/plugins/LinkbackPlugin.php index 60f7a60c7..915d15c07 100644 --- a/plugins/LinkbackPlugin.php +++ b/plugins/LinkbackPlugin.php @@ -129,18 +129,12 @@ class LinkbackPlugin extends Plugin } } - $request = xmlrpc_encode_request('pingback.ping', $args); - $context = stream_context_create(array('http' => array('method' => "POST", - 'header' => - "Content-Type: text/xml\r\n". - "User-Agent: " . $this->userAgent(), - 'content' => $request))); - $file = file_get_contents($endpoint, false, $context); - if (!$file) { - common_log(LOG_WARNING, - "Pingback request failed for '$url' ($endpoint)"); - } else { - $response = xmlrpc_decode($file); + $request = HTTPClient::start(); + try { + $response = $request->post($endpoint, + array('Content-Type: text/xml'), + xmlrpc_encode_request('pingback.ping', $args)); + $response = xmlrpc_decode($response->getBody()); if (xmlrpc_is_fault($response)) { common_log(LOG_WARNING, "Pingback error for '$url' ($endpoint): ". @@ -150,6 +144,9 @@ class LinkbackPlugin extends Plugin "Pingback success for '$url' ($endpoint): ". "'$response'"); } + } catch (HTTP_Request2_Exception $e) { + common_log(LOG_WARNING, + "Pingback request failed for '$url' ($endpoint)"); } } diff --git a/plugins/SimpleUrl/SimpleUrlPlugin.php b/plugins/SimpleUrl/SimpleUrlPlugin.php index 82d772048..d59d63e47 100644 --- a/plugins/SimpleUrl/SimpleUrlPlugin.php +++ b/plugins/SimpleUrl/SimpleUrlPlugin.php @@ -65,15 +65,6 @@ class SimpleUrlPlugin extends Plugin class SimpleUrl extends ShortUrlApi { protected function shorten_imp($url) { - $curlh = curl_init(); - curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 20); // # seconds to wait - curl_setopt($curlh, CURLOPT_USERAGENT, 'StatusNet'); - curl_setopt($curlh, CURLOPT_RETURNTRANSFER, true); - - curl_setopt($curlh, CURLOPT_URL, $this->service_url.urlencode($url)); - $short_url = curl_exec($curlh); - - curl_close($curlh); - return $short_url; + return $this->http_get($url); } } diff --git a/plugins/TwitterBridge/daemons/synctwitterfriends.php b/plugins/TwitterBridge/daemons/synctwitterfriends.php index ed2bf48a2..671e3c7af 100755 --- a/plugins/TwitterBridge/daemons/synctwitterfriends.php +++ b/plugins/TwitterBridge/daemons/synctwitterfriends.php @@ -152,8 +152,8 @@ class SyncTwitterFriendsDaemon extends ParallelizingDaemon $friends_ids = $client->friendsIds(); } catch (Exception $e) { common_log(LOG_WARNING, $this->name() . - ' - cURL error getting friend ids ' . - $e->getCode() . ' - ' . $e->getMessage()); + ' - error getting friend ids: ' . + $e->getMessage()); return $friends; } diff --git a/plugins/TwitterBridge/daemons/twitterstatusfetcher.php b/plugins/TwitterBridge/daemons/twitterstatusfetcher.php index 81bbbc7c5..b5428316b 100755 --- a/plugins/TwitterBridge/daemons/twitterstatusfetcher.php +++ b/plugins/TwitterBridge/daemons/twitterstatusfetcher.php @@ -109,12 +109,16 @@ class TwitterStatusFetcher extends ParallelizingDaemon $flink->find(); $flinks = array(); + common_log(LOG_INFO, "hello"); while ($flink->fetch()) { if (($flink->noticesync & FOREIGN_NOTICE_RECV) == FOREIGN_NOTICE_RECV) { $flinks[] = clone($flink); + common_log(LOG_INFO, "sync: foreign id $flink->foreign_id"); + } else { + common_log(LOG_INFO, "nothing to sync"); } } @@ -515,31 +519,32 @@ class TwitterStatusFetcher extends ParallelizingDaemon return $id; } + /** + * Fetch a remote avatar image and save to local storage. + * + * @param string $url avatar source URL + * @param string $filename bare local filename for download + * @return bool true on success, false on failure + */ function fetchAvatar($url, $filename) { - $avatarfile = Avatar::path($filename); + common_debug($this->name() . " - Fetching Twitter avatar: $url"); - $out = fopen($avatarfile, 'wb'); - if (!$out) { - common_log(LOG_WARNING, $this->name() . - " - Couldn't open file $filename"); + $request = HTTPClient::start(); + $response = $request->get($url); + if ($response->isOk()) { + $avatarfile = Avatar::path($filename); + $ok = file_put_contents($avatarfile, $response->getBody()); + if (!$ok) { + common_log(LOG_WARNING, $this->name() . + " - Couldn't open file $filename"); + return false; + } + } else { return false; } - common_debug($this->name() . " - Fetching Twitter avatar: $url"); - - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_FILE, $out); - curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0); - $result = curl_exec($ch); - curl_close($ch); - - fclose($out); - - return $result; + return true; } } diff --git a/plugins/TwitterBridge/twitter.php b/plugins/TwitterBridge/twitter.php index 1a5248a9b..3c6803e49 100644 --- a/plugins/TwitterBridge/twitter.php +++ b/plugins/TwitterBridge/twitter.php @@ -215,7 +215,7 @@ function broadcast_basicauth($notice, $flink) try { $status = $client->statusesUpdate($statustxt); - } catch (BasicAuthCurlException $e) { + } catch (HTTP_Request2_Exception $e) { return process_error($e, $flink); } diff --git a/plugins/TwitterBridge/twitterauthorization.php b/plugins/TwitterBridge/twitterauthorization.php index 2a93ff13e..f1daefab1 100644 --- a/plugins/TwitterBridge/twitterauthorization.php +++ b/plugins/TwitterBridge/twitterauthorization.php @@ -125,7 +125,7 @@ class TwitterauthorizationAction extends Action $auth_link = $client->getAuthorizeLink($req_tok); - } catch (TwitterOAuthClientException $e) { + } catch (OAuthClientException $e) { $msg = sprintf('OAuth client cURL error - code: %1s, msg: %2s', $e->getCode(), $e->getMessage()); $this->serverError(_('Couldn\'t link your Twitter account.')); diff --git a/plugins/TwitterBridge/twitterbasicauthclient.php b/plugins/TwitterBridge/twitterbasicauthclient.php index 1040d72fb..d1cf45aec 100644 --- a/plugins/TwitterBridge/twitterbasicauthclient.php +++ b/plugins/TwitterBridge/twitterbasicauthclient.php @@ -32,26 +32,6 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { } /** - * Exception wrapper for cURL errors - * - * @category Integration - * @package StatusNet - * @author Adrian Lang <mail@adrianlang.de> - * @author Brenda Wallace <shiny@cpan.org> - * @author Craig Andrews <candrews@integralblue.com> - * @author Dan Moore <dan@moore.cx> - * @author Evan Prodromou <evan@status.net> - * @author mEDI <medi@milaro.net> - * @author Sarven Capadisli <csarven@status.net> - * @author Zach Copley <zach@status.net> * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 - * @link http://status.net/ - * - */ -class BasicAuthCurlException extends Exception -{ -} - -/** * Class for talking to the Twitter API with HTTP Basic Auth. * * @category Integration @@ -198,45 +178,27 @@ class TwitterBasicAuthClient */ function httpRequest($url, $params = null, $auth = true) { - $options = array( - CURLOPT_RETURNTRANSFER => true, - CURLOPT_FAILONERROR => true, - CURLOPT_HEADER => false, - CURLOPT_FOLLOWLOCATION => true, - CURLOPT_USERAGENT => 'StatusNet', - CURLOPT_CONNECTTIMEOUT => 120, - CURLOPT_TIMEOUT => 120, - CURLOPT_HTTPAUTH => CURLAUTH_ANY, - CURLOPT_SSL_VERIFYPEER => false, - - // Twitter is strict about accepting invalid "Expect" headers - - CURLOPT_HTTPHEADER => array('Expect:') - ); - - if (isset($params)) { - $options[CURLOPT_POST] = true; - $options[CURLOPT_POSTFIELDS] = $params; - } + $request = HTTPClient::start(); + $request->setConfig(array( + 'follow_redirects' => true, + 'connect_timeout' => 120, + 'timeout' => 120, + 'ssl_verifypeer' => false, + )); if ($auth) { - $options[CURLOPT_USERPWD] = $this->screen_name . - ':' . $this->password; + $request->setAuth($this->screen_name, $this->password); } - $ch = curl_init($url); - curl_setopt_array($ch, $options); - $response = curl_exec($ch); - - if ($response === false) { - $msg = curl_error($ch); - $code = curl_errno($ch); - throw new BasicAuthCurlException($msg, $code); + if (isset($params)) { + // Twitter is strict about accepting invalid "Expect" headers + $headers = array('Expect:'); + $response = $request->post($url, $headers, $params); + } else { + $response = $request->get($url); } - curl_close($ch); - - return $response; + return $response->getBody(); } } diff --git a/plugins/WikiHashtagsPlugin.php b/plugins/WikiHashtagsPlugin.php index 0c5649aa4..334fc13ba 100644 --- a/plugins/WikiHashtagsPlugin.php +++ b/plugins/WikiHashtagsPlugin.php @@ -68,14 +68,13 @@ class WikiHashtagsPlugin extends Plugin $editurl = sprintf('http://hashtags.wikia.com/index.php?title=%s&action=edit', urlencode($tag)); - $context = stream_context_create(array('http' => array('method' => "GET", - 'header' => - "User-Agent: " . $this->userAgent()))); - $html = @file_get_contents($url, false, $context); + $request = HTTPClient::start(); + $response = $request->get($url); + $html = $response->getBody(); $action->elementStart('div', array('id' => 'wikihashtags', 'class' => 'section')); - if (!empty($html)) { + if ($response->isOk() && !empty($html)) { $action->element('style', null, "span.editsection { display: none }\n". "table.toc { display: none }"); @@ -100,10 +99,4 @@ class WikiHashtagsPlugin extends Plugin return true; } - - function userAgent() - { - return 'WikiHashtagsPlugin/'.WIKIHASHTAGSPLUGIN_VERSION . - ' StatusNet/' . STATUSNET_VERSION; - } } |