<?php
// +-------------------------------------------------------------+
// | HiveMail™ version 1.3.1
// +-------------------------------------------------------------+
// | HIVEMAIL™ IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than Hivemail or if you have otherwise received
// | this software from someone who is not a representative of
// | this organization you are involved in an illegal activity.
// +-------------------------------------------------------------+
// | $RCSfile: hivevb3_install.php,v $ - $Revision: 1.13 $
// | $Date: 2004/08/10 16:47:03 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);
require('./global.php');
require_once('./includes/adminfunctions.php');
require_once('./includes/adminfunctions_language.php');
require_once('./includes/adminfunctions_template.php');
$_intver = '3.0.3';
$_forvbs = array('3.0.0', '3.0.1', '3.0.2', '3.0.3');

// ############################################################################
// Define templates & phrases
$_phrasegroups['error'] = 1000; // error messages
$_phrasegroups['redirect'] = 2000; // redirect messages
$_phrasegroups['register'] = 39;
$_phrasegroups['cpug'] = 34; // control panel usergroup
$_phrasegroups['setting'] = 5000; // settings & setting groups
$_phrasegroups['usertools'] = 11; // user tools (global)
$_phrasegroups['global'] = 1;

$_phrases = array(
	'3.0.1' => array(
		'hivemail_nameillegal' => array(
			'type' => 'error',
			'text' => 'The email address you requested was not valid. Your username may only contain alphanumeric characters, underscores (_) and dots (.), must start with a letter and must be a minimum length of 2 characters.',
		),
		'hivemail_nametaken' => array(
			'type' => 'error',
			'text' => 'We\'re sorry, email address you requested is already used by another member. Please go back and enter a different address.',
		),
		'hivemail_thankyou' => array(
			'type' => 'redirect',
			'text' => 'Thank you for signing up for our mail service, your password is the same one you use for this account.',
		),
		'hivemail_choose_username' => array(
			'type' => 'register',
			'text' => 'Enter the username you wish to use for your email account. It will be the part before the @ sign in your new email address.<br />Then, choose which domain name you wish to register for. (You can always use any domain name, but the domain name you select now will be your default domain name.)<br />The password for your email account will be the same as your forum account.',
		),
		'hivemail_get_x_email_account' => array(
			'type' => 'register',
			'text' => 'Get your own {1} email account?',
		),
		'hivemail_please_choose_account_name' => array(
			'type' => 'register',
			'text' => 'If you choose to register for an email account, you must enter a username below.',
		),
		'hivemail_register_for_account' => array(
			'type' => 'register',
			'text' => 'Register for an email account',
		),
		'hivemail_signup_for_email_account' => array(
			'type' => 'register',
			'text' => 'Sign Up for an Email Account',
		),
		'hivemail_username_and_domain_selection' => array(
			'type' => 'register',
			'text' => 'Username and Domain Selection',
		),
	),
	'3.0.2' => array(
		'hivemail_nameillegal' => array(
			'type' => 'error',
			'text' => 'The email address you requested was not valid. Your username may only contain alphanumeric characters, underscores (_) and dots (.), must start with a letter and must be a minimum length of 2 characters.',
		),
		'hivemail_nametaken' => array(
			'type' => 'error',
			'text' => 'We\'re sorry, the email address you requested is already used by another member. Please go back and enter a different address.',
		),
		'hivemail_thankyou' => array(
			'type' => 'redirect',
			'text' => 'Thank you for signing up for our mail service, your password is the same one you use for this account.',
		),
		'hivemail_choose_username' => array(
			'type' => 'register',
			'text' => 'Enter the username you wish to use for your email account. It will be the part before the @ sign in your new email address.<br />Then, choose which domain name you wish to register for. (You can always use any domain name, but the domain name you select now will be your default domain name.)<br />The password for your email account will be the same as your forum account.',
		),
		'hivemail_get_email_account' => array(
			'type' => 'register',
			'text' => 'Get an email account.',
		),
		'hivemail_like_email_account' => array(
			'type' => 'register',
			'text' => 'If you choose to register for an email account, you must enter a username below.',
		),
		'hivemail_register_for_account' => array(
			'type' => 'register',
			'text' => 'Register for an email account',
		),
		'hivemail_signup_for_email_account' => array(
			'type' => 'register',
			'text' => 'Sign Up for an Email Account',
		),
		'hivemail_username_and_domain_selection' => array(
			'type' => 'register',
			'text' => 'Username and Domain Selection',
		),
		'hivemail_same_username' => array(
			'type' => 'register',
			'text' => 'Your username and password for your email account will be the same as the information you entered above for use on this forum.',
		),
		'hivemail_register_information' => array(
			'type' => 'register',
			'text' => 'Email Registration Information',
		),
		'hivemail_permissions' => array(
			'type' => 'cpug',
			'text' => 'HiveMail Integration Permissions',
		),
		'hivemail_can_check_new_mail' => array(
			'type' => 'cpug',
			'text' => 'Can have system check for new email',
		),
		'hivemail_can_have_account' => array(
			'type' => 'cpug',
			'text' => 'Can get HiveMail account (after registration)',
		),
		'hivemail_can_view_new_mail' => array(
			'type' => 'cpug',
			'text' => 'Can view list of new emails in User CP'
		),
		'hivemail_get_account' => array(
			'type' => 'usertools',
			'text' => 'Get Email Account',
		),
		'hivemail_unread_emails' => array(
			'type' => 'usertools',
			'text' => 'Unread Emails',
		),
		'hivemail_from' => array(
			'type' => 'usertools',
			'text' => 'From',
		),
		'hivemail_date_received' => array(
			'type' => 'usertools',
			'text' => 'Date Received',
		),
		'hivemail_no_unread' => array(
			'type' => 'usertools',
			'text' => 'No Unread Emails Found',
		),
		'hivemail_goto_account' => array(
			'type' => 'usertools',
			'text' => 'Go to Email Account',
		),
		'hivemail_click_read_email' => array(
			'type' => 'usertools',
			'text' => 'Click to read email',
		),
		'hivemail_click_send_email' => array(
			'type' => 'usertools',
			'text' => 'Click to send email',
		),
		'hivemail_new_messages' => array(
			'type' => 'global',
			'text' => 'new messages'
		),
		'hivemail_unread_messages' => array(
			'type' => 'global',
			'text' => 'unread messages'
		),
		'settinggroup_hivemail' => array(
			'type' => 'setting',
			'text' => 'HiveMail Integration Settings',
		),
		'setting_hive_filepath_title' => array(
			'type' => 'setting',
			'text' => 'Path to HiveMail files',
		),
		'setting_hive_filepath_desc' => array(
			'type' => 'setting',
			'text' => 'This is the path to HiveMail files on your server. This must be an absolute path (e.g. /home/username/public_html/hivemail).',
		),
		'setting_hive_checktime_title' => array(
			'type' => 'setting',
			'text' => 'Time Between Mail Checks',
		),
		'setting_hive_checktime_desc' => array(
			'type' => 'setting',
			'text' => 'Enter the number of seconds that must elapse between checks for new mail for a particular user. Each time the system checks for new mail in HiveMail for a user, it uses two queries, so the results are cached for this many seconds before updating.',
		),
		'setting_hive_enabled_title' => array(
			'type' => 'setting',
			'text' => 'Enable HiveMail Integration',
		),
		'setting_hive_enabled_desc' => array(
			'type' => 'setting',
			'text' => 'This must be enabled for HiveMail integration to function.',
		),
		'setting_hive_accountatreg_title' => array(
			'type' => 'setting',
			'text' => 'Account At Registration',
		),
		'setting_hive_accountatreg_desc' => array(
			'type' => 'setting',
			'text' => 'If you wish to allow only certain vBulletin usergroups to get HiveMail accounts, disable this option. If it is enabled, anyone will be able to register for a HiveMail account at the time of their vBulletin registration.',
		),
		'setting_hive_requiresameuser_title' => array(
			'type' => 'setting',
			'text' => 'Require Same Username',
		),
		'setting_hive_requiresameuser_desc' => array(
			'type' => 'setting',
			'text' => 'If you wish to require that the same username be used for HiveMail and vBulletin, enable this option. Otherwise, the user will be prompted to select a username at the time of registering for their HiveMail account.',
		),
		'setting_hive_url_title' => array(
			'type' => 'setting',
			'text' => 'URL to HiveMail',
		),
		'setting_hive_url_desc' => array(
			'type' => 'setting',
			'text' => 'Enter the URL to your HiveMail installation here. This must be a fully qualified URL (e.g. http://www.yourdomain.c0m/hivemail).',
		),
		'setting_hive_intver_title' => array(
			'type' => 'setting',
			'text' => 'HiveMail Integration Version',
		),
		'setting_hive_intver_desc' => array(
			'type' => 'setting',
			'text' => 'DO NOT TOUCH!',
		),
	),
	'3.0.3' => array(
		'hivemail_badpassword' => array(
			'type' => 'error',
			'text' => 'The password you entered does not match your current password. Please press back and reenter it.'
		),
		'hivemail_nosuchuser' => array(
			'type' => 'error',
			'text' => 'That user/password combination was not found. Please press back and double-check your input.',
		),
		'hivemail_nameillegal' => array(
			'type' => 'error',
			'text' => 'The email address you requested was not valid. Your username may only contain alphanumeric characters, underscores (_) and dots (.), must start with a letter and must be a minimum length of 2 characters.',
		),
		'hivemail_nametaken' => array(
			'type' => 'error',
			'text' => 'We\'re sorry, the email address you requested is already used by another member. Please go back and enter a different address.',
		),
		'hivemail_thankyou' => array(
			'type' => 'redirect',
			'text' => 'Thank you for signing up for our mail service, your password is the same one you use for this account.',
		),
		'hivemail_linksuccessful' => array(
			'type' => 'redirect',
			'text' => 'Your accounts were successfully linked.',
		),
		'hivemail_choose_username' => array(
			'type' => 'register',
			'text' => 'Enter the username you wish to use for your email account. It will be the part before the @ sign in your new email address.<br />Then, choose which domain name you wish to register for. (You can always use any domain name, but the domain name you select now will be your default domain name.)<br />The password for your email account will be the same as your forum account.',
		),
		'hivemail_get_email_account' => array(
			'type' => 'register',
			'text' => 'Get an email account.',
		),
		'hivemail_like_email_account' => array(
			'type' => 'register',
			'text' => 'If you choose to register for an email account, you must enter a username below.',
		),
		'hivemail_register_for_account' => array(
			'type' => 'register',
			'text' => 'Register for an email account',
		),
		'hivemail_signup_for_email_account' => array(
			'type' => 'global',
			'text' => 'Sign Up for an Email Account',
		),
		'hivemail_username_and_domain_selection' => array(
			'type' => 'register',
			'text' => 'Username and Domain Selection',
		),
		'hivemail_register_information' => array(
			'type' => 'register',
			'text' => 'Email Registration Information',
		),
		'hivemail_permissions' => array(
			'type' => 'cpug',
			'text' => 'HiveMail Integration Permissions',
		),
		'hivemail_can_check_new_mail' => array(
			'type' => 'cpug',
			'text' => 'Can have system check for new email',
		),
		'hivemail_can_have_account' => array(
			'type' => 'cpug',
			'text' => 'Can get HiveMail account (after registration)',
		),
		'hivemail_can_view_new_mail' => array(
			'type' => 'cpug',
			'text' => 'Can view list of new emails in User CP'
		),
		'hivemail_can_link_account' => array(
			'type' => 'cpug',
			'text' => 'Can link existing HiveMail account to vBulletin account',
		),
		'hivemail_unread_emails' => array(
			'type' => 'usertools',
			'text' => 'Unread Emails',
		),
		'hivemail_from' => array(
			'type' => 'usertools',
			'text' => 'From',
		),
		'hivemail_date_received' => array(
			'type' => 'usertools',
			'text' => 'Date Received',
		),
		'hivemail_no_unread' => array(
			'type' => 'usertools',
			'text' => 'No Unread Emails Found',
		),
		'hivemail_click_read_email' => array(
			'type' => 'usertools',
			'text' => 'Click to read email',
		),
		'hivemail_click_send_email' => array(
			'type' => 'usertools',
			'text' => 'Click to send email',
		),
		'hivemail_link_x_to_x' => array(
			'type' => 'usertools',
			'text' => 'Link {1} Account to {2} Account',
		),
		'hivemail_link_account_description' => array(
			'type' => 'usertools',
			'text' => 'This form will allow you to link an <b>existing</b> <a href="{1}">{2}</a> email account to your forum account. The password for your {2} account will be changed to the same password as this forum.',
		),
		'hivemail_x_username' => array(
			'type' => 'usertools',
			'text' => '{1} account name',
		),
		'hivemail_x_password' => array(
			'type' => 'usertools',
			'text' => '{1} password',
		),
		'hivemail_link_account_description_2' => array(
			'type' => 'usertools',
			'text' => 'If you do not yet have a {1} email account, you can scroll down on this page and sign up for one.',
		),
		'hivemail_link_accounts' => array(
			'type' => 'usertools',
			'text' => 'Link Accounts',
		),
		'hivemail_register_information_username' => array(
			'type' => 'usertools',
			'text' => 'Enter the username you wish to use for your email account. It will be the part before the @ sign in your new email address.',
		),
		'hivemail_account_name' => array(
			'type' => 'usertools',
			'text' => 'Account name',
		),
		'hivemail_current_x_password' => array(
			'type' => 'usertools',
			'text' => 'Enter your current {1} password to verify your request',
		),
		'hivemail_same_password' => array(
			'type' => 'global',
			'text' => 'The password for your email account will be the same as your password on this forum.',
		),
		'hivemail_same_username_password' => array(
			'type' => 'global',
			'text' => 'The username and password for your email account will be the same as you use on this forum.',
		),
		'hivemail_register_information_domain' => array(
			'type' => 'global',
			'text' => 'Choose which domain name you wish to register for. (You can always use any domain name, but the domain name you select now will be your default domain name.)',
		),
		'hivemail_new_messages' => array(
			'type' => 'global',
			'text' => 'new messages'
		),
		'hivemail_unread_messages' => array(
			'type' => 'global',
			'text' => 'unread messages'
		),
		'hivemail_get_account' => array(
			'type' => 'global',
			'text' => 'Get Email Account',
		),
		'hivemail_goto_account' => array(
			'type' => 'global',
			'text' => 'Go to Email Account',
		),
		'settinggroup_hivemail' => array(
			'type' => 'setting',
			'text' => 'HiveMail Integration Settings',
		),
		'setting_hive_filepath_title' => array(
			'type' => 'setting',
			'text' => 'Path to HiveMail files',
		),
		'setting_hive_filepath_desc' => array(
			'type' => 'setting',
			'text' => 'This is the path to HiveMail files on your server. This must be an absolute path (e.g. /home/username/public_html/hivemail).',
		),
		'setting_hive_checktime_title' => array(
			'type' => 'setting',
			'text' => 'Time Between Mail Checks',
		),
		'setting_hive_checktime_desc' => array(
			'type' => 'setting',
			'text' => 'Enter the number of seconds that must elapse between checks for new mail for a particular user. Each time the system checks for new mail in HiveMail for a user, it uses two queries, so the results are cached for this many seconds before updating.',
		),
		'setting_hive_enabled_title' => array(
			'type' => 'setting',
			'text' => 'Enable HiveMail Integration',
		),
		'setting_hive_enabled_desc' => array(
			'type' => 'setting',
			'text' => 'This must be enabled for HiveMail integration to function.',
		),
		'setting_hive_accountatreg_title' => array(
			'type' => 'setting',
			'text' => 'Account At Registration',
		),
		'setting_hive_accountatreg_desc' => array(
			'type' => 'setting',
			'text' => 'If you wish to allow only certain vBulletin usergroups to get HiveMail accounts, disable this option. If it is enabled, anyone will be able to register for a HiveMail account at the time of their vBulletin registration.',
		),
		'setting_hive_requiresameuser_title' => array(
			'type' => 'setting',
			'text' => 'Require Same Username',
		),
		'setting_hive_requiresameuser_desc' => array(
			'type' => 'setting',
			'text' => 'If you wish to require that the same username be used for HiveMail and vBulletin, enable this option. Otherwise, the user will be prompted to select a username at the time of registering for their HiveMail account.',
		),
		'setting_hive_url_title' => array(
			'type' => 'setting',
			'text' => 'URL to HiveMail',
		),
		'setting_hive_url_desc' => array(
			'type' => 'setting',
			'text' => 'Enter the URL to your HiveMail installation here. This must be a fully qualified URL (e.g. http://yourdomain.c0m/hivemail).',
		),
		'setting_hive_intver_title' => array(
			'type' => 'setting',
			'text' => 'HiveMail Integration Version',
		),
		'setting_hive_intver_desc' => array(
			'type' => 'setting',
			'text' => 'DO NOT TOUCH!',
		),
	),
);
$_templates = array(
	'3.0.1' => array(
		'hivemail_signup' => '$stylevar[htmldoctype]
<html dir="$stylevar[textdirection]" lang="$stylevar[languagecode]">
<head><title>$vboptions[bbtitle] - Email Account</title>
$headinclude
</head>
<body>
$header

<br />

<form action="register.php" method="post">
<input type="hidden" name="s" value="$session[sessionhash]" />
<input type="hidden" name="hive_signup" value="1" />
<input type="hidden" name="do" value="addmail" />

<table cellpadding="$stylevar[outerborderwidth]" cellspacing="0" border="0" class="tborder" width="$stylevar[tablewidth]" align="center"><tr><td>
<table cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%">
<tr>
	<td class="thead" colspan="2"><b>Sign Up for an Email Account</b></td>
</tr>
<tr>
	<td class="alt1"><b>Account name:</b></td>
	<td class="alt1"><input type="text" class="bginput" size="25" name="hive_username" value="$bbuserinfo[username]" /> <select name="hive_userdomain">$hive_domainname_options</select></td>
</tr>
<tr>
	<td class="alt2"><b>Password:</b></td>
	<td class="alt2"><input type="password" class="bginput" size="25" name="password" /></td>
</tr>
</table>
</td></tr></table>

<table cellpadding="2" cellspacing="0" border="0" width="$stylevar[tablewidth]" align="center">
<tr>
	<td align="center"><input type="submit" class="button" value="Submit Now" /></td>
</tr>
</table>

</form>

$footer

</body>
</html>',
	),
	'3.0.2' => array(
		'hivemail_signup' => '<form action="profile.php" method="post">
<input type="hidden" name="s" value="$session[sessionhash]" />
<input type="hidden" name="hive_signup" value="1" />
<input type="hidden" name="do" value="addmail" />

<table cellpadding="$stylevar[outerborderwidth]" cellspacing="0" border="0" class="tborder" width="$stylevar[tablewidth]" align="center"><tr><td>
<table cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%">
<tr>
	<td class="thead" colspan="2"><b>Sign Up for an Email Account</b></td>
</tr>
<tr>
	<td class="alt1"><b>Account name:</b></td>
	<td class="alt1">
	<if condition="$vboptions[\'hive_requiresameuser\']">
	<input type="hidden" name="hive_username" value="$bbuserinfo[username]" />
	$bbuserinfo[username]
	<else />
	<input type="text" class="bginput" size="25" name="hive_username" value="$bbuserinfo[username]" />
	</if>
	<select name="hive_userdomain">$hive_domainname_options</select>
	</td>
</tr>
<tr>
	<td class="alt2"><b>Password:</b></td>
	<td class="alt2"><input type="password" class="bginput" size="25" name="password" /></td>
</tr>
</table>
</td></tr></table>

<table cellpadding="2" cellspacing="0" border="0" width="$stylevar[tablewidth]" align="center">
<tr>
	<td align="center"><input type="submit" class="button" value="Submit Now" /></td>
</tr>
</table>

</form>',
		'hivemail_usercp_mailbit' => '<tr>
	<td class="alt2" nowrap="nowrap"><a href="$vboptions[hive_url]compose.php?data[to]=$unread[email]" title="$vbphrase[hivemail_click_send_email]">$unread[from]</a></td>
	<td class="alt1Active" id="m$unread[messageid]" width="100%" nowrap="nowrap"><a href="$vboptions[hive_url]read.email.php?messageid=$unread[messageid]" title="$vbphrase[hivemail_click_read_email]">$unread[subject]</a></td>
	<td class="alt2" nowrap="nowrap">$unread[dateline]</td>
</tr>',
	),
	'3.0.3' => array(
		'hivemail_signup' => '<if condition="$show[\'linkaccount\']">
<form action="profile.php" method="post">
<input type="hidden" name="s" value="$session[sessionhash]" />
<input type="hidden" name="do" value="linkmail" />

<table cellpadding="$stylevar[outerborderwidth]" cellspacing="0" border="0" class="tborder" width="$stylevar[tablewidth]" align="center"><tr><td>
<table cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%">
<tr>
	<td class="thead" colspan="2"><b><phrase 1="$vboptions[bbtitle]" 2="$_options[appname]">$vbphrase[hivemail_link_x_to_x]</phrase></a></b></td>
</tr>
<tr>
	<td class="alt2" colspan="2"><phrase 1="$vboptions[hive_url]" 2="$_options[appname]">$vbphrase[hivemail_link_account_description]</phrase><if condition="$show[\'newaccount\']"><br /><phrase 1="$_options[appname]">$vbphrase[hivemail_link_account_description_2]</phrase></if></td>
</tr>
<tr>
	<td class="alt1"><b><phrase 1="$_options[appname]">$vbphrase[hivemail_x_username]</phrase>:</b></td>
	<td class="alt1">
	<input type="text" class="bginput" size="25" name="hive_username" value="$bbuserinfo[username]" />
	</td>
</tr>
<tr>
	<td class="alt2"><b><phrase 1="$_options[appname]">$vbphrase[hivemail_x_password]</phrase>:</b></td>
	<td class="alt2"><input type="password" class="bginput" size="25" name="hive_password" /></td>
</tr>
<tr>
	<td class="alt1"><b><phrase 1="$vboptions[bbtitle]">$vbphrase[hivemail_current_x_password]</phrase>:</b></td>
	<td class="alt1"><input type="password" class="bginput" size="25" name="password" /></td>
</tr>
</table>
</td></tr></table>

<table cellpadding="2" cellspacing="0" border="0" width="$stylevar[tablewidth]" align="center">
<tr>
	<td align="center"><input type="submit" class="button" value="$vbphrase[hivemail_link_accounts]" /></td>
</tr>
</table>

</form>

<br /><br />
</if>

<if condition="$show[\'newaccount\']">
<form action="profile.php" method="post">
<input type="hidden" name="s" value="$session[sessionhash]" />
<input type="hidden" name="hive_signup" value="1" />
<input type="hidden" name="do" value="addmail" />

<table cellpadding="$stylevar[outerborderwidth]" cellspacing="0" border="0" class="tborder" width="$stylevar[tablewidth]" align="center"><tr><td>
<table cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%">
<tr>
	<td class="thead" colspan="2"><b>$vbphrase[hivemail_signup_for_email_account]</b></td>
</tr>
<tr>
	<td class="alt2" colspan="2"><if condition="!$vboptions[\'hive_requiresameuser\']">$vbphrase[hivemail_register_information_username]<br /><phrase 1="$vboptions[bbtitle]">$vbphrase[hivemail_register_information_domain]</phrase><br />$vbphrase[hivemail_same_password]<else />$vbphrase[hivemail_same_username_password]<br /><phrase 1="$vboptions[bbtitle]">$vbphrase[hivemail_register_information_domain]</phrase></if></td>
</tr>
<tr>
	<td class="alt1"><b>$vbphrase[hivemail_account_name]:</b></td>
	<td class="alt1">
	<if condition="$vboptions[\'hive_requiresameuser\']">
	<input type="hidden" name="hive_username" value="$bbuserinfo[username]" />
	$bbuserinfo[username]
	<else />
	<input type="text" class="bginput" size="25" name="hive_username" value="$bbuserinfo[username]" />
	</if>
	<select name="hive_userdomain">$hive_domainname_options</select>
	</td>
</tr>
<tr>
	<td class="alt2"><b><phrase 1="$vboptions[bbtitle]">$vbphrase[hivemail_current_x_password]</phrase>:</b></td>
	<td class="alt2"><input type="password" class="bginput" size="25" name="password" /></td>
</tr>
</table>
</td></tr></table>

<table cellpadding="2" cellspacing="0" border="0" width="$stylevar[tablewidth]" align="center">
<tr>
	<td align="center"><input type="submit" class="button" value="Submit Now" /></td>
</tr>
</table>

</form>
</if>',
		'hivemail_usercp_mailbit' => '<tr>
	<td class="alt2" nowrap="nowrap"><a href="$vboptions[hive_url]compose.email.php?data[to]=$unread[email]" title="$vbphrase[hivemail_click_send_email]">$unread[from]</a></td>
	<td class="alt1Active" id="m$unread[messageid]" width="100%" nowrap="nowrap"><a href="$vboptions[hive_url]read.email.php?messageid=$unread[messageid]" title="$vbphrase[hivemail_click_read_email]">$unread[subject]</a></td>
	<td class="alt2" nowrap="nowrap">$unread[dateline]</td>
</tr>',
	),
);

