Completed
Push — master ( 35a619...c36a21 )
by Robbert
100:00 queued 90:25
created

hn_captcha_X1::__construct()   C

Complexity

Conditions 12
Paths 80

Size

Total Lines 44
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156
Metric Value
dl 0
loc 44
ccs 0
cts 24
cp 0
rs 5.1612
cc 12
eloc 16
nc 80
nop 2
crap 156

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 75 and the first side effect is on line 41.

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.

Loading history...
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		= '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $counter_filename.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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_';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $prefix.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $collect_garbage_after.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $maxlifetime.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $garbage_collector_error.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
122
123
124
125
	////////////////////////////////
126
	//
127
	//	PRIVATE PARAMS
128
	//
129
130
		/** @private **/
131
		var $counter_fn_default_basename = 'hn_captcha_counter.txt';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $counter_fn_default_basename.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
146
		  *
147
		  **/
148
		function __construct($config,$secure=TRUE)
149
		{
150
			// Call Constructor of main-class
151
			$this->hn_captcha($config,$secure);
0 ignored issues
show
Bug introduced by
The method hn_captcha() does not seem to exist on object<hn_captcha_X1>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
152
153
154
			// specify counter-filename
155
			if($this->counter_filename == '') $this->counter_filename = $this->tempfolder.$this->counter_fn_default_basename;
0 ignored issues
show
Bug introduced by
The property tempfolder cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
156
			if($this->debug) echo "\n<br>-Captcha-Debug: The counterfilename is (".$this->counter_filename.")";
0 ignored issues
show
Bug introduced by
The property debug cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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.")";
0 ignored issues
show
Bug introduced by
The property debug cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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)";
0 ignored issues
show
Bug introduced by
The property debug cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
177
					$this->txt_counter($this->counter_filename,TRUE,0);
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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)";
0 ignored issues
show
Bug introduced by
The property debug cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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)";
0 ignored issues
show
Bug introduced by
The property debug cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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
0 ignored issues
show
Documentation introduced by
The doc-type counter-value could not be parsed: Unknown type name "counter-value" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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))
0 ignored issues
show
Bug introduced by
The property tempfolder cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
262
			{
263
				$OK = TRUE;
264
				while(false !== ($file = readdir($handle)))
265
				{
266
					if(!is_file($this->tempfolder.$file)) continue;
0 ignored issues
show
Bug introduced by
The property tempfolder cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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)
0 ignored issues
show
Bug introduced by
The property tempfolder cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
274
							{
275
								$trashed++;
276
								$res = @unlink($this->tempfolder.$file);
0 ignored issues
show
Bug introduced by
The property tempfolder cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
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.";
0 ignored issues
show
Bug introduced by
The property debug cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
285
			return $OK;
286
		}
287
288
289
		/** @private **/
290
		function get_filename($public="")
291
		{
292
			if($public=="") $public = $this->public_key;
0 ignored issues
show
Bug introduced by
The property public_key cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
293
			return $this->tempfolder.$this->prefix.$public.".jpg";
0 ignored issues
show
Bug introduced by
The property tempfolder cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
294
		}
295
296
297
		/** @private **/
298
		function get_filename_url($public="")
0 ignored issues
show
Coding Style introduced by
get_filename_url uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
299
		{
300
			if($public=="") $public = $this->public_key;
0 ignored issues
show
Bug introduced by
The property public_key cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
301
			return str_replace($_SERVER['DOCUMENT_ROOT'],'',$this->tempfolder).$this->prefix.$public.".jpg";
0 ignored issues
show
Bug introduced by
The property tempfolder cannot be accessed from this context as it is declared private in class hn_captcha.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
302
		}
303
304
305
} // END CLASS hn_CAPTCHA_X1
306