6 header(
'Content-Type: application/json');
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");
14 if (!CheckPerm($link, $Hu, basename($_SERVER[
'SCRIPT_NAME']), $_SERVER[
'REMOTE_ADDR'])) {
17 header(
"Location: /hcuadm/hcu_noperm.prg");
22 $dms_ok = array(
'action'=>
'string',
'trustid'=>
'string',
'cu'=>
'string');
31 case "read_masterDef":
32 readMasterDetails($dbh, $trustid);
35 clientAggregation($trustid);
37 case "export_clients":
38 exportClients($dbh, $trustid);
41 print json_encode(array(
"homecuErrors" => array(
"Unexpected action: {$action}")));
47 function readMaster($dbh) {
48 if (strpos($_SERVER[
"SERVER_NAME"],
"www4") !==
false)
50 print runCurl(
"www", array(
"action" =>
"read_master"),
true);
55 $return = cutm_list($dbh, $parms);
58 if (is_array($return[
'data'])) {
59 foreach ($return[
'data'] as $key => $val) {
61 if (is_array($val[
'fields'])) {
62 foreach ($val[
'fields'] as $vk => $vv) {
64 $thisprop[] = array(
'vname' => $vk,
'vtype' => $vv[
'Type'],
'vdefault' => $vv[
'Default']);
69 'trustedid' => $val[
'trustedid'],
70 'trustedvendor' => $val[
'trustedvendor'],
71 'trustedtype' => $val[
'trustedtype'],
72 'trustedprops' => array_values($thisprop));
73 $return[
'data'][$key] = $thisun;
76 $aryResult=(is_array($return[
'data']) ? array_values($return[
'data']) :
'[]');
78 if ($aryResult ==
'[]') {
79 $aryReply[
"homecuData"]=
null;
80 $aryReply[
"homecuCount"] = 0;
82 $aryReply[
"homecuData"] = $aryResult;
83 $aryReply[
"homecuCount"] = count($aryResult);
86 print json_encode($aryReply);
92 function clientAggregation($trustid, $sendas =
'JSON') {
94 global $link, $SYS_TYPE_WEBONLY, $SYS_TYPE_CLOSED;
96 $serversUsed= array();
97 $cuServerLookup= array();
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);
104 while($dRecord = db_fetch_assoc($sth, $i++))
106 $cu= trim($dRecord[
"cu"]);
107 $server= trim($dRecord[
"www_server"]);
108 $cuServerLookup[$cu] = $server;
110 if ($server !=
"" ) {
111 $serversUsed[$server]=
true;
114 db_free_result($sth);
117 $parameters= array(
"action" =>
"read_clients",
"trustid" => $trustid);
119 foreach ($serversUsed as $server => $unused)
121 $dataArray[$server]= json_decode(runCurl($server, $parameters),
true);
124 foreach($dataArray as $server => $serverResults)
126 if (is_array($serverResults[
"homecuData"]))
128 foreach($serverResults[
"homecuData"] as $record)
130 $cu = strtolower(trim($record[
"cu"]));
131 $correctServer= HCU_array_key_value($cu, $cuServerLookup);
132 if (isset($correctServer) && $correctServer == $server)
134 $record[
"server"]= $server;
139 if (HCU_array_key_exists(
"homecuErrors", $serverResults) && is_array($serverResults[
"homecuErrors"])) {
140 $errors= array_merge($errors, $serverResults[
"homecuErrors"]);
144 $errors[] =
"$server did not get appropriate results.";
147 if ($sendas ==
'JSON') {
148 print json_encode(array(
"homecuCount" => count($data),
"homecuData" => $data,
"homecuErrors" => $errors));
150 return (array(
"homecuCount" => count($data),
"homecuData" => $data,
"homecuErrors" => $errors));
159 function runCurl($server, $parameters, $useProxy =
false) {
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");
168 if ($server ==
"localhost") {
169 $cmd =
"http://${server}/hcuadm/TrustedReport." . ($useProxy ?
"proxy.data" :
"data");
171 $cmd =
"https://${server}/hcuadm/TrustedReport." . ($useProxy ?
"proxy.data" :
"data");
172 $curlcookies .=
";homecu_dev_oauth2_proxy={$_COOKIE['homecu_dev_oauth2_proxy']}";
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);
184 $rawresp=curl_exec($ch);
189 function exportClients($dbh, $trustid) {
192 'trustid' => $trustid,
197 $masterList = readMasterDetails( $dbh, $trustid,
'noJSON' );
200 $masterElem = array();
202 if( HCU_array_key_exists(
'trustedfields', $masterList[
'homecuData'] ) ) {
203 $masterElem = array_keys( $masterList[
'homecuData'][
'trustedfields'] );
207 $clientList = clientAggregation($trustid,
'noJSON');
209 if (!count($clientList[
'homecuData']) ) {
211 $errorList = $clientList[
'homecuErrors'];
214 $fileName =
'Errors.csv';
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);
223 if (isset( $errorList ) ) {
224 $fp = fopen(
'php://output',
'w');
225 fputcsv($fp, $errorList);
232 $outputElem = array();
236 if ( isset( $clientList[
'homecuData'] ) ) {
237 $clientDetails = $clientList[
'homecuData'];
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'];
248 foreach ( $masterElem as $melem => $mpropname ) {
250 if ( HCU_array_key_exists ( $mpropname, $clientFields ) ) {
251 $outputElem[$mpropname] = $clientFields[$mpropname];
253 $outputElem[$mpropname] =
"";
257 $output[] = $outputElem;
262 $fileName =
'Export.csv';
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);
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);
290 function readMasterDetails( $dbh, $trustid, $sendas=
'JSON' ) {
291 if ( strpos( $_SERVER[
"SERVER_NAME"],
"www4") !==
false ) {
295 $response = json_decode(runCurl(
"www", array(
"action" =>
"read_masterDef",
"trustid" => $trustid),
true),
true);
300 $parms[
'trustedid'] = $trustid;
302 $return = cutm_read( $dbh, $parms );
303 $aryResult = array();
305 $aryResult = ( isset( $return[
'data'] ) ? ( $return[
'data'] ) :
'[]' );
307 if ( $aryResult ==
'[]' ) {
308 $aryReply[
"homecuData"]=
null;
309 $aryReply[
"homecuCount"] = 0;
311 $aryReply[
"homecuData"] = $aryResult;
312 $aryReply[
"homecuCount"] = count($aryResult);
315 if ($sendas ==
'JSON') {
316 print json_encode($aryReply);