PageRenderTime 67ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/application/views/pages/admin/dhlftp.php

https://bitbucket.org/chadsaun/ifrogz-panel
PHP | 209 lines | 189 code | 18 blank | 2 comment | 26 complexity | de40b3223cca3f34f337fe62e5bfb894 MD5 | raw file
  1. <script type="text/javascript">
  2. <!--
  3. function checkDelete() {
  4. var decision = confirm("Are you sure you want to delete this file?");
  5. if(decision==true) {
  6. return true;
  7. }else{
  8. return false;
  9. }
  10. }
  11. function checkReceive() {
  12. var decision = confirm("Are you sure you want to receive this file?\nExisting information will NOT be overwritten.");
  13. if(decision==true) {
  14. return true;
  15. }else{
  16. return false;
  17. }
  18. }
  19. -->
  20. </script>
  21. <div>
  22. <div style="margin: 0 10px; padding: 3px; font-weight: bold; font-size: 18px; text-align: center; background-color: #030133; color: #E7EAEF; border: 1px solid black; border-bottom: none">DHL File Administration</div>
  23. <div style="margin: 0 10px 10px 10px; padding: 3px 5px; text-align: center; border: 1px solid black; border-top: none; background-color: #E7EAEF; color: #333333">Manage the CSV files we send them, and the CSV files they send back to us.</div>
  24. <?php
  25. if(!empty($_SESSION['dhlMsg'])) {
  26. echo "<div style='margin-bottom: 10px; color: green; text-align: center; font-weight: bold'>".$_SESSION['dhlMsg']."</div>";
  27. $_SESSION['dhlMsg'] = '';
  28. }
  29. ?>
  30. <div style="margin-right: 10px; float: right; width: 45%">
  31. <div style="margin-bottom: 10px 10px 0 0; text-align: center; font-size: 12px; font-weight: bold; background-color: #030133; color: #E7EAEF; border: 1px solid #030133; border-bottom: none">
  32. <p style="margin: 0; padding: 3px 0; font-size: 12px">Sent DHL Files</p>
  33. </div>
  34. <div style="padding: 5px; border: 1px solid #030133; background-color: #E7EAEF">
  35. <?php
  36. if(!$connID = ftpLogin("ftp.smartmail.com","ftpuserrband",'5g$Xpr!4')) {
  37. echo "Could not login to the DHL's FTP server.";
  38. }else{
  39. $files = ftpListFiles($connID,'','ifrogz');
  40. if($files===false) {
  41. echo "<div style='text-align: center; font-weight: bold'>Problem getting the list of files.</div>";
  42. }elseif (empty($files)) {
  43. echo "<div style='text-align: center; font-weight: bold'>No ifrogz files found.</div>";
  44. }else{
  45. foreach($files as $filename) {
  46. echo '<form method="post" action="/admin/dhlftpprocess.php"><div style="line-height: 24px">'.$filename.'<input type="submit" name="delete" value="Delete" style="margin-left: 5px" onclick="return checkDelete();" /><input type="hidden" name="filename" value="'.$filename.'" /></div></form>';
  47. }
  48. }
  49. }
  50. ?>
  51. </div>
  52. <div style="margin: 10px 0 0 0; text-align: center; font-size: 12px; font-weight: bold; background-color: #030133; color: #E7EAEF; border: 1px solid #030133; border-bottom: none">
  53. <p style="margin: 0; padding: 3px 0; font-size: 12px">Received DHL Files</p>
  54. </div>
  55. <div style="padding: 5px; border: 1px solid #030133; background-color: #E7EAEF">
  56. <?php
  57. if(!$connID = ftpLogin("ftp.smartmail.com","ftpuserrband",'5g$Xpr!4')) {
  58. echo "Could not login to the DHL's FTP server.";
  59. }else{
  60. $files = ftpListFiles($connID,'From_SMail','');
  61. if($files===false) {
  62. echo "<div style='text-align: center; font-weight: bold'>Problem getting the list of files.</div>";
  63. }elseif(empty($files)) {
  64. echo "<div style='text-align: center; font-weight: bold'>DHL hasn't sent us anything yet.</div>";
  65. }else{
  66. $sql = "SELECT * FROM dhl_files WHERE status = 'received'";
  67. $res = mysql_query($sql) or print(mysql_error());
  68. $aRec = array();
  69. while($row=mysql_fetch_assoc($res)) {
  70. array_push($aRec,$row['filename']);
  71. }
  72. sort($files); // sort just in-case files are out of order on FTP site
  73. array_reverse($files); // Sort list in Descending order
  74. for($i=0; $i<count($files) && $i<10; $i++) {
  75. $thisfile = $files[(count($files)-1-$i)]; // So that it shows most recent to oldest
  76. $strDate = "20".substr($thisfile,9,2)."-".substr($thisfile,11,2)."-".substr($thisfile,13,2)." ".substr($thisfile,15,2).":00:00";
  77. $date = strtotime($strDate);
  78. $received = '';
  79. if(in_array($thisfile,$aRec)) {
  80. $received = 'Received';
  81. }
  82. echo '<form method="post" action="/admin/dhlftpprocess.php"><div style="line-height: 24px"><a href="/admin/dhlftpprocess.php?view=yes&amp;path='.urlencode("From_SMail/").'&amp;file='.urlencode($thisfile).'">'.$thisfile.'</a><input type="submit" name="receive" value="Receive" style="margin-left: 5px" onclick="return checkReceive();" /><input type="hidden" name="filename" value="'.$thisfile.'" /><span style="color: #006600; font-weight: bold"> '.$received.'</span></div></form>';
  83. }
  84. }
  85. }
  86. ?>
  87. </div>
  88. </div>
  89. <div style="margin-left: 10px; float: left; width: 45%">
  90. <div style="margin-bottom: 10px 10px 0 0; text-align: center; font-size: 12px; font-weight: bold; background-color: #030133; color: #E7EAEF; border: 1px solid #030133; border-bottom: none">
  91. <p style="margin: 0; padding: 3px 0; font-size: 12px">Our DHL Files</p>
  92. </div>
  93. <div style="padding: 5px; border: 1px solid #030133; background-color: #E7EAEF">
  94. <?php
  95. $dir = "../batches/dhl";
  96. $dh = opendir($dir);
  97. $index=0;
  98. while (false !== ($filename = readdir($dh))) {
  99. if ($filename != "." && $filename != ".." && $filename != "error_log" && $filename != "tmp") {
  100. ereg("^ifrogz[0-9]*_([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})_in\.dat$",$filename,$regs);
  101. $year = $regs[1];
  102. $month = $regs[2];
  103. $day = $regs[3];
  104. $hour = $regs[4];
  105. $timestamp = mktime($hour,0,0,$month,$day,"20".$year);
  106. $listofiles[$index]['filename'] = $filename;
  107. $listofiles[$index]['timestamp'] = $timestamp;
  108. $index++;
  109. }
  110. }
  111. usort($listofiles,'compare');
  112. if(!empty($listofiles)) {
  113. $sql = "SELECT * FROM dhl_files WHERE status = 'uploaded'";
  114. $res = mysql_query($sql) or print(mysql_error());
  115. $aUp = array();
  116. while($row=mysql_fetch_assoc($res)) {
  117. array_push($aUp,$row['filename']);
  118. }
  119. for($i=0; $i<count($listofiles)&&$i<20; $i++) {
  120. $thisfile = $listofiles[$i]['filename'];
  121. $uploaded = '';
  122. if(in_array($thisfile,$aUp)) {
  123. $uploaded = 'Uploaded';
  124. }
  125. echo '<form method="post" action="/admin/dhlftpprocess.php"><div style="line-height: 24px"><a href="/admin/download.php?path='.urlencode('../batches/dhl/').'&amp;file='.urlencode($thisfile).'">'.$thisfile.'</a><input type="submit" name="upload" value="Upload" style="margin-left: 5px" /><input type="hidden" name="filename" value="'.$thisfile.'" /><span style="color: #006600; font-weight: bold"> '.$uploaded.'</span></div></form>';
  126. }
  127. }else {?>
  128. <div style="margin: 5px auto">No batches found.</div>
  129. <? } ?>
  130. </div>
  131. </div>
  132. <div style="clear:both; height: 20px"></div>
  133. </div>
  134. <?php
  135. function ftpLogin($host,$user,$pass) {
  136. $ftp = ftp_connect($host);
  137. if ($ftp) {
  138. if (ftp_login($ftp,$user,$pass)) {
  139. return $ftp;
  140. }else{
  141. return false;
  142. }
  143. }else{
  144. return false;
  145. }
  146. }
  147. function ftpListFiles($connID,$dir="",$filter="") {
  148. if(!ftp_chdir($connID,$dir)) {
  149. ftp_quit($connID);
  150. return false;
  151. }
  152. $contents = ftp_rawlist($connID,"");
  153. //showarray($contents);
  154. if(!$contents) {
  155. ftp_quit($connID);
  156. return false;
  157. }
  158. $files = array();
  159. foreach($contents as $value) {
  160. $item = split("[ ]+",$value);
  161. $item_type = substr($item[0],0,1);
  162. $filename = $item[8];
  163. if($item_type=='-') { // IF ITS A FILE NOT A DIRECTORY
  164. if(!empty($filter)) {
  165. if(strstr($filename,$filter)) {
  166. $files[] = $filename;
  167. }
  168. }else{
  169. $files[] = $filename;
  170. }
  171. }
  172. }
  173. ftp_quit($connID);
  174. //showarray($files);
  175. return $files;
  176. }
  177. function compare($x, $y) {
  178. if($x['timestamp'] == $y['timestamp']) {
  179. return 0;
  180. }elseif($x['timestamp'] > $y['timestamp']) {
  181. return false;
  182. }else{
  183. return true;
  184. }
  185. }
  186. ?>