]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Core/PasswordImage.php
Scrutinizer Auto-Fixes (#59)
[galette.git] / galette / lib / Galette / Core / PasswordImage.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Password image (captcha) for galette. The original code was found
7 * in includes/functions.inc.php
8 *
9 * PHP version 5
10 *
11 * Copyright © 2013-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 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
34 * @version SVN: $Id$
35 * @link http://galette.tuxfamily.org
36 * @since Available since 0.7.3.1 - 2012-01-03
37 */
38
39 namespace Galette\Core;
40
41 use Analog\Analog;
42 use Galette\Entity\Adherent;
43
44 /**
45 * Password image (captcha) for galette.
46 *
47 * @category Core
48 * @name PasswordImage
49 * @package 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
55 */
56 class PasswordImage extends AbstractPassword
57 {
58 /**
59 * Default constructor
60 *
61 * @param boolean $clean Whether we should clean expired passwords in database
62 */
63 public function __construct($clean = true)
64 {
65 if ($clean === true) {
66 $this->cleanExpired();
67 }
68 }
69
70 /**
71 * Cleans any password image file older than 1 minute
72 *
73 * @return void
74 */
75 protected function cleanExpired()
76 {
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
81 ) {
82 unlink(GALETTE_TEMPIMAGES_PATH . '/' . $file);
83 }
84 }
85 }
86
87 /**
88 * Generates a new password
89 *
90 * @param null $none To be compatible with parent class
91 *
92 * @return boolean
93 */
94 public function generateNewPassword($none = null)
95 {
96 //second, generate a new password and store it in the database
97 $password = $this->makeRandomPassword();
98 $this->setPassword($password);
99
100 $hash = null;
101 $hash = password_hash($password, PASSWORD_BCRYPT);
102 $this->setHash($hash);
103
104 return true;
105 }
106
107 /**
108 * Get image name
109 *
110 * @return String
111 */
112 public function getImageName()
113 {
114 return 'pw_' . md5($this->getHash()) . '.png';
115 }
116
117 /**
118 * Return base64 encoded image
119 *
120 * @return string
121 */
122 public function getImage()
123 {
124 $file = GALETTE_TEMPIMAGES_PATH . '/' . $this->getImageName();
125 $image_type = false;
126 if (function_exists('exif_imagetype')) {
127 $image_type = exif_imagetype($file);
128 } else {
129 $image_size = getimagesize($file);
130 if (is_array($image_size) && isset($image_size[2])) {
131 $image_type = $image_size[2];
132 }
133 }
134 if ($image_type) {
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);
140 }
141 }
142
143 /**
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().
148 *
149 * @return String Crypted password
150 */
151 public function newImage()
152 {
153 $this->generateNewPassword();
154 $pass = $this->getNewPassword();
155
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();
160
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)
164 @chmod($file, 0644);
165 return $this->getHash();
166 }
167
168 /**
169 * Check for password validity
170 *
171 * @param string $pass Clear password
172 * @param string $crypt Crypted password
173 *
174 * @return boolean
175 * @deprecated Seems no longer used
176 */
177 public function check($pass, $crypt)
178 {
179 return crypt($pass, $crypt) == $crypt;
180 }
181 }