ZwiiCMS/core/module/user/user.php

756 lines
27 KiB
PHP
Raw Normal View History

2018-04-02 08:29:19 +02:00
<?php
/**
* This file is part of Zwii.
* For full copyright and license information, please see the LICENSE
* file that was distributed with this source code.
*
* @author Rémi Jean <remi.jean@outlook.com>
* @copyright Copyright (C) 2008-2018, Rémi Jean
2021-02-17 13:49:58 +01:00
* @author Frédéric Tempez <frederic.tempez@outlook.com>
2021-12-18 10:25:33 +01:00
* @copyright Copyright (C) 2018-2022, Frédéric Tempez
* @license CC Attribution-NonCommercial-NoDerivatives 4.0 International
* @link http://zwiicms.fr/
2018-04-02 08:29:19 +02:00
*/
2022-09-29 08:45:59 +02:00
class user extends common
{
2018-04-02 08:29:19 +02:00
public static $actions = [
'add' => self::GROUP_ADMIN,
'delete' => self::GROUP_ADMIN,
2020-10-04 17:14:15 +02:00
'import' => self::GROUP_ADMIN,
'index' => self::GROUP_ADMIN,
2018-04-02 08:29:19 +02:00
'edit' => self::GROUP_MEMBER,
2020-10-04 17:14:15 +02:00
'logout' => self::GROUP_MEMBER,
2018-04-02 08:29:19 +02:00
'forgot' => self::GROUP_VISITOR,
'login' => self::GROUP_VISITOR,
'reset' => self::GROUP_VISITOR
];
2018-04-02 08:29:19 +02:00
public static $users = [];
2020-07-10 16:57:21 +02:00
//Paramètres pour choix de la signature
2020-07-02 19:53:52 +02:00
public static $signature = [
2020-07-10 16:57:21 +02:00
self::SIGNATURE_ID => 'Identifiant',
self::SIGNATURE_PSEUDO => 'Pseudo',
self::SIGNATURE_FIRSTLASTNAME => 'Prénom Nom',
self::SIGNATURE_LASTFIRSTNAME => 'Nom Prénom'
2020-07-02 19:53:52 +02:00
];
public static $userId = '';
2020-08-10 09:10:20 +02:00
public static $userLongtime = false;
2020-07-02 19:53:52 +02:00
2020-10-05 16:56:30 +02:00
public static $separators = [
';' => ';',
',' => ',',
':' => ':'
];
public static $languagesInstalled = [];
2018-04-02 08:29:19 +02:00
/**
* Ajout
*/
2022-09-29 08:45:59 +02:00
public function add()
{
2018-04-02 08:29:19 +02:00
// Soumission du formulaire
2022-09-29 08:45:59 +02:00
if ($this->isPost()) {
$check = true;
2018-04-02 08:29:19 +02:00
// L'identifiant d'utilisateur est indisponible
$userId = $this->getInput('userAddId', helper::FILTER_ID, true);
2022-09-29 08:45:59 +02:00
if ($this->getData(['user', $userId])) {
2018-04-02 08:29:19 +02:00
self::$inputNotices['userAddId'] = 'Identifiant déjà utilisé';
2022-09-29 08:45:59 +02:00
$check = false;
2018-04-02 08:29:19 +02:00
}
// Double vérification pour le mot de passe
2022-09-29 08:45:59 +02:00
if ($this->getInput('userAddPassword', helper::FILTER_STRING_SHORT, true) !== $this->getInput('userAddConfirmPassword', helper::FILTER_STRING_SHORT, true)) {
2018-04-02 08:29:19 +02:00
self::$inputNotices['userAddConfirmPassword'] = 'Incorrect';
$check = false;
}
2018-04-02 08:29:19 +02:00
// Crée l'utilisateur
$userFirstname = $this->getInput('userAddFirstname', helper::FILTER_STRING_SHORT, true);
$userLastname = $this->getInput('userAddLastname', helper::FILTER_STRING_SHORT, true);
$userMail = $this->getInput('userAddMail', helper::FILTER_MAIL, true);
2020-09-24 18:01:34 +02:00
2020-09-24 11:34:26 +02:00
// Stockage des données
$this->setData([
'user',
$userId,
[
'firstname' => $userFirstname,
'forgot' => 0,
'group' => $this->getInput('userAddGroup', helper::FILTER_INT, true),
'lastname' => $userLastname,
2020-10-31 17:55:54 +01:00
'pseudo' => $this->getInput('userAddPseudo', helper::FILTER_STRING_SHORT, true),
'signature' => $this->getInput('userAddSignature', helper::FILTER_INT, true),
2020-09-24 11:34:26 +02:00
'mail' => $userMail,
'password' => $this->getInput('userAddPassword', helper::FILTER_PASSWORD, true),
2021-05-20 21:52:09 +02:00
"connectFail" => null,
"connectTimeout" => null,
"accessUrl" => null,
"accessTimer" => null,
"accessCsrf" => null,
"files" => $this->getInput('userAddFiles', helper::FILTER_BOOLEAN),
'language' => $this->getInput('userEditLanguage', helper::FILTER_STRING_SHORT),
2020-09-24 11:34:26 +02:00
]
]);
// Envoie le mail
2018-04-02 08:29:19 +02:00
$sent = true;
2022-09-29 08:45:59 +02:00
if ($this->getInput('userAddSendMail', helper::FILTER_BOOLEAN) && $check === true) {
2018-04-02 08:29:19 +02:00
$sent = $this->sendMail(
$userMail,
'Compte créé sur ' . $this->getData(['locale', 'title']),
2018-04-02 08:29:19 +02:00
'Bonjour <strong>' . $userFirstname . ' ' . $userLastname . '</strong>,<br><br>' .
2022-09-29 08:45:59 +02:00
'Un administrateur vous a créé un compte sur le site ' . $this->getData(['locale', 'title']) . '. Vous trouverez ci-dessous les détails de votre compte.<br><br>' .
'<strong>Identifiant du compte :</strong> ' . $this->getInput('userAddId') . '<br>' .
'<small>Nous ne conservons pas les mots de passe, en conséquence nous vous conseillons de conserver ce message tant que vous ne vous êtes pas connecté. Vous pourrez modifier votre mot de passe après votre première connexion.</small>',
null
2018-04-02 08:29:19 +02:00
);
}
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . 'user',
2022-10-11 10:33:44 +02:00
'notification' => $sent === true ? helper::translate('Utilisateur créé') : $sent,
'state' => $sent === true ? true : null
2018-04-02 08:29:19 +02:00
]);
}
// Langues disponibles pour l'interface de l'utilisateur
if (is_dir(self::I18N_DIR)) {
$dir = getcwd();
chdir(self::I18N_DIR);
$files = glob('*.json');
chdir($dir);
}
foreach ($files as $file) {
// La langue est-elle référencée ?
if (array_key_exists(basename($file, '.json'), self::$languages)) {
self::$languagesInstalled[basename($file, '.json')] = self::$languages[basename($file, '.json')];
}
}
2018-04-02 08:29:19 +02:00
// Valeurs en sortie
$this->addOutput([
2022-10-02 10:59:42 +02:00
'title' => helper::translate('Nouvel utilisateur'),
2018-04-02 08:29:19 +02:00
'view' => 'add'
]);
}
/**
* Suppression
*/
2022-09-29 08:45:59 +02:00
public function delete()
{
2018-04-02 08:29:19 +02:00
// Accès refusé
2022-09-29 08:45:59 +02:00
if (
2018-04-02 08:29:19 +02:00
// L'utilisateur n'existe pas
2019-01-15 13:51:30 +01:00
$this->getData(['user', $this->getUrl(2)]) === null
2018-04-02 08:29:19 +02:00
// Groupe insuffisant
2022-09-29 08:45:59 +02:00
and ($this->getUrl('group') < self::GROUP_MODERATOR)
2018-04-02 08:29:19 +02:00
) {
// Valeurs en sortie
$this->addOutput([
'access' => false
]);
}
2019-01-08 17:55:18 +01:00
// Jeton incorrect
2019-01-15 13:51:30 +01:00
elseif ($this->getUrl(3) !== $_SESSION['csrf']) {
2019-01-08 17:55:18 +01:00
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . 'user',
2022-10-11 10:33:44 +02:00
'notification' => helper::translate('Action interdite')
2019-01-08 17:55:18 +01:00
]);
}
2018-04-02 08:29:19 +02:00
// Bloque la suppression de son propre compte
2022-09-29 08:45:59 +02:00
elseif ($this->getUser('id') === $this->getUrl(2)) {
2018-04-02 08:29:19 +02:00
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . 'user',
2022-10-11 10:33:44 +02:00
'notification' => helper::translate('Impossible de supprimer votre propre compte')
2018-04-02 08:29:19 +02:00
]);
}
// Suppression
else {
2019-01-15 13:51:30 +01:00
$this->deleteData(['user', $this->getUrl(2)]);
2018-04-02 08:29:19 +02:00
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . 'user',
2022-10-11 10:33:44 +02:00
'notification' => helper::translate('Utilisateur supprimé'),
2018-04-02 08:29:19 +02:00
'state' => true
]);
}
}
/**
* Édition
*/
2022-09-29 08:45:59 +02:00
public function edit()
{
if (
$this->getUrl(3) !== $_SESSION['csrf']
2022-09-29 08:45:59 +02:00
) {
// Valeurs en sortie
$this->addOutput([
'redirect' => helper::baseUrl() . 'user',
2022-10-11 10:33:44 +02:00
'notification' => helper::translate('Action interdite')
]);
}
2018-04-02 08:29:19 +02:00
// Accès refusé
2022-09-29 08:45:59 +02:00
if (
2018-04-02 08:29:19 +02:00
// L'utilisateur n'existe pas
$this->getData(['user', $this->getUrl(2)]) === null
// Droit d'édition
2022-09-29 08:45:59 +02:00
and (
2018-04-02 08:29:19 +02:00
// Impossible de s'auto-éditer
2022-09-29 08:45:59 +02:00
($this->getUser('id') === $this->getUrl(2)
and $this->getUrl('group') <= self::GROUP_VISITOR
2018-04-02 08:29:19 +02:00
)
// Impossible d'éditer un autre utilisateur
2022-09-29 08:45:59 +02:00
or ($this->getUrl('group') < self::GROUP_MODERATOR)
2018-04-02 08:29:19 +02:00
)
) {
// Valeurs en sortie
$this->addOutput([
'access' => false
]);
}
// Accès autorisé
else {
// Soumission du formulaire
2022-09-29 08:45:59 +02:00
if ($this->isPost()) {
2018-04-02 08:29:19 +02:00
// Double vérification pour le mot de passe
$newPassword = $this->getData(['user', $this->getUrl(2), 'password']);
2022-09-29 08:45:59 +02:00
if ($this->getInput('userEditNewPassword')) {
2018-04-02 08:29:19 +02:00
// L'ancien mot de passe est correct
2022-09-29 08:45:59 +02:00
if (password_verify($this->getInput('userEditOldPassword'), $this->getData(['user', $this->getUrl(2), 'password']))) {
2018-04-02 08:29:19 +02:00
// La confirmation correspond au mot de passe
2022-09-29 08:45:59 +02:00
if ($this->getInput('userEditNewPassword') === $this->getInput('userEditConfirmPassword')) {
2018-04-02 08:29:19 +02:00
$newPassword = $this->getInput('userEditNewPassword', helper::FILTER_PASSWORD, true);
// Déconnexion de l'utilisateur si il change le mot de passe de son propre compte
2022-09-29 08:45:59 +02:00
if ($this->getUser('id') === $this->getUrl(2)) {
2018-04-02 08:29:19 +02:00
helper::deleteCookie('ZWII_USER_ID');
helper::deleteCookie('ZWII_USER_PASSWORD');
}
2022-09-29 08:45:59 +02:00
} else {
2022-10-11 10:33:44 +02:00
self::$inputNotices['userEditConfirmPassword'] = helper::translate('Incorrect');
2018-04-02 08:29:19 +02:00
}
2022-09-29 08:45:59 +02:00
} else {
2022-10-11 10:33:44 +02:00
self::$inputNotices['userEditOldPassword'] = helper::translate('Incorrect');
2018-04-02 08:29:19 +02:00
}
}
// Modification du groupe
2022-09-29 08:45:59 +02:00
if (
2018-04-02 08:29:19 +02:00
$this->getUser('group') === self::GROUP_ADMIN
2022-09-29 08:45:59 +02:00
and $this->getUrl(2) !== $this->getUser('id')
2018-04-02 08:29:19 +02:00
) {
$newGroup = $this->getInput('userEditGroup', helper::FILTER_INT, true);
2022-09-29 08:45:59 +02:00
} else {
2018-04-02 08:29:19 +02:00
$newGroup = $this->getData(['user', $this->getUrl(2), 'group']);
}
2020-07-02 19:53:52 +02:00
// Modification de nom Prénom
2022-09-29 08:45:59 +02:00
if ($this->getUser('group') === self::GROUP_ADMIN) {
2020-07-02 19:53:52 +02:00
$newfirstname = $this->getInput('userEditFirstname', helper::FILTER_STRING_SHORT, true);
$newlastname = $this->getInput('userEditLastname', helper::FILTER_STRING_SHORT, true);
2022-09-29 08:45:59 +02:00
} else {
2020-07-02 19:53:52 +02:00
$newfirstname = $this->getData(['user', $this->getUrl(2), 'firstname']);
$newlastname = $this->getData(['user', $this->getUrl(2), 'lastname']);
}
2018-04-02 08:29:19 +02:00
// Modifie l'utilisateur
$this->setData([
'user',
$this->getUrl(2),
[
2020-07-02 19:53:52 +02:00
'firstname' => $newfirstname,
2018-04-02 08:29:19 +02:00
'forgot' => 0,
'group' => $newGroup,
2020-07-02 19:53:52 +02:00
'lastname' => $newlastname,
'pseudo' => $this->getInput('userEditPseudo', helper::FILTER_STRING_SHORT, true),
'signature' => $this->getInput('userEditSignature', helper::FILTER_INT, true),
2018-04-02 08:29:19 +02:00
'mail' => $this->getInput('userEditMail', helper::FILTER_MAIL, true),
'password' => $newPassword,
2022-09-29 08:45:59 +02:00
'connectFail' => $this->getData(['user', $this->getUrl(2), 'connectFail']),
'connectTimeout' => $this->getData(['user', $this->getUrl(2), 'connectTimeout']),
'accessUrl' => $this->getData(['user', $this->getUrl(2), 'accessUrl']),
'accessTimer' => $this->getData(['user', $this->getUrl(2), 'accessTimer']),
'accessCsrf' => $this->getData(['user', $this->getUrl(2), 'accessCsrf']),
'files' => $this->getInput('userEditFiles', helper::FILTER_BOOLEAN),
'language' => $this->getInput('userEditLanguage', helper::FILTER_STRING_SHORT),
2018-04-02 08:29:19 +02:00
]
]);
// Redirection spécifique si l'utilisateur change son mot de passe
2022-09-29 08:45:59 +02:00
if ($this->getUser('id') === $this->getUrl(2) and $this->getInput('userEditNewPassword')) {
2018-04-02 08:29:19 +02:00
$redirect = helper::baseUrl() . 'user/login/' . str_replace('/', '_', $this->getUrl());
}
// Redirection si retour en arrière possible
2022-09-29 08:45:59 +02:00
elseif ($this->getUser('group') === 3) {
2018-04-02 08:29:19 +02:00
$redirect = helper::baseUrl() . 'user';
}
// Redirection normale
else {
2020-11-24 08:29:53 +01:00
$redirect = helper::baseUrl();
2018-04-02 08:29:19 +02:00
}
// Valeurs en sortie
$this->addOutput([
'redirect' => $redirect,
2022-10-11 10:33:44 +02:00
'notification' => helper::translate('Modifications enregistrées'),
2018-04-02 08:29:19 +02:00
'state' => true
]);
}
// Langues disponibles pour l'interface de l'utilisateur
if (is_dir(self::I18N_DIR)) {
$dir = getcwd();
chdir(self::I18N_DIR);
$files = glob('*.json');
chdir($dir);
}
foreach ($files as $file) {
// La langue est-elle référencée ?
if (array_key_exists(basename($file, '.json'), self::$languages)) {
self::$languagesInstalled[basename($file, '.json')] = self::$languages[basename($file, '.json')];
}
}
2018-04-02 08:29:19 +02:00
// Valeurs en sortie
$this->addOutput([
'title' => $this->getData(['user', $this->getUrl(2), 'firstname']) . ' ' . $this->getData(['user', $this->getUrl(2), 'lastname']),
'view' => 'edit'
]);
}
}
/**
* Mot de passe perdu
*/
2022-09-29 08:45:59 +02:00
public function forgot()
{
2018-04-02 08:29:19 +02:00
// Soumission du formulaire
2022-09-29 08:45:59 +02:00
if ($this->isPost()) {
2018-04-02 08:29:19 +02:00
$userId = $this->getInput('userForgotId', helper::FILTER_ID, true);
2022-09-29 08:45:59 +02:00
if ($this->getData(['user', $userId])) {
2018-04-02 08:29:19 +02:00
// Enregistre la date de la demande dans le compte utilisateur
$this->setData(['user', $userId, 'forgot', time()]);
// Crée un id unique pour la réinitialisation
$uniqId = md5(json_encode($this->getData(['user', $userId])));
// Envoi le mail
$sent = $this->sendMail(
$this->getData(['user', $userId, 'mail']),
'Réinitialisation de votre mot de passe',
'Bonjour <strong>' . $this->getData(['user', $userId, 'firstname']) . ' ' . $this->getData(['user', $userId, 'lastname']) . '</strong>,<br><br>' .
2022-09-29 08:45:59 +02:00
'Vous avez demandé à changer le mot de passe lié à votre compte. Vous trouverez ci-dessous un lien vous permettant de modifier celui-ci.<br><br>' .
'<a href="' . helper::baseUrl() . 'user/reset/' . $userId . '/' . $uniqId . '" target="_blank">' . helper::baseUrl() . 'user/reset/' . $userId . '/' . $uniqId . '</a><br><br>' .
'<small>Si nous n\'avez pas demandé à réinitialiser votre mot de passe, veuillez ignorer ce mail.</small>',
null
2018-04-02 08:29:19 +02:00
);
// Valeurs en sortie
$this->addOutput([
2022-10-11 10:33:44 +02:00
'notification' => ($sent === true ? helper::translate('Un mail a été envoyé pour confirmer la réinitialisation') : $sent),
2018-04-02 08:29:19 +02:00
'state' => ($sent === true ? true : null)
]);
}
// L'utilisateur n'existe pas
else {
// Valeurs en sortie
$this->addOutput([
2022-10-11 10:33:44 +02:00
'notification' => helper::translate('Utilisateur inexistant')
2018-04-02 08:29:19 +02:00
]);
}
}
// Valeurs en sortie
$this->addOutput([
2022-10-02 10:59:42 +02:00
'title' => helper::translate('Mot de passe oublié'),
'view' => 'forgot',
2018-04-02 08:29:19 +02:00
'display' => self::DISPLAY_LAYOUT_LIGHT,
]);
}
/**
* Liste des utilisateurs
*/
2022-09-29 08:45:59 +02:00
public function index()
{
$userIdsFirstnames = helper::arrayColumn($this->getData(['user']), 'firstname');
2018-04-02 08:29:19 +02:00
ksort($userIdsFirstnames);
2022-09-29 08:45:59 +02:00
foreach ($userIdsFirstnames as $userId => $userFirstname) {
if ($this->getData(['user', $userId, 'group'])) {
self::$users[] = [
$userId,
$userFirstname . ' ' . $this->getData(['user', $userId, 'lastname']),
self::$groups[$this->getData(['user', $userId, 'group'])],
template::button('userEdit' . $userId, [
'href' => helper::baseUrl() . 'user/edit/' . $userId . '/' . $_SESSION['csrf'],
2022-02-18 12:43:48 +01:00
'value' => template::ico('pencil'),
2022-10-02 10:59:42 +02:00
'help' => 'Éditer'
]),
template::button('userDelete' . $userId, [
'class' => 'userDelete buttonRed',
2022-09-29 08:45:59 +02:00
'href' => helper::baseUrl() . 'user/delete/' . $userId . '/' . $_SESSION['csrf'],
'value' => template::ico('trash'),
2022-09-05 09:00:23 +02:00
'help' => 'Supprimer'
])
];
}
2018-04-02 08:29:19 +02:00
}
// Valeurs en sortie
$this->addOutput([
2022-10-02 10:59:42 +02:00
'title' => helper::translate('Utilisateurs'),
2018-04-02 08:29:19 +02:00
'view' => 'index'
]);
}
/**
* Connexion
*/
2022-09-29 08:45:59 +02:00
public function login()
{
2018-04-02 08:29:19 +02:00
// Soumission du formulaire
2021-01-03 18:41:25 +01:00
$logStatus = '';
2022-09-29 08:45:59 +02:00
if ($this->isPost()) {
2020-12-30 17:41:27 +01:00
// Lire Id du compte
$userId = $this->getInput('userLoginId', helper::FILTER_ID, true);
// Check le captcha
2022-09-29 08:45:59 +02:00
if (
$this->getData(['config', 'connect', 'captcha'])
and password_verify($this->getInput('userLoginCaptcha', helper::FILTER_INT), $this->getInput('userLoginCaptchaResult')) === false
) {
2021-01-03 18:41:25 +01:00
$captcha = false;
2020-10-09 08:37:34 +02:00
} else {
2021-01-03 18:41:25 +01:00
$captcha = true;
}
/**
* Aucun compte existant
*/
2022-09-29 08:45:59 +02:00
if (!$this->getData(['user', $userId])) {
$logStatus = 'Compte inconnu';
2021-01-03 18:41:25 +01:00
//Stockage de l'IP
$this->setData([
'blacklist',
$userId,
[
2022-09-29 08:45:59 +02:00
'connectFail' => $this->getData(['blacklist', $userId, 'connectFail']) + 1,
2021-01-03 18:41:25 +01:00
'lastFail' => time(),
'ip' => helper::getIp()
]
]);
// Verrouillage des IP
$ipBlackList = helper::arrayColumn($this->getData(['blacklist']), 'ip');
2022-09-29 08:45:59 +02:00
if (
$this->getData(['blacklist', $userId, 'connectFail']) >= $this->getData(['config', 'connect', 'attempt'])
and in_array($this->getData(['blacklist', $userId, 'ip']), $ipBlackList)
) {
2021-01-03 18:41:25 +01:00
$logStatus = 'Compte inconnu verrouillé';
// Valeurs en sortie
$this->addOutput([
2022-10-11 10:33:44 +02:00
'notification' => helper::translate('Compte verrouillé'),
2021-01-03 18:41:25 +01:00
'redirect' => helper::baseUrl(),
'state' => false
]);
2021-01-03 18:41:25 +01:00
} else {
// Valeurs en sortie
$this->addOutput([
2022-10-11 10:33:44 +02:00
'notification' => helper::translate('Captcha, identifiant ou mot de passe incorrects')
2021-01-03 18:41:25 +01:00
]);
}
2022-09-29 08:45:59 +02:00
/**
* Le compte existe
*/
} else {
2021-01-03 18:41:25 +01:00
// Cas 4 : le délai de blocage est dépassé et le compte est au max - Réinitialiser
2022-09-29 08:45:59 +02:00
if (
$this->getData(['user', $userId, 'connectTimeout']) + $this->getData(['config', 'connect', 'timeout']) < time()
and $this->getData(['user', $userId, 'connectFail']) === $this->getData(['config', 'connect', 'attempt'])
) {
$this->setData(['user', $userId, 'connectFail', 0]);
$this->setData(['user', $userId, 'connectTimeout', 0]);
2021-01-03 18:41:25 +01:00
}
// Check la présence des variables et contrôle du blocage du compte si valeurs dépassées
// Vérification du mot de passe et du groupe
if (
2022-09-29 08:45:59 +02:00
($this->getData(['user', $userId, 'connectTimeout']) + $this->getData(['config', 'connect', 'timeout'])) < time()
and $this->getData(['user', $userId, 'connectFail']) < $this->getData(['config', 'connect', 'attempt'])
and password_verify($this->getInput('userLoginPassword', helper::FILTER_STRING_SHORT, true), $this->getData(['user', $userId, 'password']))
and $this->getData(['user', $userId, 'group']) >= self::GROUP_MEMBER
and $captcha === true
2021-01-03 18:41:25 +01:00
) {
// RAZ
2022-09-29 08:45:59 +02:00
$this->setData(['user', $userId, 'connectFail', 0]);
$this->setData(['user', $userId, 'connectTimeout', 0]);
2021-01-03 18:41:25 +01:00
// Expiration
2022-09-29 08:45:59 +02:00
$expire = $this->getInput('userLoginLongTime', helper::FILTER_BOOLEAN) === true ? strtotime("+1 year") : 0;
setcookie('ZWII_USER_ID', $userId, $expire, helper::baseUrl(false, false), '', helper::isHttps(), true);
2021-01-03 18:41:25 +01:00
setcookie('ZWII_USER_PASSWORD', $this->getData(['user', $userId, 'password']), $expire, helper::baseUrl(false, false), '', helper::isHttps(), true);
// Accès multiples avec le même compte
2022-09-29 08:45:59 +02:00
$this->setData(['user', $userId, 'accessCsrf', $_SESSION['csrf']]);
2021-01-03 18:41:25 +01:00
// Valeurs en sortie lorsque le site est en maintenance et que l'utilisateur n'est pas administrateur
2022-09-29 08:45:59 +02:00
if (
2021-01-03 18:41:25 +01:00
$this->getData(['config', 'maintenance'])
2022-09-29 08:45:59 +02:00
and $this->getData(['user', $userId, 'group']) < self::GROUP_ADMIN
2021-01-03 18:41:25 +01:00
) {
2020-07-17 17:27:34 +02:00
$this->addOutput([
2022-10-11 10:33:44 +02:00
'notification' => helper::translate('Seul un administrateur peut se connecter lors d\'une maintenance'),
2020-07-17 17:27:34 +02:00
'redirect' => helper::baseUrl(),
'state' => false
]);
2020-07-02 19:53:52 +02:00
} else {
2021-01-03 18:41:25 +01:00
$logStatus = 'Connexion réussie';
2020-07-17 17:27:34 +02:00
// Valeurs en sortie
$this->addOutput([
2022-10-11 10:33:44 +02:00
'notification' => sprintf(helper::translate('Bienvenue %s %s'), $this->getData(['user', $userId, 'firstname']), $this->getData(['user', $userId, 'lastname'])),
2021-01-03 18:41:25 +01:00
'redirect' => helper::baseUrl() . str_replace('_', '/', str_replace('__', '#', $this->getUrl(2))),
'state' => true
2020-07-17 17:27:34 +02:00
]);
}
2022-09-29 08:45:59 +02:00
// Sinon notification d'échec
2021-01-03 18:41:25 +01:00
} else {
2022-10-11 10:33:44 +02:00
$notification = helper::translate('Captcha, identifiant ou mot de passe incorrects');
2021-01-03 18:41:25 +01:00
$logStatus = $captcha === true ? 'Erreur de mot de passe' : 'Erreur de captcha';
// Cas 1 le nombre de connexions est inférieur aux tentatives autorisées : incrément compteur d'échec
2022-09-29 08:45:59 +02:00
if ($this->getData(['user', $userId, 'connectFail']) < $this->getData(['config', 'connect', 'attempt'])) {
$this->setData(['user', $userId, 'connectFail', $this->getdata(['user', $userId, 'connectFail']) + 1]);
2020-07-17 17:27:34 +02:00
}
2021-01-03 18:41:25 +01:00
// Cas 2 la limite du nombre de connexion est atteinte : placer le timer
2022-09-29 08:45:59 +02:00
if ($this->getdata(['user', $userId, 'connectFail']) == $this->getData(['config', 'connect', 'attempt'])) {
$this->setData(['user', $userId, 'connectTimeout', time()]);
2020-07-17 17:27:34 +02:00
}
2021-01-03 18:41:25 +01:00
// Cas 3 le délai de bloquage court
2022-09-29 08:45:59 +02:00
if ($this->getData(['user', $userId, 'connectTimeout']) + $this->getData(['config', 'connect', 'timeout']) > time()) {
$notification = sprintf(helper::translate('Accès bloqué %d minutes', ($this->getData(['config', 'connect', 'timeout']) / 60)));
2021-01-03 18:41:25 +01:00
}
// Valeurs en sortie
$this->addOutput([
'notification' => $notification
]);
2020-07-02 19:53:52 +02:00
}
2018-04-02 08:29:19 +02:00
}
}
2021-01-03 18:41:25 +01:00
// Journalisation
$dataLog = helper::dateUTF8('%Y %m %d', time()) . ' - ' . helper::dateUTF8('%H:%M', time());
2022-09-29 08:45:59 +02:00
$dataLog .= helper::getIp($this->getData(['config', 'connect', 'anonymousIp'])) . ';';
2022-10-05 18:17:33 +02:00
$dataLog .= empty($this->getInput('userLoginId')) ? ';' : $this->getInput('userLoginId', helper::FILTER_ID) . ';';
2022-09-29 08:45:59 +02:00
$dataLog .= $this->getUrl() . ';';
$dataLog .= $logStatus;
2021-01-03 18:41:25 +01:00
$dataLog .= PHP_EOL;
2022-09-29 08:45:59 +02:00
if ($this->getData(['config', 'connect', 'log'])) {
2021-01-03 18:41:25 +01:00
file_put_contents(self::DATA_DIR . 'journal.log', $dataLog, FILE_APPEND);
}
// Stockage des cookies
if (!empty($_COOKIE['ZWII_USER_ID'])) {
self::$userId = $_COOKIE['ZWII_USER_ID'];
}
2018-04-02 08:29:19 +02:00
// Valeurs en sortie
$this->addOutput([
2022-10-02 10:59:42 +02:00
'title' => helper::translate('Connexion'),
'view' => 'login',
2018-04-02 08:29:19 +02:00
'display' => self::DISPLAY_LAYOUT_LIGHT,
]);
}
/**
* Déconnexion
*/
2022-09-29 08:45:59 +02:00
public function logout()
{
2022-05-19 15:20:57 +02:00
helper::deleteCookie('ZWII_USER_ID');
2018-04-02 08:29:19 +02:00
helper::deleteCookie('ZWII_USER_PASSWORD');
2019-01-13 17:54:42 +01:00
session_destroy();
2018-04-02 08:29:19 +02:00
// Valeurs en sortie
$this->addOutput([
'notification' => helper::translate('Déconnexion !'),
2018-04-02 08:29:19 +02:00
'redirect' => helper::baseUrl(false),
'state' => true
]);
}
/**
* Réinitialisation du mot de passe
*/
2022-09-29 08:45:59 +02:00
public function reset()
{
2018-04-02 08:29:19 +02:00
// Accès refusé
2022-09-29 08:45:59 +02:00
if (
2018-04-02 08:29:19 +02:00
// L'utilisateur n'existe pas
$this->getData(['user', $this->getUrl(2)]) === null
// Lien de réinitialisation trop vieux
2022-09-29 08:45:59 +02:00
or $this->getData(['user', $this->getUrl(2), 'forgot']) + 86400 < time()
2018-04-02 08:29:19 +02:00
// Id unique incorrecte
2022-09-29 08:45:59 +02:00
or $this->getUrl(3) !== md5(json_encode($this->getData(['user', $this->getUrl(2)])))
2018-04-02 08:29:19 +02:00
) {
// Valeurs en sortie
$this->addOutput([
'access' => false
]);
}
// Accès autorisé
else {
// Soumission du formulaire
2022-09-29 08:45:59 +02:00
if ($this->isPost()) {
2018-04-02 08:29:19 +02:00
// Double vérification pour le mot de passe
2022-09-29 08:45:59 +02:00
if ($this->getInput('userResetNewPassword')) {
2018-04-02 08:29:19 +02:00
// La confirmation ne correspond pas au mot de passe
2022-09-29 08:45:59 +02:00
if ($this->getInput('userResetNewPassword', helper::FILTER_STRING_SHORT, true) !== $this->getInput('userResetConfirmPassword', helper::FILTER_STRING_SHORT, true)) {
2018-04-02 08:29:19 +02:00
$newPassword = $this->getData(['user', $this->getUrl(2), 'password']);
self::$inputNotices['userResetConfirmPassword'] = 'Incorrect';
2022-09-29 08:45:59 +02:00
} else {
2018-04-02 08:29:19 +02:00
$newPassword = $this->getInput('userResetNewPassword', helper::FILTER_PASSWORD, true);
}
// Modifie le mot de passe
$this->setData(['user', $this->getUrl(2), 'password', $newPassword]);
// Réinitialise la date de la demande
$this->setData(['user', $this->getUrl(2), 'forgot', 0]);
2020-11-22 18:56:14 +01:00
// Réinitialise le blocage
2022-09-29 08:45:59 +02:00
$this->setData(['user', $this->getUrl(2), 'connectFail', 0]);
$this->setData(['user', $this->getUrl(2), 'connectTimeout', 0]);
2018-04-02 08:29:19 +02:00
// Valeurs en sortie
$this->addOutput([
2022-10-11 10:33:44 +02:00
'notification' => helper::translate('Nouveau mot de passe enregistré'),
2020-11-22 18:56:14 +01:00
//'redirect' => helper::baseUrl() . 'user/login/' . str_replace('/', '_', $this->getUrl()),
'redirect' => helper::baseUrl(),
2018-04-02 08:29:19 +02:00
'state' => true
]);
}
}
// Valeurs en sortie
$this->addOutput([
2022-10-02 10:59:42 +02:00
'title' => helper::translate('Réinitialisation du mot de passe'),
'view' => 'reset',
2020-10-07 19:01:57 +02:00
'display' => self::DISPLAY_LAYOUT_LIGHT,
2022-10-02 10:59:42 +02:00
2018-04-02 08:29:19 +02:00
]);
}
}
2020-10-04 17:14:15 +02:00
/**
* Importation CSV d'utilisateurs
*/
2022-09-29 08:45:59 +02:00
public function import()
{
// Soumission du formulaire
2020-10-06 15:56:49 +02:00
$notification = '';
$success = true;
2022-09-29 08:45:59 +02:00
if ($this->isPost()) {
// Lecture du CSV et construction du tableau
2022-09-29 08:45:59 +02:00
$file = $this->getInput('userImportCSVFile', helper::FILTER_STRING_SHORT, true);
2020-10-06 08:29:14 +02:00
$filePath = self::FILE_DIR . 'source/' . $file;
2022-09-29 08:45:59 +02:00
if ($file and file_exists($filePath)) {
2020-10-08 21:16:28 +02:00
// Analyse et extraction du CSV
2022-09-29 08:45:59 +02:00
$rows = array_map(function ($row) {
return str_getcsv($row, $this->getInput('userImportSeparator'));
}, file($filePath));
2020-10-05 16:56:30 +02:00
$header = array_shift($rows);
$csv = array();
2022-09-29 08:45:59 +02:00
foreach ($rows as $row) {
2020-10-05 16:56:30 +02:00
$csv[] = array_combine($header, $row);
}
2020-10-08 21:16:28 +02:00
// Traitement des données
2022-09-29 08:45:59 +02:00
foreach ($csv as $item) {
2020-10-08 21:16:28 +02:00
// Données valides
2022-09-29 08:45:59 +02:00
if (
array_key_exists('id', $item)
and array_key_exists('prenom', $item)
and array_key_exists('nom', $item)
and array_key_exists('groupe', $item)
and array_key_exists('email', $item)
and $item['nom']
and $item['prenom']
and $item['id']
and $item['email']
and $item['groupe']
2020-10-19 09:53:28 +02:00
) {
2020-12-04 20:57:02 +01:00
// Validation du groupe
$item['groupe'] = (int) $item['groupe'];
2022-09-29 08:45:59 +02:00
$item['groupe'] = ($item['groupe'] >= self::GROUP_BANNED and $item['groupe'] <= self::GROUP_ADMIN)
? $item['groupe'] : 1;
2020-10-08 21:16:28 +02:00
// L'utilisateur existe
2022-09-29 08:45:59 +02:00
if ($this->getData(['user', helper::filter($item['id'], helper::FILTER_ID)])) {
2020-10-08 21:16:28 +02:00
// Notification du doublon
$item['notification'] = template::ico('cancel');
// Création du tableau de confirmation
self::$users[] = [
2022-09-29 08:45:59 +02:00
helper::filter($item['id'], helper::FILTER_ID),
2020-10-08 21:16:28 +02:00
$item['nom'],
$item['prenom'],
self::$groups[$item['groupe']],
$item['prenom'],
2022-09-29 08:45:59 +02:00
helper::filter($item['email'], helper::FILTER_MAIL),
2020-10-08 21:16:28 +02:00
$item['notification']
];
// L'utilisateur n'existe pas
} else {
// Nettoyage de l'identifiant
2022-09-29 08:45:59 +02:00
$userId = helper::filter($item['id'], helper::FILTER_ID);
2020-10-08 21:16:28 +02:00
// Enregistre le user
$create = $this->setData([
2020-10-08 21:16:28 +02:00
'user',
$userId, [
'firstname' => $item['prenom'],
'forgot' => 0,
2022-09-29 08:45:59 +02:00
'group' => $item['groupe'],
2020-10-08 21:16:28 +02:00
'lastname' => $item['nom'],
'mail' => $item['email'],
'pseudo' => $item['prenom'],
'signature' => 1, // Pseudo
'password' => uniqid(), // A modifier à la première connexion
2021-05-20 21:52:09 +02:00
"connectFail" => null,
"connectTimeout" => null,
"accessUrl" => null,
"accessTimer" => null,
"accessCsrf" => null
2022-09-29 08:45:59 +02:00
]
]);
2020-10-08 21:16:28 +02:00
// Icône de notification
$item['notification'] = $create ? template::ico('check') : template::ico('cancel');
2020-10-08 21:16:28 +02:00
// Envoi du mail
2022-09-29 08:45:59 +02:00
if (
$create
and $this->getInput('userImportNotification', helper::FILTER_BOOLEAN) === true
) {
2020-10-08 21:16:28 +02:00
$sent = $this->sendMail(
$item['email'],
'Compte créé sur ' . $this->getData(['locale', 'title']),
2020-10-08 21:16:28 +02:00
'Bonjour <strong>' . $item['prenom'] . ' ' . $item['nom'] . '</strong>,<br><br>' .
2022-09-29 08:45:59 +02:00
'Un administrateur vous a créé un compte sur le site ' . $this->getData(['locale', 'title']) . '. Vous trouverez ci-dessous les détails de votre compte.<br><br>' .
'<strong>Identifiant du compte :</strong> ' . $userId . '<br>' .
'<small>Un mot de passe provisoire vous été attribué, à la première connexion cliquez sur Mot de passe Oublié.</small>'
2020-10-08 21:16:28 +02:00
);
if ($sent === true) {
// Mail envoyé changement de l'icône
2022-09-29 08:45:59 +02:00
$item['notification'] = template::ico('mail');
2020-10-08 21:16:28 +02:00
}
}
// Création du tableau de confirmation
self::$users[] = [
$userId,
$item['nom'],
$item['prenom'],
self::$groups[$item['groupe']],
$item['prenom'],
$item['email'],
$item['notification']
];
}
2020-10-05 16:56:30 +02:00
}
}
2020-10-08 12:49:09 +02:00
if (empty(self::$users)) {
2022-10-11 10:33:44 +02:00
$notification = helper::translate('Rien à importer, erreur de format ou fichier incorrect');
2020-10-08 12:49:09 +02:00
$success = false;
} else {
2022-10-11 10:33:44 +02:00
$notification = helper::translate('Importation effectuée');
2020-10-08 12:49:09 +02:00
$success = true;
}
2020-10-05 16:56:30 +02:00
} else {
2022-10-11 10:33:44 +02:00
$notification = helper::translate('Erreur de lecture, vérifiez les permissions');
2020-10-05 16:56:30 +02:00
$success = false;
}
}
2020-10-04 17:14:15 +02:00
// Valeurs en sortie
$this->addOutput([
2022-10-02 10:59:42 +02:00
'title' => 'Importation d\'utilisateurs',
2020-10-05 16:56:30 +02:00
'view' => 'import',
'notification' => $notification,
'state' => $success
2020-10-04 17:14:15 +02:00
]);
}
}