cvs: pear /Net_Geo Geo.php README package.xml

From: Date: Thu, 25 Apr 2002 23:49:52 +0000
Subject: cvs: pear /Net_Geo Geo.php README package.xml
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-3567@lists.php.net to get a copy of this message
graeme Thu Apr 25 19:49:52 2002 EDT Modified files: /pear/Net_Geo Geo.php README package.xml Log: Ready for stable release 1.0 Added pear Cache::Function Added use of localizer API General tidy up

Index: pear/Net_Geo/Geo.php diff -u pear/Net_Geo/Geo.php:1.4 pear/Net_Geo/Geo.php:1.5 --- pear/Net_Geo/Geo.php:1.4 Sat Feb 2 12:35:02 2002 +++ pear/Net_Geo/Geo.php Thu Apr 25 19:49:47 2002 @@ -17,9 +17,10 @@ // | | // +----------------------------------------------------------------------+ // -// $Id: Geo.php,v 1.4 2002/02/02 17:35:02 sterling Exp $ +// $Id: Geo.php,v 1.5 2002/04/25 23:49:47 graeme Exp $ require_once 'PEAR.php'; +require_once 'Cache/Function.php'; /** * NetGeo - determine geographic information on an internet address @@ -34,7 +35,10 @@ * Results returned are a single array of results if a string is passed in * or in the case of an array, a multi-dim array with the as the key * - * @version 0.9 + * Query service type (CAIDA or localizer) is not available as a constructer + * to retain compatibility with existing CAIDA NetGeo classes (perl + java) + * + * @version 1.0 * @package NetGeo * @author Graeme Merrall <graeme@inetix.com.au> */ @@ -49,6 +53,26 @@ { /** + * Path to local cache file. + * Caching is compulsory to reduce load on CAIDA server + * + * @var string + * @access public + */ + var $cache_path = "/tmp/"; + + /** + * How long to wait befire rechecking cached entries in *days* + * This should be comething nice and high + * + * @var in + * @access public + */ + var $cache_ttl = 30; + + /** + * CAIDA only + * * Maximum length of time, in seconds, which will be allowed during a whois * lookup by the NetGeo server. * The actual default value is maintained by the server. @@ -59,6 +83,8 @@ var $default_timeout = 60; /** + * CAIDA only + * * Location of the default netgeo server * If port not speicifed, defaults to 80 * @@ -68,24 +94,35 @@ var $default_server = "http://netgeo.caida.org/perl/netgeo.cgi"; /** - * Path to local cache file. If not empty then not used. - * It is strongly recommended you use a cache file + * localizer only + * + * Location of the localizer data file * * @var string * @access public */ - var $cache_path = "/tmp/"; + var $localizer_data = "./demo.csv"; /** - * How long to wait befire rechecking cached entries in days - * This should be comething nice and high - * NOT USED YET - * @var in - * @access public + * Type of service to use. May be either 'caida' or 'localizer' + * Default is 'caida' + * + * @var string + @ @access private */ - var $cache_ttl = 30; + var $service; + + /** + * Cache filename prefix + * + * @var string + * @access private + */ + var $cache_prefix = "netgeo"; /** + * CAIDA only + * * User Agent string. * * @var string @@ -94,14 +131,18 @@ var $useragent = "PHP/NetGeo"; /** + * CAIDA only + * * Class version * * @var string * @access private */ - var $useragent_version = "0.9"; + var $useragent_version = "1.0"; /** + * CAIDA only + * * How many targets can be read in at once * Should be enough for most everyone * @@ -111,20 +152,22 @@ var $array_limit = 100; /** - * Name of the cache file + * Function cache object * - * @var string + * @var object * @access private */ - var $cache_file = "Net_Geo.cache"; + var $cache; /** - * Container for cache file + * Name of global var for copying $this when calling function cache + * This is needed for the cache function to operate correctly * - * @var array + * @var string * @access private */ - var $cache_list = array(); + var $netgeo_global = "netgeo_global"; + /** * Constructor @@ -139,28 +182,64 @@ */ function Net_Geo($applicationName="", $alternateServerUrl="") { - // check to see if an alternate server URL is used - if (!empty($alternateServerUrl)) { - $this->default_server = $alternateServerUrl; - } + $this->applicationName = $applicationName; + $this->alternateServerUrl = $alternateServerUrl; + + // init cache object + $this->cache = new Cache_Function('file', + array('cache_dir' => $this->cache_path, + 'filename_prefix' => $this->cache_prefix + ), + $this->cache_ttl * 86400 + ); - $this->useragent = sprintf("%s %s", $this->useragent, $this->useragent_version); + return true; + } - // set the custom user agent - if (!empty($applicationName)) { - // trim whitespace - $applicationName = trim($applicationName); + + function setService($service = "caida") { + + if ($service == "localizer") { + + if (@localizer_read($this->localizer_data, FALSE) == FALSE) { + PEAR::raiseError("Can't read localizer data file ".$this->localizer_data); + return false; + } - // also set the agent name - $this->useragent = sprintf("%s/%s", $applicationName, $this->useragent); - } + } elseif ($service == "caida") { + + // check to see if an alternate server URL is used + if (!empty($alternateServerUrl)) { + $this->default_server = $this->alternateServerUrl; + } + + $this->useragent = sprintf("%s %s", $this->useragent, + $this->useragent_version + ); + + // set the custom user agent + if (!empty($applicationName)) { + // trim whitespace + $applicationName = trim($applicationName); + + // also set the agent name + $this->useragent = sprintf("%s/%s", $this->applicationName, + $this->useragent + ); + } - // load in the cache - $this->cache_list = $this->_readCache(); + } else { + // return error + return new PEAR_Error("No service specified"); + } + + + $this->service = $service; return true; + } - + /** * Gets a complete record for an address * Returns either a single or multidimentional arrray @@ -179,11 +258,11 @@ * Returns the 2-letter ISO 3166 country code * Returns NO_MATCH if the AS number has been looked up * but nothing was found in the whois lookups. - * Returns NO_COUNTRY if the lookup returned a record + * Returns NO_COUNTRY if the lookup returned a record * but no country could be found. * Returns an empty string if nothing was found in the database * - * @param mixed $target Single or list of addresses + * @param string $target single address * @return array * @access public */ @@ -202,9 +281,9 @@ * Lat/Long will be (0,0) if the target has been looked up but there was no * match in the whois lookups, or if no address could be parsed from the * whois record, or if the lat/long for the address is unknown. - * Returns undef if nothing was found in the database + * Returns an empty string if nothing was found in the database * - * @param mixed $target Single or list of addresses + * @param string $target single address * @return array * @access public */ @@ -219,7 +298,7 @@ * It's probably just as easy for the user to extract lat and long directly * from the array. * - * @param array $latLongRef Latitude/Longtitude array + * @param string $target single address * @return double * @access public */ @@ -240,7 +319,7 @@ * It's probably just as easy for the user to extract lat and long directly * from the array * - * @param array $latLongRef Latitude/Longtitude array + * @param string $target single address * @return double * @access public */ @@ -265,6 +344,12 @@ */ function _execute($methodName, $input) { + + // if we haven't got a service set, then do it now + if (empty($this->service)) { + $this->setService(); + } + // Test the target strings in the input array. Any targets not in // an acceptable format will have their STATUS field set to INPUT_ERROR. // This method will also store the standardized target into the array @@ -274,8 +359,6 @@ return $inputArray; } - - $resultArray = $this->_processArray($methodName, $inputArray); // if there is only one array, move the whole thing up one @@ -283,12 +366,6 @@ $resultArray = $resultArray[0]; } - // die if we can't write the cache file - $error = $this->_writeCache(); - if (PEAR::isError($error)) { - return $error; - } - return $resultArray; } @@ -343,7 +420,9 @@ /** - * Main function that processes adresses + * Main function that processes addresses + * + * It might be a good idea to move the caching up one level? * * @param string $methodName Lookup method * @param array $inputArray Formatted address array @@ -357,24 +436,32 @@ $entry = $this->_verifyInputFormat($entry); if (isset($entry["TARGET"]) && !isset($entry["INPUT_ERROR"])) { - // check the cache - if (!$dataArray[$i] = $this->_checkCache($entry)) { + + // set up the cache work around + $GLOBALS[$this->netgeo_global] =& $this; + + if ($this->service == "localizer") { + + $response = $this->cache->call('localizer_search', $entry["TARGET"]); + + } else { // else do the HTTP request - $url = sprintf("%s?method=%s&target=%s", $this->default_server, $methodName, $entry["TARGET"]); - $response = $this->_executeHttpRequest($url); - - if (!isset($response)) { - $entry["STATUS"] = NETGEO_HTTP_ERROR; - } - - // parse it all into something useful - // at this point we should look for NETGEO_LIMIT_EXCEEDED as well - $dataArray[$i] = $this->_processResult($response); - - // pop it into the cache - $this->cache_list[] = $dataArray[$i]; + $url = sprintf("%s?method=%s&target=%s", $this->default_server, + $methodName, $entry["TARGET"] + ); + + $response =& $this->cache->call($this->netgeo_global.'->_executeHttpRequest', $url); + } + + if (!isset($response)) { + $entry["STATUS"] = NETGEO_HTTP_ERROR; + } + + // parse it all into something useful + // at this point we should look for NETGEO_LIMIT_EXCEEDED as well + $dataArray[$i] = $this->_processResult($response); } else { $dataArray[$i] = $entry; @@ -445,8 +532,8 @@ } } else { $inputArray["INPUT_ERROR"] = NETGEO_INPUT_ERROR; - // raise some error tex - // unrecognized format for inpu + // raise some error text + // unrecognized format for input return $inputArray; } @@ -470,8 +557,7 @@ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); - } - else { + } else { // split the server url $urlinfo = parse_url($url); if (!isset($urlinfo["port"])) { @@ -503,115 +589,56 @@ */ function _processResult($response) { - - $lineArray = preg_split("/\n/", $response); - $line = array_shift($lineArray); + // process the localizer result differently + // since we already have an array + if ($this->service == "localizer") { - // first check for anything icky from the server - if (preg_match("/".NETGEO_HTTP_ERROR."/", $line) || preg_match('/^\s*$/', $response)) { + foreach ($response as $key=>$val) { - // empty empty empty - if (preg_match('/^\s*$/', $text)) { - $text = "Empty content string"; - return array("STATUS"=>$text); + $retarray[strtoupper($key)] = $val; } - } elseif (preg_match("/".NETGEO_LIMIT_EXCEEDED."/", $line)) { - return array("STATUS"=>$text); - } - - // now loop through. This should being us out at TARGET - while (isset($line) && !preg_match("/^TARGET:/", $line)) { - $line = array_shift($lineArray); - } - - // keep going - while (isset($line)) { - if (preg_match("/^TARGET:\s+(.*\S)\s*<br>/", $line, $matches)) { - $retarray["TARGET"] = $matches[1]; - } elseif (preg_match("/^STATUS:\s+([\w\s]+\S)\s*<br>/", $line, $matches)) { - $retarray["STATUS"] = $matches[1]; - } elseif (preg_match("/^(\w+):\s+(.*\S)\s*<br>/", $line, $matches)) { - $retarray[$matches[1]] = $matches[2]; - } + } elseif ($this->service == "caida") { + + $lineArray = preg_split("/\n/", $response); $line = array_shift($lineArray); - } - - return $retarray; - - } - - /** - * Writes the cache to disk - * This is a bot of a cop-out as it just serialises an array - * It'll be nice to make this more intelligent (like ttl) - * - * @return bool or pear error - * @access private - */ - function _writeCache() - { - - if (!is_dir($this->cache_path)) { - return new PEAR_Error("No such cache directory ".$this->cache_path); - } - $output = serialize($this->cache_list); - $fp = @fopen($this->cache_path."/".$this->cache_file, "w"); - if (!$fp) { - return new PEAR_Error("Unable to write to cache"); - } + // first check for anything icky from the server + if (preg_match("/".NETGEO_HTTP_ERROR."/", $line) || preg_match('/^\s*$/', $response)) { - fwrite($fp, $output); + // empty empty empty + if (preg_match('/^\s*$/', $text)) { + $text = "Empty content string"; + return array("STATUS"=>$text); + } - fclose($fp); - return true; - } + } elseif (preg_match("/".NETGEO_LIMIT_EXCEEDED."/", $line)) { + return array("STATUS"=>$text); + } - /** - * Loads the cache from disk - * - * @return array - * @access private - */ - function _readCache() - { + // now loop through. This should being us out at TARGET + while (isset($line) && !preg_match("/^TARGET:/", $line)) { + $line = array_shift($lineArray); + } - if (!is_dir($this->cache_path)) { - return new PEAR_Error("No such cache directory ".$this->cache_path); - } + // keep going + while (isset($line)) { + if (preg_match("/^TARGET:\s+(.*\S)\s*<br>/", $line, $matches)) { + $retarray["TARGET"] = $matches[1]; + } elseif (preg_match("/^STATUS:\s+([\w\s]+\S)\s*<br>/", $line, $matches)) { + $retarray["STATUS"] = $matches[1]; + } elseif (preg_match("/^(\w+):\s+(.*\S)\s*<br>/", $line, $matches)) { + $retarray[$matches[1]] = $matches[2]; + } + $line = array_shift($lineArray); + } - // cache might not exist yet so don't error out. - // can we raise a warning but not return it? - if (!is_readable($this->cache_path."/".$this->cache_file)) { - return array(); } - $input = join('', file($this->cache_path."/".$this->cache_file)); - $data = unserialize($input); + return $retarray; - return $data; } - /** - * Checks the cache for an entry - * Returns entry if present - * - * @param array $entry Entry to check caching for - * @return array or false if no entry - * @access private - */ - function _checkCache($entry) - { - foreach ($this->cache_list as $cache_entry) { - //echo $entry["TARGET"], "\n"; - - if (@in_array($entry["TARGET"], $cache_entry)) { - return $cache_entry; - } - } - return false; - } } Index: pear/Net_Geo/README diff -u pear/Net_Geo/README:1.1 pear/Net_Geo/README:1.2 --- pear/Net_Geo/README:1.1 Wed Jan 30 21:17:28 2002 +++ pear/Net_Geo/README Thu Apr 25 19:49:49 2002 @@ -2,5 +2,7 @@ with anything to do with using the NetGeo server, please check their website (http://www.caida.org/tools/utilities/netgeo/) +localizer is available from http://jan.kneschke.de/projects/localizer/ + Even though the NetGeo server will process domain names I find that success is sporadic. Using IP addresses or AS numbers will work everytime. Index: pear/Net_Geo/package.xml diff -u pear/Net_Geo/package.xml:1.3 pear/Net_Geo/package.xml:1.4 --- pear/Net_Geo/package.xml:1.3 Sun Feb 17 09:47:27 2002 +++ pear/Net_Geo/package.xml Thu Apr 25 19:49:49 2002 @@ -11,21 +11,32 @@ <name>Graeme Merrall</name> <email>graeme@inetix.com.au</email> </maintainer> - </maintainers> - <release> - <version>0.9</version> - <date>2002-01-30</date> - <state>beta</state> - <notes> - Very simplistic caching used which could do with a tidy up. - Although the NetGeo server will accept domain names, the quality is very - sporadic. Use IP or AS numbers for a better result. - </notes> + </maintainers> + <release> + <version>1.0</version> + <date>2002/04/26</date> + <state>stable</stable> + <notes> + * Added use of pear Cache::Function + * Added additional use of localizer API (see READ me for URL) + </notes> <filelist> <dir name="/" baseinstalldir="Net"> - <file role="php">Geo.php</file> - <file role="doc">README</file> + <file role="php">Geo.php</file> + <file role="doc">README</file> </dir> </filelist> - </release> + </release> + <changelog> + <release> + <version>0.9</version> + <date>2002-01-30</date> + <state>beta</state> + <notes> + Very simplistic caching used which could do with a tidy up. + Although the NetGeo server will accept domain names, the quality is very + sporadic. Use IP or AS numbers for a better result. + </notes> + </release> + </changelog> </package>
« previous php.pear.cvs (#3567) next »