Skip to content

Instantly share code, notes, and snippets.

@nprussell
Created June 14, 2016 12:16
Show Gist options
  • Save nprussell/f4d6cc3d8cefa7d2fca0e7875aa3699d to your computer and use it in GitHub Desktop.
Save nprussell/f4d6cc3d8cefa7d2fca0e7875aa3699d to your computer and use it in GitHub Desktop.
<?php session_start();?>
<?php include 'db.php';
if(isset($_SESSION['display_name'])){
$username = $_SESSION['username'];
$display_name = $_SESSION['display_name'];
}
?>
<?php
$target_dir = "/var/www/upst/data/updates/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
if(isset($_POST["submit"])) {
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo " The file was not uploaded.";
// if everything is ok, try to upload file
} else {
$handle = fopen($_FILES["fileToUpload"]["tmp_name"], 'rb');
if( ! $handle ) {
throw new Exception( "Can't open file" );
}
$buffer = fread( $handle, 14 );
while (($read = fgetc($handle)) !== "\0") {
$buffer .= $read;
}
$header_format =
'a3Magic/' .
'v1UpdaterVer/' .
'C1TestingMode/' .
'V2BuildTime/' .
'a*BuildVersion' ;
$header = unpack ($header_format, $buffer);
$magic = $header['Magic'];
$upVer = $header['UpdaterVer'];
$testM = $header['TestingMode'];
$buildTime64 = $header['BuildTime1'] + ($header['BuildTime2'] * 0x100000000);
$buildVer = $header['BuildVersion'];
fclose( $handle );
$stringOfArray = implode(',', $header);
$id = hash('sha1', $stringOfArray);
$git = $_POST['git'];
$live = 0;
$target_file = $target_dir . $id;
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}else {
$query = $connection->prepare("
INSERT INTO versions(id,version,time,testing,live,updaterVersion,git)
VALUES (:id, :version, :time, :testing, :live, :updaterVersion, :git)
");
try {
$query->execute([
'id'=>$id,
'version'=>$buildVer,
'time'=>$buildTime64,
'testing'=>$testM,
'live'=>$live,
'updaterVersion'=>$upVer,
'git'=>$git
]);
//header("Location: ../version.php");
echo "Query Executed";
} catch (PDOException $e) {
die("Query failed");
}
}
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
header('Location: ../version.php');
} else {
echo "Sorry, there was an error uploading your file.";
}
}
} else{
echo "no post data";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment