<?php
// ------------------------------------------------------------------------- //
//                XOOPS - PHP Content Management System                      //
//                       <http://www.xoops.org/>                             //
// ------------------------------------------------------------------------- //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
// ------------------------------------------------------------------------- //

$xoopsOption['pagetype'] = "user";

include("mainfile.php");
$myts =& MyTextSanitizer::getInstance();

function userCheck($uname, $email, $pass, $vpass) {
	global $xoopsConfig, $xoopsDB;
	include(XOOPS_ROOT_PATH."/modules/system/cache/badunames.php");
	$myts =& MyTextSanitizer::getInstance();
	$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 ( $xoopsConfig['uname_test_level'] ) {
		case 0:
			$restriction = $strict;
			break;
		case 1:
			$restriction = $medium;
			break;
		case 2:
			$restriction = $loose;
			break;
	}
	if ( (!$uname) || ($uname=="") || (ereg("[".$restriction."-]",$uname)) ) {
		$stop .= _US_INVALIDNICKNAME."<br />";
	}
	if ( strlen($uname) > 25 ) {
		$stop .= _US_NICKNAMETOOLONG."<br />";
	}
	foreach ($xoopsBadUnames as $xbu) {
		if ( !empty($xbu) ) {
			if ( strtolower($xbu) == strtolower($uname) ) {
				$stop .= _US_NAMERESERVED."<br />";
				break;
			}
		}
	}
	if ( strrpos($uname,' ') > 0 ) {
		$stop .= _US_NICKNAMENOSPACES."<br />";
	}
	$sql = "SELECT COUNT(*) FROM ".$xoopsDB->prefix("users")." WHERE uname='".addslashes($uname)."'";
	$result = $xoopsDB->query($sql);
	list($count) = $xoopsDB->fetchRow($result);
	if ( $count > 0 ) {
		$stop .= _US_NICKNAMETAKEN."<br />";
	}
	$count = 0;
	if ( $email ) {
		$sql = "SELECT COUNT(*) FROM ".$xoopsDB->prefix("users")." where email='".$myts->makeTboxData4Save($email)."'";
		$result = $xoopsDB->query($sql);
		list($count) = $xoopsDB->fetchRow($result);
		if ( $count > 0 ) {
			$stop .= _US_EMAILTAKEN."<br />";
		}
	}
	if ( !isset($pass) || $pass == "" || !isset($vpass) || $vpass == "" ) {
		$stop .= _US_ENTERPWD."<br />";
	} 
	if ( (isset($pass)) && ($pass != $vpass) ) {
		$stop .= _US_PASSNOTSAME."<br />";
	} elseif ( ($pass != "") && (strlen($pass) < $xoopsConfig['minpass']) ) {
		$stop .= sprintf(_US_PWDTOOSHORT,$xoopsConfig['minpass'])."<br />";
	} 
	return $stop;
}
$op = "register";
foreach ( $HTTP_POST_VARS as $k => $v ) {
	$$k = $v;
}
switch ( $op ) {
	default:
	case "register":
		include("header.php");
		OpenTable();
		include("include/registerform.php");
		$reg_form->display();
		CloseTable();
		include("footer.php");
		break;
	case "newuser":
		$myts =& MyTextSanitizer::getInstance();
		include("header.php");
		$uname = trim($uname);
		$email = trim($email);
		$pass = trim($pass);
		$vpass = trim($vpass);
		$stop = userCheck($uname, $email, $pass, $vpass);
		if ( empty($stop) ) {
			OpenTable();
			echo ""._US_USERNAME.": ".$myts->makeTboxData4Preview($uname)."<br />";
			echo ""._US_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 />";
			echo "<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)."' />
			<br /><br /><input type='hidden' name='op' value='finish' /><input type='submit' value='". _US_FINISH ."' /></form>";
			CloseTable();
		} else {
			echo "<span style='color:#ff0000;'>$stop</span>";
			include("include/registerform.php");
			$reg_form->display();
		}
		include("footer.php");
		break;
	case "finish":
		$myts =& MyTextSanitizer::getInstance();
		include("header.php");
		$uname = trim($uname);
		$email = trim($email);
		$pass = trim($pass);
		$vpass = trim($vpass);
		$stop = userCheck($uname, $email, $pass, $vpass);
		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 ( isset($url) && $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",$xoopsConfig['com_order']);
			$newuser->setVar("umode",$xoopsConfig['com_mode']);
			$newid = $newuser->store();
			if ( !$newid ) {
				echo _US_REGISTERNG;
				include("footer.php");
				exit();
			}
			$xoopsMailer =& getMailer();
			$xoopsMailer->useMail();
			$xoopsMailer->setTemplate("register.tpl");
			$xoopsMailer->assign("SITENAME", $xoopsConfig['sitename']);
			$xoopsMailer->assign("ADMINMAIL", $xoopsConfig['adminmail']);
			$xoopsMailer->assign("SITEURL", XOOPS_URL."/");
			$xoopsMailer->setToUsers(new XoopsUser($newid));
			$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
			$xoopsMailer->setFromName($xoopsConfig['sitename']);
			$xoopsMailer->setSubject(sprintf(_US_USERKEYFOR,$uname));
			OpenTable();
			if ( !$xoopsMailer->send() ) {
				echo _US_YOURREGMAILNG;
			} else {
				echo _US_YOURREGISTERED;
			}
			CloseTable();
			if ( $xoopsConfig['new_user_notify'] == 1 && !empty($xoopsConfig['new_user_notify_group']) ) {
				$xoopsMailer =& getMailer();
				$xoopsMailer->useMail();
				$xoopsMailer->setToGroups(new XoopsGroup($xoopsConfig['new_user_notify_group']));
				$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
				$xoopsMailer->setFromName($xoopsConfig['sitename']);
				$xoopsMailer->setSubject(sprintf(_US_NEWUSERREGAT,$xoopsConfig['sitename']));
				$xoopsMailer->setBody(sprintf(_US_HASJUSTREG,$uname));
				$xoopsMailer->send();
			}
		} else {
			echo "<span style='color:#ff0000; font-weight:bold;'>$stop</span>";
			include("include/registerform.php");
			$reg_form->display();
		}
		include("footer.php");
		break;
}
?>