<?php
//
//	file: includes/class_attach_config.php
//	author: ptirhiik
//	begin: 05/03/2006
//	version: 1.6.0 - 10/06/2006
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
	exit;
}

// constants
define('ATTACH_CURRENT_VERSION', '1.0.0');

// Auth (only for auth.php, in order to be able to hit regen and get back this one)
define('AUTH_DOWNLOAD', 20);

// Download Modes
define('INLINE_LINK', 1);
define('PHYSICAL_LINK', 2);

// Categories
define('NONE_CAT', 0);
define('IMAGE_CAT', 1);
define('STREAM_CAT', 2);
define('SWF_CAT', 3);
define('DENIED_CAT', 4);

// Tables
define('ATT_CONFIG_TABLE', $table_prefix . 'att_config');
define('ATT_EXT_GROUPS_TABLE', $table_prefix . 'att_ext_groups');
define('ATT_EXTENSIONS_TABLE', $table_prefix . 'att_extensions');
define('ATT_DESC_TABLE', $table_prefix . 'att_desc');
define('ATT_LINKS_TABLE', $table_prefix . 'att_links');
define('ATT_QUOTA_TABLE', $table_prefix . 'att_quota');
define('ATT_QUOTA_LIMITS_TABLE', $table_prefix . 'att_quota_limits');
define('ATT_TEMP_DIR_TABLE', $table_prefix . 'att_temp_dir');

// Misc
define('ADMIN_MAX_ATTACHMENTS', 50); // Maximum Attachments in Posts or PM's for Admin Users
define('THUMB_DIR', 'thumbs');

// Quota Types
define('QUOTA_UPLOAD_LIMIT', 1);
define('QUOTA_PM_LIMIT', 2);

// do not go further during login or installation
if ( defined('IN_LOGIN') || defined('IN_INSTALL') )
{
	return;
}

// intall directory still present
if ( file_exists(phpbb_realpath($config->root . 'install_att')) )
{
	if ( $config->data['mod_attachment_CH'] != ATTACH_CURRENT_VERSION )
	{
		header('Location: ' . $config->url('install_att/install'));
		exit;
	}
	else
	{
		message_die(GENERAL_MESSAGE, 'Please remove install_att/ directory');
	}
}

if ( !$config->data['mod_attachment_CH'] )
{
	return;
}

// Get attachment mod configuration
class attach_config
{
	var $data;
	var $done;

	function attach_config()
	{
		$this->data = array();
		$this->done = false;
	}

	function read($force=false)
	{
		global $config;

		if ( !$this->done || $force )
		{
			$db_cache = new cache_config('dta_attach_config');
			$sql = 'SELECT *
						FROM ' . ATT_CONFIG_TABLE;
			$this->data = $db_cache->sql_query($sql, __LINE__, __FILE__, $force, 'config_name');
			$data['board_lang'] = trim($config->data['default_lang']);
		}
		return $this->data;
	}
}

class attach_extensions
{
	var $data;
	var $done;

	function attach_extensions()
	{
		$this->done = false;
		$this->data = array();
	}

	function read($force=false)
	{
		if ( !$this->done || $force )
		{
			$db_cache = new cache('dta_attach_group_extensions');
			$sql = 'SELECT e.extension, g.cat_id, g.download_mode, g.upload_icon, g.max_filesize, g.forum_permissions
						FROM ' . ATT_EXTENSIONS_TABLE . ' e, ' . ATT_EXT_GROUPS_TABLE . ' g
						WHERE e.ext_group_id = g.ext_group_id
							AND g.allow_group = 1';
			$this->data = $db_cache->sql_query($sql, __LINE__, __FILE__, $force, 'extension');
			$this->done = true;
		}
		return $this->data;
	}

	function encode(&$forums_list)
	{
		$res = '';
		if ( !empty($forums_list) )
		{
			$res = substr(',' . implode(',', array_map('dechex', $forums_list)) . ',', 0, 255);
			$res = substr($res, 0, strrpos(',' . $res, ','));
		}
		return $res;
	}

