<?  

//
//  cP Account Creator Version 0.4 Beta Build 01
//  MODS
//  1.  Signup Email MOD
//  2.  Password Protected Directory and User Creation MOD
//  3.  Scripts Mod
//  Copyright (C) 2004 Brian Freeman
//  All Rights Reserved.
//
//  This script set is distributed as is with absolutely no warranty.
//  Users of this script set agree to use at their own risk.
//  Questions or comments?  Contact webmaster@freemans-web.com
//
//


include("config.php");
include("template/header.php");
error_reporting(E_ERROR | E_PARSE);
$email=strtolower($email);

$cp_email="$email@$domain";

 $conn  = mysql_connect($dbhost, $dbusername, $dbpass) or die(mysql_error( ));
mysql_select_db($dbname,$conn) or die(mysql_error( ));

//  The scripting below compares values to those entered in the banned table
//  and will not allow adding the user if a match is encountered.  Any of these
//  may be disabled by setting the variable, i.e. $multi_reg = 0 or by commenting
//  out the entire section.

// Determines if the IP has already been used for registration.

//  If the users IP is already in the email_users table it will return at
//  least one row, causing the $multi_reg variable to be set to 1.
$get_items="SELECT * FROM email_users WHERE user_ip='$user_ip'";
$get_items_res=mysql_query($get_items)  or die(mysql_error( ));

if(mysql_num_rows($get_items_res)>=1)  {

$multi_reg="0";
}

//  Determines if the IP is on the banned list
$get_items="SELECT * FROM banned WHERE banned_ip='$user_ip'";
$get_items_res=mysql_query($get_items)  or die(mysql_error( ));

if(mysql_num_rows($get_items_res)>=1)  {

$banned_ip="1";
}

//  Determines if the host is on the banned list
$get_items="SELECT * FROM banned WHERE banned_host='$user_host'";
$get_items_res=mysql_query($get_items)  or die(mysql_error( ));

if(mysql_num_rows($get_items_res)>=1)  {

$banned_host="1";
}

//  Determines if the alt email address is on the banned list
$get_items="SELECT * FROM banned WHERE banned_email='$alt_email'";
$get_items_res=mysql_query($get_items)  or die(mysql_error( ));

if(mysql_num_rows($get_items_res)>=1)  {

$banned_email="1";
}

//  Determines if the alt email is from a free email service.
//  Parses the email to determine email server.
  $freemail = chop($alt_email);

  $banemail = explode("@", $freemail);

  for ($i = 0; $i < count($banemail); $i++) {
    $banemail[$i] = urlencode($banemail[$i]);
  }
$banfree=$banemail[1];

//  If email server is in database, the result will return 1 or more rows.
$get_items="SELECT * FROM banned WHERE free_email='$banfree'";
$get_items_res=mysql_query($get_items)  or die(mysql_error( ));

if(mysql_num_rows($get_items_res)>=1)  {

$free_email="1";
}



