Odyssey
hcuService.i
1 <?php
2 /*
3  * File: hcuService
4  *
5  * Purpose: This script will be used to facilitate any needed security, setup
6  * variable contents
7  *
8  * Use: This script should be included at the top of every script using require_once
9  *
10  *
11  * NOTE: Some variables will need to be set prior to including this script to properly
12  * turn some options ON or OFF, or bypass logic
13  *
14  *
15  * ** SCRIPT FLAGS
16  *
17  * serviceSkipCredentials - {true, false (Default)}
18  * When True this will allow the code to skip credential check
19  *
20  * serviceSkipSecurity - {true, false (Default)}
21  * When true this will allow the code to skip 'security' check
22  *
23  * serviceAllowReadonly - {true, false (Default)}
24  * Does this script allow access when the Home Banking Status is set to READONLY
25  *
26  * serviceShowInfo - {true, false (Default)}
27  * This will control if we show the upper right Info Container for the member
28  *
29  * serviceLoadMenu - {true, false (default)}
30  * This controls whether we go and load the menu object from the credit
31  * union directory
32  *
33  * serviceShowMenu - {true, false (Default)}
34  * Do we show the Top and Side Menu.. This will be programmatic and be loaded
35  * from a json object in the file incMenu
36  *
37  * serviceMenuScript - {string}
38  * This is the current "menu" item that should be selected, this should be the
39  * name of the item on the left 'aside' menu. once this option is found, it
40  * also identifies the group that is selected as well
41  * * NOTE: IF THIS IS NOT SET, the _SERVER['current_script'] name is used
42  * Note 04-2019 "aside" element changed to nav because it's not an aside
43  *
44  * serviceLiveCheck - {true, false (default)}
45  * For LIVE systems, this will get the results from the fetch_packet function
46  * to determine the current status of the "Live" interface
47  * If there is a problem, hcuPreContent should load a screen with the information
48  *
49  * serviceLoadCuInfo - {true (default), false}
50  * This will determine certain CU Info settings are loaded.. This is needed for scripts
51  * like schedliveproc.php.
52  *
53  * serviceSuppressBanner - {false (default), true}
54  * This will suppress the banner from the top of the screen. The banner should not be
55  * present when the user is NOT logged in.
56  */
57 
58  /*
59  * * SET DEFAULT VALUES FOR SCRIPT FLAGS
60  */
61  $serviceMinimal = (!isset($serviceMinimal) ? false : $serviceMinimal);
62  $serviceSkipCredentials = (!isset($serviceSkipCredentials) ? false : $serviceSkipCredentials);
63  $serviceSkipSecurity = (!isset($serviceSkipSecurity) ? false : $serviceSkipSecurity);
64  $serviceAllowReadonly = (!isset($serviceAllowReadonly) ? false : $serviceAllowReadonly);
65  $serviceShowInfo = (!isset($serviceShowInfo) ? false : $serviceShowInfo);
66  $serviceLoadMenu = (!isset($serviceLoadMenu) ? false : $serviceLoadMenu);
67  $serviceShowMenu = (!isset($serviceShowMenu) ? false : $serviceShowMenu);
68  // * DO NOT default serviceMenuScript to anything
69  $serviceMenuScript = (!isset($serviceMenuScript) ? "" : $serviceMenuScript);
70  //$serviceMenuScript = (!isset($serviceMenuScript) ? "hcuAccounts" : $serviceMenuScript);
71  $serviceLiveCheck = (!isset($serviceLiveCheck) ? false : $serviceLiveCheck);
72  $serviceLoadCuInfo = (!isset($serviceLoadCuInfo) ? true : $serviceLoadCuInfo);
73  $serviceSuppressBanner = (!isset($serviceSuppressBanner) ? false : $serviceSuppressBanner);
74 
75  $validLanguageCodes = Array("en_US", "es_US", "pl_US");
76  $serviceViewFromCuAdmin = false;
77 
78  /**
79  * DECLARE HB_ENV as an array
80  */
81  $HB_ENV = Array();
82 
83  try {
84 
85  /*
86  * ** INCLUDE SCRIPTS **
87  */
88  require_once( dirname(__FILE__) . "/../library/hcuLoadLibs.i" );
89 
90  // set up the environment based on environment variables
91  $HB_ENV['SYSENV'] = LoadSystemEnv("banking");
92 
93  $errHndlr = new CatchErrorHandler(HCU_PRODUCT_BANKING, $HB_ENV['SYSENV']['logger'], $HB_ENV['SYSENV']['devmode']);
94  $excHndlr = new CatchExceptionHandler(HCU_PRODUCT_BANKING, $HB_ENV['SYSENV']['logger'], $HB_ENV['SYSENV']['devmode']);
95 
96  /**
97  * Setup the error handling class
98  *
99  */
100  set_error_handler(array($errHndlr, "catchError"));
101  set_exception_handler(array($excHndlr, "catchException"));
102 
103  $dbPluginFile = "db." . $HB_ENV['SYSENV']['db']['platform'] . ".i";
104  // ** Include the database plugin file. This file will include the database function declarations
105  include (dirname(__FILE__) . '/../../shared/library/' . $dbPluginFile);
106 
107  /**
108  * temporary variable to use in the hcuPreContent and hcuPostContent to create a responsive base to use for coding
109  */
110  $HB_ENV['USE_BS_FRAMEWORK'] = true;
111 
112  // get and save the DB handle (call after SYSENV is set up)
113  $dbh = GetDBH( $HB_ENV['SYSENV']['db'] );
114  $HB_ENV["dbh"] = $dbh;
115 
116  /*
117  * ** GLOBAL VALUES **
118  */
119 
120  // ** LOGIN PATH MAY NEED TO BE SET SOMEWHERE ELSE -- PER homecu request script
121  /**
122  * @var string currentscript - the current script running, should be able to use this in place of hard coding script name
123  */
124  $HB_ENV['currentscript'] = basename($_SERVER['SCRIPT_NAME']);
125 
126  // Retain the full URL for changing language (and perhaps other uses.)
127  $HB_ENV["currentQuery"] = $_SERVER["QUERY_STRING"];
128 
129  $HB_ENV['remoteIp'] = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_STRING);
130 
131  /*
132  * *************************
133  * START OF VALUES THAT ARE BEING MOVED TO HB_ENV['SYSENV']
134  *
135  $HB_ENV['dbplat'] = $dbplat;
136  $HB_ENV['dbhost'] = $dbhost;
137  $HB_ENV['dbuser'] = $dbuser;
138  $HB_ENV['dbpasswd'] = $dbpasswd;
139  $HB_ENV['dbname'] = $dbname;
140  $HB_ENV['TicketDomain'] = $TicketDomain;
141  $HB_ENV['TicketExpires'] = $TicketExpires;
142  $HB_ENV['TicketPersists'] = $TicketPersists;
143  $HB_ENV['TicketInactive'] = $TicketInactive;
144  *
145  * END
146  * *************************
147  */
148  // ** SET ENVIRONMENT VARIABLES
149  SetEnvStatic( $HB_ENV );
150 
151  if ($serviceLoadCuInfo) {
152  $HB_ENV['cu'] = substr(filter_input(INPUT_GET, 'cu', FILTER_SANITIZE_STRING, array('flags' => (FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH))), 0, 12);
153  if ($HB_ENV['cu'] == '' && in_array(basename($_SERVER['SCRIPT_NAME']), array('OFXRequest.prg', 'hcuAppFeed.prg', 'hcuAppDirector.prg'))) {
154  $HB_ENV['cu'] = substr(filter_input(INPUT_POST, 'ORG', FILTER_SANITIZE_STRING, array('flags' => (FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH))), 0, 12);
155  $HB_ENV['no_cookies'] = true;
156  }
157  if ($HB_ENV['cu'] == '' && in_array(basename($_SERVER['SCRIPT_NAME']), array('MoneyDesk3.prg'))) {
158  $ReqPathArr = explode("/", $_SERVER['PATH_INFO']);
159  $HB_ENV['cu'] = HCU_array_key_value(1, $ReqPathArr);
160  $HB_ENV['cu'] = substr(preg_replace("/[^A-Za-z0-9]/","",$HB_ENV['cu']), 0, 12);
161  $HB_ENV['no_cookies'] = true;
162  }
163 
164  // ** Check HomeBanking Status Online / Offline / database active
165  // * This should set the HB_ENV values based on the credit union
166  // this sets up more HB_ENV and also checks if $dbh is valid
167  $isValidCU = LoadCUAdmin( $dbh, $HB_ENV['cu'], $HB_ENV );
168  if (!$isValidCU && strlen($HB_ENV['cu']) > 0) {
169  throw new ErrorException('Credit Union Not Found');
170  }
171  if (HCU_array_key_value("live", $HB_ENV)) {
172 
173  // ** Appliance API Script
174  require_once(dirname(__FILE__) . '/../../shared/library/sAPIAppl.i');
175 
176  // ** Load the specific plugin for this appliance configuration
177  IncludeApplPlugin($HB_ENV);
178 
179  }
180 
181  // set the language
182  SetLanguageEnv( $HB_ENV );
183  } else {
184  // ** Default the language to English in this instance
185  $HB_ENV["Flang"] = "en_US";
186  }
187  // ** set to DSK -- hcuAppFeed overrides this value for APP or ADA
188  require_once dirname(__FILE__) . '/../../shared/includes/third-party/Mobile_Detect.php';
189  $mobileDetect = new Mobile_Detect();
190  if ($mobileDetect->isMobile() || $mobileDetect->isTablet()) {
191  $HB_ENV['platform'] = 'MBL';
192  } else {
193  $HB_ENV['platform'] = 'DSK';
194  }
195 
196 
197  // ** Validate the Language here
198  // ** We will be able to make it a session cookie or longer like
199  if (!in_array($HB_ENV['Flang'], $validLanguageCodes) || trim($HB_ENV['Flang']) == '') {
200  $HB_ENV['Flang'] = "en_US";
201  }
202 
203  // ** SET the current language for the site
204  $Flang = $HB_ENV['Flang'];
205  $MC = new hcu_talk_base( $Flang );
206 
207  // ** VERIFY DATABASE CONNECTION
208  if ($HB_ENV['homebanking_status'] != "O") {
209  if (!$dbh) {
210  // * NO Database connection...
211  throw new ErrorException($MC->msg('Could not Open Database', HCU_DISPLAY_AS_HTML), 900);
212 
213  }
214  } else {
215  // ** THROW ERROR - Home Banking is OFFLINE
216  throw new ErrorException($MC->msg('Could not Open Database', HCU_DISPLAY_AS_HTML), 900);
217  }
218 
219  // ** offer and make available dictionary and db handle
220  $HB_ENV['MC'] = $MC;
221  $HB_ENV['dbh'] = $dbh;
222 
223  // ** DETERMINE LIVE/BATCH STATUS -- ** 8/15/17 -- MOVED TO LoadCUAdmin
224 
225  // ** CHECK OFF LINE STATUS
226  $HB_ENV['allowReadonly'] = $serviceAllowReadonly;
227 
228  if ($serviceLoadCuInfo) {
229  $serverParts = explode(".",$_SERVER['SERVER_NAME']);
230  $HB_ENV['cuhost'] = array_shift($serverParts);
231 
232  // ** SET CURRENT SCRIPT - LOGIN SCRIPT
233  setLoginScript($HB_ENV);
234  }
235 
236  if (!$serviceMinimal) {
237  // * SET output to be UTF-8, this should work for all languages
238  header('Content-Type: text/html; charset=UTF-8');
239 
240  // ** Check Member Security Credentials --
241  $mbrStatus = Array();
242  if (!$serviceSkipCredentials) {
243 
244  $mbrStatus = Check_Member_Credentials($HB_ENV, HCU_array_key_value("Ticket", $_COOKIE));
245  // ** two dimensional array
246 
247  $serviceViewFromCuAdmin = HCU_array_key_value('Ca', $HB_ENV) != "";
248 
249  if (!$mbrStatus['result']) {
250  $loginscript = $HB_ENV['loginscript'];
251 
252  $login_query_string = $_SERVER['QUERY_STRING'];
253 
254  // ** ALWAYS INCLUDE EMPTY HASH ON END _- THE ANCHOR TAG MUST GET CLEARED
255  // ** OR jquery mobile will puke trying to find a non existent hash
256  $now = time();
257 
258  // setcookie("Ticket", "", $now, "/", $TicketDomain, 1);
259  HCU_setcookie_env($HB_ENV['SYSENV'], "Ticket", "", $now);
260 
261  header("Location: {$loginscript}?" . $login_query_string . "#");
262  exit;
263  } else {
264 
265  // ** I want to refresh the cookie timeout
266  $now = time();
267  $newCtime = $now;
268  $newCe = $now + $HB_ENV['SYSENV']['ticket']['expires'];
269 
270  // OLD SetTicket($_COOKIE['Ticket'], "Ctime={$newCtime}&Ce={$newCe}");
271  SetTicket($HB_ENV, $_COOKIE['Ticket'], "Ctime={$newCtime}&Ce={$newCe}");
272 
273  // ** CHECK the 'cu' param in URL is set, if NOT then I want to force it by going there.
274  // * At this point I know I am working with a valid cookie
275  if ($HB_ENV['cu'] == '') {
276  $redirScript = $HB_ENV['homebankingpath'] . "/{$HB_ENV['currentscript']}";
277  $redirQuery = "cu=" . $HB_ENV['Cu'];
278  header("Location: {$redirScript}?{$redirQuery}#");
279  exit;
280  }
281 
282  if (!$serviceViewFromCuAdmin) {
283  // * add the FeatureAccessList to HB_ENV
284  // * This is the compiled list of Profile features with the permissions for this specific user
285  $paramInputs = Array('Uid' => $HB_ENV['Uid']);
286  $featureAccess = Perm_FeatureAccessList($dbh, $HB_ENV, $paramInputs);
287  if ($featureAccess['status']['code'] == '000') {
288  $HB_ENV['FeatureAccessList'] = HCU_array_key_value($HB_ENV['Uid'], $featureAccess['data']);
289  } else {
290  // ** NO FEATURE ACCESS RETURNED -- SET TO EMPTY LIST
291  $HB_ENV['FeatureAccessList'] = Array();
292  }
293  }
294  }
295  } else {
296  // Check_Member_Credentials normally sets Cu from cookie --
297  // ** However, it is NOT being called. set here
298  // ** cu has sanity check in Check_HomeCU_Status
299  $HB_ENV['Cu'] = $HB_ENV['cu'];
300  $serviceSuppressBanner = true;
301  }
302 
303  // get the envinfo
304  LoadClientEnvInfo( $HB_ENV, HCU_array_key_value("envinfo", $_COOKIE) );
305 
306  // *** LOAD COMMON FRAGMENTS ??
307  // *** Will need Menus/Footers/
308 
309  $cuSettings = Array();
310  if (HCU_array_key_value("chome", $HB_ENV) != '') {
311  // * Always load settings file
312  $settingsDir = ($HB_ENV['testmenu'] == '1' ? 'preview' : 'production');
313  $HB_ENV['settingsDir'] = $settingsDir;
314  $loadSettingsFile = "/home/{$HB_ENV['chome']}/public_html/bankingIncludes/{$settingsDir}/hcuSettings.i";
315 
316  if (is_readable($loadSettingsFile)) {
317  // ** Menu Found - Load into an array
318  $fileContent = file_get_contents($loadSettingsFile);
319  // ** Now decode it and hope for the best
320  $cuSettings = HCU_JsonDecode($fileContent);
321  }
322  } else {
323  //** SOMETHING WENT WRONG..
324  throw new ErrorException('Credit Union Not Found');
325 
326  }
327 
328  // ** LOAD the CU settings file prior to checking the offline status.
329  // ** This is so the custom settings can be loaded for the screen
330  if (!hcu_checkOffline($dbh, $HB_ENV)) {
331  // ** We are OFFLINE - CALL THE OFFLINE FUNCTION -- DISPLAYS OFFLINE MESSAGE
332  //
333 
334  // ** THROW ERROR ??
335  throw new ErrorException($HB_ENV['offlinemsg'], 902);
336 
337  }
338 
339  // ** WILL NEED TO HANDLE SECURITY SETTINGS
340  // ** Force Password Change
341  // ** Force Security Change
342  // ** Force EMAIL Verify
343 
344 
345 
346  if (!$serviceSkipSecurity && !$serviceViewFromCuAdmin) {
347 
348  // ** Now that we are here, check the current settings for security,
349  // * we may need to view an alternate screen for updating security profile
350  // only check if the member is the one authenticating
351 
352  $l_MbrSettings = Check_Member_Settings($dbh, $HB_ENV, $MC);
353  $serviceViewFromCUAdmin= $HB_ENV["Ca"] != "";
354 
355  // if settings need to be addressed AND (not an Odyssey app OR is a Pegasus app)
356  $platform = $HB_ENV["platform"];
357  if ( ($l_MbrSettings['code'] != '000') &&
358  (($platform != "ADA" && $platform != "APP") ||
359  ($platform == "ADA" || $platform =="APP") && $HB_ENV["envinfo"] == "Pegasus") ) {
360  /*
361  * Values for sC
362  *
363  * sC = 1 : the user pressed UPDATE NOW
364  * sC = 2 : the user pressed UPDATE LATER
365  */
366 
367  if (intval(HCU_array_key_value('sC', $HB_ENV)) == 0) {
368  // set a cookie for the profileRequire update
369  $retryCount = $HB_ENV["failedremain"];
370  $timeExpires = time() + $HB_ENV['SYSENV']['ticket']['expires'];
371  $hashedValue = md5( $retryCount . $timeExpires . $HB_ENV["secret"] );
372  $profileCookie = "remain=$retryCount&until=$timeExpires&check=$hashedValue";
373 
374  HCU_setcookie_env($HB_ENV['SYSENV'], "securePass", $profileCookie, 0);
375  // setcookie( "securePass", $profileCookie, 0, "/", $HB_ENV['TicketDomain'], 1 );
376 
377  // simply send the user to the hcuProfileRequire script to let it decide what is needed
378  $nextScript = $HB_ENV["loginpath"]."/hcuProfileRequire.prg?".$HB_ENV["cuquery"];
379  header("Location: {$nextScript}");
380  exit;
381  }
382  } else {
383  // ** If code == '000' && sC == 1
384  // ** Then we have completed all the pieces we needed to update.
385  // * go ahead and reset sC = 0
386  if (HCU_array_key_value('sC', $HB_ENV) == 1) {
387  $tixOption = "sC=0";
388  // $_COOKIE['Ticket'] = SetTicket($_COOKIE['Ticket'], $tixOption);
389  $_COOKIE['Ticket'] = SetTicket($HB_ENV, $_COOKIE['Ticket'], $tixOption);
390  $HB_ENV['sC'] = 0;
391  // ** CONTINUE WITH WHATEVER SCRIP WE ARE IN
392  }
393  }
394 
395  // clear out the cookie in case it exists
396  HCU_setcookie_env ($HB_ENV['SYSENV'], "securePass", "", (time() - 3600));
397 
398  }
399  }
400 
401 
402  } catch ( Error $e) {
403 
404  if (isset($excHndlr)) {
405  // ** CALL on the global exception Handler for this type of error
406  $excHndlr->catchException($e);
407  } else {
408  // ** Really bad -- most likely an include script failed to load before we could load the Error Handler
409  include_once(dirname(__FILE__) . '/../../shared/includes/hcuBadRequest.i');
410  }
411  exit;
412  } catch (ErrorException $e) {
413  // ** This should be setup a little more robust so that it will work seemlessly
414  // * for destop scripts and for '.data' scripts that return json objects
415 
416  // ** Need to output something here to show we have failed.
417 
418  $serviceErrorMsg = $e->getMessage();
419  $serviceErrorCode = $e->getCode();
420 
421  // ** LOG THE exception
422  if (isset($excHndlr)) {
423  $excHndlr->logException($e);
424  }
425  require_once(dirname(__FILE__) . '/../includes/hcuErrorPage.i');
426 
427  exit;
428  }