PageRenderTime 31ms CodeModel.GetById 45ms RepoModel.GetById 1ms app.codeStats 0ms

/php-scripts/google_map_functions.php

https://github.com/wesleyzhao/wheremyfriendsbe
PHP | 307 lines | 269 code | 20 blank | 18 comment | 19 complexity | 2aecfe0373f7a2752da4eb8ae1d9379c MD5 | raw file
  1. <?php
  2. require_once("facebook_config.php");
  3. require_once("mysql_connect.php");
  4. require_once("memcache_functions.php");
  5. $oauth_id = $_GET['oauth_id'];
  6. $oauth_arr = explode(",",$oauth_id);
  7. $access_token = $_GET['access_token'];
  8. $access_token = mysql_escape_string($access_token);
  9. $user_oauth_id = $_GET['user_oauth_id'];
  10. $user_oauth_id = mysql_escape_string($user_oauth_id);
  11. $is_sql_friends = intval($_GET['is_sql_friends']);
  12. echo makeAjax($oauth_arr,$access_token,$is_sql_friends);
  13. //check users that are facebook or not right away
  14. function getLatLng($addressStr){
  15. //inputs a formatted text address string
  16. //returns an array with 'lat' and 'lng' keys corresponding to lattitude and longitude
  17. $url = "http://maps.googleapis.com/maps/api/geocode/json?address=".$addressStr."&sensor=false";
  18. return checkLatLng($addressStr);
  19. }
  20. function checkLatLng($formattedAddress){
  21. mysqlConnect();
  22. $formattedAddress = mysql_escape_string($formattedAddress);
  23. //$res = mysql_query("SELECT `lat`,`lng` FROM `geocodes` WHERE `location`='$formattedAddress'");
  24. $res = mysql_query_cache("SELECT `lat`,`lng` FROM `geocodes` WHERE `location`='$formattedAddress'");
  25. //if (mysql_num_rows($res)){
  26. if (count($res)>0){
  27. //if the location currently exists
  28. $row = $res[0];
  29. $location = array();
  30. $location['lat'] = $row['lat'];
  31. $location['lng'] = $row['lng'];
  32. return $location;
  33. }
  34. else{
  35. $url = "http://trailertrack.me/redirect.php?address=".$formattedAddress;
  36. $data = file_get_contents($url);
  37. $jsondata = json_decode($data,true);
  38. if ($jsondata['status']=="OVERY_QUERY_LIMIT") {
  39. $url = "http://dilemmaroulette.com/redirect.php?address=".$formattedAddress;
  40. $data = file_get_contents($url);
  41. $jsondata = json_decode($data,true);
  42. if ($jsondata['status']=="OVERY_QUERY_LIMIT") {
  43. mail("dshipper@gmail.com","Retard","Fix it");
  44. mail("ajayumehta@gmail.com","Retard","Fix this");
  45. mail("wesley.zhao@gmail.com","Retard","Fix it");
  46. }
  47. }
  48. $location = $jsondata['results'][0]['geometry']['location'];
  49. mysql_query("INSERT INTO geocodes (location,lat,lng) VALUES('$formattedAddress','{$location['lat']}','{$location['lng']}')");
  50. return $location;
  51. }
  52. }
  53. function makeLatLng($location,$varName = "latlng"){
  54. //inputs $location array with 'lat' and 'lng' variables
  55. //@param varName is the name it is
  56. $lat = $location['lat'];
  57. $lng = $location['lng'];
  58. return "var $varName = new google.maps.LatLng($lat,$lng);\n";
  59. }
  60. function placeMarker($LatlngObj, $markerName, $index="1"){
  61. $script = "var $markerName = new google.maps.Marker({ position: $LatlngObj,zIndex: $index}); \n markers.push($markerName);\n";
  62. return $script;
  63. }
  64. function placePath($LatlngObjHome, $LatlngObjPlace, $pathName){
  65. $script = "var flightPlanCoordinates = [$LatlngObjHome,$LatlngObjPlace];\n
  66. var $pathName = new google.maps.Polyline({
  67. path: flightPlanCoordinates,
  68. strokeColor: '#FF0000',
  69. strokeOpacity: 1.0,
  70. strokeWeight: 2
  71. });\n markers.push($pathName);\n";
  72. return $script;
  73. }
  74. function makeStringLocation($string){
  75. //inputs facebook string, converts to google url api string
  76. $arr1 = explode(', ',$string);
  77. $string = makeGoogleString($arr1);
  78. return $string;
  79. }
  80. function addPlus($arr){
  81. //must have 2 indexs 0,1
  82. $arr[0] = str_replace(' ','+',$arr[0]);
  83. $arr[1] = str_replace(' ','+',$arr[1]);
  84. return $arr;
  85. }
  86. function makeLocation($arr){
  87. $string = implode(",",$arr);
  88. return $string;
  89. }
  90. function makeGoogleString($arr){
  91. $arr = addPlus($arr);
  92. return makeLocation($arr);
  93. }
  94. function completeMarker($fb_location,$latLngObj,$markerObj,$latLngObjHome){
  95. //inputs a facebook string location
  96. //inputs the string name of the Latlng obj
  97. //inputs the string name of the Marker obj
  98. //returns scripts to place marker
  99. $location = makeStringLocation($fb_location);
  100. $arr = getLatLng($location);
  101. $makeLatlng = makeLatLng($arr,$latLngObj);
  102. $placer = placeMarker($latLngObj,$markerObj);
  103. $pather = placePath($latLngObjHome,$latLngObj,"path".$markerObj."");
  104. $script = $makeLatlng.$placer.$pather;
  105. return $script;
  106. }
  107. function makeAjax($oauth_arr,$token,$is_sql_friends=false){
  108. //gets an array of oauth_ids and returns the lat/lng/name followed by a comma
  109. global $facebook,$user_oauth_id;
  110. $returnString = "";
  111. if ($is_sql_friends) {
  112. $friends_arr = getFriendLocationsSql($oauth_arr);
  113. foreach ($friends_arr as $friend_id => $friend_arr){
  114. $lat = $friend_arr['lat'];
  115. $lng = $friend_arr['lng'];
  116. if (($lat != "") && ($lng != "")){
  117. if ($returnString == "") $comma = "";
  118. else $comma = ';';
  119. $returnString =$returnString.$comma.$arr['lat']."/".$arr['lng']."/{$friend_arr['name']}/{$friend_arr['location']}";
  120. $returnString =$returnString.$comma.$lat."/".$lng."/{$friend_arr['name']}/{$friend_arr['location']}";
  121. createFriendship($user_oauth_id,$friend_id);
  122. }
  123. }
  124. }
  125. else{
  126. $friends_arr = getFriendLocations($oauth_arr,$token);
  127. foreach ($friends_arr as $friend_id=>$friend_arr){
  128. try{
  129. if ($friend_arr['location']){
  130. $location = makeStringLocation($friend_arr['location']);
  131. $arr = getLatLng($location);
  132. if ($arr['lat'] && $arr['lng']){
  133. //if the lat and lng are properly received
  134. if ($returnString == "") $comma = "";
  135. else $comma = ';';
  136. $returnString =$returnString.$comma.$arr['lat']."/".$arr['lng']."/{$friend_arr['name']}/{$friend_arr['location']}";
  137. addUser($friend_arr['name'],$friend_id,$friend_arr['location'],$arr['lat'],$arr['lng']);
  138. createFriendship($user_oauth_id,$friend_id);
  139. }
  140. }
  141. else{
  142. //if the friend location doesnt exist, continue loop
  143. }
  144. }
  145. catch (Exception $e){
  146. return "error";
  147. }
  148. }
  149. }
  150. return $returnString;
  151. }
  152. function getFriendLocations($friends_ids,$token=""){
  153. //takes in an oauth_array of ids, returns an array with 'name' key and 'location' value
  154. global $facebook;
  155. $query = "SELECT uid,name,current_location FROM user WHERE uid='";
  156. $friends_arr = array();
  157. mysqlConnect();
  158. $has_query_changed = true;
  159. /*
  160. foreach ($friends_ids as $id){
  161. $query = $query." uid='$id' OR";
  162. $has_query_changed = true;
  163. }
  164. */
  165. if (count($friends_ids)==1){
  166. $query = $query."{$friends_ids[0]}'";
  167. }
  168. else if (count($friends_ids)>1){
  169. $query_part = implode("' OR uid='",$friends_ids);
  170. $query = $query.$query_part."'";
  171. }
  172. //print substr($query,0,(strlen($query)-3));
  173. if ($has_query_changed)
  174. {
  175. //$query = substr($query,0,(strlen($query)-3));
  176. $params = array(
  177. 'method' => 'fql.query',
  178. 'query' => $query,
  179. 'access_token' =>$token
  180. );
  181. //Run Query
  182. $result = $facebook->api($params);
  183. foreach ($result as $person){
  184. $friends_arr[$person['uid']]=array('location'=>$person['current_location']['name'],'name'=>$person['name']);
  185. }
  186. }
  187. return $friends_arr;
  188. }
  189. function addUser($name,$oauth_id,$location,$lat,$lng,$token=""){
  190. //adds users to database
  191. //if user already existed, return false
  192. //if user was added successfully, return true
  193. global $user_oauth_id;
  194. mysqlConnect();
  195. $name = mysql_escape_string($name);
  196. $oauth_id = mysql_escape_string($oauth_id);
  197. $oauth_id = intval($oauth_id);
  198. //$res = mysql_query("SELECT name FROM users WHERE oauth_id = '$oauth_id'");
  199. $res = mysql_query_cache("SELECT name FROM users WHERE oauth_id = '$oauth_id'");
  200. if (count($res)==0){
  201. $lat = mysql_escape_string($lat);
  202. $lng = mysql_escape_string ($lng);
  203. $token = mysql_escape_string($token);
  204. $location = mysql_escape_string($location);
  205. mysql_query("INSERT INTO users (name,oauth_id,lat,lng,access_token,location) VALUES('$name','$oauth_id','$lat','$lng','$token','$location')");
  206. return true;
  207. }
  208. else if ($user_oauth_id == $oauth_id){
  209. $lat = mysql_escape_string($lat);
  210. $lng = mysql_escape_string ($lng);
  211. $location = mysql_escape_string($location);
  212. mysql_query("UPDATE users SET lat = '$lat', lng='$lng',location='$location' WHERE oauth_id='$oauth_id'");
  213. return true;
  214. }
  215. return false;
  216. }
  217. function createFriendship($friend1_id,$friend2_id){
  218. //returns true if friendship is successfully added
  219. //returns false if friendship was already in database
  220. if ($friend1_id != $friend2_id){
  221. mysqlConnect();
  222. $id1 = mysql_escape_string($friend1_id);
  223. $id2 = mysql_escape_string($friend2_id);
  224. $id1 = intval($id1);
  225. $id2 = intval($id2);
  226. //$res = mysql_query("SELECT id FROM friends WHERE (friend1_id = '$id1' AND friend2_id='$id2') OR (friend2_id='$id1' AND friend1_id='$id2')");
  227. $res = mysql_query_cache("SELECT id FROM friends WHERE (friend1_id = '$id1' AND friend2_id='$id2') OR (friend2_id='$id1' AND friend1_id='$id2')");
  228. //checks to see if friendship is already in as friend1-friend2 or friend2-friend1
  229. if (count($res)==0){
  230. mysql_query("INSERT INTO friends (friend1_id,friend2_id) VALUES('$id1','$id2')");
  231. return true;
  232. }
  233. else{
  234. return false;
  235. }
  236. }
  237. else return false;
  238. }
  239. function getFriendLocationsSql($friends_ids){
  240. //takes an array of oauth_ids and returns an array with id as key and an array ('name','location','lat','lng') as values
  241. $arr = array();
  242. if (count($friends_ids) == 0) {
  243. $query = "";
  244. }
  245. else if (count($friends_ids) == 1){
  246. $friend_id = intval($friends_ids[0]);
  247. $query = "SELECT oauth_id,name,location,lat,lng FROM users WHERE oauth_id='{$friends_ids[0]}'";
  248. }
  249. else{
  250. $query = "SELECT oauth_id,name,location,lat,lng FROM users WHERE oauth_id='";
  251. $query_part = implode("' OR oauth_id='",$friends_ids);
  252. $query = $query.$query_part."'";
  253. }
  254. mysqlConnect();
  255. $res = mysql_query($query);
  256. if (mysql_num_rows($res)){
  257. while ($row = mysql_fetch_array($res)){
  258. $arr[$row['oauth_id']] = array('name'=>$row['name'],'location'=>$row['location'],'lat'=>$row['lat'],'lng'=>$row['lng']);
  259. }
  260. }
  261. /*
  262. mysqlConnect();
  263. foreach ($friends_ids as $id){
  264. $id = intval($id);
  265. $res = mysql_query("SELECT name,location,lat,lng FROM users WHERE oauth_id='$id'");
  266. $row = mysql_fetch_array($res);
  267. $arr[$id] = array('name'=>$row['name'],'location'=>$row['location'],'lat'=>$row['lat'],'lng'=>$row['lng']);
  268. }
  269. */
  270. return $arr;
  271. }
  272. ?>