############################################################## 
## MOD Title: WPM uninstall
## MOD Author: Duvelske < Duvelske@planet.nl > (Richard Wagtmans) http://www.vitrax.opweb.nl
## MOD Description: This mod gives you in the admin section a panel to control a welcome message
## on registration. You can disable it with the admin panel and type the message send by the mod.
## MOD Support site: www.vitrax.opweb.nl, www.phpbb.nl, www.phpbb2.de, www.phpbb.com
## MOD Version: 	1.0.7
## MOD Compatibility:	2.0.4 ,2.0.5 ,2.0.6, 2.0.7, 2.0.8
##############################################################
## Author Notes:
## Please Run before you mod the files the wpm_db_update.php file in the root of your forum
## Also if you run an older version of this mod read the Update from 105 previous versions.txt
## for further instructions
##############################################################
## Installation Level: easy
## Installation Time: ~3 minutes
##
## Files To Edit: includes/usercp_register.php
##		  includes/functions_post.php
##		  includes/constants.php
##
##
############################################################## 
## This MOD is released under the GPL License. 
## Intellectual Property is retained by the MOD Author(s) listed above 
############################################################## 
## For Security Purposes, Please Check: http://www.vitrax.vze.com for the 
## latest version of this MOD. Downloading this MOD from other sites could cause malicious code 
## to enter into your phpBB Forum. As such, Vitrax will not offer support for MOD's not offered 
## in our MOD support forum 
############################################################## 

#
#-----[ SQL ]-------------------------------------------------
# Note: phpbb_ must be your table prefix otherwise
# 	replace it with you prefix. Or run the uninstallwpm.php file
DROP TABLE phpbb_wpm (name varchar(255) NOT NULL default '', value text NOT NULL) TYPE=MyISAM;


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

# 
#-----[ FIND ]------------------------------------------ 
# 
define('WPM', $table_prefix.'wpm');

# 
#-----[ REPLACE WITH ]------------------------------------------------ 
# 
//here was an insert of the wpm mod 1.0.7 by Duvelske
# 
#-----[ OPEN ]------------------------------------------ 
#
includes/functions_post.php

