comparison forum/Sources/ManageSmileys.php @ 76:e3e11437ecea website

Add forum code
author Chris Cannam
date Sun, 07 Jul 2013 11:25:48 +0200
parents
children
comparison
equal deleted inserted replaced
75:72f59aa7e503 76:e3e11437ecea
1 <?php
2
3 /**
4 * Simple Machines Forum (SMF)
5 *
6 * @package SMF
7 * @author Simple Machines http://www.simplemachines.org
8 * @copyright 2011 Simple Machines
9 * @license http://www.simplemachines.org/about/smf/license.php BSD
10 *
11 * @version 2.0
12 */
13
14 if (!defined('SMF'))
15 die('Hacking attempt...');
16
17 /* // !!!
18
19 void ManageSmileys()
20 // !!!
21
22 void EditSmileySettings()
23 // !!!
24
25 void EditSmileySets()
26 // !!!
27
28 void AddSmiley()
29 // !!!
30
31 void EditSmileys()
32 // !!!
33
34 void EditSmileyOrder()
35 // !!!
36
37 void InstallSmileySet()
38 // !!!
39
40 void ImportSmileys($smileyPath)
41 // !!!
42
43 void sortSmileyTable()
44 // !!!
45 */
46
47 function ManageSmileys()
48 {
49 global $context, $txt, $scripturl, $modSettings;
50
51 isAllowedTo('manage_smileys');
52
53 loadLanguage('ManageSmileys');
54 loadTemplate('ManageSmileys');
55
56 $subActions = array(
57 'addsmiley' => 'AddSmiley',
58 'editicon' => 'EditMessageIcons',
59 'editicons' => 'EditMessageIcons',
60 'editsets' => 'EditSmileySets',
61 'editsmileys' => 'EditSmileys',
62 'import' => 'EditSmileySets',
63 'modifyset' => 'EditSmileySets',
64 'modifysmiley' => 'EditSmileys',
65 'setorder' => 'EditSmileyOrder',
66 'settings' => 'EditSmileySettings',
67 'install' => 'InstallSmileySet'
68 );
69
70 // Default the sub-action to 'edit smiley settings'.
71 $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'editsets';
72
73 $context['page_title'] = $txt['smileys_manage'];
74 $context['sub_action'] = $_REQUEST['sa'];
75 $context['sub_template'] = $context['sub_action'];
76
77 // Load up all the tabs...
78 $context[$context['admin_menu_name']]['tab_data'] = array(
79 'title' => $txt['smileys_manage'],
80 'help' => 'smileys',
81 'description' => $txt['smiley_settings_explain'],
82 'tabs' => array(
83 'editsets' => array(
84 'description' => $txt['smiley_editsets_explain'],
85 ),
86 'addsmiley' => array(
87 'description' => $txt['smiley_addsmiley_explain'],
88 ),
89 'editsmileys' => array(
90 'description' => $txt['smiley_editsmileys_explain'],
91 ),
92 'setorder' => array(
93 'description' => $txt['smiley_setorder_explain'],
94 ),
95 'editicons' => array(
96 'description' => $txt['icons_edit_icons_explain'],
97 ),
98 'settings' => array(
99 'description' => $txt['smiley_settings_explain'],
100 ),
101 ),
102 );
103
104 // Some settings may not be enabled, disallow these from the tabs as appropriate.
105 if (empty($modSettings['messageIcons_enable']))
106 $context[$context['admin_menu_name']]['tab_data']['tabs']['editicons']['disabled'] = true;
107 if (empty($modSettings['smiley_enable']))
108 {
109 $context[$context['admin_menu_name']]['tab_data']['tabs']['addsmiley']['disabled'] = true;
110 $context[$context['admin_menu_name']]['tab_data']['tabs']['editsmileys']['disabled'] = true;
111 $context[$context['admin_menu_name']]['tab_data']['tabs']['setorder']['disabled'] = true;
112 }
113
114 // Call the right function for this sub-acton.
115 $subActions[$_REQUEST['sa']]();
116 }
117
118 function EditSmileySettings($return_config = false)
119 {
120 global $modSettings, $context, $settings, $txt, $boarddir, $sourcedir, $scripturl;
121
122 // The directories...
123 $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
124 $context['smileys_dir_found'] = is_dir($context['smileys_dir']);
125
126 // Get the names of the smiley sets.
127 $smiley_sets = explode(',', $modSettings['smiley_sets_known']);
128 $set_names = explode("\n", $modSettings['smiley_sets_names']);
129
130 $smiley_context = array();
131 foreach ($smiley_sets as $i => $set)
132 $smiley_context[$set] = $set_names[$i];
133
134 // All the settings for the page...
135 $config_vars = array(
136 array('title', 'settings'),
137 // Inline permissions.
138 array('permissions', 'manage_smileys'),
139 '',
140 array('select', 'smiley_sets_default', $smiley_context),
141 array('check', 'smiley_sets_enable'),
142 array('check', 'smiley_enable', 'subtext' => $txt['smileys_enable_note']),
143 array('text', 'smileys_url'),
144 array('text', 'smileys_dir', 'invalid' => !$context['smileys_dir_found']),
145 '',
146 // Message icons.
147 array('check', 'messageIcons_enable', 'subtext' => $txt['setting_messageIcons_enable_note']),
148 );
149
150 if ($return_config)
151 return $config_vars;
152
153 // Setup the basics of the settings template.
154 require_once($sourcedir . '/ManageServer.php');
155 $context['sub_template'] = 'show_settings';
156
157 // Finish up the form...
158 $context['post_url'] = $scripturl . '?action=admin;area=smileys;save;sa=settings';
159 $context['permissions_excluded'] = array(-1);
160
161 // Saving the settings?
162 if (isset($_GET['save']))
163 {
164 checkSession();
165
166 // Validate the smiley set name.
167 $_POST['smiley_sets_default'] = empty($smiley_context[$_POST['smiley_sets_default']]) ? 'default' : $_POST['smiley_sets_default'];
168
169 // Make sure that the smileys are in the right order after enabling them.
170 if (isset($_POST['smiley_enable']))
171 sortSmileyTable();
172
173 saveDBSettings($config_vars);
174
175 cache_put_data('parsing_smileys', null, 480);
176 cache_put_data('posting_smileys', null, 480);
177
178 redirectexit('action=admin;area=smileys;sa=settings');
179 }
180
181 prepareDBSettingContext($config_vars);
182 }
183
184 function EditSmileySets()
185 {
186 global $modSettings, $context, $settings, $txt, $boarddir;
187 global $smcFunc, $scripturl, $sourcedir;
188
189 // Set the right tab to be selected.
190 $context[$context['admin_menu_name']]['current_subsection'] = 'editsets';
191
192 // They must've been submitted a form.
193 if (isset($_POST[$context['session_var']]))
194 {
195 checkSession();
196
197 // Delete selected smiley sets.
198 if (!empty($_POST['delete']) && !empty($_POST['smiley_set']))
199 {
200 $set_paths = explode(',', $modSettings['smiley_sets_known']);
201 $set_names = explode("\n", $modSettings['smiley_sets_names']);
202 foreach ($_POST['smiley_set'] as $id => $val)
203 if (isset($set_paths[$id], $set_names[$id]) && !empty($id))
204 unset($set_paths[$id], $set_names[$id]);
205
206 updateSettings(array(
207 'smiley_sets_known' => implode(',', $set_paths),
208 'smiley_sets_names' => implode("\n", $set_names),
209 'smiley_sets_default' => in_array($modSettings['smiley_sets_default'], $set_paths) ? $modSettings['smiley_sets_default'] : $set_paths[0],
210 ));
211
212 cache_put_data('parsing_smileys', null, 480);
213 cache_put_data('posting_smileys', null, 480);
214 }
215 // Add a new smiley set.
216 elseif (!empty($_POST['add']))
217 $context['sub_action'] = 'modifyset';
218 // Create or modify a smiley set.
219 elseif (isset($_POST['set']))
220 {
221 $set_paths = explode(',', $modSettings['smiley_sets_known']);
222 $set_names = explode("\n", $modSettings['smiley_sets_names']);
223
224 // Create a new smiley set.
225 if ($_POST['set'] == -1 && isset($_POST['smiley_sets_path']))
226 {
227 if (in_array($_POST['smiley_sets_path'], $set_paths))
228 fatal_lang_error('smiley_set_already_exists');
229
230 updateSettings(array(
231 'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . $_POST['smiley_sets_path'],
232 'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . $_POST['smiley_sets_name'],
233 'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path'],
234 ));
235 }
236 // Modify an existing smiley set.
237 else
238 {
239 // Make sure the smiley set exists.
240 if (!isset($set_paths[$_POST['set']]) || !isset($set_names[$_POST['set']]))
241 fatal_lang_error('smiley_set_not_found');
242
243 // Make sure the path is not yet used by another smileyset.
244 if (in_array($_POST['smiley_sets_path'], $set_paths) && $_POST['smiley_sets_path'] != $set_paths[$_POST['set']])
245 fatal_lang_error('smiley_set_path_already_used');
246
247 $set_paths[$_POST['set']] = $_POST['smiley_sets_path'];
248 $set_names[$_POST['set']] = $_POST['smiley_sets_name'];
249 updateSettings(array(
250 'smiley_sets_known' => implode(',', $set_paths),
251 'smiley_sets_names' => implode("\n", $set_names),
252 'smiley_sets_default' => empty($_POST['smiley_sets_default']) ? $modSettings['smiley_sets_default'] : $_POST['smiley_sets_path']
253 ));
254 }
255
256 // The user might have checked to also import smileys.
257 if (!empty($_POST['smiley_sets_import']))
258 ImportSmileys($_POST['smiley_sets_path']);
259
260 cache_put_data('parsing_smileys', null, 480);
261 cache_put_data('posting_smileys', null, 480);
262 }
263 }
264
265 // Load all available smileysets...
266 $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
267 $set_names = explode("\n", $modSettings['smiley_sets_names']);
268 foreach ($context['smiley_sets'] as $i => $set)
269 $context['smiley_sets'][$i] = array(
270 'id' => $i,
271 'path' => htmlspecialchars($set),
272 'name' => htmlspecialchars($set_names[$i]),
273 'selected' => $set == $modSettings['smiley_sets_default']
274 );
275
276 // Importing any smileys from an existing set?
277 if ($context['sub_action'] == 'import')
278 {
279 checkSession('get');
280 $_GET['set'] = (int) $_GET['set'];
281
282 // Sanity check - then import.
283 if (isset($context['smiley_sets'][$_GET['set']]))
284 ImportSmileys(un_htmlspecialchars($context['smiley_sets'][$_GET['set']]['path']));
285
286 // Force the process to continue.
287 $context['sub_action'] = 'modifyset';
288 $context['sub_template'] = 'modifyset';
289 }
290 // If we're modifying or adding a smileyset, some context info needs to be set.
291 if ($context['sub_action'] == 'modifyset')
292 {
293 $_GET['set'] = !isset($_GET['set']) ? -1 : (int) $_GET['set'];
294 if ($_GET['set'] == -1 || !isset($context['smiley_sets'][$_GET['set']]))
295 $context['current_set'] = array(
296 'id' => '-1',
297 'path' => '',
298 'name' => '',
299 'selected' => false,
300 'is_new' => true,
301 );
302 else
303 {
304 $context['current_set'] = &$context['smiley_sets'][$_GET['set']];
305 $context['current_set']['is_new'] = false;
306
307 // Calculate whether there are any smileys in the directory that can be imported.
308 if (!empty($modSettings['smiley_enable']) && !empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']))
309 {
310 $smileys = array();
311 $dir = dir($modSettings['smileys_dir'] . '/' . $context['current_set']['path']);
312 while ($entry = $dir->read())
313 {
314 if (in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
315 $smileys[strtolower($entry)] = $entry;
316 }
317 $dir->close();
318
319 // Exclude the smileys that are already in the database.
320 $request = $smcFunc['db_query']('', '
321 SELECT filename
322 FROM {db_prefix}smileys
323 WHERE filename IN ({array_string:smiley_list})',
324 array(
325 'smiley_list' => $smileys,
326 )
327 );
328 while ($row = $smcFunc['db_fetch_assoc']($request))
329 if (isset($smileys[strtolower($row['filename'])]))
330 unset($smileys[strtolower($row['filename'])]);
331 $smcFunc['db_free_result']($request);
332
333 $context['current_set']['can_import'] = count($smileys);
334 // Setup this string to look nice.
335 $txt['smiley_set_import_multiple'] = sprintf($txt['smiley_set_import_multiple'], $context['current_set']['can_import']);
336 }
337 }
338
339 // Retrieve all potential smiley set directories.
340 $context['smiley_set_dirs'] = array();
341 if (!empty($modSettings['smileys_dir']) && is_dir($modSettings['smileys_dir']))
342 {
343 $dir = dir($modSettings['smileys_dir']);
344 while ($entry = $dir->read())
345 {
346 if (!in_array($entry, array('.', '..')) && is_dir($modSettings['smileys_dir'] . '/' . $entry))
347 $context['smiley_set_dirs'][] = array(
348 'id' => $entry,
349 'path' => $modSettings['smileys_dir'] . '/' . $entry,
350 'selectable' => $entry == $context['current_set']['path'] || !in_array($entry, explode(',', $modSettings['smiley_sets_known'])),
351 'current' => $entry == $context['current_set']['path'],
352 );
353 }
354 $dir->close();
355 }
356 }
357
358 $listOptions = array(
359 'id' => 'smiley_set_list',
360 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsets',
361 'default_sort_col' => 'default',
362 'get_items' => array(
363 'function' => 'list_getSmileySets',
364 ),
365 'get_count' => array(
366 'function' => 'list_getNumSmileySets',
367 ),
368 'columns' => array(
369 'default' => array(
370 'header' => array(
371 'value' => $txt['smiley_sets_default'],
372 ),
373 'data' => array(
374 'function' => create_function('$rowData', '
375 return $rowData[\'selected\'] ? \'<strong>*</strong>\' : \'\';
376 '),
377 'style' => 'text-align: center;',
378 ),
379 'sort' => array(
380 'default' => 'selected DESC',
381 ),
382 ),
383 'name' => array(
384 'header' => array(
385 'value' => $txt['smiley_sets_name'],
386 ),
387 'data' => array(
388 'db_htmlsafe' => 'name',
389 'class' => 'windowbg',
390 ),
391 'sort' => array(
392 'default' => 'name',
393 'reverse' => 'name DESC',
394 ),
395 ),
396 'url' => array(
397 'header' => array(
398 'value' => $txt['smiley_sets_url'],
399 ),
400 'data' => array(
401 'sprintf' => array(
402 'format' => $modSettings['smileys_url'] . '/<strong>%1$s</strong>/...',
403 'params' => array(
404 'path' => true,
405 ),
406 ),
407 'class' => 'windowbg',
408 ),
409 'sort' => array(
410 'default' => 'path',
411 'reverse' => 'path DESC',
412 ),
413 ),
414 'modify' => array(
415 'header' => array(
416 'value' => $txt['smiley_set_modify'],
417 ),
418 'data' => array(
419 'sprintf' => array(
420 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset;set=%1$d">' . $txt['smiley_set_modify'] . '</a>',
421 'params' => array(
422 'id' => true,
423 ),
424 ),
425 'style' => 'text-align: center;',
426 ),
427 ),
428 'check' => array(
429 'header' => array(
430 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
431 ),
432 'data' => array(
433 'function' => create_function('$rowData', '
434 return $rowData[\'id\'] == 0 ? \'\' : sprintf(\'<input type="checkbox" name="smiley_set[%1$d]" class="input_check" />\', $rowData[\'id\']);
435 '),
436 'style' => 'text-align: center',
437 ),
438 ),
439 ),
440 'form' => array(
441 'href' => $scripturl . '?action=admin;area=smileys;sa=editsets',
442 ),
443 'additional_rows' => array(
444 array(
445 'position' => 'below_table_data',
446 'value' => '<input type="submit" name="delete" value="' . $txt['smiley_sets_delete'] . '" onclick="return confirm(\'' . $txt['smiley_sets_confirm'] . '\');" style="float: right;" class="button_submit" /> [<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifyset' . '">' . $txt['smiley_sets_add'] . '</a>]',
447 ),
448 ),
449 );
450
451 require_once($sourcedir . '/Subs-List.php');
452 createList($listOptions);
453 }
454
455 // !!! to be moved to Subs-Smileys.
456 function list_getSmileySets($start, $items_per_page, $sort)
457 {
458 global $modSettings;
459
460 $known_sets = explode(',', $modSettings['smiley_sets_known']);
461 $set_names = explode("\n", $modSettings['smiley_sets_names']);
462 $cols = array(
463 'id' => array(),
464 'selected' => array(),
465 'path' => array(),
466 'name' => array(),
467 );
468 foreach ($known_sets as $i => $set)
469 {
470 $cols['id'][] = $i;
471 $cols['selected'][] = $i;
472 $cols['path'][] = $set;
473 $cols['name'][] = $set_names[$i];
474 }
475 $sort_flag = strpos($sort, 'DESC') === false ? SORT_ASC : SORT_DESC;
476 if (substr($sort, 0, 4) === 'name')
477 array_multisort($cols['name'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['selected'], $cols['id']);
478 elseif (substr($sort, 0, 4) === 'path')
479 array_multisort($cols['path'], $sort_flag, SORT_REGULAR, $cols['name'], $cols['selected'], $cols['id']);
480 else
481 array_multisort($cols['selected'], $sort_flag, SORT_REGULAR, $cols['path'], $cols['name'], $cols['id']);
482
483 $smiley_sets = array();
484 foreach ($cols['id'] as $i => $id)
485 $smiley_sets[] = array(
486 'id' => $id,
487 'path' => $cols['path'][$i],
488 'name' => $cols['name'][$i],
489 'selected' => $cols['path'][$i] == $modSettings['smiley_sets_default']
490 );
491
492 return $smiley_sets;
493 }
494
495 // !!! to be moved to Subs-Smileys.
496 function list_getNumSmileySets()
497 {
498 global $modSettings;
499
500 return count(explode(',', $modSettings['smiley_sets_known']));
501 }
502
503 function AddSmiley()
504 {
505 global $modSettings, $context, $settings, $txt, $boarddir, $smcFunc;
506
507 // Get a list of all known smiley sets.
508 $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
509 $context['smileys_dir_found'] = is_dir($context['smileys_dir']);
510 $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
511 $set_names = explode("\n", $modSettings['smiley_sets_names']);
512 foreach ($context['smiley_sets'] as $i => $set)
513 $context['smiley_sets'][$i] = array(
514 'id' => $i,
515 'path' => htmlspecialchars($set),
516 'name' => htmlspecialchars($set_names[$i]),
517 'selected' => $set == $modSettings['smiley_sets_default']
518 );
519
520 // Submitting a form?
521 if (isset($_POST[$context['session_var']], $_POST['smiley_code']))
522 {
523 checkSession();
524
525 // Some useful arrays... types we allow - and ports we don't!
526 $allowedTypes = array('jpeg', 'jpg', 'gif', 'png', 'bmp');
527 $disabledFiles = array('con', 'com1', 'com2', 'com3', 'com4', 'prn', 'aux', 'lpt1', '.htaccess', 'index.php');
528
529 $_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
530 $_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
531 $_POST['smiley_filename'] = htmltrim__recursive($_POST['smiley_filename']);
532
533 // Make sure some code was entered.
534 if (empty($_POST['smiley_code']))
535 fatal_lang_error('smiley_has_no_code');
536
537 // Check whether the new code has duplicates. It should be unique.
538 $request = $smcFunc['db_query']('', '
539 SELECT id_smiley
540 FROM {db_prefix}smileys
541 WHERE code = {raw:mysql_binary_statement} {string:smiley_code}',
542 array(
543 'mysql_binary_statement' => $smcFunc['db_title'] == 'MySQL' ? 'BINARY' : '',
544 'smiley_code' => $_POST['smiley_code'],
545 )
546 );
547 if ($smcFunc['db_num_rows']($request) > 0)
548 fatal_lang_error('smiley_not_unique');
549 $smcFunc['db_free_result']($request);
550
551 // If we are uploading - check all the smiley sets are writable!
552 if ($_POST['method'] != 'existing')
553 {
554 $writeErrors = array();
555 foreach ($context['smiley_sets'] as $set)
556 {
557 if (!is_writable($context['smileys_dir'] . '/' . un_htmlspecialchars($set['path'])))
558 $writeErrors[] = $set['path'];
559 }
560 if (!empty($writeErrors))
561 fatal_lang_error('smileys_upload_error_notwritable', true, array(implode(', ', $writeErrors)));
562 }
563
564 // Uploading just one smiley for all of them?
565 if (isset($_POST['sameall']) && isset($_FILES['uploadSmiley']['name']) && $_FILES['uploadSmiley']['name'] != '')
566 {
567 if (!is_uploaded_file($_FILES['uploadSmiley']['tmp_name']) || (@ini_get('open_basedir') == '' && !file_exists($_FILES['uploadSmiley']['tmp_name'])))
568 fatal_lang_error('smileys_upload_error');
569
570 // Sorry, no spaces, dots, or anything else but letters allowed.
571 $_FILES['uploadSmiley']['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['uploadSmiley']['name']);
572
573 // We only allow image files - it's THAT simple - no messing around here...
574 if (!in_array(strtolower(substr(strrchr($_FILES['uploadSmiley']['name'], '.'), 1)), $allowedTypes))
575 fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
576
577 // We only need the filename...
578 $destName = basename($_FILES['uploadSmiley']['name']);
579
580 // Make sure they aren't trying to upload a nasty file - for their own good here!
581 if (in_array(strtolower($destName), $disabledFiles))
582 fatal_lang_error('smileys_upload_error_illegal');
583
584 // Check if the file already exists... and if not move it to EVERY smiley set directory.
585 $i = 0;
586 // Keep going until we find a set the file doesn't exist in. (or maybe it exists in all of them?)
587 while (isset($context['smiley_sets'][$i]) && file_exists($context['smileys_dir'] . '/' . un_htmlspecialchars($context['smiley_sets'][$i]['path']) . '/' . $destName))
588 $i++;
589
590 // Okay, we're going to put the smiley right here, since it's not there yet!
591 if (isset($context['smiley_sets'][$i]['path']))
592 {
593 $smileyLocation = $context['smileys_dir'] . '/' . un_htmlspecialchars($context['smiley_sets'][$i]['path']) . '/' . $destName;
594 move_uploaded_file($_FILES['uploadSmiley']['tmp_name'], $smileyLocation);
595 @chmod($smileyLocation, 0644);
596
597 // Now, we want to move it from there to all the other sets.
598 for ($n = count($context['smiley_sets']); $i < $n; $i++)
599 {
600 $currentPath = $context['smileys_dir'] . '/' . un_htmlspecialchars($context['smiley_sets'][$i]['path']) . '/' . $destName;
601
602 // The file is already there! Don't overwrite it!
603 if (file_exists($currentPath))
604 continue;
605
606 // Okay, so copy the first one we made to here.
607 copy($smileyLocation, $currentPath);
608 @chmod($currentPath, 0644);
609 }
610 }
611
612 // Finally make sure it's saved correctly!
613 $_POST['smiley_filename'] = $destName;
614 }
615 // What about uploading several files?
616 elseif ($_POST['method'] != 'existing')
617 {
618 foreach ($_FILES as $name => $data)
619 {
620 if ($_FILES[$name]['name'] == '')
621 fatal_lang_error('smileys_upload_error_blank');
622
623 if (empty($newName))
624 $newName = basename($_FILES[$name]['name']);
625 elseif (basename($_FILES[$name]['name']) != $newName)
626 fatal_lang_error('smileys_upload_error_name');
627 }
628
629 foreach ($context['smiley_sets'] as $i => $set)
630 {
631 $set['name'] = un_htmlspecialchars($set['name']);
632 $set['path'] = un_htmlspecialchars($set['path']);
633
634 if (!isset($_FILES['individual_' . $set['name']]['name']) || $_FILES['individual_' . $set['name']]['name'] == '')
635 continue;
636
637 // Got one...
638 if (!is_uploaded_file($_FILES['individual_' . $set['name']]['tmp_name']) || (@ini_get('open_basedir') == '' && !file_exists($_FILES['individual_' . $set['name']]['tmp_name'])))
639 fatal_lang_error('smileys_upload_error');
640
641 // Sorry, no spaces, dots, or anything else but letters allowed.
642 $_FILES['individual_' . $set['name']]['name'] = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $_FILES['individual_' . $set['name']]['name']);
643
644 // We only allow image files - it's THAT simple - no messing around here...
645 if (!in_array(strtolower(substr(strrchr($_FILES['individual_' . $set['name']]['name'], '.'), 1)), $allowedTypes))
646 fatal_lang_error('smileys_upload_error_types', false, array(implode(', ', $allowedTypes)));
647
648 // We only need the filename...
649 $destName = basename($_FILES['individual_' . $set['name']]['name']);
650
651 // Make sure they aren't trying to upload a nasty file - for their own good here!
652 if (in_array(strtolower($destName), $disabledFiles))
653 fatal_lang_error('smileys_upload_error_illegal');
654
655 // If the file exists - ignore it.
656 $smileyLocation = $context['smileys_dir'] . '/' . $set['path'] . '/' . $destName;
657 if (file_exists($smileyLocation))
658 continue;
659
660 // Finally - move the image!
661 move_uploaded_file($_FILES['individual_' . $set['name']]['tmp_name'], $smileyLocation);
662 @chmod($smileyLocation, 0644);
663
664 // Should always be saved correctly!
665 $_POST['smiley_filename'] = $destName;
666 }
667 }
668
669 // Also make sure a filename was given.
670 if (empty($_POST['smiley_filename']))
671 fatal_lang_error('smiley_has_no_filename');
672
673 // Find the position on the right.
674 $smiley_order = '0';
675 if ($_POST['smiley_location'] != 1)
676 {
677 $request = $smcFunc['db_query']('', '
678 SELECT MAX(smiley_order) + 1
679 FROM {db_prefix}smileys
680 WHERE hidden = {int:smiley_location}
681 AND smiley_row = {int:first_row}',
682 array(
683 'smiley_location' => $_POST['smiley_location'],
684 'first_row' => 0,
685 )
686 );
687 list ($smiley_order) = $smcFunc['db_fetch_row']($request);
688 $smcFunc['db_free_result']($request);
689
690 if (empty($smiley_order))
691 $smiley_order = '0';
692 }
693 $smcFunc['db_insert']('',
694 '{db_prefix}smileys',
695 array(
696 'code' => 'string-30', 'filename' => 'string-48', 'description' => 'string-80', 'hidden' => 'int', 'smiley_order' => 'int',
697 ),
698 array(
699 $_POST['smiley_code'], $_POST['smiley_filename'], $_POST['smiley_description'], $_POST['smiley_location'], $smiley_order,
700 ),
701 array('id_smiley')
702 );
703
704 cache_put_data('parsing_smileys', null, 480);
705 cache_put_data('posting_smileys', null, 480);
706
707 // No errors? Out of here!
708 redirectexit('action=admin;area=smileys;sa=editsmileys');
709 }
710
711 $context['selected_set'] = $modSettings['smiley_sets_default'];
712
713 // Get all possible filenames for the smileys.
714 $context['filenames'] = array();
715 if ($context['smileys_dir_found'])
716 {
717 foreach ($context['smiley_sets'] as $smiley_set)
718 {
719 if (!file_exists($context['smileys_dir'] . '/' . un_htmlspecialchars($smiley_set['path'])))
720 continue;
721
722 $dir = dir($context['smileys_dir'] . '/' . un_htmlspecialchars($smiley_set['path']));
723 while ($entry = $dir->read())
724 {
725 if (!in_array($entry, $context['filenames']) && in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
726 $context['filenames'][strtolower($entry)] = array(
727 'id' => htmlspecialchars($entry),
728 'selected' => false,
729 );
730 }
731 $dir->close();
732 }
733 ksort($context['filenames']);
734 }
735
736 // Create a new smiley from scratch.
737 $context['filenames'] = array_values($context['filenames']);
738 $context['current_smiley'] = array(
739 'id' => 0,
740 'code' => '',
741 'filename' => $context['filenames'][0]['id'],
742 'description' => $txt['smileys_default_description'],
743 'location' => 0,
744 'is_new' => true,
745 );
746 }
747
748 function EditSmileys()
749 {
750 global $modSettings, $context, $settings, $txt, $boarddir;
751 global $smcFunc, $scripturl, $sourcedir;
752
753 // Force the correct tab to be displayed.
754 $context[$context['admin_menu_name']]['current_subsection'] = 'editsmileys';
755
756 // Submitting a form?
757 if (isset($_POST[$context['session_var']]))
758 {
759 checkSession();
760
761 // Changing the selected smileys?
762 if (isset($_POST['smiley_action']) && !empty($_POST['checked_smileys']))
763 {
764 foreach ($_POST['checked_smileys'] as $id => $smiley_id)
765 $_POST['checked_smileys'][$id] = (int) $smiley_id;
766
767 if ($_POST['smiley_action'] == 'delete')
768 $smcFunc['db_query']('', '
769 DELETE FROM {db_prefix}smileys
770 WHERE id_smiley IN ({array_int:checked_smileys})',
771 array(
772 'checked_smileys' => $_POST['checked_smileys'],
773 )
774 );
775 // Changing the status of the smiley?
776 else
777 {
778 // Check it's a valid type.
779 $displayTypes = array(
780 'post' => 0,
781 'hidden' => 1,
782 'popup' => 2
783 );
784 if (isset($displayTypes[$_POST['smiley_action']]))
785 $smcFunc['db_query']('', '
786 UPDATE {db_prefix}smileys
787 SET hidden = {int:display_type}
788 WHERE id_smiley IN ({array_int:checked_smileys})',
789 array(
790 'checked_smileys' => $_POST['checked_smileys'],
791 'display_type' => $displayTypes[$_POST['smiley_action']],
792 )
793 );
794 }
795 }
796 // Create/modify a smiley.
797 elseif (isset($_POST['smiley']))
798 {
799 // Is it a delete?
800 if (!empty($_POST['deletesmiley']))
801 {
802 $smcFunc['db_query']('', '
803 DELETE FROM {db_prefix}smileys
804 WHERE id_smiley = {int:current_smiley}',
805 array(
806 'current_smiley' => $_POST['smiley'],
807 )
808 );
809 }
810 // Otherwise an edit.
811 else
812 {
813 $_POST['smiley'] = (int) $_POST['smiley'];
814 $_POST['smiley_code'] = htmltrim__recursive($_POST['smiley_code']);
815 $_POST['smiley_filename'] = htmltrim__recursive($_POST['smiley_filename']);
816 $_POST['smiley_location'] = empty($_POST['smiley_location']) || $_POST['smiley_location'] > 2 || $_POST['smiley_location'] < 0 ? 0 : (int) $_POST['smiley_location'];
817
818 // Make sure some code was entered.
819 if (empty($_POST['smiley_code']))
820 fatal_lang_error('smiley_has_no_code');
821
822 // Also make sure a filename was given.
823 if (empty($_POST['smiley_filename']))
824 fatal_lang_error('smiley_has_no_filename');
825
826 // Check whether the new code has duplicates. It should be unique.
827 $request = $smcFunc['db_query']('', '
828 SELECT id_smiley
829 FROM {db_prefix}smileys
830 WHERE code = {raw:mysql_binary_type} {string:smiley_code}' . (empty($_POST['smiley']) ? '' : '
831 AND id_smiley != {int:current_smiley}'),
832 array(
833 'current_smiley' => $_POST['smiley'],
834 'mysql_binary_type' => $smcFunc['db_title'] == 'MySQL' ? 'BINARY' : '',
835 'smiley_code' => $_POST['smiley_code'],
836 )
837 );
838 if ($smcFunc['db_num_rows']($request) > 0)
839 fatal_lang_error('smiley_not_unique');
840 $smcFunc['db_free_result']($request);
841
842 $smcFunc['db_query']('', '
843 UPDATE {db_prefix}smileys
844 SET
845 code = {string:smiley_code},
846 filename = {string:smiley_filename},
847 description = {string:smiley_description},
848 hidden = {int:smiley_location}
849 WHERE id_smiley = {int:current_smiley}',
850 array(
851 'smiley_location' => $_POST['smiley_location'],
852 'current_smiley' => $_POST['smiley'],
853 'smiley_code' => $_POST['smiley_code'],
854 'smiley_filename' => $_POST['smiley_filename'],
855 'smiley_description' => $_POST['smiley_description'],
856 )
857 );
858 }
859
860 // Sort all smiley codes for more accurate parsing (longest code first).
861 sortSmileyTable();
862 }
863
864 cache_put_data('parsing_smileys', null, 480);
865 cache_put_data('posting_smileys', null, 480);
866 }
867
868 // Load all known smiley sets.
869 $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
870 $set_names = explode("\n", $modSettings['smiley_sets_names']);
871 foreach ($context['smiley_sets'] as $i => $set)
872 $context['smiley_sets'][$i] = array(
873 'id' => $i,
874 'path' => htmlspecialchars($set),
875 'name' => htmlspecialchars($set_names[$i]),
876 'selected' => $set == $modSettings['smiley_sets_default']
877 );
878
879 // Prepare overview of all (custom) smileys.
880 if ($context['sub_action'] == 'editsmileys')
881 {
882 // Determine the language specific sort order of smiley locations.
883 $smiley_locations = array(
884 $txt['smileys_location_form'],
885 $txt['smileys_location_hidden'],
886 $txt['smileys_location_popup'],
887 );
888 asort($smiley_locations);
889
890 // Create a list of options for selecting smiley sets.
891 $smileyset_option_list = '
892 <select name="set" onchange="changeSet(this.options[this.selectedIndex].value);">';
893 foreach ($context['smiley_sets'] as $smiley_set)
894 $smileyset_option_list .= '
895 <option value="' . $smiley_set['path'] . '"' . ($modSettings['smiley_sets_default'] == $smiley_set['path'] ? ' selected="selected"' : '') . '>' . $smiley_set['name'] . '</option>';
896 $smileyset_option_list .= '
897 </select>';
898
899 $listOptions = array(
900 'id' => 'smiley_list',
901 'items_per_page' => 40,
902 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys',
903 'default_sort_col' => 'filename',
904 'get_items' => array(
905 'function' => 'list_getSmileys',
906 ),
907 'get_count' => array(
908 'function' => 'list_getNumSmileys',
909 ),
910 'no_items_label' => $txt['smileys_no_entries'],
911 'columns' => array(
912 'picture' => array(
913 'data' => array(
914 'sprintf' => array(
915 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=%1$d"><img src="' . $modSettings['smileys_url'] . '/' . $modSettings['smiley_sets_default'] . '/%2$s" alt="%3$s" style="padding: 2px;" id="smiley%1$d" /><input type="hidden" name="smileys[%1$d][filename]" value="%2$s" /></a>',
916 'params' => array(
917 'id_smiley' => false,
918 'filename' => true,
919 'description' => true,
920 ),
921 ),
922 'style' => 'text-align: center;',
923 ),
924 ),
925 'code' => array(
926 'header' => array(
927 'value' => $txt['smileys_code'],
928 ),
929 'data' => array(
930 'db_htmlsafe' => 'code',
931 ),
932 'sort' => array(
933 'default' => 'code',
934 'reverse' => 'code DESC',
935 ),
936 ),
937 'filename' => array(
938 'header' => array(
939 'value' => $txt['smileys_filename'],
940 ),
941 'data' => array(
942 'db_htmlsafe' => 'filename',
943 'class' => 'windowbg',
944 ),
945 'sort' => array(
946 'default' => 'filename',
947 'reverse' => 'filename DESC',
948 ),
949 ),
950 'location' => array(
951 'header' => array(
952 'value' => $txt['smileys_location'],
953 ),
954 'data' => array(
955 'function' => create_function('$rowData', '
956 global $txt;
957
958 if (empty($rowData[\'hidden\']))
959 return $txt[\'smileys_location_form\'];
960 elseif ($rowData[\'hidden\'] == 1)
961 return $txt[\'smileys_location_hidden\'];
962 else
963 return $txt[\'smileys_location_popup\'];
964 '),
965 'class' => 'windowbg',
966 ),
967 'sort' => array(
968 'default' => 'FIND_IN_SET(hidden, \'' . implode(',', array_keys($smiley_locations)) . '\')',
969 'reverse' => 'FIND_IN_SET(hidden, \'' . implode(',', array_keys($smiley_locations)) . '\') DESC',
970 ),
971 ),
972 'tooltip' => array(
973 'header' => array(
974 'value' => $txt['smileys_description'],
975 ),
976 'data' => array(
977 'function' => create_function('$rowData', empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir']) ? '
978 return htmlspecialchars($rowData[\'description\']);
979 ' : '
980 global $context, $txt, $modSettings;
981
982 // Check if there are smileys missing in some sets.
983 $missing_sets = array();
984 foreach ($context[\'smiley_sets\'] as $smiley_set)
985 if (!file_exists(sprintf(\'%1$s/%2$s/%3$s\', $modSettings[\'smileys_dir\'], $smiley_set[\'path\'], $rowData[\'filename\'])))
986 $missing_sets[] = $smiley_set[\'path\'];
987
988 $description = htmlspecialchars($rowData[\'description\']);
989
990 if (!empty($missing_sets))
991 $description .= sprintf(\'<br /><span class="smalltext"><strong>%1$s:</strong> %2$s</span>\', $txt[\'smileys_not_found_in_set\'], implode(\', \', $missing_sets));
992
993 return $description;
994 '),
995 'class' => 'windowbg',
996 ),
997 'sort' => array(
998 'default' => 'description',
999 'reverse' => 'description DESC',
1000 ),
1001 ),
1002 'modify' => array(
1003 'header' => array(
1004 'value' => $txt['smileys_modify'],
1005 ),
1006 'data' => array(
1007 'sprintf' => array(
1008 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=modifysmiley;smiley=%1$d">' . $txt['smileys_modify'] . '</a>',
1009 'params' => array(
1010 'id_smiley' => false,
1011 ),
1012 ),
1013 'style' => 'text-align: center;',
1014 ),
1015 ),
1016 'check' => array(
1017 'header' => array(
1018 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
1019 ),
1020 'data' => array(
1021 'sprintf' => array(
1022 'format' => '<input type="checkbox" name="checked_smileys[]" value="%1$d" class="input_check" />',
1023 'params' => array(
1024 'id_smiley' => false,
1025 ),
1026 ),
1027 'style' => 'text-align: center',
1028 ),
1029 ),
1030 ),
1031 'form' => array(
1032 'href' => $scripturl . '?action=admin;area=smileys;sa=editsmileys',
1033 'name' => 'smileyForm',
1034 ),
1035 'additional_rows' => array(
1036 array(
1037 'position' => 'above_column_headers',
1038 'value' => $smileyset_option_list,
1039 'style' => 'text-align: right;',
1040 ),
1041 array(
1042 'position' => 'below_table_data',
1043 'value' => '
1044 <select name="smiley_action" onchange="makeChanges(this.value);">
1045 <option value="-1">' . $txt['smileys_with_selected'] . ':</option>
1046 <option value="-1">--------------</option>
1047 <option value="hidden">' . $txt['smileys_make_hidden'] . '</option>
1048 <option value="post">' . $txt['smileys_show_on_post'] . '</option>
1049 <option value="popup">' . $txt['smileys_show_on_popup'] . '</option>
1050 <option value="delete">' . $txt['smileys_remove'] . '</option>
1051 </select>
1052 <noscript><input type="submit" name="perform_action" value="' . $txt['go'] . '" class="button_submit" /></noscript>',
1053 'style' => 'text-align: right;',
1054 ),
1055 ),
1056 'javascript' => '
1057 function makeChanges(action)
1058 {
1059 if (action == \'-1\')
1060 return false;
1061 else if (action == \'delete\')
1062 {
1063 if (confirm(\'' . $txt['smileys_confirm'] . '\'))
1064 document.forms.smileyForm.submit();
1065 }
1066 else
1067 document.forms.smileyForm.submit();
1068 return true;
1069 }
1070 function changeSet(newSet)
1071 {
1072 var currentImage, i, knownSmileys = [];
1073
1074 if (knownSmileys.length == 0)
1075 {
1076 for (var i = 0, n = document.images.length; i < n; i++)
1077 if (document.images[i].id.substr(0, 6) == \'smiley\')
1078 knownSmileys[knownSmileys.length] = document.images[i].id.substr(6);
1079 }
1080
1081 for (i = 0; i < knownSmileys.length; i++)
1082 {
1083 currentImage = document.getElementById("smiley" + knownSmileys[i]);
1084 currentImage.src = "' . $modSettings['smileys_url'] . '/" + newSet + "/" + document.forms.smileyForm["smileys[" + knownSmileys[i] + "][filename]"].value;
1085 }
1086 }',
1087 );
1088
1089 require_once($sourcedir . '/Subs-List.php');
1090 createList($listOptions);
1091
1092 // The list is the only thing to show, so make it the main template.
1093 $context['default_list'] = 'smiley_list';
1094 $context['sub_template'] = 'show_list';
1095 }
1096 // Modifying smileys.
1097 elseif ($context['sub_action'] == 'modifysmiley')
1098 {
1099 // Get a list of all known smiley sets.
1100 $context['smileys_dir'] = empty($modSettings['smileys_dir']) ? $boarddir . '/Smileys' : $modSettings['smileys_dir'];
1101 $context['smileys_dir_found'] = is_dir($context['smileys_dir']);
1102 $context['smiley_sets'] = explode(',', $modSettings['smiley_sets_known']);
1103 $set_names = explode("\n", $modSettings['smiley_sets_names']);
1104 foreach ($context['smiley_sets'] as $i => $set)
1105 $context['smiley_sets'][$i] = array(
1106 'id' => $i,
1107 'path' => htmlspecialchars($set),
1108 'name' => htmlspecialchars($set_names[$i]),
1109 'selected' => $set == $modSettings['smiley_sets_default']
1110 );
1111
1112 $context['selected_set'] = $modSettings['smiley_sets_default'];
1113
1114 // Get all possible filenames for the smileys.
1115 $context['filenames'] = array();
1116 if ($context['smileys_dir_found'])
1117 {
1118 foreach ($context['smiley_sets'] as $smiley_set)
1119 {
1120 if (!file_exists($context['smileys_dir'] . '/' . un_htmlspecialchars($smiley_set['path'])))
1121 continue;
1122
1123 $dir = dir($context['smileys_dir'] . '/' . un_htmlspecialchars($smiley_set['path']));
1124 while ($entry = $dir->read())
1125 {
1126 if (!in_array($entry, $context['filenames']) && in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
1127 $context['filenames'][strtolower($entry)] = array(
1128 'id' => htmlspecialchars($entry),
1129 'selected' => false,
1130 );
1131 }
1132 $dir->close();
1133 }
1134 ksort($context['filenames']);
1135 }
1136
1137 $request = $smcFunc['db_query']('', '
1138 SELECT id_smiley AS id, code, filename, description, hidden AS location, 0 AS is_new
1139 FROM {db_prefix}smileys
1140 WHERE id_smiley = {int:current_smiley}',
1141 array(
1142 'current_smiley' => (int) $_REQUEST['smiley'],
1143 )
1144 );
1145 if ($smcFunc['db_num_rows']($request) != 1)
1146 fatal_lang_error('smiley_not_found');
1147 $context['current_smiley'] = $smcFunc['db_fetch_assoc']($request);
1148 $smcFunc['db_free_result']($request);
1149
1150 $context['current_smiley']['code'] = htmlspecialchars($context['current_smiley']['code']);
1151 $context['current_smiley']['filename'] = htmlspecialchars($context['current_smiley']['filename']);
1152 $context['current_smiley']['description'] = htmlspecialchars($context['current_smiley']['description']);
1153
1154 if (isset($context['filenames'][strtolower($context['current_smiley']['filename'])]))
1155 $context['filenames'][strtolower($context['current_smiley']['filename'])]['selected'] = true;
1156 }
1157 }
1158
1159 function list_getSmileys($start, $items_per_page, $sort)
1160 {
1161 global $smcFunc;
1162
1163 $request = $smcFunc['db_query']('', '
1164 SELECT id_smiley, code, filename, description, smiley_row, smiley_order, hidden
1165 FROM {db_prefix}smileys
1166 ORDER BY ' . $sort,
1167 array(
1168 )
1169 );
1170 $smileys = array();
1171 while ($row = $smcFunc['db_fetch_assoc']($request))
1172 $smileys[] = $row;
1173 $smcFunc['db_free_result']($request);
1174
1175 return $smileys;
1176 }
1177
1178 function list_getNumSmileys()
1179 {
1180 global $smcFunc;
1181
1182 $request = $smcFunc['db_query']('', '
1183 SELECT COUNT(*)
1184 FROM {db_prefix}smileys',
1185 array(
1186 )
1187 );
1188 list($numSmileys) = $smcFunc['db_fetch_row'];
1189 $smcFunc['db_free_result']($request);
1190
1191 return $numSmileys;
1192 }
1193
1194 function EditSmileyOrder()
1195 {
1196 global $modSettings, $context, $settings, $txt, $boarddir, $smcFunc;
1197
1198 // Move smileys to another position.
1199 if (isset($_REQUEST['reorder']))
1200 {
1201 checkSession('get');
1202
1203 $_GET['location'] = empty($_GET['location']) || $_GET['location'] != 'popup' ? 0 : 2;
1204 $_GET['source'] = empty($_GET['source']) ? 0 : (int) $_GET['source'];
1205
1206 if (empty($_GET['source']))
1207 fatal_lang_error('smiley_not_found');
1208
1209 if (!empty($_GET['after']))
1210 {
1211 $_GET['after'] = (int) $_GET['after'];
1212
1213 $request = $smcFunc['db_query']('', '
1214 SELECT smiley_row, smiley_order, hidden
1215 FROM {db_prefix}smileys
1216 WHERE hidden = {int:location}
1217 AND id_smiley = {int:after_smiley}',
1218 array(
1219 'location' => $_GET['location'],
1220 'after_smiley' => $_GET['after'],
1221 )
1222 );
1223 if ($smcFunc['db_num_rows']($request) != 1)
1224 fatal_lang_error('smiley_not_found');
1225 list ($smiley_row, $smiley_order, $smileyLocation) = $smcFunc['db_fetch_row']($request);
1226 $smcFunc['db_free_result']($request);
1227 }
1228 else
1229 {
1230 $smiley_row = (int) $_GET['row'];
1231 $smiley_order = -1;
1232 $smileyLocation = (int) $_GET['location'];
1233 }
1234
1235 $smcFunc['db_query']('', '
1236 UPDATE {db_prefix}smileys
1237 SET smiley_order = smiley_order + 1
1238 WHERE hidden = {int:new_location}
1239 AND smiley_row = {int:smiley_row}
1240 AND smiley_order > {int:smiley_order}',
1241 array(
1242 'new_location' => $_GET['location'],
1243 'smiley_row' => $smiley_row,
1244 'smiley_order' => $smiley_order,
1245 )
1246 );
1247
1248 $smcFunc['db_query']('', '
1249 UPDATE {db_prefix}smileys
1250 SET
1251 smiley_order = {int:smiley_order} + 1,
1252 smiley_row = {int:smiley_row},
1253 hidden = {int:new_location}
1254 WHERE id_smiley = {int:current_smiley}',
1255 array(
1256 'smiley_order' => $smiley_order,
1257 'smiley_row' => $smiley_row,
1258 'new_location' => $smileyLocation,
1259 'current_smiley' => $_GET['source'],
1260 )
1261 );
1262
1263 cache_put_data('parsing_smileys', null, 480);
1264 cache_put_data('posting_smileys', null, 480);
1265 }
1266
1267 $request = $smcFunc['db_query']('', '
1268 SELECT id_smiley, code, filename, description, smiley_row, smiley_order, hidden
1269 FROM {db_prefix}smileys
1270 WHERE hidden != {int:popup}
1271 ORDER BY smiley_order, smiley_row',
1272 array(
1273 'popup' => 1,
1274 )
1275 );
1276 $context['smileys'] = array(
1277 'postform' => array(
1278 'rows' => array(),
1279 ),
1280 'popup' => array(
1281 'rows' => array(),
1282 ),
1283 );
1284 while ($row = $smcFunc['db_fetch_assoc']($request))
1285 {
1286 $location = empty($row['hidden']) ? 'postform' : 'popup';
1287 $context['smileys'][$location]['rows'][$row['smiley_row']][] = array(
1288 'id' => $row['id_smiley'],
1289 'code' => htmlspecialchars($row['code']),
1290 'filename' => htmlspecialchars($row['filename']),
1291 'description' => htmlspecialchars($row['description']),
1292 'row' => $row['smiley_row'],
1293 'order' => $row['smiley_order'],
1294 'selected' => !empty($_REQUEST['move']) && $_REQUEST['move'] == $row['id_smiley'],
1295 );
1296 }
1297 $smcFunc['db_free_result']($request);
1298
1299 $context['move_smiley'] = empty($_REQUEST['move']) ? 0 : (int) $_REQUEST['move'];
1300
1301 // Make sure all rows are sequential.
1302 foreach (array_keys($context['smileys']) as $location)
1303 $context['smileys'][$location] = array(
1304 'id' => $location,
1305 'title' => $location == 'postform' ? $txt['smileys_location_form'] : $txt['smileys_location_popup'],
1306 'description' => $location == 'postform' ? $txt['smileys_location_form_description'] : $txt['smileys_location_popup_description'],
1307 'last_row' => count($context['smileys'][$location]['rows']),
1308 'rows' => array_values($context['smileys'][$location]['rows']),
1309 );
1310
1311 // Check & fix smileys that are not ordered properly in the database.
1312 foreach (array_keys($context['smileys']) as $location)
1313 {
1314 foreach ($context['smileys'][$location]['rows'] as $id => $smiley_row)
1315 {
1316 // Fix empty rows if any.
1317 if ($id != $smiley_row[0]['row'])
1318 {
1319 $smcFunc['db_query']('', '
1320 UPDATE {db_prefix}smileys
1321 SET smiley_row = {int:new_row}
1322 WHERE smiley_row = {int:current_row}
1323 AND hidden = {int:location}',
1324 array(
1325 'new_row' => $id,
1326 'current_row' => $smiley_row[0]['row'],
1327 'location' => $location == 'postform' ? '0' : '2',
1328 )
1329 );
1330 // Only change the first row value of the first smiley (we don't need the others :P).
1331 $context['smileys'][$location]['rows'][$id][0]['row'] = $id;
1332 }
1333 // Make sure the smiley order is always sequential.
1334 foreach ($smiley_row as $order_id => $smiley)
1335 if ($order_id != $smiley['order'])
1336 $smcFunc['db_query']('', '
1337 UPDATE {db_prefix}smileys
1338 SET smiley_order = {int:new_order}
1339 WHERE id_smiley = {int:current_smiley}',
1340 array(
1341 'new_order' => $order_id,
1342 'current_smiley' => $smiley['id'],
1343 )
1344 );
1345 }
1346 }
1347
1348 cache_put_data('parsing_smileys', null, 480);
1349 cache_put_data('posting_smileys', null, 480);
1350 }
1351
1352 function InstallSmileySet()
1353 {
1354 global $sourcedir, $boarddir, $modSettings, $smcFunc;
1355
1356 isAllowedTo('manage_smileys');
1357 checkSession('request');
1358
1359 require_once($sourcedir . '/Subs-Package.php');
1360
1361 $name = strtok(basename(isset($_FILES['set_gz']) ? $_FILES['set_gz']['name'] : $_REQUEST['set_gz']), '.');
1362 $name = preg_replace(array('/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'), array('_', '.', ''), $name);
1363
1364 // !!! Decide: overwrite or not?
1365 if (isset($_FILES['set_gz']) && is_uploaded_file($_FILES['set_gz']['tmp_name']) && (@ini_get('open_basedir') != '' || file_exists($_FILES['set_gz']['tmp_name'])))
1366 $extracted = read_tgz_file($_FILES['set_gz']['tmp_name'], $boarddir . '/Smileys/' . $name);
1367 elseif (isset($_REQUEST['set_gz']))
1368 {
1369 // Check that the smiley is from simplemachines.org, for now... maybe add mirroring later.
1370 if (preg_match('~^http://[\w_\-]+\.simplemachines\.org/~', $_REQUEST['set_gz']) == 0 || strpos($_REQUEST['set_gz'], 'dlattach') !== false)
1371 fatal_lang_error('not_on_simplemachines');
1372
1373 $extracted = read_tgz_file($_REQUEST['set_gz'], $boarddir . '/Smileys/' . $name);
1374 }
1375 else
1376 redirectexit('action=admin;area=smileys');
1377
1378 updateSettings(array(
1379 'smiley_sets_known' => $modSettings['smiley_sets_known'] . ',' . $name,
1380 'smiley_sets_names' => $modSettings['smiley_sets_names'] . "\n" . strtok(basename(isset($_FILES['set_gz']) ? $_FILES['set_gz']['name'] : $_REQUEST['set_gz']), '.'),
1381 ));
1382
1383 cache_put_data('parsing_smileys', null, 480);
1384 cache_put_data('posting_smileys', null, 480);
1385
1386 // !!! Add some confirmation?
1387 redirectexit('action=admin;area=smileys');
1388 }
1389
1390 // A function to import new smileys from an existing directory into the database.
1391 function ImportSmileys($smileyPath)
1392 {
1393 global $modSettings, $smcFunc;
1394
1395 if (empty($modSettings['smileys_dir']) || !is_dir($modSettings['smileys_dir'] . '/' . $smileyPath))
1396 fatal_lang_error('smiley_set_unable_to_import');
1397
1398 $smileys = array();
1399 $dir = dir($modSettings['smileys_dir'] . '/' . $smileyPath);
1400 while ($entry = $dir->read())
1401 {
1402 if (in_array(strrchr($entry, '.'), array('.jpg', '.gif', '.jpeg', '.png')))
1403 $smileys[strtolower($entry)] = $entry;
1404 }
1405 $dir->close();
1406
1407 // Exclude the smileys that are already in the database.
1408 $request = $smcFunc['db_query']('', '
1409 SELECT filename
1410 FROM {db_prefix}smileys
1411 WHERE filename IN ({array_string:smiley_list})',
1412 array(
1413 'smiley_list' => $smileys,
1414 )
1415 );
1416 while ($row = $smcFunc['db_fetch_assoc']($request))
1417 if (isset($smileys[strtolower($row['filename'])]))
1418 unset($smileys[strtolower($row['filename'])]);
1419 $smcFunc['db_free_result']($request);
1420
1421 $request = $smcFunc['db_query']('', '
1422 SELECT MAX(smiley_order)
1423 FROM {db_prefix}smileys
1424 WHERE hidden = {int:postform}
1425 AND smiley_row = {int:first_row}',
1426 array(
1427 'postform' => 0,
1428 'first_row' => 0,
1429 )
1430 );
1431 list ($smiley_order) = $smcFunc['db_fetch_row']($request);
1432 $smcFunc['db_free_result']($request);
1433
1434 $new_smileys = array();
1435 foreach ($smileys as $smiley)
1436 if (strlen($smiley) <= 48)
1437 $new_smileys[] = array(':' . strtok($smiley, '.') . ':', $smiley, strtok($smiley, '.'), 0, ++$smiley_order);
1438
1439 if (!empty($new_smileys))
1440 {
1441 $smcFunc['db_insert']('',
1442 '{db_prefix}smileys',
1443 array(
1444 'code' => 'string-30', 'filename' => 'string-48', 'description' => 'string-80', 'smiley_row' => 'int', 'smiley_order' => 'int',
1445 ),
1446 $new_smileys,
1447 array('id_smiley')
1448 );
1449
1450 // Make sure the smiley codes are still in the right order.
1451 sortSmileyTable();
1452
1453 cache_put_data('parsing_smileys', null, 480);
1454 cache_put_data('posting_smileys', null, 480);
1455 }
1456 }
1457
1458 function EditMessageIcons()
1459 {
1460 global $user_info, $modSettings, $context, $settings, $txt;
1461 global $boarddir, $smcFunc, $scripturl, $sourcedir;
1462
1463 // Get a list of icons.
1464 $context['icons'] = array();
1465 $request = $smcFunc['db_query']('', '
1466 SELECT m.id_icon, m.title, m.filename, m.icon_order, m.id_board, b.name AS board_name
1467 FROM {db_prefix}message_icons AS m
1468 LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
1469 WHERE ({query_see_board} OR b.id_board IS NULL)',
1470 array(
1471 )
1472 );
1473 $last_icon = 0;
1474 $trueOrder = 0;
1475 while ($row = $smcFunc['db_fetch_assoc']($request))
1476 {
1477 $context['icons'][$row['id_icon']] = array(
1478 'id' => $row['id_icon'],
1479 'title' => $row['title'],
1480 'filename' => $row['filename'],
1481 'image_url' => $settings[file_exists($settings['theme_dir'] . '/images/post/' . $row['filename'] . '.gif') ? 'actual_images_url' : 'default_images_url'] . '/post/' . $row['filename'] . '.gif',
1482 'board_id' => $row['id_board'],
1483 'board' => empty($row['board_name']) ? $txt['icons_edit_icons_all_boards'] : $row['board_name'],
1484 'order' => $row['icon_order'],
1485 'true_order' => $trueOrder++,
1486 'after' => $last_icon,
1487 );
1488 $last_icon = $row['id_icon'];
1489 }
1490 $smcFunc['db_free_result']($request);
1491
1492 // Submitting a form?
1493 if (isset($_POST[$context['session_var']]))
1494 {
1495 checkSession();
1496
1497 // Deleting icons?
1498 if (isset($_POST['delete']) && !empty($_POST['checked_icons']))
1499 {
1500 $deleteIcons = array();
1501 foreach ($_POST['checked_icons'] as $icon)
1502 $deleteIcons[] = (int) $icon;
1503
1504 // Do the actual delete!
1505 $smcFunc['db_query']('', '
1506 DELETE FROM {db_prefix}message_icons
1507 WHERE id_icon IN ({array_int:icon_list})',
1508 array(
1509 'icon_list' => $deleteIcons,
1510 )
1511 );
1512 }
1513 // Editing/Adding an icon?
1514 elseif ($context['sub_action'] == 'editicon' && isset($_GET['icon']))
1515 {
1516 $_GET['icon'] = (int) $_GET['icon'];
1517
1518 // Do some preperation with the data... like check the icon exists *somewhere*
1519 if (strpos($_POST['icon_filename'], '.gif') !== false)
1520 $_POST['icon_filename'] = substr($_POST['icon_filename'], 0, -4);
1521 if (!file_exists($settings['default_theme_dir'] . '/images/post/' . $_POST['icon_filename'] . '.gif'))
1522 fatal_lang_error('icon_not_found');
1523 // There is a 16 character limit on message icons...
1524 elseif (strlen($_POST['icon_filename']) > 16)
1525 fatal_lang_error('icon_name_too_long');
1526 elseif ($_POST['icon_location'] == $_GET['icon'] && !empty($_GET['icon']))
1527 fatal_lang_error('icon_after_itself');
1528
1529 // First do the sorting... if this is an edit reduce the order of everything after it by one ;)
1530 if ($_GET['icon'] != 0)
1531 {
1532 $oldOrder = $context['icons'][$_GET['icon']]['true_order'];
1533 foreach ($context['icons'] as $id => $data)
1534 if ($data['true_order'] > $oldOrder)
1535 $context['icons'][$id]['true_order']--;
1536 }
1537
1538 // If there are no existing icons and this is a new one, set the id to 1 (mainly for non-mysql)
1539 if (empty($_GET['icon']) && empty($context['icons']))
1540 $_GET['icon'] = 1;
1541
1542 // Get the new order.
1543 $newOrder = $_POST['icon_location'] == 0 ? 0 : $context['icons'][$_POST['icon_location']]['true_order'] + 1;
1544 // Do the same, but with the one that used to be after this icon, done to avoid conflict.
1545 foreach ($context['icons'] as $id => $data)
1546 if ($data['true_order'] >= $newOrder)
1547 $context['icons'][$id]['true_order']++;
1548
1549 // Finally set the current icon's position!
1550 $context['icons'][$_GET['icon']]['true_order'] = $newOrder;
1551
1552 // Simply replace the existing data for the other bits.
1553 $context['icons'][$_GET['icon']]['title'] = $_POST['icon_description'];
1554 $context['icons'][$_GET['icon']]['filename'] = $_POST['icon_filename'];
1555 $context['icons'][$_GET['icon']]['board_id'] = (int) $_POST['icon_board'];
1556
1557 // Do a huge replace ;)
1558 $iconInsert = array();
1559 $iconInsert_new = array();
1560 foreach ($context['icons'] as $id => $icon)
1561 {
1562 if ($id != 0)
1563 {
1564 $iconInsert[] = array($id, $icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']);
1565 }
1566 else
1567 {
1568 $iconInsert_new[] = array($icon['board_id'], $icon['title'], $icon['filename'], $icon['true_order']);
1569 }
1570 }
1571
1572 $smcFunc['db_insert']('replace',
1573 '{db_prefix}message_icons',
1574 array('id_icon' => 'int', 'id_board' => 'int', 'title' => 'string-80', 'filename' => 'string-80', 'icon_order' => 'int'),
1575 $iconInsert,
1576 array('id_icon')
1577 );
1578
1579 if (!empty($iconInsert_new))
1580 {
1581 $smcFunc['db_insert']('replace',
1582 '{db_prefix}message_icons',
1583 array('id_board' => 'int', 'title' => 'string-80', 'filename' => 'string-80', 'icon_order' => 'int'),
1584 $iconInsert_new,
1585 array('id_icon')
1586 );
1587 }
1588 }
1589
1590 // Sort by order, so it is quicker :)
1591 $smcFunc['db_query']('alter_table_icons', '
1592 ALTER TABLE {db_prefix}message_icons
1593 ORDER BY icon_order',
1594 array(
1595 'db_error_skip' => true,
1596 )
1597 );
1598
1599 // Unless we're adding a new thing, we'll escape
1600 if (!isset($_POST['add']))
1601 redirectexit('action=admin;area=smileys;sa=editicons');
1602 }
1603
1604 $context[$context['admin_menu_name']]['current_subsection'] = 'editicons';
1605
1606 $listOptions = array(
1607 'id' => 'message_icon_list',
1608 'base_href' => $scripturl . '?action=admin;area=smileys;sa=editicons',
1609 'get_items' => array(
1610 'function' => 'list_getMessageIcons',
1611 ),
1612 'no_items_label' => $txt['icons_no_entries'],
1613 'columns' => array(
1614 'icon' => array(
1615 'data' => array(
1616 'function' => create_function('$rowData', '
1617 global $settings;
1618
1619 $images_url = $settings[file_exists(sprintf(\'%1$s/images/post/%2$s.gif\', $settings[\'theme_dir\'], $rowData[\'filename\'])) ? \'actual_images_url\' : \'default_images_url\'];
1620 return sprintf(\'<img src="%1$s/post/%2$s.gif" alt="%3$s" />\', $images_url, $rowData[\'filename\'], htmlspecialchars($rowData[\'title\']));
1621 '),
1622 ),
1623 'style' => 'text-align: center;',
1624 ),
1625 'filename' => array(
1626 'header' => array(
1627 'value' => $txt['smileys_filename'],
1628 ),
1629 'data' => array(
1630 'sprintf' => array(
1631 'format' => '%1$s.gif',
1632 'params' => array(
1633 'filename' => true,
1634 ),
1635 ),
1636 ),
1637 ),
1638 'tooltip' => array(
1639 'header' => array(
1640 'value' => $txt['smileys_description'],
1641 ),
1642 'data' => array(
1643 'db_htmlsafe' => 'title',
1644 'class' => 'windowbg',
1645 ),
1646 ),
1647 'board' => array(
1648 'header' => array(
1649 'value' => $txt['icons_board'],
1650 ),
1651 'data' => array(
1652 'function' => create_function('$rowData', '
1653 global $txt;
1654
1655 return empty($rowData[\'board_name\']) ? $txt[\'icons_edit_icons_all_boards\'] : $rowData[\'board_name\'];
1656 '),
1657 ),
1658 ),
1659 'modify' => array(
1660 'header' => array(
1661 'value' => $txt['smileys_modify'],
1662 ),
1663 'data' => array(
1664 'sprintf' => array(
1665 'format' => '<a href="' . $scripturl . '?action=admin;area=smileys;sa=editicon;icon=%1$s">' . $txt['smileys_modify'] . '</a>',
1666 'params' => array(
1667 'id_icon' => false,
1668 ),
1669 ),
1670 'style' => 'text-align: center',
1671 ),
1672 ),
1673 'check' => array(
1674 'header' => array(
1675 'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
1676 ),
1677 'data' => array(
1678 'sprintf' => array(
1679 'format' => '<input type="checkbox" name="checked_icons[]" value="%1$d" class="input_check" />',
1680 'params' => array(
1681 'id_icon' => false,
1682 ),
1683 ),
1684 'style' => 'text-align: center',
1685 ),
1686 ),
1687 ),
1688 'form' => array(
1689 'href' => $scripturl . '?action=admin;area=smileys;sa=editicons',
1690 ),
1691 'additional_rows' => array(
1692 array(
1693 'position' => 'below_table_data',
1694 'value' => '<input type="submit" name="delete" value="' . $txt['quickmod_delete_selected'] . '" style="float: right" class="button_submit" />[<a href="' . $scripturl . '?action=admin;area=smileys;sa=editicon">' . $txt['icons_add_new'] . '</a>]',
1695 ),
1696 ),
1697 );
1698
1699 require_once($sourcedir . '/Subs-List.php');
1700 createList($listOptions);
1701
1702 // If we're adding/editing an icon we'll need a list of boards
1703 if ($context['sub_action'] == 'editicon' || isset($_POST['add']))
1704 {
1705 // Force the sub_template just in case.
1706 $context['sub_template'] = 'editicon';
1707
1708 $context['new_icon'] = !isset($_GET['icon']);
1709
1710 // Get the properties of the current icon from the icon list.
1711 if (!$context['new_icon'])
1712 $context['icon'] = $context['icons'][$_GET['icon']];
1713
1714 // Get a list of boards needed for assigning this icon to a specific board.
1715 $boardListOptions = array(
1716 'use_permissions' => true,
1717 'selected_board' => isset($context['icon']['board_id']) ? $context['icon']['board_id'] : 0,
1718 );
1719 require_once($sourcedir . '/Subs-MessageIndex.php');
1720 $context['categories'] = getBoardList($boardListOptions);
1721 }
1722 }
1723
1724 function list_getMessageIcons($start, $items_per_page, $sort)
1725 {
1726 global $smcFunc, $user_info;
1727
1728 $request = $smcFunc['db_query']('', '
1729 SELECT m.id_icon, m.title, m.filename, m.icon_order, m.id_board, b.name AS board_name
1730 FROM {db_prefix}message_icons AS m
1731 LEFT JOIN {db_prefix}boards AS b ON (b.id_board = m.id_board)
1732 WHERE ({query_see_board} OR b.id_board IS NULL)',
1733 array(
1734 )
1735 );
1736
1737 $message_icons = array();
1738 while ($row = $smcFunc['db_fetch_assoc']($request))
1739 $message_icons[] = $row;
1740 $smcFunc['db_free_result']($request);
1741
1742 return $message_icons;
1743 }
1744
1745 // This function sorts the smiley table by code length, it is needed as MySQL withdrew support for functions in order by.
1746 function sortSmileyTable()
1747 {
1748 global $smcFunc;
1749
1750 db_extend('packages');
1751
1752 // Add a sorting column.
1753 $smcFunc['db_add_column']('{db_prefix}smileys', array('name' => 'temp_order', 'size' => 8, 'type' => 'mediumint', 'null' => false));
1754
1755 // Set the contents of this column.
1756 $smcFunc['db_query']('set_smiley_order', '
1757 UPDATE {db_prefix}smileys
1758 SET temp_order = LENGTH(code)',
1759 array(
1760 )
1761 );
1762
1763 // Order the table by this column.
1764 $smcFunc['db_query']('alter_table_smileys', '
1765 ALTER TABLE {db_prefix}smileys
1766 ORDER BY temp_order DESC',
1767 array(
1768 'db_error_skip' => true,
1769 )
1770 );
1771
1772 // Remove the sorting column.
1773 $smcFunc['db_remove_column']('{db_prefix}smileys', 'temp_order');
1774 }
1775
1776 ?>