-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
226 lines (185 loc) · 6.54 KB
/
upload.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
if(count($_FILES) > 0 || count($_POST) > 0) {
include_once('header.inc.php');
if(isset($_POST['pwd'])) {
$res['status'] = "err";
$json_filename = $_SESSION['file'];
$json = file_get_contents($json_filename);
$petri = json_decode($json, true);
if (hashPwd($_POST['pwd']) == getUserPwdHash()) {
$res['status'] = "ok";
$_SESSION['pwd'] = hashPwd($_POST['pwd']);
}
echo json_encode($res);
} else {
//generate unique file name
$fileName = time().'_'.basename($_FILES["file"]["name"]);
//file upload path
$targetDir = __DIR__ . "/uploads/";
$targetFilePath = $targetDir . $fileName;
//allow certain file formats
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
$allowTypes = array('json');
if (in_array($fileType, $allowTypes)){
//upload file to server
if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)) {
$response['status'] = 'ok';
$_SESSION['file'] = $targetFilePath;
$response['file'] = $_SESSION['file'];
} else {
$response['status'] = 'err';
}
} else{
$response['status'] = 'type_err';
}
echo json_encode($response);
}
exit();
}
?>
<div class="form-wrapper">
<div class="form-div">
<form method="POST" enctype="multipart/form-data" id="form1">
<div class="info">Let's import your mighty Petri net and watch the magic unfold!</div>
<div class="button">Select the JSON file</div>
<input type="file" style="width:0px; opacity:0" />
</form>
<form method="post" id="form2" style="display:none">
<div class="success">Great! Your Petri net was successfully imported!</div>
<div class="info">Your digital fingerprint is the key to unlocking endless possibilities.<br>Show us your STAMP and take control!</div>
<div class="button">Select the STAMP file</div>
<input type="file" style="width:0px; opacity:0" />
</form>
</div>
<div class="modal">
<img src="img/loader.svg" width="80px" height="80px" alt="Loading" />
</div>
</div>
<div class="message" style="display:none; position:fixed; bottom:0; left:0; width:100%; padding: 10px; text-align:center; background:rgba(0,0,0,0.8); color:white; font-size:0.8em"></div>
<script>
$(document).ready(function () {
$("form > .info").click(function(){
$(this).parent('form').find('input[type="file"]').click();
});
$(document).on('drop dragover', function(e) {
showMessage("Please select your files by clicking on the button above.");
e.preventDefault();
});
$('.button').click(function(){
$(this).parent('form').find('input[type=file]').click();
});
$('#form1 input[type="file"]').change(function(event){
event.preventDefault();
file = event.target.files[0];
const data = new FormData();
if (!file.name.match('json')) {
showMessage("Please select a JSON file.");
} else if(file.size > 1.5e7) {
showMessage("Maximum file size: 10 MB.");
} else{
$('.modal').css({ "opacity": 1, zIndex: 0 })
//append the uploadable file to FormData object
data.append('file', file, file.name);
const xhr = new XMLHttpRequest();
xhr.open('POST', 'upload.php', true);
xhr.send(data);
xhr.onload = function () {
//get response and show the uploading status
const response = JSON.parse(xhr.responseText);
if (xhr.status === 200 && response.status == 'ok') {
$('#form1').fadeOut().promise().done(function() {
$('#form2').fadeIn();
});
} else if (response.status == 'type_err') {
showMessage("Please select a JSON file.");
} else {
showMessage("An error occurred. Please try again.");
}
$('.modal').css({ "opacity": 0, zIndex: -1 });
};
}
});
$('#form2 input[type="file"]').change(function(event){
event.preventDefault();
file = event.target.files[0];
var data = new FormData();
if (file.size > 400){
showMessage("Maximum file size: 400 bytes.");
} else{
$('.modal').fadeIn();
data.append('file', file, file.name);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'uploadStamp.php', true);
xhr.send(data);
xhr.onload = function () {
var response = JSON.parse(xhr.responseText);
if (xhr.status === 200 && response.status == 'ok'){
<?php if(!$mdpRequired) {?>
loadProcessing();
<?php } else { ?>
pwdProcess();
<?php } ?>
} else{
showMessage("An error occurred. Please try again.");
}
$('.modal').fadeOut();
};
}
});
});
function pwdProcess() {
$('#form1, #form2').remove();
$('.form-div').html('<div style="text-align:center; padding:15px">Awaiting password</div>');
$('.modal-layer').fadeIn(500);
$('.modal-win').toggle('clip');
$('.bt-ok').bind('click', function(){
var val = $('input#pwd').val();
if(val.length > 0) {
$.post('upload.php', {
pwd: val
}, function(response){
var res = JSON.parse(response);
$('input#pwd').val("");
if (res.status == "ok") {
$('.modal-layer').fadeOut(500);
$('.modal-win').toggle('clip');
$('.bt-ok').unbind("click");
$('.modal-win input[type=password]').val("").unbind('keyup');
loadProcessing();
} else {
$('.result').html("Incorrect password!");
}
});
}
});
$('.modal-win input[type=password]').focus().bind('keyup', function(key){
if (key.which === 13)
$('.bt-ok').trigger("click");
});
}
</script>
<style>
.form-wrapper {
position: relative;
padding: 12px;
padding-bottom: 40px;
}
form {
text-align:center;
}
.modal {
display: flex;
background: rgba(255,255,255,0.95);
position: absolute;
left:0;
top:0;
width: 100%;
height: 100%;
text-align:center;
align-items: center;
justify-content: center;
z-index: -1;
opacity: 0;
transition: all .6s linear;
}
</style>