From 40dfa7d5c0dc8e4cb2c95e403b6b4b2c4bf70644 Mon Sep 17 00:00:00 2001 From: Matthijs Kuiper Date: Sat, 4 Feb 2017 16:05:45 +0100 Subject: Original UTStats beta 4.2.8 --- html/includes/ftp.php | 411 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 411 insertions(+) create mode 100755 html/includes/ftp.php (limited to 'html/includes/ftp.php') diff --git a/html/includes/ftp.php b/html/includes/ftp.php new file mode 100755 index 0000000..7e058a3 --- /dev/null +++ b/html/includes/ftp.php @@ -0,0 +1,411 @@ +PopError()) !== false) { + + $fctname = $err['fctname']; + + $msg = $err['msg']; + + $desc = $err['desc']; + + if($desc) $tmp=' ('.$desc.')'; else $tmp=''; + + if (strpos($msg, 'socket set') === 0) { + + $tmp .= "\nTry disabling the usage of sockets (set \$ftp_type = 'pure'; in config.php)"; + + } + + tablerow('Error details:', $fctname.': '.$msg.$tmp, true); + + } + + + +} + + + +function tablerow($left, $right, $error=false) { + + global $html, $ftp_debug; + + if ($ftp_debug) return; + + $space = ($html) ? ' ' : ' '; + + $left = (empty($left)) ? $space : (($html) ? htmlentities($left) : $left); + + $right = (empty($right)) ? $space : (($html) ? nl2br(htmlentities($right)) : $right); + + $style = ($error) ? 'style="background-color: red;"' : ''; + + if ($html) { + + echo ''; + + echo ''. $left .''; + + echo ''. $right .''; + + echo ''; + + } else { + + if (strlen($left) < 30) $left .= str_repeat(" ", 30 - strlen($left)); + + echo "$left $right\n"; + + } + + flush(); + +} + + + +function ftpupdate() + +{ + + global $html, $ftp, $ftp_uname, $ftp_upass, $ftp_hostname, $ftp_port, $g_ftp_error, $ftp_debug, + + $ftp_delete, $ftp_movedir, $ftp_dir, $ftp_passive, $import_log_start, $import_log_extension, + + $import_utdc_download_enable, $import_utdc_log_start, $import_utdc_log_extension, $import_utdc_screenshot_start, $import_utdc_screenshot_extension, + $import_ac_download_enable, $import_ac_log_start, $import_ac_log_extension, + $import_ace_download_enable, $import_ace_log_start, $import_ace_log_extension, $import_ace_screenshot_start, $import_ace_screenshot_extension; + + + + if (!$ftp_debug) { + + if ($html) echo''; + + } + + + + // Update, from here on were going to be doing multiple FTP sessions. + + for ($i = 0; $i < count($ftp_hostname); $i++) { + + if ($i != 0) { + + if ($html and !$ftp_debug) echo ''; + + echo "\n"; + + } + + + + tablerow('Connecting to server:', $ftp_hostname[$i] .':'. $ftp_port[$i]); + + if (!$ftp->SetServer($ftp_hostname[$i], $ftp_port[$i])) { + + ftp_error("Unable to set server: ". $ftp->lastmsg); $ftp->quit(true); continue; + + } + + if (!$ftp->connect()) { + + ftp_error("Unable to connect to server: ". $ftp->lastmsg); $ftp->quit(true); continue; + + } + + + + tablerow('', "Connected, now logging in..."); + + if (!$ftp->login($ftp_uname[$i], $ftp_upass[$i])) { + + ftp_error("Login failed!\nBad username/password?"); $ftp->quit(true); continue; + + } + + tablerow('', "Logged in!"); + + + + if (!$ftp->SetType(FTP_BINARY)) { + + ftp_error("Could not set type: ". $ftp->lastmsg); $ftp->quit(true); continue; + + } + + if (!isset($ftp_passive[$i]) or $ftp_passive[$i]) { + + tablerow("", "Setting passive mode"); + + if(!$ftp->Passive(true)) { + + ftp_error("Could not set passive mode: ". $ftp->lastmsg); $ftp->quit(true); continue; + + } + + } else { + + tablerow("", "Setting active mode"); + + if(!$ftp->Passive(false)) { + + ftp_error("Could not set active mode: ". $ftp->lastmsg); $ftp->quit(true); continue; + + } + + } + + if (($pwd = $ftp->pwd()) === false) { + + ftp_error("Unable to retrieve current working directory"); $ftp->quit(true); continue; + + } + + tablerow("Current directory is:", $pwd); + + + + $dl_start = time(); + + $dl_files = 0; + + $dl_bytes = 0; + + $error = false; + + foreach($ftp_dir[$i] as $dir) { + + if (!empty($dir)) { + + if (!$ftp->chdir($dir)) { + + ftp_error("Unable to change directory to: $dir"); $ftp->quit(true); continue; + + } + + tablerow('', "Changing directory to: $dir"); + + if (($pwd = $ftp->pwd()) === false) { + + ftp_error("Unable to retrieve current working directory"); $ftp->quit(true); continue; + + } + + tablerow("New directory is:", $pwd); + + } + + + + if (($filelist = $ftp->nlist()) === false) { + + ftp_error("Unable to retrieve file list"); continue; + + } + + + + tablerow("Directory contains:", count($filelist) ." ". ((count($filelist) == 1) ? 'file' : 'files')); + + if (count($filelist) == 0) { + + continue; + + } + + foreach ($filelist as $filename) { + + if (((substr($filename, 0, strlen($import_log_start)) == $import_log_start) and (substr($filename, strlen($filename) - strlen($import_log_extension)) == $import_log_extension)) + + or ($import_utdc_download_enable and (substr($filename, 0, strlen($import_utdc_log_start)) == $import_utdc_log_start) and (substr($filename, strlen($filename) - strlen($import_utdc_log_extension)) == $import_utdc_log_extension)) + + or ($import_utdc_download_enable and (substr($filename, 0, strlen($import_utdc_screenshot_start)) == $import_utdc_screenshot_start) and (substr($filename, strlen($filename) - strlen($import_utdc_screenshot_extension)) == $import_utdc_screenshot_extension)) + + or ($import_ac_download_enable and (substr($filename, 0, strlen($import_ac_log_start)) == $import_ac_log_start) and (substr($filename, strlen($filename) - strlen($import_ac_log_extension)) == $import_ac_log_extension)) + + or ($import_ace_download_enable and (substr($filename, 0, strlen($import_ace_log_start)) == $import_ace_log_start) and (substr($filename, strlen($filename) - strlen($import_ace_log_extension)) == $import_ace_log_extension)) + + or ($import_ace_download_enable and (substr($filename, 0, strlen($import_ace_screenshot_start)) == $import_ace_screenshot_start) and (substr($filename, strlen($filename) - strlen($import_ace_screenshot_extension)) == $import_ace_screenshot_extension))) { + } else { + continue; + } + + $size = $ftp->get($filename, 'logs/' . $filename); + + if ($size === FALSE) { + + $result = 'ERROR!'; + + $error = true; + + if (file_exists('logs/' . $filename)) { + unlink('logs/' . $filename); + } + + } else { + + $result = "OK (". number_format(round(($size / 1024), 0)) ." KB)"; + + $dl_files++; + + $dl_bytes += $size; + + } + + tablerow(($dl_files == 1) ? 'Downloading...' : '', "$filename -> $result"); + + if ((!isset($ftp_delete[$i]) or $ftp_delete[$i]) and !$error) { + $ftp->delete($filename); + } + else { + // rename the file to prevent reimporting + $ftp->rename($filename, "~" . $filename); + } + + } + + } + + $dl_kb = number_format(round(($dl_bytes / 1024), 0)); + + $dl_time = time() - $dl_start; + + tablerow("Downloaded:", "$dl_files ". ((count($filelist) == 1) ? 'file' : 'files') ." ($dl_kb KB) in $dl_time seconds"); + + + + if ($error) { + + ftp_error('There were errors when downoading (some) files!'); + + } + + + + tablerow("Disconnecting...", "done!"); + + $ftp->quit(true); + + } + + if (!$ftp_debug and $html) echo '
'; + + echo "FTP Transferring Log Files...\n"; + + if ($html) echo '

'; + + echo "\n\n"; + + + + //update timestamp + + + + if (!$g_ftp_error) { + + $file = fopen('includes/ftptimestamp.php', 'wb+', 1); + + fwrite($file, time()); + + fclose($file); + + } + +} + + + + + +$fname = 'includes/ftptimestamp.php'; + +$timestamp = 0; + + + +if(file_exists($fname)) + +{ + + $file = fopen($fname, 'rb'); + + $timestamp = trim(my_fgets($file)); + + fclose($file); + +} + + + +if(!$timestamp || (time() - $timestamp) > $ftp_interval*60) { + + if ($timestamp) { + + if ($html) echo '

'; + + echo "Last FTP update more than $ftp_interval minutes ago, starting update ($ftp_type): \n"; + + if ($html) echo '

'; + + } + + if ($ftp_debug) { + + if ($html) echo '
'; + + echo "FTP Debugging Output:\n"; + + if ($html) echo '
';
+
+	}
+
+	$ftp = new ftp($ftp_debug, $ftp_debug);
+
+	ftpupdate();
+
+	if ($ftp_debug and $html) echo '

'; + +} else { + + if ($html) echo '

'; + + echo "Last FTP update was ". round(((time() - $timestamp) / 60), 0) ." minutes ago, no update necessary\n"; + + if ($html) echo '

'; + +} + + + +?> \ No newline at end of file -- cgit