//  If any of the below variables are set to 1 by the scripting above the text
//  will be shown on the screen and the script will terminate without adding
//  the user.  You may want to change the text so as not to reveal the reason.
if($multi_reg=="1") {

echo "You may not register for more than one free account from a single IP address";

}elseif($banned_ip=="1") {
echo "You are attempting to register using a banned IP address";

}elseif($banned_host=="1") {
echo "You are attempting to register using a banned host";

}elseif($banned_email=="1") {
echo "You are attempting to register using a banned email address";

}elseif($free_email=="1") {
echo "You are attempting to register using a free email service email address";

}elseif ($first_name=="") {

echo "First Name is blank.  Go <a href=addpop.php>Back</a>";

}elseif($last_name=="") {

echo "Last Name is blank.  Go <a href=addpop.php>Back</a>";

}elseif($alt_email=="") {

echo "Alternate Email is blank.  Go <a href=addpop.php>Back</a>";

}elseif($email=="") {

echo "User Name is blank.  Go <a href=addpop.php>Back</a>";

}elseif($password=="") {

echo "Password is blank.  Go <a href=addpop.php>Back</a>";

}elseif($quota=="") {

echo "Script Error.  Go <a href=addpop.php>Back</a>";

} elseif(strpos($alt_email, "@") == false ) {

echo "You have enter an invalid email address.  Go <a href=addpop.php>Back</a>";

}elseif ($autocreate==1) {


// Add POP account

$request = "/frontend/$cpmod/mail/doaddpop.html?email=$email&domain=$domain&password=$password&quota=$quota"; 

$result = cprq($host,$port,$ownername,$reseller_pass,$request);



// cPanel confirm Added to fail duplicate emails



$show = strip_tags($result);

if ($debugon==1) {
echo "$show<br><br>";
}

if (strpos($show, "already") !== false && strpos($show, "exists") !== false) {

echo "Account was NOT Created.  Email user already exists.<a href=addpop.php>Go Back</a><br>";

// End Duplicate

} else {

include("admin/date_time.php");
$put_items="INSERT INTO email_users SET approved='0', cp_email = '$cp_email', email ='$alt_email', sub_domain='$usr_subdomain', first_name = '$first_name', last_name = '$last_name', password=password('$password'), username='$email', raw_pwd='$password',quota='$quota', user_ip='$user_ip', user_hostname='$user_host', date='$date_time' ";

$put_items_res=mysql_query($put_items)  or die(mysql_error( ));



if($add_script==1){

$script="http://$ownername:$reseller_pass@$host:2082/frontend/$cpmod/addoncgi/install.html?addonname=$add_on&auser=$email&apass=$password&url=$scriptdir1$scriptdir2";

($fp = fopen("$script", "r")) or die("Could not open file!");
if(!$fp){
    echo "Error:  Forum Creation Failed.";

}else{
echo"Script Installed.  Visit <a href=http://$domain/$scriptdir1$scriptdir2>http://$domain/$scriptdir1$scriptdir2</a> and login with username $email and password $password<br><br>";

}
}
// Add SQL database

if($add_sql==1) {

$request = "/frontend/$cpmod/sql/adddb.html?db=$email";

$result = cprq($host,$port,$ownername,$reseller_pass,$request);

$show = strip_tags($result);

if ($debugon==1) {
echo "$show<br><br>";
}
}

// Add Sub Domain

if($add_sub==1) {

$request = "/frontend/$cpmod/subdomain/doadddomain.html?rootdomain=$usr_subdomain&domain=$email";

$result = cprq($host,$port,$ownername,$reseller_pass,$request);

$show = strip_tags($result);

if ($debugon==1) {
echo "$show<br><br>";
}

}
// Add SQL user

if($add_sql==1) {

$sqlusr="http://$ownername:$reseller_pass@$host:2082/frontend/$cpmod/sql/adduser.html?user=$email&pass=$password";
($fp = fopen($sqlusr, "r")) or die("Could not open file!");
if(!$fp){
    echo "Error:  Database username may be corrupted contact the administrator";
}else{
  echo "Database User Created";
}

if ($debugon==1) {
echo "$show<br><br>";
}
}
// Add FTP User

if($add_ftp==1) {

if($add_sub==0){
	if($usr_subdomain!=""){
	$dir=explode(".",$usr_subdomain);
	$homedir="/$dir[0]/$email";
	}else{
	$homedir="/$email";
	}
}else{
$homedir="/$email";
}


$request = "/frontend/$cpmod/ftp/doaddftp.html?login=$email&password=$password&homedir=$homedir&quota=$ftp_lim";

$result = cprq($host,$port,$ownername,$reseller_pass,$request);

$show = strip_tags($result);

if ($debugon==1) {
echo "$show<br><br>";
}

}






// Add SQL user to database

if($add_sql==1) {
$db_user1=$ownername;
$db_user2="_" ;
$db_user3=$email;
$request = "/frontend/$cpmod/sql/addusertodb.html?user=$db_user1$db_user2$db_user3&db=$db_user1$db_user2$db_user3&ALL=ALL";
$result = cprq($host,$port,$ownername,$reseller_pass,$request);
$show = strip_tags($result);
if ($debugon==1) {
echo "$show<br><br>";
}
}

//added 071605 Adds skel directory to new users directory
if($add_ftp==1){
   // set the various variables
   $ftproot = "/";
$srcdir="skel/";
$srcroot="$srcroot1$srcdir";


   // connect to the destination FTP & enter appropriate directories both locally and remotely
   $ftpc = ftp_connect($host);
   $ftpr = ftp_login($ftpc,"$email@$domain",$password);

   if ((!$ftpc) || (!$ftpr)) { echo "FTP connection not established!"; die(); }
   if (!chdir($srcroot)) { echo "Could not enter local source root directory."; die(); }
   if (!ftp_chdir($ftpc,$ftproot)) { echo "Could not enter FTP root directory."; die(); }

$file2 = 'index.php';
include("../skel_index.php");

// Let's make sure the file exists and is writable first.
if (is_writable($file2)) {
$handle = fopen($file2, "w");
   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($file2, 'a')) {
         echo "Cannot open file ($file2)";
         exit;
   }

   // Write $somecontent to our opened file.
   if (fwrite($handle, $buffer) === FALSE) {
       echo "Cannot write to file ($file2)";
       exit;
   }

   echo "Success, wrote  to file ($file2)<br>";

   fclose($handle);

} else {
   echo "The file $filename is not writable";
}

   // start ftp'ing over the directory recursively
   ftpRec($srcrela);




if (($inst_siteframe=="1")&&($add_sql==1)){
mysql_close($conn);

if(($usr_subdomain!="")&&($add_sub==1)){
$sf_path="$email.$usr_subdomain/web";
}elseif(($usr_subdomain=="")&&($add_sub==1)){
$sf_path="$email.$domain/web";
}else{
$sf_path="$domain$homedir/web";
}

include("inst_sql/sf_install.php");

$conn  = mysql_connect($dbhost, $dbusername, $dbpass) or die(mysql_error( ));
mysql_select_db($dbname,$conn) or die(mysql_error( ));

$srcdir="skel_2/";
$srcroot="$srcroot1$srcdir";


   if ((!$ftpc) || (!$ftpr)) { echo "FTP connection not established!"; die(); }
   if (!chdir($srcroot)) { echo "Could not enter local source root directory."; die(); }
   if (!ftp_chdir($ftpc,$ftproot)) { echo "Could not enter FTP root directory."; die(); }
$file4 = 'web/config.php';
include("../sf_config.php");

// Let's make sure the file exists and is writable first.
if (is_writable($file4)) {
$handle = fopen($file4, "w");
   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($file4, 'a')) {
         echo "Cannot open file ($file4)";
         exit;
   }

   // Write $somecontent to our opened file.
   if (fwrite($handle, $buffer) === FALSE) {
       echo "Cannot write to file ($file4)";
       exit;
   }

   echo "Success, wrote to file ($file4)<br>";

   fclose($handle);

} else {
   echo "The file $filename is not writable";
}

    // start ftp'ing over the directory recursively
   ftpRec($srcrela);
}

if (($inst_forum=="1")&&($add_sql==1)){
mysql_close($conn);
                   if(($usr_subdomain!="")&&($add_sub==1)){
                   $forum_server="$email.$usr_subdomain";
                   $forum_path="/forum";
                   }elseif(($usr_subdomain=="")&&($add_sub==1)){
                   $forum_server="$email.$domain";
                   $forum_path="/forum";
                   }else{
                   $forum_server="$domain";
                   $forum_path="$homedir/forum";
                   }
include("inst_sql/forum_install.php");
$conn  = mysql_connect($dbhost, $dbusername, $dbpass) or die(mysql_error( ));
mysql_select_db($dbname,$conn) or die(mysql_error( ));

$srcdir="skel_1/";
$srcroot="$srcroot1$srcdir";

   if ((!$ftpc) || (!$ftpr)) { echo "FTP connection not established!"; die(); }
   if (!chdir($srcroot)) { echo "Could not enter local source root directory."; die(); }
   if (!ftp_chdir($ftpc,$ftproot)) { echo "Could not enter FTP root directory."; die(); }
$file3 = 'forum/config.php';
include("../forum_config.php");

// Let's make sure the file exists and is writable first.
if (is_writable($file3)) {
$handle = fopen($file3, "w");
   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($file3, 'a')) {
         echo "Cannot open file ($file3)";
         exit;
   }

   // Write $somecontent to our opened file.
   if (fwrite($handle, $buffer) === FALSE) {
       echo "Cannot write to file ($file3)";
       exit;
   }

   echo "Success, wrote to file ($file3)<br>";

   fclose($handle);

} else {
   echo "The file $filename is not writable";
}

    // start ftp'ing over the directory recursively
   ftpRec($srcrela);
}

