amazOOP amazOOP
[ class tree: amazOOP ] [ index: amazOOP ] [ all elements ]

Source for file aq.lib.php

Documentation is available at aq.lib.php

  1. <?php
  2. /**
  3. * This file contains some methods that are used throughout the entire script.
  4. *
  5. * All methods in this file are private since they are only meant to
  6. * be used internally by the script. (Not that this means anything in PHP ;)
  7. *
  8. * @author Mauricio Diaz <madd0@users.sourceforge.net>
  9. * @copyright 2004 (c) Mauricio Diaz Orlich
  10. * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  11. * @version 0.3
  12. *
  13. * @package amazOOP
  14. */
  15.  
  16. /**
  17. * For security, only {@link aq.php} should be included in your pages
  18. */
  19. defined('_AMAZOOP') or die( 'Direct Access to this location is not allowed.' );
  20.  
  21. /**
  22. * If activated, this indicates that the image URL returned by Amazon should be
  23. * used even if it is only a 1x1 GIF.
  24. * This value is to be used in function {@link getImage getImage}() together
  25. * with one of the {@link IMAGE_ALTERNATIVES_0 IMAGE_ALTERNATIVES_X} values.
  26. */
  27. define('IMAGE_AS_IS', 16);
  28.  
  29. /**
  30. * If activated, this indicates that function {@link }
  31. * http://www.php.net/getimagesize getimagesize}() should not be used.
  32. *
  33. * Not using this function implies that any generated image tags will not have
  34. * the width and height attributes, nor will these values be returned in the
  35. * array returned by {@link getImage getImage}().
  36. *
  37. * In some server configurations, however, this may reduce execution time
  38. * substantially.
  39. *
  40. * This value is to be used in function {@link getImage getImage}() together
  41. * with one of the {@link IMAGE_ALERNATIVES_0 IMAGE_ALTERNATIVES_X} values.
  42. * @var integer
  43. */
  44. define('IMAGE_NO_SIZE', 32);
  45.  
  46. /**
  47. * If activated, the script will NOT look for any alternative images even if it
  48. * is determined that the URL returned by Amazon points to a 1x1 pixel GIF.
  49. *
  50. * The difference between using IMAGE_ALTERNATIVES_0 and {@link IMAGE_AS_IS} is
  51. * that {@link IMAGE_AS_IS} will display the 1x1 pixel GIF returned by Amazon,
  52. * while IMAGE_ALTERNATIVES_0 will display the "No image available" replacement.
  53. */
  54. define('IMAGE_ALTERNATIVES_0', 0);
  55.  
  56. /**
  57. * If activated, the script will try to look for an alternative image ONLY if
  58. * the merchant name returned by Amazon (if a name was returned) is associated
  59. * to a merchant code is the user's {@link config/stores.inc.php personal}
  60. * stores}.
  61. */
  62. define('IMAGE_ALTERNATIVES_1', 1);
  63.  
  64. /**
  65. * If activated, the script will try to look for an alternative image both at
  66. * {@link IMAGE_ALTERNATIVES_1 level 1} and by looking in all Amazon sites for
  67. * an alternative.
  68. */
  69. define('IMAGE_ALTERNATIVES_2', 2);
  70.  
  71. /**
  72. * If activated, the script will try to look for an alternative image only
  73. * if you have associated the product's manufactuerer with a Merchant ID
  74. * in the {@link stores.inc.php} file.
  75. */
  76. define('IMAGE_ALTERNATIVES_3', 4);
  77.  
  78. /**
  79. * If activated, the script will try to look for an alternative image using
  80. * ALL the store codes you have defined in file {@link stores.inc.php}. It
  81. * goes without saying that this is the option that takes more time to execute.
  82. */
  83. define('IMAGE_ALTERNATIVES_4', 8);
  84.  
  85. /**
  86. * This document contains the classes where product details are stored.
  87. * @access private
  88. */
  89. require_once(CORE_PATH . "item.class.php");
  90. /**
  91. * This document contains the LiteDetails class
  92. * @access private
  93. */
  94. require_once(CORE_PATH . "liteDetails.class.php");
  95. /**
  96. * This document contains the HeavyDetails
  97. * @access private
  98. */
  99. require_once(CORE_PATH . "heavyDetails.class.php");
  100. /**
  101. * This document contains other classes used in amazOOP.
  102. * @access private
  103. */
  104. require_once(CORE_PATH . "other.class.php");
  105. /**
  106. * This document contains the scripts configuration variables.
  107. * @access private
  108. */
  109. require_once(CORE_PATH . "config/config.inc.php");
  110. /**
  111. * This document contains the XML parser used by the script.
  112. * @access private
  113. */
  114. require_once(CORE_PATH . "parser.class.php");
  115. /**
  116. * This document contains the classes needed for the Remote Shopping Cart
  117. * @access private
  118. */
  119. require_once(CORE_PATH . "cart.inc.php");
  120.  
  121. /**
  122. * Sets function logError() as the error handler if error logging is active
  123. */
  124. set_error_handler('logError');
  125.  
  126. /**
  127. * Gets the value of a variable from amazOOP's general configuration.
  128. *
  129. * The general configuration variables are the ones defined in section
  130. * 'General' of the {@tutorial amazoop.configure.pkg Configurator}, under the title
  131. * that reads 'General Configuration'.
  132. *
  133. * This function is mainly for internal use, however I have not declared it
  134. * private because it might be of interest for some developers.
  135. *
  136. * @param string $var The variable you wish to fetch. Valid values are:
  137. * - 'address': the URL of the site's home (e.g. 'www.server.com', or 'www.shared.com/mysite')
  138. * - 'path_to_script': absolute path to the script (e.g. 'c:\\path\\to\\site\\include\\amazoop\\', or '/var/www/html/amazoop/')
  139. * - 'close_tag_character': returns ' />' or ' >' if configured to use XHTML or HTML respectively
  140. * - 'file_separator': returns '\\' or '/' if configured to use Windows or *nix respectively
  141. * - 'UTF-8_encode': determines whether data from Amazon should be UTF-8 encoded
  142. * - 'open_links_in': 'new' or 'same' window
  143. * - 'log_errors': true or false
  144. * - 'log_requests': true or false
  145. * - 'temporary_directory': absolute path to temporary directory
  146. * - 'cache_directory': absolute path to cache directory
  147. * - 'max_request_log_size': maximum size in lines of request log
  148. * - 'max_error_log_size': maximum size in lines of error log
  149. * - 'request_log_file': absolute path to request log
  150. * - 'error_log_file': absolute path to error log
  151. * - 'timestamp_file': absolute path to file containing timestamp
  152. * - 'XML_template': absolute path to XML template
  153. * - 'check_alternative_images': level of image checking (0-4)
  154. *
  155. * @param string $server The server you want the variable for. Valid values
  156. * are:
  157. * - "auto": amazOOP determines which server it is running on and returns
  158. * the value for that server.
  159. * - "test": the value for the test server is returned.
  160. * - "def": the value for the definitive server is returned.
  161. *
  162. * @return string|booleanThe value of the requested variable.
  163. */
  164. function getAmazoopConfig($var, $server = "auto"){
  165. global $_AMAZOOP_CONFIG;
  166.  
  167. $get = $server;
  168.  
  169. if($server == "auto"){
  170. if($_SERVER['SERVER_NAME'] == $_AMAZOOP_CONFIG['address']['test'])
  171. $get = 'test';
  172. else
  173. $get = 'def';
  174. }
  175.  
  176. $res = $_AMAZOOP_CONFIG[$var][$get];
  177.  
  178. switch($var){
  179. case 'close_tag_character':
  180. if($res == 'xhtml')
  181. $res = ' />';
  182. else
  183. $res = ' >';
  184. break;
  185. case 'file_separator':
  186. if($res == 'windows')
  187. $res = '\\';
  188. else
  189. $res = '/';
  190. break;
  191. case 'temporary_directory':
  192. case 'cache_directory':
  193. case 'request_log_file':
  194. case 'error_log_file':
  195. case 'timestamp_file':
  196. case 'XML_template':
  197. if(substr($res, 0, 1) != '/' && substr($res, 1, 1) != ':'){
  198. $path = getAmazoopConfig('path_to_script', $server);
  199.  
  200. if(substr($path, -1) != '/' && substr($path, -1) != '\\')
  201. $path = $path . getAmazoopConfig('file_separator', $server);
  202.  
  203. $res = $path . $res;
  204. }
  205. default:
  206. break;
  207. }
  208.  
  209. if(is_numeric($res))
  210. return $res;
  211. elseif($res == 'true')
  212. return true;
  213. elseif($res == 'false')
  214. return false;
  215. else
  216. return $res;
  217. }
  218.  
  219. /**
  220. * Sets the value of a variable from amazOOP's general configuration.
  221. *
  222. * The general configuration variables are the ones defined in section
  223. * 'General' of the {@tutorial amazoop.configure.pkg Configurator}, under the title
  224. * that reads 'General Configuration'.
  225. *
  226. * Although I can't see an immediate use for this function, you can override the default
  227. * configuration of the script, using this function. These changes, however, are only
  228. * available to each instance of the script, and are lost once the script ends.
  229. *
  230. * @param string $var The variable you wish to set. Valid values are:
  231. * - 'address': the URL of the site's home (e.g. 'www.server.com', or 'www.shared.com/mysite')
  232. * - 'path_to_script': absolute path to the script (e.g. 'c:\\path\\to\\site\\include\\amazoop\\', or '/var/www/html/amazoop/')
  233. * - 'close_tag_character': 'xhtml' and 'html', will return ' />' or ' >' respectively
  234. * - 'file_separator': 'windows' or 'unix' will return '\\' or '/' respectively
  235. * - 'UTF-8_encode': 'true' or 'false'
  236. * - 'open_links_in': 'new' or 'same' window
  237. * - 'log_errors': 'true' or 'false'
  238. * - 'log_requests': 'true' or 'false'
  239. * - 'temporary_directory': path to temporary directory, either relative from 'path_to_script' or absolute
  240. * - 'cache_directory': path to cache directory, either relative from 'path_to_script' or absolute
  241. * - 'max_request_log_size': maximum size in lines of request log
  242. * - 'max_error_log_size': maximum size in lines of error log
  243. * - 'request_log_file': path to request log, either relative from 'path_to_script' or absolute
  244. * - 'error_log_file': path to error log, either relative from 'path_to_script' or absolute
  245. * - 'timestamp_file': path to file containing timestamp, either relative from 'path_to_script' or absolute
  246. * - 'XML_template': path to XML template, either relative from 'path_to_script' or absolute
  247. * - 'check_alternative_images': level of image checking (0-4)
  248. *
  249. * @param string $value The value you wish to set to $var. See the description of the $var parameter
  250. * for a list of valid values.
  251. *
  252. * @param string $server The server you want the variable for. Valid values
  253. * are:
  254. * - "test": the value for the test server is returned.
  255. * - "def": the value for the definitive server is returned.
  256. *
  257. * @return void
  258. */
  259. function setAmazoopConfig($var, $value, $server){
  260. global $_AMAZOOP_CONFIG;
  261.  
  262.  
  263. $_AMAZOOP_CONFIG[$var][$server] = $value;
  264. }
  265.  
  266. /**
  267. * Gets the {@tutorial advanced.browsenodes.pkg browse node} corresponding to
  268. * an {@tutorial advanced.categories.pkg amazOOP category}.
  269. *
  270. * This function is mainly for internal use, however I have not declared it
  271. * private because it might be of interest for some developers.
  272. *
  273. * @param string $locale the country for which the category is defined.
  274. * @param string $category the amazOOP category for which you want to retrieve
  275. * the browse node.
  276. *
  277. * @return string|booleanthe browse node for the requested category, or false
  278. * if the category doesn't exist.
  279. */
  280. function getBrowseNode($locale, $category){
  281. global $_AMAZOOP_CATEGORIES;
  282.  
  283. if(isset($_AMAZOOP_CATEGORIES[$locale][$category]['browsenode']))
  284. return $_AMAZOOP_CATEGORIES[$locale][$category]['browsenode'];
  285. else return false;
  286. }
  287.  
  288. /**
  289. * Gets the associate ID for a specified locale.
  290. *
  291. * @param string $locale the locale for which you would like the associate ID.
  292. *
  293. * @return string|booleanthe requested ID or false if the requested locale is not valid.
  294. * If the locale is valid, but no ID has been set, the ID of the developer is returned (think of
  295. * it as a contribution).
  296. */
  297. function getAssociateID($locale){
  298. global $_AMAZOOP_ASSOC_IDS, $_AMAZOOP_MADD0S_IDS;
  299.  
  300. if(isset($_AMAZOOP_ASSOC_IDS[$locale]))
  301. if($_AMAZOOP_ASSOC_IDS[$locale] == "")
  302. return $_AMAZOOP_MADD0S_IDS[$locale];
  303. else
  304. return $_AMAZOOP_ASSOC_IDS[$locale];
  305. else return false;
  306. }
  307.  
  308. /**
  309. * Gets the path to the image representing a certain medium (i.e. DVD, CD, VHS, Software platform, etc).
  310. * Paths are stored in file {@link config.inc.php}
  311. *
  312. * @param string $medium the required medium.
  313. *
  314. * @return array If an image was configured, an array with the following information:
  315. * - 0: the URL to the image,
  316. * - 1: the width of the image,
  317. * - 2: the height of the image,
  318. * - 3: a string to be used in HTML IMG tags that reads ' width="*" height="*" '
  319. *
  320. * @since version 0.3RC2 this function returns an array instead of a string.
  321. *
  322. * @access private
  323. */
  324. function getMediaImage($medium){
  325. global $_AMAZOOP_MEDIA_IMAGES;
  326.  
  327. if(isset($_AMAZOOP_MEDIA_IMAGES[$medium]))
  328. return array("http://" . getAmazoopConfig('address') . $_AMAZOOP_MEDIA_IMAGES[$medium]['url'],
  329. $_AMAZOOP_MEDIA_IMAGES[$medium]['width'],
  330. $_AMAZOOP_MEDIA_IMAGES[$medium]['height'],
  331. "width=\"" . $_AMAZOOP_MEDIA_IMAGES[$medium]['width'] . "\" height=\"" . $_AMAZOOP_MEDIA_IMAGES[$medium]['height'] . "\"");
  332. else
  333. return false;
  334. }
  335.  
  336. /**
  337. * Receives an AWS mode and gets its name in a human readable form, e.g. "classic-uk" => "Classical Music",
  338. * or an amazOOP category ("classic-uk" => "classic").
  339. *
  340. * These associations are stored in {@link $_AMAZOOP_CATEGORIES}, but you should use the
  341. * {@tutorial amazoop.configure.pkg Configurator} to set them up.
  342. *
  343. * @param string $locale the locale used, important since modes are different in each locale
  344. * @param string $mode the mode searched
  345. * @param boolean $human if true the name is returned, otherwise, the mode is returned
  346. *
  347. * @return string The name of a mode in the requested presentation.
  348. *
  349. * @access private
  350. */
  351. function getCategory($locale, $mode, $human = true){
  352. global $_AMAZOOP_CATEGORIES;
  353.  
  354. foreach($_AMAZOOP_CATEGORIES[$locale] as $catName => $cat){
  355. if($cat['mode'] == $mode){
  356. $key = $catName;
  357. $name = $cat['name'];
  358. break;
  359. }
  360. }
  361. if($human)
  362. return $name;
  363. else
  364. return $key;
  365. }
  366.  
  367. /**
  368. * Receives a locale and an amazOOP category, and looks for the corresponding AWS mode.
  369. *
  370. * Tha associations are stored in {@link $_AMAZOOP_CATEGORIES}, which is in {@link categories.inc.php}.
  371. *
  372. * @param string $locale the locale used, important since modes are different in each locale
  373. * @param string $category the amazOOP category you want the mode for
  374. *
  375. * @return string The AWS mode corresponding to the provided amazOOP mode
  376. *
  377. * @access private
  378. */
  379. function getMode($locale, $category){
  380. global $_AMAZOOP_CATEGORIES;
  381.  
  382. if(isset($_AMAZOOP_CATEGORIES[$locale][$category]))
  383. return $_AMAZOOP_CATEGORIES[$locale][$category]['mode'];
  384. else
  385. return false;
  386. }
  387.  
  388. /**
  389. * Logs an error, either at the request of the user or a PHP generated error, to the error
  390. * log file.
  391. *
  392. * This function uses values specified in amazOOP's main configuration file which should be
  393. * configured using the {@tutorial amazoop.configure.pkg Configurator}
  394. *
  395. * @param integer $errno the error number
  396. * @param string $errstr the error string
  397. * @param string $errfile the name of the file where the error was generated
  398. * @param string $errline the line in which the error was generated
  399. *
  400. * @access private
  401. */
  402. function logError($errno, $errstr, $errfile, $errline){
  403. if (error_reporting() == 0) return;
  404.  
  405. switch($errno){
  406. case E_ERROR:
  407. case E_USER_ERROR:
  408. $errormsg = "<b>Fatal Error</b>: " . $errstr;
  409. break;
  410.  
  411. case E_WARNING:
  412. case E_USER_WARNING:
  413. $errormsg = "<b>Warning</b>: " . $errstr;
  414. break;
  415.  
  416. case E_NOTICE:
  417. case E_USER_NOTICE:
  418. $errormsg = "<b>Notice</b>: " . $errstr;
  419. break;
  420.  
  421. default:
  422. $errormsg = "<b>Unknown Error</b>: " . $errstr;
  423. break;
  424. }
  425.  
  426. if(getAmazoopConfig('log_errors')){
  427. $err = array();
  428. $err[] = date('r') . "\n";
  429. $err[] = chunk_split(strip_tags($errormsg));
  430. $err[] = chunk_split("File: " . $errfile);
  431. $err[] = "Line: " . $errline . "\n";
  432. $err[] = chunk_split("URI: " . $_SERVER["REQUEST_URI"]);
  433. if(isset($_SERVER['HTTP_REFERER']))
  434. $err[] = chunk_split("Referer: " . $_SERVER['HTTP_REFERER']);
  435. else
  436. $err[] = chunk_split("Referer: Direct Hit or Hidden");
  437. $err[] = chunk_split("Remote IP: " . $_SERVER["REMOTE_ADDR"]);
  438. if(isset($_SERVER["HTTP_USER_AGENT"]))
  439. $err[] = chunk_split("User Agent: " . $_SERVER["HTTP_USER_AGENT"]);
  440. $err[] = "----------------------------------------------------------------------------\n";
  441.  
  442. $err = array_merge($err, file(getAmazoopConfig('error_log_file')));
  443.  
  444. if($fp = fopen(getAmazoopConfig('error_log_file'), 'wb+')){
  445. for($i = 0; $i < min(sizeof($err), getAmazoopConfig('max_error_log_size')); $i++)
  446. fwrite($fp, $err[$i]);
  447. fclose($fp);
  448. }
  449. else
  450. trigger_error("Could not write to the error log file (" . getAmazoopConfig('error_log_file') . ")", E_USER_WARNING);
  451. }
  452.  
  453. if(!getAmazoopConfig('log_errors') || $errno == E_ERROR || $errno == E_USER_ERROR){
  454.  
  455. $errormsg = "<br />" . $errormsg . " in <b>" . $errfile . "</b> on line <b>" . $errline . "</b><br />";
  456. echo $errormsg;
  457. flush();
  458. if($errno == E_ERROR || $errno == E_USER_ERROR)
  459. die();
  460. }
  461. }
  462.  
  463. /**
  464. * Logs requests made to Amazon to the request log file.
  465. *
  466. * Typically, logged information includes: date, URL of AWS request, request ID return by AWS,
  467. * IP of client, duration of request.
  468. *
  469. * This function uses values specified in amazOOP's main configuration file which should be
  470. * configured using the {@tutorial amazoop.configure.pkg Configurator}
  471. *
  472. * @param array $req a description of the request to Amazon
  473. *
  474. * @see config.inc.php
  475. * @see $logFile
  476. * @see $logRequests
  477. * @see $requestLogSize
  478. *
  479. * @access private
  480. */
  481. function logRequest($req){
  482. if(getAmazoopConfig('log_requests')){
  483. $res = array();
  484. $res[] = date('r') . "\n";
  485. foreach($req as $n => $line)
  486. $req[$n] = chunk_split($line);
  487. $res = array_merge($res, $req);
  488. $res[] = "----------------------------------------------------------------------------\n";
  489. $res = array_merge($res, file(getAmazoopConfig('request_log_file')));
  490.  
  491. if($fp = fopen(getAmazoopConfig('request_log_file'), 'wb+')){
  492. $max_request_log_size = getAmazoopConfig('max_request_log_size');
  493. for($i = 0; $i < min(sizeof($res), $max_request_log_size); $i++)
  494. fwrite($fp, $res[$i]);
  495. fclose($fp);
  496. }
  497. else
  498. trigger_error("Could not write to the request log file (" . getAmazoopConfig('request_log_file') . ")", E_USER_WARNING);
  499. }
  500. }
  501.  
  502. /**
  503. * Used to parse the templates for the RSS feed.
  504. *
  505. * This function uses values specified in amazOOP's main configuration file which should be
  506. * configured using the {@tutorial amazoop.configure.pkg Configurator}
  507. *
  508. * WARNING!!! This function is experimental. Use and tweak if you wish, and send feedback!!!
  509. *
  510. * @param string $tag the string extracted from the RSS template that should be replaced
  511. * @param string $locale the locale from which the Amazon data is being gathered,
  512. * this can be "us", "uk", "de" or "jp"
  513. * @param LiteDetails|HeavyDetails$details the object containing the information to be
  514. * included in the RSS feed.
  515. *
  516. * @see config.inc.php
  517. * @see $rss
  518. *
  519. * @access private
  520. */
  521. function handleTag($tag, $locale, $details = NULL){
  522. global $_AMAZOOP_RSS;
  523.  
  524. $tag = trim($tag);
  525. $res = "";
  526. switch($tag){
  527. case "startItem":
  528. $res = "stop";
  529. case "endItem":
  530. $res = "resume";
  531. break;
  532. case "language":
  533. if($locale == "us" || $locale == "uk")
  534. $res = "en-" . $locale;
  535. else
  536. $res = $locale;
  537. break;
  538. case "itemTitle":
  539. if(isset($details))
  540. $res = str_replace("&", "&amp;", $details->getProductName());
  541. break;
  542. case "itemLink":
  543. if(isset($details))
  544. $res = "http://www.madd0.com/" . $locale . "/details/" . $details->getASIN();
  545. break;
  546. case "itemDescription":
  547. if(isset($details)){
  548. $res = "<![CDATA[ ";
  549. if($description = $details->getProductDescription(70)){
  550. $res .= "<p>" . $description;
  551. $res .= " <a href=\"http://www.madd0.com/" . $locale . "/details/" . $details->getASIN() . "\" target=\"_blank\"><i>(read more)</i></a></p>\n";
  552. }
  553. $res .= "<p>Get it now for only: " . $details->getOurPrice() . "</p>\n";
  554. $res .= "<p><i>Price is accurate as of " . date('r') . ". Prices and product availability are subject to change. Any price displayed on the Amazon website at the time of purchase will govern the sale of this product.</i></p> ]]>";
  555. }
  556. break;
  557. //<category>%category%</category>
  558. //<comments>%comments%</comments>
  559. case "guid":
  560. if(isset($details))
  561. $res = $details->getURL();
  562. break;
  563. case "lastBuildDate":
  564. case "itemPubDate":
  565. $res = gmdate('D, d M Y H:i:s \G\M\T');
  566. break;
  567. default:
  568. //$tag = substr($tag, -1);
  569. //$stripped = substr($stripped, 1);
  570. if(isset($_AMAZOOP_RSS[$tag]))
  571. $res = $_AMAZOOP_RSS[$tag];
  572. else
  573. $res = $tag;
  574. }
  575. return $res;
  576. }
  577.  
  578. /**
  579. * Checks if more than 1 second has passed since the last query.
  580. * If so, the current time is stored as the time of the last query.
  581. *
  582. * This function uses values specified in amazOOP's main configuration file which should be
  583. * configured using the {@tutorial amazoop.configure.pkg Configurator}
  584. *
  585. * @return boolean true if more than 1 second has passed since the
  586. * last query to Amazon. False otherwise.
  587. *
  588. * @access private
  589. *
  590. */
  591. function inTime(){
  592. if(file_exists(getAmazoopConfig('timestamp_file'))){
  593. $lastRun = trim(file_get_contents(getAmazoopConfig('timestamp_file')));
  594. $currTime = time();
  595.  
  596. if($currTime - $lastRun <= 0)
  597. return false;
  598. }
  599. if($fp = fopen(getAmazoopConfig('timestamp_file'), "w")){
  600. fwrite($fp, time());
  601. fclose($fp);
  602. }
  603. else
  604. trigger_error("Could not write to the timestamp file (" . getAmazoopConfig('timestamp_file') . ")", E_USER_ERROR);
  605.  
  606. return true;
  607. }
  608.  
  609. /**
  610. * Checks if an image provided by Amazon exists.
  611. * To do this the function checks the headers returned by the server
  612. * to determine if the URL is valid and if it is a valid image
  613. * and not a 1x1 GIF image. Does this assuming all 1x1 GIF images are
  614. * 807 bytes in length.
  615. *
  616. * @param string $img the URL of the image you want to verify.
  617. * @return boolean true if the image exists. False otherwise.
  618. *
  619. * @access private
  620. */
  621. function imageExists($img){
  622. global $allowsettimelimit;
  623.  
  624. if($allowsettimelimit) set_time_limit(60);
  625.  
  626. /*$start_time = explode(" ", microtime());
  627. $start_time = $start_time[1] + $start_time[0];*/
  628.  
  629.  
  630. $wait = 1;
  631. $result = true;
  632.  
  633. $address = array();
  634. preg_match("/^(http:\/\/)?([^\/]+)(.*)/i", $img, $address);
  635.  
  636. for($i = 0; $i < 3; $i++){
  637. if($fp = @fsockopen($address[2], 80, $errno, $errstr, 30))
  638. break;
  639. sleep($wait);
  640. $wait *= 2;
  641. }
  642.  
  643. if(!$fp){
  644. trigger_error($errstr, $errno);
  645. $result = false;
  646. }
  647. else{
  648. $request = "HEAD ". $address[3] . " HTTP/1.1\r\n"
  649. . "Host: $address[2]\r\n"
  650. . "User-Agent: amazOOP/0.3\r\n"
  651. . "\r\n";
  652. fwrite($fp, $request);
  653.  
  654. while(!feof($fp)){
  655. $text = strtolower(fgets($fp,1024));
  656. $line = explode(" ", $text);
  657. //$line = explode(" ", strtolower(fgets($fp,1024)));
  658. //echo $text . "<br>";
  659. if(strpos($line[0], "http/1.") === false){
  660. /*if($line[0] == "content-type:"){
  661. fclose($fp);
  662. return !(strpos($line[1], "image/jpeg") === false);
  663. }*/
  664. if($line[0] == "content-length:"){
  665. fclose($fp);
  666. $result = (int)$line[1] > 807;
  667. break;
  668. }
  669. }
  670. else{
  671. if($line[1] != "200"){
  672. fclose($fp);
  673. $result = false;
  674. //trigger_error("Invalid response: $line[1]", E_USER_NOTICE);
  675. break;
  676. }
  677. }
  678. }
  679. }
  680.  
  681. /*$end_time = explode (" ", microtime());
  682. $end_time = $end_time[1] + $end_time[0];
  683.  
  684. $log = array();
  685. $log[] = "After " . number_format(1000*($end_time - $start_time), 2) . " milliseconds,";
  686. $log[] = "I concluded that image: " . $img;
  687. if($result)
  688. $log[] = "EXISTED";
  689. else
  690. $log[] = "did NOT EXIST";
  691. logRequest($log);*/
  692.  
  693. if($allowsettimelimit) set_time_limit(30);
  694.  
  695. return $result;
  696. }
  697.  
  698. /**
  699. * Advances the array's internal pointer to the last element.
  700. *
  701. * @param array $array the array whose last element should be returned.
  702. *
  703. * @return reference a reference to the last element in the array.
  704. *
  705. * @access private
  706. */
  707. function &array_last(&$array) {
  708. if (!is_array($array))
  709. return $array;
  710. if (!count($array))
  711. return null;
  712.  
  713. end($array);
  714. return $array[key($array)];
  715. }
  716.  
  717. /**
  718. * Tries to guess the AWS mode corresponding to a catalog returned in a feed.
  719. *
  720. * Used to guess the mode of a browse node obtained from a product's {@link HeavyDetails::browseList browseList}.
  721. *
  722. * WARNING!!! This is an experimental feature. Use it, tweak it, and send feedback!!!
  723. *
  724. * @param string $catalog the catalog that should be converted
  725. * @param string $locale the locale for which you want the AWS mode. Important because
  726. * modes are different in each locale.
  727. *
  728. * @return string the AWS mode corresponding to a catalog
  729. */
  730. function getModeFromCatalog($catalog, $locale){
  731.  
  732. $catalog = $new_str = ereg_replace(" +", "", strtolower($catalog));
  733.  
  734. if($catalog == "book" || $catalog == "ebook")
  735. return getMode($locale, "books");
  736. elseif($catalog == "magazine")
  737. return getMode($locale, "magazines");
  738. elseif($catalog == "homeimprovement")
  739. return getMode($locale, "tools");
  740. elseif($catalog == "toy")
  741. return getMode($locale, "toys");
  742. elseif($catalog == "video")
  743. return getMode($locale, "vhs");
  744. elseif(strstr($catalog, "lawn"))
  745. return getMode($locale, "garden");
  746. elseif(getMode($locale, $catalog))
  747. return getMode($locale, $catalog);
  748. else{
  749. logError(1024, $catalog . " could not be converted to AWS mode", "aq.lib.php", "389");
  750. return "misc";
  751. }
  752. }
  753.  
  754. /**
  755. * Devloper's token. DO NOT TOUCH. Used by AWS for tracking.
  756. * @access private
  757. */
  758. define("_AMAZOOP_DEV_TOKEN", "D3R39P4FOXC4MW");
  759.  
  760. ?>
 
Documentation generated on Sat, 21 Aug 2004 17:40:16 +0200 by phpDocumentor 1.3.0RC3
hosted by
SourceForge.net Logo