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

if($ref !== 'https://client.agilantsecurity.com/ext/form1.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'];
   $compname=$_GET['compname'];
   $secret_code = substr(sha1(time()), 0, 5);
$clientip=$_SERVER['REMOTE_ADDR'];
$str3 = bin2hex(openssl_random_pseudo_bytes(62));
$dirmake = substr(sha1(time()), 0, 22);
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");
putenv("compname_shell=$compname");
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 $secret_code;
   global $compname;
   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=$fullname");
putenv("reps_shell=$reps");
putenv("compname_shell=$compname");
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");
// added
shell_exec("cp $template_path/return2.php $ext_path/$dirmake/$str3.php");
shell_exec("cp $template_path/simp2.php $ext_path/$dirmake/");
// -------------------
//shell_exec("sed -i 's/WelcomeCus/$fullname/g' $ext_path/$dirmake/stage5.dev");
shell_exec("rm  $ext_path/$dirmake.good");
$output = shell_exec("echo 'https://client.agilantsecurity.com/vault/$dirmake/$str3.php?name=$secret_code&email=$email' >> $ext_path/$dirmake/$dirmake.txt");
shell_exec("echo '$secret_code' >> $ext_path/$dirmake/secode");
//$pwd_enc = shell_exec("sudo openssl passwd -apr1 $password");
//shell_exec("echo '$username:$pwd_enc' >>  /software/repos/httpdb/$username.db");
$to = "$email";
//--- changed 1-16
//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");

//}
if ($reps == "none") {
	shell_exec("$shell_script_path/sales_picker.sh");
}
else {
	shell_exec("$shell_script_path/sales_chosen.sh");
}
// cleanup!
shell_exec("rm $tmp_path/$dirmake.good");
shell_exec("rm $msg_path/$dirmake.html");
shell_exec("echo $email $dirmake >> /software/repos/maps/dir.maps");
shell_exec("cp $template_path/stage5.template $ext_path/$dirmake/stage5.dev");
shell_exec("sed -i 's/WelcomeCus/$fullname/g' $ext_path/$dirmake/stage5.dev");
shell_exec("cp $this_dir/init_scan.php $ext_path/$dirmake/init_scan.php");
shell_exec("echo '$dirmake' >> $ext_path/$dirmake/dirmake");
shell_exec("cp $template_path/passpromp.php $ext_path/$dirmake/passpromp.php");
shell_exec("cp -r $template_path/css $ext_path/$dirmake/css");
shell_exec("cp -r $template_path/scss $ext_path/$dirmake/scss");
shell_exec("cp -r $template_path/js $ext_path/$dirmake/js");
shell_exec("cp -r $template_path/homepage.html $ext_path/$dirmake/homepage.html");
shell_exec("sed -i 's/CustomerName/$compname/g'  $ext_path/$dirmake/homepage.html");
shell_exec("cp -r $template_path/extscan.html $ext_path/$dirmake/extscan.html");
shell_exec("sed -i 's/CustomerName/"$compname"/g'  $ext_path/$dirmake/extscan.html");
   ?>
