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