= $ipAddressEnd){ $start = $midpoint + 1; }else{ $start = $midpoint; break; } } // read the data fseek($database, $start * 5 + 4); $data = fread($database, 1); // close the database fclose($database); // store the site and match type $this->site = ord($data) >> 2; $this->matchType = ord($data) & 3; } /* Returns the appropriate Amazon site for the IP address. The return value is * one of the following constants: * * IPToAmazon::AMAZON_COM - amazon.com (United States) * IPToAmazon::AMAZON_CA - amazon.ca (Canada) * IPToAmazon::AMAZON_CN - amazon.cn (China) * IPToAmazon::AMAZON_DE - amazon.de (Germany) * IPToAmazon::AMAZON_ES - amazon.es (Spain) * IPToAmazon::AMAZON_FR - amazon.fr (France) * IpToAmazon::AMAZON_IN - amazon.in (India) * IPToAmazon::AMAZON_IT - amazon.it (Italy) * IPToAmazon::AMAZON_CO_JP - amazon.co.jp (Japan) * IPToAmazon::AMAZON_CO_UK - amazon.co.uk (United Kingdom) */ function getSite(){ // return the site return $this->site; } /* Returns the match type for the IP address. The return value is one of the * following constants: * * IPToAmazon::MATCH_COUNTRY - the IP address corresponds to a country with * its own Amazon site * IPToAmazon::MATCH_LANGUAGE - the IP address corresponds to a country whose * primary language is the same as that used by * an Amazon site * IPToAmazon::MATCH_NEITHER - neither of the above conditions are true for * the IP address * IPToAmazon::MATCH_RESERVED - the IP address is a reserved address and * hasn't been assigned to a country yet */ function getMatchType(){ // return the match type return $this->matchType; } /* Returns the appropriate Amazon URL for the IP address and specified * parameters, all of which are optional. If fewer than two parameters are * given, the URL of the appropriate Amazon home page is returned. If two * parameters are given, the URL of the appropriate Amazon product is * returned. If three parameters are given, the URL of the appropriate Amazon * product is returned, including the Amazon affiliates tag. The parameters * are: * * $defaultSite - one of the site constants, specifying which Amazon site * should be used if an ASIN or associate ID has not been * specified for the site appropriate for the IP address * $asins - an array mapping the site constants onto product ASINs. If * the associate IDs are specified, and the same ASIN should * be used on all of the sites for which an associate ID is * given, a single string can be given instead of an array. * If the associate IDs are specified, but this parameter is * set to null, the URL of the appropriate Amazon home page is * returned. * $associateIDs - an array mapping the site constants onto associate IDs */ function getURL($defaultSite = null, $asins = null, $associateIDs = null){ // determine whether to use the default site if ((is_array($asins) && !isset($asins[$this->site])) || (is_array($associateIDs) && !isset($associateIDs[$this->site]))){ $site = $defaultSite; }else{ $site = $this->site; } // initialise the URL $url = 'http://www.amazon.'; // extend the URL with the appropriate Amazon domain name switch ($site){ case self::AMAZON_COM : $url .= 'com'; break; case self::AMAZON_CA : $url .= 'ca'; break; case self::AMAZON_CN : $url .= 'cn'; break; case self::AMAZON_DE : $url .= 'de'; break; case self::AMAZON_ES : $url .= 'es'; break; case self::AMAZON_FR : $url .= 'fr'; break; case self::AMAZON_IN : $url .= 'in'; break; case self::AMAZON_IT : $url .= 'it'; break; case self::AMAZON_CO_JP : $url .= 'co.jp'; break; case self::AMAZON_CO_UK : $url .= 'co.uk'; break; } $url .= '/'; // extend the URL with the ASIN if necessary if (!is_null($asins)){ $url .= 'dp/' . (is_array($asins) ? $asins[$site] : $asins) . '/'; } // extend the URL with the affiliate tag if necessary if (is_array($associateIDs)) $url .= '?tag=' . $associateIDs[$site]; // return the URL return $url; } } ?>