Blame Identity/Webenv/phpBB/3.0.4/includes/acp/acp_attachments.php

ef5584
ef5584
/**
ef5584
*
ef5584
* @package acp
ef5584
* @version $Id: acp_attachments.php 9041 2008-11-02 11:19:12Z acydburn $
ef5584
* @copyright (c) 2005 phpBB Group
ef5584
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
ef5584
*
ef5584
*/
ef5584
ef5584
/**
ef5584
* @ignore
ef5584
*/
ef5584
if (!defined('IN_PHPBB'))
ef5584
{
ef5584
	exit;
ef5584
}
ef5584
ef5584
/**
ef5584
* @package acp
ef5584
*/
ef5584
class acp_attachments
ef5584
{
ef5584
	var $u_action;
ef5584
	var $new_config;
ef5584
ef5584
	function main($id, $mode)
ef5584
	{
ef5584
		global $db, $user, $auth, $template, $cache;
ef5584
		global $config, $phpbb_admin_path, $phpbb_root_path, $phpEx;
ef5584
ef5584
		$user->add_lang(array('posting', 'viewtopic', 'acp/attachments'));
ef5584
ef5584
		$error = $notify = array();
ef5584
		$submit = (isset($_POST['submit'])) ? true : false;
ef5584
		$action = request_var('action', '');
ef5584
ef5584
		$form_key = 'acp_attach';
ef5584
		add_form_key($form_key);
ef5584
ef5584
		if ($submit && !check_form_key($form_key))
ef5584
		{
ef5584
			trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
		}
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'attach':
ef5584
				$l_title = 'ACP_ATTACHMENT_SETTINGS';
ef5584
			break;
ef5584
ef5584
			case 'extensions':
ef5584
				$l_title = 'ACP_MANAGE_EXTENSIONS';
ef5584
			break;
ef5584
ef5584
			case 'ext_groups':
ef5584
				$l_title = 'ACP_EXTENSION_GROUPS';
ef5584
			break;
ef5584
ef5584
			case 'orphan':
ef5584
				$l_title = 'ACP_ORPHAN_ATTACHMENTS';
ef5584
			break;
ef5584
ef5584
			default:
ef5584
				trigger_error('NO_MODE', E_USER_ERROR);
ef5584
			break;
ef5584
		}
ef5584
ef5584
		$this->tpl_name = 'acp_attachments';
ef5584
		$this->page_title = $l_title;
ef5584
ef5584
		$template->assign_vars(array(
ef5584
			'L_TITLE'			=> $user->lang[$l_title],
ef5584
			'L_TITLE_EXPLAIN'	=> $user->lang[$l_title . '_EXPLAIN'],
ef5584
			'U_ACTION'			=> $this->u_action)
ef5584
		);
ef5584
ef5584
		switch ($mode)
ef5584
		{
ef5584
			case 'attach':
ef5584
ef5584
				include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
ef5584
ef5584
				$sql = 'SELECT group_name, cat_id
ef5584
					FROM ' . EXTENSION_GROUPS_TABLE . '
ef5584
					WHERE cat_id > 0
ef5584
					ORDER BY cat_id';
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				$s_assigned_groups = array();
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$s_assigned_groups[$row['cat_id']][] = $row['group_name'];
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				$l_legend_cat_images = $user->lang['SETTINGS_CAT_IMAGES'] . ' [' . $user->lang['ASSIGNED_GROUP'] . ': ' . ((!empty($s_assigned_groups[ATTACHMENT_CATEGORY_IMAGE])) ? implode(', ', $s_assigned_groups[ATTACHMENT_CATEGORY_IMAGE]) : $user->lang['NO_EXT_GROUP']) . ']';
ef5584
ef5584
				$display_vars = array(
ef5584
					'title'	=> 'ACP_ATTACHMENT_SETTINGS',
ef5584
					'vars'	=> array(
ef5584
						'legend1'				=> 'ACP_ATTACHMENT_SETTINGS',
ef5584
ef5584
						'img_max_width'			=> array('lang' => 'MAX_IMAGE_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
ef5584
						'img_max_height'		=> array('lang' => 'MAX_IMAGE_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
ef5584
						'img_link_width'		=> array('lang' => 'IMAGE_LINK_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
ef5584
						'img_link_height'		=> array('lang' => 'IMAGE_LINK_SIZE', 'validate' => 'int:0', 'type' => false, 'method' => false, 'explain' => false,),
ef5584
ef5584
						'allow_attachments'		=> array('lang' => 'ALLOW_ATTACHMENTS',		'validate' => 'bool',	'type' => 'radio:yes_no', 'explain' => false),
ef5584
						'allow_pm_attach'		=> array('lang' => 'ALLOW_PM_ATTACHMENTS',	'validate' => 'bool',	'type' => 'radio:yes_no', 'explain' => false),
ef5584
						'upload_path'			=> array('lang' => 'UPLOAD_DIR',			'validate' => 'wpath',	'type' => 'text:25:100', 'explain' => true),
ef5584
						'display_order'			=> array('lang' => 'DISPLAY_ORDER',			'validate' => 'bool',	'type' => 'custom', 'method' => 'display_order', 'explain' => true),
ef5584
						'attachment_quota'		=> array('lang' => 'ATTACH_QUOTA',			'validate' => 'string',	'type' => 'custom', 'method' => 'max_filesize', 'explain' => true),
ef5584
						'max_filesize'			=> array('lang' => 'ATTACH_MAX_FILESIZE',	'validate' => 'string',	'type' => 'custom', 'method' => 'max_filesize', 'explain' => true),
ef5584
						'max_filesize_pm'		=> array('lang' => 'ATTACH_MAX_PM_FILESIZE','validate' => 'string',	'type' => 'custom', 'method' => 'max_filesize', 'explain' => true),
ef5584
						'max_attachments'		=> array('lang' => 'MAX_ATTACHMENTS',		'validate' => 'int',	'type' => 'text:3:3', 'explain' => false),
ef5584
						'max_attachments_pm'	=> array('lang' => 'MAX_ATTACHMENTS_PM',	'validate' => 'int',	'type' => 'text:3:3', 'explain' => false),
ef5584
						'secure_downloads'		=> array('lang' => 'SECURE_DOWNLOADS',		'validate' => 'bool',	'type' => 'radio:yes_no', 'explain' => true),
ef5584
						'secure_allow_deny'		=> array('lang' => 'SECURE_ALLOW_DENY',		'validate' => 'int',	'type' => 'custom', 'method' => 'select_allow_deny', 'explain' => true),
ef5584
						'secure_allow_empty_referer'	=> array('lang' => 'SECURE_EMPTY_REFERRER', 'validate' => 'bool',	'type' => 'radio:yes_no', 'explain' => true),
ef5584
						'check_attachment_content' 		=> array('lang' => 'CHECK_CONTENT', 'validate' => 'bool',	'type' => 'radio:yes_no', 'explain' => true),
ef5584
ef5584
ef5584
						'legend2'					=> $l_legend_cat_images,
ef5584
						'img_display_inlined'		=> array('lang' => 'DISPLAY_INLINED',		'validate' => 'bool',	'type' => 'radio:yes_no', 'explain' => true),
ef5584
						'img_create_thumbnail'		=> array('lang' => 'CREATE_THUMBNAIL',		'validate' => 'bool',	'type' => 'radio:yes_no', 'explain' => true),
ef5584
						'img_max_thumb_width'		=> array('lang' => 'MAX_THUMB_WIDTH',		'validate' => 'int',	'type' => 'text:7:15', 'explain' => true, 'append' => ' px'),
ef5584
						'img_min_thumb_filesize'	=> array('lang' => 'MIN_THUMB_FILESIZE',	'validate' => 'int',	'type' => 'text:7:15', 'explain' => true, 'append' => ' ' . $user->lang['BYTES']),
ef5584
						'img_imagick'				=> array('lang' => 'IMAGICK_PATH',			'validate' => 'string',	'type' => 'text:20:200', 'explain' => true, 'append' => '  [ ' . $user->lang['SEARCH_IMAGICK'] . ' ]'),
ef5584
						'img_max'					=> array('lang' => 'MAX_IMAGE_SIZE',		'validate' => 'int',	'type' => 'dimension:3:4', 'explain' => true, 'append' => ' px'),
ef5584
						'img_link'					=> array('lang' => 'IMAGE_LINK_SIZE',		'validate' => 'int',	'type' => 'dimension:3:4', 'explain' => true, 'append' => ' px'),
ef5584
					)
ef5584
				);
ef5584
ef5584
				$this->new_config = $config;
ef5584
				$cfg_array = (isset($_REQUEST['config'])) ? request_var('config', array('' => '')) : $this->new_config;
ef5584
				$error = array();
ef5584
ef5584
				// We validate the complete config if whished
ef5584
				validate_config_vars($display_vars['vars'], $cfg_array, $error);
ef5584
ef5584
				// Do not write values if there is an error
ef5584
				if (sizeof($error))
ef5584
				{
ef5584
					$submit = false;
ef5584
				}
ef5584
ef5584
				// We go through the display_vars to make sure no one is trying to set variables he/she is not allowed to...
ef5584
				foreach ($display_vars['vars'] as $config_name => $null)
ef5584
				{
ef5584
					if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
ef5584
					{
ef5584
						continue;
ef5584
					}
ef5584
ef5584
					$this->new_config[$config_name] = $config_value = $cfg_array[$config_name];
ef5584
ef5584
					if (in_array($config_name, array('attachment_quota', 'max_filesize', 'max_filesize_pm')))
ef5584
					{
ef5584
						$size_var = request_var($config_name, '');
ef5584
						$this->new_config[$config_name] = $config_value = ($size_var == 'kb') ? round($config_value * 1024) : (($size_var == 'mb') ? round($config_value * 1048576) : $config_value);
ef5584
					}
ef5584
ef5584
					if ($submit)
ef5584
					{
ef5584
						set_config($config_name, $config_value);
ef5584
					}
ef5584
				}
ef5584
ef5584
				$this->perform_site_list();
ef5584
ef5584
				if ($submit)
ef5584
				{
ef5584
					add_log('admin', 'LOG_CONFIG_ATTACH');
ef5584
ef5584
					// Check Settings
ef5584
					$this->test_upload($error, $this->new_config['upload_path'], false);
ef5584
ef5584
					if (!sizeof($error))
ef5584
					{
ef5584
						trigger_error($user->lang['CONFIG_UPDATED'] . adm_back_link($this->u_action));
ef5584
					}
ef5584
				}
ef5584
ef5584
				$template->assign_var('S_ATTACHMENT_SETTINGS', true);
ef5584
ef5584
				if ($action == 'imgmagick')
ef5584
				{
ef5584
					$this->new_config['img_imagick'] = $this->search_imagemagick();
ef5584
				}
ef5584
ef5584
				// We strip eventually manual added convert program, we only want the patch
ef5584
				if ($this->new_config['img_imagick'])
ef5584
				{
ef5584
					// Change path separator
ef5584
					$this->new_config['img_imagick'] = str_replace('\\', '/', $this->new_config['img_imagick']);
ef5584
					$this->new_config['img_imagick'] = str_replace(array('convert', '.exe'), array('', ''), $this->new_config['img_imagick']);
ef5584
ef5584
					// Check for trailing slash
ef5584
					if (substr($this->new_config['img_imagick'], -1) !== '/')
ef5584
					{
ef5584
						$this->new_config['img_imagick'] .= '/';
ef5584
					}
ef5584
				}
ef5584
ef5584
				$supported_types = get_supported_image_types();
ef5584
ef5584
				// Check Thumbnail Support
ef5584
				if (!$this->new_config['img_imagick'] && (!isset($supported_types['format']) || !sizeof($supported_types['format'])))
ef5584
				{
ef5584
					$this->new_config['img_create_thumbnail'] = 0;
ef5584
				}
ef5584
ef5584
				$template->assign_vars(array(
ef5584
					'U_SEARCH_IMAGICK'		=> $this->u_action . '&action=imgmagick',
ef5584
					'S_THUMBNAIL_SUPPORT'	=> (!$this->new_config['img_imagick'] && (!isset($supported_types['format']) || !sizeof($supported_types['format']))) ? false : true)
ef5584
				);
ef5584
ef5584
				// Secure Download Options - Same procedure as with banning
ef5584
				$allow_deny = ($this->new_config['secure_allow_deny']) ? 'ALLOWED' : 'DISALLOWED';
ef5584
ef5584
				$sql = 'SELECT *
ef5584
					FROM ' . SITELIST_TABLE;
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				$defined_ips = '';
ef5584
				$ips = array();
ef5584
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$value = ($row['site_ip']) ? $row['site_ip'] : $row['site_hostname'];
ef5584
					if ($value)
ef5584
					{
ef5584
						$defined_ips .= '<option' . (($row['ip_exclude']) ? ' class="sep"' : '') . ' value="' . $row['site_id'] . '">' . $value . '</option>';
ef5584
						$ips[$row['site_id']] = $value;
ef5584
					}
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				$template->assign_vars(array(
ef5584
					'S_SECURE_DOWNLOADS'	=> $this->new_config['secure_downloads'],
ef5584
					'S_DEFINED_IPS'			=> ($defined_ips != '') ? true : false,
ef5584
					'S_WARNING'				=> (sizeof($error)) ? true : false,
ef5584
ef5584
					'WARNING_MSG'			=> implode('
', $error),
ef5584
					'DEFINED_IPS'			=> $defined_ips,
ef5584
ef5584
					'L_SECURE_TITLE'		=> $user->lang['DEFINE_' . $allow_deny . '_IPS'],
ef5584
					'L_IP_EXCLUDE'			=> $user->lang['EXCLUDE_FROM_' . $allow_deny . '_IP'],
ef5584
					'L_REMOVE_IPS'			=> $user->lang['REMOVE_' . $allow_deny . '_IPS'])
ef5584
				);
ef5584
ef5584
				// Output relevant options
ef5584
				foreach ($display_vars['vars'] as $config_key => $vars)
ef5584
				{
ef5584
					if (!is_array($vars) && strpos($config_key, 'legend') === false)
ef5584
					{
ef5584
						continue;
ef5584
					}
ef5584
ef5584
					if (strpos($config_key, 'legend') !== false)
ef5584
					{
ef5584
						$template->assign_block_vars('options', array(
ef5584
							'S_LEGEND'		=> true,
ef5584
							'LEGEND'		=> (isset($user->lang[$vars])) ? $user->lang[$vars] : $vars)
ef5584
						);
ef5584
ef5584
						continue;
ef5584
					}
ef5584
ef5584
					$type = explode(':', $vars['type']);
ef5584
ef5584
					$l_explain = '';
ef5584
					if ($vars['explain'] && isset($vars['lang_explain']))
ef5584
					{
ef5584
						$l_explain = (isset($user->lang[$vars['lang_explain']])) ? $user->lang[$vars['lang_explain']] : $vars['lang_explain'];
ef5584
					}
ef5584
					else if ($vars['explain'])
ef5584
					{
ef5584
						$l_explain = (isset($user->lang[$vars['lang'] . '_EXPLAIN'])) ? $user->lang[$vars['lang'] . '_EXPLAIN'] : '';
ef5584
					}
ef5584
ef5584
					$content = build_cfg_template($type, $config_key, $this->new_config, $config_key, $vars);
ef5584
					if (empty($content))
ef5584
					{
ef5584
						continue;
ef5584
					}
ef5584
ef5584
					$template->assign_block_vars('options', array(
ef5584
						'KEY'			=> $config_key,
ef5584
						'TITLE'			=> $user->lang[$vars['lang']],
ef5584
						'S_EXPLAIN'		=> $vars['explain'],
ef5584
						'TITLE_EXPLAIN'	=> $l_explain,
ef5584
						'CONTENT'		=> $content,
ef5584
						)
ef5584
					);
ef5584
ef5584
					unset($display_vars['vars'][$config_key]);
ef5584
				}
ef5584
ef5584
			break;
ef5584
ef5584
			case 'extensions':
ef5584
ef5584
				if ($submit || isset($_POST['add_extension_check']))
ef5584
				{
ef5584
					if ($submit)
ef5584
					{
ef5584
						// Change Extensions ?
ef5584
						$extension_change_list	= request_var('extension_change_list', array(0));
ef5584
						$group_select_list		= request_var('group_select', array(0));
ef5584
ef5584
						// Generate correct Change List
ef5584
						$extensions = array();
ef5584
ef5584
						for ($i = 0, $size = sizeof($extension_change_list); $i < $size; $i++)
ef5584
						{
ef5584
							$extensions[$extension_change_list[$i]]['group_id'] = $group_select_list[$i];
ef5584
						}
ef5584
ef5584
						$sql = 'SELECT *
ef5584
							FROM ' . EXTENSIONS_TABLE . '
ef5584
							ORDER BY extension_id';
ef5584
						$result = $db->sql_query($sql);
ef5584
ef5584
						while ($row = $db->sql_fetchrow($result))
ef5584
						{
ef5584
							if ($row['group_id'] != $extensions[$row['extension_id']]['group_id'])
ef5584
							{
ef5584
								$sql = 'UPDATE ' . EXTENSIONS_TABLE . '
ef5584
									SET group_id = ' . (int) $extensions[$row['extension_id']]['group_id'] . '
ef5584
									WHERE extension_id = ' . $row['extension_id'];
ef5584
								$db->sql_query($sql);
ef5584
ef5584
								add_log('admin', 'LOG_ATTACH_EXT_UPDATE', $row['extension']);
ef5584
							}
ef5584
						}
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						// Delete Extension?
ef5584
						$extension_id_list = request_var('extension_id_list', array(0));
ef5584
ef5584
						if (sizeof($extension_id_list))
ef5584
						{
ef5584
							$sql = 'SELECT extension
ef5584
								FROM ' . EXTENSIONS_TABLE . '
ef5584
								WHERE ' . $db->sql_in_set('extension_id', $extension_id_list);
ef5584
							$result = $db->sql_query($sql);
ef5584
ef5584
							$extension_list = '';
ef5584
							while ($row = $db->sql_fetchrow($result))
ef5584
							{
ef5584
								$extension_list .= ($extension_list == '') ? $row['extension'] : ', ' . $row['extension'];
ef5584
							}
ef5584
							$db->sql_freeresult($result);
ef5584
ef5584
							$sql = 'DELETE
ef5584
								FROM ' . EXTENSIONS_TABLE . '
ef5584
								WHERE ' . $db->sql_in_set('extension_id', $extension_id_list);
ef5584
							$db->sql_query($sql);
ef5584
ef5584
							add_log('admin', 'LOG_ATTACH_EXT_DEL', $extension_list);
ef5584
						}
ef5584
					}
ef5584
ef5584
					// Add Extension?
ef5584
					$add_extension			= strtolower(request_var('add_extension', ''));
ef5584
					$add_extension_group	= request_var('add_group_select', 0);
ef5584
					$add					= (isset($_POST['add_extension_check'])) ? true : false;
ef5584
ef5584
					if ($add_extension && $add)
ef5584
					{
ef5584
						if (!sizeof($error))
ef5584
						{
ef5584
							$sql = 'SELECT extension_id
ef5584
								FROM ' . EXTENSIONS_TABLE . "
ef5584
								WHERE extension = '" . $db->sql_escape($add_extension) . "'";
ef5584
							$result = $db->sql_query($sql);
ef5584
ef5584
							if ($row = $db->sql_fetchrow($result))
ef5584
							{
ef5584
								$error[] = sprintf($user->lang['EXTENSION_EXIST'], $add_extension);
ef5584
							}
ef5584
							$db->sql_freeresult($result);
ef5584
ef5584
							if (!sizeof($error))
ef5584
							{
ef5584
								$sql_ary = array(
ef5584
									'group_id'	=>	$add_extension_group,
ef5584
									'extension'	=>	$add_extension
ef5584
								);
ef5584
ef5584
								$db->sql_query('INSERT INTO ' . EXTENSIONS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary));
ef5584
								add_log('admin', 'LOG_ATTACH_EXT_ADD', $add_extension);
ef5584
							}
ef5584
						}
ef5584
					}
ef5584
ef5584
					if (!sizeof($error))
ef5584
					{
ef5584
						$notify[] = $user->lang['EXTENSIONS_UPDATED'];
ef5584
					}
ef5584
ef5584
					$cache->destroy('_extensions');
ef5584
				}
ef5584
ef5584
				$template->assign_vars(array(
ef5584
					'S_EXTENSIONS'			=> true,
ef5584
					'ADD_EXTENSION'			=> (isset($add_extension)) ? $add_extension : '',
ef5584
					'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'))
ef5584
				);
ef5584
ef5584
				$sql = 'SELECT *
ef5584
					FROM ' . EXTENSIONS_TABLE . '
ef5584
					ORDER BY group_id, extension';
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				if ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$old_group_id = $row['group_id'];
ef5584
					do
ef5584
					{
ef5584
						$s_spacer = false;
ef5584
ef5584
						$current_group_id = $row['group_id'];
ef5584
						if ($old_group_id != $current_group_id)
ef5584
						{
ef5584
							$s_spacer = true;
ef5584
							$old_group_id = $current_group_id;
ef5584
						}
ef5584
ef5584
						$template->assign_block_vars('extensions', array(
ef5584
							'S_SPACER'		=> $s_spacer,
ef5584
							'EXTENSION_ID'	=> $row['extension_id'],
ef5584
							'EXTENSION'		=> $row['extension'],
ef5584
							'GROUP_OPTIONS'	=> $this->group_select('group_select[]', $row['group_id']))
ef5584
						);
ef5584
					}
ef5584
					while ($row = $db->sql_fetchrow($result));
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
			break;
ef5584
ef5584
			case 'ext_groups':
ef5584
ef5584
				$template->assign_var('S_EXTENSION_GROUPS', true);
ef5584
ef5584
				if ($submit)
ef5584
				{
ef5584
					$action = request_var('action', '');
ef5584
					$group_id = request_var('g', 0);
ef5584
ef5584
					if ($action != 'add' && $action != 'edit')
ef5584
					{
ef5584
						trigger_error('NO_MODE', E_USER_ERROR);
ef5584
					}
ef5584
ef5584
					if (!$group_id && $action == 'edit')
ef5584
					{
ef5584
						trigger_error($user->lang['NO_EXT_GROUP_SPECIFIED'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
					}
ef5584
ef5584
					if ($group_id)
ef5584
					{
ef5584
						$sql = 'SELECT *
ef5584
							FROM ' . EXTENSION_GROUPS_TABLE . "
ef5584
							WHERE group_id = $group_id";
ef5584
						$result = $db->sql_query($sql);
ef5584
						$ext_row = $db->sql_fetchrow($result);
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						if (!$ext_row)
ef5584
						{
ef5584
							trigger_error($user->lang['NO_EXT_GROUP_SPECIFIED'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
						}
ef5584
					}
ef5584
					else
ef5584
					{
ef5584
						$ext_row = array();
ef5584
					}
ef5584
ef5584
					$group_name = utf8_normalize_nfc(request_var('group_name', '', true));
ef5584
					$new_group_name = ($action == 'add') ? $group_name : (($ext_row['group_name'] != $group_name) ? $group_name : '');
ef5584
ef5584
					if (!$group_name)
ef5584
					{
ef5584
						$error[] = $user->lang['NO_EXT_GROUP_NAME'];
ef5584
					}
ef5584
ef5584
					// Check New Group Name
ef5584
					if ($new_group_name)
ef5584
					{
ef5584
						$sql = 'SELECT group_id
ef5584
							FROM ' . EXTENSION_GROUPS_TABLE . "
ef5584
							WHERE LOWER(group_name) = '" . $db->sql_escape(utf8_strtolower($new_group_name)) . "'";
ef5584
						$result = $db->sql_query($sql);
ef5584
ef5584
						if ($db->sql_fetchrow($result))
ef5584
						{
ef5584
							$error[] = sprintf($user->lang['EXTENSION_GROUP_EXIST'], $new_group_name);
ef5584
						}
ef5584
						$db->sql_freeresult($result);
ef5584
					}
ef5584
ef5584
					if (!sizeof($error))
ef5584
					{
ef5584
						// Ok, build the update/insert array
ef5584
						$upload_icon	= request_var('upload_icon', 'no_image');
ef5584
						$size_select	= request_var('size_select', 'b');
ef5584
						$forum_select	= request_var('forum_select', false);
ef5584
						$allowed_forums	= request_var('allowed_forums', array(0));
ef5584
						$allow_in_pm	= (isset($_POST['allow_in_pm'])) ? true : false;
ef5584
						$max_filesize	= request_var('max_filesize', 0);
ef5584
						$max_filesize	= ($size_select == 'kb') ? round($max_filesize * 1024) : (($size_select == 'mb') ? round($max_filesize * 1048576) : $max_filesize);
ef5584
						$allow_group	= (isset($_POST['allow_group'])) ? true : false;
ef5584
ef5584
						if ($max_filesize == $config['max_filesize'])
ef5584
						{
ef5584
							$max_filesize = 0;
ef5584
						}
ef5584
ef5584
						if (!sizeof($allowed_forums))
ef5584
						{
ef5584
							$forum_select = false;
ef5584
						}
ef5584
ef5584
						$group_ary = array(
ef5584
							'group_name'	=> $group_name,
ef5584
							'cat_id'		=> request_var('special_category', ATTACHMENT_CATEGORY_NONE),
ef5584
							'allow_group'	=> ($allow_group) ? 1 : 0,
ef5584
							'upload_icon'	=> ($upload_icon == 'no_image') ? '' : $upload_icon,
ef5584
							'max_filesize'	=> $max_filesize,
ef5584
							'allowed_forums'=> ($forum_select) ? serialize($allowed_forums) : '',
ef5584
							'allow_in_pm'	=> ($allow_in_pm) ? 1 : 0,
ef5584
						);
ef5584
ef5584
						if ($action == 'add')
ef5584
						{
ef5584
							$group_ary['download_mode'] = INLINE_LINK;
ef5584
						}
ef5584
ef5584
						$sql = ($action == 'add') ? 'INSERT INTO ' . EXTENSION_GROUPS_TABLE . ' ' : 'UPDATE ' . EXTENSION_GROUPS_TABLE . ' SET ';
ef5584
						$sql .= $db->sql_build_array((($action == 'add') ? 'INSERT' : 'UPDATE'), $group_ary);
ef5584
						$sql .= ($action == 'edit') ? " WHERE group_id = $group_id" : '';
ef5584
ef5584
						$db->sql_query($sql);
ef5584
ef5584
						if ($action == 'add')
ef5584
						{
ef5584
							$group_id = $db->sql_nextid();
ef5584
						}
ef5584
ef5584
						add_log('admin', 'LOG_ATTACH_EXTGROUP_' . strtoupper($action), $group_name);
ef5584
					}
ef5584
ef5584
					$extension_list = request_var('extensions', array(0));
ef5584
ef5584
					if ($action == 'edit' && sizeof($extension_list))
ef5584
					{
ef5584
						$sql = 'UPDATE ' . EXTENSIONS_TABLE . "
ef5584
							SET group_id = 0
ef5584
							WHERE group_id = $group_id";
ef5584
						$db->sql_query($sql);
ef5584
					}
ef5584
ef5584
					if (sizeof($extension_list))
ef5584
					{
ef5584
						$sql = 'UPDATE ' . EXTENSIONS_TABLE . "
ef5584
							SET group_id = $group_id
ef5584
							WHERE " . $db->sql_in_set('extension_id', $extension_list);
ef5584
						$db->sql_query($sql);
ef5584
					}
ef5584
ef5584
					$cache->destroy('_extensions');
ef5584
ef5584
					if (!sizeof($error))
ef5584
					{
ef5584
						$notify[] = $user->lang['SUCCESS_EXTENSION_GROUP_' . strtoupper($action)];
ef5584
					}
ef5584
				}
ef5584
ef5584
				$cat_lang = array(
ef5584
					ATTACHMENT_CATEGORY_NONE		=> $user->lang['NO_FILE_CAT'],
ef5584
					ATTACHMENT_CATEGORY_IMAGE		=> $user->lang['CAT_IMAGES'],
ef5584
					ATTACHMENT_CATEGORY_WM			=> $user->lang['CAT_WM_FILES'],
ef5584
					ATTACHMENT_CATEGORY_RM			=> $user->lang['CAT_RM_FILES'],
ef5584
					ATTACHMENT_CATEGORY_FLASH		=> $user->lang['CAT_FLASH_FILES'],
ef5584
					ATTACHMENT_CATEGORY_QUICKTIME	=> $user->lang['CAT_QUICKTIME_FILES'],
ef5584
				);
ef5584
ef5584
				$group_id = request_var('g', 0);
ef5584
				$action = (isset($_POST['add'])) ? 'add' : $action;
ef5584
ef5584
				switch ($action)
ef5584
				{
ef5584
					case 'delete':
ef5584
ef5584
						if (confirm_box(true))
ef5584
						{
ef5584
							$sql = 'SELECT group_name
ef5584
								FROM ' . EXTENSION_GROUPS_TABLE . "
ef5584
								WHERE group_id = $group_id";
ef5584
							$result = $db->sql_query($sql);
ef5584
							$group_name = (string) $db->sql_fetchfield('group_name');
ef5584
							$db->sql_freeresult($result);
ef5584
ef5584
							$sql = 'DELETE
ef5584
								FROM ' . EXTENSION_GROUPS_TABLE . "
ef5584
								WHERE group_id = $group_id";
ef5584
							$db->sql_query($sql);
ef5584
ef5584
							// Set corresponding Extensions to a pending Group
ef5584
							$sql = 'UPDATE ' . EXTENSIONS_TABLE . "
ef5584
								SET group_id = 0
ef5584
								WHERE group_id = $group_id";
ef5584
							$db->sql_query($sql);
ef5584
ef5584
							add_log('admin', 'LOG_ATTACH_EXTGROUP_DEL', $group_name);
ef5584
ef5584
							$cache->destroy('_extensions');
ef5584
ef5584
							trigger_error($user->lang['EXTENSION_GROUP_DELETED'] . adm_back_link($this->u_action));
ef5584
						}
ef5584
						else
ef5584
						{
ef5584
							confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array(
ef5584
								'i'			=> $id,
ef5584
								'mode'		=> $mode,
ef5584
								'group_id'	=> $group_id,
ef5584
								'action'	=> 'delete',
ef5584
							)));
ef5584
						}
ef5584
ef5584
					break;
ef5584
ef5584
					case 'edit':
ef5584
ef5584
						if (!$group_id)
ef5584
						{
ef5584
							trigger_error($user->lang['NO_EXT_GROUP_SPECIFIED'] . adm_back_link($this->u_action), E_USER_WARNING);
ef5584
						}
ef5584
ef5584
						$sql = 'SELECT *
ef5584
							FROM ' . EXTENSION_GROUPS_TABLE . "
ef5584
							WHERE group_id = $group_id";
ef5584
						$result = $db->sql_query($sql);
ef5584
						$ext_group_row = $db->sql_fetchrow($result);
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						$forum_ids = (!$ext_group_row['allowed_forums']) ? array() : unserialize(trim($ext_group_row['allowed_forums']));
ef5584
ef5584
					// no break;
ef5584
ef5584
					case 'add':
ef5584
ef5584
						if ($action == 'add')
ef5584
						{
ef5584
							$ext_group_row = array(
ef5584
								'group_name'	=> utf8_normalize_nfc(request_var('group_name', '', true)),
ef5584
								'cat_id'		=> 0,
ef5584
								'allow_group'	=> 1,
ef5584
								'allow_in_pm'	=> 1,
ef5584
								'upload_icon'	=> '',
ef5584
								'max_filesize'	=> 0,
ef5584
							);
ef5584
ef5584
							$forum_ids = array();
ef5584
						}
ef5584
ef5584
						$extensions = array();
ef5584
ef5584
						$sql = 'SELECT *
ef5584
							FROM ' . EXTENSIONS_TABLE . "
ef5584
							WHERE group_id = $group_id
ef5584
								OR group_id = 0
ef5584
							ORDER BY extension";
ef5584
						$result = $db->sql_query($sql);
ef5584
						$extensions = $db->sql_fetchrowset($result);
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						if ($ext_group_row['max_filesize'] == 0)
ef5584
						{
ef5584
							$ext_group_row['max_filesize'] = (int) $config['max_filesize'];
ef5584
						}
ef5584
ef5584
						$size_format = ($ext_group_row['max_filesize'] >= 1048576) ? 'mb' : (($ext_group_row['max_filesize'] >= 1024) ? 'kb' : 'b');
ef5584
						$ext_group_row['max_filesize'] = get_formatted_filesize($ext_group_row['max_filesize'], false);
ef5584
ef5584
						$img_path = $config['upload_icons_path'];
ef5584
ef5584
						$filename_list = '';
ef5584
						$no_image_select = false;
ef5584
ef5584
						$imglist = filelist($phpbb_root_path . $img_path);
ef5584
ef5584
						if (sizeof($imglist))
ef5584
						{
ef5584
							$imglist = array_values($imglist);
ef5584
							$imglist = $imglist[0];
ef5584
ef5584
							foreach ($imglist as $key => $img)
ef5584
							{
ef5584
								if (!$ext_group_row['upload_icon'])
ef5584
								{
ef5584
									$no_image_select = true;
ef5584
									$selected = '';
ef5584
								}
ef5584
								else
ef5584
								{
ef5584
									$selected = ($ext_group_row['upload_icon'] == $img) ? ' selected="selected"' : '';
ef5584
								}
ef5584
ef5584
								if (strlen($img) > 255)
ef5584
								{
ef5584
									continue;
ef5584
								}
ef5584
ef5584
								$filename_list .= '<option value="' . htmlspecialchars($img) . '"' . $selected . '>' . htmlspecialchars($img) . '</option>';
ef5584
							}
ef5584
						}
ef5584
ef5584
						$i = 0;
ef5584
						$assigned_extensions = '';
ef5584
						foreach ($extensions as $num => $row)
ef5584
						{
ef5584
							if ($row['group_id'] == $group_id && $group_id)
ef5584
							{
ef5584
								$assigned_extensions .= ($i) ? ', ' . $row['extension'] : $row['extension'];
ef5584
								$i++;
ef5584
							}
ef5584
						}
ef5584
ef5584
						$s_extension_options = '';
ef5584
						foreach ($extensions as $row)
ef5584
						{
ef5584
							$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>';
ef5584
						}
ef5584
ef5584
						$template->assign_vars(array(
ef5584
							'PHPBB_ROOT_PATH'		=> $phpbb_root_path,
ef5584
							'IMG_PATH'				=> $img_path,
ef5584
							'ACTION'				=> $action,
ef5584
							'GROUP_ID'				=> $group_id,
ef5584
							'GROUP_NAME'			=> $ext_group_row['group_name'],
ef5584
							'ALLOW_GROUP'			=> $ext_group_row['allow_group'],
ef5584
							'ALLOW_IN_PM'			=> $ext_group_row['allow_in_pm'],
ef5584
							'UPLOAD_ICON_SRC'		=> $phpbb_root_path . $img_path . '/' . $ext_group_row['upload_icon'],
ef5584
							'EXTGROUP_FILESIZE'		=> $ext_group_row['max_filesize'],
ef5584
							'ASSIGNED_EXTENSIONS'	=> $assigned_extensions,
ef5584
ef5584
							'S_CATEGORY_SELECT'			=> $this->category_select('special_category', $group_id, 'category'),
ef5584
							'S_EXT_GROUP_SIZE_OPTIONS'	=> size_select_options($size_format),
ef5584
							'S_EXTENSION_OPTIONS'		=> $s_extension_options,
ef5584
							'S_FILENAME_LIST'			=> $filename_list,
ef5584
							'S_EDIT_GROUP'				=> true,
ef5584
							'S_NO_IMAGE'				=> $no_image_select,
ef5584
							'S_FORUM_IDS'				=> (sizeof($forum_ids)) ? true : false,
ef5584
ef5584
							'U_EXTENSIONS'		=> append_sid("{$phpbb_admin_path}index.$phpEx", "i=$id&mode=extensions"),
ef5584
							'U_BACK'			=> $this->u_action,
ef5584
ef5584
							'L_LEGEND'			=> $user->lang[strtoupper($action) . '_EXTENSION_GROUP'])
ef5584
						);
ef5584
ef5584
						$s_forum_id_options = '';
ef5584
ef5584
						/** @todo use in-built function **/
ef5584
ef5584
						$sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
ef5584
							FROM ' . FORUMS_TABLE . '
ef5584
							ORDER BY left_id ASC';
ef5584
						$result = $db->sql_query($sql, 600);
ef5584
ef5584
						$right = $cat_right = $padding_inc = 0;
ef5584
						$padding = $forum_list = $holding = '';
ef5584
						$padding_store = array('0' => '');
ef5584
ef5584
						while ($row = $db->sql_fetchrow($result))
ef5584
						{
ef5584
							if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
ef5584
							{
ef5584
								// Non-postable forum with no subforums, don't display
ef5584
								continue;
ef5584
							}
ef5584
ef5584
							if (!$auth->acl_get('f_list', $row['forum_id']))
ef5584
							{
ef5584
								// if the user does not have permissions to list this forum skip
ef5584
								continue;
ef5584
							}
ef5584
ef5584
							if ($row['left_id'] < $right)
ef5584
							{
ef5584
								$padding .= '   ';
ef5584
								$padding_store[$row['parent_id']] = $padding;
ef5584
							}
ef5584
							else if ($row['left_id'] > $right + 1)
ef5584
							{
ef5584
								$padding = empty($padding_store[$row['parent_id']]) ? '' : $padding_store[$row['parent_id']];
ef5584
							}
ef5584
ef5584
							$right = $row['right_id'];
ef5584
ef5584
							$selected = (in_array($row['forum_id'], $forum_ids)) ? ' selected="selected"' : '';
ef5584
ef5584
							if ($row['left_id'] > $cat_right)
ef5584
							{
ef5584
								// make sure we don't forget anything
ef5584
								$s_forum_id_options .= $holding;
ef5584
								$holding = '';
ef5584
							}
ef5584
ef5584
							if ($row['right_id'] - $row['left_id'] > 1)
ef5584
							{
ef5584
								$cat_right = max($cat_right, $row['right_id']);
ef5584
ef5584
								$holding .= '<option value="' . $row['forum_id'] . '"' . (($row['forum_type'] == FORUM_POST) ? ' class="sep"' : '') . $selected . '>' . $padding . $row['forum_name'] . '</option>';
ef5584
							}
ef5584
							else
ef5584
							{
ef5584
								$s_forum_id_options .= $holding . '<option value="' . $row['forum_id'] . '"' . (($row['forum_type'] == FORUM_POST) ? ' class="sep"' : '') . $selected . '>' . $padding . $row['forum_name'] . '</option>';
ef5584
								$holding = '';
ef5584
							}
ef5584
						}
ef5584
ef5584
						if ($holding)
ef5584
						{
ef5584
							$s_forum_id_options .= $holding;
ef5584
						}
ef5584
ef5584
						$db->sql_freeresult($result);
ef5584
						unset($padding_store);
ef5584
ef5584
						$template->assign_vars(array(
ef5584
							'S_FORUM_ID_OPTIONS'	=> $s_forum_id_options)
ef5584
						);
ef5584
ef5584
					break;
ef5584
				}
ef5584
ef5584
				$sql = 'SELECT *
ef5584
					FROM ' . EXTENSION_GROUPS_TABLE . '
ef5584
					ORDER BY allow_group DESC, allow_in_pm DESC, group_name';
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				$old_allow_group = $old_allow_pm = 1;
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$s_add_spacer = ($old_allow_group != $row['allow_group'] || $old_allow_pm != $row['allow_in_pm']) ? true : false;
ef5584
ef5584
					$template->assign_block_vars('groups', array(
ef5584
						'S_ADD_SPACER'		=> $s_add_spacer,
ef5584
						'S_ALLOWED_IN_PM'	=> ($row['allow_in_pm']) ? true : false,
ef5584
						'S_GROUP_ALLOWED'	=> ($row['allow_group']) ? true : false,
ef5584
ef5584
						'U_EDIT'		=> $this->u_action . "&action=edit&g={$row['group_id']}",
ef5584
						'U_DELETE'		=> $this->u_action . "&action=delete&g={$row['group_id']}",
ef5584
ef5584
						'GROUP_NAME'	=> $row['group_name'],
ef5584
						'CATEGORY'		=> $cat_lang[$row['cat_id']],
ef5584
						)
ef5584
					);
ef5584
ef5584
					$old_allow_group = $row['allow_group'];
ef5584
					$old_allow_pm = $row['allow_in_pm'];
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
			break;
ef5584
ef5584
			case 'orphan':
ef5584
ef5584
				if ($submit)
ef5584
				{
ef5584
					$delete_files = (isset($_POST['delete'])) ? array_keys(request_var('delete', array('' => 0))) : array();
ef5584
					$add_files = (isset($_POST['add'])) ? array_keys(request_var('add', array('' => 0))) : array();
ef5584
					$post_ids = request_var('post_id', array('' => 0));
ef5584
ef5584
					if (sizeof($delete_files))
ef5584
					{
ef5584
						$sql = 'SELECT *
ef5584
							FROM ' . ATTACHMENTS_TABLE . '
ef5584
							WHERE ' . $db->sql_in_set('attach_id', $delete_files) . '
ef5584
								AND is_orphan = 1';
ef5584
						$result = $db->sql_query($sql);
ef5584
ef5584
						$delete_files = array();
ef5584
						while ($row = $db->sql_fetchrow($result))
ef5584
						{
ef5584
							phpbb_unlink($row['physical_filename'], 'file');
ef5584
ef5584
							if ($row['thumbnail'])
ef5584
							{
ef5584
								phpbb_unlink($row['physical_filename'], 'thumbnail');
ef5584
							}
ef5584
ef5584
							$delete_files[$row['attach_id']] = $row['real_filename'];
ef5584
						}
ef5584
						$db->sql_freeresult($result);
ef5584
					}
ef5584
ef5584
					if (sizeof($delete_files))
ef5584
					{
ef5584
						$sql = 'DELETE FROM ' . ATTACHMENTS_TABLE . '
ef5584
							WHERE ' . $db->sql_in_set('attach_id', array_keys($delete_files));
ef5584
						$db->sql_query($sql);
ef5584
ef5584
						add_log('admin', 'LOG_ATTACH_ORPHAN_DEL', implode(', ', $delete_files));
ef5584
						$notify[] = sprintf($user->lang['LOG_ATTACH_ORPHAN_DEL'], implode(', ', $delete_files));
ef5584
					}
ef5584
ef5584
					$upload_list = array();
ef5584
					foreach ($add_files as $attach_id)
ef5584
					{
ef5584
						if (!isset($delete_files[$attach_id]) && !empty($post_ids[$attach_id]))
ef5584
						{
ef5584
							$upload_list[$attach_id] = $post_ids[$attach_id];
ef5584
						}
ef5584
					}
ef5584
					unset($add_files);
ef5584
ef5584
					if (sizeof($upload_list))
ef5584
					{
ef5584
						$template->assign_var('S_UPLOADING_FILES', true);
ef5584
ef5584
						$sql = 'SELECT forum_id, forum_name
ef5584
							FROM ' . FORUMS_TABLE;
ef5584
						$result = $db->sql_query($sql);
ef5584
ef5584
						$forum_names = array();
ef5584
						while ($row = $db->sql_fetchrow($result))
ef5584
						{
ef5584
							$forum_names[$row['forum_id']] = $row['forum_name'];
ef5584
						}
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						$sql = 'SELECT forum_id, topic_id, post_id, poster_id
ef5584
							FROM ' . POSTS_TABLE . '
ef5584
							WHERE ' . $db->sql_in_set('post_id', $upload_list);
ef5584
						$result = $db->sql_query($sql);
ef5584
ef5584
						$post_info = array();
ef5584
						while ($row = $db->sql_fetchrow($result))
ef5584
						{
ef5584
							$post_info[$row['post_id']] = $row;
ef5584
						}
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						// Select those attachments we want to change...
ef5584
						$sql = 'SELECT *
ef5584
							FROM ' . ATTACHMENTS_TABLE . '
ef5584
							WHERE ' . $db->sql_in_set('attach_id', array_keys($upload_list)) . '
ef5584
								AND is_orphan = 1';
ef5584
						$result = $db->sql_query($sql);
ef5584
ef5584
						$files_added = $space_taken = 0;
ef5584
						while ($row = $db->sql_fetchrow($result))
ef5584
						{
ef5584
							$post_row = $post_info[$upload_list[$row['attach_id']]];
ef5584
ef5584
							$template->assign_block_vars('upload', array(
ef5584
								'FILE_INFO'		=> sprintf($user->lang['UPLOADING_FILE_TO'], $row['real_filename'], $post_row['post_id']),
ef5584
								'S_DENIED'		=> (!$auth->acl_get('f_attach', $post_row['forum_id'])) ? true : false,
ef5584
								'L_DENIED'		=> (!$auth->acl_get('f_attach', $post_row['forum_id'])) ? sprintf($user->lang['UPLOAD_DENIED_FORUM'], $forum_names[$row['forum_id']]) : '')
ef5584
							);
ef5584
ef5584
							if (!$auth->acl_get('f_attach', $post_row['forum_id']))
ef5584
							{
ef5584
								continue;
ef5584
							}
ef5584
ef5584
							// Adjust attachment entry
ef5584
							$sql_ary = array(
ef5584
								'in_message'	=> 0,
ef5584
								'is_orphan'		=> 0,
ef5584
								'poster_id'		=> $post_row['poster_id'],
ef5584
								'post_msg_id'	=> $post_row['post_id'],
ef5584
								'topic_id'		=> $post_row['topic_id'],
ef5584
							);
ef5584
ef5584
							$sql = 'UPDATE ' . ATTACHMENTS_TABLE . '
ef5584
								SET ' . $db->sql_build_array('UPDATE', $sql_ary) . '
ef5584
								WHERE attach_id = ' . $row['attach_id'];
ef5584
							$db->sql_query($sql);
ef5584
ef5584
							$sql = 'UPDATE ' . POSTS_TABLE . '
ef5584
								SET post_attachment = 1
ef5584
								WHERE post_id = ' . $post_row['post_id'];
ef5584
							$db->sql_query($sql);
ef5584
ef5584
							$sql = 'UPDATE ' . TOPICS_TABLE . '
ef5584
								SET topic_attachment = 1
ef5584
								WHERE topic_id = ' . $post_row['topic_id'];
ef5584
							$db->sql_query($sql);
ef5584
ef5584
							$space_taken += $row['filesize'];
ef5584
							$files_added++;
ef5584
ef5584
							add_log('admin', 'LOG_ATTACH_FILEUPLOAD', $post_row['post_id'], $row['real_filename']);
ef5584
						}
ef5584
						$db->sql_freeresult($result);
ef5584
ef5584
						if ($files_added)
ef5584
						{
ef5584
							set_config('upload_dir_size', $config['upload_dir_size'] + $space_taken, true);
ef5584
							set_config('num_files', $config['num_files'] + $files_added, true);
ef5584
						}
ef5584
					}
ef5584
				}
ef5584
ef5584
				$template->assign_vars(array(
ef5584
					'S_ORPHAN'		=> true)
ef5584
				);
ef5584
ef5584
				// Just get the files with is_orphan set and older than 3 hours
ef5584
				$sql = 'SELECT *
ef5584
					FROM ' . ATTACHMENTS_TABLE . '
ef5584
					WHERE is_orphan = 1
ef5584
						AND filetime < ' . (time() - 3*60*60) . '
ef5584
					ORDER BY filetime DESC';
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$template->assign_block_vars('orphan', array(
ef5584
						'FILESIZE'			=> get_formatted_filesize($row['filesize']),
ef5584
						'FILETIME'			=> $user->format_date($row['filetime']),
ef5584
						'REAL_FILENAME'		=> basename($row['real_filename']),
ef5584
						'PHYSICAL_FILENAME'	=> basename($row['physical_filename']),
ef5584
						'ATTACH_ID'			=> $row['attach_id'],
ef5584
						'POST_IDS'			=> (!empty($post_ids[$row['attach_id']])) ? $post_ids[$row['attach_id']] : '',
ef5584
						'U_FILE'			=> append_sid($phpbb_root_path . 'download/file.' . $phpEx, 'mode=view&id=' . $row['attach_id']))
ef5584
					);
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
			break;
ef5584
		}
ef5584
ef5584
		if (sizeof($error))
ef5584
		{
ef5584
			$template->assign_vars(array(
ef5584
				'S_WARNING'		=> true,
ef5584
				'WARNING_MSG'	=> implode('
', $error))
ef5584
			);
ef5584
		}
ef5584
ef5584
		if (sizeof($notify))
ef5584
		{
ef5584
			$template->assign_vars(array(
ef5584
				'S_NOTIFY'		=> true,
ef5584
				'NOTIFY_MSG'	=> implode('
', $notify))
ef5584
			);
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Build Select for category items
ef5584
	*/
ef5584
	function category_select($select_name, $group_id = false, $key = '')
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		$types = array(
ef5584
			ATTACHMENT_CATEGORY_NONE		=> $user->lang['NO_FILE_CAT'],
ef5584
			ATTACHMENT_CATEGORY_IMAGE		=> $user->lang['CAT_IMAGES'],
ef5584
			ATTACHMENT_CATEGORY_WM			=> $user->lang['CAT_WM_FILES'],
ef5584
			ATTACHMENT_CATEGORY_RM			=> $user->lang['CAT_RM_FILES'],
ef5584
			ATTACHMENT_CATEGORY_FLASH		=> $user->lang['CAT_FLASH_FILES'],
ef5584
			ATTACHMENT_CATEGORY_QUICKTIME	=> $user->lang['CAT_QUICKTIME_FILES'],
ef5584
		);
ef5584
ef5584
		if ($group_id)
ef5584
		{
ef5584
			$sql = 'SELECT cat_id
ef5584
				FROM ' . EXTENSION_GROUPS_TABLE . '
ef5584
				WHERE group_id = ' . (int) $group_id;
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			$cat_type = (!($row = $db->sql_fetchrow($result))) ? ATTACHMENT_CATEGORY_NONE : $row['cat_id'];
ef5584
ef5584
			$db->sql_freeresult($result);
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$cat_type = ATTACHMENT_CATEGORY_NONE;
ef5584
		}
ef5584
ef5584
		$group_select = '<select name="' . $select_name . '"' . (($key) ? ' id="' . $key . '"' : '') . '>';
ef5584
ef5584
		foreach ($types as $type => $mode)
ef5584
		{
ef5584
			$selected = ($type == $cat_type) ? ' selected="selected"' : '';
ef5584
			$group_select .= '<option value="' . $type . '"' . $selected . '>' . $mode . '</option>';
ef5584
		}
ef5584
ef5584
		$group_select .= '</select>';
ef5584
ef5584
		return $group_select;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Extension group select
ef5584
	*/
ef5584
	function group_select($select_name, $default_group = false, $key = '')
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		$group_select = '<select name="' . $select_name . '"' . (($key) ? ' id="' . $key . '"' : '') . '>';
ef5584
ef5584
		$sql = 'SELECT group_id, group_name
ef5584
			FROM ' . EXTENSION_GROUPS_TABLE . '
ef5584
			ORDER BY group_name';
ef5584
		$result = $db->sql_query($sql);
ef5584
ef5584
		$group_name = array();
ef5584
		while ($row = $db->sql_fetchrow($result))
ef5584
		{
ef5584
			$group_name[] = $row;
ef5584
		}
ef5584
		$db->sql_freeresult($result);
ef5584
ef5584
		$row['group_id'] = 0;
ef5584
		$row['group_name'] = $user->lang['NOT_ASSIGNED'];
ef5584
		$group_name[] = $row;
ef5584
ef5584
		for ($i = 0; $i < sizeof($group_name); $i++)
ef5584
		{
ef5584
			if ($default_group === false)
ef5584
			{
ef5584
				$selected = ($i == 0) ? ' selected="selected"' : '';
ef5584
			}
ef5584
			else
ef5584
			{
ef5584
				$selected = ($group_name[$i]['group_id'] == $default_group) ? ' selected="selected"' : '';
ef5584
			}
ef5584
ef5584
			$group_select .= '<option value="' . $group_name[$i]['group_id'] . '"' . $selected . '>' . $group_name[$i]['group_name'] . '</option>';
ef5584
		}
ef5584
ef5584
		$group_select .= '</select>';
ef5584
ef5584
		return $group_select;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Search Imagick
ef5584
	*/
ef5584
	function search_imagemagick()
ef5584
	{
ef5584
		$imagick = '';
ef5584
ef5584
		$exe = ((defined('PHP_OS')) && (preg_match('#^win#i', PHP_OS))) ? '.exe' : '';
ef5584
ef5584
		$magic_home = getenv('MAGICK_HOME');
ef5584
ef5584
		if (empty($magic_home))
ef5584
		{
ef5584
			$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/');
ef5584
			$path_locations = str_replace('\\', '/', (explode(($exe) ? ';' : ':', getenv('PATH'))));
ef5584
ef5584
			$locations = array_merge($path_locations, $locations);
ef5584
ef5584
			foreach ($locations as $location)
ef5584
			{
ef5584
				// The path might not end properly, fudge it
ef5584
				if (substr($location, -1) !== '/')
ef5584
				{
ef5584
					$location .= '/';
ef5584
				}
ef5584
ef5584
				if (@file_exists($location) && @is_readable($location . 'mogrify' . $exe) && @filesize($location . 'mogrify' . $exe) > 3000)
ef5584
				{
ef5584
					$imagick = str_replace('\\', '/', $location);
ef5584
					continue;
ef5584
				}
ef5584
			}
ef5584
		}
ef5584
		else
ef5584
		{
ef5584
			$imagick = str_replace('\\', '/', $magic_home);
ef5584
		}
ef5584
ef5584
		return $imagick;
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Test Settings
ef5584
	*/
ef5584
	function test_upload(&$error, $upload_dir, $create_directory = false)
ef5584
	{
ef5584
		global $user, $phpbb_root_path;
ef5584
ef5584
		// Does the target directory exist, is it a directory and writable.
ef5584
		if ($create_directory)
ef5584
		{
ef5584
			if (!file_exists($phpbb_root_path . $upload_dir))
ef5584
			{
ef5584
				@mkdir($phpbb_root_path . $upload_dir, 0777);
ef5584
				phpbb_chmod($phpbb_root_path . $upload_dir, CHMOD_READ | CHMOD_WRITE);
ef5584
			}
ef5584
		}
ef5584
ef5584
		if (!file_exists($phpbb_root_path . $upload_dir))
ef5584
		{
ef5584
			$error[] = sprintf($user->lang['NO_UPLOAD_DIR'], $upload_dir);
ef5584
			return;
ef5584
		}
ef5584
ef5584
		if (!is_dir($phpbb_root_path . $upload_dir))
ef5584
		{
ef5584
			$error[] = sprintf($user->lang['UPLOAD_NOT_DIR'], $upload_dir);
ef5584
			return;
ef5584
		}
ef5584
ef5584
		if (!is_writable($phpbb_root_path . $upload_dir))
ef5584
		{
ef5584
			$error[] = sprintf($user->lang['NO_WRITE_UPLOAD'], $upload_dir);
ef5584
			return;
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Perform operations on sites for external linking
ef5584
	*/
ef5584
	function perform_site_list()
ef5584
	{
ef5584
		global $db, $user;
ef5584
ef5584
		if (isset($_REQUEST['securesubmit']))
ef5584
		{
ef5584
			// Grab the list of entries
ef5584
			$ips = request_var('ips', '');
ef5584
			$ip_list = array_unique(explode("\n", $ips));
ef5584
			$ip_list_log = implode(', ', $ip_list);
ef5584
ef5584
			$ip_exclude = (!empty($_POST['ipexclude'])) ? 1 : 0;
ef5584
ef5584
			$iplist = array();
ef5584
			$hostlist = array();
ef5584
ef5584
			foreach ($ip_list as $item)
ef5584
			{
ef5584
				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))
ef5584
				{
ef5584
					// Don't ask about all this, just don't ask ... !
ef5584
					$ip_1_counter = $ip_range_explode[1];
ef5584
					$ip_1_end = $ip_range_explode[5];
ef5584
ef5584
					while ($ip_1_counter <= $ip_1_end)
ef5584
					{
ef5584
						$ip_2_counter = ($ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[2] : 0;
ef5584
						$ip_2_end = ($ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[6];
ef5584
ef5584
						if ($ip_2_counter == 0 && $ip_2_end == 254)
ef5584
						{
ef5584
							$ip_2_counter = 256;
ef5584
							$ip_2_fragment = 256;
ef5584
ef5584
							$iplist[] = "'$ip_1_counter.*'";
ef5584
						}
ef5584
ef5584
						while ($ip_2_counter <= $ip_2_end)
ef5584
						{
ef5584
							$ip_3_counter = ($ip_2_counter == $ip_range_explode[2] && $ip_1_counter == $ip_range_explode[1]) ? $ip_range_explode[3] : 0;
ef5584
							$ip_3_end = ($ip_2_counter < $ip_2_end || $ip_1_counter < $ip_1_end) ? 254 : $ip_range_explode[7];
ef5584
ef5584
							if ($ip_3_counter == 0 && $ip_3_end == 254)
ef5584
							{
ef5584
								$ip_3_counter = 256;
ef5584
								$ip_3_fragment = 256;
ef5584
ef5584
								$iplist[] = "'$ip_1_counter.$ip_2_counter.*'";
ef5584
							}
ef5584
ef5584
							while ($ip_3_counter <= $ip_3_end)
ef5584
							{
ef5584
								$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;
ef5584
								$ip_4_end = ($ip_3_counter < $ip_3_end || $ip_2_counter < $ip_2_end) ? 254 : $ip_range_explode[8];
ef5584
ef5584
								if ($ip_4_counter == 0 && $ip_4_end == 254)
ef5584
								{
ef5584
									$ip_4_counter = 256;
ef5584
									$ip_4_fragment = 256;
ef5584
ef5584
									$iplist[] = "'$ip_1_counter.$ip_2_counter.$ip_3_counter.*'";
ef5584
								}
ef5584
ef5584
								while ($ip_4_counter <= $ip_4_end)
ef5584
								{
ef5584
									$iplist[] = "'$ip_1_counter.$ip_2_counter.$ip_3_counter.$ip_4_counter'";
ef5584
									$ip_4_counter++;
ef5584
								}
ef5584
								$ip_3_counter++;
ef5584
							}
ef5584
							$ip_2_counter++;
ef5584
						}
ef5584
						$ip_1_counter++;
ef5584
					}
ef5584
				}
ef5584
				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)))
ef5584
				{
ef5584
					$iplist[] = "'" . trim($item) . "'";
ef5584
				}
ef5584
				else if (preg_match('#^([\w\-_]\.?){2,}$#is', trim($item)))
ef5584
				{
ef5584
					$hostlist[] = "'" . trim($item) . "'";
ef5584
				}
ef5584
				else if (preg_match("#^([a-z0-9\-\*\._/]+?)$#is", trim($item)))
ef5584
				{
ef5584
					$hostlist[] = "'" . trim($item) . "'";
ef5584
				}
ef5584
			}
ef5584
ef5584
			$sql = 'SELECT site_ip, site_hostname
ef5584
				FROM ' . SITELIST_TABLE . "
ef5584
				WHERE ip_exclude = $ip_exclude";
ef5584
			$result = $db->sql_query($sql);
ef5584
ef5584
			if ($row = $db->sql_fetchrow($result))
ef5584
			{
ef5584
				$iplist_tmp = array();
ef5584
				$hostlist_tmp = array();
ef5584
				do
ef5584
				{
ef5584
					if ($row['site_ip'])
ef5584
					{
ef5584
						if (strlen($row['site_ip']) > 40)
ef5584
						{
ef5584
							continue;
ef5584
						}
ef5584
ef5584
						$iplist_tmp[] = "'" . $row['site_ip'] . "'";
ef5584
					}
ef5584
					else if ($row['site_hostname'])
ef5584
					{
ef5584
						if (strlen($row['site_hostname']) > 255)
ef5584
						{
ef5584
							continue;
ef5584
						}
ef5584
ef5584
						$hostlist_tmp[] = "'" . $row['site_hostname'] . "'";
ef5584
					}
ef5584
					// break;
ef5584
				}
ef5584
				while ($row = $db->sql_fetchrow($result));
ef5584
ef5584
				$iplist = array_unique(array_diff($iplist, $iplist_tmp));
ef5584
				$hostlist = array_unique(array_diff($hostlist, $hostlist_tmp));
ef5584
				unset($iplist_tmp);
ef5584
				unset($hostlist_tmp);
ef5584
			}
ef5584
			$db->sql_freeresult($result);
ef5584
ef5584
			if (sizeof($iplist))
ef5584
			{
ef5584
				foreach ($iplist as $ip_entry)
ef5584
				{
ef5584
					$sql = 'INSERT INTO ' . SITELIST_TABLE . " (site_ip, ip_exclude)
ef5584
						VALUES ($ip_entry, $ip_exclude)";
ef5584
					$db->sql_query($sql);
ef5584
				}
ef5584
			}
ef5584
ef5584
			if (sizeof($hostlist))
ef5584
			{
ef5584
				foreach ($hostlist as $host_entry)
ef5584
				{
ef5584
					$sql = 'INSERT INTO ' . SITELIST_TABLE . " (site_hostname, ip_exclude)
ef5584
						VALUES ($host_entry, $ip_exclude)";
ef5584
					$db->sql_query($sql);
ef5584
				}
ef5584
			}
ef5584
ef5584
			if (!empty($ip_list_log))
ef5584
			{
ef5584
				// Update log
ef5584
				$log_entry = ($ip_exclude) ? 'LOG_DOWNLOAD_EXCLUDE_IP' : 'LOG_DOWNLOAD_IP';
ef5584
				add_log('admin', $log_entry, $ip_list_log);
ef5584
			}
ef5584
ef5584
			trigger_error($user->lang['SECURE_DOWNLOAD_UPDATE_SUCCESS'] . adm_back_link($this->u_action));
ef5584
		}
ef5584
		else if (isset($_POST['unsecuresubmit']))
ef5584
		{
ef5584
			$unip_sql = request_var('unip', array(0));
ef5584
ef5584
			if (sizeof($unip_sql))
ef5584
			{
ef5584
				$l_unip_list = '';
ef5584
ef5584
				// Grab details of ips for logging information later
ef5584
				$sql = 'SELECT site_ip, site_hostname
ef5584
					FROM ' . SITELIST_TABLE . '
ef5584
					WHERE ' . $db->sql_in_set('site_id', $unip_sql);
ef5584
				$result = $db->sql_query($sql);
ef5584
ef5584
				while ($row = $db->sql_fetchrow($result))
ef5584
				{
ef5584
					$l_unip_list .= (($l_unip_list != '') ? ', ' : '') . (($row['site_ip']) ? $row['site_ip'] : $row['site_hostname']);
ef5584
				}
ef5584
				$db->sql_freeresult($result);
ef5584
ef5584
				$sql = 'DELETE FROM ' . SITELIST_TABLE . '
ef5584
					WHERE ' . $db->sql_in_set('site_id', $unip_sql);
ef5584
				$db->sql_query($sql);
ef5584
ef5584
				add_log('admin', 'LOG_DOWNLOAD_REMOVE_IP', $l_unip_list);
ef5584
			}
ef5584
ef5584
			trigger_error($user->lang['SECURE_DOWNLOAD_UPDATE_SUCCESS'] . adm_back_link($this->u_action));
ef5584
		}
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Write display_order config field
ef5584
	*/
ef5584
	function display_order($value, $key = '')
ef5584
	{
ef5584
		$radio_ary = array(0 => 'DESCENDING', 1 => 'ASCENDING');
ef5584
ef5584
		return h_radio('config[display_order]', $radio_ary, $value, $key);
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Adjust all three max_filesize config vars for display
ef5584
	*/
ef5584
	function max_filesize($value, $key = '')
ef5584
	{
ef5584
		// Determine size var and adjust the value accordingly
ef5584
		$size_var = ($value >= 1048576) ? 'mb' : (($value >= 1024) ? 'kb' : 'b');
ef5584
		$value = get_formatted_filesize($value, false);
ef5584
ef5584
		return '<input type="text" id="' . $key . '" size="8" maxlength="15" name="config[' . $key . ']" value="' . $value . '" /> <select name="' . $key . '">' . size_select_options($size_var) . '</select>';
ef5584
	}
ef5584
ef5584
	/**
ef5584
	* Write secure_allow_deny config field
ef5584
	*/
ef5584
	function select_allow_deny($value, $key = '')
ef5584
	{
ef5584
		$radio_ary = array(1 => 'ORDER_ALLOW_DENY', 0 => 'ORDER_DENY_ALLOW');
ef5584
ef5584
		return h_radio('config[' . $key . ']', $radio_ary, $value, $key);
ef5584
	}
ef5584
ef5584
}
ef5584
ef5584
?>