-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwrite.php
More file actions
161 lines (155 loc) · 5.53 KB
/
write.php
File metadata and controls
161 lines (155 loc) · 5.53 KB
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
<?php
// @codingStandardsIgnoreStart
$file = "db/".$_POST["db"].".json";
$apiDocumentInArray = json_decode(file_get_contents($file), true);
session_start();
header("Content-Type: application/json");
$logger = new Logger();
if (isset($_SESSION["userName"]) && $_SESSION["userName"] != "") {
$message = "";
$data = $_POST;
$action = $data["action"];
if ($action == "createnew") {
if (isset($data["copyof"]) && $data["copyof"] == "") {
$retrunData = [
"success" => true,
"message" => "New document has been created successfuly."
];
$tempData = [
"main-domain" => $data["domainUrl"],
"main-description" => $data["docDescription"],
"access" => [
$_SESSION["userName"]
],
"group-list"=> [
[
"group" => $data["docGroupName"],
"description" => $data["docGroupDescription"],
"api-list" => [
]
]
]
];
$newDocName = "db/".strtolower($data["docName"]).".json";
if (!is_file($newDocName)) {
$fp = fopen($newDocName, "w");
fwrite($fp, json_encode($tempData));
fclose($fp);
} else {
$retrunData = [
"success" => false,
"message" => "Suggest another doc name, this name is already taken."
];
}
echo json_encode(
$retrunData,
true
);
return;
} else {
$copyofDoc = "db/".$data["copyof"].".json";
if (is_file($copyofDoc)) {
$copyDocInArray = json_decode(file_get_contents($copyofDoc), true);
$tmp = $copyDocInArray["access"];
if (!in_array($_SESSION["userName"], $tmp)) {
$tmp[] = $_SESSION["userName"];
$copyDocInArray["access"] = $tmp;
}
$copytoDoc = "db/".strtolower($data["docName"]).".json";
$fp = fopen($copytoDoc, "w");
fwrite($fp, json_encode($copyDocInArray));
fclose($fp);
$retrunData = [
"success" => true,
"message" => "Copy of selected doc is created."
];
} else {
$retrunData = [
"success" => false,
"message" => "Invalid database."
];
}
echo json_encode(
$retrunData,
true
);
return;
}
} elseif (!in_array($_SESSION["userName"], $apiDocumentInArray["access"])) {
echo json_encode(
[
"success" => false,
"message" => "Sorry you don't have write permission for this database, Please cotact Admin."
],
true
);
return;
}
$apiIndex = $data["apiIndex"];
$groupIndex = $data["groupIndex"];
$changedGroup = $data["changedGroup"];
$apiData = json_decode($data["data"], true);
$apiData["api-list"][0]["possible-values"] = json_decode($apiData["api-list"][0]["possible-values"], true);
if ($action == "edit") {
if ($apiIndex != "" && $changedGroup == "") {
// $apiDocumentInArray["group-list"][$groupIndex]["api-list"][1] = $apiData["api-list"][0];
} else {
$apiDocumentInArray["group-list"][$groupIndex]["api-list"][] = $apiData["api-list"][0];
if ($changedGroup != "") {
unset($apiDocumentInArray["group-list"][$changedGroup]["api-list"][(int)$apiIndex]);
}
}
$message = "Api Updated Successfully.";
} elseif ($action == "new") {
if ($groupIndex == "new") {
$apiDocumentInArray["group-list"][] = $apiData;
} else {
$apiDocumentInArray["group-list"][$groupIndex]["api-list"][] = $apiData["api-list"][0];
}
$message = "Api Created Successfully.";
}
$updatedJsonData = str_replace("\/", "/", json_encode($apiDocumentInArray));
$updatedJsonData = str_replace("\u003C", "<", $updatedJsonData);
$updatedJsonData = str_replace("\u003E", ">", $updatedJsonData);
///////////////////////////////////////////////////////////////////////////
$logger->printLog("By : ".$_SESSION["userName"]);
$logger->printLog("Date : ".date("Y-m-d h:i:sa", time()));
$logger->printLog("Content :".json_encode($apiDocumentInArray)."\n\n");
///////////////////////////////////////////////////////////////////////////
file_put_contents($file, $updatedJsonData);
echo json_encode(
[
"success" => true,
"message" => $message
],
true
);
return;
} else {
echo json_encode(
[
"success" => false,
"message" => "Session Expired"
],
true
);
return;
}
class Logger
{
function printLog($data)
{
$todayFile = "log/mad_".date("Y_m_d").".log";
if (!is_file($todayFile)) {
$fp = fopen($todayFile, "w");
fwrite($fp, "");
fclose($fp);
}
$fileContent = file_get_contents($todayFile);
if (is_array($data) || is_object($data)) {
$data = print_r($data, true);
}
$data = $fileContent."\n".$data;
file_put_contents($todayFile, $data);
}
}