Skip to content

Instantly share code, notes, and snippets.

@alexanderankin
Last active December 22, 2015 15:07
Show Gist options
  • Save alexanderankin/d48bc9bdabab45fae1d5 to your computer and use it in GitHub Desktop.
Save alexanderankin/d48bc9bdabab45fae1d5 to your computer and use it in GitHub Desktop.
<?php
function rmdir_recursive ($dir) {
foreach (scandir ($dir) as $file) {
if ('.' === $file || '..' === $file)
continue;
if (is_dir ("$dir/$file"))
rmdir_recursive ("$dir/$file");
else
unlink ("$dir/$file");
}
rmdir ($dir);
}
if (isset($_FILES["zip_file"])) {
$errors = array();
$filename = $_FILES["zip_file"]["name"];
$source = $_FILES["zip_file"]["tmp_name"];
$type = $_FILES["zip_file"]["type"];
$file_size = $_FILES["zip_file"]['size'];
$file_ext=strtolower(end(explode('.',$_FILES['zip_file']['name'])));
$name = explode (".", $filename);
/* PHP current path */
// $path = dirname (__FILE__) . '/'; // absolute path to the directory with this .php file
$path = "/usr/share/matrix-gui-2.0/"; //absolute path to the directory with this .php
$filenoext = basename ($filename, $file_ext);
$targetdir = "/tmp/" . $filenoext . '/'; // target directory
$targetzip = $path . "install/" . $filename; // target zip file
$logpath = "/data/logs/";
/* create directory if not exists', otherwise overwrite */
/* target directory is same as filename without extension */
if (is_dir ($targetdir)) {
rmdir_recursive ($targetdir);
}
// mkdir ($targetdir, 0777);
exec ("echo $source and $targetzip $filenoext > /tmp/junk");
/* unpacking */
if (move_uploaded_file ($source, $targetzip)) {
$logfile = $logpath . "current_install.log";
exec('/bin/bash -c "exec nohup setsid /ipg/bin/update_ipg.sh '.$targetzip.' > '.$logfile.' 2>&1 &"');
//$logfile = $targetdir . $filenoext . "_install.log";
//exec ("/bin/sh " . $targetdir . $filenoext . "_install.sh > " . $logfile);
/*$log = exec ("/bin/cat " . $logfile);*/
$message = "&quot;" . $filename . "&quot; uploaded and installed.";
} else {
$message = "There was a problem with the upload.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Install firmware</title>
<script>
// Only do when page loads.
document.addEventListener("DOMContentLoaded", function(event) {
// do every .5 seconds
setInterval(function() {
// make new request
var request = new XMLHttpRequest();
// set params to request
request.open('GET', /** This needs to be a php page that returns the commented line at the bottom of this file. **/, true);
// store function for when result is loaded.
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
var resp = request.responseText;
document.getElementById('output').text(resp);
} else {
// We reached our target server, but it returned an error
}
};
// store function for when error happens
request.onerror = function() {
// There was a connection error of some sort
};
// perform request
request.send();
}, 500); // .5 seconds
});
</script>
</head>
<body style="text-align:center">
<p style="text-align:center">
<form enctype="multipart/form-data" method="post" action="">
<H3>Select installation package:</H3><br>
<input type="file" name="zip_file"/><br><br>
<ul>
<li>Sent file: <?php echo $_FILES['zip_file']['name']; ?>
<li>File size: <?php echo $_FILES['zip_file']['size']; ?>
<li>File type: <?php echo $_FILES['zip_file']['type'] ?>
</ul>
<input type="submit" name="submit" value="Install"/><br>
</form>
</p>
<?php
if($message) echo "<p style='text-align:center'>$message</p>";
if ($logfile) {
echo "<table align=center><tr><td>";
echo "<pre style='text-align:left' id=\"output\">";
// echo htmlspecialchars (file_get_contents($logfile), ENT_COMPAT, "UTF-8", true);
echo "</pre>";
echo "</table>";
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment