changeset 31:4124f103b46b

fix old :-( preg bug, finally (?) get length and escaping right
author Robert Boland <robert@markup.co.uk>
date Fri, 04 Jan 2019 08:52:01 -0500
parents b1bbf44c701b
children 8130865e9e82
files xml/getAmazonInfo.php
diffstat 1 files changed, 40 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/xml/getAmazonInfo.php	Fri Jan 04 08:50:23 2019 -0500
+++ b/xml/getAmazonInfo.php	Fri Jan 04 08:52:01 2019 -0500
@@ -137,13 +137,14 @@
 	$review3 = "";
 	// HST added this
 	$mm=array();
-	if (preg_match("<Error>",$output,$mm)) {
+	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 
@@ -292,7 +293,7 @@
 	    if ($item && $item->ItemAttributes && $item->ItemAttributes->Title) {
 	      $title = $item->ItemAttributes->Title; }
 	    else { $title = "[no title]"; };
-	    file_put_contents('/var/ywww/debug/phpDebug',"win: ".
+	    file_put_contents('/var/ywww/debug/phpDebug',"win: |$errorCode| ".
 			    $title."\n",
 			      FILE_APPEND);
 			return $output;
@@ -318,18 +319,6 @@
 		  $pubDate="\"$pubDate\"";
 		}
 		$publisher = $xml->Items->Item[0]->ItemAttributes->Publisher;
-		if ($publisher and strlen($publisher)>30) {
-		  $publisher=substr($publisher,0,30);
-		}
-		if ($author and strlen($author)>30) {
-		  $author=substr($author,0,30);
-		}
-		if ($title and strlen($title)>100) {
-		  $title=substr($title,0,100);
-		}
-		$publisher=mysqli_real_escape_string($link,$publisher);
-		$author=mysqli_real_escape_string($link,$author);
-		$title=mysqli_real_escape_string($link,$title);
 	      }
 	      else {
 		$title = $salesRank = "";
@@ -392,18 +381,42 @@
 	      if($salesRank == "")
 		$salesRank = "null";
 			
-	      $title = strtr($title, '"', "'");
-	      include "../../private/db.php";
-	      $review1 = mysqli_real_escape_string($link,$review1);
-	      if (strlen($review1)>500) { $review1=substr($review1,0,500);}
-	      $review2 = mysqli_real_escape_string($link,$review2);
-	      if (strlen($review2)>500) { $review2=substr($review2,0,500);}
-	      $review3 = mysqli_real_escape_string($link,$review3);
-	      if (strlen($review3)>500) { $review3=substr($review3,0,500);}
-			
 	      if($title != "")
 		{
-		  $queryInsert = "CALL b_addNewBook(\"$searchparameterdata\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,\"$pubDate\",\"$publisher\",$g1,$g2,$g3,$loc)";	
+		  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;
 				
 		  $res = mysqli_query($link, $queryInsert);
@@ -411,18 +424,18 @@
 		    $err=mysqli_error( $link );
 		    mysqli_close($link);
 		    file_put_contents('/var/ywww/debug/phpDebug',
-				      "anb failed: $err, $pubDate, $g2, $publisher, $title\n",
+				      "anb failed: $queryInsert\n$err\n",
 				      FILE_APPEND);
 		    exit($err);
 		  }
 		  $queryInsertReviews = "CALL b_insertReviews(\"$searchparameterdata\",\"$review1\",\"$review2\",\"$review3\")";
-		  if($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: $err, $pubDate, $g2, $publisher, $title\n",
+					"anr failed: $queryInsertReviews\n",
 					FILE_APPEND);
 		      exit($err);
 		    }