Issues (1027)

Sources/ManageSmileys.php (4 issues)

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 http://www.simplemachines.org
10
 * @copyright 2019 Simple Machines and individual contributors
11
 * @license http://www.simplemachines.org/about/smf/license.php BSD
12
 *
13
 * @version 2.1 RC2
14
 */
15
16
if (!defined('SMF'))
17
	die('No direct access...');
18
19
/**
20
 * This is the 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
	);
149
150
	call_integration_hook('integrate_modify_smiley_settings', array(&$config_vars));
151
152
	if ($return_config)
153
		return $config_vars;
154
155
	// Setup the basics of the settings template.
156
	require_once($sourcedir . '/ManageServer.php');
157
	$context['sub_template'] = 'show_settings';
158
159
	// Finish up the form...
160
	$context['post_url'] = $scripturl . '?action=admin;area=smileys;save;sa=settings';
161
162
	// Saving the settings?
163
	if (isset($_GET['save']))
164
	{
165
		checkSession();
166
167
		// Validate the smiley set name.
168
		$_POST['smiley_sets_default'] = empty($smiley_context[$_POST['smiley_sets_default']]) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_default'];
169
170
		call_integration_hook('integrate_save_smiley_settings');
171
172
		saveDBSettings($config_vars);
173
		$_SESSION['adm-save'] = true;
174
175
		foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set)
176
		{
177
			cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
178
			cache_put_data('posting_smileys_' . $smiley_set, null, 480);
179
		}
180
181
		redirectexit('action=admin;area=smileys;sa=settings');
182
	}
183
184
	// We need this for the in-line permissions
185
	createToken('admin-mp');
186
187
	prepareDBSettingContext($config_vars);
188
}
189
190
/**
191
 * List, add, remove, modify smileys sets.
192
 */
193
function EditSmileySets()
194
{
195
	global $modSettings, $context, $txt;
196
	global $smcFunc, $scripturl, $sourcedir;
197
198
	// Set the right tab to be selected.
199
	$context[$context['admin_menu_name']]['current_subsection'] = 'editsets';
200
201
	$allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg');
202
203
	// They must've been submitted a form.
204
	if (isset($_POST['smiley_save']))
205
	{
206
		checkSession();
207
		validateToken('admin-mss', 'request');
208
209
		// Delete selected smiley sets.
210
		if (!empty($_POST['delete']) && !empty($_POST['smiley_set']))
211
		{
212
			$set_paths = explode(',', $modSettings['smiley_sets_known']);
213
			$set_names = explode("\n", $modSettings['smiley_sets_names']);
214
			foreach ($_POST['smiley_set'] as $id => $val)
215
			{
216
				// If this is the set you've marked as default, or the only one remaining, you can't delete it
217
				if ($modSettings['smiley_sets_default'] != $set_paths[$id] && count($set_paths) != 1 && isset($set_paths[$id], $set_names[$id]))
218
				{
219
					// Delete this set's entries from the smiley_files table
220
					$smcFunc['db_query']('', '
221
						DELETE FROM {db_prefix}smiley_files
222
						WHERE smiley_set = {string:smiley_set}',
223
						array(
224
							'smiley_set' => $set_paths[$id],
225
						)
226
					);
227
228
					// Remove this set from our lists
229
					unset($set_paths[$id], $set_names[$id]);
230
				}
231
			}
232
233
			// Shortcut... array_merge() on a single array resets the numeric keys
234
			$set_paths = array_merge($set_paths);
235
			$set_names = array_merge($set_names);
236
237
			updateSettings(array(
238
				'smiley_sets_known' => implode(',', $set_paths),
239
				'smiley_sets_names' => implode("\n", $set_names),
240
				'smiley_sets_default' => in_array($modSettings['smiley_sets_default'], $set_paths) ? $modSettings['smiley_sets_default'] : $set_paths[0],
241
			));
242
		}
243
		// Add a new smiley set.
244
		elseif (!empty($_POST['add']))
245
			$context['sub_action'] = 'modifyset';
246
		// Create or modify a smiley set.
247
		elseif (isset($_POST['set']))
248
		{
249
			$set_paths = explode(',', $modSettings['smiley_sets_known']);
250
			$set_names = explode("\n", $modSettings['smiley_sets_names']);
251
252
			// Create a new smiley set.
253
			if ($_POST['set'] == -1 && isset($_POST['smiley_sets_path']))
254
			{
255
				if (in_array($_POST['smiley_sets_path'], $set_paths))
256
					fatal_lang_error('smiley_set_already_exists');
257
258
				updateSettings(array(
259
					'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . $_POST['smiley_sets_path'],
260
					'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . $_POST['smiley_sets_name'],
261
					'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path'],
262
				));
263
			}
264
			// Modify an existing smiley set.
265
			else
266
			{
267
				// Make sure the smiley set exists.
268
				if (!isset($set_paths[$_POST['set']]) || !isset($set_names[$_POST['set']]))
269
					fatal_lang_error('smiley_set_not_found');
270
271
				// Make sure the path is not yet used by another smileyset.
272
				if (in_array($_POST['smiley_sets_path'], $set_paths) && $_POST['smiley_sets_path'] != $set_paths[$_POST['set']])
273
					fatal_lang_error('smiley_set_path_already_used');
274
275
				$set_paths[$_POST['set']] = $_POST['smiley_sets_path'];
276
				$set_names[$_POST['set']] = $_POST['smiley_sets_name'];
277
				updateSettings(array(
278
					'smiley_sets_known' => implode(',', $set_paths),
279
					'smiley_sets_names' => implode("\n", $set_names),
280
					'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path']
281
				));
282
			}
283
284
			// Import, but only the ones that match existing smileys
285
			ImportSmileys($_POST['smiley_sets_path'], false);
286
		}
287
288
		foreach ($set_paths as $smiley_set)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $set_paths does not seem to be defined for all execution paths leading up to this point.
Loading history...
289
		{
290
			cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
291
			cache_put_data('posting_smileys_' . $smiley_set, null, 480);
292
		}
293
	}
294
295
	// Load all available smileysets...
296
	$context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
297
	$set_names = explode("\n", $modSettings['smiley_sets_names']);
298
	foreach ($context['smiley_sets'] as $i => $set)
299
		$context['smiley_sets'][$i] = array(
300
			'id' => $i,
301
			'raw_path' => $set,
302
			'path' => $smcFunc['htmlspecialchars']($set),
303
			'name' => $smcFunc['htmlspecialchars']($set_names[$i]),
304
			'selected' => $set == $modSettings['smiley_sets_default']
305
		);
306
307
	// Importing any smileys from an existing set?
308
	if ($context['sub_action'] == 'import')
309
	{
310
		checkSession('get');
311
		validateToken('admin-mss', 'request');
312
313
		$_GET['set'] = (int) $_GET['set'];
314
315
		// Sanity check - then import.
316
		if (isset($context['smiley_sets'][$_GET['set']]))
317
			ImportSmileys(un_htmlspecialchars($context['smiley_sets'][$_GET['set']]['path']), true);
318
319
		// Force the process to continue.
320
		$context['sub_action'] = 'modifyset';
321
		$context['sub_template'] = 'modifyset';
322
	}
323
	// If we're modifying or adding a smileyset, some context info needs to be set.
324
	if ($context['sub_action'] == 'modifyset')
