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 http://www.simplemachines.org |
||
10 | * @copyright 2019 Simple Machines and individual contributors |
||
11 | * @license http://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 ManageLanguages template, add_language sub-template. |
||
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 ManageLanguages template, download_language sub-template. |
||
193 | * @uses Admin template, show_list sub-template. |
||
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)) |
||
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); |
||
339 | fclose($fp); |
||
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) |
||
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'] = '<div class="centertext"><strong>' . $txt['settings_not_writable'] . '</strong></div><br>'; |
||
783 | elseif ($settings_backup_fail) |
||
784 | $context['settings_message'] = '<div class="centertext"><strong>' . $txt['admin_backup_fail'] . '</strong></div><br>'; |
||
785 | |||
786 | // Fill the config array. |
||
787 | prepareServerSettingsContext($config_vars); |
||
788 | } |
||
789 | |||
790 | /** |
||
791 | * Edit a particular set of language entries. |
||
792 | */ |
||
793 | function ModifyLanguage() |
||
794 | { |
||
795 | global $settings, $context, $smcFunc, $txt, $modSettings, $boarddir, $sourcedir, $language, $cache_enable; |
||
796 | |||
797 | loadLanguage('ManageSettings'); |
||
798 | |||
799 | // Select the languages tab. |
||
800 | $context['menu_data_' . $context['admin_menu_id']]['current_subsection'] = 'edit'; |
||
801 | $context['page_title'] = $txt['edit_languages']; |
||
802 | $context['sub_template'] = 'modify_language_entries'; |
||
803 | |||
804 | $context['lang_id'] = $_GET['lid']; |
||
805 | list($theme_id, $file_id) = empty($_REQUEST['tfid']) || strpos($_REQUEST['tfid'], '+') === false ? array(1, '') : explode('+', $_REQUEST['tfid']); |
||
806 | |||
807 | // Clean the ID - just in case. |
||
808 | preg_match('~([A-Za-z0-9_-]+)~', $context['lang_id'], $matches); |
||
809 | $context['lang_id'] = $matches[1]; |
||
810 | |||
811 | // Get all the theme data. |
||
812 | $request = $smcFunc['db_query']('', ' |
||
813 | SELECT id_theme, variable, value |
||
814 | FROM {db_prefix}themes |
||
815 | WHERE id_theme != {int:default_theme} |
||
816 | AND id_member = {int:no_member} |
||
817 | AND variable IN ({string:name}, {string:theme_dir})', |
||
818 | array( |
||
819 | 'default_theme' => 1, |
||
820 | 'no_member' => 0, |
||
821 | 'name' => 'name', |
||
822 | 'theme_dir' => 'theme_dir', |
||
823 | ) |
||
824 | ); |
||
825 | $themes = array( |
||
826 | 1 => array( |
||
827 | 'name' => $txt['dvc_default'], |
||
828 | 'theme_dir' => $settings['default_theme_dir'], |
||
829 | ), |
||
830 | ); |
||
831 | while ($row = $smcFunc['db_fetch_assoc']($request)) |
||
832 | $themes[$row['id_theme']][$row['variable']] = $row['value']; |
||
833 | $smcFunc['db_free_result']($request); |
||
834 | |||
835 | // This will be where we look |
||
836 | $lang_dirs = array(); |
||
837 | |||
838 | // There are different kinds of strings |
||
839 | $string_types = array('txt', 'helptxt', 'editortxt', 'tztxt', 'txtBirthdayEmails'); |
||
840 | $additional_string_types = array(); |
||
841 | |||
842 | // Some files allow the admin to add and/or remove certain types of strings |
||
843 | $allows_add_remove = array( |
||
844 | 'Timezones' => array( |
||
845 | 'add' => array('tztxt', 'txt'), |
||
846 | 'remove' => array('tztxt', 'txt'), |
||
847 | ), |
||
848 | 'Modifications' => array( |
||
849 | 'add' => array('txt'), |
||
850 | 'remove' => array('txt'), |
||
851 | ), |
||
852 | 'ThemeStrings' => array( |
||
853 | 'add' => array('txt'), |
||
854 | ), |
||
855 | ); |
||
856 | |||
857 | // Does a hook need to add in some additional places to look for languages or info about how to handle them? |
||
858 | call_integration_hook('integrate_modifylanguages', array(&$themes, &$lang_dirs, &$allows_add_remove, &$additional_string_types)); |
||
859 | |||
860 | $string_types = array_unique(array_merge($string_types, $additional_string_types)); |
||
861 | |||
862 | // Check we have themes with a path and a name - just in case - and add the path. |
||
863 | foreach ($themes as $id => $data) |
||
864 | { |
||
865 | if (count($data) != 2) |
||
866 | unset($themes[$id]); |
||
867 | elseif (is_dir($data['theme_dir'] . '/languages')) |
||
868 | $lang_dirs[$id] = $data['theme_dir'] . '/languages'; |
||
869 | |||
870 | // How about image directories? |
||
871 | if (is_dir($data['theme_dir'] . '/images/' . $context['lang_id'])) |
||
872 | $images_dirs[$id] = $data['theme_dir'] . '/images/' . $context['lang_id']; |
||
873 | } |
||
874 | |||
875 | $current_file = $file_id ? $lang_dirs[$theme_id] . '/' . $file_id . '.' . $context['lang_id'] . '.php' : ''; |
||
876 | |||
877 | // Now for every theme get all the files and stick them in context! |
||
878 | $context['possible_files'] = array(); |
||
879 | foreach ($lang_dirs as $theme => $theme_dir) |
||
880 | { |
||
881 | // Open it up. |
||
882 | $dir = dir($theme_dir); |
||
883 | while ($entry = $dir->read()) |
||
884 | { |
||
885 | // We're only after the files for this language. |
||
886 | if (preg_match('~^([A-Za-z]+)\.' . $context['lang_id'] . '\.php$~', $entry, $matches) == 0) |
||
887 | continue; |
||
888 | |||
889 | if (!isset($context['possible_files'][$theme])) |
||
890 | $context['possible_files'][$theme] = array( |
||
891 | 'id' => $theme, |
||
892 | 'name' => $themes[$theme]['name'], |
||
893 | 'files' => array(), |
||
894 | ); |
||
895 | |||
896 | $context['possible_files'][$theme]['files'][] = array( |
||
897 | 'id' => $matches[1], |
||
898 | 'name' => isset($txt['lang_file_desc_' . $matches[1]]) ? $txt['lang_file_desc_' . $matches[1]] : $matches[1], |
||
899 | 'selected' => $theme_id == $theme && $file_id == $matches[1], |
||
900 | ); |
||
901 | } |
||
902 | $dir->close(); |
||
903 | usort($context['possible_files'][$theme]['files'], function($val1, $val2) |
||
904 | { |
||
905 | return strcmp($val1['name'], $val2['name']); |
||
906 | }); |
||
907 | } |
||
908 | |||
909 | // We no longer wish to speak this language. |
||
910 | if (!empty($_POST['delete_main']) && $context['lang_id'] != 'english') |
||
911 | { |
||
912 | checkSession(); |
||
913 | validateToken('admin-mlang'); |
||
914 | |||
915 | // @todo Todo: FTP Controls? |
||
916 | require_once($sourcedir . '/Subs-Package.php'); |
||
917 | |||
918 | // First, Make a backup? |
||
919 | if (!empty($modSettings['package_make_backups']) && (!isset($_SESSION['last_backup_for']) || $_SESSION['last_backup_for'] != $context['lang_id'] . '$$$')) |
||
920 | { |
||
921 | $_SESSION['last_backup_for'] = $context['lang_id'] . '$$$'; |
||
922 | $result = package_create_backup('backup_lang_' . $context['lang_id']); |
||
923 | if (!$result) |
||
924 | fatal_lang_error('could_not_language_backup', false); |
||
925 | } |
||
926 | |||
927 | // Second, loop through the array to remove the files. |
||
928 | foreach ($lang_dirs as $curPath) |
||
929 | { |
||
930 | foreach ($context['possible_files'][1]['files'] as $lang) |
||
931 | if (file_exists($curPath . '/' . $lang['id'] . '.' . $context['lang_id'] . '.php')) |
||
932 | unlink($curPath . '/' . $lang['id'] . '.' . $context['lang_id'] . '.php'); |
||
933 | |||
934 | // Check for the email template. |
||
935 | if (file_exists($curPath . '/EmailTemplates.' . $context['lang_id'] . '.php')) |
||
936 | unlink($curPath . '/EmailTemplates.' . $context['lang_id'] . '.php'); |
||
937 | } |
||
938 | |||
939 | // Third, the agreement file. |
||
940 | if (file_exists($boarddir . '/agreement.' . $context['lang_id'] . '.txt')) |
||
941 | unlink($boarddir . '/agreement.' . $context['lang_id'] . '.txt'); |
||
942 | |||
943 | // Fourth, a related images folder, if it exists... |
||
944 | if (!empty($images_dirs)) |
||
945 | foreach ($images_dirs as $curPath) |
||
946 | if (is_dir($curPath)) |
||
947 | deltree($curPath); |
||
948 | |||
949 | // Members can no longer use this language. |
||
950 | $smcFunc['db_query']('', ' |
||
951 | UPDATE {db_prefix}members |
||
952 | SET lngfile = {empty} |
||
953 | WHERE lngfile = {string:current_language}', |
||
954 | array( |
||
955 | 'empty_string' => '', |
||
956 | 'current_language' => $context['lang_id'], |
||
957 | ) |
||
958 | ); |
||
959 | |||
960 | // Fifth, update getLanguages() cache. |
||
961 | if (!empty($cache_enable)) |
||
962 | { |
||
963 | cache_put_data('known_languages', null, !empty($cache_enable) && $cache_enable < 1 ? 86400 : 3600); |
||
964 | } |
||
965 | |||
966 | // Sixth, if we deleted the default language, set us back to english? |
||
967 | if ($context['lang_id'] == $language) |
||
968 | { |
||
969 | require_once($sourcedir . '/Subs-Admin.php'); |
||
970 | $language = 'english'; |
||
971 | updateSettingsFile(array('language' => '\'' . $language . '\'')); |
||
972 | } |
||
973 | |||
974 | // Seventh, get out of here. |
||
975 | redirectexit('action=admin;area=languages;sa=edit;' . $context['session_var'] . '=' . $context['session_id']); |
||
976 | } |
||
977 | |||
978 | // Saving primary settings? |
||
979 | $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'); |
||
980 | $madeSave = false; |
||
981 | if (!empty($_POST['save_main']) && !$current_file) |
||
982 | { |
||
983 | checkSession(); |
||
984 | validateToken('admin-mlang'); |
||
985 | |||
986 | // Read in the current file. |
||
987 | $current_data = implode('', file($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php')); |
||
988 | |||
989 | // Build the replacements. old => new |
||
990 | $replace_array = array(); |
||
991 | foreach ($primary_settings as $setting => $type) |
||
992 | $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])) . '\'') . ';'; |
||
993 | |||
994 | $current_data = preg_replace(array_keys($replace_array), array_values($replace_array), $current_data); |
||
995 | $fp = fopen($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php', 'w+'); |
||
996 | fwrite($fp, $current_data); |
||
997 | fclose($fp); |
||
998 | |||
999 | $madeSave = true; |
||
1000 | } |
||
1001 | |||
1002 | // Quickly load index language entries. |
||
1003 | $old_txt = $txt; |
||
1004 | require($settings['default_theme_dir'] . '/languages/index.' . $context['lang_id'] . '.php'); |
||
1005 | $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'); |
||
1006 | // Setup the primary settings context. |
||
1007 | $context['primary_settings']['name'] = $smcFunc['ucwords'](strtr($context['lang_id'], array('_' => ' ', '-utf8' => ''))); |
||
1008 | foreach ($primary_settings as $setting => $type) |
||
1009 | { |
||
1010 | $context['primary_settings'][$setting] = array( |
||
1011 | 'label' => str_replace('lang_', '', $setting), |
||
1012 | 'value' => $txt[$setting], |
||
1013 | ); |
||
1014 | } |
||
1015 | |||
1016 | // Restore normal service. |
||
1017 | $txt = $old_txt; |
||
1018 | |||
1019 | // Are we saving? |
||
1020 | $save_strings = array(); |
||
1021 | $remove_strings = array(); |
||
1022 | $add_strings = array(); |
||
1023 | if (isset($_POST['save_entries'])) |
||
1024 | { |
||
1025 | checkSession(); |
||
1026 | validateToken('admin-mlang'); |
||
1027 | |||
1028 | if (!empty($_POST['edit'])) |
||
1029 | { |
||
1030 | foreach ($_POST['edit'] as $k => $v) |
||
1031 | { |
||
1032 | if (is_string($v)) |
||
1033 | { |
||
1034 | // Only try to save if 'edit' was specified and if the string has changed |
||
1035 | if ($v == 'edit' && isset($_POST['entry'][$k]) && isset($_POST['comp'][$k]) && $_POST['entry'][$k] != $_POST['comp'][$k]) |
||
1036 | $save_strings[$k] = cleanLangString($_POST['entry'][$k], false); |
||
1037 | |||
1038 | // Record any add or remove requests. We'll decide on them later. |
||
1039 | elseif ($v == 'remove') |
||
1040 | $remove_strings[] = $k; |
||
1041 | elseif ($v == 'add' && isset($_POST['entry'][$k])) |
||
1042 | { |
||
1043 | $add_strings[$k] = array( |
||
1044 | 'group' => isset($_POST['grp'][$k]) ? $_POST['grp'][$k] : 'txt', |
||
1045 | 'string' => cleanLangString($_POST['entry'][$k], false), |
||
1046 | ); |
||
1047 | } |
||
1048 | } |
||
1049 | elseif (is_array($v)) |
||
1050 | { |
||
1051 | foreach ($v as $subk => $subv) |
||
1052 | { |
||
1053 | if ($subv == 'edit' && isset($_POST['entry'][$k][$subk]) && isset($_POST['comp'][$k][$subk]) && $_POST['entry'][$k][$subk] != $_POST['comp'][$k][$subk]) |
||
1054 | $save_strings[$k][$subk] = cleanLangString($_POST['entry'][$k][$subk], false); |
||
1055 | |||
1056 | elseif ($subv == 'remove') |
||
1057 | $remove_strings[$k][] = $subk; |
||
1058 | elseif ($subv == 'add' && isset($_POST['entry'][$k][$subk])) |
||
1059 | { |
||
1060 | $add_strings[$k][$subk] = array( |
||
1061 | 'group' => isset($_POST['grp'][$k]) ? $_POST['grp'][$k] : 'txt', |
||
1062 | 'string' => cleanLangString($_POST['entry'][$k][$subk], false), |
||
1063 | ); |
||
1064 | } |
||
1065 | |||
1066 | } |
||
1067 | } |
||
1068 | } |
||
1069 | } |
||
1070 | } |
||
1071 | |||
1072 | // If we are editing a file work away at that. |
||
1073 | $context['can_add_lang_entry'] = array(); |
||
1074 | if ($current_file) |
||
1075 | { |
||
1076 | $context['entries_not_writable_message'] = is_writable($current_file) ? '' : sprintf($txt['lang_entries_not_writable'], $current_file); |
||
1077 | |||
1078 | // How many strings will PHP let us edit at once? |
||
1079 | // Each string needs 3 inputs, and there are 5 others in the form. |
||
1080 | $context['max_inputs'] = floor(ini_get('max_input_vars') / 3) - 5; |
||
1081 | |||
1082 | // Do we want to override the helptxt for certain types of text variables? |
||
1083 | $special_groups = array( |
||
1084 | 'Timezones' => array('txt' => 'txt_for_timezones'), |
||
1085 | 'EmailTemplates' => array('txt' => 'txt_for_email_templates', 'txtBirthdayEmails' => 'txt_for_email_templates'), |
||
1086 | ); |
||
1087 | call_integration_hook('integrate_language_edit_helptext', array(&$special_groups)); |
||
1088 | |||
1089 | // Determine which groups of strings (if any) allow adding new entries |
||
1090 | if (isset($allows_add_remove[$file_id]['add'])) |
||
1091 | { |
||
1092 | foreach ($allows_add_remove[$file_id]['add'] as $var_group) |
||
1093 | { |
||
1094 | $group = !empty($special_groups[$file_id][$var_group]) ? $special_groups[$file_id][$var_group] : $var_group; |
||
1095 | if (in_array($var_group, $allows_add_remove[$file_id]['add'])) |
||
1096 | $context['can_add_lang_entry'][$group] = true; |
||
1097 | } |
||
1098 | } |
||
1099 | |||
1100 | // Read in the file's contents and process it into entries. |
||
1101 | // Also, remove any lines for uneditable variables like $forum_copyright from the working data. |
||
1102 | $entries = array(); |
||
1103 | 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) |
||
1104 | { |
||
1105 | // Comment lines at the end of the blob can make terrible messes |
||
1106 | $blob = preg_replace('~(\n[ \t]*//[^\n]*)*$~' . ($context['utf8'] ? 'u' : ''), '', $blob); |
||
1107 | |||
1108 | // Extract the variable |
||
1109 | if (preg_match('~^\$(' . implode('|', $string_types) . ')\[\'([^\n]+?)\'\](?:\[\'?([^\n]+?)\'?\])?\s?=\s?(.+);([ \t]*(?://[^\n]*)?)$~ms' . ($context['utf8'] ? 'u' : ''), strtr($blob, array("\r" => '')), $matches)) |
||
1110 | { |
||
1111 | // If no valid subkey was found, we need it to be explicitly null |
||
1112 | $matches[3] = isset($matches[3]) && $matches[3] !== '' ? $matches[3] : null; |
||
1113 | |||
1114 | // The point of this exercise |
||
1115 | $entries[$matches[2] . (isset($matches[3]) ? '[' . $matches[3] . ']' : '')] = array( |
||
1116 | 'type' => $matches[1], |
||
1117 | 'group' => !empty($special_groups[$file_id][$matches[1]]) ? $special_groups[$file_id][$matches[1]] : $matches[1], |
||
1118 | 'can_remove' => isset($allows_add_remove[$file_id]['remove']) && in_array($matches[1], $allows_add_remove[$file_id]['remove']), |
||
1119 | 'key' => $matches[2], |
||
1120 | 'subkey' => $matches[3], |
||
1121 | 'full' => $matches[0], |
||
1122 | 'entry' => $matches[4], |
||
1123 | 'cruft' => $matches[5], |
||
1124 | ); |
||
1125 | } |
||
1126 | } |
||
1127 | |||
1128 | // These will be the entries we can definitely save. |
||
1129 | $final_saves = array(); |
||
1130 | |||
1131 | $context['file_entries'] = array(); |
||
1132 | foreach ($entries as $entryKey => $entryValue) |
||
1133 | { |
||
1134 | // Ignore some things we set separately. |
||
1135 | if (in_array($entryKey, array_keys($primary_settings))) |
||
1136 | continue; |
||
1137 | |||
1138 | // These are arrays that need breaking out. |
||
1139 | if (strpos($entryValue['entry'], 'array(') === 0 && strpos($entryValue['entry'], ')', -1) === strlen($entryValue['entry']) - 1) |
||
1140 | { |
||
1141 | // No, you may not use multidimensional arrays of $txt strings. Madness stalks that path. |
||
1142 | if (isset($entryValue['subkey'])) |
||
1143 | continue; |
||
1144 | |||
1145 | // Trim off the array construct bits. |
||
1146 | $entryValue['entry'] = substr($entryValue['entry'], strpos($entryValue['entry'], 'array(') + 6, -1); |
||
1147 | |||
1148 | // This crazy regex extracts each array element, even if the value contains commas or escaped quotes |
||
1149 | // The keys can be either integers or strings |
||
1150 | // The values must be strings, or the regex will fail |
||
1151 | $m = preg_match_all('/ |
||
1152 | # Optional explicit key assignment |
||
1153 | (?: |
||
1154 | (?: |
||
1155 | \d+ |
||
1156 | | |
||
1157 | (?: |
||
1158 | (?: |
||
1159 | \'(?:[^\']|(?<=\\\)\')*\' |
||
1160 | ) |
||
1161 | | |
||
1162 | (?: |
||
1163 | "(?:[^"]|(?<=\\\)")*" |
||
1164 | ) |
||
1165 | ) |
||
1166 | ) |
||
1167 | \s*=>\s* |
||
1168 | )? |
||
1169 | |||
1170 | # String value in single or double quotes |
||
1171 | (?: |
||
1172 | (?: |
||
1173 | \'(?:[^\']|(?<=\\\)\')*\' |
||
1174 | ) |
||
1175 | | |
||
1176 | (?: |
||
1177 | "(?:[^"]|(?<=\\\)")*" |
||
1178 | ) |
||
1179 | ) |
||
1180 | |||
1181 | # Followed by a comma or the end of the string |
||
1182 | (?=,|$) |
||
1183 | |||
1184 | /x' . ($context['utf8'] ? 'u' : ''), $entryValue['entry'], $matches); |
||
1185 | |||
1186 | if (empty($m)) |
||
1187 | continue; |
||
1188 | |||
1189 | $entryValue['entry'] = $matches[0]; |
||
1190 | |||
1191 | // Now create an entry for each item. |
||
1192 | $cur_index = -1; |
||
1193 | $save_cache = array( |
||
1194 | 'enabled' => false, |
||
1195 | 'entries' => array(), |
||
1196 | ); |
||
1197 | foreach ($entryValue['entry'] as $id => $subValue) |
||
1198 | { |
||
1199 | // Is this a new index? |
||
1200 | if (preg_match('~^(\d+|(?:(?:\'(?:[^\']|(?<=\\\)\')*\')|(?:"(?:[^"]|(?<=\\\)")*")))\s*=>~', $subValue, $matches)) |
||
1201 | { |
||
1202 | $subKey = trim($matches[1], '\'"'); |
||
1203 | |||
1204 | if (ctype_digit($subKey)) |
||
1205 | $cur_index = $subKey; |
||
1206 | |||
1207 | $subValue = trim(substr($subValue, strpos($subValue, '=>') + 2)); |
||
1208 | } |
||
1209 | else |
||
1210 | $subKey = ++$cur_index; |
||
1211 | |||
1212 | // Clean up some bits. |
||
1213 | if (strpos($subValue, '\'') === 0) |
||
1214 | $subValue = trim($subValue, '\''); |
||
1215 | elseif (strpos($subValue, '"') === 0) |
||
1216 | $subValue = trim($subValue, '"'); |
||
1217 | |||
1218 | // Can we save? |
||
1219 | if (isset($save_strings[$entryKey][$subKey])) |
||
1220 | { |
||
1221 | $save_cache['entries'][$subKey] = strtr($save_strings[$entryKey][$subKey], array('\'' => '')); |
||
1222 | $save_cache['enabled'] = true; |
||
1223 | } |
||
1224 | // Should we remove this one? |
||
1225 | elseif (isset($remove_strings[$entryKey]) && in_array($subKey, $remove_strings[$entryKey]) && $entryValue['can_remove']) |
||
1226 | $save_cache['enabled'] = true; |
||
1227 | // Just keep this one as it is |
||
1228 | else |
||
1229 | $save_cache['entries'][$subKey] = $subValue; |
||
1230 | |||
1231 | $context['file_entries'][$entryValue['group']][] = array( |
||
1232 | 'key' => $entryKey, |
||
1233 | 'subkey' => $subKey, |
||
1234 | 'value' => $subValue, |
||
1235 | 'rows' => 1, |
||
1236 | 'can_remove' => $entryValue['can_remove'], |
||
1237 | ); |
||
1238 | } |
||
1239 | |||
1240 | // Should we add a new string to this array? |
||
1241 | if (!empty($context['can_add_lang_entry'][$entryValue['type']]) && isset($add_strings[$entryKey])) |
||
1242 | { |
||
1243 | foreach ($add_strings[$entryKey] as $string_key => $string_val) |
||
1244 | $save_cache['entries'][$string_key] = strtr($string_val['string'], array('\'' => '')); |
||
1245 | |||
1246 | $save_cache['enabled'] = true; |
||
1247 | |||
1248 | // Make sure we don't add this again as an independent line |
||
1249 | unset($add_strings[$entryKey][$string_key]); |
||
1250 | if (empty($add_strings[$entryKey])) |
||
1251 | unset($add_strings[$entryKey]); |
||
1252 | } |
||
1253 | |||
1254 | // Do we need to save? |
||
1255 | if ($save_cache['enabled']) |
||
1256 | { |
||
1257 | // Format the string, checking the indexes first. |
||
1258 | $items = array(); |
||
1259 | $cur_index = 0; |
||
1260 | foreach ($save_cache['entries'] as $k2 => $v2) |
||
1261 | { |
||
1262 | // Manually show the custom index. |
||
1263 | if ($k2 != $cur_index) |
||
1264 | { |
||
1265 | $items[] = $k2 . ' => \'' . $v2 . '\''; |
||
1266 | $cur_index = $k2; |
||
1267 | } |
||
1268 | else |
||
1269 | $items[] = '\'' . $v2 . '\''; |
||
1270 | |||
1271 | $cur_index++; |
||
1272 | } |
||
1273 | // Now create the string! |
||
1274 | $final_saves[$entryKey] = array( |
||
1275 | 'find' => $entryValue['full'], |
||
1276 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n" . '$' . $entryValue['type'] . '[\'' . $entryKey . '\'] = array(' . implode(', ', $items) . ');' . $entryValue['cruft'], |
||
1277 | ); |
||
1278 | } |
||
1279 | } |
||
1280 | // A single array element, like: $txt['foo']['bar'] = 'baz'; |
||
1281 | elseif (isset($entryValue['subkey'])) |
||
1282 | { |
||
1283 | // Saving? |
||
1284 | if (isset($save_strings[$entryValue['key']][$entryValue['subkey']]) && $save_strings[$entryValue['key']][$entryValue['subkey']] != $entryValue['entry']) |
||
1285 | { |
||
1286 | if ($save_strings[$entryValue['key']][$entryValue['subkey']] == '') |
||
1287 | $save_strings[$entryValue['key']][$entryValue['subkey']] = '\'\''; |
||
1288 | |||
1289 | // Preserve subkey as either digit or string |
||
1290 | $subKey = ctype_digit($entryValue['subkey']) ? $entryValue['subkey'] : '\'' . $entryValue['subkey'] . '\''; |
||
1291 | |||
1292 | // We have a new value, so we should use it |
||
1293 | $entryValue['entry'] = $save_strings[$entryValue['key']][$entryValue['subkey']]; |
||
1294 | |||
1295 | // And save it |
||
1296 | $final_saves[$entryKey] = array( |
||
1297 | 'find' => $entryValue['full'], |
||
1298 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n" . '$' . $entryValue['type'] . '[\'' . $entryValue['key'] . '\'][' . $subKey . '] = ' . $save_strings[$entryValue['key']][$entryValue['subkey']] . ';' . $entryValue['cruft'], |
||
1299 | ); |
||
1300 | } |
||
1301 | |||
1302 | // Remove this entry only if it is allowed |
||
1303 | if (isset($remove_strings[$entryValue['key']]) && in_array($entryValue['subkey'], $remove_strings[$entryValue['key']]) && $entryValue['can_remove']) |
||
1304 | { |
||
1305 | $entryValue['entry'] = '\'\''; |
||
1306 | $final_saves[$entryKey] = array( |
||
1307 | 'find' => $entryValue['full'], |
||
1308 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n", |
||
1309 | ); |
||
1310 | } |
||
1311 | |||
1312 | $editing_string = cleanLangString($entryValue['entry'], true); |
||
1313 | $context['file_entries'][$entryValue['group']][] = array( |
||
1314 | 'key' => $entryValue['key'], |
||
1315 | 'subkey' => $entryValue['subkey'], |
||
1316 | 'value' => $editing_string, |
||
1317 | 'rows' => (int) (strlen($editing_string) / 38) + substr_count($editing_string, "\n") + 1, |
||
1318 | 'can_remove' => $entryValue['can_remove'], |
||
1319 | ); |
||
1320 | } |
||
1321 | // A simple string entry |
||
1322 | else |
||
1323 | { |
||
1324 | // Saving? |
||
1325 | if (isset($save_strings[$entryValue['key']]) && $save_strings[$entryValue['key']] != $entryValue['entry']) |
||
1326 | { |
||
1327 | // @todo Fix this properly. |
||
1328 | if ($save_strings[$entryValue['key']] == '') |
||
1329 | $save_strings[$entryValue['key']] = '\'\''; |
||
1330 | |||
1331 | // Set the new value. |
||
1332 | $entryValue['entry'] = $save_strings[$entryValue['key']]; |
||
1333 | // And we know what to save now! |
||
1334 | $final_saves[$entryKey] = array( |
||
1335 | 'find' => $entryValue['full'], |
||
1336 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n" . '$' . $entryValue['type'] . '[\'' . $entryValue['key'] . '\'] = ' . $save_strings[$entryValue['key']] . ';' . $entryValue['cruft'], |
||
1337 | ); |
||
1338 | } |
||
1339 | // Remove this entry only if it is allowed |
||
1340 | if (in_array($entryValue['key'], $remove_strings) && $entryValue['can_remove']) |
||
1341 | { |
||
1342 | $entryValue['entry'] = '\'\''; |
||
1343 | $final_saves[$entryKey] = array( |
||
1344 | 'find' => $entryValue['full'], |
||
1345 | 'replace' => '// ' . implode("\n// ", explode("\n", rtrim($entryValue['full'], "\n"))) . "\n", |
||
1346 | ); |
||
1347 | } |
||
1348 | |||
1349 | $editing_string = cleanLangString($entryValue['entry'], true); |
||
1350 | $context['file_entries'][$entryValue['group']][] = array( |
||
1351 | 'key' => $entryValue['key'], |
||
1352 | 'subkey' => null, |
||
1353 | 'value' => $editing_string, |
||
1354 | 'rows' => (int) (strlen($editing_string) / 38) + substr_count($editing_string, "\n") + 1, |
||
1355 | 'can_remove' => $entryValue['can_remove'], |
||
1356 | ); |
||
1357 | } |
||
1358 | } |
||
1359 | |||
1360 | // Do they want to add some brand new strings? Does this file allow that? |
||
1361 | if (!empty($add_strings) && !empty($allows_add_remove[$file_id]['add'])) |
||
1362 | { |
||
1363 | $special_types = isset($special_groups[$file_id]) ? array_flip($special_groups[$file_id]) : array(); |
||
1364 | |||
1365 | foreach ($add_strings as $string_key => $string_val) |
||
1366 | { |
||
1367 | // Adding a normal string |
||
1368 | if (isset($string_val['string']) && is_string($string_val['string'])) |
||
1369 | { |
||
1370 | $type = isset($special_types[$string_val['group']]) ? $special_types[$string_val['group']] : $string_val['group']; |
||
1371 | |||
1372 | if (empty($context['can_add_lang_entry'][$type])) |
||
1373 | continue; |
||
1374 | |||
1375 | $final_saves[$string_key] = array( |
||
1376 | 'find' => "\s*\?" . '>$', |
||
1377 | 'replace' => "\n\$" . $type . '[\'' . $string_key . '\'] = ' . $string_val['string'] . ';' . "\n\n?" . '>', |
||
1378 | 'is_regex' => true, |
||
1379 | ); |
||
1380 | } |
||
1381 | // Adding an array element |
||
1382 | else |
||
1383 | { |
||
1384 | foreach ($string_val as $substring_key => $substring_val) |
||
1385 | { |
||
1386 | $type = isset($special_types[$substring_val['group']]) ? $special_types[$substring_val['group']] : $substring_val['group']; |
||
1387 | |||
1388 | if (empty($context['can_add_lang_entry'][$type])) |
||
1389 | continue; |
||
1390 | |||
1391 | $subKey = ctype_digit(trim($substring_key, '\'')) ? trim($substring_key, '\'') : '\'' . $substring_key . '\''; |
||
1392 | |||
1393 | $final_saves[$string_key . '[' . $substring_key . ']'] = array( |
||
1394 | 'find' => "\s*\?" . '>$', |
||
1395 | 'replace' => "\n\$" . $type . '[\'' . $string_key . '\'][' . $subKey . '] = ' . $substring_val['string'] . ';' . "\n\n?" . '>', |
||
1396 | 'is_regex' => true, |
||
1397 | ); |
||
1398 | } |
||
1399 | } |
||
1400 | } |
||
1401 | } |
||
1402 | |||
1403 | // Any saves to make? |
||
1404 | if (!empty($final_saves)) |
||
1405 | { |
||
1406 | checkSession(); |
||
1407 | |||
1408 | // Get a fresh copy of the file's current content. |
||
1409 | $file_contents = file_get_contents($current_file); |
||
1410 | |||
1411 | // Apply our changes. |
||
1412 | foreach ($final_saves as $save) |
||
1413 | { |
||
1414 | if (!empty($save['is_regex'])) |
||
1415 | $file_contents = preg_replace('~' . $save['find'] . '~' . ($context['utf8'] ? 'u' : ''), $save['replace'], $file_contents); |
||
1416 | else |
||
1417 | $file_contents = str_replace($save['find'], $save['replace'], $file_contents); |
||
1418 | } |
||
1419 | |||
1420 | // Save the result back to the file. |
||
1421 | file_put_contents($current_file, $file_contents); |
||
1422 | |||
1423 | $madeSave = true; |
||
1424 | } |
||
1425 | |||
1426 | // Another restore. |
||
1427 | $txt = $old_txt; |
||
1428 | |||
1429 | // If they can add new language entries, make sure the UI is set up for that. |
||
1430 | if (!empty($context['can_add_lang_entry'])) |
||
1431 | { |
||
1432 | // Make sure the Add button has a place to show up. |
||
1433 | foreach ($context['can_add_lang_entry'] as $group => $value) |
||
1434 | { |
||
1435 | if (!isset($context['file_entries'][$group])) |
||
1436 | $context['file_entries'][$group] = array(); |
||
1437 | } |
||
1438 | |||
1439 | addInlineJavaScript(' |
||
1440 | function add_lang_entry(group) { |
||
1441 | var key = prompt("' . $txt['languages_enter_key'] . '"); |
||
1442 | |||
1443 | if (key !== null) { |
||
1444 | ++entry_num; |
||
1445 | |||
1446 | var array_regex = /^(.*)(\[[^\[\]]*\])$/ |
||
1447 | var result = array_regex.exec(key); |
||
1448 | if (result != null) { |
||
1449 | key = result[1]; |
||
1450 | var subkey = result[2]; |
||
1451 | } else { |
||
1452 | var subkey = ""; |
||
1453 | } |
||
1454 | |||
1455 | var bracket_regex = /[\[\]]/ |
||
1456 | if (bracket_regex.test(key)) { |
||
1457 | alert("' . $txt['languages_invalid_key'] . '" + key + subkey); |
||
1458 | return; |
||
1459 | } |
||
1460 | |||
1461 | $("#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>"); |
||
1462 | } |
||
1463 | };'); |
||
1464 | |||
1465 | addInlineJavaScript(' |
||
1466 | $(".add_lang_entry_button").show();', true); |
||
1467 | } |
||
1468 | |||
1469 | // Warn them if they try to submit more changes than the server can accept in a single request. |
||
1470 | // Also make it obvious that they cannot submit changes to both the primary settings and the entries at the same time. |
||
1471 | if (!empty($context['file_entries'])) |
||
1472 | { |
||
1473 | addInlineJavaScript(' |
||
1474 | max_inputs = ' . $context['max_inputs'] . '; |
||
1475 | num_inputs = 0; |
||
1476 | |||
1477 | $(".entry_textfield").prop("disabled", true); |
||
1478 | $(".entry_oldvalue").prop("disabled", true); |
||
1479 | |||
1480 | $(".entry_toggle").click(function() { |
||
1481 | var target_dd = $( $(this).data("target") ); |
||
1482 | |||
1483 | if ($(this).prop("checked") === true && $(this).val() === "edit") { |
||
1484 | if (++num_inputs <= max_inputs) { |
||
1485 | target_dd.find(".entry_oldvalue, .entry_textfield").prop("disabled", false); |
||
1486 | } else { |
||
1487 | alert("' . sprintf($txt['languages_max_inputs_warning'], $context['max_inputs']) . '"); |
||
1488 | $(this).prop("checked", false); |
||
1489 | } |
||
1490 | } else { |
||
1491 | --num_inputs; |
||
1492 | target_dd.find(".entry_oldvalue, .entry_textfield").prop("disabled", true); |
||
1493 | } |
||
1494 | |||
1495 | if (num_inputs > 0) { |
||
1496 | $("#primary_settings").trigger("reset"); |
||
1497 | $("#primary_settings input").prop("disabled", true); |
||
1498 | } else { |
||
1499 | $("#primary_settings input").prop("disabled", false); |
||
1500 | } |
||
1501 | }); |
||
1502 | |||
1503 | $("#primary_settings input").change(function() { |
||
1504 | num_changed = 0; |
||
1505 | $("#primary_settings input:text").each(function(i, e) { |
||
1506 | if ($(e).data("orig") != $(e).val()) |
||
1507 | num_changed++; |
||
1508 | }); |
||
1509 | $("#primary_settings input:checkbox").each(function(i, e) { |
||
1510 | cur_val = $(e).is(":checked"); |
||
1511 | orig_val = $(e).val == "true"; |
||
1512 | if (cur_val != orig_val) |
||
1513 | num_changed++; |
||
1514 | }); |
||
1515 | |||
1516 | if (num_changed > 0) { |
||
1517 | $("#entry_fields").fadeOut(); |
||
1518 | } else { |
||
1519 | $("#entry_fields").fadeIn(); |
||
1520 | } |
||
1521 | }); |
||
1522 | $("#reset_main").click(function() { |
||
1523 | $("#entry_fields").fadeIn(); |
||
1524 | });', true); |
||
1525 | } |
||
1526 | } |
||
1527 | |||
1528 | // If we saved, redirect. |
||
1529 | if ($madeSave) |
||
1530 | redirectexit('action=admin;area=languages;sa=editlang;lid=' . $context['lang_id'] . (!empty($file_id) ? ';entries;tfid=' . $theme_id . rawurlencode('+') . $file_id : '')); |
||
1531 | |||
1532 | createToken('admin-mlang'); |
||
1533 | } |
||
1534 | |||
1535 | /** |
||
1536 | * This function cleans language entries to/from display. |
||
1537 | * |
||
1538 | * @todo This function could be two functions? |
||
1539 | * |
||
1540 | * @param string $string The language string |
||
1541 | * @param bool $to_display Whether or not this is going to be displayed |
||
1542 | * @return string The cleaned string |
||
1543 | */ |
||
1544 | function cleanLangString($string, $to_display = true) |
||
1545 | { |
||
1546 | global $smcFunc; |
||
1547 | |||
1548 | // If going to display we make sure it doesn't have any HTML in it - etc. |
||
1549 | $new_string = ''; |
||
1550 | if ($to_display) |
||
1551 | { |
||
1552 | // Are we in a string (0 = no, 1 = single quote, 2 = parsed) |
||
1553 | $in_string = 0; |
||
1554 | $is_escape = false; |
||
1555 | for ($i = 0; $i < strlen($string); $i++) |
||
1556 | { |
||
1557 | // Handle escapes first. |
||
1558 | if ($string{$i} == '\\') |
||
1559 | { |
||
1560 | // Toggle the escape. |
||
1561 | $is_escape = !$is_escape; |
||
1562 | // If we're now escaped don't add this string. |
||
1563 | if ($is_escape) |
||
1564 | continue; |
||
1565 | } |
||
1566 | // Special case - parsed string with line break etc? |
||
1567 | elseif (($string{$i} == 'n' || $string{$i} == 't') && $in_string == 2 && $is_escape) |
||
1568 | { |
||
1569 | // Put the escape back... |
||
1570 | $new_string .= $string{$i} == 'n' ? "\n" : "\t"; |
||
1571 | $is_escape = false; |
||
1572 | continue; |
||
1573 | } |
||
1574 | // Have we got a single quote? |
||
1575 | elseif ($string{$i} == '\'') |
||
1576 | { |
||
1577 | // Already in a parsed string, or escaped in a linear string, means we print it - otherwise something special. |
||
1578 | if ($in_string != 2 && ($in_string != 1 || !$is_escape)) |
||
1579 | { |
||
1580 | // Is it the end of a single quote string? |
||
1581 | if ($in_string == 1) |
||
1582 | $in_string = 0; |
||
1583 | // Otherwise it's the start! |
||
1584 | else |
||
1585 | $in_string = 1; |
||
1586 | |||
1587 | // Don't actually include this character! |
||
1588 | continue; |
||
1589 | } |
||
1590 | } |
||
1591 | // Otherwise a double quote? |
||
1592 | elseif ($string{$i} == '"') |
||
1593 | { |
||
1594 | // Already in a single quote string, or escaped in a parsed string, means we print it - otherwise something special. |
||
1595 | if ($in_string != 1 && ($in_string != 2 || !$is_escape)) |
||
1596 | { |
||
1597 | // Is it the end of a double quote string? |
||
1598 | if ($in_string == 2) |
||
1599 | $in_string = 0; |
||
1600 | // Otherwise it's the start! |
||
1601 | else |
||
1602 | $in_string = 2; |
||
1603 | |||
1604 | // Don't actually include this character! |
||
1605 | continue; |
||
1606 | } |
||
1607 | } |
||
1608 | // A join/space outside of a string is simply removed. |
||
1609 | elseif ($in_string == 0 && (empty($string{$i}) || $string{$i} == '.')) |
||
1610 | continue; |
||
1611 | // Start of a variable? |
||
1612 | elseif ($in_string == 0 && $string{$i} == '$') |
||
1613 | { |
||
1614 | // Find the whole of it! |
||
1615 | preg_match('~([\$A-Za-z0-9\'\[\]_-]+)~', substr($string, $i), $matches); |
||
1616 | if (!empty($matches[1])) |
||
1617 | { |
||
1618 | // Come up with some pseudo thing to indicate this is a var. |
||
1619 | /** |
||
1620 | * @todo Do better than this, please! |
||
1621 | */ |
||
1622 | $new_string .= '{%' . $matches[1] . '%}'; |
||
1623 | |||
1624 | // We're not going to reparse this. |
||
1625 | $i += strlen($matches[1]) - 1; |
||
1626 | } |
||
1627 | |||
1628 | continue; |
||
1629 | } |
||
1630 | // Right, if we're outside of a string we have DANGER, DANGER! |
||
1631 | elseif ($in_string == 0) |
||
1632 | { |
||
1633 | continue; |
||
1634 | } |
||
1635 | |||
1636 | // Actually add the character to the string! |
||
1637 | $new_string .= $string{$i}; |
||
1638 | // If anything was escaped it ain't any longer! |
||
1639 | $is_escape = false; |
||
1640 | } |
||
1641 | |||
1642 | // Unhtml then rehtml the whole thing! |
||
1643 | $new_string = $smcFunc['htmlspecialchars'](un_htmlspecialchars($new_string)); |
||
1644 | } |
||
1645 | else |
||
1646 | { |
||
1647 | // Keep track of what we're doing... |
||
1648 | $in_string = 0; |
||
1649 | // This is for deciding whether to HTML a quote. |
||
1650 | $in_html = false; |
||
1651 | for ($i = 0; $i < strlen($string); $i++) |
||
1652 | { |
||
1653 | // We don't do parsed strings apart from for breaks. |
||
1654 | if ($in_string == 2) |
||
1655 | { |
||
1656 | $in_string = 0; |
||
1657 | $new_string .= '"'; |
||
1658 | } |
||
1659 | |||
1660 | // Not in a string yet? |
||
1661 | if ($in_string != 1) |
||
1662 | { |
||
1663 | $in_string = 1; |
||
1664 | $new_string .= ($new_string ? ' . ' : '') . '\''; |
||
1665 | } |
||
1666 | |||
1667 | // Is this a variable? |
||
1668 | if ($string{$i} == '{' && $string{$i + 1} == '%' && $string{$i + 2} == '$') |
||
1669 | { |
||
1670 | // Grab the variable. |
||
1671 | preg_match('~\{%([\$A-Za-z0-9\'\[\]_-]+)%\}~', substr($string, $i), $matches); |
||
1672 | if (!empty($matches[1])) |
||
1673 | { |
||
1674 | if ($in_string == 1) |
||
1675 | $new_string .= '\' . '; |
||
1676 | elseif ($new_string) |
||
1677 | $new_string .= ' . '; |
||
1678 | |||
1679 | $new_string .= $matches[1]; |
||
1680 | $i += strlen($matches[1]) + 3; |
||
1681 | $in_string = 0; |
||
1682 | } |
||
1683 | |||
1684 | continue; |
||
1685 | } |
||
1686 | // Is this a lt sign? |
||
1687 | elseif ($string{$i} == '<') |
||
1688 | { |
||
1689 | // Probably HTML? |
||
1690 | if ($string{$i + 1} != ' ') |
||
1691 | $in_html = true; |
||
1692 | // Assume we need an entity... |
||
1693 | else |
||
1694 | { |
||
1695 | $new_string .= '<'; |
||
1696 | continue; |
||
1697 | } |
||
1698 | } |
||
1699 | // What about gt? |
||
1700 | elseif ($string{$i} == '>') |
||
1701 | { |
||
1702 | // Will it be HTML? |
||
1703 | if ($in_html) |
||
1704 | $in_html = false; |
||
1705 | // Otherwise we need an entity... |
||
1706 | else |
||
1707 | { |
||
1708 | $new_string .= '>'; |
||
1709 | continue; |
||
1710 | } |
||
1711 | } |
||
1712 | // Is it a slash? If so escape it... |
||
1713 | if ($string{$i} == '\\') |
||
1714 | $new_string .= '\\'; |
||
1715 | // The infamous double quote? |
||
1716 | elseif ($string{$i} == '"') |
||
1717 | { |
||
1718 | // If we're in HTML we leave it as a quote - otherwise we entity it. |
||
1719 | if (!$in_html) |
||
1720 | { |
||
1721 | $new_string .= '"'; |
||
1722 | continue; |
||
1723 | } |
||
1724 | } |
||
1725 | // A single quote? |
||
1726 | elseif ($string{$i} == '\'') |
||
1727 | { |
||
1728 | // Must be in a string so escape it. |
||
1729 | $new_string .= '\\'; |
||
1730 | } |
||
1731 | |||
1732 | // Finally add the character to the string! |
||
1733 | $new_string .= $string{$i}; |
||
1734 | } |
||
1735 | |||
1736 | // If we ended as a string then close it off. |
||
1737 | if ($in_string == 1) |
||
1738 | $new_string .= '\''; |
||
1739 | elseif ($in_string == 2) |
||
1740 | $new_string .= '"'; |
||
1741 | } |
||
1742 | |||
1743 | return $new_string; |
||
1744 | } |
||
1745 | |||
1746 | ?> |