/FbApi/index.php
PHP | 155 lines | 115 code | 22 blank | 18 comment | 35 complexity | 98ba2c5940bce887934062066f9b0d59 MD5 | raw file
Possible License(s): LGPL-2.1
- <?php
- include_once "fbaccess.php";
- require '../Control/RegisterLogic.php';
- require '../Model/Connect.php';
- ob_start();
-
- if(!$User) {
- echo '<script type="text/javascript"> window.location = "'.$loginUrl.'"</script>';
- } else {
- //echo "<a href='$logoutUrl' >Logout</a>";
-
- if(isset($user_info['gender'])) {
- //$sex = $user_info['gender'];
- if($user_info['gender'] == 'male'){
- $Sex = 'M';
- } else {
- $Sex = 'F';
- }
- } else $Sex = null;
-
- //if(isset($user_info['birthday'])) $dob = $user_info['birthday'];
- //else $dob = null;
-
- if(isset($user_info['email'])) $Email = $user_info['email'];
- else $Email = null;
-
- /*if(isset($user_info['education'])) {
- echo "<b> EDUCATION : </b>";
- foreach($user_info['education'] as $school){
- echo $school['school']['name'];
- break;
- }
- }
- else $sex = null;*/
-
- if(isset($user_info['hometown'])) $Hometown = $user_info['hometown']['name'];
- else $Hometown = null;
-
- $CurrentCity = $Hometown;
-
- if(isset($user_info['name'])) $Name = $user_info['name'];
- else $Name = null;
-
- $UserPicPath = "http://graph.facebook.com/".$User."/picture?type=large";
-
- $SourceCode = GetImageFromUrl($UserPicPath);
- $SaveFile = fopen("../View/UserImages/".$User.".jpg", "w"); //this is name of new file that i save
- fwrite($SaveFile, $SourceCode);
- fclose($SaveFile);
-
- if(!LoggedIn()) {
- CheckExistanceFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User);
- }
- }
-
- function GetImageFromUrl($Link){
- $Ch = curl_init();
- curl_setopt($Ch, CURLOPT_POST, 0);
- curl_setopt($Ch,CURLOPT_URL,$Link);
- curl_setopt($Ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($Ch,CURLOPT_FOLLOWLOCATION,true);
- $Result=curl_exec($Ch);
- curl_close($Ch);
- return $Result;
- }
-
- function CheckExistanceFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User) {
- $QueryCheck = "SELECT * FROM `User` WHERE `us_username` = '".$Email."'";
- $QueryCheckRun = mysql_query($QueryCheck);
-
- if (mysql_num_rows($QueryCheckRun) == 0) {
- echo 'No record';
- if(InsertFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User)) {
- if(Check($Email)) {
- echo 'done3';
- echo '<script type="text/javascript"> window.location = "../View/Home_login.php"</script>';
- } else {
- echo 'fail4';
- }
- }
- } else {
- echo 'record';
- if(UpdateFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User)) {
- if(Check($Email)) {
- echo 'done2';
- echo '<script type="text/javascript"> window.location = "../View/Home_login.php"</script>';
- } else {
- echo 'fail3';
- }
- }
- }
- }
-
- function InsertFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User) {
- $QueryInsert = "INSERT INTO `User` VALUES ('".$Email."',".GetMaxId().",'','Member','LoggedIn','','','')";
- $QueryInsert2 = "INSERT INTO `memberprofile` (`mp_username`, `mp_fullname`, `mp_sex`, `mp_hometown`, `mp_currentcity`,`mp_email`,`mp_imagename`) VALUES
- ('".$Email."', '".$Name."', '".$Sex."', '".$Hometown."', '".$CurrentCity."', '".$Email."','".$User.".jpg')";
-
- if($QueryInsertRun = mysql_query($QueryInsert)) {
- if($QueryInsert2Run = mysql_query($QueryInsert2)) {
- //echo 'done';
- return true;
- } else {
- //echo 'fail';
- return false;
- }
- } else {
- //echo 'fail2';
- return false;
- }
- }
-
- function UpdateFB($Email, $Name, $Hometown, $CurrentCity, $Sex, $User) {
- $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."'";
-
- if($QueryUpdateRun = mysql_query($QueryUpdate)) {
- //echo 'done';
- return true;
- } else {
- //echo 'fail22';
- return false;
- }
- }
-
- function GetMaxId() {
- $sql = "SELECT max(us_id)+1 AS x FROM user ";
- $ResultSql = mysql_query($sql);
- $Info = mysql_fetch_assoc( $ResultSql);
- $UserId = $Info['x'];
- return $UserId;
- }
-
- function Check($Username) {
- $QueryCheck = "SELECT * FROM `User` WHERE `us_username` ='".$Username."'";
-
- if ($QueryCheckRun = mysql_query ($QueryCheck)) {
- $QueryNumRows = mysql_num_rows ($QueryCheckRun);
- if ($QueryNumRows == 0 ) {
- return false;
- } else if ($QueryNumRows == 1){
- SetSession ($QueryCheckRun);
- return true;
- }
- }
- }
-
- function SetSession($QueryCheckRun) {
- $SessionUsername = mysql_result ($QueryCheckRun, 0, 'us_username');
- $SessionRole = mysql_result ($QueryCheckRun, 0, 'us_role');
- $_SESSION['username'] = $SessionUsername;
- $_SESSION['role'] = $SessionRole;
- //$_SESSION['timeout'] = time();
- return true;
- }
- ?>