Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 *
19
 * @package    block
20
 * @subpackage rate_course
21
 * @copyright  2009 Jenny Gray
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 *
24
 * Code was Rewritten for Moodle 2.X By Atar + Plus LTD for Comverse LTD.
25
 * @copyright &copy; 2011 Comverse LTD.
26
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
27
 */
28
 
29
 
30
if (isset($_SERVER['REMOTE_ADDR'])) {
31
    define('LINEFEED', "<br />");
32
} else {
33
    define('LINEFEED', "\n");
34
}
35
 
36
// List of patterns to search.
37
 
38
$dml = array (
39
    '(begin|commit|rollback)_sql',
40
    'count_records(_select|_sql)?',
41
    'delete_records(_select)?',
42
    'get_field(set)?(_select|sql)?',
43
    'get_record(s|set)?(_list|_menu|_select|_sql)?(_menu)?',
44
    'insert_record',
45
    'record_exists(_select|_sql)?',
46
    'records_to_menu',
47
    'recordset_to_(array|menu)',
48
    'rs_(EOF|close|fetch_next_record|fetch_record|next_record)',
49
    'set_field(_select)?',
50
    'update_record',
51
);
52
 
53
$helper = array (
54
    'db_(lowercase|uppercase)',
55
    'sql_(as|bitand|bitnot|bitor|bitxor|cast_char2int|ceil|compare_text|concat|concat_join|empty|fullname|ilike|isempty|isnotempty|length|max|null_from_clause|order_by_text|paging_limit|position|substr)'
56
);
57
 
58
$ddl = array (
59
    'add_(field|index|key)',
60
    'change_field_(default|enum|notnull|precision|type|unsigned)',
61
    'create_(table|temp_table)',
62
    'drop_(field|index|key|table)',
63
    'find_(check_constraint_name|index_name|key_name|sequence_name)',
64
    'rename_(field|index|key|table)',
65
    '(check_constraint|field|index|table)_exists'
66
);
67
 
68
$coreonly = array (
69
    'delete_tables_from_xmldb_file',
70
    'drop_plugin_tables',
71
    'get_db_directories',
72
    'get_used_table_names',
73
    'install_from_xmldb_file',
74
);
75
 
76
$internal = array (
77
    'change_db_encoding',
78
    'configure_dbconnection',
79
    'db_(detect_lobs|update_lobs)',
80
    'execute_sql(_arr)?',
81
    'onespace2empty',
82
    'oracle_dirty_hack',
83
    'rcache_(get|getforfill|releaseforfill|set|unset|unset_table)',
84
    'where_clause'
85
);
86
 
87
$unsupported = array (
88
    'column_type',
89
    'table_column',
90
    'modify_database',
91
    '(Execute|Connect|PConnect|ErrorMsg)',
92
    '(MetaTables|MetaColumns|MetaColumnNames|MetaPrimaryKeys|MetaIndexes)'
93
);
94
 
95
$other = array (
96
    '\$db[,; -]',
97
    "[^\$_'\"\.-]dbfamily",
98
    "[^\$_'\"\.-]dblibrary",
99
    "[^\$_'\"\.-]dbtype[^s]",
100
    'sql_substr\(\)'
101
);
102
 
103
// Getting current dir.
104
$dir = dirname(__FILE__);
105
 
106
echo $me . LINEFEED;
107
 
108
// Calculating megarules.
109
$dml_megarule        = calculate_megarule($dml, array('[ =@.]'), array('( )?\('), 'i');
110
$helper_megarule     = calculate_megarule($helper, array('[ =@.]'), array('( )?\('), 'i');
111
$ddl_megarule        = calculate_megarule($ddl, array('[ =@.]'), array('( )?\('), 'i');
112
$coreonly_megarule    = calculate_megarule($coreonly, array('[ =@.]'), array('( )?\('), 'i');
113
$internal_megarule   = calculate_megarule($internal, array('[ =@.]'), array('( )?\('), 'i');
114
$unsupported_megarule= calculate_megarule($unsupported, array('[ \>=@,.]'), array('( )?\('), 'i');
115
$other_megarule      = calculate_megarule($other);
116
 
117
// List of exceptions that aren't errors (function declarations, comments and some harcoded strings). False positives.
118
$excludes = '/(function |^\s*\*|^\s*\/\/|\$this-\>[a-zA-Z]*db-\>|^\s*\$CFG-\>(dbtype|dblibrary)\s*=|\$DB-\>(connect|execute)|\$this-\>execute|get_dbtype|protected \$[a-zA-Z]*db|Incorrect |check find_index_name|not available anymore|output|Replace it with the correct use of|where order of parameters is|_moodle_database|invaliddbtype)/';
119
 
120
// All rules.
121
$all_megarules = array(
122
    'DML'=>$dml_megarule,
123
    'HELPER'=>$helper_megarule,
124
    'DDL'=>$ddl_megarule,
125
    'COREONLY'=>$coreonly_megarule,
126
    'INTERNAL'=>$internal_megarule,
127
    'UNSUPPORTED'=>$unsupported_megarule,
128
    'OTHER'=>$other_megarule
129
);
130
 
