-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.php
289 lines (275 loc) · 10.4 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
278
279
280
281
282
283
284
285
286
287
288
289
<?php
session_start();
?><!DOCTYPE html>
<html lang=en>
<head>
<meta charset=utf-8>
<link rel="stylesheet" type="text/css" href="sketchy.css">
<title>Sketchy | Upload</title>
</head>
<body>
<?php
if(!isset($_SESSION['sketcherid']))
{
die("<h3>Sorry, but you are not logged in. Try <a href='index.php'>logging
in</a> again.</h3>");
}
else
{
include("inc_sketchy_connect.php");
if ($DBConnect === FALSE)
{
die("<p>Connection error: " . mysqli_error() . "</p>\n");
}
else
{
if ($DBSelect === FALSE)
{
die("<p>Could not select the \"$DBName\" " . "database: " .
mysqli_error($DBConnect) . "</p>\n");
}
if(isset($_POST['submit']) && $_POST['submit'] == "upload graphic")
{
$title = mysqli_real_escape_string($DBConnect, $_POST['title']);
$tag1 = mysqli_real_escape_string($DBConnect, $_POST['tag1']);
$tag2 = mysqli_real_escape_string($DBConnect, $_POST['tag2']);
$tag3 = mysqli_real_escape_string($DBConnect, $_POST['tag3']);
$filetype = ($_FILES["uploadedfile"]["type"]);
if (!($filetype == "image/svg+xml" || $filetype == "text/xml"))
{
die ("Only XML or SVG files may be uploaded." .
htmlentities($_FILES['uploadedfile']['name']) . " is not a valid file"
. "<br />\n");
}
else if (empty($title))
{
echo "<p class='error'>Please try again to include a title. This
field is required.</p>";
}
else
{
//renaming of file
$orgname = basename ($_FILES['uploadedfile']['name']);
$query1 = "SELECT MAX(sketchid) AS sid FROM sketch";
$result1 = mysqli_query($DBConnect, $query1);
while ($row = mysqli_fetch_array($result1))
{
$id = $row['sid'];
}
$sketchid = ++$id;
$_SESSION['sketchid'] = $sketchid;
$newname = str_replace($orgname,$sketchid . ".xml",$orgname);
/*create a file folder for the sketcher using sketcherid to store his
graphics unless a folder already exists*/
$sketcherid = $_SESSION['sketcherid'];
if (file_exists($sketcherid)) //{$_SESSION['sketcherid']}
{
//Folder where the file is going to be placed after temp location
$target_path = $sketcherid . "/"; //$_SESSION['sketcherid']
/*Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename ($newname);
//move_uploaded_file() function moves the uploaded file from its temp
//to a perm destination. result is either true(successful) or false.
$result = (move_uploaded_file($_FILES['uploadedfile']['tmp_name'],
$target_path));
}
else
{
//
mkdir($sketcherid,0755);
//Folder where the file is going to be placed after temp location
$target_path = $sketcherid . "/"; //$_SESSION['sketcherid']
/*Add the original filename to our target path.
Result is "uploads/filename.extension" */
$target_path = $target_path . basename ($newname);
//move_uploaded_file() function moves the uploaded file from its temp
//to a perm destination. result is either true(successful) or false.
$result = (move_uploaded_file($_FILES['uploadedfile']['tmp_name'],
$target_path));
}
if ($result == TRUE)
{
$query2 =
"INSERT INTO sketch (title, path, sketcherid) VALUES ('$title', '$target_path', $sketcherid)";
$result2 = mysqli_query($DBConnect, $query2);
//todo: make next three queries into a function
if (!empty($tag1))
{
//check to see if tag1 already exists
$query_tag1Exists =
"SELECT tagid FROM tags WHERE tag='$tag1'";
$result_tag1Exists = mysqli_query($DBConnect, $query_tag1Exists);
if (!mysqli_num_rows($result_tag1Exists))
{
$query_tag1 =
"INSERT INTO tags (tag) VALUES ('$tag1')";
$result_tag1 = mysqli_query($DBConnect, $query_tag1);
$tagid1 = mysqli_insert_id($DBConnect);
$query_desc1 =
"INSERT INTO describes (sketchid, tagid) VALUES ($sketchid, $tagid1)";
$result_desc1 = mysqli_query($DBConnect, $query_desc1);
}
else
{
while ($row = mysqli_fetch_assoc($result_tag1Exists))
{
$tagid1 = $row['tagid'];
}
$query_desc1 =
"INSERT INTO describes (sketchid, tagid) VALUES ($sketchid, $tagid1)";
$result_desc1 = mysqli_query($DBConnect, $query_desc1);
}
}
if (!empty($tag2))
{
//check to see if tag1 already exists
$query_tag2Exists =
"SELECT tagid FROM tags WHERE tag='$tag2'";
$result_tag2Exists = mysqli_query($DBConnect, $query_tag2Exists);
if (!mysqli_num_rows($result_tag2Exists))
{
$query_tag2 =
"INSERT INTO tags (tag) VALUES ('$tag2')";
$result_tag2 = mysqli_query($DBConnect, $query_tag2);
$tagid2 = mysqli_insert_id($DBConnect);
$query_desc2 =
"INSERT INTO describes (sketchid, tagid) VALUES ($sketchid, $tagid2)";
$result_desc2 = mysqli_query($DBConnect, $query_desc2);
}
else
{
while ($row = mysqli_fetch_assoc($result_tag2Exists))
{
$tagid2 = $row['tagid'];
}
$query_desc2 =
"INSERT INTO describes (sketchid, tagid) VALUES ($sketchid, $tagid2)";
$result_desc2 = mysqli_query($DBConnect, $query_desc2);
}
}
if (!empty($tag3))
{
//check to see if tag1 already exists
$query_tag3Exists =
"SELECT tagid FROM tags WHERE tag='$tag3'";
$result_tag3Exists = mysqli_query($DBConnect, $query_tag3Exists);
if (!mysqli_num_rows($result_tag3Exists))
{
$query_tag3 =
"INSERT INTO tags (tag) VALUES ('$tag3')";
$result_tag3 = mysqli_query($DBConnect, $query_tag3);
$tagid3 = mysqli_insert_id($DBConnect);
$query_desc3 =
"INSERT INTO describes (sketchid, tagid) VALUES ($sketchid, $tagid3)";
$result_desc3 = mysqli_query($DBConnect, $query_desc3);
}
else
{
while ($row = mysqli_fetch_assoc($result_tag3Exists))
{
$tagid3 = $row['tagid'];
}
$query_desc3 =
"INSERT INTO describes (sketchid, tagid) VALUES ($sketchid, $tagid3)";
$result_desc3 = mysqli_query($DBConnect, $query_desc3);
}
}
/*code below from: http://us1.php.net/manual/en/domelement.setattribute.php
uses DOM manipulation to change the width and height attributes
of the svg element on upload because when these attributes are
included the file cannot be appropriately scaled to a new page.
width and height in an svg element creat a viewport which
determines the size of the drawing seen in proportion to the
entire drawing. By deleting width and height we make sure the
viewport is the size of the viewBox, meaning you can see
the entire graphic uploaded, not just a portion. Also needs to
made into a function.
*/
$doc = new DOMDocument();
$doc->load($target_path);
foreach($doc->getElementsByTagName('svg') as $svg)
{
if($svg->hasAttribute('viewBox')===false)
{
$svg->setAttribute('viewBox','0 0 800 960');
}
foreach(array('width', 'height', 'preserveAspectRatio') as $attribute_to_remove)
{
if($svg->hasAttribute($attribute_to_remove))
{
$svg->removeAttribute($attribute_to_remove);
}
/*this is good code if needed in the future to change the
attribute as well
if($attribute_to_remove=='height')
{
if(!$svg->hasAttribute($attribute_to_remove))
{
$svg->setAttribute($attribute_to_remove,'25%');
}
}
if($attribute_to_remove=='width')
{
if(!$svg->hasAttribute($attribute_to_remove))
{
$svg->setAttribute($attribute_to_remove,'25%');
}
} */
}
$svg->setAttribute('preserveAspectRatio','xMinYMin meet');
}
$doc->save($target_path);
header('Location: preview.php');
}
else
{
echo "Could not upload file.<br>\n";
}
}//ends else of filetype
}//ends if upload graphic submitted
mysqli_close($DBConnect);
}//ends successful DBConnect else statement
} //ends php code on submission button
?>
<div id="page-wrap">
<div class="row">
<header>
<div class="col-1">
<div id="headleft">
<h1><a id="head" href='home.php'>Sketchy</a></h1>
</div>
<span id="sub">SVG for You and Me</span>
</div>
</header>
</div>
<div class="row">
<nav>
<div class="col-1-3">
<a href='#'>about</a>
</div>
<div class="col-1-3">
<a href='#'>resources</a>
</div>
<div class="col-1-3">
<a href='#'>terms and conditions</a>
</div>
</nav>
</div>
<section>
<h2>Add a Sketch</h2>
<form class="mid" enctype="multipart/form-data" action="upload.php" method="POST">
<div class="center">
<input class="upload" type="hidden" name="MAX_FILE_SIZE" value="250000" />
<input class="upload" type="file" name="uploadedfile" accept="image/svg+xml"/><br>
<input class="upload" type="text" name="title" placeholder="title" /><br>
<input class="upload" type="text" name="tag1" placeholder="tag 1 (optional)" /><br>
<input class="upload" type="text" name="tag2" placeholder="tag 2 (optional)" /><br>
<input class="upload" type="text" name="tag3" placeholder="tag 3 (optional)" /><br>
<input class="button" type="submit" name="submit" value="upload graphic" /><br>
</div>
</form>
</section>
</div><!--end page-wrap-->
</body>
</html>