<?php
// $Id: registration.php,v 1.28 2004/03/15 23:36:32 rcastley Exp $
/**
* Registration code
* @package Mambo Open Source
* @Copyright (C) 2000 - 2003 Miro International Pty Ltd
* @ All rights reserved
* @ Mambo Open Source is Free Software
* @ Released under GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @version $Revision: 1.28 $
**/

defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

$task = mosGetParam( $_REQUEST, 'task', "" );
require_once( $mainframe->getPath( 'front_html' ) );

switch( $task ) {
	case "lostPassword":
	lostPassForm( $option );
	break;

	case "sendNewPass":
	sendNewPass( $option );
	break;

	case "register":
	registerForm( $option, $mosConfig_emailpass );
	break;

	case "saveRegistration":
	saveRegistration( $option );
	break;
}

function lostPassForm( $option ) {
	HTML_registration::lostPassForm($option);
}

function sendNewPass( $option ) {
	global $database, $Itemid;
	global $mosConfig_live_site, $mosConfig_sitename;

	$_live_site = $mosConfig_live_site;
	$_sitename = $mosConfig_sitename;

	// ensure no malicous sql gets past
	$checkusername = trim( mosGetParam( $_POST, 'checkusername', '') );
	$checkusername = $database->getEscaped( $checkusername );
	$confirmEmail = trim( mosGetParam( $_POST, 'confirmEmail', '') );
	$confirmEmail = $database->getEscaped( $confirmEmail );

	$database->setQuery( "SELECT id FROM #__users"
	. "\nWHERE username='$checkusername' AND email='$confirmEmail'"
	);

	if (!($user_id = $database->loadResult()) || !$checkusername || !$confirmEmail) {
		mosRedirect( "index.php?option=$option&task=lostPassword&mosmsg="._ERROR_PASS );
	}

	$database->setQuery( "SELECT name, email FROM #__users"
	. "\n WHERE usertype='superadministrator'" );
	$rows = $database->loadObjectList();
	foreach ($rows AS $row) {
		$adminName = $row->name;
		$adminEmail = $row->email;
	}

	$newpass = makePass();
	$message = _NEWPASS_MSG;
	eval ("\$message = \"$message\";");
	$subject = _NEWPASS_SUB;
	eval ("\$subject = \"$subject\";");
	$headers = "";
	$headers .= "From: ".$adminName." <".$adminEmail.">\r\n";
	$headers .= "Reply-To: <".$adminEmail.">\r\n";
	$headers .= "X-Priority: 3\r\n";
	$headers .= "X-MSMail-Priority: Low\r\n";
	$headers .= "X-Mailer: Mambo Open Source 4.5\r\n";

	mail( $confirmEmail, $subject, $message, $headers );

	$newpass = md5( $newpass );
	$sql = "UPDATE #__users SET password='$newpass' WHERE id='$user_id'";
	$database->setQuery( $sql );
	if (!$database->query()) {
		die("SQL error" . $database->stderr(true));
	}

	mosRedirect( "index.php?Itemid=$Itemid&mosmsg="._NEWPASS_SENT );
}

function makePass(){
	$makepass="";
	$salt = "abchefghjkmnpqrstuvwxyz0123456789";
	srand((double)microtime()*1000000);
	$i = 0;
	while ($i <= 7) {
		$num = rand() % 33;
		$tmp = substr($salt, $num, 1);
		$makepass = $makepass . $tmp;
		$i++;
	}
	return ($makepass);
}

function registerForm( $option, $emailpass ) {
	global $mainframe, $database, $my, $acl;

	if (!$mainframe->getCfg( 'allowUserRegistration' )) {
		mosNotAuth();
		return;
	}

	HTML_registration::registerForm($option, $emailpass);
}

function saveRegistration( $option ) {
	global $database, $my, $acl;
	global $mosConfig_live_site, $mosConfig_emailpass, $mosConfig_allowUserRegistration;

	if ($mosConfig_allowUserRegistration=="0") {
		mosNotAuth();
		return;
	}

	$row = new mosUser( $database );
	
	if (!$row->bind( $_POST )) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}

	mosMakeHtmlSafe($row);

	$pwd = '';
	$row->gid = $acl->get_group_id('Registered','ARO');

	if ($row->password == '') {
		$pwd = makePass();
		$row->password = md5( $pwd );
	} else {
		$pwd = $row->password;
		$row->password = md5( $row->password );
	}

	$row->registerDate = date("Y-m-d\TH:i:s");

	if (!$row->check()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}

	if (!$row->store()) {
		echo "<script> alert('".$row->getError()."'); window.history.go(-1); </script>\n";
		exit();
	}
	$row->checkin();

	$name = $row->name;
	$email = $row->email;
	$username = $row->username;
	$newpass = $pwd;

	$subject = _SEND_SUB;
	if ($mosConfig_emailpass=="0"){
		$message = _USEND_MSG_NOPASS;
		eval ("\$message = \"$message\";");
	} else {
		$message = _USEND_MSG;
		eval ("\$message = \"$message\";");
	}

	// Send email to user
	$database->setQuery( "SELECT name, email FROM #__users"
	."\n WHERE usertype='superadministrator' AND sendEmail='1'" );
	$rows = $database->loadObjectList();
	$row = $rows[0];
	$adminName2 = $row->name;
	$adminEmail2 = $row->email;

	$headers = "From: ".$adminName2." <".$adminEmail2.">\r\n";
	$headers .= "Reply-To: <".$adminEmail2.">\r\n";
	$headers .= "X-Priority: 3\r\n";
	$headers .= "X-MSMail-Priority: Low\r\n";
	$headers .= "X-Mailer: Mambo Open Source 4.5\r\n";
	@mail($email, $subject, $message, $headers);

	// Send notification to all administrators
	$subject2 = _SEND_SUB;
	$message2 = _ASEND_MSG;
	eval ("\$message2 = \"$message2\";");

	$database->setQuery( "SELECT name, email FROM #__users"
	."\n WHERE usertype='superadministrator' AND sendEmail='1'" );
	$rows = $database->loadObjectList();

	foreach ($rows AS $row) {
		$adminName2 = $row->name;
		$adminEmail2 = $row->email;
		$headers2 = "From: ".$adminName2." <".$adminEmail2.">\r\n";
		$headers2 .= "Reply-To: <".$adminEmail2.">\r\n";
		$headers2 .= "X-Priority: 3\r\n";
		$headers2 .= "X-MSMail-Priority: Low\r\n";
		$headers2 .= "X-Mailer: Mambo Open Source 4.5\r\n";
		@mail($adminEmail2, $subject2, $message2, $headers2);
	}

	if ($mosConfig_emailpass == "0"){
		echo "&nbsp;&nbsp;"._REG_COMPLETE_NOPASS;
	} else {
		echo "&nbsp;&nbsp;"._REG_COMPLETE;
	}

}

function is_email($email){
	$rBool=false;

	if(preg_match("/[\w\.\-]+@\w+[\w\.\-]*?\.\w{1,4}/", $email)){
		$rBool=true;
	}
	return $rBool;
}
?>
