public function fetch() { $remote_archive = new RemoteRequest($this->url); if (Error::is_error($remote_archive->execute())) { throw new Exception('Could not fetch archive at ' . $this->url); } // we should also check content-disposition for filename and the url as fallbacks. // some crazy people like to send application/octet-stream, weirdos! foreach (split("\n", $remote_archive->get_response_headers()) as $line) { if (substr_compare($line, 'Content-Type', 0, 12, true) == 0) { $content_type = $line; break; } } /* Get the MIME type and character set */ preg_match('@Content-Type:\\s+([\\w/\\-+]+)(;\\s+charset=(\\S+))?@i', $content_type, $matches); if (isset($matches[1])) { $mime = $matches[1]; } else { throw new Exception('Could not determine archive type'); } $file = HabariPackages::tempnam(); if (!file_put_contents($file, $remote_archive->get_response_body(), LOCK_EX)) { throw new Exception('Please make the directory ' . dirname($file) . ' writeable by the server'); } $this->md5 = md5_file($file); $this->set_archive_reader($mime, $file); unset($remote_archive); }
private function call($action, DefensioParams $params) { $client = new RemoteRequest($this->build_url($action), 'POST'); $client->set_postdata($params->get_post_data()); if ($client->execute()) { if (self::get_http_status($client->get_response_headers()) == '401') { throw new Exception('Invalid/Unauthorized API Key'); } $response = new DefensioResponse($client->get_response_body()); unset($client); return $response; } else { throw new Exception('Server Not Responding'); } }
public function filter_send_mail($handled, $mail) { if (!$handled) { $headers = array('Accept: application/json', 'Content-Type: application/json', 'X-Postmark-Server-Token: ' . Options::get('postmark__apikey')); $data = array('To' => $mail['to'], 'subject' => $mail['subject'], 'TextBody' => $mail['message'], 'From' => $mail['headers']['From']); $rr = new RemoteRequest('http://api.postmarkapp.com/email', 'POST'); $rr->set_body(json_encode($data)); $rr->add_headers($headers); try { $rr->execute(); EventLog::log(_t('Send message to %s via Postmark', array($mail['to'])), 'info', 'default', null, array($data, $headers)); Session::notice(var_export($rr->get_response_headers(), 1)); } catch (Exception $e) { EventLog::log(_t('Failed to send message to %s via Postmark', array($mail['to'])), 'err', 'default', null, array($e->getMessage(), $data, $headers)); Session::error('There was a problem sending your message. Please contact the site administrators directly.'); } } return true; }
/** * Send a single Pingback * @param string $source_uri The URI source of the ping (here) * @param string $target_uri The URI destination of the ping (there, the site linked to in the content) * @param Post $post The post object that is initiating the ping, used to track the pings that were sent * @todo If receive error code of already pinged, add to the successful. */ public function send_pingback( $source_uri, $target_uri, $post = NULL ) { // RemoteRequest makes it easier to retrieve the headers. try { $rr = new RemoteRequest( $target_uri ); $rr->execute(); if ( ! $rr->executed() ) { return false; } } catch ( Exception $e ) { // log the pingback error EventLog::log( _t( 'Unable to retrieve target, can\'t detect pingback endpoint. (Source: %1$s | Target: %2$s)', array( $source_uri, $target_uri ) ), 'err', 'Pingback' ); return false; } $headers = $rr->get_response_headers(); $body = $rr->get_response_body(); // Find a Pingback endpoint. if ( isset( $headers['X-Pingback'] ) ) { $pingback_endpoint = $headers['X-Pingback']; } elseif ( preg_match( '/<link rel="pingback" href="([^"]+)" ?\/?'.'>/is', $body, $matches ) ) { $pingback_endpoint = $matches[1]; } else { // No Pingback endpoint found. return false; } try { $response = XMLRPCClient::open( $pingback_endpoint )->pingback->ping( $source_uri, $target_uri ); } catch ( Exception $e ) { EventLog::log( _t( 'Invalid Pingback endpoint - %1$s (Source: %2$s | Target: %3$s)', array( $pingback_endpoint, $source_uri, $target_uri ) ), 'err', 'Pingback' ); return false; } if ( isset( $response->faultString ) ) { EventLog::log( _t( 'Pingback error: %1$s - %2$s (Source: %3$s | Target: %4$s)', array( $response->faultCode, $response->faultString, $source_uri, $target_uri ) ), 'err', 'Pingback' ); return false; } else { // The pingback has been registered and is stored as a successful pingback. if ( is_object( $post ) ) { if ( isset( $post->info->pingbacks_successful ) ) { $pingbacks_successful = $post->info->pingbacks_successful; $pingbacks_successful[]= $target_uri; $post->info->pingbacks_successful = $pingbacks_successful; } else { $post->info->pingbacks_successful = array( $target_uri ); } $post->info->commit(); } return true; } }
'another' => 'variable', ) ); $res_get= $rr->execute(); if ( $res_get === TRUE ) { $results[]= array( get_class( $processor ), $rr->get_response_headers(), substr( $rr->get_response_body(), 0 ) ); } else { $results[]= array( get_class( $processor ), $res_get, ); } $rr= new RemoteRequest( 'http://test.habariproject.org/post', 'POST' ); $rr->__set_processor( $processor ); $rr->set_body( 'If you can read this, the test was successful.' ); $res_post= $rr->execute(); if ( $res_post === TRUE ) { $results[]= array( get_class( $processor ), $rr->get_response_headers(), substr( $rr->get_response_body(), 0 ) ); } else { $results[]= array( get_class( $processor ), $res_post, ); } foreach ( $tests as $name => $group ) { print( "<h2>{$name}</h2>\n" ); foreach ( $group as $test ) { $result= eval( 'return (' . $test . ');' ); printf( "<p><strong>%s</strong> == ( %s )</p>\n", bs( $result ), var_export( $test, TRUE ) ); Utils::debug( array_shift( $results ) ); if ( ! $result ) { $tests_failed[$name][]= $test; }
/** * Send a single Pingback * @param string $source_uri The URI source of the ping (here) * @param string $target_uri The URI destination of the ping (there, the site linked to in the content) * @param Post $post The post object that is initiating the ping, used to track the pings that were sent * @todo If receive error code of already pinged, add to the successful. */ public function send_pingback($source_uri, $target_uri, $post = NULL) { // RemoteRequest makes it easier to retrieve the headers. $rr = new RemoteRequest($target_uri); $rr->execute(); if (!$rr->executed()) { return false; } $headers = $rr->get_response_headers(); $body = $rr->get_response_body(); // Find a Pingback endpoint. if (preg_match('/^X-Pingback: (\\S*)/im', $headers, $matches)) { $pingback_endpoint = $matches[1]; } elseif (preg_match('/<link rel="pingback" href="([^"]+)" ?\\/?' . '>/is', $body, $matches)) { $pingback_endpoint = $matches[1]; } else { // No Pingback endpoint found. return false; } try { $response = XMLRPCClient::open($pingback_endpoint)->pingback->ping($source_uri, $target_uri); } catch (Exception $e) { EventLog::log('Invalid Pingback endpoint - ' . $pingback_endpoint . ' (Source: ' . $source_uri . ' | Target: ' . $target_uri . ')', 'info', 'Pingback'); return false; } if (isset($response->faultString)) { EventLog::log($response->faultCode . ' - ' . $response->faultString . ' (Source: ' . $source_uri . ' | Target: ' . $target_uri . ')', 'info', 'Pingback'); return false; } else { // The pingback has been registered and is stored as a successful pingback. if (is_object($post)) { if (isset($post->info->pingbacks_successful)) { $pingbacks_successful = $post->info->pingbacks_successful; $pingbacks_successful[] = $target_uri; $post->info->pingbacks_successful = $pingbacks_successful; } else { $post->info->pingbacks_successful = array($target_uri); } $post->info->commit(); } return true; } }
public function download($source, $destination, $as = null) { echo '<pre>'; var_dump($source, $destination); $rr = new RemoteRequest($source); if ($rr->execute()) { $response = $rr->get_response_body(); $headers = $rr->get_response_headers(); if (isset($headers['Location']) && $headers['Location'] != $source) { // This should probably count redirects and bail after some max value return $this->download($headers['Location'], $destination); } $basename = basename($source); if (isset($as)) { $basename = $as; } if (strpos($headers['Content-Type'], 'text/html') !== false) { if (file_exists($destination . $basename) || mkdir($destination . $basename)) { //Session::notice(_t('Created the "%s" directory', array($basename))); $dom = str_get_html($response); $as = $dom->find('a'); $hrefs = array(); foreach ($as as $a) { $href = rtrim($a->getAttribute('href'), '/'); if (strpos($href, '..') !== false || strpos($href, '/') !== false) { } else { $this->download($source . $href, $destination . $basename . '/'); } } $dom->clear(); } else { Session::error(_t('Could not create the directory for the plugin')); return false; } } else { //Session::notice(_t('Downloaded "%s" to the plugin directory', array($basename))); file_put_contents($destination . $basename, $response); //file_put_contents($destination . $basename . '.header', print_r($headers,1)); } } return true; }
function __call($name, $inputs) { if (isset($this->api_calls[$name])) { list($fn, $params) = $this->api_calls[$name]; foreach ($params as $param) { switch ($param) { case '#private_key': $outputs['private_key'] = $this->privatekey; break; case '#blog_url': $outputs['blog_url'] = Site::get_url('habari'); break; default: $outputs[$param] = array_shift($inputs); break; } } //Utils::debug(self::BASE_URL . $fn, $outputs); $rr = new RemoteRequest(self::BASE_URL . $fn, 'POST', 180); $rr->set_postdata($outputs); $rr->execute(); $headers = $rr->get_response_headers(); $response = json_decode($rr->get_response_body()); $response->_status = $headers['Status']; return $response; } }
function do_stupid_things_in_global_scope() { /** * Test for the RemoteRequest class. */ include '../htdocs/system/classes/remoterequest.php'; include '../htdocs/system/classes/curlrequestprocessor.php'; include '../htdocs/system/classes/socketrequestprocessor.php'; include '../htdocs/system/classes/utils.php'; include '../htdocs/system/classes/error.php'; error_reporting(E_ALL | E_STRICT); function bs($v) { return $v ? 'TRUE' : 'FALSE'; } $tests_failed = array(); $tests = array('GET http://test.habariproject.org/' => array("\$res"), 'GET http://test.habariproject.org/get' => array("\$res_get"), 'POST http://test.habariproject.org/post' => array("\$res_post")); print "<h1>Running tests</h1>\n"; $processors = array(new CURLRequestProcessor(), new SocketRequestProcessor()); foreach ($processors as $processor) { $rr = new RemoteRequest('http://test.habariproject.org/'); $rr->__set_processor($processor); $res = $rr->execute(); if ($res === TRUE) { $results[] = array(get_class($processor), $rr->get_response_headers(), substr($rr->get_response_body(), 0)); } else { $results[] = array(get_class($processor), $res); } $rr = new RemoteRequest('http://test.habariproject.org/get'); $rr->__set_processor($processor); $rr->set_params(array('query' => 'var', 'another' => 'variable')); $res_get = $rr->execute(); if ($res_get === TRUE) { $results[] = array(get_class($processor), $rr->get_response_headers(), substr($rr->get_response_body(), 0)); } else { $results[] = array(get_class($processor), $res_get); } $rr = new RemoteRequest('http://test.habariproject.org/post', 'POST'); $rr->__set_processor($processor); $rr->set_body('If you can read this, the test was successful.'); $res_post = $rr->execute(); if ($res_post === TRUE) { $results[] = array(get_class($processor), $rr->get_response_headers(), substr($rr->get_response_body(), 0)); } else { $results[] = array(get_class($processor), $res_post); } foreach ($tests as $name => $group) { print "<h2>{$name}</h2>\n"; foreach ($group as $test) { $result = eval('return (' . $test . ');'); printf("<p><strong>%s</strong> == ( %s )</p>\n", bs($result), var_export($test, TRUE)); Utils::debug(array_shift($results)); if (!$result) { $tests_failed[$name][] = $test; } } } } if (count($tests_failed)) { print "<h1>Failed tests</h1>\n"; foreach ($tests_failed as $name => $tests) { print "<h2>{$name}</h2>\n"; foreach ($tests as $test) { print "<p>{$test}</p>\n"; } } } else { print "<h1>All tests successful</h1>\n"; } }