$_vbtemplates = array(
	'3.0.1' => array(
		'register' => array(
			1 => array(
				'operation' => 'addabove',
				'find' => '<if condition="$show[\'customfields_option\']">',
				'replace' => '<!-- *** -->
<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" align="center">
<tr>
	<td class="thead">$vbphrase[hivemail_signup_for_email_account]</td>
</tr>
<tr>
	<td class="panelsurround" align="center">
	<div class="panel">
		<div style="width:$stylevar[formwidth_usercp]" align="$stylevar[left]">
			<fieldset class="fieldset">
				<legend><label for="hive_signup">$vbphrase[hivemail_register_for_account]</label></legend>
				<table cellpadding="0" cellspacing="$stylevar[formspacer]" border="0" width="100%">
				<tr>
					<td>$vbphrase[hivemail_please_choose_account_name]</td>
				</tr>
				<tr>
					<td><label for="hive_signup"><input type="checkbox" name="hive_signup" value="1" id="hive_signup" $hive_signup_checked /><phrase 1="$hive_domainname">$vbphrase[hivemail_get_x_email_account]</phrase></label></td>
				</tr>
				</table>
			</fieldset>

			<fieldset class="fieldset">
				<legend><label for="hive_username">$vbphrase[hivemail_username_and_domain_selection]</label></legend>
				<table cellpadding="0" cellspacing="$stylevar[formspacer]" border="0" width="100%">
				<tr>
					<td>$vbphrase[hivemail_choose_username]</td>
				</tr>
				<tr>
					<td><input type="text" name="hive_username" size="50" maxlength="50" value="$hive_username" />
					<select name="hive_userdomain">$hive_domainname_options</select></td>
				</tr>
				</table>
			</fieldset>
		</div>
	</div>
	</td>
</tr>
</table>
<!-- *** -->',
			),
		),
	),
	'3.0.2' => array(
		'register' => array(
			1 => array(
				'operation' => 'addabove',
				'find' => '<if condition="$show[\'customfields_option\']">',
				'replace' => '<!-- HIVEMAIL ADD -->
<if condition="$vboptions[\'hive_enabled\'] and $vboptions[\'hive_accountatreg\'] and VB_PLUGIN == true">
<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" align="center">
<tr>
	<td class="thead">$vbphrase[hivemail_signup_for_email_account]</td>
</tr>
<tr>
	<td class="panelsurround" align="center">
	<div class="panel">
		<div style="width:$stylevar[formwidth_usercp]" align="$stylevar[left]">
			<fieldset class="fieldset">
				<legend><label for="hive_signup">$vbphrase[hivemail_register_for_account]</label></legend>
				<table cellpadding="0" cellspacing="$stylevar[formspacer]" border="0" width="100%">
				<tr>
					<td>$vbphrase[hivemail_like_email_account]</td>
				</tr>
				<tr>
					<td><label for="hive_signup"><input type="checkbox" name="hive_signup" value="1" id="hive_signup" $hive_signup_checked /><phrase 1="$hive_domainname">$vbphrase[hivemail_get_email_account]</phrase></label></td>
				</tr>
				</table>
			</fieldset>
			
			<if condition="!$vboptions[\'hive_requiresameuser\']">
			<fieldset class="fieldset">
				<legend><label for="hive_username">$vbphrase[hivemail_username_and_domain_selection]</label></legend>
				<table cellpadding="0" cellspacing="$stylevar[formspacer]" border="0" width="100%">
				<tr>
					<td>$vbphrase[hivemail_choose_username]</td>
				</tr>
				<tr>
					<td><input type="text" name="hive_username" size="50" maxlength="50" value="$hive_username" />
					<select name="hive_userdomain">$hive_domainname_options</select></td>
				</tr>
				</table>
			</fieldset>
			<else />
			<fieldset class="fieldset">
				<legend>$vbphrase[hivemail_register_information]</legend>
				$vbphrase[hivemail_same_username]
			</fieldset>
			</if>
		</div>
	</div>
	</td>
</tr>
</table>
<br />
</if>
<!-- END HIVEMAIL ADD -->
',
			),
		),
		'USERCP_SHELL' => array(
			1 => array(
				'operation' => 'addabove',
				'find' => '<if condition="$show[\'pmstats\']">',
				'replace' => '<!-- HIVEMAIL ADD -->
<if condition="$show[\'hivemaillink\']">
	<tr>
	<if condition="!$bbuserinfo[\'hiveuserid\']">
		<td class="$navclass[hivemail]" nowrap="nowrap"><a class="smallfont" href="profile.php?$session[sessionurl]do=mailsignup">$vbphrase[hivemail_get_account]</a></td>
	<else />
		<td class="$navclass[hivemail]" nowrap="nowrap"><a class="smallfont" href="$vboptions[hive_url]">$vbphrase[hivemail_goto_account]</a>
		<if condition="$bbuserinfo[\'hivecheck\']"><span class="smallfont"><br />$bbuserinfo[hivemsgs_newcount] $vbphrase[hivemail_new_messages]<br />$bbuserinfo[hivemsgs_unreadcount] $vbphrase[hivemail_unread_messages]</span></if></td>
	</if>
	</tr>
</if>
<!-- END HIVEMAIL ADD -->
',
			),
		),
		'USERCP' => array(
			1 => array(
				'operation' => 'addabove',
				'find' => '<if condition="$show[\'privatemessages\']">',
				'replace' => '<!-- HIVEMAIL ADD -->
<if condition="$show[\'hive_newemails\']">
<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" align="center">
<thead>
	<tr>
		<td class="tcat" colspan="7">
			<a style="float:$stylevar[right]" href="#top" onclick="return toggle_collapse(\'usercp_submails\')"><img id="collapseimg_usercp_submails" src="$stylevar[imgdir_button]/collapse_tcat$vbcollapse[collapseimg_usercp_submails].gif" alt="" border="0" /></a>
			<a href="$vboptions[hive_url]">$vbphrase[hivemail_unread_emails]</a><span class="normal">: ($nummails)</span>
		</td>
	</tr>
</thead>
<tbody id="collapseobj_usercp_submails" style="$vbcollapse[collapseobj_usercp_submails]">
	<if condition="$show[\'hive_newdetails\']">
		<tr class="thead">
			<td class="thead" nowrap="nowrap">$vbphrase[hivemail_from]</td>
			<td class="thead" nowrap="nowrap">$vbphrase[subject]</td>
			<td class="thead" nowrap="nowrap">$vbphrase[hivemail_date_received]</td>
		</tr>
		$mailbits
	<else />
		<tr>
			<td class="alt1" align="center" colspan="7"><strong>$vbphrase[hivemail_no_unread]</strong></td>
		</tr>
	</if>
	<tr>
		<td align="$stylevar[right]" colspan="7" class="tfoot">
		<span class="smallfont"><a href="$vboptions[hive_url]">$vbphrase[hivemail_goto_account]</a></span>
		</td>
	</tr>
</tbody>
</table>
<br />
</if>
<!-- END HIVEMAIL ADD -->
',
			),
		),
	),
	'3.0.3' => array(
		'register' => array(
			1 => array(
				'operation' => 'addabove',
				'find' => '<if condition="$show[\'customfields_option\']">',
				'replace' => '<!-- HIVEMAIL ADD -->
<if condition="$vboptions[\'hive_enabled\'] and $vboptions[\'hive_accountatreg\'] and FORUM_PLUGIN == true">
<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" align="center">
<tr>
	<td class="thead">$vbphrase[hivemail_signup_for_email_account]</td>
</tr>
<tr>
	<td class="panelsurround" align="center">
	<div class="panel">
		<div style="width:$stylevar[formwidth_usercp]" align="$stylevar[left]">
			<fieldset class="fieldset">
				<legend><label for="hive_signup">$vbphrase[hivemail_register_for_account]</label></legend>
				<table cellpadding="0" cellspacing="$stylevar[formspacer]" border="0" width="100%">
				<tr>
					<td>$vbphrase[hivemail_like_email_account]</td>
				</tr>
				<tr>
					<td><label for="hive_signup"><input type="checkbox" name="hive_signup" value="1" id="hive_signup" $hive_signup_checked /><phrase 1="$hive_domainname">$vbphrase[hivemail_get_email_account]</phrase></label></td>
				</tr>
				</table>
			</fieldset>
			
			<if condition="!$vboptions[\'hive_requiresameuser\']">
			<fieldset class="fieldset">
				<legend><label for="hive_username">$vbphrase[hivemail_username_and_domain_selection]</label></legend>
				<table cellpadding="0" cellspacing="$stylevar[formspacer]" border="0" width="100%">
				<tr>
					<td>$vbphrase[hivemail_choose_username]</td>
				</tr>
				<tr>
					<td><input type="text" name="hive_username" size="50" maxlength="50" value="$hive_username" />
					<select name="hive_userdomain">$hive_domainname_options</select></td>
				</tr>
				</table>
			</fieldset>
			<else />
			<fieldset class="fieldset">
				<legend>$vbphrase[hivemail_register_information]</legend>
				$vbphrase[hivemail_same_username_password]
			</fieldset>
			</if>
		</div>
	</div>
	</td>
</tr>
</table>
<br />
</if>
<!-- END HIVEMAIL ADD -->
',
			),
		),
		'USERCP_SHELL' => array(
			1 => array(
				'operation' => 'addabove',
				'find' => '<if condition="$show[\'pmstats\']">',
				'replace' => '<!-- HIVEMAIL ADD -->
<if condition="$show[\'hivemaillink\']">
	<tr>
	<if condition="!$bbuserinfo[\'hiveuserid\']">
		<td class="$navclass[hivemail]" nowrap="nowrap"><a class="smallfont" href="profile.php?$session[sessionurl]do=mailsignup">$vbphrase[hivemail_get_account]</a></td>
	<else />
		<td class="$navclass[hivemail]" nowrap="nowrap"><a class="smallfont" href="$vboptions[hive_url]">$vbphrase[hivemail_goto_account]</a>
		<if condition="$bbuserinfo[\'hivecheck\']"><span class="smallfont"><br />$bbuserinfo[hivemsgs_newcount] $vbphrase[hivemail_new_messages]<br />$bbuserinfo[hivemsgs_unreadcount] $vbphrase[hivemail_unread_messages]</span></if></td>
	</if>
	</tr>
</if>
<!-- END HIVEMAIL ADD -->
',
			),
		),
		'USERCP' => array(
			1 => array(
				'operation' => 'addabove',
				'find' => '<if condition="$show[\'privatemessages\']">',
				'replace' => '<!-- HIVEMAIL ADD -->
<if condition="$show[\'hive_newemails\']">
<table class="tborder" cellpadding="$stylevar[cellpadding]" cellspacing="$stylevar[cellspacing]" border="0" width="100%" align="center">
<thead>
	<tr>
		<td class="tcat" colspan="7">
			<a style="float:$stylevar[right]" href="#top" onclick="return toggle_collapse(\'usercp_submails\')"><img id="collapseimg_usercp_submails" src="$stylevar[imgdir_button]/collapse_tcat$vbcollapse[collapseimg_usercp_submails].gif" alt="" border="0" /></a>
			<a href="$vboptions[hive_url]">$vbphrase[hivemail_unread_emails]</a><span class="normal">: ($nummails)</span>
		</td>
	</tr>
</thead>
<tbody id="collapseobj_usercp_submails" style="$vbcollapse[collapseobj_usercp_submails]">
	<if condition="$show[\'hive_newdetails\']">
		<tr class="thead">
			<td class="thead" nowrap="nowrap">$vbphrase[hivemail_from]</td>
			<td class="thead" nowrap="nowrap">$vbphrase[subject]</td>
			<td class="thead" nowrap="nowrap">$vbphrase[hivemail_date_received]</td>
		</tr>
		$mailbits
	<else />
		<tr>
			<td class="alt1" align="center" colspan="7"><strong>$vbphrase[hivemail_no_unread]</strong></td>
		</tr>
	</if>
	<tr>
		<td align="$stylevar[right]" colspan="7" class="tfoot">
		<span class="smallfont"><a href="$vboptions[hive_url]">$vbphrase[hivemail_goto_account]</a></span>
		</td>
	</tr>
</tbody>
</table>
<br />
</if>
<!-- END HIVEMAIL ADD -->
',
			),
		),
	),
);

