Issues (1626)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

phpsecinfo/PhpSecInfo/Test/Test.php (56 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 16 and the first side effect is on line 12.

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
 * Skeleton Test class file
4
 *
5
 * @package PhpSecInfo
6
 * @author Ed Finkler <[email protected]>
7
 */
8
9
/**
10
 * require the main PhpSecInfo class
11
 */
12
require_once(PHPSECINFO_BASE_DIR.'/PhpSecInfo.php');
13
14
15
16
define ('PHPSECINFO_TEST_RESULT_OK', -1);
17
18
define ('PHPSECINFO_TEST_RESULT_NOTICE', -2);
19
20
define ('PHPSECINFO_TEST_RESULT_WARN', -4);
21
22
define ('PHPSECINFO_TEST_RESULT_ERROR', -1024);
23
24
define ('PHPSECINFO_TEST_RESULT_NOTRUN', -2048);
25
26
define ('PHPSECINFO_TEST_COMMON_TMPDIR', '/tmp');
27
28
define ('PHPSECINFO_TEST_MOREINFO_BASEURL', 'http://phpsec.org/projects/phpsecinfo/tests/');
29
30
/**
31
 * This is a skeleton class for PhpSecInfo tests  You should extend this to make a "group" skeleton
32
 * to categorize tests under, then make a subdir with your group name that contains test classes
33
 * extending your group skeleton class.
34
 * @package PhpSecInfo
35
 */
36
class PhpSecInfo_Test
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
37
{
38
39
	/**
40
	 * This value is used to group test results together.
41
	 *
42
	 * For example, all tests related to the mysql lib should be grouped under "mysql."
43
	 *
44
	 * @var string
45
	 */
46
	var $test_group = 'misc';
0 ignored issues
show
The visibility should be declared for property $test_group.

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...
47
48
49
	/**
50
	 * This should be a <b>unique</b>, human-readable identifier for this test
51
	 *
52
	 * @var string
53
	 */
54
	var $test_name  = 'misc_test';
0 ignored issues
show
The visibility should be declared for property $test_name.

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...
55
56
57
	/**
58
	 * This is the recommended value the test will be looking for
59
	 *
60
	 * @var mixed
61
	 */
62
	var $recommended_value = "bar";
0 ignored issues
show
The visibility should be declared for property $recommended_value.

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...
63
64
65
	/**
66
	 * The result returned from the test
67
	 *
68
	 * @var integer
69
	 */
70
	var $_result = PHPSECINFO_TEST_RESULT_NOTRUN;
0 ignored issues
show
The visibility should be declared for property $_result.

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...
71
72
73
	/**
74
	 * The message corresponding to the result of the test
75
	 *
76
	 * @var string
77
	 */
78
	var $_message;
0 ignored issues
show
The visibility should be declared for property $_message.

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...
79
80
81
	/**
82
	 * the language code.  Should be a pointer to the setting in the PhpSecInfo object
83
	 *
84
	 * @var string
85
	 */
86
	var $_language = PHPSECINFO_LANG_DEFAULT;
0 ignored issues
show
The visibility should be declared for property $_language.

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...
87
88
	/**
89
	 * Enter description here...
90
	 *
91
	 * @var mixed
92
	 */
93
	var $current_value;
0 ignored issues
show
The visibility should be declared for property $current_value.

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...
94
95
	/**
96
	 * This is a hash of messages that correspond to various test result levels.
97
	 *
98
	 * There are five messages, each corresponding to one of the result constants
99
	 * (PHPSECINFO_TEST_RESULT_OK, PHPSECINFO_TEST_RESULT_NOTICE, PHPSECINFO_TEST_RESULT_WARN,
100
	 * PHPSECINFO_TEST_RESULT_ERROR, PHPSECINFO_TEST_RESULT_NOTRUN)
101
	 *
102
	 *
103
	 * @var array array
104
	 */
105
	var $_messages = array();
0 ignored issues
show
The visibility should be declared for property $_messages.

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
109
110
	/**
111
	 * Constructor for Test skeleton class
112
	 *
113
	 * @return PhpSecInfo_Test
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...
114
	 */
115
	function __construct() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
116
		//$this->_setTestValues();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
117
118
		$this->_retrieveCurrentValue();
119
		//$this->setRecommendedValue();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
120
121
		$this->_setMessages();
122
	}
123
124
125
	/**
126
	 * Determines whether or not it's appropriate to run this test (for example, if
127
	 * this test is for a particular library, it shouldn't be run if the lib isn't
128
	 * loaded).
129
	 *
130
	 * This is a terrible name, but I couldn't think of a better one atm.
131
	 *
132
	 * @return boolean
133
	 */
134
	function isTestable() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
135
136
		return true;
137
	}
