changeset 32:8130865e9e82

refactor to try to improve error handling
author Charlie Root
date Fri, 04 Jan 2019 09:50:55 -0500
parents 4124f103b46b
children c9d9b76ecbf9
files xml/getAmazonInfo.php
diffstat 1 files changed, 487 insertions(+), 461 deletions(-) [+]
line wrap: on
line diff
--- a/xml/getAmazonInfo.php	Fri Jan 04 08:52:01 2019 -0500
+++ b/xml/getAmazonInfo.php	Fri Jan 04 09:50:55 2019 -0500
@@ -1,488 +1,514 @@
 <?php
 
-$lastReqTime=0;
 function getAmazonDet($isbn,$go,$localeIn)
 {
+  $Adefault=array(
+		  'language'           =>'en',           //what language to render the page in
+		  'locale'             =>$localeIn,           //which server's products? available: ca,de,fr,jp,uk,us
+		  //'mode'               =>'books',        //what product category?
+		  'page'               =>1,              //first page to show (we are counting from 1 not 0)
+		  //'search'             =>'Machiavelli',  //what to search for?
+		  'operation'          =>'ItemLookup',   //what to do?	//ItemSearch
+		  // 'searchindex'        =>'Books',        //what product category for search?
+		  'searchparameter'    =>'ItemId',       //what kind of search?
+		  'searchparameterdata'=>$isbn,  //what to search for?
+		  //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true'
+		  'show_array'         =>false,          //debug: show complete incoming array? You can use this to see what other information Amazon is sending
+		  'show_url'           =>false,          //debug: show XML request url to be send to Amazon?
+		  'show_xml'           =>false,          //debug: show incoming XML code from Amazon?
+		  );
+  //change the debug options to true if you want to activate them or call the script with '?show_array=true' to see what actual information you're getting from Amazon and how little my standard script is actually showing of it
+  //for all parameters see if the user has overruled it or use the default
+  foreach ($Adefault as $i=>$d) {
+    $$i=isset($_GET[$i])?$_GET[$i]:$d;
+  }
+  switch($locale){
+  case "us": 
+    $loc = 0;
+    break;
+  case "uk": 
+    $loc = 1;
+    break;
+  case "ca": 
+    $loc = 2;
+    break;
+  case "de": 
+    $loc = 3;
+    break;
+  case "fr": 
+    $loc = 4;
+    break;
+  };		
+
+  for ($i=1; $i<=3; $i++)  {
+    try {  
+      return getAmazonDetRemote($go,$language,$locale,$loc,
+				$page,$operation,$searchparameter,
+				$searchparameterdata,
+				$show_array,$show_url,$show_xml);
+    }
+    catch (Exception $code) {
+      file_put_contents('/var/ywww/debug/phpDebug',
+			"Bang: $code $i\n",FILE_APPEND);
+      if ($code=='RequestThrottled') {
+	usleep(200000); // Try to reduce throttling until we get a 
+	// principled solution in place
+	$bail=False;
+      }
+      else {
+	$bail=True;
+      }
+    }
+    if ($bail) { break; }
+  }
+  return getAmazonDetLocal($go,$language,$loc,
+			   $page,$operation,$searchparameter,
+			   $searchparameterdata,
+			   $show_array,$show_url,$show_xml);
+}
+
+function getAmazonDetRemote($go,$language,$locale,$loc,
+			    $page,$operation,$searchparameter,
+			    $searchparameterdata,
+			    $show_array,$show_url,$show_xml)
+{
   global $output;
-$Adefault=array(
-  'language'           =>'en',           //what language to render the page in
-  'locale'             =>$localeIn,           //which server's products? available: ca,de,fr,jp,uk,us
-//'mode'               =>'books',        //what product category?
-  'page'               =>1,              //first page to show (we are counting from 1 not 0)
-//'search'             =>'Machiavelli',  //what to search for?
-  'operation'          =>'ItemLookup',   //what to do?	//ItemSearch
- // 'searchindex'        =>'Books',        //what product category for search?
-  'searchparameter'    =>'ItemId',       //what kind of search?
-  'searchparameterdata'=>$isbn,  //what to search for?
-  //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true'
-  'show_array'         =>false,          //debug: show complete incoming array? You can use this to see what other information Amazon is sending
-  'show_url'           =>false,          //debug: show XML request url to be send to Amazon?
-  'show_xml'           =>false,          //debug: show incoming XML code from Amazon?
-);
-//change the debug options to true if you want to activate them or call the script with '?show_array=true' to see what actual information you're getting from Amazon and how little my standard script is actually showing of it
 
-$Aassociates_id=array(
-  'uk' => 'bookwhack-21',
-  'us' => 'your02b-20',
-  'ca' => 'book009-20',
-  'de' => 'book04c-21',
-  'fr' => 'book07f-21',
-);
+  $Aassociates_id=array(
+			'uk' => 'bookwhack-21',
+			'us' => 'your02b-20',
+			'ca' => 'book009-20',
+			'de' => 'book04c-21',
+			'fr' => 'book07f-21',
+			);
 
-$Aserver=array(
-  'ca' => array(
-    'ext' => 'ca'                      ,  //Canadian normal server
-    'nor' => 'http://www.amazon.ca'    ,  //Canadian normal server
-    'xml' => 'http://xml.amazon.com'   ,  //Canadian xml server
-  ),
-  'de' => array(
-    'ext' => 'de'                      ,  //German normal server
-    'nor' => 'http://www.amazon.de'    ,  //German normal server
-    'xml' => 'http://xml-eu.amazon.com',  //German xml server
-  ),
-  'fr' => array(
-    'ext' => 'fr'                      ,  //French normal server
-    'nor' => 'http://www.amazon.fr'    ,  //French normal server
-    'xml' => 'http://xml-eu.amazon.com',  //French xml server
-  ),
-  'jp' => array(
-    'ext' => 'jp'                      ,  //Japanese normal server, not co.jp!
-    'nor' => 'http://www.amazon.co.jp' ,  //Japanese normal server
-    'xml' => 'http://xml.amazon.com'   ,  //Japanese xml server
-  ),
-  'uk' => array(
-    'ext' => 'co.uk'                   ,  //UK normal server
-    'nor' => 'http://www.amazon.co.uk' ,  //UK normal server
-    'xml' => 'http://xml-eu.amazon.com',  //UK xml server
-  ),
-  'us' => array(
-    'ext' => 'com'                     ,  //USA normal server
-    'nor' => 'http://www.amazon.com'   ,  //USA normal server
-    'xml' => 'http://xml.amazon.com'   ,  //USA xml server
-  ),
-);
+  $Aserver=array(
+		 'ca' => array(
+			       'ext' => 'ca'                      ,  //Canadian normal server
+			       'nor' => 'http://www.amazon.ca'    ,  //Canadian normal server
+			       'xml' => 'http://xml.amazon.com'   ,  //Canadian xml server
+			       ),
+		 'de' => array(
+			       'ext' => 'de'                      ,  //German normal server
+			       'nor' => 'http://www.amazon.de'    ,  //German normal server
+			       'xml' => 'http://xml-eu.amazon.com',  //German xml server
+			       ),
+		 'fr' => array(
+			       'ext' => 'fr'                      ,  //French normal server
+			       'nor' => 'http://www.amazon.fr'    ,  //French normal server
+			       'xml' => 'http://xml-eu.amazon.com',  //French xml server
+			       ),
+		 'jp' => array(
+			       'ext' => 'jp'                      ,  //Japanese normal server, not co.jp!
+			       'nor' => 'http://www.amazon.co.jp' ,  //Japanese normal server
+			       'xml' => 'http://xml.amazon.com'   ,  //Japanese xml server
+			       ),
+		 'uk' => array(
+			       'ext' => 'co.uk'                   ,  //UK normal server
+			       'nor' => 'http://www.amazon.co.uk' ,  //UK normal server
+			       'xml' => 'http://xml-eu.amazon.com',  //UK xml server
+			       ),
+		 'us' => array(
+			       'ext' => 'com'                     ,  //USA normal server
+			       'nor' => 'http://www.amazon.com'   ,  //USA normal server
+			       'xml' => 'http://xml.amazon.com'   ,  //USA xml server
+			       ),
+		 );
 
-//if(go != 1)
-	//include "aws_signed_request.php";  
-$public_key ="AKIAIHTNWC7L6LOUY4LQ";
-$private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde";
-//for all parameters see if the user has overruled it or use the default
-foreach ($Adefault as $i=>$d) {
-  $$i=isset($_GET[$i])?$_GET[$i]:$d;
-}
-//this is the data that is used to form the request for AWS
-//this is the part that is search specific
+  //if(go != 1)
+  //include "aws_signed_request.php";  
+  $public_key ="AKIAIHTNWC7L6LOUY4LQ";
+  $private_key="zWQlIzndJDtXNfxEXH7K7YR7hzv3u77lOcqfqPde";
+  //this is the data that is used to form the request for AWS
+  //this is the part that is search specific
   $parameters=array(
-    'Operation'       =>$operation              ,
-    //'Keywords'        =>urlencode($search)      ,
-    //'SearchIndex'     =>$searchindex            ,  //Books for example.
-    "$searchparameter"=>$searchparameterdata    ,
-    'ItemPage'        =>$page                   ,  //which page?
-    'AssociateTag'    =>$Aassociates_id[$locale],
-    'ResponseGroup'   =>'ItemAttributes,Reviews,EditorialReview,OfferSummary,Offers,Images,AlternateVersions,SalesRank,BrowseNodes'                ,  //Small, Medium, Large or SellerListing,'BrowseNodes',// ,
-	'ReviewSort'	  =>'-HelpfulVotes'
-  );
+		    'Operation'       =>$operation              ,
+		    //'Keywords'        =>urlencode($search)      ,
+		    //'SearchIndex'     =>$searchindex            ,  //Books for example.
+		    "$searchparameter"=>$searchparameterdata    ,
+		    'ItemPage'        =>$page                   ,  //which page?
+		    'AssociateTag'    =>$Aassociates_id[$locale],
+		    'ResponseGroup'   =>'ItemAttributes,Reviews,EditorialReview,OfferSummary,Offers,Images,AlternateVersions,SalesRank,BrowseNodes'                ,  //Small, Medium, Large or SellerListing,'BrowseNodes',// ,
+		    'ReviewSort'	  =>'-HelpfulVotes'
+		    );
 
