PageRenderTime 60ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/FbApi/index.php

https://bitbucket.org/onlinechessportal/onlinechessgameportal
PHP | 155 lines | 115 code | 22 blank | 18 comment | 35 complexity | 98ba2c5940bce887934062066f9b0d59 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. include_once "fbaccess.php";
  3. require '../Control/RegisterLogic.php';
  4. require '../Model/Connect.php';
  5. ob_start();
  6. if(!$User) {
  7. echo '<script type="text/javascript"> window.location = "'.$loginUrl.'"</script>';
  8. } else {
  9. //echo "<a href='$logoutUrl' >Logout</a>";
  10. if(isset($user_info['gender'])) {
  11. //$sex = $user_info['gender'];
  12. if($user_info['gender'] == 'male'){
  13. $Sex = 'M';
  14. } else {
  15. $Sex = 'F';
  16. }
  17. } else $Sex = null;
  18. //if(isset($user_info['birthday'])) $dob = $user_info['birthday'];
  19. //else $dob = null;
  20. if(isset($user_info['email'])) $Email = $user_info['email'];
  21. else $Email = null;
  22. /*if(isset($user_info['education'])) {
  23. echo "<b> EDUCATION : </b>";
  24. foreach($user_info['education'] as $school){
  25. echo $school['school']['name'];
  26. break;
  27. }
  28. }
  29. else $sex = null;*/
  30. if(isset($user_info['hometown'])) $Hometown = $user_info['hometown']['name'];
  31. else $Hometown = null;
  32. $CurrentCity = $Hometown;
  33. if(isset($user_info['name'])) $Name = $user_info['name'];
  34. else $Name = null;
  35. $UserPicPath = "http://graph.facebook.com/".$User."/picture?type=large";
  36. $SourceCode = GetImageFromUrl($UserPicPath);
  37. $SaveFile = fopen("../View/UserImages/".$User.".jpg", "w"); //this is name of new file that i save
  38. fwrite($SaveFile, $SourceCode);
  39. fclose($SaveFile);
  40. if(!LoggedIn()) {
  41. CheckExistanceFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User);
  42. }
  43. }
  44. function GetImageFromUrl($Link){
  45. $Ch = curl_init();
  46. curl_setopt($Ch, CURLOPT_POST, 0);
  47. curl_setopt($Ch,CURLOPT_URL,$Link);
  48. curl_setopt($Ch, CURLOPT_RETURNTRANSFER, 1);
  49. curl_setopt($Ch,CURLOPT_FOLLOWLOCATION,true);
  50. $Result=curl_exec($Ch);
  51. curl_close($Ch);
  52. return $Result;
  53. }
  54. function CheckExistanceFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User) {
  55. $QueryCheck = "SELECT * FROM `User` WHERE `us_username` = '".$Email."'";
  56. $QueryCheckRun = mysql_query($QueryCheck);
  57. if (mysql_num_rows($QueryCheckRun) == 0) {
  58. echo 'No record';
  59. if(InsertFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User)) {
  60. if(Check($Email)) {
  61. echo 'done3';
  62. echo '<script type="text/javascript"> window.location = "../View/Home_login.php"</script>';
  63. } else {
  64. echo 'fail4';
  65. }
  66. }
  67. } else {
  68. echo 'record';
  69. if(UpdateFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User)) {
  70. if(Check($Email)) {
  71. echo 'done2';
  72. echo '<script type="text/javascript"> window.location = "../View/Home_login.php"</script>';
  73. } else {
  74. echo 'fail3';
  75. }
  76. }
  77. }
  78. }
  79. function InsertFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User) {
  80. $QueryInsert = "INSERT INTO `User` VALUES ('".$Email."',".GetMaxId().",'','Member','LoggedIn','','','')";
  81. $QueryInsert2 = "INSERT INTO `memberprofile` (`mp_username`, `mp_fullname`, `mp_sex`, `mp_hometown`, `mp_currentcity`,`mp_email`,`mp_imagename`) VALUES
  82. ('".$Email."', '".$Name."', '".$Sex."', '".$Hometown."', '".$CurrentCity."', '".$Email."','".$User.".jpg')";
  83. if($QueryInsertRun = mysql_query($QueryInsert)) {
  84. if($QueryInsert2Run = mysql_query($QueryInsert2)) {
  85. //echo 'done';
  86. return true;
  87. } else {
  88. //echo 'fail';
  89. return false;
  90. }
  91. } else {
  92. //echo 'fail2';
  93. return false;
  94. }
  95. }
  96. function UpdateFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User) {
  97. $QueryUpdate = "Update `memberprofile` SET `mp_fullname` = '".$Name."', `mp_sex` = '".$Sex."', `mp_hometown` = '".$Hometown."', `mp_currentcity` = '".$CurrentCity."',`mp_email` = '".$Email."',`mp_imagename` = '".$User.".jpg' WHERE `mp_username` = '".$Email."'";
  98. if($QueryUpdateRun = mysql_query($QueryUpdate)) {
  99. //echo 'done';
  100. return true;
  101. } else {
  102. //echo 'fail22';
  103. return false;
  104. }
  105. }
  106. function GetMaxId() {
  107. $sql = "SELECT max(us_id)+1 AS x FROM user ";
  108. $ResultSql = mysql_query($sql);
  109. $Info = mysql_fetch_assoc( $ResultSql);
  110. $UserId = $Info['x'];
  111. return $UserId;
  112. }
  113. function Check($Username) {
  114. $QueryCheck = "SELECT * FROM `User` WHERE `us_username` ='".$Username."'";
  115. if ($QueryCheckRun = mysql_query ($QueryCheck)) {
  116. $QueryNumRows = mysql_num_rows ($QueryCheckRun);
  117. if ($QueryNumRows == 0 ) {
  118. return false;
  119. } else if ($QueryNumRows == 1){
  120. SetSession ($QueryCheckRun);
  121. return true;
  122. }
  123. }
  124. }
  125. function SetSession($QueryCheckRun) {
  126. $SessionUsername = mysql_result ($QueryCheckRun, 0, 'us_username');
  127. $SessionRole = mysql_result ($QueryCheckRun, 0, 'us_role');
  128. $_SESSION['username'] = $SessionUsername;
  129. $_SESSION['role'] = $SessionRole;
  130. //$_SESSION['timeout'] = time();
  131. return true;
  132. }
  133. ?>