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_attachments.php

Zuletzt modifiziert: 09.10.2024, 12:52 - Dateigröße: 56.91 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  class acp_attachments
0023  {
0024      /** @var \phpbb\db\driver\driver_interface */
0025      protected $db;
0026   
0027      /** @var \phpbb\config\config */
0028      protected $config;
0029   
0030      /** @var ContainerBuilder */
0031      protected $phpbb_container;
0032   
0033      /** @var \phpbb\template\template */
0034      protected $template;
0035   
0036      /** @var \phpbb\user */
0037      protected $user;
0038   
0039      /** @var  \phpbb\filesystem\filesystem_interface */
0040      protected $filesystem;
0041   
0042      /** @var \phpbb\attachment\manager */
0043      protected $attachment_manager;
0044   
0045      public $id;
0046      public $u_action;
0047      protected $new_config;
0048   
0049      function main($id, $mode)
0050      {
0051          global $db, $user, $auth, $template, $cache, $phpbb_container, $phpbb_filesystem, $phpbb_dispatcher;
0052          global $config, $phpbb_admin_path, $phpbb_root_path, $phpEx, $phpbb_log, $request;
0053   
0054          $this->id = $id;
0055          $this->db = $db;
0056          $this->config = $config;
0057          $this->template = $template;
0058          $this->user = $user;
0059          $this->phpbb_container = $phpbb_container;
0060          $this->filesystem = $phpbb_filesystem;
0061          $this->attachment_manager = $phpbb_container->get('attachment.manager');
0062   
0063          $user->add_lang(array('posting', 'viewtopic', 'acp/attachments'));
0064   
0065          $error = $notify = array();
0066          $submit = (isset($_POST['submit'])) ? true : false;
0067          $action = $request->variable('action', '');
0068   
0069          $form_key = 'acp_attach';
0070          add_form_key($form_key);
0071   
0072          if ($submit && !check_form_key($form_key))
0073          {
0074              trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
0075          }
0076   
0077          switch ($mode)
0078          {
0079              case 'attach':
0080                  $l_title = 'ACP_ATTACHMENT_SETTINGS';
0081              break;
0082   
0083              case 'extensions':
0084                  $l_title = 'ACP_MANAGE_EXTENSIONS';
0085              break;
0086   
0087              case 'ext_groups':
0088                  $l_title = 'ACP_EXTENSION_GROUPS';
0089              break;
0090   
0091              case 'orphan':
0092                  $l_title = 'ACP_ORPHAN_ATTACHMENTS';
0093              break;
0094   
0095              case 'manage':
0096                  $l_title = 'ACP_MANAGE_ATTACHMENTS';
0097              break;
0098   
0099              default:
0100                  trigger_error('NO_MODE', E_USER_ERROR);
0101              break;
0102          }
0103   
0104          $this->tpl_name = 'acp_attachments';
0105          $this->page_title = $l_title;
0106   
0107          $template->assign_vars(array(
0108              'L_TITLE'            => $user->lang[$l_title],
0109              'L_TITLE_EXPLAIN'    => $user->lang[$l_title . '_EXPLAIN'],
0110              'U_ACTION'            => $this->u_action)
0111          );
0112   
0113          switch ($mode)
0114          {
0115              case 'attach':
0116   
0117                  if (!function_exists('get_supported_image_types'))
0118                  {
0119                      include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
0120                  }
0121   
0122                  $sql = 'SELECT group_name, cat_id
0123                      FROM ' . EXTENSION_GROUPS_TABLE . '
0124                      WHERE cat_id > 0
0125                      ORDER BY cat_id';
0126                  $result = $db->sql_query($sql);
0127   
0128                  $s_assigned_groups = array();
0129                  while ($row = $db->sql_fetchrow($result))
0130                  {
0131                      $row['group_name'] = (isset($user->lang['EXT_GROUP_' . $row['group_name']])) ? $user->lang['EXT_GROUP_' . $row['group_name']] : $row['group_name'];
0132                      $s_assigned_groups[$row['cat_id']][] = $row['group_name'];
0133                  }
0134                  $db->sql_freeresult($result);
0135   
0136                  $l_legend_cat_images = $user->lang['SETTINGS_CAT_IMAGES'] . ' [' . $user->lang['ASSIGNED_GROUP'] . ': ' . ((!empty($s_assigned_groups[ATTACHMENT_CATEGORY_IMAGE])) ? implode($user->lang['COMMA_SEPARATOR'], $s_assigned_groups[ATTACHMENT_CATEGORY_IMAGE]) : $user->lang['NO_EXT_GROUP']) . ']';
0137   
0138                  $display_vars = array(
0139                      'title'    => 'ACP_ATTACHMENT_SETTINGS',
0140                      'vars'    => array(
0141                          'legend1'                => 'ACP_ATTACHMENT_SETTINGS',
0142   
0143                          'img_max_width'            => array('lang' => 'MAX_IMAGE_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
0144                          'img_max_height'        => array('lang' => 'MAX_IMAGE_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
0145                          'img_link_width'        => array('lang' => 'IMAGE_LINK_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
0146                          'img_link_height'        => array('lang' => 'IMAGE_LINK_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
0147   
0148                          'allow_attachments'        => array('lang' => 'ALLOW_ATTACHMENTS',        'validate' => 'bool',    'type' => 'radio:yes_no', 'explain' => false),
0149                          'allow_pm_attach'        => array('lang' => 'ALLOW_PM_ATTACHMENTS',    'validate' => 'bool',    'type' => 'radio:yes_no', 'explain' => false),
0150                          'upload_path'            => array('lang' => 'UPLOAD_DIR',            'validate' => 'wpath',    'type' => 'text:25:100', 'explain' => true),
0151                          'display_order'            => array('lang' => 'DISPLAY_ORDER',            'validate' => 'bool',    'type' => 'custom', 'method' => 'display_order', 'explain' => true),
0152                          'attachment_quota'        => array('lang' => 'ATTACH_QUOTA',            'validate' => 'string',    'type' => 'custom', 'method' => 'max_filesize', 'explain' => true),
0153                          'max_filesize'            => array('lang' => 'ATTACH_MAX_FILESIZE',    'validate' => 'string',    'type' => 'custom', 'method' => 'max_filesize', 'explain' => true),
0154                          'max_filesize_pm'        => array('lang' => 'ATTACH_MAX_PM_FILESIZE','validate' => 'string',    'type' => 'custom', 'method' => 'max_filesize', 'explain' => true),
0155                          'max_attachments'        => array('lang' => 'MAX_ATTACHMENTS',        'validate' => 'int:0:999',    'type' => 'number:0:999', 'explain' => false),
0156                          'max_attachments_pm'    => array('lang' => 'MAX_ATTACHMENTS_PM',    'validate' => 'int:0:999',    'type' => 'number:0:999', 'explain' => false),
0157                          'secure_downloads'        => array('lang' => 'SECURE_DOWNLOADS',        'validate' => 'bool',    'type' => 'radio:yes_no', 'explain' => true),
0158                          'secure_allow_deny'        => array('lang' => 'SECURE_ALLOW_DENY',        'validate' => 'int',    'type' => 'custom', 'method' => 'select_allow_deny', 'explain' => true),
0159                          'secure_allow_empty_referer'    => array('lang' => 'SECURE_EMPTY_REFERRER', 'validate' => 'bool',    'type' => 'radio:yes_no', 'explain' => true),
0160                          'check_attachment_content'         => array('lang' => 'CHECK_CONTENT', 'validate' => 'bool',    'type' => 'radio:yes_no', 'explain' => true),
0161   
0162                          'legend2'                    => $l_legend_cat_images,
0163                          'img_display_inlined'        => array('lang' => 'DISPLAY_INLINED',        'validate' => 'bool',    'type' => 'radio:yes_no', 'explain' => true),
0164                          'img_create_thumbnail'        => array('lang' => 'CREATE_THUMBNAIL',        'validate' => 'bool',    'type' => 'radio:yes_no', 'explain' => true),
0165                          'img_max_thumb_width'        => array('lang' => 'MAX_THUMB_WIDTH',        'validate' => 'int:0:999999999999999',    'type' => 'number:0:999999999999999', 'explain' => true, 'append' => ' ' . $user->lang['PIXEL']),
0166                          'img_min_thumb_filesize'    => array('lang' => 'MIN_THUMB_FILESIZE',    'validate' => 'int:0:999999999999999',    'type' => 'number:0:999999999999999', 'explain' => true, 'append' => ' ' . $user->lang['BYTES']),
0167                          'img_imagick'                => array('lang' => 'IMAGICK_PATH',            'validate' => 'absolute_path',    'type' => 'text:20:200', 'explain' => true, 'append' => '&nbsp;&nbsp;<span>[ <a href="' . $this->u_action . '&amp;action=imgmagick">' . $user->lang['SEARCH_IMAGICK'] . '</a> ]</span>'),
0168                          'img_max'                    => array('lang' => 'MAX_IMAGE_SIZE',        'validate' => 'int:0:9999',    'type' => 'dimension:0:9999', 'explain' => true, 'append' => ' ' . $user->lang['PIXEL']),
0169                          'img_link'                    => array('lang' => 'IMAGE_LINK_SIZE',        'validate' => 'int:0:9999',    'type' => 'dimension:0:9999', 'explain' => true, 'append' => ' ' . $user->lang['PIXEL']),
0170                      )
0171                  );
0172   
0173                  /**
0174                  * Event to add and/or modify acp_attachement configurations
0175                  *
0176                  * @event core.acp_attachments_config_edit_add
0177                  * @var    array    display_vars    Array of config values to display and process
0178                  * @var    string    mode            Mode of the config page we are displaying
0179                  * @var    boolean    submit            Do we display the form or process the submission
0180                  * @since 3.1.11-RC1
0181                  */
0182                  $vars = array('display_vars', 'mode', 'submit');
0183                  extract($phpbb_dispatcher->trigger_event('core.acp_attachments_config_edit_add', compact($vars)));
0184   
0185                  $this->new_config = $config;
0186                  $cfg_array = (isset($_REQUEST['config'])) ? $request->variable('config', array('' => '')) : $this->new_config;
0187                  $error = array();
0188   
0189                  // We validate the complete config if whished
0190                  validate_config_vars($display_vars['vars'], $cfg_array, $error);
0191   
0192                  // Do not write values if there is an error
0193                  if (sizeof($error))
0194                  {
0195                      $submit = false;
0196                  }
0197   
0198                  // We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to...
0199                  foreach ($display_vars['vars'] as $config_name => $null)
0200                  {
0201                      if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
0202                      {
0203                          continue;
0204                      }
0205   
0206                      $this->new_config[$config_name] = $config_value = $cfg_array[$config_name];
0207   
0208                      if (in_array($config_name, array('attachment_quota', 'max_filesize', 'max_filesize_pm')))
0209                      {
0210                          $size_var = $request->variable($config_name, '');
0211                          $this->new_config[$config_name] = $config_value = ($size_var == 'kb') ? round($config_value * 1024) : (($size_var == 'mb') ? round($config_value * 1048576) : $config_value);
0212                      }
0213   
0214                      if ($submit)
0215                      {
0216                          $config->set($config_name, $config_value);
0217                      }
0218                  }
0219   
0220                  $this->perform_site_list();
0221   
0222                  if ($submit)
0223                  {
0224                      $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_CONFIG_ATTACH');
0225   
0226                      // Check Settings
0227                      $this->test_upload($error, $this->new_config['upload_path'], false);
0228   
0229                      if (!sizeof($error))
0230                      {
0231                          trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action));
0232                      }
0233                  }
0234   
0235                  $template->assign_var('S_ATTACHMENT_SETTINGS', true);
0236   
0237                  if ($action == 'imgmagick')
0238                  {
0239                      $this->new_config['img_imagick'] = $this->search_imagemagick();
0240                  }
0241   
0242                  // We strip eventually manual added convert program, we only want the patch
0243                  if ($this->new_config['img_imagick'])
0244                  {
0245                      // Change path separator
0246                      $this->new_config['img_imagick'] = str_replace('\\', '/', $this->new_config['img_imagick']);
0247                      $this->new_config['img_imagick'] = str_replace(array('convert', '.exe'), array('', ''), $this->new_config['img_imagick']);
0248   
0249                      // Check for trailing slash
0250                      if (substr($this->new_config['img_imagick'], -1) !== '/')
0251                      {
0252                          $this->new_config['img_imagick'] .= '/';
0253                      }
0254                  }
0255   
0256                  $supported_types = get_supported_image_types();
0257   
0258                  // Check Thumbnail Support
0259                  if (!$this->new_config['img_imagick'] && (!isset($supported_types['format']) || !sizeof($supported_types['format'])))
0260                  {
0261                      $this->new_config['img_create_thumbnail'] = 0;
0262                  }
0263   
0264                  $template->assign_vars(array(
0265                      'U_SEARCH_IMAGICK'        => $this->u_action . '&amp;action=imgmagick',
0266                      'S_THUMBNAIL_SUPPORT'    => (!$this->new_config['img_imagick'] && (!isset($supported_types['format']) || !sizeof($supported_types['format']))) ? false : true)
0267                  );
0268   
0269                  // Secure Download Options - Same procedure as with banning
0270                  $allow_deny = ($this->new_config['secure_allow_deny']) ? 'ALLOWED' : 'DISALLOWED';
0271   
0272                  $sql = 'SELECT *
0273                      FROM ' . SITELIST_TABLE;
0274                  $result = $db->sql_query($sql);
0275   
0276                  $defined_ips = '';
0277                  $ips = array();
0278   
0279                  while ($row = $db->sql_fetchrow($result))
0280                  {
0281                      $value = ($row['site_ip']) ? $row['site_ip'] : $row['site_hostname'];
0282                      if ($value)
0283                      {
0284                          $defined_ips .= '<option' . (($row['ip_exclude']) ? ' class="sep"' : '') . ' value="' . $row['site_id'] . '">' . $value . '</option>';
0285                          $ips[$row['site_id']] = $value;
0286                      }
0287                  }
0288                  $db->sql_freeresult($result);
0289   
0290                  $template->assign_vars(array(
0291                      'S_SECURE_DOWNLOADS'    => $this->new_config['secure_downloads'],
0292                      'S_DEFINED_IPS'            => ($defined_ips != '') ? true : false,
0293                      'S_WARNING'                => (sizeof($error)) ? true : false,
0294   
0295                      'WARNING_MSG'            => implode('<br />', $error),
0296                      'DEFINED_IPS'            => $defined_ips,
0297   
0298                      'L_SECURE_TITLE'        => $user->lang['DEFINE_' . $allow_deny . '_IPS'],
0299                      'L_IP_EXCLUDE'            => $user->lang['EXCLUDE_FROM_' . $allow_deny . '_IP'],
0300                      'L_REMOVE_IPS'            => $user->lang['REMOVE_' . $allow_deny . '_IPS'])
0301                  );
0302   
0303                  // Output relevant options
0304                  foreach ($display_vars['vars'] as $config_key => $vars)
0305                  {
0306                      if (!is_array($vars) && strpos($config_key, 'legend') === false)
0307                      {
0308                          continue;
0309                      }
0310   
0311                      if (strpos($config_key, 'legend') !== false)
0312                      {
0313                          $template->assign_block_vars('options', array(
0314                              'S_LEGEND'        => true,
0315                              'LEGEND'        => (isset($user->lang[$vars])) ? $user->lang[$vars] : $vars)
0316                          );
0317   
0318                          continue;
0319                      }
0320   
0321                      $type = explode(':', $vars['type']);
0322   
0323                      $l_explain = '';
0324                      if ($vars['explain'] && isset($vars['lang_explain']))
0325                      {
0326                          $l_explain = (isset($user->lang[$vars['lang_explain']])) ? $user->lang[$vars['lang_explain']] : $vars['lang_explain'];
0327                      }
0328                      else if ($vars['explain'])
0329                      {
0330                          $l_explain = (isset($user->lang[$vars['lang'] . '_EXPLAIN'])) ? $user->lang[$vars['lang'] . '_EXPLAIN'] : '';
0331                      }
0332   
0333                      $content = build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars);
0334                      if (empty($content))
0335                      {
0336                          continue;
0337                      }
0338   
0339                      $template->assign_block_vars('options', array(
0340                          'KEY'            => $config_key,
0341                          'TITLE'            => $user->lang[$vars['lang']],
0342                          'S_EXPLAIN'        => $vars['explain'],
0343                          'TITLE_EXPLAIN'    => $l_explain,
0344                          'CONTENT'        => $content,
0345                          )
0346                      );
0347   
0348                      unset($display_vars['vars'][$config_key]);
0349                  }
0350   
0351              break;
0352   
0353              case 'extensions':
0354   
0355                  if ($submit || isset($_POST['add_extension_check']))
0356                  {
0357                      if ($submit)
0358                      {
0359                          // Change Extensions ?
0360                          $extension_change_list    = $request->variable('extension_change_list', array(0));
0361                          $group_select_list        = $request->variable('group_select', array(0));
0362   
0363                          // Generate correct Change List
0364                          $extensions = array();
0365   
0366                          for ($i = 0, $size = sizeof($extension_change_list); $i < $size; $i++)
0367                          {
0368                              $extensions[$extension_change_list[$i]]['group_id'] = $group_select_list[$i];
0369                          }
0370   
0371                          $sql = 'SELECT *
0372                              FROM ' . EXTENSIONS_TABLE . '
0373                              ORDER BY extension_id';
0374                          $result = $db->sql_query($sql);
0375   
0376                          while ($row = $db->sql_fetchrow($result))
0377                          {
0378                              if ($row['group_id'] != $extensions[$row['extension_id']]['group_id'])
0379                              {
0380                                  $sql = 'UPDATE ' . EXTENSIONS_TABLE . '
0381                                      SET group_id = ' . (int) $extensions[$row['extension_id']]['group_id'] . '
0382                                      WHERE extension_id = ' . $row['extension_id'];
0383                                  $db->sql_query($sql);
0384   
0385                                  $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTACH_EXT_UPDATE', false, array($row['extension']));
0386                              }
0387                          }
0388                          $db->sql_freeresult($result);
0389   
0390                          // Delete Extension?
0391                          $extension_id_list = $request->variable('extension_id_list', array(0));
0392   
0393                          if (sizeof($extension_id_list))
0394                          {
0395                              $sql = 'SELECT extension
0396                                  FROM ' . EXTENSIONS_TABLE . '
0397                                  WHERE ' . $db->sql_in_set('extension_id', $extension_id_list);
0398                              $result = $db->sql_query($sql);
0399   
0400                              $extension_list = '';
0401                              while ($row = $db->sql_fetchrow($result))
0402                              {
0403                                  $extension_list .= ($extension_list == '') ? $row['extension'] : ', ' . $row['extension'];
0404                              }
0405                              $db->sql_freeresult($result);
0406   
0407                              $sql = 'DELETE
0408                                  FROM ' . EXTENSIONS_TABLE . '
0409                                  WHERE ' . $db->sql_in_set('extension_id', $extension_id_list);
0410                              $db->sql_query($sql);
0411   
0412                              $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTACH_EXT_DEL', false, array($extension_list));
0413                          }
0414                      }
0415   
0416                      // Add Extension?
0417                      $add_extension            = strtolower($request->variable('add_extension', ''));
0418                      $add_extension_group    = $request->variable('add_group_select', 0);
0419                      $add                    = (isset($_POST['add_extension_check'])) ? true : false;
0420   
0421                      if ($add_extension && $add)
0422                      {
0423                          if (!sizeof($error))
0424                          {
0425                              $sql = 'SELECT extension_id
0426                                  FROM ' . EXTENSIONS_TABLE . "
0427                                  WHERE extension = '" . $db->sql_escape($add_extension) . "'";
0428                              $result = $db->sql_query($sql);
0429   
0430                              if ($row = $db->sql_fetchrow($result))
0431                              {
0432                                  $error[] = sprintf($user->lang['EXTENSION_EXIST'], $add_extension);
0433                              }
0434                              $db->sql_freeresult($result);
0435   
0436                              if (!sizeof($error))
0437                              {
0438                                  $sql_ary = array(
0439                                      'group_id'    =>    $add_extension_group,
0440                                      'extension'    =>    $add_extension
0441                                  );
0442   
0443                                  $db->sql_query('INSERT INTO ' . EXTENSIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
0444   
0445                                  $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTACH_EXT_ADD', false, array($add_extension));
0446                              }
0447                          }
0448                      }
0449   
0450                      if (!sizeof($error))
0451                      {
0452                          $notify[] = $user->lang['EXTENSIONS_UPDATED'];
0453                      }
0454   
0455                      $cache->destroy('_extensions');
0456                  }
0457   
0458                  $template->assign_vars(array(
0459                      'S_EXTENSIONS'            => true,
0460                      'ADD_EXTENSION'            => (isset($add_extension)) ? $add_extension : '',
0461                      'GROUP_SELECT_OPTIONS'    => (isset($_POST['add_extension_check'])) ? $this->group_select('add_group_select', $add_extension_group, 'extension_group') : $this->group_select('add_group_select', false, 'extension_group'))
0462                  );
0463   
0464                  $sql = 'SELECT *
0465                      FROM ' . EXTENSIONS_TABLE . '
0466                      ORDER BY group_id, extension';
0467                  $result = $db->sql_query($sql);
0468   
0469                  if ($row = $db->sql_fetchrow($result))
0470                  {
0471                      $old_group_id = $row['group_id'];
0472                      do
0473                      {
0474                          $s_spacer = false;
0475   
0476                          $current_group_id = $row['group_id'];
0477                          if ($old_group_id != $current_group_id)
0478                          {
0479                              $s_spacer = true;
0480                              $old_group_id = $current_group_id;
0481                          }
0482   
0483                          $template->assign_block_vars('extensions', array(
0484                              'S_SPACER'        => $s_spacer,
0485                              'EXTENSION_ID'    => $row['extension_id'],
0486                              'EXTENSION'        => $row['extension'],
0487                              'GROUP_OPTIONS'    => $this->group_select('group_select[]', $row['group_id']))
0488                          );
0489                      }
0490                      while ($row = $db->sql_fetchrow($result));
0491                  }
0492                  $db->sql_freeresult($result);
0493   
0494              break;
0495   
0496              case 'ext_groups':
0497   
0498                  $template->assign_var('S_EXTENSION_GROUPS', true);
0499   
0500                  if ($submit)
0501                  {
0502                      $action = $request->variable('action', '');
0503                      $group_id = $request->variable('g', 0);
0504   
0505                      if ($action != 'add' && $action != 'edit')
0506                      {
0507                          trigger_error('NO_MODE', E_USER_ERROR);
0508                      }
0509   
0510                      if (!$group_id && $action == 'edit')
0511                      {
0512                          trigger_error($user->lang['NO_EXT_GROUP_SPECIFIED'] . adm_back_link($this->u_action), E_USER_WARNING);
0513                      }
0514   
0515                      if ($group_id)
0516                      {
0517                          $sql = 'SELECT *
0518                              FROM ' . EXTENSION_GROUPS_TABLE . "
0519                              WHERE group_id = $group_id";
0520                          $result = $db->sql_query($sql);
0521                          $ext_row = $db->sql_fetchrow($result);
0522                          $db->sql_freeresult($result);
0523   
0524                          if (!$ext_row)
0525                          {
0526                              trigger_error($user->lang['NO_EXT_GROUP_SPECIFIED'] . adm_back_link($this->u_action), E_USER_WARNING);
0527                          }
0528                      }
0529                      else
0530                      {
0531                          $ext_row = array();
0532                      }
0533   
0534                      $group_name = $request->variable('group_name', '', true);
0535                      $new_group_name = ($action == 'add') ? $group_name : (($ext_row['group_name'] != $group_name) ? $group_name : '');
0536   
0537                      if (!$group_name)
0538                      {
0539                          $error[] = $user->lang['NO_EXT_GROUP_NAME'];
0540                      }
0541   
0542                      // Check New Group Name
0543                      if ($new_group_name)
0544                      {
0545                          $sql = 'SELECT group_id
0546                              FROM ' . EXTENSION_GROUPS_TABLE . "
0547                              WHERE LOWER(group_name) = '" . $db->sql_escape(utf8_strtolower($new_group_name)) . "'";
0548                          if ($group_id)
0549                          {
0550                              $sql .= ' AND group_id <> ' . $group_id;
0551                          }
0552                          $result = $db->sql_query($sql);
0553   
0554                          if ($db->sql_fetchrow($result))
0555                          {
0556                              $error[] = sprintf($user->lang['EXTENSION_GROUP_EXIST'], $new_group_name);
0557                          }
0558                          $db->sql_freeresult($result);
0559                      }
0560   
0561                      if (!sizeof($error))
0562                      {
0563                          // Ok, build the update/insert array
0564                          $upload_icon    = $request->variable('upload_icon', 'no_image');
0565                          $size_select    = $request->variable('size_select', 'b');
0566                          $forum_select    = $request->variable('forum_select', false);
0567                          $allowed_forums    = $request->variable('allowed_forums', array(0));
0568                          $allow_in_pm    = (isset($_POST['allow_in_pm'])) ? true : false;
0569                          $max_filesize    = $request->variable('max_filesize', 0);
0570                          $max_filesize    = ($size_select == 'kb') ? round($max_filesize * 1024) : (($size_select == 'mb') ? round($max_filesize * 1048576) : $max_filesize);
0571                          $allow_group    = (isset($_POST['allow_group'])) ? true : false;
0572   
0573                          if ($max_filesize == $config['max_filesize'])
0574                          {
0575                              $max_filesize = 0;
0576                          }
0577   
0578                          if (!sizeof($allowed_forums))
0579                          {
0580                              $forum_select = false;
0581                          }
0582   
0583                          $group_ary = array(
0584                              'group_name'    => $group_name,
0585                              'cat_id'        => $request->variable('special_category', ATTACHMENT_CATEGORY_NONE),
0586                              'allow_group'    => ($allow_group) ? 1 : 0,
0587                              'upload_icon'    => ($upload_icon == 'no_image') ? '' : $upload_icon,
0588                              'max_filesize'    => $max_filesize,
0589                              'allowed_forums'=> ($forum_select) ? serialize($allowed_forums) : '',
0590                              'allow_in_pm'    => ($allow_in_pm) ? 1 : 0,
0591                          );
0592   
0593                          if ($action == 'add')
0594                          {
0595                              $group_ary['download_mode'] = INLINE_LINK;
0596                          }
0597   
0598                          $sql = ($action == 'add') ? 'INSERT INTO ' . EXTENSION_GROUPS_TABLE . ' ' : 'UPDATE ' . EXTENSION_GROUPS_TABLE . ' SET ';
0599                          $sql .= $db->sql_build_array((($action == 'add') ? 'INSERT' : 'UPDATE'), $group_ary);
0600                          $sql .= ($action == 'edit') ? " WHERE group_id = $group_id" : '';
0601   
0602                          $db->sql_query($sql);
0603   
0604                          if ($action == 'add')
0605                          {
0606                              $group_id = $db->sql_nextid();
0607                          }
0608   
0609                          $group_name = (isset($user->lang['EXT_GROUP_' . $group_name])) ? $user->lang['EXT_GROUP_' . $group_name] : $group_name;
0610                          $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTACH_EXTGROUP_' . strtoupper($action), false, array($group_name));
0611                      }
0612   
0613                      $extension_list = $request->variable('extensions', array(0));
0614   
0615                      if ($action == 'edit' && sizeof($extension_list))
0616                      {
0617                          $sql = 'UPDATE ' . EXTENSIONS_TABLE . "
0618                              SET group_id = 0
0619                              WHERE group_id = $group_id";
0620                          $db->sql_query($sql);
0621                      }
0622   
0623                      if (sizeof($extension_list))
0624                      {
0625                          $sql = 'UPDATE ' . EXTENSIONS_TABLE . "
0626                              SET group_id = $group_id
0627                              WHERE " . $db->sql_in_set('extension_id', $extension_list);
0628                          $db->sql_query($sql);
0629                      }
0630   
0631                      $cache->destroy('_extensions');
0632   
0633                      if (!sizeof($error))
0634                      {
0635                          $notify[] = $user->lang['SUCCESS_EXTENSION_GROUP_' . strtoupper($action)];
0636                      }
0637                  }
0638   
0639                  $cat_lang = array(
0640                      ATTACHMENT_CATEGORY_NONE        => $user->lang['NO_FILE_CAT'],
0641                      ATTACHMENT_CATEGORY_IMAGE        => $user->lang['CAT_IMAGES'],
0642                      ATTACHMENT_CATEGORY_FLASH        => $user->lang['CAT_FLASH_FILES'],
0643                  );
0644   
0645                  $group_id = $request->variable('g', 0);
0646                  $action = (isset($_POST['add'])) ? 'add' : $action;
0647   
0648                  switch ($action)
0649                  {
0650                      case 'delete':
0651   
0652                          if (confirm_box(true))
0653                          {
0654                              $sql = 'SELECT group_name
0655                                  FROM ' . EXTENSION_GROUPS_TABLE . "
0656                                  WHERE group_id = $group_id";
0657                              $result = $db->sql_query($sql);
0658                              $group_name = (string) $db->sql_fetchfield('group_name');
0659                              $db->sql_freeresult($result);
0660   
0661                              $sql = 'DELETE
0662                                  FROM ' . EXTENSION_GROUPS_TABLE . "
0663                                  WHERE group_id = $group_id";
0664                              $db->sql_query($sql);
0665   
0666                              // Set corresponding Extensions to a pending Group
0667                              $sql = 'UPDATE ' . EXTENSIONS_TABLE . "
0668                                  SET group_id = 0
0669                                  WHERE group_id = $group_id";
0670                              $db->sql_query($sql);
0671   
0672                              $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTACH_EXTGROUP_DEL', false, array($group_name));
0673   
0674                              $cache->destroy('_extensions');
0675   
0676                              trigger_error($user->lang['EXTENSION_GROUP_DELETED'] . adm_back_link($this->u_action));
0677                          }
0678                          else
0679                          {
0680                              confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
0681                                  'i'            => $id,
0682                                  'mode'        => $mode,
0683                                  'group_id'    => $group_id,
0684                                  'action'    => 'delete',
0685                              )));
0686                          }
0687   
0688                      break;
0689   
0690                      case 'edit':
0691   
0692                          if (!$group_id)
0693                          {
0694                              trigger_error($user->lang['NO_EXT_GROUP_SPECIFIED'] . adm_back_link($this->u_action), E_USER_WARNING);
0695                          }
0696   
0697                          $sql = 'SELECT *
0698                              FROM ' . EXTENSION_GROUPS_TABLE . "
0699                              WHERE group_id = $group_id";
0700                          $result = $db->sql_query($sql);
0701                          $ext_group_row = $db->sql_fetchrow($result);
0702                          $db->sql_freeresult($result);
0703   
0704                          $forum_ids = (!$ext_group_row['allowed_forums']) ? array() : unserialize(trim($ext_group_row['allowed_forums']));
0705   
0706                      // no break;
0707   
0708                      case 'add':
0709   
0710                          if ($action == 'add')
0711                          {
0712                              $ext_group_row = array(
0713                                  'group_name'    => $request->variable('group_name', '', true),
0714                                  'cat_id'        => 0,
0715                                  'allow_group'    => 1,
0716                                  'allow_in_pm'    => 1,
0717                                  'upload_icon'    => '',
0718                                  'max_filesize'    => 0,
0719                              );
0720   
0721                              $forum_ids = array();
0722                          }
0723   
0724                          $sql = 'SELECT *
0725                              FROM ' . EXTENSIONS_TABLE . "
0726                              WHERE group_id = $group_id
0727                                  OR group_id = 0
0728                              ORDER BY extension";
0729                          $result = $db->sql_query($sql);
0730                          $extensions = $db->sql_fetchrowset($result);
0731                          $db->sql_freeresult($result);
0732   
0733                          if ($ext_group_row['max_filesize'] == 0)
0734                          {
0735                              $ext_group_row['max_filesize'] = (int) $config['max_filesize'];
0736                          }
0737   
0738                          $max_filesize = get_formatted_filesize($ext_group_row['max_filesize'], false, array('mb', 'kb', 'b'));
0739                          $size_format = $max_filesize['si_identifier'];
0740                          $ext_group_row['max_filesize'] = $max_filesize['value'];
0741   
0742                          $img_path = $config['upload_icons_path'];
0743   
0744                          $filename_list = '';
0745                          $no_image_select = false;
0746   
0747                          $imglist = filelist($phpbb_root_path . $img_path);
0748   
0749                          if (!empty($imglist['']))
0750                          {
0751                              $imglist = array_values($imglist);
0752                              $imglist = $imglist[0];
0753   
0754                              foreach ($imglist as $key => $img)
0755                              {
0756                                  if (!$ext_group_row['upload_icon'])
0757                                  {
0758                                      $no_image_select = true;
0759                                      $selected = '';
0760                                  }
0761                                  else
0762                                  {
0763                                      $selected = ($ext_group_row['upload_icon'] == $img) ? ' selected="selected"' : '';
0764                                  }
0765   
0766                                  if (strlen($img) > 255)
0767                                  {
0768                                      continue;
0769                                  }
0770   
0771                                  $filename_list .= '<option value="' . htmlspecialchars($img) . '"' . $selected . '>' . htmlspecialchars($img) . '</option>';
0772                              }
0773                          }
0774   
0775                          $i = 0;
0776                          $assigned_extensions = '';
0777                          foreach ($extensions as $num => $row)
0778                          {
0779                              if ($row['group_id'] == $group_id && $group_id)
0780                              {
0781                                  $assigned_extensions .= ($i) ? ', ' . $row['extension'] : $row['extension'];
0782                                  $i++;
0783                              }
0784                          }
0785   
0786                          $s_extension_options = '';
0787                          foreach ($extensions as $row)
0788                          {
0789                              $s_extension_options .= '<option' . ((!$row['group_id']) ? ' class="disabled"' : '') . ' value="' . $row['extension_id'] . '"' . (($row['group_id'] == $group_id && $group_id) ? ' selected="selected"' : '') . '>' . $row['extension'] . '</option>';
0790                          }
0791   
0792                          $template->assign_vars(array(
0793                              'IMG_PATH'                => $img_path,
0794                              'ACTION'                => $action,
0795                              'GROUP_ID'                => $group_id,
0796                              'GROUP_NAME'            => $ext_group_row['group_name'],
0797                              'ALLOW_GROUP'            => $ext_group_row['allow_group'],
0798                              'ALLOW_IN_PM'            => $ext_group_row['allow_in_pm'],
0799                              'UPLOAD_ICON_SRC'        => $phpbb_root_path . $img_path . '/' . $ext_group_row['upload_icon'],
0800                              'EXTGROUP_FILESIZE'        => $ext_group_row['max_filesize'],
0801                              'ASSIGNED_EXTENSIONS'    => $assigned_extensions,
0802   
0803                              'S_CATEGORY_SELECT'            => $this->category_select('special_category', $group_id, 'category'),
0804                              'S_EXT_GROUP_SIZE_OPTIONS'    => size_select_options($size_format),
0805                              'S_EXTENSION_OPTIONS'        => $s_extension_options,
0806                              'S_FILENAME_LIST'            => $filename_list,
0807                              'S_EDIT_GROUP'                => true,
0808                              'S_NO_IMAGE'                => $no_image_select,
0809                              'S_FORUM_IDS'                => (sizeof($forum_ids)) ? true : false,
0810   
0811                              'U_EXTENSIONS'        => append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&amp;mode=extensions"),
0812                              'U_BACK'            => $this->u_action,
0813   
0814                              'L_LEGEND'            => $user->lang[strtoupper($action) . '_EXTENSION_GROUP'])
0815                          );
0816   
0817                          $s_forum_id_options = '';
0818   
0819                          /** @todo use in-built function **/
0820   
0821                          $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
0822                              FROM ' . FORUMS_TABLE . '
0823                              ORDER BY left_id ASC';
0824                          $result = $db->sql_query($sql, 600);
0825   
0826                          $right = $cat_right = $padding_inc = 0;
0827                          $padding = $forum_list = $holding = '';
0828                          $padding_store = array('0' => '');
0829   
0830                          while ($row = $db->sql_fetchrow($result))
0831                          {
0832                              if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
0833                              {
0834                                  // Non-postable forum with no subforums, don't display
0835                                  continue;
0836                              }
0837   
0838                              if (!$auth->acl_get('f_list', $row['forum_id']))
0839                              {
0840                                  // if the user does not have permissions to list this forum skip
0841                                  continue;
0842                              }
0843   
0844                              if ($row['left_id'] < $right)
0845                              {
0846                                  $padding .= '&nbsp; &nbsp;';
0847                                  $padding_store[$row['parent_id']] = $padding;
0848                              }
0849                              else if ($row['left_id'] > $right + 1)
0850                              {
0851                                  $padding = empty($padding_store[$row['parent_id']]) ? '' : $padding_store[$row['parent_id']];
0852                              }
0853   
0854                              $right = $row['right_id'];
0855   
0856                              $selected = (in_array($row['forum_id'], $forum_ids)) ? ' selected="selected"' : '';
0857   
0858                              if ($row['left_id'] > $cat_right)
0859                              {
0860                                  // make sure we don't forget anything
0861                                  $s_forum_id_options .= $holding;
0862                                  $holding = '';
0863                              }
0864   
0865                              if ($row['right_id'] - $row['left_id'] > 1)
0866                              {
0867                                  $cat_right = max($cat_right, $row['right_id']);
0868   
0869                                  $holding .= '<option value="' . $row['forum_id'] . '"' . (($row['forum_type'] == FORUM_POST) ? ' class="sep"' : '') . $selected . '>' . $padding . $row['forum_name'] . '</option>';
0870                              }
0871                              else
0872                              {
0873                                  $s_forum_id_options .= $holding . '<option value="' . $row['forum_id'] . '"' . (($row['forum_type'] == FORUM_POST) ? ' class="sep"' : '') . $selected . '>' . $padding . $row['forum_name'] . '</option>';
0874                                  $holding = '';
0875                              }
0876                          }
0877   
0878                          if ($holding)
0879                          {
0880                              $s_forum_id_options .= $holding;
0881                          }
0882   
0883                          $db->sql_freeresult($result);
0884                          unset($padding_store);
0885   
0886                          $template->assign_vars(array(
0887                              'S_FORUM_ID_OPTIONS'    => $s_forum_id_options)
0888                          );
0889   
0890                      break;
0891                  }
0892   
0893                  $sql = 'SELECT *
0894                      FROM ' . EXTENSION_GROUPS_TABLE . '
0895                      ORDER BY allow_group DESC, allow_in_pm DESC, group_name';
0896                  $result = $db->sql_query($sql);
0897   
0898                  $old_allow_group = $old_allow_pm = 1;
0899                  while ($row = $db->sql_fetchrow($result))
0900                  {
0901                      $s_add_spacer = ($old_allow_group != $row['allow_group'] || $old_allow_pm != $row['allow_in_pm']) ? true : false;
0902   
0903                      $template->assign_block_vars('groups', array(
0904                          'S_ADD_SPACER'        => $s_add_spacer,
0905                          'S_ALLOWED_IN_PM'    => ($row['allow_in_pm']) ? true : false,
0906                          'S_GROUP_ALLOWED'    => ($row['allow_group']) ? true : false,
0907   
0908                          'U_EDIT'        => $this->u_action . "&amp;action=edit&amp;g={$row['group_id']}",
0909                          'U_DELETE'        => $this->u_action . "&amp;action=delete&amp;g={$row['group_id']}",
0910   
0911                          'GROUP_NAME'    => (isset($user->lang['EXT_GROUP_' . $row['group_name']])) ? $user->lang['EXT_GROUP_' . $row['group_name']] : $row['group_name'],
0912                          'CATEGORY'        => $cat_lang[$row['cat_id']],
0913                          )
0914                      );
0915   
0916                      $old_allow_group = $row['allow_group'];
0917                      $old_allow_pm = $row['allow_in_pm'];
0918                  }
0919                  $db->sql_freeresult($result);
0920   
0921              break;
0922   
0923              case 'orphan':
0924   
0925                  if ($submit)
0926                  {
0927                      $delete_files = (isset($_POST['delete'])) ? array_keys($request->variable('delete', array('' => 0))) : array();
0928                      $add_files = (isset($_POST['add'])) ? array_keys($request->variable('add', array('' => 0))) : array();
0929                      $post_ids = $request->variable('post_id', array('' => 0));
0930   
0931                      if (sizeof($delete_files))
0932                      {
0933                          $sql = 'SELECT *
0934                              FROM ' . ATTACHMENTS_TABLE . '
0935                              WHERE ' . $db->sql_in_set('attach_id', $delete_files) . '
0936                                  AND is_orphan = 1';
0937                          $result = $db->sql_query($sql);
0938   
0939                          $delete_files = array();
0940                          while ($row = $db->sql_fetchrow($result))
0941                          {
0942                              $this->attachment_manager->unlink($row['physical_filename'], 'file');
0943   
0944                              if ($row['thumbnail'])
0945                              {
0946                                  $this->attachment_manager->unlink($row['physical_filename'], 'thumbnail');
0947                              }
0948   
0949                              $delete_files[$row['attach_id']] = $row['real_filename'];
0950                          }
0951                          $db->sql_freeresult($result);
0952                      }
0953   
0954                      if (sizeof($delete_files))
0955                      {
0956                          $sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
0957                              WHERE ' . $db->sql_in_set('attach_id', array_keys($delete_files));
0958                          $db->sql_query($sql);
0959   
0960                          $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTACH_ORPHAN_DEL', false, array(implode(', ', $delete_files)));
0961                          $notify[] = sprintf($user->lang['LOG_ATTACH_ORPHAN_DEL'], implode($user->lang['COMMA_SEPARATOR'], $delete_files));
0962                      }
0963   
0964                      $upload_list = array();
0965                      foreach ($add_files as $attach_id)
0966                      {
0967                          if (!isset($delete_files[$attach_id]) && !empty($post_ids[$attach_id]))
0968                          {
0969                              $upload_list[$attach_id] = $post_ids[$attach_id];
0970                          }
0971                      }
0972                      unset($add_files);
0973   
0974                      if (sizeof($upload_list))
0975                      {
0976                          $template->assign_var('S_UPLOADING_FILES', true);
0977   
0978                          $sql = 'SELECT forum_id, forum_name
0979                              FROM ' . FORUMS_TABLE;
0980                          $result = $db->sql_query($sql);
0981   
0982                          $forum_names = array();
0983                          while ($row = $db->sql_fetchrow($result))
0984                          {
0985                              $forum_names[$row['forum_id']] = $row['forum_name'];
0986                          }
0987                          $db->sql_freeresult($result);
0988   
0989                          $sql = 'SELECT forum_id, topic_id, post_id, poster_id
0990                              FROM ' . POSTS_TABLE . '
0991                              WHERE ' . $db->sql_in_set('post_id', $upload_list);
0992                          $result = $db->sql_query($sql);
0993   
0994                          $post_info = array();
0995                          while ($row = $db->sql_fetchrow($result))
0996                          {
0997                              $post_info[$row['post_id']] = $row;
0998                          }
0999                          $db->sql_freeresult($result);
1000   
1001                          // Select those attachments we want to change...
1002                          $sql = 'SELECT *
1003                              FROM ' . ATTACHMENTS_TABLE . '
1004                              WHERE ' . $db->sql_in_set('attach_id', array_keys($upload_list)) . '
1005                                  AND is_orphan = 1';
1006                          $result = $db->sql_query($sql);
1007   
1008                          $files_added = $space_taken = 0;
1009                          while ($row = $db->sql_fetchrow($result))
1010                          {
1011                              $post_row = $post_info[$upload_list[$row['attach_id']]];
1012   
1013                              $template->assign_block_vars('upload', array(
1014                                  'FILE_INFO'        => sprintf($user->lang['UPLOADING_FILE_TO'], $row['real_filename'], $post_row['post_id']),
1015                                  'S_DENIED'        => (!$auth->acl_get('f_attach', $post_row['forum_id'])) ? true : false,
1016                                  'L_DENIED'        => (!$auth->acl_get('f_attach', $post_row['forum_id'])) ? sprintf($user->lang['UPLOAD_DENIED_FORUM'], $forum_names[$row['forum_id']]) : '')
1017                              );
1018   
1019                              if (!$auth->acl_get('f_attach', $post_row['forum_id']))
1020                              {
1021                                  continue;
1022                              }
1023   
1024                              // Adjust attachment entry
1025                              $sql_ary = array(
1026                                  'in_message'    => 0,
1027                                  'is_orphan'        => 0,
1028                                  'poster_id'        => $post_row['poster_id'],
1029                                  'post_msg_id'    => $post_row['post_id'],
1030                                  'topic_id'        => $post_row['topic_id'],
1031                              );
1032   
1033                              $sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
1034                                  SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
1035                                  WHERE attach_id = ' . $row['attach_id'];
1036                              $db->sql_query($sql);
1037   
1038                              $sql = 'UPDATE ' . POSTS_TABLE . '
1039                                  SET post_attachment = 1
1040                                  WHERE post_id = ' . $post_row['post_id'];
1041                              $db->sql_query($sql);
1042   
1043                              $sql = 'UPDATE ' . TOPICS_TABLE . '
1044                                  SET topic_attachment = 1
1045                                  WHERE topic_id = ' . $post_row['topic_id'];
1046                              $db->sql_query($sql);
1047   
1048                              $space_taken += $row['filesize'];
1049                              $files_added++;
1050   
1051                              $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTACH_FILEUPLOAD', false, array($post_row['post_id'], $row['real_filename']));
1052                          }
1053                          $db->sql_freeresult($result);
1054   
1055                          if ($files_added)
1056                          {
1057                              $config->increment('upload_dir_size', $space_taken, false);
1058                              $config->increment('num_files', $files_added, false);
1059                          }
1060                      }
1061                  }
1062   
1063                  $template->assign_vars(array(
1064                      'S_ORPHAN'        => true)
1065                  );
1066   
1067                  // Just get the files with is_orphan set and older than 3 hours
1068                  $sql = 'SELECT *
1069                      FROM ' . ATTACHMENTS_TABLE . '
1070                      WHERE is_orphan = 1
1071                          AND filetime < ' . (time() - 3*60*60) . '
1072                      ORDER BY filetime DESC';
1073                  $result = $db->sql_query($sql);
1074   
1075                  while ($row = $db->sql_fetchrow($result))
1076                  {
1077                      $template->assign_block_vars('orphan', array(
1078                          'FILESIZE'            => get_formatted_filesize($row['filesize']),
1079                          'FILETIME'            => $user->format_date($row['filetime']),
1080                          'REAL_FILENAME'        => utf8_basename($row['real_filename']),
1081                          'PHYSICAL_FILENAME'    => utf8_basename($row['physical_filename']),
1082                          'ATTACH_ID'            => $row['attach_id'],
1083                          'POST_IDS'            => (!empty($post_ids[$row['attach_id']])) ? $post_ids[$row['attach_id']] : '',
1084                          'U_FILE'            => append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'mode=view&amp;id=' . $row['attach_id']))
1085                      );
1086                  }
1087                  $db->sql_freeresult($result);
1088   
1089              break;
1090   
1091              case 'manage':
1092   
1093                  if ($submit)
1094                  {
1095                      $delete_files = (isset($_POST['delete'])) ? array_keys($request->variable('delete', array('' => 0))) : array();
1096   
1097                      if (sizeof($delete_files))
1098                      {
1099                          // Select those attachments we want to delete...
1100                          $sql = 'SELECT real_filename
1101                              FROM ' . ATTACHMENTS_TABLE . '
1102                              WHERE ' . $db->sql_in_set('attach_id', $delete_files) . '
1103                                  AND is_orphan = 0';
1104                          $result = $db->sql_query($sql);
1105                          while ($row = $db->sql_fetchrow($result))
1106                          {
1107                              $deleted_filenames[] = $row['real_filename'];
1108                          }
1109                          $db->sql_freeresult($result);
1110   
1111                          if ($num_deleted = $this->attachment_manager->delete('attach', $delete_files))
1112                          {
1113                              if (sizeof($delete_files) != $num_deleted)
1114                              {
1115                                  $error[] = $user->lang['FILES_GONE'];
1116                              }
1117   
1118                              $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTACHMENTS_DELETED', false, array(implode(', ', $deleted_filenames)));
1119                              $notify[] = sprintf($user->lang['LOG_ATTACHMENTS_DELETED'], implode($user->lang['COMMA_SEPARATOR'], $deleted_filenames));
1120                          }
1121                          else
1122                          {
1123                              $error[] = $user->lang['NO_FILES_TO_DELETE'];
1124                          }
1125                      }
1126                  }
1127   
1128                  if ($action == 'stats')
1129                  {
1130                      $this->handle_stats_resync();
1131                  }
1132   
1133                  $stats_error = $this->check_stats_accuracy();
1134   
1135                  if ($stats_error)
1136                  {
1137                      $error[] = $stats_error;
1138                  }
1139   
1140                  $template->assign_vars(array(
1141                      'S_MANAGE'        => true,
1142                  ));
1143   
1144                  $start        = $request->variable('start', 0);
1145   
1146                  // Sort keys
1147                  $sort_days    = $request->variable('st', 0);
1148                  $sort_key    = $request->variable('sk', 't');
1149                  $sort_dir    = $request->variable('sd', 'd');
1150   
1151                  // Sorting
1152                  $limit_days = array(0 => $user->lang['ALL_ENTRIES'], 1 => $user->lang['1_DAY'], 7 => $user->lang['7_DAYS'], 14 => $user->lang['2_WEEKS'], 30 => $user->lang['1_MONTH'], 90 => $user->lang['3_MONTHS'], 180 => $user->lang['6_MONTHS'], 365 => $user->lang['1_YEAR']);
1153                  $sort_by_text = array('f' => $user->lang['FILENAME'], 't' => $user->lang['FILEDATE'], 's' => $user->lang['FILESIZE'], 'x' => $user->lang['EXTENSION'], 'd' => $user->lang['DOWNLOADS'],'p' => $user->lang['ATTACH_POST_TYPE'], 'u' => $user->lang['AUTHOR']);
1154                  $sort_by_sql = array('f' => 'a.real_filename', 't' => 'a.filetime', 's' => 'a.filesize', 'x' => 'a.extension', 'd' => 'a.download_count', 'p' => 'a.in_message', 'u' => 'u.username');
1155   
1156                  $s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
1157                  gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
1158   
1159                  $min_filetime = ($sort_days) ? (time() - ($sort_days * 86400)) : '';
1160                  $limit_filetime = ($min_filetime) ? " AND a.filetime >= $min_filetime " : '';
1161                  $start = ($sort_days && isset($_POST['sort'])) ? 0 : $start;
1162   
1163                  $attachments_per_page = (int) $config['topics_per_page'];
1164   
1165                  $stats = $this->get_attachment_stats($limit_filetime);
1166                  $num_files = $stats['num_files'];
1167                  $total_size = $stats['upload_dir_size'];
1168   
1169                  // Make sure $start is set to the last page if it exceeds the amount
1170                  /* @var $pagination \phpbb\pagination */
1171                  $pagination = $phpbb_container->get('pagination');
1172                  $start = $pagination->validate_start($start, $attachments_per_page, $num_files);
1173   
1174                  // If the user is trying to reach the second half of the attachments list, fetch it starting from the end
1175                  $store_reverse = false;
1176                  $sql_limit = $attachments_per_page;
1177   
1178                  if ($start > $num_files / 2)
1179                  {
1180                      $store_reverse = true;
1181   
1182                      // Select the sort order. Add time sort anchor for non-time sorting cases
1183                      $sql_sort_anchor = ($sort_key != 't') ? ', a.filetime ' . (($sort_dir == 'd') ? 'ASC' : 'DESC') : '';
1184                      $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'ASC' : 'DESC') . $sql_sort_anchor;
1185                      $sql_limit = $pagination->reverse_limit($start, $sql_limit, $num_files);
1186                      $sql_start = $pagination->reverse_start($start, $sql_limit, $num_files);
1187                  }
1188                  else
1189                  {
1190                      // Select the sort order. Add time sort anchor for non-time sorting cases
1191                      $sql_sort_anchor = ($sort_key != 't') ? ', a.filetime ' . (($sort_dir == 'd') ? 'DESC' : 'ASC') : '';
1192                      $sql_sort_order = $sort_by_sql[$sort_key] . ' ' . (($sort_dir == 'd') ? 'DESC' : 'ASC') . $sql_sort_anchor;
1193                      $sql_start = $start;
1194                  }
1195   
1196                  $attachments_list = array();
1197   
1198                  // Just get the files
1199                  $sql = 'SELECT a.*, u.username, u.user_colour, t.topic_title
1200                      FROM ' . ATTACHMENTS_TABLE . ' a
1201                      LEFT JOIN ' . USERS_TABLE . ' u ON (u.user_id = a.poster_id)
1202                      LEFT JOIN ' . TOPICS_TABLE . " t ON (a.topic_id = t.topic_id)
1203                      WHERE a.is_orphan = 0
1204                          $limit_filetime
1205                      ORDER BY $sql_sort_order";
1206                  $result = $db->sql_query_limit($sql, $sql_limit, $sql_start);
1207   
1208                  $i = ($store_reverse) ? $sql_limit - 1 : 0;
1209   
1210                  // Store increment value in a variable to save some conditional calls
1211                  $i_increment = ($store_reverse) ? -1 : 1;
1212                  while ($attachment_row = $db->sql_fetchrow($result))
1213                  {
1214                      $attachments_list[$i] = $attachment_row;
1215                      $i = $i + $i_increment;
1216                  }
1217                  $db->sql_freeresult($result);
1218   
1219                  $base_url = $this->u_action . "&amp;$u_sort_param";
1220                  $pagination->generate_template_pagination($base_url, 'pagination', 'start', $num_files, $attachments_per_page, $start);
1221   
1222                  $template->assign_vars(array(
1223                      'TOTAL_FILES'        => $num_files,
1224                      'TOTAL_SIZE'        => get_formatted_filesize($total_size),
1225   
1226                      'S_LIMIT_DAYS'        => $s_limit_days,
1227                      'S_SORT_KEY'        => $s_sort_key,
1228                      'S_SORT_DIR'        => $s_sort_dir)
1229                  );
1230   
1231                  // Grab extensions
1232                  $extensions = $cache->obtain_attach_extensions(true);
1233   
1234                  for ($i = 0, $end = sizeof($attachments_list); $i < $end; ++$i)
1235                  {
1236                      $row = $attachments_list[$i];
1237   
1238                      $row['extension'] = strtolower(trim((string) $row['extension']));
1239                      $comment = ($row['attach_comment'] && !$row['in_message']) ? str_replace(array("\n", "\r"), array('<br />', "\n"), $row['attach_comment']) : '';
1240                      $display_cat = $extensions[$row['extension']]['display_cat'];
1241                      $l_downloaded_viewed = ($display_cat == ATTACHMENT_CATEGORY_NONE) ? 'DOWNLOAD_COUNTS' : 'VIEWED_COUNTS';
1242   
1243                      $template->assign_block_vars('attachments', array(
1244                          'ATTACHMENT_POSTER'    => get_username_string('full', (int) $row['poster_id'], (string) $row['username'], (string) $row['user_colour'], (string) $row['username']),
1245                          'FILESIZE'            => get_formatted_filesize((int) $row['filesize']),
1246                          'FILETIME'            => $user->format_date((int) $row['filetime']),
1247                          'REAL_FILENAME'        => (!$row['in_message']) ? utf8_basename((string) $row['real_filename']) : '',
1248                          'PHYSICAL_FILENAME'    => utf8_basename((string) $row['physical_filename']),
1249                          'EXT_GROUP_NAME'    => (!empty($extensions[$row['extension']]['group_name'])) ? $user->lang['EXT_GROUP_' . $extensions[$row['extension']]['group_name']] : '',
1250                          'COMMENT'            => $comment,
1251                          'TOPIC_TITLE'        => (!$row['in_message']) ? (string) $row['topic_title'] : '',
1252                          'ATTACH_ID'            => (int) $row['attach_id'],
1253                          'POST_ID'            => (int) $row['post_msg_id'],
1254                          'TOPIC_ID'            => (int) $row['topic_id'],
1255                          'POST_IDS'            => (!empty($post_ids[$row['attach_id']])) ? (int) $post_ids[$row['attach_id']] : '',
1256   
1257                          'L_DOWNLOAD_COUNT'    => $user->lang($l_downloaded_viewed, (int) $row['download_count']),
1258   
1259                          'S_IN_MESSAGE'        => (bool) $row['in_message'],
1260   
1261                          'U_VIEW_TOPIC'        => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t={$row['topic_id']}&amp;p={$row['post_msg_id']}") . "#p{$row['post_msg_id']}",
1262                          'U_FILE'            => append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'mode=view&amp;id=' . $row['attach_id']))
1263                      );
1264                  }
1265   
1266              break;
1267          }
1268   
1269          if (sizeof($error))
1270          {
1271              $template->assign_vars(array(
1272                  'S_WARNING'        => true,
1273                  'WARNING_MSG'    => implode('<br />', $error))
1274              );
1275          }
1276   
1277          if (sizeof($notify))
1278          {
1279              $template->assign_vars(array(
1280                  'S_NOTIFY'        => true,
1281                  'NOTIFY_MSG'    => implode('<br />', $notify))
1282              );
1283          }
1284      }
1285   
1286      /**
1287      * Get attachment file count and size of upload directory
1288      *
1289      * @param $limit string    Additional limit for WHERE clause to filter stats by.
1290      * @return array Returns array with stats: num_files and upload_dir_size
1291      */
1292      public function get_attachment_stats($limit = '')
1293      {
1294          $sql = 'SELECT COUNT(a.attach_id) AS num_files, SUM(a.filesize) AS upload_dir_size
1295              FROM ' . ATTACHMENTS_TABLE . " a
1296              WHERE a.is_orphan = 0
1297                  $limit";
1298          $result = $this->db->sql_query($sql);
1299          $row = $this->db->sql_fetchrow($result);
1300          $this->db->sql_freeresult($result);
1301   
1302          return array(
1303              'num_files'            => (int) $row['num_files'],
1304              'upload_dir_size'    => (float) $row['upload_dir_size'],
1305          );
1306      }
1307   
1308      /**
1309      * Set config attachment stat values
1310      *
1311      * @param $stats array    Array of config key => value pairs to set.
1312      * @return null
1313      */
1314      public function set_attachment_stats($stats)
1315      {
1316          foreach ($stats as $key => $value)
1317          {
1318              $this->config->set($key, $value, true);
1319          }
1320      }
1321   
1322      /**
1323      * Check accuracy of attachment statistics.
1324      *
1325      * @return bool|string    Returns false if stats are correct or error message
1326      *    otherwise.
1327      */
1328      public function check_stats_accuracy()
1329      {
1330          // Get fresh stats.
1331          $stats = $this->get_attachment_stats();
1332   
1333          // Get current files stats
1334          $num_files = (int) $this->config['num_files'];
1335          $total_size = (float) $this->config['upload_dir_size'];
1336   
1337          if (($num_files != $stats['num_files']) || ($total_size != $stats['upload_dir_size']))
1338          {
1339              $u_resync = $this->u_action . '&amp;action=stats';
1340   
1341              return $this->user->lang(
1342                  'FILES_STATS_WRONG',
1343                  (int) $stats['num_files'],
1344                  get_formatted_filesize($stats['upload_dir_size']),
1345                  '<a href="' . $u_resync . '">',
1346                  '</a>'
1347              );
1348          }
1349          return false;
1350      }
1351   
1352      /**
1353      * Handle stats resync.
1354      *
1355      * @return null
1356      */
1357      public function handle_stats_resync()
1358      {
1359          if (!confirm_box(true))
1360          {
1361              confirm_box(false, $this->user->lang['RESYNC_FILES_STATS_CONFIRM'], build_hidden_fields(array(
1362                  'i'            => $this->id,
1363                  'mode'        => 'manage',
1364                  'action'    => 'stats',
1365              )));
1366          }
1367          else
1368          {
1369              $this->set_attachment_stats($this->get_attachment_stats());
1370   
1371              /* @var $log \phpbb\log\log_interface */
1372              $log = $this->phpbb_container->get('log');
1373              $log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_RESYNC_FILES_STATS');
1374          }
1375   
1376      }
1377   
1378      /**
1379      * Build Select for category items
1380      */
1381      function category_select($select_name, $group_id = false, $key = '')
1382      {
1383          global $db, $user;
1384   
1385          $types = array(
1386              ATTACHMENT_CATEGORY_NONE        => $user->lang['NO_FILE_CAT'],
1387              ATTACHMENT_CATEGORY_IMAGE        => $user->lang['CAT_IMAGES'],
1388              ATTACHMENT_CATEGORY_FLASH        => $user->lang['CAT_FLASH_FILES'],
1389          );
1390   
1391          if ($group_id)
1392          {
1393              $sql = 'SELECT cat_id
1394                  FROM ' . EXTENSION_GROUPS_TABLE . '
1395                  WHERE group_id = ' . (int) $group_id;
1396              $result = $db->sql_query($sql);
1397   
1398              $cat_type = (!($row = $db->sql_fetchrow($result))) ? ATTACHMENT_CATEGORY_NONE : $row['cat_id'];
1399   
1400              $db->sql_freeresult($result);
1401          }
1402          else
1403          {
1404              $cat_type = ATTACHMENT_CATEGORY_NONE;
1405          }
1406   
1407          $group_select = '<select name="' . $select_name . '"' . (($key) ? ' id="' . $key . '"' : '') . '>';
1408   
1409          foreach ($types as $type => $mode)
1410          {
1411              $selected = ($type == $cat_type) ? ' selected="selected"' : '';
1412              $group_select .= '<option value="' . $type . '"' . $selected . '>' . $mode . '</option>';
1413          }
1414   
1415          $group_select .= '</select>';
1416   
1417          return $group_select;
1418      }
1419   
1420      /**
1421      * Extension group select
1422      */
1423      function group_select($select_name, $default_group = false, $key = '')
1424      {
1425          global $db, $user;
1426   
1427          $group_select = '<select name="' . $select_name . '"' . (($key) ? ' id="' . $key . '"' : '') . '>';
1428   
1429          $sql = 'SELECT group_id, group_name
1430              FROM ' . EXTENSION_GROUPS_TABLE . '
1431              ORDER BY group_name';
1432          $result = $db->sql_query($sql);
1433   
1434          $group_name = array();
1435          while ($row = $db->sql_fetchrow($result))
1436          {
1437              $row['group_name'] = (isset($user->lang['EXT_GROUP_' . $row['group_name']])) ? $user->lang['EXT_GROUP_' . $row['group_name']] : $row['group_name'];
1438              $group_name[] = $row;
1439          }
1440          $db->sql_freeresult($result);
1441   
1442          $row['group_id'] = 0;
1443          $row['group_name'] = $user->lang['NOT_ASSIGNED'];
1444          $group_name[] = $row;
1445   
1446          for ($i = 0, $groups_size = sizeof($group_name); $i < $groups_size; $i++)
1447          {
1448              if ($default_group === false)
1449              {
1450                  $selected = ($i == 0) ? ' selected="selected"' : '';
1451              }
1452              else
1453              {
1454                  $selected = ($group_name[$i]['group_id'] == $default_group) ? ' selected="selected"' : '';
1455              }
1456   
1457              $group_select .= '<option value="' . $group_name[$i]['group_id'] . '"' . $selected . '>' . $group_name[$i]['group_name'] . '</option>';
1458          }
1459   
1460          $group_select .= '</select>';
1461   
1462          return $group_select;
1463      }
1464   
1465      /**
1466      * Search Imagick
1467      */
1468      function search_imagemagick()
1469      {
1470          $imagick = '';
1471   
1472          $exe = ((defined('PHP_OS')) && (preg_match('#^win#i', PHP_OS))) ? '.exe' : '';
1473   
1474          $magic_home = getenv('MAGICK_HOME');
1475   
1476          if (empty($magic_home))
1477          {
1478              $locations = array('C:/WINDOWS/', 'C:/WINNT/', 'C:/WINDOWS/SYSTEM/', 'C:/WINNT/SYSTEM/', 'C:/WINDOWS/SYSTEM32/', 'C:/WINNT/SYSTEM32/', '/usr/bin/', '/usr/sbin/', '/usr/local/bin/', '/usr/local/sbin/', '/opt/', '/usr/imagemagick/', '/usr/bin/imagemagick/');
1479              $path_locations = str_replace('\\', '/', (explode(($exe) ? ';' : ':', getenv('PATH'))));
1480   
1481              $locations = array_merge($path_locations, $locations);
1482   
1483              foreach ($locations as $location)
1484              {
1485                  // The path might not end properly, fudge it
1486                  if (substr($location, -1) !== '/')
1487                  {
1488                      $location .= '/';
1489                  }
1490   
1491                  if (@file_exists($location) && @is_readable($location . 'mogrify' . $exe) && @filesize($location . 'mogrify' . $exe) > 3000)
1492                  {
1493                      $imagick = str_replace('\\', '/', $location);
1494                      continue;
1495                  }
1496              }
1497          }
1498          else
1499          {
1500              $imagick = str_replace('\\', '/', $magic_home);
1501          }
1502   
1503          return $imagick;
1504      }
1505   
1506      /**
1507      * Test Settings
1508      */
1509      function test_upload(&$error, $upload_dir, $create_directory = false)
1510      {
1511          global $user, $phpbb_root_path;
1512   
1513          // Does the target directory exist, is it a directory and writable.
1514          if ($create_directory)
1515          {
1516              if (!file_exists($phpbb_root_path . $upload_dir))
1517              {
1518                  @mkdir($phpbb_root_path . $upload_dir, 0777);
1519   
1520                  try
1521                  {
1522                      $this->filesystem->phpbb_chmod($phpbb_root_path . $upload_dir, CHMOD_READ | CHMOD_WRITE);
1523                  }
1524                  catch (\phpbb\filesystem\exception\filesystem_exception $e)
1525                  {
1526                      // Do nothing
1527                  }
1528              }
1529          }
1530   
1531          if (!file_exists($phpbb_root_path . $upload_dir))
1532          {
1533              $error[] = sprintf($user->lang['NO_UPLOAD_DIR'], $upload_dir);
1534              return;
1535          }
1536   
1537          if (!is_dir($phpbb_root_path . $upload_dir))
1538          {
1539              $error[] = sprintf($user->lang['UPLOAD_NOT_DIR'], $upload_dir);
1540              return;
1541          }
1542   
1543          if (!$this->filesystem->is_writable($phpbb_root_path . $upload_dir))
1544          {
1545              $error[] = sprintf($user->lang['NO_WRITE_UPLOAD'], $upload_dir);
1546              return;
1547          }
1548      }
1549   
1550      /**
1551      * Perform operations on sites for external linking
1552      */
1553      function perform_site_list()
1554      {
1555          global $db, $user, $request, $phpbb_log;
1556   
1557          if (isset($_REQUEST['securesubmit']))
1558          {
1559              // Grab the list of entries
1560              $ips = $request->variable('ips', '');
1561              $ip_list = array_unique(explode("\n", $ips));
1562              $ip_list_log = implode(', ', $ip_list);
1563   
1564              $ip_exclude = (int) $request->variable('ipexclude', false, false, \phpbb\request\request_interface::POST);
1565   
1566              $iplist = array();
1567              $hostlist = array();
1568   
1569              foreach ($ip_list as $item)
1570              {
1571                  if (preg_match('#^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})[ ]*\-[ ]*([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$#', trim($item), $ip_range_explode))
1572                  {
1573                      // Don't ask about all this, just don't ask ... !
1574                      $ip_1_counter = $ip_range_explode[1];
1575                      $ip_1_end = $ip_range_explode[5];
1576   
1577                      while ($ip_1_counter <= $ip_1_end)
1578                      {
1579                          $ip_2_counter = ($ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[2] : 0;
1580                          $ip_2_end = ($ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[6];
1581   
1582                          if ($ip_2_counter == 0 && $ip_2_end == 254)
1583                          {
1584                              $ip_2_counter = 256;
1585   
1586                              $iplist[] = "'$ip_1_counter.*'";
1587                          }
1588   
1589                          while ($ip_2_counter <= $ip_2_end)
1590                          {
1591                              $ip_3_counter = ($ip_2_counter == $ip_range_explode[2] && $ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[3] : 0;
1592                              $ip_3_end = ($ip_2_counter < $ip_2_end || $ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[7];
1593   
1594                              if ($ip_3_counter == 0 && $ip_3_end == 254)
1595                              {
1596                                  $ip_3_counter = 256;
1597   
1598                                  $iplist[] = "'$ip_1_counter.$ip_2_counter.*'";
1599                              }
1600   
1601                              while ($ip_3_counter <= $ip_3_end)
1602                              {
1603                                  $ip_4_counter = ($ip_3_counter == $ip_range_explode[3] && $ip_2_counter == $ip_range_explode[2] && $ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[4] : 0;
1604                                  $ip_4_end = ($ip_3_counter < $ip_3_end || $ip_2_counter < $ip_2_end) ? 254 : $ip_range_explode[8];
1605   
1606                                  if ($ip_4_counter == 0 && $ip_4_end == 254)
1607                                  {
1608                                      $ip_4_counter = 256;
1609   
1610                                      $iplist[] = "'$ip_1_counter.$ip_2_counter.$ip_3_counter.*'";
1611                                  }
1612   
1613                                  while ($ip_4_counter <= $ip_4_end)
1614                                  {
1615                                      $iplist[] = "'$ip_1_counter.$ip_2_counter.$ip_3_counter.$ip_4_counter'";
1616                                      $ip_4_counter++;
1617                                  }
1618                                  $ip_3_counter++;
1619                              }
1620                              $ip_2_counter++;
1621                          }
1622                          $ip_1_counter++;
1623                      }
1624                  }
1625                  else if (preg_match('#^([0-9]{1,3})\.([0-9\*]{1,3})\.([0-9\*]{1,3})\.([0-9\*]{1,3})$#', trim($item)) || preg_match('#^[a-f0-9:]+\*?$#i', trim($item)))
1626                  {
1627                      $iplist[] = "'" . trim($item) . "'";
1628                  }
1629                  else if (preg_match('#^([\w\-_]\.?){2,}$#is', trim($item)))
1630                  {
1631                      $hostlist[] = "'" . trim($item) . "'";
1632                  }
1633                  else if (preg_match("#^([a-z0-9\-\*\._/]+?)$#is", trim($item)))
1634                  {
1635                      $hostlist[] = "'" . trim($item) . "'";
1636                  }
1637              }
1638   
1639              $sql = 'SELECT site_ip, site_hostname
1640                  FROM ' . SITELIST_TABLE . "
1641                  WHERE ip_exclude = $ip_exclude";
1642              $result = $db->sql_query($sql);
1643   
1644              if ($row = $db->sql_fetchrow($result))
1645              {
1646                  $iplist_tmp = array();
1647                  $hostlist_tmp = array();
1648                  do
1649                  {
1650                      if ($row['site_ip'])
1651                      {
1652                          if (strlen($row['site_ip']) > 40)
1653                          {
1654                              continue;
1655                          }
1656   
1657                          $iplist_tmp[] = "'" . $row['site_ip'] . "'";
1658                      }
1659                      else if ($row['site_hostname'])
1660                      {
1661                          if (strlen($row['site_hostname']) > 255)
1662                          {
1663                              continue;
1664                          }
1665   
1666                          $hostlist_tmp[] = "'" . $row['site_hostname'] . "'";
1667                      }
1668                      // break;
1669                  }
1670                  while ($row = $db->sql_fetchrow($result));
1671   
1672                  $iplist = array_unique(array_diff($iplist, $iplist_tmp));
1673                  $hostlist = array_unique(array_diff($hostlist, $hostlist_tmp));
1674                  unset($iplist_tmp);
1675                  unset($hostlist_tmp);
1676              }
1677              $db->sql_freeresult($result);
1678   
1679              if (sizeof($iplist))
1680              {
1681                  foreach ($iplist as $ip_entry)
1682                  {
1683                      $sql = 'INSERT INTO ' . SITELIST_TABLE . " (site_ip, ip_exclude)
1684                          VALUES ($ip_entry$ip_exclude)";
1685                      $db->sql_query($sql);
1686                  }
1687              }
1688   
1689              if (sizeof($hostlist))
1690              {
1691                  foreach ($hostlist as $host_entry)
1692                  {
1693                      $sql = 'INSERT INTO ' . SITELIST_TABLE . " (site_hostname, ip_exclude)
1694                          VALUES ($host_entry$ip_exclude)";
1695                      $db->sql_query($sql);
1696                  }
1697              }
1698   
1699              if (!empty($ip_list_log))
1700              {
1701                  // Update log
1702                  $log_entry = ($ip_exclude) ? 'LOG_DOWNLOAD_EXCLUDE_IP' : 'LOG_DOWNLOAD_IP';
1703                  $phpbb_log->add('admin', $user->data['user_id'], $user->ip, $log_entry, false, array($ip_list_log));
1704              }
1705   
1706              trigger_error($user->lang['SECURE_DOWNLOAD_UPDATE_SUCCESS'] . adm_back_link($this->u_action));
1707          }
1708          else if (isset($_POST['unsecuresubmit']))
1709          {
1710              $unip_sql = $request->variable('unip', array(0));
1711   
1712              if (sizeof($unip_sql))
1713              {
1714                  $l_unip_list = '';
1715   
1716                  // Grab details of ips for logging information later
1717                  $sql = 'SELECT site_ip, site_hostname
1718                      FROM ' . SITELIST_TABLE . '
1719                      WHERE ' . $db->sql_in_set('site_id', $unip_sql);
1720                  $result = $db->sql_query($sql);
1721   
1722                  while ($row = $db->sql_fetchrow($result))
1723                  {
1724                      $l_unip_list .= (($l_unip_list != '') ? ', ' : '') . (($row['site_ip']) ? $row['site_ip'] : $row['site_hostname']);
1725                  }
1726                  $db->sql_freeresult($result);
1727   
1728                  $sql = 'DELETE FROM ' . SITELIST_TABLE . '
1729                      WHERE ' . $db->sql_in_set('site_id', $unip_sql);
1730                  $db->sql_query($sql);
1731   
1732                  $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_DOWNLOAD_REMOVE_IP', false, array($l_unip_list));
1733              }
1734   
1735              trigger_error($user->lang['SECURE_DOWNLOAD_UPDATE_SUCCESS'] . adm_back_link($this->u_action));
1736          }
1737      }
1738   
1739      /**
1740      * Write display_order config field
1741      */
1742      function display_order($value, $key = '')
1743      {
1744          $radio_ary = array(0 => 'DESCENDING', 1 => 'ASCENDING');
1745   
1746          return h_radio('config[display_order]', $radio_ary, $value, $key);
1747      }
1748   
1749      /**
1750      * Adjust all three max_filesize config vars for display
1751      */
1752      function max_filesize($value, $key = '')
1753      {
1754          // Determine size var and adjust the value accordingly
1755          $filesize = get_formatted_filesize($value, false, array('mb', 'kb', 'b'));
1756          $size_var = $filesize['si_identifier'];
1757          $value = $filesize['value'];
1758   
1759          // size and maxlength must not be specified for input of type number
1760          return '<input type="number" id="' . $key . '" min="0" max="999999999999999" step="any" name="config[' . $key . ']" value="' . $value . '" /> <select name="' . $key . '">' . size_select_options($size_var) . '</select>';
1761      }
1762   
1763      /**
1764      * Write secure_allow_deny config field
1765      */
1766      function select_allow_deny($value, $key = '')
1767      {
1768          $radio_ary = array(1 => 'ORDER_ALLOW_DENY', 0 => 'ORDER_DENY_ALLOW');
1769   
1770          return h_radio('config[' . $key . ']', $radio_ary, $value, $key);
1771      }
1772   
1773  }
1774