1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * This file handles the administration of languages tasks. |
||||
5 | * |
||||
6 | * Simple Machines Forum (SMF) |
||||
7 | * |
||||
8 | * @package SMF |
||||
9 | * @author Simple Machines https://www.simplemachines.org |
||||
10 | * @copyright 2020 Simple Machines and individual contributors |
||||
11 | * @license https://www.simplemachines.org/about/smf/license.php BSD |
||||
12 | * |
||||
13 | * @version 2.1 RC2 |
||||
14 | */ |
||||
15 | |||||
16 | if (!defined('SMF')) |
||||
17 | die('No direct access...'); |
||||
18 | |||||
19 | /** |
||||
20 | * This is the main function for the languages area. |
||||
21 | * It dispatches the requests. |
||||
22 | * Loads the ManageLanguages template. (sub-actions will use it) |
||||
23 | * |
||||
24 | * @todo lazy loading. |
||||
25 | * |
||||
26 | * Uses ManageSettings language file |
||||
27 | */ |
||||
28 | function ManageLanguages() |
||||
29 | { |
||||
30 | global $context, $txt; |
||||
31 | |||||
32 | loadTemplate('ManageLanguages'); |
||||
33 | loadLanguage('ManageSettings'); |
||||
34 | |||||
35 | $context['page_title'] = $txt['edit_languages']; |
||||
36 | $context['sub_template'] = 'show_settings'; |
||||
37 | |||||
38 | $subActions = array( |
||||
39 | 'edit' => 'ModifyLanguages', |
||||
40 | 'add' => 'AddLanguage', |
||||
41 | 'settings' => 'ModifyLanguageSettings', |
||||
42 | 'downloadlang' => 'DownloadLanguage', |
||||
43 | 'editlang' => 'ModifyLanguage', |
||||
44 | ); |
||||
45 | |||||
46 | // By default we're managing languages. |
||||
47 | $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'edit'; |
||||
48 | $context['sub_action'] = $_REQUEST['sa']; |
||||
49 | |||||
50 | // Load up all the tabs... |
||||
51 | $context[$context['admin_menu_name']]['tab_data'] = array( |
||||
52 | 'title' => $txt['language_configuration'], |
||||
53 | 'description' => $txt['language_description'], |
||||
54 | ); |
||||
55 | |||||
56 | call_integration_hook('integrate_manage_languages', array(&$subActions)); |
||||
57 | |||||
58 | // Call the right function for this sub-action. |
||||
59 | call_helper($subActions[$_REQUEST['sa']]); |
||||
60 | } |
||||
61 | |||||
62 | /** |
||||
63 | * Interface for adding a new language |
||||
64 | * |
||||
65 | * @uses template_add_language() |
||||
66 | */ |
||||
67 | function AddLanguage() |
||||
68 | { |
||||
69 | global $context, $sourcedir, $txt, $smcFunc; |
||||
70 | |||||
71 | // Are we searching for new languages courtesy of Simple Machines? |
||||
72 | if (!empty($_POST['smf_add_sub'])) |
||||
73 | { |
||||
74 | $context['smf_search_term'] = $smcFunc['htmlspecialchars'](trim($_POST['smf_add'])); |
||||
75 | |||||
76 | $listOptions = array( |
||||
77 | 'id' => 'smf_languages', |
||||
78 | 'get_items' => array( |
||||
79 | 'function' => 'list_getLanguagesList', |
||||
80 | ), |
||||
81 | 'columns' => array( |
||||
82 | 'name' => array( |
||||
83 | 'header' => array( |
||||
84 | 'value' => $txt['name'], |
||||
85 | ), |
||||
86 | 'data' => array( |
||||
87 | 'db' => 'name', |
||||
88 | ), |
||||
89 | ), |
||||
90 | 'description' => array( |
||||
91 | 'header' => array( |
||||
92 | 'value' => $txt['add_language_smf_desc'], |
||||
93 | ), |
||||
94 | 'data' => array( |
||||
95 | 'db' => 'description', |
||||
96 | ), |
||||
97 | ), |
||||
98 | 'version' => array( |
||||
99 | 'header' => array( |
||||
100 | 'value' => $txt['add_language_smf_version'], |
||||
101 | ), |
||||
102 | 'data' => array( |
||||
103 | 'db' => 'version', |
||||
104 | ), |
||||
105 | ), |
||||
106 | 'utf8' => array( |
||||
107 | 'header' => array( |
||||
108 | 'value' => $txt['add_language_smf_utf8'], |
||||
109 | ), |
||||
110 | 'data' => array( |
||||
111 | 'db' => 'utf8', |
||||
112 | ), |
||||
113 | ), |
||||
114 | 'install_link' => array( |
||||
115 | 'header' => array( |
||||
116 | 'value' => $txt['add_language_smf_install'], |
||||
117 | 'class' => 'centercol', |
||||
118 | ), |
||||
119 | 'data' => array( |
||||
120 | 'db' => 'install_link', |
||||
121 | 'class' => 'centercol', |
||||
122 | ), |
||||
123 | ), |
||||
124 | ), |
||||
125 | ); |
||||
126 | |||||
127 | require_once($sourcedir . '/Subs-List.php'); |
||||
128 | createList($listOptions); |
||||
129 | |||||
130 | $context['default_list'] = 'smf_languages'; |
||||
131 | } |
||||
132 | |||||
133 | $context['sub_template'] = 'add_language'; |
||||
134 | } |
||||
135 | |||||
136 | /** |
||||
137 | * Gets a list of available languages from the mother ship |
||||
138 | * Will return a subset if searching, otherwise all avaialble |
||||
139 | * |
||||
140 | * @return array An array containing information about each available language |
||||
141 | */ |
||||
142 | function list_getLanguagesList() |
||||
143 | { |
||||
144 | global $context, $sourcedir, $smcFunc, $txt, $scripturl; |
||||
145 | |||||
146 | // We're going to use this URL. |
||||
147 | $url = 'https://download.simplemachines.org/fetch_language.php?version=' . urlencode(SMF_VERSION); |
||||
148 | |||||
149 | // Load the class file and stick it into an array. |
||||
150 | require_once($sourcedir . '/Class-Package.php'); |
||||
151 | $language_list = new xmlArray(fetch_web_data($url), true); |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
152 | |||||
153 | // Check that the site responded and that the language exists. |
||||
154 | if (!$language_list->exists('languages')) |
||||
155 | $context['smf_error'] = 'no_response'; |
||||
156 | elseif (!$language_list->exists('languages/language')) |
||||
157 | $context['smf_error'] = 'no_files'; |
||||
158 | else |
||||
159 | { |
||||
160 | $language_list = $language_list->path('languages[0]'); |
||||
161 | $lang_files = $language_list->set('language'); |
||||
162 | $smf_languages = array(); |
||||
163 | foreach ($lang_files as $file) |
||||
164 | { |
||||
165 | // Were we searching? |
||||
166 | if (!empty($context['smf_search_term']) && strpos($file->fetch('name'), $smcFunc['strtolower']($context['smf_search_term'])) === false) |
||||
167 | continue; |
||||
168 | |||||
169 | $smf_languages[] = array( |
||||
170 | 'id' => $file->fetch('id'), |
||||
171 | 'name' => $smcFunc['ucwords']($file->fetch('name')), |
||||
172 | 'version' => $file->fetch('version'), |
||||
173 | 'utf8' => $txt['yes'], |
||||
174 | 'description' => $file->fetch('description'), |
||||
175 | 'install_link' => '<a href="' . $scripturl . '?action=admin;area=languages;sa=downloadlang;did=' . $file->fetch('id') . ';' . $context['session_var'] . '=' . $context['session_id'] . '">' . $txt['add_language_smf_install'] . '</a>', |
||||
176 | ); |
||||
177 | } |
||||
178 | if (empty($smf_languages)) |
||||
179 | $context['smf_error'] = 'no_files'; |
||||
180 | else |
||||
181 | return $smf_languages; |
||||
182 | } |
||||
183 | } |
||||
184 | |||||
185 | /** |
||||
186 | * Download a language file from the Simple Machines website. |
||||
187 | * Requires a valid download ID ("did") in the URL. |
||||
188 | * Also handles installing language files. |
||||
189 | * Attempts to chmod things as needed. |
||||
190 | * Uses a standard list to display information about all the files and where they'll be put. |
||||
191 | * |
||||
192 | * @uses template_download_language() |
||||
193 | * Uses a standard list for displaying languages (@see createList()) |
||||
194 | */ |
||||
195 | function DownloadLanguage() |
||||
196 | { |
||||
197 | global $context, $sourcedir, $boarddir, $txt, $scripturl, $modSettings, $cache_enable; |
||||
198 | |||||
199 | loadLanguage('ManageSettings'); |
||||
200 | require_once($sourcedir . '/Subs-Package.php'); |
||||
201 | |||||
202 | // Clearly we need to know what to request. |
||||
203 | if (!isset($_GET['did'])) |
||||
204 | fatal_lang_error('no_access', false); |
||||
205 | |||||
206 | // Some lovely context. |
||||
207 | $context['download_id'] = $_GET['did']; |
||||
208 | $context['sub_template'] = 'download_language'; |
||||
209 | $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'add'; |
||||
210 | |||||
211 | // Can we actually do the installation - and do they want to? |
||||
212 | if (!empty($_POST['do_install']) && !empty($_POST['copy_file'])) |
||||
213 | { |
||||
214 | checkSession('get'); |
||||
215 | validateToken('admin-dlang'); |
||||
216 | |||||
217 | $chmod_files = array(); |
||||
218 | $install_files = array(); |
||||
219 | |||||
220 | // Check writable status. |
||||
221 | foreach ($_POST['copy_file'] as $file) |
||||
222 | { |
||||
223 | // Check it's not very bad. |
||||
224 | if (strpos($file, '..') !== false || (strpos($file, 'Themes') !== 0 && !preg_match('~agreement\.[A-Za-z-_0-9]+\.txt$~', $file))) |
||||
225 | fatal_error($txt['languages_download_illegal_paths']); |
||||
226 | |||||
227 | $chmod_files[] = $boarddir . '/' . $file; |
||||
228 | $install_files[] = $file; |
||||
229 | } |
||||
230 | |||||
231 | // Call this in case we have work to do. |
||||
232 | $file_status = create_chmod_control($chmod_files); |
||||
233 | $files_left = $file_status['files']['notwritable']; |
||||
234 | |||||
235 | // Something not writable? |
||||
236 | if (!empty($files_left)) |
||||
237 | $context['error_message'] = $txt['languages_download_not_chmod']; |
||||
238 | // Otherwise, go go go! |
||||
239 | elseif (!empty($install_files)) |
||||
240 | { |
||||
241 | read_tgz_file('https://download.simplemachines.org/fetch_language.php?version=' . urlencode(SMF_VERSION) . ';fetch=' . urlencode($_GET['did']), $boarddir, false, true, $install_files); |
||||
242 | |||||
243 | // Make sure the files aren't stuck in the cache. |
||||
244 | package_flush_cache(); |
||||
245 | $context['install_complete'] = sprintf($txt['languages_download_complete_desc'], $scripturl . '?action=admin;area=languages'); |
||||
246 | |||||
247 | return; |
||||
248 | } |
||||
249 | } |
||||
250 | |||||
251 | // Open up the old china. |
||||
252 | if (!isset($archive_content)) |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
253 | $archive_content = read_tgz_file('https://download.simplemachines.org/fetch_language.php?version=' . urlencode(SMF_VERSION) . ';fetch=' . urlencode($_GET['did']), null); |
||||
254 | |||||
255 | if (empty($archive_content)) |
||||
256 | fatal_error($txt['add_language_error_no_response']); |
||||
257 | |||||
258 | // Now for each of the files, let's do some *stuff* |
||||
259 | $context['files'] = array( |
||||
260 | 'lang' => array(), |
||||
261 | 'other' => array(), |
||||
262 | ); |
||||
263 | $context['make_writable'] = array(); |
||||
264 | foreach ($archive_content as $file) |
||||
265 | { |
||||
266 | $pathinfo = pathinfo($file['filename']); |
||||
267 | $dirname = $pathinfo['dirname']; |
||||
268 | $basename = $pathinfo['basename']; |
||||
269 | $extension = $pathinfo['extension']; |
||||
270 | |||||
271 | // Don't do anything with files we don't understand. |
||||
272 | if (!in_array($extension, array('php', 'jpg', 'gif', 'jpeg', 'png', 'txt'))) |
||||
273 | continue; |
||||
274 | |||||
275 | // Basic data. |
||||
276 | $context_data = array( |
||||
277 | 'name' => $basename, |
||||
278 | 'destination' => $boarddir . '/' . $file['filename'], |
||||
279 | 'generaldest' => $file['filename'], |
||||
280 | 'size' => $file['size'], |
||||
281 | // Does chmod status allow the copy? |
||||
282 | 'writable' => false, |
||||
283 | // Should we suggest they copy this file? |
||||
284 | 'default_copy' => true, |
||||
285 | // Does the file already exist, if so is it same or different? |
||||
286 | 'exists' => false, |
||||
287 | ); |
||||
288 | |||||
289 | // Does the file exist, is it different and can we overwrite? |
||||
290 | if (file_exists($boarddir . '/' . $file['filename'])) |
||||
291 | { |
||||
292 | if (is_writable($boarddir . '/' . $file['filename'])) |
||||
293 | $context_data['writable'] = true; |
||||
294 | |||||
295 | // Finally, do we actually think the content has changed? |
||||
296 | if ($file['size'] == filesize($boarddir . '/' . $file['filename']) && $file['md5'] == md5_file($boarddir . '/' . $file['filename'])) |
||||
297 | { |
||||
298 | $context_data['exists'] = 'same'; |
||||
299 | $context_data['default_copy'] = false; |
||||
300 | } |
||||
301 | // Attempt to discover newline character differences. |
||||
302 | elseif ($file['md5'] == md5(preg_replace("~[\r]?\n~", "\r\n", file_get_contents($boarddir . '/' . $file['filename'])))) |
||||
303 | { |
||||
304 | $context_data['exists'] = 'same'; |
||||
305 | $context_data['default_copy'] = false; |
||||
306 | } |
||||
307 | else |
||||
308 | $context_data['exists'] = 'different'; |
||||
309 | } |
||||
310 | // No overwrite? |
||||
311 | else |
||||
312 | { |
||||
313 | // Can we at least stick it in the directory... |
||||
314 | if (is_writable($boarddir . '/' . $dirname)) |
||||
315 | $context_data['writable'] = true; |
||||
316 | } |
||||
317 | |||||
318 | // I love PHP files, that's why I'm a developer and not an artistic type spending my time drinking absinth and living a life of sin... |
||||
319 | if ($extension == 'php' && preg_match('~\w+\.\w+(?:-utf8)?\.php~', $basename)) |
||||
320 | { |
||||
321 | $context_data += array( |
||||
322 | 'version' => '??', |
||||
323 | 'cur_version' => false, |
||||
324 | 'version_compare' => 'newer', |
||||
325 | ); |
||||
326 | |||||
327 | list ($name, $language) = explode('.', $basename); |
||||
328 | |||||
329 | // Let's get the new version, I like versions, they tell me that I'm up to date. |
||||
330 | if (preg_match('~\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '~i', $file['preview'], $match) == 1) |
||||
331 | $context_data['version'] = $match[1]; |
||||
332 | |||||
333 | // Now does the old file exist - if so what is it's version? |
||||
334 | if (file_exists($boarddir . '/' . $file['filename'])) |
||||
335 | { |
||||
336 | // OK - what is the current version? |
||||
337 | $fp = fopen($boarddir . '/' . $file['filename'], 'rb'); |
||||
338 | $header = fread($fp, 768); |
||||
0 ignored issues
–
show
It seems like
$fp can also be of type false ; however, parameter $handle of fread() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
339 | fclose($fp); |
||||
0 ignored issues
–
show
It seems like
$fp can also be of type false ; however, parameter $handle of fclose() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
340 | |||||
341 | // Find the version. |
||||
342 | if (preg_match('~(?://|/\*)\s*Version:\s+(.+?);\s*' . preg_quote($name, '~') . '(?:[\s]{2}|\*/)~i', $header, $match) == 1) |
||||
343 | { |
||||
344 | $context_data['cur_version'] = $match[1]; |
||||
345 | |||||
346 | // How does this compare? |
||||
347 | if ($context_data['cur_version'] == $context_data['version']) |
||||
348 | $context_data['version_compare'] = 'same'; |
||||
349 | elseif ($context_data['cur_version'] > $context_data['version']) |
||||
350 | $context_data['version_compare'] = 'older'; |
||||
351 | |||||
352 | // Don't recommend copying if the version is the same. |
||||
353 | if ($context_data['version_compare'] != 'newer') |
||||
354 | $context_data['default_copy'] = false; |
||||
355 | } |
||||
356 | } |
||||
357 | |||||
358 | // Add the context data to the main set. |
||||
359 | $context['files']['lang'][] = $context_data; |
||||
360 | } |
||||
361 | elseif ($extension == 'txt' && stripos($basename, 'agreement') !== false) |
||||
362 | { |
||||
363 | $context_data += array( |
||||
364 | 'version' => '??', |
||||
365 | 'cur_version' => false, |
||||
366 | 'version_compare' => 'newer', |
||||
367 | ); |
||||
368 | |||||
369 | // Registration agreement is a primary file |
||||
370 | $context['files']['lang'][] = $context_data; |
||||
371 | } |
||||
372 | else |
||||
373 | { |
||||
374 | // There shouldn't be anything else, but load this into "other" in case we decide to handle it in the future |
||||
375 | $context['files']['other'][] = $context_data; |
||||
376 | } |
||||
377 | |||||
378 | // Collect together all non-writable areas. |
||||
379 | if (!$context_data['writable']) |
||||
380 | $context['make_writable'][] = $context_data['destination']; |
||||
381 | } |
||||
382 | |||||
383 | // Before we go to far can we make anything writable, eh, eh? |
||||
384 | if (!empty($context['make_writable'])) |
||||
385 | { |
||||
386 | // What is left to be made writable? |
||||
387 | $file_status = create_chmod_control($context['make_writable']); |
||||
388 | $context['still_not_writable'] = $file_status['files']['notwritable']; |
||||
389 | |||||
390 | // Mark those which are now writable as such. |
||||
391 | foreach ($context['files'] as $type => $data) |
||||
392 | { |
||||
393 | foreach ($data as $k => $file) |
||||
394 | { |
||||
395 | if (!$file['writable'] && !in_array($file['destination'], $context['still_not_writable'])) |
||||
396 | $context['files'][$type][$k]['writable'] = true; |
||||
397 | } |
||||
398 | } |
||||
399 | |||||
400 | // Are we going to need more language stuff? |
||||
401 | if (!empty($context['still_not_writable'])) |
||||
402 | loadLanguage('Packages'); |
||||
403 | } |
||||
404 | |||||
405 | // This is the list for the main files. |
||||
406 | $listOptions = array( |
||||
407 | 'id' => 'lang_main_files_list', |
||||
408 | 'title' => $txt['languages_download_main_files'], |
||||
409 | 'get_items' => array( |
||||
410 | 'function' => function() use ($context) |
||||
411 | { |
||||
412 | return $context['files']['lang']; |
||||
413 | }, |
||||
414 | ), |
||||
415 | 'columns' => array( |
||||
416 | 'name' => array( |
||||
417 | 'header' => array( |
||||
418 | 'value' => $txt['languages_download_filename'], |
||||
419 | ), |
||||
420 | 'data' => array( |
||||
421 | 'function' => function($rowData) use ($txt) |
||||
422 | { |
||||
423 | return '<strong>' . $rowData['name'] . '</strong><br><span class="smalltext">' . $txt['languages_download_dest'] . ': ' . $rowData['destination'] . '</span>' . ($rowData['version_compare'] == 'older' ? '<br>' . $txt['languages_download_older'] : ''); |
||||
424 | }, |
||||
425 | ), |
||||
426 | ), |
||||
427 | 'writable' => array( |
||||
428 | 'header' => array( |
||||
429 | 'value' => $txt['languages_download_writable'], |
||||
430 | ), |
||||
431 | 'data' => array( |
||||
432 | 'function' => function($rowData) use ($txt) |
||||
433 | { |
||||
434 | return '<span style="color: ' . ($rowData['writable'] ? 'green' : 'red') . ';">' . ($rowData['writable'] ? $txt['yes'] : $txt['no']) . '</span>'; |
||||
435 | }, |
||||
436 | ), |
||||
437 | ), |
||||
438 | 'version' => array( |
||||
439 | 'header' => array( |
||||
440 | 'value' => $txt['languages_download_version'], |
||||
441 | ), |
||||
442 | 'data' => array( |
||||
443 | 'function' => function($rowData) use ($txt) |
||||
0 ignored issues
–
show
|
|||||
444 | { |
||||
445 | return '<span style="color: ' . ($rowData['version_compare'] == 'older' ? 'red' : ($rowData['version_compare'] == 'same' ? 'orange' : 'green')) . ';">' . $rowData['version'] . '</span>'; |
||||
446 | }, |
||||
447 | ), |
||||
448 | ), |
||||
449 | 'exists' => array( |
||||
450 | 'header' => array( |
||||
451 | 'value' => $txt['languages_download_exists'], |
||||
452 | ), |
||||
453 | 'data' => array( |
||||
454 | 'function' => function($rowData) use ($txt) |
||||
455 | { |
||||
456 | return $rowData['exists'] ? ($rowData['exists'] == 'same' ? $txt['languages_download_exists_same'] : $txt['languages_download_exists_different']) : $txt['no']; |
||||
457 | }, |
||||
458 | ), |
||||
459 | ), |
||||
460 | 'copy' => array( |
||||
461 | 'header' => array( |
||||
462 | 'value' => $txt['languages_download_overwrite'], |
||||
463 | 'class' => 'centercol', |
||||
464 | ), |
||||
465 | 'data' => array( |
||||
466 | 'function' => function($rowData) |
||||
467 | { |
||||
468 | return '<input type="checkbox" name="copy_file[]" value="' . $rowData['generaldest'] . '"' . ($rowData['default_copy'] ? ' checked' : '') . '>'; |
||||
469 | }, |
||||
470 | 'style' => 'width: 4%;', |
||||
471 | 'class' => 'centercol', |
||||
472 | ), |
||||
473 | ), |
||||
474 | ), |
||||
475 | ); |
||||
476 | |||||
477 | // Kill the cache, as it is now invalid.. |
||||
478 | if (!empty($cache_enable)) |
||||
479 | { |
||||
480 | cache_put_data('known_languages', null, !empty($cache_enable) && $cache_enable < 1 ? 86400 : 3600); |
||||
481 | cache_put_data('known_languages_all', null, !empty($cache_enable) && $cache_enable < 1 ? 86400 : 3600); |
||||
482 | } |
||||
483 | |||||
484 | require_once($sourcedir . '/Subs-List.php'); |
||||
485 | createList($listOptions); |
||||
486 | |||||
487 | $context['default_list'] = 'lang_main_files_list'; |
||||
488 | createToken('admin-dlang'); |
||||
489 | } |
||||
490 | |||||
491 | /** |
||||
492 | * This lists all the current languages and allows editing of them. |
||||
493 | */ |
||||
494 | function ModifyLanguages() |
||||
495 | { |
||||
496 | global $txt, $context, $scripturl, $modSettings; |
||||
497 | global $sourcedir, $language, $boarddir; |
||||
498 | |||||
499 | // Setting a new default? |
||||
500 | if (!empty($_POST['set_default']) && !empty($_POST['def_language'])) |
||||
501 | { |
||||
502 | checkSession(); |
||||
503 | validateToken('admin-lang'); |
||||
504 | |||||
505 | getLanguages(); |
||||
506 | $lang_exists = false; |
||||
507 | foreach ($context['languages'] as $lang) |
||||
508 | { |
||||
509 | if ($_POST['def_language'] == $lang['filename']) |
||||
510 | { |
||||
511 | $lang_exists = true; |
||||
512 | break; |
||||
513 | } |
||||
514 | } |
||||
515 | |||||
516 | if ($_POST['def_language'] != $language && $lang_exists) |
||||
517 | { |
||||
518 | require_once($sourcedir . '/Subs-Admin.php'); |
||||
519 | updateSettingsFile(array('language' => $_POST['def_language'])); |
||||
520 | $language = $_POST['def_language']; |
||||
521 | } |
||||
522 | } |
||||
523 | |||||
524 | // Create another one time token here. |
||||
525 | createToken('admin-lang'); |
||||
526 | |||||
527 | $listOptions = array( |
||||
528 | 'id' => 'language_list', |
||||
529 | 'items_per_page' => $modSettings['defaultMaxListItems'], |
||||
530 | 'base_href' => $scripturl . '?action=admin;area=languages', |
||||
531 | 'title' => $txt['edit_languages'], |
||||
532 | 'get_items' => array( |
||||
533 | 'function' => 'list_getLanguages', |
||||
534 | ), |
||||
535 | 'get_count' => array( |
||||
536 | 'function' => 'list_getNumLanguages', |
||||
537 | ), |
||||
538 | 'columns' => array( |
||||
539 | 'default' => array( |
||||
540 | 'header' => array( |
||||
541 | 'value' => $txt['languages_default'], |
||||
542 | 'class' => 'centercol', |
||||
543 | ), |
||||
544 | 'data' => array( |
||||
545 | 'function' => function($rowData) |
||||
546 | { |
||||
547 | return '<input type="radio" name="def_language" value="' . $rowData['id'] . '"' . ($rowData['default'] ? ' checked' : '') . ' onclick="highlightSelected(\'list_language_list_' . $rowData['id'] . '\');">'; |
||||
548 | }, |
||||
549 | 'style' => 'width: 8%;', |
||||
550 | 'class' => 'centercol', |
||||
551 | ), |
||||
552 | ), |
||||
553 | 'name' => array( |
||||
554 | 'header' => array( |
||||
555 | 'value' => $txt['languages_lang_name'], |
||||
556 | ), |
||||
557 | 'data' => array( |
||||
558 | 'function' => function($rowData) use ($scripturl) |
||||
559 | { |
||||
560 | return sprintf('<a href="%1$s?action=admin;area=languages;sa=editlang;lid=%2$s">%3$s</a>', $scripturl, $rowData['id'], $rowData['name']); |
||||
561 | }, |
||||
562 | 'class' => 'centercol', |
||||
563 | ), |
||||
564 | ), |
||||
565 | 'character_set' => array( |
||||
566 | 'header' => array( |
||||
567 | 'value' => $txt['languages_character_set'], |
||||
568 | ), |
||||
569 | 'data' => array( |
||||
570 | 'db_htmlsafe' => 'char_set', |
||||
571 | 'class' => 'centercol', |
||||
572 | ), |
||||
573 | ), |
||||
574 | 'count' => array( |
||||
575 | 'header' => array( |
||||
576 | 'value' => $txt['languages_users'], |
||||
577 | ), |
||||
578 | 'data' => array( |
||||
579 | 'db_htmlsafe' => 'count', |
||||
580 | 'class' => 'centercol', |
||||
581 | ), |
||||
582 | ), |
||||
583 | 'locale' => array( |
||||
584 | 'header' => array( |
||||
585 | 'value' => $txt['languages_locale'], |
||||
586 | ), |
||||
587 | 'data' => array( |
||||
588 | 'db_htmlsafe' => 'locale', |
||||
589 | 'class' => 'centercol', |
||||
590 | ), |
||||
591 | ), |
||||
592 | 'editlang' => array( |
||||
593 | 'header' => array( |
||||
594 | 'value' => '', |
||||
595 | ), |
||||
596 | 'data' => array( |
||||
597 | 'function' => function($rowData) use ($scripturl, $txt) |
||||
598 | { |
||||
599 | return sprintf('<a href="%1$s?action=admin;area=languages;sa=editlang;lid=%2$s" class="button">%3$s</a>', $scripturl, $rowData['id'], $txt['edit']); |
||||
600 | }, |
||||
601 | 'style' => 'width: 8%;', |
||||
602 | 'class' => 'centercol', |
||||
603 | ), |
||||
604 | ), |
||||
605 | ), |
||||
606 | 'form' => array( |
||||
607 | 'href' => $scripturl . '?action=admin;area=languages', |
||||
608 | 'token' => 'admin-lang', |
||||
609 | ), |
||||
610 | 'additional_rows' => array( |
||||
611 | array( |
||||
612 | 'position' => 'bottom_of_list', |
||||
613 | 'value' => '<input type="hidden" name="' . $context['session_var'] . '" value="' . $context['session_id'] . '"><input type="submit" name="set_default" value="' . $txt['save'] . '"' . (is_writable($boarddir . '/Settings.php') ? '' : ' disabled') . ' class="button">', |
||||
614 | ), |
||||
615 | ), |
||||
616 | ); |
||||
617 | |||||
618 | // We want to highlight the selected language. Need some Javascript for this. |
||||
619 | addInlineJavaScript(' |
||||
620 | function highlightSelected(box) |
||||
621 | { |
||||
622 | $("tr.highlight2").removeClass("highlight2"); |
||||
623 | $("#" + box).addClass("highlight2"); |
||||
624 | } |
||||
625 | highlightSelected("list_language_list_' . ($language == '' ? 'english' : $language) . '");', true); |
||||
626 | |||||
627 | // Display a warning if we cannot edit the default setting. |
||||
628 | if (!is_writable($boarddir . '/Settings.php')) |
||||
629 | $listOptions['additional_rows'][] = array( |
||||
630 | 'position' => 'after_title', |
||||
631 | 'value' => $txt['language_settings_writable'], |
||||
632 | 'class' => 'smalltext alert', |
||||
633 | ); |
||||
634 | |||||
635 | require_once($sourcedir . '/Subs-List.php'); |
||||
636 | createList($listOptions); |
||||
637 | |||||
638 | $context['sub_template'] = 'show_list'; |
||||
639 | $context['default_list'] = 'language_list'; |
||||
640 | } |
||||
641 | |||||
642 | /** |
||||
643 | * How many languages? |
||||
644 | * Callback for the list in ManageLanguageSettings(). |
||||
645 | * |
||||
646 | * @return int The number of available languages |
||||
647 | */ |
||||
648 | function list_getNumLanguages() |
||||
649 | { |
||||
650 | return count(getLanguages()); |
||||
651 | } |
||||
652 | |||||
653 | /** |
||||
654 | * Fetch the actual language information. |
||||
655 | * Callback for $listOptions['get_items']['function'] in ManageLanguageSettings. |
||||
656 | * Determines which languages are available by looking for the "index.{language}.php" file. |
||||
657 | * Also figures out how many users are using a particular language. |
||||
658 | * |
||||
659 | * @return array An array of information about currenty installed languages |
||||
660 | */ |
||||
661 | function list_getLanguages() |
||||
662 | { |
||||
663 | global $settings, $smcFunc, $language, $context, $txt; |
||||
664 | |||||
665 | $languages = array(); |
||||
666 | // Keep our old entries. |
||||
667 | $old_txt = $txt; |
||||
668 | $backup_actual_theme_dir = $settings['actual_theme_dir']; |
||||
669 | $backup_base_theme_dir = !empty($settings['base_theme_dir']) ? $settings['base_theme_dir'] : ''; |
||||
670 | |||||
671 | // Override these for now. |
||||
672 | $settings['actual_theme_dir'] = $settings['base_theme_dir'] = $settings['default_theme_dir']; |
||||
673 | getLanguages(); |
||||
674 | |||||
675 | // Put them back. |
||||
676 | $settings['actual_theme_dir'] = $backup_actual_theme_dir; |
||||
677 | if (!empty($backup_base_theme_dir)) |
||||
678 | $settings['base_theme_dir'] = $backup_base_theme_dir; |
||||
679 | else |
||||
680 | unset($settings['base_theme_dir']); |
||||
681 | |||||
682 | // Get the language files and data... |
||||
683 | foreach ($context['languages'] as $lang) |
||||
684 | { |
||||
685 | // Load the file to get the character set. |
||||
686 | require($settings['default_theme_dir'] . '/languages/index.' . $lang['filename'] . '.php'); |
||||
687 | |||||
688 | $languages[$lang['filename']] = array( |
||||
689 | 'id' => $lang['filename'], |
||||
690 | 'count' => 0, |
||||
691 | 'char_set' => $txt['lang_character_set'], |
||||
692 | 'default' => $language == $lang['filename'] || ($language == '' && $lang['filename'] == 'english'), |
||||
693 | 'locale' => $txt['lang_locale'], |
||||
694 | 'name' => $smcFunc['ucwords'](strtr($lang['filename'], array('_' => ' ', '-utf8' => ''))), |
||||
695 | ); |
||||
696 | } |
||||
697 | |||||
698 | // Work out how many people are using each language. |
||||
699 | $request = $smcFunc['db_query']('', ' |
||||
700 | SELECT lngfile, COUNT(*) AS num_users |
||||
701 | FROM {db_prefix}members |
||||
702 | GROUP BY lngfile', |
||||
703 | array( |
||||
704 | ) |
||||
705 | ); |
||||
706 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||||
707 | { |
||||
708 | // Default? |
||||
709 | if (empty($row['lngfile']) || !isset($languages[$row['lngfile']])) |
||||
710 | $row['lngfile'] = $language; |
||||
711 | |||||
712 | if (!isset($languages[$row['lngfile']]) && isset($languages['english'])) |
||||
713 | $languages['english']['count'] += $row['num_users']; |
||||
714 | elseif (isset($languages[$row['lngfile']])) |
||||
715 | $languages[$row['lngfile']]['count'] += $row['num_users']; |
||||
716 | } |
||||
717 | $smcFunc['db_free_result']($request); |
||||
718 | |||||
719 | // Restore the current users language. |
||||
720 | $txt = $old_txt; |
||||
721 | |||||
722 | // Return how many we have. |
||||
723 | return $languages; |
||||
724 | } |
||||
725 | |||||
726 | /** |
||||
727 | * Edit language related settings. |
||||
728 | * |
||||
729 | * @param bool $return_config Whether to return the $config_vars array (used in admin search) |
||||
730 | * @return void|array Returns nothing or the $config_vars array if $return_config is true |
||||
731 | */ |
||||
732 | function ModifyLanguageSettings($return_config = false) |
||||
733 | { |
||||
734 | global $scripturl, $context, $txt, $boarddir, $sourcedir; |
||||
735 | |||||
736 | // We'll want to save them someday. |
||||
737 | require_once $sourcedir . '/ManageServer.php'; |
||||
738 | |||||
739 | // Warn the user if the backup of Settings.php failed. |
||||
740 | $settings_not_writable = !is_writable($boarddir . '/Settings.php'); |
||||
741 | $settings_backup_fail = !@is_writable($boarddir . '/Settings_bak.php') || !@copy($boarddir . '/Settings.php', $boarddir . '/Settings_bak.php'); |
||||
742 | |||||
743 | /* If you're writing a mod, it's a bad idea to add things here.... |
||||
744 | For each option: |
||||
745 | variable name, description, type (constant), size/possible values, helptext. |
||||
746 | OR an empty string for a horizontal rule. |
||||
747 | OR a string for a titled section. */ |
||||
748 | $config_vars = array( |
||||
749 | 'language' => array('language', $txt['default_language'], 'file', 'select', array(), null, 'disabled' => $settings_not_writable), |
||||
750 | array('userLanguage', $txt['userLanguage'], 'db', 'check', null, 'userLanguage'), |
||||
751 | ); |
||||
752 | |||||
753 | call_integration_hook('integrate_language_settings', array(&$config_vars)); |
||||
754 | |||||
755 | if ($return_config) |
||||
756 | return $config_vars; |
||||
757 | |||||
758 | // Get our languages. No cache |
||||
759 | getLanguages(false); |
||||
760 | foreach ($context['languages'] as $lang) |
||||
761 | $config_vars['language'][4][$lang['filename']] = array($lang['filename'], $lang['name']); |
||||
762 | |||||
763 | // Saving settings? |
||||
764 | if (isset($_REQUEST['save'])) |
||||
765 | { |
||||
766 | checkSession(); |
||||
767 | |||||
768 | call_integration_hook('integrate_save_language_settings', array(&$config_vars)); |
||||
769 | |||||
770 | saveSettings($config_vars); |
||||
771 | if (!$settings_not_writable && !$settings_backup_fail) |
||||
772 | $_SESSION['adm-save'] = true; |
||||
773 | redirectexit('action=admin;area=languages;sa=settings'); |
||||
774 | } |
||||
775 | |||||
776 | // Setup the template stuff. |
||||
777 | $context['post_url'] = $scripturl . '?action=admin;area=languages;sa=settings;save'; |
||||
778 | $context['settings_title'] = $txt['language_settings']; |
||||
779 | $context['save_disabled'] = $settings_not_writable; |
||||
780 | |||||
781 | if ($settings_not_writable) |
||||
782 | $context['settings_message'] = array( |
||||
783 | 'label' => $txt['settings_not_writable'], |
||||
784 | 'tag' => 'div', |
||||
785 | 'class' => 'centertext strong' |
||||
786 | ); |
||||
787 | elseif ($settings_backup_fail) |
||||
788 | $context['settings_message'] = array( |
||||
789 | 'label' => $txt['admin_backup_fail'], |
||||
790 | 'tag' => 'div', |
||||
791 | 'class' => 'centertext strong' |
||||
792 | ); |
||||
793 | |||||
794 | // Fill the config array. |
||||
795 | prepareServerSettingsContext($config_vars); |
||||
796 | } |
||||
797 | |||||
798 | /** |
||||
799 | * Edit a particular set of language entries. |
||||
800 | */ |
||||
801 | function ModifyLanguage() |
||||
802 | { |
||||
803 | global $settings, $context, $smcFunc, $txt, $modSettings, $boarddir, $sourcedir, $language, $cache_enable; |
||||
804 | |||||
805 | loadLanguage('ManageSettings'); |
||||
806 | |||||
807 | // Select the languages tab. |
||||
808 | $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'edit'; |
||||
809 | $context['page_title'] = $txt['edit_languages']; |
||||
810 | $context['sub_template'] = 'modify_language_entries'; |
||||
811 | |||||
812 | $context['lang_id'] = $_GET['lid']; |
||||
813 | list($theme_id, $file_id) = empty($_REQUEST['tfid']) || strpos($_REQUEST['tfid'], '+') === false ? array(1, '') : explode('+', $_REQUEST['tfid']); |
||||
814 | |||||
815 | // Clean the ID - just in case. |
||||
816 | preg_match('~([A-Za-z0-9_-]+)~', $context['lang_id'], $matches); |
||||
817 | $context['lang_id'] = $matches[1]; |
||||
818 | |||||
819 | // Get all the theme data. |
||||
820 | $request = $smcFunc['db_query']('', ' |
||||
821 | SELECT id_theme, variable, value |
||||
822 | FROM {db_prefix}themes |
||||
823 | WHERE id_theme != {int:default_theme} |
||||
824 | AND id_member = {int:no_member} |
||||
825 | AND variable IN ({string:name}, {string:theme_dir})', |
||||
826 | array( |
||||
827 | 'default_theme' => 1, |
||||
828 | 'no_member' => 0, |
||||
829 | 'name' => 'name', |
||||
830 | 'theme_dir' => 'theme_dir', |
||||
831 | ) |
||||
832 | ); |
||||
833 | $themes = array( |
||||
834 | 1 => array( |
||||
835 | 'name' => $txt['dvc_default'], |
||||
836 | 'theme_dir' => $settings['default_theme_dir'], |
||||
837 | ), |
||||
838 | ); |
||||
839 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||||
840 | $themes[$row['id_theme']][$row['variable']] = $row['value']; |
||||
841 | $smcFunc['db_free_result']($request); |
||||
842 | |||||
843 | // This will be where we look |
||||
844 | $lang_dirs = array(); |
||||
845 | |||||
846 | // There are different kinds of strings |
||||
847 | $string_types = array('txt', 'helptxt', 'editortxt', 'tztxt', 'txtBirthdayEmails'); |
||||
848 | $additional_string_types = array(); |
||||
849 | |||||
850 | // Some files allow the admin to add and/or remove certain types of strings |
||||
851 | $allows_add_remove = array( |
||||
852 | 'Timezones' => array( |
||||
853 | 'add' => array('tztxt', 'txt'), |
||||
854 | 'remove' => array('tztxt', 'txt'), |
||||
855 | ), |
||||
856 | 'Modifications' => array( |
||||
857 | 'add' => array('txt'), |
||||
858 | 'remove' => array('txt'), |
||||
859 | ), |
||||
860 | 'ThemeStrings' => array( |
||||
861 | 'add' => array('txt'), |
||||
862 | ), |
||||
863 | ); |
||||
864 | |||||
865 | // Does a hook need to add in some additional places to look for languages or info about how to handle them? |
||||
866 | call_integration_hook('integrate_modifylanguages', array(&$themes, &$lang_dirs, &$allows_add_remove, &$additional_string_types)); |
||||
867 | |||||
868 | $string_types = array_unique(array_merge($string_types, $additional_string_types)); |
||||
869 | |||||
870 | // Check we have themes with a path and a name - just in case - and add the path. |
||||
871 | foreach ($themes as $id => $data) |
||||
872 | { |
||||
873 | if (count($data) != 2) |
||||
874 | unset($themes[$id]); |
||||
875 | elseif (is_dir($data['theme_dir'] . '/languages')) |
||||
876 | $lang_dirs[$id] = $data['theme_dir'] . '/languages'; |
||||
877 | |||||
878 | // How about image directories? |
||||
879 | if (is_dir($data['theme_dir'] . '/images/' . $context['lang_id'])) |
||||
880 | $images_dirs[$id] = $data['theme_dir'] . '/images/' . $context['lang_id']; |
||||
881 | } |
||||
882 | |||||
883 | $current_file = $file_id ? $lang_dirs[$theme_id] . '/' . $file_id . '.' . $context['lang_id'] . '.php' : ''; |
||||
884 | |||||
885 | // Now for every theme get all the files and stick them in context! |
||||
886 | $context['possible_files'] = array(); |
||||
887 | foreach ($lang_dirs as $theme => $theme_dir) |
||||
888 | { |
||||
889 | // Open it up. |
||||
890 | $dir = dir($theme_dir); |
||||
891 | while ($entry = $dir->read()) |
||||
892 | { |
||||
893 | // We're only after the files for this language. |
||||
894 | if (preg_match('~^([A-Za-z]+)\.' . $context['lang_id'] . '\.php$~', $entry, $matches) == 0) |
||||
895 | continue; |
||||
896 | |||||
897 | if (!isset($context['possible_files'][$theme])) |
||||
898 | $context['possible_files'][$theme] = array( |
||||
899 | 'id' => $theme, |
||||
900 | 'name' => $themes[$theme]['name'], |
||||
901 | 'files' => array(), |
||||
902 | ); |
||||
903 | |||||
904 | $context['possible_files'][$theme]['files'][] = array( |
||||
905 | 'id' => $matches[1], |
||||
906 | 'name' => isset($txt['lang_file_desc_' . $matches[1]]) ? $txt['lang_file_desc_' . $matches[1]] : $matches[1], |
||||
907 | 'selected' => $theme_id == $theme && $file_id == $matches[1], |
||||
908 | ); |
||||
909 | } |
||||
910 | $dir->close(); |
||||
911 | usort($context['possible_files'][$theme]['files'], function($val1, $val2) |
||||
912 | { |
||||
913 | return strcmp($val1['name'], $val2['name']); |
||||
914 | }); |
||||
915 | } |
||||
916 | |||||
917 | // We no longer wish to speak this language. |
||||
918 | if (!empty($_POST['delete_main']) && $context['lang_id'] != 'english') |
||||
919 | { |
||||
920 | checkSession(); |
||||
921 | validateToken('admin-mlang'); |
||||
922 | |||||
923 | // @todo Todo: FTP Controls? |
||||
924 | require_once($sourcedir . '/Subs-Package.php'); |
||||
925 | |||||
926 | // First, Make a backup? |
||||
927 | if (!empty($modSettings['package_make_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['lang_id'] . '$$$')) |
||||
928 | { |
||||
929 | $_SESSION['last_backup_for'] = $context['lang_id'] . '$$$'; |
||||
930 | $result = package_create_backup('backup_lang_' . $context['lang_id']); |
||||
931 | if (!$result) |
||||
932 | fatal_lang_error('could_not_language_backup', false); |
||||
933 | } |
||||
934 | |||||
935 | // Second, loop through the array to remove the files. |
||||
936 | foreach ($lang_dirs as $curPath) |
||||
937 | { |
||||
938 | foreach ($context['possible_files'][1]['files'] as $lang) |
||||
939 | if (file_exists($curPath . '/' . $lang['id'] . '.' . $context['lang_id'] . '.php')) |
||||
940 | unlink($curPath . '/' . $lang['id'] . '.' . $context['lang_id'] . '.php'); |
||||
941 | |||||
942 | // Check for the email template. |
||||
943 | if (file_exists($curPath . '/EmailTemplates.' . $context['lang_id'] . '.php')) |
||||
944 | unlink($curPath . '/EmailTemplates.' . $context['lang_id'] . '.php'); |
||||
945 | } |
||||
946 | |||||
947 | // Third, the agreement file. |
||||
948 | if (file_exists($boarddir . '/agreement.' . $context['lang_id'] . '.txt')) |
||||
949 | unlink($boarddir . '/agreement.' . $context['lang_id'] . '.txt'); |
||||
950 | |||||
951 | // Fourth, a related images folder, if it exists... |
||||
952 | if (!empty($images_dirs)) |
||||
953 | foreach ($images_dirs as $curPath) |
||||
954 | if (is_dir($curPath)) |
||||
955 | deltree($curPath); |
||||
956 | |||||
957 | // Members can no longer use this language. |
||||
958 | $smcFunc['db_query']('', ' |
||||
959 | UPDATE {db_prefix}members |
||||
960 | SET lngfile = {empty} |
||||
961 | WHERE lngfile = {string:current_language}', |
||||
962 | array( |
||||
963 | 'empty_string' => '', |
||||
964 | 'current_language' => $context['lang_id'], |
||||
965 | ) |
||||
966 | ); |
||||
967 | |||||
968 | // Fifth, update getLanguages() cache. |
||||
969 | if (!empty($cache_enable)) |
||||
970 | { |
||||
971 | cache_put_data('known_languages', null, !empty($cache_enable) && $cache_enable < 1 ? 86400 : 3600); |
||||
972 | } |
||||
973 | |||||
974 | // Sixth, if we deleted the default language, set us back to english? |
||||
975 | if ($context['lang_id'] == $language) |
||||
976 | { |
||||
977 | require_once($sourcedir . '/Subs-Admin.php'); |
||||
978 | $language = 'english'; |
||||
979 | updateSettingsFile(array('language' => $language)); |
||||
980 | } |
||||
981 | |||||
982 | // Seventh, get out of here. |
||||
983 | redirectexit('action=admin;area=languages;sa=edit;' . $context['session_var'] . '=' . $context['session_id']); |
||||
984 | } |
||||
985 | |||||
986 | // Saving primary settings? |
||||
987 | $primary_settings = array('native_name' => 'string', 'lang_character_set' => 'string', 'lang_locale' => 'string', 'lang_rtl' => 'bool', 'lang_dictionary' => 'string', 'lang_spelling' => 'string', 'lang_recaptcha' => 'string'); |
||||
988 | $madeSave = false; |
||||
989 | if (!empty($_POST['save_main']) && !$current_file) |
||||
990 | { |
||||
991 | checkSession(); |
||||
992 | validateToken('admin-mlang'); |
||||
993 | |||||
994 | // Read in the current file. |
||||
995 | $current_data = implode('', file($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php')); |
||||
996 | |||||
997 | // Build the replacements. old => new |
||||
998 | $replace_array = array(); |
||||
999 | foreach ($primary_settings as $setting => $type) |
||||
1000 | $replace_array['~\$txt\[\'' . $setting . '\'\]\s*=\s*[^\r\n]+~'] = '$txt[\'' . $setting . '\'] = ' . ($type === 'bool' ? (!empty($_POST[$setting]) ? 'true' : 'false') : '\'' . ($setting = 'native_name' ? htmlentities(un_htmlspecialchars($_POST[$setting]), ENT_QUOTES, $context['character_set']) : preg_replace('~[^\w-]~i', '', $_POST[$setting])) . '\'') . ';'; |
||||
1001 | |||||
1002 | $current_data = preg_replace(array_keys($replace_array), array_values($replace_array), $current_data); |
||||
1003 | $fp = fopen($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php', 'w+'); |
||||
1004 | fwrite($fp, $current_data); |
||||
1005 | fclose($fp); |
||||
1006 | |||||
1007 | $madeSave = true; |
||||
1008 | } |
||||
1009 | |||||
1010 | // Quickly load index language entries. |
||||
1011 | $old_txt = $txt; |
||||
1012 | require($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php'); |
||||
1013 | $context['lang_file_not_writable_message'] = is_writable($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php') ? '' : sprintf($txt['lang_file_not_writable'], $settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php'); |
||||
1014 | // Setup the primary settings context. |
||||
1015 | $context['primary_settings']['name'] = $smcFunc['ucwords'](strtr($context['lang_id'], array('_' => ' ', '-utf8' => ''))); |
||||
1016 | foreach ($primary_settings as $setting => $type) |
||||
1017 | { |
||||
1018 | $context['primary_settings'][$setting] = array( |
||||
1019 | 'label' => str_replace('lang_', '', $setting), |
||||
1020 | 'value' => $txt[$setting], |
||||
1021 | ); |
||||
1022 | } |
||||
1023 | |||||
1024 | // Restore normal service. |
||||
1025 | $txt = $old_txt; |
||||
1026 | |||||
1027 | // Are we saving? |
||||
1028 | $save_strings = array(); |
||||
1029 | $remove_strings = array(); |
||||
1030 | $add_strings = array(); |
||||
1031 | if (isset($_POST['save_entries'])) |
||||
1032 | { |
||||
1033 | checkSession(); |
||||
1034 | validateToken('admin-mlang'); |
||||
1035 | |||||
1036 | if (!empty($_POST['edit'])) |
||||
1037 | { |
||||
1038 | foreach ($_POST['edit'] as $k => $v) |
||||
1039 | { |
||||
1040 | if (is_string($v)) |
||||
1041 | { |
||||
1042 | // Only try to save if 'edit' was specified and if the string has changed |
||||
1043 | if ($v == 'edit' && isset($_POST['entry'][$k]) && isset($_POST['comp'][$k]) && $_POST['entry'][$k] != $_POST['comp'][$k]) |
||||
1044 | $save_strings[$k] = cleanLangString($_POST['entry'][$k], false); |
||||
1045 | |||||
1046 | // Record any add or remove requests. We'll decide on them later. |
||||
1047 | elseif ($v == 'remove') |
||||
1048 | $remove_strings[] = $k; |
||||
1049 | elseif ($v == 'add' && isset($_POST['entry'][$k])) |
||||
1050 | { |
||||
1051 | $add_strings[$k] = array( |
||||
1052 | 'group' => isset($_POST['grp'][$k]) ? $_POST['grp'][$k] : 'txt', |
||||
1053 | 'string' => cleanLangString($_POST['entry'][$k], false), |
||||
1054 | ); |
||||
1055 | } |
||||
1056 | } |
||||
1057 | elseif (is_array($v)) |
||||
1058 | { |
||||
1059 | foreach ($v as $subk => $subv) |
||||
1060 | { |
||||
1061 | if ($subv == 'edit' && isset($_POST['entry'][$k][$subk]) && isset($_POST['comp'][$k][$subk]) && $_POST['entry'][$k][$subk] != $_POST['comp'][$k][$subk]) |
||||
1062 | $save_strings[$k][$subk] = cleanLangString($_POST['entry'][$k][$subk], false); |
||||
1063 | |||||
1064 | elseif ($subv == 'remove') |
||||
1065 | $remove_strings[$k][] = $subk; |
||||
1066 | elseif ($subv == 'add' && isset($_POST['entry'][$k][$subk])) |
||||
1067 | { |
||||
1068 | $add_strings[$k][$subk] = array( |
||||
1069 | 'group' => isset($_POST['grp'][$k]) ? $_POST['grp'][$k] : 'txt', |
||||
1070 | 'string' => cleanLangString($_POST['entry'][$k][$subk], false), |
||||
1071 | ); |
||||
1072 | } |
||||
1073 | |||||
1074 | } |
||||
1075 | } |
||||
1076 | } |
||||
1077 | } |
||||
1078 | } |
||||
1079 | |||||
1080 | // If we are editing a file work away at that. |
||||
1081 | $context['can_add_lang_entry'] = array(); |
||||
1082 | if ($current_file) |
||||
1083 | { |
||||
1084 | $context['entries_not_writable_message'] = is_writable($current_file) ? '' : sprintf($txt['lang_entries_not_writable'], $current_file); |
||||
1085 | |||||
1086 | // How many strings will PHP let us edit at once? |
||||
1087 | // Each string needs 3 inputs, and there are 5 others in the form. |
||||
1088 | $context['max_inputs'] = floor(ini_get('max_input_vars') / 3) - 5; |
||||
1089 | |||||
1090 | // Do we want to override the helptxt for certain types of text variables? |
||||
1091 | $special_groups = array( |
||||
1092 | 'Timezones' => array('txt' => 'txt_for_timezones'), |
||||
1093 | 'EmailTemplates' => array('txt' => 'txt_for_email_templates', 'txtBirthdayEmails' => 'txt_for_email_templates'), |
||||
1094 | ); |
||||
1095 | call_integration_hook('integrate_language_edit_helptext', array(&$special_groups)); |
||||
1096 | |||||
1097 | // Determine which groups of strings (if any) allow adding new entries |
||||
1098 | if (isset($allows_add_remove[$file_id]['add'])) |
||||
1099 | { |
||||
1100 | foreach ($allows_add_remove[$file_id]['add'] as $var_group) |
||||
1101 | { |
||||
1102 | $group = !empty($special_groups[$file_id][$var_group]) ? $special_groups[$file_id][$var_group] : $var_group; |
||||
1103 | if (in_array($var_group, $allows_add_remove[$file_id]['add'])) |
||||
1104 | $context['can_add_lang_entry'][$group] = true; |
||||
1105 | } |
||||
1106 | } |
||||
1107 | |||||
1108 | // Read in the file's contents and process it into entries. |
||||
1109 | // Also, remove any lines for uneditable variables like $forum_copyright from the working data. |
||||
1110 | $entries = array(); |
||||
1111 | foreach (preg_split('~^(?=\$(?:' . implode('|', $string_types) . ')\[\'([^\n]+?)\'\])~m' . ($context['utf8'] ? 'u' : ''), preg_replace('~\s*\n(\$(?!(?:' . implode('|', $string_types) . '))[^\n]*)~', '', file_get_contents($current_file))) as $blob) |
||||
1112 | { |
||||
1113 | // Comment lines at the end of the blob can make terrible messes |
||||
1114 | $blob = preg_replace('~(\n[ \t]*//[^\n]*)*$~' . ($context['utf8'] ? 'u' : ''), '', $blob); |
||||
1115 | |||||
1116 | // Extract the variable |
||||
1117 | if (preg_match('~^\$(' . implode('|', $string_types) . ')\[\'([^\n]+?)\'\](?:\[\'?([^\n]+?)\'?\])?\s?=\s?(.+);([ \t]*(?://[^\n]*)?)$~ms' . ($context['utf8'] ? 'u' : ''), strtr($blob, array("\r" => '')), $matches)) |
||||
1118 | { |
||||
1119 | // If no valid subkey was found, we need it to be explicitly null |
||||
1120 | $matches[3] = isset($matches[3]) && $matches[3] !== '' ? $matches[3] : null; |
||||
1121 | |||||
1122 | // The point of this exercise |
||||
1123 | $entries[$matches[2] . (isset($matches[3]) ? '[' . $matches[3] . ']' : '')] = array( |
||||
1124 | 'type' => $matches[1], |
||||
1125 | 'group' => !empty($special_groups[$file_id][$matches[1]]) ? $special_groups[$file_id][$matches[1]] : $matches[1], |
||||
1126 | 'can_remove' => isset($allows_add_remove[$file_id]['remove']) && in_array($matches[1], $allows_add_remove[$file_id]['remove']), |
||||
1127 | 'key' => $matches[2], |
||||
1128 | 'subkey' => $matches[3], |
||||
1129 | 'full' => $matches[0], |
||||
1130 | 'entry' => $matches[4], |
||||
1131 | 'cruft' => $matches[5], |
||||
1132 | ); |
||||
1133 | } |
||||
1134 | } |
||||
1135 | |||||
1136 | // These will be the entries we can definitely save. |
||||
1137 | $final_saves = array(); |
||||
1138 | |||||
1139 | $context['file_entries'] = array(); |
||||
1140 | foreach ($entries as $entryKey => $entryValue) |
||||
1141 | { |
||||
1142 | // Ignore some things we set separately. |
||||
1143 | if (in_array($entryKey, array_keys($primary_settings))) |
||||
1144 | continue; |
||||
1145 | |||||
1146 | // These are arrays that need breaking out. |
||||
1147 | if (strpos($entryValue['entry'], 'array(') === 0 && strpos($entryValue['entry'], ')', -1) === strlen($entryValue['entry']) - 1) |
||||
1148 | { |
||||
1149 | // No, you may not use multidimensional arrays of $txt strings. Madness stalks that path. |
||||
1150 | if (isset($entryValue['subkey'])) |
||||
1151 | continue; |
||||
1152 | |||||
1153 | // Trim off the array construct bits. |
||||
1154 | $entryValue['entry'] = substr($entryValue['entry'], strpos($entryValue['entry'], 'array(') + 6, -1); |
||||
1155 | |||||
1156 | // This crazy regex extracts each array element, even if the value contains commas or escaped quotes |
||||
1157 | // The keys can be either integers or strings |
||||
1158 | // The values must be strings, or the regex will fail |
||||
1159 | $m = preg_match_all('/ |
||||
1160 | # Optional explicit key assignment |
||||
1161 | (?: |
||||
1162 | (?: |
||||
1163 | \d+ |
||||
1164 | | |
||||
1165 | (?: |
||||
1166 | (?: |
||||
1167 | \'(?:[^\']|(?<=\\\)\')*\' |
||||
1168 | ) |
||||
1169 | | |
||||
1170 | (?: |
||||
1171 | "(?:[^"]|(?<=\\\)")*" |
||||
1172 | ) |
||||
1173 | ) |
||||
1174 | ) |
||||
1175 | \s*=>\s* |
||||
1176 | )? |
||||
1177 | |||||
1178 | # String value in single or double quotes |
||||
1179 | (?: |
||||
1180 | (?: |
||||
1181 | \'(?:[^\']|(?<=\\\)\')*\' |
||||
1182 | ) |
||||
1183 | | |
||||
1184 | (?: |
||||
1185 | "(?:[^"]|(?<=\\\)")*" |
||||
1186 | ) |
||||
1187 | ) |
||||
1188 | |||||
1189 | # Followed by a comma or the end of the string |
||||
1190 | (?=,|$) |
||||
1191 | |||||
1192 | /x' . ($context['utf8'] ? 'u' : ''), $entryValue['entry'], $matches); |
||||
1193 | |||||
1194 | if (empty($m)) |
||||
1195 | continue; |
||||
1196 | |||||
1197 | $entryValue['entry'] = $matches[0]; |
||||
1198 | |||||
1199 | // Now create an entry for each item. |
||||
1200 | $cur_index = -1; |
||||
1201 | $save_cache = array( |
||||
1202 | 'enabled' => false, |
||||
1203 | 'entries' => array(), |
||||
1204 | ); |
||||
1205 | foreach ($entryValue['entry'] as $id => $subValue) |
||||
1206 | { |
||||
1207 | // Is this a new index? |
||||
1208 | if (preg_match('~^(\d+|(?:(?:\'(?:[^\']|(?<=\\\)\')*\')|(?:"(?:[^"]|(?<=\\\)")*")))\s*=>~', $subValue, $matches)) |
||||
1209 | { |
||||
1210 | $subKey = trim($matches[1], '\'"'); |
||||
1211 | |||||
1212 | if (ctype_digit($subKey)) |
||||
1213 | $cur_index = $subKey; |
||||
1214 | |||||
1215 | $subValue = trim(substr($subValue, strpos($subValue, '=>') + 2)); |
||||
1216 | } |
||||
1217 | else |
||||
1218 | $subKey = ++$cur_index; |
||||
1219 | |||||
1220 | // Clean up some bits. |
||||
1221 | if (strpos($subValue, '\'') === 0) |
||||
1222 | $subValue = trim($subValue, '\''); |
||||
1223 | elseif (strpos($subValue, '"') === 0) |
||||
1224 | $subValue = trim($subValue, '"'); |
||||
1225 | |||||
1226 | // Can we save? |
||||
1227 | if (isset($save_strings[$entryKey][$subKey])) |
||||
1228 | { |
||||
1229 | $save_cache['entries'][$subKey] = strtr($save_strings[$entryKey][$subKey], array('\'' => '')); |
||||
1230 | $save_cache['enabled'] = true; |
||||
1231 | } |
||||
1232 | // Should we remove this one? |
||||
1233 | elseif (isset($remove_strings[$entryKey]) && in_array($subKey, $remove_strings[$entryKey]) && $entryValue['can_remove']) |
||||
1234 | $save_cache['enabled'] = true; |
||||
1235 | // Just keep this one as it is |
||||
1236 | else |
||||
1237 | $save_cache['entries'][$subKey] = $subValue; |
||||
1238 | |||||
1239 | $context['file_entries'][$entryValue['group']][] = array( |
||||
1240 | 'key' => $entryKey, |
||||
1241 | 'subkey' => $subKey, |
||||
1242 | 'value' => $subValue, |
||||
1243 | 'rows' => 1, |
||||
1244 | 'can_remove' => $entryValue['can_remove'], |
||||
1245 | ); |
||||
1246 | } |
||||
1247 | |||||
1248 | // Should we add a new string to this array? |
||||
1249 | if (!empty($context['can_add_lang_entry'][$entryValue['type']]) && isset($add_strings[$entryKey])) |
||||
1250 | { |
||||
1251 | foreach ($add_strings[$entryKey] as $string_key => $string_val) |
||||
1252 | $save_cache['entries'][$string_key] = strtr($string_val['string'], array('\'' => '')); |
||||
1253 | |||||
1254 | $save_cache['enabled'] = true; |
||||
1255 | |||||
1256 | // Make sure we don't add this again as an independent line |
||||
1257 | unset($add_strings[$entryKey][$string_key]); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
1258 | if (empty($add_strings[$entryKey])) |
||||
1259 | unset($add_strings[$entryKey]); |
||||
1260 | } |
||||
1261 | |||||
1262 | // Do we need to save? |
||||
1263 | if ($save_cache['enabled']) |
||||
1264 | { |
||||
1265 | // Format the string, checking the indexes first. |
||||
1266 | $items = array(); |
||||
1267 | $cur_index = 0; |
||||
1268 | foreach ($save_cache['entries'] as $k2 => $v2) |
||||
1269 | { |
||||
1270 | // Manually show the custom index. |
||||
1271 | if ($k2 != $cur_index) |
||||
1272 | { |
||||
1273 | $items[] = $k2 . ' => \'' . $v2 . '\''; |
||||
1274 | $cur_index = $k2; |
||||
1275 | } |
||||
1276 | else |
||||
1277 | $items[] = '\'' . $v2 . '\''; |
||||
1278 | |||||
1279 | $cur_index++; |
||||
1280 | } |
||||
1281 | // Now create the string! |
||||
1282 | $final_saves[$entryKey] = array( |
||||
1283 | 'find' => $entryValue['full'], |
||||
1284 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n" . '$' . $entryValue['type'] . '[\'' . $entryKey . '\'] = array(' . implode(', ', $items) . ');' . $entryValue['cruft'], |
||||
1285 | ); |
||||
1286 | } |
||||
1287 | } |
||||
1288 | // A single array element, like: $txt['foo']['bar'] = 'baz'; |
||||
1289 | elseif (isset($entryValue['subkey'])) |
||||
1290 | { |
||||
1291 | // Saving? |
||||
1292 | if (isset($save_strings[$entryValue['key']][$entryValue['subkey']]) && $save_strings[$entryValue['key']][$entryValue['subkey']] != $entryValue['entry']) |
||||
1293 | { |
||||
1294 | if ($save_strings[$entryValue['key']][$entryValue['subkey']] == '') |
||||
1295 | $save_strings[$entryValue['key']][$entryValue['subkey']] = '\'\''; |
||||
1296 | |||||
1297 | // Preserve subkey as either digit or string |
||||
1298 | $subKey = ctype_digit($entryValue['subkey']) ? $entryValue['subkey'] : '\'' . $entryValue['subkey'] . '\''; |
||||
1299 | |||||
1300 | // We have a new value, so we should use it |
||||
1301 | $entryValue['entry'] = $save_strings[$entryValue['key']][$entryValue['subkey']]; |
||||
1302 | |||||
1303 | // And save it |
||||
1304 | $final_saves[$entryKey] = array( |
||||
1305 | 'find' => $entryValue['full'], |
||||
1306 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n" . '$' . $entryValue['type'] . '[\'' . $entryValue['key'] . '\'][' . $subKey . '] = ' . $save_strings[$entryValue['key']][$entryValue['subkey']] . ';' . $entryValue['cruft'], |
||||
1307 | ); |
||||
1308 | } |
||||
1309 | |||||
1310 | // Remove this entry only if it is allowed |
||||
1311 | if (isset($remove_strings[$entryValue['key']]) && in_array($entryValue['subkey'], $remove_strings[$entryValue['key']]) && $entryValue['can_remove']) |
||||
1312 | { |
||||
1313 | $entryValue['entry'] = '\'\''; |
||||
1314 | $final_saves[$entryKey] = array( |
||||
1315 | 'find' => $entryValue['full'], |
||||
1316 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n", |
||||
1317 | ); |
||||
1318 | } |
||||
1319 | |||||
1320 | $editing_string = cleanLangString($entryValue['entry'], true); |
||||
1321 | $context['file_entries'][$entryValue['group']][] = array( |
||||
1322 | 'key' => $entryValue['key'], |
||||
1323 | 'subkey' => $entryValue['subkey'], |
||||
1324 | 'value' => $editing_string, |
||||
1325 | 'rows' => (int) (strlen($editing_string) / 38) + substr_count($editing_string, "\n") + 1, |
||||
1326 | 'can_remove' => $entryValue['can_remove'], |
||||
1327 | ); |
||||
1328 | } |
||||
1329 | // A simple string entry |
||||
1330 | else |
||||
1331 | { |
||||
1332 | // Saving? |
||||
1333 | if (isset($save_strings[$entryValue['key']]) && $save_strings[$entryValue['key']] != $entryValue['entry']) |
||||
1334 | { |
||||
1335 | // @todo Fix this properly. |
||||
1336 | if ($save_strings[$entryValue['key']] == '') |
||||
1337 | $save_strings[$entryValue['key']] = '\'\''; |
||||
1338 | |||||
1339 | // Set the new value. |
||||
1340 | $entryValue['entry'] = $save_strings[$entryValue['key']]; |
||||
1341 | // And we know what to save now! |
||||
1342 | $final_saves[$entryKey] = array( |
||||
1343 | 'find' => $entryValue['full'], |
||||
1344 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n" . '$' . $entryValue['type'] . '[\'' . $entryValue['key'] . '\'] = ' . $save_strings[$entryValue['key']] . ';' . $entryValue['cruft'], |
||||
1345 | ); |
||||
1346 | } |
||||
1347 | // Remove this entry only if it is allowed |
||||
1348 | if (in_array($entryValue['key'], $remove_strings) && $entryValue['can_remove']) |
||||
1349 | { |
||||
1350 | $entryValue['entry'] = '\'\''; |
||||
1351 | $final_saves[$entryKey] = array( |
||||
1352 | 'find' => $entryValue['full'], |
||||
1353 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n", |
||||
1354 | ); |
||||
1355 | } |
||||
1356 | |||||
1357 | $editing_string = cleanLangString($entryValue['entry'], true); |
||||
1358 | $context['file_entries'][$entryValue['group']][] = array( |
||||
1359 | 'key' => $entryValue['key'], |
||||
1360 | 'subkey' => null, |
||||
1361 | 'value' => $editing_string, |
||||
1362 | 'rows' => (int) (strlen($editing_string) / 38) + substr_count($editing_string, "\n") + 1, |
||||
1363 | 'can_remove' => $entryValue['can_remove'], |
||||
1364 | ); |
||||
1365 | } |
||||
1366 | } |
||||
1367 | |||||
1368 | // Do they want to add some brand new strings? Does this file allow that? |
||||
1369 | if (!empty($add_strings) && !empty($allows_add_remove[$file_id]['add'])) |
||||
1370 | { |
||||
1371 | $special_types = isset($special_groups[$file_id]) ? array_flip($special_groups[$file_id]) : array(); |
||||
1372 | |||||
1373 | foreach ($add_strings as $string_key => $string_val) |
||||
1374 | { |
||||
1375 | // Adding a normal string |
||||
1376 | if (isset($string_val['string']) && is_string($string_val['string'])) |
||||
1377 | { |
||||
1378 | $type = isset($special_types[$string_val['group']]) ? $special_types[$string_val['group']] : $string_val['group']; |
||||
1379 | |||||
1380 | if (empty($context['can_add_lang_entry'][$type])) |
||||
1381 | continue; |
||||
1382 | |||||
1383 | $final_saves[$string_key] = array( |
||||
1384 | 'find' => "\s*\?" . '>$', |
||||
1385 | 'replace' => "\n\$" . $type . '[\'' . $string_key . '\'] = ' . $string_val['string'] . ';' . "\n\n?" . '>', |
||||
1386 | 'is_regex' => true, |
||||
1387 | ); |
||||
1388 | } |
||||
1389 | // Adding an array element |
||||
1390 | else |
||||
1391 | { |
||||
1392 | foreach ($string_val as $substring_key => $substring_val) |
||||
1393 | { |
||||
1394 | $type = isset($special_types[$substring_val['group']]) ? $special_types[$substring_val['group']] : $substring_val['group']; |
||||
1395 | |||||
1396 | if (empty($context['can_add_lang_entry'][$type])) |
||||
1397 | continue; |
||||
1398 | |||||
1399 | $subKey = ctype_digit(trim($substring_key, '\'')) ? trim($substring_key, '\'') : '\'' . $substring_key . '\''; |
||||
1400 | |||||
1401 | $final_saves[$string_key . '[' . $substring_key . ']'] = array( |
||||
1402 | 'find' => "\s*\?" . '>$', |
||||
1403 | 'replace' => "\n\$" . $type . '[\'' . $string_key . '\'][' . $subKey . '] = ' . $substring_val['string'] . ';' . "\n\n?" . '>', |
||||
1404 | 'is_regex' => true, |
||||
1405 | ); |
||||
1406 | } |
||||
1407 | } |
||||
1408 | } |
||||
1409 | } |
||||
1410 | |||||
1411 | // Any saves to make? |
||||
1412 | if (!empty($final_saves)) |
||||
1413 | { |
||||
1414 | checkSession(); |
||||
1415 | |||||
1416 | // Get a fresh copy of the file's current content. |
||||
1417 | $file_contents = file_get_contents($current_file); |
||||
1418 | |||||
1419 | // Apply our changes. |
||||
1420 | foreach ($final_saves as $save) |
||||
1421 | { |
||||
1422 | if (!empty($save['is_regex'])) |
||||
1423 | $file_contents = preg_replace('~' . $save['find'] . '~' . ($context['utf8'] ? 'u' : ''), $save['replace'], $file_contents); |
||||
1424 | else |
||||
1425 | $file_contents = str_replace($save['find'], $save['replace'], $file_contents); |
||||
1426 | } |
||||
1427 | |||||
1428 | // Save the result back to the file. |
||||
1429 | file_put_contents($current_file, $file_contents); |
||||
1430 | |||||
1431 | $madeSave = true; |
||||
1432 | } |
||||
1433 | |||||
1434 | // Another restore. |
||||
1435 | $txt = $old_txt; |
||||
1436 | |||||
1437 | // If they can add new language entries, make sure the UI is set up for that. |
||||
1438 | if (!empty($context['can_add_lang_entry'])) |
||||
1439 | { |
||||
1440 | // Make sure the Add button has a place to show up. |
||||
1441 | foreach ($context['can_add_lang_entry'] as $group => $value) |
||||
1442 | { |
||||
1443 | if (!isset($context['file_entries'][$group])) |
||||
1444 | $context['file_entries'][$group] = array(); |
||||
1445 | } |
||||
1446 | |||||
1447 | addInlineJavaScript(' |
||||
1448 | function add_lang_entry(group) { |
||||
1449 | var key = prompt("' . $txt['languages_enter_key'] . '"); |
||||
1450 | |||||
1451 | if (key !== null) { |
||||
1452 | ++entry_num; |
||||
1453 | |||||
1454 | var array_regex = /^(.*)(\[[^\[\]]*\])$/ |
||||
1455 | var result = array_regex.exec(key); |
||||
1456 | if (result != null) { |
||||
1457 | key = result[1]; |
||||
1458 | var subkey = result[2]; |
||||
1459 | } else { |
||||
1460 | var subkey = ""; |
||||
1461 | } |
||||
1462 | |||||
1463 | var bracket_regex = /[\[\]]/ |
||||
1464 | if (bracket_regex.test(key)) { |
||||
1465 | alert("' . $txt['languages_invalid_key'] . '" + key + subkey); |
||||
1466 | return; |
||||
1467 | } |
||||
1468 | |||||
1469 | $("#language_" + group).append("<dt><span>" + key + subkey + "</span></dt> <dd id=\"entry_" + entry_num + "\"><input id=\"entry_" + entry_num + "_edit\" class=\"entry_toggle\" type=\"checkbox\" name=\"edit[" + key + "]" + subkey + "\" value=\"add\" data-target=\"#entry_" + entry_num + "\" checked> <label for=\"entry_" + entry_num + "_edit\">' . $txt['edit'] . '</label> <input type=\"hidden\" class=\"entry_oldvalue\" name=\"grp[" + key + "]\" value=\"" + group + "\"> <textarea name=\"entry[" + key + "]" + subkey + "\" class=\"entry_textfield\" cols=\"40\" rows=\"1\" style=\"width: 96%; margin-bottom: 2em;\"></textarea></dd>"); |
||||
1470 | } |
||||
1471 | };'); |
||||
1472 | |||||
1473 | addInlineJavaScript(' |
||||
1474 | $(".add_lang_entry_button").show();', true); |
||||
1475 | } |
||||
1476 | |||||
1477 | // Warn them if they try to submit more changes than the server can accept in a single request. |
||||
1478 | // Also make it obvious that they cannot submit changes to both the primary settings and the entries at the same time. |
||||
1479 | if (!empty($context['file_entries'])) |
||||
1480 | { |
||||
1481 | addInlineJavaScript(' |
||||
1482 | max_inputs = ' . $context['max_inputs'] . '; |
||||
1483 | num_inputs = 0; |
||||
1484 | |||||
1485 | $(".entry_textfield").prop("disabled", true); |
||||
1486 | $(".entry_oldvalue").prop("disabled", true); |
||||
1487 | |||||
1488 | $(".entry_toggle").click(function() { |
||||
1489 | var target_dd = $( $(this).data("target") ); |
||||
1490 | |||||
1491 | if ($(this).prop("checked") === true && $(this).val() === "edit") { |
||||
1492 | if (++num_inputs <= max_inputs) { |
||||
1493 | target_dd.find(".entry_oldvalue, .entry_textfield").prop("disabled", false); |
||||
1494 | } else { |
||||
1495 | alert("' . sprintf($txt['languages_max_inputs_warning'], $context['max_inputs']) . '"); |
||||
1496 | $(this).prop("checked", false); |
||||
1497 | } |
||||
1498 | } else { |
||||
1499 | --num_inputs; |
||||
1500 | target_dd.find(".entry_oldvalue, .entry_textfield").prop("disabled", true); |
||||
1501 | } |
||||
1502 | |||||
1503 | if (num_inputs > 0) { |
||||
1504 | $("#primary_settings").trigger("reset"); |
||||
1505 | $("#primary_settings input").prop("disabled", true); |
||||
1506 | } else { |
||||
1507 | $("#primary_settings input").prop("disabled", false); |
||||
1508 | } |
||||
1509 | }); |
||||
1510 | |||||
1511 | $("#primary_settings input").change(function() { |
||||
1512 | num_changed = 0; |
||||
1513 | $("#primary_settings input:text").each(function(i, e) { |
||||
1514 | if ($(e).data("orig") != $(e).val()) |
||||
1515 | num_changed++; |
||||
1516 | }); |
||||
1517 | $("#primary_settings input:checkbox").each(function(i, e) { |
||||
1518 | cur_val = $(e).is(":checked"); |
||||
1519 | orig_val = $(e).val == "true"; |
||||
1520 | if (cur_val != orig_val) |
||||
1521 | num_changed++; |
||||
1522 | }); |
||||
1523 | |||||
1524 | if (num_changed > 0) { |
||||
1525 | $("#entry_fields").fadeOut(); |
||||
1526 | } else { |
||||
1527 | $("#entry_fields").fadeIn(); |
||||
1528 | } |
||||
1529 | }); |
||||
1530 | $("#reset_main").click(function() { |
||||
1531 | $("#entry_fields").fadeIn(); |
||||
1532 | });', true); |
||||
1533 | } |
||||
1534 | } |
||||
1535 | |||||
1536 | // If we saved, redirect. |
||||
1537 | if ($madeSave) |
||||
0 ignored issues
–
show
|
|||||
1538 | redirectexit('action=admin;area=languages;sa=editlang;lid=' . $context['lang_id'] . (!empty($file_id) ? ';entries;tfid=' . $theme_id . rawurlencode('+') . $file_id : '')); |
||||
1539 | |||||
1540 | createToken('admin-mlang'); |
||||
1541 | } |
||||
1542 | |||||
1543 | /** |
||||
1544 | * This function cleans language entries to/from display. |
||||
1545 | * |
||||
1546 | * @todo This function could be two functions? |
||||
1547 | * |
||||
1548 | * @param string $string The language string |
||||
1549 | * @param bool $to_display Whether or not this is going to be displayed |
||||
1550 | * @return string The cleaned string |
||||
1551 | */ |
||||
1552 | function cleanLangString($string, $to_display = true) |
||||
1553 | { |
||||
1554 | global $smcFunc; |
||||
1555 | |||||
1556 | // If going to display we make sure it doesn't have any HTML in it - etc. |
||||
1557 | $new_string = ''; |
||||
1558 | if ($to_display) |
||||
1559 | { |
||||
1560 | // Are we in a string (0 = no, 1 = single quote, 2 = parsed) |
||||
1561 | $in_string = 0; |
||||
1562 | $is_escape = false; |
||||
1563 | for ($i = 0; $i < strlen($string); $i++) |
||||
1564 | { |
||||
1565 | // Handle escapes first. |
||||
1566 | if ($string[$i] == '\\') |
||||
1567 | { |
||||
1568 | // Toggle the escape. |
||||
1569 | $is_escape = !$is_escape; |
||||
0 ignored issues
–
show
|
|||||
1570 | // If we're now escaped don't add this string. |
||||
1571 | if ($is_escape) |
||||
1572 | continue; |
||||
1573 | } |
||||
1574 | // Special case - parsed string with line break etc? |
||||
1575 | elseif (($string[$i] == 'n' || $string[$i] == 't') && $in_string == 2 && $is_escape) |
||||
1576 | { |
||||
1577 | // Put the escape back... |
||||
1578 | $new_string .= $string[$i] == 'n' ? "\n" : "\t"; |
||||
1579 | $is_escape = false; |
||||
1580 | continue; |
||||
1581 | } |
||||
1582 | // Have we got a single quote? |
||||
1583 | elseif ($string[$i] == '\'') |
||||
1584 | { |
||||
1585 | // Already in a parsed string, or escaped in a linear string, means we print it - otherwise something special. |
||||
1586 | if ($in_string != 2 && ($in_string != 1 || !$is_escape)) |
||||
1587 | { |
||||
1588 | // Is it the end of a single quote string? |
||||
1589 | if ($in_string == 1) |
||||
1590 | $in_string = 0; |
||||
1591 | // Otherwise it's the start! |
||||
1592 | else |
||||
1593 | $in_string = 1; |
||||
1594 | |||||
1595 | // Don't actually include this character! |
||||
1596 | continue; |
||||
1597 | } |
||||
1598 | } |
||||
1599 | // Otherwise a double quote? |
||||
1600 | elseif ($string[$i] == '"') |
||||
1601 | { |
||||
1602 | // Already in a single quote string, or escaped in a parsed string, means we print it - otherwise something special. |
||||
1603 | if ($in_string != 1 && ($in_string != 2 || !$is_escape)) |
||||
1604 | { |
||||
1605 | // Is it the end of a double quote string? |
||||
1606 | if ($in_string == 2) |
||||
1607 | $in_string = 0; |
||||
1608 | // Otherwise it's the start! |
||||
1609 | else |
||||
1610 | $in_string = 2; |
||||
1611 | |||||
1612 | // Don't actually include this character! |
||||
1613 | continue; |
||||
1614 | } |
||||
1615 | } |
||||
1616 | // A join/space outside of a string is simply removed. |
||||
1617 | elseif ($in_string == 0 && (empty($string[$i]) || $string[$i] == '.')) |
||||
1618 | continue; |
||||
1619 | // Start of a variable? |
||||
1620 | elseif ($in_string == 0 && $string[$i] == '$') |
||||
1621 | { |
||||
1622 | // Find the whole of it! |
||||
1623 | preg_match('~([\$A-Za-z0-9\'\[\]_-]+)~', substr($string, $i), $matches); |
||||
1624 | if (!empty($matches[1])) |
||||
1625 | { |
||||
1626 | // Come up with some pseudo thing to indicate this is a var. |
||||
1627 | /** |
||||
1628 | * @todo Do better than this, please! |
||||
1629 | */ |
||||
1630 | $new_string .= '{%' . $matches[1] . '%}'; |
||||
1631 | |||||
1632 | // We're not going to reparse this. |
||||
1633 | $i += strlen($matches[1]) - 1; |
||||
1634 | } |
||||
1635 | |||||
1636 | continue; |
||||
1637 | } |
||||
1638 | // Right, if we're outside of a string we have DANGER, DANGER! |
||||
1639 | elseif ($in_string == 0) |
||||
1640 | { |
||||
1641 | continue; |
||||
1642 | } |
||||
1643 | |||||
1644 | // Actually add the character to the string! |
||||
1645 | $new_string .= $string[$i]; |
||||
1646 | // If anything was escaped it ain't any longer! |
||||
1647 | $is_escape = false; |
||||
1648 | } |
||||
1649 | |||||
1650 | // Unhtml then rehtml the whole thing! |
||||
1651 | $new_string = $smcFunc['htmlspecialchars'](un_htmlspecialchars($new_string)); |
||||
1652 | } |
||||
1653 | else |
||||
1654 | { |
||||
1655 | // Keep track of what we're doing... |
||||
1656 | $in_string = 0; |
||||
1657 | // This is for deciding whether to HTML a quote. |
||||
1658 | $in_html = false; |
||||
1659 | for ($i = 0; $i < strlen($string); $i++) |
||||
1660 | { |
||||
1661 | // We don't do parsed strings apart from for breaks. |
||||
1662 | if ($in_string == 2) |
||||
1663 | { |
||||
1664 | $in_string = 0; |
||||
1665 | $new_string .= '"'; |
||||
1666 | } |
||||
1667 | |||||
1668 | // Not in a string yet? |
||||
1669 | if ($in_string != 1) |
||||
1670 | { |
||||
1671 | $in_string = 1; |
||||
1672 | $new_string .= ($new_string ? ' . ' : '') . '\''; |
||||
1673 | } |
||||
1674 | |||||
1675 | // Is this a variable? |
||||
1676 | if ($string[$i] == '{' && $string[$i + 1] == '%' && $string[$i + 2] == '$') |
||||
1677 | { |
||||
1678 | // Grab the variable. |
||||
1679 | preg_match('~\{%([\$A-Za-z0-9\'\[\]_-]+)%\}~', substr($string, $i), $matches); |
||||
1680 | if (!empty($matches[1])) |
||||
1681 | { |
||||
1682 | if ($in_string == 1) |
||||
1683 | $new_string .= '\' . '; |
||||
1684 | elseif ($new_string) |
||||
1685 | $new_string .= ' . '; |
||||
1686 | |||||
1687 | $new_string .= $matches[1]; |
||||
1688 | $i += strlen($matches[1]) + 3; |
||||
1689 | $in_string = 0; |
||||
1690 | } |
||||
1691 | |||||
1692 | continue; |
||||
1693 | } |
||||
1694 | // Is this a lt sign? |
||||
1695 | elseif ($string[$i] == '<') |
||||
1696 | { |
||||
1697 | // Probably HTML? |
||||
1698 | if ($string[$i + 1] != ' ') |
||||
1699 | $in_html = true; |
||||
1700 | // Assume we need an entity... |
||||
1701 | else |
||||
1702 | { |
||||
1703 | $new_string .= '<'; |
||||
1704 | continue; |
||||
1705 | } |
||||
1706 | } |
||||
1707 | // What about gt? |
||||
1708 | elseif ($string[$i] == '>') |
||||
1709 | { |
||||
1710 | // Will it be HTML? |
||||
1711 | if ($in_html) |
||||
1712 | $in_html = false; |
||||
1713 | // Otherwise we need an entity... |
||||
1714 | else |
||||
1715 | { |
||||
1716 | $new_string .= '>'; |
||||
1717 | continue; |
||||
1718 | } |
||||
1719 | } |
||||
1720 | // Is it a slash? If so escape it... |
||||
1721 | if ($string[$i] == '\\') |
||||
1722 | $new_string .= '\\'; |
||||
1723 | // The infamous double quote? |
||||
1724 | elseif ($string[$i] == '"') |
||||
1725 | { |
||||
1726 | // If we're in HTML we leave it as a quote - otherwise we entity it. |
||||
1727 | if (!$in_html) |
||||
1728 | { |
||||
1729 | $new_string .= '"'; |
||||
1730 | continue; |
||||
1731 | } |
||||
1732 | } |
||||
1733 | // A single quote? |
||||
1734 | elseif ($string[$i] == '\'') |
||||
1735 | { |
||||
1736 | // Must be in a string so escape it. |
||||
1737 | $new_string .= '\\'; |
||||
1738 | } |
||||
1739 | |||||
1740 | // Finally add the character to the string! |
||||
1741 | $new_string .= $string[$i]; |
||||
1742 | } |
||||
1743 | |||||
1744 | // If we ended as a string then close it off. |
||||
1745 | if ($in_string == 1) |
||||
1746 | $new_string .= '\''; |
||||
1747 | elseif ($in_string == 2) |
||||
1748 | $new_string .= '"'; |
||||
1749 | } |
||||
1750 | |||||
1751 | return $new_string; |
||||
1752 | } |
||||
1753 | |||||
1754 | ?> |