Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

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: 29.11 KiB


0001  <?php
0002  /**
0003  *
0004  * This file is part of the phpBB Forum Software package.
0005  *
0006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
0007  * @license GNU General Public License, version 2 (GPL-2.0)
0008  *
0009  * For full copyright and license information, please see
0010  * the docs/CREDITS.txt file.
0011  *
0012  */
0013   
0014  /**
0015  * @ignore
0016  */
0017  if (!defined('IN_PHPBB'))
0018  {
0019      exit;
0020  }
0021   
0022  /**
0023  * @todo [smilies] check regular expressions for special char replacements (stored specialchared in db)
0024  */
0025  class acp_icons
0026  {
0027      var $u_action;
0028   
0029      function main($id, $mode)
0030      {
0031          global $db, $user, $template, $cache;
0032          global $config, $phpbb_root_path;
0033          global $request, $phpbb_container;
0034   
0035          $user->add_lang('acp/posting');
0036   
0037          // Set up general vars
0038          $action = $request->variable('action', '');
0039          $action = (isset($_POST['add'])) ? 'add' : $action;
0040          $action = (isset($_POST['edit'])) ? 'edit' : $action;
0041          $action = (isset($_POST['import'])) ? 'import' : $action;
0042          $icon_id = $request->variable('id', 0);
0043          $submit = $request->is_set_post('submit', false);
0044   
0045          $form_key = 'acp_icons';
0046          add_form_key($form_key);
0047   
0048          $mode = ($mode == 'smilies') ? 'smilies' : 'icons';
0049   
0050          $this->tpl_name = 'acp_icons';
0051   
0052          // What are we working on?
0053          switch ($mode)
0054          {
0055              case 'smilies':
0056                  $table = SMILIES_TABLE;
0057                  $lang = 'SMILIES';
0058                  $fields = 'smiley';
0059                  $img_path = $config['smilies_path'];
0060              break;
0061   
0062              case 'icons':
0063                  $table = ICONS_TABLE;
0064                  $lang = 'ICONS';
0065                  $fields = 'icons';
0066                  $img_path = $config['icons_path'];
0067              break;
0068          }
0069   
0070          $this->page_title = 'ACP_' . $lang;
0071   
0072          // Clear some arrays
0073          $_images = $_paks = array();
0074          $notice = '';
0075   
0076          // Grab file list of paks and images
0077          if ($action == 'edit' || $action == 'add' || $action == 'import')
0078          {
0079              $imglist = filelist($phpbb_root_path . $img_path, '');
0080   
0081              foreach ($imglist as $path => $img_ary)
0082              {
0083                  if (empty($img_ary))
0084                  {
0085                      continue;
0086                  }
0087   
0088                  asort($img_ary, SORT_STRING);
0089   
0090                  foreach ($img_ary as $img)
0091                  {
0092                      $img_size = getimagesize($phpbb_root_path . $img_path . '/' . $path . $img);
0093   
0094                      if (!$img_size[0] || !$img_size[1] || strlen($img) > 255)
0095                      {
0096                          continue;
0097                      }
0098   
0099                      // adjust the width and height to be lower than 128px while perserving the aspect ratio (for icons)
0100                      if ($mode == 'icons')
0101                      {
0102                          if ($img_size[0] > 127 && $img_size[0] > $img_size[1])
0103                          {
0104                              $img_size[1] = (int) ($img_size[1] * (127 / $img_size[0]));
0105                              $img_size[0] = 127;
0106                          }
0107                          else if ($img_size[1] > 127)
0108                          {
0109                              $img_size[0] = (int) ($img_size[0] * (127 / $img_size[1]));
0110                              $img_size[1] = 127;
0111                          }
0112                      }
0113   
0114                      $_images[$path . $img]['file'] = $path . $img;
0115                      $_images[$path . $img]['width'] = $img_size[0];
0116                      $_images[$path . $img]['height'] = $img_size[1];
0117                  }
0118              }
0119              unset($imglist);
0120   
0121              if ($dir = @opendir($phpbb_root_path . $img_path))
0122              {
0123                  while (($file = readdir($dir)) !== false)
0124                  {
0125                      if (is_file($phpbb_root_path . $img_path . '/' . $file) && preg_match('#\.pak$#i', $file))
0126                      {
0127                          $_paks[] = $file;
0128                      }
0129                  }
0130                  closedir($dir);
0131   
0132                  if (!empty($_paks))
0133                  {
0134                      asort($_paks, SORT_STRING);
0135                  }
0136              }
0137          }
0138   
0139          // What shall we do today? Oops, I believe that's trademarked ...
0140          switch ($action)
0141          {
0142              case 'edit':
0143                  unset($_images);
0144                  $_images = array();
0145   
0146              // no break;
0147   
0148              case 'add':
0149   
0150                  $smilies = $default_row = array();
0151                  $smiley_options = $order_list = $add_order_list = '';
0152   
0153                  if ($action == 'add' && $mode == 'smilies')
0154                  {
0155                      $sql = 'SELECT *
0156                          FROM ' . SMILIES_TABLE . '
0157                          ORDER BY smiley_order';
0158                      $result = $db->sql_query($sql);
0159   
0160                      while ($row = $db->sql_fetchrow($result))
0161                      {
0162                          if (empty($smilies[$row['smiley_url']]))
0163                          {
0164                              $smilies[$row['smiley_url']] = $row;
0165                          }
0166                      }
0167                      $db->sql_freeresult($result);
0168   
0169                      if (sizeof($smilies))
0170                      {
0171                          foreach ($smilies as $row)
0172                          {
0173                              $selected = false;
0174   
0175                              if (!$smiley_options)
0176                              {
0177                                  $selected = true;
0178                                  $default_row = $row;
0179                              }
0180                              $smiley_options .= '<option value="' . $row['smiley_url'] . '"' . (($selected) ? ' selected="selected"' : '') . '>' . $row['smiley_url'] . '</option>';
0181   
0182                              $template->assign_block_vars('smile', array(
0183                                  'SMILEY_URL'    => addslashes($row['smiley_url']),
0184                                  'CODE'            => addslashes($row['code']),
0185                                  'EMOTION'        => addslashes($row['emotion']),
0186                                  'WIDTH'            => $row['smiley_width'],
0187                                  'HEIGHT'        => $row['smiley_height'],
0188                                  'ORDER'            => $row['smiley_order'] + 1,
0189                              ));
0190                          }
0191                      }
0192                  }
0193   
0194                  $sql = "SELECT *
0195                      FROM $table
0196                      ORDER BY {$fields}_order " . (($icon_id || $action == 'add') ? 'DESC' : 'ASC');
0197                  $result = $db->sql_query($sql);
0198   
0199                  $data = array();
0200                  $after = false;
0201                  $order_lists = array('', '');
0202                  $add_order_lists = array('', '');
0203                  $display_count = 0;
0204   
0205                  while ($row = $db->sql_fetchrow($result))
0206                  {
0207                      if ($action == 'add')
0208                      {
0209                          unset($_images[$row[$fields . '_url']]);
0210                      }
0211   
0212                      if ($row[$fields . '_id'] == $icon_id)
0213                      {
0214                          $after = true;
0215                          $data[$row[$fields . '_url']] = $row;
0216                      }
0217                      else
0218                      {
0219                          if ($action == 'edit' && !$icon_id)
0220                          {
0221                              $data[$row[$fields . '_url']] = $row;
0222                          }
0223   
0224                          $selected = '';
0225                          if (!empty($after))
0226                          {
0227                              $selected = ' selected="selected"';
0228                              $after = false;
0229                          }
0230                          if ($row['display_on_posting'])
0231                          {
0232                              $display_count++;
0233                          }
0234                          $after_txt = ($mode == 'smilies') ? $row['code'] : $row['icons_url'];
0235                          $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']];
0236   
0237                          if (!empty($default_row))
0238                          {
0239                              $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']];
0240                          }
0241                      }
0242                  }
0243                  $db->sql_freeresult($result);
0244   
0245                  $order_list = '<option value="1"' . ((!isset($after)) ? ' selected="selected"' : '') . '>' . $user->lang['FIRST'] . '</option>';
0246                  $add_order_list = '<option value="1">' . $user->lang['FIRST'] . '</option>';
0247   
0248                  if ($action == 'add')
0249                  {
0250                      $data = $_images;
0251                  }
0252   
0253                  $colspan = (($mode == 'smilies') ? 7 : 6);
0254                  $colspan += ($icon_id) ? 1 : 0;
0255                  $colspan += ($action == 'add') ? 2 : 0;
0256   
0257                  $template->assign_vars(array(
0258                      'S_EDIT'        => true,
0259                      'S_SMILIES'        => ($mode == 'smilies') ? true : false,
0260                      'S_ADD'            => ($action == 'add') ? true : false,
0261   
0262                      'S_ORDER_LIST_DISPLAY'        => $order_list . $order_lists[1],
0263                      'S_ORDER_LIST_UNDISPLAY'    => $order_list . $order_lists[0],
0264                      'S_ORDER_LIST_DISPLAY_COUNT'    => $display_count + 1,
0265   
0266                      'L_TITLE'        => $user->lang['ACP_' . $lang],
0267                      'L_EXPLAIN'        => $user->lang['ACP_' . $lang . '_EXPLAIN'],
0268                      'L_CONFIG'        => $user->lang[$lang . '_CONFIG'],
0269                      'L_URL'            => $user->lang[$lang . '_URL'],
0270                      'L_LOCATION'    => $user->lang[$lang . '_LOCATION'],
0271                      'L_WIDTH'        => $user->lang[$lang . '_WIDTH'],
0272                      'L_HEIGHT'        => $user->lang[$lang . '_HEIGHT'],
0273                      'L_ORDER'        => $user->lang[$lang . '_ORDER'],
0274                      'L_NO_ICONS'    => $user->lang['NO_' . $lang . '_' . strtoupper($action)],
0275   
0276                      'COLSPAN'        => $colspan,
0277                      'ID'            => $icon_id,
0278   
0279                      'U_BACK'        => $this->u_action,
0280                      'U_ACTION'        => $this->u_action . '&amp;action=' . (($action == 'add') ? 'create' : 'modify'),
0281                  ));
0282   
0283                  foreach ($data as $img => $img_row)
0284                  {
0285                      $template->assign_block_vars('items', array(
0286                          'IMG'        => $img,
0287                          'A_IMG'        => addslashes($img),
0288                          'IMG_SRC'    => $phpbb_root_path . $img_path . '/' . $img,
0289   
0290                          'CODE'        => ($mode == 'smilies' && isset($img_row['code'])) ? $img_row['code'] : '',
0291                          'EMOTION'    => ($mode == 'smilies' && isset($img_row['emotion'])) ? $img_row['emotion'] : '',
0292   
0293                          'S_ID'                => (isset($img_row[$fields . '_id'])) ? true : false,
0294                          'ID'                => (isset($img_row[$fields . '_id'])) ? $img_row[$fields . '_id'] : 0,
0295                          'WIDTH'                => (!empty($img_row[$fields .'_width'])) ? $img_row[$fields .'_width'] : $img_row['width'],
0296                          'HEIGHT'            => (!empty($img_row[$fields .'_height'])) ? $img_row[$fields .'_height'] : $img_row['height'],
0297                          'TEXT_ALT'            => ($mode == 'icons' && !empty($img_row['icons_alt'])) ? $img_row['icons_alt'] : $img,
0298                          'ALT'                => ($mode == 'icons' && !empty($img_row['icons_alt'])) ? $img_row['icons_alt'] : '',
0299                          'POSTING_CHECKED'    => (!empty($img_row['display_on_posting']) || $action == 'add') ? ' checked="checked"' : '',
0300                      ));
0301                  }
0302   
0303                  // Ok, another row for adding an addition code for a pre-existing image...
0304                  if ($action == 'add' && $mode == 'smilies' && sizeof($smilies))
0305                  {
0306                      $template->assign_vars(array(
0307                          'S_ADD_CODE'        => true,
0308   
0309                          'S_IMG_OPTIONS'        => $smiley_options,
0310   
0311                          'S_ADD_ORDER_LIST_DISPLAY'        => $add_order_list . $add_order_lists[1],
0312                          'S_ADD_ORDER_LIST_UNDISPLAY'    => $add_order_list . $add_order_lists[0],
0313   
0314                          'IMG_SRC'            => $phpbb_root_path . $img_path . '/' . $default_row['smiley_url'],
0315                          'IMG_PATH'            => $img_path,
0316   
0317                          'CODE'                => $default_row['code'],
0318                          'EMOTION'            => $default_row['emotion'],
0319   
0320                          'WIDTH'                => $default_row['smiley_width'],
0321                          'HEIGHT'            => $default_row['smiley_height'],
0322                      ));
0323                  }
0324   
0325                  return;
0326   
0327              break;
0328   
0329              case 'create':
0330              case 'modify':
0331   
0332                  if (!check_form_key($form_key))
0333                  {
0334                      trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
0335                  }
0336   
0337                  // Get items to create/modify
0338                  $images = (isset($_POST['image'])) ? array_keys($request->variable('image', array('' => 0))) : array();
0339   
0340                  // Now really get the items
0341                  $image_id        = (isset($_POST['id'])) ? $request->variable('id', array('' => 0)) : array();
0342                  $image_order    = (isset($_POST['order'])) ? $request->variable('order', array('' => 0)) : array();
0343                  $image_width    = (isset($_POST['width'])) ? $request->variable('width', array('' => 0)) : array();
0344                  $image_height    = (isset($_POST['height'])) ? $request->variable('height', array('' => 0)) : array();
0345                  $image_add        = (isset($_POST['add_img'])) ? $request->variable('add_img', array('' => 0)) : array();
0346                  $image_emotion    = $request->variable('emotion', array('' => ''), true);
0347                  $image_code        = $request->variable('code', array('' => ''), true);
0348                  $image_alt        = ($request->is_set_post('alt')) ? $request->variable('alt', array('' => ''), true) : array();
0349                  $image_display_on_posting = (isset($_POST['display_on_posting'])) ? $request->variable('display_on_posting', array('' => 0)) : array();
0350   
0351                  // Ok, add the relevant bits if we are adding new codes to existing emoticons...
0352                  if ($request->variable('add_additional_code', false, false, \phpbb\request\request_interface::POST))
0353                  {
0354                      $add_image            = $request->variable('add_image', '');
0355                      $add_code            = $request->variable('add_code', '', true);
0356                      $add_emotion        = $request->variable('add_emotion', '', true);
0357   
0358                      if ($add_image && $add_emotion && $add_code)
0359                      {
0360                          $images[] = $add_image;
0361                          $image_add[$add_image] = true;
0362   
0363                          $image_code[$add_image] = $add_code;
0364                          $image_emotion[$add_image] = $add_emotion;
0365                          $image_width[$add_image] = $request->variable('add_width', 0);
0366                          $image_height[$add_image] = $request->variable('add_height', 0);
0367   
0368                          if ($request->variable('add_display_on_posting', false, false, \phpbb\request\request_interface::POST))
0369                          {
0370                              $image_display_on_posting[$add_image] = 1;
0371                          }
0372   
0373                          $image_order[$add_image] = $request->variable('add_order', 0);
0374                      }
0375                  }
0376   
0377                  if ($mode == 'smilies' && $action == 'create')
0378                  {
0379                      $smiley_count = $this->item_count($table);
0380   
0381                      $addable_smileys_count = sizeof($images);
0382                      foreach ($images as $image)
0383                      {
0384                          if (!isset($image_add[$image]))
0385                          {
0386                              --$addable_smileys_count;
0387                          }
0388                      }
0389   
0390                      if ($smiley_count + $addable_smileys_count > SMILEY_LIMIT)
0391                      {
0392                          trigger_error($user->lang('TOO_MANY_SMILIES', SMILEY_LIMIT) . adm_back_link($this->u_action), E_USER_WARNING);
0393                      }
0394                  }
0395   
0396                  $icons_updated = 0;
0397                  $errors = array();
0398                  foreach ($images as $image)
0399                  {
0400                      if ($mode == 'smilies' && ($image_emotion[$image] == '' || $image_code[$image] == ''))
0401                      {
0402                          $errors[$image] = 'SMILIE_NO_' . (($image_emotion[$image] == '') ? 'EMOTION' : 'CODE');
0403                      }
0404                      else if ($action == 'create' && !isset($image_add[$image]))
0405                      {
0406                          // skip images where add wasn't checked
0407                      }
0408                      else if (!file_exists($phpbb_root_path . $img_path . '/' . $image))
0409                      {
0410                          $errors[$image] = 'SMILIE_NO_FILE';
0411                      }
0412                      else
0413                      {
0414                          if ($image_width[$image] == 0 || $image_height[$image] == 0)
0415                          {
0416                              $img_size = getimagesize($phpbb_root_path . $img_path . '/' . $image);
0417                              $image_width[$image] = $img_size[0];
0418                              $image_height[$image] = $img_size[1];
0419                          }
0420   
0421                          // Adjust image width/height for icons
0422                          if ($mode == 'icons')
0423                          {
0424                              if ($image_width[$image] > 127 && $image_width[$image] > $image_height[$image])
0425                              {
0426                                  $image_height[$image] = (int) ($image_height[$image] * (127 / $image_width[$image]));
0427                                  $image_width[$image] = 127;
0428                              }
0429                              else if ($image_height[$image] > 127)
0430                              {
0431                                  $image_width[$image] = (int) ($image_width[$image] * (127 / $image_height[$image]));
0432                                  $image_height[$image] = 127;
0433                              }
0434                          }
0435   
0436                          $img_sql = array(
0437                              $fields . '_url'        => $image,
0438                              $fields . '_width'        => $image_width[$image],
0439                              $fields . '_height'        => $image_height[$image],
0440                              'display_on_posting'    => (isset($image_display_on_posting[$image])) ? 1 : 0,
0441                          );
0442   
0443                          if ($mode == 'smilies')
0444                          {
0445                              $img_sql = array_merge($img_sql, array(
0446                                  'emotion'    => $image_emotion[$image],
0447                                  'code'        => $image_code[$image])
0448                              );
0449                          }
0450   
0451                          if ($mode == 'icons')
0452                          {
0453                              $img_sql = array_merge($img_sql, array(
0454                                  'icons_alt'    => $image_alt[$image])
0455                              );
0456                          }
0457   
0458                          // Image_order holds the 'new' order value
0459                          if (!empty($image_order[$image]))
0460                          {
0461                              $img_sql = array_merge($img_sql, array(
0462                                  $fields . '_order'    =>    $image_order[$image])
0463                              );
0464   
0465                              // Since we always add 'after' an item, we just need to increase all following + the current by one
0466                              $sql = "UPDATE $table
0467                                  SET {$fields}_order = {$fields}_order + 1
0468                                  WHERE {$fields}_order >= {$image_order[$image]}";
0469                              $db->sql_query($sql);
0470   
0471                              // If we adjust the order, we need to adjust all other orders too - they became inaccurate...
0472                              foreach ($image_order as $_image => $_order)
0473                              {
0474                                  if ($_image == $image)
0475                                  {
0476                                      continue;
0477                                  }
0478   
0479                                  if ($_order >= $image_order[$image])
0480                                  {
0481                                      $image_order[$_image]++;
0482                                  }
0483                              }
0484                          }
0485   
0486                          if ($action == 'modify'  && !empty($image_id[$image]))
0487                          {
0488                              $sql = "UPDATE $table
0489                                  SET " . $db->sql_build_array('UPDATE', $img_sql) . "
0490                                  WHERE {$fields}_id = " . $image_id[$image];
0491                              $db->sql_query($sql);
0492                              $icons_updated++;
0493                          }
0494                          else if ($action !== 'modify')
0495                          {
0496                              $sql = "INSERT INTO $table " . $db->sql_build_array('INSERT', $img_sql);
0497                              $db->sql_query($sql);
0498                              $icons_updated++;
0499                          }
0500   
0501                      }
0502                  }
0503   
0504                  $cache->destroy('_icons');
0505                  $cache->destroy('sql', $table);
0506                  $phpbb_container->get('text_formatter.cache')->invalidate();
0507   
0508                  $level = ($icons_updated) ? E_USER_NOTICE : E_USER_WARNING;
0509                  $errormsgs = '';
0510                  foreach ($errors as $img => $error)
0511                  {
0512                      $errormsgs .= '<br />' . sprintf($user->lang[$error], $img);
0513                  }
0514                  if ($action == 'modify')
0515                  {
0516                      trigger_error($user->lang($lang . '_EDITED', $icons_updated) . $errormsgs . adm_back_link($this->u_action), $level);
0517                  }
0518                  else
0519                  {
0520                      trigger_error($user->lang($lang . '_ADDED', $icons_updated) . $errormsgs . adm_back_link($this->u_action), $level);
0521                  }
0522   
0523              break;
0524   
0525              case 'import':
0526   
0527                  $pak = $request->variable('pak', '');
0528                  $current = $request->variable('current', '');
0529   
0530                  if ($pak != '')
0531                  {
0532                      $order = 0;
0533   
0534                      if (!check_form_key($form_key))
0535                      {
0536                          trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
0537                      }
0538   
0539                      if (!($pak_ary = @file($phpbb_root_path . $img_path . '/' . $pak)))
0540                      {
0541                          trigger_error($user->lang['PAK_FILE_NOT_READABLE'] . adm_back_link($this->u_action), E_USER_WARNING);
0542                      }
0543   
0544                      // Make sure the pak_ary is valid
0545                      foreach ($pak_ary as $pak_entry)
0546                      {
0547                          if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
0548                          {
0549                              if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
0550                                  ((sizeof($data[1]) != 6 || (empty($data[1][4]) || empty($data[1][5]))) && $mode == 'smilies' ))
0551                              {
0552                                  trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
0553                              }
0554                          }
0555                          else
0556                          {
0557                              trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
0558                          }
0559                      }
0560   
0561                      // The user has already selected a smilies_pak file
0562                      if ($current == 'delete')
0563                      {
0564                          switch ($db->get_sql_layer())
0565                          {
0566                              case 'sqlite3':
0567                                  $db->sql_query('DELETE FROM ' . $table);
0568                              break;
0569   
0570                              default:
0571                                  $db->sql_query('TRUNCATE TABLE ' . $table);
0572                              break;
0573                          }
0574   
0575                          switch ($mode)
0576                          {
0577                              case 'smilies':
0578                              break;
0579   
0580                              case 'icons':
0581                                  // Reset all icon_ids
0582                                  $db->sql_query('UPDATE ' . TOPICS_TABLE . ' SET icon_id = 0');
0583                                  $db->sql_query('UPDATE ' . POSTS_TABLE . ' SET icon_id = 0');
0584                              break;
0585                          }
0586                      }
0587                      else
0588                      {
0589                          $cur_img = array();
0590   
0591                          $field_sql = ($mode == 'smilies') ? 'code' : 'icons_url';
0592   
0593                          $sql = "SELECT $field_sql
0594                              FROM $table";
0595                          $result = $db->sql_query($sql);
0596   
0597                          while ($row = $db->sql_fetchrow($result))
0598                          {
0599                              ++$order;
0600                              $cur_img[$row[$field_sql]] = 1;
0601                          }
0602                          $db->sql_freeresult($result);
0603                      }
0604   
0605                      if ($mode == 'smilies')
0606                      {
0607                          $smiley_count = $this->item_count($table);
0608                          if ($smiley_count + sizeof($pak_ary) > SMILEY_LIMIT)
0609                          {
0610                              trigger_error($user->lang('TOO_MANY_SMILIES', SMILEY_LIMIT) . adm_back_link($this->u_action), E_USER_WARNING);
0611                          }
0612                      }
0613   
0614                      foreach ($pak_ary as $pak_entry)
0615                      {
0616                          $data = array();
0617                          if (preg_match_all("#'(.*?)', ?#", $pak_entry, $data))
0618                          {
0619                              if ((sizeof($data[1]) != 4 && $mode == 'icons') ||
0620                                  (sizeof($data[1]) != 6 && $mode == 'smilies'))
0621                              {
0622                                  trigger_error($user->lang['WRONG_PAK_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
0623                              }
0624   
0625                              // Stripslash here because it got addslashed before... (on export)
0626                              $img = stripslashes($data[1][0]);
0627                              $width = stripslashes($data[1][1]);
0628                              $height = stripslashes($data[1][2]);
0629                              $display_on_posting = stripslashes($data[1][3]);
0630   
0631                              if (isset($data[1][4]) && isset($data[1][5]))
0632                              {
0633                                  $emotion = stripslashes($data[1][4]);
0634                                  $code = stripslashes($data[1][5]);
0635                              }
0636   
0637                              if ($current == 'replace' &&
0638                                  (($mode == 'smilies' && !empty($cur_img[$code])) ||
0639                                  ($mode == 'icons' && !empty($cur_img[$img]))))
0640                              {
0641                                  $replace_sql = ($mode == 'smilies') ? $code : $img;
0642                                  $sql = array(
0643                                      $fields . '_url'        => $img,
0644                                      $fields . '_height'        => (int) $height,
0645                                      $fields . '_width'        => (int) $width,
0646                                      'display_on_posting'    => (int) $display_on_posting,
0647                                  );
0648   
0649                                  if ($mode == 'smilies')
0650                                  {
0651                                      $sql = array_merge($sql, array(
0652                                          'emotion'                => $emotion,
0653                                      ));
0654                                  }
0655   
0656                                  $sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql) . "
0657                                      WHERE $field_sql = '" . $db->sql_escape($replace_sql) . "'";
0658                                  $db->sql_query($sql);
0659                              }
0660                              else
0661                              {
0662                                  ++$order;
0663   
0664                                  $sql = array(
0665                                      $fields . '_url'    => $img,
0666                                      $fields . '_height'    => (int) $height,
0667                                      $fields . '_width'    => (int) $width,
0668                                      $fields . '_order'    => (int) $order,
0669                                      'display_on_posting'=> (int) $display_on_posting,
0670                                  );
0671   
0672                                  if ($mode == 'smilies')
0673                                  {
0674                                      $sql = array_merge($sql, array(
0675                                          'code'                => $code,
0676                                          'emotion'            => $emotion,
0677                                      ));
0678                                  }
0679                                  $db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql));
0680                              }
0681                          }
0682                      }
0683   
0684                      $cache->destroy('_icons');
0685                      $cache->destroy('sql', $table);
0686                      $phpbb_container->get('text_formatter.cache')->invalidate();
0687   
0688                      trigger_error($user->lang[$lang . '_IMPORT_SUCCESS'] . adm_back_link($this->u_action));
0689                  }
0690                  else
0691                  {
0692                      $pak_options = '';
0693   
0694                      foreach ($_paks as $pak)
0695                      {
0696                          $pak_options .= '<option value="' . $pak . '">' . htmlspecialchars($pak) . '</option>';
0697                      }
0698   
0699                      $template->assign_vars(array(
0700                          'S_CHOOSE_PAK'        => true,
0701                          'S_PAK_OPTIONS'        => $pak_options,
0702   
0703                          'L_TITLE'            => $user->lang['ACP_' . $lang],
0704                          'L_EXPLAIN'            => $user->lang['ACP_' . $lang . '_EXPLAIN'],
0705                          'L_NO_PAK_OPTIONS'    => $user->lang['NO_' . $lang . '_PAK'],
0706                          'L_CURRENT'            => $user->lang['CURRENT_' . $lang],
0707                          'L_CURRENT_EXPLAIN'    => $user->lang['CURRENT_' . $lang . '_EXPLAIN'],
0708                          'L_IMPORT_SUBMIT'    => $user->lang['IMPORT_' . $lang],
0709   
0710                          'U_BACK'        => $this->u_action,
0711                          'U_ACTION'        => $this->u_action . '&amp;action=import',
0712                          )
0713                      );
0714                  }
0715              break;
0716   
0717              case 'export':
0718   
0719                  $this->page_title = 'EXPORT_' . $lang;
0720                  $this->tpl_name = 'message_body';
0721   
0722                  $template->assign_vars(array(
0723                      'MESSAGE_TITLE'        => $user->lang['EXPORT_' . $lang],
0724                      'MESSAGE_TEXT'        => sprintf($user->lang['EXPORT_' . $lang . '_EXPLAIN'], '<a href="' . $this->u_action . '&amp;action=send&amp;hash=' . generate_link_hash('acp_icons') . '">', '</a>'),
0725   
0726                      'S_USER_NOTICE'        => true,
0727                      )
0728                  );
0729   
0730                  return;
0731   
0732              break;
0733   
0734              case 'send':
0735   
0736                  if (!check_link_hash($request->variable('hash', ''), 'acp_icons'))
0737                  {
0738                      trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
0739                  }
0740   
0741                  $sql = "SELECT *
0742                      FROM $table
0743                      ORDER BY {$fields}_order";
0744                  $result = $db->sql_query($sql);
0745   
0746                  $pak = '';
0747                  while ($row = $db->sql_fetchrow($result))
0748                  {
0749                      $pak .= "'" . addslashes($row[$fields . '_url']) . "', ";
0750                      $pak .= "'" . addslashes($row[$fields . '_width']) . "', ";
0751                      $pak .= "'" . addslashes($row[$fields . '_height']) . "', ";
0752                      $pak .= "'" . addslashes($row['display_on_posting']) . "', ";
0753   
0754                      if ($mode == 'smilies')
0755                      {
0756                          $pak .= "'" . addslashes($row['emotion']) . "', ";
0757                          $pak .= "'" . addslashes($row['code']) . "', ";
0758                      }
0759   
0760                      $pak .= "\n";
0761                  }
0762                  $db->sql_freeresult($result);
0763   
0764                  if ($pak != '')
0765                  {
0766                      garbage_collection();
0767   
0768                      header('Cache-Control: public');
0769   
0770                      // Send out the Headers
0771                      header('Content-Type: text/x-delimtext; name="' . $mode . '.pak"');
0772                      header('Content-Disposition: inline; filename="' . $mode . '.pak"');
0773                      echo $pak;
0774   
0775                      flush();
0776                      exit;
0777                  }
0778                  else
0779                  {
0780                      trigger_error($user->lang['NO_' . strtoupper($fields) . '_EXPORT'] . adm_back_link($this->u_action), E_USER_WARNING);
0781                  }
0782   
0783              break;
0784   
0785              case 'delete':
0786   
0787                  if (confirm_box(true))
0788                  {
0789                      $sql = "DELETE FROM $table
0790                          WHERE {$fields}_id = $icon_id";
0791                      $db->sql_query($sql);
0792   
0793                      switch ($mode)
0794                      {
0795                          case 'smilies':
0796                          break;
0797   
0798                          case 'icons':
0799                              // Reset appropriate icon_ids
0800                              $db->sql_query('UPDATE ' . TOPICS_TABLE . "
0801                                  SET icon_id = 0
0802                                  WHERE icon_id = $icon_id");
0803   
0804                              $db->sql_query('UPDATE ' . POSTS_TABLE . "
0805                                  SET icon_id = 0
0806                                  WHERE icon_id = $icon_id");
0807                          break;
0808                      }
0809   
0810                      $notice = $user->lang[$lang . '_DELETED'];
0811   
0812                      $cache->destroy('_icons');
0813                      $cache->destroy('sql', $table);
0814                      $phpbb_container->get('text_formatter.cache')->invalidate();
0815   
0816                      if ($request->is_ajax())
0817                      {
0818                          $json_response = new \phpbb\json_response;
0819                          $json_response->send(array(
0820                              'MESSAGE_TITLE'    => $user->lang['INFORMATION'],
0821                              'MESSAGE_TEXT'    => $notice,
0822                              'REFRESH_DATA'    => array(
0823                                  'time'    => 3
0824                              )
0825                          ));
0826                      }
0827                  }
0828                  else
0829                  {
0830                      confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
0831                          'i'            => $id,
0832                          'mode'        => $mode,
0833                          'id'        => $icon_id,
0834                          'action'    => 'delete',
0835                      )));
0836                  }
0837   
0838              break;
0839   
0840              case 'move_up':
0841              case 'move_down':
0842   
0843                  if (!check_link_hash($request->variable('hash', ''), 'acp_icons'))
0844                  {
0845                      trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
0846                  }
0847   
0848                  // Get current order id...
0849                  $sql = "SELECT {$fields}_order as current_order
0850                      FROM $table
0851                      WHERE {$fields}_id = $icon_id";
0852                  $result = $db->sql_query($sql);
0853                  $current_order = (int) $db->sql_fetchfield('current_order');
0854                  $db->sql_freeresult($result);
0855   
0856                  if ($current_order == 0 && $action == 'move_up')
0857                  {
0858                      break;
0859                  }
0860   
0861                  // on move_down, switch position with next order_id...
0862                  // on move_up, switch position with previous order_id...
0863                  $switch_order_id = ($action == 'move_down') ? $current_order + 1 : $current_order - 1;
0864   
0865                  //
0866                  $sql = "UPDATE $table
0867                      SET {$fields}_order = $current_order
0868                      WHERE {$fields}_order = $switch_order_id
0869                          AND {$fields}_id <> $icon_id";
0870                  $db->sql_query($sql);
0871                  $move_executed = (bool) $db->sql_affectedrows();
0872   
0873                  // Only update the other entry too if the previous entry got updated
0874                  if ($move_executed)
0875                  {
0876                      $sql = "UPDATE $table
0877                          SET {$fields}_order = $switch_order_id
0878                          WHERE {$fields}_order = $current_order
0879                              AND {$fields}_id = $icon_id";
0880                      $db->sql_query($sql);
0881                  }
0882   
0883                  $cache->destroy('_icons');
0884                  $cache->destroy('sql', $table);
0885                  $phpbb_container->get('text_formatter.cache')->invalidate();
0886   
0887                  if ($request->is_ajax())
0888                  {
0889                      $json_response = new \phpbb\json_response;
0890                      $json_response->send(array(
0891                          'success'    => $move_executed,
0892                      ));
0893                  }
0894   
0895              break;
0896          }
0897   
0898          // By default, check that image_order is valid and fix it if necessary
0899          $sql = "SELECT {$fields}_id AS order_id, {$fields}_order AS fields_order
0900              FROM $table
0901              ORDER BY display_on_posting DESC, {$fields}_order";
0902          $result = $db->sql_query($sql);
0903   
0904          if ($row = $db->sql_fetchrow($result))
0905          {
0906              $order = 0;
0907              do
0908              {
0909                  ++$order;
0910                  if ($row['fields_order'] != $order)
0911                  {
0912                      $db->sql_query("UPDATE $table
0913                          SET {$fields}_order = $order
0914                          WHERE {$fields}_id = " . $row['order_id']);
0915                  }
0916              }
0917              while ($row = $db->sql_fetchrow($result));
0918          }
0919          $db->sql_freeresult($result);
0920   
0921          $template->assign_vars(array(
0922              'L_TITLE'            => $user->lang['ACP_' . $lang],
0923              'L_EXPLAIN'            => $user->lang['ACP_' . $lang . '_EXPLAIN'],
0924              'L_IMPORT'            => $user->lang['IMPORT_' . $lang],
0925              'L_EXPORT'            => $user->lang['EXPORT_' . $lang],
0926              'L_NOT_DISPLAYED'    => $user->lang[$lang . '_NOT_DISPLAYED'],
0927              'L_ICON_ADD'        => $user->lang['ADD_' . $lang],
0928              'L_ICON_EDIT'        => $user->lang['EDIT_' . $lang],
0929   
0930              'NOTICE'            => $notice,
0931              'COLSPAN'            => ($mode == 'smilies') ? 5 : 3,
0932   
0933              'S_SMILIES'            => ($mode == 'smilies') ? true : false,
0934   
0935              'U_ACTION'            => $this->u_action,
0936              'U_IMPORT'            => $this->u_action . '&amp;action=import',
0937              'U_EXPORT'            => $this->u_action . '&amp;action=export',
0938              )
0939          );
0940   
0941          /* @var $pagination \phpbb\pagination */
0942          $pagination = $phpbb_container->get('pagination');
0943          $pagination_start = $request->variable('start', 0);
0944          $spacer = false;
0945   
0946          $item_count = $this->item_count($table);
0947   
0948          $sql = "SELECT *
0949              FROM $table
0950              ORDER BY {$fields}_order ASC";
0951          $result = $db->sql_query_limit($sql, $config['smilies_per_page'], $pagination_start);
0952   
0953          while ($row = $db->sql_fetchrow($result))
0954          {
0955              $alt_text = ($mode == 'smilies') ? $row['code'] : (($mode == 'icons' && !empty($row['icons_alt'])) ? $row['icons_alt'] : $row['icons_url']);
0956   
0957              $template->assign_block_vars('items', array(
0958                  'S_SPACER'        => (!$spacer && !$row['display_on_posting']) ? true : false,
0959                  'ALT_TEXT'        => $alt_text,
0960                  'IMG_SRC'        => $phpbb_root_path . $img_path . '/' . $row[$fields . '_url'],
0961                  'WIDTH'            => $row[$fields . '_width'],
0962                  'HEIGHT'        => $row[$fields . '_height'],
0963                  'CODE'            => (isset($row['code'])) ? $row['code'] : '',
0964                  'EMOTION'        => (isset($row['emotion'])) ? $row['emotion'] : '',
0965                  'U_EDIT'        => $this->u_action . '&amp;action=edit&amp;id=' . $row[$fields . '_id'],
0966                  'U_DELETE'        => $this->u_action . '&amp;action=delete&amp;id=' . $row[$fields . '_id'],
0967                  'U_MOVE_UP'        => $this->u_action . '&amp;action=move_up&amp;id=' . $row[$fields . '_id'] . '&amp;start=' . $pagination_start . '&amp;hash=' . generate_link_hash('acp_icons'),
0968                  'U_MOVE_DOWN'    => $this->u_action . '&amp;action=move_down&amp;id=' . $row[$fields . '_id'] . '&amp;start=' . $pagination_start . '&amp;hash=' . generate_link_hash('acp_icons'),
0969              ));
0970   
0971              if (!$spacer && !$row['display_on_posting'])
0972              {
0973                  $spacer = true;
0974              }
0975          }
0976          $db->sql_freeresult($result);
0977   
0978          $pagination->generate_template_pagination($this->u_action, 'pagination', 'start', $item_count, $config['smilies_per_page'], $pagination_start);
0979      }
0980   
0981      /**
0982       * Returns the count of smilies or icons in the database
0983       *
0984       * @param string $table The table of items to count.
0985       * @return int number of items
0986       */
0987      /* private */ function item_count($table)
0988      {
0989          global $db;
0990   
0991          $sql = "SELECT COUNT(*) AS item_count
0992              FROM $table";
0993          $result = $db->sql_query($sql);
0994          $item_count = (int) $db->sql_fetchfield('item_count');
0995          $db->sql_freeresult($result);
0996   
0997          return $item_count;
0998      }
0999  }
1000