Просмотр файла ftp/index.php

Размер файла: 16.03Kb
<?php
require_once "../includes/start.php";
require_once "../includes/functions.php";
require_once "../includes/header.php";
include_once "../themes/".$config['themes']."/index.php";
	$downloadDir = "/tmp/";
	$unzipCommand = "sudo -u $user /usr/bin/unzip";
	$port=22;

	function del_recursive($currentDir,$connection,$file){
		//echo "entering $currentDir/$file<BR>";
		if ($lista = @ftp_nlist($connection, "$file")){
			for ($x=0;$x<count($lista);$x++){
				//echo "tryng to delete $lista[$x]<BR>";
				if (!@ftp_delete($connection, "$lista[$x]"))
				  del_recursive($currentDir,$connection,$lista[$x]);
			}
			@ftp_rmdir($connection, "$file");
		 }
	}

	include("parser.inc.php");

	session_start();

	$HPV = $HTTP_POST_VARS;

	// Get the POST, GET and SESSION variables (if register_globals=off (PHP4.2.1+))
	// It's a bit of a dirty hack but variables are sometimes GET and sometimes POST variables
	$mode=(isset($HPV['mode']))?$HPV['mode']:$HTTP_GET_VARS['mode'];
	$action=(isset($HPV['action']))?$HPV['action']:$HTTP_GET_VARS['action'];
	$currentDir=(isset($HPV['currentDir']))?$HPV['currentDir']:$HTTP_GET_VARS['currentDir'];
	$file=(isset($HPV['file']))?$HPV['file']:$HTTP_GET_VARS['file'];
	$file2=(isset($HPV['file2']))?$HPV['file2']:$HTTP_GET_VARS['file2'];
	$permissions=(isset($HPV['permissions']))?$HPV['permissions']:$HTTP_GET_VARS['permissions'];
	$directory=(isset($HPV['directory']))?$HPV['directory']:$HTTP_GET_VARS['directory'];	$MAX_FILE_SIZE=(isset($HPV['MAX_FILE_SIZE']))?$HPV['MAX_FILE_SIZE']:$HTTP_GET_VARS['MAX_FILE_SIZE'];
	$logoff=(isset($HPV['logoff']))?$HPV['logoff']:$HTTP_GET_VARS['logoff'];

	if(isset($HTTP_SESSION_VARS['server'])) {
		$server=$HTTP_SESSION_VARS['server'];
		$user=$HTTP_SESSION_VARS['user'];
		$password=$HTTP_SESSION_VARS['password'];
		$port=$HTTP_SESSION_VARS['port'];
	} else {
		$server=$HTTP_POST_VARS['server'];
		$user=$HTTP_POST_VARS['user'];
		$password=$HTTP_POST_VARS['password'];
		$port=$HTTP_POST_VARS['port'];
	}


	if (isset($logoff))
	{
		session_unregister('server');
		session_unregister('user');
		session_unregister('password');
		session_unregister('port');
		unset($server);
		unset($user);
		unset($password);
		unset($port);
		session_destroy();
	}

	if (isset($server))
	{
		session_register('server', $server);
		session_register('user', $user);
		session_register('password', $password);
		session_register('port', $port);

//		$connection = @ftp_connect($server);
		$connection = @ftp_connect($server, $port);
		$loggedOn = @ftp_login($connection, $user, $password);
		$systype = @ftp_systype($connection);

		if(!isset($mode))
		{
			$mode=1; //(FTP_ASCII = 0; FTP_BINARY=1)
		}
		if ($loggedOn)
		{
			if (isset($currentDir))
			{
				ftp_chdir($connection, $currentDir);
			}
			$currentDir = ftp_pwd($connection);
			$msg = "Текущая папка = $currentDir";
			// what to do now ???
			if(isset($action)) {
				switch ($action) {
					case "chmod":	// Change permissions
						if(@ftp_site($connection, "chmod $permissions $file"))
						{
							$msg= "Права выствлены.";
						} else
						{
							$msg= "Не удалось выставить права для " . $file;
						}
						break;
					case "cd":			// Change directory
						//First try : normal directory
						if(@ftp_chdir($connection, $currentDir . "/" . $file))
						{
							$currentDir = @ftp_pwd($connection);
							$msg = "Текущая папка = " . $currentDir;
						}
						elseif(@ftp_chdir($connection, $file)) // Symbolic link directory
						{
							$currentDir = @ftp_pwd($connection);
							$msg = "Текущая папка = " . $currentDir;
						}
						else // link to a file so let's retrieve this...
						{
							header("Content-disposition: attachment; filename=\"$file\"");
							header("Content-type: application/octetstream");
							header("Pragma: ");
							header("Cache-Control: cache");
							header("Expires: 0");

							//Determine original filename
							$filearray = explode("/",$file);
							$file = $filearray[sizeof($filearray)-1];
							$msg = $file;

							$fp = fopen($downloadDir . $file, "w");
							if(!@ftp_fget($connection,$fp,"$file",$mode))
							{
								fclose($fp);
								exit;
							}
							fclose($fp);
							$data = readfile($downloadDir . $file);
							$i=0;
							while ($data[$i] != "")
							{
								echo $data[$i];
								$i++;
							}
							unlink($downloadDir . $file);
							exit;

						}
						break;
					case "get":			// Download file
						header("Content-disposition: attachment; filename=\"$file\"");
						header("Content-type: application/octetstream");
						header("Pragma: ");
						header("Cache-Control: cache");
						header("Expires: 0");

						$fp = fopen($downloadDir . $file, "w");
						ftp_fget($connection,$fp,"$file",$mode) OR DIE("Error downloading file");
						fclose($fp);
						$data = readfile($downloadDir . $file);
						$i=0;
						while ($data[$i] != "")
						{
							echo $data[$i];
							$i++;
						}
						unlink($downloadDir . $file);
						exit;
						break;
					case "put":			// Upload file
						if($file_size > $MAX_FILE_SIZE)
						{
							$msg = "<B>Размер файла слишком большой !</B> (max. " . $MAX_FILE_SIZE . "bytes)<P>";
						}
						else
						{
							if(file_exists($HTTP_POST_FILES['file']['tmp_name']))
							{
								if ($mode==1)
								{
									ftp_put($connection, $currentDir . "/" . $HTTP_POST_FILES['file']['name'], $HTTP_POST_FILES['file']['tmp_name'], 1);
								}
								else
								{
									ftp_put($connection, $currentDir . "/" . $HTTP_POST_FILES['file']['name'], $HTTP_POST_FILES['file']['tmp_name'], 0);
								}
								unlink($HTTP_POST_FILES['file']['tmp_name']);
							}
							else
							{
								$msg = "Файл не закачан.";
							}
						}
						break;
					case "deldir";		// Delete directory
						if(@ftp_rmdir($connection, "$file"))
						{
								$msg = "$file удалён";
						}
						else
						{
							//Verify if has files inside and if so, call recursive del
							if ($lista = @ftp_nlist($connection, "$currentDir/$file")){
								del_recursive($currentDir,$connection,$file);
								$msg = "Папка $currentDir/$file удалён";
							}
							else $msg = "Не могу удалить $file";
						}
						break;
					case "delfile";		// Delete file
						if(@ftp_delete($connection, "$file"))
						{
							$msg = "$file удалён";
						}
						else
						{
							$msg = "Не удалось удалить $file";
						}
						break;
					case "rename";		// Rename file
						if(@ftp_rename($connection, "$file", "$file2"))
						{
							$msg = "$file переименован на to $file2";
						}
						else
						{
							$msg = "Не удалось переименовать $file в $file2";
						}
						break;
					case "createdir":  // Create a new directory
						if(@ftp_mkdir($connection, "$file"))
						{
							$msg = "$file создана";
						}
						else
						{
							$msg = "Не удалось создать $file";
						}
						break;
				  case "unzipfile";
						$filens = str_replace(" ","\\ ",$file);

						if (exec("$unzipCommand $currentDir/$filens -d $currentDir/"))
						{
							$msg = "$file разархивирован";
						} else {
							$msg = "Не удалось разархивировать файл !";
						}
						break;
				}
			}
?>
<HTML>
<HEAD>
	<SCRIPT LANGUAGE="JavaScript" SRC="include/script.js"></SCRIPT>
</HEAD>
<BODY>
		<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0 WIDTH='100%'>
			<TR>
				<TD CLASS=menu>
					<?php if($loggedOn) { ?>
					[&nbsp;&nbsp;<A CLASS=menu HREF="<?=$PHP_SELF;?>?logoff=true">Выход</A>&nbsp;&nbsp;
					|&nbsp;&nbsp;<A CLASS=menu HREF="javascript:changeMode('0')">Режим ASCII</A>&nbsp;&nbsp;
					|&nbsp;&nbsp;<A CLASS=menu HREF="javascript:changeMode('1')">Режим Binary</A>&nbsp;&nbsp;
					]
					<?php } else { ?>
					[&nbsp;&nbsp;<A CLASS=menu HREF="<?=$PHP_SELF;?>?logoff=true">Повторить</A>&nbsp;&nbsp;]
					<?php } ?>
				</TD>
				<TD CLASS=menu ALIGN=RIGHT>
					<FORM METHOD=POST NAME="currentMode">
					Текущий режим :<INPUT TYPE='text' NAME='showmode' VALUE='<?=$mode==1?"FTP_BINARY":"FTP_ASCII";?>' STYLE='border: none; background-color: #cfcfbb; text-align: right; size:200px;' ALIGN=RIGHT></TD>
					</FORM>
			</TR>
			<TR><TD><?=$msg;?></TD><TD ALIGN=RIGHT><?php print ($loggedOn)?"Подключён к $server:$port ($systype)":"Not connected";?></TD></TR>
		</TABLE>

		<FORM NAME="actionform" METHOD=POST ACTION='<?=$PHP_SELF;?>'>
			<INPUT TYPE='hidden' NAME='action' VALUE=''>
			<INPUT TYPE='hidden' NAME='currentDir' VALUE='<?=$currentDir;?>'>
			<INPUT TYPE='hidden' NAME='file' VALUE=''>
			<INPUT TYPE='hidden' NAME='file2' VALUE=''>
			<INPUT TYPE='hidden' NAME='permissions' VALUE=''>
			<INPUT TYPE='hidden' NAME='mode' VALUE='<?=$mode;?>' STYLE='border: none; background-color: #EFEFEF;'>
		</FORM>
		<HR>

		<TABLE CELLPADDING=2 CELLSPACING=0>
			<TR>
				<!-- Goto directory -->
				<FORM NAME='cdDirect' METHOD=POST ACTION='<?=$PHP_SELF;?>'>
				<INPUT TYPE='hidden' NAME='action' VALUE='cd'>
				<INPUT TYPE='hidden' NAME='currentDir' VALUE='<?=$currentDir;?>'>
				<TD VALIGN=TOP>
					<INPUT TYPE="text" NAME="file" VALUE="">
				</TD>
				<TD VALIGN=TOP>
					<INPUT TYPE="SUBMIT" VALUE="Перейти к папке" STYLE='width=120;'>
				</TD>
				</FORM>
			</TR>
			<TR>
				<!-- Create directory -->
				<FORM  METHOD=POST NAME='dirinput' ACTION="<?=$PHP_SELF;?>">
				<TD VALIGN=TOP>
					<INPUT TYPE="text" NAME="directory" VALUE="">
				</TD>
				<TD VALIGN=TOP>
					<INPUT TYPE="BUTTON" VALUE="Создать папку" OnClick='javascript:createDirectory(dirinput.directory.value)' STYLE='width=120;'>
				</TD>
				</FORM>
			</TR>
			<TR>
				<FORM NAME='putForm' ENCTYPE="multipart/form-data" METHOD=POST ACTION="<?=$PHP_SELF;?>">
				<INPUT TYPE="hidden" NAME="action" VALUE="put">
				<INPUT TYPE='hidden' NAME='currentDir' VALUE='<?=$currentDir;?>'>
				<INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="2000000">
				<INPUT TYPE='hidden' NAME='mode' VALUE='<?=$mode;?>'>
				<TD VALIGN=TOP>
					<INPUT TYPE="file" NAME="file" STYLE="width:250px;">
				</TD>
				<TD VALIGN=TOP>
					<INPUT TYPE="SUBMIT" VALUE="Загрузить файл" STYLE='width=120;'>
				</TD>
				</FORM>
			</TR>
		</TABLE>
		<HR>
		<P>
		<?php
			$list=Array();
			$list=ftp_rawlist($connection, "");
		?>
		<TABLE>
		<TR>
			<TD><IMG SRC="img/parent.gif" HEIGHT=20 WIDTH=20 ALIGN=TOP></TD>
			<TD ALIGN=LEFT COLSPAN=7><A HREF='javascript:submitForm("cd","..")'>..</A></TD>
		</TR>
		<?php
			$list = parse_ftp_rawlist($list, $systype);
			if (is_array($list))
			{
				// Directories
				foreach($list as $myDir)
				{
					if ($myDir["is_dir"]==1)
					{
						$fileAction = "cd";
						$fileName = $myDir["name"];
						print "<TR>\n";
						print "<TD><IMG SRC=img/folder.gif ALIGN=TOP></TD>\n";
						print "<TD><A HREF='javascript:submitForm(\"cd\",\"". $fileName . "\")'>". $myDir["name"] . "</A></TD>\n";
						print "<TD ALIGN=RIGHT>". $myDir["size"] . "</TD>\n";
						print "<TD>". $myDir["date"] . "</TD>\n";
						print "<TD>". $myDir["perms"] . "</TD>\n";
						print "<TD>". $myDir["user"] . "</TD>\n";
						print "<TD>". $myDir["group"] . "</TD>\n";
						print "<TD><A HREF='javascript:Confirmation(\"" . $PHP_SELF . "?action=deldir&file=". $myDir["name"] . "&currentDir=". $currentDir . "\")'><IMG SRC=img/delete.gif BORDER=0 ALT=\"Delete\"></A></TD>\n";
						print "<TD><A HREF='javascript:renameFile(\"" . $myDir["name"] . "\")'><IMG SRC=img/rename.gif BORDER=0 ALT=\"Rename\"></A></TD>\n";
						print "<TD>";
						print "<A HREF='javascript:;' OnClick='window.open(\"setpermission.php?file=" . $fileName . "&perms=" . $myDir["perms"] . "\",\"permissions\",\"width=250,height=150,scrollbars=no,menubar=no,status=yes,directories=no,location=no\")'><IMG SRC='img/settings.gif' WIDTH='20' HEIGHT='20' BORDER=0 ALT='Change permissions'></A>";
						print "</TD>\n";
						print "</TR>\n";
					}
				}

				// Links
				foreach($list as $myDir)
				{
					if ($myDir["is_link"]==1)
					{
						$fileAction = "cd";
						$fileName = $myDir["target"];
						print "<TR>\n";
						print "<TD><IMG SRC=img/link.gif ALIGN=TOP></TD>\n";
						print "<TD><A HREF='javascript:submitForm(\"cd\",\"". $fileName . "\")'>". $myDir["name"] . "</A></TD>\n";
						print "<TD ALIGN=RIGHT>". $myDir["size"] . "</TD>\n";
						print "<TD>". $myDir["date"] . "</TD>\n";
						print "<TD>". $myDir["perms"] . "</TD>\n";
						print "<TD>". $myDir["user"] . "</TD>\n";
						print "<TD>". $myDir["group"] . "</TD>\n";
						print "<TD><A HREF='javascript:Confirmation(\"" . $PHP_SELF . "?action=deldir&file=". $myDir["name"] . "&currentDir=". $currentDir . "\")'><IMG SRC=img/delete.gif BORDER=0 ALT=\"Delete\"></A></TD>\n";
						print "<TD><A HREF='javascript:renameFile(\"" . $myDir["name"] . "\")'><IMG SRC=img/rename.gif BORDER=0 ALT=\"Rename\"></A></TD>\n";
						print "<TD>";
						print "Ссылка ". $myDir["target"];
						print "</TD>\n";
						print "</TR>\n";
					}
				}

				// Files
				foreach($list as $myDir)
				{
					if ($myDir["is_link"]!=1 && $myDir["is_dir"]!=1)
					{
						$fileAction = "get";
						$fileName = $myDir["name"];
						print "<TR>\n";
						print "<TD><IMG SRC=img/file.gif ALIGN=TOP></TD>\n";
						print "<TD><A HREF='javascript:submitForm(\"get\",\"". $fileName . "\")'>". $myDir["name"] . "</A></TD>\n";
						print "<TD ALIGN=RIGHT>". $myDir["size"] . "</TD>\n";
						print "<TD>". $myDir["date"] . "</TD>\n";
						print "<TD>" . $myDir["perms"] . "</TD>\n";
						print "<TD>". $myDir["user"] . "</TD>\n";
						print "<TD>". $myDir["group"] . "</TD>\n";
						print "<TD><A HREF='javascript:Confirmation(\"" . $PHP_SELF . "?action=delfile&file=". $myDir["name"] . "&currentDir=". $currentDir . "\")'><IMG SRC=img/delete.gif BORDER=0 ALT=\"Delete\"></A></TD>\n";
						print "<TD><A HREF='javascript:renameFile(\"" . $myDir["name"] . "\")'><IMG SRC=img/rename.gif BORDER=0 ALT=\"Rename\"></A></TD>\n";
						print "<TD>";
						print "<A HREF='javascript:;' OnClick='window.open(\"setpermission.php?file=" . $fileName . "&perms=" . $myDir["perms"] . "\",\"permissions\",\"width=250,height=150,scrollbars=no,menubar=no,status=yes,directories=no,location=no\")'><IMG SRC='img/settings.gif' WIDTH='20' HEIGHT='20' BORDER=0 ALT='Change permissions'></A>";
						print "</TD>\n";

						if (strtolower($myDir["extension"]) == "zip")
						{
							print "<TD><A HREF='javascript:ConfirmationUnzip(\"" . $PHP_SELF . "?action=unzipfile&file=". $myDir["name"] . "&currentDir=". $currentDir . "\")'><IMG SRC=img/zip.gif BORDER=0 ALT=\"Unzip\"></A></TD>\n";
						} else {
							echo "<TD>&nbsp;</td>";
						}

						print "</TR>\n";
					}
				}
			}
			print "	</TABLE>";
		}
		else
		{
			if(!isset($msg))
			{
				$msg = "Could not connect to server $server:$port with user $user<P><A HREF='" . $PHP_SELF . "?logoff=true'>Try again...</A>";
			}
?>
<HTML>
<HEAD>
	
	<SCRIPT LANGUAGE="JavaScript" SRC="include/script.js"></SCRIPT>
</HEAD>
<BODY>
<?php
			print $msg;
		}
	}
	else // Still need to logon...
	{
?>
<HTML>
<HEAD>
	<SCRIPT LANGUAGE="JavaScript" SRC="include/script.js"></SCRIPT>
</HEAD>
<BODY>
		<TABLE BORDER=0 CELLPADDING=2 CELLSPACING=0 WIDTH='100%'>
			<TR>
				<TD CLASS=menu>
					<B>Online FTP</B>
				</TD>
			</TR>
		</TABLE>

		<FORM NAME=logon action='<?=$PHP_SELF;?>' METHOD=POST>
		<TABLE>
			<TR>
				<TD>Сервер</TD>
				<TD><INPUT TYPE=TEXT NAME=server SIZE=18>&nbsp;Port : <INPUT TYPE=TEXT NAME=port SIZE=6 VALUE=21></TD>
			</TR>
			<TR>
				<TD>Имя пользователя</TD>
				<TD>
					<INPUT TYPE=TEXT NAME=user SIZE=18>
					<INPUT TYPE="checkbox" NAME="anonymous" VALUE=1 OnClick="anonymousAccess()"> : Anonymous access
				</TD>
			</TR>
			<TR>
				<TD>Пароль</TD>
				<TD><INPUT TYPE=PASSWORD NAME=password SIZE=18></TD>
			</TR>
			<TR>
				<TD COLSPAN=2 ALIGN=CENTER><INPUT TYPE=SUBMIT VALUE="Войти"></TD>
			</TR>
		</TABLE>
		</FORM>
<?php
	}
?>
</BODY>
</HTML>
<?
echo '<br /><img src="'.BASEDIR.'../images/img/act_home.gif" alt="image" /> <a href="'.BASEDIR.'../index.php?'.SID.'">На главную</a>';

include_once "../themes/".$config['themes']."/foot.php";
?>