-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
399 lines (292 loc) · 10.6 KB
/
index.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
session_start();
$count = 0;
if(empty($_SESSION ["isEditor"])){
$_SESSION ["isEditor"] = false;
}
//checks if the user logs in or not
try{
if(isset($_POST["LOGIN"])){
if($_POST['LOGIN'] == "LOG IN"){
$_SESSION ["isEditor"] = true;
unset($_POST['LOGIN']);
$loggedIn = true;
}elseif($_POST['LOGIN'] == "LOG OUT"){
$_SESSION ["isEditor"] = false;
unset($_POST['LOGIN']);
$loggedIn = true;
}else{
$loggedIn = false;
}
}else{
$loggedIn = false;
}
}catch(Exception $e){
}
$file = "galleryinfo.json";
if (!file_exists($file)) {
touch($file);
}
// read json file into array of strings
$jsonstring = file_get_contents($file);
//decode the string from json to PHP array
$phparray = json_decode($jsonstring, true);
$isEditor = $_SESSION ["isEditor"];
include "header.inc";
// Link image type to correct image loader and saver
// - makes it easier to add additional types later on
// - makes the function easier to read
const IMAGE_HANDLERS = [
IMAGETYPE_JPEG => [
'load' => 'imagecreatefromjpeg',
'save' => 'imagejpeg',
'quality' => 100
],
IMAGETYPE_PNG => [
'load' => 'imagecreatefrompng',
'save' => 'imagepng',
'quality' => 0
],
IMAGETYPE_GIF => [
'load' => 'imagecreatefromgif',
'save' => 'imagegif'
]
];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// set this to false first -- when the form is all correct, it will be set to true again
$GLOBALS['showGallery'] = false;
if(array_key_exists('resetJSON', $_POST)) {
deleteJSON();
} else if (array_key_exists('deleteUID', $_POST)) {
deleteUID();
} else if (array_key_exists('deleteAllEntries', $_POST)) {
deleteJSONandImages();
}
} // if SERVER == POST
// BUTTONS --------------------------------
function deleteJSON() {
// echo "FROM DELETE BUTTON";
$file = "galleryinfo.json";
if (file_exists($file)) {
unlink(realpath($file));
}
$file2 = "allinfo.json";
if (file_exists($file2)) {
unlink(realpath($file2));
}
} // deleteJSON
function deleteJSONandImages() {
deleteJSON();
$files = glob('uploadimages/*');
foreach($files as $file){ // iterate files
if(is_file($file)) {
unlink($file); // delete file
}
}
$tbfiles = glob('thumbnails/*');
foreach($tbfiles as $tbfile){ // iterate files
if(is_file($tbfile)) {
unlink($tbfile); // delete file
}
}
} // deleteJSONandImages
function deleteUID() {
$file = "identifier.txt";
if (file_exists($file)) {
unlink(realpath($file));
}
} // deleteFolder
function searchForId($id, $array) {
foreach ($array as $key => $val) {
if ($val['UID'] == $id) {
return $key;
}
}
} // searchForID
// ------------------------
// PHP is responsible for uploading new images and create thumbnails
// PHP prints the first appearance of the thumbs
// When users hit the Filter or Sorting buttons --> JS takes over and handles all images
// ------------------------
function viewGallery() {
global $isEditor;
global $phparray;
$file = "galleryinfo.json";
if (file_exists($file)) {
echo '<div class="grid-container">';
// ------ HEADING ---------
echo "<div class='header title'>";
echo '<div id="school">';
echo "<h1>YEARBOOK GALLERY 2021</h1>";
echo "</div>";
if ($isEditor == true) {
echo '<div>';
echo "<h2>MODERATOR GALLERY</h2>";
echo "</div>";
$sch = "search('" . 'all' . "')";
echo '<div id=searchDiv>';
echo '<input id="search" type="text" placeholder="Quick Look Up" name="search">';
echo '<span id="searchButton" onclick="' . $sch . '">Search</span>'; // search all
echo "</div>";
echo '<form action="index.php" method="POST">';
echo '<input type="submit" name="LOGIN" id="loginout" class="masterButton" value="LOG OUT">';
echo '</form>';
echo '<div id="approval" class="masterButton">';
echo '<a id="yellowLink" href="approval.php"> <i class="gg-eye-alt"></i> <em>Awaiting Approval</em></a>';
echo '</div>';
} else {
echo '<div>';
echo "<h2>PUBLIC GALLERY</h2>";
echo "</div>";
$sch = "search('" . 'public' . "')";
echo '<div id=searchDiv>';
echo '<input id="search" type="text" placeholder="Quick Look Up" name="search">';
echo '<span id="searchButton" onclick="' . $sch . '">Search</span>'; // search all
echo "</div>";
echo '<form action="index.php" method="POST">';
echo '<input type="submit" name="LOGIN" id="loginout" class="masterButton" value="LOG IN">';
echo '</form>';
echo '<div id="belowSchool" class="masterButton">';
echo '<a id="yellowLink" href="approval.php?form=true">Upload Image</a>';
echo '</div>';
}
echo "</div>"; // close header
// LEFT -------
echo "<div class='left'>";
echo "</div>";
// ------------
// ---------- BODY ----------
echo "<div class='middle'>";
echo "<div id='mainBody'>";
if ($phparray == NULL) {
echo "Public Gallery Is So Empty. Upload an image!" . "<br>";
echo '<div class="masterButton">';
echo '<a href="approval.php?form=true">Upload Image</a>';
echo '</div>';
if ($isEditor == true) {
include "footer.inc";
}
return null;
}
if (isset($_GET['success'])) {
echo "Your Form Has Been Submitted Successfully." . "<br>";
echo "It Will Appear Here Once Reviewed." . "<br>";
}
echo '<div class="main middle" id="upperPanel">';
// MODERATOR Heading
if ($isEditor == true) {
echo "Filter by: ";
echo '<span class="filter" id="all" onclick="updateVar(\'none\', \'all\')">All Images</span>';
echo '<span class="filter" id="private" onclick="updateVar(\'none\', \'private\')">Private Images Only</span>';
echo '<span class="filter" id="public" onclick="updateVar(\'none\', \'public\')">Public Images Only</span>';
echo "<br><br><br>";
echo '<div id="sort">';
echo "Sort by: ";
echo '<span class="filter" id="date" onclick="updateVar(\'date\', \'none\')">Earliest Submission</span>';
echo '<span class="filter" id="fname" onclick="updateVar(\'fname\', \'none\')">First Name</span>';
echo '<span class="filter" id="lname" onclick="updateVar(\'lname\', \'none\')">Last Name</span>';
echo "</div>"; // close div sort
// PUBLIC Heading
} else {
echo '<div id="sort">';
echo "Sort by: ";
echo '<span class="filter" id="date" onclick="updateVar(\'date\', \'public\')">Earliest Submission</span>';
echo '<span class="filter" id="fname" onclick="updateVar(\'fname\', \'public\')">First Name</span>';
echo '<span class="filter" id="lname" onclick="updateVar(\'lname\', \'public\')">Last Name</span>';
echo "</div>"; // close div sort
} // else
echo "</div>"; // close div upperPanel
echo "<br><br>";
// for MODERATOR --> show "Download All Button"
if ($isEditor == true) {
echo '<div class="masterButton">';
echo '<a href="downloadall.php" download>Download All Images</a>';
echo '</div>';
}
echo "<div class='picFrame' id='picFrame'>";
// LOADING IMAGES
// isEditor --> show all images
// if not --> show public only
if ($isEditor == true) {
foreach ($phparray as $element) {
$source = "thumbnails/" . $element['UID'] . "." . $element['imagetype'];
$displayPrompt = "displayLightBox('" . $element['UID'] . "', 'none', 'all', 'true')";
echo "<div class='imgBox'>";
echo '<img class="thumb" id="' . ($element['UID']) . '" src="'.$source.'"
alt="'.$element['UID'].'"
onclick=" '.$displayPrompt.' ">';
echo "<p>" . $element['fname'] . " " . $element['lname'] . "</p>";
echo "</div>";
} // foreach
// PUBLIC GALLERY
} else {
foreach ($phparray as $element) {
if ($element['privacy'] == "public") {
$source = "thumbnails/" . $element['UID'] . "." . $element['imagetype'];
$displayPrompt = "displayLightBox('" . $element['UID'] . "', 'none', 'public', 'true')";
echo "<div class='imgBox'>";
echo '<img class="thumb" id="' . ($element['UID']) . '" src="'.$source.'"
alt="'.$element['UID'].'"
onclick=" '.$displayPrompt.' ">';
echo "<p>" . $element['fname'] . " " . $element['lname'] . "</p>";
echo "</div>";
}
} // foreach
} // else (public gallery)
echo "</div>";
// HTML CODE FOR LIGHTBOX - copy from CS11
echo '<div id="lightbox" class="hidden"></div>';
echo '<div id="positionBigImage">';
echo '<div id="boundaryBigImage" class="hidden">';
echo '<span id="x" onclick=displayLightBox()><i class="gg-close"></i></span>';
echo '<img id="bigImage" src="placeholder.png" alt="Test">';
// HTML CODE FOR BUTTONS - only show with lightbox image
echo '<div class="masterButton">';
echo "<span id='prev'><i class='gg-chevron-left-o'></i></span>";
echo '<a id="link" href="" download> DOWNLOAD </a>';
echo "<span id='next'><i class='gg-chevron-right-o'></i></span>";
echo '</div>';
//populate the lightbox with the structure of the JSON data
echo "<div id='phpInfo' class='unhidden'>";
echo '<span id="imgID" class="unhidden">ID </span> ' . '<br>';
echo '<span id="imgName" class="unhidden">Image Owner</span> ' . '<br>';
echo '<span id="imgDesc" class="unhidden">Image Description</span> ' . '<br>';
echo '<span id="imgTag" class="unhidden">Image Tag</span> ' . '<br>';
//if user is the editor let them access the edit view
if($isEditor){
echo '<button id="editJSONData" class="masterButton" onclick="displayEdit()">';
echo 'Edit';
echo '</button>';
}
echo "</div>"; // phpInfo
echo '</div>'; // boundaryBigImage
echo '</div>'; // positionBigImage
echo "<div></div>";
echo "</div>"; // mainBody close div
echo "</div>"; // close div middle
// ----- End MIDDLE --------
// RIGHT ---------
echo "<div class='right'>";
echo "</div>";
// ---------------
// RIGHT ---------
echo "<div class='footer'>";
if ($isEditor == true) {
include "footer.inc";
}
echo "</div>";
// ---------------
echo "</div>"; // close grid container
// if galleryinfo.json doesn't exist
// then tell user to upload something
} else {
echo "Gallery Now Empty. Upload an Image";
echo '<div class="masterButton">';
echo '<a href="approval.php?form=true">Upload Image</a>';
echo '</div>';
}
} // viewGallery
viewGallery();
echo "<script src='myscript.js'></script>";
echo "</html>";
?>