Odyssey
corerequests_report.data
1 <?php
2 /*
3 * File: corerequests_report.data
4 *
5 * Purpose: request contents of core requests from the cucorerequests table
6 */
7 
8 require_once('dms_imp_val.i');
9 require_once('cu_top');
10 
11 header('Content-type: application/json');
12 
13 try {
14  $dms_ok = array(
15  'action'=>'string',
16  'cucode'=>'string',
17  'member'=>'string',
18  'applianceip'=>'string',
19  'requesttype'=>'string',
20  'timestart'=>'string',
21  'timeend'=>'string'
22  );
23  dms_import($dms_ok);
24 
25  $action = isset($action) ? trim($action) : "";
26 
27  $aryResult = array();
28  $aryReply = array();
29 
30  $dbh = $link;
31 
32  switch ($action) {
33  case 'search':
34 
35  // Setup the where clause depending on parameters
36  $sqlWhere = "";
37 
38  if (strlen($cucode) > 0) {
39  $cucode = strtoupper($cucode);
40  $cucode = prep_save($cucode, 12);
41  $sqlWhere .= " cu = '{$cucode}' AND";
42  }
43 
44  if (strlen($member) > 0) {
45  $member = trim($member);
46  $member = prep_save($member, 12);
47  $sqlWhere .= " accountnumber = '{$member}' AND";
48  }
49 
50  if (strlen($applianceip) > 0) {
51  $applianceip = trim($applianceip);
52  $applianceip = prep_save($applianceip, 100);
53  $sqlWhere .= " appliance_ip = '{$applianceip}' AND";
54  }
55 
56  if (strlen($requesttype) > 0) {
57  $requesttype = trim($requesttype);
58  $requesttype = prep_save($requesttype, 20);
59  $sqlWhere .= " request_type = '{$requesttype}' AND";
60  }
61 
62  if (strlen($timestart) > 0) {
63  $dateStart = strtotime($timestart);
64  $timestart = date('Y-m-d H:i:s', $dateStart);
65  $sqlWhere .= " request_start >= '{$timestart}' AND";
66 
67  // Since End time must have a start date, we check for end time in side
68  // this if statement.
69  if (strlen($timeend) > 0) {
70  $dateEnd = strtotime($timeend);
71  $timeend = date('Y-m-d H:i:s', $dateEnd);
72  $sqlWhere .= " request_end <= '{$timeend}'";
73  }
74  }
75 
76  // Trim last occurence of AND
77  $sqlWhere = trim($sqlWhere);
78  $sqlWhere = rtrim($sqlWhere, "AND");
79 
80  $sql = "
81  SELECT cu, appliance_ip, accountnumber, request_type, request_url, request_start, request_end, request_elapsed, request_status, remote_ip
82  FROM cucorerequests ccr";
83  if (strlen($sqlWhere) > 0) {
84  $sql .= " WHERE $sqlWhere ORDER BY id";
85  }
86  logtofile(print_r($sql, true));
87 
88  $sqlRs = db_query($sql, $dbh);
89  if (!$sqlRs) {
90  throw new Exception(db_last_error(), 2);
91  }
92 
93  $i = 0;
94  $aryReply['requests'] = array();
95  while ($row = db_fetch_assoc($sqlRs, $i++)) {
96  // Must decode this field so it will display properly on client
97  $status = json_decode($row['request_status']);
98  $row['request_status'] = $status;
99  $aryReply['requests'][] = $row;
100  }
101  break;
102 
103  default:
104  throw new Exception("Requested action not recognized: $action", 1);
105  break;
106  }
107 } catch (Exception $e) {
108  $aryReply = array();
109  $aryReply['error'] = $e->getMessage();
110 }
111 
112 print json_encode(array("Results" => $aryReply));
113 
114 function logtofile($msg) {
115  $fh = fopen('/tmp/mgh.log', 'a+');
116  fwrite($fh, $msg);
117  fclose($fh);
118 }
119 ?>