fork download
  1. <?
  2. /* ############################################# */
  3. # #
  4. # Because Water - Drop In App API #
  5. # Designed and Developed at Boston University #
  6. # #
  7. /* ############################################# */
  8.  
  9. /* API KEY AUTHENTICATION */
  10.  
  11. $apiKey = '7yEgyaKqGTEyX7wqtGHwX';
  12.  
  13. if (stripcslashes($_GET['api']) != $apiKey) { # Make sure requester is using a valid API Key
  14. die(cleanupJson(json_encode(array('status' => 'false', 'error' => 'Invalid API Key'))));
  15. }
  16.  
  17. /* ACTIONS */
  18.  
  19. $action = stripslashes($_GET['action']);
  20.  
  21. if ($action == 'get') {
  22. $radius = stripslashes($_GET['radius']);
  23. $lat = stripslashes($_GET['lat']);
  24. $lng = stripslashes($_GET['lng']);
  25.  
  26. if ($lat == null or $lng == null) {
  27. die(cleanupJson(json_encode(array('status' => 'false', 'error' => 'Invalid parameters'))));
  28. }
  29.  
  30. getData($radius, $lat, $lng);
  31.  
  32. } else if ($action == 'push') {
  33. $name = stripslashes($_GET['name']);
  34. $address = stripslashes($_GET['address']);
  35. $category = stripslashes($_GET['category']);
  36. $lat = stripslashes($_GET['lat']);
  37. $lng = stripslashes($_GET['lng']);
  38. $details = stripslashes($_GET['details']);
  39. $locationName= stripslashes($_GET['locationName']);
  40. $personName = stripslashes($_GET['personName']);
  41.  
  42. if ($name == null or $address == null or $category == null or $lat == null or $lng == null) {
  43. die(cleanupJson(json_encode(array('status' => 'false', 'error' => 'Invalid parameters'))));
  44. }
  45.  
  46. pushData($name, $address, $category, $lat, $lng, $details,$locationName,$personName );
  47.  
  48. } else {
  49. die(cleanupJson(json_encode(array('status' => 'false', 'error' => 'Invalid request'))));
  50. }
  51.  
  52. /* FUNCTIONS */
  53.  
  54. function getData($radius, $lat, $lng) {
  55. # Retrieves drop data from the database
  56. # Inputs:
  57. # $radius: Radius to show
  58. # $lat: User's latitude
  59. # $lng: User's longitude
  60.  
  61. if ($radius == null) { # If radius was not specified
  62. $radius = 5;
  63. }
  64.  
  65. $con = mysqli_connect("localhost","becausf5","BlueRush.993","becausf5_DropIn_Geolocation") or die("Error " . mysqli_error($con)); # Connect to database
  66.  
  67. // Updated Lat/Lng Formula Code from Easement: http://stackoverflow.com/questions/2296824/php-mysql-compare-long-and-lat-return-ones-under-10-miles
  68. $sql = mysqli_query($con, "select *, ((acos(sin($lat * pi() / 180) * sin(lat * pi() / 180) + cos($lat * pi() / 180) * cos(lat * pi() / 180) * cos(($lng - lng) * pi() / 180)) * 180 / pi()) * 60 * 1.1515) AS distance from markers1 where visible = 1 having distance < $radius order by distance limit 30") or die(mysqli_error($con)); # Select rows
  69.  
  70. $data = array();
  71. while($row = mysqli_fetch_assoc($sql)) { # Loop through and add each row to array
  72. $data['success'] = 'true';
  73. $data[] = array_map('htmlentities',$row);
  74. }
  75.  
  76. if ($data == null) { # If SQL returned null
  77. $data = array('success' => 'false','error' => 'There are no drops within the given radius');
  78. }
  79.  
  80. $json = json_encode($data); # Encode array as json
  81. echo cleanupJson($json); # Echo json
  82. }
  83.  
  84.  
  85.  
  86. function pushData($name, $address, $category, $lat, $lng, $details, $locationName,$personName) {
  87. # Adds new drop data to the database
  88. # Inputs:
  89. # $name: Friendly name of drop location
  90. # $address: Street address of drop location
  91. # $category: Type of drop (Public Bubbler, Concord on Tap, etc.)
  92. # $lat: Latitude coordinate of drop location
  93. # $lng: Longitude coordinate of drop location
  94. # $details: Description of drop
  95.  
  96. /* Still need to implement: Check for duplicates */
  97.  
  98. $categoryArray = array('0' => 'Concord on Tap', '1' => 'Drop In', '2' => 'Public Fountain', '3' => 'User Submitted');
  99.  
  100. $con = mysqli_connect("localhost","becausf5","BlueRush.993","becausf5_DropIn_Geolocation") or die("Error " . mysqli_error($con)); # Connect to database
  101. $sql = mysqli_query($con, "insert into markers1 (name, address, category, lat, lng, details, visible, locationName, personName)
  102. values ('{$name}', '{$address}', '{$categoryArray[{$category}]}', '{$lat}', '{$lng}', '{$details}', '1','{$locationName}','{$personName}')") or die(mysqli_error($con)); # Insert rows
  103. if ($sql) { # If insert was successful
  104. echo json_encode(array('success' => 'true'));
  105. } else { # If insert was NOT successful
  106. echo json_encode(array('success' => 'false'));
  107. }
  108. }
  109.  
  110.  
  111.  
  112. function cleanupJson($json) {
  113. # Cleans up json by indenting and formatting the json into a more readable format
  114. # Inputs:
  115. # $json: A json array
  116.  
  117. if (stripslashes($_GET['readable']) == 'true') {
  118.  
  119. // Code from Edi Budimilic: http://stackoverflow.com/questions/6672656/how-can-i-beautify-json-programmatically
  120. $json = str_replace('\r', ' ', $json);
  121. return str_replace(array("{", "}", '","'), array("{<br />&nbsp;&nbsp;&nbsp;&nbsp;", "<br />}", '",<br />&nbsp;&nbsp;&nbsp;&nbsp;"'), $json);
  122.  
  123. } else {
  124. $json = str_replace('\r', ' ', $json);
  125. return $json;
  126. }
  127. }
  128. ?>
Success #stdin #stdout 0.03s 25908KB
stdin
Standard input is empty
stdout
<?
	/* ############################################# */
	#                                                 #
	#	Because Water - Drop In App API               #
	#   Designed and Developed at Boston University   #
	#                                                 #
	/* ############################################# */
	
	/* API KEY AUTHENTICATION */
	
	$apiKey = '7yEgyaKqGTEyX7wqtGHwX';
	
	if (stripcslashes($_GET['api']) != $apiKey) { # Make sure requester is using a valid API Key
		die(cleanupJson(json_encode(array('status' => 'false', 'error' => 'Invalid API Key'))));
	}

	/* ACTIONS */
		
	$action = stripslashes($_GET['action']);
	
	if ($action == 'get') {
		$radius = stripslashes($_GET['radius']);
		$lat = stripslashes($_GET['lat']);
		$lng = stripslashes($_GET['lng']);
		
		if ($lat == null or $lng == null) {
			die(cleanupJson(json_encode(array('status' => 'false', 'error' => 'Invalid parameters'))));	
		}
		
		getData($radius, $lat, $lng);
		
	} else if ($action == 'push') {		
		$name = stripslashes($_GET['name']);
		$address = stripslashes($_GET['address']);
		$category = stripslashes($_GET['category']);
		$lat = stripslashes($_GET['lat']);
		$lng = stripslashes($_GET['lng']);
		$details = stripslashes($_GET['details']);
	 	$locationName= stripslashes($_GET['locationName']);
      		$personName = stripslashes($_GET['personName']);
		
		if ($name == null or $address == null or $category == null or $lat == null or $lng == null) {
			die(cleanupJson(json_encode(array('status' => 'false', 'error' => 'Invalid parameters'))));	
		}
		
		pushData($name, $address, $category, $lat, $lng, $details,$locationName,$personName );
		
	} else {
		die(cleanupJson(json_encode(array('status' => 'false', 'error' => 'Invalid request'))));
	}	

	/* FUNCTIONS */
		
	function getData($radius, $lat, $lng) {
		# Retrieves drop data from the database
		# Inputs:
		#	$radius: Radius to show
		#	$lat: User's latitude
		#	$lng: User's longitude
		
		if ($radius == null) { # If radius was not specified
			$radius = 5;
		}
		
		$con = mysqli_connect("localhost","becausf5","BlueRush.993","becausf5_DropIn_Geolocation") or die("Error " . mysqli_error($con)); # Connect to database
		
		// Updated Lat/Lng Formula Code from Easement: http://stackoverflow.com/questions/2296824/php-mysql-compare-long-and-lat-return-ones-under-10-miles
		$sql = mysqli_query($con, "select *, ((acos(sin($lat * pi() / 180) * sin(lat * pi() / 180) + cos($lat * pi() / 180) * cos(lat * pi() / 180) * cos(($lng - lng) * pi() / 180)) * 180 / pi()) * 60 * 1.1515) AS distance from markers1 where visible = 1 having distance < $radius order by distance limit 30") or die(mysqli_error($con)); # Select rows
					
		$data = array();
		while($row = mysqli_fetch_assoc($sql)) { # Loop through and add each row to array
			$data['success'] = 'true';
			$data[] = array_map('htmlentities',$row);
		}
			
		if ($data == null) { # If SQL returned null
			$data = array('success' => 'false','error' => 'There are no drops within the given radius');
		}		 
		
		$json = json_encode($data); # Encode array as json
		echo cleanupJson($json); # Echo json
	}
	
	
	
	function pushData($name, $address, $category, $lat, $lng, $details, $locationName,$personName) {
		# Adds new drop data to the database
		# Inputs:
		#	$name: Friendly name of drop location
		#	$address: Street address of drop location
		#	$category: Type of drop (Public Bubbler, Concord on Tap, etc.)
		#	$lat: Latitude coordinate of drop location
		#	$lng: Longitude coordinate of drop location
		#	$details: Description of drop
		
		/* Still need to implement: Check for duplicates */
		
		$categoryArray = array('0' => 'Concord on Tap', '1' => 'Drop In', '2' => 'Public Fountain', '3' => 'User Submitted');
		
		$con = mysqli_connect("localhost","becausf5","BlueRush.993","becausf5_DropIn_Geolocation") or die("Error " . mysqli_error($con)); # Connect to database
		$sql = mysqli_query($con, "insert into markers1 (name, address, category, lat, lng, details, visible, locationName, personName) 
		values ('{$name}', '{$address}', '{$categoryArray[{$category}]}', '{$lat}', '{$lng}', '{$details}', '1','{$locationName}','{$personName}')") or die(mysqli_error($con)); # Insert rows
		if ($sql) { # If insert was successful
			echo json_encode(array('success' => 'true'));
		} else { # If insert was NOT successful
			echo json_encode(array('success' => 'false'));
		}
	}
	
	
	
	function cleanupJson($json) {
		# Cleans up json by indenting and formatting the json into a more readable format
		# Inputs:
		#	$json: A json array
		
		if (stripslashes($_GET['readable']) == 'true') {
		
			// Code from Edi Budimilic: http://stackoverflow.com/questions/6672656/how-can-i-beautify-json-programmatically
			$json = str_replace('\r', ' ', $json);
			return str_replace(array("{", "}", '","'), array("{<br />&nbsp;&nbsp;&nbsp;&nbsp;", "<br />}", '",<br />&nbsp;&nbsp;&nbsp;&nbsp;"'), $json);
			
		} else {
			$json = str_replace('\r', ' ', $json);
			return $json;
		}
	}
?>