-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmultilan.php
232 lines (197 loc) · 6.63 KB
/
multilan.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
<?php
class multilan
{
/**
* Convert a single language file from define() to array format
* @param string $path Full path to the language file
* @return bool Success status
*/
public static function convertFile($path)
{
self::log("Processing file: $path");
if (!is_readable($path))
{
self::log("Cannot read file: $path");
return false;
}
// Check if the file already returns an array
$content = @include($path);
if (is_array($content))
{
self::log("Skipped: $path (already returns an array)");
return false;
}
// Parse the file and build the output
$outputLines = self::parseLanFile($path);
if (empty($outputLines['body']))
{
self::log("$path: No define() calls found.");
return false;
}
// Build the final content with header and body
$newContent = $outputLines['header'] . "\nreturn [\n" . implode("\n", $outputLines['body']) . "\n];\n";
// Save the updated content
if (file_put_contents($path, $newContent) !== false)
{
self::log("Converted: $path");
return true;
}
self::log("Failed to convert: $path");
return false;
}
/**
* Convert all language files in a specific plugin's language folder from define() to array format
* @param string $pluginFolder Plugin folder name (e.g., 'forum', 'news')
* @return void
*/
public static function convertPlugin($pluginFolder)
{
$path = e_PLUGIN . $pluginFolder . '/languages/';
self::convertFilesInPath($path, $pluginFolder);
}
/**
* Convert all language files in a specific theme's language folder from define() to array format
* @param string $themeFolder Theme folder name (e.g., 'bootstrap3', 'voux')
* @return void
*/
public static function convertTheme($themeFolder)
{
$path = e_THEME . $themeFolder . '/languages/';
self::convertFilesInPath($path, $themeFolder);
}
/**
* Convert all language files in a given path from define() to array format
* @param string $path Full path to the language directory
* @param string $folderName Name of the plugin or theme folder for logging
* @return void
*/
private static function convertFilesInPath($path, $folderName)
{
$fl = e107::getFile();
$fl->setMode('full');
$files = $fl->get_files($path, '\.php$', 'standard', 2);
$converted = 0;
$skipped = 0;
foreach ($files as $file)
{
if (self::convertFile($file))
{
$converted++;
}
else
{
$skipped++;
}
}
self::log("\nSummary for $folderName:\nConverted: $converted files\nSkipped: $skipped files");
}
/**
* Parse a language file and build output lines, preserving commented define() positions, header comments, and inline comments
* @param string $file Full path to the language file
* @return array Array with 'header' (top comments) and 'body' (array lines)
*/
private static function parseLanFile($file)
{
$lines = file($file);
$headerLines = [];
$active = [];
$commented = [];
$inMultiLineComment = false;
$headerDone = false;
$outputLines = ['body' => []];
$lineCount = count($lines);
$i = 0;
while ($i < $lineCount) {
$currentLine = $lines[$i];
$trimmedLine = trim($currentLine);
// Handle header section
if (!$headerDone) {
if ($i === 0 && strpos($trimmedLine, '<?php') === 0) {
$headerLines[] = $currentLine;
$i++;
continue;
}
if (!$inMultiLineComment && preg_match('/^\s*\/\*/', $trimmedLine)) {
$inMultiLineComment = true;
}
if ($inMultiLineComment || preg_match('/^\s*(\/\/|#)/', $trimmedLine) || empty($trimmedLine)) {
$headerLines[] = $currentLine;
if ($inMultiLineComment && preg_match('/\*\//', $trimmedLine)) {
$inMultiLineComment = false;
}
$i++;
continue;
} else {
$headerDone = true;
}
}
// Skip fully commented lines, including commented-out array entries
if (preg_match('/^\s*(\/\/|#)/', $trimmedLine) || empty($trimmedLine)) {
$i++;
continue;
}
// Detect define statement start
if (preg_match('/^define\s*\(\s*[\'"](.+?)[\'"]\s*,\s*([\'"])(.*)$/', $trimmedLine, $matches)) {
$constName = $matches[1];
$quoteType = $matches[2];
$defineValuePart = $matches[3];
$defineEndPattern = '/' . preg_quote($quoteType) . '\s*\)\s*;\s*(?:\/\/.*|#.*)?$/';
$defineValueLines = [];
if (preg_match($defineEndPattern, $defineValuePart)) {
// Single-line define
$defineValuePartCleaned = preg_replace($defineEndPattern, '', $defineValuePart);
$defineValueLines[] = trim($defineValuePartCleaned);
} else {
// Multi-line define
$defineValueLines[] = $defineValuePart;
$i++;
while ($i < $lineCount) {
$nextLine = $lines[$i];
$nextLineTrimmed = trim($nextLine);
// Completely ignore commented lines in between
if (preg_match('/^\s*(\/\/|#)/', $nextLineTrimmed) || empty($nextLineTrimmed)) {
$i++;
continue;
}
if (preg_match($defineEndPattern, $nextLine)) {
$lineTextWithoutEnd = preg_replace($defineEndPattern, '', $nextLine);
$defineValueLines[] = trim($lineTextWithoutEnd);
break;
} else {
$defineValueLines[] = $nextLine;
}
$i++;
}
}
$finalValue = implode("\n", $defineValueLines);
$finalValue = trim($finalValue);
$finalValue = stripslashes($finalValue);
$active[$constName] = $finalValue;
$outputLines['body'][] = " '{$constName}' => \"" . str_replace('"', '\"', $finalValue) . "\",";
}
$i++;
}
$headerContent = implode("", $headerLines);
if (strpos($headerContent, '2008-2013') !== false) {
$headerContent = str_replace('2008-2013', '2008-2025', $headerContent);
self::log("Updated header year in file: $file");
}
self::log("Active defines extracted:\n" . json_encode($active, JSON_PRETTY_PRINT));
return [
'header' => $headerContent,
'body' => $outputLines['body']
];
}
/**
* Log conversion results or issues to a file in the plugin directory
* @param string $message Message to log
* @return void
*/
public static function log($message)
{
$logFile = __DIR__ . '/multilan.log';
$timestamp = date('Y-m-d H:i:s');
$logEntry = "[$timestamp] $message\n";
file_put_contents($logFile, $logEntry, FILE_APPEND | LOCK_EX);
}
}