Odyssey
hcuAlerts.data
1 <?php
2 /*
3  * File: hcuAlerts.data
4  * Purpose: Handle the CRUD portion of the Alerts. When returning the requested alert data
5  * do it in a JSON format, for the client to display accordingly.
6  *
7  *
8  * Call this script with the following parameters
9  * action - what the client side is requesting.
10  *
11  * Alert Types: 1 = Balance, 2 = Transactions, 3 = Check Number, 4 = Loan / Missed Payment Date
12  *
13  * Returns JSON OBJECT.
14  */
15 try {
16  // ** SET HOMECU FLAGS
17  $serviceShowInfo = true;
18  $serviceLoadMenu = true;
19  $serviceShowMenu = true;
20 
21  // ** INCLUDE MAIN GLOBAL SCRIPT -- Handles security / global variable values
22  // hcuService will be returning a status object: e.g. ["homecuErrors":{[{"message":"message1"}...{"message":"messageN"}}]
23  require_once(dirname(__FILE__) . '/../library/hcuService.i');
24 
25  $logger= $HB_ENV["SYSENV"]["logger"];
26 
27  // ** IMPORT FORM VALUES
28  $string= array("filter" => FILTER_SANITIZE_STRING);
29  $string_special = array("filter" => FILTER_SANITIZE_SPECIAL_CHARS);
30  $digits= array("filter" => FILTER_SANITIZE_NUMBER_INT);
31  $dms_ok=array( 'action'=>$string, "mbr_account" => $string,
32  'type'=>$string,'id'=>$string, 'chome'=>$string,
33  'selacct'=>$string, 'emailtype'=>$string,
34  'notifyto'=>$string,
35  'notifymsg'=>$string_special, 'incbal'=>$string, 'notifyamt'=>$string,
36  'useavailbal'=>$string, 'desc_amtmin'=>$string, 'desc_amtmax'=>$string,
37  'userange'=>$string, 'transtype'=>$string,
38  'notifydesc'=>$string_special, 'days_prior'=>$string,
39  'chknum'=>$string, 'incamt'=>$string, 'inctransdesc'=>$digits);
40 
41  HCU_ImportVars($HB_ENV, 'HCUPOST', $dms_ok);
42 
43  header('Content-Type: application/json');
44 
45  if (!$dbh) {
46  // The connection was not made to the database
47  // unresolved: return an error??
48  }
49 
50  $altopts = "";
51  $myaccount = "";
52 
53  // initialize the error and result objects
54  $aryResult = array();
55  $aryErrors = array();
56  $aryReply = array();
57  $aryInfo = array();
58  // ** First check the refer script -- make sure coming from right place
59  $parseRefer = parse_url($_SERVER['HTTP_REFERER']);
60  $referScript = basename($parseRefer['path']);
61  if (!in_array($referScript, array('hcuAlerts.prg'))) {
62  // ** Wrong script calling this data routine
63  $aryErrors[] = $MC->msg('Feature Unavailable', HCU_DISPLAY_AS_RAW);
64  throw new Exception (HCU_JsonEncode($aryErrors));
65  }
66 
67  /*
68  * ** CHECK USER FEATURE PERMISSIONS **
69  * NOTE: DO NOT AUTO-REDIR. Handle perm error here
70  */
71  if (!PermCheckFeatureScreen($dbh, $HB_ENV, $MC, FEATURE_ALERTS, '', false)) {
72  throw new Exception (HCU_JsonEncode(Array($MC->msg('Rights not set', HCU_DISPLAY_AS_HTML))));
73  }
74 
75  $alertsEnabled = Check_AlertsEnabled( $dbh, $HB_ENV );
76  if ( $alertsEnabled === false ) {
77  $aryErrors[] = $MC->msg('Feature Not Set', HCU_DISPLAY_AS_RAW);
78  throw new Exception (HCU_JsonEncode($aryErrors));
79  }
80 
81  // do the requested operation
82  switch ( $HB_ENV["HCUPOST"]["action"] ) {
83  case "create":
84  case "update": // update has an id
85  // validate the inputs
86 
87  // convert any UTF-8 characters to encoded html entities
88  $HB_ENV["HCUPOST"]["notifymsg"] = ConvertFromUTF8( $HB_ENV["HCUPOST"]["notifymsg"] );
89  // notifydesc only exists for transaction type alerts
90  $HB_ENV["HCUPOST"]["notifydesc"] = HCU_array_key_exists("notifydesc", $HB_ENV["HCUPOST"]) ?
91  ConvertFromUTF8( $HB_ENV["HCUPOST"]["notifydesc"] ) : "";
92 
93  // must decode because single quotes cannot be sanitized with special chars like `<, > and &`
94  // when using FILTER_SANITIZE_SPECIAL_CHAR we decode them back to the actual characters.
95  $HB_ENV["HCUPOST"]["notifymsg"] = html_entity_decode( $HB_ENV["HCUPOST"]["notifymsg"], ENT_QUOTES, "UTF-8" );
96  // notifydesc only exists for transaction type alerts
97  $HB_ENV["HCUPOST"]["notifydesc"] = HCU_array_key_exists("notifydesc", $HB_ENV["HCUPOST"]) ?
98  html_entity_decode( $HB_ENV["HCUPOST"]["notifydesc"], ENT_QUOTES, "UTF-8" ) : "";
99 
100  // verify the parameters
101  $aryUpdate = Validate_Alert( $dbh, $HB_ENV, $MC );
102 
103  if ($aryUpdate['code'] == '000') {
104  $aryUpdate = Update_Alert( $dbh, $HB_ENV, $MC );
105  }
106 
107  if ($aryUpdate['code'] != '000') {
108  // an error occurred
109  $aryErrors = array();
110  for ( $e = 0; $e < count( $aryUpdate["errors"] ); $e++ ) {
111  $aryErrors[] = $aryUpdate["errors"][$e];
112  }
113 
114  throw new Exception (HCU_JsonEncode($aryErrors));
115  } else {
116  // return status
117  if ( $HB_ENV["HCUPOST"]["id"] > 0 ) {
118  $aryInfo[] = $MC->msg('Alert Updated', HCU_DISPLAY_AS_RAW);
119  } else {
120  $aryInfo[] = $MC->msg('Alert Saved', HCU_DISPLAY_AS_RAW);
121  }
122  }
123 
124  break;
125  case "read":
126  $return = Get_AlertsDetailed( $dbh, $HB_ENV["Cu"], $HB_ENV["Uid"], $HB_ENV["Fset3"]);
127 
128  $aryResult = $return["data"];
129  for ( $i = 0; $i < count( $aryResult ); $i++ ) {
130  // decode the row display message and the edit/details display message
131  $aryResult[$i]["notifymsg"] = html_entity_decode( $aryResult[$i]["notifymsg"], ENT_QUOTES, "UTF-8" );
132  $aryResult[$i]["notifydisplaymsg"] = html_entity_decode( $aryResult[$i]["notifymsg"], ENT_QUOTES, "UTF-8" );
133  if ( isset( $aryResult[$i]["lastalert"] ) && strlen( $aryResult[$i]["lastalert"] ) > 0 ) {
134  $aryResult[$i]["lastalert"] = date( "M j, Y g:ia", strtotime( $aryResult[$i]["lastalert"] ) );
135  }
136  }
137  break;
138  case "read_alert_accounts":
139  $return = Get_AlertAccountList( $dbh, $HB_ENV );
140 
141  $aryResult = $return["data"];
142  break;
143  case "destroy":
144  $aryDelete = Delete_Alert( $dbh, $HB_ENV, $MC );
145 
146  if ($aryDelete['code'] != '000') {
147  // an error occurred
148  $aryErrors = array();
149  for ( $e = 0; $e < count( $aryDelete["errors"] ); $e++ ) {
150  $aryErrors[] = $aryDelete["errors"][$e];
151  }
152 
153  throw new Exception (HCU_JsonEncode($aryErrors));
154  } else {
155  $aryInfo[] = $MC->msg('Alert Deleted', HCU_DISPLAY_AS_RAW);
156  }
157 
158  break;
159  case "readalert":
160  // return as a result
161  $oneAlert = Read_OneAlert( $dbh, $HB_ENV );
162  $aryResult[] = $oneAlert["data"];
163 
164  $aryResult[0]["notifymsg"] = html_entity_decode( $aryResult[0]["notifymsg"], ENT_COMPAT | ENT_HTML401, "UTF-8" );
165 
166  break;
167  default:
168  $aryErrors[] = "Unexpected action: {$HB_ENV["HCUPOST"]["action"]}";
169  throw new Exception (HCU_JsonEncode($aryErrors));
170  }
171 }
172 catch(Exception $ex)
173 {
174  //Return error message
175  $aryReply["homecuErrors"] = HCU_JsonDecode( $ex->getMessage() );
176 
177  // if returning error, not replying with data
178  $aryResult = array();
179 
180  // if returning error, not returning status
181  $aryInfo = array();
182 }
183 
184  if ( count( $aryInfo ) ) {
185  $aryReply["homecuInfo"] = $aryInfo;
186  }
187 
188  if ( count( $aryResult ) ) {
189  $aryReply["homecuData"] = $aryResult;
190  }
191 
192  print HCU_JsonEncode($aryReply);