<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.3 www.exoops.info 2006/05
//v1.2 www.exoops.info 2006/03 
// $Id: register.php,v 1.14 2003/07/10 20:45:12 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: The Xoops Project
// Author Website : http://www.xoops.org
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //
$xoopsOption['pagetype'] = 'user';
$xoopsOption['page_style'] = 8;
$xoopsOption['nocache']  = 1;
include_once("./mainfile.php");
if ( empty($bcoosConfig['allow_register']) ) {
	redirect_header('index.php', 1, _US_NOREG);
	exit();
}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function userCheck($uname, $email, $pass, $vpass, $verify_text, $verify_crc) {
global $bcoosConfig, $db, $myts, $xoopsBadEmails, $xoopsBadUnames;
$stop = "";
if ( !checkEmail($email) ) {
	$stop .= _US_INVALIDMAIL."<br />";
}
if ( strrpos($uname,' ') > 0 ) {
	$stop .= _US_EMAILNOSPACES."<br />";
}
$uname  = $myts->oopsStripSlashesGPC($uname);
$strict = "^a-zA-Z0-9_";
$medium = $strict."<>,.$%#@!\'\"";
$loose  = $medium."?{}\[\]\(\)\^&*`~;:\\+=";
switch ( $bcoosConfig['uname_test_level'] ) {
	case 0:
		$restriction = $strict;
		break;
	case 1:
		$restriction = $medium;
		break;
	case 2:
		$restriction = $loose;
		break;
	}
if ( empty($uname) || preg_match('#['.$restriction.'-]#', $uname) ) {
	$stop .= _US_INVALIDNICKNAME."<br />";
}
if ( strlen($uname) > 25 ) {
	$stop .= _US_NICKNAMETOOLONG."<br />";
}
/*
if ( !empty($xoopsBadUnames) && hasMatch($xoopsBadUnames, $uname) ) {
			$stop .= _US_NAMERESERVED."<br />";
			break;
		}
*/		
if ( strrpos($uname, ' ') > 0 ) {
	$stop .= _US_NICKNAMENOSPACES."<br />";
}
$sql    = "SELECT COUNT(*) FROM ".$db->prefix("users")." WHERE uname='".addslashes($uname)."'";
$result = $db->query($sql);
list($count) = $db->fetchRow($result);
if ($count > 0) {
	$stop .= _US_NICKNAMETAKEN."<br />";
}
$count = 0;
if ($email) {
	/*
	if ( !empty($xoopsBadEmails) && hasMatch($xoopsBadEmails, $email) ) {
				$stop .= _US_EMAILRESERVED."<br />";
				break;
			}
	*/		
	$sql    = "SELECT COUNT(*) FROM ".$db->prefix("users")." WHERE email='".$myts->makeTboxData4Save($email)."'";
	$result = $db->query($sql);
	list($count) = $db->fetchRow($result);
		if ( $count > 0 ) {
			$stop .= _US_EMAILTAKEN."<br />";
		}
}
if ( empty($pass) || empty($vpass) ) {
	$stop .= _US_ENTERPWD."<br />";
}
if ( (isset($pass)) && ($pass != $vpass) ) {
	$stop .= _US_PASSNOTSAME."<br />";
	} elseif ( ($pass != "") && (strlen($pass) < $bcoosConfig['minpass']) ) {
		$stop .= sprintf(_US_PWDTOOSHORT, $bcoosConfig['minpass'])."<br />";
	}
if ($bcoosConfig['img_verify'] == 1 && function_exists('imagecreate')) {
	include_once(XOOPS_ROOT_PATH."/class/rc4.crypt.php");
	$rc4 = new rc4_crypt();
	$rc4->key(strtoupper($verify_text));
	$verify_crc = base64_decode($verify_crc);
	$rc4->decrypt($verify_crc);
	if ( empty($verify_text) || $verify_crc != "verified" ) {
		$stop .= _US_BADVERIF."<br />";
	}
}
return $stop;
}
//---------------------------------------------------------------------------------------//
if ( empty($bcoosConfig['agree_register']) ) { $op = "register"; } else { $op = "agree";}	
foreach ($_POST as $k => $v) {
	$$k = $v;
}
switch ($op) {
case "agree":
	include_once("header.php");
	OpenTable();
	include_once("include/agreeform.php");
	$reg_form->display();
	CloseTable();
	include_once("footer.php");
	break;
case "register":
	$agreeok = 0;
	if ($bcoosConfig['agree_register']) {
		if ( $agree_yesno) { $agreeok = 1; 
		} else {
			redirect_header('index.php', 1, _AGREE_REQUIRED);
			exit();
		}
	}
	include_once("header.php");
	OpenTable();
	include_once("include/registerform.php");
	$reg_form->display();
	CloseTable();
	include_once("footer.php");
	break;
case "newuser":
	include_once("header.php");
	$uname       = trim($uname);
	$email       = trim($email);
	$pass        = trim($pass);
	$vpass       = trim($vpass);
	$verify_text = trim($verify_text);
	$verify_crc  = trim($verify_crc);
	$stop        = userCheck($uname, $email, $pass, $vpass, $verify_text, $verify_crc);
	if ( empty($stop) ) {
		OpenTable();
		echo ""._US_USERNAME.": ".$myts->makeTboxData4Preview($uname)."<br />";
		echo ""._EMAIL.": ".$myts->makeTboxData4Preview($email)."<br />";
		if ( $user_avatar != "" ) {
			echo ""._US_AVATAR.": <img src='images/avatar/".$user_avatar."' alt='' /><br />";
			}
		if ( isset($url) && $url != "" ) {
			$url = formatURL($myts->makeTboxData4Preview($url));
			echo ""._US_WEBSITE.": $url<br />";
		}
		$f_timezone = ($timezone_offset < 0) ? "GMT ".$timezone_offset : "GMT +".$timezone_offset;
		echo
		_US_TIMEZONE.": $f_timezone<br />
		<form action='register.php' method='post'>
		<input type='hidden' name='uname' value='".$myts->makeTboxData4PreviewInForm($uname)."' />
		<input type='hidden' name='email' value='".$myts->makeTboxData4PreviewInForm($email)."' />
		<input type='hidden' name='user_viewemail' value='".intval($user_viewemail)."' />
		<input type='hidden' name='user_avatar' value='".$myts->makeTboxData4PreviewInForm($user_avatar)."' />
		<input type='hidden' name='timezone_offset' value='".(float)$timezone_offset."' />
		<input type='hidden' name='url' value='".$myts->makeTboxData4PreviewInForm($url)."' />
		<input type='hidden' name='pass' value='".$myts->makeTboxData4PreviewInForm($pass)."' />
		<input type='hidden' name='vpass' value='".$myts->makeTboxData4PreviewInForm($vpass)."' />
		<input type='hidden' name='user_mailok' value='".intval($user_mailok)."' />
		<input type='hidden' name='verify_text' value='".$myts->makeTboxData4PreviewInForm($verify_text)."' />
		<input type='hidden' name='verify_crc' value='".$myts->makeTboxData4PreviewInForm($verify_crc)."' />
		<br /><br /><input type='hidden' name='op' value='finish' /><input type='submit' class='button' value='". _US_FINISH ."' />
		</form>";
		CloseTable();
		} else {
			echo "<span style='color:#ff0000;'>$stop</span>";
			include_once("include/registerform.php");
			$reg_form->display();
		}
	include_once("footer.php");
	break;
case "finish":
	include_once("header.php");
	$uname       = trim($uname);
	$email       = trim($email);
	$pass        = trim($pass);
	$vpass       = trim($vpass);
	$verify_text = trim($verify_text);
	$verify_crc  = trim($verify_crc);
	$stop        = userCheck($uname, $email, $pass, $vpass, $verify_text, $verify_crc);
	if ( empty($stop) ) {
		$newuser = new XoopsUser();
		if ( isset($user_viewemail) ) {
			$newuser->setVar("user_viewemail", $user_viewemail);
		}
		if ( isset($attachsig) ) {
			$newuser->setVar("attachsig",$attachsig);
		}
		$newuser->setVar("name" , $name);
		$newuser->setVar("uname", $uname);
		$newuser->setVar("email", $email);
		if ( !empty($url) ) {
			$newuser->setVar("url", formatURL($url));
		}
		$newuser->setVar("user_avatar", $user_avatar);
		$newuser->setVar("actkey", substr(md5(makepass()), 0, 8));
		$newuser->setVar("pass", md5($pass));
		$newuser->setVar("timezone_offset", $timezone_offset);
		$newuser->setVar("user_regdate", time());
		$newuser->setVar("uorder", $bcoosConfig['com_order']);
		$newuser->setVar("umode", $bcoosConfig['com_mode']);
		$newuser->setVar("user_mailok", $user_mailok);
		$newid = $newuser->store();
		if ( !$newid ) {
			OpenTable();
			echo _US_REGISTERNG;
			foreach($newuser->errors as $value) {
				echo "<br />-->".$value;
			}
			CloseTable();
			include_once("footer.php");
			exit();
		}
		if (!empty($bcoosConfig['auto_register'])) {
			$newuser->setVar("uid", $newid);
			if ($newuser->activate()) {
				OpenTable();
				printf(_US_AUTOREGISTERED, $uname);
				CloseTable();
				include_once("footer.php");
				exit();
			}
		}
		$xoopsMailer =& getMailer();
		$xoopsMailer->useMail();
		if ($bcoosConfig['coppa']) {
			$xoopsMailer->setTemplate("coppa.tpl");
			$xoopsMailer->assign("UEMAIL"  , $email);
			$xoopsMailer->assign("UWEBSITE", $url);
			$xoopsMailer->assign("UNAME"   , $uname);
			$xoopsMailer->assign("UPASS"   , $pass);
			} else {
				$xoopsMailer->setTemplate("register.tpl");
			}
		$xoopsMailer->setSubject(sprintf(_US_USERKEYFOR, $uname));
		$xoopsMailer->assign("SITENAME", $bcoosConfig['title']);
		$xoopsMailer->assign("ADMINMAIL", $bcoosConfig['adminmail']);
		$xoopsMailer->assign("SITEURL", XOOPS_URL."/");
		$xoopsMailer->setToUsers($newid);
		$xoopsMailer->setFromEmail($bcoosConfig['adminmail']);
		$xoopsMailer->setFromName($bcoosConfig['title']);
		$xoopsMailer->setPriority(2);
		OpenTable();
		if ( !$xoopsMailer->send() ) {
			echo _US_YOURREGMAILNG;
			foreach($xoopsMailer->errors as $value) {
				echo "<br />-->".$value;
			}
			} else {
				echo _US_YOURREGISTERED;
			}
		CloseTable();
		if ( $bcoosConfig['new_user_notify'] == 1 && !empty($bcoosConfig['new_user_notify_group']) ) {
			$xoopsMailer =& getMailer();
			$xoopsMailer->useMail();
			$xoopsMailer->setToGroups($bcoosConfig['new_user_notify_group']);
			$xoopsMailer->setFromEmail($bcoosConfig['adminmail']);
			$xoopsMailer->setFromName($bcoosConfig['title']);
			$xoopsMailer->setSubject(sprintf(_US_NEWUSERREGAT, $bcoosConfig['title']));
			$xoopsMailer->setBody(sprintf(_US_HASJUSTREG, $uname));
			$xoopsMailer->send();
		}
		} else {
			echo "<span style='font-weight:bold;'>$stop</span>";
			include_once("include/registerform.php");
			$reg_form->display();
		}
	include_once("footer.php");
	break;
}
?>
