5
* @version $Id: acp_icons.php,v 1.40 2007/10/21 11:26:24 acydburn Exp $
6
* @copyright (c) 2005 phpBB Group
7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
14
if (!defined('IN_PHPBB'))
20
* @todo [smilies] check regular expressions for special char replacements (stored specialchared in db)
27
function main($id, $mode)
29
global $db, $user, $auth, $template, $cache;
30
global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
32
$user->add_lang('acp/posting');
34
// Set up general vars
35
$action = request_var('action', '');
36
$action = (isset($_POST['add'])) ? 'add' : $action;
37
$action = (isset($_POST['edit'])) ? 'edit' : $action;
38
$action = (isset($_POST['import'])) ? 'import' : $action;
39
$icon_id = request_var('id', 0);
41
$mode = ($mode == 'smilies') ? 'smilies' : 'icons';
43
$this->tpl_name = 'acp_icons';
45
// What are we working on?
49
$table = SMILIES_TABLE;
52
$img_path = $config['smilies_path'];
59
$img_path = $config['icons_path'];
63
$this->page_title = 'ACP_' . $lang;
66
$_images = $_paks = array();
69
// Grab file list of paks and images
70
if ($action == 'edit' || $action == 'add' || $action == 'import')
72
$imglist = filelist($phpbb_root_path . $img_path, '');
74
foreach ($imglist as $path => $img_ary)
76
foreach ($img_ary as $img)
78
$img_size = getimagesize($phpbb_root_path . $img_path . '/' . $path . $img);
80
if (!$img_size[0] || !$img_size[1] || strlen($img) > 255)
85
$_images[$path . $img]['file'] = $path . $img;
86
$_images[$path . $img]['width'] = $img_size[0];
87
$_images[$path . $img]['height'] = $img_size[1];
92
if ($dir = @opendir($phpbb_root_path . $img_path))
94
while (($file = readdir($dir)) !== false)
96
if (is_file($phpbb_root_path . $img_path . '/' . $file) && preg_match('#\.pak$#i', $file))
105
// What shall we do today? Oops, I believe that's trademarked ...
116
$smilies = $default_row = array();
117
$smiley_options = $order_list = $add_order_list = '';
119
if ($action == 'add' && $mode == 'smilies')
122
FROM ' . SMILIES_TABLE . '
123
ORDER BY smiley_order';
124
$result = $db->sql_query($sql);
126
while ($row = $db->sql_fetchrow($result))
128
if (empty($smilies[$row['smiley_url']]))
130
$smilies[$row['smiley_url']] = $row;
133
$db->sql_freeresult($result);
135
if (sizeof($smilies))
137
foreach ($smilies as $row)
141
if (!$smiley_options)
146
$smiley_options .= '<option value="' . $row['smiley_url'] . '"' . (($selected) ? ' selected="selected"' : '') . '>' . $row['smiley_url'] . '</option>';
148
$template->assign_block_vars('smile', array(
149
'SMILEY_URL' => addslashes($row['smiley_url']),
150
'CODE' => addslashes($row['code']),
151
'EMOTION' => addslashes($row['emotion']),
152
'WIDTH' => $row['smiley_width'],
153
'HEIGHT' => $row['smiley_height'],
154
'ORDER' => $row['smiley_order'] + 1,
162
ORDER BY {$fields}_order " . (($icon_id || $action == 'add') ? 'DESC' : 'ASC');
163
$result = $db->sql_query($sql);
168
$order_lists = array('', '');
169
$add_order_lists = array('', '');
172
while ($row = $db->sql_fetchrow($result))
174
if ($action == 'add')
176
unset($_images[$row[$fields . '_url']]);
180
if ($row[$fields . '_id'] == $icon_id)
183
$display = $row['display_on_posting'];
184
$data[$row[$fields . '_url']] = $row;
188
if ($action == 'edit' && !$icon_id)
190
$data[$row[$fields . '_url']] = $row;
196
$selected = ' selected="selected"';
199
if ($row['display_on_posting'])
203
$after_txt = ($mode == 'smilies') ? $row['code'] : $row['icons_url'];
204
$order_lists[$row['display_on_posting']] = '<option value="' . ($row[$fields . '_order'] + 1) . '"' . $selected . '>' . sprintf($user->lang['AFTER_' . $lang], ' -> ' . $after_txt) . '</option>' . $order_lists[$row['display_on_posting']];
206
if (!empty($default_row))
208
$add_order_lists[$row['display_on_posting']] = '<option value="' . ($row[$fields . '_order'] + 1) . '"' . (($row[$fields . '_id'] == $default_row['smiley_id']) ? ' selected="selected"' : '') . '>' . sprintf($user->lang['AFTER_' . $lang], ' -> ' . $after_txt) . '</option>' . $add_order_lists[$row['display_on_posting']];
212
$db->sql_freeresult($result);
214
$order_list = '<option value="1"' . ((!isset($after)) ? ' selected="selected"' : '') . '>' . $user->lang['FIRST'] . '</option>';
215
$add_order_list = '<option value="1">' . $user->lang['FIRST'] . '</option>';
217
if ($action == 'add')
222
$colspan = (($mode == 'smilies') ? '7' : '5');
223
$colspan += ($icon_id) ? 1 : 0;
224
$colspan += ($action == 'add') ? 2 : 0;
226
$template->assign_vars(array(
228
'S_SMILIES' => ($mode == 'smilies') ? true : false,
229
'S_ADD' => ($action == 'add') ? true : false,
231
'S_ORDER_LIST_DISPLAY' => $order_list . $order_lists[1],
232
'S_ORDER_LIST_UNDISPLAY' => $order_list . $order_lists[0],
233
'S_ORDER_LIST_DISPLAY_COUNT' => $display_count + 1,
235
'L_TITLE' => $user->lang['ACP_' . $lang],
236
'L_EXPLAIN' => $user->lang['ACP_' . $lang . '_EXPLAIN'],
237
'L_CONFIG' => $user->lang[$lang . '_CONFIG'],
238
'L_URL' => $user->lang[$lang . '_URL'],
239
'L_LOCATION' => $user->lang[$lang . '_LOCATION'],
240
'L_WIDTH' => $user->lang[$lang . '_WIDTH'],
241
'L_HEIGHT' => $user->lang[$lang . '_HEIGHT'],
242
'L_ORDER' => $user->lang[$lang . '_ORDER'],
243
'L_NO_ICONS' => $user->lang['NO_' . $lang . '_' . strtoupper($action)],
245
'COLSPAN' => $colspan,
248
'U_BACK' => $this->u_action,
249
'U_ACTION' => $this->u_action . '&action=' . (($action == 'add') ? 'create' : 'modify'),
252
foreach ($data as $img => $img_row)
254
$template->assign_block_vars('items', array(
256
'A_IMG' => addslashes($img),
257
'IMG_SRC' => $phpbb_root_path . $img_path . '/' . $img,
259
'CODE' => ($mode == 'smilies' && isset($img_row['code'])) ? $img_row['code'] : '',
260
'EMOTION' => ($mode == 'smilies' && isset($img_row['emotion'])) ? $img_row['emotion'] : '',
262
'S_ID' => (isset($img_row[$fields . '_id'])) ? true : false,
263
'ID' => (isset($img_row[$fields . '_id'])) ? $img_row[$fields . '_id'] : 0,
264
'WIDTH' => (!empty($img_row[$fields .'_width'])) ? $img_row[$fields .'_width'] : $img_row['width'],
265
'HEIGHT' => (!empty($img_row[$fields .'_height'])) ? $img_row[$fields .'_height'] : $img_row['height'],
266
'POSTING_CHECKED' => (!empty($img_row['display_on_posting']) || $action == 'add') ? ' checked="checked"' : '',
270
// Ok, another row for adding an addition code for a pre-existing image...
271
if ($action == 'add' && $mode == 'smilies' && sizeof($smilies))
273
$template->assign_vars(array(
274
'S_ADD_CODE' => true,
276
'S_IMG_OPTIONS' => $smiley_options,
278
'S_ADD_ORDER_LIST_DISPLAY' => $add_order_list . $add_order_lists[1],
279
'S_ADD_ORDER_LIST_UNDISPLAY' => $add_order_list . $add_order_lists[0],
281
'IMG_SRC' => $phpbb_root_path . $img_path . '/' . $default_row['smiley_url'],
282
'IMG_PATH' => $img_path,
283
'PHPBB_ROOT_PATH' => $phpbb_root_path,
285
'CODE' => $default_row['code'],
286
'EMOTION' => $default_row['emotion'],
288
'WIDTH' => $default_row['smiley_width'],
289
'HEIGHT' => $default_row['smiley_height'],
300
// Get items to create/modify
301
$images = (isset($_POST['image'])) ? array_keys(request_var('image', array('' => 0))) : array();
303
// Now really get the items
304
$image_id = (isset($_POST['id'])) ? request_var('id', array('' => 0)) : array();
305
$image_order = (isset($_POST['order'])) ? request_var('order', array('' => 0)) : array();
306
$image_width = (isset($_POST['width'])) ? request_var('width', array('' => 0)) : array();
307
$image_height = (isset($_POST['height'])) ? request_var('height', array('' => 0)) : array();
308
$image_add = (isset($_POST['add_img'])) ? request_var('add_img', array('' => 0)) : array();
309
$image_emotion = utf8_normalize_nfc(request_var('emotion', array('' => ''), true));
310
$image_code = utf8_normalize_nfc(request_var('code', array('' => ''), true));
311
$image_display_on_posting = (isset($_POST['display_on_posting'])) ? request_var('display_on_posting', array('' => 0)) : array();
313
// Ok, add the relevant bits if we are adding new codes to existing emoticons...
314
if (!empty($_POST['add_additional_code']))
316
$add_image = request_var('add_image', '');
317
$add_code = utf8_normalize_nfc(request_var('add_code', '', true));
318
$add_emotion = utf8_normalize_nfc(request_var('add_emotion', '', true));
320
if ($add_image && $add_emotion && $add_code)
322
$images[] = $add_image;
323
$image_add[$add_image] = true;
325
$image_code[$add_image] = $add_code;
326
$image_emotion[$add_image] = $add_emotion;
327
$image_width[$add_image] = request_var('add_width', 0);
328
$image_height[$add_image] = request_var('add_height', 0);
330
if (!empty($_POST['add_display_on_posting']))
332
$image_display_on_posting[$add_image] = 1;
335
$image_order[$add_image] = request_var('add_order', 0);
340
foreach ($images as $image)
342
if (($mode == 'smilies' && ($image_emotion[$image] == '' || $image_code[$image] == '')) ||
343
($action == 'create' && !isset($image_add[$image])))
348
if ($image_width[$image] == 0 || $image_height[$image] == 0)
350
$img_size = getimagesize($phpbb_root_path . $img_path . '/' . $image);
351
$image_width[$image] = $img_size[0];
352
$image_height[$image] = $img_size[1];
356
$fields . '_url' => $image,
357
$fields . '_width' => $image_width[$image],
358
$fields . '_height' => $image_height[$image],
359
'display_on_posting' => (isset($image_display_on_posting[$image])) ? 1 : 0,
362
if ($mode == 'smilies')
364
$img_sql = array_merge($img_sql, array(
365
'emotion' => $image_emotion[$image],
366
'code' => $image_code[$image])
370
// Image_order holds the 'new' order value
371
if (!empty($image_order[$image]))
373
$img_sql = array_merge($img_sql, array(
374
$fields . '_order' => $image_order[$image])
377
// Since we always add 'after' an item, we just need to increase all following + the current by one
378
$sql = "UPDATE $table
379
SET {$fields}_order = {$fields}_order + 1
380
WHERE {$fields}_order >= {$image_order[$image]}";
381
$db->sql_query($sql);
383
// If we adjust the order, we need to adjust all other orders too - they became inaccurate...
384
foreach ($image_order as $_image => $_order)
386
if ($_image == $image)
391
if ($_order >= $image_order[$image])
393
$image_order[$_image]++;
398
if ($action == 'modify' && !empty($image_id[$image]))
400
$sql = "UPDATE $table
401
SET " . $db->sql_build_array('UPDATE', $img_sql) . "
402
WHERE {$fields}_id = " . $image_id[$image];
403
$db->sql_query($sql);
406
else if ($action !== 'modify')
408
$sql = "INSERT INTO $table " . $db->sql_build_array('INSERT', $img_sql);
409
$db->sql_query($sql);
416
$cache->destroy('_icons');
417
$cache->destroy('sql', $table);
419
$level = E_USER_NOTICE;
420
switch ($icons_updated)
423
$suc_lang = "{$lang}_NONE";
424
$level = E_USER_WARNING;
428
$suc_lang = "{$lang}_ONE";
434
if ($action == 'modify')
436
trigger_error($user->lang[$suc_lang . '_EDITED'] . adm_back_link($this->u_action), $level);
440
trigger_error($user->lang[$suc_lang . '_ADDED'] . adm_back_link($this->u_action), $level);
447
$pak = request_var('pak', '');
448
$current = request_var('current', '');
454
if (!($pak_ary = @file($phpbb_root_path . $img_path . '/' . $pak)))
456
trigger_error($user->lang['PAK_FILE_NOT_READABLE'] . adm_back_link($this->u_action), E_USER_WARNING);
459
// Make sure the pak_ary is valid
460
foreach ($pak_ary as $pak_entry)
462
if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
464
if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
465
(sizeof($data[1]) != 6 && $mode == 'smilies'))
467
trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
472
trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
477
// The user has already selected a smilies_pak file
478
if ($current == 'delete')
480
switch ($db->sql_layer)
484
$db->sql_query('DELETE FROM ' . $table);
488
$db->sql_query('TRUNCATE TABLE ' . $table);
498
// Reset all icon_ids
499
$db->sql_query('UPDATE ' . TOPICS_TABLE . ' SET icon_id = 0');
500
$db->sql_query('UPDATE ' . POSTS_TABLE . ' SET icon_id = 0');
508
$field_sql = ($mode == 'smilies') ? 'code' : 'icons_url';
510
$sql = "SELECT $field_sql
512
$result = $db->sql_query($sql);
514
while ($row = $db->sql_fetchrow($result))
517
$cur_img[$row[$field_sql]] = 1;
519
$db->sql_freeresult($result);
522
foreach ($pak_ary as $pak_entry)
525
if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
527
if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
528
(sizeof($data[1]) != 6 && $mode == 'smilies'))
530
trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
533
// Stripslash here because it got addslashed before... (on export)
534
$img = stripslashes($data[1][0]);
535
$width = stripslashes($data[1][1]);
536
$height = stripslashes($data[1][2]);
537
$display_on_posting = stripslashes($data[1][3]);
539
if (isset($data[1][4]) && isset($data[1][5]))
541
$emotion = stripslashes($data[1][4]);
542
$code = stripslashes($data[1][5]);
545
if ($current == 'replace' &&
546
(($mode == 'smilies' && !empty($cur_img[$code])) ||
547
($mode == 'icons' && !empty($cur_img[$img]))))
549
$replace_sql = ($mode == 'smilies') ? $code : $img;
551
$fields . '_url' => $img,
552
$fields . '_height' => (int) $height,
553
$fields . '_width' => (int) $width,
554
'display_on_posting' => (int) $display_on_posting,
557
if ($mode == 'smilies')
559
$sql = array_merge($sql, array(
560
'emotion' => $emotion,
564
$sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql) . "
565
WHERE $field_sql = '" . $db->sql_escape($replace_sql) . "'";
566
$db->sql_query($sql);
573
$fields . '_url' => $img,
574
$fields . '_height' => (int) $height,
575
$fields . '_width' => (int) $width,
576
$fields . '_order' => (int) $order,
577
'display_on_posting'=> (int) $display_on_posting,
580
if ($mode == 'smilies')
582
$sql = array_merge($sql, array(
584
'emotion' => $emotion,
587
$db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql));
592
$cache->destroy('_icons');
593
$cache->destroy('sql', $table);
595
trigger_error($user->lang[$lang . '_IMPORT_SUCCESS'] . adm_back_link($this->u_action));
601
foreach ($_paks as $pak)
603
$pak_options .= '<option value="' . $pak . '">' . htmlspecialchars($pak) . '</option>';
606
$template->assign_vars(array(
607
'S_CHOOSE_PAK' => true,
608
'S_PAK_OPTIONS' => $pak_options,
610
'L_TITLE' => $user->lang['ACP_' . $lang],
611
'L_EXPLAIN' => $user->lang['ACP_' . $lang . '_EXPLAIN'],
612
'L_NO_PAK_OPTIONS' => $user->lang['NO_' . $lang . '_PAK'],
613
'L_CURRENT' => $user->lang['CURRENT_' . $lang],
614
'L_CURRENT_EXPLAIN' => $user->lang['CURRENT_' . $lang . '_EXPLAIN'],
615
'L_IMPORT_SUBMIT' => $user->lang['IMPORT_' . $lang],
617
'U_BACK' => $this->u_action,
618
'U_ACTION' => $this->u_action . '&action=import',
626
$this->page_title = 'EXPORT_' . $lang;
627
$this->tpl_name = 'message_body';
629
$template->assign_vars(array(
630
'MESSAGE_TITLE' => $user->lang['EXPORT_' . $lang],
631
'MESSAGE_TEXT' => sprintf($user->lang['EXPORT_' . $lang . '_EXPLAIN'], '<a href="' . $this->u_action . '&action=send">', '</a>'),
633
'S_USER_NOTICE' => true,
645
ORDER BY {$fields}_order";
646
$result = $db->sql_query($sql);
649
while ($row = $db->sql_fetchrow($result))
651
$pak .= "'" . addslashes($row[$fields . '_url']) . "', ";
652
$pak .= "'" . addslashes($row[$fields . '_width']) . "', ";
653
$pak .= "'" . addslashes($row[$fields . '_height']) . "', ";
654
$pak .= "'" . addslashes($row['display_on_posting']) . "', ";
656
if ($mode == 'smilies')
658
$pak .= "'" . addslashes($row['emotion']) . "', ";
659
$pak .= "'" . addslashes($row['code']) . "', ";
664
$db->sql_freeresult($result);
668
garbage_collection();
670
header('Pragma: public');
672
// Send out the Headers
673
header('Content-Type: text/x-delimtext; name="' . $mode . '.pak"');
674
header('Content-Disposition: inline; filename="' . $mode . '.pak"');
682
trigger_error($user->lang['NO_' . strtoupper($fields) . '_EXPORT'] . adm_back_link($this->u_action), E_USER_WARNING);
689
if (confirm_box(true))
691
$sql = "DELETE FROM $table
692
WHERE {$fields}_id = $icon_id";
693
$db->sql_query($sql);
701
// Reset appropriate icon_ids
702
$db->sql_query('UPDATE ' . TOPICS_TABLE . "
704
WHERE icon_id = $icon_id");
706
$db->sql_query('UPDATE ' . POSTS_TABLE . "
708
WHERE icon_id = $icon_id");
712
$notice = $user->lang[$lang . '_DELETED'];
714
$cache->destroy('_icons');
715
$cache->destroy('sql', $table);
719
confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
723
'action' => 'delete',
732
// Get current order id...
733
$sql = "SELECT {$fields}_order as current_order
735
WHERE {$fields}_id = $icon_id";
736
$result = $db->sql_query($sql);
737
$current_order = (int) $db->sql_fetchfield('current_order');
738
$db->sql_freeresult($result);
740
if ($current_order == 0 && $action == 'move_up')
745
// on move_down, switch position with next order_id...
746
// on move_up, switch position with previous order_id...
747
$switch_order_id = ($action == 'move_down') ? $current_order + 1 : $current_order - 1;
750
$sql = "UPDATE $table
751
SET {$fields}_order = $current_order
752
WHERE {$fields}_order = $switch_order_id
753
AND {$fields}_id <> $icon_id";
754
$db->sql_query($sql);
756
// Only update the other entry too if the previous entry got updated
757
if ($db->sql_affectedrows())
759
$sql = "UPDATE $table
760
SET {$fields}_order = $switch_order_id
761
WHERE {$fields}_order = $current_order
762
AND {$fields}_id = $icon_id";
763
$db->sql_query($sql);
766
$cache->destroy('_icons');
767
$cache->destroy('sql', $table);
772
// By default, check that image_order is valid and fix it if necessary
773
$sql = "SELECT {$fields}_id AS order_id, {$fields}_order AS fields_order
775
ORDER BY display_on_posting DESC, {$fields}_order";
776
$result = $db->sql_query($sql);
778
if ($row = $db->sql_fetchrow($result))
784
if ($row['fields_order'] != $order)
786
$db->sql_query("UPDATE $table
787
SET {$fields}_order = $order
788
WHERE {$fields}_id = " . $row['order_id']);
791
while ($row = $db->sql_fetchrow($result));
793
$db->sql_freeresult($result);
795
$template->assign_vars(array(
796
'L_TITLE' => $user->lang['ACP_' . $lang],
797
'L_EXPLAIN' => $user->lang['ACP_' . $lang . '_EXPLAIN'],
798
'L_IMPORT' => $user->lang['IMPORT_' . $lang],
799
'L_EXPORT' => $user->lang['EXPORT_' . $lang],
800
'L_NOT_DISPLAYED' => $user->lang[$lang . '_NOT_DISPLAYED'],
801
'L_ICON_ADD' => $user->lang['ADD_' . $lang],
802
'L_ICON_EDIT' => $user->lang['EDIT_' . $lang],
805
'COLSPAN' => ($mode == 'smilies') ? 5 : 3,
807
'S_SMILIES' => ($mode == 'smilies') ? true : false,
809
'U_ACTION' => $this->u_action,
810
'U_IMPORT' => $this->u_action . '&action=import',
811
'U_EXPORT' => $this->u_action . '&action=export',
819
ORDER BY {$fields}_order ASC";
820
$result = $db->sql_query($sql);
822
while ($row = $db->sql_fetchrow($result))
824
$alt_text = ($mode == 'smilies') ? $row['code'] : '';
826
$template->assign_block_vars('items', array(
827
'S_SPACER' => (!$spacer && !$row['display_on_posting']) ? true : false,
828
'ALT_TEXT' => $alt_text,
829
'IMG_SRC' => $phpbb_root_path . $img_path . '/' . $row[$fields . '_url'],
830
'WIDTH' => $row[$fields . '_width'],
831
'HEIGHT' => $row[$fields . '_height'],
832
'CODE' => (isset($row['code'])) ? $row['code'] : '',
833
'EMOTION' => (isset($row['emotion'])) ? $row['emotion'] : '',
834
'U_EDIT' => $this->u_action . '&action=edit&id=' . $row[$fields . '_id'],
835
'U_DELETE' => $this->u_action . '&action=delete&id=' . $row[$fields . '_id'],
836
'U_MOVE_UP' => $this->u_action . '&action=move_up&id=' . $row[$fields . '_id'],
837
'U_MOVE_DOWN' => $this->u_action . '&action=move_down&id=' . $row[$fields . '_id'])
840
if (!$spacer && !$row['display_on_posting'])
845
$db->sql_freeresult($result);
b'\\ No newline at end of file'