Files
WrenchBoradWeb/www/application/controllers/Home.php
T
2022-03-10 22:22:54 -05:00

178 lines
5.9 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
//Include Hybridauth autoloader
require APPPATH . '/third_party/hybridauth/autoload.php';
//Import Hybridauth's namespace
use Hybridauth\Hybridauth;
class Home extends WRB_Controller {
function __construct()
{
parent::__construct();
//Load URL helper
$this->load->helper('url');
//Load session library
$this->load->library('session');
}
//Displays social login links
public function index() {
//Instantiate Hybridauth's classes
$hybrid = new Hybridauth($this->getHybridConfig());
//Get enabled providers array
$providers = $hybrid->getProviders();
$login_links = "";
$provider_icons = array(
'Apple' => '/site3/assets/media/svg/brand-logos/apple-black.svg',
'Facebook' => '/site3/assets/media/svg/brand-logos/facebook-4.svg',
'Google' => '/site3/assets/media/svg/brand-logos/google-icon.svg'
);
//List a link to login
foreach ($providers as $provider)
{
$href = sprintf(base_url('%s/auth/%s/') , strtolower($this->router->fetch_class()) , $provider);
$login_links .= '<a href="' . $href . '" class="btn btn-flex flex-center btn-light btn-lg w-100 mb-5">';
if (array_key_exists($provider, $provider_icons)) {
$login_links .= '<img alt="Logo" src="' . $provider_icons[$provider] . '" class="h-20px me-3">';
}
$login_links .= 'Continue with ' . $provider . '</a>';
}
$data['login_links'] =''; // $login_links;
//$this->home1('');
$this->load->view('site3/external/view_home', $data);
}
public function about() {
$this->home2('home2/about');
}
//Processes social login
function auth($provider = NULL)
{
$service = NULL;
try
{
//Instantiate Hybridauth's classes
$hybrid = new Hybridauth($this->getHybridConfig());
//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);
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);
}
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();
}
//Hybridauth configuration
private function getHybridConfig()
{
$config = array(
'callback' => site_url('social/auth/') ,
'providers' => array(
'Google' => array(
'enabled' => true,
'keys' => array(
'id' => 'YOUR_CLIENT_ID',
'secret' => 'YOUR_CLIENT_SECRET'
) ,
'scope' => 'https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile'
) ,
'Facebook' => array(
'enabled' => true,
'keys' => array(
'id' => (ENVIRONMENT == 'development') ? '390204307987009' : '390204307987009',
'secret' => (ENVIRONMENT == 'development') ? '19f778e312f2ab96d147bacb612910c2' : '19f778e312f2ab96d147bacb612910c2'
) ,
'scope' => 'email, public_profile'
) ,
'Apple' => array(
"enabled" => true,
"keys" => [
"id" => "Your Apple ID",
"team_id" => "Your Apple team id",
"key_id" => "Your Apple key id",
"key_content" => "Your Apple key (content including BEGIN and END lines)",
"key_file" => "Full path to your Apple key file (alternative to key_content)"
],
"scope" => "name email",
"verifyTokenSignature" => true
)
) ,
'hybrid_debug' => array(
'debug_mode' => 'info', /* none, debug, info, error */
'debug_file' => APPPATH . '/logs/log-' . date('Y-m-d') . '.php'
)
);
return $config;
}
}