-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcheck-smf-license.php
executable file
·153 lines (133 loc) · 4.63 KB
/
check-smf-license.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
<?php
/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2024 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 3.0 Alpha 1
*/
// Stuff we will ignore.
$ignoreFiles = [
// Index files.
'\./attachments/index\.php',
'\./avatars/index\.php',
'\./avatars/[A-Za-z0-9]+/index\.php',
'\./cache/index\.php',
'\./custom_avatar/index\.php',
'\./Packages/index\.php',
'\./Packages/backups/index\.php',
'\./Smileys/[A-Za-z0-9]+/index\.php',
'\./Smileys/index\.php',
'\./Sources/index\.php',
'\./Sources/[\w/]+/index\.php',
'\./other/Schema/[\w/]+/*\.php',
'\./Themes/default/index\.php',
'\./Themes/default/[\w/]+/index\.php',
'\./Themes/index\.php',
// Language Files are ignored as they don't use the License format.
'./Themes/default/languages/[A-Za-z0-9]+\.english\.php',
'./Languages/en_US/[A-Za-z0-9]+\.php',
'./Themes/default/languages/en_US/[A-Za-z0-9]+\.php',
'\./Languages/index\.php',
// Cache and miscellaneous.
'\./cache/',
'\./other/db_last_error\.php',
'\./other/update_version_numbers.php',
'\./other/update_unicode_data.php',
'\./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',
];
$checkVersionAndYearFiles = [
'\./index\.php',
'\./SSI\.php',
'\./cron\.php',
'\./proxy\.php',
'\./other/.*\.php',
];
try {
if (($indexFile = fopen('./index.php', 'r')) !== false) {
$indexContents = fread($indexFile, 1500);
if (!preg_match('~define\(\'SMF_VERSION\', \'([^\']+)\'\);~i', $indexContents, $versionResults)) {
throw new Exception('Could not locate SMF_VERSION');
}
$currentVersion = $versionResults[1];
if (!preg_match('~define\(\'SMF_SOFTWARE_YEAR\', \'(\d{4})\'\);~i', $indexContents, $yearResults)) {
throw new Exception('Could not locate SMF_SOFTWARE_YEAR');
}
$currentSoftwareYear = (int) $yearResults[1];
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) {
// Some files, *cough* ManageServer *cough* have lots of junk before the license, otherwise this could easily be 500.
$contents = fread($file, 4000);
// How the license file should look, in a regex type format.
$match = array(
0 => '\* Simple Machines Forum \(SMF\)' . '[\r]?\n',
1 => ' \*' . '[\r]?\n',
2 => ' \* @package SMF' . '[\r]?\n',
3 => ' \* @author Simple Machines https?://www.simplemachines.org' . '[\r]?\n',
4 => ' \* @copyright \d{4} Simple Machines and individual contributors' . '[\r]?\n',
5 => ' \* @license https?://www.simplemachines.org/about/smf/license.php BSD' . '[\r]?\n',
6 => ' \*' . '[\r]?\n',
7 => ' \* @version',
);
// Just see if the license is there.
if (!preg_match('~' . implode('', $match) . '~i', $contents)) {
throw new Exception('License File is invalid or not found in ' . $currentFile);
}
$shouldCheckVersionAndYear = false;
foreach ($checkVersionAndYearFiles as $f) {
if (preg_match('~' . $f . '~i', $currentFile)) {
$shouldCheckVersionAndYear = true;
break;
}
}
if ($shouldCheckVersionAndYear) {
// Check the year is correct.
$yearMatch = $match;
$yearMatch[4] = ' \* @copyright ' . $currentSoftwareYear . ' Simple Machines and individual contributors' . '[\r]?\n';
if (!preg_match('~' . implode('', $yearMatch) . '~i', $contents)) {
throw new Exception('The software year is incorrect in ' . $currentFile);
}
// Check the version is correct.
$versionMatch = $match;
$versionMatch[7] = ' \* @version ' . $currentVersion . '[\r]?\n';
if (!preg_match('~' . implode('', $versionMatch) . '~i', $contents)) {
throw new Exception('The version is incorrect in ' . $currentFile);
}
}
} else {
throw new Exception('Unable to open file ' . $currentFile);
}
}
}
} else {
throw new Exception('Unable to open file ./index.php');
}
}
catch (Exception $e) {
fwrite(STDERR, $e->getMessage());
exit(1);
}