<?
  include_once("config.php");
include_once("main_header.php");
  $link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die ("Could not connect");

  @mysql_select_db(DB_NAME, $link);
  
  function signup_step1() {

?>
<center><h1>Sign Up</h1></center>
<form action="signup.php?step=2" method="post">
<table width="100%">
 <tr>
  <td width="10%">
  </td>
  <td width="30%" valign="top" align="right">
   Name:<br>
  </td>
  <td>
   <input type="text" name="username" size="30"><br>All Lowercase,No Spaces!
  </td>
 </tr>
 <tr>
  <td>
  </td>
  <td valign="top" align="right">
   Password:<br>
  </td>
  <td>
   <input type="password" name="password" size="30">
  </td>
 </tr>
 <tr>
  <td>
  </td>
  <td valign="top" align="right">
   Confirm password:<br>
  </td>
  <td>
   <input type="password" name="password2" size="30">
  </td>
 </tr>
 <tr>
  <td>
  </td>
  <td valign="top" align="right">
   E-Mail:<br>
  </td>
  <td>
   <input type="text" name="email" size="30">
  </td>
 </tr>
</table>
<center><br><input type="submit" value="  Sign Up  "></center>
</form>
<? 
    return(0);
  }

  function signup_step2() {
    global $link;
    global $siteurl;
    global $sitename;

    $username = htmlspecialchars($_POST['username']);
    $password = htmlspecialchars($_POST['password']);
    $password2 = htmlspecialchars($_POST['password2']);
    $email = htmlspecialchars($_POST['email']);

    if ($password!=$password2) {
      echo "Confirm password disabled";
      signup_step1();
      return(11);
    }

    if (($username=="") || ($password=="") || ($email=="")) {
      echo "You must fill all fields";
      signup_step1();
      return(12);
    }

    $r=mysql_query("select * from blogusers where username='".mysql_escape_string($username)."'",$link);
    if (mysql_num_rows($r)) {
      echo "Such user has already registered";
      signup_step1();
      return(13);
    }

    $code=activation_const+time();

    $r=mysql_query("insert into blogusers(username,password,template,code) values('".mysql_escape_string($username)."','".mysql_escape_string($password)."','1','$code')",$link);

    if ($r) {
      echo "<center>To protect your account, your address is being verified.<br>";
      echo "An email was just sent to:<br>";
      echo "<b>$email</b></center>";

      $body="This message has been sent automatically by $sitename.\n\n";
      $body.="Go to the following URL to continue the signup process:\n\n";
      $body.="$siteurl"."signup.php?step=3&user=$username&code=$code&email=$email";

      mail($email,"Confirm Email",$body);

    }
 
    return(0);
  }

  function signup_step3() {
    global $link;

    $username=$_GET['user'];
    $code=$_GET['code'];
$email=$_GET['email'];
    $r=mysql_query("select code from blogusers where username='".mysql_escape_string($username)."'",$link);
    list($code2)=mysql_fetch_row($r);

    $filename="templates/css/template1.css";

    $fp1=fopen($filename,"r");
    $fp2=fopen("users_css/"."$username".".css","w+");

    $contents = fread ($fp1, filesize ($filename));
    fwrite($fp2,$contents);    

    fclose($fp2);
    fclose($fp1);

    if ($code==$code2) {
      $r=mysql_query("update blogusers set code='0' where username='".mysql_escape_string($username)."'",$link);
      echo "<center>Your account has succesfully registered</center>";
      echo "<center><a href=\"wp-admin/install.php?bloguser=$username&email=$email\">Click here</a> to install your blog</center>";
    }

    return(0);
  }

  if (isset($_GET['step'])) {
    $step=$_GET['step'];
    if ($step==2) { 
      signup_step2();
    }
    if ($step==3) { 
      signup_step3();
    }
  }
  else {
    signup_step1();
  }  

include_once("main_footer.php");
?>