<?php
session_start();
if ($finame>=1){
echo "Prego effettuare il login <a href=\"/admin/index.php\"> Qui<br>
&copy;Bloghosteria.com<br>
</a>";
}
 elseif  ( empty( $finame ) ) {
?> Prego effettuare il login <a href="/admin/index.php"> Qui<br>
&copy;Bloghosteria.com<br>
</a>
<?php
} elseif ($finame==$_SESSION['finame']) {
include ("headnavadmin.php"); 
include ("italiano.php");
include 'db.php';

// Define post fields into simple variables
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$username = $_POST['username'];
$info = $_POST['info'];

/* Lets strip some slashes in case the user entered
any escaped characters. */

$first_name = stripslashes($first_name);
$last_name = stripslashes($last_name);
$email_address = stripslashes($email_address);
$username = stripslashes($username);
$info = stripslashes($info);


/* Do some error checking on the form posted fields */

if((!$first_name) || (!$last_name) || (!$email_address) || (!$username)){
	echo "$lang0112 <br />";
	if(!$first_name){
		echo "$lang0113 <br />";
	}
	if(!$last_name){
		echo "$lang0114 <br />";
	}
	if(!$email_address){
		echo "$lang0115 <br />";
	}
	if(!$username){
		echo "$lang0116 <br />";
	}
	include 'joinform2.php'; // Show the form again!
	/* End the error checking and if everything is ok, we'll move on to
	 creating the user account */
	exit(); // if the error checking has failed, we'll exit the script!
}

/* Let's do some checking and ensure that the user's email address or username
 does not exist in the database */
 $sql_last_name_check = mysql_query("SELECT last_name FROM users WHERE last_name='$last_name'");
 $sql_email_check = mysql_query("SELECT email_address FROM users WHERE email_address='$email_address'");
 $sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'");
 
 $last_name_check = mysql_num_rows($sql_last_name_check);
 $email_check = mysql_num_rows($sql_email_check);
 $username_check = mysql_num_rows($sql_username_check);

 if(($email_check > 0) || ($username_check > 0) || ($last_name_check > 0)){
 	echo "$lang0117 <br />";
 	if($email_check > 0){
 		echo "<strong>$lang0118<br />";
 		unset($email_address);
 	}
 	if($username_check > 0){
 		echo "$lang0119<br />";
 		unset($username);
 	}
	if($last_name_check > 0){
 		echo "$lang0120<br />";
 		unset($last_name);
 	}
 
   include ("joinform2.php");// Show the form again!
 	exit();  // exit the script so that we do not create this account!
 }

/* Everything has passed both error checks that we have done.
It's time to create the account! */

/* Random Password generator.
http://www.phpfreaks.com/quickcode/Random_Password_Generator/56.php

We'll generate a random password for the
user and encrypt it, email it and then enter it into the db.
*/

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;
}

$random_password = makeRandomPassword();

$db_password = md5($random_password);

