Odyssey
upload.prg
1 <?php
2  // This file will upload a file for the credit union
3  // It will perform the following functions
4 
5  // Process File
6  // Redirect User
7  // Exit File Exists
8  // Exist Size Error
9  // Print a form for upload
10 
11  require_once ("$admLibrary/ck_aticket.i"); // Just a precaution to make sure that it is processed through the cookie
12 
13 $dms_ok=array('action'=>'string','FULLREFRESH'=>'digits',
14 'upload_file_name'=>'string');
15 dms_import($dms_ok);
16 
17  $self = "$PHP_SELF?ft={$ft}";
18  $saved_file = "$home_path/tmp/$chome";
19 
20  // "full_refresh" -- this value is set for uploads in the /admbin/main container
21  // value - true -- then show the FULL REFRESH checkbox
22  // value - false -- then don't show the checkbox -- error check for them trying to circumvent
23 
24  switch ($action) {
25  case "Upload File":
26  // Now process the file, but only if...
27 
28  // First make sure the pgload finished -- If I find the .dat file then stop processing
29  // and not allow them to upload any additional files
30  if (file_exists($saved_file . ".dat")) {
31  // ERROR --- STOP THEM From processing
32  print <<< print_html
33  <center>
34  <br>
35  <h2><font color="blue">File Upload Error</font></h2>
36  <br>
37  <h2><font color="blue">Your previous upload did not complete, please contact support at DMS, Inc. for instructions.</font></h2>
38  </center>
39 print_html;
40  } elseif (!is_uploaded_file($_FILES['upload_file']['tmp_name'])) {
41  // A problem occurred record the information that I know and mail it to mark@homecu.net
42  // Gather msg together
43  switch ($_FILES['upload_file']['error']) {
44  case 0:
45  $up_err = "0: No error, the file was uploaded successfully";
46  break;
47  case 1:
48  $up_err = "1: The upload is larger than the amount allowable by the upload_max_filesize directive in the php.ini";
49  break;
50  case 2:
51  $up_err = "2: The upload is larger than the MAX_FILE_SIZE directive that was specified via html";
52  break;
53  case 3:
54  $up_err = "3: The file was only partially uploaded";
55  break;
56  case 3:
57  $up_err = "4: no file was uploaded";
58  break;
59  default:
60  $up_err = "NOT SET";
61  }
62  $up_fe = (!file_exists("{$_FILES['upload_file']['tmp_name']}") ? "True" : "False");
63  $msg = "
64 Upload File Error --\n
65 Client Information \n
66  Client Filename: {$_FILES['upload_file']['name']}\n
67  Client Filesize: {$_FILES['upload_file']['size']}\n
68  Client Filetype: {$_FILES['upload_file']['type']}\n
69  Client IP: {$_SERVER['REMOTE_ADDR']}\n\n
70 Web Server Information \n
71  Temp File Name - {$_FILES['upload_file']['tmp_name']}
72  Temp File exist - $up_fe\n
73  Error Reported - $up_err\n\n
74 
75  ";
76  $notify = new ErrorMail;
77  $notify->line = __LINE__;
78  $notify->file = __FILE__;
79  $notify->callingfunction = __FUNCTION__;
80  $notify->sql = $msg;
81  $notify->cu = "$Cu";
82  $notify->survey = "";
83  $notify->mailto = "mark@homecu.net";
84  $notify->SendErr();
85  // IF we got here, the file is NOT an uploaded file report an error
86 
87  // An error may occur here if the file is too large, this can be set in the php.ini file the variable is upload_max_filesize
88  print <<< print_html
89  <center>
90  <br>
91  There was an error uploading the file <b>{$_FILES['upload_file']['name']}</b>.<br>
92  Please retry uploading the file.<br>
93  If the problem persists call DMS and report the problem.
94 
95  </center>
96 print_html;
97  } else {
98  // PHP Variables provided with a file upload
99  // $_FILES['###']['tmp_name'] - this is the field name that was on the multi-part form
100  // -- this will also contain the location of the file when it was uploaded
101  // $_FILES['###']['name'] - name of the file that was uploaded
102  // $_FILES['###']['size'] - size of file uploaded
103  // $_FILES['###']['type'] - type of file uploaded -- mime/type
104  // Our first Check is Okay, so now I want to Process the file
105  $real_name = "";
106  $find_ary = array("\\", "/", " ", ";"); // Create the list of characters for replacement
107  $rpl_ary = array("", "", "", ""); // Create the list of characters that will be replacing
108 
109  $real_name = str_replace($find_ary, $rpl_ary, $_FILES['upload_file']['name']);
110  // Now move the file from the temporary position into the credit unions /tmp directory
111  if (!move_uploaded_file($_FILES['upload_file']['tmp_name'], $saved_file . ".daz")) {
112  unlink ($_FILES['upload_file']['tmp_name']);
113  } else {
114  // File was moved change the mod settings
115  $rc = @chmod($saved_file . ".daz", 0644);
116  }
117 
118  // File has now been uploaded and moved successfully
119 
120  // Need to copy the status files From old.txt to older.txt, from .txt to old.txt
121  // Currently I will use the php "copy" function for the files
122  if (file_exists("$home_path/public_html/status.old.txt"))
123  copy ("$home_path/public_html/status.old.txt", "$home_path/public_html/status.older.txt");
124 
125  if (file_exists("$home_path/public_html/status.txt"))
126  copy ("$home_path/public_html/status.txt", "$home_path/public_html/status.old.txt");
127 
128 
129  // Now RUN the pgload process for this credit union
130  // Use escapeshellcmd when using the home_path variable
131 
132  // MWS 2/27/03 -- before executing set the environment path for the users e-mail address
133  $sql = "SELECT role, email
134  FROM cuadmnotify
135  WHERE cu = '$Cu'
136  AND role in ('upload', 'alert') ";
137  $em_rs = db_query($sql, $dbh);
138  for ($row=0; $em_row = db_fetch_array($em_rs,$row); $row++) {
139  $role = trim($em_row['role']);
140  $$role = trim($em_row['email']);
141  }
142  db_free_result($em_rs);
143 
144  // Miki 5/27/04 -- get the pgload command string from the database
145  $sql = "SELECT liveserver
146  FROM cuadmin
147  WHERE cu = '$Cu'";
148  $em_rs = db_query($sql, $dbh);
149  list($pgloadcmd) = db_fetch_array($em_rs, 0);
150  db_free_result($em_rs);
151  if (trim($pgloadcmd) == '') {
152  $pgloadcmd = "/usr/local/bin/pgload.pl -h $home_path";
153  }
154  $pgloadcmd = (escapeshellcmd("$pgloadcmd") . " >" . escapeshellcmd("$home_path/public_html/status.txt") . " 2>&1 &");
155 
156  $sql = "SELECT email FROM cuadminusers
157  WHERE cu = '$Cu' AND user_name = '$Cn' ";
158  $em_rs = db_query($sql, $dbh);
159  list($email) = db_fetch_array($em_rs, 0);
160  db_free_result($em_rs);
161 
162  if (trim($email) != '')
163  { $upload = no_dupes("$email;$upload"); }
164 
165  putenv("ADMUSER=$Cn");
166  putenv("ADMMAIL=$upload");
167  if ("$alert" != "" ) { putenv("ALERTMAIL=$alert"); }
168 
169  if ($full_refresh && ($FULLREFRESH == 1)) {
170  // For FULL REFRESH Perform this extra step
171  // Set the "histtrunc" flag in the cuadmin table
172  $sql = "UPDATE cuadmin
173  SET histtrunc = '1'
174  WHERE cu = '$Cu' ";
175 
176  $hist_rs = db_query($sql, $dbh); // Execute the query
177  db_free_result($hist_rs); // Release
178  }
179 
180  #$command = escapeshellcmd("$home_path/bin/pgload.sh") . " >" . escapeshellcmd("$home_path/public_html/status.txt") . " 2>&1 &";
181  //$command = "$home_path/bin/pgload.pl >/dev/null &"; // This is a test script, to make sure php is handling this in the background
182 
183  #$rc = exec ($command);
184  $rc = exec ($pgloadcmd);
185  // Print the page text
186  $refresh_text;
187  if ($full_refresh && ($FULLREFRESH == 1)) {
188  $refresh_text = "<span style=\"color: red\">NOTE: You just performed a full refresh of your credit unions' data. <b>If the file you just sent was not a full refresh</b>, you must create and upload a full refresh file now to fix the data!</span><br><br>";
189  }
190 
191  // Now set a cookie for the filename that was last uploaded
192  HCU_setcookie_env($SYSENV, "LastFileName", $upload_file_name, time()+60*60*24*30);
193 
194  print <<< html_text
195  <br>
196  $refresh_text
197 
198 
199 
200  <h3>Thank You! Your upload file '{$_FILES['upload_file']['name']}' ({$_FILES['upload_file']['size']} bytes) was received and it is now being processed.</h3>
201 
202  <br>
203  <b>THIS DOES NOT MEAN:</b>
204  <ul>
205  <li>That we received the correct file, </li>
206  <li>That this was the next sequential file,</li>
207  <li>Or that this it was even a digital banking file.</li>
208  </ul>
209  <br>
210  <br>
211  Please review the <a href="/admbin/main.prg/admin/status.html?ft=80" class='link2'>results</a> of this upload. Within about 30 seconds (or longer for large files) an email will be sent to confirm the results of this upload. You should insure that the file processed and there are no missing export files.
212  <br>
213  <br>
214  If you prefer, you can click on the 'Upload History' link at the left. You should see that this export was processed and no exports are missing. If this does not show in the list, be sure to check your email so you can see what happened and how to fix it.
215 
216 html_text;
217 
218 
219  }
220  break;
221  default:
222 
223  /*
224  upload_status -- values
225  0 - The .dat file was NOT found -- all as usual
226  1 - The .dat file was found -- but is NOT being used by another process
227  2 - The .dat file was found AND the status.txt file is being used by another process
228  */
229  $upload_status = 0;
230  // *** MWS 2/17/2006 -- Add new options for showing different stats based on where the CU is in the upload
231  // process
232 
233  // *** CHECK STATUS OF EXISTENCE .dat FILE
234  $dat_path_file = "/home/{$chome}/tmp/{$chome}.dat";
235 
236  // *** CHECK FOR USER ON .txt FILE
237  $txt_path_file = "/home/{$chome}/public_html/status.txt";
238 
239  if (file_exists($dat_path_file)) {
240  $upload_status = 1;
241  // *** CHECK LOCK STATUS OF .dat FILE
242  $ret_fuser = exec("/bin/fuser " . escapeshellcmd($txt_path_file));
243  if ($ret_fuser != false) {
244  $upload_status = 2;
245  }
246 
247  }
248 
249  // *** LAST UPLOAD STATUS
250  $lastupl_msg = "";
251  if (is_readable("$home_path/admin/status.html")) {
252  $lastupload = date("m/d/y g:i:s A", filemtime("$home_path/admin/status.html"));
253 
254  if (!empty($lastupload)) {
255  $lastupl_msg = <<< set_msg
256  <tr><td colspan="2" class="dtl"><img src='/IMAGES/transfer.gif'>
257  <a href="/admbin/main.prg/admin/status.html?ft=80">View last upload status</a> dated $lastupload
258  </td></tr>
259 set_msg;
260  }
261  }
262 
263  if ($upload_status == 1) {
264  // ** FILE WAS FOUND --- THEY WILL NOT BE ABLE TO UPLOAD --- BUT THE UPLOAD PROCESS HAS STOPPED
265  print <<< form_data
266 
267  <form method="" action="">
268  <br><br>
269  <table cellpadding="4" align="CENTER" cellspacing="0" border="0" class="dtl">
270  <tr><th align="CENTER" colspan="2" class="bar">$Cu File Upload</th></tr>
271  <tr><td colspan="2" align='center'>
272  <font color="blue">Your previous upload did not complete.</font>
273  </td></tr>
274  $lastupl_msg
275  <tr><td colspan="2">Contact DMS Support for help on completing the upload.</td></tr>
276  </table>
277  <br>
278  </form>
279 
280 form_data;
281 
282  } elseif($upload_status == 2) {
283  // ** FILE WAS FOUND --- THEY WILL NOT BE ABLE TO UPLOAD --- BUT THE UPLOAD PROCESS IS ONGOING
284 
285  print <<< form_data
286 
287  <form method="" action="">
288  <br><br>
289  <table cellpadding="4" align="CENTER" cellspacing="0" border="0" class="dtl">
290  <tr><th align="CENTER" colspan="2" class="bar">$Cu File Upload</th></tr>
291  <tr><td colspan="2">Your upload is currently being processed. Please check back later.</td></tr>
292  </table>
293  <br>
294  </form>
295 
296 form_data;
297 
298  } else {
299  // This is the default, which is to print the multi-part form for uploading
300 
301  // Now create a special text for FULL REFRESH
302  if ($full_refresh) {
303  $refresh_text = "<span style=\"color: red\" >(FULL REFRESH)</span>";
304  $refresh_js = <<< js
305  if (form_pass) {
306  if (document.forms[0].FULLREFRESH.checked) {
307  msg = "Uploading this file will perform a FULL REFRESH for your Credit Unions' data. Are you sure this is what you want to do? Press 'OK' to continue, otherwise press 'Cancel'";
308 
309  if (!window.confirm(msg)) {
310  form_pass = false;
311  }
312  }
313  }
314 js;
315  $refresh_box = <<< refresh
316  <tr><td>&nbsp;</td>
317  <td><input type="checkbox" name="FULLREFRESH" value="1">Perform Full Refresh?</td>
318  </tr>
319 refresh;
320  } else {
321  $refresh_text = "";
322  $refresh_js = "";
323  }
324 
325  // Check for the existance of the LastFileName cookie, if it's there,
326  // then add some text to the script
327  $LastFileName_Text = "";
328  if ($_COOKIE['LastFileName']) {
329 
330  $LastFileName_Text = "<tr><td colspan=\"2\">The path to the last file uploaded was:<br><br>" .
331  "<input type=\"text\" name=\"defvalue\" READONLY onfocus=\"this.select();\"value=\"" . trim($_COOKIE['LastFileName']) . "\" class=\"dtl\"size=70 style=\"border: 0;\">" .
332  "</td></tr>";
333  }
334 
335  print <<< form_data
336  <script language="javascript">
337  function check_upload() {
338  var form_pass = true;
339  var msg = "";
340  var up_file = "";
341 
342  // Be certain that a value has been entered into the file box
343  up_file = document.forms[0].upload_file.value;
344 
345  if (up_file.length == 0) {
346  msg = "I noticed you have not selected a file to upload. Please choose a file before continuing.";
347  window.alert (msg);
348  form_pass = false;
349  }
350 
351  $refresh_js
352 
353  return form_pass;
354  }
355  </script>
356  <form method="post" action="$self" enctype="multipart/form-data">
357  <br><br>
358  <table cellpadding="4" align="CENTER" cellspacing="0" border="0" class="dtl">
359  <tr><th align="CENTER" colspan="2" class="bar">$Cu File Upload</th></tr>
360  $lastupl_msg
361  $LastFileName_Text
362  <tr><td>Choose a File:</td> <td><input type="file" name="upload_file" size="36" onChange="copyFile()"></td></tr>
363  <input type="hidden" name="upload_file_name" value="">
364  $refresh_box
365  <tr><td align="CENTER" colspan="2"><input type="submit" name="action" value="Upload File" onClick="return check_upload()"></td></tr>
366  </table>
367  <br>
368  </form>
369  <font color="blue" /><em>Note: </em>
370  The 'Upload File' button only requires a single-click. A double-click causes the file to be uploaded twice!
371  <p>Also, please wait for the upload to finish before trying to send again.</p></font>
372 
373  <script language="javascript">
374  <!--
375  function copyFile() {
376  document.forms[0].upload_file_name.value = document.forms[0].upload_file.value;
377  }
378  // -->
379  </script>
380 form_data;
381  }
382  break;
383  }
384 function no_dupes($estring) {
385  foreach (explode(";",$estring) as $x => $ema) {
386  $ema = trim($ema);
387  if ($ema != "" && (strpos("$return","$ema") === false)) {
388  $return .= "$ema;";
389  }
390  }
391  return $return;
392 }
393 ?>