############################################################## 
## MOD Title:  BPM Mod (AK. Birthday message mod) - EM Friendly
## MOD Author: Duvelske < Duvelske@planet.nl > (Richard Wagtmans) http://www.vitrax.vze.com/ 
## MOD Author, Secondary: Tuxman < tuxman@sumpfkuh.de >
## MOD Description:    With this mod you can sendt an pm or an email
##			to the user who has his birthday. The message you
##			can define in the admin panel with some functions
##			like with the wpm mod. The old pop-up wil appear,
##			but you can shut it down in the bpm panel as well as
##			in the board configuration.			
##
## MOD Version: 1.0.2
## Compatibility: You must have the birthday mod of mods.db9.dk installed
##		  In order to let this mod work.
##
## Notes: This MOD includes Tuxman's Birthday Mail Extension, available at
##        http://www.phpbb.de/viewtopic.php?t=53274 (German)
##        http://www.phpbb.com/phpBB/viewtopic.php?p=1077492#1077492 (English)
##	  This one is also edited to go with the norms of the bpm mod (by Duvelske)
## 
## Installation Level: Simple
## Installation Time:  10 Minutes
##
## Files To Edit: 2
##      index.php
##	includes/functions_post.php
##	admin/admin_board.php
##	templates/subSilver/admin/board_config_body.tpl
##
## Included Files: 5
##      language/lang_english/email/birthday.tpl
##	admin_bpm.php
##	bpm_body.tpl
##	bpm_db_install.php
##	uninstallbpm.php
##
##############################################################
## Author Notes:
## 1. You also must have the wpm mod installed! Otherwise it doesn't work!
##
## 2.Please Run before you mod the files the bpm_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
##############################################################
## MOD History:
##	1.0.2 - Let the mod sendt an pm or email when some has his birthday instead of when someone
##		who has his/hers birthday logs in.
##	      - Made an activate/deactivate pop-up button for in the bpm admin panel.
##	      - Added some suggestion code for removal of the pop-up button code in the admin panel
##		for your board configuration
##
##	1.0.1 - Fixed the sendt options.
##	      - Modified some errors.
##
##	1.0.0 - Got approval for adding the mail section to the bpm mod.
##	      - Fixed and changed some of the functions of the mail section to work with the bpm standards.
##	      - First release of the bpm mod
##
##	0.1.1 - Fixed the bpm mod now it sends a good pm
##
############################################################## 
## 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 
##############################################################
#
#-----[ DIY INSTRUCTIONS ]------------------------------------------------
#
Thank you for using my mod. If you have any problems with it 
Visit my site: http://www.vitrax.vze.com and ask. I will help you out!
You can also visit: http://www.phpbbhacks.com

After install:
In the admin panel stands after the installation:
Sendt by: Anonymous
This you have to change. Otherwise you cannot change the bpm private message!
This for the pm it sends
#
#-----[ COPY ]------------------------------------------------
#
copy root/english/birthday.tpl to language/lang_english/email/birthday.tpl
copy root/admin_bpm.php to admin/admin_bpm.php
copy root/bpm_body.tpl to templates/subSilver/admin/bpm_body.tpl
copy root/english/lang_admin_bpm.php to language/lang_english/lang_admin_bpm.php
#
#-----[ SQL ]-------------------------------------------------
# Note: phpbb_ must be your table prefix otherwise
# 	replace it with you prefix. Or run the bpm_db_update.php file
INSERT INTO phpbb_wpm VALUES ('bpm_version', '1.0.2');
INSERT INTO phpbb_wpm VALUES ('bpm_username', 'Anonymous');
INSERT INTO phpbb_wpm VALUES ('bpm_userid', '-1');
INSERT INTO phpbb_wpm VALUES ('active_bpm', '0');
INSERT INTO phpbb_wpm VALUES ('active_bpmmail', '1');
INSERT INTO phpbb_wpm VALUES ('bpm_subject', 'Happy birthday [username]!');
INSERT INTO phpbb_wpm VALUES ('bpm_message', 'Happy birthday [username] with your [age] birthday!');

