-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcheck-eof.php
executable file
·83 lines (70 loc) · 2.37 KB
/
check-eof.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
<?php
/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2022 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.1.0
*/
// Stuff we will ignore.
$ignoreFiles = [
'\./cache/',
'\./other/',
'\./tests/',
'\./vendor/',
// Minify Stuff.
'\./Sources/minify/',
// random_compat().
'\./Sources/random_compat/',
// ReCaptcha Stuff.
'\./Sources/ReCaptcha/',
// We will ignore Settings.php if this is a live dev site.
'\./Settings\.php',
'\./Settings_bak\.php',
'\./db_last_error\.php',
];
try {
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.', FilesystemIterator::UNIX_PATHS)) as $currentFile => $fileInfo) {
// Starts with a dot, skip. Also gets Mac OS X resource files.
if ($currentFile[0] == '.') {
continue;
}
if ($fileInfo->getExtension() == 'php') {
foreach ($ignoreFiles as $if) {
if (preg_match('~' . $if . '~i', $currentFile)) {
continue 2;
}
}
if (($file = fopen($currentFile, 'r')) !== false) {
// Seek the end minus some bytes.
fseek($file, -100, SEEK_END);
$contents = fread($file, 100);
// There is some white space here.
if (preg_match('~\?>\s+$~', $contents, $matches)) {
throw new Exception('End of File contains extra spaces in ' . $currentFile);
}
// Test to see if its there even, SMF 2.1 base package needs it there in our main files to allow package manager to properly handle end operations. Customizations do not need it.
if (!preg_match('~\?>$~', $contents, $matches)) {
throw new Exception('End of File missing in ' . $currentFile);
}
// Test to see if a function/class ending is here but with no return (because we are OCD).
if (preg_match('~}([\r]?\n)?\?>~', $contents, $matches)) {
throw new Exception('Incorrect return(s) after last function/class but before EOF in ' . $currentFile);
}
// Test to see if a string ending is here but with no return (because we are OCD).
if (preg_match('~;([\r]?\n)?\?>~', $contents, $matches)) {
throw new Exception('Incorrect return(s) after last string but before EOF in ' . $currentFile);
}
} else {
throw new Exception('Unable to open file ' . $currentFile);
}
}
}
}
catch (Exception $e) {
fwrite(STDERR, $e->getMessage());
exit(1);
}