Odyssey
packetdump.prg
1 <?php
2  header("Expires: Sat 20 May 1995 03:32:38 GMT");
3 header("Pragma: no-cache");
4 header("Cache-Control: no-cache, must-revalidate");
5 
6  $monLibrary= dirname(__FILE__) . "/../library";
7  $sharedLibrary= dirname(__FILE__) . "/../../shared/library";
8  require_once("$monLibrary/cu_top.i");
9  require_once("$monLibrary/ck_hticket.i");
10  require_once("$sharedLibrary/xml_processing.i");
11 
12  $dms_ok = array('Hu' => 'string', 'Cn' => 'string', 'Pass' => 'string',
13  'Showtab' => 'string', 'Clw' => 'string',
14  'pktstamp' => 'string', 'cutoff' => 'string', 'dowhat' => 'string',
15  'holds2' => 'string', 'fileit' => 'string', 'truncit' => 'string',
16  'showsplit' => 'string', 'action' => 'string', 'R1' => 'string',
17  'R2' => 'string', 'R3' => 'string', 'R4' => 'string',
18  'R5' => 'string', 'Amt' => 'string', 'Fs' => 'string',
19  'Tc' => 'string');
20 
21  dms_import($dms_ok);
22 
23 if (!CheckPerm($dbh, $Hu, basename($_SERVER['SCRIPT_NAME']), $_SERVER['REMOTE_ADDR'])) {
24  // ** Permissions failed
25  // ** redirect to new page
26  header("Location: /hcuadm/hcu_noperm.prg");
27  exit;
28 }
29 
30 #error_reporting (E_ALL);
31 error_reporting(E_ERROR | E_WARNING | E_PARSE);
32 echo "<html><LINK REL=stylesheet HREF='/monitor/css/dms.css' TYPE='text/css'>";
33 $action = (empty($action) ? "Show" : $action);
34 $thirty = mktime() - (30 * 86400);
35 $thirty = date("Ymd", $thirty);
36 #syslog(LOG_ERR,"Packetdump action is $action");
37 $host = substr($_SERVER['SERVER_NAME'], 0, strpos($_SERVER['SERVER_NAME'], "."));
38 $Fs = isset($Fs) ? $Fs : '';
39 
40 switch ($action) {
41  case "Show":
42 # show forms for fetch/post
43  $sql = "select cu, liveserver, histdays, flagset2
44  from cuadmin
45  where liveserver > ''
46  and liveserver not ilike '%pgload%'
47  and livebatch <> 'C'
48  order by cu";
49  $sth = db_query($sql, $dbh);
50  $options = "<select name=Fs>\n";
51  for ($row = 0; list($cu, $fetcher, $histdays, $fset2) = db_fetch_array($sth, $row); $row++) {
52  $cu = trim($cu);
53  $seltext = ($cu == $Fs ? ' selected' : '');
54  $options .= "<OPTION VALUE='$cu|$fetcher|$histdays|$fset2' $seltext>$cu</OPTION>\n";
55  }
56  $options .= "</select>\n";
57  db_free_result($sth);
58 
59  print <<<EOF
60  <head><title>Packet Dump</title></head>
61  <body bgcolor=#FFFFFF onLoad="self.focus()">
62  <div align='center' class='usu'>$host Live Packet Tester</div>
63  <div align='right' class='dtl'><a href="javascript:window.close()">Close Window</a></div>
64  <table width=98% cellpadding=2><tr>
65  <td valign=top align=center width=20%>
66  <form action="{$_SERVER['PHP_SELF']}?action='fetch'">
67  <table cellpadding=2>
68  <tr><td class='bar' colspan=2 align=center>Fetch a Packet</td></tr>
69  <tr><td class='dtl' align="right">Fetcher: </td>
70  <td>$options</td></tr>
71  <tr><td class='dtl' align="right">Member: </td>
72  <td><input type=text name="Cn" size=10></td></tr>
73  <tr><td class='dtl' align="right">Password: </td>
74  <td><input type=password name="Pass" size=10 MAXLENGTH="20"></td></tr>
75  <tr><td class='dtl' align="right">Show Tabs as: </td>
76  <td><input type=text name="Showtab" size=10></td></tr>
77  <tr><td class='dtl' align="right">Query Delay: </td>
78  <td><input type=text name="Clw" size=10 value=1></td></tr>
79  <tr><td class='dtl' align="right">Last Packet Stamp: </td>
80  <td><input type=text name="pktstamp" size=10 value=1></td>
81  </tr>
82  <tr><td class='dtl' align="right">Earliest Date: </td>
83  <td><input type=text name="cutoff" size=10 value='$thirty'></td>
84  </tr>
85  <tr><td class='dtl' align="right">Display Packet: </td>
86  <td><font size=-2><input type=radio name="dowhat" value="S" checked>&nbsp;Display Only<br>
87  <input type=radio name="dowhat" value="D">&nbsp;<nobr>Load Database<br>
88  <input type=radio name="dowhat" value="C">&nbsp;<nobr>Count Columns</font></td>
89  </tr>
90  <tr><td class='dtl' align="right">Include Holds </td>
91  <td><input type=checkbox name="holds2" value="512"></td>
92  </tr>
93  <tr><td class='dtl' align="right" valign="top">Save raw packet to file: </td>
94  <td><input type=checkbox name="fileit" value="OK"></td>
95  </tr>
96  <tr><td class='dtl' align="right">Truncate packet file: </td>
97  <td><input type=checkbox name="truncit" value="OK"></td>
98  </tr>
99  <tr><td class='dtl' align="right">Show Split Data: </td>
100  <td><input type=checkbox name="showsplit" value="OK"></td>
101  </tr>
102  <tr><td colspan=2 align="center">
103  <input type=submit name="action" value="Fetch" class='bar'>
104  </td></tr>
105  </table>
106  </form>
107  </td><td valign=top align=center width=20%>
108  <form action="{$_SERVER['PHP_SELF']}?action='retrieve'">
109  <table cellpadding=2>
110  <tr><td class='bar' colspan=2 align=center>Cross-Accounts</td></tr>
111  <tr><td class='dtl' align="right">Fetcher: </td>
112  <td>$options</td></tr>
113  <tr><td class='dtl' align="right">Member: </td>
114  <td><input type=text name="Cn" size=10></td></tr>
115  <tr><td class='dtl' align="right">Show Tabs as: </td>
116  <td><input type=text name="Showtab" size=10></td></tr>
117  <tr><td class='dtl' align="right">Display Packet: </td>
118  <td><font size=-2><input type=radio name="dowhat" value="S" checked>&nbsp;Display Only<br>
119  <input type=radio name="dowhat" value="D">&nbsp;<nobr>Load Database<br>
120  <input type=radio name="dowhat" value="C">&nbsp;<nobr>Count Columns</font></td>
121  </tr>
122  <tr><td class='dtl' align="right" valign="top">Save raw<br>packet to file: </td>
123  <td><input type=checkbox name="fileit" value="OK"></td>
124  </tr>
125  <tr><td class='dtl' align="right">Truncate packet file: </td>
126  <td><input type=checkbox name="truncit" value="OK"></td>
127  </tr>
128  <tr><td class='dtl' align="right">Show Split Data: </td>
129  <td><input type=checkbox name="showsplit" value="OK"></td>
130  </tr>
131  <tr><td colspan=2 align="center">
132  <input type=submit name="action" value="Retrieve" class='bar'></td></tr>
133  </table>
134  </form>
135  </td><td valign=top align=center width=20%>
136  <form action="{$_SERVER['PHP_SELF']}?action='mir'">
137  <table cellpadding=2>
138  <tr><td class='bar' colspan=2 align=center>Member Info</td></tr>
139  <tr><td class='dtl' align="right">Fetcher: </td>
140  <td>$options</td></tr>
141  <tr><td class='dtl' align="right">Member: </td>
142  <td><input type=text name="Cn" size=10></td></tr>
143  <tr><td class='dtl' align="right">Show Tabs as: </td>
144  <td><input type=text name="Showtab" size=10></td></tr>
145  <tr><td class='dtl' align="right">Display Packet: </td>
146  <td><font size=-2><input type=radio name="dowhat" value="S" checked>&nbsp;Display Only<br>
147  <input type=radio name="dowhat" value="C">&nbsp;<nobr>Count Columns</font></td>
148  </tr>
149  <tr><td class='dtl' align="right" valign="top">Save raw<br>packet to file: </td>
150  <td><input type=checkbox name="fileit" value="OK"></td>
151  </tr>
152  <tr><td class='dtl' align="right">Truncate packet file: </td>
153  <td><input type=checkbox name="truncit" value="OK"></td>
154  </tr>
155  <tr><td class='dtl' align="right">Show Split Data: </td>
156  <td><input type=checkbox name="showsplit" value="OK"></td>
157  </tr>
158  <tr><td colspan=2 align="center">
159  <input type=submit name="action" value="Mir" class='bar'></td></tr>
160  </table>
161  </form>
162  </td><td valign=top align=center width=20%>
163  <form action="{$_SERVER['PHP_SELF']}?action='estm'">
164  <table cellpadding=2>
165  <tr><td class='bar' colspan=2 align=center>Core Estatements</td></tr>
166  <tr><td class='dtl' align="right">Fetcher: </td>
167  <td>$options</td></tr>
168  <tr><td class='dtl' align="right">Member: </td>
169  <td><input type=text name="Cn" size=10></td></tr>
170  <tr><td class='dtl' align="right">Statement ID: </td>
171  <td><input type=text name="stId" size=10></td></tr>
172  <tr><td colspan=2 align="center">
173  <input type=submit name="action" value="Estm" class='bar'></td></tr>
174  </table>
175  </form>
176  </td><td valign=top align=center width=*>
177  <form action="{$_SERVER['PHP_SELF']}?action='post'">
178  <table cellpadding=2>
179  <tr><td class='bar' colspan=2 align=center>Post a Packet</td></tr>
180  <tr><td class='dtl' align="right">Fetcher: </td>
181  <td>$options</td></tr>
182  <tr><td class='dtl' align="right">Member: </td>
183  <td><input type=text name="Cn" size=10></td></tr>
184  <tr><td class='dtl' align="right">Transaction Code: </td>
185  <td><select name="Tc">
186  <option>AT</option>
187  <option>LP</option>
188  <option>LA</option>
189  <option>CW</option>
190  <option>CP</option>
191  <option>MD</option>
192  <option>MA</option>
193  <option>ES</option>
194  <option>ED</option>
195  </select>
196  </td></tr>
197  <tr><td class='dtl' align="right">Ref 1 (From Sfx): </td>
198  <td><input type=text name="R1" size=10 value=1></td></tr>
199  <tr><td class='dtl' align="right">Ref 2 (To Sfx): </td>
200  <td><input type=text name="R2" size=10 value=1></td></tr>
201  <tr><td class='dtl' align="right">Ref 3 (email): </td>
202  <td><input type=text name="R3" size=10 value=1></td></tr>
203  <tr><td class='dtl' align="right">Ref 4 (To misc): </td>
204  <td><input type=text name="R4" size=10 value=1></td></tr>
205  <tr><td class='dtl' align="right">Ref 5 (To Mem): </td>
206  <td><input type=text name="R5" size=10 value=1></td></tr>
207  <tr><td class='dtl' align="right">Amount: </td>
208  <td><input type=text name="Amt" size=10></td></tr>
209  <tr><td colspan=2 align="center">
210  <input type=submit name="action" value="Post" class='bar'></td></tr>
211  </table>
212  </form>
213  </td>
214  </tr></table></body></html>
215 EOF;
216  exit;
217  case "Fetch":
218  $dms_ok = array('Fs' => 'string', 'cutoff' => 'string', 'Cn' => 'digits',
219  'Pass' => 'string', 'ShowHolds' => 'string', 'Showtab' => 'string',
220  'URL' => 'string', 'holds2' => 'string', 'Clw' => 'digits', 'pktstamp' => 'string',
221  'cutoff' => 'string', 'dowhat' => 'string', 'fileit' => 'string',
222  'truncit' => 'string', 'showsplit' => 'string');
223  dms_import($dms_ok);
224 
225 # check parameters
226 # get a packet
227  list($Cu, $Fs, $histdays, $fset2) = explode("|", "$Fs", 4);
228  if (empty($cutoff)) {
229  $cutoff = "$thirty";
230  }
231  if (empty($histdays)) {
232  $histdays = 0;
233  }
234  if (empty($fset2)) {
235  $fset2 = 0;
236  }
237  $CU2_SHOWHOLD = 512;
238  $ShowHolds = ($fset2 & $CU2_SHOWHOLD);
239  if ($holds2) {
240  $ShowHolds = $CU2_SHOWHOLD;
241  }
242  print "<html><head><title>Packet Fetch</title></head><body bgcolor=#FFFFFF>
243  Fetching packet using parameters<ul>
244  <li>Credit Union: $Cu</li>
245  <li>Member: $Cn</li>
246  <li>Password: $Pass</li>
247  <li>Show Holds: $ShowHolds</li>
248  <li>Show Tabs: $Showtab</li>
249  <li>Fetched from: $Fs</li>
250  <li>URL: $URL</li>
251  <li>Wait between calls: $Clw</li>
252  <li>Last Packet Stamp: $pktstamp</li>
253  <li>Requested Cutoff: $cutoff</li>
254  </ul><br>";
255  if ($dowhat == "D") {
256  stuff_packet($Fs, $Cu, $Cn, $Clw, $pktstamp, $Showtab, $Pass, $fileit, $truncit, $cutoff, $histdays, $fset2, $showsplit, $holds2);
257  } elseif ($dowhat == "C") {
258  valid_packet($Fs, $Cu, $Cn, $Clw, $pktstamp, $Showtab, $Pass, $fileit, $truncit, $cutoff, $histdays, $fset2, $showsplit, $holds2);
259  } else {
260  fetch_packet($Fs, $Cu, $Cn, $Clw, $pktstamp, $Showtab, $Pass, $fileit, $truncit, $cutoff, $histdays, $showsplit, $holds2);
261  }
262  exit;
263  break;
264  case "Retrieve":
265 # check parameters
266 # get a packet
267  $dms_ok = array('Fs' => 'string', 'Cn' => 'digits', 'Showtab' => 'string',
268  'dowhat' => 'string', 'fileit' => 'string', 'truncit' => 'string',
269  'showsplit' => 'string');
270  dms_import($dms_ok);
271 
272  list($Cu, $Fs, $histdays) = explode("|", "$Fs", 3);
273  print "<html><head><title>Cross-Account Retrieve</title></head><body bgcolor=#FFFFFF>
274  Retrieving Cross-Account Authorizations using parameters<ul>
275  <li>Credit Union: $Cu</li>
276  <li>Member: $Cn</li>
277  <li>Show Tabs: $Showtab</li>
278  <li>Fetched from: $Fs</li>
279  </ul><br>";
280  if ($dowhat == "D") {
281  stuff_xac($Fs, $Cu, $Cn, $Showtab, $fileit, $truncit, $showsplit);
282  } elseif ($dowhat == "C") {
283  valid_xac($Fs, $Cu, $Cn, $Showtab, $fileit, $truncit, $showsplit);
284  } else {
285  fetch_xac($Fs, $Cu, $Cn, $Showtab, $fileit, $truncit);
286  }
287  exit;
288  break;
289  case "Mir":
290 # check parameters
291 # get a packet
292  $dms_ok = array('Fs' => 'string', 'Cn' => 'digits', 'Showtab' => 'string',
293  'dowhat' => 'string', 'fileit' => 'string', 'truncit' => 'string',
294  'showsplit' => 'string');
295  dms_import($dms_ok);
296 
297  list($Cu, $Fs, $histdays) = explode("|", "$Fs", 3);
298  print "<html><head><title>Member Info Retrieve</title></head><body bgcolor=#FFFFFF>
299  Retrieving Member Info using parameters<ul>
300  <li>Credit Union: $Cu</li>
301  <li>Member: $Cn</li>
302  <li>Show Tabs: $Showtab</li>
303  <li>Fetched from: $Fs</li>
304  </ul><br>";
305  if ($dowhat == "C") {
306  valid_mir($Fs, $Cu, $Cn, $Showtab, $fileit, $truncit, $showsplit);
307  } else {
308  fetch_mir($Fs, $Cu, $Cn, $Showtab, $fileit, $truncit);
309  }
310  exit;
311  break;
312  case "Estm":
313 # check parameters
314 # get a packet
315  $dms_ok = array('Fs' => 'string', 'Cn' => 'digits', 'stId' => 'string');
316  dms_import($dms_ok);
317 
318  list($Cu, $Fs, $histdays) = explode("|", "$Fs", 3);
319  if ($stId == '') {
320  $Command = 'ETOC';
321  } else {
322  $Command = 'ESTM';
323  }
324  print "<html><head><title>Core Estatement Retrieve</title></head><body bgcolor=#FFFFFF>
325  Retrieving Estatement using parameters<ul>
326  <li>Credit Union: $Cu</li>
327  <li>Member: $Cn</li>
328  <li>Statement ID: $stId</li>
329  <li>Cmd: $Command</li>
330  <li>Fetched from: $Fs</li>
331  </ul><br>";
332  $result = fetch_estatement(array('Cu'=>$Cu, 'Cn'=>$Cn),$Command,$stId);
333  $showit = print_r($result,true);
334  print "<h4>Response(s):</h4>";
335  print "<pre>$showit</pre>";
336  exit;
337  break;
338  case "Post":
339 # check parameters
340 # post a packet
341 
342  $dms_ok = array('Fs' => 'string', 'Cn' => 'digits', 'Showtab' => 'string',
343  'TC' => 'string', 'Amt' => 'string', 'R2' => 'string', 'R3' => 'string',
344  'R4' => 'string', 'R5' => 'string');
345  dms_import($dms_ok);
346 
347  list($Cu, $Fs, $histdays) = explode("|", "$Fs", 3);
348  $R2 = (trim($R2) == "" ? "NULL" : $R2);
349  $R3 = (trim($R3) == "" ? "NULL" : $R3);
350  $R4 = (trim($R4) == "" ? "NULL" : $R4);
351  $R5 = (trim($R5) == "" ? "NULL" : $R5);
352  print "<html><head><title>Packet Post</title></head><body bgcolor=#FFFFFF>
353  Posting packet using parameters<ul>
354  <li>Credit Union: $Cu</li>
355  <li>Member: $Cn</li>
356  <li>Posted to: $Fs</li>
357  <li>Txn Code: $Tc</li>
358  <li>Ref1: $R1</li>
359  <li>Ref2: $R2</li>
360  <li>Ref3: $R3</li>
361  <li>Ref4: $R4</li>
362  <li>Ref5: $R5</li>
363  <li>Amount: $Amt</li>
364  <li>URL: $Fs?member=$Cn&type=T&tran_code=$Tc&ref1=$R1&ref2=$R2&ref3=$R3&ref4=$R4&ref5=$R5&amount=$Amt</li>
365  </ul><br>";
366  post_packet($Fs, $Cn, $Tc, $R1, $R2, $R3, $R4, $R5, $Amt);
367  exit;
368  break;
369 }
370 
371 /**
372  * Modify default libxml_get_errors, and print useful, formatted
373  * xml parsing errors, if any.
374  */
375 function checkForXmlParseErrors($xmlstr) {
376 
377  libxml_use_internal_errors(true);
378  $xmlLoaded = simplexml_load_string($xmlstr);
379 
380  if ($xmlLoaded === false) {
381  $errors = libxml_get_errors();
382  list($errors, $isError) = modifyXmlErrorsForLogging($errors, $xmlstr);
383 
384  if ($isError) {
385  print "<h4>XML parsing: encountered '" . count($errors) . "' error(s)</h4>";
386  print "<textarea rows='20' cols='120' readonly>";
387  $count = 0;
388  foreach($errors as $error) {
389  print "\n" . ++$count . ". [" . $error["level"] . "] ";
390  print $error["message"] . " (code: " . $error["code"] . ")\n\n";
391  print "Context (line " . $error["line"] . ", column " . $error["column"] . "):\n";
392  print "---> '" . $error["context"] . "'\n\n";
393  }
394  print "</textarea>";
395  }
396  }
397 }
398 
399 function fetch_packet($Fs, $Cu, $Cn, $Clw, $pktstamp, $Showtab, $Pass, $fileit, $truncit, $cutoff, $histdays, $showsplit, $holds2) {
400 
401  if ($Pass === '0') {$Pass = '00';}
402  if ("$pktstamp" == "") {
403 # read last pktstamp from database. Calculate new threshold as
404 # now - pktinterval; if pktstamp > threshold, return (use existing)
405 #
406  set_time_limit(100);
407  global $dbh;
408  $sql = "select pktstamp, pktdate, livewait
409  from cuusers u, cuadmin a
410  where a.cu = u.cu
411  and a.cu = '" . $Cu . "' and u.user_name = '" . $Cn . "'";
412  $sth = db_query($sql, $dbh);
413  list($pktstamp, $pktdate, $Clw) = db_fetch_array($sth, 0);
414  db_free_result($sth);
415  }
416 
417  $pktstamp = (empty($pktstamp) ? 1 : $pktstamp);
418  $pktdate = (empty($pktdate) ? date("D M j Y H:i:s T") : $pktdate);
419  $Clw = (($Clw == 0 || is_null($Clw)) ? 300 : $Clw);
420 
421  $threshold = mktime() - $Clw;
422  $fs = date("D M j Y H:i:s T", $pktstamp);
423  $ft = date("D M j Y H:i:s T", $threshold);
424  if ($pktstamp > $threshold) {
425  print "Too soon to ask again<br>
426  Last Stamp: $fs<br>
427  Threshold: $ft<br></body></html>";
428  } else {
429 
430  # if the last packet was fairly recent only fetch 7 days
431  # "recent" defined as 3 times the packet timeout setting
432  $threshold = mktime() - (3 * $Clw);
433  if (empty($cutoff)) {
434  $cutoff = "$thirty";
435  }
436  if (empty($histdays)) {
437  $histdays = 0;
438  }
439 # 1072940400 is the timestamp for 20040101
440  if ($pktstamp > 1072940400 && $histdays > 0 && $cutoff == $thirty) {
441  # calculate cutoff as pktstamp - interval($histdays days)
442  if ($pktstamp > $threshold) {
443  # last packet age is less than 3 times the packet timeout setting
444  # set cutoff for 7 days
445  $cutoff = date("Ymd", mktime() - 604800);
446  } else {
447  $cutoff = date("Ymd", $pktstamp - (86400 * $histdays));
448  }
449  }
450  $delim = (strpos($Fs,'?')=== false ? '?' : '&');
451  $URL = "{$delim}member=$Cn&type=I&if_mod_since=$pktstamp&cutoff=$cutoff";
452  if (!empty($Pass)) {
453  $URL.="&pass=" . urlencode($Pass);
454  }
455  $rqx = "$Fs{$delim}member=$Cn&type=I&if_mod_since=$pktstamp&cutoff=$cutoff";
456  if (!empty($Pass)) {
457  $rqx .= "&pass=" . urlencode($Pass);
458  }
459  list ($open_fail, $pipe_err, $packet) = get_data($rqx);
460  if ($packet != '') {
461  checkForXmlParseErrors($packet);
462 //if ($packet = file_get_contents("$Fs$URL")){
463 #if ($packet = file_get_contents("/home/ubuntu/packetdump.txt")){
464 # $fd = @fopen ("$Fs$URL", "r");
465 #
466 #if ($fd) {
467 # stream_set_timeout($fd,100);
468 # while ($buff = fread ($fd, 1500)) {
469 # $packet .= $buff;
470 # }
471 #
472 # add feature here to log (and optionally truncate first) packet to a temp file
473 #
474  if ($fileit == "OK") {
475  if ($truncit == "OK") {
476  $fp = fopen("/tmp/packetdump.txt", "w");
477  } else {
478  $fp = fopen("/tmp/packetdump.txt", "a");
479  }
480  fwrite($fp, $packet);
481  }
482  if ($showsplit == 'OK') {
483  $pktpattern = '/<([^<>\/]*)?>\n?([^<]*)?/'; # current code chokes on <
484  $pktpattern = '/<([^<>\/]*)>\n*(.*)<\/\\1>/Us';
485  $pktshow = array();
486  $inqpattern = '/<\/*Inquiry>/i';
487 # ugly hack, but appears to work. Strip <Inquiry></Inquiry> to remove nesting
488  preg_match_all("$pktpattern", preg_replace($inqpattern, "", $packet), $pktshow);
489 
490  print "Split Results: using pattern $pktpattern<br><pre>++++++++++++++++++++++++++++++++++++++++\n";
491  array_walk($pktshow, 'showquoted');
492  print_r($pktshow);
493  print "++++++++++++++++++++++++++++++++++++++++\n</pre>";
494  }
495 # $packet = preg_replace("/ ?\t ?/","\t",$packet);
496  if ("$Showtab" != "") {
497  $packet = preg_replace("/\t/", "$Showtab", $packet);
498  }
499  $packet = HTMLSpecialChars($packet);
500  print "<h4>Response(s):</h4>";
501  print "URL: $URL<br><pre>$packet</pre></body></html>";
502  } else {
503  print "Couldn't get a packet!<br>$php_errormsg</body></html>";
504  }
505  }
506 }
507 
508 function stuff_packet($Fs, $Cu, $Cn, $Clw, $pktstamp, $Showtab, $Pass, $fileit, $truncit, $cutoff, $histdays, $fset2, $showsplit, $holds2) {
509 
510  global $dbh;
511  if ("$pktstamp" == "") {
512 # read last pktstamp from database. Calculate new threshold as
513 # now - pktinterval; if pktstamp > threshold, return (use existing)
514 #
515  $sql = "select pktstamp, pktdate, livewait
516  from cuusers u, cuadmin a
517  where a.cu = u.cu
518  and a.cu = '" . $Cu . "' and u.user_name = '" . $Cn . "'";
519  $sth = db_query($sql, $dbh);
520  list($pktstamp, $pktdate, $Clw) = db_fetch_array($sth, 0);
521  db_free_result($sth);
522  }
523 
524  $pktstamp = (empty($pktstamp) ? 1 : $pktstamp);
525  $pktdate = (empty($pktdate) ? date("D M j Y H:i:s T") : $pktdate);
526  $Clw = (($Clw == 0 || is_null($Clw)) ? 300 : $Clw);
527 
528  $threshold = mktime() - $Clw;
529  $rmt = mktime();
530  $fs = date("D M j Y H:i:s T", $pktstamp);
531  $ft = date("D M j Y H:i:s T", $threshold);
532  if ($pktstamp > $threshold) {
533  print "Too soon to ask again<br>
534  Last Stamp: $fs<br>
535  Threshold: $ft<br></body></html>";
536  } else {
537  # if the last packet was fairly recent only fetch 7 days
538  # "recent" defined as 3 times the packet timeout setting
539  $threshold = mktime() - (3 * $Clw);
540  if (empty($cutoff)) {
541  $cutoff = "$thirty";
542  }
543  if (empty($histdays)) {
544  $histdays = 0;
545  }
546 # 1072940400 is the timestamp for 20040101
547  if ($pktstamp > 1072940400 && $histdays > 0 && $cutoff == $thirty) {
548  # calculate cutoff as pktstamp - interval($histdays days)
549  if ($pktstamp > $threshold) {
550  # last packet age is less than 3 times the packet timeout setting
551  # set cutoff for 7 days
552  $cutoff = date("Ymd", mktime() - 604800);
553  } else {
554  $cutoff = date("Ymd", $pktstamp - (86400 * $histdays));
555  }
556  }
557  $delim = (strpos($Fs,'?')=== false ? '?' : '&');
558  $URL = "{$delim}member=$Cn&type=I&if_mod_since=$pktstamp&cutoff=$cutoff";
559  if (!empty($Pass)) {
560  $URL.="&pass=" . urlencode($Pass);
561  }
562  $rqx = "$Fs$URL";
563  list ($open_fail, $pipe_err, $packet) = get_data($rqx);
564  if ($packet != '') {
565 
566 // $fd = @fopen ("$Fs$URL", "r");
567 //
568 //if ($fd) {
569 // while ($buff = fread ($fd, 1500)) {
570 // $packet .= $buff;
571 // }
572  if ($fileit == "OK") {
573  if ($truncit == "OK") {
574  $fp = fopen("/tmp/packetdump.txt", "w");
575  } else {
576  $fp = fopen("/tmp/packetdump.txt", "a");
577  }
578  fwrite($fp, $packet);
579  }
580 
581  $pktpattern = '/<([^<>\/]*)?>\n?([^<]*)?/';
582  $pktpieces = array();
583  $pktpattern = '/<([^<>\/]*)>(.*)<\/\\1>/Us';
584  $inqpattern = '/<\/*Inquiry>/i';
585 # ugly hack, but appears to work. Strip <Inquiry></Inquiry> to remove nesting
586  preg_match_all("$pktpattern", preg_replace($inqpattern, "", $packet), $pktpieces);
587 
588  if ($showsplit == 'OK') {
589  #
590  # use a different array so we don't change the actual data
591 
592  $pktpattern = '/<([^<>\/]*)?>\n?([^<]*)?/'; # current code chokes on <
593  $pktpattern = '/<([^<>\/]*)>\n*(.*)<\/\\1>/Us';
594  $pktshow = array();
595  $inqpattern = '/<\/*Inquiry>/i';
596 # ugly hack, but appears to work. Strip <Inquiry></Inquiry> to remove nesting
597  preg_match_all("$pktpattern", preg_replace($inqpattern, "", $packet), $pktshow);
598 
599  print "Split Results: using pattern $pktpattern<br><pre>++++++++++++++++++++++++++++++++++++++++\n";
600  array_walk($pktshow, 'showquoted');
601  print_r($pktshow);
602  print "++++++++++++++++++++++++++++++++++++++++\n</pre>";
603  }
604  array_shift($pktpieces);
605  foreach ($pktpieces[0] as $pos => $tag) {
606  $tags[(strtolower($tag))] = $pos;
607  }
608  array_shift($pktpieces);
609  list($mem, $pcutoff) = explode("\t", $pktpieces[0][($tags['parameters'])]);
610  # if the packet presents a cutoff date, let it override our request
611  $cutoff = ("$pcutoff" == "" ? $cutoff : $pcutoff);
612  list($mem, $statcode, $statdesc) = explode("\t", $pktpieces[0][($tags['status'])]);
613  list($pktstamp, $pktdate) = explode("\t", $pktpieces[0][($tags['time'])]);
614  print "<br>URL: $URL<br>";
615  print "Status: <br>
616  Code: $statcode<br>
617  Description: $statdesc<br>
618  Cutoff: (requested) $cutoff (returned) $pcutoff<br>";
619  if (trim($mem) != $Cn) {
620  $statcode = 999;
621  $statdesc = "Returned packet doesn't match request!";
622  }
623 
624 # check the return status to decide what to do here
625 # 101 New Data -- process packet, update pktstamp
626 # 201 System Unavailable, New Data -- process packet, update pktstamp
627 # 100 No New Data -- update pktstamp
628 # 200 System Unavailable, No New Data -- return immediately
629 # 202 System Unavailable, No Data -- return immediately
630 # 999 Calling error or Invalid Parms -- return immediately
631 #
632  $errstat = 0;
633  $CU2_SHOWHOLD = 512;
634  switch ($statcode) {
635  case 101:
636  case 201:
637  # Need to refresh the db data. Start parsing that packet...
638  foreach ($tags as $tag => $pos) {
639  switch ($tag) {
640  case "member":
641  case "accountbalance":
642  case "loanbalance":
643  case "accounthistory":
644  case "loanhistory":
645  case "messages":
646  case "holds":
647  $$tag = explode("\n", $pktpieces[0][$pos]);
648 
649  # newlines around data on multiline components
650  # causes empty items in array.
651  # Pop the empty off the end
652  # new regex leaves newline at top as well,
653  # so shift it off also
654 
655 
656  array_shift($$tag);
657  array_pop($$tag);
658  }
659  }
660 
661  # stuff each piece in the db
662 # set pktstamp to 1 so if we die in the middle next query will force reload
663 
664  $sql = "update cuusers set pktstamp = 1,
665  pktdate = '" . date("D M j Y H:i:s T") . "'
666  where cu = '" . $Cu . "'
667  and user_name = '" . $Cn . "'";
668  $sth = db_query($sql, $dbh);
669 
670  $tblist = array("accountbalance", "loanbalance", "accounthistory", "loanhistory", "messages");
671  if ($holds2 || ($fset2 & $CU2_SHOWHOLD) == $CU2_SHOWHOLD) {
672  $tblist[] = "holds";
673  }
674  foreach ($tblist as $tbl) {
675  switch ($tbl) {
676  case "messages":
677  case "accountbalance":
678  case "loanbalance":
679  case "holds":
680  $sql = "delete from ${Cu}${tbl}
681  where accountnumber='" . $Cn . "'";
682  $sth = db_query($sql, $dbh);
683  break;
684  case "accounthistory":
685  case "loanhistory":
686  $sql = "delete from ${Cu}${tbl}
687  where accountnumber='" . $Cn . "'
688  and date >= '" . $cutoff . "'";
689  $sth = db_query($sql, $dbh);
690  break;
691  }
692 
693  print "<font color='blue'>Loading " . sizeof($$tbl) . " records for $tbl...</font><br>\n";
694  if (sizeof($$tbl)) {
695  print "<pre>";
696 
697  foreach ($$tbl as $line) {
698  $copyerr = "";
699  $line = preg_replace("/ ?\t ?/", "\t", $line);
700  $line = preg_replace("/ $/", "", $line);
701  $line = preg_replace("/\\0/", "", $line);
702  $sth = db_query("copy ${Cu}${tbl} from stdin with null as ''", $dbh);
703  pg_put_line($dbh, "$line\n");
704  pg_put_line($dbh, "\\.\n");
705  if (!(@pg_end_copy($dbh))) {
706  $copyerr = "Error writing previous line";
707  $errstat++;
708  }
709  if ("$Showtab" != "") {
710  $line = preg_replace("/\t/", "$Showtab", $line);
711  }
712  $line = HTMLSpecialChars($line);
713  print "$line\n";
714  if ($copyerr > "") {
715  print "<font color='red'>$copyerr</font>\n<br>";
716  }
717  }
718  print "</pre>";
719  }
720  }
721 
722  case 100:
723  if ($errstat == 0) {
724  $sql = "update cuusers set pktstamp = $pktstamp,
725  pktdate = '" . $pktdate . "'
726  where cu = '" . $Cu . "'
727  and user_name = '" . $Cn . "'";
728  $sth = db_query($sql, $dbh);
729  if (!$sth) {
730  print "FAILED setting pktstamp for $Cu:$Cn" . pg_errormessage() . "\n<br>";
731  }
732  }
733  case 200:
734  case 202:
735  case 999:
736  if ($errstat == 0) {
737  print "Status: $statcode Date: $pktdate Description: $statdesc<br>\n";
738  } else {
739  print "Status: $statcode Date: 1 Description: $statdesc<br>\n";
740  }
741  }
742  } else {
743  print "Couldn't get a packet!<br>$php_errormsg</body></html>";
744  }
745  }
746 }
747 
748 function valid_packet($Fs, $Cu, $Cn, $Clw, $pktstamp, $Showtab, $Pass, $fileit, $truncit, $cutoff, $histdays, $fset2, $showsplit, $holds2) {
749 
750  global $dbh;
751  if ("$pktstamp" == "") {
752 # read last pktstamp from database. Calculate new threshold as
753 # now - pktinterval; if pktstamp > threshold, return (use existing)
754 #
755  $sql = "select pktstamp, pktdate, livewait
756  from cuusers u, cuadmin a
757  where a.cu = u.cu
758  and a.cu = '" . $Cu . "' and u.user_name = '" . $Cn . "'";
759  $sth = db_query($sql, $dbh);
760  list($pktstamp, $pktdate, $Clw) = db_fetch_array($sth, 0);
761  db_free_result($sth);
762  }
763 
764  $pktstamp = (empty($pktstamp) ? 1 : $pktstamp);
765  $pktdate = (empty($pktdate) ? date("D M j Y H:i:s T") : $pktdate);
766  $Clw = (($Clw == 0 || is_null($Clw)) ? 300 : $Clw);
767 
768  $threshold = mktime() - $Clw;
769  $rmt = mktime();
770  $fs = date("D M j Y H:i:s T", $pktstamp);
771  $ft = date("D M j Y H:i:s T", $threshold);
772  if ($pktstamp > $threshold) {
773  print "Too soon to ask again<br>
774  Last Stamp: $fs<br>
775  Threshold: $ft<br></body></html>";
776  } else {
777  # if the last packet was fairly recent only fetch 7 days
778  # "recent" defined as 3 times the packet timeout setting
779  $threshold = mktime() - (3 * $Clw);
780  if (empty($cutoff)) {
781  $cutoff = "$thirty";
782  }
783  if (empty($histdays)) {
784  $histdays = 0;
785  }
786 # 1072940400 is the timestamp for 20040101
787  if ($pktstamp > 1072940400 && $histdays > 0 && $cutoff == $thirty) {
788  # calculate cutoff as pktstamp - interval($histdays days)
789  if ($pktstamp > $threshold) {
790  # last packet age is less than 3 times the packet timeout setting
791  # set cutoff for 7 days
792  $cutoff = date("Ymd", mktime() - 604800);
793  } else {
794  $cutoff = date("Ymd", $pktstamp - (86400 * $histdays));
795  }
796  }
797  $delim = (strpos($Fs,'?')=== false ? '?' : '&');
798  $URL = "{$delim}member=$Cn&type=I&if_mod_since=$pktstamp&cutoff=$cutoff";
799  if (!empty($Pass)) {
800  $URL.="&pass=" . urlencode($Pass);
801  }
802  $rqx = "$Fs$URL";
803  list ($open_fail, $pipe_err, $packet) = get_data($rqx);
804  if ($packet != '') {
805 
806 // $fd = @fopen ("$Fs$URL", "r");
807 //
808 //if ($fd) {
809 // while ($buff = fread ($fd, 1500)) {
810 // $packet .= $buff;
811 // }
812  if ($fileit == "OK") {
813  if ($truncit == "OK") {
814  $fp = fopen("/tmp/packetdump.txt", "w");
815  } else {
816  $fp = fopen("/tmp/packetdump.txt", "a");
817  }
818  fwrite($fp, $packet);
819  }
820 
821  $pktpattern = '/<([^<>\/]*)?>\n?([^<]*)?/';
822  $pktpieces = array();
823  $pktpattern = '/<([^<>\/]*)>(.*)<\/\\1>/Us';
824  $inqpattern = '/<\/*Inquiry>/i';
825 # ugly hack, but appears to work. Strip <Inquiry></Inquiry> to remove nesting
826  preg_match_all("$pktpattern", preg_replace($inqpattern, "", $packet), $pktpieces);
827 
828  if ($showsplit == 'OK') {
829  #
830  # use a different array so we don't change the actual data
831 
832  $pktpattern = '/<([^<>\/]*)?>\n?([^<]*)?/'; # current code chokes on <
833  $pktpattern = '/<([^<>\/]*)>\n*(.*)<\/\\1>/Us';
834  $pktshow = array();
835  $inqpattern = '/<\/*Inquiry>/i';
836 # ugly hack, but appears to work. Strip <Inquiry></Inquiry> to remove nesting
837  preg_match_all("$pktpattern", preg_replace($inqpattern, "", $packet), $pktshow);
838 
839  print "Split Results: using pattern $pktpattern<br><pre>++++++++++++++++++++++++++++++++++++++++\n";
840  array_walk($pktshow, 'showquoted');
841  print_r($pktshow);
842  print "++++++++++++++++++++++++++++++++++++++++\n</pre>";
843  }
844  array_shift($pktpieces);
845  foreach ($pktpieces[0] as $pos => $tag) {
846  $tags[(strtolower($tag))] = $pos;
847  }
848  array_shift($pktpieces);
849  list($mem, $pcutoff) = explode("\t", $pktpieces[0][($tags['parameters'])]);
850  # if the packet presents a cutoff date, let it override our request
851  $cutoff = ("$pcutoff" == "" ? $cutoff : $pcutoff);
852  list($mem, $statcode, $statdesc) = explode("\t", $pktpieces[0][($tags['status'])]);
853  list($pktstamp, $pktdate) = explode("\t", $pktpieces[0][($tags['time'])]);
854  print "<br>URL: $URL<br>";
855  print "Status: <br>
856  Code: $statcode<br>
857  Description: $statdesc<br>
858  Cutoff: (requested) $cutoff (returned) $pcutoff<br>";
859  if (trim($mem) != $Cn) {
860  $statcode = 999;
861  $statdesc = "Returned packet doesn't match request!";
862  }
863 
864 # check the return status to decide what to do here
865 # 101 New Data -- process packet, update pktstamp
866 # 201 System Unavailable, New Data -- process packet, update pktstamp
867 # 100 No New Data -- update pktstamp
868 # 200 System Unavailable, No New Data -- return immediately
869 # 202 System Unavailable, No Data -- return immediately
870 # 999 Calling error or Invalid Parms -- return immediately
871 #
872  $errstat = 0;
873  switch ($statcode) {
874  case 101:
875  case 201:
876  # Need to refresh the db data. Start parsing that packet...
877  foreach ($tags as $tag => $pos) {
878  switch ($tag) {
879  case "member":
880  case "accountbalance":
881  case "loanbalance":
882  case "accounthistory":
883  case "loanhistory":
884  case "messages":
885  case "holds":
886  $$tag = explode("\n", $pktpieces[0][$pos]);
887 
888  # newlines around data on multiline components
889  # causes empty items in array.
890  # Pop the empty off the end
891  # new regex leaves newline at top as well,
892  # so shift it off also
893 
894 
895  array_shift($$tag);
896  array_pop($$tag);
897  }
898  }
899 
900  # count each part
901  $expected{'accountbalance'} = 10;
902  $expected{'accounthistory'} = 10;
903  $expected{'loanbalance'} = 14;
904  $expected{'loanhistory'} = 9;
905  $expected{'messages'} = 4;
906  $expected{'holds'} = 9;
907 
908 
909 
910  $tblist = array("accountbalance", "loanbalance", "accounthistory", "loanhistory", "messages");
911  if ($holds2 || ($fset2 & $CU2_SHOWHOLD) == $CU2_SHOWHOLD) {
912  $tblist[] = "holds";
913  }
914  foreach ($tblist as $tbl) {
915 # print "<font color='blue'>Counting " . sizeof($$tbl) . " records for $tbl...</font><br>\n";
916  if (sizeof($$tbl)) {
917  print "<pre>";
918  $mincnt = 0;
919  $maxcnt = 0;
920 
921  foreach ($$tbl as $line) {
922  $line = preg_replace("/ ?\t ?/", "\t", $line);
923  $line = preg_replace("/ $/", "", $line);
924  $line = preg_replace("/\\0/", "", $line);
925  $count = (count(explode("\t", $line)));
926  $mincnt = (($count < $mincnt || $mincnt == 0) ? $count : $mincnt);
927  $maxcnt = ($count > $maxcnt ? $count : $maxcnt);
928  if ("$Showtab" != "") {
929  $line = preg_replace("/\t/", "$Showtab", $line);
930  }
931  $line = HTMLSpecialChars($line);
932  print "$line\n";
933  }
934  print "</pre>";
935  print "<font color='blue'>Counted " . sizeof($$tbl) . " records for $tbl... (Expected: " . $expected{$tbl} . ")</font> Min: $mincnt Max: $maxcnt<br><br>\n";
936  }
937  }
938 
939  case 100:
940  case 200:
941  case 202:
942  case 999:
943  if ($errstat == 0) {
944  print "Status: $statcode Date: $pktdate Description: $statdesc<br>\n";
945  } else {
946  print "Status: $statcode Date: 1 Description: $statdesc<br>\n";
947  }
948  }
949  } else {
950  print "Couldn't get a packet!<br>$php_errormsg</body></html>";
951  }
952  }
953 }
954 
955 function fetch_xac($Fs, $Cu, $Cn, $Showtab, $fileit, $truncit) {
956 
957  $delim = (strpos($Fs,'?')=== false ? '?' : '&');
958  $URL = "{$delim}member=$Cn&type=X&f=xxxx";
959  $rqx = "$Fs$URL";
960  list ($open_fail, $pipe_err, $packet) = get_data($rqx);
961  if ($packet != '') {
962  checkForXmlParseErrors($packet);
963 
964 // $fd = @fopen ("$Fs$URL", "r");
965 //
966 //if ($fd) {
967 // while ($buff = fread ($fd, 1500)) {
968 // $packet .= $buff;
969 // }
970 #
971 # add feature here to log (and optionally truncate first) packet to a temp file
972 #
973  if ($fileit == "OK") {
974  if ($truncit == "OK") {
975  $fp = fopen("/tmp/packetdump.txt", "w");
976  } else {
977  $fp = fopen("/tmp/packetdump.txt", "a");
978  }
979  fwrite($fp, $packet);
980  }
981 
982  if ("$Showtab" != "") {
983  $packet = preg_replace("/\t/", "$Showtab", $packet);
984  }
985  $packet = HTMLSpecialChars($packet);
986  print "<h4>Response(s):</h4>";
987  print "URL: $URL<br><pre>$packet</pre></body></html>";
988  } else {
989  print "Couldn't get a packet!<br>$php_errormsg</body></html>";
990  }
991 }
992 
993 function fetch_mir($Fs, $Cu, $Cn, $Showtab, $fileit, $truncit) {
994  $delim = (strpos($Fs,'?')=== false ? '?' : '&');
995  $URL = "{$delim}member=$Cn&type=MIR&f=xxxx";
996  $rqx = "$Fs$URL";
997  list ($open_fail, $pipe_err, $packet) = get_data($rqx);
998  if ($packet != '') {
999  checkForXmlParseErrors($packet);
1000 // $fd = @fopen ("$Fs$URL", "r");
1001 //
1002 //if ($fd) {
1003 // while ($buff = fread ($fd, 1500)) {
1004 // $packet .= $buff;
1005 // }
1006 #
1007 # add feature here to log (and optionally truncate first) packet to a temp file
1008 #
1009  if ($fileit == "OK") {
1010  if ($truncit == "OK") {
1011  $fp = fopen("/tmp/packetdump.txt", "w");
1012  } else {
1013  $fp = fopen("/tmp/packetdump.txt", "a");
1014  }
1015  fwrite($fp, $packet);
1016  }
1017 
1018  if ("$Showtab" != "") {
1019  $packet = preg_replace("/\t/", "$Showtab", $packet);
1020  }
1021  $packet = HTMLSpecialChars($packet);
1022  print "<h4>Response(s):</h4>";
1023  print "URL: $URL<br><pre>$packet</pre></body></html>";
1024  } else {
1025  print "Couldn't get a packet!<br>$php_errormsg</body></html>";
1026  }
1027 }
1028 
1029 function stuff_xac($Fs, $Cu, $Cn, $Showtab, $fileit, $truncit, $showsplit) {
1030 
1031  global $dbh;
1032  $delim = (strpos($Fs,'?')=== false ? '?' : '&');
1033  $URL = "{$delim}member=$Cn&type=X&f=xxxx";
1034 
1035  $rqx = "$Fs$URL";
1036  list ($open_fail, $pipe_err, $packet) = get_data($rqx);
1037  if ($packet != '') {
1038 
1039 #
1040 # $switchfrom should be set
1041 # from ((cuadmin.flagset & $CU_PKT_SWITCH) == $CU_PKT_SWITCH)
1042 # but for now no one gets switch account info in the pkt so I'll skip
1043 # the call to the cuadmin table--
1044 
1045  $switchfrom = 'A';
1046 //$fd = @fopen ("$Fs$URL", "r");
1047 //if ($fd) {
1048 // while ($buff = fread ($fd, 1500)) {
1049 // $packet .= $buff;
1050 // }
1051  if ($fileit == "OK") {
1052  if ($truncit == "OK") {
1053  $fp = fopen("/tmp/packetdump.txt", "w");
1054  } else {
1055  $fp = fopen("/tmp/packetdump.txt", "a");
1056  }
1057  fwrite($fp, $packet);
1058  }
1059  $pkt = $packet;
1060  if ("$Showtab" != "") {
1061  $pkt = preg_replace("/\t/", "$Showtab", $pkt);
1062  }
1063  $pkt = HTMLSpecialChars($pkt);
1064  print "<br>URL: $URL<br><pre>$pkt</pre><br>";
1065 
1066 
1067  $pktpieces = array();
1068 
1069  $status = (preg_match("/<status>\n?(.*)<\/status>/s", $packet, $pktpieces) ? $pktpieces[1] : "");
1070  list($mem, $statcode, $statdesc) = explode("\t", $status);
1071  if (trim($mem) != $Cn) {
1072  return array(999, 1, "Unable to retrieve a packet");
1073  }
1074 
1075  # check the return status to decide what to do here
1076  # 101 New Data -- process packet, update pktstamp
1077  # 200 System Unavailable, No New Data -- return immediately
1078  # 001 Invalid Account Number -- return immediately
1079  # 999 Calling error or Invalid Parms -- return immediately
1080  #
1081  $errstat = 0;
1082  $u = date("U");
1083  switch ($statcode) {
1084  case 101:
1085  # Need to refresh the db data. Start parsing that packet...
1086  # find out what we have in the database...
1087 
1088  $sql = "select trim(accountnumber), trim(tomember),
1089  trim(accounttype), deposittype, description, misc1, id
1090  from culivetx where cu = '$Cu' and accountnumber = '$Cn'";
1091  if ($switchfrom == 'A') {
1092  $sql .= " and deposittype not in ('W','w')";
1093  }
1094 
1095  $sth = db_query($sql, $dbh);
1096  $db_keys = array();
1097  for ($row = 0; $rec = db_fetch_row($sth, $row); $row++) {
1098  $db_keys["$rec[0]\t$rec[1]\t$rec[2]\t$rec[3]"] = "$rec[4]\t$rec[5]\t$rec[6]";
1099  }
1100 
1101  # make an array from the stuff in the packet
1102 
1103  preg_match("/<txaccount>\n?(.*)<\/txaccount>/si", $packet, $pktpieces);
1104  $txaccount = explode("\n", $pktpieces[1]);
1105  array_pop($txaccount);
1106  $tx_keys = array();
1107 
1108  foreach ($txaccount as $line) {
1109  $rec = array();
1110  $rec = explode("\t", $line);
1111  $tx_keys["$rec[0]\t$rec[1]\t$rec[2]\t$rec[3]"] = "$rec[4]\t$rec[5]";
1112  }
1113 
1114  $inserts = array_diff(array_keys($tx_keys), array_keys($db_keys));
1115  $deletes = array_diff(array_keys($db_keys), array_keys($tx_keys));
1116  $updates = array_intersect(array_keys($tx_keys), array_keys($db_keys));
1117 
1118  if ($showsplit == 'OK') {
1119  $pktshow = array();
1120  $pktshow = $txaccount;
1121 
1122  print "Packet Contains:<br><pre>++++++++++++++++++++++++++++++++++++++++\n";
1123  array_walk($pktshow, 'showquoted');
1124  print_r($pktshow);
1125  print "++++++++++++++++++++++++++++++++++++++++\n</pre>";
1126  $pktshow = $db_keys;
1127  print "Database Has:<br><pre>++++++++++++++++++++++++++++++++++++++++\n";
1128  array_walk($pktshow, 'showquoted');
1129  print_r($pktshow);
1130  print "++++++++++++++++++++++++++++++++++++++++\n</pre>";
1131  $pktshow = $inserts;
1132  print "Insert keys: <br><pre>++++++++++++++++++++++++++++++++++++++++\n";
1133  array_walk($pktshow, 'showquoted');
1134  print_r($pktshow);
1135  $pktshow = $updates;
1136  print "Update keys: <br><pre>++++++++++++++++++++++++++++++++++++++++\n";
1137  array_walk($pktshow, 'showquoted');
1138  print_r($pktshow);
1139  print "++++++++++++++++++++++++++++++++++++++++\n</pre>";
1140  $pktshow = $deletes;
1141  print "Delete keys: <br><pre>++++++++++++++++++++++++++++++++++++++++\n";
1142  array_walk($pktshow, 'showquoted');
1143  print_r($pktshow);
1144  }
1145 
1146  $sql = "";
1147  foreach ($updates as $line) {
1148  $rec = explode("\t", $db_keys["$line"]);
1149  $trec = array_merge(explode("\t", $line), explode("\t", $tx_keys["$line"]));
1150 # don't update descriptions on switchacct records.
1151 # If the member changes the description we want it to 'stick'.
1152 
1153  if ($trec[3] != 'W' && $trec[3] != 'w' &&
1154  ($rec[0] != $trec[4] || $rec[1] != $trec[5])) {
1155  $sql .= "update culivetx set description = '" .
1156  prep_save($trec[4]) . "',
1157  misc1 = '" . prep_save($trec[5]) . "' where id = ${rec[2]};\n";
1158  }
1159  }
1160 
1161  foreach ($inserts as $line) {
1162  $rec = array_merge(explode("\t", $line), explode("\t", $tx_keys["$line"]));
1163 
1164 # switchfrom is A when switchaccts are maintained in admin. Only insert the
1165 # switchacct records coming from the core if that's where we expect to get 'em.
1166 
1167  if ($switchfrom != 'A' || ($rec[3] != 'W' && $rec[3] != 'w')) {
1168  $sql .= "insert into culivetx (cu, accountnumber, tomember,
1169  accounttype, deposittype, description, misc1) values (
1170  '$Cu','${rec[0]}','${rec[1]}', '${rec[2]}', '${rec[3]}',
1171  '" . prep_save($rec[4]) . "', '" . prep_save($rec[5]) . "');\n";
1172  }
1173  }
1174 
1175  foreach ($deletes as $line) {
1176  $rec = explode("\t", $db_keys["$line"]);
1177  $sql .= "delete from culivetx where id = ${rec[2]};\n";
1178  }
1179  if ($showsplit == 'OK') {
1180  print "SQL: <br>++++++++++++++++++++++++++++++++++++++++<br>";
1181  }
1182  if ($sql > '') {
1183  print "Will execute SQL <pre>$sql</pre>\n";
1184  } else {
1185  print "No SQL to execute\n";
1186  }
1187 
1188  if ($sql > '') {
1189  $sth = db_query($sql, $dbh);
1190  if (!($sth)) {
1191  $err = pg_last_error();
1192  $errstat++;
1193  syslog(LOG_ERR, "FAILED $err setting txaccounts for $Cu:$Cn\n$sql");
1194  }
1195  }
1196  db_free_result($sth);
1197 
1198  if ($errstat == 0) {
1199  return array("$statcode", "$u", "$statdesc");
1200  } else {
1201  return array("$statcode", 1, "Error loading member data. Please contact Credit Union");
1202  }
1203  break;
1204  case 200:
1205  case 001:
1206  case 999:
1207  return array("$statcode", 1, "$statdesc");
1208  }
1209  } else {
1210  return array(999, 1, $php_errormsg);
1211  }
1212 }
1213 
1214 function valid_xac($Fs, $Cu, $Cn, $Showtab, $fileit, $truncit, $showsplit) {
1215 
1216  global $dbh;
1217  $delim = (strpos($Fs,'?')=== false ? '?' : '&');
1218  $URL = "{$delim}member=$Cn&type=X&f=xxx";
1219  $rqx = "$Fs$URL";
1220  list ($open_fail, $pipe_err, $packet) = get_data($rqx);
1221  if ($packet != '') {
1222 
1223 
1224 // $fd = @fopen ("$Fs$URL", "r");
1225 //
1226 //if ($fd) {
1227 // while ($buff = fread ($fd, 1500)) {
1228 // $packet .= $buff;
1229 // }
1230  if ($fileit == "OK") {
1231  if ($truncit == "OK") {
1232  $fp = fopen("/tmp/packetdump.txt", "w");
1233  } else {
1234  $fp = fopen("/tmp/packetdump.txt", "a");
1235  }
1236  fwrite($fp, $packet);
1237  }
1238 
1239 
1240  $pktpieces = array();
1241 
1242  $status = (preg_match("/<status>\n?(.*)<\/status>/s", $packet, $pktpieces) ? $pktpieces[1] : "");
1243  list($mem, $statcode, $statdesc) = explode("\t", $status);
1244  if (trim($mem) != $Cn) {
1245  return array(999, 1, "Unable to retrieve a packet");
1246  }
1247 
1248  # check the return status to decide what to do here
1249  # 101 New Data -- process packet, update pktstamp
1250  # 200 System Unavailable, No New Data -- return immediately
1251  # 001 Invalid Account Number -- return immediately
1252  # 999 Calling error or Invalid Parms -- return immediately
1253  #
1254  $errstat = 0;
1255  $u = date("U");
1256  switch ($statcode) {
1257  case 101:
1258  # Need to refresh the db data. Start parsing that packet...
1259  # find out what we have in the database...
1260 
1261  $sql = "select trim(accountnumber), trim(tomember),
1262  trim(accounttype), deposittype, description, misc1, id
1263  from culivetx where cu = '$Cu' and accountnumber = '$Cn'";
1264  if ($switchfrom == 'A') {
1265  $sql .= " and deposittype not in ('W','w')";
1266  }
1267 
1268  $sth = db_query($sql, $dbh);
1269  $db_keys = array();
1270  for ($row = 0; $rec = db_fetch_row($sth, $row); $row++) {
1271  $db_keys["$rec[0]\t$rec[1]\t$rec[2]\t$rec[3]"] = "$rec[4]\t$rec[5]\t$rec[6]";
1272  }
1273 
1274  # make an array from the stuff in the packet
1275 
1276  preg_match("/<txaccount>\n?(.*)<\/txaccount>/si", $packet, $pktpieces);
1277  $txaccount = explode("\n", $pktpieces[1]);
1278  array_pop($txaccount);
1279  $tx_keys = array();
1280  $mincnt = 0;
1281  $maxcnt = 0;
1282 
1283  print "<pre>";
1284  foreach ($txaccount as $line) {
1285  $count = (count(explode("\t", $line)));
1286  $mincnt = (($count < $mincnt || $mincnt == 0) ? $count : $mincnt);
1287  $maxcnt = ($count > $maxcnt ? $count : $maxcnt);
1288  $rec = array();
1289  $rec = explode("\t", $line);
1290  $tx_keys["$rec[0]\t$rec[1]\t$rec[2]\t$rec[3]"] = "$rec[4]\t$rec[5]";
1291  if ("$Showtab" != "") {
1292  $line = preg_replace("/\t/", "$Showtab", $line);
1293  }
1294  $line = HTMLSpecialChars($line);
1295  print "$line\n";
1296  }
1297  print "</pre>";
1298 
1299  print "<font color='blue'>Counted " . sizeof($txaccount) . " Cross-Account records...(Expected: 6)</font> Min: $mincnt Max: $maxcnt<br><br>\n";
1300 
1301  $inserts = array_diff(array_keys($tx_keys), array_keys($db_keys));
1302  $deletes = array_diff(array_keys($db_keys), array_keys($tx_keys));
1303  $updates = array_intersect(array_keys($tx_keys), array_keys($db_keys));
1304 
1305  if ($showsplit == 'OK') {
1306  $pktshow = array();
1307  $pktshow = $txaccount;
1308 
1309  print "Packet Contains:<br><pre>++++++++++++++++++++++++++++++++++++++++\n";
1310  array_walk($pktshow, 'showquoted');
1311  print_r($pktshow);
1312  print "++++++++++++++++++++++++++++++++++++++++\n</pre>";
1313  $pktshow = $db_keys;
1314  print "Database Has:<br><pre>++++++++++++++++++++++++++++++++++++++++\n";
1315  array_walk($pktshow, 'showquoted');
1316  print_r($pktshow);
1317  print "++++++++++++++++++++++++++++++++++++++++\n</pre>";
1318  $pktshow = $inserts;
1319  print "Insert keys: <br><pre>++++++++++++++++++++++++++++++++++++++++\n";
1320  array_walk($pktshow, 'showquoted');
1321  print_r($pktshow);
1322  $pktshow = $updates;
1323  print "Update keys: <br><pre>++++++++++++++++++++++++++++++++++++++++\n";
1324  array_walk($pktshow, 'showquoted');
1325  print_r($pktshow);
1326  print "++++++++++++++++++++++++++++++++++++++++\n</pre>";
1327  $pktshow = $deletes;
1328  print "Delete keys: <br><pre>++++++++++++++++++++++++++++++++++++++++\n";
1329  array_walk($pktshow, 'showquoted');
1330  print_r($pktshow);
1331  }
1332 
1333  $sql = "";
1334  foreach ($updates as $line) {
1335  $rec = explode("\t", $db_keys["$line"]);
1336  $trec = array_merge(explode("\t", $line), explode("\t", $tx_keys["$line"]));
1337 # don't update descriptions on switchacct records.
1338 # If the member changes the description we want it to 'stick'.
1339 
1340  if ($trec[3] != 'W' && $trec[3] != 'w' &&
1341  ($rec[0] != $trec[4] || $rec[1] != $trec[5])) {
1342  $sql .= "update culivetx set description = '" .
1343  prep_save($trec[4]) . "',
1344  misc1 = '${trec[5]}' where id = ${rec[2]};\n";
1345  }
1346  }
1347 
1348  foreach ($inserts as $line) {
1349  $rec = array_merge(explode("\t", $line), explode("\t", $tx_keys["$line"]));
1350 
1351 # switchfrom is A when switchaccts are maintained in admin. Only insert the
1352 # switchacct records coming from the core if that's where we expect to get 'em.
1353 
1354  if ($switchfrom != 'A' || ($rec[3] != 'W' && $rec[3] != 'w')) {
1355  $sql .= "insert into culivetx (cu, accountnumber, tomember,
1356  accounttype, deposittype, description, misc1) values (
1357  '$Cu','${rec[0]}','${rec[1]}', '${rec[2]}', '${rec[3]}',
1358  '" . prep_save($rec[4]) . "', '${rec[5]}');\n";
1359  }
1360  }
1361 
1362  foreach ($deletes as $line) {
1363  $rec = explode("\t", $db_keys["$line"]);
1364  $sql .= "delete from culivetx where id = ${rec[2]};\n";
1365  }
1366  if ($showsplit == 'OK') {
1367  print "SQL: <br>++++++++++++++++++++++++++++++++++++++++<br>";
1368  if ($sql > '') {
1369  print "$sql\n";
1370  } else {
1371  print "No SQL\n";
1372  }
1373  }
1374 
1375  if ($errstat == 0) {
1376  return array("$statcode", "$u", "$statdesc");
1377  } else {
1378  return array("$statcode", 1, "Error loading member data. Please contact Credit Union");
1379  }
1380  break;
1381  case 200:
1382  case 001:
1383  case 999:
1384  return array("$statcode", 1, "$statdesc");
1385  }
1386  } else {
1387  return array(999, 1, $php_errormsg);
1388  }
1389 }
1390 
1391 function valid_mir($Fs, $Cu, $Cn, $Showtab, $fileit, $truncit, $showsplit) {
1392 
1393  global $dbh;
1394  $delim = (strpos($Fs,'?')=== false ? '?' : '&');
1395  $URL = "{$delim}member=$Cn&type=MIR&f=xxx";
1396  $rqx = "$Fs$URL";
1397  list ($open_fail, $pipe_err, $packet) = get_data($rqx);
1398  if ($packet != '') {
1399 
1400 // $fd = @fopen ("$Fs$URL", "r");
1401 //
1402 //if ($fd) {
1403 // while ($buff = fread ($fd, 1500)) {
1404 // $packet .= $buff;
1405 // }
1406  if ($fileit == "OK") {
1407  if ($truncit == "OK") {
1408  $fp = fopen("/tmp/packetdump.txt", "w");
1409  } else {
1410  $fp = fopen("/tmp/packetdump.txt", "a");
1411  }
1412  fwrite($fp, $packet);
1413  }
1414 
1415 
1416  $pktpieces = array();
1417 
1418  $status = (preg_match("/<status>\n?(.*)<\/status>/s", $packet, $pktpieces) ? $pktpieces[1] : "");
1419  list($mem, $statcode, $statdesc) = explode("\t", $status);
1420  if (trim($mem) != $Cn) {
1421  return array(999, 1, "Unable to retrieve a packet");
1422  }
1423 
1424  # check the return status to decide what to do here
1425  # 101 New Data -- process packet, update pktstamp
1426  # 200 System Unavailable, No New Data -- return immediately
1427  # 001 Invalid Account Number -- return immediately
1428  # 999 Calling error or Invalid Parms -- return immediately
1429  #
1430  $errstat = 0;
1431  $u = date("U");
1432  switch ($statcode) {
1433  case 101:
1434  # New data. Start parsing that packet...
1435  # make an array from the stuff in the packet
1436 
1437  preg_match("/<info>\n?(.*)<\/info>/si", $packet, $pktpieces);
1438  $mir = explode("\n", $pktpieces[1]);
1439  array_pop($mir);
1440  $mincnt = 0;
1441  $maxcnt = 0;
1442 
1443  print "<pre>";
1444  foreach ($mir as $line) {
1445  $count = (count(explode("\t", $line)));
1446  $mincnt = (($count < $mincnt || $mincnt == 0) ? $count : $mincnt);
1447  $maxcnt = ($count > $maxcnt ? $count : $maxcnt);
1448  if ("$Showtab" != "") {
1449  $line = preg_replace("/\t/", "$Showtab", $line);
1450  }
1451  $line = HTMLSpecialChars($line);
1452  print "$line\n";
1453  }
1454  print "</pre>";
1455 
1456  print "<font color='blue'>Counted " . sizeof($mir) . " Member Info records...<br>(Expected 17: AccountNumber, FirstName, MiddleName, LastName, Email, HomePhone, WorkPhone, CellPhone, Fax, SSN, Address1, Address2, City, State, Zip, CC, DOB)</font><br> Min: $mincnt Max: $maxcnt<br><br>\n";
1457 
1458  break;
1459  case 200:
1460  case 001:
1461  case 999:
1462  return array("$statcode", 1, "$statdesc");
1463  }
1464  } else {
1465  return array(999, 1, $php_errormsg);
1466  }
1467 }
1468 
1469 function post_packet($Fs, $Cn, $Tc, $R1, $R2, $R3, $R4, $R5, $Amt) {
1470  global $Fs;
1471 
1472  $delim = (strpos($Fs,'?')=== false ? '?' : '&');
1473  $URL = "{$delim}member=$Cn&type=T&tran_code=$Tc&ref1=$R1&ref2=$R2&ref3=$R3&ref4=$R4&ref5=$R5&amount=$Amt";
1474  $rqx = "$Fs$URL";
1475  list ($open_fail, $pipe_err, $packet) = get_data($rqx);
1476  if ($packet != '') {
1477 
1478 //$fd = @fopen ("$Fs$URL", "r");
1479 //
1480 //if ($fd) {
1481 // while ($buff = fread ($fd, 1500)) {
1482 // $packet .= $buff;
1483 // }
1484 
1485  $packet = HTMLSpecialChars($packet);
1486  print "<h4>Response(s):</h4>";
1487  print "<pre>$packet</pre></body></html>";
1488  } else {
1489  print "Couldn't get a packet!<br>$php_errormsg<br>";
1490  print "URL=$Fs?member=$Cn&type=T&tran_code=$Tc&ref1=$R1&ref2=$R2&ref3=$R3&ref4=$R4&ref5=$R5&amount=$Amt<br>";
1491  }
1492 }
1493 
1494 function showquoted(&$val) {
1495  if (is_array($val)) {
1496  array_walk($val, 'showquoted');
1497  return;
1498  }
1499  $val = htmlentities($val);
1500 }
1501 function get_data($rqx) {
1502 # returns open_fail as 0 no error or 1 fopen/popen failed
1503 # pipe_err as termination code from piped command (always zero if fopen?)
1504 # packet as data returned from core
1505 
1506  global $fetcher;
1507  global $Cu;
1508  global $Cn;
1509 
1510  $open_fail=0;
1511  $pipe_err=0;
1512  $packet="";
1513 
1514  $pipe=1; # force use of popen but allow for easy recoding
1515 
1516  if ($pipe) {
1517  // * Force -1 tlsv1 sslv3 for the packet test -- Was first needed for MEDFED, mostly this is needed for 10.04 install on www6
1518  $fd=popen("/usr/bin/curl -k -N --silent -1 '$rqx'","r");
1519  if (!$fd) {
1520  $open_fail=1;
1521  } else {
1522  while ($buff = fread ($fd, 1500)) {
1523  $packet .= $buff;
1524  }
1525  $pipe_err=pclose($fd);
1526  }
1527 // $fp=@fopen("/tmp/catchpkt.txt", "w");
1528 // @fwrite($fp,"$Cu:$Cn /usr/bin/curl -k -N --silent $opts '$rqx'\n");
1529 // @fwrite($fp,"open_fail $open_fail pipe_err $pipe_err\n");
1530 // @fwrite($fp,"$packet\n");
1531 // @fclose($fp);
1532  return array($open_fail,$pipe_err,$packet);
1533  } else {
1534  $fd = @fopen("$rqx", "r");
1535  if (!$fd) {
1536  $open_fail=1;
1537  } else {
1538  $pkti = stream_get_meta_data($fd);
1539  while ((!feof($fd)) && (!$pkti['timed_out'])) {
1540  $packet .= fread($fd, 4096);
1541  $pkti = stream_get_meta_data($fd);
1542  }
1543  fclose($fd);
1544  if ($pkti['timed_out']) {
1545  $open_fail=1;
1546  $packet = "";
1547  }
1548  return array($open_fail,$pipe_err,$packet);
1549  }
1550  }
1551 }
1552 function fetch_estatement($HB_ENV, $Command, $eStatementID = '') {
1553 
1554  $FETCHER = $GLOBALS['Fs'];
1555 
1556  $estmt = array();
1557  try {
1558  # throw error 999 missing / invalid parameters if:
1559  # missing $HB_ENV['Cn'] or $HB_ENV['Cu']
1560  # Command not in (ETOC, ESTM)
1561  # Command == ESTM and empty(eStatementID)
1562 
1563  if (empty($HB_ENV['Cn']) || empty($HB_ENV['Cu']) ||
1564  ($Command == 'ESTM' && empty($eStatementID))) {
1565  throw new Exception('Missing Parameters', '999');
1566  }
1567 
1568  if ($Command != 'ESTM' && $Command != 'ETOC') {
1569  throw new Exception('Invalid Command', '999');
1570  }
1571 
1572  $delim = (strpos($FETCHER, '?') === false ? '?' : '&');
1573  $Ml = (trim($HB_ENV['Ml']) == "" || $HB_ENV['Ml'] == "no\@email.com" ? "NULL" : urlencode(trim($HB_ENV['Ml'])));
1574 
1575  # https://208.53.36.134/hculive/hculive2me.mp?member=555&type=ETOC&ref3=nobody@nowhere.com
1576  # https://208.53.36.134/hculive/hculive2me.mp?member=555&type=ESTM&ref1=2015-02&ref3=nobody@nowhere.com
1577  $rqx = "{$FETCHER}{$delim}member={$HB_ENV['Cn']}&type=$Command&f=xxxx";
1578  if ($Command == 'ESTM')
1579  $rqx .= "&ref1={$eStatementID}";
1580  $rqx .= "&ref3=" . urlencode($HB_ENV['Ml']) . "&tauth={$HB_ENV['Cn']}";
1581 
1582  list ($open_fail, $pipe_err, $packet) = get_data($rqx);
1583 
1584  if (trim("$packet") == "") {
1585  if ($open_fail == 1) {
1586  trigger_error("{$HB_ENV['Cu']}:{$HB_ENV['Cn']} Estmt open failed", E_USER_ERROR);
1587  } else {
1588  trigger_error("{$HB_ENV['Cu']}:{$HB_ENV['Cn']} Estmt empty packet response (pipe_err $pipe_err)", E_USER_ERROR);
1589  }
1590  throw new Exception('Unable to retrieve data', '999');
1591  }
1592 
1593 // if ($Command == 'ETOC') {
1594 // $fp = @fopen("/tmp/stmntpkt.txt", "a");
1595 // @fwrite($fp, "{$HB_ENV['Cu']}:{$HB_ENV['Cn']}\n${rqx}\n");
1596 // @fwrite($fp, "$packet\n");
1597 // @fwrite($fp, "++++++++++++++++++++++\n");
1598 // @fclose($fp);
1599 // }
1600  checkForXmlParseErrors($packet);
1601  $pktpieces = array();
1602 
1603  # find something besides preg_match -- won't work with large data
1604  # do the preg_match on just the first 2k - <status> and <member> should
1605  # always be in the beginning of the packet
1606 
1607  $pktheader = substr($packet,0,2048);
1608  $status = (preg_match("/<status>\n?(.*)\n?<\/status>/si", $pktheader, $pktpieces) ? $pktpieces[1] : "");
1609  $member = (preg_match("/<member>\n?(.*)\n?<\/member>/si", $pktheader, $pktpieces) ? $pktpieces[1] : "");
1610  list($mem, $statcode, $statdesc) = explode("\t", $status);
1611  if ("$member" > "") {
1612  list($mem) = explode("\t", $member);
1613  }
1614  if (trim("$mem") != $HB_ENV['Cn']) {
1615  trigger_error("{$HB_ENV['Cu']} possibly out-of-sync ($mem:{$HB_ENV['Cn']})", E_USER_ERROR);
1616  throw new Exception('Unable to retrieve data', '999');
1617  }
1618 
1619  # check the return status to decide what to do here
1620  # 101 New Data -- process packet
1621  # 201 System Unavailable, New Data -- can't use stale for EStatement
1622  # -- change to 200 and return
1623  # 100 No New Data -- return immediately
1624  # 200 System Unavailable, No New Data -- return immediately
1625  # 202 System Unavailable, No Data -- return immediately
1626  # 001 Invalid Account Number -- return immediately
1627  # 002 Invalid Password -- change to 001 Invalid Account and return
1628  # 003 Closed Account -- change to 001 Invalid Account and return
1629  # 010 Member not enrolled for E-statements
1630  # 012 Statement not found
1631  # 999 Calling error or Invalid Parms -- return immediately
1632  #
1633  $errstat = 0;
1634  switch (sprintf('%03d',$statcode)) {
1635  case 101:
1636  # New data. Start parsing that packet...
1637  if ($Command == 'ETOC') {
1638  $stPtrn = '<EStatementTOC>';
1639  $ePtrn = '</EStatementTOC>';
1640  } elseif ($Command == 'ESTM') {
1641  $stPtrn = '<EStatement>';
1642  $ePtrn = '</EStatement>';
1643  } else {
1644  # probably redundant since we checked $Command above....
1645  throw new Exception('Invalid Command', '999');
1646  }
1647 
1648  if (stripos($packet, $stPtrn) === false ||
1649  stripos($packet, $ePtrn, stripos($packet, $stPtrn)) === false) {
1650  trigger_error("{$HB_ENV['Cu']}:{$HB_ENV['$Cn']} EStatement short packet response(pipe_err $pipe_err)", E_USER_ERROR);
1651  throw new Exception("$Command Unable to retrieve data", '999');
1652  }
1653 
1654  /*
1655  * This '101' is the only "successful" statcode for this call
1656  * return '000' instead
1657  * $estmt[status][status] = $statcode;
1658  */
1659  $estmt[status][status] = "000";
1660  $estmt[status][asofdate] = date("D M j Y H:i:s T");
1661  $estmt[status][reason] = $statdesc;
1662 
1663  if ($Command == 'ETOC') {
1664  $tblist = array("TOC");
1665  foreach ($tblist as $tbl) {
1666  # find a different way to do this, as large data will foul it up
1667  preg_match("/<$tbl>\n?(.*)<\/$tbl>/si", $packet, $pktpieces);
1668  $$tbl = explode("\n", $pktpieces[1]);
1669  array_pop($$tbl);
1670 
1671  if (sizeof($$tbl)) {
1672  $tblidx = 0;
1673 # format and return array ala new cu_data functions
1674  foreach ($$tbl as $line) {
1675  $line = preg_replace("/ ?\t ?/", "\t", $line);
1676  $line = preg_replace("/ $/", "", $line);
1677  $line = preg_replace("/\\0/", " ", $line);
1678  list($Member, $ID, $sType, $PeriodEnd, $Description, $Format) = explode("\t", $line);
1679 
1680  $estmt[$tbl][$tblidx]['Member'] = $Member;
1681  $estmt[$tbl][$tblidx]['ID'] = $ID;
1682  $estmt[$tbl][$tblidx]['Type'] = $sType;
1683  $estmt[$tbl][$tblidx]['PeriodEnd'] = $PeriodEnd;
1684  $estmt[$tbl][$tblidx]['Description'] = $Description;
1685  $estmt[$tbl][$tblidx]['Format'] = $Format;
1686  $tblidx++;
1687  }
1688  }
1689  }
1690  } else {
1691  # presumably ESTM - parse <member> into TOC and pop off <data>...</data>
1692  $start = stripos($packet,"<MEMBER>");
1693  $end = stripos($packet,"</MEMBER>");
1694  if($start === false || $end === false) {
1695  $estmt['TOC']=array();
1696  } else {
1697  $start += 8; # move past the <MEMBER> tag
1698  $line = substr($packet,$start,$end-$start);
1699  $line = trim($line);
1700  $line = preg_replace("/ ?\t ?/", "\t", $line);
1701  $line = preg_replace("/ $/", "", $line);
1702  $line = preg_replace("/\\0/", " ", $line);
1703  list($Member, $ID, $sType, $PeriodEnd, $Description, $Format) = explode("\t", $line);
1704 
1705  $estmt[TOC][0]['Member'] = $Member;
1706  $estmt[TOC][0]['ID'] = $ID;
1707  $estmt[TOC][0]['Type'] = $sType;
1708  $estmt[TOC][0]['PeriodEnd'] = $PeriodEnd;
1709  $estmt[TOC][0]['Description'] = $Description;
1710  $estmt[TOC][0]['Format'] = $Format;
1711 
1712  }
1713  $start = stripos($packet,"<DATA>");
1714  $end = strripos($packet,"</DATA>");
1715  if($start === false || $end === false) {
1716  $estmt['data']='';
1717  } else {
1718  $start += 6; # move past the <DATA> tag
1719  $estmt['data'] = substr($packet,$start,$end-$start);
1720  }
1721  }
1722  break;
1723 
1724  # 001 Invalid Account Number
1725  # 002 Invalid Password -- change to 001 Invalid Account
1726  # 003 Closed Account -- change to 001 Invalid Account
1727  case 002:
1728  case 003:
1729  case 001:
1730  throw new Exception('INVALID ACCOUNT NUMBER', '001');
1731  break;
1732 
1733  case 201:
1734  # 201 System Unavailable, New Data -- can't use stale for EStatement
1735  throw new Exception('Unusable Stale Data', $statcode);
1736  break;
1737 
1738  # 100 No New Data
1739  # 200 System Unavailable, No New Data
1740  # 202 System Unavailable, No Data
1741  # 010 Member not enrolled for E-statements
1742  # 012 Statement not found
1743  # 999 Calling error or Invalid Parms
1744  case 100:
1745  case 200:
1746  case 202:
1747  case 010:
1748  case 012:
1749  case 999:
1750  default:
1751  throw new Exception($statdesc, $statcode);
1752  break;
1753  }
1754  } catch (Exception $e) {
1755  $code = $e->getCode();
1756  $reason = $e->getMessage();
1757 
1758  $estmt[status][status] = sprintf('%03d',$code);
1759  $estmt[status][asofdate] = date("D M j Y H:i:s T");
1760  $estmt[status][reason] = $reason;
1761  }
1762  return $estmt;
1763 }
1764 ?>