You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1289 lines
30 KiB
1289 lines
30 KiB
7 years ago
|
<?php
|
||
7 years ago
|
/**
|
||
7 years ago
|
* CodeIgniter-Aauth
|
||
|
*
|
||
7 years ago
|
* Aauth is a User Authorization Library for CodeIgniter 4.x, which aims to make
|
||
|
* easy some essential jobs such as login, permissions and access operations.
|
||
|
* Despite ease of use, it has also very advanced features like groupping,
|
||
|
* access management, public access etc..
|
||
|
*
|
||
7 years ago
|
* @package CodeIgniter-Aauth
|
||
|
* @author Magefly Team
|
||
|
* @copyright 2014-2017 Emre Akay
|
||
|
* @copyright 2018 Magefly
|
||
|
* @license https://opensource.org/licenses/MIT MIT License
|
||
|
* @link https://github.com/magefly/CodeIgniter-Aauth
|
||
7 years ago
|
*/
|
||
7 years ago
|
|
||
|
namespace App\Libraries;
|
||
7 years ago
|
|
||
6 years ago
|
use \App\Models\Aauth\UserModel;
|
||
|
use \App\Models\Aauth\LoginAttemptModel;
|
||
|
use \App\Models\Aauth\LoginTokenModel;
|
||
|
use \App\Models\Aauth\UserVariableModel;
|
||
7 years ago
|
|
||
|
/**
|
||
|
* Aauth Library
|
||
|
*
|
||
|
* @package CodeIgniter-Aauth
|
||
|
*/
|
||
7 years ago
|
class Aauth
|
||
|
{
|
||
|
/**
|
||
|
* Variable for loading the config array into
|
||
|
*
|
||
7 years ago
|
* @var \Config\Aauth
|
||
7 years ago
|
*/
|
||
7 years ago
|
protected $config;
|
||
7 years ago
|
|
||
|
/**
|
||
|
* Variable for loading the session service into
|
||
|
*
|
||
7 years ago
|
* @var \CodeIgniter\Session\Session
|
||
7 years ago
|
*/
|
||
7 years ago
|
protected $session;
|
||
7 years ago
|
|
||
|
/**
|
||
|
* Array to store error messages
|
||
|
*
|
||
|
* @var array
|
||
|
*/
|
||
7 years ago
|
protected $errors = [];
|
||
7 years ago
|
|
||
|
/**
|
||
|
* Local temporary storage for current flash errors
|
||
|
*
|
||
|
* Used to update current flash data list since flash data is only available on the next page refresh
|
||
|
*
|
||
|
* @var array
|
||
|
*/
|
||
7 years ago
|
protected $flashErrors = [];
|
||
7 years ago
|
|
||
|
/**
|
||
|
* Array to store info messages
|
||
|
*
|
||
|
* @var array
|
||
|
*/
|
||
7 years ago
|
protected $infos = [];
|
||
7 years ago
|
|
||
|
/**
|
||
|
* Local temporary storage for current flash infos
|
||
|
*
|
||
|
* Used to update current flash data list since flash data is only available on the next page refresh
|
||
|
*
|
||
|
* @var array
|
||
|
*/
|
||
7 years ago
|
protected $flashInfos = [];
|
||
7 years ago
|
|
||
|
/**
|
||
|
* Array to cache permission-ids.
|
||
|
*
|
||
|
* @var array
|
||
|
*/
|
||
7 years ago
|
protected $cachePermIds = [];
|
||
7 years ago
|
|
||
|
/**
|
||
|
* Array to cache group-ids.
|
||
|
*
|
||
|
* @var array
|
||
|
*/
|
||
7 years ago
|
protected $cacheGroupIds = [];
|
||
7 years ago
|
|
||
|
/**
|
||
|
* Constructor
|
||
|
*
|
||
|
* Prepares config & session variable.
|
||
|
*/
|
||
6 years ago
|
public function __construct($config = null, $session = null)
|
||
7 years ago
|
{
|
||
6 years ago
|
if (is_null($config))
|
||
|
{
|
||
6 years ago
|
$config = new \Config\Aauth();
|
||
6 years ago
|
}
|
||
|
|
||
|
if (is_null($session))
|
||
|
{
|
||
|
$session = \Config\Services::session();
|
||
|
}
|
||
6 years ago
|
|
||
|
$this->config = $config;
|
||
6 years ago
|
$this->session = $session;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
//--------------------------------------------------------------------------
|
||
|
// Login Functions
|
||
|
//--------------------------------------------------------------------------
|
||
7 years ago
|
|
||
7 years ago
|
/**
|
||
6 years ago
|
* Login user
|
||
7 years ago
|
*
|
||
6 years ago
|
* Check provided details against the database. Add items to error array on fail
|
||
7 years ago
|
*
|
||
6 years ago
|
* @param string $identifier Identifier
|
||
|
* @param string $password Password
|
||
|
* @param boolean $remember Whether to remember login
|
||
|
* @param string $totpCode TOTP Code
|
||
7 years ago
|
*
|
||
6 years ago
|
* @return boolean
|
||
7 years ago
|
*/
|
||
6 years ago
|
public function login(string $identifier, string $password, bool $remember = null, string $totpCode = null)
|
||
7 years ago
|
{
|
||
6 years ago
|
helper('cookie');
|
||
|
delete_cookie('remember');
|
||
7 years ago
|
|
||
6 years ago
|
$userModel = new UserModel();
|
||
|
$loginAttemptModel = new LoginAttemptModel();
|
||
|
$userVariableModel = new UserVariableModel();
|
||
7 years ago
|
|
||
6 years ago
|
if ($this->config->loginProtection && ! $loginAttemptModel->save())
|
||
7 years ago
|
{
|
||
6 years ago
|
$this->error(lang('Aauth.loginAttemptsExceeded'));
|
||
7 years ago
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
6 years ago
|
// if ($this->config->ddos_protection && $this->config->recaptcha_active && $loginAttempts->get() > $this->config->recaptcha_login_attempts){
|
||
|
// $this->CI->load->helper('recaptchalib');
|
||
|
// $reCaptcha = new ReCaptcha( $this->config->recaptcha_secret);
|
||
|
// $resp = $reCaptcha->verifyResponse( $this->CI->input->server("REMOTE_ADDR"), $this->CI->input->post("g-recaptcha-response") );
|
||
|
// if( ! $resp->success){
|
||
|
// $this->error(lang('Aauth.aauth_error_recaptcha_not_correct'));
|
||
|
// return false;
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
if ($this->config->loginUseUsername)
|
||
7 years ago
|
{
|
||
6 years ago
|
if (! $identifier || strlen($password) < $this->config->passwordMin || strlen($password) > $this->config->passwordMax)
|
||
|
{
|
||
|
$this->error(lang('Aauth.loginFailedUsername'));
|
||
7 years ago
|
|
||
6 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
if (! $user = $userModel->where('username', $identifier)->first())
|
||
|
{
|
||
|
$this->error(lang('Aauth.notFoundUser'));
|
||
|
|
||
|
return false;
|
||
|
}
|
||
7 years ago
|
}
|
||
6 years ago
|
else
|
||
|
{
|
||
|
$validation = \Config\Services::validation();
|
||
7 years ago
|
|
||
6 years ago
|
if (! $validation->check($identifier, 'valid_email') || strlen($password) < $this->config->passwordMin || strlen($password) > $this->config->passwordMax)
|
||
|
{
|
||
|
$this->error(lang('Aauth.loginFailedEmail'));
|
||
7 years ago
|
|
||
6 years ago
|
return false;
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
if (! $user = $userModel->where('email', $identifier)->first())
|
||
|
{
|
||
|
$this->error(lang('Aauth.notFoundUser'));
|
||
7 years ago
|
|
||
6 years ago
|
return false;
|
||
|
}
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
if (! empty($userVariableModel->find($user['id'], 'verification_code', true)))
|
||
|
{
|
||
|
$this->error(lang('Aauth.notVerified'));
|
||
7 years ago
|
return false;
|
||
|
}
|
||
6 years ago
|
else if ($user['banned'])
|
||
7 years ago
|
{
|
||
6 years ago
|
$this->error(lang('Aauth.invalidUserBanned'));
|
||
7 years ago
|
return false;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
// if ($this->config->totpEnabled && ! $this->config->totpOnIpChange && $this->config->totpLogin)
|
||
|
// {
|
||
|
// if ($this->config->totpLogin == true)
|
||
|
// {
|
||
|
// $this->session->set('totp_required', true);
|
||
|
// }
|
||
7 years ago
|
|
||
6 years ago
|
// $totp_secret = $userVariableModel->find($user['id'], 'totp_secret', true);
|
||
|
// if ( ! empty($totp_secret) && ! $totp_code) {
|
||
|
// $this->error(lang('Aauth.requiredTOTPCode'));
|
||
|
// return false;
|
||
|
// } else {
|
||
|
// if( ! empty($totp_secret)){
|
||
|
// $this->CI->load->helper('googleauthenticator');
|
||
|
// $ga = new PHPGangsta_GoogleAuthenticator();
|
||
|
// $checkResult = $ga->verifyCode($totp_secret, $totp_code, 0);
|
||
|
// if ( ! $checkResult) {
|
||
|
// $this->error(lang('Aauth.invalidTOTPCode'));
|
||
|
// return false;
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
// else if ($this->config->totpEnabled && $this->config->totpOnIpChange)
|
||
|
// {
|
||
|
// $query = null;
|
||
|
// $query = $this->aauth_db->where($db_identifier, $identifier);
|
||
|
// $query = $this->aauth_db->get($this->config->users);
|
||
|
// $totp_secret = $query->row()->totp_secret;
|
||
|
// $ip_address = $query->row()->ip_address;
|
||
|
// $current_ip_address = $this->CI->input->ip_address();
|
||
|
// if ($query->num_rows() > 0 AND !$totp_code) {
|
||
|
// if($ip_address != $current_ip_address ){
|
||
|
// if($this->config->totpLogin == false){
|
||
|
// $this->error(lang('Aauth.aauth_error_totp_code_required'));
|
||
|
// return false;
|
||
|
// } else if($this->config->totpLogin == true){
|
||
|
// $this->session->set('totp_required', true);
|
||
|
// }
|
||
|
// }
|
||
|
// }else {
|
||
|
// if(!empty($totp_secret)){
|
||
|
// if($ip_address != $current_ip_address ){
|
||
|
// $this->CI->load->helper('googleauthenticator');
|
||
|
// $ga = new PHPGangsta_GoogleAuthenticator();
|
||
|
// $checkResult = $ga->verifyCode($totp_secret, $totp_code, 0);
|
||
|
// if (!$checkResult) {
|
||
|
// $this->error(lang('Aauth.aauth_error_totp_code_invalid'));
|
||
|
// return false;
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
7 years ago
|
|
||
6 years ago
|
if (password_verify($password, $user['password']))
|
||
7 years ago
|
{
|
||
6 years ago
|
$data['id'] = $user['id'];
|
||
|
$data['username'] = $user['username'];
|
||
|
$data['email'] = $user['email'];
|
||
|
$data['loggedIn'] = true;
|
||
|
$this->session->set('user', $data);
|
||
7 years ago
|
|
||
6 years ago
|
if ($remember)
|
||
|
{
|
||
|
helper('text');
|
||
|
$loginTokenModel = new LoginTokenModel();
|
||
|
$expire = $this->config->loginRemember;
|
||
|
$userId = base64_encode($user['id']);
|
||
|
$randomString = random_string('alnum', 32);
|
||
|
$selectorString = random_string('alnum', 16);
|
||
7 years ago
|
|
||
6 years ago
|
$cookieData['name'] = 'remember';
|
||
|
$cookieData['value'] = $userId . ';' . $randomString . ';' . $selectorString;
|
||
|
$cookieData['expire'] = YEAR;
|
||
|
|
||
|
$tokenData['user_id'] = $user['id'];
|
||
|
$tokenData['random_hash'] = password_hash($randomString, PASSWORD_DEFAULT);
|
||
|
$tokenData['selector_hash'] = password_hash($selectorString, PASSWORD_DEFAULT);
|
||
|
$tokenData['expires_at'] = date('Y-m-d H:i:s', strtotime($expire));
|
||
|
|
||
|
set_cookie($cookieData);
|
||
|
$loginTokenModel->insert($tokenData);
|
||
|
}
|
||
|
|
||
|
$userModel->updateLastLogin($user['id']);
|
||
|
|
||
|
if ($this->config->loginAttemptRemoveSuccessful)
|
||
|
{
|
||
|
$loginAttemptModel->delete();
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
return true;
|
||
7 years ago
|
}
|
||
6 years ago
|
else
|
||
|
{
|
||
|
$this->error(lang('Aauth.loginFailedAll'));
|
||
7 years ago
|
|
||
6 years ago
|
return false;
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
6 years ago
|
* Logout
|
||
7 years ago
|
*
|
||
6 years ago
|
* Deletes session and cookie
|
||
7 years ago
|
*
|
||
6 years ago
|
* @return void
|
||
7 years ago
|
*/
|
||
6 years ago
|
public function logout()
|
||
7 years ago
|
{
|
||
6 years ago
|
helper('cookie');
|
||
|
set_cookie('remember', '', -3600);
|
||
|
$this->session->remove('user');
|
||
|
@$this->session->destroy();
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
6 years ago
|
* Fast login
|
||
7 years ago
|
*
|
||
6 years ago
|
* Login with just a user id
|
||
7 years ago
|
*
|
||
6 years ago
|
* @param integer $userId User id
|
||
7 years ago
|
*
|
||
6 years ago
|
* @return boolean
|
||
7 years ago
|
*/
|
||
6 years ago
|
protected function loginFast(int $userId)
|
||
7 years ago
|
{
|
||
|
$userModel = new UserModel();
|
||
6 years ago
|
$userModel->select('id, email, username');
|
||
|
$userModel->where('id', $userId);
|
||
|
$userModel->where('banned', 0);
|
||
6 years ago
|
|
||
6 years ago
|
if ($user = $userModel->get()->getFirstRow())
|
||
|
{
|
||
|
$this->session->set('user', [
|
||
|
'id' => $user->id,
|
||
|
'username' => $user->username,
|
||
|
'email' => $user->email,
|
||
|
'loggedIn' => true,
|
||
|
]);
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------
|
||
|
// Access Functions
|
||
|
//--------------------------------------------------------------------------
|
||
|
|
||
|
/**
|
||
|
* Check user login
|
||
|
*
|
||
|
* Checks if user logged in, also checks remember.
|
||
|
*
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function isLoggedIn()
|
||
|
{
|
||
|
helper('cookie');
|
||
|
|
||
|
if (isset($this->session->get('user')['loggedIn']))
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
else if ($cookie = get_cookie('remember'))
|
||
|
{
|
||
|
$cookie = explode(';', $cookie);
|
||
|
$cookie[0] = base64_decode($cookie[0]);
|
||
|
|
||
|
if (! is_numeric($cookie[0]) || strlen($cookie[1]) !== 32 || strlen($cookie[2]) !== 16)
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$loginTokenModel = new LoginTokenModel();
|
||
|
$loginTokens = $loginTokenModel->findAllByUserId($cookie[0]);
|
||
|
|
||
|
foreach ($loginTokens as $loginToken)
|
||
|
{
|
||
|
if (password_verify($cookie[1], $loginToken['random_hash']) && password_verify($cookie[2], $loginToken['selector_hash']))
|
||
|
{
|
||
|
if (strtotime($loginToken['expires_at']) > strtotime('now'))
|
||
|
{
|
||
|
$loginTokenModel->update($loginToken['id']);
|
||
|
|
||
|
return $this->loginFast($loginToken['user_id']);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$loginTokenModel->deleteExpired($cookie[0]);
|
||
|
delete_cookie('remember');
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------
|
||
|
// User Functions
|
||
|
//--------------------------------------------------------------------
|
||
|
|
||
|
/**
|
||
|
* Create user
|
||
|
*
|
||
|
* Creates a new user
|
||
|
*
|
||
|
* @param string $email User's email address
|
||
|
* @param string $password User's password
|
||
|
* @param string|boolean $username User's username
|
||
|
*
|
||
|
* @return integer|boolean
|
||
|
*/
|
||
|
public function createUser(string $email, string $password, string $username = null)
|
||
|
{
|
||
|
$userModel = new UserModel();
|
||
|
|
||
|
$data['email'] = $email;
|
||
|
$data['password'] = $password;
|
||
|
|
||
|
if (! is_null($username))
|
||
|
{
|
||
|
$data['username'] = $username;
|
||
|
}
|
||
|
|
||
|
if (! $userId = $userModel->insert($data))
|
||
|
{
|
||
|
$this->error(array_values($userModel->errors()));
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
if ($this->config->userVerification)
|
||
|
{
|
||
|
$this->sendVerification($userId, $email);
|
||
|
$this->info(lang('Aauth.infoCreateVerification'));
|
||
|
|
||
|
return $userId;
|
||
|
}
|
||
|
|
||
|
$this->info(lang('Aauth.infoCreateSuccess'));
|
||
|
|
||
|
return $userId;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Update user
|
||
|
*
|
||
|
* Updates existing user details
|
||
|
*
|
||
|
* @param integer $userId User id to update
|
||
|
* @param string|boolean $email User's email address, or FALSE if not to be updated
|
||
|
* @param string|boolean $password User's password, or FALSE if not to be updated
|
||
|
* @param string|boolean $username User's name, or FALSE if not to be updated
|
||
|
*
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function updateUser(int $userId, $email = null, string $password = null, string $username = null)
|
||
|
{
|
||
|
$userModel = new UserModel();
|
||
|
|
||
|
if (! $userModel->existsById($userId))
|
||
|
{
|
||
|
$this->error(lang('Aauth.notFoundUser'));
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
else if (is_null($email) && is_null($password) && is_null($username))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
$data['id'] = $userId;
|
||
|
|
||
|
if (! is_null($email))
|
||
|
{
|
||
|
$data['email'] = $email;
|
||
|
}
|
||
|
|
||
|
if (! is_null($password))
|
||
|
{
|
||
|
$data['password'] = $password;
|
||
|
}
|
||
|
|
||
|
if (! is_null($username))
|
||
|
{
|
||
|
$data['username'] = $username;
|
||
|
}
|
||
|
|
||
|
if ($userModel->update($userId, $data))
|
||
|
{
|
||
|
$this->info(lang('Aauth.infoUpdateSuccess'));
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
$this->error(array_values($userModel->errors()));
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Delete user
|
||
|
*
|
||
|
* @param integer $userId User id to delete
|
||
|
*
|
||
|
* @return boolen
|
||
|
*/
|
||
|
public function deleteUser(int $userId)
|
||
|
{
|
||
|
$userModel = new UserModel();
|
||
|
|
||
|
if (! $userModel->existsById($userId))
|
||
|
{
|
||
|
$this->error(lang('Aauth.notFoundUser'));
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return $userModel->delete($userId);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* List users
|
||
|
*
|
||
|
* Return users as an object array
|
||
|
*
|
||
|
* @param integer $limit Limit of users to be returned
|
||
|
* @param integer $offset Offset for limited number of users
|
||
|
* @param boolean $includeBanneds Include banned users
|
||
|
* @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC')
|
||
|
*
|
||
|
* @return array Array of users
|
||
|
*/
|
||
|
public function listUsers(int $limit = 0, int $offset = 0, bool $includeBanneds = null, string $orderBy = null)
|
||
|
{
|
||
|
$userModel = new UserModel();
|
||
|
$user = $userModel->limit($limit, $offset);
|
||
|
|
||
|
$userModel->select('id, email, username, banned, created_at, updated_at, last_activity, last_ip_address, last_login');
|
||
|
// eanbool $group_par = null,
|
||
7 years ago
|
|
||
|
if (is_null($includeBanneds))
|
||
|
{
|
||
|
$user->where('banned', 0);
|
||
|
}
|
||
|
|
||
|
if (! is_null($orderBy))
|
||
|
{
|
||
6 years ago
|
$user->orderBy($orderBy);
|
||
7 years ago
|
}
|
||
|
|
||
|
return $user->findAll();
|
||
|
}
|
||
|
|
||
6 years ago
|
/**
|
||
|
* List users with paginate
|
||
|
*
|
||
|
* Return users as an object array
|
||
|
*
|
||
|
* @param integer $limit Limit of users to be returned
|
||
|
* @param integer $offset Offset for limited number of users
|
||
|
* @param boolean $includeBanneds Include banned users
|
||
|
* @param string $orderBy Order by MYSQL string (e.g. 'name ASC', 'email DESC')
|
||
|
*
|
||
|
* @return array Array of users
|
||
|
*/
|
||
|
public function listUsersPaginated(int $limit = 10, bool $includeBanneds = null, string $orderBy = null)
|
||
|
{
|
||
|
$userModel = new UserModel();
|
||
|
|
||
|
$userModel->select('id, email, username, banned, created_at, updated_at, last_activity, last_ip_address, last_login');
|
||
|
// eanbool $group_par = null,
|
||
|
|
||
|
if (is_null($includeBanneds))
|
||
|
{
|
||
|
$userModel->where('banned', 0);
|
||
|
}
|
||
|
|
||
|
if (! is_null($orderBy))
|
||
|
{
|
||
|
$userModel->orderBy($orderBy);
|
||
|
}
|
||
|
|
||
|
return [
|
||
6 years ago
|
'users' => $userModel->paginate($limit),
|
||
|
'pager' => $userModel->pager,
|
||
|
];
|
||
6 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Send verification email
|
||
|
*
|
||
|
* Sends a verification email based on user id
|
||
|
*
|
||
|
* @param integer $userId User id to send verification email to
|
||
|
* @param string $email Email to send verification email to
|
||
|
*
|
||
|
* @return boolean
|
||
|
*/
|
||
7 years ago
|
protected function sendVerification(int $userId, string $email)
|
||
7 years ago
|
{
|
||
|
helper('text');
|
||
|
$userVariableModel = new UserVariableModel();
|
||
|
$emailService = \Config\Services::email();
|
||
7 years ago
|
$verificationCode = sha1(strtotime('now'));
|
||
7 years ago
|
|
||
|
$userVariableModel->save($userId, 'verification_code', $verificationCode, true);
|
||
|
|
||
|
$messageData['code'] = $verificationCode;
|
||
7 years ago
|
$messageData['link'] = site_url($this->config->linkVerification . '/' . $userId . '/' . $verificationCode);
|
||
7 years ago
|
|
||
|
$emailService->initialize(isset($this->config->emailConfig) ? $this->config->emailConfig : []);
|
||
|
$emailService->setFrom($this->config->emailFrom, $this->config->emailFromName);
|
||
|
$emailService->setTo($email);
|
||
|
$emailService->setSubject(lang('Aauth.subjectVerification'));
|
||
|
$emailService->setMessage(view('Aauth/Verification', $messageData));
|
||
|
|
||
|
return $emailService->send();
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
7 years ago
|
* Verify user
|
||
7 years ago
|
*
|
||
7 years ago
|
* Activates user account based on verification code
|
||
7 years ago
|
*
|
||
7 years ago
|
* @param string $verificationCode Code to validate against
|
||
7 years ago
|
*
|
||
7 years ago
|
* @return boolean Activation fails/succeeds
|
||
7 years ago
|
*/
|
||
7 years ago
|
public function verifyUser(string $verificationCode)
|
||
7 years ago
|
{
|
||
7 years ago
|
$userVariableModel = new UserVariableModel();
|
||
7 years ago
|
$userVariable = [
|
||
|
'data_key' => 'verification_code',
|
||
|
'data_value' => $verificationCode,
|
||
|
'system' => 1,
|
||
|
];
|
||
7 years ago
|
|
||
7 years ago
|
if ($verificationCodeStored = $userVariableModel->where($userVariable)->first())
|
||
7 years ago
|
{
|
||
7 years ago
|
$userVariableModel->delete($verificationCodeStored['user_id'], 'verification_code', true);
|
||
|
$this->info(lang('Aauth.infoVerification'));
|
||
7 years ago
|
|
||
7 years ago
|
return true;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
$this->error(lang('Aauth.invalidVerficationCode'));
|
||
|
|
||
7 years ago
|
return false;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Get user
|
||
|
*
|
||
|
* Get user information
|
||
|
*
|
||
7 years ago
|
* @param integer|boolean $userId User id to get or FALSE for current user
|
||
|
* @param boolean $withVariables Whether to get user variables
|
||
|
* @param boolean $inclSystem Whether to get system user variables
|
||
7 years ago
|
*
|
||
|
* @return object|boolean User information or false if user not found
|
||
|
*/
|
||
7 years ago
|
public function getUser($userId = null, bool $withVariables = false, bool $inclSystem = false)
|
||
7 years ago
|
{
|
||
7 years ago
|
$userModel = new UserModel();
|
||
|
$userVariableModel = new UserVariableModel();
|
||
7 years ago
|
|
||
6 years ago
|
$userModel->select('id, email, username, banned, created_at, updated_at, last_activity, last_ip_address, last_login');
|
||
|
|
||
7 years ago
|
if (! $userId)
|
||
7 years ago
|
{
|
||
6 years ago
|
$userId = $this->session->user['id'];
|
||
7 years ago
|
}
|
||
|
|
||
|
if ($user = $userModel->find($userId))
|
||
|
{
|
||
7 years ago
|
if ($withVariables)
|
||
|
{
|
||
|
$variables = $userVariableModel->select('data_key, data_value' . ($inclSystem ? ', system' : ''));
|
||
|
$variables = $variables->findAll($userId, $inclSystem);
|
||
|
|
||
|
$user['variables'] = $variables;
|
||
|
}
|
||
|
|
||
7 years ago
|
return $user;
|
||
|
}
|
||
|
|
||
|
$this->error(lang('Aauth.notFoundUser'));
|
||
7 years ago
|
|
||
7 years ago
|
return false;
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Get user id
|
||
|
*
|
||
|
* Get user id from email address, if par. not given, return current user's id
|
||
|
*
|
||
6 years ago
|
* @param string|boolean $email Email address for user,
|
||
7 years ago
|
*
|
||
|
* @return object|boolean User information or false if user not found
|
||
|
*/
|
||
|
public function getUserId($email = null)
|
||
|
{
|
||
|
$userModel = new UserModel();
|
||
|
|
||
|
if (! $email)
|
||
|
{
|
||
6 years ago
|
$where = ['id' => $this->session->user['id']];
|
||
7 years ago
|
}
|
||
|
else
|
||
|
{
|
||
|
$where = ['email' => $email];
|
||
|
}
|
||
|
|
||
|
if ($user = $userModel->where($where)->first())
|
||
|
{
|
||
7 years ago
|
return $user['id'];
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
return false;
|
||
|
}
|
||
|
|
||
6 years ago
|
/**
|
||
|
* Is banned
|
||
|
*
|
||
6 years ago
|
* @param integer $userId User id, can be null to use session user
|
||
6 years ago
|
*
|
||
|
* @return boolean
|
||
|
*/
|
||
6 years ago
|
public function isBanned(int $userId = null)
|
||
6 years ago
|
{
|
||
|
$userModel = new UserModel();
|
||
|
|
||
|
if (! $userId)
|
||
|
{
|
||
6 years ago
|
$userId = $this->session->user['id'];
|
||
6 years ago
|
}
|
||
|
|
||
|
if (! $userModel->existsById($userId))
|
||
|
{
|
||
6 years ago
|
return true;
|
||
6 years ago
|
}
|
||
|
|
||
|
return $userModel->isBanned($userId);
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Ban User
|
||
|
*
|
||
6 years ago
|
* @param integer $userId User id, can be null to use session user
|
||
7 years ago
|
*
|
||
|
* @return boolean
|
||
|
*/
|
||
6 years ago
|
public function banUser(int $userId = null)
|
||
7 years ago
|
{
|
||
|
$userModel = new UserModel();
|
||
|
|
||
|
if (! $userId)
|
||
|
{
|
||
6 years ago
|
$userId = $this->session->user['id'];
|
||
7 years ago
|
}
|
||
|
|
||
|
if (! $userModel->existsById($userId))
|
||
|
{
|
||
|
$this->error(lang('Aauth.notFoundUser'));
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return $userModel->updateBanned($userId, 1);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Unban User
|
||
|
*
|
||
6 years ago
|
* @param integer $userId User id, can be null to use session user
|
||
7 years ago
|
*
|
||
|
* @return boolean
|
||
|
*/
|
||
6 years ago
|
public function unbanUser(int $userId = null)
|
||
7 years ago
|
{
|
||
|
$userModel = new UserModel();
|
||
|
|
||
|
if (! $userId)
|
||
|
{
|
||
6 years ago
|
$userId = $this->session->user['id'];
|
||
7 years ago
|
}
|
||
|
|
||
|
if (! $userModel->existsById($userId))
|
||
|
{
|
||
|
$this->error(lang('Aauth.notFoundUser'));
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
return $userModel->updateBanned($userId, 0);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Remind password
|
||
|
*
|
||
|
* Emails user with link to reset password
|
||
|
*
|
||
|
* @param string $email Email for account to remind
|
||
|
*
|
||
|
* @return boolean Remind fails/succeeds
|
||
|
*/
|
||
|
public function remindPassword(string $email)
|
||
|
{
|
||
|
$userModel = new UserModel();
|
||
|
if ($user = $userModel->where('email', $email)->first())
|
||
|
{
|
||
|
$userVariableModel = new UserVariableModel();
|
||
|
$emailService = \Config\Services::email();
|
||
|
$resetCode = sha1(strtotime('now'));
|
||
7 years ago
|
$userVariableModel->save($user['id'], 'verification_code', $resetCode, true);
|
||
7 years ago
|
|
||
|
$messageData['code'] = $resetCode;
|
||
7 years ago
|
$messageData['link'] = site_url($this->config->linkResetPassword . '/' . $resetCode);
|
||
7 years ago
|
|
||
|
$emailService->initialize(isset($this->config->emailConfig) ? $this->config->emailConfig : []);
|
||
|
$emailService->setFrom($this->config->emailFrom, $this->config->emailFromName);
|
||
7 years ago
|
$emailService->setTo($user['email']);
|
||
7 years ago
|
$emailService->setSubject(lang('Aauth.subjectReset'));
|
||
7 years ago
|
$emailService->setMessage(view('Aauth/RemindPassword', $messageData));
|
||
|
|
||
|
if ($email = $emailService->send())
|
||
|
{
|
||
|
$this->info(lang('Aauth.infoRemindSuccess'));
|
||
|
|
||
|
return $email;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$this->error(explode('<br />', $emailService->printDebugger([])));
|
||
7 years ago
|
|
||
7 years ago
|
return false;
|
||
|
}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
$this->error(lang('Aauth.notFoundUser'));
|
||
|
|
||
7 years ago
|
return false;
|
||
|
}
|
||
6 years ago
|
|
||
7 years ago
|
/**
|
||
|
* Reset password
|
||
|
*
|
||
|
* Generate new password and email it to the user
|
||
|
*
|
||
|
* @param string $resetCode Verification code for account
|
||
|
*
|
||
|
* @return boolean Password reset fails/succeeds
|
||
|
*/
|
||
|
public function resetPassword(string $resetCode)
|
||
|
{
|
||
|
$userVariableModel = new UserVariableModel();
|
||
|
$variable = [
|
||
|
'data_key' => 'verification_code',
|
||
|
'data_value' => $resetCode,
|
||
|
'system' => 1,
|
||
|
];
|
||
|
|
||
|
if ($userVariable = $userVariableModel->where($variable)->first())
|
||
|
{
|
||
|
helper('text');
|
||
|
$userModel = new UserModel();
|
||
|
$password = random_string('alnum', $this->config->passwordMin);
|
||
|
|
||
|
if ($user = $userModel->find($userVariable['user_id']))
|
||
|
{
|
||
|
$emailService = \Config\Services::email();
|
||
|
|
||
|
$data['id'] = $user['id'];
|
||
|
$data['password'] = $password;
|
||
|
|
||
|
$userModel->update($user['id'], $data);
|
||
|
$userVariableModel->delete($user['id'], 'verification_code', true);
|
||
|
|
||
|
if ($this->config->totpEnabled && $this->config->totpResetPassword)
|
||
|
{
|
||
|
$userVariableModel->delete($user['id'], 'totp_secret', true);
|
||
|
}
|
||
|
|
||
|
$messageData['password'] = $password;
|
||
|
|
||
|
$emailService->initialize(isset($this->config->emailConfig) ? $this->config->emailConfig : []);
|
||
|
$emailService->setFrom($this->config->emailFrom, $this->config->emailFromName);
|
||
|
$emailService->setTo($user['email']);
|
||
|
$emailService->setSubject(lang('Aauth.subjectResetSuccess'));
|
||
7 years ago
|
$emailService->setMessage(view('Aauth/ResetPassword', $messageData));
|
||
7 years ago
|
|
||
7 years ago
|
if ($email = $emailService->send())
|
||
|
{
|
||
|
$this->info(lang('Aauth.infoResetSuccess'));
|
||
|
|
||
|
return $email;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$this->error(explode('<br />', $emailService->printDebugger([])));
|
||
|
|
||
|
return false;
|
||
|
}
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
$this->error(lang('Aauth.invalidVerficationCode'));
|
||
|
|
||
7 years ago
|
return false;
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
6 years ago
|
* Set User Variable as key value
|
||
|
* if variable not set before, it will ve set
|
||
|
* if set, overwrites the value
|
||
7 years ago
|
*
|
||
6 years ago
|
* @param string $key
|
||
|
* @param string $value
|
||
|
* @param integer $userId User id, can be null to use session user
|
||
7 years ago
|
*
|
||
7 years ago
|
* @return boolean
|
||
7 years ago
|
*/
|
||
6 years ago
|
public function setUserVar(string $key, string $value, int $userId = null)
|
||
7 years ago
|
{
|
||
6 years ago
|
if (! $userId)
|
||
7 years ago
|
{
|
||
6 years ago
|
$userId = $this->session->user['id'];
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
$userModel = new UserModel();
|
||
7 years ago
|
|
||
6 years ago
|
if (! $userModel->existsById($userId))
|
||
7 years ago
|
{
|
||
6 years ago
|
return false;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
$userVariableModel = new UserVariableModel();
|
||
7 years ago
|
|
||
6 years ago
|
return $userVariableModel->save($userId, $key, $value);
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
/**
|
||
|
* Unset User Variable as key value
|
||
|
*
|
||
|
* @param string $key
|
||
|
* @param integer $userId User id, can be null to use session user
|
||
|
*
|
||
|
* @return boolean
|
||
|
*/
|
||
|
public function unsetUserVar(string $key, int $userId = null)
|
||
|
{
|
||
|
if (! $userId)
|
||
7 years ago
|
{
|
||
6 years ago
|
$userId = $this->session->user['id'];
|
||
7 years ago
|
}
|
||
6 years ago
|
|
||
|
$userModel = new UserModel();
|
||
|
|
||
|
if (! $userModel->existsById($userId))
|
||
7 years ago
|
{
|
||
|
return false;
|
||
|
}
|
||
|
|
||
6 years ago
|
$userVariableModel = new UserVariableModel();
|
||
7 years ago
|
|
||
6 years ago
|
return $userVariableModel->delete($userId, $key);
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
/**
|
||
|
* Get User Variable by key
|
||
|
*
|
||
|
* @param string $key Variable Key
|
||
|
* @param integer $userId User id, can be null to use session user
|
||
|
*
|
||
|
* @return boolean|string FALSE if var is not set, the value of var if set
|
||
|
*/
|
||
|
public function getUserVar(string $key, int $userId = null)
|
||
|
{
|
||
|
if (! $userId)
|
||
7 years ago
|
{
|
||
6 years ago
|
$userId = $this->session->user['id'];
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
$userModel = new UserModel();
|
||
7 years ago
|
|
||
6 years ago
|
if (! $userModel->existsById($userId))
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
7 years ago
|
|
||
6 years ago
|
$userVariableModel = new UserVariableModel();
|
||
7 years ago
|
|
||
6 years ago
|
if ($variable = $userVariableModel->find($user['id'], 'verification_code', true))
|
||
7 years ago
|
{
|
||
6 years ago
|
return $variable;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
return false;
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
6 years ago
|
* Get User Variables by user id
|
||
|
* Return array with all user keys & variables
|
||
7 years ago
|
*
|
||
6 years ago
|
* @param integer $user_id ; if not given current user
|
||
|
* @return boolean|array , FALSE if var is not set, the value of var if set
|
||
7 years ago
|
*/
|
||
6 years ago
|
public function getUserVars(int $userId = null)
|
||
7 years ago
|
{
|
||
6 years ago
|
if (! $userId)
|
||
|
{
|
||
|
$userId = $this->session->user['id'];
|
||
|
}
|
||
|
|
||
7 years ago
|
$userModel = new UserModel();
|
||
|
|
||
6 years ago
|
if (! $userModel->existsById($userId))
|
||
7 years ago
|
{
|
||
6 years ago
|
return false;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
$userVariableModel = new UserVariableModel();
|
||
7 years ago
|
|
||
6 years ago
|
return $userVariableModel->findAll();
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
/**
|
||
6 years ago
|
* List User Variable Keys by UserID
|
||
|
* Return array of variable keys or FALSE
|
||
7 years ago
|
*
|
||
6 years ago
|
* @param integer $user_id ; if not given current user
|
||
|
* @return boolean|array
|
||
7 years ago
|
*/
|
||
6 years ago
|
public function list_user_var_keys($user_id = false)
|
||
7 years ago
|
{
|
||
6 years ago
|
if (! $userId)
|
||
7 years ago
|
{
|
||
6 years ago
|
$userId = $this->session->user['id'];
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
$userModel = new UserModel();
|
||
7 years ago
|
|
||
6 years ago
|
if (! $userModel->existsById($userId))
|
||
|
{
|
||
|
return false;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
$userVariableModel = new UserVariableModel();
|
||
|
$userVariableModel->select('data_key as key');
|
||
7 years ago
|
|
||
6 years ago
|
return $userVariableModel->findAll();
|
||
|
}
|
||
7 years ago
|
//--------------------------------------------------------------------------
|
||
|
// Error Functions
|
||
|
//--------------------------------------------------------------------------
|
||
7 years ago
|
|
||
7 years ago
|
/**
|
||
|
* Error
|
||
|
*
|
||
|
* Add message to error array and set flash data
|
||
|
*
|
||
7 years ago
|
* @param string|array $message Message to add to array
|
||
|
* @param boolean $flashdata Whether to add $message to session flashdata
|
||
|
*
|
||
|
* @return void
|
||
7 years ago
|
*/
|
||
7 years ago
|
public function error($message, bool $flashdata = null)
|
||
7 years ago
|
{
|
||
7 years ago
|
if (is_array($message))
|
||
|
{
|
||
|
$this->errors = array_merge($this->errors, $message);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$this->errors[] = $message;
|
||
|
}
|
||
7 years ago
|
|
||
|
if ($flashdata)
|
||
|
{
|
||
7 years ago
|
if (is_array($message))
|
||
|
{
|
||
|
$this->flashErrors = array_merge($this->flashErrors, $message);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$this->flashErrors[] = $message;
|
||
|
}
|
||
|
|
||
6 years ago
|
$this->session->setFlashdata('errors', $this->flashErrors);
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Keep Errors
|
||
|
*
|
||
|
* Keeps the flashdata errors for one more page refresh. Optionally adds the default errors into the
|
||
|
* flashdata list. This should be called last in your controller, and with care as it could continue
|
||
|
* to revive all errors and not let them expire as intended.
|
||
|
* Benefitial when using Ajax Requests
|
||
|
*
|
||
7 years ago
|
* @param boolean $includeNonFlash Wheter to store basic errors as flashdata
|
||
7 years ago
|
*
|
||
7 years ago
|
* @return void
|
||
7 years ago
|
*/
|
||
7 years ago
|
public function keepErrors(bool $includeNonFlash = null)
|
||
7 years ago
|
{
|
||
|
if ($includeNonFlash)
|
||
7 years ago
|
{
|
||
6 years ago
|
$flashErrorsOld = $this->session->getFlashdata('errors');
|
||
6 years ago
|
$this->flashErrors = array_merge((is_array($flashErrorsOld) ? $flashErrorsOld : []), $this->errors);
|
||
6 years ago
|
$this->session->setFlashdata('errors', $this->flashErrors);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$this->session->keepFlashdata('errors');
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Get Errors Array
|
||
|
*
|
||
|
* Return array of errors
|
||
|
*
|
||
7 years ago
|
* @return array
|
||
7 years ago
|
*/
|
||
7 years ago
|
public function getErrorsArray()
|
||
|
{
|
||
|
return $this->errors;
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
7 years ago
|
* Printeger Errors
|
||
7 years ago
|
*
|
||
|
* Prints string of errors separated by delimiter
|
||
|
*
|
||
7 years ago
|
* @param string $divider Separator for error
|
||
|
* @param boolean $return Whether to return instead of echoing
|
||
|
*
|
||
|
* @return void|string
|
||
7 years ago
|
*/
|
||
7 years ago
|
public function printErrors(string $divider = '<br />', bool $return = null)
|
||
7 years ago
|
{
|
||
7 years ago
|
$msg = implode($divider, $this->errors);
|
||
7 years ago
|
|
||
|
if ($return)
|
||
7 years ago
|
{
|
||
7 years ago
|
return $msg;
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
echo $msg;
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Clear Errors
|
||
|
*
|
||
|
* Removes errors from error list and clears all associated flashdata
|
||
7 years ago
|
*
|
||
|
* @return void
|
||
7 years ago
|
*/
|
||
7 years ago
|
public function clearErrors()
|
||
|
{
|
||
6 years ago
|
$this->errors = [];
|
||
|
$this->flashErrors = [];
|
||
7 years ago
|
$this->session->remove('errors');
|
||
|
}
|
||
|
|
||
7 years ago
|
//--------------------------------------------------------------------------
|
||
|
// Info Functions
|
||
|
//--------------------------------------------------------------------------
|
||
7 years ago
|
|
||
7 years ago
|
/**
|
||
|
* Info
|
||
|
*
|
||
|
* Add message to info array and set flash data
|
||
|
*
|
||
6 years ago
|
* @param string|array $message Message to add to infos array
|
||
|
* @param boolean $flashdata Whether add $message to CI flashdata (deflault: FALSE)
|
||
7 years ago
|
*
|
||
|
* @return void
|
||
7 years ago
|
*/
|
||
6 years ago
|
public function info($message, bool $flashdata = null)
|
||
7 years ago
|
{
|
||
7 years ago
|
if (is_array($message))
|
||
|
{
|
||
|
$this->infos = array_merge($this->infos, $message);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$this->infos[] = $message;
|
||
|
}
|
||
7 years ago
|
|
||
|
if ($flashdata)
|
||
|
{
|
||
7 years ago
|
if (is_array($message))
|
||
|
{
|
||
|
$this->flashInfos = array_merge($this->flashInfos, $message);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$this->flashInfos[] = $message;
|
||
|
}
|
||
|
|
||
6 years ago
|
$this->session->setFlashdata('infos', $this->flashInfos);
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Keep Infos
|
||
|
*
|
||
|
* Keeps the flashdata infos for one more page refresh. Optionally adds the default infos into the
|
||
|
* flashdata list. This should be called last in your controller, and with care as it could continue
|
||
|
* to revive all infos and not let them expire as intended.
|
||
|
* Benefitial by using Ajax Requests
|
||
|
*
|
||
7 years ago
|
* @param boolean $includeNonFlash Wheter to store basic errors as flashdata
|
||
7 years ago
|
*
|
||
7 years ago
|
* @return void
|
||
7 years ago
|
*/
|
||
7 years ago
|
public function keepInfos(bool $includeNonFlash = null)
|
||
7 years ago
|
{
|
||
|
if ($includeNonFlash)
|
||
7 years ago
|
{
|
||
6 years ago
|
$flashInfosOld = $this->session->getFlashdata('infos');
|
||
6 years ago
|
$this->flashInfos = array_merge((is_array($flashInfosOld) ? $flashInfosOld : []), $this->infos);
|
||
6 years ago
|
$this->session->setFlashdata('infos', $this->flashInfos);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
$this->session->keepFlashdata('infos');
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Get Info Array
|
||
|
*
|
||
|
* Return array of infos
|
||
|
*
|
||
|
* @return array Array of messages, empty array if no errors
|
||
|
*/
|
||
7 years ago
|
public function getInfosArray()
|
||
|
{
|
||
|
return $this->infos;
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
7 years ago
|
* Printeger Info
|
||
7 years ago
|
*
|
||
7 years ago
|
* Printeger string of info separated by delimiter
|
||
7 years ago
|
*
|
||
7 years ago
|
* @param string $divider Separator for info
|
||
|
* @param boolean $return Whether to return instead of echoing
|
||
7 years ago
|
*
|
||
7 years ago
|
* @return string|void
|
||
7 years ago
|
*/
|
||
7 years ago
|
public function printInfos(string $divider = '<br />', bool $return = null)
|
||
7 years ago
|
{
|
||
7 years ago
|
$msg = implode($divider, $this->infos);
|
||
7 years ago
|
|
||
|
if ($return)
|
||
7 years ago
|
{
|
||
7 years ago
|
return $msg;
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
|
echo $msg;
|
||
|
}
|
||
|
|
||
7 years ago
|
/**
|
||
|
* Clear Info List
|
||
|
*
|
||
|
* Removes info messages from info list and clears all associated flashdata
|
||
7 years ago
|
*
|
||
|
* @return void
|
||
7 years ago
|
*/
|
||
7 years ago
|
public function clearInfos()
|
||
|
{
|
||
6 years ago
|
$this->infos = [];
|
||
|
$this->flashInfos = [];
|
||
7 years ago
|
$this->session->remove('infos');
|
||
|
}
|
||
|
}
|