// Enter info into the Database.
$info2 = htmlspecialchars($info);
$sql = mysql_query("INSERT INTO users (first_name, last_name, email_address, username, password, info, signup_date, decrypted_password, via, citta, cap, piva, telef, fax, cogn, nome, web, prov, codfisc)
		VALUES('$first_name', '$last_name', '$email_address', '$username', '$db_password', '$info2', now(), '$random_password', '$via', '$citta', '$cap', '$piva', '$telef', '$fax', '$cogn', '$nome', '$web', '$prov', '$codfisc')") or die (mysql_error());

if(!$sql){
	echo '$lang0121';
} else {
	$userid = mysql_insert_id();
	$activatepath = "activate.php?id=$userid&code=$db_password";
	// Let's mail the user!
	$subject = "$lang0122 $sitename";
	$message = "$lang0003 $first_name $last_name,
	on $sitepath !

	

	$lang0068
	Username: $username
	Password: $random_password
	

	Ciao!
	Webmaster, $sitename



	$lang0069";

	mail($email_address, $subject, $message, "From: $sitename <$adminemail>\nX-Mailer: PHP/" . phpversion());
	
}
echo "<style type=\"text/css\">

a:link, a:visited {
	color: #009933; text-decoration: none;
	font-weight: bold;
}

a:hover {
	color: #ff6600;	text-decoration: none;
	font-weight: bold;	
}
.style2 {
color: #009933;
font-weight: bold;
}
.style3 {
color: #FF0000;
font-weight: bold;
}
</style><p>$lang0123</p> <table width=300 border=1 cellspacing=0 cellpadding=0.3>
  <tr>
    <td width=89>Ditta</td>
    <td width=205>$last_name</td>
  </tr>
   <tr>
    <td width=89>Nome</td>
    <td width=205>$nome</td>
  </tr>
  <tr>
    <td width=89>Cognome</td>
    <td width=205>$cogn</td>
  </tr>
  <tr>
    <td>Via</td>
    <td>$via</td>
  </tr>
  <tr>
    <td>Citt&agrave;</td>
    <td>$citta</td>
  </tr>
  <tr>
    <td width=89>Prov./Stato</td>
    <td width=205>$prov</td>
  </tr>
  <tr>
    <td>Cap</td>
    <td>$cap</td>
  </tr>
   <tr>
    <td>cdfisc</td>
    <td>$codfisc</td>
  </tr>
  <tr>
    <td>P.Iva</td>
    <td>$piva</td>
  </tr>
  <tr>
    <td>Email</td>
    <td>$email_address</td>
  </tr>
  <tr>
    <td>Tel.</td>
    <td>$telef</td>
  </tr>
  <tr>
    <td>Fax</td>
    <td>$fax</td>
  </tr>
  <tr>
    <td>login</td>
    <td>$username</td>
  </tr>
 <tr>
    <td>password</td>
    <td>$random_password</td>
  </tr> 
</table><p>$lang0124 oppure se errato <a href=\"modfdati2.php?last_name=$last_name\">clicca qui</a> per reinserire i dati. </p>";
if ($first_name=="Cli"){
$motreg="regamm.php";
} else {
$motreg="regamm2.php";
}
echo "<html>
<head>

<meta http-equiv=Content-Type content=text/html; charset=iso-8859-1><style type=text/css>
<!--
body {
	margin-left: 0px;
	margin-top: 0px;
}
-->
</style></head>
<body>

<form name=form1 method=post action=$motreg>
 
        
     <input name=form[first_name] type=hidden value=$first_name id=first_name>
  
      <input name=form[Ragione_Sociale] type=hidden value=\"$last_name\" id=Ragione_sociale maxlength=100 size=50 >

      <input name=form[Cognome] type=hidden value=\"$cogn\" id=cognome maxlength=30 size=30 >
 
     <input name=form[Nome] type=hidden value=\"$nome\" id=nome maxlength=30 size=30>
 
     <input name=form[CF] type=hidden value=$codfisc id=CF maxlength=16 size=16>
   
    
      <input name=form[PI] type=hidden value=$piva id=PI maxlength=16 size=12>

     <input name=form[Indirizzo] type=hidden value=\"$via\" id=indirizzo maxlength=60 size=50>
    
      <input name=form[Citta] type=hidden value=\"$citta\" id=citta maxlength=60 size=50>
   
      <input name=form[Provincia] type=hidden value=$prov id=prov maxlength=2 size=2>

      <input name=form[CAP] type=hidden value=$cap id=cap maxlength=5 size=5>
   
      <input name=form[Telefono] type=hidden value=$telef id=telefono maxlength=20 size=20>
  
      <input name=form[Fax] type=hidden value=$fax id=fax maxlength=20 size=20>
 
      <input name=form[Web] type=hidden value=$web id=web maxlength=50 size=50>

     <input name=form[E_mail] type=hidden value=$email_address id=email>
 
      <input type=submit name=Submit value=$lang0056>
  

</form>

</body>
</html>";
	   
 } else {
echo "Prego effettuare il login <a href=\"/admin/index.php\"> Qui<br>
&copy;Bloghosteria.com<br>
</a>";
}  
?>
