view us/index.php @ 8:226f00d9f6f5

another error check
author Henry Thompson <ht@markup.co.uk>
date Fri, 17 Feb 2017 15:22:53 +0000
parents 1dfe64e365a0
children 385ddd7c4b55 84a412b8122c
line wrap: on
line source

<?PHP
session_start();
  
	function uniqueRand($n, $min = 0, $max = null)
	{
		if($max === null)
			$max = getrandmax();
		$array = range($min, $max);
		$return = array();
		$keys = array_rand($array, $n);
		foreach($keys as $key)
			$return[] = $array[$key];
		return $return;
	}
  
	include "../mdetect.php";
	$uagent_obj = new uagent_info();
	$iPhone = $uagent_obj->DetectSmartphone();
	$iPad = $uagent_obj->DetectIpad();
  
  	$proceed = true;
	$launchScript = "<script type=\"text/javascript\" language=\"javascript\" src=\"../books/books.nocache.js\"></script>";
  
	//print_r($_SERVER);
  	if (isset($_SERVER['HTTP_REFERER']) || isset($_GET['facebook']))
	{
		$pos = strpos($_SERVER['HTTP_REFERER'], "apps.facebook.com");
		if($pos !== false || isset($_GET['facebook']))
		{
			//we are in facebook canvas so react appropriately
			include_once '../facebook.php';

			$facebook = new Facebook(array(
				'appId'  => '128245333876633',
				'secret' => 'f51afc7e04289db62448edd8b70d83bf',
				'cookie' => true,
				'domain' => 'yournextpresent.com'
			));

			$session = $facebook->getSession();

			if (!$session) {
				$proceed = false;
				$url = $facebook->getLoginUrl(array(
						   'canvas' => 1,
						   'fbconnect' => 0,
						   'req_perms' => 'email,user_birthday,publish_stream'
					   ));

				echo "<script type='text/javascript'>top.location.href = '$url';</script>";
			} 
			else
			{
				$launchScript = "<script type=\"text/javascript\" language=\"javascript\" src=\"../booksFB/books.nocache.js\"></script>";
			}
		}
	}
	echo "<!DOCTYPE html><html>";
	if($proceed)
	{
	echo "<head>";
	echo "<meta name=\"fragment\" content=\"!\">";
	echo "<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\" />";
    echo "<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">";
	echo "<link type=\"text/css\" rel=\"stylesheet\" href=\"../Books.css\">";
	
		if(isset($_GET['_escaped_fragment_'])) {
			$isbn = $_GET['_escaped_fragment_'];			
			if($isbn == ""){
				echo "<META NAME=\"Description\" CONTENT=\"Discover and share your next book at YourNextRead. YourNextRead makes book discovery and recommendation simple. Great for both bookworms and casual readers!\">";

				echo "<title>YourNextRead: Book Recommendations (USA)</title>";
				echo "<div id=\"script\"></div><noscript>
				<p>You need to have Javascript enabled to use YourNextRead - Sorry!<p>
				<p>Please see instructions on how to switch this on, depending on which browser you use:<p>
				<ul>
				<li><a href=\"http://www.ehow.com/how_2033406_enable-javascript-firefox.html\">Firefox</a></li>
				<li><a href=\"http://www.chromefans.org/chrome-tutorial/how-to-enable-or-disable-javascript-in-google-chrome.htm\">Chrome</a></li>
				<li><a href=\"http://support.microsoft.com/gp/howtoscript\">Internet Explorer</a></li>
				</ul>
				<p>Thank you for using YourNextRead<p>
				<img src=\"http://www.yournextread.com/images/topleft.gif\" alt=\"YourNextRead\"/>
				</noscript>";
				if($iPhone && !$iPad)
				{
					echo "<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\" />";
					echo "<script type=\"text/javascript\" language=\"javascript\" src=\"../booksMob/books.nocache.js\"></script>";
				}
				else
				{
					echo "<script type=\"text/javascript\" language=\"javascript\" src=\"../raphael-min.js\"></script>";
					echo $launchScript;//"<script type=\"text/javascript\" language=\"javascript\" src=\"../books/books.nocache.js\"></script>";
				}
				echo "</head>";
				echo "<body>";
				//we are at the launch page
				echo "<p>Get started, search for a book you like... or choose from one of the lists below:</p><BR></BR>";
				
				$ret = 1;													
								
				$bookLists = include "../php/read/getAllSavedBooks.php"; 			
				$xml = new SimpleXMLElement($bookLists);
				
				$numOfLists = sizeof($xml->List) - 1;	//-1 for zero indexing 
					
				$randomFourLists = uniqueRand(4,0,$numOfLists);				
				
				foreach ($randomFourLists as $id) {
					$ListName = $xml->List[$id]->SavedListName;
					echo "<p><b>$ListName</b></p><BR></BR>";
					for($i=0;$i<sizeof($xml->List[$id]->ListDetails->Book);$i++){
						$bookTitle = $xml->List[$id]->ListDetails->Book[$i]->BookTitle;
						$isbn = $xml->List[$id]->ListDetails->Book[$i]->ISBN;
						echo "<a href=http://www.YourNextRead.com/us/#!isbn=$isbn>$bookTitle</a> ";
					}
				}		
			}
			else
			{
				try{
				$isbn = substr($isbn, 5); 
				$chars = explode("/",$isbn);
				$isbn = $chars[0];
				$ret = 1;
				$countryCode = 0;
				
				include "../../private/db.php"; 
				$query = "CALL b_getBookInfo('$isbn', $countryCode)";
				//echo $query;
				$res = mysqli_query($link, $query) or exit( mysqli_error( $link ));
				include "../xml/aws_signed_request.php";
				include "../xml/getAmazonInfo.php";
				if ( mysqli_num_rows( $res ) > 0 )
				{
					$rows=mysqli_fetch_array($res, MYSQLI_ASSOC);		
					$title = $rows["Title"];
					$author = $rows["Author"];
				}		
				else
				{
					$bookDetails = getAmazonDet($isbn,1,'us'); 
					$bookXML = new SimpleXMLElement($bookDetails);
					// HST added this
					if (preg_match("<Error>",$bookDetails)) {
					  $resName=$bookXML->getName();
					  $code=$bookXML->Error->Code;
					  file_put_contents('/var/ywww/debug/phpDebug',"Losing 3: $resName, ".
							    $code."\n",FILE_APPEND);
					  if ($code=='RequestThrottled') {
					    usleep(200000); // Try to reduce throttling until we get a 
					    // principled solution in place
					  }
					  $title = "";
					}
					else {
					  $title = $bookXML->Items->Item[0]->ItemAttributes->Title;
					  $author = $bookXML->Items->Item[0]->ItemAttributes->Author;
					}
				}

				$linkedBooks = include "../php/read/getLinkedBooks.php"; 
				$xml = new SimpleXMLElement($linkedBooks);
				
				$recommended = "";
				$linkedRecommended = "";
				for($i=0;$i<sizeof($xml->isbn);$i++){
				  $linkedISBN = $xml->isbn[$i];
				  include "../../private/db.php"; 
				  $query = "CALL b_getBookInfo('$linkedISBN', $countryCode)";
				  //echo $query;
				  $res = mysqli_query($link, $query) or exit( mysqli_error( $link ));
				  set_error_handler(function () {
				      global $bookLDetails, $linkedISBN;
				      file_put_contents('/var/ywww/debug/phpDebug',
							"Caught other one?: ".$linkedISBN,
							FILE_APPEND);
				      //file_put_contents('/var/ywww/debug/phpDebug',
				      //		print_r($bookLDetails, TRUE)."\n",
				      //		FILE_APPEND);
				    } );
				  if ( mysqli_num_rows( $res ) > 0 )
				    {
				      $rows=mysqli_fetch_array($res, MYSQLI_ASSOC);		
				      $titleL = $rows["Title"];
				      $authorL = $rows["Author"];
				      mysqli_close($link);
				    }		
				  else
				    {					
				      $bookLDetails = getAmazonDet($linkedISBN,1,'us'); 
				      $bookLXML = new SimpleXMLElement($bookLDetails);
				      // HST added this
				      if (preg_match("<Error>",$bookLDetails)) {
					$resName=$bookLXML->getName();
					$code=$bookLXML->Error->Code;
					file_put_contents('/var/ywww/debug/phpDebug',"Losing 2: $resName, ".
							  $code."\n",FILE_APPEND);
					if ($code=='RequestThrottled') {
					  usleep(200000); // Try to reduce throttling until we get a 
					  // principled solution in place
					}
					$titleL = "";
				      }
				      else {
					$titleL = $bookLXML->Items->Item[0]->ItemAttributes->Title;
					$authorL = $bookLXML->Items->Item[0]->ItemAttributes->Author;
				      }
				    }
				  restore_error_handler();
				  if($titleL != "")
				    {
				      $recommended .= " $titleL by $authorL,";
				      $underAuthor = str_replace(" ","_",$authorL);
				      $underTitle = str_replace(" ","_",$titleL);
				      $linkedRecommended .= "<a href=\"http://www.YourNextRead.com/us/#!isbn=$linkedISBN/$underTitle" . "_by_$authorL\">$titleL by $authorL</a>, ";		
				    }					
				}
				
				$vote = "Vote on these recommendations!";
				
				echo "<META NAME=\"Description\" CONTENT=\"YourNextRead recommended books for $title by $author:$recommended $vote\">";

				echo "<title>YourNextRead (USA) Recommended Books for $title by $author</title>";
				echo "<div id=\"script\"></div><noscript>
				<p>You need to have Javascript enabled to use YourNextRead - Sorry!<p>
				<p>Please see instructions on how to switch this on, depending on which browser you use:<p>
				<ul>
				<li><a href=\"http://www.ehow.com/how_2033406_enable-javascript-firefox.html\">Firefox</a></li>
				<li><a href=\"http://www.chromefans.org/chrome-tutorial/how-to-enable-or-disable-javascript-in-google-chrome.htm\">Chrome</a></li>
				<li><a href=\"http://support.microsoft.com/gp/howtoscript\">Internet Explorer</a></li>
				</ul>
				<p>Thank you for using YourNextRead<p>
				<img src=\"http://www.yournextread.com/images/topleft.gif\" alt=\"YourNextRead\"/>
				</noscript>";
				if($iPhone && !$iPad)
				{
					echo "<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\" />";
					echo "<script type=\"text/javascript\" language=\"javascript\" src=\"../booksMob/books.nocache.js\"></script>";
				}
				else
				{
					echo "<script type=\"text/javascript\" language=\"javascript\" src=\"../raphael-min.js\"></script>";
					echo $launchScript;//"<script type=\"text/javascript\" language=\"javascript\" src=\"../books/books.nocache.js\"></script>";
				}
				
				echo "</head>";
				
				echo "<body>";				
				echo "<p>Recommended Books for <b>$title</b> by <b>$author</b>:";
				echo $linkedRecommended . "</p>";
				echo "<p>" . $vote . "</p>";
				
				echo "Change to <a href=\"http://www.YourNextRead.com/de/#!isbn=$isbn/$title" . "_by_$author\">YourNextRead Germany</a>, ";
				echo "Change to <a href=\"http://www.YourNextRead.com/ca/#!isbn=$isbn/$title" . "_by_$author\">YourNextRead Canada</a>, ";
				echo "Change to <a href=\"http://www.YourNextRead.com/fr/#!isbn=$isbn/$title" . "_by_$author\">YourNextRead France</a>, ";				
				echo "Change to <a href=\"http://www.YourNextRead.com/us/#!isbn=$isbn/$title" . "_by_$author\">YourNextRead USA</a>";
				echo ", See this on <a href=\"http://www.YourNextPresent.com/us/#!isbn=$isbn\">YourNextPresent</a>, ";
				echo "Switch to <a href=\"http://www.YourNextFilm.com/us/\">YourNextFilm</a>, ";
				echo "Switch to <a href=\"http://www.YourNextGame.com/us/\">YourNextGame</a>";
				}
				catch(Exception $e)
				{
				  echo 'Message: ' .$e->getMessage();
				}
			}
		}
		else
		{
			echo "<META NAME=\"Description\" CONTENT=\"YourNextRead recommends your next book. YourNextRead provides a book recommendation system showing aggregated book reviews, updated by real peoples opinions, in a simple visual map, helping you to decide 'What Should I Read Next?'. Perfect for both bookworms and casual readers!\">";

			echo "<title>YourNextRead: Book Recommendations (USA)</title>";
			//the title will get changed dynamically in the GWT code
			echo "<div id=\"script\"></div><noscript>
			<p>You need to have Javascript enabled to use YourNextRead - Sorry!<p>
			<p>Please see instructions on how to switch this on, depending on which browser you use:<p>
			<ul>
			<li><a href=\"http://www.ehow.com/how_2033406_enable-javascript-firefox.html\">Firefox</a></li>
			<li><a href=\"http://www.chromefans.org/chrome-tutorial/how-to-enable-or-disable-javascript-in-google-chrome.htm\">Chrome</a></li>
			<li><a href=\"http://support.microsoft.com/gp/howtoscript\">Internet Explorer</a></li>
			</ul>
			<p>Thank you for using YourNextRead<p>
			<img src=\"http://www.yournextread.com/images/topleft.gif\" alt=\"YourNextRead\"/>
			</noscript>";
			if($iPhone && !$iPad)
			{
				echo "<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\" />";
				echo "<script type=\"text/javascript\" language=\"javascript\" src=\"../booksMob/books.nocache.js\"></script>";
			}
			else
			{
				echo "<script type=\"text/javascript\" language=\"javascript\" src=\"../raphael-min.js\"></script>";
				echo $launchScript;//"<script type=\"text/javascript\" language=\"javascript\" src=\"../books/books.nocache.js\"></script>";
			}
			
			echo "</head>";

			echo "<body>";
			echo "<div id=\"fb-root\"></div>
			<script type=\"text/javascript\" src=\"http://connect.facebook.net/en_US/all.js\"></script>
			 <script type=\"text/javascript\">
			   FB.init({
				 appId  : '128245333876633',
				 status : true, // check login status
				 cookie : true, // enable cookies to allow the server to access the session
				 xfbml  : true  // parse XFBML
			   });
			 </script>";
			echo "<script type=\"text/javascript\">
			var obj =   new Object;
			obj.height=960;
			FB.Canvas.setSize(obj);
			</script>";
		}
	}
	?>
  
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe>
	
	<script type="text/javascript">
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	</script>
	<script type="text/javascript">
	try {
	var pageTracker = _gat._getTracker("UA-15026249-1");
	pageTracker._setDomainName("none");
	pageTracker._setAllowLinker(true);
	pageTracker._trackPageview();
	} catch(err) {}</script>
  </body>
</html>