-
Notifications
You must be signed in to change notification settings - Fork 0
/
editJSON.php
114 lines (95 loc) · 3.4 KB
/
editJSON.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
<?php
if(!isset($_SERVER['HTTP_REFERER'])){
// redirect them to your desired location
header('location: index.php');
exit;
}
// awaiting approval file
$file = "galleryinfo.json";
if (!file_exists($file)) {
touch($file);
}//if
$jsonstring = file_get_contents($file); // read json file into array of strings
$phparray = json_decode($jsonstring, true); //decode the string from json to PHP array
$returnData = []; // elements of the array are added to this to make a new array
$length = count($phparray); // how many elements are in $phparray
if(!empty($_POST['submit'])){// if the user made changes to the input construct a new element with the changes in it
for($i = 0; $i < $length; $i++){
if("ID: " . $phparray[$i]["UID"] == $_POST["imgUIDEdit"]){
$constructor = [];
$constructor += ["fname"=>$_POST["fname"]];
$constructor += ["lname"=>$_POST["lname"]];
$constructor += ["description"=>$_POST["description"]];
$constructor += ["tag"=>$_POST["tag"]];
$constructor += ["copyRight"=>$phparray[$i]["copyRight"]];
$constructor += ["privacy"=>$phparray[$i]["privacy"]];
$constructor += ["UID"=>$phparray[$i]["UID"]];
$constructor += ["imagetype"=>$phparray[$i]["imagetype"]];
$constructor += ["isApproved"=>"true"];
$returnData [] = $constructor;
}else{// saves the non edited elements
$returnData [] = $phparray[$i];
}//else
}//for
}else{// if the user deleteed an element unlink the photoes and don't save the JSON data
for($i = 0; $i < $length; $i++){
if("ID: " . $phparray[$i]["UID"] == $_POST["imgUIDEdit"]){
unlink(realpath("thumbnails/" . $phparray[$i]["UID"] . "." . $phparray[$i]["imagetype"]));
unlink(realpath("uploadimages/" . $phparray[$i]["UID"] . "." . $phparray[$i]["imagetype"]));
}else{
$returnData [] = $phparray[$i];
}
}
}
// encode the returnData array to json
$jsoncode = json_encode($returnData, JSON_PRETTY_PRINT);
// write the jsoncode to the allinfo.json file
file_put_contents($file, $jsoncode);
//updates allinfo.json
$fileA = "allinfo.json";
$jsonstring = file_get_contents($fileA); // read json file into array of strings
$allPhparray = json_decode($jsonstring, true); //decode the string from json to PHP array
/*
$newPhparray = [];
$countAll = count(allPhparray);
$found = false;
for($i = 0; $i < $countAll; $i++){
for($j = 0; $j < $length; $j++){
if($allPhparray[$i]["UID"] == $returnData[$j]["UID"]){
$newPhparray [] = $returnData[$j];
$found = true;
break
}
}
if(!$found){
newPhparray [] = $allPhparray[$i];
}
$found = false;
}
*/
//merges a the two arrays together
$newPhparray = $allPhparray + $returnData;
$constructNewArray = [];
//removes the elementfrom the JSON
if(!empty($_POST["Delete"])){
$countAll = count($newPhparray);
for($i = 0; $i < $countAll; $i++){
if("ID: " . $newPhparray[$i]["UID"] != $_POST["imgUIDEdit"]){
$constructNewArray [] = $newPhparray[$i];
}
}
}
/*
echo"<pre>";
var_dump($constructNewArray);
echo"</pre>";
echo $countAll;
*/
//saves the json to allinfo
$jsoncode = json_encode($constructNewArray, JSON_PRETTY_PRINT);
file_put_contents($fileA, $jsoncode);
//clears post
unset($_POST);
//redirect to approval.php and **force it to reload gallery
header("Location: index.php");
?>