function linkback_get_target($target) { // Resolve target (https://github.com/converspace/webmention/issues/43) $request = HTTPClient::start(); try { $response = $request->head($target); } catch (Exception $ex) { return NULL; } try { $notice = Notice::fromUri($response->getEffectiveUrl()); } catch (UnknownUriException $ex) { preg_match('/\\/notice\\/(\\d+)(?:#.*)?$/', $response->getEffectiveUrl(), $match); $notice = Notice::getKV('id', $match[1]); } if ($notice instanceof Notice && $notice->isLocal()) { return $notice; } else { $user = User::getKV('uri', $response->getEffectiveUrl()); if (!$user) { preg_match('/\\/user\\/(\\d+)(?:#.*)?$/', $response->getEffectiveUrl(), $match); $user = User::getKV('id', $match[1]); } if (!$user) { preg_match('/\\/([^\\/\\?#]+)(?:#.*)?$/', $response->getEffectiveUrl(), $match); if (linkback_lenient_target_match(common_profile_url($match[1]), $response->getEffectiveUrl())) { $user = User::getKV('nickname', $match[1]); } } if ($user instanceof User) { return $user; } } return NULL; }
function onStartShowSections($action) { $name = $action->trimmed('action'); if ($name == 'tag') { $taginput = $action->trimmed('tag'); $tag = common_canonical_tag($taginput); if (!empty($tag)) { $url = sprintf('http://hashtags.wikia.com/index.php?title=%s&action=render', urlencode($tag)); $editurl = sprintf('http://hashtags.wikia.com/index.php?title=%s&action=edit', urlencode($tag)); $request = HTTPClient::start(); $response = $request->get($url); $html = $response->getBody(); $action->elementStart('div', array('id' => 'wikihashtags', 'class' => 'section')); if ($response->isOk() && !empty($html)) { $action->element('style', null, "span.editsection { display: none }\n" . "table.toc { display: none }"); $action->raw($html); $action->elementStart('p'); $action->element('a', array('href' => $editurl, 'title' => sprintf(_m('Edit the article for #%s on WikiHashtags'), $tag)), _m('Edit')); $action->element('a', array('href' => 'http://www.gnu.org/copyleft/fdl.html', 'title' => _m('Shared under the terms of the GNU Free Documentation License'), 'rel' => 'license'), _m('GNU FDL')); $action->elementEnd('p'); } else { $action->element('a', array('href' => $editurl), sprintf(_m('Start the article for #%s on WikiHashtags'), $tag)); } $action->elementEnd('div'); } } return true; }
function httpRequest($endpoint, $poststr) { $request = HTTPClient::start(); $request->setConfig(array('follow_redirects' => true, 'connect_timeout' => 120, 'timeout' => 120, 'ssl_verify_peer' => false, 'ssl_verify_host' => false)); // Turn signed request query string back into an array parse_str($poststr, $postdata); return $request->post($endpoint, null, $postdata); }
function ping_broadcast_notice($notice) { if ($notice->is_local != Notice::LOCAL_PUBLIC && $notice->is_local != Notice::LOCAL_NONPUBLIC) { return true; } # Array of servers, URL => type $notify = common_config('ping', 'notify'); $profile = $notice->getProfile(); $tags = ping_notice_tags($notice); foreach ($notify as $notify_url => $type) { switch ($type) { case 'xmlrpc': case 'extended': $req = xmlrpc_encode_request('weblogUpdates.ping', array($profile->nickname, common_local_url('showstream', array('nickname' => $profile->nickname)), common_local_url('shownotice', array('notice' => $notice->id)), common_local_url('userrss', array('nickname' => $profile->nickname)), $tags)); $request = HTTPClient::start(); $request->setConfig('connect_timeout', common_config('ping', 'timeout')); $request->setConfig('timeout', common_config('ping', 'timeout')); try { $httpResponse = $request->post($notify_url, array('Content-Type: text/xml'), $req); } catch (Exception $e) { common_log(LOG_ERR, "Exception pinging {$notify_url}: " . $e->getMessage()); continue; } if (!$httpResponse || mb_strlen($httpResponse->getBody()) == 0) { common_log(LOG_WARNING, "XML-RPC empty results for ping ({$notify_url}, {$notice->id}) "); continue; } $response = xmlrpc_decode($httpResponse->getBody()); if (is_array($response) && xmlrpc_is_fault($response)) { common_log(LOG_WARNING, "XML-RPC error for ping ({$notify_url}, {$notice->id}) " . "{$response['faultString']} ({$response['faultCode']})"); } else { common_log(LOG_INFO, "Ping success for {$notify_url} {$notice->id}"); } break; case 'get': case 'post': $args = array('name' => $profile->nickname, 'url' => common_local_url('showstream', array('nickname' => $profile->nickname)), 'changesURL' => common_local_url('userrss', array('nickname' => $profile->nickname))); $fetcher = Auth_Yadis_Yadis::getHTTPFetcher(); if ($type === 'get') { $result = $fetcher->get($notify_url . '?' . http_build_query($args), array('User-Agent: StatusNet/' . STATUSNET_VERSION)); } else { $result = $fetcher->post($notify_url, http_build_query($args), array('User-Agent: StatusNet/' . STATUSNET_VERSION)); } if ($result->status != '200') { common_log(LOG_WARNING, "Ping error for '{$notify_url}' ({$notice->id}): " . "{$result->body}"); } else { common_log(LOG_INFO, "Ping success for '{$notify_url}' ({$notice->id}): " . "'{$result->body}'"); } break; default: common_log(LOG_WARNING, 'Unknown notify type for ' . $notify_url . ': ' . $type); } } return true; }
function doSpotifyLookup($uri, $isArtist) { $request = HTTPClient::start(); $response = $request->get('http://ws.spotify.com/lookup/1/?uri=' . $uri); if ($response->isOk()) { $xml = simplexml_load_string($response->getBody()); if ($isArtist) { return $xml->name; } else { return $xml->artist->name . ' - ' . $xml->name; } } }
function mention_url_representative_hcard($url, $fn = null, $mf2 = null) { if (!$mf2) { $request = HTTPClient::start(); try { $response = $request->get($url); } catch (Exception $ex) { return null; } $url = $response->getEffectiveUrl(); $mf2 = new Mf2\Parser($response->getBody(), $url); $mf2 = $mf2->parse(); } $hcard = null; if (!empty($mf2['items'])) { $hcards = array(); foreach ($mf2['items'] as $item) { if (!in_array('h-card', $item['type'])) { continue; } // We found a match, return it immediately if (isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) { $hcard = $item['properties']; break; } // Let's keep all the hcards for later, to return one of them at least $hcards[] = $item['properties']; } // No match immediately for the url we expected, but there were h-cards found if (count($hcards) > 0) { $hcard = $hcards[0]; } } if (!$hcard && $fn) { $hcard = array('name' => array($fn)); } if (!$hcard && $response) { preg_match('/<title>([^<]+)/', $response->getBody(), $match); $hcard = array('name' => array($match[1])); } if ($hcard && !$hcard['url']) { $hcard['url'] = array($url); } return $hcard; }
function handle($notice) { $profile = Profile::staticGet($notice->profile_id); $this->log(LOG_INFO, "Posting Notice " . $notice->id . " from " . $profile->nickname); if (!$notice->is_local) { $this->log(LOG_INFO, "Skipping remote notice"); return "skipped"; } # # Build an Atom message from the notice # $noticeurl = common_local_url('shownotice', array('notice' => $notice->id)); $msg = $profile->nickname . ': ' . $notice->content; $atom = "<entry xmlns='http://www.w3.org/2005/Atom'>\n"; $atom .= "<apisource>" . common_config('enjit', 'source') . "</apisource>\n"; $atom .= "<source>\n"; $atom .= "<title>" . $profile->nickname . " - " . common_config('site', 'name') . "</title>\n"; $atom .= "<link href='" . $profile->profileurl . "'/>\n"; $atom .= "<link rel='self' type='application/rss+xml' href='" . common_local_url('userrss', array('nickname' => $profile->nickname)) . "'/>\n"; $atom .= "<author><name>" . $profile->nickname . "</name></author>\n"; $atom .= "<icon>" . $profile->avatarUrl(AVATAR_PROFILE_SIZE) . "</icon>\n"; $atom .= "</source>\n"; $atom .= "<title>" . htmlspecialchars($msg) . "</title>\n"; $atom .= "<summary>" . htmlspecialchars($msg) . "</summary>\n"; $atom .= "<link rel='alternate' href='" . $noticeurl . "' />\n"; $atom .= "<id>" . $notice->uri . "</id>\n"; $atom .= "<published>" . common_date_w3dtf($notice->created) . "</published>\n"; $atom .= "<updated>" . common_date_w3dtf($notice->modified) . "</updated>\n"; $atom .= "</entry>\n"; $url = common_config('enjit', 'apiurl') . "/submit/" . common_config('enjit', 'apikey'); $data = array('msg' => $atom); # # POST the message to $config['enjit']['apiurl'] # $request = HTTPClient::start(); $response = $request->post($url, null, $data); return $response->isOk(); }
/** * Reports statistics to headquarters * * Posts statistics to a reporting server. * * @return void */ function report() { // XXX: Use OICU2 and OAuth to make authorized requests $reporturl = common_config('snapshot', 'reporturl'); try { $request = HTTPClient::start(); $request->post($reporturl, null, $this->stats); } catch (Exception $e) { common_log(LOG_WARNING, "Error in snapshot: " . $e->getMessage()); } }
/** * Make an HTTP GET request with OAuth headers and return an HTTPResponse * with the returned body and codes. * * @param string $url * @return HTTPResponse * * @throws Exception on low-level network error */ protected function httpGet($url) { $headers = array('Authorization: ' . $this->authHeader()); $client = HTTPClient::start(); return $client->get($url, $headers); }
function getGeonames($method, $params) { if ($this->lastTimeout && time() - $this->lastTimeout < $this->timeoutWindow) { // TRANS: Exception thrown when a geo names service is not used because of a recent timeout. throw new Exception(_m('Skipping due to recent web service timeout.')); } $client = HTTPClient::start(); $client->setConfig('connect_timeout', $this->timeout); $client->setConfig('timeout', $this->timeout); try { $result = $client->get($this->wsUrl($method, $params)); } catch (Exception $e) { common_log(LOG_ERR, __METHOD__ . ": " . $e->getMessage()); $this->lastTimeout = time(); throw $e; } if (!$result->isOk()) { // TRANS: Exception thrown when a geo names service does not return an expected response. // TRANS: %s is an HTTP error code. throw new Exception(sprintf(_m('HTTP error code %s.'), $result->getStatus())); } $body = $result->getBody(); if (empty($body)) { // TRANS: Exception thrown when a geo names service returns an empty body. throw new Exception(_m('Empty HTTP body in response.')); } // This will throw an exception if the XML is mal-formed $document = new SimpleXMLElement($body); // No children, usually no results $children = $document->children(); if (count($children) == 0) { return array(); } if (isset($document->status)) { // TRANS: Exception thrown when a geo names service return a specific error number and error text. // TRANS: %1$s is an error code, %2$s is an error message. throw new Exception(sprintf(_m('Error #%1$s ("%2$s").'), $document->status['value'], $document->status['message'])); } // Array of elements, >0 elements return $document->geoname; }
/** * Make an HTTP request * * @param string $url Where to make the request * @param array $params post parameters * * @return mixed the request * @throws BasicAuthException */ function httpRequest($url, $params = null, $auth = true) { $request = HTTPClient::start(); $request->setConfig(array('follow_redirects' => true, 'connect_timeout' => 120, 'timeout' => 120, 'ssl_verify_peer' => false, 'ssl_verify_host' => false)); if ($auth) { $request->setAuth($this->screen_name, $this->password); } 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); } $code = $response->getStatus(); if ($code < 200 || $code >= 400) { throw new BasicAuthException($response->getBody(), $code); } return $response->getBody(); }
/** * Inject API key into query before sending out... * * @param string $url * @return HTTPResponse */ protected function query($url) { // http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/shorten $params = http_build_query(array('login' => $this->getLogin(), 'apiKey' => $this->getApiKey()), '', '&'); $serviceUrl = sprintf($this->serviceUrl, $url) . '&' . $params; $request = HTTPClient::start(); return $request->get($serviceUrl); }
/** * 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 */ protected function fetchRemoteUrl($url, $filename) { common_debug(__METHOD__ . " - Fetching Twitter avatar: {$url} to {$filename}"); $request = HTTPClient::start(); $request->setConfig('connect_timeout', 3); // I had problems with throttling $request->setConfig('timeout', 6); // and locking the process sucks. $response = $request->get($url); if ($response->isOk()) { if (!file_put_contents($filename, $response->getBody())) { throw new ServerException('Failed saving fetched file'); } } else { throw new Exception('Unexpected HTTP status code'); } return true; }
/** * Utility to post a request and get a response URL * * @param string $url URL to fetch * @param array $data post parameters * * @return string response body * * @todo rename to code-standard httpPost() */ protected function http_post($url, $data) { $request = HTTPClient::start(); $response = $request->post($url, null, $data); return $response->getBody(); }
function getGeonames($method, $params) { if ($this->lastTimeout && time() - $this->lastTimeout < $this->timeoutWindow) { throw new Exception("skipping due to recent web service timeout"); } $client = HTTPClient::start(); $client->setConfig('connect_timeout', $this->timeout); $client->setConfig('timeout', $this->timeout); try { $result = $client->get($this->wsUrl($method, $params)); } catch (Exception $e) { common_log(LOG_ERR, __METHOD__ . ": " . $e->getMessage()); $this->lastTimeout = time(); throw $e; } if (!$result->isOk()) { throw new Exception("HTTP error code " . $result->getStatus()); } $body = $result->getBody(); if (empty($body)) { throw new Exception("Empty HTTP body in response"); } // This will throw an exception if the XML is mal-formed $document = new SimpleXMLElement($body); // No children, usually no results $children = $document->children(); if (count($children) == 0) { return array(); } if (isset($document->status)) { throw new Exception("Error #" . $document->status['value'] . " ('" . $document->status['message'] . "')"); } // Array of elements, >0 elements return $document->geoname; }
/** * 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) { common_debug($this->name() . " - Fetching Twitter avatar: {$url}"); $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; } return true; }
/** * Report local notices to GeoURL.org when they're created * * @param Notice &$notice queued notice * * @return boolean event handler flag */ function onHandleQueuedNotice(&$notice) { if ($notice->is_local == 1) { $request = HTTPClient::start(); $url = common_local_url('shownotice', array('notice' => $notice->id)); try { $request->post($this->ping, null, array('p' => $url)); } catch (HTTP_Request2_Exception $e) { common_log(LOG_WARNING, "GeoURL.org ping failed for '{$url}' ({$this->ping})"); } } return true; }
/** * Given a user's WikiHow profile URL, find their avatar. * * @param string $profileUrl user page on the wiki * * @return array of data; possible members: * 'avatar' => full URL to avatar image * * @throws Exception on various low-level failures * * @todo pull location, web site, and about sections -- they aren't currently marked up cleanly. */ private function fetchProfile($profileUrl) { $client = HTTPClient::start(); $response = $client->get($profileUrl); if (!$response->isOk()) { throw new Exception("WikiHow profile page fetch failed."); // HTTP error response already logged. return false; } // Suppress warnings during HTML parsing; non-well-formed bits will // spew horrible warning everywhere even though it works fine. $old = error_reporting(); error_reporting($old & ~E_WARNING); $dom = new DOMDocument(); $ok = $dom->loadHTML($response->getBody()); error_reporting($old); if (!$ok) { throw new Exception("HTML parse failure during check for WikiHow avatar."); return false; } $data = array(); $avatar = $dom->getElementById('avatarULimg'); if ($avatar) { $src = $avatar->getAttribute('src'); $base = new Net_URL2($profileUrl); $absolute = $base->resolve($src); $avatarUrl = strval($absolute); common_log(LOG_DEBUG, "WikiHow avatar found for {$profileUrl} - {$avatarUrl}"); $data['avatar'] = $avatarUrl; } return $data; }
* the documentation and/or other materials provided with the * distribution. * * * Neither the name of Abhinav Singh nor the names of his * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRIC * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */ if ($argc < 2) { echo "Usage: {$argv['0']} url\n"; exit; } require_once 'jaxl.php'; JAXLLogger::$level = JAXL_DEBUG; require_once JAXL_CWD . '/http/http_client.php'; $request = new HTTPClient($argv[1]); $request->start();
function pingback($url, $endpoint) { $args = array($this->notice->uri, $url); if (!extension_loaded('xmlrpc')) { if (!dl('xmlrpc.so')) { common_log(LOG_ERR, "Can't pingback; xmlrpc extension not available."); return; } } $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}): " . "{$response['faultString']} ({$response['faultCode']})"); } else { common_log(LOG_INFO, "Pingback success for '{$url}' ({$endpoint}): " . "'{$response}'"); } } catch (HTTP_Request2_Exception $e) { common_log(LOG_WARNING, "Pingback request failed for '{$url}' ({$endpoint})"); } }
function httpRequest($url) { $request = HTTPClient::start(); $request->setConfig(array('follow_redirects' => true, 'connect_timeout' => 120, 'timeout' => 120, 'ssl_verify_peer' => false, 'ssl_verify_host' => false)); return $request->get($url); }
/** * Hit some web API and return data on success. * @param string $url * @param array $params * @return string */ protected static function http($url, $params = array()) { $client = HTTPClient::start(); if ($params) { $query = http_build_query($params, null, '&'); if (strpos($url, '?') === false) { $url .= '?' . $query; } else { $url .= '&' . $query; } } $response = $client->get($url); if ($response->isOk()) { return $response->getBody(); } else { throw new Exception('Bad HTTP response code: ' . $response->getStatus()); } }