<?php
// $Id: register.php,v 1.1 2004/07/11 10:29:14 Farsus Exp $
// -------------------------------------------------------------------------//
//			       RUNCMS                                                   //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// 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($xoopsConfig['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 $xoopsConfig, $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 ( $xoopsConfig['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->fetch_row($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->fetch_row($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) < $xoopsConfig['minpass']) ) {
		$stop .= sprintf(_US_PWDTOOSHORT, $xoopsConfig['minpass'])."<br />";
	}

if ($xoopsConfig['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;
}

//---------------------------------------------------------------------------------------//
$op = "register";
foreach ($HTTP_POST_VARS as $k => $v) {
	$$k = $v;
}

switch ($op) {

case "register":
	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 ""._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 />
		<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", $xoopsConfig['com_order']);
		$newuser->setVar("umode", $xoopsConfig['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($xoopsConfig['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 ($xoopsConfig['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", $meta['title']);
		$xoopsMailer->assign("ADMINMAIL", $xoopsConfig['adminmail']);
		$xoopsMailer->assign("SITEURL", XOOPS_URL."/");
		$xoopsMailer->setToUsers($newid);
		$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
		$xoopsMailer->setFromName($meta['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 ( $xoopsConfig['new_user_notify'] == 1 && !empty($xoopsConfig['new_user_notify_group']) ) {
			$xoopsMailer =& getMailer();
			$xoopsMailer->useMail();
			$xoopsMailer->setToGroups($xoopsConfig['new_user_notify_group']);
			$xoopsMailer->setFromEmail($xoopsConfig['adminmail']);
			$xoopsMailer->setFromName($meta['title']);
			$xoopsMailer->setSubject(sprintf(_US_NEWUSERREGAT, $meta['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;
}
?>
