ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ProfileChangeMailTokenDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 {
25  private const TABLE_NAME = 'usr_change_email_token';
26  private const VALIDITY = 300;
27  private $db;
28 
29  public function __construct(
31  ) {
32  $this->db = $db;
33  $this->deleteExpiredEntries();
34  }
35 
36  public function getNewTokenForUser(\ilObjUser $user, string $new_email) : string
37  {
38  $token = hash('md5', $user->getId() . '-' . $user->getEmail());
39  $result = $this->db->replace(
40  self::TABLE_NAME,
41  [
42  'token' => ['text', $token]
43  ],
44  [
45  'new_email' => [\ilDBConstants::T_TEXT, $new_email],
46  'valid_until' => [\ilDBConstants::T_INTEGER, time() + self::VALIDITY]
47  ]
48  );
49 
50  if ($result === 1) {
51  return $token;
52  }
53 
54  return '';
55  }
56 
57  public function getNewEmailForUser(\ilObjUser $user, string $received_token) : string
58  {
59  if (hash('md5', $user->getId() . '-' . $user->getEmail()) !== $received_token) {
60  return '';
61  }
62 
63  $query = $this->db->queryF(
64  'SELECT `new_email` FROM `' . self::TABLE_NAME . '` WHERE `token` = %s AND `valid_until` >= %s',
66  [$received_token, time()]
67  );
68 
69  $result = $this->db->fetchObject($query);
70 
71  if ($result !== null) {
72  return $result->new_email;
73  }
74 
75  return '';
76  }
77 
78  public function deleteEntryByToken(string $token) : void
79  {
80  $query = 'DELETE FROM `' . self::TABLE_NAME . '` WHERE `token` = %s';
81  $this->db->manipulateF($query, [\ilDBConstants::T_TEXT], [$token]);
82  }
83 
84  private function deleteExpiredEntries() : void
85  {
86  $query = 'DELETE FROM `' . self::TABLE_NAME . '` WHERE `valid_until` <= %s';
87  $this->db->manipulateF($query, [\ilDBConstants::T_INTEGER], [time()]);
88  }
89 }
$result
getEmail()
get email address public
getId()
get object id public
$token
Definition: xapitoken.php:52
getNewEmailForUser(\ilObjUser $user, string $received_token)
This Function will check if the token is actually valid for the given user before returning the new e...
$query
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...