forked from rbwatson/wlux_test_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.php
More file actions
210 lines (198 loc) · 6.61 KB
/
Copy pathlog.php
File metadata and controls
210 lines (198 loc) · 6.61 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
<?php
require 'config_files.php';
// handles log database requests
$DB_SERVER = 'localhost';
$link = mysqli_connect($DB_SERVER, $DB_USER, $DB_PASS, $DB_DATABASE_NAME);
if (!$link) {
// can't open DB so return an error
// this line only works on PHP > 5.4.0, which not everyone seems to have.
// http_response_code(500);
// this works on PHP > 4.3 (or so)
$errData['message'] = 'Can\'t connect to server: '.$DB_SERVER.' as: '.$DB_USER;
$response['error'] = $errData;
} else {
// get the request data
if (!empty($HTTP_RAW_POST_DATA)) {
$postData = json_decode($HTTP_RAW_POST_DATA,true);
}
// if the data is not in the raw post data, try the post form
if (empty($postData)) {
$postData = $_POST;
}
// if the data is not in the the post form, try the query string
if (empty($postData)) {
$postData = $_GET;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// add record to the appropriate log table
// determine log type from variable name
$logType = 'open';
if (!empty($postData[$logType])) {
$logData = $postData[$logType];
// $logData contains an open data block
$logTable = $DB_TABLE_OPEN_LOG;
// TODO: Validate fields
} else {
// $logData contains an transition data block
$logType = 'transition';
if (!empty($postData[$logType])) {
$logData = $postData[$logType];
// process transition log request
$logTable = $DB_TABLE_TRANSITION_LOG;
// TODO: Validate fields
} else {
// unrecognized command
$errData['message'] = 'Log type not recognized. Log type must be \'open\' or \'transition\'';
$errData['postData'] = $postData;
$errData['getData'] = $_GET;
//$errData['globals'] = $GLOBALS;
$response['error'] = $errData;
}
}
if (!empty($logTable)) {
// process the log request
// make query string from the data structure
// add server-generated fields to insert query
$dbColList = 'recordSeq, serverTimestamp, recordType';
$dbValList = 'NULL, CURRENT_TIMESTAMP, \''.$logType.'\'';
// add the client-provided fields
foreach ($logData as $dbCol => $dbVal) {
isset($dbColList) ? $dbColList .= ', ' : $dbColList = '';
isset($dbValList) ? $dbValList .= ', ' : $dbValList = '';
$dbColList .= $dbCol;
if (empty($dbVal) && (strlen($dbVal)==0)) {
$dbValList .= 'NULL';
} else {
$dbValList .= '\''.$dbVal.'\'';
}
}
// everything goes into the transition log
$queryString = 'INSERT INTO '.$DB_TABLE_TRANSITION_LOG.' ('.$dbColList.') VALUES ('.$dbValList.')';
$qResult = mysqli_query($link, $queryString);
// $respDbg['globals'] = $GLOBALS;
$respDbg['table'] = $logTable;
$respDbg['queryString'] = $queryString;
$respDbg['argData'] = $logData;
$respDbg['columns'] = $dbColList;
$respDbg['values'] = $dbValList;
$response['debug'] = $respDbg;
if (!$qResult) {
// SQL ERROR
$respData['sqlQuery'] = $query_string;
$respData['result'] = 'Error logging data to OPEN log';
$respData['sqlError'] = mysqli_sqlstate($link);
$respData['message'] = mysqli_error($link);
$response['error'] = $respData;
} else {
// success
$respData['result'] = $qResult;
$respData['message'] = 'Log record added to '.$logType.' log';
$response['data'] = $respData;
}
}
} else if ($_SERVER['REQUEST_METHOD'] == 'GET') {
// query the database for the requested info
// check the parameters
$thisParam = 'sessionId';
$sessionId = 0;
if (array_key_exists($thisParam, $postData)) {
$sessionId = trim($postData[$thisParam]);
if (!is_numeric($sessionId)) {
$badParam[$thisParam] = "Not a number";
}
} else {
$badParam[$thisParam] = "Missing";
}
$thisParam = 'taskId';
$taskId = 0;
if (array_key_exists($thisParam, $postData)) {
$taskId = trim($postData[$thisParam]);
if (!is_numeric($taskId)) {
$badParam[$thisParam] = "Not a number";
}
} else {
// Task ID == 0 == All tasks in the session
$taskId = 0;
}
//+
if (empty($badParam)) {
// no parameter errors, so get task configuration record
// first get the open records
$response['debug']['sessionId'] = $sessionId ;
$response['debug']['taskId'] = $taskId ;
if ($taskId > 0) {
$query = 'SELECT * FROM '.$DB_TABLE_TRANSITION_LOG.
' WHERE taskId = '.$taskId.
' AND sessionId = '.$sessionId.
' ORDER BY serverTimestamp ;';
} else {
// get all tasks for this session
$query = 'SELECT * FROM '.$DB_TABLE_TRANSITION_LOG.
' WHERE sessionId = '.$sessionId.
' ORDER BY serverTimestamp ;';
}
$response['debug']['query'] = $query;
$result = mysqli_query ($link, $query);
if ($result) {
$openLogRecords = array();
$openLogRecordCount = 0;
while ($thisRecord = mysqli_fetch_assoc($result)) {
unset($thisRecord['recordSeq']);
if ($taskId > 0) {
$openLogRecords[$openLogRecordCount] = array_merge($thisRecord);
$openLogRecordCount = $openLogRecordCount + 1;
} else {
$thisTask = $thisRecord['taskId'];
if (empty($openLogRecords[$thisTask])) {
$openLogRecords[$thisTask] = array();
}
array_push($openLogRecords[$thisTask], array_merge($thisRecord));
}
}
$response['data'] = $openLogRecords;
} else {
// query error
$respData['sqlQuery'] = $query;
$respData['result'] = 'Error logging data to OPEN log';
$respData['sqlError'] = mysqli_sqlstate($link);
$respData['message'] = mysqli_error($link);
$response['error'] = $respData;
}
} else {
// bad or missing parameter
$localErr = '';
$localErr['message'] = 'Bad parameter in finish request.';
$localErr['paramError'] = $badParam;
$localErr['request'] = $postData;
// $errData['globals'] = $GLOBALS;
$errData['validation'] = $localErr;
}
//-
} else {
// method not supported
$errData['message'] = 'HTTP method not recognized. Method must be \'GET\' or \'POST\'';
$response['error'] = $errData;
}
mysqli_close($link);
}
if (!headers_sent()) {
header('content-type: application/json');
header('X-PHP-Response-Code: 200', true, 200);
}
$thisParam = "callback";
if (array_key_exists($thisParam, $_GET)) {
$jsonpTag = $_GET[$thisParam]; // set by jquery ajax call when using jsonp data type
}
if (!empty($jsonpTag)) {
// format and send output
// no error information is returned in the JSONP response!
$fnResponse = $jsonpTag . '(' . json_encode($response['data']) . ')';
} else {
// no callback param name so return an error
// this line only works on PHP > 5.4.0, which not everyone seems to have.
// http_response_code(500);
// this works on PHP > 4.3 (or so)
$fnResponse = json_encode($response);
}
print ($fnResponse);
?>