325
	{
326
		$_GET['set'] = !isset($_GET['set']) ? -1 : (int) $_GET['set'];
327
		if ($_GET['set'] == -1 || !isset($context['smiley_sets'][$_GET['set']]))
328
			$context['current_set'] = array(
329
				'id' => '-1',
330
				'raw_path' => '',
331
				'path' => '',
332
				'name' => '',
333
				'selected' => false,
334
				'is_new' => true,
335
			);
336
		else
337
		{
338
			$context['current_set'] = &$context['smiley_sets'][$_GET['set']];
339
			$context['current_set']['is_new'] = false;
340
341
			// Calculate whether there are any smileys in the directory that can be imported.
342
			if (!empty($modSettings['smiley_enable']) && !empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']))
343
			{
344
				$smileys = array();
345
				$dir = dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']);
346
				while ($entry = $dir->read())
347
				{
348
					$pathinfo = pathinfo($entry);
349
					if (empty($pathinfo['filename']) || empty($pathinfo['extension']))
350
						continue;
351
					if (in_array($pathinfo['extension'], $allowedTypes) && $pathinfo['filename'] != 'blank')
352
						$smileys[strtolower($entry)] = $entry;
353
				}
354
				$dir->close();
355
356
				if (empty($smileys))
357
					fatal_lang_error('smiley_set_dir_not_found', false, array($context['current_set']['name']));
358
359
				// Exclude the smileys that are already in the database.
360
				$request = $smcFunc['db_query']('', '
361
					SELECT filename
362
					FROM {db_prefix}smiley_files
363
					WHERE filename IN ({array_string:smiley_list})
364
						AND smiley_set = {string:smiley_set}',
365
					array(
366
						'smiley_list' => $smileys,
367
						'smiley_set' => $context['current_set']['path'],
368
					)
369
				);
370
				while ($row = $smcFunc['db_fetch_assoc']($request))
371
				{
372
					if (isset($smileys[strtolower($row['filename'])]))
373
						unset($smileys[strtolower($row['filename'])]);
374
				}
375
376
				$smcFunc['db_free_result']($request);
377
378
				$context['current_set']['can_import'] = count($smileys);
379
				$context['current_set']['import_url'] = $scripturl . '?action=admin;area=smileys;sa=import;set=' . $context['current_set']['id'] . ';' . $context['session_var'] . '=' . $context['session_id'];
380
			}
381
		}
382
383
		// Retrieve all potential smiley set directories.
384
		$context['smiley_set_dirs'] = array();
385
		if (!empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir']))
386
		{
387
			$dir = dir($modSettings['smileys_dir']);
388
			while ($entry = $dir->read())
389
			{
390
				if (!in_array($entry, array('.', '..')) && is_dir($modSettings['smileys_dir'] . '/' . $entry))
391
					$context['smiley_set_dirs'][] = array(
392
						'id' => $entry,
393
						'path' => $modSettings['smileys_dir'] . '/' . $entry,
394
						'selectable' => $entry == $context['current_set']['path'] || !in_array($entry, explode(',', $modSettings['smiley_sets_known'])),
395
						'current' => $entry == $context['current_set']['path'],
396
					);
397
			}
398
			$dir->close();
399
		}
400
	}
401
402
	// This is our save haven.
403
	createToken('admin-mss', 'request');
404
405
	// In case we need to import smileys, we need to add the token in now.
406
	if (isset($context['current_set']['import_url']))
407
	{
408
		$context['current_set']['import_url'] .= ';' . $context['admin-mss_token_var'] . '=' . $context['admin-mss_token'];
409
		$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']);
410
	}
411
412
	$listOptions = array(
413
		'id' => 'smiley_set_list',
414
		'title' => $txt['smiley_sets'],
415
		'no_items_label' => $txt['smiley_sets_none'],
416
		'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsets',
417
		'default_sort_col' => 'name',
418
		'get_items' => array(
419
			'function' => 'list_getSmileySets',
420
		),
421
		'get_count' => array(
422
			'function' => 'list_getNumSmileySets',
423
		),
424
		'columns' => array(
425
			'default' => array(
426
				'header' => array(
427
					'value' => $txt['smiley_sets_default'],
428
					'class' => 'centercol',
429
				),
430
				'data' => array(
431
					'function' => function($rowData)
432
					{
433
						return $rowData['selected'] ? '<span class="main_icons valid"></span>' : '';
434
					},
435
					'class' => 'centercol',
436
				),
437
				'sort' => array(
438
					'default' => 'selected',
439
					'reverse' => 'selected DESC',
440
				),
441
			),
442
			'name' => array(
443
				'header' => array(
444
					'value' => $txt['smiley_sets_name'],
445
				),
446
				'data' => array(
447
					'db_htmlsafe' => 'name',
448
				),
449
				'sort' => array(
450
					'default' => 'name',
451
					'reverse' => 'name DESC',
452
				),
453
			),
454
			'url' => array(
455
				'header' => array(
456
					'value' => $txt['smiley_sets_url'],
457
				),
458
				'data' => array(
459
					'sprintf' => array(
460
						'format' => $modSettings['smileys_url'] . '/<strong>%1$s</strong>/...',
461
						'params' => array(
462
							'path' => true,
463
						),
464
					),
465
				),
466
				'sort' => array(
467
					'default' => 'path',
468
					'reverse' => 'path DESC',
469
				),
470
			),
471
			'modify' => array(
472
				'header' => array(
473
					'value' => $txt['smiley_set_modify'],
474
					'class' => 'centercol',
475
				),
476
				'data' => array(
477
					'sprintf' => array(
478
						'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset;set=%1$d">' . $txt['smiley_set_modify'] . '</a>',
479
						'params' => array(
480
							'id' => true,
481
						),
482
					),
483
					'class' => 'centercol',
484
				),
485
			),
486
			'check' => array(
487
				'header' => array(
488
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
489
					'class' => 'centercol',
490
				),
491
				'data' => array(
492
					'function' => function($rowData)
493
					{
494
						return $rowData['selected'] ? '' : sprintf('<input type="checkbox" name="smiley_set[%1$d]">', $rowData['id']);
495
					},
496
					'class' => 'centercol',
497
				),
498
			),
499
		),
500
		'form' => array(
501
			'href' => $scripturl . '?action=admin;area=smileys;sa=editsets',
502
			'token' => 'admin-mss',
503
		),
504
		'additional_rows' => array(
505
			array(
506
				'position' => 'above_table_headers',
507
				'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> ',
508
			),
509
			array(
510
				'position' => 'below_table_data',
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
		),
514
	);
515
516
	require_once($sourcedir . '/Subs-List.php');
517
	createList($listOptions);
518
}
519
520
/**
521
 * Callback function for createList().
522
 *
523
 * @todo to be moved to Subs-Smileys?
524
 *
525
 * @param int $start The item to start with (not used here)
526
 * @param int $items_per_page The number of items to show per page (not used here)
527
 * @param string $sort A string indicating how to sort the results
528
 * @return array An array of info about the smiley sets
529
 */
530
function list_getSmileySets($start, $items_per_page, $sort)
531
{
532
	global $modSettings;
533
534
	$known_sets = explode(',', $modSettings['smiley_sets_known']);
535
	$set_names = explode("\n", $modSettings['smiley_sets_names']);
536
	$cols = array(
537
		'id' => array(),
538
		'selected' => array(),
539
		'path' => array(),
540
		'name' => array(),
541
	);
542
	foreach ($known_sets as $i => $set)
543
	{
544
		$cols['id'][] = $i;
545
		$cols['selected'][] = $set == $modSettings['smiley_sets_default'];
546
		$cols['path'][] = $set;
547
		$cols['name'][] = $set_names[$i];
548
	}
549
	$sort_flag = strpos($sort, 'DESC') === false ? SORT_ASC : SORT_DESC;
550
	if (substr($sort, 0, 4) === 'name')
551
		array_multisort($cols['name'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['selected'], $cols['id']);
552
	elseif (substr($sort, 0, 4) === 'path')
553
		array_multisort($cols['path'], $sort_flag, SORT_REGULAR, $cols['name'], $cols['selected'], $cols['id']);
554
	else
555
		array_multisort($cols['selected'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['name'], $cols['id']);
556
557
	$smiley_sets = array();
558
	foreach ($cols['id'] as $i => $id)
559
		$smiley_sets[] = array(
560
			'id' => $id,
561
			'path' => $cols['path'][$i],
562
			'name' => $cols['name'][$i],
563
			'selected' => $cols['selected'][$i],
564
		);
565
566
	return $smiley_sets;
567
}
568
569
/**
570
 * Callback function for createList().
571
 *
572
 * @todo to be moved to Subs-Smileys?
573
 * @return int The total number of known smiley sets
574
 */
575
function list_getNumSmileySets()
576
{
577
	global $modSettings;
578
579
	return count(explode(',', $modSettings['smiley_sets_known']));
580
}
581
582
/**
583
 * Add a smiley, that's right.
584
 */
585
function AddSmiley()
586
{
587
	global $modSettings, $context, $txt, $boarddir, $smcFunc;
588
589
	// Get a list of all known smiley sets.
590
	$context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
591
	$context['smileys_dir_found'] = is_dir($context['smileys_dir']);
592
	$context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
593
	$set_names = explode("\n", $modSettings['smiley_sets_names']);
594
	foreach ($context['smiley_sets'] as $i => $set)
595
		$context['smiley_sets'][$i] = array(
596
			'id' => $i,
597
			'raw_path' => $set,
598
			'path' => $smcFunc['htmlspecialchars']($set),
599
			'name' => $smcFunc['htmlspecialchars']($set_names[$i]),
600
			'selected' => $set == $modSettings['smiley_sets_default']
601
		);
602
603
	// Some useful arrays... types we allow - and ports we don't!
604
	$allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg');
605
	$disabledFiles = array('con', 'com1', 'com2', 'com3', 'com4', 'prn', 'aux', 'lpt1', '.htaccess', 'index.php');
606
607
	// This will hold the names of the added files for each set
608
	$filename_array = array();
609
610
	// Submitting a form?
611
	if (isset($_POST[$context['session_var']], $_POST['smiley_code']))
612
	{
613
		checkSession();
614
615
		$_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
616
		$_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
617
		$_POST['smiley_filename'] = htmltrim__recursive($_POST['smiley_filename']);
618
619
		// Make sure some code was entered.
620
		if (empty($_POST['smiley_code']))
621
			fatal_lang_error('smiley_has_no_code');
622
623
		// Check whether the new code has duplicates. It should be unique.
624
		$request = $smcFunc['db_query']('', '
625
			SELECT id_smiley
626
			FROM {db_prefix}smileys
627
			WHERE code = {raw:mysql_binary_statement} {string:smiley_code}',
628
			array(
629
				'mysql_binary_statement' => $smcFunc['db_title'] == 'MySQL' ? 'BINARY' : '',
630
				'smiley_code' => $_POST['smiley_code'],
631
			)
632
		);
633
		if ($smcFunc['db_num_rows']($request) > 0)
634
			fatal_lang_error('smiley_not_unique');
635
		$smcFunc['db_free_result']($request);
636
637
		// If we are uploading - check all the smiley sets are writable!
638
		if ($_POST['method'] != 'existing')
639
		{
640
			$writeErrors = array();
641
			foreach ($context['smiley_sets'] as $set)
642
			{
643
				if (!is_writable($context['smileys_dir'] . '/' . $set['raw_path']))
644
					$writeErrors[] = $set['path'];
645
			}
646
			if (!empty($writeErrors))
647
				fatal_lang_error('smileys_upload_error_notwritable', true, array(implode(', ', $writeErrors)));
648
		}
649
650
		// Uploading just one smiley for all of them?
651
		if (isset($_POST['sameall']) && isset($_FILES['uploadSmiley']['name']) && $_FILES['uploadSmiley']['name'] != '')
652
		{
653
			if (!is_uploaded_file($_FILES['uploadSmiley']['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['uploadSmiley']['tmp_name'])))
654
				fatal_lang_error('smileys_upload_error');
655
656
			// Sorry, no spaces, dots, or anything else but letters allowed.
657
			$_FILES['uploadSmiley']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['uploadSmiley']['name']);
658
659
			// We only allow image files - it's THAT simple - no messing around here...
660
			if (!in_array(strtolower(pathinfo($_FILES['uploadSmiley']['name'], PATHINFO_EXTENSION)), $allowedTypes))
661
				fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
662
663
			// We only need the filename...
664
			$destName = basename($_FILES['uploadSmiley']['name']);
665
666
			// Make sure they aren't trying to upload a nasty file - for their own good here!
667
			if (in_array(strtolower($destName), $disabledFiles))
668
				fatal_lang_error('smileys_upload_error_illegal');
669
670
			// Check if the file already exists... and if not move it to EVERY smiley set directory.
671
			$smileyLocation = null;
672
			foreach ($context['smiley_sets'] as $i => $set)
673
			{
674
				// Okay, we're going to put the smiley right here, since it's not there yet!
675
				if (!file_exists($context['smileys_dir'] . '/' . $context['smiley_sets'][$i]['raw_path']) . '/' . $destName)
676
				{
677
					$smileyLocation = $context['smileys_dir'] . '/' . $context['smiley_sets'][$i]['raw_path'] . '/' . $destName;
678
					move_uploaded_file($_FILES['uploadSmiley']['tmp_name'], $smileyLocation);
679
					smf_chmod($smileyLocation, 0644);
680
					break;
681
				}
682
			}
683
684
			// Now, we want to move it from there to all the other sets.
685
			foreach ($context['smiley_sets'] as $j => $set)
686
			{
687
				$currentPath = $context['smileys_dir'] . '/' . $context['smiley_sets'][$j]['raw_path'] . '/' . $destName;
688
689
				// Copy the first one we made to here, unless it already exists there
690
				if (!empty($smileyLocation) && !file_exists($currentPath))
691
				{
692
					copy($smileyLocation, $currentPath);
693
					smf_chmod($currentPath, 0644);
694
				}
695
696
				// Double-check
697
				if (!file_exists($currentPath))
698
					fatal_lang_error('smiley_not_found');
699
700
				// Finally make sure it's saved correctly!
701
				$filename_array[$context['smiley_sets'][$j]['raw_path']] = $destName;
702
			}
703
		}
704
		// What about uploading several files?
705
		elseif ($_POST['method'] != 'existing')
706
		{
707
			$newName = '';
708
			foreach ($_FILES as $name => $data)
709
			{
710
				if ($_FILES[$name]['name'] == '')
711
					fatal_lang_error('smileys_upload_error_blank');
712
713
				// if (empty($newName))
714
				// 	$newName = basename($_FILES[$name]['name']);
715
				// elseif (basename($_FILES[$name]['name']) != $newName)
716
				// 	fatal_lang_error('smileys_upload_error_name');
717
			}
718
719
			foreach ($context['smiley_sets'] as $i => $set)
720
			{
721
				$set['name'] = un_htmlspecialchars($set['name']);
722
723
				if (!isset($_FILES['individual_' . $set['raw_path']]['name']) || $_FILES['individual_' . $set['raw_path']]['name'] == '')
724
					continue;
725
726
				// Got one...
727
				if (!is_uploaded_file($_FILES['individual_' . $set['raw_path']]['tmp_name']) || (ini_get('open_basedir') == '' && !file_exists($_FILES['individual_' . $set['raw_path']]['tmp_name'])))
728
					fatal_lang_error('smileys_upload_error');
729
730
				// Sorry, no spaces, dots, or anything else but letters allowed.
731
				$_FILES['individual_' . $set['raw_path']]['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['individual_' . $set['raw_path']]['name']);
732
733
				// We only allow image files - it's THAT simple - no messing around here...
734
				if (!in_array(strtolower(pathinfo($_FILES['individual_' . $set['raw_path']]['name'], PATHINFO_EXTENSION)), $allowedTypes))
735
					fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
736
737
				// We only need the filename...
738
				$destName = basename($_FILES['individual_' . $set['raw_path']]['name']);
739
740
				// Make sure they aren't trying to upload a nasty file - for their own good here!
741
				if (in_array(strtolower($destName), $disabledFiles))
742
					fatal_lang_error('smileys_upload_error_illegal');
743
744
				// If the file exists - ignore it.
745
				$smileyLocation = $context['smileys_dir'] . '/' . $set['raw_path'] . '/' . $destName;
746
				if (!file_exists($smileyLocation))
747
				{
748
					// Finally - move the image!
749
					move_uploaded_file($_FILES['individual_' . $set['raw_path']]['tmp_name'], $smileyLocation);
750
					smf_chmod($smileyLocation, 0644);
751
				}
752
753
				// Double-check
754
				if (!file_exists($smileyLocation))
755
					fatal_lang_error('smiley_not_found');
756
757
				// Should always be saved correctly!
758
				$filename_array[$set['raw_path']] = $destName;
759
			}
760
		}
761
		// Re-using an existing image
762
		else
763
		{
764
			// Make sure a filename was given
765
			if (empty($_POST['smiley_filename']))
766
				fatal_lang_error('smiley_has_no_filename');
767
768
			// And make sure it is legitimate
769
			$pathinfo = pathinfo($_POST['smiley_filename']);
770
771
			if (!in_array($pathinfo['extension'], $allowedTypes))
772
				fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
773
			if (strpos($pathinfo['filename'], '.') !== false)
774
				fatal_lang_error('smileys_upload_error_illegal');
775
			if (!in_array($pathinfo['dirname'], explode(',', $modSettings['smiley_sets_known'])))
776
				fatal_lang_error('smiley_set_not_found');
777
			if (!file_exists($context['smileys_dir'] . '/' . $pathinfo['dirname'] . '/' . $pathinfo['basename']))
778
				fatal_lang_error('smiley_not_found');
779
780
			// Now ensure every set has a file to use for this smiley
781
			foreach (explode(',', $modSettings['smiley_sets_known']) as $set)
782
			{
783
				unset($basename);
784
785
				// Check whether any similarly named files exist in the other set's directory
786
				$similar_files = glob($context['smileys_dir'] . '/' . $set . '/' . $pathinfo['filename'] . '.{' . implode(',', $allowedTypes) . '}', GLOB_BRACE);
787
788
				// If there's a similarly named file already there, use it
789
				if (!empty($similar_files))
790
				{
791
					// Prefer an exact match if there is one
792
					foreach ($similar_files as $similar_file)
793
					{
794
						if (basename($similar_file) == $pathinfo['basename'])
795
							$basename = $pathinfo['basename'];
796
					}
797
798
					// Same name, different extension
799
					if (empty($basename))
800
						$basename = basename(reset($similar_files));
801
				}
802
				// Otherwise, copy the image to the other set's directory
803
				else
804
				{
805
					copy($context['smileys_dir'] . '/' . $pathinfo['dirname'] . '/' . $pathinfo['basename'], $context['smileys_dir'] . '/' . $set . '/' . $pathinfo['basename']);
806
					smf_chmod($context['smileys_dir'] . '/' . $set . '/' . $pathinfo['basename'], 0644);
807
808
					$basename = $pathinfo['basename'];
809
				}
810
811
				// Double-check that everything went as expected
812
				if (empty($basename) || !file_exists($context['smileys_dir'] . '/' . $set . '/' . $basename))
813
					fatal_lang_error('smiley_not_found');
814
815
				// Okay, let's add this one
816
				$filename_array[$set] = $basename;
817
			}
818
		}
819
820
		// Find the position on the right.
821
		$smiley_order = '0';
822
		if ($_POST['smiley_location'] != 1)
823
		{
824
			$request = $smcFunc['db_query']('', '
825
				SELECT MAX(smiley_order) + 1
826
				FROM {db_prefix}smileys
827
				WHERE hidden = {int:smiley_location}
828
					AND smiley_row = {int:first_row}',
829
				array(
830
					'smiley_location' => $_POST['smiley_location'],
831
					'first_row' => 0,
832
				)
833
			);
834
			list ($smiley_order) = $smcFunc['db_fetch_row']($request);
835
			$smcFunc['db_free_result']($request);
836
837
			if (empty($smiley_order))
838
				$smiley_order = '0';
839
		}
840
841
		// Add the new smiley to the main smileys table
842
		$new_id_smiley = $smcFunc['db_insert']('',
843
			'{db_prefix}smileys',
844
			array(
845
				'code' => 'string-30', 'description' => 'string-80', 'hidden' => 'int', 'smiley_order' => 'int',
846
			),
847
			array(
848
				$_POST['smiley_code'], $_POST['smiley_description'], $_POST['smiley_location'], $smiley_order,
849
			),
850
			array('id_smiley'),
851
			1
852
		);
853
854
		// Add the filename info to the smiley_files table
855
		$inserts = array();
856
		foreach ($filename_array as $set => $basename)
857
			$inserts[] = array($new_id_smiley, $set, $basename);
858
859
		$smcFunc['db_insert']('ignore',
860
			'{db_prefix}smiley_files',
861
			array(
862
				'id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48',
863
			),
864
			$inserts,
865
			array('id_smiley', 'smiley_set')
866
		);
867
868
		foreach ($context['smiley_sets'] as $smiley_set)
869
		{
870
			cache_put_data('parsing_smileys_' . $smiley_set['raw_path'], null, 480);
871
			cache_put_data('posting_smileys_' . $smiley_set['raw_path'], null, 480);
872
		}
873
874
		// No errors? Out of here!
875
		redirectexit('action=admin;area=smileys;sa=editsmileys');
876
	}
877
878
	$context['selected_set'] = $modSettings['smiley_sets_default'];
879
880
	// Get all possible filenames for the smileys.
881
	$context['filenames'] = array();
882
	if ($context['smileys_dir_found'])
883
	{
884
		foreach ($context['smiley_sets'] as $smiley_set)
885
		{
886
			if (!file_exists($context['smileys_dir'] . '/' . $smiley_set['raw_path']))
887
				continue;
888
889
			$dir = dir($context['smileys_dir'] . '/' . $smiley_set['raw_path']);
890
			while ($entry = $dir->read())
891
			{
892
				$entry_info = pathinfo($entry);
893
				if (empty($entry_info['filename']) || empty($entry_info['extension']))
894
					continue;
895
				if (empty($context['filenames'][$smiley_set['path']][strtolower($entry_info['filename'])]) && in_array(strtolower($entry_info['extension']), $allowedTypes))
896
					$context['filenames'][$smiley_set['path']][strtolower($entry_info['filename'])] = array(
897
						'id' => $smcFunc['htmlspecialchars']($entry),
898
						'selected' => $entry_info['filename'] == 'smiley' && $smiley_set['path'] == $context['selected_set'],
899
					);
900
			}
901
			$dir->close();
902
			ksort($context['filenames'][$smiley_set['path']]);
903
		}
904
		ksort($context['filenames']);
905
	}
906
907
	// Create a new smiley from scratch.
908
	$context['current_smiley'] = array(
909
		'id' => 0,
910
		'code' => '',
911
		'filename' => $context['filenames'][$smcFunc['htmlspecialchars']($context['selected_set'])]['smiley']['id'],
912
		'description' => $txt['smileys_default_description'],
913
		'location' => 0,
914
		'is_new' => true,
915
	);
916
}
917
918
/**
919
 * Add, remove, edit smileys.
920
 */
921
function EditSmileys()
922
{
923
	global $modSettings, $context, $txt, $boarddir;
924
	global $smcFunc, $scripturl, $sourcedir;
925
926
	// Force the correct tab to be displayed.
927
	$context[$context['admin_menu_name']]['current_subsection'] = 'editsmileys';
928
929
	$allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg');
930
	$known_sets = explode(',', $modSettings['smiley_sets_known']);
931
932
	// Submitting a form?
933
	if (isset($_POST['smiley_save']) || isset($_POST['smiley_action']) || isset($_POST['deletesmiley']))
934
	{
935
		checkSession();
936
937
		// Changing the selected smileys?
938
		if (isset($_POST['smiley_action']) && !empty($_POST['checked_smileys']))
939
		{
940
			foreach ($_POST['checked_smileys'] as $id => $smiley_id)
941
				$_POST['checked_smileys'][$id] = (int) $smiley_id;
942
943
			if ($_POST['smiley_action'] == 'delete')
944
			{
945
				$smcFunc['db_query']('', '
946
					DELETE FROM {db_prefix}smileys
947
					WHERE id_smiley IN ({array_int:checked_smileys})',
948
					array(
949
						'checked_smileys' => $_POST['checked_smileys'],
950
					)
951
				);
952
				$smcFunc['db_query']('', '
953
					DELETE FROM {db_prefix}smiley_files
954
					WHERE id_smiley IN ({array_int:checked_smileys})',
955
					array(
956
						'checked_smileys' => $_POST['checked_smileys'],
957
					)
958
				);
959
			}
960
			// Changing the status of the smiley?
961
			else
962
			{
963
				// Check it's a valid type.
964
				$displayTypes = array(
965
					'post' => 0,
966
					'hidden' => 1,
967
					'popup' => 2
968
				);
969
				if (isset($displayTypes[$_POST['smiley_action']]))
970
					$smcFunc['db_query']('', '
971
						UPDATE {db_prefix}smileys
972
						SET hidden = {int:display_type}
973
						WHERE id_smiley IN ({array_int:checked_smileys})',
974
						array(
975
							'checked_smileys' => $_POST['checked_smileys'],
976
							'display_type' => $displayTypes[$_POST['smiley_action']],
977
						)
978
					);
979
			}
980
		}
981
		// Create/modify a smiley.
982
		elseif (isset($_POST['smiley']))
983
		{
984
			// Is it a delete?
985
			if (!empty($_POST['deletesmiley']) && $_POST['smiley'] == (int) $_POST['smiley'])
986
			{
987
				$smcFunc['db_query']('', '
988
					DELETE FROM {db_prefix}smileys
989
					WHERE id_smiley = {int:current_smiley}',
990
					array(
991
						'current_smiley' => $_POST['smiley'],
992
					)
993
				);
994
				$smcFunc['db_query']('', '
995
					DELETE FROM {db_prefix}smiley_files
996
					WHERE id_smiley = {int:current_smiley}',
997
					array(
998
						'current_smiley' => $_POST['smiley'],
999
					)
1000
				);
1001
			}
1002
			// Otherwise an edit.
1003
			else
1004
			{
1005
				$_POST['smiley'] = (int) $_POST['smiley'];
1006
				$_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
1007
				$_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
1008
1009
				// Make sure some code was entered.
1010
				if (empty($_POST['smiley_code']))
1011
					fatal_lang_error('smiley_has_no_code');
1012
1013
				// Make sure all submitted filenames are clean.
1014
				$filenames = array();
1015
				foreach ($_POST['smiley_filename'] as $posted_set => $posted_filename)
1016
				{
1017
					$posted_set = htmltrim__recursive($posted_set);
1018
					$posted_filename = htmltrim__recursive($posted_filename);
1019
1020
					// Make sure the set already exists.
1021
					if (!in_array($posted_set, $known_sets))
1022
						continue;
1023
1024
					$filenames[$posted_set] = pathinfo($posted_filename, PATHINFO_BASENAME);
1025
				}
1026
				// Fill in any missing sets.
1027
				foreach ($known_sets as $known_set)
1028
				{
1029
					// Uh-oh, something is missing.
1030
					if (empty($filenames[$known_set]))
1031
					{
1032
						// Try to make it the same as the default set.
1033
						if (!empty($filenames[$modSettings['smiley_sets_default']]))
1034
							$filenames[$known_set] = $filenames[$modSettings['smiley_sets_default']];
1035
						// As a last resort, just try to get whatever the first one is.
1036
						elseif (!empty($filenames))
1037
							$filenames[$known_set] = reset($filenames);
1038
					}
1039
				}
1040
1041
				// Can't do anything without filenames for the smileys.
1042
				if (empty($filenames))
1043
					fatal_lang_error('smiley_has_no_filename');
1044
1045
				// Check whether the new code has duplicates. It should be unique.
1046
				$request = $smcFunc['db_query']('', '
1047
					SELECT id_smiley
1048
					FROM {db_prefix}smileys
1049
					WHERE code = {raw:mysql_binary_type} {string:smiley_code}' . (empty($_POST['smiley']) ? '' : '
1050
						AND id_smiley != {int:current_smiley}'),
1051
					array(
1052
						'current_smiley' => $_POST['smiley'],
1053
						'mysql_binary_type' => $smcFunc['db_title'] == 'MySQL' ? 'BINARY' : '',
1054
						'smiley_code' => $_POST['smiley_code'],
1055
					)
1056
				);
1057
				if ($smcFunc['db_num_rows']($request) > 0)
1058
					fatal_lang_error('smiley_not_unique');
1059
				$smcFunc['db_free_result']($request);
1060
1061
				$smcFunc['db_query']('', '
1062
					UPDATE {db_prefix}smileys
1063
					SET
1064
						code = {string:smiley_code},
1065
						description = {string:smiley_description},
1066
						hidden = {int:smiley_location}
1067
					WHERE id_smiley = {int:current_smiley}',
1068
					array(
1069
						'smiley_location' => $_POST['smiley_location'],
1070
						'current_smiley' => $_POST['smiley'],
1071
						'smiley_code' => $_POST['smiley_code'],
1072
						'smiley_description' => $_POST['smiley_description'],
1073
					)
1074
				);
1075
1076
				// Update filename info in the smiley_files table
1077
				$inserts = array();
1078
				foreach ($filenames as $set => $filename)
1079
					$inserts[] = array($_POST['smiley'], $set, $filename);
1080
1081
				$smcFunc['db_insert']('replace',
1082
					'{db_prefix}smiley_files',
1083
					array(
1084
						'id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48',
1085
					),
1086
					$inserts,
1087
					array('id_smiley', 'smiley_set')
1088
				);
1089
			}
1090
		}
1091
1092
		foreach ($known_sets as $smiley_set)
1093
		{
1094
			cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
1095
			cache_put_data('posting_smileys_' . $smiley_set, null, 480);
1096
		}
1097
	}
1098
1099
	// Load all known smiley sets.
1100
	$context['smiley_sets'] = array_flip($known_sets);
1101
	$set_names = explode("\n", $modSettings['smiley_sets_names']);
1102
	foreach ($context['smiley_sets'] as $set => $i)
1103
		$context['smiley_sets'][$set] = array(
1104
			'id' => $i,
1105
			'raw_path' => $set,
1106
			'path' => $smcFunc['htmlspecialchars']($set),
1107
			'name' => $smcFunc['htmlspecialchars']($set_names[$i]),
1108
			'selected' => $set == $modSettings['smiley_sets_default']
1109
		);
1110
1111
	// Prepare overview of all (custom) smileys.
1112
	if ($context['sub_action'] == 'editsmileys')
1113
	{
1114
		// Determine the language specific sort order of smiley locations.
1115
		$smiley_locations = array(
1116
			$txt['smileys_location_form'],
1117
			$txt['smileys_location_hidden'],
1118
			$txt['smileys_location_popup'],
1119
		);
1120
		asort($smiley_locations);
1121
1122
		// Create a list of options for selecting smiley sets.
1123
		$smileyset_option_list = '
1124
			<select name="set" onchange="changeSet(this.options[this.selectedIndex].value);">';
1125
		foreach ($context['smiley_sets'] as $smiley_set)
1126
			$smileyset_option_list .= '
1127
				<option value="' . $smiley_set['path'] . '"' . ($modSettings['smiley_sets_default'] == $smiley_set['path'] ? ' selected' : '') . '>' . $smiley_set['name'] . '</option>';
1128
		$smileyset_option_list .= '
1129
			</select>';
1130
1131
		$listOptions = array(
1132
			'id' => 'smiley_list',
1133
			'title' => $txt['smileys_edit'],
1134
			'items_per_page' => 40,
1135
			'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys',
1136
			'default_sort_col' => 'filename',
1137
			'get_items' => array(
1138
				'function' => 'list_getSmileys',
1139
			),
1140
			'get_count' => array(
1141
				'function' => 'list_getNumSmileys',
1142
			),
1143
			'no_items_label' => $txt['smileys_no_entries'],
1144
			'columns' => array(
1145
				'picture' => array(
1146
					'data' => array(
1147
						'function' => function($rowData) use ($scripturl, $modSettings, $context)
0 ignored issues
show
The import $context is not used and could be removed.

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

Loading history...
1148
						{
1149
							$return = '';
1150
1151
							foreach ($rowData['filename_array'] as $set => $filename)
1152
							{
1153
								$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>';
1154
							}
1155
1156
							return $return;
1157
						},
1158
						'class' => 'centercol',
1159
					),
1160
				),
1161
				'smileys_code' => array(
1162
					'header' => array(
1163
						'value' => $txt['smileys_code'],
1164
					),
1165
					'data' => array(
1166
						'db_htmlsafe' => 'code',
1167
					),
1168
					'sort' => array(
1169
						'default' => 'code',
1170
						'reverse' => 'code DESC',
1171
					),
1172
				),
1173
				'filename' => array(
1174
					'header' => array(
1175
						'value' => $txt['smileys_filename'],
1176
					),
1177
					'data' => array(
1178
						'function' => function($rowData)
1179
						{
1180
							$return = '<span style="display:none">' . $rowData['filename'] . '</span>';
1181
1182
							foreach ($rowData['filename_array'] as $set => $filename)
1183
								$return .= ' <span class="smiley_set ' . $set . '">' . $filename . '</span>';
1184
1185
							return $return;
1186
						},
1187
					),
1188
					'sort' => array(
1189
						'default' => 'filename',
1190
						'reverse' => 'filename DESC',
1191
					),
1192
				),
1193
				'location' => array(
1194
					'header' => array(
1195
						'value' => $txt['smileys_location'],
1196
					),
1197
					'data' => array(
1198
						'function' => function($rowData) use ($txt)
1199
						{
1200
							if (empty($rowData['hidden']))
1201
								return $txt['smileys_location_form'];
1202
							elseif ($rowData['hidden'] == 1)
1203
								return $txt['smileys_location_hidden'];
1204
							else
1205
								return $txt['smileys_location_popup'];
1206
						},
1207
					),
1208
					'sort' => array(
1209
						'default' => $smcFunc['db_custom_order']('hidden', array_keys($smiley_locations)),
1210
						'reverse' => $smcFunc['db_custom_order']('hidden', array_keys($smiley_locations), true),
1211
					),
1212
				),
1213
				'description' => array(
1214
					'header' => array(
1215
						'value' => $txt['smileys_description'],
1216
					),
1217
					'data' => array(
1218
						'function' => function($rowData) use ($context, $txt, $modSettings, $smcFunc)
1219
						{
1220
							if (empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir']))
1221
								return $smcFunc['htmlspecialchars']($rowData['description']);
1222
1223
							// Check if there are smileys missing in some sets.
1224
							$missing_sets = array();
1225
							foreach ($context['smiley_sets'] as $smiley_set)
1226
								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']])))
1227
									$missing_sets[] = $smiley_set['path'];
1228
1229
							$description = $smcFunc['htmlspecialchars']($rowData['description']);
1230
1231
							if (!empty($missing_sets))
1232
								$description .= sprintf('<br><span class="smalltext"><strong>%1$s:</strong> %2$s</span>', $txt['smileys_not_found_in_set'], implode(', ', $missing_sets));
1233
1234
							return $description;
1235
						},
1236
					),
1237
					'sort' => array(
1238
						'default' => 'description',
1239
						'reverse' => 'description DESC',
1240
					),
1241
				),
1242
				'modify' => array(
1243
					'header' => array(
1244
						'value' => $txt['smileys_modify'],
1245
						'class' => 'centercol',
1246
					),
1247
					'data' => array(
1248
						'sprintf' => array(
1249
							'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=%1$d">' . $txt['smileys_modify'] . '</a>',
1250
							'params' => array(
1251
								'id_smiley' => false,
1252
							),
1253
						),
1254
						'class' => 'centercol',
1255
					),
1256
				),
1257
				'check' => array(
1258
					'header' => array(
1259
						'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
1260
						'class' => 'centercol',
1261
					),
1262
					'data' => array(
1263
						'sprintf' => array(
1264
							'format' => '<input type="checkbox" name="checked_smileys[]" value="%1$d">',
1265
							'params' => array(
1266
								'id_smiley' => false,
1267
							),
1268
						),
1269
						'class' => 'centercol',
1270
					),
1271
				),
1272
			),
1273
			'form' => array(
1274
				'href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys',
1275
				'name' => 'smileyForm',
1276
			),
1277
			'additional_rows' => array(
1278
				array(
1279
					'position' => 'above_column_headers',
1280
					'value' => $smileyset_option_list,
1281
					'class' => 'righttext',
1282
				),
1283
				array(
1284
					'position' => 'below_table_data',
1285
					'value' => '
1286
						<select name="smiley_action" onchange="makeChanges(this.value);">
1287
							<option value="-1">' . $txt['smileys_with_selected'] . ':</option>
1288
							<option value="-1" disabled>--------------</option>
1289
							<option value="hidden">' . $txt['smileys_make_hidden'] . '</option>
1290
							<option value="post">' . $txt['smileys_show_on_post'] . '</option>
1291
							<option value="popup">' . $txt['smileys_show_on_popup'] . '</option>
1292
							<option value="delete">' . $txt['smileys_remove'] . '</option>
1293
						</select>
1294
						<noscript>
1295
							<input type="submit" name="perform_action" value="' . $txt['go'] . '" class="button">
1296
						</noscript>',
1297
					'class' => 'righttext',
1298
				),
1299
			),
1300
			'javascript' => '
1301
				function makeChanges(action)
1302
				{
1303
					if (action == \'-1\')
1304
						return false;
1305
					else if (action == \'delete\')
1306
					{
1307
						if (confirm(\'' . $txt['smileys_confirm'] . '\'))
1308
							document.forms.smileyForm.submit();
1309
					}
1310
					else
1311
						document.forms.smileyForm.submit();
1312
					return true;
1313
				}
1314
				function changeSet(newSet)
1315
				{
1316
					$(".smiley_set").hide();
1317
					$(".smiley_set." + newSet).show();
1318
				}',
1319
		);
1320
1321
		require_once($sourcedir . '/Subs-List.php');
1322
		createList($listOptions);
1323
1324
		// The list is the only thing to show, so make it the main template.
1325
		$context['default_list'] = 'smiley_list';
1326
		$context['sub_template'] = 'show_list';
1327
1328
		addInlineJavaScript("\n\t" . 'changeSet("' . $modSettings['smiley_sets_default'] . '");', true);
1329
	}
1330
	// Modifying smileys.
1331
	elseif ($context['sub_action'] == 'modifysmiley')
1332
	{
1333
		$context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
1334
		$context['smileys_dir_found'] = is_dir($context['smileys_dir']);
1335
1336
		$context['selected_set'] = $modSettings['smiley_sets_default'];
1337
1338
		$request = $smcFunc['db_query']('', '
1339
			SELECT s.id_smiley AS id, s.code, f.filename, f.smiley_set, s.description, s.hidden AS location
1340
			FROM {db_prefix}smileys AS s
1341
				JOIN {db_prefix}smiley_files AS f ON (s.id_smiley = f.id_smiley)
1342
			WHERE s.id_smiley = {int:current_smiley}',
1343
			array(
1344
				'current_smiley' => (int) $_REQUEST['smiley'],
1345
			)
1346
		);
1347
		while ($row = $smcFunc['db_fetch_assoc']($request))
1348
		{
1349
			// The empty() bit is for just in case the default set is missing this smiley
1350
			if ($row['smiley_set'] == $context['selected_set'] || empty($context['current_smiley']))
1351
				$context['current_smiley'] = $row;
1352
1353
			$filenames[$row['smiley_set']] = $row['filename'];
1354
		}
1355
		$smcFunc['db_free_result']($request);
1356
1357
		if (empty($context['current_smiley']))
1358
			fatal_lang_error('smiley_not_found');
1359
1360
		$context['current_smiley']['code'] = $smcFunc['htmlspecialchars']($context['current_smiley']['code']);
1361
		$context['current_smiley']['description'] = $smcFunc['htmlspecialchars']($context['current_smiley']['description']);
1362
		$context['current_smiley']['filename'] = $smcFunc['htmlspecialchars']($context['current_smiley']['filename']);
1363
1364
		// Get all possible filenames for the smileys.
1365
		$context['filenames'] = array();
1366
		$context['missing_sets'] = array();
1367
		if ($context['smileys_dir_found'])
1368
		{
1369
			foreach ($context['smiley_sets'] as $smiley_set)
1370
			{
1371
				if (!file_exists($context['smileys_dir'] . '/' . $smiley_set['raw_path']))
1372
					continue;
1373
1374
				// No file currently defined for this smiley in this set? That's no good.
1375
				if (!isset($filenames[$smiley_set['raw_path']]))
1376
				{
1377
					$context['missing_sets'][] = $smiley_set['raw_path'];
1378
					$context['filenames'][$smiley_set['path']][''] = array('id' => '', 'selected' => true, 'disabled' => true);
1379
				}
1380
1381
				$dir = dir($context['smileys_dir'] . '/' . $smiley_set['raw_path']);
1382
				while ($entry = $dir->read())
1383
				{
1384
					if (empty($context['filenames'][$smiley_set['path']][$entry]) && in_array(pathinfo($entry, PATHINFO_EXTENSION), $allowedTypes))
1385
						$context['filenames'][$smiley_set['path']][$entry] = array(
1386
							'id' => $smcFunc['htmlspecialchars']($entry),
1387
							'selected' => isset($filenames[$smiley_set['raw_path']]) && strtolower($entry) == strtolower($filenames[$smiley_set['raw_path']]),
1388
							'disabled' => false,
1389
						);
1390
				}
1391
				$dir->close();
1392
				ksort($context['filenames'][$smiley_set['path']]);
1393
			}
1394
			ksort($context['filenames']);
1395
		}
1396
	}
1397
}
1398
1399
/**
1400
 * Callback function for createList().
1401
 *
1402
 * @param int $start The item to start with (not used here)
1403
 * @param int $items_per_page The number of items to show per page (not used here)
1404
 * @param string $sort A string indicating how to sort the results
1405
 * @return array An array of info about the smileys
1406
 */
1407
function list_getSmileys($start, $items_per_page, $sort)
1408
{
1409
	global $smcFunc, $modSettings;
1410
1411
	$request = $smcFunc['db_query']('', '
1412
		SELECT s.id_smiley, s.code, f.filename, f.smiley_set, s.description, s.smiley_row, s.smiley_order, s.hidden
1413
		FROM {db_prefix}smileys AS s
1414
			JOIN {db_prefix}smiley_files AS f ON (s.id_smiley = f.id_smiley)
1415
		ORDER BY {raw:sort}',
1416
		array(
1417
			'sort' => $sort,
1418
		)
1419
	);
1420
	$smileys = array();
1421
	while ($row = $smcFunc['db_fetch_assoc']($request))
1422
	{
1423
		if (empty($smileys[$row['id_smiley']]))
1424
		{
1425
			$smileys[$row['id_smiley']] = $row;
1426
			unset($smileys[$row['id_smiley']]['smiley_set']);
1427
			$smileys[$row['id_smiley']]['filename_array'] = array($row['smiley_set'] => $row['filename']);
1428
		}
1429
		else
1430
		{
1431
			$smileys[$row['id_smiley']]['filename_array'][$row['smiley_set']] = $row['filename'];
1432
		}
1433
1434
		// Use the filename for the default set as the primary filename for this smiley
1435
		if (isset($smileys[$row['id_smiley']]['filename_array'][$modSettings['smiley_sets_default']]))
1436
			$smileys[$row['id_smiley']]['filename'] = $smileys[$row['id_smiley']]['filename_array'][$modSettings['smiley_sets_default']];
1437
		else
1438
			$smileys[$row['id_smiley']]['filename'] = reset($smileys[$row['id_smiley']]['filename_array']);
1439
	}
1440
	$smcFunc['db_free_result']($request);
1441
1442
	return $smileys;
1443
}
1444
1445
/**
1446
 * Callback function for createList().
1447
 *
1448
 * @return int The number of smileys
1449
 */
1450
function list_getNumSmileys()
1451
{
1452
	global $smcFunc;
1453
1454
	$request = $smcFunc['db_query']('', '
1455
		SELECT COUNT(*)
1456
		FROM {db_prefix}smileys',
1457
		array()
1458
	);
1459
	list($numSmileys) = $smcFunc['db_fetch_row'];
1460
	$smcFunc['db_free_result']($request);
1461
1462
	return $numSmileys;
1463
}
1464
1465
/**
1466
 * Allows to edit smileys order.
1467
 */
1468
function EditSmileyOrder()
1469
{
1470
	global $context, $txt, $smcFunc, $modSettings;
1471
1472
	// Move smileys to another position.
1473
	if (isset($_REQUEST['reorder']))
1474
	{
1475
		checkSession('get');
1476
1477
		$_GET['location'] = empty($_GET['location']) || $_GET['location'] != 'popup' ? 0 : 2;
1478
		$_GET['source'] = empty($_GET['source']) ? 0 : (int) $_GET['source'];
1479
1480
		if (empty($_GET['source']))
1481
			fatal_lang_error('smiley_not_found');
1482
1483
		if (!empty($_GET['after']))
1484
		{
1485
			$_GET['after'] = (int) $_GET['after'];
1486
1487
			$request = $smcFunc['db_query']('', '
1488
				SELECT smiley_row, smiley_order, hidden
1489
				FROM {db_prefix}smileys
1490
				WHERE hidden = {int:location}
1491
					AND id_smiley = {int:after_smiley}',
1492
				array(
1493
					'location' => $_GET['location'],
1494
					'after_smiley' => $_GET['after'],
1495
				)
1496
			);
1497
			if ($smcFunc['db_num_rows']($request) != 1)
1498
				fatal_lang_error('smiley_not_found');
1499
			list ($smiley_row, $smiley_order, $smileyLocation) = $smcFunc['db_fetch_row']($request);
1500
			$smcFunc['db_free_result']($request);
1501
		}
1502
		else
1503
		{
1504
			$smiley_row = (int) $_GET['row'];
1505
			$smiley_order = -1;
1506
			$smileyLocation = (int) $_GET['location'];
1507
		}
1508
1509
		$smcFunc['db_query']('', '
1510
			UPDATE {db_prefix}smileys
1511
			SET smiley_order = smiley_order + 1
1512
			WHERE hidden = {int:new_location}
1513
				AND smiley_row = {int:smiley_row}
1514
				AND smiley_order > {int:smiley_order}',
1515
			array(
1516
				'new_location' => $_GET['location'],
1517
				'smiley_row' => $smiley_row,
1518
				'smiley_order' => $smiley_order,
1519
			)
1520
		);
1521
1522
		$smcFunc['db_query']('', '
1523
			UPDATE {db_prefix}smileys
1524
			SET
1525
				smiley_order = {int:smiley_order} + 1,
1526
				smiley_row = {int:smiley_row},
1527
				hidden = {int:new_location}
1528
			WHERE id_smiley = {int:current_smiley}',
1529
			array(
1530
				'smiley_order' => $smiley_order,
1531
				'smiley_row' => $smiley_row,
1532
				'new_location' => $smileyLocation,
1533
				'current_smiley' => $_GET['source'],
1534
			)
1535
		);
1536
1537
		foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set)
1538
		{
1539
			cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
1540
			cache_put_data('posting_smileys_' . $smiley_set, null, 480);
1541
		}
1542
	}
1543
1544
	$request = $smcFunc['db_query']('', '
1545
		SELECT s.id_smiley, s.code, f.filename, s.description, s.smiley_row, s.smiley_order, s.hidden
1546
		FROM {db_prefix}smileys AS s
1547
			JOIN {db_prefix}smiley_files AS f ON (s.id_smiley = f.id_smiley)
1548
		WHERE s.hidden != {int:popup}
1549
			AND f.smiley_set = {string:smiley_set}
1550
		ORDER BY s.smiley_order, s.smiley_row',
1551
		array(
1552
			'popup' => 1,
1553
			'smiley_set' => $modSettings['smiley_sets_default'],
1554
		)
1555
	);
1556
	$context['smileys'] = array(
1557
		'postform' => array(
1558
			'rows' => array(),
1559
		),
1560
		'popup' => array(
1561
			'rows' => array(),
1562
		),
1563
	);
1564
	while ($row = $smcFunc['db_fetch_assoc']($request))
1565
	{
1566
		$location = empty($row['hidden']) ? 'postform' : 'popup';
1567
		$context['smileys'][$location]['rows'][$row['smiley_row']][] = array(
1568
			'id' => $row['id_smiley'],
1569
			'code' => $smcFunc['htmlspecialchars']($row['code']),
1570
			'filename' => $smcFunc['htmlspecialchars']($row['filename']),
1571
			'description' => $smcFunc['htmlspecialchars']($row['description']),
1572
			'row' => $row['smiley_row'],
1573
			'order' => $row['smiley_order'],
1574
			'selected' => !empty($_REQUEST['move']) && $_REQUEST['move'] == $row['id_smiley'],
1575
		);
1576
	}
1577
	$smcFunc['db_free_result']($request);
1578
1579
	$context['move_smiley'] = empty($_REQUEST['move']) ? 0 : (int) $_REQUEST['move'];
1580
1581
	// Make sure all rows are sequential.
1582
	foreach (array_keys($context['smileys']) as $location)
1583
		$context['smileys'][$location] = array(
1584
			'id' => $location,
1585
			'title' => $location == 'postform' ? $txt['smileys_location_form'] : $txt['smileys_location_popup'],
1586
			'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $txt['smileys_location_popup_description'],
1587
			'last_row' => count($context['smileys'][$location]['rows']),
1588
			'rows' => array_values($context['smileys'][$location]['rows']),
1589
		);
1590
1591
	// Check & fix smileys that are not ordered properly in the database.
1592
	foreach (array_keys($context['smileys']) as $location)
1593
	{
1594
		foreach ($context['smileys'][$location]['rows'] as $id => $smiley_row)
1595
		{
1596
			// Fix empty rows if any.
1597
			if ($id != $smiley_row[0]['row'])
1598
			{
1599
				$smcFunc['db_query']('', '
1600
					UPDATE {db_prefix}smileys
1601
					SET smiley_row = {int:new_row}
1602
					WHERE smiley_row = {int:current_row}
1603
						AND hidden = {int:location}',
1604
					array(
1605
						'new_row' => $id,
1606
						'current_row' => $smiley_row[0]['row'],
1607
						'location' => $location == 'postform' ? '0' : '2',
1608
					)
1609
				);
1610
				// Only change the first row value of the first smiley (we don't need the others :P).
1611
				$context['smileys'][$location]['rows'][$id][0]['row'] = $id;
1612
			}
1613
			// Make sure the smiley order is always sequential.
1614
			foreach ($smiley_row as $order_id => $smiley)
1615
				if ($order_id != $smiley['order'])
1616
					$smcFunc['db_query']('', '
1617
						UPDATE {db_prefix}smileys
1618
						SET smiley_order = {int:new_order}
1619
						WHERE id_smiley = {int:current_smiley}',
1620
						array(
1621
							'new_order' => $order_id,
1622
							'current_smiley' => $smiley['id'],
1623
						)
1624
					);
1625
		}
1626
	}
1627
1628
	foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set)
1629
	{
1630
		cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
1631
		cache_put_data('posting_smileys_' . $smiley_set, null, 480);
1632
	}
1633
}
1634
1635
/**
1636
 * Install a smiley set.
1637
 */
1638
function InstallSmileySet()
1639
{
1640
	global $sourcedir, $boarddir, $packagesdir, $modSettings, $smcFunc, $scripturl, $context, $txt, $user_info;
1641
1642
	isAllowedTo('manage_smileys');
1643
	checkSession('request');
1644
	// One of these two may be necessary
1645
	loadLanguage('Errors');
1646
	loadLanguage('Packages');
1647
1648
	require_once($sourcedir . '/Subs-Package.php');
1649
1650
	// Installing unless proven otherwise
1651
	$testing = false;
1652
1653
	if (isset($_REQUEST['set_gz']))
1654
	{
1655
		$base_name = strtr(basename($_REQUEST['set_gz']), ':/', '-_');
1656
		$name = $smcFunc['htmlspecialchars'](strtok(basename($_REQUEST['set_gz']), '.'));
1657
		$context['filename'] = $base_name;
1658
1659
		// Check that the smiley is from simplemachines.org, for now... maybe add mirroring later.
1660
		// @ TODO: Our current xml files serve http links.  Allowing both for now until we serve https.
1661
		if (preg_match('~^https?://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['set_gz']) == 0 || strpos($_REQUEST['set_gz'], 'dlattach') !== false)
1662
			fatal_lang_error('not_on_simplemachines');
1663
1664
		$destination = $packagesdir . '/' . $base_name;
1665
1666
		if (file_exists($destination))
1667
			fatal_lang_error('package_upload_error_exists');
1668
1669
		// Let's copy it to the Packages directory
1670
		file_put_contents($destination, fetch_web_data($_REQUEST['set_gz']));
1671
		$testing = true;
1672
	}
1673
	elseif (isset($_REQUEST['package']))
1674
	{
1675
		$base_name = basename($_REQUEST['package']);
1676
		$name = $smcFunc['htmlspecialchars'](strtok(basename($_REQUEST['package']), '.'));
1677
		$context['filename'] = $base_name;
1678
1679
		$destination = $packagesdir . '/' . basename($_REQUEST['package']);
1680
	}
1681
1682
	if (empty($destination) || !file_exists($destination))
1683
		fatal_lang_error('package_no_file', false);
1684
1685
	// Make sure temp directory exists and is empty.
1686
	if (file_exists($packagesdir . '/temp'))
1687
		deltree($packagesdir . '/temp', false);
1688
1689
	if (!mktree($packagesdir . '/temp', 0755))
1690
	{
1691
		deltree($packagesdir . '/temp', false);
1692
		if (!mktree($packagesdir . '/temp', 0777))
1693
		{
1694
			deltree($packagesdir . '/temp', false);
1695
			// @todo not sure about url in destination_url
1696
			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));
1697
1698
			deltree($packagesdir . '/temp', false);
1699
			if (!mktree($packagesdir . '/temp', 0777))
1700
				fatal_lang_error('package_cant_download', false);
1701
		}
1702
	}
1703
1704
	$extracted = read_tgz_file($destination, $packagesdir . '/temp');
1705
	if (!$extracted)
1706
		fatal_lang_error('packageget_unable', false, array('https://custom.simplemachines.org/mods/index.php?action=search;type=12;basic_search=' . $name));
1707
	if ($extracted && !file_exists($packagesdir . '/temp/package-info.xml'))
1708
		foreach ($extracted as $file)
1709
			if (basename($file['filename']) == 'package-info.xml')
1710
			{
1711
				$base_path = dirname($file['filename']) . '/';
1712
				break;
1713
			}
1714
1715
	if (!isset($base_path))
1716
		$base_path = '';
1717
1718
	if (!file_exists($packagesdir . '/temp/' . $base_path . 'package-info.xml'))
1719
		fatal_lang_error('package_get_error_missing_xml', false);
1720
1721
	$smileyInfo = getPackageInfo($context['filename']);
1722
	if (!is_array($smileyInfo))
1723
		fatal_lang_error($smileyInfo);
1724
1725
	// See if it is installed?
1726
	$request = $smcFunc['db_query']('', '
1727
		SELECT version, themes_installed, db_changes
1728
		FROM {db_prefix}log_packages
1729
		WHERE package_id = {string:current_package}
1730
			AND install_state != {int:not_installed}
1731
		ORDER BY time_installed DESC
1732
		LIMIT 1',
1733
		array(
1734
			'not_installed' => 0,
1735
			'current_package' => $smileyInfo['id'],
1736
		)
1737
	);
1738
1739
	if ($smcFunc['db_num_rows']($request) > 0)
1740
		fatal_lang_error('package_installed_warning1');
1741
1742
	// Everything is fine, now it's time to do something
1743
	$actions = parsePackageInfo($smileyInfo['xml'], true, 'install');
1744
1745
	$context['post_url'] = $scripturl . '?action=admin;area=smileys;sa=install;package=' . $base_name;
1746
	$context['has_failure'] = false;
1747
	$context['actions'] = array();
1748
	$context['ftp_needed'] = false;
1749
1750
	foreach ($actions as $action)
1751
	{
1752
		if ($action['type'] == 'readme' || $action['type'] == 'license')
1753
		{
1754
			$type = 'package_' . $action['type'];
1755
			if (file_exists($packagesdir . '/temp/' . $base_path . $action['filename']))
1756
				$context[$type] = $smcFunc['htmlspecialchars'](trim(file_get_contents($packagesdir . '/temp/' . $base_path . $action['filename']), "\n\r"));
1757
			elseif (file_exists($action['filename']))
1758
				$context[$type] = $smcFunc['htmlspecialchars'](trim(file_get_contents($action['filename']), "\n\r"));
1759
1760
			if (!empty($action['parse_bbc']))
1761
			{
1762
				require_once($sourcedir . '/Subs-Post.php');
1763
				preparsecode($context[$type]);
1764
				$context[$type] = parse_bbc($context[$type]);
1765
			}
1766
			else
1767
				$context[$type] = nl2br($context[$type]);
1768
1769
			continue;
1770
		}
1771
		elseif ($action['type'] == 'require-dir')
1772
		{
1773
			// Do this one...
1774
			$thisAction = array(
1775
				'type' => $txt['package_extract'] . ' ' . ($action['type'] == 'require-dir' ? $txt['package_tree'] : $txt['package_file']),
1776
				'action' => $smcFunc['htmlspecialchars'](strtr($action['destination'], array($boarddir => '.')))
1777
			);
1778
1779
			$file = $packagesdir . '/temp/' . $base_path . $action['filename'];
1780
			if (isset($action['filename']) && (!file_exists($file) || !is_writable(dirname($action['destination']))))
1781
			{
1782
				$context['has_failure'] = true;
1783
1784
				$thisAction += array(
1785
					'description' => $txt['package_action_error'],
1786
					'failed' => true,
1787
				);
1788
			}
1789
			// @todo None given?
1790
			if (empty($thisAction['description']))
1791
				$thisAction['description'] = isset($action['description']) ? $action['description'] : '';
1792
1793
			$context['actions'][] = $thisAction;
1794
		}
1795
		elseif ($action['type'] == 'credits')
1796
		{
1797
			// Time to build the billboard
1798
			$credits_tag = array(
1799
				'url' => $action['url'],
1800
				'license' => $action['license'],
1801
				'copyright' => $action['copyright'],
1802
				'title' => $action['title'],
1803
			);
1804
		}
1805
	}
1806
1807
	if ($testing)
1808
	{
1809
		$context['sub_template'] = 'view_package';
1810
		$context['uninstalling'] = false;
1811
		$context['is_installed'] = false;
1812
		$context['package_name'] = $smileyInfo['name'];
1813
		loadTemplate('Packages');
1814
	}
1815
	// Do the actual install
1816
	else
1817
	{
1818
		// @TODO Does this call have side effects? ($actions is not used)
1819
		$actions = parsePackageInfo($smileyInfo['xml'], false, 'install');
1820
		foreach ($context['actions'] as $action)
1821
		{
1822
			updateSettings(array(
1823
				'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . basename($action['action']),
1824
				'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . $smileyInfo['name'] . (count($context['actions']) > 1 ? ' ' . (!empty($action['description']) ? $smcFunc['htmlspecialchars']($action['description']) : basename($action['action'])) : ''),
1825
			));
1826
		}
1827
1828
		package_flush_cache();
1829
1830
		// Credits tag?
1831
		$credits_tag = (empty($credits_tag)) ? '' : $smcFunc['json_encode']($credits_tag);
1832
		$smcFunc['db_insert']('',
1833
			'{db_prefix}log_packages',
1834
			array(
1835
				'filename' => 'string', 'name' => 'string', 'package_id' => 'string', 'version' => 'string',
1836
				'id_member_installed' => 'int', 'member_installed' => 'string', 'time_installed' => 'int',
1837
				'install_state' => 'int', 'failed_steps' => 'string', 'themes_installed' => 'string',
1838
				'member_removed' => 'int', 'db_changes' => 'string', 'credits' => 'string',
1839
			),
1840
			array(
1841
				$smileyInfo['filename'], $smileyInfo['name'], $smileyInfo['id'], $smileyInfo['version'],
1842
				$user_info['id'], $user_info['name'], time(),
1843
				1, '', '',
1844
				0, '', $credits_tag,
1845
			),
1846
			array('id_install')
1847
		);
1848
1849
		logAction('install_package', array('package' => $smcFunc['htmlspecialchars']($smileyInfo['name']), 'version' => $smcFunc['htmlspecialchars']($smileyInfo['version'])), 'admin');
1850
1851
		foreach (explode(',', $modSettings['smiley_sets_known']) as $smiley_set)
1852
		{
1853
			cache_put_data('parsing_smileys_' . $smiley_set, null, 480);
1854
			cache_put_data('posting_smileys_' . $smiley_set, null, 480);
1855
		}
1856
	}
1857
1858
	if (file_exists($packagesdir . '/temp'))
1859
		deltree($packagesdir . '/temp');
1860
1861
	if (!$testing)
1862
		redirectexit('action=admin;area=smileys');
1863
}
1864
1865
/**
1866
 * A function to import new smileys from an existing directory into the database.
1867
 *
1868
 * @param string $smileyPath The path to the directory to import smileys from
1869
 * @param bool $create Whether or not to make brand new smileys for files that don't match any existing smileys
1870
 */
1871
function ImportSmileys($smileyPath, $create = false)
1872
{
1873
	global $modSettings, $smcFunc;
1874
1875
	if (empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir'] . '/' . $smileyPath))
1876
		fatal_lang_error('smiley_set_unable_to_import');
1877
1878
	$allowedTypes = array('gif', 'png', 'jpg', 'jpeg', 'tiff', 'svg');
1879
	$known_sets = explode(',', $modSettings['smiley_sets_known']);
1880
	sort($known_sets);
1881
1882
	// Get the smileys in the folder
1883
	$smileys = array();
1884
	$dir = dir($modSettings['smileys_dir'] . '/' . $smileyPath);
1885
	while ($entry = $dir->read())
1886
	{
1887
		$pathinfo = pathinfo($entry);
1888
		if (empty($pathinfo['filename']) || empty($pathinfo['extension']))
1889
			continue;
1890
		if (in_array($pathinfo['extension'], $allowedTypes) && $pathinfo['filename'] != 'blank' && strlen($pathinfo['basename']) <= 48)
1891
			$smiley_files[strtolower($pathinfo['basename'])] = $pathinfo['basename'];
1892
	}
1893
	$dir->close();
1894
1895
	// Get the smileys that are already in the database.
1896
	$existing_smileys = array();
1897
	$request = $smcFunc['db_query']('', '
1898
		SELECT id_smiley, smiley_set, filename
1899
		FROM {db_prefix}smiley_files',
1900
		array()
1901
	);
1902
	while ($row = $smcFunc['db_fetch_assoc']($request))
1903
		$existing_smileys[pathinfo($row['filename'], PATHINFO_FILENAME)][$row['id_smiley']][] = $row['smiley_set'];
1904
	$smcFunc['db_free_result']($request);
1905
1906
	// Filter $smiley_files down to just the ones not already in the database.
1907
	$to_unset = array();
1908
	$to_fix = array();
1909
	foreach ($smiley_files as $key => $smiley_file)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $smiley_files does not seem to be defined for all execution paths leading up to this point.
Loading history...
1910
	{
1911
		$smiley_name = pathinfo($smiley_file, PATHINFO_FILENAME);
1912
1913
		// A brand new one
1914
		if (empty($existing_smileys[$smiley_name]))
1915
			continue;
1916
1917
		// A file with this name is already being used for at least one smiley, so we have more work to do...
1918
		foreach ($existing_smileys[$smiley_name] as $existing_id => $existing_sets)
1919
		{
1920
			$to_unset[$key][$existing_id] = false;
1921
1922
			sort($existing_sets);
1923
1924
			// Already done
1925
			if ($existing_sets === $known_sets)
1926
				$to_unset[$key][$existing_id] = true;
1927
1928
			// Used in some sets but not others
1929
			else
1930
			{
1931
				// Do the other sets have some other file already defined?
1932
				foreach ($existing_smileys as $file => $info)
1933
				{
1934
					foreach ($info as $info_id => $info_sets)
1935
					{
1936
						if ($existing_id == $info_id)
1937
							$existing_sets = array_unique(array_merge($existing_sets, $info_sets));
1938
					}
1939
				}
1940
				sort($existing_sets);
1941
1942
				// If every set already has a file for this smiley, we can skip it
1943
				if ($known_sets == $existing_sets)
1944
					$to_unset[$key][$existing_id] = true;
1945
1946
				// Need to add the file for these sets
1947
				else
1948
					$to_fix[$key][$existing_id] = array_diff($known_sets, $existing_sets);
1949
			}
1950
		}
1951
	}
1952
1953
	// Fix any sets with missing files
1954
	// This part handles files for pre-existing smileys in a newly created smiley set
1955
	$inserts = array();
1956
	foreach ($to_fix as $key => $ids)
1957
	{
1958
		foreach ($ids as $id_smiley => $sets_missing)
1959
		{
1960
			// Find the file we need to copy to the other sets
1961
			if (file_exists($modSettings['smileys_dir'] . '/' . $smileyPath . '/' . $smiley_files[$key]))
1962
				$p = $smileyPath;
1963
			else
1964
			{
1965
				foreach (array_diff($known_sets, $sets_missing) as $set)
1966
				{
1967
					if (file_exists($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key]))
1968
					{
1969
						$p = $set;
1970
						break;
1971
					}
1972
				}
1973
			}
1974
1975
			foreach ($sets_missing as $set)
1976
			{
1977
				if ($set !== $p)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $p does not seem to be defined for all execution paths leading up to this point.
Loading history...
1978
				{
1979
					// Copy the file into the set's folder
1980
					copy($modSettings['smileys_dir'] . '/' . $p . '/' . $smiley_files[$key], $modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key]);
1981
					smf_chmod($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key], 0644);
1982
				}
1983
1984
				// Double-check that everything went as expected
1985
				if (!file_exists($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_files[$key]))
1986
					continue;
1987
1988
				// Update the database
1989
				$inserts[] = array($id_smiley, $set, $smiley_files[$key]);
1990
1991
				// This isn't a new smiley
1992
				$to_unset[$key][$id_smiley] = true;
1993
			}
1994
		}
1995
	}
1996
1997
	// Remove anything that isn't actually new from our list of files
1998
	foreach ($to_unset as $key => $ids)
1999
	{
2000
		if (array_reduce($ids, function ($carry, $item) { return $carry * $item; }, true) == true)
2001
			unset($smiley_files[$key]);
2002
	}
2003
2004
	// We only create brand new smileys if asked.
2005
	if (empty($create))
2006
		$smiley_files = array();
2007
2008
	// New smileys go at the end of the list
2009
	$request = $smcFunc['db_query']('', '
2010
		SELECT MAX(smiley_order)
2011
		FROM {db_prefix}smileys
2012
		WHERE hidden = {int:postform}
2013
			AND smiley_row = {int:first_row}',
2014
		array(
2015
			'postform' => 0,
2016
			'first_row' => 0,
2017
		)
2018
	);
2019
	list ($smiley_order) = $smcFunc['db_fetch_row']($request);
2020
	$smcFunc['db_free_result']($request);
2021
2022
	// This part handles brand new smileys that don't exist in any set
2023
	$new_smileys = array();
2024
	foreach ($smiley_files as $key => $smiley_file)
2025
	{
2026
		// Ensure every set has a file to use for the new smiley
2027
		foreach ($known_sets as $set)
2028
		{
2029
			unset($basename);
2030
2031
			if ($smileyPath != $set)
2032
			{
2033
				// Check whether any similarly named files exist in the other set's directory
2034
				$similar_files = glob($modSettings['smileys_dir'] . '/' . $set . '/' . pathinfo($smiley_file, PATHINFO_FILENAME) . '.{' . implode(',', $allowedTypes) . '}', GLOB_BRACE);
2035
2036
				// If there's a similarly named file already there, use it
2037
				if (!empty($similar_files))
2038
				{
2039
					// Prefer an exact match if there is one
2040
					foreach ($similar_files as $similar_file)
2041
					{
2042
						if (basename($similar_file) == $smiley_file)
2043
							$basename = $smiley_file;
2044
					}
2045
2046
					// Same name, different extension
2047
					if (empty($basename))
2048
						$basename = basename(reset($similar_files));
2049
				}
2050
				// Otherwise, copy the image to the other set's directory
2051
				else
2052
				{
2053
					copy($modSettings['smileys_dir'] . '/' . $smileyPath . '/' . $smiley_file, $modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_file);
2054
					smf_chmod($modSettings['smileys_dir'] . '/' . $set . '/' . $smiley_file, 0644);
2055
2056
					$basename = $smiley_file;
2057
				}
2058
2059
				// Double-check that everything went as expected
2060
				if (empty($basename) || !file_exists($modSettings['smileys_dir'] . '/' . $set . '/' . $basename))
2061
					continue;
2062
			}
2063
			else
2064
				$basename = $smiley_file;
2065
2066
			$new_smileys[$key]['files'][$set] = $basename;
2067
		}
2068
2069
		$new_smileys[$key]['info'] = array(':' . pathinfo($smiley_file, PATHINFO_FILENAME) . ':', pathinfo($smiley_file, PATHINFO_FILENAME), 0, ++$smiley_order);
2070
	}
2071
2072
	// Add the info for any new smileys to the database
2073
	foreach ($new_smileys as $new_smiley)
2074
	{
2075
		$new_id_smiley = $smcFunc['db_insert']('',
2076
			'{db_prefix}smileys',
2077
			array(
2078
				'code' => 'string-30', 'description' => 'string-80', 'smiley_row' => 'int', 'smiley_order' => 'int',
2079
			),
2080
			$new_smiley['info'],
2081
			array('id_smiley'),
2082
			1
2083
		);
2084
2085
		// We'll also need to add filename info to the smiley_files table
2086
		foreach ($new_smiley['files'] as $set => $filename)
2087
			$inserts[] = array($new_id_smiley, $set, $filename);
2088
	}
2089
2090
	// Finally, update the smiley_files table with all our new files
2091
	if (!empty($inserts))
2092
	{
2093
		$smcFunc['db_insert']('replace',
2094
			'{db_prefix}smiley_files',
2095
			array(
2096
				'id_smiley' => 'int', 'smiley_set' => 'string-48', 'filename' => 'string-48',
2097
			),
2098
			$inserts,
2099
			array('id_smiley', 'smiley_set')
2100
		);
2101
2102
		foreach ($known_sets as $set)
2103
		{
2104
			cache_put_data('parsing_smileys_' . $set, null, 480);
2105
			cache_put_data('posting_smileys_' . $set, null, 480);
2106
		}
2107
	}
2108
}
2109
2110
/**
2111
 * Handles editing message icons
2112
 */
2113
function EditMessageIcons()
2114
{
2115
	global $context, $settings, $txt;
2116
	global $smcFunc, $scripturl, $sourcedir;
2117
2118
	// Get a list of icons.
2119
	$context['icons'] = array();
2120
	$request = $smcFunc['db_query']('', '
2121
		SELECT m.id_icon, m.title, m.filename, m.icon_order, m.id_board, b.name AS board_name
2122
		FROM {db_prefix}message_icons AS m
2123
			LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
2124
		WHERE ({query_see_board} OR b.id_board IS NULL)
2125
		ORDER BY m.icon_order',
2126
		array(
2127
		)
2128
	);
2129
	$last_icon = 0;
2130
	$trueOrder = 0;
2131
	while ($row = $smcFunc['db_fetch_assoc']($request))
2132
	{
2133
		$context['icons'][$row['id_icon']] = array(
2134
			'id' => $row['id_icon'],
2135
			'title' => $row['title'],
2136
			'filename' => $row['filename'],
2137
			'image_url' => $settings[file_exists($settings['theme_dir'] . '/images/post/' . $row['filename'] . '.png') ? 'actual_images_url' : 'default_images_url'] . '/post/' . $row['filename'] . '.png',
2138
			'board_id' => $row['id_board'],
2139
			'board' => empty($row['board_name']) ? $txt['icons_edit_icons_all_boards'] : $row['board_name'],
2140
			'order' => $row['icon_order'],
2141
			'true_order' => $trueOrder++,
2142
			'after' => $last_icon,
2143
		);
2144
		$last_icon = $row['id_icon'];
2145
	}
2146
	$smcFunc['db_free_result']($request);
2147
2148
	// Submitting a form?
2149
	if (isset($_POST['icons_save']) || isset($_POST['delete']))
2150
	{
2151
		checkSession();
2152
2153
		// Deleting icons?
2154
		if (isset($_POST['delete']) && !empty($_POST['checked_icons']))
2155
		{
2156
			$deleteIcons = array();
2157
			foreach ($_POST['checked_icons'] as $icon)
2158
				$deleteIcons[] = (int) $icon;
2159
2160
			// Do the actual delete!
2161
			$smcFunc['db_query']('', '
2162
				DELETE FROM {db_prefix}message_icons
2163
				WHERE id_icon IN ({array_int:icon_list})',
2164
				array(
2165
					'icon_list' => $deleteIcons,
2166
				)
2167
			);
2168
		}
2169
		// Editing/Adding an icon?
2170
		elseif ($context['sub_action'] == 'editicon' && isset($_GET['icon']))
2171
		{
2172
			$_GET['icon'] = (int) $_GET['icon'];
2173
2174
			// Do some preperation with the data... like check the icon exists *somewhere*
2175
			if (strpos($_POST['icon_filename'], '.png') !== false)
2176
				$_POST['icon_filename'] = substr($_POST['icon_filename'], 0, -4);
2177
			if (!file_exists($settings['default_theme_dir'] . '/images/post/' . $_POST['icon_filename'] . '.png'))
2178
				fatal_lang_error('icon_not_found');
2179
			// There is a 16 character limit on message icons...
2180
			elseif (strlen($_POST['icon_filename']) > 16)
2181
				fatal_lang_error('icon_name_too_long');
2182
			elseif ($_POST['icon_location'] == $_GET['icon'] && !empty($_GET['icon']))
2183
				fatal_lang_error('icon_after_itself');
2184
2185
			// First do the sorting... if this is an edit reduce the order of everything after it by one ;)
2186
			if ($_GET['icon'] != 0)
2187
			{
2188
				$oldOrder = $context['icons'][$_GET['icon']]['true_order'];
2189
				foreach ($context['icons'] as $id => $data)
2190
					if ($data['true_order'] > $oldOrder)
2191
						$context['icons'][$id]['true_order']--;
2192
			}
2193
2194
			// If there are no existing icons and this is a new one, set the id to 1 (mainly for non-mysql)
2195
			if (empty($_GET['icon']) && empty($context['icons']))
2196
				$_GET['icon'] = 1;
2197
2198
			// Get the new order.
2199
			$newOrder = $_POST['icon_location'] == 0 ? 0 : $context['icons'][$_POST['icon_location']]['true_order'] + 1;
2200
			// Do the same, but with the one that used to be after this icon, done to avoid conflict.
2201
			foreach ($context['icons'] as $id => $data)
2202
				if ($data['true_order'] >= $newOrder)
2203
					$context['icons'][$id]['true_order']++;
2204
2205
			// Finally set the current icon's position!
2206
			$context['icons'][$_GET['icon']]['true_order'] = $newOrder;
2207
2208
			// Simply replace the existing data for the other bits.
2209
			$context['icons'][$_GET['icon']]['title'] = $_POST['icon_description'];
2210
			$context['icons'][$_GET['icon']]['filename'] = $_POST['icon_filename'];
2211
			$context['icons'][$_GET['icon']]['board_id'] = (int) $_POST['icon_board'];
2212
2213
			// Do a huge replace ;)
2214
			$iconInsert = array();
2215
			$iconInsert_new = array();
2216
			foreach ($context['icons'] as $id => $icon)
2217
			{
2218
				if ($id != 0)
2219
				{
2220
					$iconInsert[] = array($id, $icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']);
2221
				}
2222
				else
2223
				{
2224
					$iconInsert_new[] = array($icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']);
2225
				}
2226
			}
2227
2228
			$smcFunc['db_insert']('replace',
2229
				'{db_prefix}message_icons',
2230
				array('id_icon' => 'int', 'id_board' => 'int', 'title' => 'string-80', 'filename' => 'string-80', 'icon_order' => 'int'),
2231
				$iconInsert,
2232
				array('id_icon')
2233
			);
2234
2235
			if (!empty($iconInsert_new))
2236
			{
2237
				$smcFunc['db_insert']('insert',
2238
					'{db_prefix}message_icons',
2239
					array('id_board' => 'int', 'title' => 'string-80', 'filename' => 'string-80', 'icon_order' => 'int'),
2240
					$iconInsert_new,
2241
					array('id_icon')
2242
				);
2243
			}
2244
		}
2245
2246
		// Unless we're adding a new thing, we'll escape
2247
		if (!isset($_POST['add']))
2248
			redirectexit('action=admin;area=smileys;sa=editicons');
2249
	}
2250
2251
	$context[$context['admin_menu_name']]['current_subsection'] = 'editicons';
2252
2253
	$listOptions = array(
2254
		'id' => 'message_icon_list',
2255
		'title' => $txt['icons_edit_message_icons'],
2256
		'base_href' => $scripturl . '?action=admin;area=smileys;sa=editicons',
2257
		'get_items' => array(
2258
			'function' => 'list_getMessageIcons',
2259
		),
2260
		'no_items_label' => $txt['icons_no_entries'],
2261
		'columns' => array(
2262
			'icon' => array(
2263
				'data' => array(
2264
					'function' => function($rowData) use ($settings, $smcFunc)
2265
					{
2266
						$images_url = $settings[file_exists(sprintf('%1$s/images/post/%2$s.png', $settings['theme_dir'], $rowData['filename'])) ? 'actual_images_url' : 'default_images_url'];
2267
						return sprintf('<img src="%1$s/post/%2$s.png" alt="%3$s">', $images_url, $rowData['filename'], $smcFunc['htmlspecialchars']($rowData['title']));
2268
					},
2269
					'class' => 'centercol',
2270
				),
2271
			),
2272
			'filename' => array(
2273
				'header' => array(
2274
					'value' => $txt['smileys_filename'],
2275
				),
2276
				'data' => array(
2277
					'sprintf' => array(
2278
						'format' => '%1$s.png',
2279
						'params' => array(
2280
							'filename' => true,
2281
						),
2282
					),
2283
				),
2284
			),
2285
			'description' => array(
2286
				'header' => array(
2287
					'value' => $txt['smileys_description'],
2288
				),
2289
				'data' => array(
2290
					'db_htmlsafe' => 'title',
2291
				),
2292
			),
2293
			'board' => array(
2294
				'header' => array(
2295
					'value' => $txt['icons_board'],
2296
				),
2297
				'data' => array(
2298
					'function' => function($rowData) use ($txt)
2299
					{
2300
						return empty($rowData['board_name']) ? $txt['icons_edit_icons_all_boards'] : $rowData['board_name'];
2301
					},
2302
				),
2303
			),
2304
			'modify' => array(
2305
				'header' => array(
2306
					'value' => $txt['smileys_modify'],
2307
					'class' => 'centercol',
2308
				),
2309
				'data' => array(
2310
					'sprintf' => array(
2311
						'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=editicon;icon=%1$s">' . $txt['smileys_modify'] . '</a>',
2312
						'params' => array(
2313
							'id_icon' => false,
2314
						),
2315
					),
2316
					'class' => 'centercol',
2317
				),
2318
			),
2319
			'check' => array(
2320
				'header' => array(
2321
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);">',
2322
					'class' => 'centercol',
2323
				),
2324
				'data' => array(
2325
					'sprintf' => array(
2326
						'format' => '<input type="checkbox" name="checked_icons[]" value="%1$d">',
2327
						'params' => array(
2328
							'id_icon' => false,
2329
						),
2330
					),
2331
					'class' => 'centercol',
2332
				),
2333
			),
2334
		),
2335
		'form' => array(
2336
			'href' => $scripturl . '?action=admin;area=smileys;sa=editicons',
2337
		),
2338
		'additional_rows' => array(
2339
			array(
2340
				'position' => 'below_table_data',
2341
				'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>',
2342
			),
2343
		),
2344
	);
2345
2346
	require_once($sourcedir . '/Subs-List.php');
2347
	createList($listOptions);
2348
2349
	// If we're adding/editing an icon we'll need a list of boards
2350
	if ($context['sub_action'] == 'editicon' || isset($_POST['add']))
2351
	{
2352
		// Force the sub_template just in case.
2353
		$context['sub_template'] = 'editicon';
2354
2355
		$context['new_icon'] = !isset($_GET['icon']);
2356
2357
		// Get the properties of the current icon from the icon list.
2358
		if (!$context['new_icon'])
2359
			$context['icon'] = $context['icons'][$_GET['icon']];
2360
2361
		// Get a list of boards needed for assigning this icon to a specific board.
2362
		$boardListOptions = array(
2363
			'use_permissions' => true,
2364
			'selected_board' => isset($context['icon']['board_id']) ? $context['icon']['board_id'] : 0,
2365
		);
2366
		require_once($sourcedir . '/Subs-MessageIndex.php');
2367
		$context['categories'] = getBoardList($boardListOptions);
2368
	}
2369
}
2370
2371
/**
2372
 * Callback function for createList().
2373
 *
2374
 * @param int $start The item to start with (not used here)
2375
 * @param int $items_per_page The number of items to display per page (not used here)
2376
 * @param string $sort A string indicating how to sort the items (not used here)
2377
 * @return array An array of information about message icons
2378
 */
2379
function list_getMessageIcons($start, $items_per_page, $sort)
2380
{
2381
	global $smcFunc;
2382
2383
	$request = $smcFunc['db_query']('', '
2384
		SELECT m.id_icon, m.title, m.filename, m.icon_order, m.id_board, b.name AS board_name
2385
		FROM {db_prefix}message_icons AS m
2386
			LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
2387
		WHERE ({query_see_board} OR b.id_board IS NULL)
2388
		ORDER BY m.icon_order',
2389
		array()
2390
	);
2391
2392
	$message_icons = array();
2393
	while ($row = $smcFunc['db_fetch_assoc']($request))
2394
		$message_icons[] = $row;
2395
	$smcFunc['db_free_result']($request);
2396
2397
	return $message_icons;
2398
}
2399
2400
?>