138
139
140
	/**
141
	 * The "meat" of the test.  This is where the real test code goes.  You should override this when extending
142
	 *
143
	 * @return integer
144
	 */
145
	function _execTest() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
146
147
		return PHPSECINFO_TEST_RESULT_NOTRUN;
148
	}
149
150
151
	/**
152
	 * This function loads up result messages into the $this->_messages array.
153
	 *
154
	 * Using this method rather than setting $this->_messages directly allows result
155
	 * messages to be inherited.  This is broken out into a separate function rather
156
	 * than the constructor for ease of extension purposes (php4 is whack, man).
157
	 *
158
	 */
159 View Code Duplication
	function _setMessages() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
160
		$this->setMessageForResult(PHPSECINFO_TEST_RESULT_OK,		'en', 'This setting should be safe');
161
		$this->setMessageForResult(PHPSECINFO_TEST_RESULT_NOTICE,	'en', 'This could potentially be a security issue');
162
		$this->setMessageForResult(PHPSECINFO_TEST_RESULT_WARN,	'en', 'This setting may be a serious security problem');
163
		$this->setMessageForResult(PHPSECINFO_TEST_RESULT_ERROR,	'en', 'There was an error running this test');
164
		$this->setMessageForResult(PHPSECINFO_TEST_RESULT_NOTRUN,	'en', 'This test cannot be run');
165
	}
166
167
168
	/**
169
	 * Placeholder - extend for tests
170
	 *
171
	 */
172
	function _retrieveCurrentValue() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
173
		$this->current_value = "foo";
174
	}
175
176
177
178
	/**
179
	 * This is the wrapper that executes the test and sets the result code and message
180
	 */
181
	function test() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
182
		$result = $this->_execTest();
183
		$this->_setResult($result);
184
185
	}
186
187
188
189
	/**
190
	 * Retrieves the result
191
	 *
192
	 * @return integer
193
	 */
194
	function getResult() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
195
		return $this->_result;
196
	}
197
198
199
200
201
	/**
202
	 * Retrieves the message for the current result
203
	 *
204
	 * @return string
0 ignored issues
show
Should the return type not be null|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
205
	 */
206
	function getMessage() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
207
		if (!isset($this->_message) || empty($this->_message)) {
208
			$this->_setMessage($this->_result, $this->_language);
209
		}
210
211
		return $this->_message;
212
	}
213
214
215
216
	/**
217
	 * Sets the message for a given result code and language
218
	 *
219
	 * <code>
220
	 * $this->setMessageForResult(PHPSECINFO_TEST_RESULT_NOTRUN,	'en', 'This test cannot be run');
221
	 * </code>
222
	 *
223
	 * @param integer $result_code
224
	 * @param string $language_code
225
	 * @param string $message
226
	 *
227
	 */
228
	function setMessageForResult($result_code, $language_code, $message) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
229
230
		if ( !isset($this->_messages[$result_code]) ) {
231
			$this->_messages[$result_code] = array();
232
		}
233
234
		if ( !is_array($this->_messages[$result_code]) ) {
235
			$this->_messages[$result_code] = array();
236
		}
237
238
		$this->_messages[$result_code][$language_code] = $message;
239
240
	}
241
242
243
244
245
	/**
246
	 * returns the current value.  This function should be used to access the
247
	 * value for display.  All values are cast as strings
248
	 *
249
	 * @return string
250
	 */
251
	function getCurrentTestValue() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
252
		return $this->getStringValue($this->current_value);
253
	}
254
255
	/**
256
	 * returns the recommended value.  This function should be used to access the
257
	 * value for display.  All values are cast as strings
258
	 *
259
	 * @return string
260
	 */
261
	function getRecommendedTestValue() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
262
		return $this->getStringValue($this->recommended_value);
263
	}
264
265
266
	/**
267
	 * Sets the result code
268
	 *
269
	 * @param integer $result_code
270
	 */
271
	function _setResult($result_code) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
272
		$this->_result = $result_code;
273
	}
274
275
276
	/**
277
	 * Sets the $this->_message variable based on the passed result and language codes
278
	 *
279
	 * @param integer $result_code
280
	 * @param string $language_code
281
	 */
