<?php
//
//	file: includes/_cp_attach_fields.php
//	author: ptirhiik
//	begin: 15/02/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('Hack attempt');
}

function get_special_categories()
{
	return array(
		NONE_CAT => 'Not_assigned',
		IMAGE_CAT => 'Category_images',
		STREAM_CAT => 'Category_stream_files',
		SWF_CAT => 'Category_swf_files',
		DENIED_CAT => 'Category_denied',
	);
}

function get_download_modes()
{
	return array(
		INLINE_LINK => 'Link_inline',
		PHYSICAL_LINK => 'Link_physical',
	);
}

function attach_quota_limit_list($txt_none='')
{
	global $db;
	static $data;

	$txt_none = empty($txt_none) ? 'Not_assigned' : $txt_none;
	if ( !$data )
	{
		$data = array();
		$sql = 'SELECT quota_limit_id, quota_desc
					FROM ' . ATT_QUOTA_LIMITS_TABLE . '
					ORDER BY quota_limit ASC';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$data[ intval($row['quota_limit_id']) ] = $row['quota_desc'];
		}
		$db->sql_freeresult($result);
	}
	return array(0 => $txt_none) + $data;
}

function attach_get_user_quota($quota_type='upload', $check_exists=false)
{
	global $db, $view_user, $attach_config;
	static $quotas, $done;

	if ( !$done )
	{
		$done = true;
		$quotas = array();
		$sql = 'SELECT quota_limit_id, quota_type
					FROM ' . ATT_QUOTA_TABLE . '
					WHERE group_id = ' . intval($view_user->data['group_id']);
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$type = intval($row['quota_type']) == QUOTA_UPLOAD_LIMIT ? 'upload' : 'pm';
			$quotas[$type] = intval($row['quota_limit_id']);
		}
		$db->sql_freeresult($result);
	}
	$dft = array(
		'upload' => intval($attach_config->data['default_upload_quota']),
		'pm' => intval($attach_config->data['default_pm_quota']),
	);
	return $check_exists ? isset($quotas[$quota_type]) : (isset($quotas[$quota_type]) ? intval($quotas[$quota_type]) : intval($dft[$quota_type]));
}

function get_extension_groups_list()
{
	global $db, $user;
	static $done, $data;

	if ( !$done )
	{
		$done = true;
		$data = array(0 => $user->lang('Not_assigned'));
		$sql = 'SELECT cat_id, ext_group_id, ext_group_name
					FROM ' . ATT_EXT_GROUPS_TABLE . '
					ORDER BY cat_id, ext_group_name, ext_group_id';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$data[ intval($row['ext_group_id']) ] = $row['ext_group_name'];
		}
		$db->sql_freeresult($result);
	}
	return $data;
}

class field_attach_dft_quota extends field_list
{
	function field_attach_dft_quota($field_name, &$data)
	{
		$data['options'] = attach_quota_limit_list('No_quota_limit');
		parent::field_list($field_name, $data);
	}

	function init()
	{
		parent::init();
		$this->type = 'list';
	}
}

class field_attach_quota extends field_list
{
	function field_attach_quota($field_name, &$data)
	{
		$data['options'] = attach_quota_limit_list('Not_assigned');
		parent::field_list($field_name, $data);
	}

	function init()
	{
		// get the original value
		$this->data['value'] = attach_get_user_quota($this->data['quota_type']);
		parent::init();
		$this->type = 'list';
	}

	function validate()
	{
		global $db, $view_user;

		$quota_type = $this->data['quota_type'] == 'upload' ? QUOTA_UPLOAD_LIMIT : QUOTA_PM_LIMIT;
		$exists = attach_get_user_quota($this->data['quota_type'], true);

		// delete
		if ( !$this->value )
		{
			if ( $exists )
			{
				$sql = 'DELETE FROM ' . ATT_QUOTA_TABLE . '
							WHERE group_id = ' . intval($view_user->data['group_id']) . '
								AND quota_type = ' . intval($quota_type);
				$db->sql_query($sql, false, __LINE__, __FILE__);
			}
		}

		// update
		else if ( $exists )
		{
			$sql = 'UPDATE ' . ATT_QUOTA_TABLE . '
						SET quota_limit_id = ' . intval($this->value) . '
						WHERE group_id = ' . intval($view_user->data['group_id']) . '
							AND quota_type = ' . intval($quota_type);
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}

		// create
		else
		{
			$fields = array(
				'group_id' => intval($view_user->data['group_id']),
				'quota_type' => intval($quota_type),
				'quota_limit_id' => intval($this->value),
			);
			$db->sql_statement($fields);
			$sql = 'INSERT INTO ' . ATT_QUOTA_TABLE . '
						(' . $db->sql_fields . ') VALUES(' . $db->sql_values . ')';
			$db->sql_query($sql, false, __LINE__, __FILE__);
		}
	}
}

class field_attach_filesize extends field
{
	var $fields;

	function field_attach_filesize($name, &$data)
	{
		$units = array();
		$unit = size_get_units($data['value'], $units);
		$fields = array(
			$name . '_size' => array_merge($data, array('type' => 'int', 'value' => round($data['value'] / $unit))),
			$name . '_unit' => array('type' => 'list', 'combined' => true, 'output' => $data['output'], 'options' => $units, 'value' => $unit),
		);
		$this->fields = array();
		foreach ( $fields as $field_name => $field )
		{
			$field_class = 'field_' . $field['type'];
			$this->fields[$field_name] = new $field_class($field_name, $field);
		}
		parent::field($name, $data);
	}

	function get_value($value)
	{
		$size = intval($this->fields[ $this->name . '_size' ]->get_value($this->fields[ $this->name . '_size' ]->data['value']));
		$unit = intval($this->fields[ $this->name . '_unit' ]->get_value($this->fields[ $this->name . '_unit' ]->data['value']));
		return $size * $unit;
	}

	function check()
	{
		global $error, $error_msg;
		if ( $this->data['output'] )
		{
			return;
		}
		// do basic checks
		foreach ($this->fields as $field_name => $field)
		{
			$field->check();
		}
	}

	function display()
	{
		foreach ($this->fields as $field_name => $field)
		{
			$field->display();
		}
	}
}

class field_attach_search_username extends field_varchar
{
	function init()
	{
		parent::init();
		$this->type = 'varchar';
	}

	function get_value($value)
	{
		return $this->encode(_read($this->name, TYPE_HTML, $this->decode($value)));
	}

	function encode($value)
	{
		return stripslashes(phpbb_clean_username(addslashes(trim(preg_replace('#\s+#', ' ', $value)))));
	}
}

?>