if (($inst_guestbook=="1")&&($add_sql==1)){
mysql_close($conn);
                   if(($usr_subdomain!="")&&($add_sub==1)){
                   $forum_server="$email.$usr_subdomain";
                   $forum_path="/forum";
                   }elseif(($usr_subdomain=="")&&($add_sub==1)){
                   $forum_server="$email.$domain";
                   $forum_path="/forum";
                   }else{
                   $forum_server="$domain";
                   $forum_path="$homedir/forum";
                   }
include("inst_sql/guestbook_install.php");
$conn  = mysql_connect($dbhost, $dbusername, $dbpass) or die(mysql_error( ));
mysql_select_db($dbname,$conn) or die(mysql_error( ));

$srcdir="skel_3/";
$srcroot="$srcroot1$srcdir";

   if ((!$ftpc) || (!$ftpr)) { echo "FTP connection not established!"; die(); }
   if (!chdir($srcroot)) { echo "Could not enter local source root directory."; die(); }
   if (!ftp_chdir($ftpc,$ftproot)) { echo "Could not enter FTP root directory."; die(); }
$file3 = 'guestbook/config.php';
include("../guestbook_config.php");

// Let's make sure the file exists and is writable first.
if (is_writable($file3)) {
$handle = fopen($file3, "w");
   // In our example we're opening $filename in append mode.
   // The file pointer is at the bottom of the file hence
   // that's where $somecontent will go when we fwrite() it.
   if (!$handle = fopen($file3, 'a')) {
         echo "Cannot open file ($file3)";
         exit;
   }

   // Write $somecontent to our opened file.
   if (fwrite($handle, $buffer) === FALSE) {
       echo "Cannot write to file ($file3)";
       exit;
   }

   echo "Success, wrote to file ($file3)<br>";

   fclose($handle);

} else {
   echo "The file $filename is not writable";
}

    // start ftp'ing over the directory recursively
   ftpRec($srcrela);
}









   // close the FTP connection
   ftp_close($ftpc);
}
//end add


