Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function facebook() {
- if($this->fbapp) {
- $getToken = $this->getFbToken($this->fbappid, $this->fbappsecret, $this->url.'/requests/connect.php?facebook=true', $this->fbcode);
- $user = $this->parseFbInfo($getToken['access_token']);
- if($getToken == null || $_SESSION['state'] == null || ($_SESSION['state'] != $this->fbstate) || empty($user->email)) {
- header("Location: ".$this->url);
- }
- if(!empty($user->email)) {
- $this->email = $user->email;
- $this->first_name = $user->first_name;
- $this->last_name = $user->last_name;
- $checkEmail = $this->verify_if_email_exists(1);
- // If user already exist
- if($checkEmail) {
- // Set sessions and log-in
- $_SESSION['username'] = $checkEmail['username'];
- $_SESSION['password'] = $checkEmail['password'];
- // Redirect user
- header("Location: ".$this->url);
- } else {
- $this->profile_image = $this->parseFbPicture($getToken['access_token']);
- $this->generateUsername();
- $this->password = $this->generatePassword(8);
- $this->query();
- $_SESSION['username'] = $this->username;
- $_SESSION['password'] = md5($this->password);
- return 1;
- }
- }
- }
- }
- function generateUsername($type = null) {
- // If type is set, generate a random username
- if($type) {
- $this->username = $this->parseUsername().rand(0, 999);
- } else {
- $this->username = $this->parseUsername();
- }
- // Replace the '.' sign with '_' (allows @user_mention)
- $this->username = str_replace('.', '_', $this->username);
- // Check if the username exists
- $checkUser = $this->verify_if_user_exist();
- if($checkUser) {
- $this->generateUsername(1);
- }
- }
- function parseUsername() {
- if(ctype_alnum($this->first_name) && ctype_alnum($this->last_name)) {
- return $this->username = $this->first_name.'.'.$this->last_name;
- } elseif(ctype_alnum($this->first_name)) {
- return $this->first_name;
- } elseif(ctype_alnum($this->last_name)) {
- return $this->last_name;
- } else {
- // Parse email address
- $email = explode('@', $this->email);
- $email = preg_replace("/[^a-z0-9]+/i", "", $email[0]);
- if(ctype_alnum($email)) {
- return $email;
- } else {
- return rand(0, 9999);
- }
- }
- }
- function generatePassword($length) {
- // Allowed characters
- $chars = str_split("abcdefghijklmnopqrstuvwxyz0123456789");
- // Generate password
- for($i = 1; $i <= $length; $i++) {
- // Get a random character
- $n = array_rand($chars, 1);
- // Store random char
- $password .= $chars[$n];
- }
- return $password;
- }
- function getFbToken($app_id, $app_secret, $redirect_url, $code) {
- // Build the token URL
- $url = 'https://graph.facebook.com/oauth/access_token?client_id='.$app_id.'&redirect_uri='.urlencode($redirect_url).'&client_secret='.$app_secret.'&code='.$code;
- // Get the file
- $response = json_decode(fetch($url), true);
- // Return parameters
- return $response;
- }
- function parseFbInfo($access_token) {
- // Build the Graph URL
- $url = "https://graph.facebook.com/me?fields=id,email,first_name,gender,last_name,link,locale,name,timezone,updated_time,verified&access_token=".$access_token;
- // Get the file
- $user = json_decode(fetch($url));
- // Return user
- if($user != null && isset($user->name)) {
- return $user;
- }
- return null;
- }
- function parseFbPicture($access_token) {
- // Build the Graph URL
- $url = "https://graph.facebook.com/me/picture?width=500&height=500&access_token=".$access_token;
- // Get the image
- $image = fetch($url);
- // Generate the file name
- $file_name = mt_rand().'_'.mt_rand().'_'.mt_rand().'.jpg';
- $file_path = __DIR__ .'/../uploads/avatars/';
- // Create the file
- $fp = fopen($file_path.$file_name, 'wb');
- // If the file can't be written
- if(!file_exists($file_path.$file_name)) {
- // Return the file name
- return false;
- }
- // Write the image
- fwrite($fp, $image);
- // Close
- fclose($fp);
- // Return the filename
- return $file_name;
- }
- function process() {
- global $LNG;
- // Prevents bypassing the FILTER_VALIDATE_EMAIL
- $this->email = htmlspecialchars($this->email, ENT_QUOTES, 'UTF-8');
- $arr = $this->validate_values(); // Must be stored in a variable before executing an empty condition
- if(empty($arr)) { // If there is no error message then execute the query;
- $this->query();
- // Set a session and log-in the user
- $_SESSION['username'] = $this->username;
- $_SESSION['password'] = md5($this->password);
- // Return (int) 1 if everything was validated
- return 1;
- // return $LNG['user_success'];
- } else { // If there is an error message
- foreach($arr as $err) {
- return notificationBox('error', $LNG["$err"], 1); // Return the error value for translation file
- }
- }
- }
- function verify_if_user_exist() {
- $query = sprintf("SELECT `username` FROM `users` WHERE `username` = '%s'", $this->db->real_escape_string(mb_strtolower($this->username)));
- $result = $this->db->query($query);
- return ($result->num_rows == 0 && !in_array(mb_strtolower($this->username), array('playlists', 'subscribers', 'subscriptions', 'about', 'messages'))) ? 0 : 1;
- }
- function verify_accounts_per_ip() {
- if($this->accounts_per_ip) {
- $query = $this->db->query(sprintf("SELECT COUNT(`ip`) FROM `users` WHERE `ip` = '%s'", $this->db->real_escape_string(getUserIP())));
- $result = $query->fetch_row();
- if($result[0] < $this->accounts_per_ip) {
- return true;
- } else {
- return false;
- }
- } else {
- return true;
- }
- }
- function verify_if_email_exists($type = null) {
- // Type 0: Normal check
- // Type 1: Facebook check & return type
- if($type) {
- $query = sprintf("SELECT `username`, `password` FROM `users` WHERE `email` = '%s'", $this->db->real_escape_string(mb_strtolower($this->email)));
- } else {
- $query = sprintf("SELECT `email` FROM `users` WHERE `email` = '%s'", $this->db->real_escape_string(mb_strtolower($this->email)));
- }
- $result = $this->db->query($query);
- if($type) {
- return ($result->num_rows == 0) ? 0 : $result->fetch_assoc();
- } else {
- return ($result->num_rows == 0) ? 0 : 1;
- }
- }
- function verify_captcha() {
- if($this->captcha_on) {
- if($this->captcha == "{$_SESSION['captcha']}" && !empty($this->captcha)) {
- return true;
- } else {
- return false;
- }
- } else {
- return true;
- }
- }
- function validate_values() {
- // Create the array which contains the Language variable
- $error = array();
- // Define the Language variable for each type of error
- if($this->verify_accounts_per_ip() == false) {
- $error[] = 'user_limit';
- }
- if($this->verify_if_user_exist() !== 0) {
- $error[] = 'user_exists';
- }
- if($this->verify_if_email_exists() !== 0) {
- $error[] = 'email_exists';
- }
- if(empty($this->username) && empty($this->password) && empty($email)) {
- $error[] = 'all_fields';
- }
- if(strlen($this->password) < 6) {
- $error[] = 'password_too_short';
- }
- if(!ctype_alnum($this->username)) {
- $error[] = 'user_alnum';
- }
- if(strlen($this->username) <= 2 || strlen($this->username) >= 33) {
- $error[] = 'user_too_short';
- }
- if(!filter_var($this->email, FILTER_VALIDATE_EMAIL)) {
- $error[] = 'invalid_email';
- }
- if($this->verify_captcha() == false) {
- $error[] = 'invalid_captcha';
- }
- return $error;
- }
- function query() {
- $query = sprintf("INSERT into `users` (`username`, `password`, `first_name`, `last_name`, `email`, `date`, `image`, `cover`, `online`, `ip`, `notificationl`, `notificationc`, `notificationd`, `notificationf`, `email_comment`, `email_like`, `email_new_friend`) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', 'default.png', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');", $this->db->real_escape_string(mb_strtolower($this->username)), md5($this->db->real_escape_string($this->password)), $this->db->real_escape_string($this->first_name), $this->db->real_escape_string($this->last_name), $this->db->real_escape_string($this->email), date("Y-m-d H:i:s"), ($this->profile_image ? $this->profile_image : 'default.png'), time(), $this->db->real_escape_string(getUserIp()), 1, 1, 1, 1, $this->email_comment, $this->email_like, $this->email_new_friend);
- $this->db->query($query);
- }
Add Comment
Please, Sign In to add comment