##############################################################
## MOD Title: Post Expire
## MOD Author: crazygandalf < gandalfszalony@o2.pl > (Bartłomiej Zielonka) http://kkr.nsc.pl
## MOD Description: Mod allows you to set the expire time for posts/topics and choose to delete posts/topics or move/lock topics.    
## MOD Version: 1.0.3
##
## Installation Level: Easy
## Installation Time: 22 Minutes
## Files To Edit: posting.php
##                viewforum.php
##								viewtopic.php
##                includes/functions_display.php
##                includes/functions_posting.php
##                language/en/acp/common.php
##                styles/prosilver/template/overall_header.html
##                styles/prosilver/template/posting_editor.html
##                styles/prosilver/template/viewforum_body.html
##                styles/prosilver/template/viewtopic_body.html
##
## Included Files: root/adm/style/acp_post_expire.html 
##								 root/includes/acp/info/acp_post_expire.php
##								 root/includes/acp/acp_post_expire.php
##								 root/language/en/mods/pe.php 
##								 root/language/en/mods/permissions_post_expire.php
##								 root/install/index.php
##								 root/styles/prosilver/template/datepicker/*.*
##
## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##############################################################
## For security purposes, please check: http://www.phpbb.com/mods/
## for the latest version of this MOD. Although MODs are checked
## before being allowed in the MODs Database there is no guarantee
## that there are no security problems within the MOD. No support
## will be given for MODs not found within the MODs Database which
## can be found at http://www.phpbb.com/mods/
##############################################################
## Author Notes: When posting you can set expire time of the post. If this is the first post in topic, expire refers to this entire topic. You can set to delete post/topic after expire time or to lock/move/lock&move the topic. Of course it happens automatically. Expire time is displayed on viewtopic page in case of posts, and on viewforum page in case of topics. Mod is fully configurable and allows to set users permissions. For more info about configuration, permission or changes please take a look at contrib/readme.txt.
##############################################################
## MOD History:
##
##   2010-02-02 - Version 1.0.3 - RC
##			- small fix in install file
##			- small fix with exact method of selecting time 
##
##   2010-01-10 - Version 1.0.2 - RC
##			- changes for XHTML compatibility
##			- reduced size of datepicker js (about 15%) 
##			- install file foolproof
##			- some other small fixes in coding 
##
##   2009-11-22 - Version 1.0.1 - RC
##			- datepicker moved to template dir
##			- day/month names in datepicker are now taken from lang files instead being hardcoded
##			- some other small fixes in coding 
##
##   2009-10-05 - Version 1.0.0 - RC
##			- new intall file
##			- repacked to meet new MODX requirements
##
##   2009-06-05 - Version 0.4.2 - BETA
##			- new datepicker with GPL v.2 compatible license
##			- bugfix - some variables weren't initialised
##			- bugfix - expire time wasn't including timezones
##			- added possibility to set unlock expire time for locked topics  
##			- changes for phpBB 3.0.5 compatibility
##
##   2009-04-13 - Version 0.4.1 - BETA
##			- bugfix - allowed moving when forum wasn't clickable
##			- bugfix - some variables weren't initialised
##			- minor change - datepicker uses form id instead of it's number
##			- added time before expire in ACP for changing colour  
##			- added max time expire can be set
##			- new permission - can use only default move forum
##			- added parsing 12h time AM/PM
##
##   2009-03-23 - Version 0.4.0 - BETA
##			- two new types of expire - move, lock&move topic
##			- datepicker instead of five <select> inputs
##			- new permissions  
##			- configuration panel in ACP
##			- reduced some sql queries
##			- some changes according to Coding Guidelines
##
##   2009-01-23 - Version 0.3.0 - BETA
##			- two types of expire - delete post/topic and lock topic
##			- two expire time settings - exact and with after specified time  
##			- user permissions to allow writing expiring posts  
##			- install file added
##			- post_expire_remove() function instead of file including
##			- function post_expire_remove() called in viewtopic and viewforum
##			- added exact date validations preventing setting past and invalid dates
##			- changes for phpBB 3.0.4 compatibility
##
##   2008-11-30 - Version 0.2.0 - BETA
##			- topic expire, when first post is set to expire
##			- support for subsilver2
##			- post color change, when expire time is set  
##
##   2008-11-27 - Version 0.1.0 - BETA
##			- initial beta release 
##
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
##############################################################

