2023-02-25 05:02:27 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace EesyPHP\Auth;
|
|
|
|
|
|
|
|
use EesyPHP\App;
|
|
|
|
use EesyPHP\Auth;
|
|
|
|
use EesyPHP\Hook;
|
2023-02-25 17:18:51 +01:00
|
|
|
use EesyPHP\I18n;
|
2023-02-25 05:02:27 +01:00
|
|
|
use EesyPHP\Url;
|
|
|
|
use EesyPHP\Tpl;
|
|
|
|
|
|
|
|
class Form extends Method {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
Url :: add_url_handler('#^login$#', array('EesyPHP\\Auth\\Form', 'handle_login'), null, false);
|
|
|
|
Hook :: register('logged_in', array('\\EesyPHP\\Auth\\Form', 'logged_in_hook'));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log user
|
|
|
|
* @param bool $force Force user authentication
|
|
|
|
* @return \EesyPHP\Auth\User|null
|
|
|
|
*/
|
|
|
|
public static function login($force=false) {
|
|
|
|
$user = null;
|
|
|
|
if (isset($_REQUEST['username']) && isset($_REQUEST['password'])) {
|
|
|
|
$user = Auth :: authenticate($_REQUEST['username'], $_REQUEST['password']);
|
|
|
|
if (!$user) Tpl::add_error(_('Invalid username or password.'));
|
|
|
|
}
|
|
|
|
if ($force && !$user) {
|
|
|
|
if (Url :: get_current_url() != 'login')
|
|
|
|
Url :: redirect('login?next='.urlencode(Url :: get_current_url()));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The login form view
|
|
|
|
* @param \EesyPHP\UrlRequest $request
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function handle_login($request) {
|
|
|
|
$user = Auth :: login(false, 'Form');
|
|
|
|
$display_other_methods = array();
|
|
|
|
foreach (App::get('auth.login_form.display_other_methods', array(), 'array') as $method => $label)
|
|
|
|
if (Auth::method_is_enabled($method))
|
|
|
|
$display_other_methods[$method] = $label;
|
|
|
|
if (
|
|
|
|
!$user && isset($_REQUEST['method']) &&
|
|
|
|
array_key_exists($_REQUEST['method'], $display_other_methods)
|
|
|
|
) {
|
|
|
|
$user = Auth :: login($_REQUEST['method'], $_REQUEST['method']);
|
|
|
|
}
|
|
|
|
if ($user)
|
|
|
|
Url :: redirect(isset($_REQUEST['next'])?urldecode($_REQUEST['next']):null);
|
|
|
|
else
|
|
|
|
Tpl :: assign('next', (isset($_REQUEST['next'])?urldecode($_REQUEST['next']):''));
|
|
|
|
Tpl :: assign('display_other_methods', $display_other_methods);
|
2023-02-25 17:18:51 +01:00
|
|
|
Tpl :: display('login.tpl', I18n::_('Sign in'));
|
2023-02-25 05:02:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logged in hook
|
|
|
|
* @param \EesyPHP\HookEvent $event
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function logged_in_hook($event) {
|
|
|
|
if ($event->method == 'Form' && isset($_REQUEST['next']))
|
|
|
|
Url :: redirect(urldecode($_REQUEST['next']));
|
|
|
|
}
|
|
|
|
}
|