<?php
	// 
	// index.php
	//
	// This is the source code for the KingOfTheHill application.  
	// All the application logic is on this page.
	//
	// This code can be freely copied, modified, and distributed.  
	//    

	// If someone wants to view the source, then dump it out.
	if( isset( $_GET["source"] ) ) {
		echo "<pre>";
		echo htmlentities( file_get_contents( "index.php") );								
		echo "</pre>";
		exit;
	}

	// $strRoot will be the URL to this page
	$strPath = str_replace( "\\", "/", $_SERVER['REQUEST_URI'] );
	$aPath = explode( "?", $strPath);
	$strRoot = "http://" . $_SERVER['SERVER_NAME'] . ":" . $_SERVER['SERVER_PORT'] . $aPath[0];
	if ($strRoot[strlen($strRoot)-1] != "/")
		$strRoot .= "/";

	// $strPicnikUrl is the URL that we use to launch Picnik.
	$strPicnikUrl = "http://www.picnik.com/service";
	
	// You can get your own API key at http://www.picnik.com/info/api
	$apikey = "a77f917f0058eb066a87af4d8a540960";
	
	// $aPicnikParams collects together all the params we'll give Picnik.  Start with an API key
	$aPicnikParams['_apikey'] = $apikey;
	
	// tell Picnik where to send the exported image
	$aPicnikParams['_export'] = $strRoot;
	
	// give the export button a title
	$aPicnikParams['_export_title'] = "King Me!";
	
	// turn on the close button, and tell it to come back here
	$aPicnikParams['_close_target'] = $strRoot;
	
	// send in the previous "king" image in case the user feels like decorating it
	$aPicnikParams['_import'] = $strRoot . "img/king.jpg";
	
	// tell Picnik to redirect the user to the following URL after the HTTP POST instead of just redirecting to _export
	$aPicnikParams['_redirect'] = $strRoot . "?coronation";
	
	// tell Picnik our name.  It'll use it in a few places as appropriate
	$aPicnikParams['_host_name'] = "King Of The Hill";
	
	// turn off the "Save & Share" tab so users don't get confused
	$aPicnikParams['_exclude'] = "out";
	
	// See if we've been given a new picture to use as the king. 
	// Note that the when Picnik is exporting from its servers, this page will be hit TWICE.  
	// Once will be the POST with the image data contained in $_FILES.  The second will be
	// a GET of the _redirect URL we passed in above.
	if (isset($_FILES['file'])) {
		// retrieve the image's attributes from the $_FILES array
		$image_tmp_filename = $_FILES['file']['tmp_name'];	
		$image_data = file_get_contents( $image_tmp_filename );
		file_put_contents( "img/king.jpg", $image_data );	
	}
	if (isset($_POST['address'])) {
		$address = htmlentities( $_POST['address'] );
		file_put_contents( "img/address.txt", $address );
	} else {
		// read in the "address.txt" file so that we know what to call the King.
		// This data was posted to us from the Picnik server on a previous call.
		$address = @file_get_contents( "img/address.txt" );
	}

	// When you're debugging this kind of application, keep in mind that Picnik will
	// invoke your script twice: once with the POST'd data, and then again with
	// a GET to the value of the _redirect parameter.  To see what happens
	// on the first (POST) call, you can use something like the below to dump 
	// variables to a debug file.
	//$debug = "";
	//$debug .= "\n\nFILES: " . print_r($_FILES, true);
	//$debug .= "\n\nPOST: " . print_r($_POST, true);
	//$debug .= "\n\nGET: " . print_r($_GET, true);
	//file_put_contents( "img/debug." . time() . ".txt", $debug);

?>
<html>
<head>
	<title>Picnik Sample: King Of The Hill</title>
</head>
<body>
<h2>Welcome to King Of The Hill!</h2>
<p>King Of The Hill is an easy game to play.  Just use Picnik to upload a photo, and <b>you win!</b></p>
<p><font size="-1">(King Of The Hill is a Picnik API sample that demonstrates how to receive an image from Picnik's servers via HTTP POST.  You can <a href="index.php?source">view the source code</a> for this page.</font>)</p>
<?php 
	if( isset( $_POST["coronation"] ) ) {
		echo "<hr/>";
		
		echo "<h3>You're the new King!  Congratulations, " . $_POST["coronation"] . "!<h3>";
	} else {
		// echo a form so that new Kings can be crowned
		echo "<hr/>";
		echo "<h3>Become the new King!</h3>";
		echo	"<p>Just tell us how you would like to be addressed at your coronation:</p>\n";
		echo "<form method='POST' action='$strPicnikUrl'>\n";
		
		// put all the API parameters into the form as hidden inputs
		foreach( $aPicnikParams as $key => $value ) {
			echo "<input type='hidden' name='$key' value='$value'/>\n";
		}
		
		// anything that doesn't start with an underscore will be sent back to us.  
		// We'll use the "coronation" value to determine when we've got a new King.
		echo "<input type='text' name='address' value='Your Majesty'/>\n";
		echo "<input type='submit' value='Crown Me!'/>\n";
		echo "</form>";
	}
?>
<hr/>
<h2>Long Live The King!</h2>
<img src="img/king.jpg?<?php echo microtime(true); /* cache busting */ ?>" border="0">
<p>The King would like to be addressed as <b>"<?php echo $address ?>"</b>.</p>
</body>
</html>