<?php 
/**
* @version		$Id: gmail.auth.php 2569 2024-05-02 18:34:42Z IOS $
* @package		Elxis
* @subpackage	Component User / Authentication
* @copyright	Copyright (c) 2006-2024 Elxis CMS (https://www.elxis.org). All rights reserved.
* @license		Elxis Public License ( https://www.elxis.org/elxis-public-license.html )
* @author		Elxis Team ( https://www.elxis.org )
* @description 	Elxis CMS is free software. Read the license for copyright notices and details
*/

defined('_ELXIS_') or die ('Direct access to this location is not allowed');


class gmailAuthentication {


	/********************/
	/* MAGIC CONTRUCTOR */
	/********************/
	public function __construct($params) {
	}


	/*****************************/
	/* AUTHENTICATE A GMAIL USER */
	/*****************************/
	public function authenticate(&$response, $options=array()) {
		$elxis = eFactory::getElxis();

		$uname = (isset($options['uname'])) ? trim($options['uname']) : '';
		$pword = (isset($options['pword'])) ? trim($options['pword']) : '';
		if ($uname == '') {
			$eLang = eFactory::getLang();
			$response->errormsg = sprintf($eLang->get('FIELDNOEMPTY'), $eLang->get('EMAIL'));
			return false;
		}
		if ($pword == '') {
			$eLang = eFactory::getLang();
			$response->errormsg = sprintf($eLang->get('FIELDNOEMPTY'), $eLang->get('PASSWORD'));
			return false;
		}
		if (strlen($pword) < 4) {
			$response->errormsg = eFactory::getLang()->get('PASSTOOSHORT');
			return false;
		}

		if (!function_exists('curl_init')) {
			$response->errormsg = 'CURL is not supported by the web server!';
			return false;
		}

		//remove ascii chars 0-31
		$uname_san = eUTF::trim(filter_var($uname, FILTER_SANITIZE_EMAIL));
		$pword_san = eUTF::trim($elxis->sanitizeVar($pword, true, false));
		if ($uname !== $uname_san) {
			$eLang = eFactory::getLang();
			$response->errormsg = sprintf($eLang->get('FIELDNOACCCHAR'), $eLang->get('EMAIL'));
			return false;
		}
		if ($pword !== $pword_san) {
			$eLang = eFactory::getLang();
			$response->errormsg = sprintf($eLang->get('FIELDNOACCCHAR'), $eLang->get('PASSWORD'));
			return false;
		}

		if (eFactory::getElxis()->getConfig('TWOFA') != '') {
			$response->errormsg = '2 Factor authentication is not supported for GMail login!';
			return false;
		}

		$curl = curl_init('https://mail.google.com/mail/feed/atom');
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
		curl_setopt($curl, CURLOPT_HEADER, 0);
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($curl, CURLOPT_USERPWD, $uname.':'.$pword);
		$result = @curl_exec($curl);
		$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
		$result = false;
		switch($code) {
			case 200: $result = true; break;
			case 401: $response->errormsg = eFactory::getLang()->get('AUTHFAILED'); break;
			default: $response->errormsg = eFactory::getLang()->get('AUTHFAILED'); break;
		}

		if ($result == true) {
			$parts = preg_split('/\@/', $uname, 2, PREG_SPLIT_NO_EMPTY);
			$response->uname = $parts[0];
			$response->email = $uname;
		}

		return $result;
	}


	/**********************************************/
	/* MAKE ONE TIME PASSWORD (VERIFICATION CODE) */
	/**********************************************/
	public function makeOTP($options=array()) {
		$iresponse = array('success' => false, 'errormsg' => '', 'firstname' => '', 'lastname' => '', 'uname' => '', 'gid' => '', 'email' => '', 'mobile' => '', 'otp' => '');

		$iresponse['errormsg'] = '2 factor authentication is not supported by GMAIL login!';
		return $iresponse;
	}


	/*************************/
	/* SHOW GMAIL LOGIN FORM */
	/*************************/
	public function loginForm() {
		$eLang = eFactory::getLang();
		$elxis = eFactory::getElxis();
		$eDoc = eFactory::getDocument();
		$eAuth = eRegistry::get('eAuth');

		$action = $elxis->makeURL('user:login/gmail.html', '', true, false);
		$return = base64_encode($elxis->makeURL('user:/'));
		elxisLoader::loadFile('includes/libraries/elxis/form5.class.php');

		$data = $eAuth->getAuthData('gmail');
		$title = sprintf($eLang->get('LOGIN_WITH'), $data['title']);
		$eDoc->setTitle($eLang->get('LOGIN').' - '.$data['title']);
		$eDoc->setDescription($title);

		$form = new elxis5Form(array('idprefix' => 'ulog', 'labelclass' => 'elx5_labelblock', 'sideclass' => 'elx5_zero'));
		$form->openForm(array('name' => 'fmuserlogin', 'method' => 'post', 'action' => $action, 'id' => 'fmuserlogin'));
		$form->openFieldset($title);
		$form->addEmail('uname', '', $eLang->get('EMAIL'), array('required' => 'required', 'autocomplete' => 'off'));
		$form->addPassword('pword', '', $eLang->get('PASSWORD'), array('required' => 'required', 'maxlength' => 60, 'autocomplete' => 'off'));
		$form->addHidden('return', $return);
		$form->addHidden('auth_method', 'gmail');
		$form->addToken('fmuserlogin');
		$form->addHTML('<div class="elx5_dspace">');
		$form->addButton('sbmlog', $eLang->get('LOGIN'), 'submit');
		$form->addHTML('</div>');
		$form->closeFieldset();
		$form->closeForm();
		unset($form);
	}


	/***********************/
	/* EXECUTE CUSTOM TASK */
	/***********************/
	public function runTask($etask) {
		if (ob_get_length() > 0) { @ob_end_clean(); }
		header('content-type:text/plain; charset=utf-8');
		echo 'Invalid request';
		exit;
	}


	/***************************************/
	/* CUSTOM ACTIONS TO PERFORM ON LOGOUT */
	/***************************************/
	public function logout() {
	}

}

?>