-$requestURI = $_SERVER['REQUEST_URI'];
-$requestIP = $_SERVER['REMOTE_ADDR'];
-// if ($requestIP=="173.161.113.65" || $requestIP=="141.8.132.25") {
-// 	    $delay=60;
-// 	    file_put_contents('/var/ywww/debug/phpDebug',
-// 			      "bad guy: $requestIP, $requestURI\n",
-// 			      FILE_APPEND);
-// 	    sleep($delay);
-// 	    # No, can't do this
-// 	    # return; # bomb!
-// 	    # 'Kung', sitting on my desk in the office while I'm at home,
-// 	    # is occasionally hitting xml/getAmazonInfo.php:
-// 	    # e.g. Losing: ItemLookupErrorResponse, RequestThrottled, 129.215.197.36, /xml/getAmazonInfo.php?searchparameterdata=075154454X&locale=uk
-// 	    # repeatedly, same params
-// }
-$ext=$Aserver[$locale]['ext'];  
-$file_data=$ext;
-ksort($parameters);
-foreach ($parameters as $i=>$d) {
-  $file_data.='&'.$i.'='.$d;
-}
-        $gotit=0;
-        $url=aws_signed_request($ext,$parameters,$public_key,$private_key);  
-        $crl = curl_init();
-        $timeout = 5;
-        curl_setopt ($crl, CURLOPT_URL,$url);
-		curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); 
-        curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
-        curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
-	$semaphore = new SyncSemaphore("Amazon");
-	$gotit = $semaphore->lock(1000);
-	if (!$gotit) {
-	  file_put_contents('/var/ywww/debug/phpDebug',
-			"Got it: ".(int)$gotit." $requestIP 1\n",FILE_APPEND);
-	  $gotit=$semaphore->lock(1000);
-	  file_put_contents('/var/ywww/debug/phpDebug',
-		        "Got it: ".(int)$gotit." $requestIP 1a\n",FILE_APPEND);
-	}
-        $output = curl_exec($crl);
-        curl_close($crl);
-	usleep(500000);
-	if ($gotit) {
-	  $semaphore->unlock();
-	}
-	else {
-	  file_put_contents('/var/ywww/debug/phpDebug',
-			    "W/o lock for $requestIP 1b\n",FILE_APPEND);
-	}
-	$review = "";
-	$review1 = "";
-	$review2 = "";
-	$review3 = "";
-	// HST added this
-	$mm=array();
-	if (preg_match("/<Error>/",$output,$mm)) {
-	  $xml = new SimpleXMLElement($output);	
-	  $resName=$xml->getName();
-	  $code=$xml->Error->Code;
-	  if (!$code) {
-	    $code=$xml->Items->Request->Errors->Error->Code;
-	  }
-	  $errorCode=$code;
-	  file_put_contents('/var/ywww/debug/phpDebug',"Losing: ".$mm[0].", $resName, $code, $requestIP, $requestURI\n",FILE_APPEND);
-	  if ($code=='RequestThrottled') {
-	      usleep(200000); // Try to reduce throttling until we get a 
-	      // principled solution in place
+  $requestURI = $_SERVER['REQUEST_URI'];
+  $requestIP = $_SERVER['REMOTE_ADDR'];
+  // if ($requestIP=="173.161.113.65" || $requestIP=="141.8.132.25") {
+  // 	    $delay=60;
+  // 	    file_put_contents('/var/ywww/debug/phpDebug',
+  // 			      "bad guy: $requestIP, $requestURI\n",
+  // 			      FILE_APPEND);
+  // 	    sleep($delay);
+  // 	    # No, can't do this
+  // 	    # return; # bomb!
+  // 	    # 'Kung', sitting on my desk in the office while I'm at home,
+  // 	    # is occasionally hitting xml/getAmazonInfo.php:
+  // 	    # e.g. Losing: ItemLookupErrorResponse, RequestThrottled, 129.215.197.36, /xml/getAmazonInfo.php?searchparameterdata=075154454X&locale=uk
+  // 	    # repeatedly, same params
+  // }
+  $ext=$Aserver[$locale]['ext'];  
+  $file_data=$ext;
+  ksort($parameters);
+  foreach ($parameters as $i=>$d) {
+    $file_data.='&'.$i.'='.$d;
+  }
+  $gotit=0;
+  $url=aws_signed_request($ext,$parameters,$public_key,$private_key);  
+  $crl = curl_init();
+  $timeout = 5;
+  curl_setopt ($crl, CURLOPT_URL,$url);
+  curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); 
+  curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
+  curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
+  $semaphore = new SyncSemaphore("Amazon");
+  $gotit = $semaphore->lock(1000);
+  if (!$gotit) {
+    file_put_contents('/var/ywww/debug/phpDebug',
+		      "Got it: ".(int)$gotit." $requestIP 1\n",FILE_APPEND);
+    $gotit=$semaphore->lock(1000);
+    file_put_contents('/var/ywww/debug/phpDebug',
+		      "Got it: ".(int)$gotit." $requestIP 1a\n",FILE_APPEND);
+  }
+  $output = curl_exec($crl);
+  curl_close($crl);
+  usleep(500000);
+  if ($gotit) {
+    $semaphore->unlock();
+  }
+  else {
+    file_put_contents('/var/ywww/debug/phpDebug',
+		      "W/o lock for $requestIP 1b\n",FILE_APPEND);
+  }
+  $review = "";
+  $review1 = "";
+  $review2 = "";
+  $review3 = "";
+  // HST added this
+  $mm=array();
+  if (preg_match("/<Error>/",$output,$mm)) {
+    $xml = new SimpleXMLElement($output);	
+    $resName=$xml->getName();
+    $code=$xml->Error->Code;
+    if (!$code) {
+      $code=$xml->Items->Request->Errors->Error->Code;
+    }
+    file_put_contents('/var/ywww/debug/phpDebug',"Losing: ".$mm[0].", $resName, $code, $requestIP, $requestURI\n",FILE_APPEND);
+    if ($code!='RequestThrottled') {
+      file_put_contents('/var/ywww/debug/phpDebug',"parms: $go\nurl: $url\n".
+			print_r($parameters,TRUE)."\n",FILE_APPEND);
+      if ($code=="") {
+	file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$output\n",FILE_APPEND);
+      }
+    }
+    throw new Exception($code);
+  }
+  else {
+    $xml = new SimpleXMLElement($output);	
+    set_error_handler(function () {
+	global $output;
+	file_put_contents('/var/ywww/debug/phpDebug',
+			  "Caught one?: ".$searchparameterdata,
+			  FILE_APPEND);
+	file_put_contents('/var/ywww/debug/phpDebug',
+			  print_r($output, TRUE)."\n",
+			  FILE_APPEND);
+      } );
+    $review = $xml->Items->Item->CustomerReviews->IFrameURL;
+    // The above is failing repeatedly -- 
+    //PHP Notice: Trying to get property of non-object in
+    // /var/ywww/xml/getAmazonInfo.php on line [109]
+    // See the dumped structure at the end of this file for the
+    // cause
+    restore_error_handler();
+    //echo $review;
+  }
+  if ($review != "")
+    {
+      $text = @file_get_contents($review . "&truncate=300");
+      $removeTop1 = preg_replace('~<div class="crIFrameHeaderLeftColumn">(.*?)<div class="crIFrameHeaderHistogram">~si', '', $text);
+      $removeTop2 = preg_replace('~<div class="crIFrameHeaderHistogram">(.*?)<div class="crIframeReviewList">~si', '', $text);
+      if (preg_match('~<body[^>]*>(.*?)</body>~si', $removeTop2, $body)){ $getBody = trim($body[1]); } else { $getBody = $removeTop2;}
+      $removeDiv = preg_replace('~<div[^>]*>(.*?)</div>~si', '', $getBody);
+      $removeCloseDivs = preg_replace('/<\/div>/','', $removeDiv);
+      $setBoundary = str_replace('<!-- BOUNDARY -->','BOTTOM-TOP', $removeCloseDivs);
+      //replace <!-- BOUNDARY --> with BOTTOM-TOP
+      $remove1 = '~<table cellpadding="0"(.*?)%">~s';
+      $setBoundary = preg_replace($remove1,'', $setBoundary);
+      $remove2 = '~</td><td bg(.*?)</table>~s';
+      $setBoundary = preg_replace($remove2,'', $setBoundary);
+      $remove3 = '~<a name=(.*?)</a>~s';
+      $setBoundary = preg_replace($remove3,'', $setBoundary);
+      $setBoundary2 = str_replace('<br />','', $setBoundary);
+      //remove all extra crap;
+      $setBoundary3 = str_replace('</td>','BOTTOM', $setBoundary2);
+      //replace </td> with BOTTOM
+		
+      if (preg_match_all('~TOP(.*?)BOTTOM~s', $setBoundary3, $reviews))
+	{ 
+	  $reviewContents = $reviews[1];
+	  //print_r($reviewContents);
+	  $review1 = trim($reviewContents[0]);
+	  $review1 = str_replace("\n", "", $review1);
+	  $review1 = str_replace("\r", "", $review1);
+	  if (isset($reviewContents[1])) {
+	    $review2 = trim($reviewContents[1]);
+	    $review2 = str_replace("\n", "", $review2);
+	    $review2 = str_replace("\r", "", $review2);
 	  }
 	  else {
-	     file_put_contents('/var/ywww/debug/phpDebug',"parms: $go\nurl: $url\n".
-		   print_r($parameters,TRUE)."\n",FILE_APPEND);
-	     if ($code=="") {
-	       file_put_contents('/var/ywww/debug/phpDebug',"error elt:\n$output\n",FILE_APPEND);
-	     }
+	    $review2 = "";
+	  }
+	  if (isset($reviewContents[2])) {
+	    $review3 = trim($reviewContents[2]);
+	    $review3 = str_replace("\n", "", $review3);
+	    $review3 = str_replace("\r", "", $review3);
+	  }
+	  else {
+	    $review3 = "";
 	  }
 	}
+      else
+	{
+	  $review1 = "";
+	  $review2 = "";
+	  $review3 = "";
+	  //echo "EPIC FAIL";
+	}
+
+      unset($xml->Items->Item->CustomerReviews);
+      $xdoc = new DomDocument;
+      $xdoc->loadXML($xml->asXML());
+		
+      $cReviews = $xdoc ->createElement('CustomerReviews');
+      $cReviewHolder = $xdoc ->createElement('Review');
+      $cReview = $xdoc ->createElement('Content');
+      $cReviewHolder2 = $xdoc ->createElement('Review');
+      $cReview2 = $xdoc ->createElement('Content');	
+      $cReviewHolder3 = $xdoc ->createElement('Review');
+      $cReview3 = $xdoc ->createElement('Content');
+		
+      $txtNode = $xdoc ->createTextNode ($review1);
+      $cReview -> appendChild($txtNode);
+		
+      $txtNode2 = $xdoc ->createTextNode ($review2);
+      $cReview2 -> appendChild($txtNode2);
+		
+      $txtNode3 = $xdoc ->createTextNode ($review3);
+      $cReview3 -> appendChild($txtNode3);
+		
+      $cReviewHolder -> appendChild($cReview);
+      $cReviewHolder2 -> appendChild($cReview2);
+      $cReviewHolder3 -> appendChild($cReview3);
+		
+      $cReviews -> appendChild($cReviewHolder);
+      $cReviews -> appendChild($cReviewHolder2);
+      $cReviews -> appendChild($cReviewHolder3);
+		
+      $xdoc->documentElement->childNodes->item(1)->childNodes->item(1)->appendChild($cReviews);
+      $newXML = simplexml_import_dom($xdoc);
+      $output = $newXML->asXml();
+    }
+	
+  if($go == 1) {
+    $item = $xml->Items->Item[0];
+    if ($item && $item->ItemAttributes && $item->ItemAttributes->Title) {
+      $title = $item->ItemAttributes->Title; }
+    else { $title = "[no title]"; };
+    file_put_contents('/var/ywww/debug/phpDebug',"win: |$errorCode| ".
+		      $title."\n",
+		      FILE_APPEND);
+    return $output;
+  }
+  else
+    {
+      if ($xml->Items->Item) {
+	$title = $xml->Items->Item[0]->ItemAttributes->Title;
+	$author = $xml->Items->Item[0]->ItemAttributes->Author;
+	$binding = $xml->Items->Item[0]->ItemAttributes->Binding;
+	$dewey = $xml->Items->Item[0]->ItemAttributes->DeweyDecimalNumber;
+	if($dewey == "")
+	  $dewey = "null";
+	$imageURL = $xml->Items->Item[0]->MediumImage->URL;
+	$salesRank = $xml->Items->Item[0]->SalesRank;
+	$pubDate = $xml->Items->Item[0]->ItemAttributes->PublicationDate;
+	if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";}
+	if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";}
+	if (strlen($pubDate)==0) {
+	  $pubDate="null";
+	}
 	else {
-	  $xml = new SimpleXMLElement($output);	
-	  set_error_handler(function () {
-	      global $output;
-	      file_put_contents('/var/ywww/debug/phpDebug',
-				"Caught one?: ".$searchparameterdata,
-				FILE_APPEND);
-	      file_put_contents('/var/ywww/debug/phpDebug',
-				print_r($output, TRUE)."\n",
-				FILE_APPEND);
-	    } );
-	  $review = $xml->Items->Item->CustomerReviews->IFrameURL;
-	  // The above is failing repeatedly -- 
-	  //PHP Notice: Trying to get property of non-object in
-	  // /var/ywww/xml/getAmazonInfo.php on line [109]
-	  // See the dumped structure at the end of this file for the
-	  // cause
-	  restore_error_handler();
-	  //echo $review;
+	  $pubDate="\"$pubDate\"";
+	}
+	$publisher = $xml->Items->Item[0]->ItemAttributes->Publisher;
+      }
+      else {
+	$title = $salesRank = "";
+	$dewey = "null";
+      }
+			
+      $genreID = "";
+      $genre = "";		
+      $genArr = array();
+
+      if ($xml->Items->Item[0] && $xml->Items->Item[0]->BrowseNodes) {
+	for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){	
+	  //sexy recursive function
+	  findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre);
+				
+	  if($genre != "")
+	    $genArr[strval($genreID)] = strval($genre);
+	  //$genArr[$i] = array(strval($genreID) => strval($genre));
+
+	  //echo $genre;
+	  //echo $genreID;
+				
+	  $genre = "";
+	  $genreID = "";
 	}
-	if ($review != "")
+      }
+			
+      $g1 = "null";
+      $g2 = "null";
+      $g3 = "null";
+      $loop = 1;
+			
+      foreach ($genArr as $key => $value) {
+	//echo "$key => $value";
+	if ($key>2047) {
+	  //HST added
+	  break;
+	}
+	$queryG = "CALL b_addBrowseNode($key,\"$value\")";	//add the name value pair for genre to new table	
+	//echo $queryG;
+	include "../../private/db.php";
+	$resG = mysqli_query($link, $queryG);
+	mysqli_close($link);
+				
+	switch ($loop) {
+	case 1:
+	  $g1 = $key;
+	  break;
+	case 2:
+	  $g2 = $key;
+	  break;
+	case 3:
+	  $g3 = $key;
+	  break;
+	}
+				
+	$loop++;
+      }
+			
+      if($salesRank == "")
+	$salesRank = "null";
+			
+      if($title != "")
 	{
-		$text = @file_get_contents($review . "&truncate=300");
-		$removeTop1 = preg_replace('~<div class="crIFrameHeaderLeftColumn">(.*?)<div class="crIFrameHeaderHistogram">~si', '', $text);
-		$removeTop2 = preg_replace('~<div class="crIFrameHeaderHistogram">(.*?)<div class="crIframeReviewList">~si', '', $text);
-		if (preg_match('~<body[^>]*>(.*?)</body>~si', $removeTop2, $body)){ $getBody = trim($body[1]); } else { $getBody = $removeTop2;}
-		$removeDiv = preg_replace('~<div[^>]*>(.*?)</div>~si', '', $getBody);
-		$removeCloseDivs = preg_replace('/<\/div>/','', $removeDiv);
-		$setBoundary = str_replace('<!-- BOUNDARY -->','BOTTOM-TOP', $removeCloseDivs);
-		//replace <!-- BOUNDARY --> with BOTTOM-TOP
-		$remove1 = '~<table cellpadding="0"(.*?)%">~s';
-		$setBoundary = preg_replace($remove1,'', $setBoundary);
-		$remove2 = '~</td><td bg(.*?)</table>~s';
-		$setBoundary = preg_replace($remove2,'', $setBoundary);
-		$remove3 = '~<a name=(.*?)</a>~s';
-		$setBoundary = preg_replace($remove3,'', $setBoundary);
-		$setBoundary2 = str_replace('<br />','', $setBoundary);
-		//remove all extra crap;
-		$setBoundary3 = str_replace('</td>','BOTTOM', $setBoundary2);
-		//replace </td> with BOTTOM
-		
-		if (preg_match_all('~TOP(.*?)BOTTOM~s', $setBoundary3, $reviews))
-		{ 
-			$reviewContents = $reviews[1];
-			//print_r($reviewContents);
-			$review1 = trim($reviewContents[0]);
-			$review1 = str_replace("\n", "", $review1);
-			$review1 = str_replace("\r", "", $review1);
-			if (isset($reviewContents[1])) {
-			  $review2 = trim($reviewContents[1]);
-			  $review2 = str_replace("\n", "", $review2);
-			  $review2 = str_replace("\r", "", $review2);
-			}
-			else {
-			  $review2 = "";
-			}
-			if (isset($reviewContents[2])) {
-			  $review3 = trim($reviewContents[2]);
-			  $review3 = str_replace("\n", "", $review3);
-			  $review3 = str_replace("\r", "", $review3);
-			}
-			else {
-			  $review3 = "";
-			}
-		}
-		else
-		{
-			$review1 = "";
-			$review2 = "";
-			$review3 = "";
-			//echo "EPIC FAIL";
-		}
-
-		unset($xml->Items->Item->CustomerReviews);
-		$xdoc = new DomDocument;
-		$xdoc->loadXML($xml->asXML());
-		
-		$cReviews = $xdoc ->createElement('CustomerReviews');
-		$cReviewHolder = $xdoc ->createElement('Review');
-		$cReview = $xdoc ->createElement('Content');
-		$cReviewHolder2 = $xdoc ->createElement('Review');
-		$cReview2 = $xdoc ->createElement('Content');	
-		$cReviewHolder3 = $xdoc ->createElement('Review');
-		$cReview3 = $xdoc ->createElement('Content');
-		
-		$txtNode = $xdoc ->createTextNode ($review1);
-		$cReview -> appendChild($txtNode);
-		
-		$txtNode2 = $xdoc ->createTextNode ($review2);
-		$cReview2 -> appendChild($txtNode2);
-		
-		$txtNode3 = $xdoc ->createTextNode ($review3);
-		$cReview3 -> appendChild($txtNode3);
-		
-		$cReviewHolder -> appendChild($cReview);
-		$cReviewHolder2 -> appendChild($cReview2);
-		$cReviewHolder3 -> appendChild($cReview3);
-		
-		$cReviews -> appendChild($cReviewHolder);
-		$cReviews -> appendChild($cReviewHolder2);
-		$cReviews -> appendChild($cReviewHolder3);
-		
-		$xdoc->documentElement->childNodes->item(1)->childNodes->item(1)->appendChild($cReviews);
-		$newXML = simplexml_import_dom($xdoc);
-		$output = $newXML->asXml();
-	}
-	
-	switch($locale){
-		case "us": 
-			$loc = 0;
-			break;
-		case "uk": 
-			$loc = 1;
-			break;
-		case "ca": 
-			$loc = 2;
-			break;
-		case "de": 
-			$loc = 3;
-			break;
-		case "fr": 
-			$loc = 4;
-			break;
-	};		
-	
-	$errorCode = $xml->Error->Code;
-	//echo $errorCode;
-
-	if($errorCode != "AccountLimitExceeded")
-	{
-	  if($go == 1) {
-	    $item = $xml->Items->Item[0];
-	    if ($item && $item->ItemAttributes && $item->ItemAttributes->Title) {
-	      $title = $item->ItemAttributes->Title; }
-	    else { $title = "[no title]"; };
-	    file_put_contents('/var/ywww/debug/phpDebug',"win: |$errorCode| ".
-			    $title."\n",
-			      FILE_APPEND);
-			return $output;
+	  include "../../private/db.php";
+	  if ($publisher->count()==0) {
+	    $publisher="null";
+	  }
+	  else {
+	    $publisher=mysqli_real_escape_string($link,$publisher);
+	    if (strlen($publisher)>30) {
+	      $publisher=rtrim(substr($publisher,0,30),"\\");
+	    }
+	    $publisher="\"".$publisher."\"";
+	  }
+	  if ($author->count()==0) {
+	    $author="unknown";
 	  }
-	  else
-	    {
-	      if ($xml->Items->Item) {
-		$title = $xml->Items->Item[0]->ItemAttributes->Title;
-		$author = $xml->Items->Item[0]->ItemAttributes->Author;
-		$binding = $xml->Items->Item[0]->ItemAttributes->Binding;
-		$dewey = $xml->Items->Item[0]->ItemAttributes->DeweyDecimalNumber;
-		if($dewey == "")
-		  $dewey = "null";
-		$imageURL = $xml->Items->Item[0]->MediumImage->URL;
-		$salesRank = $xml->Items->Item[0]->SalesRank;
-		$pubDate = $xml->Items->Item[0]->ItemAttributes->PublicationDate;
-		if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";}
-		if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";}
-		if (strlen($pubDate)==0) {
-		  $pubDate="null";
-		}
-		else {
-		  $pubDate="\"$pubDate\"";
-		}
-		$publisher = $xml->Items->Item[0]->ItemAttributes->Publisher;
-	      }
-	      else {
-		$title = $salesRank = "";
-		$dewey = "null";
-	      }
-			
-	      $genreID = "";
-	      $genre = "";		
-	      $genArr = array();
-
-	      if ($xml->Items->Item[0] && $xml->Items->Item[0]->BrowseNodes) {
-		for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){	
-		  //sexy recursive function
-		  findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre);
-				
-		  if($genre != "")
-		    $genArr[strval($genreID)] = strval($genre);
-		  //$genArr[$i] = array(strval($genreID) => strval($genre));
-
-		  //echo $genre;
-		  //echo $genreID;
-				
-		  $genre = "";
-		  $genreID = "";
-		}
-	      }
-			
-	      $g1 = "null";
-	      $g2 = "null";
-	      $g3 = "null";
-	      $loop = 1;
-			
-	      foreach ($genArr as $key => $value) {
-		//echo "$key => $value";
-		if ($key>2047) {
-		  //HST added
-		  break;
-		}
-		$queryG = "CALL b_addBrowseNode($key,\"$value\")";	//add the name value pair for genre to new table	
-		//echo $queryG;
-		include "../../private/db.php";
-		$resG = mysqli_query($link, $queryG);
-		mysqli_close($link);
-				
-		switch ($loop) {
-		case 1:
-		  $g1 = $key;
-		  break;
-		case 2:
-		  $g2 = $key;
-		  break;
-		case 3:
-		  $g3 = $key;
-		  break;
-		}
-				
-		$loop++;
-	      }
-			
-	      if($salesRank == "")
-		$salesRank = "null";
+	  else {
+	    $author=mysqli_real_escape_string($link,$author);
+	    if (strlen($author)>30) {
+	      $author=rtrim(substr($author,0,30),"\\");
+	    }
+	  }
+	  $author="\"".$author."\"";
+	  $title=mysqli_real_escape_string($link,$title);
+	  if (strlen($title)>100) {
+	    $title=rtrim(substr($title,0,100),"\\");
+	  }
+	  $title="\"".$title."\"";
+	  $review1 = mysqli_real_escape_string($link,$review1);
+	  if (strlen($review1)>500) { $review1=rtrim(substr($review1,0,500),"\\");}
+	  $review2 = mysqli_real_escape_string($link,$review2);
+	  if (strlen($review2)>500) { $review2=rtrim(substr($review2,0,500),"\\");}
+	  $review3 = mysqli_real_escape_string($link,$review3);
+	  if (strlen($review3)>500) { $review3=rtrim(substr($review3,0,500),"\\");}
 			
-	      if($title != "")
-		{
-		  include "../../private/db.php";
-		  if ($publisher->count()==0) {
-		    $publisher="null";
-		  }
-		  else {
-		    $publisher=mysqli_real_escape_string($link,$publisher);
-		    if (strlen($publisher)>30) {
-		      $publisher=rtrim(substr($publisher,0,30),"\\");
-		    }
-		    $publisher="\"".$publisher."\"";
-		  }
-		  if ($author->count()==0) {
-		    $author="unknown";
-		  }
-		  else {
-		    $author=mysqli_real_escape_string($link,$author);
-		    if (strlen($author)>30) {
-		      $author=rtrim(substr($author,0,30),"\\");
-		    }
-		  }
-		  $author="\"".$author."\"";
-		  $title=mysqli_real_escape_string($link,$title);
-		  if (strlen($title)>100) {
-		    $title=rtrim(substr($title,0,100),"\\");
-		  }
-		  $title="\"".$title."\"";
-		  $review1 = mysqli_real_escape_string($link,$review1);
-		  if (strlen($review1)>500) { $review1=rtrim(substr($review1,0,500),"\\");}
-		  $review2 = mysqli_real_escape_string($link,$review2);
-		  if (strlen($review2)>500) { $review2=rtrim(substr($review2,0,500),"\\");}
-		  $review3 = mysqli_real_escape_string($link,$review3);
-		  if (strlen($review3)>500) { $review3=rtrim(substr($review3,0,500),"\\");}
-			
-		  $queryInsert = "CALL b_addNewBook(\"$searchparameterdata\",$title, $author,\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,$publisher,$g1,$g2,$g3,$loc)";	             
-		  //echo $queryInsert;
+	  $queryInsert = "CALL b_addNewBook(\"$searchparameterdata\",$title, $author,\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,$publisher,$g1,$g2,$g3,$loc)";	             
+	  //echo $queryInsert;
 				
-		  $res = mysqli_query($link, $queryInsert);
-		  if (!$res) {
-		    $err=mysqli_error( $link );
-		    mysqli_close($link);
-		    file_put_contents('/var/ywww/debug/phpDebug',
-				      "anb failed: $queryInsert\n$err\n",
-				      FILE_APPEND);
-		    exit($err);
-		  }
-		  $queryInsertReviews = "CALL b_insertReviews(\"$searchparameterdata\",\"$review1\",\"$review2\",\"$review3\")";
-		  if($review1 != "") {
-		    $res = mysqli_query($link, $queryInsertReviews);
-		    if (!$res) {
-		      $err=mysqli_error( $link );
-		      mysqli_close($link);
-		      file_put_contents('/var/ywww/debug/phpDebug',
-					"anr failed: $queryInsertReviews\n",
-					FILE_APPEND);
-		      exit($err);
-		    }
-		  }
-		  mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop		
-		}
-
-	      echo $output;
+	  $res = mysqli_query($link, $queryInsert);
+	  if (!$res) {
+	    $err=mysqli_error( $link );
+	    mysqli_close($link);
+	    file_put_contents('/var/ywww/debug/phpDebug',
+			      "anb failed: $queryInsert\n$err\n",
+			      FILE_APPEND);
+	    exit($err);
+	  }
+	  $queryInsertReviews = "CALL b_insertReviews(\"$searchparameterdata\",\"$review1\",\"$review2\",\"$review3\")";
+	  if($review1 != "") {
+	    $res = mysqli_query($link, $queryInsertReviews);
+	    if (!$res) {
+	      $err=mysqli_error( $link );
+	      mysqli_close($link);
+	      file_put_contents('/var/ywww/debug/phpDebug',
+				"anr failed: $queryInsertReviews\n",
+				FILE_APPEND);
+	      exit($err);
 	    }
+	  }
+	  mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop		
 	}
