-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.php
277 lines (240 loc) · 8.29 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload</title>
<link href="css/style.css" rel="stylesheet">
</head>
<?php
session_start();
include_once 'include/config.php';
include_once 'include/water_marker.php';
include_once 'include/getuser.php';
function uploadEdit($url)
{
$fileNameNew = uniqid('', true) . ".png";
//create path to folder
$fileDestination = 'images/uploads/' . $fileNameNew;
file_put_contents($fileDestination, file_get_contents($url));
return $fileDestination;
}
function uploadFile($file)
{
//upload image to upload folder and return new file path
$MAX_FILE_SIZE = 10000000;/*b*/
//print_r($file);
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];
//get extention of file ie jpg
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));
//check thst uploaded file type is allowed
$allowed = array('jpg', 'jpeg', 'png');
if (in_array($fileActualExt, $allowed)) {
//check for upload error
if ($fileError === 0) {
//check file size
//echo $MAX_FILE_SIZE . ">" . $fileSize;
if ($fileSize < $MAX_FILE_SIZE) {
//create uniqe file name using time stamp
$fileNameNew = uniqid('', true) . "." . $fileActualExt;
//create path to folder
$fileDestination = 'images/uploads/' . $fileNameNew;
//upload file
move_uploaded_file($fileTmpName, $fileDestination);
return $fileDestination;
} else {
echo 'Your file is too big';
return 'error';
}
} else {
echo "There was an error uploading your file";
return 'error';
}
} else {
echo "You cannot upload file of this type";
return 'error';
}
}
function getIdByPath($db, $path)
{
//get image id using path name
$query = "SELECT * FROM `image` WHERE Img_file_name = '$path'";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
$row = mysqli_fetch_assoc($result);
//echo "id: " . $row["Img_id"] . " - path Name: " . $row["Img_file_name"] . "<br>";
return $row["Img_id"];
}
}
function getUserName($db, $id)
{
//get user name
$query = "SELECT * FROM `user` WHERE user_id = '$id'";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
$row = mysqli_fetch_assoc($result);
//echo "id: " . $row["tag_id"] . " - Name: " . $row["tag_name"] . "<br>";
return $row["name"];
}
return false;
}
function getTagIdByname($db, $name)
{
//get tag id if the tag exits
$query = "SELECT * FROM `tag` WHERE tag_name = '$name'";
$result = mysqli_query($db, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
$row = mysqli_fetch_assoc($result);
//echo "id: " . $row["tag_id"] . " - Name: " . $row["tag_name"] . "<br>";
return $row["tag_id"];
}
return false;
}
function insertTag($db, $tag)
{
//inserts new tag and returns id
//use sanitation
$query = "INSERT INTO `tag` (`tag_id`, `tag_name`) VALUES (NULL, ?);";
if ($statement = mysqli_prepare($db, $query)) {
// bind parameters s - string,
$result = mysqli_stmt_bind_param($statement, 's', $tag);
if (!$result) {
print "<p>bounding error</p>";
}
// execute query
$result = mysqli_stmt_execute($statement);
if ($result) {
return getTagIdByname($db, $tag);
} else {
print "Mysql insert tag Error" . mysqli_stmt_error($statement);
}
}
}
function insertImgTag($db, $imgId, $tagId)
{
//inserts img tag relation in img_tag table
//use sanitation
$query = "INSERT INTO `img_tag` (`img_id`, `tag_id`) VALUES (?, ?);";
if ($statement = mysqli_prepare($db, $query)) {
// bind parameters s - string,
$result = mysqli_stmt_bind_param($statement, 'ss', $imgId, $tagId);
if (!$result) {
print "<p>bounding error</p>";
}
// execute query
$result = mysqli_stmt_execute($statement);
if (!$result) {
print "Mysql insert img-tag Error" . mysqli_stmt_error($statement);
}
}
}
function insertImgedit($db, $imgId, $orgId)
{
//inserts img edit relation in img_edit table
//use sanitation
$query = "INSERT INTO `img_edit` (`img_id`, `edit_id`) VALUES (?, ?);";
if ($statement = mysqli_prepare($db, $query)) {
// bind parameters s - string,
$result = mysqli_stmt_bind_param($statement, 'ss', $imgId, $orgId);
if (!$result) {
print "<p>bounding error</p>";
}
// execute query
$result = mysqli_stmt_execute($statement);
if (!$result) {
print "Mysql insert edit Error" . mysqli_stmt_error($statement);
}
}
}
?>
<div class="form-box">
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['img'])) {
//upload img from editor canvas
$path = uploadEdit($_POST['img']);
} else {
$file = $_FILES['file'];
$path = uploadFile($file);
}
$title = $_POST['title'];
$desc = $_POST['description'];
$tags = $_POST['tags'];
//seperate tags
$tagList = explode(' ', $tags);
// Connect to MySQL
$db = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
// Error checking
if (!$db) {
print "<p>Error - Could not connect to MySQL</p>";
exit;
}
$error = mysqli_connect_error();
if ($error != null) {
$output = "<p>Unable to connet to database</p>" . $error;
exit($output);
}
//insert imag
$user = getSessionUser();
$edit_of = -1;
$is_original = 1;
$upload_user = $user;
$edit_user = "NULL";
if (isset($_POST['edit_of'])) {
$edit_of = $_POST['edit_of'];
if (isset($_POST['upload_by'])) {
$upload_user = $_POST['upload_by'];
$edit_user = $user;
}
$is_original = 0;
}
//use sanitation
$query = "INSERT INTO `image` (`Img_id`, `title`, `Img_file_name`, `edited_by`, `uploaded_by`, `is_original`, `uploaded_on`, `description`) VALUES (NULL, ?, ?, ?, ?, ?, current_timestamp(), ?)";
if ($statement = mysqli_prepare($db, $query)) {
// bind parameters s - string,
$result = mysqli_stmt_bind_param($statement, 'ssssss', $title, $path, $edit_user, $upload_user, $is_original, $desc);
if (!$result) {
print "<p>bounding error</p>";
}
// execute query
$result = mysqli_stmt_execute($statement);
if ($result) {
print "<p>Your image post was successful</p>";
} else {
print "Mysql insert image Error" . mysqli_stmt_error($statement);
}
}
$imgId = getIdByPath($db, $path);
//echo $imgId;
//for each tag see if it exits in tag table and the link it with img
foreach ($tagList as $tag) {
$tag = strtolower($tag);
$id = getTagIdByname($db, $tag);
if ($id == false) {
//no such tag so insert it
$id = insertTag($db, $tag);
// echo $id ."(inserted) ".$tag."<br>";
}/*else{
echo $id ." ".$tag."<br>";
}*/
//insert relation
insertImgTag($db, $imgId, $id);
}
if (isset($_POST['edit_of'])) {
//set new image to be an edit of old image
insertImgedit($db, $imgId, $edit_of);
} else {
//add watermark to orginal image
watermark($path, getUserName($db, $upload_user));
}
mysqli_close($db);
}
?>
<button id="upload-con" onclick="window.location.href='gallery.php';">Continue</button>
</div>