131
// To store errors found.
132
$errors = array();
133
$counterrors = 0;
134
 
135
// Process starts here.
136
 
137
echo "Checking the $dir directory recursively" . LINEFEED;
138
 
139
$files = files_to_check($dir);
140
 
141
foreach ($files as $file) {
142
    echo "  - $file: ";
143
 
144
    // Read the file, line by line, applying all the megarules.
145
    $handle = @fopen($file, 'r');
146
    if ($handle) {
147
        $line = 0;
148
        while (!feof($handle)) {
149
            $buffer = fgets($handle, 65535); // Long lines supported on purpose.
150
            $line++;
151
            // Search for megarules.
152
            foreach ($all_megarules as $name => $megarule) {
153
                if (!empty($megarule) && preg_match($megarule, $buffer) && !preg_match($excludes, $buffer)) {
154
                    // Error found, add to errrors.
155
                    if (!isset($errors[$file])) {
156
                        $errors[$file] = array();
157
                        echo LINEFEED . "      * ERROR found!" . LINEFEED;
158
                    }
159
                    $errors[$file][] = "- ERROR ( $name ) - line $line : " . trim($buffer);
160
                    echo "          - ERROR ( $name ) - line $line : " . trim($buffer) . LINEFEED;
161
                    $counterrors++;
162
                    break;
163
                }
164
            }
165
        }
166
        if (!isset($errors[$file])) {
167
            echo "... OK" . LINEFEED;
168
        }
169
        fclose($handle);
170
    }
171
 
172
}
173
 
174
echo LINEFEED . LINEFEED;
175
echo "  SUMMARY: " . count($errors) . " files with errors ($counterrors ocurrences)" . LINEFEED;
176
foreach ($errors as $file => $errarr) {
177
    echo LINEFEED . "    * $file" . LINEFEED;
178
    foreach ($errarr as $err) {
179
        echo "        $err" . LINEFEED;
180
    }
181
}
182
 
183
// INTERNAL FUNCTIONS.
184
 
185
/**
186
 * Given an array of search patterns, create one "megarule", with the specified prefixes and suffixes
187
 */
188
function calculate_megarule($patterns, $prefixes=array(), $suffixes=array(), $modifiers='') {
189
 
190
    $megarule  = '';
191
    $totalrule = '';
192
 
193
    if (empty($patterns)) {
194
        return false;
195
    }
196
 
197
    foreach ($patterns as $pattern) {
198
        $megarule .= '|(' . $pattern . ')';
199
    }
200
    $megarule = trim($megarule, '|');
201
 
202
    // Add all the prefix/suffix combinations.
203
    foreach ($prefixes as $prefix) {
204
        foreach ($suffixes as $suffix) {
205
            $totalrule .= '|(' . $prefix . '(' . $megarule . ')' . $suffix . ')';
206
        }
207
    }
208
    $totalrule = trim($totalrule, '|');
209
 
210
    return '/' . (empty($totalrule) ? $megarule : $totalrule) . '/' . $modifiers;
211
}
212
 
213
/**
214
 * Given one full path, return one array with all the files to check
215
 */
216
function files_to_check($path) {
217
 
218
    $results = array();
219
    $pending = array();
220
 
221
    $dir = opendir($path);
222
    while (false !== ($file=readdir($dir))) {
223
 
224
        $fullpath = $path . '/' . $file;
225
 
226
        if (substr($file, 0, 1)=='.' || $file=='CVS') { // Exclude some dirs.
227
            continue;
228
        }
229
 
230
        if (is_dir($fullpath)) { // Process dirs later.
231
            $pending[] = $fullpath;
232
            continue;
233
        }
234
 
235
        if (is_file($fullpath) && strpos($file, basename(__FILE__))!==false) { // Exclude me.
236
            continue;
237
        }
238
 
239
        if (is_file($fullpath) && (strpos($fullpath, 'lib/adodb')!==false ||
240
                                   strpos($fullpath, 'lib/simpletest')!==false ||
241
                                   strpos($fullpath, 'lib/htmlpurifier')!==false ||
242
                                   strpos($fullpath, 'lib/memcached.class.php')!==false ||
243
                                   strpos($fullpath, 'lib/phpmailer')!==false ||
244
                                   strpos($fullpath, 'lib/soap')!==false ||
245
                                   strpos($fullpath, 'search/Zend/Search')!==false ||
246
                                   strpos($fullpath, 'lang/')!==false)) { // Exclude adodb, simpletest, htmlpurifier, memcached, phpmailer, soap and lucene libs and lang dir.
247
            continue;
248
        }
249
 
250
        if (is_file($fullpath) && strpos($file, '.php')===false && strpos($file, '.html')===false) { // Exclude some files.
251
            continue;
252
        }
253
 
254
        if (!in_array($fullpath, $results)) { // Add file if doesn't exists.
255
            $results[$fullpath] = $fullpath;
256
        }
257
    }
258
    closedir($dir);
259
 
260
    foreach ($pending as $pend) {
261
        $results = array_merge($results, files_to_check($pend));
262
    }
263
 
264
    return $results;
265
}