Issues (1014)

Sources/ManageSmileys.php (1 issue)

1
<?php
2
3
/**
4
 * This file takes care of all administration of smileys.
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 dispatcher of smileys administration.
21
 */
22
function ManageSmileys()
23
{
24
	global $context, $txt, $modSettings;
25
26
	isAllowedTo('manage_smileys');
27
28
	loadLanguage('ManageSmileys');
29
	loadTemplate('ManageSmileys');
30
31
	$subActions = array(
32
		'addsmiley' => 'AddSmiley',
33
		'editicon' => 'EditMessageIcons',
34
		'editicons' => 'EditMessageIcons',
35
		'editsets' => 'EditSmileySets',
36
		'editsmileys' => 'EditSmileys',
37
		'import' => 'EditSmileySets',
38
		'modifyset' => 'EditSmileySets',
39
		'modifysmiley' => 'EditSmileys',
40
		'setorder' => 'EditSmileyOrder',
41
		'settings' => 'EditSmileySettings',
42
		'install' => 'InstallSmileySet'
43
	);
44
45
	// If customized smileys is disabled don't show the setting page
46
	if (empty($modSettings['smiley_enable']))
47
	{
48
		unset($subActions['addsmiley']);
49
		unset($subActions['editsmileys']);
50
		unset($subActions['setorder']);
51
		unset($subActions['modifysmiley']);
52
	}
53
	if (empty($modSettings['messageIcons_enable']))
54
	{
55
		unset($subActions['editicon']);
56
		unset($subActions['editicons']);
57
	}
58
59
	// Default the sub-action to 'edit smiley settings'.
60
	$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'editsets';
61
62
	$context['page_title'] = $txt['smileys_manage'];
63
	$context['sub_action'] = $_REQUEST['sa'];
64
	$context['sub_template'] = $context['sub_action'];
65
66
	// Load up all the tabs...
67
	$context[$context['admin_menu_name']]['tab_data'] = array(
68
		'title' => $txt['smileys_manage'],
69
		'help' => 'smileys',
70
		'description' => $txt['smiley_settings_explain'],
71
		'tabs' => array(
72
			'editsets' => array(
73
				'description' => $txt['smiley_editsets_explain'],
74
			),
75
			'addsmiley' => array(
76
				'description' => $txt['smiley_addsmiley_explain'],
77
			),
78
			'editsmileys' => array(
79
				'description' => $txt['smiley_editsmileys_explain'],
80
			),
81
			'setorder' => array(
82
				'description' => $txt['smiley_setorder_explain'],
83
			),
84
			'editicons' => array(
85
				'description' => $txt['icons_edit_icons_explain'],
86
			),
87
			'settings' => array(
88
				'description' => $txt['smiley_settings_explain'],
89
			),
90
		),
91
	);
92
93
	// Some settings may not be enabled, disallow these from the tabs as appropriate.
94
	if (empty($modSettings['messageIcons_enable']))
95
		$context[$context['admin_menu_name']]['tab_data']['tabs']['editicons']['disabled'] = true;
96
	if (empty($modSettings['smiley_enable']))
97
	{
98
		$context[$context['admin_menu_name']]['tab_data']['tabs']['addsmiley']['disabled'] = true;
99
		$context[$context['admin_menu_name']]['tab_data']['tabs']['editsmileys']['disabled'] = true;
100
		$context[$context['admin_menu_name']]['tab_data']['tabs']['setorder']['disabled'] = true;
101
	}
102
103
	call_integration_hook('integrate_manage_smileys', array(&$subActions));
104
105
	// Call the right function for this sub-action.
106
	call_helper($subActions[$_REQUEST['sa']]);
107
}
108
109
/**
110
 * Handles modifying smileys settings.
111
 *
112
 * @param bool $return_config Whether or not to return the config_vars array (used for admin search)
113
 * @return void|array Returns nothing or returns the $config_vars array if $return_config is true
114
 */
115
function EditSmileySettings($return_config = false)
116
{
117
	global $modSettings, $context, $txt, $boarddir, $sourcedir, $scripturl;
118
119
	// The directories...
120
	$context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
121
	$context['smileys_dir_found'] = is_dir($context['smileys_dir']);
122
123
	// Get the names of the smiley sets.
124
	$smiley_sets = explode(',', $modSettings['smiley_sets_known']);
125
	$set_names = explode("\n", $modSettings['smiley_sets_names']);
126
127
	$smiley_context = array();
128
	foreach ($smiley_sets as $i => $set)
129
		$smiley_context[$set] = $set_names[$i];
130
131
	// All the settings for the page...
132
	$config_vars = array(
133
		array('title', 'settings'),
134
		// Inline permissions.
135
		array('permissions', 'manage_smileys'),
136
		'',
137
138
		array('select', 'smiley_sets_default', $smiley_context),
139
		array('check', 'smiley_sets_enable'),
140
		array('check', 'smiley_enable', 'subtext' => $txt['smileys_enable_note']),
141
		array('text', 'smileys_url', 40),
142
		array('warning', !is_dir($context['smileys_dir']) ? 'setting_smileys_dir_wrong' : ''),
143
		array('text', 'smileys_dir', 'invalid' => !$context['smileys_dir_found'], 40),
144
		'',
145
146
		// Message icons.
147
		array('check', 'messageIcons_enable', 'subtext' => $txt['setting_messageIcons_enable_note']),
148
		array('check', 'messageIconChecks_enable', 'subtext' => $txt['setting_messageIconChecks_enable_note'])
149
	);
150
151
	call_integration_hook('integrate_modify_smiley_settings', array(&$config_vars));
152
153
	if ($return_config)
154
		return $config_vars;
155
156
	// Setup the basics of the settings template.
157
	require_once($sourcedir . '/ManageServer.php');
158
	$context['sub_template'] = 'show_settings';
159
160
	// Finish up the form...
161
	$context['post_url'] = $scripturl . '?action=admin;area=smileys;save;sa=settings';
162
163
	// Saving the settings?
164
	if (isset($_GET['save']))
165
	{
166
		checkSession();
167
168
		// Validate the smiley set name.
169
		$_POST['smiley_sets_default'] = empty($smiley_context[$_POST['smiley_sets_default']]) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_default'];
170
171
		call_integration_hook('integrate_save_smiley_settings');
172
173
		saveDBSettings($config_vars);
174
		$_SESSION['adm-save'] = true;
175
176
		foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set)
177
		{
178
			cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
179
			cache_put_data('posting_smileys_' . $smiley_set, null, 480);
180
		}
181
182
		redirectexit('action=admin;area=smileys;sa=settings');
183
	}
184
185
	// We need this for the in-line permissions
186
	createToken('admin-mp');
187
188
	prepareDBSettingContext($config_vars);
189
}
190
191
/**
192
 * List, add, remove, modify smileys sets.
193
 */
194
function EditSmileySets()
195
{
196
	global $modSettings, $context, $txt;
197
	global $smcFunc, $scripturl, $sourcedir;
198
199
	// Set the right tab to be selected.
200
	$context[$context['admin_menu_name']]['current_subsection'] = 'editsets';
201
202
	$allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg');
203
204
	// They must've been submitted a form.
205
	if (isset($_POST['smiley_save']))
206
	{
207
		checkSession();
208
		validateToken('admin-mss', 'request');
209
210
		// Delete selected smiley sets.
211
		if (!empty($_POST['delete']) && !empty($_POST['smiley_set']))
212
		{
213
			$set_paths = explode(',', $modSettings['smiley_sets_known']);
214
			$set_names = explode("\n", $modSettings['smiley_sets_names']);
215
			foreach ($_POST['smiley_set'] as $id => $val)
216
			{
217
				// If this is the set you've marked as default, or the only one remaining, you can't delete it
218
				if ($modSettings['smiley_sets_default'] != $set_paths[$id] && count($set_paths) != 1 && isset($set_paths[$id], $set_names[$id]))
219
				{
220
					// Delete this set's entries from the smiley_files table
221
					$smcFunc['db_query']('', '
222
						DELETE FROM {db_prefix}smiley_files
223
						WHERE smiley_set = {string:smiley_set}',
224
						array(
225
							'smiley_set' => $set_paths[$id],
226
						)
227
					);
228
229
					// Remove this set from our lists
230
					unset($set_paths[$id], $set_names[$id]);
231
				}
232
			}
233
234
			// Shortcut... array_merge() on a single array resets the numeric keys
235
			$set_paths = array_merge($set_paths);
236
			$set_names = array_merge($set_names);
237
238
			updateSettings(array(
239
				'smiley_sets_known' => implode(',', $set_paths),
240
				'smiley_sets_names' => implode("\n", $set_names),
241
				'smiley_sets_default' => in_array($modSettings['smiley_sets_default'], $set_paths) ? $modSettings['smiley_sets_default'] : $set_paths[0],
242
			));
243
		}
244
		// Add a new smiley set.
245
		elseif (!empty($_POST['add']))
246
			$context['sub_action'] = 'modifyset';
247
		// Create or modify a smiley set.
248
		elseif (isset($_POST['set']))
249
		{
250
			$set_paths = explode(',', $modSettings['smiley_sets_known']);
251
			$set_names = explode("\n", $modSettings['smiley_sets_names']);
252
253
			foreach (array('smiley_sets_path', 'smiley_sets_name') as $key)
254
				$_POST[$key] = $smcFunc['normalize']($_POST[$key]);
255
256
			// Create a new smiley set.
257
			if ($_POST['set'] == -1 && isset($_POST['smiley_sets_path']))
258
			{
259
				if (in_array($_POST['smiley_sets_path'], $set_paths))
260
					fatal_lang_error('smiley_set_already_exists', false);
261
262
				updateSettings(array(
263
					'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . $_POST['smiley_sets_path'],
264
					'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . $_POST['smiley_sets_name'],
265
					'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path'],
266
				));
267
			}
268
			// Modify an existing smiley set.
269
			else
270
			{
271
				// Make sure the smiley set exists.
272
				if (!isset($set_paths[$_POST['set']]) || !isset($set_names[$_POST['set']]))
273
					fatal_lang_error('smiley_set_not_found', false);
274
275
				// Make sure the path is not yet used by another smileyset.
276
				if (in_array($_POST['smiley_sets_path'], $set_paths) && $_POST['smiley_sets_path'] != $set_paths[$_POST['set']])
277
					fatal_lang_error('smiley_set_path_already_used', false);
278
279
				$set_paths[$_POST['set']] = $_POST['smiley_sets_path'];
280
				$set_names[$_POST['set']] = $_POST['smiley_sets_name'];
281
				updateSettings(array(
282
					'smiley_sets_known' => implode(',', $set_paths),
283
					'smiley_sets_names' => implode("\n", $set_names),
284
					'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path']
285
				));
286
			}
287
288
			// Import, but only the ones that match existing smileys
289
			ImportSmileys($_POST['smiley_sets_path'], false);
290
		}
291
292
		foreach ($set_paths as $smiley_set)
293
		{
294
			cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
295
			cache_put_data('posting_smileys_' . $smiley_set, null, 480);
296
		}
297
	}
298
299
	// Load all available smileysets...
300
	$context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
301
	$set_names = explode("\n", $modSettings['smiley_sets_names']);
302
	foreach ($context['smiley_sets'] as $i => $set)
303
		$context['smiley_sets'][$i] = array(
304
			'id' => $i,
305
			'raw_path' => $set,
306
			'path' => $smcFunc['htmlspecialchars']($set),
307
			'name' => $smcFunc['htmlspecialchars']($set_names[$i]),
308
			'selected' => $set == $modSettings['smiley_sets_default']
309
		);
310
311
	// Importing any smileys from an existing set?
312
	if ($context['sub_action'] == 'import')
313
	{
314
		checkSession('get');
315
		validateToken('admin-mss', 'request');
316
317
		$_GET['set'] = (int) $_GET['set'];
318
319
		// Sanity check - then import.
320
		if (isset($context['smiley_sets'][$_GET['set']]))
321
			ImportSmileys(un_htmlspecialchars($context['smiley_sets'][$_GET['set']]['path']), true);
322
323
		// Force the process to continue.
324
		$context['sub_action'] = 'modifyset';
325
		$context['sub_template'] = 'modifyset';
326
	}
327
	// If we're modifying or adding a smileyset, some context info needs to be set.
328
	if ($context['sub_action'] == 'modifyset')
