-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcheck-smf-license.php
More file actions
executable file
·128 lines (110 loc) · 3.98 KB
/
check-smf-license.php
File metadata and controls
executable file
·128 lines (110 loc) · 3.98 KB
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
<?php
/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines https://www.simplemachines.org
* @copyright 2025 Simple Machines and individual contributors
* @license https://www.simplemachines.org/about/smf/license.php BSD
*
* @version 3.0 Alpha 4
*/
// Stuff we will ignore.
$ignoreFiles = [
// Index files in subdirectories.
'\./(?:\w+/)+\bindex\.php',
// Language Files are ignored as they don't use the License format.
'./Themes/default/languages/',
'\./Languages/',
// Cache and miscellaneous.
'\./cache/',
'\./tests/',
'\./vendor/',
// Everything in other except install.php, upgrade.php, Settings.php and Settings_bak.php.
'\./other/(?!install|upgrade|Settings)\w+\.php',
// 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);
}