]>
git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/PasswordImage.php
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Password image (captcha) for galette. The original code was found
7 * in includes/functions.inc.php
11 * Copyright © 2013-2014 The Galette Team
13 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
31 * @author Johan Cwiklinski <johan@x-tnd.be>
32 * @copyright 2013-2014 The Galette Team
33 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
35 * @link http://galette.tuxfamily.org
36 * @since Available since 0.7.3.1 - 2012-01-03
39 namespace Galette\Core
;
42 use Galette\Entity\Adherent
;
45 * Password image (captcha) for galette.
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2013-2014 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.7.3.1 - 2012-01-03
56 class PasswordImage
extends AbstractPassword
61 * @param boolean $clean Whether we should clean expired passwords in database
63 public function __construct($clean = true)
65 if ($clean === true) {
66 $this->cleanExpired();
71 * Cleans any password image file older than 1 minute
75 protected function cleanExpired()
77 $dh = @opendir
(GALETTE_TEMPIMAGES_PATH
);
78 while ($file = readdir($dh)) {
79 if (substr($file, 0, 3) == 'pw_'
80 && time() - filemtime(GALETTE_TEMPIMAGES_PATH
. '/' . $file) > 60
82 unlink(GALETTE_TEMPIMAGES_PATH
. '/' . $file);
88 * Generates a new password
90 * @param null $none To be compatible with parent class
94 public function generateNewPassword($none = null)
96 //second, generate a new password and store it in the database
97 $password = $this->makeRandomPassword();
98 $this->setPassword($password);
101 $hash = password_hash($password, PASSWORD_BCRYPT
);
102 $this->setHash($hash);
112 public function getImageName()
114 return 'pw_' . md5($this->getHash()) . '.png';
118 * Return base64 encoded image
122 public function getImage()
124 $file = GALETTE_TEMPIMAGES_PATH
. '/' . $this->getImageName();
126 if (function_exists('exif_imagetype')) {
127 $image_type = exif_imagetype($file);
129 $image_size = getimagesize($file);
130 if (is_array($image_size) && isset($image_size[2])) {
131 $image_type = $image_size[2];
135 /*return str_replace(GALETTE_ROOT, '', $file);*/
136 $filetype = pathinfo($file, PATHINFO_EXTENSION
);
137 $imgbinary = @file_get_contents
($file);
138 return 'data:image/' . $filetype . ';base64,' .
139 base64_encode($imgbinary);
144 * Outputs a png image for a random password
145 * and a crypted string for it. The filename
146 * for this image can be computed from the crypted
147 * string by getPasswordImageName().
149 * @return String Crypted password
151 public function newImage()
153 $this->generateNewPassword();
154 $pass = $this->getNewPassword();
156 $png = imagecreate(10 +
7.5*strlen($pass), 18);
157 $bg = imagecolorallocate($png, 160, 160, 160);
158 imagestring($png, 3, 5, 2, $pass, imagecolorallocate($png, 0, 0, 0));
159 $file = GALETTE_TEMPIMAGES_PATH
. '/' . $this->getImageName();
161 imagepng($png, $file);
162 // The perms of the file can be wrong, try to correct it
163 // WARN : chmod() can be desacivated (i.e. : Free/Online)
165 return $this->getHash();
169 * Check for password validity
171 * @param string $pass Clear password
172 * @param string $crypt Crypted password
175 * @deprecated Seems no longer used
177 public function check($pass, $crypt)
179 return crypt($pass, $crypt) == $crypt;