#
#-----[ COPY ]------------------------------------------
#
copy root/adm/style/acp_post_expire.html to adm/style/acp_post_expire.html 
copy root/includes/acp/info/acp_post_expire.php to includes/acp/info/acp_post_expire.php
copy root/includes/acp/acp_post_expire.php to includes/acp/acp_post_expire.php
copy root/language/en/mods/pe.php to language/en/mods/pe.php
copy root/language/en/mods/permissions_post_expire.php to language/en/mods/permissions_post_expire.php
copy root/install/index.php to install/index.php 
copy root/styles/prosilver/template/datepicker/*.* to styles/prosilver/template/datepicker/*.*

#
#-----[ OPEN ]------------------------------------------
#
posting.php

#
#-----[ FIND ]------------------------------------------
#
$user->setup(array('posting', 'mcp', 'viewtopic'), $post_data['forum_style']);

#
#-----[ AFTER, ADD ]------------------------------------------
#
$user->add_lang('mods/pe');

#
#-----[ FIND ]------------------------------------------
#
// Set some default variables
$uninit = array('post_attachment' => 0, 'poster_id' => $user->data['user_id'], 'enable_magic_url' => 0, 'topic_status' => 0, 'topic_type' => POST_NORMAL, 'post_subject' => '', 'topic_title' => '', 'post_time' => 0, 'post_edit_reason' => '', 'notify_set' => 0);

#
#-----[ REPLACE WITH ]------------------------------------------
#
// Set some default variables
$post_data['topic_first_post_id'] = (isset($post_data['topic_first_post_id'])) ? (int) $post_data['topic_first_post_id'] : 0;
$post_data['date_picker'] = '';
$post_data['post_expire_time'] = (isset($post_data['post_expire_time'])) ? (int) $post_data['post_expire_time'] : -1;
$post_data['post_expire_time'] = ($post_data['post_expire_time'] != 0) ? $post_data['post_expire_time'] : -1;
$expire_default_action = -1;
$perm_actions = array('0' => $auth->acl_get('u_post_expire_delete'), '1' => $auth->acl_get('u_post_expire_lock'), '2' => $auth->acl_get('u_post_expire_move'), '3' => $auth->acl_get('u_post_expire_lockandmove'));

foreach ($perm_actions as $key => $value)
{
	if ($value)
	{
		if ($key == $config['expire_default_action'])
		{
			$expire_default_action = $key;
			break;
		}
		else if ($expire_default_action == -1)
		{
			$expire_default_action = $key;
		}
	}
}
unset($perm_actions);

if ($expire_default_action == -1)
{
	$expire_default_action = 0;
}

$uninit = array('post_attachment' => 0, 'poster_id' => $user->data['user_id'], 'enable_magic_url' => 0, 'topic_status' => 0, 'topic_type' => POST_NORMAL, 'post_subject' => '', 'topic_title' => '', 'post_time' => 0, 'post_edit_reason' => '', 'notify_set' => 0, 'topic_expire_type' => $expire_default_action);

#
#-----[ FIND ]------------------------------------------
#
	$post_data['enable_sig']		= (!$config['allow_sig'] || !$auth->acl_get('f_sigs', $forum_id) || !$auth->acl_get('u_sig')) ? false : ((isset($_POST['attach_sig']) && $user->data['is_registered']) ? true : false);  

#
#-----[ AFTER, ADD ]------------------------------------------
#
	$post_data['date_picker'] = request_var('date_picker', '');
	$post_data['post_expire_time'] = request_var('post_expire_time', -1);
	$post_data['topic_expire_type'] = request_var('topic_expire_type', $expire_default_action);
	$post_data['expire_forum_id'] = request_var('expire_forum_id', 0);

	if ($config['expire_enable'])
	{
		if(!empty($post_data['date_picker'])) 
		{ 
			$date_picker = str_replace('-', ' ', $post_data['date_picker']);
			$date_picker = str_replace(':', ' ', $date_picker);
			$date_array = explode(' ', $date_picker);

			$day = (isset($date_array[0])) ? $date_array[0] : NULL;
			$month = (isset($date_array[1])) ? $date_array[1] : NULL;
			$year = (isset($date_array[2])) ? $date_array[2] : NULL;
			(!isset($date_array[3])) ? $hour = NULL : $date_array[3] ? $hour = $date_array[3] : $hour = '00';
			(!isset($date_array[4])) ? $minute = NULL : $date_array[4] ? $minute = $date_array[4] : $minute = '00';
			$am_pm = (isset($date_array[5])) ? $date_array[5] : NULL;
   
			if (preg_match("/^(\d{2}) (\d{2}) (\d{4}) ([01][0-9]|2[0-3]) ([0-5][0-9])$/", "$day $month $year $hour $minute", $matches)) 
			{
				if (isset($am_pm))
				{
					$am_pm = strtolower($am_pm);  
					switch ($am_pm)
					{
						case 'am':
							$hour = ($hour == 12) ? 0 : $hour;
						break;
						case 'pm':
							$hour = ($hour == 12) ? 12 : $hour + 12;
						break;
						default:
							$error[] = $user->lang['INVALID_DATE'];
						break;
					}
				}
				
				if (checkdate($matches[2], $matches[1], $matches[3])) 
				{
					$post_data['post_expire_time'] = gmmktime(intval($hour), intval($minute), 0, intval($month), intval($day), intval($year)) - (time() + $user->timezone + $user->dst);
					if ($post_data['post_expire_time'] < 0)
					{
						$error[] = $user->lang['PAST_DATE'];
					}
				} 
				else 
				{
					$error[] = $user->lang['INVALID_DATE'];
				}       
			} 
			else 
			{
				$error[] = $user->lang['INVALID_DATE'];
			} 
		}
		
		if (intval($config['expire_max_time']) != 0 && (intval($post_data['post_expire_time'])) > intval($config['expire_max_time'])*86400)
		{
			$error[] = sprintf($user->lang['MAX_DATE'], $config['expire_max_time']);
		}
		
		if($post_data['post_expire_time'] == -1) 
		{
			$post_data['topic_expire_type'] = $expire_default_action;
			$post_data['expire_forum_id'] = 0;
		} 
		else if ($post_data['post_expire_time'] != -1 && ($post_data['topic_expire_type'] == 0 || $post_data['topic_expire_type'] == 1))
		{  
			$post_data['expire_forum_id'] = 0;
		}
		
		if ($post_data['post_expire_time'] != -1 && $post_data['expire_forum_id'] == 0 && ($post_data['topic_expire_type'] == 2 || $post_data['topic_expire_type'] == 3) && $post_data['topic_first_post_id'] == $post_id)
		{
			if (!$auth->acl_get('u_post_expire_only'))
			{
				$error[] = $user->lang['MOVE_UNABLE'];
			}
		}
	}
	
#
#-----[ FIND ]------------------------------------------
#
				'post_edit_locked'		=> (int) $post_data['post_edit_locked'],

#
#-----[ AFTER, ADD ]------------------------------------------
#
				'post_expire_time'		=> ($post_data['post_expire_time'] != -1) ? (int) $post_data['post_expire_time'] : -1,
				'topic_expire_type'		=> (int) $post_data['topic_expire_type'],
				'expire_forum_id'		=> (int) $post_data['expire_forum_id'],

#
#-----[ FIND ]------------------------------------------
#
add_form_key('posting');

#
#-----[ AFTER, ADD ]------------------------------------------
#
$s_expire_forum_options = 0;
$post_expire_select = '';
$topic_expire_type_select = '';
$expire_after_show = 0;
$expire_exact_show = 0;
		
if ($config['expire_enable'] && ($auth->acl_get('u_post_expire_delete') || $auth->acl_get('u_post_expire_lock') || $auth->acl_get('u_post_expire_move') || $auth->acl_get('u_post_expire_lockandmove'))) 
{
	if (!$auth->acl_get('u_topic_expire_only') || ($auth->acl_get('u_topic_expire_only') && $post_data['topic_first_post_id'] == $post_id) || ($auth->acl_get('u_topic_expire_only') && $auth->acl_get('u_post_expire_only')))
	{
		switch ($config['expire_kind_set']) 
		{
			case 0:
					if ($mode == 'edit' && $post_data['post_expire_time'] != -1) 
					{
						$expire_after_show = 0;
						$expire_exact_show = 1;
					} 
					else 
					{
						$expire_after_show = 1;
						$expire_exact_show = 0;
					}
			break;
	
			case 1:
					$expire_after_show = 0;
					$expire_exact_show = 1;
			break;
	
			case 2:
					$expire_after_show = 1;
					$expire_exact_show = 1;
			break;
	
			default:
					$expire_after_show = 0;
					$expire_exact_show = 0;
			break;	
		}
	
		if ($expire_after_show)
		{
			foreach ($user->lang['post_expire'] as $offset => $expire)
			{
				if (is_numeric($offset))
				{
					$selected = ($offset == $post_data['post_expire_time']) ? ' selected="selected"' : '';
					$post_expire_select .= '<option title="' . $expire . '" value="' . $offset . '"' . $selected . '>' . $expire . '</option>';
				}
				else 
				{
					trigger_error('POST_EXPIRE_NUMBER_ERROR');
				}
			} 
		} 

		if ($expire_exact_show)
		{
			if($post_data['post_expire_time'] && !$submit && !$refresh && !$preview && $mode == 'edit' && $post_data['post_expire_time'] != -1) 
			{
				$post_data['date_picker'] = gmdate("d-m-Y H:i", $post_data['post_expire_time'] + $user->timezone + $user->dst); //Use for 24h time
//				$post_data['date_picker'] = gmdate("d-m-Y h:i A", $post_data['post_expire_time'] + $user->timezone + $user->dst); //Use for 12h am/pm time
			}
		}

		if ($post_data['topic_first_post_id'] == $post_id && !$auth->acl_get('u_post_expire_only'))
		{
			$selected1 = ($post_data['topic_expire_type'] == '0') ? ' checked="checked"' : '';
			$selected2 = ($post_data['topic_expire_type'] == '1') ? ' checked="checked"' : '';
			$selected3 = ($post_data['topic_expire_type'] == '2') ? ' checked="checked"' : '';
			$selected4 = ($post_data['topic_expire_type'] == '3') ? ' checked="checked"' : '';
			
			$lock_unlock_lang = ($post_data['topic_status'] == ITEM_LOCKED) ? $user->lang['TOPIC_EXPIRE_UNLOCK'] : $user->lang['TOPIC_EXPIRE_LOCK'];
			$lock_unlock_and_move_lang = ($post_data['topic_status'] == ITEM_LOCKED) ? $user->lang['TOPIC_EXPIRE_UNLOCK_AND_MOVE'] : $user->lang['TOPIC_EXPIRE_LOCK_AND_MOVE']; 						
			if ($auth->acl_get('u_post_expire_delete'))
			{
				$topic_expire_type_select .= '<label for="tet0"><input type="radio" name="topic_expire_type" id="tet0" value="0"'.$selected1.' />'.$user->lang['TOPIC_EXPIRE_DELETE'].'</label>';
			}
			if ($auth->acl_get('u_post_expire_lock'))
			{
				$topic_expire_type_select .= '<label for="tet1"><input type="radio" name="topic_expire_type" id="tet1" value="1"'.$selected2.' />'.$lock_unlock_lang.'</label>';
			}
			if ($auth->acl_get('u_post_expire_move'))
			{
				$topic_expire_type_select .= '<label for="tet2"><input type="radio" name="topic_expire_type" id="tet2" value="2"'.$selected3.' />'.$user->lang['TOPIC_EXPIRE_MOVE'].'</label>';
			}
			if ($auth->acl_get('u_post_expire_lockandmove'))
			{
				$topic_expire_type_select .= '<label for="tet3"><input type="radio" name="topic_expire_type" id="tet3" value="3"'.$selected4.' />'.$lock_unlock_and_move_lang.'</label>';
			}
			
			$post_data['expire_forum_id'] = (isset($post_data['expire_forum_id'])) ? $post_data['expire_forum_id'] : 0;
			if ($auth->acl_get('u_post_expire_move') || $auth->acl_get('u_post_expire_lockandmove'))
			{
				$forum_id_selected = ($post_data['expire_forum_id'] == 0) ? $config['expire_default_forum'] : $post_data['expire_forum_id'];
				if ($auth->acl_get('u_expire_default_forum'))
				{
					$sql = 'SELECT forum_name
						FROM ' . FORUMS_TABLE . '
						WHERE forum_id = ' . $forum_id_selected;
					$result = $db->sql_query($sql);
					$row = $db->sql_fetchrow($result);
					$db->sql_freeresult($result);
 					
 					if (!empty($row['forum_name']))
 					{
						$s_expire_forum_options	= '<option value="' . $forum_id_selected . '" ' . 'selected="selected">' . $row['forum_name'] . '</option>';					 
					}
					else
					{
						$sql = 'SELECT forum_name
							FROM ' . FORUMS_TABLE . '
							WHERE forum_id = ' . $config['expire_default_forum'];
						$result = $db->sql_query($sql);
						$row = $db->sql_fetchrow($result);
						$db->sql_freeresult($result);						
						if (!empty($row['forum_name']))
						{
							$s_expire_forum_options	= '<option value="' . $config['expire_default_forum'] . '" ' . 'selected="selected">' . $row['forum_name'] . '</option>';
						}
						else
						{
							if (!function_exists('make_forum_select'))
							{
								include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
							}
							$s_expire_forum_options = make_forum_select($forum_id_selected, $forum_id, true, true);
						}
					}
				}
				else
				{
					if (!function_exists('make_forum_select'))
					{
						include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
					}
							$s_expire_forum_options = make_forum_select($forum_id_selected, $forum_id, true, true);
				}
			}
			else 
			{
				$s_expire_forum_options = 0;
			}
		}
	}
}

#
#-----[ FIND ]------------------------------------------
#
	'S_FORM_ENCTYPE'			=> $form_enctype,

#
#-----[ AFTER, ADD ]------------------------------------------
#
	'S_POST_EXPIRE'			=> ($config['expire_enable']) ? true : false,
	'S_POST_EXPIRE_SELECT'				=> $post_expire_select,
	'S_TOPIC_EXPIRE_TYPE'				=> $topic_expire_type_select,
	'S_DATE_PICKER'	=> $post_data['date_picker'],
	'S_EXPIRE_FORUM_OPTIONS' => $s_expire_forum_options,
	'S_EXPIRE_AFTER_SHOW' => $expire_after_show,
	'S_EXPIRE_EXACT_SHOW' => $expire_exact_show,
	'S_DATEPICKER_NEEDED' => true,
	'MONTH_NAME' => '["' . $user->lang['datetime']['January'] . '", "' . $user->lang['datetime']['February'] . '", "' . $user->lang['datetime']['March'] . '", "' . $user->lang['datetime']['April'] . '", "' . $user->lang['datetime']['May'] . '", "' . $user->lang['datetime']['June'] . '", "' . $user->lang['datetime']['July'] . '", "' . $user->lang['datetime']['August'] . '", "' . $user->lang['datetime']['September'] . '", "' . $user->lang['datetime']['October'] . '", "' . $user->lang['datetime']['November'] . '", "' . $user->lang['datetime']['December'] . '"]',
	'WEEK_DAY_NAME_1' => '["' . $user->lang['datetime']['Sun'] . '", "' . $user->lang['datetime']['Mon'] . '", "' . $user->lang['datetime']['Tue'] . '", "' . $user->lang['datetime']['Wed'] . '", "' . $user->lang['datetime']['Thu'] . '", "' . $user->lang['datetime']['Fri'] . '", "' . $user->lang['datetime']['Sat'] . '"]',
	'WEEK_DAY_NAME_2' => '["' . $user->lang['datetime']['Mon'] . '", "' . $user->lang['datetime']['Tue'] . '", "' . $user->lang['datetime']['Wed'] . '", "' . $user->lang['datetime']['Thu'] . '", "' . $user->lang['datetime']['Fri'] . '", "' . $user->lang['datetime']['Sat'] . '", "' . $user->lang['datetime']['Sun'] . '"]',

#
#-----[ OPEN ]------------------------------------------
#
viewforum.php

#
#-----[ FIND ]------------------------------------------
#
$user->setup('viewforum', $forum_data['forum_style']);

#
#-----[ AFTER, ADD ]------------------------------------------
#
$user->add_lang('mods/pe');

#
#-----[ FIND ]------------------------------------------
#
// Grab icons
$icons = $cache->obtain_icons();

#
#-----[ AFTER, ADD ]------------------------------------------
#
post_expire_remove();

#
#-----[ FIND ]------------------------------------------
#
// Send vars to template

#
#-----[ BEFORE, ADD ]------------------------------------------
#
		switch($row['topic_expire_type'])
		{
			case 0:
				$l_topic_expire_type = $user->lang['TOPIC_EXPIRE_DELETE'];
			break;
			case 1:
				$l_topic_expire_type = ($row['topic_status'] == ITEM_LOCKED) ? $user->lang['TOPIC_EXPIRE_UNLOCK'] : $user->lang['TOPIC_EXPIRE_LOCK']; 
			break;
			case 2:
				$l_topic_expire_type = $user->lang['TOPIC_EXPIRE_MOVE'];
			break;	
			case 3:
				$l_topic_expire_type = ($row['topic_status'] == ITEM_LOCKED) ? $user->lang['TOPIC_EXPIRE_UNLOCK_AND_MOVE'] : $user->lang['TOPIC_EXPIRE_LOCK_AND_MOVE'];
			break;	
			default:
				$l_topic_expire_type = '';
			break;
		}
		
		$change_colour = false;
		if ($row['topic_expire_time'] && $config['expire_colour_set'])
		{
			if (intval($config['expire_colour_time']) != 0)
			{
				$change_colour = (time() > (intval($row['topic_expire_time']) - intval($config['expire_colour_time'])*3600)) ? true : false;
			}
			else
			{
				$change_colour = true;
			}
		}
		else
		{
			$change_colour = false;
		}
		
#
#-----[ FIND ]------------------------------------------
#
			'LAST_POST_AUTHOR_FULL'		=> get_username_string('full', $row['topic_last_poster_id'], $row['topic_last_poster_name'], $row['topic_last_poster_colour']),

#
#-----[ AFTER, ADD ]------------------------------------------
#
			'TOPIC_EXPIRE'		=> ($row['topic_expire_time']) ? $user->format_date($row['topic_expire_time']) : 0,
			'L_TOPIC_EXPIRE_TYPE'		=> $l_topic_expire_type,
			'S_CHANGE_COLOUR' => $change_colour,

#
#-----[ OPEN ]------------------------------------------
#
viewtopic.php

#
#-----[ FIND ]------------------------------------------
#
$user->setup('viewtopic', $topic_data['forum_style']);

#
#-----[ AFTER, ADD ]------------------------------------------
#
$user->add_lang('mods/pe');

#
#-----[ FIND ]------------------------------------------
#
// Go ahead and pull all data for this topic

#
#-----[ BEFORE, ADD ]------------------------------------------
#
post_expire_remove();

#
#-----[ FIND ]------------------------------------------
#
		'post_edit_locked'	=> $row['post_edit_locked'],

#
#-----[ AFTER, ADD ]------------------------------------------
#
		'post_expire_time'	=> $row['post_expire_time'],

#
#-----[ FIND ]------------------------------------------
#
$postrow = array(

#
#-----[ BEFORE, ADD ]------------------------------------------
#
	$change_colour = false;
	if ($row['post_expire_time'] && $config['expire_colour_set'])
	{
		if (intval($config['expire_colour_time']) != 0)
		{
			$change_colour = (time() > (intval($row['post_expire_time']) - intval($config['expire_colour_time'])*3600)) ? true : false;
		}
		else
		{
			$change_colour = true;
		}
	}
	else
	{
		$change_colour = false;
	}

#
#-----[ FIND ]------------------------------------------
#
		'POST_DATE'			=> $user->format_date($row['post_time'], false, ($view == 'print') ? true : false),
				
#
#-----[ AFTER, ADD ]------------------------------------------
#
		'POST_EXPIRE'		=> ($row['post_expire_time']) ? $user->format_date($row['post_expire_time']) : 0,
		'S_CHANGE_COLOUR' => $change_colour,

#
#-----[ OPEN ]------------------------------------------
#
includes/functions_display.php

#
#-----[ FIND ]------------------------------------------
#
?>

#
#-----[ BEFORE, ADD ]------------------------------------------
#
/**
* POST EXPIRE FUNCTION
* 
* Remove posts and topics that had reached expire time
*/
function post_expire_remove()
{
	global $db, $phpbb_root_path, $phpEx;

	$current_time = gmdate('U');

	$sql = 'SELECT topic_id, topic_expire_type, expire_forum_id, topic_status
					FROM '. TOPICS_TABLE . ' 
					WHERE topic_expire_time < '. $current_time . ' AND topic_expire_time <> 0';
	$result = $db->sql_query($sql);

	$topic_delete_ids = array();
	$topic_lock_ids = array();
	$topic_move_ids = array();
	$forum_move_ids = array();
	$topic_status = array();
		
	while ($row = $db->sql_fetchrow($result))
	{
		switch ($row['topic_expire_type']) 
		{
			case 0: 
				$topic_delete_ids[] = (int) $row['topic_id'];
			break;
			case 1:
				$topic_lock_ids[] = (int) $row['topic_id'];
				$topic_status[]= (bool) $row['topic_status'];
			break;
			case 2:
				$topic_move_ids[] = (int) $row['topic_id'];
				$forum_move_ids[] = (int) $row['expire_forum_id'];
			break;
			case 3:
				$topic_lock_ids[] = (int) $row['topic_id'];
				$topic_move_ids[] = (int) $row['topic_id'];
				$forum_move_ids[] = (int) $row['expire_forum_id'];
				$topic_status[]= (bool) $row['topic_status'];
			break;
			default:
		}
	}
	$db->sql_freeresult($result);

	if ($topic_delete_ids)
	{
		if (!function_exists('delete_topics'))
		{
			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
		}
		delete_topics('topic_id', $topic_delete_ids);
	}

	if ($topic_lock_ids)
	{
		for ($i=0; $i < sizeof($topic_lock_ids); $i++)
		{
			$sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
							SET t.topic_status = ' . (int) !$topic_status[$i] . ', t.topic_expire_time = 0, t.topic_expire_type = 0, p.post_expire_time = 0
							WHERE t.topic_id = ' . $topic_lock_ids[$i] . '
							AND t.topic_moved_id = 0
							AND t.topic_first_post_id = p.post_id';
			$db->sql_query($sql);
		}
	}

	if ($topic_move_ids)
	{
		if (!function_exists('move_topics'))
		{
			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
		}
		delete_posts('post_id', $row);
		for ($i=0; $i < sizeof($topic_move_ids); $i++)
		{
			move_topics($topic_move_ids[$i], $forum_move_ids[$i]);
		}
		
		$sql = 'UPDATE ' . TOPICS_TABLE . ' t, ' . POSTS_TABLE . ' p
						SET t.topic_expire_time = 0, t.topic_expire_type = 0, p.post_expire_time = 0
						WHERE ' . $db->sql_in_set('t.topic_id', $topic_move_ids) . '
						AND t.topic_moved_id = 0
						AND t.topic_first_post_id = p.post_id';
		$db->sql_query($sql);	
	}
	
	$sql = 'SELECT post_id 
					FROM '. POSTS_TABLE . ' 
					WHERE post_expire_time < '. $current_time . ' AND post_expire_time <> 0';
	$result = $db->sql_query($sql);
	$row = $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	if ($row) 
	{
		if (!function_exists('delete_posts'))
		{
			include($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
		}
		delete_posts('post_id', $row);
	}
	
	unset ($topic_delete_ids);
	unset ($topic_lock_ids);
	unset ($topic_move_ids);
	unset ($forum_move_ids);
	unset ($topic_status);
}

#
#-----[ OPEN ]------------------------------------------
#
includes/functions_posting.php

#
#-----[ FIND ]------------------------------------------
#
				'post_edit_locked'	=> $data['post_edit_locked']

#
#-----[ REPLACE WITH ]------------------------------------------
#
				'post_edit_locked'	=> $data['post_edit_locked'],
				'post_expire_time'	=> ($data['post_expire_time'] == -1) ? 0 : $current_time + $data['post_expire_time']

#
#-----[ FIND ]------------------------------------------
#
				'post_edit_locked'	=> $data['post_edit_locked'])

