-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceDeskDatabase.class.php
More file actions
302 lines (246 loc) · 7.34 KB
/
ServiceDeskDatabase.class.php
File metadata and controls
302 lines (246 loc) · 7.34 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
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
<?php
/**
* JIRA REST library
*
* @package lib-jira
* @file ServiceDeskDatabase.class.php
* @created 12.13.2012
* @author JKCampbell <jimcampbell@psu.edu>
*/
/**
* DATABASE TABLES
* -----------------------------------------------------------
*
* SD_ISSUE
* ------------------------------
* | issue_id | INT (PK) |
* | serialized_data | STRING |
* | created_date | DATE |
* ------------------------------
*
* SD_ATTACHMENT
* ---------------------------------
* | attachment_id | INT (PK)|
* | issue_id | INT |
* | attachment_filename | STRING |
* | attachment_content | BLOB |
* ---------------------------------
*
*/
class ServiceDeskDatabase extends SQLite3 {
private $db_filename;
private $attachment_tables_created = false;
private $email_tables_created = false;
/**
* build! destroy!
*
*/
function __construct($db_filename) {
$this->open($db_filename);
$this->exec('CREATE TABLE IF NOT EXISTS sd_issue (issue_id INTEGER PRIMARY KEY, issue_serialized BLOB, created_date DATE)');
$this->exec('CREATE TABLE IF NOT EXISTS sd_attachment (attachment_id INTEGER PRIMARY KEY, issue_id INTEGER, attachment_path STRING, attachment_filename STRING, attachment_type STRING, attachment_content BLOB)');
$this->db_filename = $db_filename;
}
function __destruct() {
// this caused insert errors
//$this->close();
}
/**
* db_error - throws an ServiceDeskException built with db error
*
* @param none
* @return exception, with db error code and message
*
*/
private function db_error() {
// get db errors
$lastErrorCode = $this->lastErrorCode();
$lastErrorMsg = $this->lastErrorMsg();
// build ServiceDeskException
$e = new ServiceDeskException($lastErrorMsg);
$e->sd_code = $lastErrorCode;
$e->sd_message = $lastErrorMsg;
throw ($e);
}
/**
* db_exec - xecute a result-less query against a given database
*
* @param string, an INSERT, UPDATE, or DELETE query
* @return true - success
* @throws ServiceDeskException on error
*
*/
public function db_exec($query) {
if (!$this->exec($query)) {
$this->db_error();
}
return 1;
}
/**
* function issue_delete - delete issue from database
*
* @param int, issue_id
* @return 1 - success
* @throws ServiceDeskException on error
*
*/
public function issue_delete($issue_id) {
// delete issue
$stmt = $this->prepare("DELETE FROM sd_issue WHERE issue_id=:issue_id");
$stmt->bindValue(':issue_id', $issue_id, SQLITE3_INTEGER);
if (!$results = $stmt->execute()) {
$this->db_error();
}
// delete attachments
$stmt = $this->prepare("DELETE FROM sd_attachment WHERE issue_id=:issue_id");
$stmt->bindValue(':issue_id', $issue_id, SQLITE3_INTEGER);
if (!$results = $stmt->execute()) {
$this->db_error();
}
return 1;
}
/**
* function issue_get - get full issue
*
* @param int, issue_id
* @return object, full object of issue
*
*/
public function issue_get($issue_id) {
// get issue
$stmt = $this->prepare("SELECT * FROM sd_issue WHERE issue_id=:issue_id");
$stmt->bindValue(':issue_id', $issue_id, SQLITE3_INTEGER);
if (!$results = $stmt->execute()) {
$this->db_error();
}
// build issue object
$row_count = 0;
$issue = new stdClass();
while ($row = $results->fetchArray()) {
$issue_id = $row['issue_id'];
$issue_serialized = $row['issue_serialized'];
$issue_unserialized = unserialize($issue_serialized);
$issue = $issue_unserialized;
$issue->issue_id = $issue_id;
$row_count++;
}
// no issue
if (!$row_count) {
return $issue;
}
// get issue attachments
$issue->attachments = array ();
$stmt = $this->prepare("SELECT * FROM sd_attachment WHERE issue_id=:issue_id");
$stmt->bindValue(':issue_id', $issue_id, SQLITE3_INTEGER);
$results = $stmt->execute();
while ($row = $results->fetchArray()) {
// build attachments
$attachment = array ();
foreach ($row as $key => $value) {
if (!is_int($key)) {
$attachment[$key] = $value;
}
}
$issue->attachments[] = $attachment;
}
return $issue;
}
/**
* function issue_get_ids - get issue_ids of all issues
*
* @param none
* @return array, an array of issue_ids
*
*/
public function issue_get_ids() {
$ret = array ();
// get issue ids
$query = "SELECT issue_id from sd_issue";
$results = $this->query($query);
while ($row = $results->fetchArray()) {
$ret[] = $row['issue_id'];
}
return $ret;
}
/**
* function issue_save - save an email to the database
*
* @param $email - object, the email headers, text, and html body
* @return beer
*
*/
public function issue_save($email) {
// remove attachments from email object
$attachments = array ();
if (property_exists($email, 'attachments')) {
$attachments = $email->attachments;
}
$email->attachments = array ();
// insert issue
$time = time();
$issue_serialized = serialize($email);
$issue_escaped = $this->escapeString($issue_serialized);
$query = "INSERT INTO sd_issue (issue_serialized, created_date) VALUES ('" . $issue_escaped . "', $time)";
if (!$result = $this->exec($query)) {
$this->db_error();
}
// get issue id
$issue_id = $this->lastInsertRowID();
// save attachments to a different table
if ($attachments) {
$this->attachment_save($issue_id, $attachments);
}
return $issue_id;
}
/**
* function attachment_delete - delete attachments from database
*
* @param int, issue_id
* @param int, attachment_id
* @return 1 - success
* @throws ServiceDeskException on error
*
*/
public function attachment_delete($issue_id, $attachment_id) {
// delete issue
$stmt = $this->prepare("DELETE FROM sd_attachment WHERE issue_id=:issue_id AND attachment_id=:attachment_id");
$stmt->bindValue(':issue_id', $issue_id, SQLITE3_INTEGER);
$stmt->bindValue(':attachment_id', $attachment_id, SQLITE3_INTEGER);
if (!$results = $stmt->execute()) {
$this->db_error();
}
return 1;
}
/**
* function attachment_save - save an attachment to the database
*
* @param int, issue_id in database
* @param array, attachment filenames and base64_encoded content
* @return
*
*/
private function attachment_save($issue_id, $attachments) {
$attachment_ids = array ();
// loop over each attachment
foreach ($attachments as $attachment) {
$attachment_path = $attachment['attachment_path'];
$attachment_filename = $attachment['attachment_filename'];
$attachment_type = $attachment['attachment_type'];
$attachment_content = $attachment['attachment_content'];
// insert attachment
$attachment_path_escaped = $this->escapeString($attachment_path);
$attachment_filename_escaped = $this->escapeString($attachment_filename);
$attachment_type_escaped = $this->escapeString($attachment_type);
$attachment_content_encoded = base64_encode($attachment_content);
$query = "INSERT INTO sd_attachment (issue_id, attachment_path, attachment_filename, attachment_type, attachment_content) VALUES ($issue_id, '$attachment_path_escaped', '$attachment_filename_escaped', '$attachment_type_escaped', '$attachment_content_encoded')";
if (!$result = $this->exec($query)) {
$this->db_error();
}
// get attachment id
$attachment_id = $this->lastInsertRowID();
$attachment_ids[] = $attachment_id;
}
return $attachment_ids;
}
}
?>