<?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: vbulletin_plugin.php,v $ - $Revision: 1.28 $
// | $Date: 2004/08/03 04:28:45 $ - $Author: tubedogg $
// +-------------------------------------------------------------+

error_reporting(E_ALL & ~E_NOTICE);

// ############################################################################
// Stop people from calling this file directly
if (!defined('FORUM_PLUGIN')) {
	return;
}

// ############################################################################
// Change to right directory
$vB_folder = getcwd();
chdir(dirname(__FILE__));

// ############################################################################
// We are outside the admin CP
define('INADMIN', false);

// ############################################################################
// Start database connection
require('../includes/config.php');
require('../includes/db_mysql.php');
//$config['persistent'] = !$usepconnect;
if (!function_exists('version_compare') or version_compare(phpversion(), '4.2.0') < 0) {
	$config['server'] .= ':3306';
}
$DB_Hive = new DB_MySQL($config, true, true);

// ############################################################################
// Get options
$DB_Hive->setup_options();

// ############################################################################
// Get all common functions
require('../includes/init.php');

// ############################################################################
// Registration screen
$hive_domainname_options = '';
foreach ($_options['domainnames'] as $curdomainname) {
	$hive_domainname_options .= "<option value=\"$curdomainname\"".iif($userdomain == $curdomainname, ' selected="selected"', '').">$curdomainname</option>\n";
}
$hive_primary_domain = $_options['domainname'];

// ############################################################################
// Creates a user
function hivemail_register_user($forumuserid, $password, $onreg = true) {
	global $DB_Hive, $DB_site, $_options, $bbuserinfo;

	if (!$_options['regopen']) {
		return;
	}

	$ipaddress = getenv('HTTP_CLIENT_IP') or $ipaddress = getenv('HTTP_X_FORWARDED_FOR') or $ipaddress = getenv('REMOTE_ADDR');
	$ipaddress = trim(preg_replace('#^([^,]+)(,.*)?#', '$1', $ipaddress));

	// Domain name
	if (!in_array($_POST['hive_userdomain'], $_options['domainnames'])) {
		$_POST['hive_userdomain'] = $_options['domainname'];
	}

	// Some error checking
	if ($DB_Hive->query_first("SELECT alias FROM hive_alias WHERE alias = '".addslashes($_POST['hive_username'])."'")) {
		if ($onreg) {
			hivemail_delete_user($forumuserid);
		}
		eval(print_standard_error('error_hivemail_nametaken'));
		exit;
	} elseif (preg_match('#[^a-z0-9_.]#i', $_POST['hive_username']) or preg_match('#^[^a-z]#i', $_POST['hive_username']) or strlen($_POST['hive_username']) < 2) {
		if ($onreg) {
			hivemail_delete_user($forumuserid);
		}
		eval(print_standard_error('error_hivemail_nameillegal'));
		exit;
	}

	// User options
	$defuseroptions = unserialize($DB_Hive->get_field('SELECT value FROM hive_setting WHERE variable = "defuseroptions"'));

	// Create user
	$DB_Hive->query("
		INSERT INTO hive_user
		(userid,
		username,
		password,
		regipaddr,
		usergroupid,
		altemail,
		skinid,
		languageid,
		realname,
		regdate,
		lastvisit,
		cols,
		birthday,
		options,
		options2,
		replyto,
		font,
		timezone,
		soundid,
		domain,
		aliases,
		forumuserid)
		VALUES
		(NULL,
		'".addslashes($_POST['hive_username'])."',
		'".addslashes(iif(strlen($password) == 32, $password, md5($password)))."',
		'".addslashes($ipaddress)."',
		".iif($_options['moderate'], 3, 2).",
		'".addslashes($_POST['email'])."',
		".$_options['defaultskin'].",
		".$_options['defaultlang'].",
		'".addslashes(iif($onreg, $_POST['username'], $bbuserinfo['username']))."',
		".TIMENOW.",
		".TIMENOW.",
		'".addslashes(USER_DEFAULTCOLS)."',
		'".addslashes($birthday)."',
		$defuseroptions[0],
		$defuseroptions[1],
		'".addslashes($_POST['hive_username'].$_POST['hive_userdomain'])."',
		'Verdana|10|Regular|Black|None',
		'".addslashes($_POST['timezoneoffset'])."',
		".intval($DB_Hive->get_field('SELECT soundid FROM hive_sound WHERE userid <= 0 ORDER BY userid LIMIT 1')).", '".addslashes($_POST['hive_userdomain'])."',
		'".addslashes($_POST['hive_username'])."',
		$forumuserid)
	");
	$hive_userid = $DB_Hive->insert_id();
	$DB_Hive->query("
		INSERT INTO hive_alias
		SET userid = $hive_userid, alias = '".addslashes($_POST['hive_username'])."'
	");

	// Log user in
	if ($onreg) {
		$userid = $forumuserid;
		ini_set('session.name', SESSION_VARNAME);
		session_start();
		$_SESSION['userid'] = $hive_userid;
		$_SESSION['password'] = md5(md5($password));
		$_SESSION['ipaddress'] = md5(IPADDRESS);
		$_SESSION['staylogged'] = 365;
		vbsetcookie(session_name(), session_id(), TIMENOW + (60 * 60 * 24 * 365));
		vbsetcookie(session_name(), session_id(), TIMENOW + (60 * 60 * 24 * 365));
		vbsetcookie('hive_userid', $hive_userid, TIMENOW + (60 * 60 * 24 * 365));
		vbsetcookie('hive_password', md5(md5($password)), TIMENOW + (60 * 60 * 24 * 365));
		$DB_Hive->query("
			INSERT INTO hive_iplog
			VALUES
			(NULL, $hive_userid, ".TIMENOW.", ".TIMENOW.", '".IPADDRESS."')
		");
		$forumuserid = $userid;
	}

	// Update vBulletin's database
	$DB_site->query("
		UPDATE ".TABLE_PREFIX."user
		SET hiveuserid = $hive_userid
		WHERE userid = $forumuserid
	");

	return $hive_userid;
}

// ############################################################################
// Delete user
function hivemail_delete_user($forumuserid) {
	global $DB_site;

	$DB_site->query("
		DELETE FROM ".TABLE_PREFIX."user
		WHERE userid = $forumuserid
	");
	$DB_site->query("
		DELETE FROM ".TABLE_PREFIX."usertextfield
		WHERE userid = $forumuserid
	");
}

// ############################################################################
// Updates the user's password
function hivemail_update_password($password, $forumuserid) {
	global $DB_Hive;

	$DB_Hive->query("
		UPDATE hive_user
		SET password = '".addslashes($password)."'
		WHERE forumuserid = $forumuserid
	");
}

// ############################################################################
// Switch back to vB's folder
chdir($vB_folder);

?>