329
	{
330
		$_GET['set'] = !isset($_GET['set']) ? -1 : (int) $_GET['set'];
331
		if ($_GET['set'] == -1 || !isset($context['smiley_sets'][$_GET['set']]))
332
			$context['current_set'] = array(
333
				'id' => '-1',
334
				'raw_path' => '',
335
				'path' => '',
336
				'name' => '',
337
				'selected' => false,
338
				'is_new' => true,
339
			);
340
		else
341
		{
342
			$context['current_set'] = &$context['smiley_sets'][$_GET['set']];
343
			$context['current_set']['is_new'] = false;
344
345
			// Calculate whether there are any smileys in the directory that can be imported.
346
			if (!empty($modSettings['smiley_enable']) && !empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']))
347
			{
348
				$smileys = array();
349
				$dir = dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']);
350
				while ($entry = $dir->read())
351
				{
352
					$pathinfo = pathinfo($entry);
353
					if (empty($pathinfo['filename']) || empty($pathinfo['extension']))
354
						continue;
355
					if (in_array($pathinfo['extension'], $allowedTypes) && $pathinfo['filename'] != 'blank')
356
						$smileys[strtolower($entry)] = $entry;
357
				}
358
				$dir->close();
359
360
				if (empty($smileys))
361
					fatal_lang_error('smiley_set_dir_not_found', false, array($context['current_set']['name']));
362
363
				// Exclude the smileys that are already in the database.
364
				$request = $smcFunc['db_query']('', '
365
					SELECT filename
366
					FROM {db_prefix}smiley_files
367
					WHERE filename IN ({array_string:smiley_list})
368
						AND smiley_set = {string:smiley_set}',
369
					array(
370
						'smiley_list' => $smileys,
371
						'smiley_set' => $context['current_set']['path'],
372
					)
373
				);
374
				while ($row = $smcFunc['db_fetch_assoc']($request))
375
				{
376
					if (isset($smileys[strtolower($row['filename'])]))
377
						unset($smileys[strtolower($row['filename'])]);
378
				}
379
380
				$smcFunc['db_free_result']($request);
381
382
				$context['current_set']['can_import'] = count($smileys);
383
				$context['current_set']['import_url'] = $scripturl . '?action=admin;area=smileys;sa=import;set=' . $context['current_set']['id'] . ';' . $context['session_var'] . '=' . $context['session_id'];
384
			}
385
		}
386
387
		// Retrieve all potential smiley set directories.
388
		$context['smiley_set_dirs'] = array();
389
		if (!empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir']))
390
		{
391
			$dir = dir($modSettings['smileys_dir']);
392
			while ($entry = $dir->read())
393
			{
394
				if (!in_array($entry, array('.', '..')) && is_dir($modSettings['smileys_dir'] . '/' . $entry))
395
					$context['smiley_set_dirs'][] = array(
396
						'id' => $entry,
397
						'path' => $modSettings['smileys_dir'] . '/' . $entry,
398
						'selectable' => $entry == $context['current_set']['path'] || !in_array($entry, explode(',', $modSettings['smiley_sets_known'])),
399
						'current' => $entry == $context['current_set']['path'],
400
					);
401
			}
402
			$dir->close();
403
		}
404
	}
405
406
	// This is our save haven.
407
	createToken('admin-mss', 'request');
408
409
	// In case we need to import smileys, we need to add the token in now.
410
	if (isset($context['current_set']['import_url']))
411
	{
412
		$context['current_set']['import_url'] .= ';' . $context['admin-mss_token_var'] . '=' . $context['admin-mss_token'];
413
		$context['smiley_set_unused_message'] = sprintf($txt['smiley_set_unused'], $scripturl . '?action=admin;area=smileys;sa=editsmileys', $scripturl . '?action=admin;area=smileys;sa=addsmiley', $context['current_set']['import_url']);
414
	}
415
416
	$listOptions = array(
417
		'id' => 'smiley_set_list',
418
		'title' => $txt['smiley_sets'],
419
		'no_items_label' => $txt['smiley_sets_none'],
420
		'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsets',
421
		'default_sort_col' => 'name',
422
		'get_items' => array(
423
			'function' => 'list_getSmileySets',
424
		),
425
		'get_count' => array(
426
			'function' => 'list_getNumSmileySets',
427
		),
428
		'columns' => array(
429
			'default' => array(
430
				'header' => array(
431
					'value' => $txt['smiley_sets_default'],
432
					'class' => 'centercol',
433
				),
434
				'data' => array(
435
					'function' => function($rowData)
436
					{
437
						return $rowData['selected'] ? '<span class="main_icons valid"></span>' : '';
438
					},
439
					'class' => 'centercol',
440
				),
441
				'sort' => array(
442
					'default' => 'selected',
443
					'reverse' => 'selected DESC',
444
				),
445
			),
446
			'name' => array(
447
				'header' => array(
448
					'value' => $txt['smiley_sets_name'],
449
				),
450
				'data' => array(
451
					'db_htmlsafe' => 'name',
452
				),
453
				'sort' => array(
454
					'default' => 'name',
455
					'reverse' => 'name DESC',
456
				),
457
			),
458
			'url' => array(
459
				'header' => array(
460
					'value' => $txt['smiley_sets_url'],
461
				),
462
				'data' => array(
463
					'sprintf' => array(
464
						'format' => $modSettings['smileys_url'] . '/<strong>%1$s</strong>/...',
465
						'params' => array(
466
							'path' => true,
467
						),
468
					),
469
				),
470
				'sort' => array(
471
					'default' => 'path',
472
					'reverse' => 'path DESC',
473
				),
474
			),
475
			'modify' => array(
476
				'header' => array(
477
					'value' => $txt['smiley_set_modify'],
478
					'class' => 'centercol',
479
				),
480
				'data' => array(
481
					'sprintf' => array(
482
						'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset;set=%1$d">' . $txt['smiley_set_modify'] . '</a>',
483
						'params' => array(
484
							'id' => true,
485
						),
486
					),
487
					'class' => 'centercol',
488
				),
489
			),
490
			'check' => array(
491
				'header' => array(
492
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
493
					'class' => 'centercol',
494
				),
495
				'data' => array(
496
					'function' => function($rowData)
497
					{
498
						return $rowData['selected'] ? '' : sprintf('<input type="checkbox" name="smiley_set[%1$d]">', $rowData['id']);
499
					},
500
					'class' => 'centercol',
501
				),
502
			),
503
		),
504
		'form' => array(
505
			'href' => $scripturl . '?action=admin;area=smileys;sa=editsets',
506
			'token' => 'admin-mss',
507
		),
508
		'additional_rows' => array(
509
			array(
510
				'position' => 'above_table_headers',
511
				'value' => '<input type="hidden" name="smiley_save"><input type="submit" name="delete" value="' . $txt['smiley_sets_delete'] . '" data-confirm="' . $txt['smiley_sets_confirm'] . '" class="button you_sure"> <a class="button" href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset' . '">' . $txt['smiley_sets_add'] . '</a> ',
512
			),
513
			array(
514
				'position' => 'below_table_data',
515
				'value' => '<input type="hidden" name="smiley_save"><input type="submit" name="delete" value="' . $txt['smiley_sets_delete'] . '" data-confirm="' . $txt['smiley_sets_confirm'] . '" class="button you_sure"> <a class="button" href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset' . '">' . $txt['smiley_sets_add'] . '</a> ',
516
			),
517
		),
518
	);
519
520
	require_once($sourcedir . '/Subs-List.php');
521
	createList($listOptions);
522
}
523
524
/**
525
 * Callback function for createList().
526
 *
527
 * @todo to be moved to Subs-Smileys?
528
 *
529
 * @param int $start The item to start with (not used here)
530
 * @param int $items_per_page The number of items to show per page (not used here)
531
 * @param string $sort A string indicating how to sort the results
532
 * @return array An array of info about the smiley sets
533
 */
534
function list_getSmileySets($start, $items_per_page, $sort)
535
{
536
	global $modSettings;
537
538
	$known_sets = explode(',', $modSettings['smiley_sets_known']);
539
	$set_names = explode("\n", $modSettings['smiley_sets_names']);
540
	$cols = array(
541
		'id' => array(),
542
		'selected' => array(),
543
		'path' => array(),
544
		'name' => array(),
545
	);
546
	foreach ($known_sets as $i => $set)
547
	{
548
		$cols['id'][] = $i;
549
		$cols['selected'][] = $set == $modSettings['smiley_sets_default'];
550
		$cols['path'][] = $set;
551
		$cols['name'][] = $set_names[$i];
552
	}
553
	$sort_flag = strpos($sort, 'DESC') === false ? SORT_ASC : SORT_DESC;
554
	if (substr($sort, 0, 4) === 'name')
555
		array_multisort($cols['name'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['selected'], $cols['id']);
556
	elseif (substr($sort, 0, 4) === 'path')
557
		array_multisort($cols['path'], $sort_flag, SORT_REGULAR, $cols['name'], $cols['selected'], $cols['id']);
558
	else
559
		array_multisort($cols['selected'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['name'], $cols['id']);
560
561
	$smiley_sets = array();
562
	foreach ($cols['id'] as $i => $id)
563
		$smiley_sets[] = array(
564
			'id' => $id,
565
			'path' => $cols['path'][$i],
566
			'name' => $cols['name'][$i],
567
			'selected' => $cols['selected'][$i],
568
		);
569
570
	return $smiley_sets;
571
}
572
573
/**
574
 * Callback function for createList().
575
 *
576
 * @todo to be moved to Subs-Smileys?
577
 * @return int The total number of known smiley sets
578
 */
579
function list_getNumSmileySets()
580
{
581
	global $modSettings;
582
583
	return count(explode(',', $modSettings['smiley_sets_known']));
584
}
585
586
/**
587
 * Add a smiley, that's right.
588
 */
589
function AddSmiley()
590
{
591
	global $modSettings, $context, $txt, $boarddir, $smcFunc;
592
593
	// Get a list of all known smiley sets.
594
	$context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
595
	$context['smileys_dir_found'] = is_dir($context['smileys_dir']);
596
	$context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
597
	$set_names = explode("\n", $modSettings['smiley_sets_names']);
598
	foreach ($context['smiley_sets'] as $i => $set)
599
		$context['smiley_sets'][$i] = array(
600
			'id' => $i,
601
			'raw_path' => $set,
602
			'path' => $smcFunc['htmlspecialchars']($set),
603
			'name' => $smcFunc['htmlspecialchars']($set_names[$i]),
604
			'selected' => $set == $modSettings['smiley_sets_default']
605
		);
606
607
	// Some useful arrays... types we allow - and ports we don't!
608
	$allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg');
609
	$disabledFiles = array('con', 'com1', 'com2', 'com3', 'com4', 'prn', 'aux', 'lpt1', '.htaccess', 'index.php');
610
611
	// This will hold the names of the added files for each set
612
	$filename_array = array();
613
614
	// Submitting a form?
615
	if (isset($_POST[$context['session_var']], $_POST['smiley_code']))
616
	{
617
		checkSession();
618
619
		foreach (array('smiley_code', 'smiley_filename', 'smiley_description') as $key)
620
			$_POST[$key] = $smcFunc['normalize']($_POST[$key]);
621
622
		$_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
623
		$_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
624
		$_POST['smiley_filename'] = htmltrim__recursive($_POST['smiley_filename']);
625
626
		// Make sure some code was entered.
627
		if (empty($_POST['smiley_code']))
628
			fatal_lang_error('smiley_has_no_code', false);
629
630
		// Check whether the new code has duplicates. It should be unique.
631
		$request = $smcFunc['db_query']('', '
632
			SELECT id_smiley
633
			FROM {db_prefix}smileys
634
			WHERE code = {raw:mysql_binary_statement} {string:smiley_code}',
635
			array(
636
				'mysql_binary_statement' => $smcFunc['db_title'] == MYSQL_TITLE ? 'BINARY' : '',
637
				'smiley_code' => $_POST['smiley_code'],
638
			)
639
		);
640
		if ($smcFunc['db_num_rows']($request) > 0)
641
			fatal_lang_error('smiley_not_unique', false);
642
		$smcFunc['db_free_result']($request);
643
644
		// If we are uploading - check all the smiley sets are writable!
645
		if ($_POST['method'] != 'existing')
646
		{
647
			$writeErrors = array();
648
			foreach ($context['smiley_sets'] as $set)
649
			{
650
				if (!is_writable($context['smileys_dir'] . '/' . $set['raw_path']))
651
					$writeErrors[] = $set['path'];
652
			}
653
			if (!empty($writeErrors))
654
				fatal_lang_error('smileys_upload_error_notwritable', false, array(implode(', ', $writeErrors)));
655
		}
656
657
		// Uploading just one smiley for all of them?
658
		if (isset($_POST['sameall']) && isset($_FILES['uploadSmiley']['name']) && $_FILES['uploadSmiley']['name'] != '')
659
		{
660
			if (!is_uploaded_file($_FILES['uploadSmiley']['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['uploadSmiley']['tmp_name'])))
661
				fatal_lang_error('smileys_upload_error', false);
662
663
			// Sorry, no spaces, dots, or anything else but letters allowed.
664
			$_FILES['uploadSmiley']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['uploadSmiley']['name']);
665
666
			// We only allow image files - it's THAT simple - no messing around here...
667
			if (!in_array(strtolower(pathinfo($_FILES['uploadSmiley']['name'], PATHINFO_EXTENSION)), $allowedTypes))
668
				fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
669
670
			// We only need the filename...
671
			$destName = basename($_FILES['uploadSmiley']['name']);
672
673
			// Make sure they aren't trying to upload a nasty file - for their own good here!
674
			if (in_array(strtolower($destName), $disabledFiles))
675
				fatal_lang_error('smileys_upload_error_illegal', false);
676
677
			// Check if the file already exists... and if not move it to EVERY smiley set directory.
678
			$smileyLocation = null;
679
			foreach ($context['smiley_sets'] as $i => $set)
680
			{
681
				// Okay, we're going to put the smiley right here, since it's not there yet!
682
				if (!file_exists($context['smileys_dir'] . '/' . $context['smiley_sets'][$i]['raw_path']) . '/' . $destName)
683
				{
684
					$smileyLocation = $context['smileys_dir'] . '/' . $context['smiley_sets'][$i]['raw_path'] . '/' . $destName;
685
					move_uploaded_file($_FILES['uploadSmiley']['tmp_name'], $smileyLocation);
686
					smf_chmod($smileyLocation, 0644);
687
					break;
688
				}
689
			}
690
691
			// Now, we want to move it from there to all the other sets.
692
			foreach ($context['smiley_sets'] as $j => $set)
693
			{
694
				$currentPath = $context['smileys_dir'] . '/' . $context['smiley_sets'][$j]['raw_path'] . '/' . $destName;
695
696
				// Copy the first one we made to here, unless it already exists there
697
				if (!empty($smileyLocation) && !file_exists($currentPath))
698
				{
699
					copy($smileyLocation, $currentPath);
700
					smf_chmod($currentPath, 0644);
701
				}
702
703
				// Double-check
704
				if (!file_exists($currentPath))
705
					fatal_lang_error('smiley_not_found', false);
706
707
				// Finally make sure it's saved correctly!
708
				$filename_array[$context['smiley_sets'][$j]['raw_path']] = $destName;
709
			}
710
		}
711
		// What about uploading several files?
712
		elseif ($_POST['method'] != 'existing')
713
		{
714
			$newName = '';
715
			foreach ($_FILES as $name => $data)
716
			{
717
				if ($_FILES[$name]['name'] == '')
718
					fatal_lang_error('smileys_upload_error_blank', false);
719
720
				// if (empty($newName))
721
				// 	$newName = basename($_FILES[$name]['name']);
722
				// elseif (basename($_FILES[$name]['name']) != $newName)
723
				// 	fatal_lang_error('smileys_upload_error_name', false);
724
			}
725
726
			foreach ($context['smiley_sets'] as $i => $set)
727
			{
728
				$set['name'] = un_htmlspecialchars($set['name']);
729
730
				if (!isset($_FILES['individual_' . $set['raw_path']]['name']) || $_FILES['individual_' . $set['raw_path']]['name'] == '')
731
					continue;
732
733
				// Got one...
734
				if (!is_uploaded_file($_FILES['individual_' . $set['raw_path']]['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['individual_' . $set['raw_path']]['tmp_name'])))
735
					fatal_lang_error('smileys_upload_error', false);
736
737
				// Sorry, no spaces, dots, or anything else but letters allowed.
738
				$_FILES['individual_' . $set['raw_path']]['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['individual_' . $set['raw_path']]['name']);
739
740
				// We only allow image files - it's THAT simple - no messing around here...
741
				if (!in_array(strtolower(pathinfo($_FILES['individual_' . $set['raw_path']]['name'], PATHINFO_EXTENSION)), $allowedTypes))
742
					fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
743
744
				// We only need the filename...
745
				$destName = basename($_FILES['individual_' . $set['raw_path']]['name']);
746
747
				// Make sure they aren't trying to upload a nasty file - for their own good here!
748
				if (in_array(strtolower($destName), $disabledFiles))
749
					fatal_lang_error('smileys_upload_error_illegal', false);
750
751
				// If the file exists - ignore it.
752
				$smileyLocation = $context['smileys_dir'] . '/' . $set['raw_path'] . '/' . $destName;
753
				if (!file_exists($smileyLocation))
754
				{
755
					// Finally - move the image!
756
					move_uploaded_file($_FILES['individual_' . $set['raw_path']]['tmp_name'], $smileyLocation);
757
					smf_chmod($smileyLocation, 0644);
758
				}
759
760
				// Double-check
761
				if (!file_exists($smileyLocation))
762
					fatal_lang_error('smiley_not_found', false);
763
764
				// Should always be saved correctly!
765
				$filename_array[$set['raw_path']] = $destName;
766
			}
767
		}
768
		// Re-using an existing image
769
		else
770
		{
771
			// Make sure a filename was given
772
			if (empty($_POST['smiley_filename']))
773
				fatal_lang_error('smiley_has_no_filename', false);
774
775
			// And make sure it is legitimate
776
			$pathinfo = pathinfo($_POST['smiley_filename']);
777
778
			if (!in_array($pathinfo['extension'], $allowedTypes))
779
				fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
780
			if (strpos($pathinfo['filename'], '.') !== false)
781
				fatal_lang_error('smileys_upload_error_illegal', false);
782
			if (!in_array($pathinfo['dirname'], explode(',', $modSettings['smiley_sets_known'])))
783
				fatal_lang_error('smiley_set_not_found', false);
784
			if (!file_exists($context['smileys_dir'] . '/' . $pathinfo['dirname'] . '/' . $pathinfo['basename']))
785
				fatal_lang_error('smiley_not_found', false);
786
787
			// Now ensure every set has a file to use for this smiley
788
			foreach (explode(',', $modSettings['smiley_sets_known']) as $set)
789
			{
790
				unset($basename);
791
792
				// Check whether any similarly named files exist in the other set's directory
793
				$similar_files = glob($context['smileys_dir'] . '/' . $set . '/' . $pathinfo['filename'] . '.{' . implode(',', $allowedTypes) . '}', GLOB_BRACE);
794
795
				// If there's a similarly named file already there, use it
796
				if (!empty($similar_files))
797
				{
798
					// Prefer an exact match if there is one
799
					foreach ($similar_files as $similar_file)
800
					{
801
						if (basename($similar_file) == $pathinfo['basename'])
802
							$basename = $pathinfo['basename'];
803
					}
804
805
					// Same name, different extension
806
					if (empty($basename))
807
						$basename = basename(reset($similar_files));
808
				}
809
				// Otherwise, copy the image to the other set's directory
810
				else
811
				{
812
					copy($context['smileys_dir'] . '/' . $pathinfo['dirname'] . '/' . $pathinfo['basename'], $context['smileys_dir'] . '/' . $set . '/' . $pathinfo['basename']);
813
					smf_chmod($context['smileys_dir'] . '/' . $set . '/' . $pathinfo['basename'], 0644);
814
815
					$basename = $pathinfo['basename'];
816
				}
817
818
				// Double-check that everything went as expected
819
				if (empty($basename) || !file_exists($context['smileys_dir'] . '/' . $set . '/' . $basename))
820
					fatal_lang_error('smiley_not_found', false);
821
822
				// Okay, let's add this one
823
				$filename_array[$set] = $basename;
824
			}
825
		}
826
827
		// Find the position on the right.
828
		$smiley_order = '0';
829
		if ($_POST['smiley_location'] != 1)
830
		{
831
			$request = $smcFunc['db_query']('', '
832
				SELECT MAX(smiley_order) + 1
833
				FROM {db_prefix}smileys
834
				WHERE hidden = {int:smiley_location}
835
					AND smiley_row = {int:first_row}',
836
				array(
837
					'smiley_location' => $_POST['smiley_location'],
838
					'first_row' => 0,
839
				)
840
			);
841
			list ($smiley_order) = $smcFunc['db_fetch_row']($request);
842
			$smcFunc['db_free_result']($request);
843
844
			if (empty($smiley_order))
845
				$smiley_order = '0';
846
		}
847
848
		// Add the new smiley to the main smileys table
849
		$new_id_smiley = $smcFunc['db_insert']('',
850
			'{db_prefix}smileys',
851
			array(
852
				'code' => 'string-30', 'description' => 'string-80', 'hidden' => 'int', 'smiley_order' => 'int',
853
			),
854
			array(
855
				$_POST['smiley_code'], $_POST['smiley_description'], $_POST['smiley_location'], $smiley_order,
856
			),
857
			array('id_smiley'),
858
			1
859
		);
860
861
		// Add the filename info to the smiley_files table
862
		$inserts = array();
863
		foreach ($filename_array as $set => $basename)
864
			$inserts[] = array($new_id_smiley, $set, $basename);
865
866
		$smcFunc['db_insert']('ignore',
867
			'{db_prefix}smiley_files',
868
			array(
869
				'id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48',
870
			),
871
			$inserts,
872
			array('id_smiley', 'smiley_set')
873
		);
874
875
		foreach ($context['smiley_sets'] as $smiley_set)
876
		{
877
			cache_put_data('parsing_smileys_' . $smiley_set['raw_path'], null, 480);
878
			cache_put_data('posting_smileys_' . $smiley_set['raw_path'], null, 480);
879
		}
880
881
		// No errors? Out of here!
882
		redirectexit('action=admin;area=smileys;sa=editsmileys');
883
	}
884
885
	$context['selected_set'] = $modSettings['smiley_sets_default'];
886
887
	// Get all possible filenames for the smileys.
888
	$context['filenames'] = array();
889
	if ($context['smileys_dir_found'])
890
	{
891
		foreach ($context['smiley_sets'] as $smiley_set)
892
		{
893
			if (!file_exists($context['smileys_dir'] . '/' . $smiley_set['raw_path']))
894
				continue;
895
896
			$dir = dir($context['smileys_dir'] . '/' . $smiley_set['raw_path']);
897
			while ($entry = $dir->read())
898
			{
899
				$entry_info = pathinfo($entry);
900
				if (empty($entry_info['filename']) || empty($entry_info['extension']))
901
					continue;
902
				if (empty($context['filenames'][$smiley_set['path']][strtolower($entry_info['filename'])]) && in_array(strtolower($entry_info['extension']), $allowedTypes))
903
					$context['filenames'][$smiley_set['path']][strtolower($entry_info['filename'])] = array(
904
						'id' => $smcFunc['htmlspecialchars']($entry),
905
						'selected' => $entry_info['filename'] == 'smiley' && $smiley_set['path'] == $context['selected_set'],
906
					);
907
			}
908
			$dir->close();
909
			ksort($context['filenames'][$smiley_set['path']]);
910
		}
911
		ksort($context['filenames']);
912
	}
913
914
	// Create a new smiley from scratch.
915
	$context['current_smiley'] = array(
916
		'id' => 0,
917
		'code' => '',
918
		'filename' => $context['filenames'][$smcFunc['htmlspecialchars']($context['selected_set'])]['smiley']['id'],
919
		'description' => $txt['smileys_default_description'],
920
		'location' => 0,
921
		'is_new' => true,
922
	);
923
}
924
925
/**
926
 * Add, remove, edit smileys.
927
 */
928
function EditSmileys()
929
{
930
	global $modSettings, $context, $txt, $boarddir;
931
	global $smcFunc, $scripturl, $sourcedir;
932
933
	// Force the correct tab to be displayed.
934
	$context[$context['admin_menu_name']]['current_subsection'] = 'editsmileys';
935
	$context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
936
937
	$allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg');
938
	$disabledFiles = array('con', 'com1', 'com2', 'com3', 'com4', 'prn', 'aux', 'lpt1', '.htaccess', 'index.php');
939
	$known_sets = explode(',', $modSettings['smiley_sets_known']);
940
941
	// Submitting a form?
942
	if (isset($_POST['smiley_save']) || isset($_POST['smiley_action']) || isset($_POST['deletesmiley']))
943
	{
944
		checkSession();
945
946
		// Changing the selected smileys?
947
		if (isset($_POST['smiley_action']) && !empty($_POST['checked_smileys']))
948
		{
949
			foreach ($_POST['checked_smileys'] as $id => $smiley_id)
950
				$_POST['checked_smileys'][$id] = (int) $smiley_id;
951
952
			if ($_POST['smiley_action'] == 'delete')
953
			{
954
				$smcFunc['db_query']('', '
955
					DELETE FROM {db_prefix}smileys
956
					WHERE id_smiley IN ({array_int:checked_smileys})',
957
					array(
958
						'checked_smileys' => $_POST['checked_smileys'],
959
					)
960
				);
961
				$smcFunc['db_query']('', '
962
					DELETE FROM {db_prefix}smiley_files
963
					WHERE id_smiley IN ({array_int:checked_smileys})',
964
					array(
965
						'checked_smileys' => $_POST['checked_smileys'],
966
					)
967
				);
968
			}
969
			// Changing the status of the smiley?
970
			else
971
			{
972
				// Check it's a valid type.
973
				$displayTypes = array(
974
					'post' => 0,
975
					'hidden' => 1,
976
					'popup' => 2
977
				);
978
				if (isset($displayTypes[$_POST['smiley_action']]))
979
					$smcFunc['db_query']('', '
980
						UPDATE {db_prefix}smileys
981
						SET hidden = {int:display_type}
982
						WHERE id_smiley IN ({array_int:checked_smileys})',
983
						array(
984
							'checked_smileys' => $_POST['checked_smileys'],
985
							'display_type' => $displayTypes[$_POST['smiley_action']],
986
						)
987
					);
988
			}
989
		}
990
		// Create/modify a smiley.
991
		elseif (isset($_POST['smiley']))
992
		{
993
			// Is it a delete?
994
			if (!empty($_POST['deletesmiley']) && $_POST['smiley'] == (int) $_POST['smiley'])
995
			{
996
				$smcFunc['db_query']('', '
997
					DELETE FROM {db_prefix}smileys
998
					WHERE id_smiley = {int:current_smiley}',
999
					array(
1000
						'current_smiley' => $_POST['smiley'],
1001
					)
1002
				);
1003
				$smcFunc['db_query']('', '
1004
					DELETE FROM {db_prefix}smiley_files
1005
					WHERE id_smiley = {int:current_smiley}',
1006
					array(
1007
						'current_smiley' => $_POST['smiley'],
1008
					)
1009
				);
1010
			}
1011
			// Otherwise an edit.
1012
			else
1013
			{
1014
				foreach (array('smiley_code', 'smiley_description') as $key)
1015
					$_POST[$key] = $smcFunc['normalize']($_POST[$key]);
1016
1017
				$_POST['smiley'] = (int) $_POST['smiley'];
1018
				$_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
1019
				$_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
1020
1021
				// Make sure some code was entered.
1022
				if (empty($_POST['smiley_code']))
1023
					fatal_lang_error('smiley_has_no_code', false);
1024
1025
				// If upload a new smiley image, check that smiley set folders are writable for the sets with new images.
1026
				$writeErrors = array();
1027
				foreach ($_FILES['smiley_upload']['name'] as $set => $name)
1028
				{
1029
					if (!empty($name) && !is_writable($context['smileys_dir'] . '/' . $set))
1030
						$writeErrors[] = $set['path'];
1031
				}
1032
1033
				if (!empty($writeErrors))
1034
					fatal_lang_error('smileys_upload_error_notwritable', false, array(implode(', ', $writeErrors)));
1035
1036
				foreach ($known_sets as $set)
1037
				{
1038
					if (!isset($_FILES['smiley_upload']['name'][$set]) || empty($_FILES['smiley_upload']['name'][$set]))
1039
						continue;
1040
1041
					// Got a new image for this set
1042
					if (!is_uploaded_file($_FILES['smiley_upload']['tmp_name'][$set]) || (ini_get('open_basedir') == '' && !file_exists($_FILES['smiley_upload']['tmp_name'][$set])))
1043
						fatal_lang_error('smileys_upload_error', false);
1044
1045
					// Sorry, no spaces, dots, or anything else but letters allowed.
1046
					$_FILES['smiley_upload']['name'][$set] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['smiley_upload']['name'][$set]);
1047
1048
					// We only allow image files - it's THAT simple - no messing around here...
1049
					if (!in_array(strtolower(pathinfo($_FILES['smiley_upload']['name'][$set], PATHINFO_EXTENSION)), $allowedTypes))
1050
						fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
1051
1052
					// We only need the filename...
1053
					$destName = basename($_FILES['smiley_upload']['name'][$set]);
1054
1055
					// Make sure they aren't trying to upload a nasty file - for their own good here!
1056
					if (in_array(strtolower($destName), $disabledFiles))
1057
						fatal_lang_error('smileys_upload_error_illegal', false);
1058
1059
					// If the file exists - ignore it.
1060
					$smileyLocation = $context['smileys_dir'] . '/' . $set . '/' . $destName;
1061
					if (!file_exists($smileyLocation))
1062
					{
1063
						// Finally - move the image!
1064
						move_uploaded_file($_FILES['smiley_upload']['tmp_name'][$set], $smileyLocation);
1065
						smf_chmod($smileyLocation, 0644);
1066
					}
1067
1068
					// Double-check
1069
					if (!file_exists($smileyLocation))
1070
						fatal_lang_error('smiley_not_found', false);
1071
1072
					// Overwrite smiley filename with uploaded filename
1073
					$_POST['smiley_filename'][$set] = $destName;
1074
				}
1075
1076
				// Make sure all submitted filenames are clean.
1077
				$filenames = array();
1078
				foreach ($_POST['smiley_filename'] as $posted_set => $posted_filename)
1079
				{
1080
					$posted_set = $smcFunc['htmltrim']($smcFunc['normalize']($posted_set));
1081
					$posted_filename = $smcFunc['htmltrim']($smcFunc['normalize']($posted_filename));
1082
1083
					// Make sure the set already exists.
1084
					if (!in_array($posted_set, $known_sets))
1085
						continue;
1086
1087
					$filenames[$posted_set] = pathinfo($posted_filename, PATHINFO_BASENAME);
1088
				}
1089
				// Fill in any missing sets.
1090
				foreach ($known_sets as $known_set)
1091
				{
1092
					// Uh-oh, something is missing.
1093
					if (empty($filenames[$known_set]))
1094
					{
1095
						// Try to make it the same as the default set.
1096
						if (!empty($filenames[$modSettings['smiley_sets_default']]))
1097
							$filenames[$known_set] = $filenames[$modSettings['smiley_sets_default']];
1098
						// As a last resort, just try to get whatever the first one is.
1099
						elseif (!empty($filenames))
1100
							$filenames[$known_set] = reset($filenames);
1101
					}
1102
				}
1103
1104
				// Can't do anything without filenames for the smileys.
1105
				if (empty($filenames))
1106
					fatal_lang_error('smiley_has_no_filename', false);
1107
1108
				// Check whether the new code has duplicates. It should be unique.
1109
				$request = $smcFunc['db_query']('', '
1110
					SELECT id_smiley
1111
					FROM {db_prefix}smileys
1112
					WHERE code = {raw:mysql_binary_type} {string:smiley_code}' . (empty($_POST['smiley']) ? '' : '
1113
						AND id_smiley != {int:current_smiley}'),
1114
					array(
1115
						'current_smiley' => $_POST['smiley'],
1116
						'mysql_binary_type' => $smcFunc['db_title'] == MYSQL_TITLE ? 'BINARY' : '',
1117
						'smiley_code' => $_POST['smiley_code'],
1118
					)
1119
				);
1120
				if ($smcFunc['db_num_rows']($request) > 0)
1121
					fatal_lang_error('smiley_not_unique', false);
1122
				$smcFunc['db_free_result']($request);
1123
1124
				$smcFunc['db_query']('', '
1125
					UPDATE {db_prefix}smileys
1126
					SET
1127
						code = {string:smiley_code},
1128
						description = {string:smiley_description},
1129
						hidden = {int:smiley_location}
1130
					WHERE id_smiley = {int:current_smiley}',
1131
					array(
1132
						'smiley_location' => $_POST['smiley_location'],
1133
						'current_smiley' => $_POST['smiley'],
1134
						'smiley_code' => $_POST['smiley_code'],
1135
						'smiley_description' => $_POST['smiley_description'],
1136
					)
1137
				);
1138
1139
				// Update filename info in the smiley_files table
1140
				$inserts = array();
1141
				foreach ($filenames as $set => $filename)
1142
					$inserts[] = array($_POST['smiley'], $set, $filename);
1143
1144
				$smcFunc['db_insert']('replace',
1145
					'{db_prefix}smiley_files',
1146
					array(
1147
						'id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48',
1148
					),
1149
					$inserts,
1150
					array('id_smiley', 'smiley_set')
1151
				);
1152
			}
1153
		}
1154
1155
		foreach ($known_sets as $smiley_set)
1156
		{
1157
			cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
1158
			cache_put_data('posting_smileys_' . $smiley_set, null, 480);
1159
		}
1160
	}
1161
1162
	// Load all known smiley sets.
1163
	$context['smiley_sets'] = array_flip($known_sets);
1164
	$set_names = explode("\n", $modSettings['smiley_sets_names']);
1165
	foreach ($context['smiley_sets'] as $set => $i)
1166
		$context['smiley_sets'][$set] = array(
1167
			'id' => $i,
1168
			'raw_path' => $set,
1169
			'path' => $smcFunc['htmlspecialchars']($set),
1170
			'name' => $smcFunc['htmlspecialchars']($set_names[$i]),
1171
			'selected' => $set == $modSettings['smiley_sets_default']
1172
		);
1173
1174
	// Prepare overview of all (custom) smileys.
1175
	if ($context['sub_action'] == 'editsmileys')
1176
	{
1177
		// Determine the language specific sort order of smiley locations.
1178
		$smiley_locations = array(
1179
			$txt['smileys_location_form'],
1180
			$txt['smileys_location_hidden'],
1181
			$txt['smileys_location_popup'],
1182
		);
1183
		asort($smiley_locations);
1184
1185
		// Create a list of options for selecting smiley sets.
1186
		$smileyset_option_list = '
1187
			<select name="set" onchange="changeSet(this.options[this.selectedIndex].value);">';
1188
		foreach ($context['smiley_sets'] as $smiley_set)
1189
			$smileyset_option_list .= '
1190
				<option value="' . $smiley_set['path'] . '"' . ($modSettings['smiley_sets_default'] == $smiley_set['path'] ? ' selected' : '') . '>' . $smiley_set['name'] . '</option>';
1191
		$smileyset_option_list .= '
1192
			</select>';
1193
1194
		$listOptions = array(
1195
			'id' => 'smiley_list',
1196
			'title' => $txt['smileys_edit'],
1197
			'items_per_page' => 40,
1198
			'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys',
1199
			'default_sort_col' => 'filename',
1200
			'get_items' => array(
1201
				'function' => 'list_getSmileys',
1202
			),
1203
			'get_count' => array(
1204
				'function' => 'list_getNumSmileys',
1205
			),
1206
			'no_items_label' => $txt['smileys_no_entries'],
1207
			'columns' => array(
1208
				'picture' => array(
1209
					'data' => array(
1210
						'function' => function($rowData) use ($scripturl, $modSettings, $context)
1211
						{
1212
							$return = '';
1213
1214
							foreach ($rowData['filename_array'] as $set => $filename)
1215
							{
1216
								$return .= ' <a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=' . $rowData['id_smiley'] . '" class="smiley_set ' . $set . '"><img src="' . $modSettings['smileys_url'] . '/' . $set . '/' . $filename . '" alt="' . $rowData['description'] . '" style="padding: 2px;" id="smiley' . $rowData['id_smiley'] . '"><input type="hidden" name="smileys[' . $rowData['id_smiley'] . '][filename]" value="' . $filename . '"></a>';
1217
							}
1218
1219
							return $return;
1220
						},
1221
						'class' => 'centercol',
1222
					),
1223
				),
1224
				'smileys_code' => array(
1225
					'header' => array(
1226
						'value' => $txt['smileys_code'],
1227
					),
1228
					'data' => array(
1229
						'db_htmlsafe' => 'code',
1230
					),
1231
					'sort' => array(
1232
						'default' => 'code',
1233
						'reverse' => 'code DESC',
1234
					),
1235
				),
1236
				'filename' => array(
1237
					'header' => array(
1238
						'value' => $txt['smileys_filename'],
1239
					),
1240
					'data' => array(
1241
						'function' => function($rowData)
1242
						{
1243
							$return = '<span style="display:none">' . $rowData['filename'] . '</span>';
1244
1245
							foreach ($rowData['filename_array'] as $set => $filename)
1246
								$return .= ' <span class="smiley_set ' . $set . '">' . $filename . '</span>';
1247
1248
							return $return;
1249
						},
1250
					),
1251
					'sort' => array(
1252
						'default' => 'filename',
1253
						'reverse' => 'filename DESC',
1254
					),
1255
				),
1256
				'location' => array(
1257
					'header' => array(
1258
						'value' => $txt['smileys_location'],
1259
					),
1260
					'data' => array(
1261
						'function' => function($rowData) use ($txt)
1262
						{
1263
							if (empty($rowData['hidden']))
1264
								return $txt['smileys_location_form'];
1265
							elseif ($rowData['hidden'] == 1)
1266
								return $txt['smileys_location_hidden'];
1267
							else
1268
								return $txt['smileys_location_popup'];
1269
						},
1270
					),
1271
					'sort' => array(
1272
						'default' => $smcFunc['db_custom_order']('hidden', array_keys($smiley_locations)),
1273
						'reverse' => $smcFunc['db_custom_order']('hidden', array_keys($smiley_locations), true),
1274
					),
1275
				),
1276
				'description' => array(
1277
					'header' => array(
1278
						'value' => $txt['smileys_description'],
1279
					),
1280
					'data' => array(
1281
						'function' => function($rowData) use ($context, $txt, $modSettings, $smcFunc)
1282
						{
1283
							if (empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir']))
1284
								return $smcFunc['htmlspecialchars']($rowData['description']);
1285
1286
							// Check if there are smileys missing in some sets.
1287
							$missing_sets = array();
1288
							foreach ($context['smiley_sets'] as $smiley_set)
1289
								if (empty($rowData['filename_array'][$smiley_set['path']]) || !file_exists(sprintf('%1$s/%2$s/%3$s', $modSettings['smileys_dir'], $smiley_set['path'], $rowData['filename_array'][$smiley_set['path']])))
1290
									$missing_sets[] = $smiley_set['path'];
1291
1292
							$description = $smcFunc['htmlspecialchars']($rowData['description']);
1293
1294
							if (!empty($missing_sets))
1295
								$description .= sprintf('<br><span class="smalltext"><strong>%1$s:</strong> %2$s</span>', $txt['smileys_not_found_in_set'], implode(', ', $missing_sets));
1296
1297
							return $description;
1298
						},
1299
					),
1300
					'sort' => array(
1301
						'default' => 'description',
1302
						'reverse' => 'description DESC',
1303
					),
1304
				),
1305
				'modify' => array(
1306
					'header' => array(
1307
						'value' => $txt['smileys_modify'],
1308
						'class' => 'centercol',
1309
					),
1310
					'data' => array(
1311
						'sprintf' => array(
1312
							'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=%1$d">' . $txt['smileys_modify'] . '</a>',
1313
							'params' => array(
1314
								'id_smiley' => false,
1315
							),
1316
						),
1317
						'class' => 'centercol',
1318
					),
1319
				),
1320
				'check' => array(
1321
					'header' => array(
1322
						'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
1323
						'class' => 'centercol',
1324
					),
1325
					'data' => array(
1326
						'sprintf' => array(
1327
							'format' => '<input type="checkbox" name="checked_smileys[]" value="%1$d">',
1328
							'params' => array(
1329
								'id_smiley' => false,
1330
							),
1331
						),
1332
						'class' => 'centercol',
1333
					),
1334
				),
1335
			),
1336
			'form' => array(
1337
				'href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys',
1338
				'name' => 'smileyForm',
1339
			),
1340
			'additional_rows' => array(
1341
				array(
1342
					'position' => 'above_column_headers',
1343
					'value' => $smileyset_option_list,
1344
					'class' => 'righttext',
1345
				),
1346
				array(
1347
					'position' => 'below_table_data',
1348
					'value' => '
1349
						<select name="smiley_action" onchange="makeChanges(this.value);">
1350
							<option value="-1">' . $txt['smileys_with_selected'] . ':</option>
1351
							<option value="-1" disabled>--------------</option>
1352
							<option value="hidden">' . $txt['smileys_make_hidden'] . '</option>
1353
							<option value="post">' . $txt['smileys_show_on_post'] . '</option>
1354
							<option value="popup">' . $txt['smileys_show_on_popup'] . '</option>
1355
							<option value="delete">' . $txt['smileys_remove'] . '</option>
1356
						</select>
1357
						<noscript>
1358
							<input type="submit" name="perform_action" value="' . $txt['go'] . '" class="button">
1359
						</noscript>',
1360
					'class' => 'righttext',
1361
				),
1362
			),
1363
			'javascript' => '
1364
				function makeChanges(action)
1365
				{
1366
					if (action == \'-1\')
1367
						return false;
1368
					else if (action == \'delete\')
1369
					{
1370
						if (confirm(\'' . $txt['smileys_confirm'] . '\'))
1371
							document.forms.smileyForm.submit();
1372
					}
1373
					else
1374
						document.forms.smileyForm.submit();
1375
					return true;
1376
				}
1377
				function changeSet(newSet)
1378
				{
1379
					$(".smiley_set").hide();
1380
					$(".smiley_set." + newSet).show();
1381
				}',
1382
		);
1383
1384
		require_once($sourcedir . '/Subs-List.php');
1385
		createList($listOptions);
1386
1387
		// The list is the only thing to show, so make it the main template.
1388
		$context['default_list'] = 'smiley_list';
1389
		$context['sub_template'] = 'show_list';
1390
1391
		addInlineJavaScript("\n\t" . 'changeSet("' . $modSettings['smiley_sets_default'] . '");', true);
1392
	}
1393
	// Modifying smileys.
1394
	elseif ($context['sub_action'] == 'modifysmiley')
1395
	{
1396
		$context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
1397
		$context['smileys_dir_found'] = is_dir($context['smileys_dir']);
1398
1399
		$context['selected_set'] = $modSettings['smiley_sets_default'];
1400
1401
		$request = $smcFunc['db_query']('', '
1402
			SELECT s.id_smiley AS id, s.code, f.filename, f.smiley_set, s.description, s.hidden AS location
1403
			FROM {db_prefix}smileys AS s
1404
				LEFT JOIN {db_prefix}smiley_files AS f ON (s.id_smiley = f.id_smiley)
1405
			WHERE s.id_smiley = {int:current_smiley}',
1406
			array(
1407
				'current_smiley' => (int) $_REQUEST['smiley'],
1408
			)
1409
		);
1410
		while ($row = $smcFunc['db_fetch_assoc']($request))
1411
		{
1412
			// The empty() bit is for just in case the default set is missing this smiley
1413
			if ($row['smiley_set'] == $context['selected_set'] || empty($context['current_smiley']))
1414
				$context['current_smiley'] = $row;
1415
1416
			$filenames[$row['smiley_set']] = $row['filename'];
1417
		}
1418
		$smcFunc['db_free_result']($request);
1419
1420
		if (empty($context['current_smiley']))
1421
			fatal_lang_error('smiley_not_found', false);
1422
1423
		$context['current_smiley']['code'] = $smcFunc['htmlspecialchars']($context['current_smiley']['code']);
1424
		$context['current_smiley']['description'] = $smcFunc['htmlspecialchars']($context['current_smiley']['description']);
1425
		$context['current_smiley']['filename'] = $smcFunc['htmlspecialchars']($context['current_smiley']['filename']);
1426
1427
		// Get all possible filenames for the smileys.
1428
		$context['filenames'] = array();
1429
		$context['missing_sets'] = array();
1430
		if ($context['smileys_dir_found'])
1431
		{
1432
			foreach ($context['smiley_sets'] as $smiley_set)
1433
			{
1434
				if (!file_exists($context['smileys_dir'] . '/' . $smiley_set['raw_path']))
1435
					continue;
1436
1437
				// No file currently defined for this smiley in this set? That's no good.
1438
				if (!isset($filenames[$smiley_set['raw_path']]))
1439
				{
1440
					$context['missing_sets'][] = $smiley_set['raw_path'];
1441
					$context['filenames'][$smiley_set['path']][''] = array('id' => '', 'selected' => true, 'disabled' => true);
1442
				}
1443
1444
				$dir = dir($context['smileys_dir'] . '/' . $smiley_set['raw_path']);
1445
				while ($entry = $dir->read())
1446
				{
1447
					if (empty($context['filenames'][$smiley_set['path']][$entry]) && in_array(pathinfo($entry, PATHINFO_EXTENSION), $allowedTypes))
1448
						$context['filenames'][$smiley_set['path']][$entry] = array(
1449
							'id' => $smcFunc['htmlspecialchars']($entry),
1450
							'selected' => isset($filenames[$smiley_set['raw_path']]) && strtolower($entry) == strtolower($filenames[$smiley_set['raw_path']]),
1451
							'disabled' => false,
1452
						);
1453
				}
1454
				$dir->close();
1455
				ksort($context['filenames'][$smiley_set['path']]);
1456
			}
1457
			ksort($context['filenames']);
1458
		}
1459
	}
1460
}
1461
1462
/**
1463
 * Callback function for createList().
1464
 *
1465
 * @param int $start The item to start with (not used here)
1466
 * @param int $items_per_page The number of items to show per page (not used here)
1467
 * @param string $sort A string indicating how to sort the results
1468
 * @return array An array of info about the smileys
1469
 */
1470
function list_getSmileys($start, $items_per_page, $sort)
1471
{
1472
	global $smcFunc, $modSettings;
1473
1474
	$request = $smcFunc['db_query']('', '
1475
		SELECT s.id_smiley, s.code, f.filename, f.smiley_set, s.description, s.smiley_row, s.smiley_order, s.hidden
1476
		FROM {db_prefix}smileys AS s
1477
			LEFT JOIN {db_prefix}smiley_files AS f ON (s.id_smiley = f.id_smiley)
1478
		ORDER BY {raw:sort}',
1479
		array(
1480
			'sort' => $sort,
1481
		)
1482
	);
1483
	$smileys = array();
1484
	while ($row = $smcFunc['db_fetch_assoc']($request))
1485
	{
1486
		if (empty($smileys[$row['id_smiley']]))
1487
		{
1488
			$smileys[$row['id_smiley']] = $row;
1489
			unset($smileys[$row['id_smiley']]['smiley_set']);
1490
			$smileys[$row['id_smiley']]['filename_array'] = array($row['smiley_set'] => $row['filename']);
1491
		}
1492
		else
1493
		{
1494
			$smileys[$row['id_smiley']]['filename_array'][$row['smiley_set']] = $row['filename'];
1495
		}
1496
1497
		// Use the filename for the default set as the primary filename for this smiley
1498
		if (isset($smileys[$row['id_smiley']]['filename_array'][$modSettings['smiley_sets_default']]))
1499
			$smileys[$row['id_smiley']]['filename'] = $smileys[$row['id_smiley']]['filename_array'][$modSettings['smiley_sets_default']];
1500
		else
1501
			$smileys[$row['id_smiley']]['filename'] = reset($smileys[$row['id_smiley']]['filename_array']);
1502
	}
1503
	$smcFunc['db_free_result']($request);
1504
1505
	return $smileys;
1506
}
1507
1508
/**
1509
 * Callback function for createList().
1510
 *
1511
 * @return int The number of smileys
1512
 */
1513
function list_getNumSmileys()
1514
{
1515
	global $smcFunc;
1516
1517
	$request = $smcFunc['db_query']('', '
1518
		SELECT COUNT(*)
1519
		FROM {db_prefix}smileys',
1520
		array()
1521
	);
1522
	list($numSmileys) = $smcFunc['db_fetch_row'];
1523
	$smcFunc['db_free_result']($request);
1524
1525
	return $numSmileys;
1526
}
1527
1528
/**
1529
 * Allows to edit smileys order.
1530
 */
1531
function EditSmileyOrder()
1532
{
1533
	global $context, $txt, $smcFunc, $modSettings;
1534
1535
	// Move smileys to another position.
1536
	if (isset($_REQUEST['reorder']))
1537
	{
1538
		checkSession('get');
1539
1540
		$_GET['location'] = empty($_GET['location']) || $_GET['location'] != 'popup' ? 0 : 2;
1541
		$_GET['source'] = empty($_GET['source']) ? 0 : (int) $_GET['source'];
1542
1543
		if (empty($_GET['source']))
1544
			fatal_lang_error('smiley_not_found', false);
1545
1546
		if (!empty($_GET['after']))
1547
		{
1548
			$_GET['after'] = (int) $_GET['after'];
1549
1550
			$request = $smcFunc['db_query']('', '
1551
				SELECT smiley_row, smiley_order, hidden
1552
				FROM {db_prefix}smileys
1553
				WHERE hidden = {int:location}
1554
					AND id_smiley = {int:after_smiley}',
1555
				array(
1556
					'location' => $_GET['location'],
1557
					'after_smiley' => $_GET['after'],
1558
				)
1559
			);
1560
			if ($smcFunc['db_num_rows']($request) != 1)
1561
				fatal_lang_error('smiley_not_found', false);
1562
			list ($smiley_row, $smiley_order, $smileyLocation) = $smcFunc['db_fetch_row']($request);
1563
			$smcFunc['db_free_result']($request);
1564
		}
1565
		else
1566
		{
1567
			$smiley_row = (int) $_GET['row'];
1568
			$smiley_order = -1;
1569
			$smileyLocation = (int) $_GET['location'];
1570
		}
1571
1572
		$smcFunc['db_query']('', '
1573
			UPDATE {db_prefix}smileys
1574
			SET smiley_order = smiley_order + 1
1575
			WHERE hidden = {int:new_location}
1576
				AND smiley_row = {int:smiley_row}
1577
				AND smiley_order > {int:smiley_order}',
1578
			array(
1579
				'new_location' => $_GET['location'],
1580
				'smiley_row' => $smiley_row,
1581
				'smiley_order' => $smiley_order,
1582
			)
1583
		);
1584
1585
		$smcFunc['db_query']('', '
1586
			UPDATE {db_prefix}smileys
1587
			SET
1588
				smiley_order = {int:smiley_order} + 1,
1589
				smiley_row = {int:smiley_row},
1590
				hidden = {int:new_location}
1591
			WHERE id_smiley = {int:current_smiley}',
1592
			array(
1593
				'smiley_order' => $smiley_order,
1594
				'smiley_row' => $smiley_row,
1595
				'new_location' => $smileyLocation,
1596
				'current_smiley' => $_GET['source'],
1597
			)
1598
		);
1599
1600
		foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set)
1601
		{
1602
			cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
1603
			cache_put_data('posting_smileys_' . $smiley_set, null, 480);
1604
		}
1605
	}
1606
1607
	$request = $smcFunc['db_query']('', '
1608
		SELECT s.id_smiley, s.code, f.filename, s.description, s.smiley_row, s.smiley_order, s.hidden
1609
		FROM {db_prefix}smileys AS s
1610
			LEFT JOIN {db_prefix}smiley_files AS f ON (s.id_smiley = f.id_smiley AND f.smiley_set = {string:smiley_set})
1611
		WHERE s.hidden != {int:popup}
1612
		ORDER BY s.smiley_order, s.smiley_row',
1613
		array(
1614
			'popup' => 1,
1615
			'smiley_set' => $modSettings['smiley_sets_default'],
1616
		)
1617
	);
1618
	$context['smileys'] = array(
1619
		'postform' => array(
1620
			'rows' => array(),
1621
		),
1622
		'popup' => array(
1623
			'rows' => array(),
1624
		),
1625
	);
1626
	while ($row = $smcFunc['db_fetch_assoc']($request))
1627
	{
1628
		$location = empty($row['hidden']) ? 'postform' : 'popup';
1629
		$context['smileys'][$location]['rows'][$row['smiley_row']][] = array(
1630
			'id' => $row['id_smiley'],
1631
			'code' => $smcFunc['htmlspecialchars']($row['code']),
1632
			'filename' => $smcFunc['htmlspecialchars']($row['filename']),
1633
			'description' => $smcFunc['htmlspecialchars']($row['description']),
1634
			'row' => $row['smiley_row'],
1635
			'order' => $row['smiley_order'],
1636
			'selected' => !empty($_REQUEST['move']) && $_REQUEST['move'] == $row['id_smiley'],
1637
		);
1638
	}
1639
	$smcFunc['db_free_result']($request);
1640
1641
	$context['move_smiley'] = empty($_REQUEST['move']) ? 0 : (int) $_REQUEST['move'];
1642
1643
	// Make sure all rows are sequential.
1644
	foreach (array_keys($context['smileys']) as $location)
1645
		$context['smileys'][$location] = array(
1646
			'id' => $location,
1647
			'title' => $location == 'postform' ? $txt['smileys_location_form'] : $txt['smileys_location_popup'],
1648
			'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $txt['smileys_location_popup_description'],
1649
			'last_row' => count($context['smileys'][$location]['rows']),
1650
			'rows' => array_values($context['smileys'][$location]['rows']),
1651
		);
1652
1653
	// Check & fix smileys that are not ordered properly in the database.
1654
	foreach (array_keys($context['smileys']) as $location)
1655
	{
1656
		foreach ($context['smileys'][$location]['rows'] as $id => $smiley_row)
1657
		{
1658
			// Fix empty rows if any.
1659
			if ($id != $smiley_row[0]['row'])
1660
			{
1661
				$smcFunc['db_query']('', '
1662
					UPDATE {db_prefix}smileys
1663
					SET smiley_row = {int:new_row}
1664
					WHERE smiley_row = {int:current_row}
1665
						AND hidden = {int:location}',
1666
					array(
1667
						'new_row' => $id,
1668
						'current_row' => $smiley_row[0]['row'],
1669
						'location' => $location == 'postform' ? '0' : '2',
1670
					)
1671
				);
1672
				// Only change the first row value of the first smiley (we don't need the others :P).
1673
				$context['smileys'][$location]['rows'][$id][0]['row'] = $id;
1674
			}
1675
			// Make sure the smiley order is always sequential.
1676
			foreach ($smiley_row as $order_id => $smiley)
1677
				if ($order_id != $smiley['order'])
1678
					$smcFunc['db_query']('', '
1679
						UPDATE {db_prefix}smileys
1680
						SET smiley_order = {int:new_order}
1681
						WHERE id_smiley = {int:current_smiley}',
1682
						array(
1683
							'new_order' => $order_id,
1684
							'current_smiley' => $smiley['id'],
1685
						)
1686
					);
1687
		}
1688
	}
1689
1690
	foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set)
1691
	{
1692
		cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
1693
		cache_put_data('posting_smileys_' . $smiley_set, null, 480);
1694
	}
1695
}
1696
1697
/**
1698
 * Install a smiley set.
1699
 */
1700
function InstallSmileySet()
1701
{
1702
	global $sourcedir, $boarddir, $packagesdir, $modSettings, $smcFunc, $scripturl, $context, $txt, $user_info;
1703
1704
	isAllowedTo('manage_smileys');
1705
	checkSession('request');
1706
	// One of these two may be necessary
1707
	loadLanguage('Errors');
1708
	loadLanguage('Packages');
1709
1710
	require_once($sourcedir . '/Subs-Package.php');
1711
1712
	// Installing unless proven otherwise
1713
	$testing = false;
1714
1715
	if (isset($_REQUEST['set_gz']))
1716
	{
1717
		$base_name = strtr(basename($_REQUEST['set_gz']), ':/', '-_');
1718
		$name = $smcFunc['htmlspecialchars'](strtok(basename($_REQUEST['set_gz']), '.'));
1719
		$context['filename'] = $base_name;
1720
1721
		// Check that the smiley is from simplemachines.org, for now... maybe add mirroring later.
1722
		// @ TODO: Our current xml files serve http links.  Allowing both for now until we serve https.
1723
		if (preg_match('~^https?://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['set_gz']) == 0 || strpos($_REQUEST['set_gz'], 'dlattach') !== false)
1724
			fatal_lang_error('not_on_simplemachines', false);
1725
1726
		$destination = $packagesdir . '/' . $base_name;
1727
1728
		if (file_exists($destination))
1729
			fatal_lang_error('package_upload_error_exists', false);
1730
1731
		// Let's copy it to the Packages directory
1732
		file_put_contents($destination, fetch_web_data($_REQUEST['set_gz']));
1733
		$testing = true;
1734
	}
1735
	elseif (isset($_REQUEST['package']))
1736
	{
1737
		$base_name = basename($_REQUEST['package']);
1738
		$name = $smcFunc['htmlspecialchars'](strtok(basename($_REQUEST['package']), '.'));
1739
		$context['filename'] = $base_name;
1740
1741
		$destination = $packagesdir . '/' . basename($_REQUEST['package']);
1742
	}
1743
1744
	if (empty($destination) || !file_exists($destination))
1745
		fatal_lang_error('package_no_file', false);
1746
1747
	// Make sure temp directory exists and is empty.
1748
	if (file_exists($packagesdir . '/temp'))
1749
		deltree($packagesdir . '/temp', false);
1750
1751
	if (!mktree($packagesdir . '/temp', 0755))
1752
	{
1753
		deltree($packagesdir . '/temp', false);
1754
		if (!mktree($packagesdir . '/temp', 0777))
1755
		{
1756
			deltree($packagesdir . '/temp', false);
1757
			// @todo not sure about url in destination_url
1758
			create_chmod_control(array($packagesdir . '/temp/delme.tmp'), array('destination_url' => $scripturl . '?action=admin;area=smileys;sa=install;set_gz=' . $_REQUEST['set_gz'], 'crash_on_error' => true));
1759
1760
			deltree($packagesdir . '/temp', false);
1761
			if (!mktree($packagesdir . '/temp', 0777))
1762
				fatal_lang_error('package_cant_download', false);
1763
		}
1764
	}
1765
1766
	$extracted = read_tgz_file($destination, $packagesdir . '/temp');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $destination does not seem to be defined for all execution paths leading up to this point.
Loading history...
1767
	if (!$extracted)
1768
		fatal_lang_error('packageget_unable', false, array('https://custom.simplemachines.org/mods/index.php?action=search;type=12;basic_search=' . $name));
1769
	if ($extracted && !file_exists($packagesdir . '/temp/package-info.xml'))
1770
		foreach ($extracted as $file)
1771
			if (basename($file['filename']) == 'package-info.xml')
1772
			{
1773
				$base_path = dirname($file['filename']) . '/';
1774
				break;
1775
			}
1776
1777
	if (!isset($base_path))
1778
		$base_path = '';
1779
1780
	if (!file_exists($packagesdir . '/temp/' . $base_path . 'package-info.xml'))
1781
		fatal_lang_error('package_get_error_missing_xml', false);
1782
1783
	$smileyInfo = getPackageInfo($context['filename']);
1784
	if (!is_array($smileyInfo))
1785
		fatal_lang_error($smileyInfo, false);
1786
1787
	// See if it is installed?
1788
	$request = $smcFunc['db_query']('', '
1789
		SELECT version, themes_installed, db_changes
1790
		FROM {db_prefix}log_packages
1791
		WHERE package_id = {string:current_package}
1792
			AND install_state != {int:not_installed}
1793
		ORDER BY time_installed DESC
1794
		LIMIT 1',
1795
		array(
1796
			'not_installed' => 0,
1797
			'current_package' => $smileyInfo['id'],
1798
		)
1799
	);
1800
1801
	if ($smcFunc['db_num_rows']($request) > 0)
1802
		fatal_lang_error('package_installed_warning1', false);
1803
1804
	// Everything is fine, now it's time to do something
1805
	$actions = parsePackageInfo($smileyInfo['xml'], true, 'install');
1806
1807
	$context['post_url'] = $scripturl . '?action=admin;area=smileys;sa=install;package=' . $base_name;
1808
	$context['has_failure'] = false;
1809
	$context['actions'] = array();
1810
	$context['ftp_needed'] = false;
1811
1812
	foreach ($actions as $action)
1813
	{
1814
		if ($action['type'] == 'readme' || $action['type'] == 'license')
1815
		{
1816
			$type = 'package_' . $action['type'];
1817
			if (file_exists($packagesdir . '/temp/' . $base_path . $action['filename']))
1818
				$context[$type] = $smcFunc['htmlspecialchars'](trim(file_get_contents($packagesdir . '/temp/' . $base_path . $action['filename']), "\n\r"));
1819
			elseif (file_exists($action['filename']))
1820
				$context[$type] = $smcFunc['htmlspecialchars'](trim(file_get_contents($action['filename']), "\n\r"));
1821
1822
			if (!empty($action['parse_bbc']))
1823
			{
1824
				require_once($sourcedir . '/Subs-Post.php');
1825
				preparsecode($context[$type]);
1826
				$context[$type] = parse_bbc($context[$type]);
1827
			}
1828
			else
1829
				$context[$type] = nl2br($context[$type]);
1830
1831
			continue;
1832
		}
1833
		elseif ($action['type'] == 'require-dir')
1834
		{
1835
			// Do this one...
1836
			$thisAction = array(
1837
				'type' => $txt['package_extract'] . ' ' . ($action['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
1838
				'action' => $smcFunc['htmlspecialchars'](strtr($action['destination'], array($boarddir => '.')))
1839
			);
1840
1841
			$file = $packagesdir . '/temp/' . $base_path . $action['filename'];
1842
			if (isset($action['filename']) && (!file_exists($file) || !is_writable(dirname($action['destination']))))
1843
			{
1844
				$context['has_failure'] = true;
1845
1846
				$thisAction += array(
1847
					'description' => $txt['package_action_error'],
1848
					'failed' => true,
1849
				);
1850
			}
1851
			// @todo None given?
1852
			if (empty($thisAction['description']))
1853
				$thisAction['description'] = isset($action['description']) ? $action['description'] : '';
1854
1855
			$context['actions'][] = $thisAction;
1856
		}
1857
		elseif ($action['type'] == 'credits')
1858
		{
1859
			// Time to build the billboard
1860
			$credits_tag = array(
1861
				'url' => $action['url'],
1862
				'license' => $action['license'],
1863
				'copyright' => $action['copyright'],
1864
				'title' => $action['title'],
1865
			);
1866
		}
1867
	}
1868
1869
	if ($testing)
1870
	{
1871
		$context['sub_template'] = 'view_package';
1872
		$context['uninstalling'] = false;
1873
		$context['is_installed'] = false;
1874
		$context['package_name'] = $smileyInfo['name'];
1875
		loadTemplate('Packages');
1876
	}
1877
	// Do the actual install
1878
	else
1879
	{
1880
		// @TODO Does this call have side effects? ($actions is not used)
1881
		$actions = parsePackageInfo($smileyInfo['xml'], false, 'install');
1882
		foreach ($context['actions'] as $action)
1883
		{
1884
			updateSettings(array(
1885
				'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . basename($action['action']),
1886
				'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . $smileyInfo['name'] . (count($context['actions']) > 1 ? ' ' . (!empty($action['description']) ? $smcFunc['htmlspecialchars']($action['description']) : basename($action['action'])) : ''),
1887
			));
1888
		}
1889
1890
		package_flush_cache();
1891
1892
		// Credits tag?
1893
		$credits_tag = (empty($credits_tag)) ? '' : $smcFunc['json_encode']($credits_tag);
1894
		$smcFunc['db_insert']('',
1895
			'{db_prefix}log_packages',
1896
			array(
1897
				'filename' => 'string', 'name' => 'string', 'package_id' => 'string', 'version' => 'string',
1898
				'id_member_installed' => 'int', 'member_installed' => 'string', 'time_installed' => 'int',
1899
				'install_state' => 'int', 'failed_steps' => 'string', 'themes_installed' => 'string',
1900
				'member_removed' => 'int', 'db_changes' => 'string', 'credits' => 'string',
1901
			),
1902
			array(
1903
				$smileyInfo['filename'], $smileyInfo['name'], $smileyInfo['id'], $smileyInfo['version'],
1904
				$user_info['id'], $user_info['name'], time(),
1905
				1, '', '',
1906
				0, '', $credits_tag,
1907
			),
1908
			array('id_install')
1909
		);
1910
1911
		logAction('install_package', array('package' => $smcFunc['htmlspecialchars']($smileyInfo['name']), 'version' => $smcFunc['htmlspecialchars']($smileyInfo['version'])), 'admin');
1912
1913
		foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set)
1914
		{
1915
			cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
1916
			cache_put_data('posting_smileys_' . $smiley_set, null, 480);
1917
		}
1918
	}
1919
1920
	if (file_exists($packagesdir . '/temp'))
1921
		deltree($packagesdir . '/temp');
1922
1923
	if (!$testing)
1924
		redirectexit('action=admin;area=smileys');
1925
}
1926
1927
/**
1928
 * A function to import new smileys from an existing directory into the database.
1929
 *
1930
 * @param string $smileyPath The path to the directory to import smileys from
1931
 * @param bool $create Whether or not to make brand new smileys for files that don't match any existing smileys
1932
 */
1933
function ImportSmileys($smileyPath, $create = false)
1934
{
1935
	global $modSettings, $smcFunc;
1936
1937
	if (empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir'] . '/' . $smileyPath))
1938
		fatal_lang_error('smiley_set_unable_to_import', false);
1939
1940
	$allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg');
1941
	$known_sets = explode(',', $modSettings['smiley_sets_known']);
1942
	sort($known_sets);
1943
1944
	// Get the smileys in the folder
1945
	$smileys = array();
1946
	$dir = dir($modSettings['smileys_dir'] . '/' . $smileyPath);
1947
	while ($entry = $dir->read())
1948
	{
1949
		$pathinfo = pathinfo($entry);
1950
		if (empty($pathinfo['filename']) || empty($pathinfo['extension']))
1951
			continue;
1952
		if (in_array($pathinfo['extension'], $allowedTypes) && $pathinfo['filename'] != 'blank' && strlen($pathinfo['basename']) <= 48)
1953
			$smiley_files[strtolower($pathinfo['basename'])] = $pathinfo['basename'];
1954
	}
1955
	$dir->close();
1956
1957
	// Get the smileys that are already in the database.
1958
	$existing_smileys = array();
1959
	$request = $smcFunc['db_query']('', '
1960
		SELECT id_smiley, smiley_set, filename
1961
		FROM {db_prefix}smiley_files',
1962
		array()
1963
	);
1964
	while ($row = $smcFunc['db_fetch_assoc']($request))
1965
		$existing_smileys[pathinfo($row['filename'], PATHINFO_FILENAME)][$row['id_smiley']][] = $row['smiley_set'];
1966
	$smcFunc['db_free_result']($request);
1967
1968
	// Filter $smiley_files down to just the ones not already in the database.
1969
	$to_unset = array();
1970
	$to_fix = array();
1971
	foreach ($smiley_files as $key => $smiley_file)
1972
	{
1973
		$smiley_name = pathinfo($smiley_file, PATHINFO_FILENAME);
1974
1975
		// A brand new one
1976
		if (empty($existing_smileys[$smiley_name]))
1977
			continue;
1978
1979
		// A file with this name is already being used for at least one smiley, so we have more work to do...
1980
		foreach ($existing_smileys[$smiley_name] as $existing_id => $existing_sets)
1981
		{
1982
			$to_unset[$key][$existing_id] = false;
1983
1984
			sort($existing_sets);
1985
1986
			// Already done
1987
			if ($existing_sets === $known_sets)
1988
				$to_unset[$key][$existing_id] = true;
1989
1990
			// Used in some sets but not others
1991
			else
1992
			{
1993
				// Do the other sets have some other file already defined?
1994
				foreach ($existing_smileys as $file => $info)
1995
				{
1996
					foreach ($info as $info_id => $info_sets)
1997
					{
1998
						if ($existing_id == $info_id)
1999
							$existing_sets = array_unique(array_merge($existing_sets, $info_sets));
2000
					}
2001
				}
2002
				sort($existing_sets);
2003
2004
				// If every set already has a file for this smiley, we can skip it
2005
				if ($known_sets == $existing_sets)
2006
					$to_unset[$key][$existing_id] = true;
2007
2008
				// Need to add the file for these sets
2009
				else
2010
					$to_fix[$key][$existing_id] = array_diff($known_sets, $existing_sets);
2011
			}
2012
		}
2013
	}
2014
2015
	// Fix any sets with missing files
2016
	// This part handles files for pre-existing smileys in a newly created smiley set
2017
	$inserts = array();
2018
	foreach ($to_fix as $key => $ids)
2019
	{
2020
		foreach ($ids as $id_smiley => $sets_missing)
2021
		{
2022
			// Find the file we need to copy to the other sets
2023
			if (file_exists($modSettings['smileys_dir'] . '/' . $smileyPath . '/' . $smiley_files[$key]))
2024
				$p = $smileyPath;
2025
			else
2026
			{
2027
				foreach (array_diff($known_sets, $sets_missing) as $set)
2028
				{
2029
					if (file_exists($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key]))
2030
					{
2031
						$p = $set;
2032
						break;
2033
					}
2034
				}
2035
			}
2036
2037
			foreach ($sets_missing as $set)
2038
			{
2039
				if ($set !== $p)
2040
				{
2041
					// Copy the file into the set's folder
2042
					copy($modSettings['smileys_dir'] . '/' . $p . '/' . $smiley_files[$key], $modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key]);
2043
					smf_chmod($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key], 0644);
2044
				}
2045
2046
				// Double-check that everything went as expected
2047
				if (!file_exists($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key]))
2048
					continue;
2049
2050
				// Update the database
2051
				$inserts[] = array($id_smiley, $set, $smiley_files[$key]);
2052
2053
				// This isn't a new smiley
2054
				$to_unset[$key][$id_smiley] = true;
2055
			}
2056
		}
2057
	}
2058
2059
	// Remove anything that isn't actually new from our list of files
2060
	foreach ($to_unset as $key => $ids)
2061
	{
2062
		if (array_reduce($ids, function ($carry, $item) { return $carry * $item; }, true) == true)
2063
			unset($smiley_files[$key]);
2064
	}
2065
2066
	// We only create brand new smileys if asked.
2067
	if (empty($create))
2068
		$smiley_files = array();
2069
2070
	// New smileys go at the end of the list
2071
	$request = $smcFunc['db_query']('', '
2072
		SELECT MAX(smiley_order)
2073
		FROM {db_prefix}smileys
2074
		WHERE hidden = {int:postform}
2075
			AND smiley_row = {int:first_row}',
2076
		array(
2077
			'postform' => 0,
2078
			'first_row' => 0,
2079
		)
2080
	);
2081
	list ($smiley_order) = $smcFunc['db_fetch_row']($request);
2082
	$smcFunc['db_free_result']($request);
2083
2084
	// This part handles brand new smileys that don't exist in any set
2085
	$new_smileys = array();
2086
	foreach ($smiley_files as $key => $smiley_file)
2087
	{
2088
		// Ensure every set has a file to use for the new smiley
2089
		foreach ($known_sets as $set)
2090
		{
2091
			unset($basename);
2092
2093
			if ($smileyPath != $set)
2094
			{
2095
				// Check whether any similarly named files exist in the other set's directory
2096
				$similar_files = glob($modSettings['smileys_dir'] . '/' . $set . '/' . pathinfo($smiley_file, PATHINFO_FILENAME) . '.{' . implode(',', $allowedTypes) . '}', GLOB_BRACE);
2097
2098
				// If there's a similarly named file already there, use it
2099
				if (!empty($similar_files))
2100
				{
2101
					// Prefer an exact match if there is one
2102
					foreach ($similar_files as $similar_file)
2103
					{
2104
						if (basename($similar_file) == $smiley_file)
2105
							$basename = $smiley_file;
2106
					}
2107
2108
					// Same name, different extension
2109
					if (empty($basename))
2110
						$basename = basename(reset($similar_files));
2111
				}
2112
				// Otherwise, copy the image to the other set's directory
2113
				else
2114
				{
2115
					copy($modSettings['smileys_dir'] . '/' . $smileyPath . '/' . $smiley_file, $modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_file);
2116
					smf_chmod($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_file, 0644);
2117
2118
					$basename = $smiley_file;
2119
				}
2120
2121
				// Double-check that everything went as expected
2122
				if (empty($basename) || !file_exists($modSettings['smileys_dir'] . '/' . $set . '/' . $basename))
2123
					continue;
2124
			}
2125
			else
2126
				$basename = $smiley_file;
2127
2128
			$new_smileys[$key]['files'][$set] = $basename;
2129
		}
2130
2131
		$new_smileys[$key]['info'] = array(':' . pathinfo($smiley_file, PATHINFO_FILENAME) . ':', pathinfo($smiley_file, PATHINFO_FILENAME), 0, ++$smiley_order);
2132
	}
2133
2134
	// Add the info for any new smileys to the database
2135
	foreach ($new_smileys as $new_smiley)
2136
	{
2137
		$new_id_smiley = $smcFunc['db_insert']('',
2138
			'{db_prefix}smileys',
2139
			array(
2140
				'code' => 'string-30', 'description' => 'string-80', 'smiley_row' => 'int', 'smiley_order' => 'int',
2141
			),
2142
			$new_smiley['info'],
2143
			array('id_smiley'),
2144
			1
2145
		);
2146
2147
		// We'll also need to add filename info to the smiley_files table
2148
		foreach ($new_smiley['files'] as $set => $filename)
2149
			$inserts[] = array($new_id_smiley, $set, $filename);
2150
	}
2151
2152
	// Finally, update the smiley_files table with all our new files
2153
	if (!empty($inserts))
2154
	{
2155
		$smcFunc['db_insert']('replace',
2156
			'{db_prefix}smiley_files',
2157
			array(
2158
				'id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48',
2159
			),
2160
			$inserts,
2161
			array('id_smiley', 'smiley_set')
2162
		);
2163
2164
		foreach ($known_sets as $set)
2165
		{
2166
			cache_put_data('parsing_smileys_' . $set, null, 480);
2167
			cache_put_data('posting_smileys_' . $set, null, 480);
2168
		}
2169
	}
2170
}
2171
2172
/**
2173
 * Handles editing message icons
2174
 */
2175
function EditMessageIcons()
2176
{
2177
	global $context, $settings, $txt;
2178
	global $smcFunc, $scripturl, $sourcedir;
2179
2180
	// Get a list of icons.
2181
	$context['icons'] = array();
2182
	$request = $smcFunc['db_query']('', '
2183
		SELECT m.id_icon, m.title, m.filename, m.icon_order, m.id_board, b.name AS board_name
2184
		FROM {db_prefix}message_icons AS m
2185
			LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
2186
		WHERE ({query_see_board} OR b.id_board IS NULL)
2187
		ORDER BY m.icon_order',
2188
		array(
2189
		)
2190
	);
2191
	$last_icon = 0;
2192
	$trueOrder = 0;
2193
	while ($row = $smcFunc['db_fetch_assoc']($request))
2194
	{
2195
		$context['icons'][$row['id_icon']] = array(
2196
			'id' => $row['id_icon'],
2197
			'title' => $row['title'],
2198
			'filename' => $row['filename'],
2199
			'image_url' => $settings[file_exists($settings['theme_dir'] . '/images/post/' . $row['filename'] . '.png') ? 'actual_images_url' : 'default_images_url'] . '/post/' . $row['filename'] . '.png',
2200
			'board_id' => $row['id_board'],
2201
			'board' => empty($row['board_name']) ? $txt['icons_edit_icons_all_boards'] : $row['board_name'],
2202
			'order' => $row['icon_order'],
2203
			'true_order' => $trueOrder++,
2204
			'after' => $last_icon,
2205
		);
2206
		$last_icon = $row['id_icon'];
2207
	}
2208
	$smcFunc['db_free_result']($request);
2209
2210
	// Submitting a form?
2211
	if (isset($_POST['icons_save']) || isset($_POST['delete']))
2212
	{
2213
		checkSession();
2214
2215
		// Deleting icons?
2216
		if (isset($_POST['delete']) && !empty($_POST['checked_icons']))
2217
		{
2218
			$deleteIcons = array();
2219
			foreach ($_POST['checked_icons'] as $icon)
2220
				$deleteIcons[] = (int) $icon;
2221
2222
			// Do the actual delete!
2223
			$smcFunc['db_query']('', '
2224
				DELETE FROM {db_prefix}message_icons
2225
				WHERE id_icon IN ({array_int:icon_list})',
2226
				array(
2227
					'icon_list' => $deleteIcons,
2228
				)
2229
			);
2230
		}
2231
		// Editing/Adding an icon?
2232
		elseif ($context['sub_action'] == 'editicon' && isset($_GET['icon']))
2233
		{
2234
			$_GET['icon'] = (int) $_GET['icon'];
2235
2236
			foreach (array('icon_filename', 'icon_description') as $key)
2237
				$_POST[$key] = $smcFunc['normalize']($_POST[$key]);
2238
2239
			// Do some preperation with the data... like check the icon exists *somewhere*
2240
			if (strpos($_POST['icon_filename'], '.png') !== false)
2241
				$_POST['icon_filename'] = substr($_POST['icon_filename'], 0, -4);
2242
			if (!file_exists($settings['default_theme_dir'] . '/images/post/' . $_POST['icon_filename'] . '.png'))
2243
				fatal_lang_error('icon_not_found', false);
2244
			// There is a 16 character limit on message icons...
2245
			elseif (strlen($_POST['icon_filename']) > 16)
2246
				fatal_lang_error('icon_name_too_long', false);
2247
			elseif ($_POST['icon_location'] == $_GET['icon'] && !empty($_GET['icon']))
2248
				fatal_lang_error('icon_after_itself', false);
2249
2250
			// First do the sorting... if this is an edit reduce the order of everything after it by one ;)
2251
			if ($_GET['icon'] != 0)
2252
			{
2253
				$oldOrder = $context['icons'][$_GET['icon']]['true_order'];
2254
				foreach ($context['icons'] as $id => $data)
2255
					if ($data['true_order'] > $oldOrder)
2256
						$context['icons'][$id]['true_order']--;
2257
			}
2258
2259
			// If there are no existing icons and this is a new one, set the id to 1 (mainly for non-mysql)
2260
			if (empty($_GET['icon']) && empty($context['icons']))
2261
				$_GET['icon'] = 1;
2262
2263
			// Get the new order.
2264
			$newOrder = $_POST['icon_location'] == 0 ? 0 : $context['icons'][$_POST['icon_location']]['true_order'] + 1;
2265
			// Do the same, but with the one that used to be after this icon, done to avoid conflict.
2266
			foreach ($context['icons'] as $id => $data)
2267
				if ($data['true_order'] >= $newOrder)
2268
					$context['icons'][$id]['true_order']++;
2269
2270
			// Finally set the current icon's position!
2271
			$context['icons'][$_GET['icon']]['true_order'] = $newOrder;
2272
2273
			// Simply replace the existing data for the other bits.
2274
			$context['icons'][$_GET['icon']]['title'] = $_POST['icon_description'];
2275
			$context['icons'][$_GET['icon']]['filename'] = $_POST['icon_filename'];
2276
			$context['icons'][$_GET['icon']]['board_id'] = (int) $_POST['icon_board'];
2277
2278
			// Do a huge replace ;)
2279
			$iconInsert = array();
2280
			$iconInsert_new = array();
2281
			foreach ($context['icons'] as $id => $icon)
2282
			{
2283
				if ($id != 0)
2284
				{
2285
					$iconInsert[] = array($id, $icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']);
2286
				}
2287
				else
2288
				{
2289
					$iconInsert_new[] = array($icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']);
2290
				}
2291
			}
2292
2293
			$smcFunc['db_insert']('replace',
2294
				'{db_prefix}message_icons',
2295
				array('id_icon' => 'int', 'id_board' => 'int', 'title' => 'string-80', 'filename' => 'string-80', 'icon_order' => 'int'),
2296
				$iconInsert,
2297
				array('id_icon')
2298
			);
2299
2300
			if (!empty($iconInsert_new))
2301
			{
2302
				$smcFunc['db_insert']('insert',
2303
					'{db_prefix}message_icons',
2304
					array('id_board' => 'int', 'title' => 'string-80', 'filename' => 'string-80', 'icon_order' => 'int'),
2305
					$iconInsert_new,
2306
					array('id_icon')
2307
				);
2308
			}
2309
		}
2310
2311
		// Unless we're adding a new thing, we'll escape
2312
		if (!isset($_POST['add']))
2313
			redirectexit('action=admin;area=smileys;sa=editicons');
2314
	}
2315
2316
	$context[$context['admin_menu_name']]['current_subsection'] = 'editicons';
2317
2318
	$listOptions = array(
2319
		'id' => 'message_icon_list',
2320
		'title' => $txt['icons_edit_message_icons'],
2321
		'base_href' => $scripturl . '?action=admin;area=smileys;sa=editicons',
2322
		'get_items' => array(
2323
			'function' => 'list_getMessageIcons',
2324
		),
2325
		'no_items_label' => $txt['icons_no_entries'],
2326
		'columns' => array(
2327
			'icon' => array(
2328
				'data' => array(
2329
					'function' => function($rowData) use ($settings, $smcFunc)
2330
					{
2331
						$images_url = $settings[file_exists(sprintf('%1$s/images/post/%2$s.png', $settings['theme_dir'], $rowData['filename'])) ? 'actual_images_url' : 'default_images_url'];
2332
						return sprintf('<img src="%1$s/post/%2$s.png" alt="%3$s">', $images_url, $rowData['filename'], $smcFunc['htmlspecialchars']($rowData['title']));
2333
					},
2334
					'class' => 'centercol',
2335
				),
2336
			),
2337
			'filename' => array(
2338
				'header' => array(
2339
					'value' => $txt['smileys_filename'],
2340
				),
2341
				'data' => array(
2342
					'sprintf' => array(
2343
						'format' => '%1$s.png',
2344
						'params' => array(
2345
							'filename' => true,
2346
						),
2347
					),
2348
				),
2349
			),
2350
			'description' => array(
2351
				'header' => array(
2352
					'value' => $txt['smileys_description'],
2353
				),
2354
				'data' => array(
2355
					'db_htmlsafe' => 'title',
2356
				),
2357
			),
2358
			'board' => array(
2359
				'header' => array(
2360
					'value' => $txt['icons_board'],
2361
				),
2362
				'data' => array(
2363
					'function' => function($rowData) use ($txt)
2364
					{
2365
						return empty($rowData['board_name']) ? $txt['icons_edit_icons_all_boards'] : $rowData['board_name'];
2366
					},
2367
				),
2368
			),
2369
			'modify' => array(
2370
				'header' => array(
2371
					'value' => $txt['smileys_modify'],
2372
					'class' => 'centercol',
2373
				),
2374
				'data' => array(
2375
					'sprintf' => array(
2376
						'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=editicon;icon=%1$s">' . $txt['smileys_modify'] . '</a>',
2377
						'params' => array(
2378
							'id_icon' => false,
2379
						),
2380
					),
2381
					'class' => 'centercol',
2382
				),
2383
			),
2384
			'check' => array(
2385
				'header' => array(
2386
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
2387
					'class' => 'centercol',
2388
				),
2389
				'data' => array(
2390
					'sprintf' => array(
2391
						'format' => '<input type="checkbox" name="checked_icons[]" value="%1$d">',
2392
						'params' => array(
2393
							'id_icon' => false,
2394
						),
2395
					),
2396
					'class' => 'centercol',
2397
				),
2398
			),
2399
		),
2400
		'form' => array(
2401
			'href' => $scripturl . '?action=admin;area=smileys;sa=editicons',
2402
		),
2403
		'additional_rows' => array(
2404
			array(
2405
				'position' => 'below_table_data',
2406
				'value' => '<input type="submit" name="delete" value="' . $txt['quickmod_delete_selected'] . '" class="button"> <a class="button" href="' . $scripturl . '?action=admin;area=smileys;sa=editicon">' . $txt['icons_add_new'] . '</a>',
2407
			),
2408
		),
2409
	);
2410
2411
	require_once($sourcedir . '/Subs-List.php');
2412
	createList($listOptions);
2413
2414
	// If we're adding/editing an icon we'll need a list of boards
2415
	if ($context['sub_action'] == 'editicon' || isset($_POST['add']))
2416
	{
2417
		// Force the sub_template just in case.
2418
		$context['sub_template'] = 'editicon';
2419
2420
		$context['new_icon'] = !isset($_GET['icon']);
2421
2422
		// Get the properties of the current icon from the icon list.
2423
		if (!$context['new_icon'])
2424
			$context['icon'] = $context['icons'][$_GET['icon']];
2425
2426
		// Get a list of boards needed for assigning this icon to a specific board.
2427
		$boardListOptions = array(
2428
			'use_permissions' => true,
2429
			'selected_board' => isset($context['icon']['board_id']) ? $context['icon']['board_id'] : 0,
2430
		);
2431
		require_once($sourcedir . '/Subs-MessageIndex.php');
2432
		$context['categories'] = getBoardList($boardListOptions);
2433
	}
2434
}
2435
2436
/**
2437
 * Callback function for createList().
2438
 *
2439
 * @param int $start The item to start with (not used here)
2440
 * @param int $items_per_page The number of items to display per page (not used here)
2441
 * @param string $sort A string indicating how to sort the items (not used here)
2442
 * @return array An array of information about message icons
2443
 */
2444
function list_getMessageIcons($start, $items_per_page, $sort)
2445
{
2446
	global $smcFunc;
2447
2448
	$request = $smcFunc['db_query']('', '
2449
		SELECT m.id_icon, m.title, m.filename, m.icon_order, m.id_board, b.name AS board_name
2450
		FROM {db_prefix}message_icons AS m
2451
			LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
2452
		WHERE ({query_see_board} OR b.id_board IS NULL)
2453
		ORDER BY m.icon_order',
2454
		array()
2455
	);
2456
2457
	$message_icons = array();
2458
	while ($row = $smcFunc['db_fetch_assoc']($request))
2459
		$message_icons[] = $row;
2460
	$smcFunc['db_free_result']($request);
2461
2462
	return $message_icons;
2463
}
2464
2465
?>