Odyssey
cutrustdetail.data
1 <?php
2  /*
3  * File: cutrustvendor.data.v2
4  *
5  * Purpose: This script will handle the data / business logic for cu trusted vendor
6  * maintenance
7  * NOTE: properties are stored as key, value pairs in the database. This has not changed from the original.
8  */
9 
10 $monLibrary= dirname(__FILE__) . "/../library";
11 $sharedLibrary= dirname(__FILE__) . "/../../shared/library";
12 require_once("$monLibrary/cu_top.i");
13 require_once("$monLibrary/ck_hticket.i");
14 require_once("$sharedLibrary/cutrusted.i");
15 require_once("$sharedLibrary/commonPhpFunctions.i");
16 
17 
18  if (!CheckPerm($link, $Hu, "cutrustdetail", $_SERVER['REMOTE_ADDR'])) {
19  // ** Permissions failed
20  // ** redirect to new page
21  header("Location: /hcuadm/hcu_noperm.prg");
22  exit;
23  }
24 
25 $showSQL= false;
26 
27 dms_import_v2($DATA_PARAMETERS, "TOP_LEVEL", array("operation" => "string"));
28 $operation= $DATA_PARAMETERS["TOP_LEVEL"]["operation"];
29 
30 switch ($operation)
31 {
32  case "readTrustedVendors":
33  $returnArray= readTrustedVendorsDetails($dbh);
34  break;
35  case "createTrustedId":
36  $returnArray= createTrustedIdDetails($dbh);
37  break;
38  case "removeTrustedId":
39  $returnArray= removeTrustedIdDetails($dbh);
40  break;
41  case "addTrustedDetail":
42  $returnArray= changeTrustedDetailDetails($dbh, "add");
43  break;
44  case "updateTrustedDetail":
45  $returnArray= changeTrustedDetailDetails($dbh, "update");
46  break;
47  case "removeTrustedDetail":
48  $returnArray= changeTrustedDetailDetails($dbh, "delete");
49  break;
50  case "addServerSetting":
51  $returnArray= changeServiceProperty($dbh, "add");
52  break;
53  case "updateServerSetting":
54  $returnArray= changeServiceProperty($dbh, "update");
55  break;
56  case "removeServerSetting":
57  $returnArray= changeServiceProperty($dbh, "delete");
58  break;
59  case "readServerSettings":
60  $returnArray= readServerSettings($dbh);
61  break;
62  case "serviceUpdate":
63  $returnArray= serviceUpdate($dbh);
64  break;
65  case "pullDownOptions":
66  $returnArray= pullDownOptions($dbh);
67  break;
68  case "reorderProperties":
69  $returnArray= reorderProperties($dbh);
70  break;
71  default: $returnArray= array("sql" => array(), "error" => array("Operation not specified: '$operation'"), "record" => "", "operation" => "");
72 }
73 
74 if (!$showSQL)
75  unset($returnArray["sql"]);
76 header('Content-type: application/json');
77 print json_encode($returnArray);
78 
79 /**
80  * function getServerOptions()
81  * Gets the three states of the server
82  * @return the DDL
83  */
84 function getServerOptions()
85 {
86  return array("N" => "Active", "U" => "Offline (Auto Up)", "Y" => "Offline (Stay Down)");
87 }
88 
89 /**
90  * function getServiceUpdateOptions()
91  * Gets the DDL menu for updating all services
92  * @return the DDL menu
93  */
94 function getServiceUpdateOptions()
95 {
96  return array(array("text" => "Turn On", "value" => "Y"), array("text" => "Turn Off", "value" => "N"));
97 }
98 
99 /**
100  * function extractDetails($records, $cu)
101  * Extracts the details using the particular masterKey.
102  *
103  * @param array $records -- the trusted details
104  * @param string $cu -- the credit union
105  * @return array -- the records decrypted and decoded.
106  */
107 function extractDetails($records, $cu)
108 {
109  $masterKey = sha1("${cu}:3pk4osso");
110  $extractedRecords= array();
111 
112  foreach($records as $record)
113  {
114  $details= array();
115  $detailIndex= 0;
116  $detailsDecoded= array();
117 
118  if ($record["detailsEncoded"] != "")
119  {
120  $detailsDecrypted= parmdecrypt($record["detailsEncoded"], $masterKey);
121  $detailsDecoded= json_decode($detailsDecrypted, true);
122  }
123 
124  $record["detailsDecoded"]= $detailsDecoded;
125  unset($record["detailsEncoded"]);
126  $extractedRecords[]= $record;
127  }
128  return $extractedRecords;
129 }
130 
131 /**
132  * function getDetailsForGrid($almostExpandedRecords, $trustedIdDetailDDL, $masterDetailLength= array(), &$warnings)
133  * Forms the extracted details into a format for the grid.
134  *
135  * @param array $almostExpandedRecords -- the records retrieved from the database and decrypted and decoded.
136  * @param array $trustedIdDetailDDL -- the details from the master by trusted id. Needed to add field type and messages to the detail records.
137  * @param array $masterDetailLength -- contains the lengths of the details by trusted id.
138  * @param array $warnings -- if there are any warnings, append them to this array.
139  * @return array --
140  * $topRecords -- the records of the trusted ids.
141  * $detailRecords -- the records of the trusted ids' properties.
142  */
143 function getDetailsForGrid($almostExpandedRecords, $trustedIdDetailDDL, $masterDetailLength= array(), &$warnings)
144 {
145  $expandedRecords= array();
146  $details= array();
147  $detailIndex= 0;
148 
149  // print var_dump($almostExpandedRecords);
150 
151  foreach ($almostExpandedRecords as $record)
152  {
153  // print "\$record: ".var_dump($record)."\n";
154 
155  $detailDDL= $trustedIdDetailDDL[$record["trustedId"]];
156  $detailsDecoded= $record["detailsDecoded"];
157  $record["hasMasterRecord"]= isset($detailDDL);
158  if (is_array($detailsDecoded))
159  {
160  $displayOrder= 0;
161  foreach($detailsDecoded as $key => $value)
162  {
163  $fieldType= isset($detailDDL[$key]["fieldType"]) ? $detailDDL[$key]["fieldType"] : "string";
164  $message= isset($detailDDL[$key]["message"]) ? $detailDDL[$key]["message"] : "";
165  $value= stripslashes($value);
166  $message= stripslashes($message);
167 
168  $details[]= array("property" => $key, "value" => $value, "fieldType" => $fieldType, "hasMasterRecord" => $record["hasMasterRecord"], "message" => $message,
169  "trustedId" => $record["trustedId"], "displayOrder" => $displayOrder, "pid" => $displayOrder + 1);
170  $displayOrder++;
171  }
172  }
173  else if ($detailsDecoded != "")
174  {
175  $trustedId= $record["trustedId"];
176  $warnings[]= "TrustedId's details are not encoded correctly: $trustedId";
177  }
178 
179  unset($record["detailsDecoded"]);
180  $record["masterDetailLength"]= isset($masterDetailLength[$record["trustedId"]]) ? $masterDetailLength[$record["trustedId"]] : 0;
181  $expandedRecords[]= $record;
182  }
183  return array("topRecords" => $expandedRecords, "detailRecords" => $details);
184 }
185 
186 /**
187  * function encryptDetails($detailRecords, $cu)
188  * Encrypts the details.
189  *
190  * @param array $detailRecords -- the detail records to encrypt
191  * @param string $cu -- the credit union
192  * @return string -- the encrypted string to place in database.
193  */
194 function encryptDetails($detailRecords, $cu)
195 {
196  $masterKey = sha1("${cu}:3pk4osso");
197 
198  if (is_array($detailRecords) && count($detailRecords) > 0)
199  return parmencrypt(json_encode($detailRecords), $masterKey);
200  else
201  return "";
202 }
203 
204 /**
205  * function readTrustedVendorsDetails($dbh)
206  * Reads the grid (top-level and bottom-level)
207  *
208  * @param integer $dbh -- the database connection
209  */
210 function readTrustedVendorsDetails($dbh)
211 {
212  $parameters= array();
213  dms_import_v2($parameters, "BOTTOM_LEVEL", array("cu" => "string"));
214 
215  $sqls= array();
216  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
217  array("cu" => array("type" => "string", "required" => true, "maxlength" => 12)));
218  $cu= strtoupper($parameters["BOTTOM_LEVEL"]["cu"]);
219 
220  $sql= "select td.trustedid, td.parms from cutrusteddetail td where upper(td.cu) = '$cu' order by td.trustedid";
221  $queryResults= runSelectStatement($sql, $dbh, array("trustedid" => "trustedId", "parms" => "detailsEncoded"));
222  $errors= array_merge($errors, $queryResults["error"]);
223  $warnings= array();
224 
225  $almostExpandedRecords= extractDetails($queryResults["record"], $cu);
226 
227  $masterSQL= "select tm.trustedid, tm.trustedfields from cutrustedmaster tm order by tm.trustedid, tm.trustedvendor";
228  $queryResults= runSelectStatement($masterSQL, $dbh, array("trustedid" => "trustedId", "trustedfields" => "masterFields"));
229 
230  $errors= array_merge($errors, $queryResults["error"]);
231 
232  $trustedIdDetailDDL= array();
233  $masterDetailLength= array();
234  foreach($queryResults["record"] as $record)
235  {
236  $details= json_decode($record["masterFields"], true);
237  $detailDDL= array();
238  $trustedId= $record["trustedId"];
239 
240  if (is_array($details))
241  {
242  foreach($details as $key => $detailRow)
243  {
244  $detail= array("fieldType" => "string", "defaultValue" => "");
245  if (is_array($detailRow))
246  {
247  foreach($detailRow as $lowerKey => $value) // type and default
248  {
249  if ($lowerKey == "Type")
250  $detail["fieldType"]= $value;
251  else if ($lowerKey == "Default")
252  $detail["defaultValue"]= stripslashes($value);
253  else if ($lowerKey == "Message")
254  $detail["message"]= stripslashes($value);
255  }
256  }
257  $detailDDL[stripslashes($key)]= $detail;
258  }
259  $masterDetailLength[$trustedId]= count($details);
260  }
261  else if ($record["masterFields"] != "")
262  {
263  $trustedId= $record["trustedId"];
264  $warnings[]= "TrustedId master's details are not encoded correctly: $trustedId";
265  $details= array();
266  $masterDetailLength[$trustedId]= 0;
267  }
268 
269  $trustedIdDetailDDL[$trustedId]= $detailDDL;
270  }
271 
272  $expandedRecords= getDetailsForGrid($almostExpandedRecords, $trustedIdDetailDDL, $masterDetailLength, $warnings);
273 
274  return array("sql" => array($sql, $masterSQL), "error" => $errors, "topRecords" => $expandedRecords["topRecords"], "detailRecords" => $expandedRecords["detailRecords"],
275  "trustedIdDetailDDL" => $trustedIdDetailDDL, "operation" => "read", "warning" => $warnings);
276 }
277 
278 /**
279  * function readServerSettings($dbh)
280  * Reads the grid (bottom-level for the server property "HOMECUSERVICE")
281  *
282  * @param integer $dbh -- the database connection
283  * @return array --
284  * $serviceUpdateOptions -- the DDL at the top of this script: "N", "U", "Y" are the valid options
285  * $serverOptions -- the DDL at the top of this script: "N" and "Y" for on and off
286  * $hasMasterRecord -- this will always be true
287  * $record -- the list of properties with the options and messages.
288  * $defaultsDDL -- the data from the master table. It is used when a property is created.
289  */
290 function readServerSettings($dbh)
291 {
292  $parameters= array();
293  dms_import_v2($parameters, "BOTTOM_LEVEL", array("initializeDDLs" => "string"));
294 
295  $sqls= array();
296  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
297  array("initializeDDLs" => array("type" => "boolean", "required" => false)));
298  $initializeDDLs= $parameters["BOTTOM_LEVEL"]["initializeDDLs"] == "Y";
299 
300  $sql= "select td.trustedid, td.parms, tm.trustedfields, tm.trustedid as master_id from cutrusteddetail td left join cutrustedmaster tm on td.trustedid= tm.trustedid " .
301  "where upper(td.cu) = 'HOMECU' and td.trustedid= 'HOMECUSERVICE' and upper(tm.trustedvendor) = 'HOMECU' order by td.trustedid";
302  $queryResults= runSelectStatement($sql, $dbh, array("trustedid" => "trustedId", "parms" => "detailsEncoded", "trustedfields" => "masterDetailsEncoded", "master_id" => "masterId"));
303  $errors= $queryResults["error"];
304  $warnings= array();
305 
306  $almostExpandedRecords= extractDetails($queryResults["record"], "HOMECU");
307 
308  $returnArray= array("error" => $errors, "sql" => array($sql), "record" => array(), "defaultsDDL" => array(), "hasMasterRecord" => false, "operation" => "read");
309  $serverOptions= getServerOptions();
310 
311  if ($initializeDDLs)
312  {
313 
314  $serverOptionsDDL= array();
315  $serviceUpdateOptions= getServiceUpdateOptions();
316  foreach($serverOptions as $key => $value)
317  {
318  $serverOptionsDDL[]= array("value" => $key, "text" => $value);
319  }
320  $returnArray["serviceUpdateOptions"]= $serviceUpdateOptions;
321  $returnArray["serverOptions"]= $serverOptionsDDL;
322  }
323  foreach($queryResults["record"] as $record)
324  {
325  $details= array();
326  $messages= array();
327  $masterDetails= array();
328  $masterMessages= array();
329  $detailsDecoded= $almostExpandedRecords[0]["detailsDecoded"];
330  if (is_array($detailsDecoded))
331  {
332  foreach($detailsDecoded as $key => $value)
333  {
334  $pos= stripos($key, "_MSG");
335  $len= strlen($key);
336 
337  if ($pos !== false && $len - $pos == 4) // _MSG at end of the string
338  $messages[stripslashes(substr($key, 0, $pos))]= stripslashes($value);
339  else
340  $details[]= array("property" => $key, "value" => stripslashes($value));
341  }
342  }
343 
344  $returnArray["hasMasterRecord"]= $record["masterId"] != "";
345 
346  for($i=0, $count=count($details); $i != $count; $i++)
347  {
348  $key= $details[$i]["property"];
349  $value= $details[$i]["value"];
350  $details[$i]["message"]= strval($messages[$key]);
351  $details[$i]["text"]= isset($serverOptions[$value]) ? $serverOptions[$value] : $value;
352  }
353  $returnArray["record"]= $details;
354 
355  if ($initializeDDLs)
356  {
357  $masterDetailsDecoded= json_decode($record["masterDetailsEncoded"], true);
358  if (is_array($masterDetailsDecoded))
359  {
360  foreach($masterDetailsDecoded as $key => $value)
361  {
362  $pos= stripos($key, "_MSG");
363  if ($pos !== false && strlen($key) - $pos == 4) // _MSG at end of the string
364  $masterMessages[stripslashes(substr($key, 0, $pos))]= stripslashes($value["Default"]);
365  else
366  $masterDetails[]= array("property" => stripslashes($key), "defaultValue" => stripslashes($value["Default"]));
367  }
368  }
369  for($i=0, $count=count($masterDetails); $i != $count; $i++)
370  {
371  $key= $masterDetails[$i]["property"];
372  $masterDetails[$i]["defaultMessage"]= strval($masterMessages[$key]);
373  }
374 
375  $returnArray["defaultsDDL"]= $masterDetails;
376  }
377  }
378  return $returnArray;
379 }
380 
381 /**
382  * function createTrustedIdDetails($dbh)
383  * Creates a top-level record
384  *
385  * @param integer $dbh -- the database connection
386  * @return array --
387  * $record -- the top level record for the trusted id.
388  * $error -- one or zero errors encountered.
389  * $sqls -- the SQLs used
390  * $operation -- set to "add" for informational purposes
391  * $trustedId -- the trusted id to create
392  * $bottomRecords -- the property records to create.
393  */
394 function createTrustedIdDetails($dbh)
395 {
396  $parameters= array();
397  $sqls= array();
398  dms_import_v2($parameters, "BOTTOM_LEVEL", array("trustedId" => "string", "cu" => "string", "masterDetails" => "string"));
399  $trustedId= trim($parameters["BOTTOM_LEVEL"]["trustedId"]);
400  $cu= trim($parameters["BOTTOM_LEVEL"]["cu"]);
401  $masterDetails= trim($parameters["BOTTOM_LEVEL"]["masterDetails"]);
402  $record= array();
403  $newBottomRecords= array();
404  try
405  {
406  if ($trustedId == "")
407  throw new exception("TrustedId is required!", 1);
408  if (strlen($trustedId) > 20)
409  throw new exception("TrustedId is too long!", 2);
410  if ($cu == "")
411  throw new exception("Cu is required!", 3);
412  if (strlen($cu) > 12)
413  throw new exception("Cu is too long!", 4);
414  if ($masterDetails == "")
415  throw new exception("Master details are required!", 5);
416  $masterDetails= json_decode($masterDetails, true);
417  if (!is_array($masterDetails))
418  throw new exception("Master details are malformed!", 6);
419 
420  foreach($masterDetails as $key => $record)
421  {
422  $property= stripslashes($key);
423  $value= stripslashes(strval($record["defaultValue"]));
424  $message= stripslashes(strval($record["message"]));
425  $expandedDetails[$property]= $value;
426  $fieldType= isset($record["fieldType"]) ? $record["fieldType"] : "string";
427  $newBottomRecords[]= array("property" => $property, "fieldType" => $fieldType, "value" => $value, "message" => $message, "hasMasterRecord" => true, "trustedId" => $trustedId);
428  }
429  $encryptedDetails= encryptDetails($expandedDetails, $cu);
430 
431  $sql= "insert into cutrusteddetail (cu, trustedid, parms) values ('" . prep_save($cu, 12) . "', '" . prep_save($trustedId, 20) . "', '" . prep_save($encryptedDetails) . "')";
432  $sqls[]= $sql;
433  $sth= db_query($sql, $dbh);
434  if (!$sth)
435  throw new exception("Insert query failed!", 7);
436  $record= array("trustedId" => $trustedId, "hasMasterRecord" => true, "masterDetailLength" => count($newBottomRecords));
437  }
438  catch(exception $e)
439  {
440  return array("record" => array(), "error" => array($e->getMessage()), "sql" => $sqls, "operation" => "add", "trustedId" => $trustedId, "bottomRecords" => array());
441  }
442  return array("record" => $record, "error" => array(), "sql" => $sqls, "operation" => "add", "trustedId" => $trustedId, "bottomRecords" => $newBottomRecords);
443 }
444 
445 /**
446  * function removeTrustedIdDetails($dbh)
447  * Removes a top-level record
448  *
449  * @param integer $dbh -- the database connection
450  * @return array --
451  * $errors -- any errors encountered
452  * $sqls -- the SQLs used
453  * $operation -- set to "delete" for informational purposes
454  */
455 function removeTrustedIdDetails($dbh)
456 {
457  $parameters= array();
458  dms_import_v2($parameters, "BOTTOM_LEVEL", array("trustedId" => "string", "cu" => "string"));
459 
460  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
461  array("trustedId" => array("type" => "string", "required" => true, "maxlength" => 20),
462  "cu" => array("type" => "string", "required" => true, "maxlength" => 12)));
463  $trustedId= $parameters["BOTTOM_LEVEL"]["trustedId"];
464  $cu= $parameters["BOTTOM_LEVEL"]["cu"];
465 
466  $sql= "delete from cutrusteddetail where cu= '$cu' and trustedid= '$trustedId'";
467  $queryResults= runExecStatement($sql, $dbh);
468  $errors= array_merge($errors, $queryResults["error"]);
469 
470  return array("error" => $errors, "sql" => array($sql), "operation" => "delete");
471 }
472 
473 /**
474  * function changeTrustedDetailDetails($dbh, $mode)
475  * Creates, updates, or remove a bottom-level record
476  *
477  * @param integer $dbh -- the database connection
478  * @param string $mode -- the operation: "delete", "update", "create"
479  * @return array --
480  * $sql -- the SQLs used
481  * $error -- any errors encountered
482  * $operation -- the value of the $mode parameter
483  * $record -- the modified record
484  */
485 function changeTrustedDetailDetails($dbh, $mode)
486 {
487  $parameters= array();
488 
489  $valid= array("trustedId" => "string", "property" => "string", "cu" => "string");
490  if ($mode != "delete")
491  {
492  $valid["value"]= "string";
493  $valid["fieldType"]= "string";
494  $valid["message"]= "string";
495  }
496  dms_import_v2($parameters, "BOTTOM_LEVEL", $valid);
497 
498  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
499  array("trustedId" => array("type" => "string", "required" => true, "maxlength" => 20),
500  "cu" => array("type" => "string", "required" => true, "maxlength" => 12)));
501  $trustedId= $parameters["BOTTOM_LEVEL"]["trustedId"];
502  $property= trim($parameters["BOTTOM_LEVEL"]["property"]);
503  $value= trim($parameters["BOTTOM_LEVEL"]["value"]);
504  $cu= $parameters["BOTTOM_LEVEL"]["cu"];
505  $fieldType= trim($parameters["BOTTOM_LEVEL"]["fieldType"]);
506  $message= trim($parameters["BOTTOM_LEVEL"]["message"]);
507 
508  if ($property == "")
509  $errors[]= "Property is required";
510 
511  $retrievalSQL= "select parms from cutrusteddetail where trustedid= '$trustedId' and cu= '$cu'";
512  $queryResults= runSelectStatement($retrievalSQL, $dbh, array("parms" => "detailsEncoded"));
513  $errors= array_merge($errors, $queryResults["error"]);
514  $sqls= array($retrievalSQL);
515 
516  $detailArray= array();
517  $records= array();
518  if (count($errors) == 0)
519  {
520  $expandedRecords= extractDetails($queryResults["record"], $cu, $errors);
521  if ($expandedRecords == "" || count($expandedRecords) == 0)
522  $expandedDetails= array();
523  else
524  $expandedDetails= $expandedRecords[0]["detailsDecoded"];
525 
526  if ($mode == "delete")
527  unset($expandedDetails[$property]);
528  else
529  $expandedDetails[$property]= $value;
530 
531  $encrypedDetails= encryptDetails($expandedDetails, $cu);
532  $returnRecord= array(array("property" => $property, "value" => $value, "fieldType" => $fieldType, "message" => $message, "hasMasterRecord" => true, "trustedId" => $trustedId));
533 
534  $updateSQL= "update cutrusteddetail set parms= '$encrypedDetails' where trustedid= '$trustedId' and cu= '$cu'";
535  $queryResults= runExecStatement($updateSQL, $dbh);
536  $errors= array_merge($errors, $queryResults["error"]);
537  $sqls[]= $updateSQL;
538  }
539  return array("sql" => $sqls, "error" => $errors, "operation" => $mode, "record" => $returnRecord);
540 }
541 
542 /**
543  * function reorderProperties($dbh)
544  * Reorders the properties
545  *
546  * @param integer $dbh -- the database connection
547  * @return array --
548  * $sql -- the SQLs used
549  * $error -- any errors encountered
550  * $operation -- apparently null
551  * $record -- the properties in the revised order
552  */
553 function reorderProperties($dbh)
554 {
555  $parameters= array();
556 
557  dms_import_v2($parameters, "BOTTOM_LEVEL", array("trustedId" => "string", "property" => "string", "cu" => "string", "position" => "string"));
558 
559  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
560  array("trustedId" => array("type" => "string", "required" => true, "maxlength" => 20),
561  "cu" => array("type" => "string", "required" => true, "maxlength" => 12),
562  "position" => array("type" => "int", "required" => true)));
563  $trustedId= $parameters["BOTTOM_LEVEL"]["trustedId"];
564  $property= trim($parameters["BOTTOM_LEVEL"]["property"]);
565  $cu= $parameters["BOTTOM_LEVEL"]["cu"];
566  $position= $parameters["BOTTOM_LEVEL"]["position"];
567 
568  if ($property == "")
569  $errors[]= "Property is required";
570 
571  $retrievalSQL= "select parms from cutrusteddetail where trustedid= '$trustedId' and cu= '$cu'";
572  $queryResults= runSelectStatement($retrievalSQL, $dbh, array("parms" => "detailsEncoded"));
573  $errors= array_merge($errors, $queryResults["error"]);
574  $sqls= array($retrievalSQL);
575 
576  $detailArray= array();
577  $records= array();
578  if (count($errors) == 0)
579  {
580  $expandedRecords= extractDetails($queryResults["record"], $cu, $errors);
581  if ($expandedRecords == "" || count($expandedRecords) == 0)
582  $expandedDetails= array();
583  else
584  $expandedDetails= $expandedRecords[0]["detailsDecoded"];
585 
586  $newExpandedDetails= array();
587  $index= 0;
588  foreach($expandedDetails as $key => $value)
589  {
590  if ($index == $position)
591  $newExpandedDetails[$property]= $expandedDetails[$property];
592  if ($key != $property)
593  $newExpandedDetails[$key]= $value;
594  $index++;
595  }
596 
597  if ($index >= count($expandedDetails))
598  $newExpandedDetails[$property]= $expandedDetails[$property];
599 
600  $encrypedDetails= encryptDetails($newExpandedDetails, $cu);
601 
602  $masterSQL= "select tm.trustedid, tm.trustedfields from cutrustedmaster tm where tm.trustedid = '$trustedId' order by tm.trustedid, tm.trustedvendor";
603  $queryResults= runSelectStatement($masterSQL, $dbh, array("trustedid" => "trustedId", "trustedfields" => "masterFields"));
604  $errors= array_merge($errors, $queryResults["error"]);
605 
606  $trustedIdDetailDDL= array();
607  $masterDetailLength= array();
608  foreach($queryResults["record"] as $record)
609  {
610  $details= json_decode($record["masterFields"], true);
611  $detailDDL= array();
612  $trustedId= $record["trustedId"];
613 
614  if (is_array($details))
615  {
616  foreach($details as $key => $detailRow)
617  {
618  $detail= array("fieldType" => "string", "defaultValue" => "");
619  if (is_array($detailRow))
620  {
621  foreach($detailRow as $lowerKey => $value) // type and default
622  {
623  if ($lowerKey == "Type")
624  $detail["fieldType"]= $value;
625  else if ($lowerKey == "Default")
626  $detail["defaultValue"]= stripslashes($value);
627  else if ($lowerKey == "Message")
628  $detail["message"]= stripslashes($value);
629  }
630  }
631  $detailDDL[stripslashes($key)]= $detail;
632  }
633  $masterDetailLength[$trustedId]= count($details);
634  }
635  else if ($record["masterFields"] != "")
636  {
637  $trustedId= $record["trustedId"];
638  $warnings[]= "TrustedId master's details are not encoded correctly: $trustedId";
639  $details= array();
640  $masterDetailLength[$trustedId]= 0;
641  }
642 
643  $trustedIdDetailDDL[$trustedId]= $detailDDL;
644  }
645 
646  $warnings= array();
647  $expandedRecords= getDetailsForGrid(array(array("trustedId" => $trustedId, "detailsDecoded" => $newExpandedDetails)), $trustedIdDetailDDL, $masterDetailLength, $warnings);
648 
649  $returnRecord= $expandedRecords["detailRecords"];
650 
651  $updateSQL= "update cutrusteddetail set parms= '$encrypedDetails' where trustedid= '$trustedId' and cu= '$cu'";
652  $queryResults= runExecStatement($updateSQL, $dbh);
653  $errors= array_merge($errors, $queryResults["error"]);
654  $sqls[]= $updateSQL;
655  }
656  return array("sql" => $sqls, "error" => $errors, "operation" => $mode, "record" => $returnRecord);
657 }
658 
659 /**
660  * function changeServiceProperty($dbh, $mode)
661  * Creates, updates, or remove a bottom-level record for the HOMECUSERVICE thing.
662  *
663  * @param integer $dbh -- the database connection
664  * @param string $mode -- the mode: "delete", "update", and "create"
665  * @return array --
666  * $sql -- the SQLs used
667  * $error -- any errors encountered
668  * $operation -- the value of the $mode parameter
669  * $record -- the modified record
670  */
671 function changeServiceProperty($dbh, $mode)
672 {
673  $parameters= array();
674 
675  $valid= array("property" => "string");
676  if ($mode != "delete")
677  {
678  $valid["value"]= "string";
679  $valid["message"]= "string";
680  }
681  dms_import_v2($parameters, "BOTTOM_LEVEL", $valid);
682 
683  $property= trim($parameters["BOTTOM_LEVEL"]["property"]);
684  $value= trim($parameters["BOTTOM_LEVEL"]["value"]);
685  $message= trim($parameters["BOTTOM_LEVEL"]["message"]);
686 
687  $errors= array();
688  if ($property == "")
689  $errors[]= "Property is required";
690 
691  $serverOptions= getServerOptions();
692 
693  $cu= "HOMECU";
694  $trustedId= "HOMECUSERVICE";
695 
696  $retrievalSQL= "select trustedid, parms from cutrusteddetail where trustedid= '$trustedId' and cu= '$cu'";
697  $queryResults= runSelectStatement($retrievalSQL, $dbh, array("trustedid" => "validId", "parms" => "detailsEncoded"));
698  $errors= array_merge($errors, $queryResults["error"]);
699  $sqls= array($retrievalSQL);
700 
701  $detailArray= array();
702  $records= array();
703  if (count($errors) == 0)
704  {
705  $expandedRecords= extractDetails($queryResults["record"], $cu, $errors);
706  if ($expandedRecords == "" || count($expandedRecords) == 0)
707  $expandedDetails= array();
708  else
709  $expandedDetails= $expandedRecords[0]["detailsDecoded"];
710 
711  if ($mode == "delete")
712  {
713  unset($expandedDetails[$property]);
714  unset($expandedDetails["${property}_MSG"]);
715  }
716  else
717  {
718  $expandedDetails[$property]= $value;
719 
720  if ($message != "")
721  $expandedDetails[$property . "_MSG"]= $message;
722  else
723  unset($expandedDetails[$property . "_MSG"]);
724  }
725 
726  $encrypedDetails= encryptDetails($expandedDetails, $cu);
727 
728  $returnRecord= array(array("property" => $property, "value" => $value, "message" => $message, "text" => $serverOptions[$value], "hasMasterRecord" => true, "trustedId" => $trustedId));
729 
730  $updateSQL= "update cutrusteddetail set parms= '$encrypedDetails' where trustedid= '$trustedId' and cu= '$cu'";
731  $queryResults= runExecStatement($updateSQL, $dbh);
732  $errors= array_merge($errors, $queryResults["error"]);
733  $sqls[]= $updateSQL;
734  }
735  return array("sql" => $sqls, "error" => $errors, "operation" => $mode, "record" => $returnRecord);
736 }
737 
738 /**
739  * function serviceUpdate($dbh)
740  * This should now be an array of key/value sets.
741  *
742  * key is the homecuService ie IPAY,ENSENTA
743  *
744  * value {Y, N, U}
745  *
746  * N - Service is ONLINE
747  * Y - Service is OFFLINE - and stays down, if the script is set to enable services, this service will NOT change
748  * U - Service is OFFLINE - and if the script is set to enable services, the service value is set to N
749  *
750  * NOW -- LOOP through all the key/value sets
751  * when action is set to Off -a off, then turn all occurrences of N to U
752  * when action is set to On -a on, then turn all occurrencens of U to N
753  * if -s ALL is set, then all services will be affected
754  * if -s is an array of services, only those listed services should be affected
755  *
756  * @param integer $dbh -- the database connection
757  * @return array --
758  * $sql -- the SQLs used
759  * $error -- any errors encountered
760  * $operation -- set to "bulkUpdate" for informational purposes
761  */
762 function serviceUpdate($dbh)
763 {
764  $parameters= array();
765  dms_import_v2($parameters, "BOTTOM_LEVEL", array("isTurnOff" => "string"));
766  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
767  array("isTurnOff" => array("type" => "boolean", "required" => true)));
768 
769  $isTurnOff= $parameters["BOTTOM_LEVEL"]["isTurnOff"] == "Y";
770 
771  $cu= "HOMECU";
772  $trustedId= "HOMECUSERVICE";
773 
774  $retrievalSQL= "select trustedid, parms from cutrusteddetail where trustedid= '$trustedId' and cu= '$cu'";
775  $queryResults= runSelectStatement($retrievalSQL, $dbh, array("trustedid" => "validId", "parms" => "detailsEncoded"));
776  $errors= array_merge($errors, $queryResults["error"]);
777  $sqls= array($retrievalSQL);
778 
779  $detailArray= array();
780  $records= array();
781  if (count($errors) == 0)
782  {
783  $expandedRecords= extractDetails($queryResults["record"], $cu, $errors);
784  if ($expandedRecords == "" || count($expandedRecords) == 0)
785  $expandedDetails= array();
786  else
787  $expandedDetails= $expandedRecords[0]["detailsDecoded"];
788 
789  foreach($expandedDetails as $key => $value)
790  {
791  $pos= stripos($key, "_MSG");
792  $len= strlen($key);
793 
794  if ($pos !== false && $len - $pos == 4) // _MSG at end of the string
795  continue; // Do not worry about the messages. It is possible that they could have U or N values and then they would be falsely updated.
796  if ($isTurnOff)
797  {
798  if ($value == "U")
799  $expandedDetails[$key]= "N";
800  }
801  else
802  {
803  if ($value == "N")
804  $expandedDetails[$key]= "U";
805  }
806  }
807 
808  $encrypedDetails= encryptDetails($expandedDetails, $cu);
809 
810  $updateSQL= "update cutrusteddetail set parms= '$encrypedDetails' where trustedid= '$trustedId' and cu= '$cu'";
811  $queryResults= runExecStatement($updateSQL, $dbh);
812  $errors= array_merge($errors, $queryResults["error"]);
813  $sqls[]= $updateSQL;
814  }
815  return array("sql" => $sqls, "error" => $errors, "operation" => "bulkUpdate");
816 }
817 
818 /**
819  * function pullDownOptions($dbh)
820  * Pulls in properties from the master list that aren't currently in the list
821  *
822  * @param integer $dbh -- the database connection
823  * @return array --
824  * $sql -- the SQLs used
825  * $error -- any errors encountered
826  * $record -- any additional properties added
827  */
828 function pullDownOptions($dbh)
829 {
830  $parameters= array();
831  dms_import_v2($parameters, "BOTTOM_LEVEL", array("cu" => "string", "trustedId" => "string", "options" => "string"));
832 
833  $cu= $parameters["BOTTOM_LEVEL"]["cu"];
834  $trustedId= $parameters["BOTTOM_LEVEL"]["trustedId"];
835 
836  $errors= cleanValuesForDatabase($parameters["BOTTOM_LEVEL"],
837  array("cu" => array("type" => "string", "required" => true, "maxlength" => 12),
838  "trustedId" => array("type" => "string", "required" => true, "maxlength" => 20)));
839 
840  $cuClean= $parameters["BOTTOM_LEVEL"]["cu"];
841  $trustedIdClean= $parameters["BOTTOM_LEVEL"]["trustedId"];
842 
843  $optionsEncoded= $parameters["BOTTOM_LEVEL"]["options"];
844 
845  $retrievalSQL= "select parms from cutrusteddetail where trustedid= '$trustedIdClean' and cu= '$cuClean'";
846  $queryResults= runSelectStatement($retrievalSQL, $dbh, array("parms" => "detailsEncoded"));
847  $errors= array_merge($errors, $queryResults["error"]);
848  $sqls= array($retrievalSQL);
849 
850  $returnRecords= array();
851  $expandedRecords= array();
852  $expandedDetails= array();
853  if (count($errors) == 0)
854  {
855  $expandedRecords= extractDetails($queryResults["record"], $cu, $errors);
856  $expandedDetails= $expandedRecords[0]["detailsDecoded"];
857  }
858 
859  if (trim($optionsEncoded) == "")
860  $errors[]= "Options are required";
861  else
862  {
863  $options= json_decode($optionsEncoded, true);
864  if (is_array($options))
865  {
866  foreach($options as $record)
867  {
868  if (isset($record["property"]))
869  {
870  $property= stripslashes($record["property"]);
871  $value= stripslashes(strval($record["value"]));
872  $message= stripslashes(strval($record["message"]));
873  $expandedDetails[$property]= $value;
874  $fieldType= isset($record["fieldType"]) ? $record["fieldType"] : "string";
875  $returnRecords[]= array("property" => $property, "fieldType" => $fieldType, "value" => $value, "message" => $message, "hasMasterRecord" => true, "trustedId" => $trustedId);
876  }
877  else
878  {
879  $errors[]= "One or more options rows is not formed correctly";
880  break;
881  }
882  }
883  }
884  else
885  $errors[]= "Options are not encoded correctly";
886  }
887 
888  $encryptedDetails= encryptDetails($expandedDetails, $cu);
889 
890  $updateSQL= "update cutrusteddetail set parms= '$encryptedDetails' where trustedid= '$trustedIdClean' and cu= '$cuClean'";
891  $sqls[]= $updateSQL;
892  if (count($errors) == 0)
893  {
894  $queryResults= runExecStatement($updateSQL, $dbh);
895  $errors= array_merge($errors, $queryResults["error"]);
896  }
897 
898  return array("sql" => $sqls, "error" => $errors, "record" => $returnRecords);
899 }