Files
crm-ms1/application/controllers/Login.php

132 lines
3.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class Login extends CI_Controller {
function __construct()
{
parent::__construct('login');
$this->lang->load('login');
$this->load->helper('form');
$this->load->library('session');
$this->load->database();
}
public function index()
{
error_log('LOGIN PAGE DISPLAYED. COOKIE derniere_page=' . ($_COOKIE['derniere_page'] ?? 'NONE'));
error_log('LOGIN SESSION: '.print_r($this->session->userdata(), true));
$data['test']='ss';
if (!empty($_SERVER['HTTP_REFERER'])) {
$this->session->set_userdata('redirect_after_login', $_GET['redirect'] ?? '/');
}
$this->load->view('login/login',$data );
$this->load->view('login/footer', $data);
}
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
public function valide()
{error_log('LOGIN VALIDATION START');
$this->db->select('*');
$this->db->from('usa_usagers');
$this->db->where("usa_login ", $_POST['user']);
$query = $this->db->get();
$sortie = $query->row();
if ($sortie != null) {
if ($sortie->usa_password == $_POST['password']) {
$newdata = array(
'username' => $sortie->usa_prenom.' '.$sortie->usa_nom,
'email' => $sortie->usa_courriel,
'id' => $sortie->usa_id,
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
// ===== AJOUT ICI =====
$token = bin2hex(random_bytes(32));
$this->db->insert('auth_tokens', [
'user_id' => $sortie->usa_id,
'token' => $token,
'expires_at' => date('Y-m-d H:i:s', time()+86400),
'ip' => $_SERVER['REMOTE_ADDR'] ?? null,
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? null
]);
setcookie(
'auth_token',
$token,
time()+86400,
'/',
'',
isset($_SERVER['HTTPS']),
true
);
// ===== FIN AJOUT =====
$page = $_COOKIE['derniere_page'] ?? null;
if ($page) {
setcookie('derniere_page','',time()-3600,'/');
// Path only — rester sur le host où on sest logué (pas prod via URL absolue).
if (preg_match('#^https?://#i', $page)) {
$parts = parse_url($page);
$page = ($parts['path'] ?? '/')
. (isset($parts['query']) ? '?'.$parts['query'] : '');
}
if ($page === '' || $page[0] !== '/') {
$page = '/';
}
header('Location: '.$page);
exit;
}
redirect('/');
}
redirect('/login');
}
redirect('/login');
}
public function logout()
{
$token = $_COOKIE['auth_token'] ?? null;
if ($token) {
$this->db->where('token', $token);
$this->db->delete('auth_tokens');
}
setcookie('auth_token','',time()-3600,'/');
$this->session->sess_destroy();
redirect('/login');
}
}