	function decode(&$forum_permissions)
	{
		return trim($forum_permissions) == '' ? '' : array_map('hexdec', explode(',', substr(trim($forum_permissions), 1, -1)));
	}

	function auth($forum_permissions, $forum_id, $admin_check=true)
	{
		if ( $admin_check )
		{
			global $user;
			if ( $user->data['user_level'] == ADMIN )
			{
				return true;
			}
		}
		return !empty($forum_id) && ((trim($forum_permissions) == '') || strpos(' ' . $forum_permissions, $this->encode(array($forum_id))));
	}
}

// get_var : read $_POST/GET vars
function get_var($var_name, $default, $multibyte = false)
{
	global $HTTP_POST_VARS, $HTTP_GET_VARS;

	$request_var = (isset($HTTP_POST_VARS[$var_name])) ? $HTTP_POST_VARS : $HTTP_GET_VARS;
	if ( !isset($request_var[$var_name]) || (is_array($request_var[$var_name]) && !is_array($default)) || (is_array($default) && !is_array($request_var[$var_name])) )
	{
		return is_array($default) ? array() : $default;
	}

	// get type from default
	if ( !is_array($default) )
	{
		$type = gettype($default);
	}
	else
	{
		list($key_type, $type) = each($default);
		$type = gettype($type);
		$key_type = gettype($key_type);
	}

	// get value
	$var = $request_var[$var_name];
	if ( !is_array($var) )
	{
		_set_var($var, $var, $type, $multibyte);
	}
	else
	{
		$_var = $var;
		$var = array();
		foreach ($_var as $k => $v)
		{
			_set_var($k, $k, $key_type);
			if ( !is_array($v) )
			{
				_set_var($var[$k], $v, $type, $multibyte);
			}
			else
			{
				foreach ($v as $_k => $_v)
				{
					_set_var($_k, $_k, $key_type);
					_set_var($var[$k][$_k], $_v, $type, $multibyte);
				}
			}
		}
	}
	return $var;
}

// _set_var : type cast for get_var()
function _set_var(&$result, $var, $type, $multibyte = false)
{
	settype($var, $type);
	$result = $var;

	if ($type == 'string')
	{
		$result = trim(htmlspecialchars(str_replace(array("\r\n", "\r", '\xFF'), array("\n", "\n", ' '), $result)));
		// 2.0.x is doing addslashes on all variables
		$result = stripslashes($result);
		if ($multibyte)
		{
			$result = preg_replace('#&amp;(\#[0-9]+;)#', '&\1', $result);
		}
	}
}

// all the stuff
if ( defined('IN_ADMIN') || in_array($requester, array('faq', 'modcp', 'posting', 'search', 'viewtopic', 'privmsg', 'profile', 'usercp')) )
{
	$att_lang_files = array('lang_main_attach');
	if ( defined('IN_ADMIN') )
	{
		$att_lang_files[] = 'lang_admin_attach';
	}
	$user->use_lang_file($att_lang_files);
	$user->use_lang('attachcp');
	unset($att_lang_files);

	// Get Attachment Config
	$attach_config = new attach_config();
	$attach_config->read();

	// instantiate group/extensions
	$attach_extensions = new attach_extensions();

	// file handling
	$file_handler_layer = intval($attach_config->data['allow_ftp_upload']) ? 'includes/class_file_handler_ftp' : 'includes/class_file_handler_copy';
	include($config->url($file_handler_layer));
	unset($file_handler_layer);
	include($config->url('includes/class_file_handler'));

	include($config->url('includes/class_image'));
	include($config->url('includes/class_attach_display'));

	if ( ($requester == 'posting') || ($requester == 'privmsg') )
	{
		include($config->url('includes/class_thumbnail'));
		include($config->url('includes/class_attach_parent'));
		include($config->url('includes/class_attach_' . $requester));
	}
}

?>