<?php
$helpbits='';
$logbits='';
$ibb="";
exec('man rsync > /tmp/rsync_in.aaa');
$helpbits=@file_get_contents('/tmp/rsync_in.aaa');
$helpbits=str_replace("\n", "~", str_replace('"','`',$helpbits));
unlink('/tmp/rsync_in.aaa');
if (isset($_POST['dfile'])) {
 if ($_POST['rsync'] != '') {
  if ($_POST['dfile'] != '') {
   if ($_POST['sfile'] != '') {
    if ($_POST['aswitch'] != '') {
     if (strpos(urldecode($_POST['aswitch']), "@") === false) $ibb=" ";
     if ($_POST['rpassword'] != '') {
      file_put_contents('/tmp/rsync_in.ans', urldecode($_POST['rpassword']));
      exec(urldecode($_POST['rsync']) . ' ' . urldecode($_POST['aswitch']) . $ibb . urldecode($_POST['sfile']) . ' ' . urldecode($_POST['dfile']) . ' < /tmp/rsync_in.ans ' . ' > /tmp/rsync_out.log 2> /tmp/rsync_out.err');
     } else {
      exec(urldecode($_POST['rsync']) . ' ' . urldecode($_POST['aswitch']) . $ibb . urldecode($_POST['sfile']) . ' ' . urldecode($_POST['dfile']) . ' > /tmp/rsync_out.log 2> /tmp/rsync_out.err');
     }
     if (file_exists('/tmp/rsync_in.ans')) {
        unlink('/tmp/rsync_in.ans');
     }
     if (file_exists('/tmp/rsync_out.log')) {
        $logbits=@file_get_contents('/tmp/rsync_out.log');
        $logbits .= "\n";
        unlink('/tmp/rsync_out.log');
     }
     if (file_exists('/tmp/rsync_out.err')) {
        $logbits.=@file_get_contents('/tmp/rsync_out.err');
        unlink('/tmp/rsync_out.err');
     }
    }
   }
  }
 }
}
?>
<!doctype html>
<html>
<head>
<title>PHP Supervises rsync Backup</title>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<script type='text/javascript'>
 function passmaybe(thisv) {
   if (thisv.indexOf('@') != -1) {
     if (document.getElementById('rdpassword').innerHTML == '') {
       document.getElementById('rdpassword').innerHTML="  Remote Access Password: <input type='password' name='rpassword' value=''></input>";
     }
   } 
 }
 
 function andthen() {
   if (document.URL.toLowerCase().indexOf('http://localhost') != -1) {
     return true;
   }
   return false;
 }
 
 function help() {
   <?php global $helpbits; echo "\n" . ' document.getElementById("ta").innerHTML="' . $helpbits . '".replace(/~/g, String.fromCharCode(10)); ' . "\n"; ?>
   document.getElementById('ta').style.display='inline';
 }
</script>
</head>
<body style='background-color:yellow;'>
<?php global $logbits;  if ($logbits != '') { echo "<div align='center'>" . $logbits . "</div><br>"; } ?>
<h1 align='center' id='myh1'>Linux Backup</h1><br><br>
<div align='center'>
<form onsubmit='return andthen();' action='./rsync_backup.php' method='POST' id='myform' style='background-color:pink;'>
<br><br><input type='text' readonly value='rsync ' name='rsync' id='rsync'></input><a href='#' onclick='help();'>? </a><textarea style='display:none;' id='ta' rows=19 cols=80 value=''></textarea><br><br>
Switch (add u for incremental and add -e ssh user@server_IP: for remote (good with -avzP)): <input onchange='passmaybe(this.value);' type='text' value='-rv' name='aswitch' id='aswitch'></input><br><br>
<span>Local/Remote Directory to be Backed Up: <input type='text' name='sfile' id='sfile' value=''></input><div id='rdpassword'></div></span><br><br>
Local Media Destination Directory: <input type='text' name='dfile' id='dfile' value=''></input><br><br><br>
<input type='submit' value='Backup Now'></input><br><br>
</form>
</div>
</body>
</html>