$_settings = array(
	'hive_enabled' => array(
		'grouptitle' => 'hivemail',
		'value' => 0,
		'defaultvalue' => 0,
		'optioncode' => 'yesno',
		'displayorder' => '10',
		'advanced' => 0,
		'volatile' => 0,
	),
	'hive_filepath' => array(
		'grouptitle' => 'hivemail',
		'value' => '',
		'defaultvalue' => '',
		'optioncode' => '',
		'displayorder' => '20',
		'advanced' => 0,
		'volatile' => 0,
	),
	'hive_url' => array(
		'grouptitle' => 'hivemail',
		'value' => '',
		'defaultvalue' => '',
		'optioncode' => '',
		'displayorder' => '30',
		'advanced' => 0,
		'volatile' => 0,
	),
	'hive_checktime' => array(
		'grouptitle' => 'hivemail',
		'value' => '300',
		'defaultvalue' => '300',
		'optioncode' => '',
		'displayorder' => '40',
		'advanced' => 0,
		'volatile' => 0,
	),
	'hive_accountatreg' => array(
		'grouptitle' => 'hivemail',
		'value' => 0,
		'defaultvalue' => 0,
		'optioncode' => 'yesno',
		'displayorder' => '50',
		'advanced' => 0,
		'volatile' => 0,
	),
	'hive_requiresameuser' => array(
		'grouptitle' => 'hivemail',
		'value' => 0,
		'defaultvalue' => 0,
		'optioncode' => 'yesno',
		'displayorder' => '60',
		'advanced' => 0,
		'volatile' => 0,
	),
	'hive_intver' => array(
		'grouptitle' => 'version',
		'value' => $_intver,
		'defaultvalue' => $_intver,
		'optioncode' => '',
		'displayorder' => '500',
		'advanced' => 0,
		'volatile' => 0,
	),
);
$_templatetitles = '"'.implode('","', array_keys($_templates["$_intver"])).'"';
$_phrasetitles = '"'.implode('","', array_keys($_phrases["$_intver"])).'"';
$_settingtitles = '"'.implode('","', array_keys($_settings)).'"';
$_vbtemplatetitles = '"'.implode('","', array_keys($_vbtemplates["$_intver"])).'"';
foreach (array_keys($_vbtemplates["$_intver"]) as $temptitle) {
	$temptitles .= '<li>'.$temptitle.'</li>';
}