#
#-----[ REPLACE WITH ]------------------------------------------
#
				'post_edit_locked'	=> $data['post_edit_locked'],
				'post_expire_time'	=> ($data['post_expire_time'] == -1) ? 0 : $current_time + $data['post_expire_time'])

#
#-----[ FIND ]------------------------------------------
#
				'topic_attachment'			=> (!empty($data['attachment_data'])) ? 1 : 0,

#
#-----[ AFTER, ADD ]------------------------------------------
#
				'topic_expire_time'			=> ($auth->acl_get('u_post_expire_only')) ? 0 : (($data['post_expire_time'] == -1) ? 0 : $current_time + $data['post_expire_time']),
				'topic_expire_type'			=> isset($data['topic_expire_type']) ? $data['topic_expire_type'] : 0,
				'expire_forum_id'			=> isset($data['expire_forum_id']) ? $data['expire_forum_id'] : 0,

#
#-----[ FIND ]------------------------------------------
#
				'topic_attachment'			=> (!empty($data['attachment_data'])) ? 1 : (isset($data['topic_attachment']) ? $data['topic_attachment'] : 0),

#
#-----[ AFTER, ADD ]------------------------------------------
#
				'topic_expire_time'			=> ($auth->acl_get('u_post_expire_only')) ? 0 : (($data['post_expire_time'] == -1) ? 0 : $current_time + $data['post_expire_time']),
				'topic_expire_type'			=> isset($data['topic_expire_type']) ? $data['topic_expire_type'] : 0,
				'expire_forum_id'			=> isset($data['expire_forum_id']) ? $data['expire_forum_id'] : 0,
	
