initCommit
diff --git a/check_login_status.php b/check_login_status.php
new file mode 100644
index 0000000..17ede84
--- /dev/null
+++ b/check_login_status.php
@@ -0,0 +1,41 @@
+<?php
+session_start();
+include_once("mySQL/connect.php");
+// Files that inculde this file at the very top would NOT require
+// connection to database or session_start(), be careful.
+// Initialize some vars
+$user_ok = false;
+$log_id = "";
+$log_username = "";
+$log_password = "";
+// User Verify function
+function evalLoggedUser($link,$id,$username,$password){
+ $sql = "SELECT ip FROM users WHERE id='$id' AND username='$username' AND password='$password' LIMIT 1";
+ $query = mysqli_query($link, $sql);
+ $numrows = mysqli_num_rows($query);
+ if($numrows > 0){
+ return true;
+ }
+}
+if(isset($_SESSION["id"]) && isset($_SESSION["username"]) && isset($_SESSION["password"])) {
+ $log_id = preg_replace('#[^0-9]#', '', $_SESSION['id']);
+ $log_username = preg_replace('#[^a-z0-9]#i', '', $_SESSION['username']);
+ $log_password = preg_replace('#[^a-z0-9]#i', '', $_SESSION['password']);
+ // Verify the user
+ $user_ok = evalLoggedUser($link,$log_id,$log_username,$log_password);
+} else if(isset($_COOKIE["id"]) && isset($_COOKIE["user"]) && isset($_COOKIE["pass"])){
+ $_SESSION['id'] = preg_replace('#[^0-9]#', '', $_COOKIE['id']);
+ $_SESSION['username'] = preg_replace('#[^a-z0-9]#i', '', $_COOKIE['user']);
+ $_SESSION['password'] = preg_replace('#[^a-z0-9]#i', '', $_COOKIE['pass']);
+ $log_id = $_SESSION['id'];
+ $log_username = $_SESSION['username'];
+ $log_password = $_SESSION['password'];
+ // Verify the user
+ $user_ok = evalLoggedUser($link,$log_id,$log_username,$log_password);
+ if($user_ok == true){
+ // Update their lastlogin datetime field
+ $sql = "UPDATE users SET lastlogin=now() WHERE id='$log_id' LIMIT 1";
+ $query = mysqli_query($link, $sql);
+ }
+}
+?>
\ No newline at end of file
diff --git a/facebooksdk.php b/facebooksdk.php
new file mode 100644
index 0000000..56a7870
--- /dev/null
+++ b/facebooksdk.php
@@ -0,0 +1,2 @@
+<?php
+echo phpversion();
\ No newline at end of file
diff --git a/fb.js b/fb.js
new file mode 100644
index 0000000..0a846dc
--- /dev/null
+++ b/fb.js
@@ -0,0 +1,43 @@
+function statusChangeCallback(response) {
+ console.log('statusChangeCallback');
+ console.log(response);
+ // The response object is returned with a status field that lets the
+ // app know the current login status of the person.
+ // Full docs on the response object can be found in the documentation
+ // for FB.getLoginStatus().
+ if (response.status === 'connected') {
+ // Logged into your app and Facebook.
+ window.location.replace('./facebook/index.php');
+ } else if (response.status === 'not_authorized') {
+ // The person is logged into Facebook, but not your app.
+ } else {
+ // The person is not logged into Facebook, so we're not sure if
+ // they are logged into this app or not.
+ }
+}
+
+function checkLoginState() {
+ FB.getLoginStatus(function(response) {
+ statusChangeCallback(response);
+ });
+}
+
+window.fbAsyncInit = function() {
+ FB.init({
+ appId : '757964754360580',
+ cookie : true, // enable cookies to allow the server to access
+ // the session
+ xfbml : true, // parse social plugins on this page
+ version : 'v2.5' // use any version
+});
+
+};
+
+// Load the SDK asynchronously
+(function(d, s, id) {
+ var js, fjs = d.getElementsByTagName(s)[0];
+ if (d.getElementById(id)) return;
+ js = d.createElement(s); js.id = id;
+ js.src = "//connect.facebook.net/en_US/sdk.js";
+ fjs.parentNode.insertBefore(js, fjs);
+}(document, 'script', 'facebook-jssdk'));
diff --git a/index.php b/index.php
new file mode 100644
index 0000000..f15af18
--- /dev/null
+++ b/index.php
@@ -0,0 +1,154 @@
+<?php
+ob_start();
+session_start();
+require_once __DIR__ . '/src/Facebook/autoload.php';
+
+$fb = new Facebook\Facebook([
+ 'app_id' => '757964754360580',
+ 'app_secret' => '0e01c0a241e8f173f701f9d57939a866',
+ 'default_graph_version' => 'v2.8',
+ ]);
+
+$redirect = 'http://iedu-eg.com/facebook/';
+
+$helper = $fb->getRedirectLoginHelper();
+
+ # Get the access token and catch the exceptions if any
+ try
+ {
+ if(isset($_SESSION['facebook_access_token']))
+ {
+ $accessToken=$_SESSION['facebook_access_token'];
+ }
+ else
+ {
+ $accessToken = $helper->getAccessToken();
+ }
+
+ }
+
+ catch(Facebook\Exceptions\FacebookResponseException $e)
+ {
+ // When Graph returns an error
+ echo 'Graph returned an error: ' . $e->getMessage();
+ exit;
+ }
+ catch(Facebook\Exceptions\FacebookSDKException $e)
+ {
+ // When validation fails or other local issues
+ echo 'Facebook SDK returned an error: ' . $e->getMessage();
+ exit;
+ }
+
+ # If the
+ if (isset($accessToken))
+ {
+ // Logged in!
+ // Now you can redirect to another page and use the
+ // access token from $_SESSION['facebook_access_token']
+ // But we shall we the same page
+
+ // Sets the default fallback access token so
+ // we don't have to pass it to each request
+ if(isset($_SESSION['facebook_access_token']))
+ {
+ $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
+ }
+ else
+ {
+ $_SESSION['facebook_access_token']=(string) $accessToken;
+ $oAuth2Client = $fb->getOAuth2Client();
+ $longLivedAccessToken=$oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
+ $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
+ }
+
+ try
+ {
+ $response = $fb->get('/me?fields=email,name');
+ $userNode = $response->getGraphUser();
+ }
+ catch(Facebook\Exceptions\FacebookResponseException $e)
+ {
+ // When Graph returns an error
+ echo 'Graph returned an error: ' . $e->getMessage();
+ exit;
+ }
+ catch(Facebook\Exceptions\FacebookSDKException $e)
+ {
+ // When validation fails or other local issues
+ echo 'Facebook SDK returned an error: ' . $e->getMessage();
+ exit;
+ }
+
+
+ // Fetching user Details
+ // Connect to DB
+ $link=mysqli_connect("localhost","iedu","@*K)?6Ev!eK9","iedu");
+ //Storing user name
+ $_SESSION['username']=$userNode->getName();
+ $username=mysqli_real_escape_string($link,$_SESSION['username']);
+ //Storing id
+ $_SESSION['id']=$userNode->getId();
+ $id=$_SESSION['id'];
+ //Storing the ip Address
+ $ip=preg_replace('#[^0-9.]#','',getenv('REMOTE_ADDR'));
+ //Storing user FB profile image
+ $image = 'https://graph.facebook.com/'.$userNode->getId().'/picture?width=200';
+ //Storing the email
+ $_SESSION['email']=$userNode->getProperty('email');
+ $email=$_SESSION['email'];
+ //Checking User Email in the Database
+ $query="SELECT * FROM users WHERE email='".mysqli_real_escape_string($link, $_SESSION['email'])."'";
+ $result=mysqli_query($link, $query);
+ $results=mysqli_num_rows($result); // It will return 1 if the email exists and 0 if not...
+ // COOKIES
+ /*
+ setcookie("id",$id,strtotime('+30 days'),"/","","",TRUE);
+ setcookie("username",$username,strtotime('+30 days'),"/","","",TRUE);
+ setcookie("email",$email,strtotime('+30 days'),"/","","",TRUE);
+ */
+
+ // Checking user Status if FOUND redirect to mainpage
+ if($results)
+ {
+ //UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS
+ $sql="UPDATE `users` SET `ip`='$ip', `lastlogin`=now() WHERE `username`='$username' LIMIT 1 ";
+ $query=mysqli_query($link, $query);
+ header("location:../");
+
+ }
+ // if NOT insert into DB and create a folder for him
+ else
+ {
+ //Insert the user into main users table in DB
+ $sql = "INSERT INTO `users` (`username`, `email`, `id`,`avatar`,`ip`,`signup`,`lastlogin`,`notescheck`) VALUES ('$username', '$email', '$id','$image','$ip',now(),now(),now())";
+ $query=mysqli_query($link, $sql);
+ //Insert into useroptions table
+ $useroptions="INSERT INTO `useroptions` (`username`, `background`, `id`) VALUES ('$username', 'original', '$id')";
+ $queryOption=mysqli_query($link, $useroptions);
+ //create user folder
+ if(!file_exists("facebookuser/$username"))
+ {
+ mkdir("facebookuser/$username", 0755);
+
+ }
+ if($query)
+ {
+ header("location:../");
+ }
+ else
+ {
+ echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
+ }
+ }
+
+ }
+
+ else
+ {
+ $permissions = ['email'];
+ $loginUrl = $helper->getLoginUrl($redirect,$permissions);
+ echo '<a href="' . $loginUrl . '" style="text-decoration:none;">Log in with Facebook!</a>';
+ }
+
+?>