-
Notifications
You must be signed in to change notification settings - Fork 0
/
addLog.php
211 lines (178 loc) · 5.87 KB
/
addLog.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
<?php
include("common.php");
checkLoggedIn();
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST"){
addLog();
}else{
showLogEdit();
}
?>
<?php
# this is a function block
# pre: when the log is finished and is ready to write into xml
# post: add log and status into xml
function addLog(){
if(!isset($_POST["id"]) || !isset($_POST["status"]) || !is_numeric($_POST["id"]) ||
!statusMatch($_POST["status"])){
showErrorMessage();
die();
}
$ticket = new DOMDocument();
$ticket->load("data/tickets/".$_POST["id"].".xml");
$log = $ticket->createElement("log");
$log->setAttribute("time", microtime(true));
$log->setAttribute("author", $_SESSION["user"]);
$log->setAttribute("status", $_POST["status"]);
$text = $ticket->createElement("text", $_POST["comment"]);
$files = $ticket->createElement("files");
# check if the img is uploaded
if(is_uploaded_file($_FILES["img"]["tmp_name"])){
$files->appendChild($ticket->createElement("images", uploadImg()));
}
$log->appendChild($text);
$log->appendChild($files);
$ticket->getElementsByTagName("logs")->item(0)->appendChild($log);
if($ticket->save("data/tickets/".$_POST["id"].".xml")){
if($_POST["status"] != "Comment"){
updateStatusInDB($_POST["status"]);
}else{
openTicket();
}
header("Location: ticket.php?id=".$_POST["id"]);
}else{
showErrorMessage();
}
}
# pre: when the user did uploaded a img
# post: upload the file to imgur and return the url
/*
** For how to upload the files to imgur, see reference:
** http://subinsb.com/uploading-images-using-imgur-api-in-php
*/
function uploadImg(){
$img = file_get_contents($_FILES["img"]["tmp_name"]);
$client_id = trim(file("data/imgurAPI.txt")[0]);
$pvars = array("image" => base64_encode($img));
$upload = curl_init();
curl_setopt($upload, CURLOPT_URL, "https://api.imgur.com/3/image.json");
curl_setopt($upload, CURLOPT_TIMEOUT, 30);
curl_setopt($upload, CURLOPT_HTTPHEADER, array("Authorization: Client-ID $client_id"));
curl_setopt($upload, CURLOPT_POST, true);
curl_setopt($upload, CURLOPT_RETURNTRANSFER, true);
curl_setopt($upload, CURLOPT_POSTFIELDS, array("image" => base64_encode($img)));
curl_setopt($upload, CURLOPT_SSL_VERIFYPEER, false);
$out = curl_exec($upload);
curl_close($upload);
if($out){
$json = json_decode($out);
return $json->data->link;
}else{
print $out;
}
}
# pre: when a status is passed in as a POST method
# post: return true if the status passed in is the one we want
function statusMatch($data){
$statuses = array("Open", "Stalled", "Comment", "Fail", "Success");
foreach($statuses as $status){
if($data == $status){
return true;
}
}
return false;
}
# pre: when the user is leaving a comment with a statua change
# post: update the status change in db
function updateStatusInDB($status){
$conn = connectToDB("data/dbInformation.txt");
$id = $conn->quote($_POST["id"]);
$status = $conn->quote($status);
$conn->query("UPDATE dbo.ticket
SET [status] = $status
WHERE [id] = $id");
}
# pre: when the user is trying to left a comment and the ticket is new
# post: open the ticket and left a system comment
function openTicket(){
$conn = connectToDB("data/dbInformation.txt");
$id = $conn->quote($_POST["id"]);
$count = $conn->query("SELECT COUNT(*)
FROM dbo.ticket
WHERE [id] = $id AND [status] = 'new'");
if($count->fetchColumn() > 0){
updateStatusInDB("Open");
$ticket = new DOMDocument();
$ticket->load("data/tickets/".$_POST["id"].".xml");
$log = $ticket->createElement("log");
$log->setAttribute("time", microtime(true));
$log->setAttribute("author", "system");
$log->setAttribute("status", "Open");
$text = $ticket->createElement("text", "system has changed the status to \"Open\"");
$files = $ticket->createElement("files");
$log->appendChild($text);
$log->appendChild($files);
$ticket->getElementsByTagName("logs")->item(0)->appendChild($log);
if(!$ticket->save("data/tickets/".$_POST["id"].".xml")){
showErrorMessage();
}
}
}
# pre: when log info is not yet done
# post: show a page to prompt the user to leave a log
function showLogEdit(){
/*
** NOTE: Maybe try using imgur to save photos?
** imgur api: https://api.imgur.com/endpoints/image
*/
if(!isset($_GET["id"]) || !isset($_GET["status"]) || $_GET["id"] == "" ||
$_GET["status"] == ""){
showErrorMessage();
die();
}
HTMLHeader("Add Log", "", "");
?>
<div class="container">
<h1>新增紀錄 Add Log</h1>
<form class="form-horizontal" enctype="multipart/form-data" action="addLog.php" method="POST">
<div class="form-group">
<label for="status">狀態 Status:</label>
<select name="status" id="status" class="form-control" required>
<?php
$status = array("Open", "Stalled", "Comment", "Fail", "Success");
foreach($status as $temp){
if(strtoupper($temp) == strtoupper($_GET["status"])){
?>
<option selected value=<?=$temp?>><?=$temp?></option>
<?php
}else{
?>
<option value=<?=$temp?>><?=$temp?></option>
<?php
}
}
?>
</select>
</div>
<div class="form-group">
<label for="comment">紀錄 Comment: </label>
<textarea name="comment" id="comment" class="form-control" row="10"></textarea>
</div>
<div class="form-group">
<label for="img" class="control-label col-sm-2">上傳照片</label>
<div class="col-sm-10">
<input class="form-control" type="file" name="img" accept="image/*">
</div>
</div>
<div class="form-group">
<input type="hidden" name=id value=<?=$_GET["id"]?> />
<div class="col-sm-offest-5 col-sm-7">
<button type="submit" class="btn btn-primary">新增紀錄</button>
</div>
</div>
</form>
</div>
<?php
HTMLFooter();
}
?>