<?php
//
//	file: includes/class_attach_rules.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;
}

class attach_rules
{
	var $requester;
	var $data;
	var $forum_id;

	function attach_rules($requester)
	{
		$this->requester = $requester;
		$this->data = array();
		$this->forum_id = 0;
	}

	function read($forum_id=0)
	{
		global $db, $user;
		global $attach_config;

		$this->data = array();
		$this->forum_id = $forum_id;
		if ( $this->forum_id )
		{
			$user->get_cache(POST_FORUM_URL);
			$authorised = $user->auth(POST_FORUM_URL, 'auth_attachments', $this->forum_id) || $user->auth(POST_FORUM_URL, 'auth_mod', $this->forum_id);
			$_max_filesize = intval($attach_config->data['max_filesize']);
		}
		else
		{
			$authorised = ($user->data['user_level'] == ADMIN) || intval($attach_config->data['allow_pm_attach']);
			$_max_filesize = intval($attach_config->data['max_filesize_pm']);
		}
		if ( $authorised )
		{
			$sql = 'SELECT g.ext_group_id, g.ext_group_name, g.max_filesize, g.forum_permissions, e.extension
						FROM ' . ATT_EXT_GROUPS_TABLE . ' g
							LEFT JOIN ' . ATT_EXTENSIONS_TABLE . ' e
								ON e.ext_group_id = g.ext_group_id
						WHERE g.allow_group = 1
						ORDER BY g.ext_group_name, e.extension';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);
			while ( $row = $db->sql_fetchrow($result) )
			{
				$extension = $row['extension'];
				unset($row['extension']);
				if ( !isset($this->data[ intval($row['ext_group_id']) ]) )
				{
					$row['extensions'] = array($extension);
					$this->data[ intval($row['ext_group_id']) ] = $row;
				}
				else
				{
					$this->data[ intval($row['ext_group_id']) ]['extensions'][] = $extension;
				}
			}
			$db->sql_freeresult($result);
		}
		else
		{
			message_die(GENERAL_ERROR, 'No_auths_to_rules');
		}

		return true;
	}

	function display()
	{
		global $template, $user, $config;
		global $attach_config, $attach_extensions;

		// start the display
		if ( !empty($this->data) )
		{
			foreach ( $this->data as $ext_group_id => $row )
			{
				if ( $authorised = (!$this->forum_id || $attach_extensions->auth($row['forum_permissions'], $this->forum_id, false)) )
				{
					$units = array();
					$max_filesize = intval($row['max_filesize']);
					$max_filesize_unit = size_get_units($max_filesize, $units);
					$template->assign_block_vars('group', array(
						'GROUP_RULE_HEADER' => sprintf($user->lang('Group_rule_header'), $row['ext_group_name'], round($max_filesize / $max_filesize_unit, 2) . ' ' . $user->lang($units[$max_filesize_unit])),
					));

					// display extensions
					$count_extensions = count($row['extensions']);
					for ( $i = 0; $i < $count_extensions; $i++ )
					{
						$template->assign_block_vars('group.extension', array(
							'EXTENSION' => $row['extensions'][$i],
						));
						$template->set_switch('group.extension.first', $i == 0);
					}
				}
			}
		}

		// send header
		$template->assign_vars(array(
			'L_RULES_TITLE' => $user->lang('Attach_rules_title'),
			'L_EMPTY' => $user->lang('Note_user_empty_group_permissions'),
			'L_CLOSE_WINDOW' => $user->lang('Close_window'),
		));
	}
}

// get forum_id
$forum_id = _read(POST_FORUM_URL, TYPE_INT);

$userdata = session_pagestart($user_ip, $forum_id ? PAGE_POSTING : PAGE_PRIVMSGS);
$user->set($requester, 'attachcp');

$attach_rules = new attach_rules($requester);
$attach_rules->read($forum_id);
$attach_rules->display();
unset($attach_rules);

$gen_simple_header = true;
$page_title = $user->lang('Attach_rules_title');
include($config->url('includes/page_header'));
$template->set_filenames(array('body' => 'attach_rules_body.tpl'));
$template->pparse('body');
include($config->url('includes/page_tail'));

?>