Mercurial > hg > ywww
annotate php/read/getBookDetails.php @ 48:da133c130c1d
include path normalise
author | Charlie Root |
---|---|
date | Sun, 06 Jan 2019 12:46:24 -0500 |
parents | b7376319d25a |
children | a67bf725e87b |
rev | line source |
---|---|
2 | 1 <?php |
44
38d209611508
change includes to assume (contra previos ASSUMES) that we have .user.ini which adds /var/test to include_path
Charlie Root
parents:
43
diff
changeset
|
2 include_once "ywww/dlog.php"; |
2 | 3 |
44
38d209611508
change includes to assume (contra previos ASSUMES) that we have .user.ini which adds /var/test to include_path
Charlie Root
parents:
43
diff
changeset
|
4 include_once "private/web.php"; |
2 | 5 function findGenre($browseNode, &$ID, &$gen) |
6 { | |
7 if($browseNode->Name == "Subjects") | |
8 { | |
9 return true; | |
10 } | |
11 else | |
12 { | |
13 if($browseNode->Ancestors->BrowseNode) | |
14 { | |
15 if(findGenre($browseNode->Ancestors->BrowseNode, $ID, $gen) == true) | |
16 { | |
17 $gen = $browseNode->Name; | |
18 $ID = $browseNode->BrowseNodeId; | |
19 | |
20 //if($browseNode->Name == "Literature & Fiction") //to return one level up the tree as well | |
21 // return true; | |
22 } | |
23 } | |
24 return false; | |
25 } | |
26 } | |
27 | |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
28 global $out,$isbn,$Aserver,$Aassociates_id; |
2 | 29 if(isset($_GET['isbn'])){ |
30 $isbn = $_GET['isbn']; | |
31 if(isset($_GET['locale'])){ | |
32 $locale=$_GET['locale']; | |
33 | |
34 switch ($locale) { | |
35 case "uk": | |
36 $loc = 1; | |
37 break; | |
38 case "us": | |
39 $loc = 0; | |
40 break; | |
41 case "ca": | |
42 $loc = 2; | |
43 break; | |
44 case "de": | |
45 $loc = 3; | |
46 break; | |
47 case "fr": | |
48 $loc = 4; | |
49 break; | |
50 case "": | |
51 $loc = 0; | |
52 break; | |
53 } | |
54 } | |
55 else{ | |
56 $loc=0; | |
57 } | |
58 $output = ""; | |
59 | |
60 $output .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; | |
61 $output .= "<BookDetails>"; | |
62 | |
63 $proceed = true; | |
44
38d209611508
change includes to assume (contra previos ASSUMES) that we have .user.ini which adds /var/test to include_path
Charlie Root
parents:
43
diff
changeset
|
64 include "private/db.php"; |
2 | 65 |
66 $query = "CALL b_getBookInfo('$isbn', $loc)"; | |
67 //select Timestamp,Title,Author,Binding,DeweyDecimal,ImageURL,SalesRank,PublicationDate,Publisher,Genre1,Genre2,Genre3 from books where ISBN = '$isbn'"; | |
68 //echo $query; | |
46 | 69 $res = my_query($query,'gbd1'); |
2 | 70 |
71 if ( mysqli_num_rows( $res ) > 0 ) | |
72 { | |
73 $rows=mysqli_fetch_array($res, MYSQLI_ASSOC); | |
23
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
74 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
2 | 75 $timestamp=$rows['Timestamp']; |
76 //echo $timestamp; | |
77 date_default_timezone_set('UTC'); | |
78 $dbStamp = strtotime($timestamp); | |
79 //echo $dbStamp; | |
80 $unixTime = time(); | |
81 //echo $unixTime; | |
82 $timeDiff = $unixTime - $dbStamp; | |
83 //echo $timeDiff; | |
84 | |
85 if($timeDiff < 2592000) //if it has been updated in the last 30 days | |
86 { | |
87 $proceed = false; | |
88 $output .= "<ISBN>" . $isbn . "</ISBN>"; | |
89 $output .= "<Title>" . htmlspecialchars($rows["Title"]) . "</Title>"; | |
90 $output .= "<Author>" . htmlspecialchars($rows["Author"]) . "</Author>"; | |
91 $output .= "<Binding>" . htmlspecialchars($rows["Binding"]) . "</Binding>"; | |
92 $output .= "<Dewey>" . htmlspecialchars($rows["DeweyDecimal"]) . "</Dewey>"; | |
93 $output .= "<ImageURL>" . htmlspecialchars($rows["ImageURL"]) . "</ImageURL>"; | |
94 $output .= "<SalesRank>" . htmlspecialchars($rows["SalesRank"]) . "</SalesRank>"; | |
95 $output .= "<PublicationDate>" . htmlspecialchars($rows["PublicationDate"]) . "</PublicationDate>"; | |
96 $output .= "<Publisher>" . htmlspecialchars($rows["Publisher"]) . "</Publisher>"; | |
97 $output .= "<Genre1>" . htmlspecialchars($rows["Genre1"]) . "</Genre1>"; | |
98 $output .= "<Genre2>" . htmlspecialchars($rows["Genre2"]) . "</Genre2>"; | |
99 $output .= "<Genre3>" . htmlspecialchars($rows["Genre3"]) . "</Genre3>"; | |
100 $output .= "<ProductGroup>Book</ProductGroup>"; | |
101 } | |
102 } | |
23
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
103 else { |
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
104 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
d606320ec331
post-5.7-upgrade efforts to reduce dropped connections, db insertion fails
Charlie Root
parents:
21
diff
changeset
|
105 } |
2 | 106 |
107 if($proceed == true) | |
36 | 108 { |
109 include "aws_signed_request.php"; | |
48 | 110 include_once "ywww/xml/doAmazonRequest.php"; |
36 | 111 |
112 //book does not exist already so look up all the info from browse nodes.... | |
2 | 113 |
36 | 114 $Adefault=array( |
115 'language' =>'en', //what language to render the page in | |
116 'locale' =>'us', //which server's products? available: ca,de,fr,jp,uk,us | |
117 'page' =>1, //first page to show (we are counting from 1 not 0) | |
118 'operation' =>'ItemLookup', //what to do? //ItemSearch | |
119 'searchparameter' =>'ItemId', //what kind of search? | |
120 'searchindex' => 'Books', | |
121 'searchparameterdata'=>$isbn, //what to search for? | |
122 'search' =>$isbn, | |
123 //here some debugging flags you can put at the end of the URL to call this script with, like: '?show_array=true' | |
124 'show_array' =>false, //debug: show complete incoming array? You can use this to see what other information Amazon is sending | |
125 'show_url' =>false, //debug: show XML request url to be send to Amazon? | |
126 'show_xml' =>false, //debug: show incoming XML code from Amazon? | |
127 ); | |
128 //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 | |
2 | 129 |
130 | |
36 | 131 //for all parameters see if the user has overruled it or use the default |
132 foreach ($Adefault as $i=>$d) { | |
133 $$i=isset($_GET[$i])?$_GET[$i]:$d; | |
134 } | |
135 | |
136 $parameters=array( | |
137 'Operation' =>$operation , | |
138 'Keywords' =>urlencode($search) , | |
139 "$searchparameter"=>$searchparameterdata , | |
140 'ItemPage' =>$page , //which page? | |
141 'AssociateTag' =>$Aassociates_id[$locale], | |
142 'ResponseGroup' =>'ItemAttributes,Images,SalesRank,BrowseNodes' | |
143 ); | |
144 | |
145 if ($searchindex!='Books') { | |
146 $parameters['SearchIndex']=$searchindex; | |
147 } | |
2 | 148 |
36 | 149 try { |
38 | 150 $xml=doAmazonRequest($Aserver[$locale]['ext'],$parameters,6,'d'); |
36 | 151 //use this xml to pull out the necessary information and save it |
152 set_error_handler(function () { | |
153 global $out,$isbn; | |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
154 dl("Caught one bd: $isbn\n"); |
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
155 dl(print_r($out, TRUE)."\n"); |
36 | 156 } ); |
44
38d209611508
change includes to assume (contra previos ASSUMES) that we have .user.ini which adds /var/test to include_path
Charlie Root
parents:
43
diff
changeset
|
157 include "private/db.php"; |
36 | 158 |
159 $title=""; | |
160 $author=""; | |
161 $binding=""; | |
162 $dewey=""; | |
163 $imageURL=""; | |
164 $salesRank=""; | |
165 $pubDate=""; | |
166 $publisher=""; | |
167 | |
168 $title = $xml->Items->Item->ItemAttributes->Title; | |
169 $author = $xml->Items->Item->ItemAttributes->Author; | |
170 $binding = $xml->Items->Item->ItemAttributes->Binding; | |
171 $dewey = $xml->Items->Item->ItemAttributes->DeweyDecimalNumber; | |
172 if($dewey == "") | |
173 $dewey = "null"; | |
174 $imageURL = $xml->Items->Item->MediumImage->URL; | |
175 $salesRank = $xml->Items->Item->SalesRank; | |
176 $pubDate = $xml->Items->Item->ItemAttributes->PublicationDate; | |
177 $publisher = $xml->Items->Item->ItemAttributes->Publisher; | |
178 restore_error_handler(); | |
179 if ($publisher and strlen($publisher)>30) { | |
180 $publisher=substr($publisher,0,30); | |
181 } | |
182 if ($author and strlen($author)>30) { | |
183 $author=substr($author,0,30); | |
184 } | |
185 if ($title and strlen($title)>100) { | |
186 $title=substr($title,0,100); | |
187 } | |
188 $publisher=mysqli_real_escape_string($link,$publisher); | |
189 $author=mysqli_real_escape_string($link,$author); | |
190 $title=mysqli_real_escape_string($link,$title); | |
191 if (strlen($pubDate)==4) { $pubDate=$pubDate."-01-01";} | |
192 if (strlen($pubDate)==7) { $pubDate=$pubDate."-01";} | |
193 if (strlen($pubDate)==0) { | |
194 $pubDate="null"; | |
195 } | |
196 else { | |
197 $pubDate="\"$pubDate\""; | |
198 } | |
199 /*echo $title; | |
200 echo $author; | |
201 echo $binding; | |
202 echo $dewey; | |
203 echo $imageURL; | |
204 echo $salesRank; | |
205 echo $pubDate; | |
206 echo $publisher;*/ | |
207 | |
208 $genreID = ""; | |
209 $genre = ""; | |
210 $genArr = array(); | |
211 $g1 = "null"; | |
212 $g2 = "null"; | |
213 $g3 = "null"; | |
214 if(isset($xml->Items->Item->BrowseNodes->BrowseNode)){ | |
215 for($i=0;$i<sizeof($xml->Items->Item->BrowseNodes->BrowseNode);$i++){ | |
216 //sexy recursive function | |
217 findGenre($xml->Items->Item->BrowseNodes->BrowseNode[$i], $genreID, $genre); | |
218 | |
219 if($genre != "") | |
220 $genArr[strval($genreID)] = strval($genre); | |
221 //$genArr[$i] = array(strval($genreID) => strval($genre)); | |
2 | 222 |
36 | 223 //echo $genre; |
224 //echo $genreID; | |
225 | |
226 $genre = ""; | |
227 $genreID = ""; | |
21
46382face560
use a semaphore to try to cut down on RequestThrottled fails
Charlie Root
parents:
18
diff
changeset
|
228 } |
36 | 229 |
230 $g1 = "null"; | |
231 $g2 = "null"; | |
232 $g3 = "null"; | |
233 $loop = 1; | |
234 | |
235 foreach ($genArr as $key => $value) { | |
236 //echo "$key => $value"; | |
237 if ($key>2047) { | |
238 //HST added | |
239 break; | |
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
6
diff
changeset
|
240 } |
36 | 241 $queryG = "CALL b_addBrowseNode($key,\"$value\")"; //add the name value pair for genre to new table |
242 //echo $queryG; | |
46 | 243 $resG = my_query($queryG,'gbd2'); |
36 | 244 |
245 switch ($loop) { | |
246 case 1: | |
247 $g1 = $key; | |
248 break; | |
249 case 2: | |
250 $g2 = $key; | |
251 break; | |
252 case 3: | |
253 $g3 = $key; | |
254 break; | |
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
6
diff
changeset
|
255 } |
36 | 256 |
257 $loop++; | |
9
232deb0b066a
tidy up debugging, only show parms on non-throttled error
Henry S. Thompson <ht@markup.co.uk>
parents:
6
diff
changeset
|
258 } |
36 | 259 } |
260 | |
261 if($salesRank == "") | |
262 $salesRank = "null"; | |
2 | 263 |
36 | 264 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
48 | 265 include "private/db.php"; |
2 | 266 |
36 | 267 $title = strtr($title, '"', "'"); |
268 $queryInsert = "CALL b_addNewBook(\"$isbn\",\"$title\", \"$author\",\"$binding\",\"$imageURL\", $dewey, $salesRank,$pubDate,\"$publisher\",$g1,$g2,$g3,$loc)"; | |
269 //echo $queryInsert; | |
2 | 270 |
46 | 271 $res = my_query($queryInsert,'gbd3'); |
36 | 272 mysqli_close($link); //do not remove. reset is needed otherwise mysqli_fetch_array doesn't work after first loop |
2 | 273 |
274 | |
36 | 275 $output .= "<ISBN>" . htmlspecialchars($isbn) . "</ISBN>"; |
276 $output .= "<Title>" . htmlspecialchars($title) . "</Title>"; | |
277 $output .= "<Author>" . htmlspecialchars($author) . "</Author>"; | |
278 $output .= "<Binding>" . htmlspecialchars($binding) . "</Binding>"; | |
279 $output .= "<Dewey>" . htmlspecialchars($dewey) . "</Dewey>"; | |
280 $output .= "<ImageURL>" . htmlspecialchars($imageURL) . "</ImageURL>"; | |
281 $output .= "<SalesRank>" . htmlspecialchars($salesRank) . "</SalesRank>"; | |
282 $output .= "<PublicationDate>" . htmlspecialchars($pubDate) . "</PublicationDate>"; | |
283 $output .= "<Publisher>" . htmlspecialchars($publisher) . "</Publisher>"; | |
284 $output .= "<Genre1>" . htmlspecialchars($g1) . "</Genre1>"; | |
285 $output .= "<Genre2>" . htmlspecialchars($g2) . "</Genre2>"; | |
286 $output .= "<Genre3>" . htmlspecialchars($g3) . "</Genre3>"; | |
287 $output .= "<ProductGroup>Book</ProductGroup>"; | |
288 } | |
289 catch (Exception $e) { | |
43
dbc006408d2b
ASSUMES we have SetEnv PHP_VALUE "include_path =.:/var/test/private:/var/test/ywww:/usr/share/php" in apache2/.../test.conf
Charlie Root
parents:
38
diff
changeset
|
290 dl("gBD: dAR failed:".$e->getMessage()."\n"); |
36 | 291 } |
292 } | |
2 | 293 $output .= "</BookDetails>"; |
294 | |
295 echo $output; | |
296 } | |
297 ?> |