98 lines
1.9 KiB
PHP
98 lines
1.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
require_once __DIR__ . '/auth.php';
|
||
|
|
require_once __DIR__ . '/../database.php';
|
||
|
|
|
||
|
|
header('Content-Type: application/json; charset=utf-8');
|
||
|
|
|
||
|
|
try {
|
||
|
|
|
||
|
|
$data = json_decode(
|
||
|
|
file_get_contents('php://input'),
|
||
|
|
true
|
||
|
|
);
|
||
|
|
|
||
|
|
$id = (int)($data['id'] ?? 0);
|
||
|
|
$pass = $data['pass'] ?? '';
|
||
|
|
|
||
|
|
|
||
|
|
/*
|
||
|
|
* =====================================================
|
||
|
|
* VERIFICATION UTILISATEUR
|
||
|
|
* =====================================================
|
||
|
|
*/
|
||
|
|
|
||
|
|
if ($id <= 0) {
|
||
|
|
|
||
|
|
throw new Exception(
|
||
|
|
'Utilisateur invalide.'
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/*
|
||
|
|
* =====================================================
|
||
|
|
* VERIFICATION MOT DE PASSE
|
||
|
|
* =====================================================
|
||
|
|
*/
|
||
|
|
|
||
|
|
if (strlen($pass) < 4) {
|
||
|
|
|
||
|
|
throw new Exception(
|
||
|
|
'Le mot de passe doit contenir au moins 4 caractères.'
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/*
|
||
|
|
* =====================================================
|
||
|
|
* HASH DU MOT DE PASSE
|
||
|
|
* =====================================================
|
||
|
|
*/
|
||
|
|
|
||
|
|
$hash = password_hash(
|
||
|
|
$pass,
|
||
|
|
PASSWORD_DEFAULT
|
||
|
|
);
|
||
|
|
|
||
|
|
|
||
|
|
/*
|
||
|
|
* =====================================================
|
||
|
|
* MODIFICATION
|
||
|
|
* =====================================================
|
||
|
|
*/
|
||
|
|
|
||
|
|
$sql = "
|
||
|
|
UPDATE login
|
||
|
|
SET pass = :pass
|
||
|
|
WHERE id = :id
|
||
|
|
";
|
||
|
|
|
||
|
|
$stmt = $pdo->prepare($sql);
|
||
|
|
|
||
|
|
$stmt->execute([
|
||
|
|
':pass' => $hash,
|
||
|
|
':id' => $id
|
||
|
|
]);
|
||
|
|
|
||
|
|
|
||
|
|
/*
|
||
|
|
* =====================================================
|
||
|
|
* REPONSE
|
||
|
|
* =====================================================
|
||
|
|
*/
|
||
|
|
|
||
|
|
echo json_encode([
|
||
|
|
'success' => true,
|
||
|
|
'message' => 'Mot de passe modifié.'
|
||
|
|
], JSON_UNESCAPED_UNICODE);
|
||
|
|
} catch (Exception $e) {
|
||
|
|
|
||
|
|
http_response_code(400);
|
||
|
|
|
||
|
|
echo json_encode([
|
||
|
|
'success' => false,
|
||
|
|
'message' => $e->getMessage()
|
||
|
|
], JSON_UNESCAPED_UNICODE);
|
||
|
|
}
|