if($add_sub==0) {
  $sub_domain="$domain$homedir";
  $act_sub=0;
}
if($add_sub==1) {
  if($usr_subdomain==""){
$sub_domain="$email.$domain";
 }else{
 $sub_domain="$email.$usr_subdomain";
}
$act_sub=1;
}

include("template/add_email.php");

$recipient=$admin_email;

$subject="New $domain Account created";

$mailheaders="From:  Site Admin <admin@$domain>\n";

mail ($recipient, $subject, $msg_admin_email, $mailheaders);
include("admin/date_time.php");
//$put_items="INSERT INTO email_users SET cp_email = '$cp_email', email ='$alt_email', sub_domain='$sub_domain', act_sub='$act_sub',first_name = '$first_name', last_name = '$last_name', password=password('$password'), username='$email', raw_pwd='$password',quota='$quota', user_ip='$user_ip', user_hostname='$user_host', approved='1', date='$date_time'";
$put_items="UPDATE email_users SET approved='1', act_sub='1', sub_domain='$sub_domain' WHERE username='$email'";
$put_items_res=mysql_query($put_items)  or die(mysql_error( ));
echo $panel_info_disp;
   echo $web_info_disp;

   echo $mail_info_disp;

if ($add_ftp==1) {

  echo $ftp_info_disp;
}

if ($add_sql==1) {

  echo $sql_info_disp;
}
if ($inst_forum==1){
  echo $forum_info_disp;
}
if ($inst_siteframe==1){
  echo $siteframe_info_disp;
}
if ($inst_guestbook==1){
  echo $guestbook_info_disp;
}
if ($pphlogger==1){
  echo $pphlogger_info_disp;
}





