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