function facebooklogin() { $app = JFactory::getApplication('site'); $db =& JFactory::getDBO(); $user = & JFactory::getUser(); $config = & $app->getParams('com_awdwall'); $fb_id = $config->get('fb_id', ''); $fb_key = $config->get('fb_key', ''); $fb_secret = $config->get('fb_secret', ''); $Itemid = AwdwallHelperUser::getComItemId(); $awdreturnurl=JRoute::_('index.php?option=com_awdwall&view=awdwall&layout=main&Itemid=' . $Itemid, false); if(isset($_COOKIE['fbsr_' . $fb_id])){ list($encoded_sig, $payload) = explode('.', $_COOKIE['fbsr_' . $fb_id], 2); $sig = base64_decode(strtr($encoded_sig, '-_', '+/')); $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true); if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') { return null; } $expected_sig = hash_hmac('sha256', $payload, $fb_secret, $raw = true); if ($sig !== $expected_sig) { return null; } $token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $fb_id . "&client_secret=" . $fb_secret. "&redirect_uri=" . "&code=" . $data['code']; if (function_exists('curl_init')) { $parsedUrl = parse_url($token_url); $ch = curl_init(); $options = array( CURLOPT_URL => $token_url, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HTTPHEADER => array("Host: " . $parsedUrl['host']), CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => false ); curl_setopt_array($ch, $options); $response = @curl_exec($ch); } else { $response = file_get_contents($token_url); } //$response = @file_get_contents($token_url); $params = null; parse_str($response, $params); $data['access_token'] = $params['access_token']; $cookie= $data; } else { $cookie= null; } if($cookie){ if (function_exists('curl_init')) { $newurl='https://graph.facebook.com/me?access_token=' . $cookie['access_token']; $parsedUrl = parse_url($newurl); $ch = curl_init(); $options = array( CURLOPT_URL => $newurl, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HTTPHEADER => array("Host: " . $parsedUrl['host']), CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => false ); curl_setopt_array($ch, $options); $newresponse = @curl_exec($ch); $user_details = json_decode($newresponse); } else { $user_details = json_decode(file_get_contents('https://graph.facebook.com/me?access_token=' . $cookie['access_token'])); } //$user_details = json_decode(file_get_contents('https://graph.facebook.com/me?access_token=' . $cookie['access_token'])); if(count($user_details)){ $db->setQuery("SELECT u.id, u.username, u.email FROM #__users AS u INNER JOIN #__jconnector_ids AS ji ON u.id=ji.user_id WHERE ji.facebook_id = ".$user_details->id); $user_data = $db->loadObject(); if(!$user_data) //we don't have this FB user in our DB yet { if ($user->id) //update existing user with his facebook_id { $username = $user->username; $user_id = $user->id; } else //register a new user { //generate an unique username $i = 0; $username_base = str_replace(' ', '_', $user_details->name); $username = ''; do { if (!$username) $username = $username_base; else $username = $username_base.$i; $db->setQuery("SELECT id FROM #__users WHERE username = '******'"); $data = $db->loadObject(); $i++; }while($data); $generated_details['username'] = $username; $generated_details['password'] = substr(uniqid(true), 0, 20); $generated_details['email'] = $user_details->email; $user_id = awdwallController::register_joomla($generated_details, $user_details); } //create entry that associates user_id with facebook_id if ($user_id) { $sql="SELECT count(*) as cnt FROM #__jconnector_ids WHERE facebook_id='".$user_details->id."'"; $db->setQuery($sql); $cnt = $db->loadResult(); if($cnt==0) { $query = 'INSERT INTO #__jconnector_ids (user_id, facebook_id) VALUES("'.$user_id.'", "'.$user_details->id.'")'; $db->setQuery($query); $db->query(); } else { $query = "UPDATE #__jconnector_ids SET user_id='".$user_id."' where facebook_id='".$user_details->id."'"; $db->setQuery($query); $db->query(); } } } else { if ($user->id) //somebody is trying to connect second Joomla account with the same Facebook user { $username = $user_data->username; } else //a connected user is trying to sign in { $username = $user_data->username; } } $sql="SELECT id FROM #__users, #__jconnector_ids WHERE user_id = id AND facebook_id=".$user_details->id; //echo $sql; $db->setQuery($sql); $rows = $db->loadObject(); $userid = $rows->id; //echo $userid;exit; if($userid){ if (function_exists('curl_init')) { $newurl='https://api.facebook.com/method/users.getInfo?uids='.$user_details->id.'&fields=pic&access_token='. $cookie['access_token']; $parsedUrl = parse_url($newurl); $ch = curl_init(); $options = array( CURLOPT_URL => $newurl, CURLOPT_RETURNTRANSFER => 1, CURLOPT_HTTPHEADER => array("Host: " . $parsedUrl['host']), CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => false ); curl_setopt_array($ch, $options); $str = @curl_exec($ch); } else { $str = file_get_contents('https://api.facebook.com/method/users.getInfo?uids='.$user_details->id.'&fields=pic&access_token='. $cookie['access_token']); } //get avatar //$str = file_get_contents('https://api.facebook.com/method/users.getInfo?uids='.$user_details->id.'&fields=pic&access_token='. $cookie['access_token']); $title_regex = "/<pic>(.+)<\/pic>/i"; preg_match_all($title_regex, $str, $url, PREG_PATTERN_ORDER); $img = $url[1]; $url = $img[0]; $temp = explode('/', $url); $fileName = $temp[(count($temp)-1)]; $src = awdwallController::send_request($url); awdwallController::upavatar(JPATH_BASE . DS . 'images' . DS . 'wallavatar' . DS . $userid . DS); awdwallController::upavatar(JPATH_BASE . DS . 'images' . DS . 'wallavatar' . DS . $userid . DS . 'original' . DS); awdwallController::upavatar(JPATH_BASE . DS . 'images' . DS . 'wallavatar' . DS . $userid . DS . 'thumb' . DS); $path = JPATH_BASE . DS . 'images' . DS . 'wallavatar' . DS . $userid . DS . $fileName; file_put_contents($path, $src); $path_thub = JPATH_BASE . DS . 'images' . DS . 'wallavatar' . DS . $userid . DS . 'thumb' . DS; //thumb $file_thumb = str_replace('_s.', '_q.', $fileName); $url_thumb = str_replace('_s.', '_q.', $url); $src_thumb = awdwallController::send_request($url_thumb); file_put_contents($path_thub.DS.$file_thumb, $src_thumb); rename($path_thub.DS.$file_thumb, $path_thub.DS.'tn'.$fileName); file_put_contents($path_thub.DS.$fileName, $src); //original $path_ori = JPATH_BASE . DS . 'images' . DS . 'wallavatar' . DS . $userid . DS . 'original' . DS; $file_thumb = str_replace('_s.', '_q.', $fileName); $url_thumb = str_replace('_s.', '_q.', $url); $src_thumb = awdwallController::send_request($url_thumb); file_put_contents($path_ori.DS.$file_thumb, $src_thumb); file_put_contents($path_ori.DS.$fileName, $src); $db->setQuery("SELECT user_id FROM #__awd_wall_users WHERE user_id = '$userid'"); $rows = $db->loadObject(); if($user_details->gender == 'male') $gender = 1; else $gender = 0; if(count($rows)){ $query_wall = "UPDATE #__awd_wall_users SET avatar='$fileName', gender='$gender', birthday='".$user_details->birthday."' WHERE user_id= '$userid'"; //echo $query_wall; $db->setQuery($query_wall); $db->query(); }else{ $query_wall = "INSERT INTO #__awd_wall_users (user_id, avatar, gender, birthday, aboutme) VALUES('$userid', '$fileName', '$gender', '".$user_details->birthday."', '')"; //echo $query_wall; $db->setQuery($query_wall); $db->query(); } } if($userid){ $app = &JFactory::getApplication(); $query = "SELECT password FROM #__users WHERE id='".$userid."';"; //echo $query.'<br>'; $db->setQuery($query); $oldpass = $db->loadResult(); //echo $oldpass.'<br>'; jimport( 'joomla.user.helper' ); $password = JUserHelper::genRandomPassword(5); $query = "UPDATE #__users SET password='******' WHERE id='".$userid."';"; //echo $query.'<br>'; $db->setQuery($query); $db->query(); $app = JFactory::getApplication(); $credentials = array( "username" => $username, "password" => $password ); $app->login($credentials); $query = "UPDATE #__users SET password='******' WHERE id='".$userid."';"; $db->setQuery($query); $db->query(); } } } $this->setRedirect($awdreturnurl); }