[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

/includes/acp/ -> acp_icons.php (source)

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


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1