function makeRandomPassword() {
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
srand((double)microtime()*1000000);
$i = 0;
while ($i <= 7) {
$num = rand() % 33;
$tmp = substr($salt, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return $pass;
}
function check_email_mx($email) {
if( (preg_match('/(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/', $email)) ||
(preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/',$email)) ) {
$host = explode('@', $email);
if(checkdnsrr($host[1].'.', 'MX') ) return true;
if(checkdnsrr($host[1].'.', 'A') ) return true;
if(checkdnsrr($host[1].'.', 'CNAME') ) return true;
}
return false;
}
if(isset($_POST["add"]))
{
$link = sqlite_open("adhakal.db", 0666, $errmsg);
if (!$_POST["username"])
$uerr="You must enter a username.";
if (!$_POST["email"])
$eerr="You must enter an email address.";
if (!$uerr && !$perr && !$eerr)
{
sqlite_query ( "BEGIN TRANSACTION", $link );
$result1 = sqlite_query("select * from user_authorization where user_id='$_POST[username]'", $link);
$result2 = sqlite_query("select * from user_information where first_name='$_POST[firstname]' and last_name='$_POST[lastname]'", $link);
$result3 = sqlite_query("select * from user_information where email='$_POST[email]'", $link);
if ( sqlite_num_rows($result2) && $_POST[firstname]!="" )
{
$nerr="There is already a user with this name. If you forgot your username or password click here";
}
else
if( sqlite_num_rows($result1) )
$uerr="This username has been reserved by someone else. Please select a different one.";
else
if( sqlite_num_rows($result3) )
$eerr="Seems like this email address belongs to someone else. Select a different email or click here if you forgot your username or password";
else
if (!check_email_mx($_POST[email])) {
$eerr="Invalid Email. Please enter a valid email";
}
else
{
$random_password = makeRandomPassword ();
$password = md5($random_password);
$result1 = sqlite_query("insert into user_authorization values ('$_POST[username]','$random_password', '$password', 0, 1, datetime('now'), datetime('now') )", $link);
if ( !$result1 )
{
echo "
There has been an error creating your account. Please contact the administrator at adhakal@gmail.com
\n";
sqlite_query("ROLLBACK TRANSACTION", $link);
exit;
}
$result2 = sqlite_query("insert into user_information values ('$_POST[username]', '$_POST[firstname]', '$_POST[lastname]', '$_POST[phone]', '$_POST[email]')", $link);
if ( !$result2 )
{
echo "
There has been an error creating your account. Please contact the administrator at adhakal@gmail.com
\n";
sqlite_query("ROLLBACK TRANSACTION", $link);
exit;
}
$subject = "Membership at http://students.cs.byu.edu/~adhakal";
$body = "Dear $_POST[firstname] $_POST[lastname],\nThank you for registering at our website http://students.cs.byu.edu/~adhakal\nYour are two steps away from logging in and accessing our exclusive membership area.\nTo activate, click on the following link:\n\thttp://students.cs.byu.edu/~adhakal/activate.php?username=$_POST[username]&code=$password\n\nOnce you activate, you will be able to login with following information:\n\t\tUSERNAME=$_POST[username]\n\t\tPASSWORD=$random_password\nThank you.\n\nSincerely,\nAdministrator(adhakal@gmail.com)\nhttp://students.cs.byu.edu/~adhakal\n\nP.S. This is an automated email. Please do not respond.";
if ( mail($_POST[email], $subject, $body) )
{
echo "Thank you for registering with us. Your membership information has been emailed to you. Please follow instructions in the email
";
echo "Please click here to go back to the login page";
sqlite_query("COMMIT TRANSACTION", $link);
}
else
{
echo "Looks like the CS mail daemon is down. Please try again later.";
sqlite_query("ROLLBACK TRANSACTION", $link);
}
exit;
}
}
}
?>