# 
#-----[ FIND ]------------------------------------------ 
#
// start wpm mod by Duvelske (http://www.vitrax.vze.com)
function wpm_send_pm($user_to_id, $wpm_subject, $wpm_message, $send_email)
{
	global $board_config, $bot_config, $lang, $db, $phpbb_root_path, $phpEx;

	$sql = "SELECT *
		FROM " . USERS_TABLE . " 
		WHERE user_id = " . $user_to_id . "
		AND user_id <> " . ANONYMOUS;
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Tried obtaining data for a non-existent user', '', __LINE__, __FILE__, $sql);
	}
	$usertodata = $db->sql_fetchrow($result);

	// prepare wpm message
	$bbcode_uid = make_bbcode_uid();
	$wpm_message = str_replace("'", "''", $wpm_message);

	if(empty($wpm_message))
	{
		$wpm_message = "Thank you for registering.";
	}
	$wpm_message = prepare_message(trim($wpm_message), 0, 1, 1, $bbcode_uid);

	$msg_time = time();

	// Do inbox limit stuff
	$sql = "SELECT COUNT(privmsgs_id) AS inbox_items, MIN(privmsgs_date) AS oldest_post_time 
		FROM " . PRIVMSGS_TABLE . " 
		WHERE ( privmsgs_type = " . PRIVMSGS_NEW_MAIL . " 
			OR privmsgs_type = " . PRIVMSGS_READ_MAIL . "  
			OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . " ) 
			AND privmsgs_to_userid = " . $usertodata['user_id'];
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_MESSAGE, $lang['No_such_user']);
	}

	$sql_priority = ( SQL_LAYER == 'mysql' ) ? 'LOW_PRIORITY' : '';

	if ( $inbox_info = $db->sql_fetchrow($result) )
	{
		if ( $inbox_info['inbox_items'] >= $board_config['max_inbox_privmsgs'] )
		{
			$sql = "DELETE $sql_priority FROM " . PRIVMSGS_TABLE . " 
				WHERE ( privmsgs_type = " . PRIVMSGS_NEW_MAIL . " 
					OR privmsgs_type = " . PRIVMSGS_READ_MAIL . " 
					OR privmsgs_type = " . PRIVMSGS_UNREAD_MAIL . "  ) 
					AND privmsgs_date = " . $inbox_info['oldest_post_time'] . " 
					AND privmsgs_to_userid = " . $usertodata['user_id'];
			if ( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, 'Could not delete your oldest privmsgs', '', __LINE__, __FILE__, $sql);
			}
		}
	}

	$sql_info = "INSERT INTO " . PRIVMSGS_TABLE . " (privmsgs_type, privmsgs_subject, privmsgs_from_userid, privmsgs_to_userid, privmsgs_date, privmsgs_ip, privmsgs_enable_html, privmsgs_enable_bbcode, privmsgs_enable_smilies, privmsgs_attach_sig)
		VALUES (" . PRIVMSGS_NEW_MAIL . ", '" . str_replace("\'", "''", $wpm_subject) . "', " . $bot_config['wpm_userid'] . ", " . $usertodata['user_id'] . ", $msg_time, '$user_ip', 0, 1, 1, 1)";

	if ( !($result = $db->sql_query($sql_info, BEGIN_TRANSACTION)) )
	{
		message_die(GENERAL_ERROR, "Could not insert private message sent info.", "", __LINE__, __FILE__, $sql_info);
	}

	$privmsg_sent_id = $db->sql_nextid();

	$sql = "INSERT INTO " . PRIVMSGS_TEXT_TABLE . " (privmsgs_text_id, privmsgs_bbcode_uid, privmsgs_text)
		VALUES ($privmsg_sent_id, '" . $bbcode_uid . "', '" . str_replace("\'", "''", $wpm_message) . "')";

	if ( !$db->sql_query($sql, END_TRANSACTION) )
	{
		message_die(GENERAL_ERROR, "Could not insert/update private message sent text.", "", __LINE__, __FILE__, $sql_info);
	}

	// Add to the users new pm counter
	$sql = "UPDATE " . USERS_TABLE . "
		SET user_new_privmsg = user_new_privmsg + 1, user_last_privmsg = '9999999999'
		WHERE user_id = " . $usertodata['user_id']; 
	if ( !$status = $db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, 'Could not update private message new/read status for user', '', __LINE__, __FILE__, $sql);
	}

	if ( $send_email && $usertodata['user_notify_pm'] && !empty($usertodata['user_email']) && $usertodata['user_active'] )
	{
		$email_headers = 'From: ' . $board_config['board_email'] . "\nReturn-Path: " . $board_config['board_email'] . "\r\n";

		$script_name = preg_replace('/^\/?(.*?)\/?$/', "\\1", trim($board_config['script_path']));
		$script_name = ( $script_name != '' ) ? $script_name . '/privmsg.'.$phpEx : 'privmsg.'.$phpEx;
		$server_name = trim($board_config['server_name']);
		$server_protocol = ( $board_config['cookie_secure'] ) ? 'https://' : 'http://';
		$server_port = ( $board_config['server_port'] <> 80 ) ? ':' . trim($board_config['server_port']) . '/' : '/';

		include($phpbb_root_path . 'includes/emailer.'.$phpEx);
		$emailer = new emailer($board_config['smtp_delivery']);
			
		$emailer->use_template('privmsg_notify', $usertodata['user_lang']);
		$emailer->extra_headers($email_headers);
		$emailer->email_address($usertodata['user_email']);
		$emailer->set_subject(); //$lang['Notification_subject']
			
		$emailer->assign_vars(array(
			'USERNAME' => $usertodata['username'], 
			'SITENAME' => $board_config['sitename'],
			'EMAIL_SIG' => str_replace('<br />', "\n", "-- \n" . $board_config['board_email_sig']), 

			'U_INBOX' => $server_protocol . $server_name . $server_port . $script_name . '?folder=inbox')
		);

		$emailer->send();
		$emailer->reset();
	}

	return;
}
// end wpm mod
# 
#-----[ REPLACE WITH ]------------------------------------------------ 
# 
//here was an insert of the wpm mod 1.0.7 by Duvelske
# 
#-----[ OPEN ]------------------------------------------ 
#
includes/usercp_register.php

# 
#-----[ FIND ]------------------------------------------ 
#
			//
			// START wpm mod by Duvelske (http://www.vitrax.vze.com)
			{
				$sql = "SELECT *
					FROM " . WPM;
				if(!$result = $db->sql_query($sql))
				{
					message_die(GENERAL_ERROR, "", "", __LINE__, __FILE__, $sql);
				}
				else
				{
					while($row = $db->sql_fetchrow($result))
					{
						$bot_config[$row['name']] = $row['value'];
					}
				}
				if($bot_config['active_wpm'])
				{
				// Just a couple of replaces for better customisation on a per user basis
				// no point making a special function since there are so few needed

				$wpm_subject = str_replace("[username]", $username, $bot_config['wpm_subject']);
				$wpm_subject = str_replace("[user_id]", $user_id, $wpm_subject);
				$wpm_subject = str_replace("[sitename]", $board_config['sitename'], $wpm_subject);

				$wpm_message = str_replace("[username]", $username, $bot_config['wpm_message']);
				$wpm_message = str_replace("[user_id]", $user_id, $wpm_message);
				$wpm_message = str_replace("[sitename]", $board_config['sitename'], $wpm_message);

				// Will not send a pm notification via email (already sent the standard welcome email)
				// Simply change the 0 to a 1 to turn it on
				wpm_send_pm($user_id, $wpm_subject, $wpm_message, 0);
				}
			}
			// END wpm mod
			//
# 
#-----[ REPLACE WITH ]------------------------------------------------ 
# 
//here was an insert of the wpm mod 1.0.7 by Duvelske
#
#-----[ DELETE ]------------------------------------------
#
delete admin/admin_wpm.php
delete templates/subSilver/admin/wpm_body.tpl
delete language/lang_english/lang_admin_wpm.php
delete language/lang_dutch/lang_admin_wpm.php
delete language/lang_german/lang_admin_wpm.php

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

# EoM                                                                                                                                                                                                                                                             