Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

acp_icons.php

Zuletzt modifiziert: 09.10.2024, 12:52 - Dateigröße: 27.44 KiB


001  <?php
002  /**
003  *
004  * This file is part of the phpBB Forum Software package.
005  *
006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
007  * @license GNU General Public License, version 2 (GPL-2.0)
008  *
009  * For full copyright and license information, please see
010  * the docs/CREDITS.txt file.
011  *
012  */
013   
014  /**
015  * @ignore
016  */
017  if (!defined('IN_PHPBB'))
018  {
019      exit;
020  }
021   
022  /**
023  * @todo [smilies] check regular expressions for special char replacements (stored specialchared in db)
024  */
025  class acp_icons
026  {
027      var $u_action;
028   
029      function main($id, $mode)
030      {
031          global $db, $user, $auth, $template, $cache;
032          global $config, $phpbb_root_path, $phpbb_admin_path, $phpEx;
033          global $request, $phpbb_container;
034   
035          $user->add_lang('acp/posting');
036   
037          // Set up general vars
038          $action = request_var('action', '');
039          $action = (isset($_POST['add'])) ? 'add' : $action;
040          $action = (isset($_POST['edit'])) ? 'edit' : $action;
041          $action = (isset($_POST['import'])) ? 'import' : $action;
042          $icon_id = request_var('id', 0);
043   
044          $mode = ($mode == 'smilies') ? 'smilies' : 'icons';
045   
046          $this->tpl_name = 'acp_icons';
047   
048          // What are we working on?
049          switch ($mode)
050          {
051              case 'smilies':
052                  $table = SMILIES_TABLE;
053                  $lang = 'SMILIES';
054                  $fields = 'smiley';
055                  $img_path = $config['smilies_path'];
056              break;
057   
058              case 'icons':
059                  $table = ICONS_TABLE;
060                  $lang = 'ICONS';
061                  $fields = 'icons';
062                  $img_path = $config['icons_path'];
063              break;
064          }
065   
066          $this->page_title = 'ACP_' . $lang;
067   
068          // Clear some arrays
069          $_images = $_paks = array();
070          $notice = '';
071   
072          // Grab file list of paks and images
073          if ($action == 'edit' || $action == 'add' || $action == 'import')
074          {
075              $imglist = filelist($phpbb_root_path . $img_path, '');
076   
077              foreach ($imglist as $path => $img_ary)
078              {
079                  if (empty($img_ary))
080                  {
081                      continue;
082                  }
083   
084                  asort($img_ary, SORT_STRING);
085   
086                  foreach ($img_ary as $img)
087                  {
088                      $img_size = getimagesize($phpbb_root_path . $img_path . '/' . $path . $img);
089   
090                      if (!$img_size[0] || !$img_size[1] || strlen($img) > 255)
091                      {
092                          continue;
093                      }
094   
095                      // adjust the width and height to be lower than 128px while perserving the aspect ratio (for icons)
096                      if ($mode == 'icons')
097                      {
098                          if ($img_size[0] > 127 && $img_size[0] > $img_size[1])
099                          {
100                              $img_size[1] = (int) ($img_size[1] * (127 / $img_size[0]));
101                              $img_size[0] = 127;
102                          }
103                          else if ($img_size[1] > 127)
104                          {
105                              $img_size[0] = (int) ($img_size[0] * (127 / $img_size[1]));
106                              $img_size[1] = 127;
107                          }
108                      }
109   
110                      $_images[$path . $img]['file'] = $path . $img;
111                      $_images[$path . $img]['width'] = $img_size[0];
112                      $_images[$path . $img]['height'] = $img_size[1];
113                  }
114              }
115              unset($imglist);
116   
117              if ($dir = @opendir($phpbb_root_path . $img_path))
118              {
119                  while (($file = readdir($dir)) !== false)
120                  {
121                      if (is_file($phpbb_root_path . $img_path . '/' . $file) && preg_match('#\.pak$#i', $file))
122                      {
123                          $_paks[] = $file;
124                      }
125                  }
126                  closedir($dir);
127   
128                  if (!empty($_paks))
129                  {
130                      asort($_paks, SORT_STRING);
131                  }
132              }
133          }
134   
135          // What shall we do today? Oops, I believe that's trademarked ...
136          switch ($action)
137          {
138              case 'edit':
139                  unset($_images);
140                  $_images = array();
141   
142              // no break;
143   
144              case 'add':
145   
146                  $smilies = $default_row = array();
147                  $smiley_options = $order_list = $add_order_list = '';
148   
149                  if ($action == 'add' && $mode == 'smilies')
150                  {
151                      $sql = 'SELECT *
152                          FROM ' . SMILIES_TABLE . '
153                          ORDER BY smiley_order';
154                      $result = $db->sql_query($sql);
155   
156                      while ($row = $db->sql_fetchrow($result))
157                      {
158                          if (empty($smilies[$row['smiley_url']]))
159                          {
160                              $smilies[$row['smiley_url']] = $row;
161                          }
162                      }
163                      $db->sql_freeresult($result);
164   
165                      if (sizeof($smilies))
166                      {
167                          foreach ($smilies as $row)
168                          {
169                              $selected = false;
170   
171                              if (!$smiley_options)
172                              {
173                                  $selected = true;
174                                  $default_row = $row;
175                              }
176                              $smiley_options .= '<option value="' . $row['smiley_url'] . '"' . (($selected) ? ' selected="selected"' : '') . '>' . $row['smiley_url'] . '</option>';
177   
178                              $template->assign_block_vars('smile', array(
179                                  'SMILEY_URL'    => addslashes($row['smiley_url']),
180                                  'CODE'            => addslashes($row['code']),
181                                  'EMOTION'        => addslashes($row['emotion']),
182                                  'WIDTH'            => $row['smiley_width'],
183                                  'HEIGHT'        => $row['smiley_height'],
184                                  'ORDER'            => $row['smiley_order'] + 1,
185                              ));
186                          }
187                      }
188                  }
189   
190                  $sql = "SELECT *
191                      FROM $table
192                      ORDER BY {$fields}_order " . (($icon_id || $action == 'add') ? 'DESC' : 'ASC');
193                  $result = $db->sql_query($sql);
194   
195                  $data = array();
196                  $after = false;
197                  $display = 0;
198                  $order_lists = array('', '');
199                  $add_order_lists = array('', '');
200                  $display_count = 0;
201   
202                  while ($row = $db->sql_fetchrow($result))
203                  {
204                      if ($action == 'add')
205                      {
206                          unset($_images[$row[$fields . '_url']]);
207                      }
208   
209                      if ($row[$fields . '_id'] == $icon_id)
210                      {
211                          $after = true;
212                          $display = $row['display_on_posting'];
213                          $data[$row[$fields . '_url']] = $row;
214                      }
215                      else
216                      {
217                          if ($action == 'edit' && !$icon_id)
218                          {
219                              $data[$row[$fields . '_url']] = $row;
220                          }
221   
222                          $selected = '';
223                          if (!empty($after))
224                          {
225                              $selected = ' selected="selected"';
226                              $after = false;
227                          }
228                          if ($row['display_on_posting'])
229                          {
230                              $display_count++;
231                          }
232                          $after_txt = ($mode == 'smilies') ? $row['code'] : $row['icons_url'];
233                          $order_lists[$row['display_on_posting']] = '<option value="' . ($row[$fields . '_order'] + 1) . '"' . $selected . '>' . sprintf($user->lang['AFTER_' . $lang], ' -&gt; ' . $after_txt) . '</option>' . $order_lists[$row['display_on_posting']];
234   
235                          if (!empty($default_row))
236                          {
237                              $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], ' -&gt; ' . $after_txt) . '</option>' . $add_order_lists[$row['display_on_posting']];
238                          }
239                      }
240                  }
241                  $db->sql_freeresult($result);
242   
243                  $order_list = '<option value="1"' . ((!isset($after)) ? ' selected="selected"' : '') . '>' . $user->lang['FIRST'] . '</option>';
244                  $add_order_list = '<option value="1">' . $user->lang['FIRST'] . '</option>';
245   
246                  if ($action == 'add')
247                  {
248                      $data = $_images;
249                  }
250   
251                  $colspan = (($mode == 'smilies') ? 7 : 5);
252                  $colspan += ($icon_id) ? 1 : 0;
253                  $colspan += ($action == 'add') ? 2 : 0;
254   
255                  $template->assign_vars(array(
256                      'S_EDIT'        => true,
257                      'S_SMILIES'        => ($mode == 'smilies') ? true : false,
258                      'S_ADD'            => ($action == 'add') ? true : false,
259   
260                      'S_ORDER_LIST_DISPLAY'        => $order_list . $order_lists[1],
261                      'S_ORDER_LIST_UNDISPLAY'    => $order_list . $order_lists[0],
262                      'S_ORDER_LIST_DISPLAY_COUNT'    => $display_count + 1,
263   
264                      'L_TITLE'        => $user->lang['ACP_' . $lang],
265                      'L_EXPLAIN'        => $user->lang['ACP_' . $lang . '_EXPLAIN'],
266                      'L_CONFIG'        => $user->lang[$lang . '_CONFIG'],
267                      'L_URL'            => $user->lang[$lang . '_URL'],
268                      'L_LOCATION'    => $user->lang[$lang . '_LOCATION'],
269                      'L_WIDTH'        => $user->lang[$lang . '_WIDTH'],
270                      'L_HEIGHT'        => $user->lang[$lang . '_HEIGHT'],
271                      'L_ORDER'        => $user->lang[$lang . '_ORDER'],
272                      'L_NO_ICONS'    => $user->lang['NO_' . $lang . '_' . strtoupper($action)],
273   
274                      'COLSPAN'        => $colspan,
275                      'ID'            => $icon_id,
276   
277                      'U_BACK'        => $this->u_action,
278                      'U_ACTION'        => $this->u_action . '&amp;action=' . (($action == 'add') ? 'create' : 'modify'),
279                  ));
280   
281                  foreach ($data as $img => $img_row)
282                  {
283                      $template->assign_block_vars('items', array(
284                          'IMG'        => $img,
285                          'A_IMG'        => addslashes($img),
286                          'IMG_SRC'    => $phpbb_root_path . $img_path . '/' . $img,
287   
288                          'CODE'        => ($mode == 'smilies' && isset($img_row['code'])) ? $img_row['code'] : '',
289                          'EMOTION'    => ($mode == 'smilies' && isset($img_row['emotion'])) ? $img_row['emotion'] : '',
290   
291                          'S_ID'                => (isset($img_row[$fields . '_id'])) ? true : false,
292                          'ID'                => (isset($img_row[$fields . '_id'])) ? $img_row[$fields . '_id'] : 0,
293                          'WIDTH'                => (!empty($img_row[$fields .'_width'])) ? $img_row[$fields .'_width'] : $img_row['width'],
294                          'HEIGHT'            => (!empty($img_row[$fields .'_height'])) ? $img_row[$fields .'_height'] : $img_row['height'],
295                          'POSTING_CHECKED'    => (!empty($img_row['display_on_posting']) || $action == 'add') ? ' checked="checked"' : '',
296                      ));
297                  }
298   
299                  // Ok, another row for adding an addition code for a pre-existing image...
300                  if ($action == 'add' && $mode == 'smilies' && sizeof($smilies))
301                  {
302                      $template->assign_vars(array(
303                          'S_ADD_CODE'        => true,
304   
305                          'S_IMG_OPTIONS'        => $smiley_options,
306   
307                          'S_ADD_ORDER_LIST_DISPLAY'        => $add_order_list . $add_order_lists[1],
308                          'S_ADD_ORDER_LIST_UNDISPLAY'    => $add_order_list . $add_order_lists[0],
309   
310                          'IMG_SRC'            => $phpbb_root_path . $img_path . '/' . $default_row['smiley_url'],
311                          'IMG_PATH'            => $img_path,
312   
313                          'CODE'                => $default_row['code'],
314                          'EMOTION'            => $default_row['emotion'],
315   
316                          'WIDTH'                => $default_row['smiley_width'],
317                          'HEIGHT'            => $default_row['smiley_height'],
318                      ));
319                  }
320   
321                  return;
322   
323              break;
324   
325              case 'create':
326              case 'modify':
327   
328                  // Get items to create/modify
329                  $images = (isset($_POST['image'])) ? array_keys(request_var('image', array('' => 0))) : array();
330   
331                  // Now really get the items
332                  $image_id        = (isset($_POST['id'])) ? request_var('id', array('' => 0)) : array();
333                  $image_order    = (isset($_POST['order'])) ? request_var('order', array('' => 0)) : array();
334                  $image_width    = (isset($_POST['width'])) ? request_var('width', array('' => 0)) : array();
335                  $image_height    = (isset($_POST['height'])) ? request_var('height', array('' => 0)) : array();
336                  $image_add        = (isset($_POST['add_img'])) ? request_var('add_img', array('' => 0)) : array();
337                  $image_emotion    = utf8_normalize_nfc(request_var('emotion', array('' => ''), true));
338                  $image_code        = utf8_normalize_nfc(request_var('code', array('' => ''), true));
339                  $image_display_on_posting = (isset($_POST['display_on_posting'])) ? request_var('display_on_posting', array('' => 0)) : array();
340   
341                  // Ok, add the relevant bits if we are adding new codes to existing emoticons...
342                  if ($request->variable('add_additional_code', false, false, \phpbb\request\request_interface::POST))
343                  {
344                      $add_image            = request_var('add_image', '');
345                      $add_code            = utf8_normalize_nfc(request_var('add_code', '', true));
346                      $add_emotion        = utf8_normalize_nfc(request_var('add_emotion', '', true));
347   
348                      if ($add_image && $add_emotion && $add_code)
349                      {
350                          $images[] = $add_image;
351                          $image_add[$add_image] = true;
352   
353                          $image_code[$add_image] = $add_code;
354                          $image_emotion[$add_image] = $add_emotion;
355                          $image_width[$add_image] = request_var('add_width', 0);
356                          $image_height[$add_image] = request_var('add_height', 0);
357   
358                          if ($request->variable('add_display_on_posting', false, false, \phpbb\request\request_interface::POST))
359                          {
360                              $image_display_on_posting[$add_image] = 1;
361                          }
362   
363                          $image_order[$add_image] = request_var('add_order', 0);
364                      }
365                  }
366   
367                  if ($mode == 'smilies' && $action == 'create')
368                  {
369                      $smiley_count = $this->item_count($table);
370   
371                      $addable_smileys_count = sizeof($images);
372                      foreach ($images as $image)
373                      {
374                          if (!isset($image_add[$image]))
375                          {
376                              --$addable_smileys_count;
377                          }
378                      }
379   
380                      if ($smiley_count + $addable_smileys_count > SMILEY_LIMIT)
381                      {
382                          trigger_error($user->lang('TOO_MANY_SMILIES', SMILEY_LIMIT) . adm_back_link($this->u_action), E_USER_WARNING);
383                      }
384                  }
385   
386                  $icons_updated = 0;
387                  $errors = array();
388                  foreach ($images as $image)
389                  {
390                      if ($mode == 'smilies' && ($image_emotion[$image] == '' || $image_code[$image] == ''))
391                      {
392                          $errors[$image] = 'SMILIE_NO_' . (($image_emotion[$image] == '') ? 'EMOTION' : 'CODE');
393                      }
394                      else if ($action == 'create' && !isset($image_add[$image]))
395                      {
396                          // skip images where add wasn't checked
397                      }
398                      else if (!file_exists($phpbb_root_path . $img_path . '/' . $image))
399                      {
400                          $errors[$image] = 'SMILIE_NO_FILE';
401                      }
402                      else
403                      {
404                          if ($image_width[$image] == 0 || $image_height[$image] == 0)
405                          {
406                              $img_size = getimagesize($phpbb_root_path . $img_path . '/' . $image);
407                              $image_width[$image] = $img_size[0];
408                              $image_height[$image] = $img_size[1];
409                          }
410   
411                          // Adjust image width/height for icons
412                          if ($mode == 'icons')
413                          {
414                              if ($image_width[$image] > 127 && $image_width[$image] > $image_height[$image])
415                              {
416                                  $image_height[$image] = (int) ($image_height[$image] * (127 / $image_width[$image]));
417                                  $image_width[$image] = 127;
418                              }
419                              else if ($image_height[$image] > 127)
420                              {
421                                  $image_width[$image] = (int) ($image_width[$image] * (127 / $image_height[$image]));
422                                  $image_height[$image] = 127;
423                              }
424                          }
425   
426                          $img_sql = array(
427                              $fields . '_url'        => $image,
428                              $fields . '_width'        => $image_width[$image],
429                              $fields . '_height'        => $image_height[$image],
430                              'display_on_posting'    => (isset($image_display_on_posting[$image])) ? 1 : 0,
431                          );
432   
433                          if ($mode == 'smilies')
434                          {
435                              $img_sql = array_merge($img_sql, array(
436                                  'emotion'    => $image_emotion[$image],
437                                  'code'        => $image_code[$image])
438                              );
439                          }
440   
441                          // Image_order holds the 'new' order value
442                          if (!empty($image_order[$image]))
443                          {
444                              $img_sql = array_merge($img_sql, array(
445                                  $fields . '_order'    =>    $image_order[$image])
446                              );
447   
448                              // Since we always add 'after' an item, we just need to increase all following + the current by one
449                              $sql = "UPDATE $table
450                                  SET {$fields}_order = {$fields}_order + 1
451                                  WHERE {$fields}_order >= {$image_order[$image]}";
452                              $db->sql_query($sql);
453   
454                              // If we adjust the order, we need to adjust all other orders too - they became inaccurate...
455                              foreach ($image_order as $_image => $_order)
456                              {
457                                  if ($_image == $image)
458                                  {
459                                      continue;
460                                  }
461   
462                                  if ($_order >= $image_order[$image])
463                                  {
464                                      $image_order[$_image]++;
465                                  }
466                              }
467                          }
468   
469                          if ($action == 'modify'  && !empty($image_id[$image]))
470                          {
471                              $sql = "UPDATE $table
472                                  SET " . $db->sql_build_array('UPDATE', $img_sql) . "
473                                  WHERE {$fields}_id = " . $image_id[$image];
474                              $db->sql_query($sql);
475                              $icons_updated++;
476                          }
477                          else if ($action !== 'modify')
478                          {
479                              $sql = "INSERT INTO $table " . $db->sql_build_array('INSERT', $img_sql);
480                              $db->sql_query($sql);
481                              $icons_updated++;
482                          }
483   
484                      }
485                  }
486   
487                  $cache->destroy('_icons');
488                  $cache->destroy('sql', $table);
489   
490                  $level = ($icons_updated) ? E_USER_NOTICE : E_USER_WARNING;
491                  $errormsgs = '';
492                  foreach ($errors as $img => $error)
493                  {
494                      $errormsgs .= '<br />' . sprintf($user->lang[$error], $img);
495                  }
496                  if ($action == 'modify')
497                  {
498                      trigger_error($user->lang($lang . '_EDITED', $icons_updated) . $errormsgs . adm_back_link($this->u_action), $level);
499                  }
500                  else
501                  {
502                      trigger_error($user->lang($lang . '_ADDED', $icons_updated) . $errormsgs . adm_back_link($this->u_action), $level);
503                  }
504   
505              break;
506   
507              case 'import':
508   
509                  $pak = request_var('pak', '');
510                  $current = request_var('current', '');
511   
512                  if ($pak != '')
513                  {
514                      $order = 0;
515   
516                      if (!($pak_ary = @file($phpbb_root_path . $img_path . '/' . $pak)))
517                      {
518                          trigger_error($user->lang['PAK_FILE_NOT_READABLE'] . adm_back_link($this->u_action), E_USER_WARNING);
519                      }
520   
521                      // Make sure the pak_ary is valid
522                      foreach ($pak_ary as $pak_entry)
523                      {
524                          if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
525                          {
526                              if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
527                                  ((sizeof($data[1]) != 6 || (empty($data[1][4]) || empty($data[1][5]))) && $mode == 'smilies' ))
528                              {
529                                  trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
530                              }
531                          }
532                          else
533                          {
534                              trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
535                          }
536                      }
537   
538                      // The user has already selected a smilies_pak file
539                      if ($current == 'delete')
540                      {
541                          switch ($db->get_sql_layer())
542                          {
543                              case 'sqlite':
544                              case 'sqlite3':
545                                  $db->sql_query('DELETE FROM ' . $table);
546                              break;
547   
548                              default:
549                                  $db->sql_query('TRUNCATE TABLE ' . $table);
550                              break;
551                          }
552   
553                          switch ($mode)
554                          {
555                              case 'smilies':
556                              break;
557   
558                              case 'icons':
559                                  // Reset all icon_ids
560                                  $db->sql_query('UPDATE ' . TOPICS_TABLE . ' SET icon_id = 0');
561                                  $db->sql_query('UPDATE ' . POSTS_TABLE . ' SET icon_id = 0');
562                              break;
563                          }
564                      }
565                      else
566                      {
567                          $cur_img = array();
568   
569                          $field_sql = ($mode == 'smilies') ? 'code' : 'icons_url';
570   
571                          $sql = "SELECT $field_sql
572                              FROM $table";
573                          $result = $db->sql_query($sql);
574   
575                          while ($row = $db->sql_fetchrow($result))
576                          {
577                              ++$order;
578                              $cur_img[$row[$field_sql]] = 1;
579                          }
580                          $db->sql_freeresult($result);
581                      }
582   
583                      if ($mode == 'smilies')
584                      {
585                          $smiley_count = $this->item_count($table);
586                          if ($smiley_count + sizeof($pak_ary) > SMILEY_LIMIT)
587                          {
588                              trigger_error($user->lang('TOO_MANY_SMILIES', SMILEY_LIMIT) . adm_back_link($this->u_action), E_USER_WARNING);
589                          }
590                      }
591   
592                      foreach ($pak_ary as $pak_entry)
593                      {
594                          $data = array();
595                          if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
596                          {
597                              if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
598                                  (sizeof($data[1]) != 6 && $mode == 'smilies'))
599                              {
600                                  trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
601                              }
602   
603                              // Stripslash here because it got addslashed before... (on export)
604                              $img = stripslashes($data[1][0]);
605                              $width = stripslashes($data[1][1]);
606                              $height = stripslashes($data[1][2]);
607                              $display_on_posting = stripslashes($data[1][3]);
608   
609                              if (isset($data[1][4]) && isset($data[1][5]))
610                              {
611                                  $emotion = stripslashes($data[1][4]);
612                                  $code = stripslashes($data[1][5]);
613                              }
614   
615                              if ($current == 'replace' &&
616                                  (($mode == 'smilies' && !empty($cur_img[$code])) ||
617                                  ($mode == 'icons' && !empty($cur_img[$img]))))
618                              {
619                                  $replace_sql = ($mode == 'smilies') ? $code : $img;
620                                  $sql = array(
621                                      $fields . '_url'        => $img,
622                                      $fields . '_height'        => (int) $height,
623                                      $fields . '_width'        => (int) $width,
624                                      'display_on_posting'    => (int) $display_on_posting,
625                                  );
626   
627                                  if ($mode == 'smilies')
628                                  {
629                                      $sql = array_merge($sql, array(
630                                          'emotion'                => $emotion,
631                                      ));
632                                  }
633   
634                                  $sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql) . "
635                                      WHERE $field_sql = '" . $db->sql_escape($replace_sql) . "'";
636                                  $db->sql_query($sql);
637                              }
638                              else
639                              {
640                                  ++$order;
641   
642                                  $sql = array(
643                                      $fields . '_url'    => $img,
644                                      $fields . '_height'    => (int) $height,
645                                      $fields . '_width'    => (int) $width,
646                                      $fields . '_order'    => (int) $order,
647                                      'display_on_posting'=> (int) $display_on_posting,
648                                  );
649   
650                                  if ($mode == 'smilies')
651                                  {
652                                      $sql = array_merge($sql, array(
653                                          'code'                => $code,
654                                          'emotion'            => $emotion,
655                                      ));
656                                  }
657                                  $db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql));
658                              }
659                          }
660                      }
661   
662                      $cache->destroy('_icons');
663                      $cache->destroy('sql', $table);
664   
665                      trigger_error($user->lang[$lang . '_IMPORT_SUCCESS'] . adm_back_link($this->u_action));
666                  }
667                  else
668                  {
669                      $pak_options = '';
670   
671                      foreach ($_paks as $pak)
672                      {
673                          $pak_options .= '<option value="' . $pak . '">' . htmlspecialchars($pak) . '</option>';
674                      }
675   
676                      $template->assign_vars(array(
677                          'S_CHOOSE_PAK'        => true,
678                          'S_PAK_OPTIONS'        => $pak_options,
679   
680                          'L_TITLE'            => $user->lang['ACP_' . $lang],
681                          'L_EXPLAIN'            => $user->lang['ACP_' . $lang . '_EXPLAIN'],
682                          'L_NO_PAK_OPTIONS'    => $user->lang['NO_' . $lang . '_PAK'],
683                          'L_CURRENT'            => $user->lang['CURRENT_' . $lang],
684                          'L_CURRENT_EXPLAIN'    => $user->lang['CURRENT_' . $lang . '_EXPLAIN'],
685                          'L_IMPORT_SUBMIT'    => $user->lang['IMPORT_' . $lang],
686   
687                          'U_BACK'        => $this->u_action,
688                          'U_ACTION'        => $this->u_action . '&amp;action=import',
689                          )
690                      );
691                  }
692              break;
693   
694              case 'export':
695   
696                  $this->page_title = 'EXPORT_' . $lang;
697                  $this->tpl_name = 'message_body';
698   
699                  $template->assign_vars(array(
700                      'MESSAGE_TITLE'        => $user->lang['EXPORT_' . $lang],
701                      'MESSAGE_TEXT'        => sprintf($user->lang['EXPORT_' . $lang . '_EXPLAIN'], '<a href="' . $this->u_action . '&amp;action=send">', '</a>'),
702   
703                      'S_USER_NOTICE'        => true,
704                      )
705                  );
706   
707                  return;
708   
709              break;
710   
711              case 'send':
712   
713                  $sql = "SELECT *
714                      FROM $table
715                      ORDER BY {$fields}_order";
716                  $result = $db->sql_query($sql);
717   
718                  $pak = '';
719                  while ($row = $db->sql_fetchrow($result))
720                  {
721                      $pak .= "'" . addslashes($row[$fields . '_url']) . "', ";
722                      $pak .= "'" . addslashes($row[$fields . '_width']) . "', ";
723                      $pak .= "'" . addslashes($row[$fields . '_height']) . "', ";
724                      $pak .= "'" . addslashes($row['display_on_posting']) . "', ";
725   
726                      if ($mode == 'smilies')
727                      {
728                          $pak .= "'" . addslashes($row['emotion']) . "', ";
729                          $pak .= "'" . addslashes($row['code']) . "', ";
730                      }
731   
732                      $pak .= "\n";
733                  }
734                  $db->sql_freeresult($result);
735   
736                  if ($pak != '')
737                  {
738                      garbage_collection();
739   
740                      header('Cache-Control: public');
741   
742                      // Send out the Headers
743                      header('Content-Type: text/x-delimtext; name="' . $mode . '.pak"');
744                      header('Content-Disposition: inline; filename="' . $mode . '.pak"');
745                      echo $pak;
746   
747                      flush();
748                      exit;
749                  }
750                  else
751                  {
752                      trigger_error($user->lang['NO_' . strtoupper($fields) . '_EXPORT'] . adm_back_link($this->u_action), E_USER_WARNING);
753                  }
754   
755              break;
756   
757              case 'delete':
758   
759                  if (confirm_box(true))
760                  {
761                      $sql = "DELETE FROM $table
762                          WHERE {$fields}_id = $icon_id";
763                      $db->sql_query($sql);
764   
765                      switch ($mode)
766                      {
767                          case 'smilies':
768                          break;
769   
770                          case 'icons':
771                              // Reset appropriate icon_ids
772                              $db->sql_query('UPDATE ' . TOPICS_TABLE . "
773                                  SET icon_id = 0
774                                  WHERE icon_id = $icon_id");
775   
776                              $db->sql_query('UPDATE ' . POSTS_TABLE . "
777                                  SET icon_id = 0
778                                  WHERE icon_id = $icon_id");
779                          break;
780                      }
781   
782                      $notice = $user->lang[$lang . '_DELETED'];
783   
784                      $cache->destroy('_icons');
785                      $cache->destroy('sql', $table);
786   
787                      if ($request->is_ajax())
788                      {
789                          $json_response = new \phpbb\json_response;
790                          $json_response->send(array(
791                              'MESSAGE_TITLE'    => $user->lang['INFORMATION'],
792                              'MESSAGE_TEXT'    => $notice,
793                              'REFRESH_DATA'    => array(
794                                  'time'    => 3
795                              )
796                          ));
797                      }
798                  }
799                  else
800                  {
801                      confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
802                          'i'            => $id,
803                          'mode'        => $mode,
804                          'id'        => $icon_id,
805                          'action'    => 'delete',
806                      )));
807                  }
808   
809              break;
810   
811              case 'move_up':
812              case 'move_down':
813   
814                  // Get current order id...
815                  $sql = "SELECT {$fields}_order as current_order
816                      FROM $table
817                      WHERE {$fields}_id = $icon_id";
818                  $result = $db->sql_query($sql);
819                  $current_order = (int) $db->sql_fetchfield('current_order');
820                  $db->sql_freeresult($result);
821   
822                  if ($current_order == 0 && $action == 'move_up')
823                  {
824                      break;
825                  }
826   
827                  // on move_down, switch position with next order_id...
828                  // on move_up, switch position with previous order_id...
829                  $switch_order_id = ($action == 'move_down') ? $current_order + 1 : $current_order - 1;
830   
831                  //
832                  $sql = "UPDATE $table
833                      SET {$fields}_order = $current_order
834                      WHERE {$fields}_order = $switch_order_id
835                          AND {$fields}_id <> $icon_id";
836                  $db->sql_query($sql);
837                  $move_executed = (bool) $db->sql_affectedrows();
838   
839                  // Only update the other entry too if the previous entry got updated
840                  if ($move_executed)
841                  {
842                      $sql = "UPDATE $table
843                          SET {$fields}_order = $switch_order_id
844                          WHERE {$fields}_order = $current_order
845                              AND {$fields}_id = $icon_id";
846                      $db->sql_query($sql);
847                  }
848   
849                  $cache->destroy('_icons');
850                  $cache->destroy('sql', $table);
851   
852                  if ($request->is_ajax())
853                  {
854                      $json_response = new \phpbb\json_response;
855                      $json_response->send(array(
856                          'success'    => $move_executed,
857                      ));
858                  }
859   
860              break;
861          }
862   
863          // By default, check that image_order is valid and fix it if necessary
864          $sql = "SELECT {$fields}_id AS order_id, {$fields}_order AS fields_order
865              FROM $table
866              ORDER BY display_on_posting DESC, {$fields}_order";
867          $result = $db->sql_query($sql);
868   
869          if ($row = $db->sql_fetchrow($result))
870          {
871              $order = 0;
872              do
873              {
874                  ++$order;
875                  if ($row['fields_order'] != $order)
876                  {
877                      $db->sql_query("UPDATE $table
878                          SET {$fields}_order = $order
879                          WHERE {$fields}_id = " . $row['order_id']);
880                  }
881              }
882              while ($row = $db->sql_fetchrow($result));
883          }
884          $db->sql_freeresult($result);
885   
886          $template->assign_vars(array(
887              'L_TITLE'            => $user->lang['ACP_' . $lang],
888              'L_EXPLAIN'            => $user->lang['ACP_' . $lang . '_EXPLAIN'],
889              'L_IMPORT'            => $user->lang['IMPORT_' . $lang],
890              'L_EXPORT'            => $user->lang['EXPORT_' . $lang],
891              'L_NOT_DISPLAYED'    => $user->lang[$lang . '_NOT_DISPLAYED'],
892              'L_ICON_ADD'        => $user->lang['ADD_' . $lang],
893              'L_ICON_EDIT'        => $user->lang['EDIT_' . $lang],
894   
895              'NOTICE'            => $notice,
896              'COLSPAN'            => ($mode == 'smilies') ? 5 : 3,
897   
898              'S_SMILIES'            => ($mode == 'smilies') ? true : false,
899   
900              'U_ACTION'            => $this->u_action,
901              'U_IMPORT'            => $this->u_action . '&amp;action=import',
902              'U_EXPORT'            => $this->u_action . '&amp;action=export',
903              )
904          );
905   
906          $spacer = false;
907          $pagination = $phpbb_container->get('pagination');
908          $pagination_start = request_var('start', 0);
909   
910          $item_count = $this->item_count($table);
911   
912          $sql = "SELECT *
913              FROM $table
914              ORDER BY {$fields}_order ASC";
915          $result = $db->sql_query_limit($sql, $config['smilies_per_page'], $pagination_start);
916   
917          while ($row = $db->sql_fetchrow($result))
918          {
919              $alt_text = ($mode == 'smilies') ? $row['code'] : '';
920   
921              $template->assign_block_vars('items', array(
922                  'S_SPACER'        => (!$spacer && !$row['display_on_posting']) ? true : false,
923                  'ALT_TEXT'        => $alt_text,
924                  'IMG_SRC'        => $phpbb_root_path . $img_path . '/' . $row[$fields . '_url'],
925                  'WIDTH'            => $row[$fields . '_width'],
926                  'HEIGHT'        => $row[$fields . '_height'],
927                  'CODE'            => (isset($row['code'])) ? $row['code'] : '',
928                  'EMOTION'        => (isset($row['emotion'])) ? $row['emotion'] : '',
929                  'U_EDIT'        => $this->u_action . '&amp;action=edit&amp;id=' . $row[$fields . '_id'],
930                  'U_DELETE'        => $this->u_action . '&amp;action=delete&amp;id=' . $row[$fields . '_id'],
931                  'U_MOVE_UP'        => $this->u_action . '&amp;action=move_up&amp;id=' . $row[$fields . '_id'] . '&amp;start=' . $pagination_start,
932                  'U_MOVE_DOWN'    => $this->u_action . '&amp;action=move_down&amp;id=' . $row[$fields . '_id'] . '&amp;start=' . $pagination_start,
933              ));
934   
935              if (!$spacer && !$row['display_on_posting'])
936              {
937                  $spacer = true;
938              }
939          }
940          $db->sql_freeresult($result);
941   
942          $pagination->generate_template_pagination($this->u_action, 'pagination', 'start', $item_count, $config['smilies_per_page'], $pagination_start);
943      }
944   
945      /**
946       * Returns the count of smilies or icons in the database
947       *
948       * @param string $table The table of items to count.
949       * @return int number of items
950       */
951      /* private */ function item_count($table)
952      {
953          global $db;
954   
955          $sql = "SELECT COUNT(*) AS item_count
956              FROM $table";
957          $result = $db->sql_query($sql);
958          $item_count = (int) $db->sql_fetchfield('item_count');
959          $db->sql_freeresult($result);
960   
961          return $item_count;
962      }
963  }
964