ModifyLanguage()   F
last analyzed

Complexity

Conditions 138
Paths > 20000

Size

Total Lines 747
Code Lines 328

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 138
eloc 328
c 0
b 0
f 0
nc 536594688
nop 0
dl 0
loc 747
rs 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 2022 Simple Machines and individual contributors
11
 * @license https://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1.0
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
It seems like fetch_web_data($url) can also be of type false; however, parameter $data of xmlArray::__construct() does only seem to accept string, 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 ignore-type  annotation

151
	$language_list = new xmlArray(/** @scrutinizer ignore-type */ fetch_web_data($url), true);
Loading history...
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
The variable $archive_content seems to never exist and therefore isset should always be false.
Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The import $txt is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

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