if($info_email==1){
$recipient=$alt_email;
$subject="New user account on $domain";

$mailheaders="From:  Site Admin <admin@$domain>\n";

mail ($recipient, $subject, $msg_user_email, $mailheaders);
}



}

}else {

$get_items="SELECT * FROM email_users WHERE username='$email'";
$get_items_res=mysql_query($get_items)  or die(mysql_error( ));

if(mysql_num_rows($get_items_res)>=1)  {

echo "Email address already exists.  Go <a href=addpop.php>Back</a>";
} else {




$recipient=$admin_email;

$subject="New user account request needs approval";

$msg="username $email has been created with password of $password";

$mailheaders="From:  Site Admin <admin@$domain>\n";

mail ($recipient, $subject, $msg, $mailheaders);



if($add_sub==0){
	if($usr_subdomain!=""){
	$dir=explode(".",$usr_subdomain);
	$homedir="/$dir[0]/$email";
	}else{
	$homedir="/$email";
	}
}else{
$homedir="/$email";
}


if($add_sub==0) {
  $sub_domain="$domain$homedir";
  $act_sub=0;
  }
if($add_sub==1) {
  if($usr_subdomain==""){
$sub_domain="$email.$domain";
 }else{
 $sub_domain="$email.$usr_subdomain";
}
$act_sub=1;
}
include("admin/date_time.php");
$put_items="INSERT INTO email_users SET cp_email = '$cp_email', email ='$alt_email', sub_domain='$sub_domain', act_sub='$act_sub',first_name = '$first_name', last_name = '$last_name', password=password('$password'), username='$email', raw_pwd='$password',quota='$quota', user_ip='$user_ip', user_hostname='$user_host', date='$date_time' ";

$put_items_res=mysql_query($put_items)  or die(mysql_error( ));

echo "Your application has been accepted and is awaiting approval by the site administrator.";
}
}


echo "<br><br>";
include("template/footer.php");


end;



function cprq($host,$port,$ownername,$reseller_pass,$request)  {
global $email,$db_user1,$db_user2,$db_user3,$password;



$sock = fsockopen($host,$port);

if(!$sock)

{

print('Socket error');

exit();

}

$authstr = "$ownername:$reseller_pass";

$pass = base64_encode($authstr);

$in  = "GET $request\r\n";

$in .= "HTTP/1.0\r\n";

$in .= "Host:$domain\r\n";

$in .= "Authorization: Basic $pass\r\n";

$in .= "\r\n";





   fputs($sock, $in);

   while (!feof($sock))  {

// fgets ($sock,128);

$result .= fgets ($sock,128);

}

fclose( $sock );

return $result;


}

//added 071605
function ftpRec($srcrela)
{
   global $srcroot;
   global $ftproot;
   global $ftpc;
   global $ftpr;
   global $inst_forum;

if($inst_forum==0){
  $sk_dir="forum";
}else{
  $sk_dir="templ";
}
   // enter the local directory to be recursed through
   chdir($srcroot.$srcrela);

   // check if the directory exists & change to it on the destination
   if (!ftp_chdir($ftpc,$ftproot.$srcrela))
   {
       // remote directory doesn't exist so create & enter it
          ftp_mkdir    ($ftpc,$ftproot.$srcrela);
          ftp_chdir    ($ftpc,$ftproot.$srcrela);
   }

   if ($handle = opendir("."))
   {
      while (false !== ($fil = readdir($handle)))
      {
          if ($fil != "." && $fil != "..")
          {
              // check if it's a file or directory
              if (!is_dir($fil))
              {
                  // it's a file so upload it
                  ftp_put($ftpc, $ftproot.$srcrela.$fil, $fil, FTP_BINARY);
               }
               else
               {
                   // it's a directory so recurse through it
                   if ($fil == $sk_dir)
                   {
                           // I want the script to ignore any directories named "templates"
                           // and therefore, not recurse through them and upload their contents
                   }
                   else
                   {
                       ftpRec($srcrela.$fil."/");
                       chdir("../");
                   }
               }
         }
     }
     closedir($handle);
   }
}
//end add
?>







