| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Ensures that getRequestData() is used to access super globals.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer_MySource
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
11 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
12 |
* @version CVS: $Id: GetRequestDataSniff.php 276662 2009-03-02 05:25:04Z squiz $
|
|
|
13 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
14 |
*/
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Ensures that getRequestData() is used to access super globals.
|
|
|
18 |
*
|
|
|
19 |
* @category PHP
|
|
|
20 |
* @package PHP_CodeSniffer_MySource
|
|
|
21 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
22 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
23 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
24 |
* @version Release: 1.2.1
|
|
|
25 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
26 |
*/
|
|
|
27 |
class MySource_Sniffs_PHP_GetRequestDataSniff implements PHP_CodeSniffer_Sniff
|
|
|
28 |
{
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
/**
|
|
|
32 |
* Returns an array of tokens this test wants to listen for.
|
|
|
33 |
*
|
|
|
34 |
* @return array
|
|
|
35 |
*/
|
|
|
36 |
public function register()
|
|
|
37 |
{
|
|
|
38 |
return array(T_VARIABLE);
|
|
|
39 |
|
|
|
40 |
}//end register()
|
|
|
41 |
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* Processes this sniff, when one of its tokens is encountered.
|
|
|
45 |
*
|
|
|
46 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
47 |
* @param int $stackPtr The position of the current token in
|
|
|
48 |
* the stack passed in $tokens.
|
|
|
49 |
*
|
|
|
50 |
* @return void
|
|
|
51 |
*/
|
|
|
52 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
53 |
{
|
|
|
54 |
$tokens = $phpcsFile->getTokens();
|
|
|
55 |
|
|
|
56 |
$varName = $tokens[$stackPtr]['content'];
|
|
|
57 |
if ($varName !== '$_REQUEST'
|
|
|
58 |
&& $varName !== '$_GET'
|
|
|
59 |
&& $varName !== '$_POST'
|
|
|
60 |
) {
|
|
|
61 |
return;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
// The only place these super globals can be accessed directly is
|
|
|
65 |
// in the getRequestData() method of the Security class.
|
|
|
66 |
$inClass = false;
|
|
|
67 |
foreach ($tokens[$stackPtr]['conditions'] as $i => $type) {
|
|
|
68 |
if ($tokens[$i]['code'] === T_CLASS) {
|
|
|
69 |
$className = $phpcsFile->findNext(T_STRING, $i);
|
|
|
70 |
$className = $tokens[$className]['content'];
|
|
|
71 |
if (strtolower($className) === 'security') {
|
|
|
72 |
$inClass = true;
|
|
|
73 |
} else {
|
|
|
74 |
// We don't have nested classes.
|
|
|
75 |
break;
|
|
|
76 |
}
|
|
|
77 |
} else if ($inClass == true && $tokens[$i]['code'] === T_FUNCTION) {
|
|
|
78 |
$funcName = $phpcsFile->findNext(T_STRING, $i);
|
|
|
79 |
$funcName = $tokens[$funcName]['content'];
|
|
|
80 |
if (strtolower($funcName) === 'getrequestdata') {
|
|
|
81 |
// This is valid.
|
|
|
82 |
return;
|
|
|
83 |
} else {
|
|
|
84 |
// We don't have nested functions.
|
|
|
85 |
break;
|
|
|
86 |
}
|
|
|
87 |
}//end if
|
|
|
88 |
}//end foreach
|
|
|
89 |
|
|
|
90 |
// If we get to here, the super global was used incorrectly.
|
|
|
91 |
// First find out how it is being used.
|
|
|
92 |
$globalName = strtolower(substr($varName, 2));
|
|
|
93 |
$usedVar = '';
|
|
|
94 |
|
|
|
95 |
$openBracket = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true);
|
|
|
96 |
if ($tokens[$openBracket]['code'] === T_OPEN_SQUARE_BRACKET) {
|
|
|
97 |
$closeBracket = $tokens[$openBracket]['bracket_closer'];
|
|
|
98 |
$usedVar = $phpcsFile->getTokensAsString(($openBracket + 1), ($closeBracket - $openBracket - 1));
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
$error = "The $varName super global must not be accessed directly; use Security::getRequestData(";
|
|
|
102 |
if ($usedVar !== '') {
|
|
|
103 |
$error .= "$usedVar, '$globalName'";
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
$error .= ') instead';
|
|
|
107 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
108 |
|
|
|
109 |
}//end process()
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
}//end class
|
|
|
113 |
|
|
|
114 |
?>
|