-	else
-	{
-		//look up info from db	
-		include "../../private/db.php"; 
-		$query = "CALL b_getBookInfo('$searchparameterdata', $loc)";
-		//echo $query;
-		$res = mysqli_query($link, $query) or exit( mysqli_error( $link ));
+
+      echo $output;
+    }
+}
+
+function getAmazonDetLocal($go,$language,$loc,
+			   $page,$operation,$searchparameter,
+			   $searchparameterdata,
+			   $show_array,$show_url,$show_xml)
+{
+  global $output;
+  //look up info from db	
+  include "../../private/db.php"; 
+  $query = "CALL b_getBookInfo('$searchparameterdata', $loc)";
+  //echo $query;
+  $res = mysqli_query($link, $query) or exit( mysqli_error( $link ));
 	
-		$output = "";
-		$output .=  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
-		$output .=  "<Details>";
-		if ( mysqli_num_rows( $res ) > 0 )
-		{
-			$rows=mysqli_fetch_array($res, MYSQLI_ASSOC);
-			mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop						
-			$output .=  "<ASIN>" . $searchparameterdata . "</ASIN>";
-			$output .=  "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>";			
-			$output .=  "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>";
-			$output .=  "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>";
-			$output .=  "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>";
-			$output .=  "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>";
-			$output .=  "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>";
-			$output .=  "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>";
-			$output .=  "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>";
-			$output .=  "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>";
-			$output .=  "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>";
-			$output .=  "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>";
-			$output .=  "<ProductGroup>Book</ProductGroup>";
-			$output .=  "<Error>AccountLimitExceeded</Error>";
+  $output = "";
+  $output .=  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
+  $output .=  "<Details>";
+  if ( mysqli_num_rows( $res ) > 0 )
+    {
+      $rows=mysqli_fetch_array($res, MYSQLI_ASSOC);
+      mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop						
+      $output .=  "<ASIN>" . $searchparameterdata . "</ASIN>";
+      $output .=  "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>";			
+      $output .=  "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>";
+      $output .=  "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>";
+      $output .=  "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>";
+      $output .=  "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>";
+      $output .=  "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>";
+      $output .=  "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>";
+      $output .=  "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>";
+      $output .=  "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>";
+      $output .=  "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>";
+      $output .=  "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>";
+      $output .=  "<ProductGroup>Book</ProductGroup>";
+      $output .=  "<Error>AccountLimitExceeded</Error>";
 			
-		}
-		else {
-		  mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
-		}
-		$output .=  "</Details>";
-		echo $output;
-	}     
+    }
+  else {
+    mysqli_close($link);	//do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop
+  }
+  $output .=  "</Details>";
+  echo $output;
 }
 
 function findGenre($browseNode, &$ID, &$gen)