/** * Perform a check of all beaconids. * Notifies update_check plugin hooks when checking so that they can add their beaconids to the list. * @return array An array of update beacon information for components that have updates * @throws \Exception */ public static function check() { try { // get a local version of the instance to save typing $instance = self::instance(); // load beacons self::register_beacons(); // setup the remote request $request = new RemoteRequest(self::UPDATE_URL, 'POST'); // add all the beacon versions as parameters $request->set_params(Utils::array_map_field($instance->beacons, 'version')); // we're not desperate enough to wait too long $request->set_timeout(5); // execute the request $result = $request->execute(); // grab the body of the response, which has our xml in it $update_data = $request->get_response_body(); // i don't know why we hold the XML in a class variable, but we'll keep doing that in this rewrite $instance->update = new \SimpleXMLElement($update_data); foreach ($instance->update as $beacon) { $beacon_id = (string) $beacon['id']; $beacon_url = (string) $beacon['url']; $beacon_type = isset($beacon['type']) ? (string) $beacon['type'] : 'addon'; // do we have this beacon? if not, don't process it // even though we POST all our beacons to the update script right now, it still hands back the whole list if (empty($instance->beacons[$beacon_id])) { continue; } // add the beacon's basic info $instance->beacons[$beacon_id]['id'] = $beacon_id; $instance->beacons[$beacon_id]['url'] = $beacon_url; $instance->beacons[$beacon_id]['type'] = $beacon_type; foreach ($beacon->update as $update) { // pick out and cast all the values from the XML $u = array('severity' => (string) $update['severity'], 'version' => (string) $update['version'], 'date' => isset($update['date']) ? (string) $update['date'] : '', 'url' => isset($update['url']) ? (string) $update['url'] : '', 'text' => (string) $update); // if the remote update info version is newer... we want all newer versions if (version_compare($u['version'], $instance->beacons[$beacon_id]['version']) > 0) { // if this version is more recent than all the other versions if (!isset($instance->beacons[$beacon_id]['latest_version']) || version_compare($u['version'], $instance->beacons[$beacon_id]['latest_version']) > 0) { // set this as the latest version $instance->beacons[$beacon_id]['latest_version'] = $u['version']; } // add the version to the list $instance->beacons[$beacon_id]['updates'][$u['version']] = $u; } } } // return an array of beacons that have updates return array_filter($instance->beacons, Method::create('\\Habari\\Update', 'filter_unchanged')); } catch (\Exception $e) { // catches any RemoteRequest errors or XML parsing problems, etc. // bubble up throw $e; } }
/** * Perform a check of all beaconids. * Notifies update_check plugin hooks when checking so that they can add their beaconids to the list. * @return array An array of update beacon information for components that have updates */ public static function check() { try { $instance = self::instance(); if (count($instance->beacons) == 0) { Update::add('Habari', '7a0313be-d8e3-11db-8314-0800200c9a66', Version::get_habariversion()); Plugins::act('update_check'); } $request = new RemoteRequest(UPDATE_URL, 'POST'); $request->set_params(array_map(create_function('$a', 'return $a["version"];'), $instance->beacons)); $request->set_timeout(10); $result = $request->execute(); if (Error::is_error($result)) { throw $result; } $updatedata = $request->get_response_body(); if (Error::is_error($updatedata)) { throw $updatedata; } $instance->update = new SimpleXMLElement($updatedata); foreach ($instance->update as $beacon) { $beaconid = (string) $beacon['id']; foreach ($beacon->update as $update) { // Do we have this beacon? If not, don't process it. if (empty($instance->beacons[$beaconid])) { continue; } // If the remote update info version is newer... if (version_compare($update['version'], $instance->beacons[$beaconid]['version']) > 0) { // If this version is more recent than all other newer versions... if (empty($instance->beacons[$beaconid]['latest_version']) || version_compare((string) $update['version'], $instance->beacons[$beaconid]['latest_version']) > 0) { $instance->beacons[$beaconid]['latest_version'] = (string) $update['version']; } if (isset($instance->beacons[$beaconid]['severity'])) { $instance->beacons[$beaconid]['severity'][] = (string) $update['severity']; array_unique($instance->beacons[$beaconid]['severity']); } else { $instance->beacons[$beaconid]['severity'] = array((string) $update['severity']); } $instance->beacons[$beaconid]['url'] = (string) $beacon['url']; $instance->beacons[$beaconid]['changes'][(string) $update['version']] = (string) $update; } } } return array_filter($instance->beacons, array('Update', 'filter_unchanged')); } catch (Exception $e) { return $e; } }
public function fetch_yahoo_tags($text) { $appID = 'UZuNQnrV34En.c9itu77sQrdjp.FQU81t8azZeE5YmWjRkP9wVlPg.CPIc8eLZT68GI-'; $context = $text; $request = new RemoteRequest('http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction', 'POST'); $request->set_params(array('appid' => $appID, 'context' => $context)); $tags = array(); // Utils::debug($request->execute()); if (!is_object($request->execute())) { $response = $request->get_response_body(); $xml = new SimpleXMLElement($response); foreach ($xml->Result as $tag) { $tags[] = strval($tag); } } return $tags; }
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 ) );
public static function zima($url) { $call = new RemoteRequest('http://zi.ma/'); $call->set_params(array('module' => 'ShortURL', 'file' => 'Add', 'mode' => 'API', 'url' => $url)); $call->set_timeout(5); $result = $call->execute(); if (Error::is_error($result)) { return FALSE; } return trim($call->get_response_body()); }
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"; } }