|
1
|
|
|
<?php |
|
|
|
|
|
|
2
|
|
|
/** |
|
3
|
|
|
* PHP-Class hn_captcha_X1 Version 1.0, released 19-Apr-2004 |
|
4
|
|
|
* is an extension for PHP-Class hn_captcha. |
|
5
|
|
|
* It adds a garbage-collector. (Useful, if you cannot use cronjobs.) |
|
6
|
|
|
* Author: Horst Nogajski, [email protected] |
|
7
|
|
|
* |
|
8
|
|
|
* License: GNU GPL (http://www.opensource.org/licenses/gpl-license.html) |
|
9
|
|
|
* Download: http://hn273.users.phpclasses.org/browse/package/1569.html |
|
10
|
|
|
* |
|
11
|
|
|
* If you find it useful, you might rate it on http://www.phpclasses.org/rate.html |
|
12
|
|
|
* If you use this class in a productional environment, you might drop me a note, so I can add a link to the page. |
|
13
|
|
|
* |
|
14
|
|
|
**/ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* License: GNU GPL (http://www.opensource.org/licenses/gpl-license.html) |
|
18
|
|
|
* |
|
19
|
|
|
* This program is free software; |
|
20
|
|
|
* |
|
21
|
|
|
* you can redistribute it and/or modify it under the terms of the GNU General Public License |
|
22
|
|
|
* as published by the Free Software Foundation; either version 2 of the License, |
|
23
|
|
|
* or (at your option) any later version. |
|
24
|
|
|
* |
|
25
|
|
|
* This program is distributed in the hope that it will be useful, |
|
26
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
27
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|
28
|
|
|
* |
|
29
|
|
|
* You should have received a copy of the GNU General Public License along with this program; |
|
30
|
|
|
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
31
|
|
|
* |
|
32
|
|
|
**/ |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Tabsize: 4 |
|
36
|
|
|
* |
|
37
|
|
|
**/ |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
require_once(dirname(__file__).'/hn_captcha.class.php'); |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* This class is an extension for hn_captcha-class. It adds a garbage-collector! |
|
47
|
|
|
* |
|
48
|
|
|
* Normally all used images will be deleted automatically. But everytime a user |
|
49
|
|
|
* doesn't finish a request one image stays as garbage in tempfolder. |
|
50
|
|
|
* With this extension you can collect & trash this. |
|
51
|
|
|
* |
|
52
|
|
|
* You can specify: |
|
53
|
|
|
* - when the garbage-collector should run, (default = after 100 calls) |
|
54
|
|
|
* - the maxlifetime for images, (default is 600, = 10 minutes) |
|
55
|
|
|
* - a filename-prefix for the captcha-images (default = 'hn_captcha_') |
|
56
|
|
|
* - absolute filename for a textfile which stores the current counter-value |
|
57
|
|
|
* (default is $tempfolder.'hn_captcha_counter.txt') |
|
58
|
|
|
* |
|
59
|
|
|
* The classextension needs the filename-prefix to identify lost images |
|
60
|
|
|
* also if the tempfolder is shared with other scripts. |
|
61
|
|
|
* |
|
62
|
|
|
* If an error occures (with counting or trash-file-deleting), the class sets |
|
63
|
|
|
* the variable $classhandle->garbage_collector_error to TRUE. |
|
64
|
|
|
* You can check this in your scripts and if is TRUE, you might execute |
|
65
|
|
|
* an email-notification or something else. |
|
66
|
|
|
* |
|
67
|
|
|
* |
|
68
|
|
|
* @shortdesc Class that adds a garbage-collector to the class hn_captcha |
|
69
|
|
|
* @public |
|
70
|
|
|
* @author Horst Nogajski, (mail: [email protected]) |
|
71
|
|
|
* @version 1.0 |
|
72
|
|
|
* @date 2004-April-19 |
|
73
|
|
|
* |
|
74
|
|
|
**/ |
|
75
|
|
|
class hn_captcha_X1 extends hn_captcha |
|
76
|
|
|
{ |
|
77
|
|
|
|
|
78
|
|
|
//////////////////////////////// |
|
79
|
|
|
// |
|
80
|
|
|
// PUBLIC PARAMS |
|
81
|
|
|
// |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @shortdesc You optionally can specify an absolute filename for the counter. If is not specified, the class use the tempfolder and the default_basename. |
|
85
|
|
|
* @public |
|
86
|
|
|
* @type string |
|
87
|
|
|
* |
|
88
|
|
|
**/ |
|
89
|
|
|
var $counter_filename = ''; |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @shortdesc This is used as prefix for the picture filenames, so we can identify them also if we share the tempfolder with other programs. |
|
93
|
|
|
* @public |
|
94
|
|
|
* @type string |
|
95
|
|
|
* |
|
96
|
|
|
**/ |
|
97
|
|
|
var $prefix = 'hn_captcha_'; |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @shortdesc The garbage-collector will started once when the class was called that number times. |
|
101
|
|
|
* @public |
|
102
|
|
|
* @type integer |
|
103
|
|
|
* |
|
104
|
|
|
**/ |
|
105
|
|
|
var $collect_garbage_after = 100; |
|
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @shortdesc Only trash files which are older than this number of seconds. |
|
109
|
|
|
* @public |
|
110
|
|
|
* @type integer |
|
111
|
|
|
* |
|
112
|
|
|
**/ |
|
113
|
|
|
var $maxlifetime = 600; |
|
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @shortdesc This becomes TRUE if the counter doesn't work or if trashfiles couldn't be deleted. |
|
117
|
|
|
* @public |
|
118
|
|
|
* @type boolean |
|
119
|
|
|
* |
|
120
|
|
|
**/ |
|
121
|
|
|
var $garbage_collector_error = FALSE; |
|
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
//////////////////////////////// |
|
126
|
|
|
// |
|
127
|
|
|
// PRIVATE PARAMS |
|
128
|
|
|
// |
|
129
|
|
|
|
|
130
|
|
|
/** @private **/ |
|
131
|
|
|
var $counter_fn_default_basename = 'hn_captcha_counter.txt'; |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
//////////////////////////////// |
|
137
|
|
|
// |
|
138
|
|
|
// CONSTRUCTOR |
|
139
|
|
|
// |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @shortdesc This calls the constructor of main-class for extracting the config array and generating all needed params. Additionally it control the garbage-collector. |
|
143
|
|
|
* @public |
|
144
|
|
|
* @type void |
|
145
|
|
|
* @return nothing |
|
|
|
|
|
|
146
|
|
|
* |
|
147
|
|
|
**/ |
|
148
|
|
|
function __construct($config,$secure=TRUE) |
|
149
|
|
|
{ |
|
150
|
|
|
// Call Constructor of main-class |
|
151
|
|
|
$this->hn_captcha($config,$secure); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
// specify counter-filename |
|
155
|
|
|
if($this->counter_filename == '') $this->counter_filename = $this->tempfolder.$this->counter_fn_default_basename; |
|
|
|
|
|
|
156
|
|
|
if($this->debug) echo "\n<br>-Captcha-Debug: The counterfilename is (".$this->counter_filename.")"; |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
// retrieve last counter-value |
|
160
|
|
|
$test = $this->txt_counter($this->counter_filename); |
|
161
|
|
|
|
|
162
|
|
|
// set and retrieve current counter-value |
|
163
|
|
|
$counter = $this->txt_counter($this->counter_filename,TRUE); |
|
164
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
// check if counter works correct |
|
167
|
|
|
if(($counter !== FALSE) && ($counter - $test == 1)) |
|
168
|
|
|
{ |
|
169
|
|
|
// Counter works perfect, =:) |
|
170
|
|
|
if($this->debug) echo "\n<br>-Captcha-Debug: Current counter-value is ($counter). Garbage-collector should start at (".$this->collect_garbage_after.")"; |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
// check if garbage-collector should run |
|
173
|
|
|
if($counter >= $this->collect_garbage_after) |
|
174
|
|
|
{ |
|
175
|
|
|
// Reset counter |
|
176
|
|
|
if($this->debug) echo "\n<br>-Captcha-Debug: Reset the counter-value. (0)"; |
|
|
|
|
|
|
177
|
|
|
$this->txt_counter($this->counter_filename,TRUE,0); |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
// start garbage-collector |
|
180
|
|
|
$this->garbage_collector_error = $this->collect_garbage() ? FALSE : TRUE; |
|
181
|
|
|
if($this->debug && $this->garbage_collector_error) echo "\n<br>-Captcha-Debug: ERROR! SOME TRASHFILES COULD NOT BE DELETED! (Set the garbage_collector_error to TRUE)"; |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
else |
|
186
|
|
|
{ |
|
187
|
|
|
// Counter-ERROR! |
|
188
|
|
|
if($this->debug) echo "\n<br>-Captcha-Debug: ERROR! NO COUNTER-VALUE AVAILABLE! (Set the garbage_collector_error to TRUE)"; |
|
|
|
|
|
|
189
|
|
|
$this->garbage_collector_error = TRUE; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
//////////////////////////////// |
|
195
|
|
|
// |
|
196
|
|
|
// PRIVATE METHODS |
|
197
|
|
|
// |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @shortdesc Store/Retrieve a counter-value in/from a textfile. Optionally count it up or store a (as third param) specified value. |
|
202
|
|
|
* @private |
|
203
|
|
|
* @type integer |
|
204
|
|
|
* @return counter-value |
|
|
|
|
|
|
205
|
|
|
* |
|
206
|
|
|
**/ |
|
207
|
|
|
function txt_counter($filename,$add=FALSE,$fixvalue=FALSE) |
|
208
|
|
|
{ |
|
209
|
|
|
if(is_file($filename) ? TRUE : touch($filename)) |
|
210
|
|
|
{ |
|
211
|
|
|
if(is_readable($filename) && is_writable($filename)) |
|
212
|
|
|
{ |
|
213
|
|
|
$fp = @fopen($filename, "r"); |
|
214
|
|
|
if($fp) |
|
215
|
|
|
{ |
|
216
|
|
|
$counter = (int)trim(fgets($fp)); |
|
217
|
|
|
fclose($fp); |
|
218
|
|
|
|
|
219
|
|
|
if($add) |
|
220
|
|
|
{ |
|
221
|
|
|
if($fixvalue !== FALSE) |
|
222
|
|
|
{ |
|
223
|
|
|
$counter = (int)$fixvalue; |
|
224
|
|
|
} |
|
225
|
|
|
else |
|
226
|
|
|
{ |
|
227
|
|
|
$counter++; |
|
228
|
|
|
} |
|
229
|
|
|
$fp = @fopen($filename, "w"); |
|
230
|
|
|
if($fp) |
|
231
|
|
|
{ |
|
232
|
|
|
fputs($fp,$counter); |
|
233
|
|
|
fclose($fp); |
|
234
|
|
|
return $counter; |
|
235
|
|
|
} |
|
236
|
|
|
else return FALSE; |
|
237
|
|
|
} |
|
238
|
|
|
else |
|
239
|
|
|
{ |
|
240
|
|
|
return $counter; |
|
241
|
|
|
} |
|
242
|
|
|
} |
|
243
|
|
|
else return FALSE; |
|
244
|
|
|
} |
|
245
|
|
|
else return FALSE; |
|
246
|
|
|
} |
|
247
|
|
|
else return FALSE; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* @shortdesc Scanns the tempfolder for jpeg-files with nameprefix used by the class and trash them if they are older than maxlifetime. |
|
253
|
|
|
* @private |
|
254
|
|
|
* |
|
255
|
|
|
**/ |
|
256
|
|
|
function collect_garbage() |
|
257
|
|
|
{ |
|
258
|
|
|
$OK = FALSE; |
|
259
|
|
|
$captchas = 0; |
|
260
|
|
|
$trashed = 0; |
|
261
|
|
|
if($handle = @opendir($this->tempfolder)) |
|
|
|
|
|
|
262
|
|
|
{ |
|
263
|
|
|
$OK = TRUE; |
|
264
|
|
|
while(false !== ($file = readdir($handle))) |
|
265
|
|
|
{ |
|
266
|
|
|
if(!is_file($this->tempfolder.$file)) continue; |
|
|
|
|
|
|
267
|
|
|
// check for name-prefix, extension and filetime |
|
268
|
|
|
if(substr($file,0,strlen($this->prefix)) == $this->prefix) |
|
269
|
|
|
{ |
|
270
|
|
|
if(strrchr($file, ".") == ".jpg") |
|
271
|
|
|
{ |
|
272
|
|
|
$captchas++; |
|
273
|
|
|
if((time() - filemtime($this->tempfolder.$file)) >= $this->maxlifetime) |
|
|
|
|
|
|
274
|
|
|
{ |
|
275
|
|
|
$trashed++; |
|
276
|
|
|
$res = @unlink($this->tempfolder.$file); |
|
|
|
|
|
|
277
|
|
|
if(!$res) $OK = FALSE; |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
closedir($handle); |
|
283
|
|
|
} |
|
284
|
|
|
if($this->debug) echo "\n<br>-Captcha-Debug: There are ($captchas) captcha-images in tempfolder, where ($trashed) are seems to be lost."; |
|
|
|
|
|
|
285
|
|
|
return $OK; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
|
|
289
|
|
|
/** @private **/ |
|
290
|
|
|
function get_filename($public="") |
|
291
|
|
|
{ |
|
292
|
|
|
if($public=="") $public = $this->public_key; |
|
|
|
|
|
|
293
|
|
|
return $this->tempfolder.$this->prefix.$public.".jpg"; |
|
|
|
|
|
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
|
|
297
|
|
|
/** @private **/ |
|
298
|
|
|
function get_filename_url($public="") |
|
|
|
|
|
|
299
|
|
|
{ |
|
300
|
|
|
if($public=="") $public = $this->public_key; |
|
|
|
|
|
|
301
|
|
|
return str_replace($_SERVER['DOCUMENT_ROOT'],'',$this->tempfolder).$this->prefix.$public.".jpg"; |
|
|
|
|
|
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
} // END CLASS hn_CAPTCHA_X1 |
|
306
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.