// ####################################################################################
// #################################### START SCRIPT ##################################
// ####################################################################################
$quicktest = $DB_site->query_first("
	SELECT *
	FROM ".TABLE_PREFIX."user
	LIMIT 1
");
$prevversion = iif(isset($quicktest['hiveuserid']), 1, 0);

if (	$prevversion
	and $_REQUEST['do'] != 'uninstall'
	and $_REQUEST['do'] != 'douninstall'
	and $_REQUEST['do'] != 'upgrade'
	and $_REQUEST['do'] != 'doupgrade'
	and $_REQUEST['do'] != 'install_upgrade')
{
	if ($vboptions['hive_intver'] == $_intver)
	{
		print_cp_message('You have already installed vBulletin/HiveMail&trade; integration.<br /><br />
		If you have just upgraded vBulletin, and reverted any of the following templates:
		<ul>'.$temptitles.'</ul>
		<a href="hivevb3_install.php?do=doinstall&templatesonly=1">Click here</a> to reapply template changes automatically.
		If you wish to uninstall vBulletin/HiveMail&trade; integration, <a href="hivevb3_install.php?do=uninstall">click here</a>.');
	}
	elseif (empty($vboptions['hive_intver']) or convert_version_to_int($vboptions['hive_intver']) < convert_version_to_int($_intver))
	{
		$_REQUEST['do'] = 'install_upgrade';
	}
}

print_cp_header(
	iif($_REQUEST['do'] == 'uninstall' or $_REQUEST['do'] == 'douninstall', 'Uninstall').
	iif($_REQUEST['do'] == 'install' or $_REQUEST['do'] == 'doinstall' or empty($_REQUEST['do']), 'Install').
	iif($_REQUEST['do'] == 'upgrade' or $_REQUEST['do'] == 'doupgrade' or $_REQUEST['do'] == 'install_upgrade', 'Upgrade').
	" vBulletin/HiveMail&trade; Integration - $vboptions[bbtitle] - vBulletin $vbphrase[control_panel]"
);

$vboptions = build_options();

if ($prevversion and empty($vboptions['hive_intver']))
{
	$vboptions['hive_intver'] = '3.0.1';
}

if (empty($_REQUEST['do']) or $_REQUEST['do'] == 'install' or $_REQUEST['do'] == 'install_upgrade')
{
	echo '<b>vBulletin/HiveMail&trade; Integration</b><br /><br />';
	echo 'This script will '.iif($_REQUEST['do'] == 'install_upgrade', 'upgrade', 'install').' vBulletin/HiveMail&trade; integration.<br /><br />

	If you have not modified the following vBulletin templates, this script can make the necessary changes to these templates automatically.
	<ul>'.$temptitles.'</ul>
	If you have modified them, the script can still attempt to make the necessary changes, but it may not be successful. Note that at no time will custom templates be in danger - if the script can\'t find the code it needs, it will simply skip that template and tell you at the end which template(s) were not modified successfully.<br />
	Do you want to have the script attempt to make the necessary changes automatically?<br /><br />
	Proceed and <a href="hivevb3_install.php?'.iif($_REQUEST['do'] == 'install_upgrade', 'do=doupgrade', 'do=doinstall').'&dotemplates=0">do not make</a> automatic template changes<br /><br />
	Proceed and <a href="hivevb3_install.php?'.iif($_REQUEST['do'] == 'install_upgrade', 'do=doupgrade', 'do=doinstall').'&dotemplates=1">do make</a> automatic template changes';
}

if ($_REQUEST['do'] == 'doinstall' or $_REQUEST['do'] == 'doupgrade')
{
	echo '<b>'.iif($_REQUEST['do'] == 'doinstall', 'Installing').iif($_REQUEST['do'] == 'doupgrade', 'Upgrading').' vBulletin/HiveMail&trade; integration</b><br /><br />';
	if ($templatesonly != 1) {
		// ############################################################################
		// Check if the field has been created
		echo '<b>Checking database</b><br />';
		echo 'Checking database structure ... ';
		$user = $DB_site->query_first('
			SELECT *
			FROM '.TABLE_PREFIX.'user, '.TABLE_PREFIX.'usergroup
			LIMIT 1
		');
		$create_field1 = !isset($user['hiveuserid']);
		$create_field2 = !isset($user['hivecheck']);
		$create_field3 = !isset($user['hivepermissions']);
		echo 'done!<br />';

		// ############################################################################
		// Create field is needed
		if ($create_field1)
		{
			echo 'Altering database structure ... ';
			$DB_site->query('
				ALTER TABLE '.TABLE_PREFIX.'user
				ADD hiveuserid int(10) unsigned NOT NULL default "0"
			');
			echo 'done!<br />';
		}
		if ($create_field2)
		{
			echo 'Altering database structure ... ';
			$DB_site->query('
				ALTER TABLE '.TABLE_PREFIX.'user
				ADD hivecheck varchar(50) NOT NULL
			');
			echo 'done!<br />';
		}
		if ($create_field3)
		{
			echo 'Altering database struction ... ';
			$DB_site->query('
				ALTER TABLE '.TABLE_PREFIX.'usergroup
				ADD hivepermissions INT UNSIGNED NOT NULL AFTER adminpermissions
			');
			$DB_site->query('
				UPDATE '.TABLE_PREFIX.'usergroup
				SET hivepermissions = 15
				WHERE usergroupid = 6
			');
			echo 'done!<br />';
		}
		echo '<br />';

		// ############################################################################
		// Check which templates already exist
		echo '<b>Creating templates</b><br />';
		echo 'Checking existing templates ... ';
		$templates = $DB_site->query("
			SELECT *
			FROM ".TABLE_PREFIX."template
			WHERE title IN ($_templatetitles)
			AND styleid <> -1
		");
		$existing = array();
		while ($template = $DB_site->fetch_array($templates))
		{
			$existing["$template[styleid]"]["$template[title]"] = $template['template_un'];
		}
		echo 'done!<br />';
	}

	// ############################################################################
	// Get style ID's
	echo 'Fetching styles ... ';
	$styles = $DB_site->query('
		SELECT *
		FROM '.TABLE_PREFIX.'style
	');
	$_styleids = array();
	$_styletitles = array();
	while ($style = $DB_site->fetch_array($styles))
	{
		$_styleids[] = $style['styleid'];
		$_styletitles["$style[styleid]"] = $style['title'];
	}
	echo 'done!<br />';

	if ($templatesonly != 1) {
		// ############################################################################
		// Create new templates
		foreach ($_templates["$_intver"] as $title => $template)
		{
			$values = array();
			foreach ($_styleids as $styleid)
			{
				if (empty($existing["$styleid"]["$title"]))
				{
					$values[] = "($styleid, '".addslashes($title)."', '".addslashes($template)."', '".addslashes(compile_template($template))."', ".time().", '$bbuserinfo[username]', '$_intver')";
				}
				elseif ($_REQUEST['do'] == 'doupgrade')
				{
					if ($_templates["$vboptions[hive_intver]"]["$title"] == $existing["$styleid"]["$title"])
					{
						$DB_site->query(
							"UPDATE ".TABLE_PREFIX."template
							SET template_un = '".addslashes($template)."',
							template = '".addslashes(compile_template($template))."',
							dateline = ".time().",
							username = '".addslashes($bbuserinfo['username'])."',
							version = '$_intver'
							WHERE title = '".addslashes($title)."'
							AND styleid = $styleid
						");
					}
					else
					{
						$errors[1]["$styleid"][] = "$title";
					}
				}
			}
			if (empty($values))
			{
				continue;
			}
			echo 'Creating template <tt>'.$title.'</tt> ... ';
			$values = implode(', ', $values);
			$DB_site->query("
				INSERT INTO ".TABLE_PREFIX."template
				(styleid, title, template_un, template, dateline, username, version)
				VALUES $values
			");
			echo 'done!<br />';
		}
	}

	// ############################################################################
	// Update existing templates
	if ($dotemplates == 1 or $templatesonly == 1)
	{
		$vbtemplates = $DB_site->query("
			SELECT *
			FROM ".TABLE_PREFIX."template
			WHERE title IN ($_vbtemplatetitles)
		");
		while ($vbtmpl = $DB_site->fetch_array($vbtemplates))
		{
			$tomodify["$vbtmpl[styleid]"]["$vbtmpl[title]"] = $vbtmpl['template_un'];
		}
		$error = array();
		foreach ($_vbtemplates["$_intver"] as $title => $operations)
		{
			$values = array();
			foreach ($_styleids as $styleid) {
				unset($curtemplate, $curtemplate2);
				if (!empty($tomodify["$styleid"]["$title"]))
				{
					$curtemplate2 = $curtemplate = $tomodify["$styleid"]["$title"];
				}
				else
				{
					$curtemplate2 = $curtemplate = $tomodify[-1]["$title"];
				}
				if ($_REQUEST['do'] == 'doupgrade')
				{
					// Undo operations that were done in a previous installation
					foreach ($_vbtemplates["$vboptions[hive_intver]"]["$title"] as $operationid => $findreplace)
					{
						unset($curtemplate100, $find, $replace, $operation);
						$find = $findreplace['find'];
						$replace = $findreplace['replace'];
						$operation = $findreplace['operation'];
						if (strpos($curtemplate, $replace) !== false)
						{
							switch($operation)
							{
								case 'replace':
									$curtemplate100 = str_replace($replace, $find, $curtemplate);
									break;
								case 'addabove':
								case 'addbelow':
									$curtemplate100 = str_replace($replace, '', $curtemplate);
								break;
							}
							if ($curtemplate == $curtemplate100)
							{
								$errors[2]["$styleid"][] = $title;
								continue 2;
							}
							else
							{
								$curtemplate = $curtemplate100;
							}
						}
					}
				}
				foreach ($operations as $operationid => $findreplace)
				{
					// Do install operations for this version
					if ($_REQUEST['do'] == 'doinstall' and strpos($curtemplate, $findreplace['replace']) !== false)
					{
						continue;
					}
					$find = $findreplace['find'];
					$replace = $findreplace['replace'];
					$operation = $findreplace['operation'];
					switch($operation)
					{
						case 'addabove':
							$curtemplate = str_replace($find, $replace.$find, $curtemplate);
							break;
						case 'addbelow':
							$curtemplate = str_replace($find, $find.$replace, $curtemplate);
							break;
						case 'replace':
							$curtemplate = str_replace($find, $replace, $curtemplate);
							break;
					}
				}
				if ($curtemplate == $curtemplate2)
				{
					$errors[2]["$styleid"][] = $title;
				}
				elseif ($_REQUEST['do'] == 'doupgrade')
				{
					echo 'Modifying template <tt>'.$title.'</tt> ... ';
					$DB_site->query("
						UPDATE ".TABLE_PREFIX."template
						SET template_un = '".addslashes($curtemplate)."',
						template = '".addslashes(compile_template($curtemplate))."',
						dateline = ".time().",
						username = '".addslashes($bbuserinfo['username'])."',
						version = '$_intver'
						WHERE title = '$title'
						AND styleid = $styleid
					");
					echo 'done!<br />';
				}
				else
				{
					$values[] = "($styleid, '".addslashes($title)."', '".addslashes("$curtemplate")."', '".addslashes(compile_template($curtemplate))."', ".time().", '".addslashes($bbuserinfo['username'])."', '3.0.3')";
				}
			}
			if (empty($values))
			{
				continue;
			}
			echo 'Modifying template <tt>'.$title.'</tt> ... ';
			$values = implode(', ', $values);
			$DB_site->query("
				INSERT INTO ".TABLE_PREFIX."template
				(styleid, title, template_un, template, dateline, username, version)
				VALUES $values
			");
			echo 'done!<br />';
		}
	}
	foreach ($_styleids as $styleid)
	{
		print_rebuild_style($styleid, '', 0, 0, 0, 0);
	}
	echo '<br />';
	unset($existing);

	if ($templatesonly != 1) {
		// ############################################################################
		// Check which phrases already exist
		echo '<b>Creating phrases</b><br />';
		echo 'Checking existing phrases ... ';
		$phrases = $DB_site->query("
			SELECT *
			FROM ".TABLE_PREFIX."phrase
			WHERE varname IN ($_phrasetitles)
			AND languageid <> -1
		");
		$existing = array();
		while ($phrase = $DB_site->fetch_array($phrases))
		{
			$existing["$phrase[languageid]"]["$phrase[varname]"] = $phrase['text'];
		}
		echo 'done!<br />';

		// ############################################################################
		// Get language ID's
		echo 'Fetching languages ... ';
		$languages = $DB_site->query('
			SELECT languageid, title
			FROM '.TABLE_PREFIX.'language
		');
		$_languageids = array('0');
		$_languagetitles = array('0' => 'Master');
		while ($language = $DB_site->fetch_array($languages))
		{
			$_languageids[] = $language['languageid'];
			$_languageidtitles["$language[languageid]"] = $language['title'];
		}
		echo 'done!<br />';

		// ############################################################################
		// Create new phrases
		foreach ($_phrases["$_intver"] as $varname => $info)
		{
			$values = array();
			foreach ($_languageids as $languageid)
			{
				if (!empty($existing["$languageid"]["$varname"]))
				{
					if ($existing["$languageid"]["$varname"] == $info['text'] or $languageid <> 0)
					{
						continue;
					}
					else
					{
						echo 'Updating phrase <tt>'.$varname.'</tt> ... ';
						$DB_site->query(
							"UPDATE ".TABLE_PREFIX."phrase
							SET text = '".addslashes($info['text'])."'
							WHERE languageid = 0
							AND varname = '".addslashes($varname)."'
						");
						echo 'done!<br />';
						continue;
					}
				}
				elseif ($languageid == 0)
				{
					$values[] = "(0, '".addslashes($info['text'])."', '".addslashes($varname)."', ".$_phrasegroups["$info[type]"].")";
					echo 'Creating';
				}
			}
			if (empty($values))
			{
				continue;
			}
			echo ' phrase <tt>'.$varname.'</tt> ... ';
			$values = implode(', ', $values);
			$DB_site->query("
				INSERT INTO ".TABLE_PREFIX."phrase
				(languageid, text, varname, phrasetypeid)
				VALUES $values
			");
			echo 'done!<br />';
		}

		// ############################################################################
		// Rebuild languages
		echo 'Rebuilding languages ... ';
		build_language(-1);
		unset($existing);
		echo 'done!<br /><br />';

		// ############################################################################
		// Check setting group
		echo '<b>Creating options</b><br />';
		echo 'Checking setting groups ... ';
		$settinggroup = $DB_site->query_first("
			SELECT *
			FROM ".TABLE_PREFIX."settinggroup
			WHERE grouptitle = 'hivemail'
		");
		echo 'done!<br />';
		if (empty($settinggroup))
		{
			echo 'Inserting setting group ... ';
			$DB_site->query("
				INSERT INTO ".TABLE_PREFIX."settinggroup
				(grouptitle, displayorder, volatile) VALUES ('hivemail', 1000, 0)
			");
			echo 'done!<br />';
		}

		// ############################################################################
		// Check settings
		echo 'Checking settings ... ';
		$settings = $DB_site->query("
			SELECT *
			FROM ".TABLE_PREFIX."setting
			WHERE grouptitle = 'hivemail' OR varname = 'hive_intver'
		");
		$existing = array();
		while ($setting = $DB_site->fetch_array($settings))
		{
			$existing["$setting[varname]"] = true;
		}
		echo 'done!<br />';

		// ############################################################################
		// Insert settings
		foreach ($_settings as $varname => $info)
		{
			if ($existing["$varname"] === true)
			{
				continue;
			}
			$values = "('".$varname."', '".$info['grouptitle']."', '".$info['value']."', '".$info['defaultvalue']."', '".$info['optioncode']."', ".$info['displayorder'].", ".$info['advanced'].", ".$info['volatile'].")";
			echo 'Creating setting <tt>'.$varname.'</tt> ... ';
			$DB_site->query("
				INSERT INTO ".TABLE_PREFIX."setting
				(varname, grouptitle, value, defaultvalue, optioncode, displayorder, advanced, volatile)
				VALUES $values
			");
			echo 'done!<br />';
		}
		if ($_REQUEST['do'] == 'doupgrade')
		{
			$DB_site->query("
				UPDATE ".TABLE_PREFIX."setting
				SET value = '$_intver',
				defaultvalue = '$_intver'
				WHERE varname = 'hive_intver'
			");
		}
		$vboptions = build_options();
		echo '<br />';
	}

	// ############################################################################
	// Errors to report?
	echo '<b>Results</b><br />';
	if (!empty($errors)) {
		$_errortypes = array(1 => 'Couldn\'t update template originally created by HiveMail', 2 => 'Couldn\'t apply template modifications to vBulletin template');
		echo 'The script encountered problem(s), as detailed below.<br />';
		if (!empty($errors[1]))
		{
			echo 'For this error: "'.$_errortypes[1].'", the most likely scenario is that you modified a template that HiveMail previously created. You will need to remove your custom version and replace it with the most current version provided by HiveMail, or compare the two versions and make the necessary changes to yours.<br />';
		}
		if (!empty($errors[2]))
		{
			echo 'For this error: "'.$_errortypes[2].'", the script simply couldn\'t find the code it needed in order to perform the necessary template modifications. (This most likely means you have previously modified these template(s).) To resolve this, make note of the template(s) listed with this error, and manually make modifications to them, as found in step 2 of the integration instructions in the HiveMail&trade; manual.<br />';
		}
		echo '<br />';
		foreach ($errors as $errortype => $styleids)
		{
			// error types: 1 = couldn't update custom HM template, 2 = couldn't apply template mods
			echo '<ul>';
			foreach ($styleids as $styleid => $errors)
			{
				echo '<li>Style: '.$_styletitles["$styleid"].' (ID #'.$styleid.')<ul>';
				foreach ($errors as $blah => $title)
				{
						echo '<li><i>'.$title.'</i> - '.$_errortypes["$errortype"].'</li>';
				}
				echo '</ul></li>';
			}
			echo '</ul>';
		}
		echo '<br /><br />';
	}

	// ############################################################################
	// All done
	echo '<b>All done!</b><br /><br />
	You should now proceed to the <a href="index.php?loc=options.php%3Fdogroup%3Dhivemail%26do%3Doptions">vBulletin Options screen</a> and set the HiveMail&trade; integration options.';
}

if ($_REQUEST['do'] == 'uninstall')
{
	print_cp_message('Are you sure you wish to uninstall vBulletin/HiveMail&trade; integration? Users who registered for HiveMail&trade; through vBulletin will *not* be removed from HiveMail&trade;.<br /><br />
	The script can attempt to remove modifications made to the templates listed below as a result of installing vBulletin/HiveMail&trade; integration. If you have made further changes to the HiveMail&trade; code added to one or more of these templates, it will not work -- the script will simply be unable to locate the code it is trying to find, and will skip that template.<br />It is recommended that you let the script try to remove the modifications; if it is unable to do so, a message will appear letting you know which templates need to be manually modified.<br />These templates are affected:
	<ul>'.$temptitles.'</ul>
	Do you want to remove vBulletin/HiveMail&trade; integration?<br /><br />
	No, I <a href="index.php">do not want</a> to uninstall vBulletin integration.<br /><br />
	Yes, I <a href="hivevb3_install.php?do=douninstall">do want</a> to uninstall vBulletin integration, but do not want the script to remove template modifications.<br /><br />
	Yes, I <a href="hivevb3_install.php?do=douninstall&dotemplates=1">do want</a> to uninstall vBulletin integration, and have the script attempt to remove template modifications.');
}

if ($_REQUEST['do'] == 'douninstall')
{
	// ############################################################################
	// Check if fields exist
	echo '<b>Checking database</b><br />';
	echo 'Determining database structure ... ';
	$user = $DB_site->query_first('
		SELECT *
		FROM '.TABLE_PREFIX.'user, '.TABLE_PREFIX.'usergroup
		LIMIT 1
	');
	$drop_field1 = isset($user['hiveuserid']);
	$drop_field2 = isset($user['hivecheck']);
	$drop_field3 = isset($user['hivepermissions']);
	echo 'done!<br />';

	// ############################################################################
	// Drop fields as needed
	if ($drop_field1)
	{
		echo 'Altering database ... ';
		$DB_site->query('
			ALTER TABLE '.TABLE_PREFIX.'user
			DROP hiveuserid
		');
		echo 'done!<br />';
	}
	if ($drop_field2)
	{
		echo 'Altering database ... ';
		$DB_site->query('
			ALTER TABLE '.TABLE_PREFIX.'user
			DROP hivecheck
		');
		echo 'done!<br />';
	}
	if ($drop_field3)
	{
		echo 'Altering database ... ';
		$DB_site->query('
			ALTER TABLE '.TABLE_PREFIX.'usergroup
			DROP hivepermissions
		');
		echo 'done!<br />';
	}
	echo '<br />';

	// ############################################################################
	// Drop templates
	echo '<b>Checking templates</b><br />';
	echo 'Removing template(s) ... ';
	$DB_site->query("
		DELETE FROM ".TABLE_PREFIX."template
		WHERE title IN ($_templatetitles)
	");
	echo 'done!<br />';

	if ($dotemplates == 1)
	{
		// ############################################################################
		// Get style ID's
		echo 'Fetching styles ... ';
		$styles = $DB_site->query('
			SELECT *
			FROM '.TABLE_PREFIX.'style
		');
		$_styleids = array();
		$_styletitles = array();
		while ($style = $DB_site->fetch_array($styles))
		{
			$_styleids[] = $style['styleid'];
			$_styletitles["$style[styleid]"] = $style['title'];
		}
		echo 'done!<br />';

		// ############################################################################
		// Update existing templates
		$tomodify = array();
		$vbtemplates = $DB_site->query("
			SELECT *
			FROM ".TABLE_PREFIX."template
			WHERE title IN ($_vbtemplatetitles)
		");
		while ($vbtmpl = $DB_site->fetch_array($vbtemplates))
		{
			$tomodify["$vbtmpl[styleid]"]["$vbtmpl[title]"] = $vbtmpl['template_un'];
		}
		$error = array();
		foreach ($_vbtemplates["$vboptions[hive_intver]"] as $title => $operations)
		{
			$values = array();
			foreach ($_styleids as $styleid)
			{
				unset($curtemplate, $curtemplate2);
				$curtemplate2 = $curtemplate = $tomodify["$styleid"]["$title"];
				foreach ($operations as $operationid => $findreplace)
				{
					unset($find, $replace, $operation);
					if (strpos($curtemplate, $findreplace['replace']) === false)
					{
						continue;
					}
					$find = $findreplace['find'];
					$replace = $findreplace['replace'];
					$operation = $findreplace['operation'];
					switch($operation)
					{
						case 'replace':
							$curtemplate = str_replace($replace, $find, $curtemplate);
							break;
						case 'addabove':
						case 'addbelow':
							$curtemplate = str_replace($replace, '', $curtemplate);
							break;
					}
				}
				if ($curtemplate2 == $curtemplate)
				{
					$errors[1]["$styleid"][] = $title;
				} elseif ($curtemplate == $tomodify[-1]["$title"])
				{
					echo 'Modifying template <tt>'.$title.'</tt> ... ';
					$DB_site->query("
						DELETE FROM ".TABLE_PREFIX."template
						WHERE styleid = $styleid
						AND title = '".addslashes($title)."'
					");
					echo 'done!<br />';
				}
				else
				{
					echo 'Modifying template <tt>'.$title.'</tt> ... ';
					$DB_site->query("
						UPDATE ".TABLE_PREFIX."template
						SET template_un = '".addslashes($curtemplate)."',
						template = '".addslashes(compile_template($curtemplate))."',
						dateline = ".time().",
						username = '".addslashes($bbuserinfo['username'])."',
						version = '3.0.3'
						WHERE styleid = $styleid
						AND title = '".addslashes($title)."'
					");
					echo 'done!<br />';
				}
			}
		}
		foreach ($_styleids as $styleid)
		{
			print_rebuild_style($styleid, '', 0, 0, 0, 0);
		}
	}
	echo '<br />';

	// ############################################################################
	// Drop phrases
	echo '<b>Checking phrases</b><br />';
	echo 'Removing phrases ... ';
	$DB_site->query("
		DELETE FROM ".TABLE_PREFIX."phrase
		WHERE varname IN ($_phrasetitles)
	");
	echo 'done!<br />';

	// ############################################################################
	// Rebuild languages
	echo 'Rebuilding languages ... ';
	build_language(-1);
	echo 'done!<br /><br />';

	// ############################################################################
	// Drop setting group
	echo '<b>Checking settings</b><br />';
	$settinggroup = $DB_site->query_first("
		SELECT *
		FROM ".TABLE_PREFIX."settinggroup
		WHERE grouptitle = 'hivemail'
	");
	if (!empty($settinggroup))
	{
		echo 'Removing setting group ... ';
		$DB_site->query("
			DELETE FROM ".TABLE_PREFIX."settinggroup
			WHERE grouptitle = 'hivemail'
		");
		echo 'done!<br />';
	}

	// ############################################################################
	// Drop settings
	echo 'Removing settings ... ';
	$DB_site->query("
		DELETE FROM ".TABLE_PREFIX."setting
		WHERE varname IN ($_settingtitles)
	");
	echo 'done!<br /><br />';

	$vboptions = build_options();

	// ############################################################################
	// Errors to report?
	if ($dotemplates == 0)
	{
		echo '<b>Results</b><br />';
		echo 'You should now proceed to the <a href="index.php?loc=template.php">template editor</a> and revert the following templates which you customized for vBulletin/HiveMail&trade; integration:
		<ul>'.$temptitles.'</ul>
		You may have customized these in more than one style, so check carefully. You can review the additions you made in step 2 of the integration instructions in the HiveMail&trade; manual.<br /><br />';
	}
	elseif (!empty($errors))
	{
		echo '<b>Results</b><br />';
		echo 'The script encountered a problem when trying to modify one or more templates, as listed below. You should make note of the template(s) listed, and manually remove the modifications made to them. Essentially, reverse the steps found in step 2 of the integration instructions in the HiveMail&trade; manual.<br />';
		foreach ($errors as $errortype => $styleids)
		{
			// error types: 1 = couldn't make template mods
			echo '<ul>';
			foreach ($styleids as $styleid => $errors)
			{
				echo '<li>Style: '.$_styletitles["$styleid"].' (ID #'.$styleid.')<ul>';
				foreach ($errors as $blah => $title)
				{
						echo '<li><i>'.$title.'</i> - Couldn\'t remove template modifications</li>';
				}
				echo '</ul></li>';
			}
			echo '</ul>';
		}
	}

	// ############################################################################
	// All done
	echo '<b>All done!</b>';
}

print_cp_footer();
?>