############################################################## 
## MOD Title: WPM on nukeportal
## 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.8
## 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
##
## Included Files: 9
## 		   admin_wpm.php
## 		   wpm_body.tpl
## 		   lang_admin_wpm.php (English,Dutch & German)
##		   wpm_db_update.php
##		   wpm_db_install.php
##		   uninstallwpm.php
##		   
##
## MOD History:
##	1.0.8	-  Changed some of the code. For better working with some new mods and older ones.
##		-  Fixed some little typo's in the install.txt file.
##
##	1.0.7	-  Fixed the bug with the pop-up when receving a wpm
##		-  Because the how-to working of the wpm mod changed in the 1.0.6
##		   version some bugs where solved.
##
##	1.0.6	-  Fixed some errors in admin_wpm.php
##		-  Fixed some typo's in the lang files
##		-  Made an wpmuninstall file
##		-  Replaced several txt files into one called 
##		   READMEBEFOREINSTALL.txt
##
##	1.0.5	-  Changed the way the mod works. Now it should
##		   also be compatible most portal and premodded versions
##		   of phpbb
##		-  Changed the place in the acp menu. Now it is with general
##		-  Fixed a little pop up error (didn't show up)
##		-  Changed some minor things in the lang files
##
##	1.0.4	-  ' Bug fixed
##		-  Some minor changes to admin_wpm.php
##		-  Changes to the install script
##
##	1.0.3	-  Quote fix (thnx by Elo)
##		-  Lang Files fix. (forgot some entries)
##		-  Admin_wpm.php solved some bugs
##
##	1.0.2	-  Added some changes in the working of the mod
##		-  Fixed some minor bugs with other mods
##		-  New function added with the Name wich sendt the PM
##
##
##	1.0.1	-  Added some code to the install file.
##		-  Some minor fixes in the admin_wpm.php file &
##		   in the wpm_body.tpl
##	
##	1.0.0	-  initial release
##	     	-  An added request for disabling the mod
##	     	-  EM Ready
##
##	0.9.0	-  initial BETA release
##	
##
############################################################## 
## 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 ]-------------------------------------------------
#
CREATE TABLE nuke_wpm (name varchar(255) NOT NULL default '', value text NOT NULL) TYPE=MyISAM;
INSERT INTO nuke_wpm VALUES ('wpm_version', '1.0.8');
INSERT INTO nuke_wpm VALUES ('active_wpm', '1');
INSERT INTO nuke_wpm VALUES ('wpm_username', 'Anonymous');
INSERT INTO nuke_wpm VALUES ('wpm_userid', '-1');
INSERT INTO nuke_wpm VALUES ('wpm_subject', 'Welcome to [sitename]!');
INSERT INTO nuke_wpm VALUES ('wpm_message', 'Hi');

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

# 
#-----[ FIND ]------------------------------------------ 
#
define('VOTE_USERS_TABLE', $prefix.'_bbvote_voters');

# 
#-----[ AFTER, ADD ]------------------------------------------ 
#
define('WPM', $prefix.'_wpm');
# 
#-----[ OPEN ]------------------------------------------ 
#
includes/functions_post.php

# 
#-----[ FIND ]------------------------------------------ 
#
//
// This function will prepare a posted message for
// entry into the database.
//

# 
#-----[ BEFORE, ADD ]------------------------------------------ 
#
// 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, $swpm_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, $lang['non_existing_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 = $lang['No_entry_wpm'];
	}
	$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, $lang['not_delete_pm'], '', __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) . "', " . $swpm_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, $lang['no_sent_pm_insert'], "", __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, $lang['no_sent_pm_insert'], "", __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, $lang['no_sent_pm_insert'], '', __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
# 
#-----[ OPEN ]------------------------------------------ 
#
includes/usercp_register.php

# 
#-----[ FIND ]------------------------------------------ 
#
			message_die(GENERAL_MESSAGE, $message);
		} // if mode == register
	}
} // End of submit

# 
#-----[ BEFORE, ADD ]------------------------------------------ 
#
						//
						// 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))
								{
									$swpm_config[$row['name']] = $row['value'];
								}
							}
							if($swpm_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, $swpm_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, $swpm_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
						//
# 
#-----[ OPEN ]------------------------------------------ 
#
language/lang_english/lang_main.php

# 
#-----[ FIND ]------------------------------------------ 
#
?>
# 
#-----[ BEFORE, ADD ]------------------------------------------ 
# 
$lang['not_delete_pm'] = 'Could not delete your oldest privmsgs';
$lang['non_existing_user'] = 'Tried obtaining data for a non-existent user';
$lang['No_entry_wpm'] = 'Thank you for register at [sitename]!';
$lang['no_sent_pm_insert'] = 'Could not insert private message sent info!';
#
#-----[ COPY ]------------------------------------------
#
copy root/admin_wpm.php to admin/admin_wpm.php
copy root/wpm_body.tpl to templates/subSilver/admin/wpm_body.tpl
copy root/english/lang_admin_wpm.php to language/lang_english/lang_admin_wpm.php
copy root/dutch/lang_admin_wpm.php to language/lang_dutch/lang_admin_wpm.php
copy root/german/lang_admin_wpm.php to language/lang_german/lang_admin_wpm.php
copy root/french/lang_admin_wpm.php to language/lang_french/lang_admin_wpm.php
copy root/russian/lang_admin_wpm.php to language/lang_russian/lang_admin_wpm.php

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

# EoM                                                                                                                                                                                                                                                             