# 
#-----[ OPEN ]------------------------------------------ 
#
includes/functions_post.php
# 
#-----[ FIND ]------------------------------------------ 
#
//
// This function will prepare a posted message for
# 
#-----[ BEFORE, ADD ]------------------------------------------ 
#
// start bpm mod by Duvelske (http://www.vitrax.vze.com)
function bpm_send_pm($user_to_id, $bpm_subject, $bpm_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, 'Tried obtaining data for a non-existent user', '', __LINE__, __FILE__, $sql);
	}
	$usertodata = $db->sql_fetchrow($result);

	// prepare bpm message
	$bbcode_uid = make_bbcode_uid();
	$bpm_message = str_replace("'", "''", $bpm_message);

	if(empty($bpm_message))
	{
		$bpm_message = "Happy Birthday.";
	}
	$bpm_message = prepare_message(trim($bpm_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("\'", "''", $bpm_subject) . "', " . $swpm_config['bpm_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("\'", "''", $bpm_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 bpm mod
#
#-----[ OPEN ]------------------------------------------------
#
index.php
#
#-----[ FIND ]------------------------------------------------
#
$birthday_today_list .= 
# 
#-----[ BEFORE, ADD ]------------------------------------------ 
#
				// START Birthday Mail Extension & bpm mod
				{
					$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_bpm'])
					{
						$bpm_subject = str_replace("[username]", $username, $swpm_config['bpm_subject']);
						$bpm_subject = str_replace("[age]", $user_age, $bpm_subject);
						$bpm_subject = str_replace("[sitename]", $board_config['sitename'], $bpm_subject);
						$bpm_message = str_replace("[username]", $username, $swpm_config['bpm_message']);
						$bpm_message = str_replace("[age]", $user_age, $bpm_message);
						$bpm_message = str_replace("[sitename]", $board_config['sitename'], $bpm_message);

						bpm_send_pm($user_id, $bpm_subject, $bpm_message, 1);
						}
					}
					if($swpm_config['active_bpmmail'])
					{
						$sql = "SELECT * FROM " . USERS_TABLE . " WHERE user_id = " . $userdata['user_id'];
						if ( !($result = $db->sql_query($sql)) )
						{
							message_die(GENERAL_ERROR, "", "", __LINE__, __FILE__, $sql);
						}
						while( $row = $db->sql_fetchrow($result) )
						{
							$bd_mail = intval($row['user_email']);
						}
						$sql = "SELECT * FROM " . USERS_TABLE . " WHERE user_id = $user_id";
						if ( !($result = $db->sql_query($sql)) )
						{
							message_die(GENERAL_ERROR, "", "", __LINE__, __FILE__, $sql);
						}
						while ($row = $db->sql_fetchrow($result))
							$emailer->from($board_config['board_email']); 
							$emailer->replyto($board_config['board_email']); 
							$emailer->email_address(trim($row['user_email'])); 
							$emailer->use_template("birthday", $row['user_lang']);
							$emailer->set_subject($bpm_subject);
							$emailer->set_message($bpm_message);
							$bpm_subject = str_replace("[username]", $username, $swpm_config['bpm_subject']);
							$bpm_subject = str_replace("[age]", $user_age, $bpm_subject);
							$bpm_subject = str_replace("[sitename]", $board_config['sitename'], $bpm_subject);
							$bpm_message = str_replace("[username]", $username, $swpm_config['bpm_message']);
							$bpm_message = str_replace("[age]", $user_age, $bpm_message);
							$bpm_message = str_replace("[sitename]", $board_config['sitename'], $bpm_message);
							$emailer->assign_vars(array(
								'MESSAGE' => str_replace($bpm_message),
								'USERNAME' => preg_replace($unhtml_specialchars_match, $unhtml_specialchars_replace, substr(str_replace("\'", "'", $row['username']), 0, 25)),
								'EMAIL_SIG' => str_replace('<br />', "\n", "-- \n" . $board_config['board_email_sig']))
								);
							$emailer->send();
							$emailer->reset();
					}
#
#-----[ OPEN ]------------------------------------------------
#
admin/admin_board.php
#
#-----[ FIND ]------------------------------------------------
#
$birthday_greeting_yes =
$birthday_greeting_no =
#
#-----[ REPLACE WITH ]------------------------------------------------
#
//
#
#-----[ FIND ]------------------------------------------------
#
"BIRTHDAY_GREETING_YES" =>
"BIRTHDAY_GREETING_NO" =>
#
#-----[ REPLACE WITH ]------------------------------------------------
#
//
#
#-----[ OPEN ]------------------------------------------------
#
templates/subSilver/admin/board_config_body.tpl
#
#-----[ FIND,DELETE ]------------------------------------------------
#
<tr>
{L_ENABLE_BIRTHDAY_GREETING}
{BIRTHDAY_GREETING_YES}
</tr>
#
#-----[ REPLACE WITH ]------------------------------------------------
#
<i></i>
#
#-----[ OPEN ]------------------------------------------------
#
language/lang_english/lang_admin.php
#
#-----[ FIND,DELETE ]------------------------------------------------
#
$lang['Enable_birthday_greeting'] =
$lang['Birthday_greeting_expain'] =
#
#-----[ REPLACE WITH ]------------------------------------------------
#
//
#
#-----[ SAVE/CLOSE ALL FILES ]--------------------------
#
# EoM