-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathschematicSaveLocally.php
119 lines (95 loc) · 3.96 KB
/
schematicSaveLocally.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/*
* JavaScript Redstone Simulator
* Copyright (C) 2012 Jonathan Lydall (Email: [email protected])
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
//include_once 'includes/error_handling.php';
include_once 'includes/utf8_handling.php';
include_once 'includes/config_mysqlConnection.php';
function exitWithError($message) {
echo json_encode(array("error" => true, "errorDescription" => $message));
exit;
}
$mysqli = new mysqli($mysqlConfig['host'], $mysqlConfig['username'], $mysqlConfig['password'], $mysqlConfig['schema']);
$mysqli->set_charset('utf8');
date_default_timezone_set("utc");
$timeNow = date("Y-m-d H:i:s");
//Always delete any records which were uploaded more than an hour ago:
$deleteOlderThan = new DateTime();
$deleteOlderThan->sub(new DateInterval('PT1H')); //delete any records older than an hour
$formattedDate = $deleteOlderThan->format("Y-m-d H:i:s");
$stmt = $mysqli->prepare("DELETE FROM `schematics_tempholder` WHERE timestamp<?");
$stmt->bind_param('s', $formattedDate);
$stmt->execute();
$stmt->close();
if (!isset($_GET["task"])) exitWithError("Task not set.");
switch ($_GET["task"]) {
case "put":
if (!isset($_POST['schematicData'])) exitWithError("No data.");
if (!isset($_POST['fileName'])) exitWithError("No file name.");
$fileData = base64_decode($_POST['schematicData']);
$fileName = utf8_decode($_POST['fileName']);
$dataBlob_split = str_split($fileData, $mysqlConfig['max_allowed_packet']);
$dataBlob = NULL;
$stmt = $mysqli->prepare("INSERT INTO `schematics_tempholder` SET `data`=?, `fileName`=?, `timestamp`=?");
$stmt->bind_param("bss", $dataBlob, $fileName, $timeNow);
if ( false===$stmt ) exitWithError('bind_param() failed: ' . htmlspecialchars($mysqli->error));
foreach($dataBlob_split as $packet) {
$stmt->send_long_data(0, $packet);
}
$stmt->execute();
$insert_id = $stmt->insert_id;
$stmt->close();
$returnArray = array("id" => $insert_id, "fileSize" => strlen($fileData));
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
echo json_encode($returnArray);
}
else {
echo '<textarea>' . json_encode($returnArray) . '</textarea>';
}
break;
case "get":
if (!isset($_POST["id"])) exitWithError("ID not set.");
if (!is_numeric($_POST["id"])) exitWithError("ID not an integer.");
$recordId = $_POST["id"];
$stmt = $mysqli->prepare("SELECT `data`, `fileName` FROM `schematics_tempholder` WHERE id=?");
$stmt->bind_param('i', $recordId);
$stmt->execute();
$stmt->bind_result($fileContents, $fileName);
if (!$stmt->fetch()) {
exitWithError("No record found with ID $recordId.");
}
$stmt->close();
$fileSize = strlen($fileContents);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $fileName . '.schematic');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $fileSize);
echo $fileContents;
//Delete the record:
$stmt = $mysqli->prepare("DELETE FROM `schematics_tempholder` WHERE id=?");
$stmt->bind_param('i', $recordId);
$stmt->execute();
$stmt->close();
break;
}
?>