#
#-----[ OPEN ]------------------------------------------
#
language/en/acp/common.php

#
#-----[ FIND ]------------------------------------------
#
	'ACP_PHP_INFO'				=> 'PHP information',

#
#-----[ AFTER, ADD ]------------------------------------------
#
	'ACP_POST_EXPIRE_SETTINGS'	=> 'Post expire settings',

#
#-----[ FIND ]------------------------------------------
#
	'LOG_WORD_EDIT'			=> '<strong>Edited word censor</strong><br />» %s',

#
#-----[ AFTER, ADD ]------------------------------------------
#
	'LOG_POST_EXPIRE_SETTINGS_CHANGED' => '<strong>Post expire settings changed</strong>',

#
#-----[ OPEN ]------------------------------------------
#
styles/prosilver/template/overall_header.html

#
#-----[ FIND ]------------------------------------------
#
<script type="text/javascript" src="{T_TEMPLATE_PATH}/forum_fn.js"></script>

#
#-----[ AFTER, ADD ]------------------------------------------
#
<!-- IF S_DATEPICKER_NEEDED -->
<script type="text/javascript" src="{T_TEMPLATE_PATH}/datepicker/datetimepicker_css.js"></script>
<script type="text/javascript">
// <![CDATA[
var MonthName={MONTH_NAME};
var WeekDayName1={WEEK_DAY_NAME_1};
var WeekDayName2={WEEK_DAY_NAME_2};
var img_path="{T_TEMPLATE_PATH}/datepicker/images";
// ]]>
</script>
<!-- ENDIF -->

#
#-----[ OPEN ]------------------------------------------
#
styles/prosilver/template/posting_editor.html

#
#-----[ FIND ]------------------------------------------
#
</fieldset>

<!-- IF $EXTRA_POSTING_OPTIONS eq 1 -->

#
#-----[ BEFORE, ADD ]------------------------------------------
#
	<!-- IF not S_PRIVMSGS and not S_SHOW_DRAFTS and S_POST_EXPIRE -->
		<!-- IF S_TOPIC_EXPIRE_TYPE -->
			<dl>
				<dt style="width:300px;"><label>{L_TOPIC_EXPIRE_TYPE}:</label><br /><span>{L_TOPIC_EXPIRE_TYPE_EXPLAIN}</span></dt>
				<dd>{S_TOPIC_EXPIRE_TYPE}</dd>
			</dl>
		<!-- ENDIF -->
		<!-- IF S_EXPIRE_AFTER_SHOW -->
		<dl>
			<dt style="width:300px;"><label for="post_expire_time">{L_POST_EXPIRE_TIME}:</label><br /><span>{L_POST_EXPIRE_TIME_EXPLAIN}</span></dt>
			<dd><select name="post_expire_time" id="post_expire_time">{S_POST_EXPIRE_SELECT}</select></dd>
		</dl>
		<!-- ENDIF -->
		<!-- IF S_EXPIRE_EXACT_SHOW -->
		<dl>
			<dt><label for="date_picker">{L_POST_EXPIRE_DATE}:</label><br /><span>{L_POST_EXPIRE_DATE_EXPLAIN}</span></dt>
			<dd>
				<input class="inputbox" name="date_picker" id="date_picker" value="{S_DATE_PICKER}" style="width:130px" /><a href="javascript:NewCssCal('date_picker','ddmmyyyy', true, 24)"><img src="{T_TEMPLATE_PATH}/datepicker/images/cal.gif" width="16" height="16" alt="" title="" /></a>
			</dd>
		</dl> 
		<!-- ENDIF -->
		<!-- IF S_EXPIRE_FORUM_OPTIONS -->
			<dl>
				<dt style="width:300px;"><label for="expire_forum_id">{L_TOPIC_EXPIRE_SELECT_FORUM}:</label><br /><span>{L_TOPIC_EXPIRE_SELECT_FORUM_EXPLAIN}</span></dt>
				<dd><select name="expire_forum_id" id="expire_forum_id">{S_EXPIRE_FORUM_OPTIONS}</select></dd>
			</dl>
		<!-- ENDIF -->
	<!-- ENDIF -->

#
#-----[ OPEN ]------------------------------------------
#
styles/prosilver/template/viewforum_body.html

#
#-----[ FIND ]------------------------------------------
#
		<li class="row<!-- IF topicrow.S_ROW_COUNT is even --> bg1<!-- ELSE --> bg2<!-- ENDIF --><!-- IF topicrow.S_POST_ANNOUNCE --> announce<!-- ENDIF --><!-- IF topicrow.S_POST_STICKY --> sticky<!-- ENDIF --><!-- IF topicrow.S_TOPIC_REPORTED --> reported<!-- ENDIF -->">

#
#-----[ REPLACE WITH ]------------------------------------------
#
		<li class="row<!-- IF topicrow.S_CHANGE_COLOUR --> bg3<!-- ELSEIF topicrow.S_ROW_COUNT is even --> bg1<!-- ELSE --> bg2<!-- ENDIF --><!-- IF topicrow.S_POST_ANNOUNCE --> announce<!-- ENDIF --><!-- IF topicrow.S_POST_STICKY --> sticky<!-- ENDIF --><!-- IF topicrow.S_TOPIC_REPORTED --> reported<!-- ENDIF -->">

#
#-----[ FIND ]------------------------------------------
#
					<!-- IF topicrow.ATTACH_ICON_IMG -->{topicrow.ATTACH_ICON_IMG} <!-- ENDIF -->{L_POST_BY_AUTHOR} {topicrow.TOPIC_AUTHOR_FULL} &raquo; {topicrow.FIRST_POST_TIME}

