409 lines
17 KiB
PHP
409 lines
17 KiB
PHP
<?php
|
|
|
|
defined('BASEPATH') OR exit('No direct script access allowed');
|
|
|
|
//Import Hybridauth's namespace
|
|
use Hybridauth\Hybridauth;
|
|
|
|
class Login extends WRB_Controller {
|
|
|
|
public function __construct() {
|
|
parent::__construct();
|
|
// Load library and url helper
|
|
//$this->load->library('facebook');
|
|
//$this->load->helper('url');
|
|
$this->load->library(array('session', 'lib_login'));
|
|
}
|
|
|
|
private function excudedEmails(){
|
|
$ecData= [
|
|
'ses66181+1@gmail.com',
|
|
'tokslawus@gmail.com',
|
|
'ses6681+3@gmail.com',
|
|
'ameye@chiefsoft.com',
|
|
'ses66181@gmail.com',
|
|
];
|
|
|
|
if (in_array($_SESSION['username'], $ecData))
|
|
{
|
|
$_SESSION['allow_payment_features'] = true;
|
|
}
|
|
else
|
|
{
|
|
$_SESSION['allow_payment_features'] = false;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public function index() {
|
|
$data["login_message"] = $this->session->flashdata('login_message');
|
|
$this->load->library('form_validation');
|
|
$this->form_validation->set_rules('username', 'Username', 'required');
|
|
$this->form_validation->set_rules('password', 'Password', 'required');
|
|
|
|
if ($this->form_validation->run() !== false) {
|
|
|
|
|
|
$data['username'] = strtolower($this->input->post('username')); // '08174596144';
|
|
$data['password'] = $this->input->post('password'); //'7978';
|
|
|
|
$data['sessionid'] = rand(10000, 99999) . "A" . rand(10000, 99999);
|
|
$data['action'] = WRENCHBOARD_ACCOUNT_LOGIN;
|
|
$this->load->model('backend_model');
|
|
$out = array();
|
|
$res = $this->backend_model->wrenchboard_api($data, $out);
|
|
$is_live = $this->backend_model->cfgReadChar("system.live");
|
|
// print_r($out);
|
|
if ($res == PHP_LOGIN_OK) {
|
|
$this->load->model('account_model');
|
|
$ret = $this->account_model->username_data($data);
|
|
if ($res !== false) {
|
|
$data["login_message"] = "Welcome...";
|
|
// yes person has an account
|
|
$_SESSION['session_id'] = $out["session"];
|
|
$_SESSION['username'] = $this->input->post('username');
|
|
$_SESSION['name'] = $this->input->post('username');
|
|
$_SESSION['firstname'] = $ret->firstname;
|
|
$_SESSION['lastname'] = $ret->lastname;
|
|
$_SESSION['email'] = $ret->email;
|
|
$_SESSION['member_id'] = $ret->id;
|
|
$_SESSION['log_count'] = 0;
|
|
$_SESSION['mesaage_count'] = 0;
|
|
$this->findOffers($_SESSION['email']);
|
|
$_SESSION['message_snapshot'] = $this->myMessagesSnapshot();
|
|
$_SESSION['total_jobs'] = 0;
|
|
if (trim($ret->profile_pic) == '') {
|
|
// Show default picture
|
|
$_SESSION['profile_picture'] = "../smedia/DEFAULTS/default-profile.png";
|
|
} else {
|
|
// show the profile picture
|
|
$_SESSION['profile_picture'] = "../smedia/" . ($is_live ? "LIVE" : "TEST") . "/profile/" . $ret->profile_pic;
|
|
}
|
|
|
|
|
|
$this->logUser('Account login');
|
|
$this->refreshAccountDetail($_SESSION['member_id']);
|
|
$this->getSessionArray();
|
|
$this->excudedEmails();
|
|
$this->load->model('autopref_model');
|
|
|
|
$prefArray = $this->autopref_model->afterLoginPage();
|
|
redirect($prefArray['returnPage']);
|
|
}
|
|
} else {
|
|
$data["login_message"] = "Invalid username or password";
|
|
}
|
|
|
|
$res = false; // $this->login_model->verify_login($data);
|
|
} else {
|
|
echo validation_errors();
|
|
}
|
|
|
|
if ($this->lib_login->is_authenticated()) {
|
|
$fb_data = $this->lib_login->getData($this->lib_login->get_access_token());
|
|
if (isset($fb_data['me'])) {
|
|
$user = $fb_data['me'];
|
|
$user['facebook'] = 1;
|
|
|
|
list($proceed, $data) = $this->checkUserExists($user, $data);
|
|
|
|
if ($proceed) {
|
|
$name = (string) $user["name"];
|
|
if (trim($name) == "")
|
|
$name = strtok($user["email"], "@");
|
|
$data['fb_id'] = $user["id"];
|
|
$data['firstname'] = strtok($name, " ");
|
|
$data['lastname'] = strtok(" ");
|
|
$data['email'] = $user["email"];
|
|
$data['sessionid'] = rand(10000, 99999) . "A" . rand(10000, 99999);
|
|
$data['action'] = WRENCHBOARD_FACEBOOK_LOGIN;
|
|
$this->load->model('backend_model');
|
|
$out = array();
|
|
$res = $this->backend_model->wrenchboard_api($data, $out);
|
|
if ($res == PHP_LOGIN_OK) {
|
|
$this->load->model('account_model');
|
|
$ret = $this->account_model->username_data($out);
|
|
if ($res !== false) {
|
|
$data["login_message"] = "Welcome...";
|
|
// yes person has an account
|
|
$_SESSION['session_id'] = $out["session"];
|
|
$_SESSION['username'] = $ret->username;
|
|
$_SESSION['name'] = $ret->username;
|
|
$_SESSION['firstname'] = $ret->firstname;
|
|
$_SESSION['lastname'] = $ret->lastname;
|
|
$_SESSION['email'] = $ret->email;
|
|
$_SESSION['member_id'] = $ret->id;
|
|
$_SESSION['log_count'] = 0;
|
|
$_SESSION['mesaage_count'] = 0;
|
|
$this->findOffers($_SESSION['email']);
|
|
$_SESSION['message_snapshot'] = $this->myMessagesSnapshot();
|
|
$_SESSION['profile_picture'] = "../smedia/DEFAULTS/default-profile.png";
|
|
$this->logUser('Account login');
|
|
$this->refreshAccountDetail($_SESSION['member_id']);
|
|
$this->getSessionArray();
|
|
|
|
$this->excudedEmails();
|
|
|
|
redirect('dash');
|
|
}
|
|
}
|
|
}
|
|
} else if ($data["login_message"] == "") {
|
|
//$data["login_message"] = "Facebook login failed";
|
|
}
|
|
}
|
|
|
|
if( strlen( $data["login_message"]) > 0 )
|
|
{
|
|
$data["login_message"] = "<div class=\"text-left\"><div class=\"alert alert-danger no-border\">" . $data["login_message"] . "-<a href=\"/password_recovery\">Forgot Password ?</a></div></div>";
|
|
}
|
|
$data['title'] = "Login"; // Capitalize the first letter
|
|
$data['page_title'] = "Login WrenchBoard";
|
|
//----- long out $this->load->view('templates/header_boxed', $data);
|
|
//$this->load->view('users/view_login', $data);
|
|
//$this->load->view('users/view_external_footer');
|
|
|
|
// Load Hybridauth's helper
|
|
$this->load->helper('hybridauth');
|
|
|
|
/*
|
|
// Instantiate Hybridauth's classes
|
|
$hybrid = new Hybridauth(get_hybridauth_config());
|
|
|
|
// Get login links
|
|
$login_links = get_hybridauth_links($hybrid, $this->router);
|
|
*/
|
|
$login_links = array(
|
|
'Google' => get_google_login_link()
|
|
);
|
|
// Pass login links to html template
|
|
$data['login_links'] = $login_links;
|
|
|
|
// Render html template
|
|
$this->load->view('site3/external/view_home',$data);
|
|
}
|
|
|
|
//<a href="/password_recovery">Forgot Password ?</a>
|
|
|
|
public function refreshAccountDetail($member_id) {
|
|
$this->load->model('account_model');
|
|
$qr = $this->account_model->loadAccountData($member_id);
|
|
$this->load->model('account_model');
|
|
|
|
$_SESSION['current_balance'] = $this->account_model->getUserWallet($member_id,$currency='NAIRA'); // $out['current_balance'];
|
|
// $_SESSION['current_balance'] = $qr[0]['balance'];
|
|
$_SESSION['active_task'] = $this->account_model->loadActiveTaskCount($member_id);
|
|
}
|
|
|
|
private function checkUserExists($user, $data)
|
|
{
|
|
$proceed = false;
|
|
$q = " AND external_idp_provider=1";
|
|
if (is_array($user) && array_key_exists('google',$user)) {
|
|
$q = " AND external_idp_provider=2";
|
|
}
|
|
//$data["login_message"] = $user["id"]."/".$user["name"]."/".$user["email"]."<a href='".$this->lib_login->getLogoutUrl()."'>Logout</a>";
|
|
$q = "SELECT * FROM members WHERE lower(email)=lower('" . $user["name"] . "') AND password IS NULL AND external_idp_provider_id='" . $user["id"] . "'" . $q;
|
|
$query = $this->db->query($q);
|
|
if (count($query->result_array)) {
|
|
// Account exists
|
|
$proceed = true;
|
|
} else {
|
|
$q = "SELECT * FROM members WHERE lower(email)=lower('" . $user["name"] . "')";
|
|
$query = $this->db->query($q);
|
|
if (count($query->result_array)) {
|
|
$data["login_message"] = "The e-mail " . $user["email"] . " is already registered. You should login with your username and password.";
|
|
} else {
|
|
// Create account
|
|
$proceed = true;
|
|
}
|
|
}
|
|
return array($proceed, $data);
|
|
}
|
|
|
|
public function logout() {
|
|
$this->lib_login->destroy_session();
|
|
redirect('login', redirect);
|
|
}
|
|
|
|
//Processes social login
|
|
function auth($provider = NULL)
|
|
{
|
|
$data = array();
|
|
// Load Hybridauth's helper
|
|
$this->load->helper('hybridauth');
|
|
$code = $this->input->get('code');
|
|
if ($code!='') {
|
|
$client = get_google_client();
|
|
$token = $client->fetchAccessTokenWithAuthCode($code);
|
|
if (is_array($token) && array_key_exists('access_token',$token)) {
|
|
$client->setAccessToken($token['access_token']);
|
|
|
|
// get profile info
|
|
$google_oauth = new Google_Service_Oauth2($client);
|
|
$google_account_info = $google_oauth->userinfo->get();
|
|
$id = $google_account_info->id;
|
|
$email = $google_account_info->email;
|
|
$name = $google_account_info->name;
|
|
|
|
//var_dump($google_account_info);
|
|
//exit();
|
|
|
|
if ($id !='' && $email != '') {
|
|
$user = array(
|
|
'id' => $id,
|
|
'name' => $name,
|
|
'email' => $email,
|
|
'google' => 1
|
|
);
|
|
list($proceed, $data) = $this->checkUserExists($user, $data);
|
|
|
|
if ($proceed) {
|
|
|
|
$name = (string) $user["name"];
|
|
if (trim($name) == "") {
|
|
$name = strtok($user["email"], "@");
|
|
}
|
|
$data['google_id'] = $user["id"];
|
|
$data['firstname'] = strtok($name, " ");
|
|
$data['lastname'] = strtok(" ");
|
|
$data['email'] = $user["email"];
|
|
$data['sessionid'] = rand(10000, 99999) . "A" . rand(10000, 99999);
|
|
$data['action'] = WRENCHBOARD_GOOGLE_LOGIN;
|
|
$this->load->model('backend_model');
|
|
$out = array();
|
|
|
|
$res = $this->backend_model->wrenchboard_api($data, $out);
|
|
if ($res == PHP_LOGIN_OK) {
|
|
$this->load->model('account_model');
|
|
$ret = $this->account_model->username_data($out);
|
|
if ($res !== false) {
|
|
$data["login_message"] = "Welcome...";
|
|
// yes person has an account
|
|
$_SESSION['session_id'] = $out["session"];
|
|
$_SESSION['username'] = $ret->username;
|
|
$_SESSION['name'] = $ret->username;
|
|
$_SESSION['firstname'] = $ret->firstname;
|
|
$_SESSION['lastname'] = $ret->lastname;
|
|
$_SESSION['email'] = $ret->email;
|
|
$_SESSION['member_id'] = $ret->id;
|
|
$_SESSION['log_count'] = 0;
|
|
$_SESSION['mesaage_count'] = 0;
|
|
$this->findOffers($_SESSION['email']);
|
|
$_SESSION['message_snapshot'] = $this->myMessagesSnapshot();
|
|
$_SESSION['profile_picture'] = "../smedia/DEFAULTS/default-profile.png";
|
|
$this->logUser('Account login');
|
|
$this->refreshAccountDetail($_SESSION['member_id']);
|
|
$this->getSessionArray();
|
|
|
|
$this->excudedEmails();
|
|
|
|
redirect('dash');
|
|
} else {
|
|
// Cannot proceed - account model error?
|
|
$this->session->set_flashdata('login_message','Cannot proceed - account model error: '.$ret);
|
|
redirect('login');
|
|
}
|
|
} else {
|
|
// Cannot proceed - backend error?
|
|
$msg = (is_array($out) && array_key_exists('status',$out) && $out['status']!='')
|
|
? $out ['status'] : json_encode($out);
|
|
$this->session->set_flashdata('login_message','Cannot proceed - backend error: '.$msg);
|
|
redirect('login');
|
|
}
|
|
} else {
|
|
// Cannot proceed - duplicate email?
|
|
$this->session->set_flashdata('login_message','Cannot proceed - duplicate email');
|
|
redirect('login');
|
|
}
|
|
} else {
|
|
// Invalid or missing id and/or email - login failed?
|
|
$this->session->set_flashdata('login_message','Invalid or missing id and/or email - login failed!');
|
|
redirect('login');
|
|
}
|
|
} else {
|
|
// Missing access token / invalid grant - expired credentials?
|
|
$this->session->set_flashdata('login_message','Missing access token / invalid grant - expired credentials!');
|
|
redirect('login');
|
|
}
|
|
} else {
|
|
// Invalid or missing code - login failed?
|
|
$this->session->set_flashdata('login_message','Invalid or missing code - login failed!');
|
|
redirect('login');
|
|
}
|
|
}
|
|
|
|
//Processes social login
|
|
function authh($provider = NULL)
|
|
{
|
|
// Load Hybridauth's helper
|
|
$this->load->helper('hybridauth');
|
|
|
|
$service = NULL;
|
|
|
|
try
|
|
{
|
|
//Instantiate Hybridauth's classes
|
|
$hybrid = new Hybridauth(get_hybridauth_config());
|
|
|
|
//Check if given provider is enabled
|
|
if ((isset($provider)) && in_array($provider, $hybrid->getProviders()))
|
|
{
|
|
$this->session->set_userdata('provider', $provider);
|
|
}
|
|
|
|
//Update variable with the valid provider
|
|
$provider = $this->session->userdata('provider');
|
|
|
|
if ($provider)
|
|
{
|
|
$service = $hybrid->authenticate($provider);
|
|
var_dump($service);
|
|
exit();
|
|
if ($service->isConnected())
|
|
{
|
|
//Get user profile
|
|
$profile = $service->getUserProfile();
|
|
|
|
//Get user contacts
|
|
$contacts = $service->getUserContacts();
|
|
|
|
/*
|
|
Disconnect the service else HA would reuse stored session data
|
|
rather making a fresh request in case the user has denied permissions
|
|
in the previous authorization request
|
|
*/
|
|
$service->disconnect();
|
|
|
|
$this->session->unset_userdata('provider');
|
|
|
|
//Display the profile data
|
|
echo 'Name: ' . $profile->displayName;
|
|
print_r($profile);
|
|
exit();
|
|
}
|
|
else
|
|
{
|
|
$this->session->set_flashdata('showmsg', array('msg' => 'Sorry! We couldn\'t authenticate your identity.'));
|
|
}
|
|
}
|
|
}
|
|
catch(Exception $e)
|
|
{
|
|
if (isset($service) && $service->isConnected())
|
|
$service->disconnect();
|
|
|
|
$error = 'Sorry! We couldn\'t authenticate you.';
|
|
$this->session->set_flashdata('showmsg', array('msg' => $error));
|
|
$error .= '\nError Code: ' . $e->getCode();
|
|
$error .= '\nError Message: ' . $e->getMessage();
|
|
|
|
log_message('error', $error);
|
|
}
|
|
|
|
//redirect();
|
|
}
|
|
|
|
|
|
}
|