]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/Password.php
CS: declare visibility for constants
[galette.git] / galette / lib / Galette / Core / Password.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Password for galette. The original code was found
7 * in includes/functions.inc.php
8 *
9 * PHP version 5
10 *
11 * Copyright © 2003-2014 The Galette Team
12 *
13 * This file is part of Galette (http://galette.tuxfamily.org).
14 *
15 * Galette is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * Galette is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
27 *
28 * @category Core
29 * @package Galette
30 *
31 * @author Frédéric Jacquot <unknown@unknow.com>
32 * @author Georges Khaznadar (password encryption, images) <unknown@unknow.com>
33 * @author Johan Cwiklinski <johan@x-tnd.be>
34 * @copyright 2003-2014 The Galette Team
35 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
36 * @link http://galette.tuxfamily.org
37 * @since Available since 0.7dev - 2009-02-28
38 */
39
40 namespace Galette\Core;
41
42 use Analog\Analog;
43 use Galette\Entity\Adherent;
44
45 /**
46 * Temporary password managment
47 *
48 * @category Core
49 * @name Password
50 * @package Galette
51 * @author Frédéric Jacquot <unknown@unknow.com>
52 * @author Georges Khaznadar (password encryption, images) <unknown@unknow.com>
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2009-2014 The Galette Team
55 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
56 * @link http://galette.tuxfamily.org
57 * @since Available since 0.7dev - 2011-06-16
58 */
59
60 class Password extends AbstractPassword
61 {
62 public const TABLE = 'tmppasswds';
63 public const PK = Adherent::PK;
64
65 private $zdb;
66
67 /**
68 * Default constructor
69 *
70 * @param Db $zdb Database instance:
71 * @param boolean $clean Whether we should clean expired passwords in database
72 */
73 public function __construct(Db $zdb, $clean = true)
74 {
75 $this->zdb = $zdb;
76 if ($clean === true) {
77 $this->cleanExpired();
78 }
79 }
80
81 /**
82 * Remove all old password entries
83 *
84 * @param int $id_adh Member identifier
85 *
86 * @return boolean
87 */
88 private function removeOldEntries($id_adh)
89 {
90 try {
91 $delete = $this->zdb->delete(self::TABLE);
92 $delete->where(self::PK . ' = ' . $id_adh);
93
94 $del = $this->zdb->execute($delete);
95 if ($del) {
96 Analog::log(
97 'Temporary passwords for `' . $id_adh . '` has been removed.',
98 Analog::DEBUG
99 );
100 }
101 } catch (\Exception $e) {
102 Analog::log(
103 'An error has occurred removing old tmppasswords ' .
104 $e->getMessage(),
105 Analog::ERROR
106 );
107 return false;
108 }
109 }
110
111 /**
112 * Generates a new password for specified member
113 *
114 * @param int $id_adh Member identifier
115 *
116 * @return boolean
117 */
118 public function generateNewPassword($id_adh)
119 {
120 //first of all, we'll remove all existant entries for specified id
121 $this->removeOldEntries($id_adh);
122
123 //second, generate a new password and store it in the database
124 $password = $this->makeRandomPassword();
125 $hash = password_hash($password, PASSWORD_BCRYPT);
126
127 try {
128 $values = array(
129 self::PK => $id_adh,
130 'tmp_passwd' => $hash,
131 'date_crea_tmp_passwd' => date('Y-m-d H:i:s')
132 );
133
134 $insert = $this->zdb->insert(self::TABLE);
135 $insert->values($values);
136
137 $add = $this->zdb->execute($insert);
138 if ($add) {
139 Analog::log(
140 'New passwords temporary set for `' . $id_adh . '`.',
141 Analog::DEBUG
142 );
143 $this->setPassword($password);
144 $this->setHash($hash);
145 return true;
146 } else {
147 return false;
148 }
149 } catch (\Exception $e) {
150 Analog::log(
151 "An error occurred trying to add temporary password entry. " .
152 $e->getMessage(),
153 Analog::ERROR
154 );
155 return false;
156 }
157 }
158
159 /**
160 * Remove expired passwords queries (older than 24 hours)
161 *
162 * @return boolean
163 */
164 protected function cleanExpired()
165 {
166 $date = new \DateTime();
167 $date->sub(new \DateInterval('PT24H'));
168
169 try {
170 $delete = $this->zdb->delete(self::TABLE);
171 $delete->where->lessThan(
172 'date_crea_tmp_passwd',
173 $date->format('Y-m-d H:i:s')
174 );
175 $del = $this->zdb->execute($delete);
176 if ($del) {
177 Analog::log(
178 'Old Temporary passwords has been deleted.',
179 Analog::DEBUG
180 );
181 }
182 } catch (\Exception $e) {
183 Analog::log(
184 'An error occurred deleting expired temporary passwords. ' .
185 $e->getMessage(),
186 Analog::WARNING
187 );
188 return false;
189 }
190 }
191
192 /**
193 * Check if requested hash is valid
194 *
195 * @param string $hash the hash
196 *
197 * @return false if hash is not valid, member id otherwise
198 */
199 public function isHashValid($hash)
200 {
201 try {
202 $select = $this->zdb->select(self::TABLE);
203 $select->columns(
204 array(self::PK)
205 )->where(array('tmp_passwd' => $hash));
206
207 $results = $this->zdb->execute($select);
208
209 if ($results->count() > 0) {
210 $result = $results->current();
211 $pk = self::PK;
212 return $result->$pk;
213 } else {
214 return false;
215 }
216 } catch (\Exception $e) {
217 Analog::log(
218 'An error occurred getting requested hash. ' . $e->getMessage(),
219 Analog::WARNING
220 );
221 return false;
222 }
223 }
224
225 /**
226 * Remove a hash that has been used (ie. once password has been updated)
227 *
228 * @param string $hash hash
229 *
230 * @return boolean
231 */
232 public function removeHash($hash)
233 {
234 try {
235 $delete = $this->zdb->delete(self::TABLE);
236 $delete->where(
237 array('tmp_passwd' => $hash)
238 );
239
240 $del = $this->zdb->execute($delete);
241 if ($del) {
242 Analog::log(
243 'Used hash has been successfully remove',
244 Analog::DEBUG
245 );
246 return true;
247 }
248 } catch (\Exception $e) {
249 Analog::log(
250 'An error ocured attempting to delete used hash' .
251 $e->getMessage(),
252 Analog::WARNING
253 );
254 return false;
255 }
256 }
257 }