Odyssey
TrustedReport.proxy.data
1 <?php
2 /* File: TrustedReport.proxy
3  * Purpose: aggregates data from TrustedReport.data calls so that all the servers show up in the report.
4  */
5 
6 header('Content-Type: application/json');
7 
8 $monLibrary= dirname(__FILE__) . "/../library";
9 $sharedLibrary= dirname(__FILE__) . "/../../shared/library";
10 require_once("$monLibrary/cu_top.i");
11 require_once("$monLibrary/ck_hticket.i");
12 require_once("$sharedLibrary/cutrusted.i");
13 
14  if (!CheckPerm($link, $Hu, basename($_SERVER['SCRIPT_NAME']), $_SERVER['REMOTE_ADDR'])) {
15  // ** Permissions failed
16  // ** redirect to new page
17  header("Location: /hcuadm/hcu_noperm.prg");
18  exit;
19  }
20 
21 $dbh= $link;
22 $dms_ok = array('action'=>'string', 'trustid'=>'string', 'cu'=>'string');
23 
24 dms_import($dms_ok);
25 
26 switch($action)
27 {
28  case "read_master":
29  readMaster($dbh);
30  break;
31  case "read_masterDef":
32  readMasterDetails($dbh, $trustid);
33  break;
34  case "read_clients":
35  clientAggregation($trustid);
36  break;
37  case "export_clients":
38  exportClients($dbh, $trustid);
39  break;
40  default:
41  print json_encode(array("homecuErrors" => array("Unexpected action: {$action}")));
42 }
43 
44 /* Function copied from TrustedReport.data.
45  * The difference is it is always getting from monitor.
46  */
47 function readMaster($dbh) {
48  if (strpos($_SERVER["SERVER_NAME"], "www4") !== false) // Will need to get from monitor
49  {
50  print runCurl("www", array("action" => "read_master"), true);
51  return;
52  }
53 
54  $parms = array();
55  $return = cutm_list($dbh, $parms);
56 
57  $aryResult = array();
58  if (is_array($return['data'])) {
59  foreach ($return['data'] as $key => $val) {
60  $thisprop = array();
61  if (is_array($val['fields'])) {
62  foreach ($val['fields'] as $vk => $vv) {
63  if (is_array($vv)) {
64  $thisprop[] = array('vname' => $vk, 'vtype' => $vv['Type'], 'vdefault' => $vv['Default']);
65  }
66  }
67  }
68  $thisun = array(
69  'trustedid' => $val['trustedid'],
70  'trustedvendor' => $val['trustedvendor'],
71  'trustedtype' => $val['trustedtype'],
72  'trustedprops' => array_values($thisprop));
73  $return['data'][$key] = $thisun;
74  }
75  }
76  $aryResult=(is_array($return['data']) ? array_values($return['data']) : '[]');
77 
78  if ($aryResult == '[]') {
79  $aryReply["homecuData"]=null;
80  $aryReply["homecuCount"] = 0;
81  } else {
82  $aryReply["homecuData"] = $aryResult;
83  $aryReply["homecuCount"] = count($aryResult);
84  }
85 
86  print json_encode($aryReply);
87 }
88 
89 /* This function queries the cuinfo table to see what servers it needs to pull from and calls the TrustedReport.data file from each server.
90  * PARAMETERS: trustedid-- the trustedId to pull data for.
91  */
92 function clientAggregation($trustid, $sendas = 'JSON') {
93 
94  global $link, $SYS_TYPE_WEBONLY, $SYS_TYPE_CLOSED;
95  $errors= array();
96  $serversUsed= array();
97  $cuServerLookup= array();
98 
99  $sql= "select lower(user_name) as cu, www_server from cuinfo where system_options & $SYS_TYPE_WEBONLY = 0 and system_options & $SYS_TYPE_CLOSED = 0";
100  $sth = db_query($sql,$link);
101 
102  $i = 0;
103 
104  while($dRecord = db_fetch_assoc($sth, $i++))
105  {
106  $cu= trim($dRecord["cu"]);
107  $server= trim($dRecord["www_server"]);
108  $cuServerLookup[$cu] = $server;
109 
110  if ($server != "" ) {
111  $serversUsed[$server]= true;
112  }
113  }
114  db_free_result($sth);
115 
116  $dataArray= array();
117  $parameters= array("action" => "read_clients", "trustid" => $trustid);
118  $data= array();
119  foreach ($serversUsed as $server => $unused)
120  {
121  $dataArray[$server]= json_decode(runCurl($server, $parameters), true);
122  }
123 
124  foreach($dataArray as $server => $serverResults)
125  {
126  if (is_array($serverResults["homecuData"]))
127  {
128  foreach($serverResults["homecuData"] as $record)
129  {
130  $cu = strtolower(trim($record["cu"]));
131  $correctServer= HCU_array_key_value($cu, $cuServerLookup);
132  if (isset($correctServer) && $correctServer == $server) // Checks to see the data is in the right server for the CU.
133  {
134  $record["server"]= $server;
135  $data[]= $record;
136  }
137  }
138 
139  if (HCU_array_key_exists("homecuErrors", $serverResults) && is_array($serverResults["homecuErrors"])) {
140  $errors= array_merge($errors, $serverResults["homecuErrors"]);
141  }
142  }
143  else
144  $errors[] = "$server did not get appropriate results.";
145  }
146 
147  if ($sendas == 'JSON') {
148  print json_encode(array("homecuCount" => count($data), "homecuData" => $data, "homecuErrors" => $errors));
149  } else {
150  return (array("homecuCount" => count($data), "homecuData" => $data, "homecuErrors" => $errors));
151  }
152 }
153 
154 /* Calls TrustedReport.data or TrustedReport.proxy for data.
155  * PARAMETERS: $server-- server to call i.e. www4,
156  * $parameters-- array of parameters for the particular call
157  * $useProxy-- if true, calls TrustedReport.proxy, if false calls TrustedReport.data.
158  */
159 function runCurl($server, $parameters, $useProxy = false) {
160 
161  $cmd = "";
162  $curlcookies = "HCUTicket={$_COOKIE['HCUTicket']}";
163  $mammothServers = array('www3','www4','www5','www6','www');
164  if (in_array($server, $mammothServers)) {
165  $cmd = "https://${server}.homecu.net/hcuadm/TrustedReport." . ($useProxy ? "proxy.data" : "data");
166  } else {
167 
168  if ($server == "localhost") {
169  $cmd = "http://${server}/hcuadm/TrustedReport." . ($useProxy ? "proxy.data" : "data");
170  } else {
171  $cmd = "https://${server}/hcuadm/TrustedReport." . ($useProxy ? "proxy.data" : "data");
172  $curlcookies .= ";homecu_dev_oauth2_proxy={$_COOKIE['homecu_dev_oauth2_proxy']}";
173  }
174  }
175 
176  $ch=curl_init($cmd);
177 
178  curl_setopt($ch,CURLOPT_COOKIE, $curlcookies);
179  curl_setopt($ch,CURLOPT_USERPWD,"nobody:no1home");
180  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); # get response as string
181  curl_setopt($ch,CURLOPT_POST, true);
182  curl_setopt($ch,CURLOPT_POSTFIELDS, $parameters);
183 
184  $rawresp=curl_exec($ch);
185 
186  return $rawresp;
187 }
188 
189 function exportClients($dbh, $trustid) {
190 
191  $fields = array(
192  'trustid' => $trustid,
193  'cu' => ''
194  );
195 
196  //getting the master list of trusted details for a given vendor
197  $masterList = readMasterDetails( $dbh, $trustid, 'noJSON' );
198 
199  //create new array of master trusted details for the vendor
200  $masterElem = array();
201 
202  if( HCU_array_key_exists( 'trustedfields', $masterList['homecuData'] ) ) {
203  $masterElem = array_keys( $masterList['homecuData']['trustedfields'] );
204  }
205 
206  //getting list of clients and their trusted details for a specific vendor
207  $clientList = clientAggregation($trustid, 'noJSON');
208 
209  if (!count($clientList['homecuData']) ) {
210  //error occured, no records to report
211  $errorList = $clientList['homecuErrors'];
212 
213  //creating the csv file to export
214  $fileName = 'Errors.csv';
215  ob_clean();
216  header('Pragma: public');
217  header('Expires: 0');
218  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
219  header('Cache-Control: private', false);
220  header('Content-Type: text/csv');
221  header('Content-Disposition: attachment;filename=' . $fileName);
222 
223  if (isset( $errorList ) ) {
224  $fp = fopen('php://output', 'w');
225  fputcsv($fp, $errorList);
226  }
227  ob_flush();
228 
229  } else {
230 
231  //create new array for csv client data
232  $outputElem = array();
233  // put into a consistent structure
234  $output = array();
235 
236  if ( isset( $clientList['homecuData'] ) ) {
237  $clientDetails = $clientList['homecuData'];
238 
239  foreach ( $clientDetails as $innerArray ) {
240  $outputElem['trustedid'] = $innerArray['trustedid'];
241  $outputElem['cu'] = $innerArray['cu'];
242  $outputElem['server'] = $innerArray['server'];
243  if ( HCU_array_key_exists( 'trustedprops', $innerArray ) ) {
244  $clientFields = array();
245  foreach ($innerArray['trustedprops'] as $prop) {
246  $clientFields[$prop['propname']] = $prop['propval'];
247  }
248  foreach ( $masterElem as $melem => $mpropname ) {
249  //if property exist for client
250  if ( HCU_array_key_exists ( $mpropname, $clientFields ) ) {
251  $outputElem[$mpropname] = $clientFields[$mpropname];
252  } else {
253  $outputElem[$mpropname] = "";
254  }
255  }
256  }
257  $output[] = $outputElem;
258  }
259  }
260 
261  //creating the csv file to export
262  $fileName = 'Export.csv';
263  ob_clean();
264  header('Pragma: public');
265  header('Expires: 0');
266  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
267  header('Cache-Control: private', false);
268  header('Content-Type: text/csv');
269  header('Content-Disposition: attachment;filename=' . $fileName);
270 
271  if(isset($output['0'])){
272  $fp = fopen('php://output', 'w');
273  fputcsv($fp, array_keys($output['0']));
274  foreach($output as $values){
275  fputcsv($fp, $values);
276  }
277  }
278  ob_flush();
279  }
280 
281 }
282 
283 /* This function will return the master list of trusted details
284  * for a given vendor. This is used for the trusted detail export,
285  * so it is not needed in JSON format.
286  * PARAMETERS: dbh - database handle, parms - must contain the
287  * vendor to pull data for, CU is optional
288  * RETURNS: master detail array - NOT in JSON format
289  */
290 function readMasterDetails( $dbh, $trustid, $sendas='JSON' ) {
291  if ( strpos( $_SERVER["SERVER_NAME"], "www4") !== false ) {
292  // Master list for www4 comes from monitor server www instead
293  // because www4 has a lot of testing garbage.
294  // So get this from monitor server as well, to be consistent
295  $response = json_decode(runCurl( "www", array("action" => "read_masterDef", "trustid" => $trustid), true), true);
296  return $response;
297  }
298 
299  $parms = array();
300  $parms['trustedid'] = $trustid;
301 
302  $return = cutm_read( $dbh, $parms );
303  $aryResult = array();
304 
305  $aryResult = ( isset( $return['data'] ) ? ( $return['data'] ) : '[]' );
306 
307  if ( $aryResult == '[]' ) {
308  $aryReply["homecuData"]=null;
309  $aryReply["homecuCount"] = 0;
310  } else {
311  $aryReply["homecuData"] = $aryResult;
312  $aryReply["homecuCount"] = count($aryResult);
313  }
314 
315  if ($sendas == 'JSON') {
316  print json_encode($aryReply);
317  } else {
318  return $aryReply;
319  }
320 
321 }