<?php
$ref = $_SERVER['HTTP_REFERER'];

if($ref !== 'https://client.agilantsecurity.com/ext/stage1.html') {
  die("Hotlinking not permitted");
}
include('/var/www/html/client/dirs.php');
$reps=$_GET['reps'];
$reps_chosen=$_GET['yesno'];
   $fullname=$_GET['fullname'];
   $username=$_GET['username'];
   $password=$_GET['password'];
   $email=$_GET['email'];
   //echo "$username " . " $password " . " $email";
$clientip=$_SERVER['REMOTE_ADDR'];
$str3 = bin2hex(openssl_random_pseudo_bytes(62));
$dirmake = substr(sha1(time()), 0, 10);
var_export($dirmake, $email);
putenv("clientip_shell=$clientip");
putenv("dirmake_shell=$dirmake");
putenv("email_shell=$email");
putenv("username_shell=$username");
putenv("fullname_shell=$username");
putenv("reps_shell=$reps");
shell_exec("$shell_script_path/lookup.sh");
sleep(4);
$data = file_get_contents("$tmp_path/$dirmake.good");
if(strpos($data, 'good') !== FALSE)
{
   conTinuanceGlobal();
}
else
{
   dieHereGlobal();
}
function dieHereGlobal(){
	global $dirmake;
	global $msg_path;
	include("$msg_path/$dirmake.html");
	sleep(1);
	shell_exec("rm $tmp_path/$dirmake.good");
	shell_exec("rm $msg_path/$dirmake.html");
}
// this point down needs to become a function called conTinuance()
//
function conTinuanceGlobal() {

   global $fullname;
   global $username;
   global $password;
   global $email;
   global $clientip;
   global $reps;
   global $dirmake;
   global $str3;
   global $tmp_path;
   global $shell_script_path;
   global $msg_path;
   global $ext_path;
   global $template_path;
putenv("clientip_shell=$clientip");
putenv("dirmake_shell=$dirmake");
putenv("email_shell=$email");
putenv("username_shell=$username");
putenv("fullname_shell=$username");
putenv("reps_shell=$reps");
shell_exec("mkdir $ext_path/$dirmake");
shell_exec("chmod -R 7777 $ext_path/$dirmake/*");
shell_exec("chown -R www-data:www-data $ext_path/$dirmake/*");
shell_exec("echo 'order deny,allow' >> $ext_path/$dirmake/.htaccess");
shell_exec("echo 'deny from all' >> $ext_path/$dirmake/.htaccess");
shell_exec("echo 'allow from $clientip' >> $ext_path/$dirmake/.htaccess");
shell_exec("cp $template_path/stage2.html $ext_path/$dirmake/$str3");
shell_exec("sed -i 's/WelcomeCus/$fullname/g' $ext_path/$dirmake/$str3");
shell_exec("rm  $ext_path/$dirmake.good");
$output = shell_exec("echo 'https://client.agilantsecurity.com/vault/$dirmake/$str3' >> $ext_path/$dirmake/$dirmake.txt");
$pwd_enc = shell_exec("sudo openssl passwd -apr1 $password");
shell_exec("echo '$username:$pwd_enc' >>  /software/repos/httpdb/$username.db");
$to = "$email";
shell_exec("echo 'AuthType Basic' >> $ext_path/$dirmake/.htaccess");
shell_exec("echo 'AuthName restricted' >> $ext_path/$dirmake/.htaccess");
shell_exec("echo 'AuthUserFile /software/repos/httpdb/$username.db' >> $ext_path/$dirmake/.htaccess");
shell_exec("echo 'require valid-user' >> $ext_path/$dirmake/.htaccess");
$subject = "Agilant Welcomes You";
$txt = file_get_contents("$ext_path/$dirmake/$dirmake.txt");
$headers = "From: noreply@agilantsecurity.com" . "\r\n" .
"CC: somebodyelse@example.com";
mail($to,$subject,$txt,$headers);
// code in write to a common file so that reports can be run on emails
include("$msg_path/$dirmake.html");
//echo "$reps";
sleep(1);
shell_exec("rm $tmp_path/$dirmake.good");
shell_exec("rm $msg_path/$dirmake.html");
}
if (empty($reps)){
  shell_exec("$shell_script_path/sales_picker.sh");
}
if (isset($reps)) {
	//	shell_exec("echo $reps >> $ext_path/$dirmake/exec.txt");
	shell_exec("$shell_script_path/sales_chosen.sh");
	
}
// cleanup!
shell_exec("rm $tmp_path/$dirmake.good");
shell_exec("rm $msg_path/$dirmake.html");
   ?>
