/
upload.php
190 lines (163 loc) · 5.53 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
<?php
/*
* CloudLevels, an easy way to share user created level files for video games.
* Copyright (C) 2016 Alexander Aquino
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
//CloudLevels Upload File Page
//Header + Vars:
$page_title='Upload';
include 'header.php';
//Members only!
if($user_type==-1||$user_type==1){
errorbox('You do not have permission to view this page.');
include 'footer.php';
exit(0);
}
//When there is input data
if(!empty($_POST["title"])){
//Upload file
//Check for errors
if($_FILES['file']['error']!=UPLOAD_ERR_OK||$_FILES['screenshot']['error']!=UPLOAD_ERR_OK){
errorbox('Upload failed. Please try again later.');
include 'footer.php';
exit(0);
}
//Verify file size
if($_FILES['file']['size']>$file_size_limit){
errorbox('Your file has exceeded the maximum file size of ' . $file_size_limit/1000000 . 'MB.');
include 'footer.php';
exit(0);
}
//Verify image size
if($_FILES['screenshot']['size']>$file_size_limit){
errorbox('Your screenshot has exceeded the maximum file size of ' . $file_size_limit/1000000 . 'MB.');
include 'footer.php';
exit(0);
}
//File must be a ZIP
if(strtolower(substr($_FILES['file']['name'], -4))!='.zip'){
errorbox('You must upload a file of type ZIP.');
include 'footer.php';
exit(0);
}
//Image must be a PNG
if(strtolower(substr($_FILES['screenshot']['name'], -4))!='.png'){
errorbox('You must upload a screenshot of type PNG.');
include 'footer.php';
exit(0);
}
$last_id=0;
try{
//Create database entries
//Begin
$db->beginTransaction();
//File table entry
date_default_timezone_set('America/New_York');
$stmt = $db->prepare("
INSERT INTO cl_file(name, author, date, ip, description)
VALUES(?,?,?,?,?)");
$stmt->execute(array(htmlspecialchars($_POST["title"]), $_SESSION['uid'], date("F j, Y"), $_SERVER['REMOTE_ADDR'], nl2br(htmlspecialchars($_POST["description"]))));
//Get file id
$last_id = $db->lastInsertId();
//Tag table entry
if(!empty($_POST["tags"])){
foreach($_POST["tags"] as $tag){
$stmt = $db->prepare("
INSERT INTO cl_tag(file, tag)
VALUES(?,?)");
$stmt->execute(array($last_id, htmlspecialchars($tag)));
}
}
//Increment upload count
$stmt = $db->prepare("
UPDATE cl_user
SET uploads = uploads+1
WHERE id = ?");
$stmt->execute(array($_SESSION['uid']));
//End
$db->commit();
}
//Handle errors
catch(PDOException $ex){
$db->rollBack();
errorbox('Upload failed. Please try again later.');
include 'footer.php';
exit(0);
}
//Actually upload the files
move_uploaded_file($_FILES["file"]["tmp_name"], "data/" . $last_id . ".zip");
move_uploaded_file($_FILES["screenshot"]["tmp_name"], "data/" . $last_id . ".png");
//Success!
successbox('File uploaded. Please wait.');
//Refresh
header("Refresh:2;url=file.php?id=" . $last_id);
}
else{
?>
<br>
<div class="container">
<div class="row card hoverable">
<span class="col s12 card-title <?php echo $theme ?> white-text center" style="font-size: 200%;">Upload File</span>
<form action="upload.php" method="post" enctype="multipart/form-data" class="col s12 m10 l8 offset-m1 offset-l2">
<div class="file-field input-field">
<div class="btn <?php echo $theme ?>">
<span>File (<?php echo ($file_size_limit/1000000); ?>MB MAX)</span>
<input type="file" name="file" accept="application/x-zip-compressed" required>
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<div class="file-field input-field">
<div class="btn <?php echo $theme ?>">
<span>Screenshot</span>
<input type="file" name="screenshot" accept="image/png" required>
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
<div class="input-field col s12">
<i class="fa fa-commenting-o prefix" aria-hidden="true"></i>
<input id="title" name="title" type="text" class="validate" required>
<label for="title">Title</label>
</div>
<div class="input-field col s12">
<i class="fa fa-comment prefix" aria-hidden="true"></i>
<textarea id="description" name="description" class="materialize-textarea" required></textarea>
<label for="description">Description</label>
</div>
<i class="fa fa-cloud small col s1" aria-hidden="true"></i>
<div class="input-field col s11">
<select id="tags" name="tags[]" multiple>
<option value="" disabled selected>Select Tags</option>
<?php
$get_tags=explode("\n", $tags);
foreach($get_tags as $tag)
echo '<option value="' . trim($tag) . '">' . trim($tag) . '</option>';
?>
</select>
<label for="tags">Tags</label>
</div>
<button class="btn waves-effect waves-light <?php echo $theme ?> col s12" type="submit">Upload</button>
</form><div class="row"></div>
</div>
</div>
<?php
}
//Footer
include 'footer.php';
?>