Odyssey
cudppacket.prg
1 <?php
2 $monLibrary= dirname(__FILE__) . "/../library";
3 require_once("$monLibrary/cu_top.i");
4 require_once("$monLibrary/ck_hticket.i");
5 
6 
7  $dms_ok=array('Hu'=>'string', 'Cn'=>'string', 'Pass'=>'string',
8  'Showtab'=>'string', 'Clw'=>'string',
9  'pktstamp'=>'string', 'cutoff'=>'string',
10  '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  $SYSENV = LoadSystemEnv('admin'); // From hcuEnv.i
17  require("db." . $SYSENV['db']['platform'] . ".i");
18 
19  $dbh = db_pconnect($SYSENV['db']); // Only call this once for admin
20  if (!CheckPerm($dbh, $Hu, basename($_SERVER['SCRIPT_NAME']), $_SERVER['REMOTE_ADDR'])) {
21  // ** Permissions failed
22  // ** redirect to new page
23  header("Location: /hcuadm/hcu_noperm.prg");
24  exit;
25  }
26 
27 #error_reporting (E_ALL);
28 error_reporting (E_ERROR | E_WARNING | E_PARSE);
29 echo "<html><LINK REL=stylesheet HREF='/monitor/css/cudp.css' TYPE='text/css'>";
30 $action = (empty($action) ? "Show" : $action);
31 $thirty = mktime() - (30*86400);
32 $thirty = date("Ymd",$thirty);
33 
34 #define_syslog_variables();
35 #syslog(LOG_ERR,"Packetdump action is $action");
36 $host = substr($_SERVER['SERVER_NAME'], 0,strpos($_SERVER['SERVER_NAME'], "."));
37 
38 switch ($action) {
39 case "Show":
40 # show forms for fetch/post
41 $sql = "select cu, liveserver, histdays
42  from cuadmin
43  where liveserver > ''
44  and vendor = 'CUPRODIGY'
45  and cu <> 'MIKI'
46  order by cu";
47  $sth = db_query($sql,$dbh);
48  $options = "<select name=Fs>\n";
49  for ($row=0;list($cu, $fetcher, $histdays) = db_fetch_array($sth,$row); $row++) {
50  $cu = trim($cu);
51  $options .= "<OPTION VALUE='$cu|$fetcher|$histdays'>$cu</OPTION>\n";
52  }
53  $options .= "</select>\n";
54  db_free_result($sth);
55 
56 print <<<EOF
57  <head><title>Packet Dump</title></head><body bgcolor=#FFFFFF>
58  <div align='center' class='usu'>CUDP Live Packet Tester</div>
59  <table width=98% cellpadding=2><tr>
60  <td valign=top align=center width=34%>
61  <form action="$PHP_SELF?action='fetch'">
62  <table cellpadding=2>
63  <tr><td class='bar' colspan=2 align=center>Fetch a Packet</td></tr>
64  <tr><td class='dtl' align="right">Fetcher: </td>
65  <td>$options</td></tr>
66  <tr><td class='dtl' align="right">Member: </td>
67  <td><input type=text name="Cn" size=10></td></tr>
68  <tr><td class='dtl' align="right">Password: </td>
69  <td><input type=password name="Pass" size=10 MAXLENGTH="8"></td></tr>
70  <tr><td class='dtl' align="right">Show Tabs as: </td>
71  <td><input type=text name="Showtab" size=10></td></tr>
72  <tr><td class='dtl' align="right">Query Delay: </td>
73  <td><input type=text name="Clw" size=10 value=1></td></tr>
74  <tr><td class='dtl' align="right">Last Packet Stamp: </td>
75  <td><input type=text name="pktstamp" size=10 value=1></td>
76  </tr>
77  <tr><td class='dtl' align="right">Earliest Date: </td>
78  <td><input type=text name="cutoff" size=10 value='$thirty'></td>
79  <tr><td class='dtl' align="right">Count Columns: </td>
80  <td><input type=checkbox name="validresp" value="OK"></td>
81  </tr>
82  <tr><td colspan=2 align="center">
83  <input type=submit name="action" value="Fetch" class='bar'>
84  </td></tr>
85  </table>
86  </form>
87  </td><td valign=top align=center width=33%>
88  <form action="$PHP_SELF?action='retrieve'">
89  <table cellpadding=2>
90  <tr><td class='bar' colspan=2 align=center>Cross-Accounts</td></tr>
91  <tr><td class='dtl' align="right">Fetcher: </td>
92  <td>$options</td></tr>
93  <tr><td class='dtl' align="right">Member: </td>
94  <td><input type=text name="Cn" size=10></td></tr>
95  <tr><td class='dtl' align="right">Show Tabs as: </td>
96  <td><input type=text name="Showtab" size=10></td></tr>
97  <tr><td class='dtl' align="right">Count Columns: </td>
98  <td><input type=checkbox name="validresp" value="OK"></td>
99  </tr>
100  <tr><td colspan=2 align="center">
101  <input type=submit name="action" value="Retrieve" class='bar'></td></tr>
102  </table>
103  </form>
104  </td><td valign=top align=center width=*>
105  <form action="$PHP_SELF?action='post'">
106  <table cellpadding=2>
107  <tr><td class='bar' colspan=2 align=center>Post a Packet</td></tr>
108  <tr><td class='dtl' align="right">Fetcher: </td>
109  <td>$options</td></tr>
110  <tr><td class='dtl' align="right">Member: </td>
111  <td><input type=text name="Cn" size=10></td></tr>
112  <tr><td class='dtl' align="right">Transaction Code: </td>
113  <td><select name="Tc">
114  <option>AT</option>
115  <option>LP</option>
116  <option>LA</option>
117  <option>CW</option>
118  <option>CP</option>
119  <option>MD</option>
120  <option>MA</option>
121  <option>ES</option>
122  <option>ED</option>
123  </select>
124  </td></tr>
125  <tr><td class='dtl' align="right">Ref 1 (From Sfx): </td>
126  <td><input type=text name="R1" size=10 value=1></td></tr>
127  <tr><td class='dtl' align="right">Ref 2 (To Sfx): </td>
128  <td><input type=text name="R2" size=10 value=1></td></tr>
129  <tr><td class='dtl' align="right">Ref 3 (email): </td>
130  <td><input type=text name="R3" size=10 value=1></td></tr>
131  <tr><td class='dtl' align="right">Ref 4 (To misc): </td>
132  <td><input type=text name="R4" size=10 value=1></td></tr>
133  <tr><td class='dtl' align="right">Ref 5 (To Mem): </td>
134  <td><input type=text name="R5" size=10 value=1></td></tr>
135  <tr><td class='dtl' align="right">Amount: </td>
136  <td><input type=text name="Amt" size=10></td></tr>
137  <tr><td colspan=2 align="center">
138  <input type=submit name="action" value="Post" class='bar'></td></tr>
139  </table>
140  </form>
141  </td>
142  </tr></table></body></html>
143 EOF;
144 exit;
145 case "Fetch":
146 # check parameters
147 # get a packet
148 list($Cu,$Fs, $histdays) = explode("|","$Fs",3);
149 if (empty($cutoff)) { $cutoff = '$thirty'; }
150 if (empty($histdays)) { $histdays = 0; }
151 print "<html><head><title>Packet Fetch</title></head><body bgcolor=#FFFFFF>
152  Fetching packet using parameters<ul>
153  <li>Credit Union: $Cu</li>
154  <li>Member: $Cn</li>
155  <li>Password: $Pass</li>
156  <li>Show Tabs: $Showtab</li>
157  <li>Fetched from: $Fs</li>
158  <li>URL: $URL</li>
159  <li>Wait between calls: $Clw</li>
160  <li>Last Packet Stamp: $pktstamp</li>
161  <li>Requested Cutoff: $cutoff</li>
162  </ul><br>";
163 if ($validresp == "OK") {
164  valid_packet($Fs, $Cu, $Cn, $Clw, $pktstamp, $Showtab, $Pass, $cutoff, $histdays);
165 } else {
166  fetch_packet($Fs, $Cu, $Cn, $Clw, $pktstamp, $Showtab, $Pass, $cutoff, $histdays);
167 }
168 exit;
169 break;
170 case "Retrieve":
171 # check parameters
172 # get a packet
173 list($Cu,$Fs, $histdays) = explode("|","$Fs",3);
174 print "<html><head><title>Cross-Account Retrieve</title></head><body bgcolor=#FFFFFF>
175  Retrieving Cross-Account Authorizations using parameters<ul>
176  <li>Credit Union: $Cu</li>
177  <li>Member: $Cn</li>
178  <li>Show Tabs: $Showtab</li>
179  <li>Fetched from: $Fs</li>
180  </ul><br>";
181 if ($validresp == "OK") {
182  valid_xac($Fs, $Cu, $Cn, $Showtab);
183 } else {
184  fetch_xac($Fs, $Cu, $Cn, $Showtab);
185 }
186 exit;
187 break;
188 case "Post":
189 # check parameters
190 # post a packet
191 list($Cu,$Fs,$histdays) = explode("|","$Fs",3);
192 $R2 = (trim($R2) == "" ? "NULL" : $R2);
193 $R3 = (trim($R3) == "" ? "NULL" : $R3);
194 $R4 = (trim($R4) == "" ? "NULL" : $R4);
195 $R5 = (trim($R5) == "" ? "NULL" : $R5);
196 print "<html><head><title>Packet Post</title></head><body bgcolor=#FFFFFF>
197  Posting packet using parameters<ul>
198  <li>Credit Union: $Cu</li>
199  <li>Member: $Cn</li>
200  <li>Posted to: $Fs</li>
201  <li>Txn Code: $Tc</li>
202  <li>Ref1: $R1</li>
203  <li>Ref2: $R2</li>
204  <li>Ref3: $R3</li>
205  <li>Ref4: $R4</li>
206  <li>Ref5: $R5</li>
207  <li>Amount: $Amt</li>
208  <li>URL: $Fs?member=$Cn&type=T&tran_code=$Tc&ref1=$R1&ref2=$R2&ref3=$R3&ref4=$R4&ref5=$R5&amount=$Amt</li>
209  </ul><br>";
210 post_packet($Fs, $Cn, $Tc, $R1, $R2, $R3, $R4, $R5, $Amt);
211 exit;
212 break;
213 }
214 
215 
216 function fetch_packet($Fs, $Cu, $Cn, $Clw, $pktstamp, $Showtab, $Pass, $cutoff, $histdays) {
217 
218 if ("$pktstamp"=="") {
219 # read last pktstamp from database. Calculate new threshold as
220 # now - pktinterval; if pktstamp > threshold, return (use existing)
221 #
222 $sql = "select pktstamp, pktdate, livewait
223  from cuusers u, cuadmin a
224  where a.cu = u.cu
225  and a.cu = '" . $Cu . "' and u.user_name = '" . $Cn . "'";
226 $sth = db_query($sql,$dbh);
227 list($pktstamp, $pktdate,$Clw) = db_fetch_array($sth,0);
228 db_free_result($sth);
229 }
230 
231 $pktstamp = (empty($pktstamp) ? 1 : $pktstamp);
232 $pktdate = (empty($pktdate) ? date("D M j Y H:i:s T") : $pktdate);
233 $Clw = (($Clw == 0 || is_null($Clw)) ? 300 : $Clw);
234 
235 $threshold = mktime() - $Clw;
236 $fs = date("D M j Y H:i:s T",$pktstamp);
237 $ft = date("D M j Y H:i:s T",$threshold);
238 if ($pktstamp > $threshold) {
239  print "Too soon to ask again<br>
240  Last Stamp: $fs<br>
241  Threshold: $ft<br></body></html>";
242 } else {
243 
244  # if the last packet was fairly recent only fetch 7 days
245  # "recent" defined as 3 times the packet timeout setting
246  $threshold = mktime() - (3 * $Clw);
247  if (empty($cutoff)) { $cutoff = '$thirty'; }
248  if (empty($histdays)) { $histdays = 0; }
249 # 1072940400 is the timestamp for 20040101
250  if ($pktstamp >1072940400 && $histdays > 0 && $cutoff == $thirty) {
251  # calculate cutoff as pktstamp - interval($histdays days)
252  if ($pktstamp > $threshold) {
253  # last packet age is less than 3 times the packet timeout setting
254  # set cutoff for 7 days
255  $cutoff = date("Ymd",mktime()-604800);
256  } else {
257  $cutoff = date("Ymd",$pktstamp-(86400 * $histdays));
258  }
259  }
260  $URL="?member=$Cn&type=I&if_mod_since=$pktstamp&cutoff=$cutoff";
261  if (!empty($Pass)) {
262  $URL.="&pass=$Pass";
263  }
264  $fd = @fopen ("$Fs$URL", "r");
265 
266 if ($fd) {
267  while ($buff = fread ($fd, 1500)) {
268  $packet .= $buff;
269  }
270 #
271 # add feature here to log (and optionally truncate first) packet to a temp file
272 #
273 # $packet = preg_replace("/ ?\t ?/","\t",$packet);
274  if ("$Showtab" != "") { $packet = preg_replace("/\t/","$Showtab",$packet);}
275  $packet = HTMLSpecialChars($packet);
276  print "<br>URL: $URL<br><pre>$packet</pre></body></html>";
277 } else {
278  print "Couldn't get a packet!<br>$php_errormsg</body></html>";
279 }
280 }
281 }
282 
283 #
284 function fetch_xac($Fs, $Cu, $Cn, $Showtab) {
285 
286  $URL="?member=$Cn&type=X";
287 $URL .= "&f=x";
288  $fd = @fopen ("$Fs$URL", "r");
289 
290 if ($fd) {
291  while ($buff = fread ($fd, 1500)) {
292  $packet .= $buff;
293  }
294 #
295 # add feature here to log (and optionally truncate first) packet to a temp file
296 #
297 
298  if ("$Showtab" != "") { $packet = preg_replace("/\t/","$Showtab",$packet);}
299  $packet = HTMLSpecialChars($packet);
300  print "<br>URL: $URL<br><pre>$packet</pre></body></html>";
301 } else {
302  print "Couldn't get a packet!<br>$php_errormsg</body></html>";
303 }
304 }
305 
306 function post_packet($Fs, $Cn, $Tc, $R1, $R2, $R3, $R4, $R5, $Amt) {
307 global $Fs;
308 
309 $URL = "?member=$Cn&type=T&tran_code=$Tc&ref1=$R1&ref2=$R2&ref3=$R3&ref4=$R4&ref5=$R5&amount=$Amt";
310 $fd = @fopen ("$Fs$URL", "r");
311 
312 if ($fd) {
313  while ($buff = fread ($fd, 1500)) {
314  $packet .= $buff;
315  }
316 
317  $packet = HTMLSpecialChars($packet);
318  print "<pre>$packet</pre></body></html>";
319 
320 } else {
321  print "Couldn't get a packet!<br>$php_errormsg<br>";
322  print "URL=$Fs?member=$Cn&type=T&tran_code=$Tc&ref1=$R1&ref2=$R2&ref3=$R3&ref4=$R4&ref5=$R5&amount=$Amt<br>";
323 }
324 }
325 
326 function showquoted(&$val) {
327  if (is_array($val)) {
328  array_walk($val, 'showquoted');
329  return;
330  }
331  $val = htmlentities($val);
332 }
333 
334 function valid_packet ($Fs, $Cu, $Cn, $Clw, $pktstamp, $Showtab, $Pass, $cutoff, $histdays) {
335 global $dbh;
336 
337 if ("$pktstamp"=="") {
338 # read last pktstamp from database. Calculate new threshold as
339 # now - pktinterval; if pktstamp > threshold, return (use existing)
340 #
341 $sql = "select pktstamp, pktdate, livewait
342  from cuusers u, cuadmin a
343  where a.cu = u.cu
344  and a.cu = '" . $Cu . "' and u.user_name = '" . $Cn . "'";
345 $sth = db_query($sql,$dbh);
346 list($pktstamp, $pktdate,$Clw) = db_fetch_array($sth,0);
347 db_free_result($sth);
348 }
349 
350 $pktstamp = (empty($pktstamp) ? 1 : $pktstamp);
351 $pktdate = (empty($pktdate) ? date("D M j Y H:i:s T") : $pktdate);
352 $Clw = (($Clw == 0 || is_null($Clw)) ? 300 : $Clw);
353 
354 $threshold = mktime() - $Clw;
355 $rmt = mktime();
356 $fs = date("D M j Y H:i:s T",$pktstamp);
357 $ft = date("D M j Y H:i:s T",$threshold);
358 if ($pktstamp > $threshold) {
359  print "Too soon to ask again<br>
360  Last Stamp: $fs<br>
361  Threshold: $ft<br></body></html>";
362 } else {
363  # if the last packet was fairly recent only fetch 7 days
364  # "recent" defined as 3 times the packet timeout setting
365  $threshold = mktime() - (3 * $Clw);
366  if (empty($cutoff)) { $cutoff = '$thirty'; }
367  if (empty($histdays)) { $histdays = 0; }
368 # 1072940400 is the timestamp for 20040101
369  if ($pktstamp >1072940400 && $histdays > 0 && $cutoff == $thirty) {
370  # calculate cutoff as pktstamp - interval($histdays days)
371  if ($pktstamp > $threshold) {
372  # last packet age is less than 3 times the packet timeout setting
373  # set cutoff for 7 days
374  $cutoff = date("Ymd",mktime()-604800);
375  } else {
376  $cutoff = date("Ymd",$pktstamp-(86400 * $histdays));
377  }
378  }
379  $URL="?member=$Cn&type=I&if_mod_since=$pktstamp&cutoff=$cutoff";
380  if (!empty($Pass)) {
381  $URL.="&pass=$Pass";
382  }
383  $fd = @fopen ("$Fs$URL", "r");
384 
385 if ($fd) {
386  while ($buff = fread ($fd, 1500)) {
387  $packet .= $buff;
388  }
389 
390  $pktpattern='/<([^<>\/]*)?>\n?([^<]*)?/';
391  $pktpieces = array();
392  $pktpattern='/<([^<>\/]*)>(.*)<\/\\1>/Us';
393  $inqpattern='/<\/*Inquiry>/i';
394 # ugly hack, but appears to work. Strip <Inquiry></Inquiry> to remove nesting
395  preg_match_all("$pktpattern",preg_replace($inqpattern,"",$packet),$pktpieces);
396 
397  array_shift($pktpieces);
398  foreach ($pktpieces[0] as $pos => $tag) {
399  $tags[(strtolower($tag))] = $pos;
400  }
401  array_shift($pktpieces);
402  list($mem, $pcutoff) = explode("\t",$pktpieces[0][($tags['parameters'])]);
403  # if the packet presents a cutoff date, let it override our request
404  $cutoff = ("$pcutoff" == "" ? $cutoff : $pcutoff);
405  list($mem, $statcode, $statdesc) = explode("\t",$pktpieces[0][($tags['status'])]);
406  list($pktstamp, $pktdate) = explode ("\t",$pktpieces[0][($tags['time'])]);
407  print "<br>URL: $URL<br>";
408  print "Status: <br>
409  Code: $statcode<br>
410  Description: $statdesc<br>
411  Cutoff: (requested) $cutoff (returned) $pcutoff<br>";
412  if (trim($mem) != $Cn) {
413  $statcode = 999;
414  $statdesc = "Returned packet doesn't match request!";
415  }
416 
417 # check the return status to decide what to do here
418 
419 # 101 New Data -- process packet, update pktstamp
420 # 201 System Unavailable, New Data -- process packet, update pktstamp
421 # 100 No New Data -- update pktstamp
422 # 200 System Unavailable, No New Data -- return immediately
423 # 202 System Unavailable, No Data -- return immediately
424 # 999 Calling error or Invalid Parms -- return immediately
425 #
426  $errstat = 0;
427  foreach ($tags as $tag => $pos) {
428  switch ($tag) {
429  case "member":
430  case "accountbalance":
431  case "loanbalance":
432  case "accounthistory":
433  case "loanhistory":
434  case "messages":
435  case "holds":
436  $$tag = explode ("\n",$pktpieces[0][$pos]);
437 
438  # newlines around data on multiline components
439  # causes empty items in array.
440  # Pop the empty off the end
441  # new regex leaves newline at top as well,
442  # so shift it off also
443 
444 
445  array_shift($$tag);
446  array_pop($$tag);
447  }
448 
449  }
450 
451  # count each part
452 
453 $expected{'accountbalance'}=10;
454 $expected{'accounthistory'}=10;
455 $expected{'loanbalance'}=14;
456 $expected{'loanhistory'}=9;
457 $expected{'messages'}=4;
458 $expected{'holds'}=9;
459 
460 
461  foreach(array("accountbalance","loanbalance",
462  "accounthistory","loanhistory","messages","holds") as $tbl ){
463 
464 # print "<font color='blue'>Counting " . sizeof($$tbl) . " records for $tbl...</font><br>\n";
465  if (sizeof($$tbl)) {
466  print "<pre>";
467  $mincnt=0;
468  $maxcnt=0;
469 
470  foreach ($$tbl as $line) {
471  $line = preg_replace("/ ?\t ?/","\t",$line);
472  $line = preg_replace("/ $/","",$line);
473  $line = preg_replace("/\\0/","",$line);
474  $count=(count(explode("\t",$line)));
475  $mincnt=(($count < $mincnt || $mincnt==0) ? $count : $mincnt);
476  $maxcnt=($count > $maxcnt ? $count : $maxcnt);
477  if ("$Showtab" != "") {
478  $line = preg_replace("/\t/","$Showtab",$line);
479  }
480  $line = HTMLSpecialChars($line);
481  print "$line\n";
482  }
483  print "</pre>";
484  print "<font color='blue'>Counted " . sizeof($$tbl) . " records for $tbl... (Expected: " . $expected{$tbl} . ")</font> Min: $mincnt Max: $maxcnt<br><br>\n";
485  }
486  }
487 
488  print "Status: $statcode Date: $pktdate Description: $statdesc<br>\n";
489 
490 } else {
491  print "Couldn't get a packet!<br>$php_errormsg</body></html>";
492 }
493 }
494 }
495 
496 function valid_xac ($Fs, $Cu, $Cn, $Showtab) {
497  global $dbh;
498  $URL="?member=$Cn&type=X&f=xxx";
499  $fd = @fopen ("$Fs$URL", "r");
500 
501 if ($fd) {
502  while ($buff = fread ($fd, 1500)) {
503  $packet .= $buff;
504  }
505 
506 
507  $pktpieces = array();
508 
509  $status = (preg_match("/<status>\n?(.*)<\/status>/s",$packet,$pktpieces) ? $pktpieces[1] : "");
510  list($mem, $statcode, $statdesc) = explode("\t",$status);
511  if (trim($mem) != $Cn) {
512  return array(999, 1, "Unable to retrieve a packet");
513  }
514 
515  # check the return status to decide what to do here
516 
517  # 101 New Data -- process packet, update pktstamp
518  # 200 System Unavailable, No New Data -- return immediately
519  # 001 Invalid Account Number -- return immediately
520  # 999 Calling error or Invalid Parms -- return immediately
521  #
522  $errstat = 0;
523  $u = date("U");
524 
525  # make an array from the stuff in the packet
526 
527  preg_match("/<txaccount>\n?(.*)<\/txaccount>/si",$packet,$pktpieces);
528  $txaccount = explode ("\n",$pktpieces[1]);
529  array_pop($txaccount);
530  $tx_keys = array();
531  $mincnt=0;
532  $maxcnt=0;
533 
534  print "<pre>";
535  foreach ($txaccount as $line) {
536  $count=(count(explode("\t",$line)));
537  $mincnt=(($count < $mincnt || $mincnt==0) ? $count : $mincnt);
538  $maxcnt=($count > $maxcnt ? $count : $maxcnt);
539  if ("$Showtab" != "") {
540  $line = preg_replace("/\t/","$Showtab",$line);
541  }
542  $line = HTMLSpecialChars($line);
543  print "$line\n";
544  }
545  print "</pre>";
546 
547  print "<font color='blue'>Counted " . sizeof($txaccount) . " Cross-Account records...(Expected: 6)</font> Min: $mincnt Max: $maxcnt<br><br>\n";
548 
549  return array("$statcode", "$u", "$statdesc");
550 
551  } else {
552  return array(999,1,$php_errormsg);
553  }
554 }
555 ?>