282
	function _setMessage($result_code, $language_code) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
283
		$messages = $this->_messages[$result_code];
284
		$message  = $messages[$language_code];
285
		$this->_message = $message;
286
	}
287
288
289
	/**
290
	 * Returns a link to a page with detailed information about the test
291
	 *
292
	 * URL is formatted as PHPSECINFO_TEST_MOREINFO_BASEURL + testName
293
	 *
294
	 * @see PHPSECINFO_TEST_MOREINFO_BASEURL
295
	 *
296
	 * @return string|boolean
297
	 */
298
	function getMoreInfoURL() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
299
		if ($tn = $this->getTestName()) {
300
			return PHPSECINFO_TEST_MOREINFO_BASEURL.strtolower("{$tn}.html");
301
		} else {
302
			return false;
303
		}
304
	}
305
306
307
308
309
	/**
310
	 * This retrieves the name of this test.
311
	 *
312
	 * If a name has not been set, this returns a formatted version of the class name.
313
	 *
314
	 * @return string
315
	 */
316
	function getTestName() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
317
		if (isset($this->test_name) && !empty($this->test_name)) {
318
			return $this->test_name;
319
		} else {
320
			return ucwords(
321
			str_replace('_', ' ',
322
			get_class($this)
323
			)
324
			);
325
		}
326
327
	}
328
329
330
	/**
331
	 * sets the test name
332
	 *
333
	 * @param string $test_name
334
	 */
335
	function setTestName($test_name) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
336
		$this->test_name = $test_name;
337
	}
338
339
340
	/**
341
	 * Returns the test group this test belongs to
342
	 *
343
	 * @return string
344
	 */
345
	function getTestGroup() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
346
		return $this->test_group;
347
	}
348
349
350
	/**
351
	 * sets the test group
352
	 *
353
	 * @param string $test_group
354
	 */
355
	function setTestGroup($test_group) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
356
		$this->test_group = $test_group;
357
	}
358
359
360
	/**
361
	 * This function takes the shorthand notation used in memory limit settings for PHP
362
	 * and returns the byte value.  Totally stolen from http://us3.php.net/manual/en/function.ini-get.php
363
	 *
364
	 * <code>
365
	 * echo 'post_max_size in bytes = ' . $this->return_bytes(ini_get('post_max_size'));
366
	 * </code>
367
	 *
368
	 * @link http://php.net/manual/en/function.ini-get.php
369
	 * @param string $val
370
	 * @return integer
0 ignored issues
show
Should the return type not be integer|double|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
371
	 */
372
	function returnBytes($val) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
373
		$val = trim($val);
374
375
		if ( (int)$val === 0 ) {
376
			return 0;
377
		}
378
379
		$last = strtolower($val{strlen($val)-1});
380
		switch($last) {
381
			// The 'G' modifier is available since PHP 5.1.0
382
			case 'g':
0 ignored issues
show
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
383
				$val *= 1024;
384
			case 'm':
0 ignored issues
show
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
385
				$val *= 1024;
386
			case 'k':
387
				$val *= 1024;
388
		}
389
390
		return $val;
391
	}
392
393
394
	/**
395
	 * This just does the usual PHP string casting, except for
396
	 * the boolean FALSE value, where the string "0" is returned
397
	 * instead of an empty string
398
	 *
399
	 * @param mixed $val
400
	 * @return string
401
	 */
402
	function getStringValue($val) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
403
		if ($val === FALSE) {
404
			return "0";
405
		} else {
406
			return (string)$val;
407
		}
408
	}
409
410
411
	/**
412
	 * This method converts the several possible return values from
413
	 * allegedly "boolean" ini settings to proper booleans
414
	 *
415
	 * Properly converted input values are: 'off', 'on', 'false', 'true', '', '0', '1'
416
	 * (the last two might not be neccessary, but I'd rather be safe)
417
	 *
418
	 * If the ini_value doesn't match any of those, the value is returned as-is.
419
	 *
420
	 * @param string $ini_key   the ini_key you need the value of
421
	 * @return boolean|mixed
422
	 */
423
	function getBooleanIniValue($ini_key) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
424
425
		$ini_val = ini_get($ini_key);