#
#-----[ AFTER, ADD ]------------------------------------------
#
					<br /><!-- IF topicrow.TOPIC_EXPIRE --><strong>{L_TOPIC_WILL_EXPIRE} [{topicrow.L_TOPIC_EXPIRE_TYPE}]:</strong> {topicrow.TOPIC_EXPIRE}<!-- ENDIF -->

#
#-----[ OPEN ]------------------------------------------
#
styles/prosilver/template/viewtopic_body.html

#
#-----[ FIND ]------------------------------------------
#
	<div id="p{postrow.POST_ID}" class="post <!-- IF postrow.S_ROW_COUNT is odd -->bg1<!-- ELSE -->bg2<!-- ENDIF --><!-- IF postrow.S_UNREAD_POST --> unreadpost<!-- ENDIF --><!-- IF postrow.S_POST_REPORTED --> reported<!-- ENDIF --><!-- IF postrow.S_ONLINE and not postrow.S_IGNORE_POST --> online<!-- ENDIF -->">

#
#-----[ REPLACE WITH ]------------------------------------------
#
	<div id="p{postrow.POST_ID}" class="post <!-- IF postrow.S_CHANGE_COLOUR -->bg3<!-- ELSEIF postrow.S_ROW_COUNT is odd -->bg1<!-- ELSE -->bg2<!-- ENDIF --><!-- IF postrow.S_UNREAD_POST --> unreadpost<!-- ENDIF --><!-- IF postrow.S_POST_REPORTED --> reported<!-- ENDIF --><!-- IF postrow.S_ONLINE and not postrow.S_IGNORE_POST --> online<!-- ENDIF -->">
	 
#
#-----[ FIND ]------------------------------------------
#
			<p class="author"><!-- IF S_IS_BOT -->{postrow.MINI_POST_IMG}<!-- ELSE --><a href="{postrow.U_MINI_POST}">{postrow.MINI_POST_IMG}</a><!-- ENDIF -->{L_POST_BY_AUTHOR} <strong>{postrow.POST_AUTHOR_FULL}</strong> &raquo; {postrow.POST_DATE} </p>

#
#-----[ IN-LINE FIND ]------------------------------------------
#
{postrow.POST_DATE}

#
#-----[ IN-LINE AFTER, ADD ]------------------------------------------
#
 <!-- IF postrow.POST_EXPIRE -->&nbsp;&nbsp;&nbsp;<strong>{L_POST_WILL_EXPIRE}:</strong> {postrow.POST_EXPIRE}<!-- ENDIF -->

#
#-----[ DIY INSTRUCTIONS ]------------------------------------------
#
Run install/index.php and delete it including install folder.
Post expire permissions are disabled by default. You have to set them in ACP. These are user permissions.
Do not set 'yes' to all permissions. Please look at contrib/readme.txt for details about permissions and config settings. 

#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM
