if (!defined('BASEPATH'))
exit('No direct script access allowed');
// Skip these two lines if you're using Composer
define('FACEBOOK_SDK_V4_SRC_DIR', APPPATH . 'libraries/facebook-php-sdk-v4/src/Facebook/');
require APPPATH . 'libraries/facebook-php-sdk-v4/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\GraphUser;
class login extends HO_Site {
public function __construct() {
parent::__construct();
$this->load->config('facebook');
$this->load->library('session');
$this->lang->load('user');
$this->load->library('validation/UserValidation', array($this), 'validation');
FacebookSession::setDefaultApplication(config_item('facebook_app_id'), config_item('facebook_app_secret'));
}
public function fbs() {
$helper = new FacebookRedirectLoginHelper($this->config->item('redirect_url', 'facebook'));
$loginUrl = $helper->getLoginUrl(); //($this->config->item('permissions', 'facebook'));
redirect($loginUrl);
}
public function fbe() {
$helper = new FacebookRedirectLoginHelper($this->config->item('redirect_url', 'facebook'));
$session = FALSE;
try {
if ($session === FALSE) {
$session = $helper->getSessionFromRedirect();
}
} catch (FacebookRequestException $ex) {
log_message('error', $ex->getMessage());
} catch (\Exception $ex) {
log_message('error', $ex->getMessage());
}
if ($session) {
try {
$me = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());
$firstname = $me->getFirstName();
$lastname = $me->getLastName();
$email = $me->getEmail();
if (!User()->logged_in()) {
$user = $this->user_model->find_user($email);
if (empty($user) || !isset($user['id'])) {
$new_password = User()->get_random_password();
$posted['firstname'] = $firstname;
$posted['lastname'] = $lastname;
$posted['username'] = $email;
$posted['email'] = $email;
$posted['salt'] = User()->get_salt();
$posted['password'] = User()->get_hashed_password($posted, $new_password);
$posted['createddate'] = utc_datetime();
$role = $this->role_model->get(array('code' => 'user'));
$posted['role_id'] = $role['id'];
$posted['id'] = $this->user_model->save($posted);
} else {
if (User()->login($email, $user['password'], true, true)) {
redirect(site_url());
} else {
redirect(site_url('login'));
}
}
} else {
redirect(site_url());
}
} catch (FacebookRequestException $e) {
// The Graph API returned an error
$this->error($ex->getMessage());
} catch (\Exception $e) {
// Some other error occurred
$this->error($ex->getMessage());
}
} else {
redirect(site_url('login'));
}
}
public function index($error = '') {
if (User()->logged_in()) {
redirect(site_url());
} else {
$loginUrl = site_url('login/fbs');
$this->view('index', array('loginUrl' => $loginUrl, 'error' => $error));
}
}
public function auth() {
if ($this->validation->validate_login()) {
$posted = $this->input->post();
$email = $posted['username'];
$password = $posted['password'];
try {
if (!User()->logged_in()) {
$user = $this->user_model->find_user($email);
if (empty($user) || !isset($user['id'])) {
$posted['username'] = $email;
$posted['email'] = $email;
$posted['salt'] = User()->get_salt();
$posted['password'] = User()->get_hashed_password($posted, $password);
$posted['createddate'] = utc_datetime();
$role = $this->role_model->get(array('code' => 'user'));
$posted['role_id'] = $role['id'];
$posted['id'] = $this->user_model->save($posted);
} else {
if (User()->login($email, $password, true)) {
redirect(site_url());
} else {
$this->index('Нэр эсвэл нууц үг буруу байна!');
}
}
} else {
redirect(site_url());
}
} catch (Exception $ex) {
//500 Internal Server Error
log_message('error', $ex->getMessage());
$this->index('Нэвтрэх үед алдаа гарлаа!');
}
} else {
//400 Bad Request
$this->index('Нэр эсвэл нууц үг буруу байна!');
}
}
public function reset() {
if ($this->validation->validate_reset()) {
try {
$posted = $this->input->post('email');
if (User()->reset($posted)) {
$this->success('Нууц үгийг сэргээлээ!');
} else {
$this->error('Нууц үгийг сэргээлээ!');
}
} catch (Exception $ex) {
log_message('error', $ex->getMessage());
$this->error($ex->getMessage());
}
} else {
$this->error('Хүсэлт буруу байна!');
}
}
}