-
Notifications
You must be signed in to change notification settings - Fork 69
/
check_db_syntax.php
367 lines (314 loc) · 15.1 KB
/
check_db_syntax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php // $Id: check_db_syntax.php,v 1.12 2010/06/09 13:46:46 stronk7 Exp $
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://moodle.com //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
if (isset($_SERVER['REMOTE_ADDR'])) {
define('LINEFEED', "<br />");
} else {
define('LINEFEED', "\n");
}
/// List of patterns to search
$dml = array (
'(begin|commit|rollback)_sql',
'count_records(_select|_sql)?',
'delete_records(_select)?',
'get_field(set)?(_select|sql)?',
'get_record(s|set)?(_list|_menu|_select|_sql)?(_menu)?',
'insert_record',
'record_exists(_select|_sql)?',
'records_to_menu',
'recordset_to_(array|menu)',
'rs_(EOF|close|fetch_next_record|fetch_record|next_record)',
'set_field(_select)?',
'update_record',
);
$helper = array (
'db_(lowercase|uppercase)',
'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)'
);
$ddl = array (
'add_(field|index|key)',
'change_field_(default|enum|notnull|precision|type|unsigned)',
'create_(table|temp_table)',
'drop_(field|index|key|table)',
'find_(check_constraint_name|index_name|key_name|sequence_name)',
'rename_(field|index|key|table)',
'(check_constraint|field|index|table)_exists'
);
$coreonly = array (
'delete_tables_from_xmldb_file',
'drop_plugin_tables',
'get_db_directories',
'get_used_table_names',
'install_from_xmldb_file',
);
$enum = array (
'ENUM(VALUES)?=".*?" ',
'>getEnum\(',
'new xmldb_field\((((\'[^\']*?\')|[^\',]+?|array\(.*)[,\)]\s?){9,20}',
'>add_field\((((\'[^\']*?\')|[^\',]+?|array\(.*)[,\)]\s?){9,20}',
'>set_attributes\((((\'[^\']*?\')|[^\',]+?|array\(.*)[,\)]\s?){8,20}',
'change_field_enum'
);
$internal = array (
'change_db_encoding',
'configure_dbconnection',
'db_(detect_lobs|update_lobs)',
'execute_sql(_arr)?',
'onespace2empty',
'oracle_dirty_hack',
'rcache_(get|getforfill|releaseforfill|set|unset|unset_table)',
'where_clause'
);
$unsupported = array (
'column_type',
'table_column',
'modify_database',
'(Execute|Connect|PConnect|ErrorMsg)',
'(MetaTables|MetaColumns|MetaColumnNames|MetaPrimaryKeys|MetaIndexes)'
);
$other = array (
'\$db[,; -]',
"[^\$_'\"\.-]dbfamily",
"[^\$_'\"\.-]dblibrary",
"[^\$_'\"\.-]dbtype[^s]",
'sql_substr\(\)',
'\$CFG->prefix',
'NEWNAMEGOESHERE',
'new\s(XMLDBTable|XMLDBField|XMLDBIndex|XMLDBKey)',
'>(addFieldInfo|addIndexInfo|addKeyInfo|setAttributes)',
'>(begin|commit|rollback)_sql'
);
/// Define some known false positives to take them out from errors report (nested array of => file => regular expressions considered false positives)
$fp = array (
'install.php' => array(
'empty\(\$distro-\>dbtype\)', /// Allow $distro->dbtype stuff to work in install
'= trim\(\$_POST\[\'dbtype\'\]', /// Allow $config->dbtype stuff to work in install
'get_driver_instance\(\$config-|>dbtype' /// Allow $config->dbtype stuff to work in install
),
'admin/blocks.php' => array(
'drop_plugin_tables.*\/blocks' /// Tables can be dropped from blocks admin interface
),
'admin/health.php' => array(
'\. \$CFG-\>prefix \.' /// health script is allowed to use $CFG->prefix when building suggested SQLs to be shown
),
'admin/modules.php' => array(
'drop_plugin_tables.*\/mod' /// Tables can be dropped from modules admin interface
),
'admin/qtypes.php' => array(
'drop_plugin_tables.*\$QTYPES\[\$delete\]-\>' /// Tables can be dropped from qtype admin interface
),
'lib/form/recaptcha.php' => array( /// recaptcha form has own setAttributes method
'this->setAttributes'
)
);
/// List of exceptions that aren't errors (function declarations, comments, adodb usage from adodb drivers and harcoded strings). Non reportable false positives
$excludes = '/(function |^\s*\*|^\s*\/\/|\$this-\>adodb-\>(Execute|Connect|PConnect|ErrorMsg|MetaTables|MetaIndexes|MetaColumns|MetaColumnNames|MetaPrimaryKeys|)|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|has been deprecated in Moodle 2\.0\. Will be out in Moodle 2\.1|NEWNAMEGOESHERE.*XMLDB_LINEFEED)/';
/// Getting current dir
$dir = dirname(__FILE__);
/// Check if the dir seems to be moodle root (with some random shots)
$is_moodle_root = false;
if (file_exists($dir . '/lang/en') && file_exists($dir . '/lib/db') && file_exists($dir . '/install/lang/en')) {
$is_moodle_root = true;
}
/// Calculating megarules
$dml_megarule = calculate_megarule($dml,array('[ =@.]'), array('( )?\('), 'i');
$helper_megarule = calculate_megarule($helper,array('[ =@.]'), array('( )?\('), 'i');
$ddl_megarule = calculate_megarule($ddl,array('[ =@.]'), array('( )?\('), 'i');
$coreonly_megarule = calculate_megarule($coreonly,array('[ =@.]'), array('( )?\('), 'i');
$enum_megarule = calculate_megarule($enum);
$internal_megarule = calculate_megarule($internal,array('[ =@.]'), array('( )?\('), 'i');
$unsupported_megarule= calculate_megarule($unsupported,array('[ \>=@,.]'), array('( )?\('));
$other_megarule = calculate_megarule($other);
/// All rules
$all_megarules = array(
'DML'=>$dml_megarule,
'HELPER'=>$helper_megarule,
'DDL'=>$ddl_megarule,
'COREONLY'=>$coreonly_megarule,
'ENUM'=>$enum_megarule,
'INTERNAL'=>$internal_megarule,
'UNSUPPORTED'=>$unsupported_megarule,
'OTHER'=>$other_megarule
);
/// To store errors found
$errors = array();
$counterrors = 0;
/// To store known false positives
$falsepositives = array();
$countfalsepositives = 0;
/// Process starts here
echo "Checking the $dir directory recursively" . LINEFEED;
if ($is_moodle_root) {
echo "(detected Moodle root directory - false positive detection enabled)" . LINEFEED;
} else {
echo "(executed from custom directory - false positive detection DISABLED!)" . LINEFEED;
}
$files = files_to_check($dir);
foreach ($files as $file) {
echo " - $file: ";
/// Read the file, line by line, applying all the megarules
$handle = @fopen($file, 'r');
if ($handle) {
$line = 0;
while (!feof($handle)) {
$buffer = fgets($handle, 65535); /// Long lines supported on purpose
$line++;
/// Search for megarules
foreach ($all_megarules as $name=>$megarule) {
if (!empty($megarule) && preg_match($megarule, $buffer) && !preg_match($excludes, $buffer)) {
/// Let's see if that's a well known false positive (only if executed from Moodle root)
if ($is_moodle_root && is_known_false_positive($fp, $file, $buffer, $is_moodle_root)) {
/// Known false positive found, annotate it
if (!isset($falsepositives[$file])) {
$falsepositives[$file] = array();
}
$falsepositives[$file][] = "- NOTICE ( $name ) - line $line : " . trim($buffer);
$countfalsepositives++;
break;
} else {
/// Error found, add to errrors
if (!isset($errors[$file])) {
$errors[$file] = array();
echo LINEFEED . " * ERROR found!" . LINEFEED;
}
$errors[$file][] = "- ERROR ( $name ) - line $line : " . trim($buffer);
echo " - ERROR ( $name ) - line $line : " . trim($buffer) . LINEFEED;
$counterrors++;
break;
}
}
}
}
if (!isset($errors[$file])) {
echo "... OK" . LINEFEED;
}
fclose($handle);
}
}
echo LINEFEED . LINEFEED;
echo " SUMMARY: " . count($errors) . " files with errors ($counterrors ocurrences)" . LINEFEED;
foreach ($errors as $file=>$errarr) {
echo LINEFEED . " * $file" . LINEFEED;
foreach ($errarr as $err) {
echo " $err" . LINEFEED;
}
}
echo LINEFEED . LINEFEED;
echo " Known false positive: " . count($falsepositives) . " files with $countfalsepositives ocurrences" . LINEFEED;
echo " (you should ignore these, although reviewing them from time to time isn't a bad idea either)" . LINEFEED;
foreach ($falsepositives as $file=>$fparr) {
echo LINEFEED . " * $file" . LINEFEED;
foreach ($fparr as $fp) {
echo " $fp" . LINEFEED;
}
}
/// INTERNAL FUNCIONS
/**
* Given an array of search patterns, create one "megarule", with the specified prefixes and suffixes
*/
function calculate_megarule($patterns, $prefixes=array(), $suffixes=array(), $modifiers='') {
$megarule = '';
$totalrule = '';
if (empty($patterns)) {
return false;
}
foreach ($patterns as $pattern) {
$megarule .= '|(' . $pattern . ')';
}
$megarule = trim($megarule, '|');
/// Add all the prefix/suffix combinations
foreach ($prefixes as $prefix) {
foreach ($suffixes as $suffix) {
$totalrule .= '|(' . $prefix . '(' . $megarule . ')' . $suffix . ')';
}
}
$totalrule = trim($totalrule, '|');
return '/' . (empty($totalrule) ? $megarule : $totalrule) . '/' . $modifiers;
}
/**
* Given one full path, return one array with all the files to check
*/
function files_to_check($path) {
$results = array();
$pending = array();
$dir = opendir($path);
while (false !== ($file=readdir($dir))) {
$fullpath = $path . '/' . $file;
if (substr($file, 0, 1)=='.' || $file=='CVS') { /// Exclude some dirs
continue;
}
if (is_dir($fullpath)) { /// Process dirs later
$pending[] = $fullpath;
continue;
}
if (is_file($fullpath) && strpos($file, basename(__FILE__))!==false) { /// Exclude me
continue;
}
if (is_file($fullpath) && (strpos($fullpath, 'lib/adodb')!==false ||
strpos($fullpath, 'lib/pear')!==false ||
strpos($fullpath, 'lib/simpletest')!==false ||
strpos($fullpath, 'lib/htmlpurifier')!==false ||
strpos($fullpath, 'lib/memcached.class.php')!==false ||
strpos($fullpath, 'lib/phpmailer')!==false ||
strpos($fullpath, 'lib/soap')!==false ||
strpos($fullpath, 'search/Zend/Search')!==false ||
strpos($fullpath, 'lang/')!==false ||
strpos($fullpath, 'config.php')!==false ||
strpos($fullpath, 'config-dist.php')!=false)) { /// Exclude adodb, pear, simpletest, htmlpurifier, memcached, phpmailer, soap and lucene libs, lang and config files
continue;
}
if (is_file($fullpath) && strpos($file, '.php')===false && strpos($file, '.html')===false && strpos($file,'.xml')===false) { /// Exclude some files
continue;
}
if (!in_array($fullpath, $results)) { /// Add file if doesn't exists
$results[$fullpath] = $fullpath;
}
}
closedir($dir);
foreach ($pending as $pend) {
$results = array_merge($results, files_to_check($pend));
}
return $results;
}
/// Function used to discard some well known false positives ($fp) when
/// some $text in $file has been detected as error. Only processed if
/// we detect the script is being executed from moodle root directory.
/// Simply returns true/false
function is_known_false_positive($fp, $file, $text, $is_moodle_root = false) {
if (!$is_moodle_root) {
return false;
}
/// Take out dirroot from $file
$file = trim(str_replace(dirname(__FILE__), '', $file), '/');
/// Look for $file in array of known false positives
if (array_key_exists($file, $fp)) {
foreach ($fp[$file] as $fprule) {
if (preg_match('/' . $fprule . '/i', $text)) {
return true;
}
}
}
/// Arrived hed, no false positives found for that file/$text
return false;
}
?>