426
427
		switch ( strtolower($ini_val) ) {
428
429
			case 'off':
430
				return false;
431
				break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
432
			case 'on':
433
				return true;
434
				break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
435
			case 'false':
436
				return false;
437
				break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
438
			case 'true':
439
				return true;
440
				break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
441
			case '0':
442
				return false;
443
				break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
444
			case '1':
445
				return true;
446
				break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
447
			case '':
448
				return false;
449
				break;
0 ignored issues
show
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
450
			default:
451
				return $ini_val;
452
453
		}
454
455
	}
456
457
	/**
458
	 * sys_get_temp_dir provides some temp dir detection capability
459
	 * that is lacking in versions of PHP that do not have the
460
	 * sys_get_temp_dir() function
461
	 *
462
	 * @return string|NULL
463
	 */
464
	function sys_get_temp_dir() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
465
		// Try to get from environment variable
466
		$vars = array('TMP', 'TMPDIR', 'TEMP');
467
		foreach($vars as $var) {
468
			$tmp = getenv($var);
469
			if ( !empty($tmp) ) {
470
				return realpath( $tmp );
471
			}
472
		}
473
		return NULL;
474
	}
475
476
477
	/**
478
	 * A quick function to determine whether we're running on Windows.
479
	 * Uses the PHP_OS constant.
480
	 *
481
	 * @return boolean
482
	 */
483
	function osIsWindows() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
484
		if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
485
			return true;
486
		} else {
487
			return false;
488
		}
489
	}
490
491
492
	/**
493
	 * Returns an array of data returned from the UNIX 'id' command
494
	 *
495
	 * includes uid, username, gid, groupname, and groups (if "exec"
496
	 * is enabled). Groups is an array of all the groups the user
497
	 * belongs to.  Keys are the group ids, values are the group names.
498
	 *
499
	 * returns FALSE if no suitable function is available to retrieve
500
	 * the data
501
	 *
502
	 * @return array|boolean
503
	 */
504
	function getUnixId() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
505
506
		if ($this->osIsWindows()) {
507
			return false;
508
		}
509
510
		$success = false;
511
512
513
		if (function_exists("exec") && !PhpSecInfo_Test::getBooleanIniValue('safe_mode')) {
0 ignored issues
show
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.

Loading history...
514
			$id_raw = exec('id');
515
			// uid=1000(coj) gid=1000(coj) groups=1000(coj),1001(admin)
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
516
			preg_match( "|uid=(\d+)\((\S+)\)\s+gid=(\d+)\((\S+)\)\s+groups=(.+)|i",
517
						$id_raw,
518
						$matches);
519
520
			if (!$matches) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $matches of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
521
				/**
522
				 * for some reason the output from 'id' wasn't as we expected.
523
				 * return false so the test doesn't run.
524
				 */
525
				$success = false;
526
			} else {
527
				$id_data = array(	'uid'=>$matches[1],
528
									'username'=>$matches[2],
529
									'gid'=>$matches[3],
530
									'group'=>$matches[4] );
531
532
				$groups = array();
533
				if ($matches[5]) {
534
					$gs = $matches[5];
535
					$gs = explode(',', $gs);
536
					foreach ($gs as $groupstr) {
537
						if (preg_match("/(\d+)\(([^\)]+)\)/", $groupstr, $subs)) {
538
							$groups[$subs[1]] = $subs[2];
539
						} else {
540
							$groups[$groupstr] = '';
541
						}
542
					}
543
					ksort($groups);
544
				}
545
				$id_data['groups'] = $groups;
546
				$success = true;
547
			}
548
549
		}
550
551
		if (!$success && function_exists("posix_getpwuid") && function_exists("posix_geteuid")
552
			&& function_exists('posix_getgrgid') && function_exists('posix_getgroups') ) {
553
			$data = posix_getpwuid( posix_getuid() );
554
			$id_data['uid'] = $data['uid'];
0 ignored issues
show
The variable $id_data does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
555
			$id_data['username'] = $data['name'];
556
			$id_data['gid'] = $data['gid'];
557
			//$group_data = posix_getgrgid( posix_getegid() );
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
558
			//$id_data['group'] = $group_data['name'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
559
			$id_data['groups'] = array();
560
			$groups = posix_getgroups();
561
			foreach ( $groups as $gid ) {
562
				//$group_data = posix_getgrgid(posix_getgid());
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
563
				$id_data['groups'][$gid] = '<unknown>';
564
			}
565
			$success = true;
566
		}
567
568
		if ($success) {
569
			return $id_data;
570
		} else {
571
			return false;
572
		}
573
	}
574
575
}
576