Location: PHPKode > projects > web-cp - Web Hosting Control Panel > webcp/web/personal/file_mng.phps
<?	/*
	// File:	file_mng.inc.phps
	// Purpose:	internal ftp client
	// Creation:	2001-10-07 @ 2001-12-10
	// Author:	Tom|420
	//		Jonathan Haskins <hide@address.com>
	*/

$error_level = error_reporting(E_ALL);

if (!@is_dir(getenv('TMPDIR'))) {
    putenv('TMPDIR='.$cfg['tmpdir']);
}

// assign common code to variables so we can eval() them later

$CONNECT_TO_FTP = '
	if (version_compare(phpversion(), \'4.2\', \'<\'))
		$FTPid = @ftp_connect($cfg[\'ftpsysname\']);
	else {
		if (!$FTPid = @ftp_connect($cfg[\'ftpsysname\'], $cfg[\'ftpsysport\'], 8)) {
			sleep(2);
			$FTPid = @ftp_connect($cfg[\'ftpsysname\'], $cfg[\'ftpsysport\'], 16);
		}
	}

	$login_result = @ftp_login($FTPid, $personaldata[\'username\'], $personaldata[\'password\']);
	if (!$FTPid || !$login_result) {
		echo \'<p>\'.$T[\'FTPerror\'].\'!</p>\';
		@ftp_quit($FTPid);
		return;
	}
';


// ************************** CONFIG *******************************************

// ftp server has chrooted environment (true|false)
$ftp_chrooted = true;

// 2000-12-31 4:20:00
$dateformat  = 'Y-m-d G:i:s';

// number of lines of text to show at once in the viewer
$fviewlen = 200;

// Icons
$icon_dir = '/icon/';
$s_icon_size = 16;  // width/height of small icons
$l_icon_size = 34;  // width/height of large icons

$icons['lfolder']  = 'folder34.gif';		// folders
$icons['sfolder']  = 'folder16.gif';
$icons['lofolder'] = 'folder_open34.gif';	// open folder
$icons['sofolder'] = 'folder_open16.gif';
$icons['lhomefld'] = 'folder_home34.gif';	// link to user's root
$icons['shomefld'] = 'folder_home16.gif';
$icons['lhtmlfld'] = 'folder_html34.gif';	// /web/
$icons['shtmlfld'] = 'folder_html16.gif';
$icons['llckfld']  = 'folder_locked34.gif';	// dir protected by .htaccess
$icons['slckfld']  = 'folder_locked16.gif';
$icons['sparent']  = 'parent16.gif';
$icons['lhtml']    = 'html34.gif';
$icons['shtml']    = 'html16.gif';
$icons['ltxt']     = 'txt34.gif';
$icons['stxt']     = 'txt16.gif';
$icons['limg']     = 'image34.gif';
$icons['simg']     = 'image16.gif';
$icons['larc']     = 'archive34.gif';
$icons['sarc']     = 'archive16.gif';
$icons['lmedia']   = 'video34.gif';
$icons['smedia']   = 'video16.gif';
$icons['llog']     = 'log34.gif';
$icons['slog']     = 'log16.gif';
$icons['ldoc']     = 'document34.gif';
$icons['sdoc']     = 'document16.gif';
$icons['lother']   = 'unknown34.gif';
$icons['sother']   = 'unknown16.gif';

$file_icon = '/icon/document.gif';	// small icon for files
$pfolder_icon = '/icon/parent.gif';	// small icon for cdup

// Extensions for file types (passed as a regex that checks end of filename)

$file_types['html']	= '[spd]?html?|php[3s]?|cgi|perl|pl|asp|asa';
$file_types['txt']	= 'te?xt|asc|ascii';
$file_types['img']	= 'gif|jpe?g|[pm]ng|bmp';
$file_types['arc']	= 'zip|tar|t?gz|ace|[pr]ar|arj';
$file_types['media']	= 'mpe?g|mov|avi|asf|ram?|rm|wmv|mp3|wav|ogg';
$file_types['doc']	= 'htaccess|htpasswd|js|css|sql|xml';
$file_types['log']	= 'access|error|log';


// ********************** INIT **********************************

if (empty($dir)) $dir = '';
if (empty($act)) $act = '';

// remove any ../ from requested file so they can't
// ascend into forbidden directories
$dir = str_replace('../', '', rtrim($dir, '/').'/');

// strip possible leading and trailing slashes from dir
$dir = trim($dir, '/');

if ($ftp_chrooted) {
	$currentdir = '/'.$dir;
} else {
	// strip possible trailing slash from rootdir
	$rootdir = rtrim($personaldata['root'], '/');

	if (!empty($dir)) {
		$currentdir = $rootdir.'/'.$dir;
	} else {
		$currentdir = $rootdir;
	}
}

$domain = $domaindata['host'].'.'.$domaindata['domain'];
$webpath = ereg_replace("^/web/", '', strstr($currentdir, '/web/'));


// *****************************************************************************
// *** ACTION: newdir (new directory)
// *** creates a new directory
if ($act == 'newdir') {

	// Shows the form for creating a new directory
	if (!isset($_POST['confirm'])) {
		?>
		<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>&act=newdir" method="post">

		<p><?=$T['Create new directory in: '];?><b><?=$currentdir?></b></p>
		<p><?=$T['Name: '];?><input type="text" name="dir_name" size="50"></p>
		<table><tr>
		<td>
			<input type="submit" name="confirm" value="<?=$T['Create']?>">
			</form>
		</td>
		<td>
			<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>" method="post">
			<input type="submit" name="cancel" value="<?=$T['Cancel']?>">
			</form>
		</td>
		</tr></table>
		<?

	}

	// Create a new directory
	elseif ($userdata['type'] != 'demo') {

		eval($CONNECT_TO_FTP);

		if (@ftp_mkdir($FTPid, $currentdir.'/'.$dir_name)) {
			echo '<p>'.$T['Successfully created '].$T['Dir']." <b>$currentdir/$dir_name</b></p>\n";
		} else {
			echo '<p>'.$T['Failed to create '].$T['Dir']." <b>$currentdir/$dir_name</b>! ".$T['IncorrectPermission']."</p>\n";
		}

		@ftp_quit($FTPid);

		echo '<p><a href="/?url='.$url.'&cp='.$cp.'&user='.$user.'&dir='.$dir.'">'.$T['Return'].'</a>'."\n";
	} else {
		echo "<center>".$T['err']['demo user']."<br><a href='/?url=$url&cp=$cp&user=$user&dir=$dir'>".$T['Return']."</a></center>\n";
	}
}

// *****************************************************************************
// *** ACTION: upload (file upload)
// *** Receives uploaded files, transfers from tmp dir to final dir through FTP
elseif ($act == 'upload') {

	// Shows the form for selecting files to upload
	if (!isset($_POST['confirm'])) {
		if (!isset($upload_files_num)) { 
			$upload_files_num = $cfg['upload_files_num']; 
		} else {
			$upload_files_num = intval($upload_files_num);
		}
		?>
		<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>&act=upload" method="post">
		<?=$T['Files to upload: '];?><select name="upload_files_num" onchange="form.submit();">
		<?
			for ($i=1; $i<=20; $i++) {
				echo '<option value="'.$i.'"';
				if ($upload_files_num == $i) { echo " selected "; }
				echo '>'.$i.'</option>\n';
			}
		?>
		</select>
		</form>
		<form enctype="multipart/form-data" action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>&act=upload" method="post">
		<table border="0" width="100%">
		<?
		for ($i = 1; $i <= $upload_files_num; $i++) {
			echo '<tr><td align=right width=60>'.$T['File']." ".$i.': </td><td><input type="file" name="uplfile'.$i.'" size="50"></td></tr>'."\n";
		}
		?>
		</table>
		<table><tr>
		<td>
			<input type="submit" name="confirm" value="<?=$T['Upload']?>">
			</form>
		</td>
		<td>
			<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>" method="post">
			<input type="submit" name="cancel" value="<?=$T['Cancel']?>">
			</form>
		</td>
		</tr></table>
		<?
	}

	elseif ($userdata['type'] != 'demo') {
		eval($CONNECT_TO_FTP);

		// upload each temp file to the new path as the original name	
		foreach($_FILES AS $val) {
			if (file_exists($val['tmp_name'])) {
				if (ereg('^(txt|html|log|doc)$', ftp_file_type($val['name'], $file_types)))
					$mode = FTP_ASCII;
				else
					$mode = FTP_BINARY;
	
				if (@ftp_put($FTPid, $currentdir.'/'.$val['name'], $val['tmp_name'], $mode))
					echo '<p>'.$T['UploadSuccess'].' <b>'.$val['name']."</b></p>\n";
				else
					echo '<p>'.$T['UploadError'].' <b>'.$val['name']."</b></p>\n";
			}
		}

		@ftp_quit($FTPid);

		echo '<p><a href="/?url='.$url.'&cp='.$cp.'&user='.$user.'&dir='.$dir.'">'.$T['Return'].'</a>'."\n";
	} else {
		echo "<center>".$T['err']['demo user']."<br><a href='/?url=$url&cp=$cp&user=$user&dir=$dir'>".$T['Return']."</a></center>\n";
	}
}

//******************************************************************************
// *** ACTION: hta
// *** Edit .htaccess file


elseif ($act == 'hta') {

	eval($CONNECT_TO_FTP);

	echo eval($CONNECT_TO_FTP);

	$tmp_name = '/tmp/php'.uniqid(getmypid());
	$tmp2_name = '/tmp/php'.uniqid(getmypid());
	
    if (isset($_POST['htpasswd']) && ($userdata['type'] != 'demo'))   {
		// make .htaccess and .htpasswd
	
		$htpath = $cfg['webdir'] . "/" . $domaindata['domain'] . $currentdir . "/.htpasswd";


	if ((!empty($htuser)) && (!empty($htpasswd)))
		// not sure if a isset is required also?
		/* It seems that for hotlink $hotlink is not created unless box is ticked, must be because its a checkbox....*/
		{

		if (($htuser != "") && ($htpasswd != "")) {
			$htaccess_txt  = "AuthType Basic" . "\n";
			$htaccess_txt .= "AuthName protected-area" . "\n";
			$htaccess_txt .= "AuthUserFile $htpath" . "\n";
			$htaccess_txt .= "require valid-user" . "\n";

	        $htpasswd_txt = "";
    	    $htpasswd_txt .= "$htuser:".crypt($htpasswd,CRYPT_STD_DES)."\n";
    	}
    }
    	
    	if ((isset($hotlink)) && ($hotlink == "on")) {
    		$htaccess_txt .= "RewriteEngine On\n";
			$htaccess_txt .= "RewriteCond %{HTTP_REFERER} !^$\n";
			$htaccess_txt .= "RewriteCond %{HTTP_REFERER} !^http://(".$domaindata['host']."\.)?".$domaindata['domain']."/.*$ [NC]\n";
			$htaccess_txt .= "RewriteRule \.(gif|jpg)$ - [F]\n";
		}
        
		if ($fp = fopen($tmp_name, 'w')) {
			fwrite($fp, $htaccess_txt, strlen($htaccess_txt));
			fclose($fp);
		}
		if ($fp2 = fopen($tmp2_name, 'w')) {
			fwrite($fp2, $htpasswd_txt, strlen($htpasswd_txt));
			fclose($fp2);
		}

		if (@ftp_put($FTPid, $currentdir.'/.htaccess', $tmp_name, FTP_ASCII)) {
			echo "<p><b>$currentdir/.htaccess ".$T['edited successfully'].".</b></p>\n";
		} else {
			echo '<p>'.$T['IncorrectPermission']."</p>\n";
		}

		if (@ftp_put($FTPid, $currentdir.'/.htpasswd', $tmp2_name, FTP_ASCII)) {
			echo "<p><b>$currentdir/.htpasswd ".$T['edited successfully'].".</b></p>\n";
		} else {
			echo '<p>'.$T['IncorrectPermission']."</p>\n";
		}

		echo "<p><a href='/?url=$url&cp=$cp&user=$user&dir=$dir'>".$T['Return']."</a></p>\n";
		
	} elseif (isset($_POST['htpasswd']) && ($userdata['type'] == 'demo')) {
		echo "<center>".$T['err']['demo user']."<br><a href='/?url=$url&cp=$cp&user=$user&dir=$dir'>".$T['Return']."</a></center>\n";
	}

	// otherwise display form
	else { ?>
		<img src="<?=$icon_dir?><?=$icons['lofolder']?>" width="<?=$l_icon_size?>" height="<?=$l_icon_size?>"><b><?=$currentdir?></b>
		<p>

		<? if (@ftp_get($FTPid, $tmp_name, $currentdir.'/.htaccess', FTP_ASCII)) {
			echo $T['Caution: .htaccess file exists and will be overwritten.'];
		} ?>
		</p>
		<form action="/?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&act=hta&dir=<?=$dir?>" method="post">
		<table width=450>
		<tr>
			<Td colspan="3"><?=$T['Password protect this directory: '];?></td>
		</tr>
		<tr>
			<td class="normal" align=right><?=$T['Username'];?>: </td><td><input type="text" name="htuser"></td>
		</tr>
		<tr>
			<td class="normal" align=right><?=$T['Password'];?>: </td><td><input type="text" name="htpasswd"></td>
		</tr>
		<tr>
			<Td colspan="3">&nbsp;</td>
		</tr>
		<tr>
			<Td colspan="3"><?=$T['Hot-link Image Protection: '];?><input type="checkbox" name="hotlink"></td>
		</tr>
		<tr>
			<Td colspan="3">&nbsp;</td>
		</tr>
		<tr>
			<td colspan="1" align="right"><input type="submit" value="<?=$T['Protect'];?>" class="check">
			</form>
		</td>
		<td align="left">
			<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>" method="post">
			<input type="submit" name="cancel" value="<?=$T['Cancel']?>">
			</form>
            </td>
		</tr>
		</table>


		<?
	}

	@ftp_quit($FTPid);

        if (file_exists($tmp_name)) {unlink($tmp_name);}
        if (file_exists($tmp2_name)) {unlink($tmp2_name);}

}



//******************************************************************************
// *** ACTION: view
// *** Displays the content of a file

elseif ($act == 'view') {

	$file_type = ftp_file_type(trim($file), $file_types);

	echo '<p><img src="'.$icon_dir.$icons['l'.$file_type].'" width="'.$l_icon_size.'" height="'.
		$l_icon_size.'"> <b>'.$currentdir.'/'.$file."</b></p>\n";

	if ($file_type == 'img') {
		echo '<img src="http://'.$domain.'/'.trim($webpath.'/'.$file, '/').'" border="1">';
	} elseif (ereg('^(txt|html|log|doc)$', $file_type)) { 
		if (!isset($fstart)) $fstart = '0';

		eval($CONNECT_TO_FTP);

		$tmp_name = '/tmp/php'.uniqid(getmypid());
		if (!@ftp_get($FTPid, $tmp_name, $currentdir.'/'.$file, FTP_ASCII)) {
			echo 'error: viewing file';
		}

		@ftp_quit($FTPid);

		if ($fp = fopen($tmp_name, 'r')) {
			echo '<div style="border-style: solid; border-width: 1px;">';
			echo '<font size="1"><b>';
			$flast = $fstart + $fviewlen;
			$fpos = 0;
			while (!feof($fp)) {
			$nextline = fgets ($fp, 4096);
				if ($fpos >= $fstart) {
					echo nl2br(str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;",
						str_replace("  ","&nbsp;&nbsp;",htmlentities($nextline))));
				}
				if ($fpos++ >= $flast) break;
			}
			echo "</b></font>\n";
			echo '</div>';

			echo '<p>';
			if ($fstart > 0)
				echo "<a href='/?url=$url&cp=$cp&user=$user&act=view&dir=$dir&file=$file&fstart=".
					($fstart-$fviewlen)."'>".$T['Previous']."</a>\n";
			else
				echo $T['Previous'];

			echo ' - ';
			if (!feof($fp))
				echo "<a href='/?url=$url&cp=$cp&user=$user&act=view&dir=$dir&file=$file&fstart=$flast'>".
					$T['Next']."</a>\n";
			else
				echo $T['Next'];
			echo '</p>';
		}
		fclose ($fp);
		unlink($tmp_name);

	} else {
		echo '<p><a href="http://'.$domain.'/'.trim($webpath.'/'.$file, '/').'">'.$T['Download'].'</a></p>'."\n";
	}

	echo "<p><a href='/?url=$url&cp=$cp&user=$user&dir=$dir'>".$T['Return']."</a></p>\n";
}

//******************************************************************************
// *** ACTION: delete
// *** remove the selected files from the server
elseif (isset($_REQUEST['delete'])) {

	// display confirmation form
	if (!isset($_POST['confirm'])) {
		?>
		<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>&delete=1" method="post">
		<?
		foreach($_POST['files'] AS $file => $attrib) {
			?>
			<input type="hidden" name="files[<?=$file?>]" value="<?=$attrib?>">
			<p><?=$T['DeleteFile']?> <b><?=$currentdir.'/'.$file?></b>?</p>
			<p>
			<?
		}
		?>
		<table><tr>
		<td>
			<input type="submit" name="confirm" value="<?=$T['Delete']?>">
			</form>
		</td>
		<td>
			<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>" method="post">
			<input type="submit" name="cancel" value="<?=$T['Cancel']?>">
			</form>
		</td>
		</tr></table>
		<?
	}

	// Do actual delete
	elseif ($userdata['type'] != 'demo') {
		eval($CONNECT_TO_FTP);

		foreach($_POST['files'] AS $file => $attrib) {
			if ($attrib == 'd') {
				// is dir, for now dir must be empty but we should eventually do a recursive delete
				if (@ftp_rmdir($FTPid, $currentdir.'/'.$file)) {
					echo '<p>'.ucfirst($T['Dir'])." <b>$currentdir/$file</b> ".$T['DeleteSuccess']."</p>\n";
				} else {
					echo '<p>'.$T['DeleteError'].' '.$T['Dir']." <b>$currentdir/$file</b>! ".$T['IncorrectPermission']."</p>\n";
				}
			} elseif ($attrib == 'f') {
				// is file
				if (@ftp_delete($FTPid, $currentdir.'/'.$file)) {
					echo '<p>'.ucfirst($T['File'])." <b>$currentdir/$file</b> ".$T['DeleteSuccess']."</p>\n";
				} else {
					echo '<p>'.$T['DeleteError'].' '.$T['File'].' <b>'." <b>$currentdir/$file</b>! ".$T['IncorrectPermission']."</p>\n";
				}
			}
		}
		@ftp_quit($FTPid);

		echo '<p><a href="./?url='.$url.'&cp='.$cp.'&user='.$user.'&dir='.$dir.'">'.$T['Return'].'</a></p>'."\n";
	} elseif ($userdata['type'] == 'demo') {
		echo "<center>".$T['err']['demo user']."<br><a href='/?url=$url&cp=$cp&user=$user&dir=$dir'>".$T['Return']."</a></center>\n";
	}
}

// *****************************************************************************
// *** ACTION: rename (rename)
// *** Renames files or directories
elseif (isset($_REQUEST['rename'])) {

	if (!isset($_POST['confirm'])) {
		?>
		<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>=&act=ren&dir=<?=$dir?>&rename=1?>" method="post">
		<?
		foreach($_POST['files'] AS $file => $attrib) {
			?>
			<p><?=$T['Rename']?> <b><?=$currentdir.'/'.$file?></b> <?=$T['To']?>:<p>
			<input type="text" name="files[<?=$file?>]" value="<?=$file?>" size="50">
			<?
		}

		?>
		<table><tr>
		<td>
			<input type="submit" name="confirm" value="<?=$T['Rename']?>">
			</form>
		</td>
		<td>
			<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>" method="post">
			<input type="submit" name="cancel" value="<?=$T['Cancel']?>">
			</form>
		</td>
		</tr></table>
		<?

	}

	elseif ($userdata['type'] != 'demo') {
		eval($CONNECT_TO_FTP);

		foreach($_POST['files'] AS $file => $attrib) {
			if (@ftp_rename($FTPid, $currentdir.'/'.$file, $currentdir.'/'.$attrib)) {
				echo "<p>".$T['RenameSuccess']." ".$T['File']." <b>$currentdir/$file</b> ".$T['To']." <b>$currentdir/$attrib</b></p>";
			} else {
				echo '<p>'. $T['RenameError']." <b>$currentdir/$file</b>! ".$T['IncorrectPermission']."!</p>";
			}
		}

		@ftp_quit($FTPid);

		echo "<p><a href='./?url=$url&cp=$cp&user=$user&dir=$dir'>".$T['Return']."</a></p>\n";
	} elseif ($userdata['type'] == 'demo') {
		echo "<center>".$T['err']['demo user']."<br><a href='/?url=$url&cp=$cp&user=$user&dir=$dir'>".$T['Return']."</a></center>\n";
	}
}
	
// *****************************************************************************
// *** ACTION: chmod
// *** Perfoms permission changes, using FTP to run as the file owner
elseif (isset($_REQUEST['chmod'])) {

	// Shows form with checkboxes to select new permissions
	if (!isset($_POST['confirm'])) {
		// TO DO: check the boxes according to current file permissions
		?>
		<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&act=chmod&dir=<?=$dir?>&chmod=1" method="post">

		<? foreach ($_POST['files'] AS $file => $attrib) { ?>
			<p><b><?=$T['Chmod']?> <?=$currentdir.'/'.$file?></b></p>
			<table><tr>
			<td><b><?=$T['Owner']?>:</b></td>
			<td><input class="check" type="checkbox" name="files[<?=$file?>][user_read]"><?=$T['Read']?></td>
			<td><input class="check" type="checkbox" name="files[<?=$file?>][user_write]"><?=$T['Write']?></td>
			<td><input class="check" type="checkbox" name="files[<?=$file?>][user_exe]"><?=$T['Execute']?></td>
			</tr><tr>
			<td><b><?=$T['Group']?>:</b></td>
			<td><input class="check" type="checkbox" name="files[<?=$file?>][group_read]"><?=$T['Read']?></td>
			<td><input class="check" type="checkbox" name="files[<?=$file?>][group_write]"><?=$T['Write']?></td>
			<td><input class="check" type="checkbox" name="files[<?=$file?>][group_exe]"><?=$T['Execute']?></td>
			</tr><tr>
			<td><b><?=$T['World']?>:</b></td>
			<td><input class="check" type="checkbox" name="files[<?=$file?>][world_read]"><?=$T['Read']?></td>
			<td><input class="check" type="checkbox" name="files[<?=$file?>][world_write]"><?=$T['Write']?></td>
			<td><input class="check" type="checkbox" name="files[<?=$file?>][world_exe]"><?=$T['Execute']?></td>
			</tr></table>
		<? } ?>
		<table><tr>
		<td>
			<input type="submit" name="confirm" value="<?=$T['Modify']?>">
			</form>
		</td>
		<td>
			<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>" method="post">
			<input type="submit" name="cancel" value="<?=$T['Cancel']?>">
			</form>
		</td>
		</tr></table>
		<?
	}

	elseif ($userdata['type'] != 'demo') {
		eval($CONNECT_TO_FTP);

		foreach ($_POST['files'] AS $file => $attrib) {
			$newmode['u'] = 0;
			$newmode['g'] = 0;
			$newmode['w'] = 0;

			if (isset($attrib['user_read'])) $newmode['u'] += 4;
			if (isset($attrib['user_write'])) $newmode['u'] += 2;
			if (isset($attrib['user_exe'])) $newmode['u'] += 1;

			if (isset($attrib['group_read'])) $newmode['g'] += 4;
			if (isset($attrib['group_write'])) $newmode['g'] += 2;
			if (isset($attrib['group_exe'])) $newmode['g'] += 1;

			if (isset($attrib['world_read'])) $newmode['w'] += 4;
			if (isset($attrib['world_write'])) $newmode['w'] += 2;
			if (isset($attrib['world_exe'])) $newmode['w'] += 1;

			$new_mod = $newmode['u'].$newmode['g'].$newmode['w'];

			if (@ftp_site($FTPid, "chmod $new_mod $currentdir/$file")) {
				echo '<p>'.$T['ChmodSuccess']." <b>$currentdir/$file</b> ".$T['To']." ".$new_mod."</p>\n";
			} else {
				echo '<p>'.$T['ChmodError']." <b>$currentdir/$file</b>! ".$T['IncorrectPermission']."</p>\n";
			}
		}

		echo "<p><a href='/?url=$url&cp=$cp&user=$user&dir=$dir'>".$T['Return']."</a></p>\n";

		@ftp_quit($FTPid);
	} else {
		echo "<center>".$T['err']['demo user']."<br><a href='/?url=$url&cp=$cp&user=$user&dir=$dir'>".$T['Return']."</a></center>\n";
	}
}

// *****************************************************************************
// *** ACTION: (none) -> Directory listing
// *** List directories and files in current directory
else {

	eval($CONNECT_TO_FTP);

	// get directory listing
	if (!$list = @ftp_rawlist($FTPid, '-a '.$currentdir)) {
		echo '<p>'.$T['FTPerror'].'!</p>';
		@ftp_quit($FTPid);
		return;
	}

	@ftp_quit($FTPid);

	$filelist = array();

	foreach ($list AS $row) {

		// parse a standard unix ftp dir line into an array
		$exp = '/([pcdblsu-])'.		// 1. type
			'([rwxst-]{9})\s+'.	// 2. permissions
			'([0-9]+)\s+'.		// 3. links
			'(.+?)\s+'.		// 4. owner
			'(.+?)\s+'.		// 5. group
			'([0-9]+)\s+'.		// 6. size
			'([a-zA-Z]+)\s+'.	// 7. modified month
			'([0-9]+)\s+'.		// 8. modified day
			'([0-9:]+)\s+'.		// 9. modiefied time|year
			'(.+)/';		// 10. file name

		preg_match($exp, $row, $file);

		// get file's permissions
		$perm['u'] = 0;
		$perm['g'] = 0;
		$perm['o'] = 0;

		$u = substr($file[2], 0, 3);
		if (strpos($u, 'r') !== false) $perm['u'] += 4;
		if (strpos($u, 'w') !== false) $perm['u'] += 2;
		if (strpos($u, 'x') !== false) $perm['u'] += 1;
		$g = substr($file[2], 3, 3);
		if (strpos($g, 'r') !== false) $perm['g'] += 4;
		if (strpos($g, 'w') !== false) $perm['g'] += 2;
		if (strpos($g, 'x') !== false) $perm['g'] += 1;
		$o = substr($file[2], 6);
		if (strpos($o, 'r') !== false) $perm['o'] += 4;
		if (strpos($o, 'w') !== false) $perm['o'] += 2;
		if (strpos($o, 'x') !== false) $perm['o'] += 1;

		$stats['p'] = $perm['u'].$perm['g'].$perm['o'];

		// get file's size
		$stats['s'] = $file[6];

		// get the file's last modified date
		if (strpos($file[9], ':') !== false) {
			$stats['m'] = date($dateformat, strtotime($file[9].' '.$file[7].' '.$file[8]));
		} else {
			$stats['m'] = date($dateformat, strtotime($file[7].' '.$file[8].' '.$file[9]));
		}

		// get file's owner
		if ($pwuid = @posix_getpwuid($file[4]))
			$stats['o'] = $pwuid['name'];
		else
			$stats['o'] = "<i>".$T['none']."</i>";

		// get file's group
		if ($gruid = @posix_getgrgid($file[5]))
			$stats['g'] = $gruid['name'];
		else
			$stats['g'] = "<i>".$T['none']."</i>";

		// separate dirs into an array and files into another
		if ($file[10] != '.' && $file[10] != '..') {
			if (strpos($file[1], 'd') === 0) {
				$dirlist[] = $file[10];
				$dirstats[] = $stats;
			} elseif (strpos($file[1], 'l') === 0) {
				if (!$cfg['users_chrooted']) {
					if (is_dir($dir."/".realpath($file[10]))) {
						$dirlist[] = preg_replace("/ -> .*/", "", $file[10]);
						$dirstats[] = $stats;
					} else {
						$filelist[] = preg_replace("/ -> .*/", "", $file[10]);
						$filestats[] = $stats;
					}
				}
			} else {
				$filelist[] = $file[10];
				$filestats[] = $stats;
			}
		}
	}

        // Get cdup (parent directory)
        if ($dir) {
                $segs = explode ('/', $dir);

		$cdup = '';
                for ($i = 0; $i < count($segs) - 1; $i++) {
                        $cdup .= '/'.$segs[$i];
                }
		$cdup = substr($cdup, 1);
        }

	?>
	<form action="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>&file=<?=$file?>" method="post">

	<img src="<?=$icon_dir?><?=$icons['lofolder']?>" width="<?=$l_icon_size?>" height="<?=$l_icon_size?>"><b><?=$currentdir?></b>

	<p>
	<a href="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>&act=upload"><?=$T['Upload Files'];?></a> -
	<a href="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>&act=newdir"><?=$T['New Directory'];?></a> -
	<a href="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=$dir?>&act=hta"><?=$T['Protect Directory'];?></a>
	</p>

	<table width="100%" border="0" cellspacing="0" cellpadding="0" class="tblbg"><tr><td>
	<table width="100%" border="0" cellspacing="1">
 	<tr>
		<td class="tbltop"></td>
		<td class="tbltop"></td>
		<td class="tbltop"><?=$T['Filename'];?></td>
		<td class="tbltop"><?=$T['Size'];?></td>
		<td class="tbltop"><?=$T['Date/Time'];?></td>
		<td class="tbltop"><?=$T['Owner'];?></td>
		<td class="tbltop"><?=$T['Group'];?></td>
		<td class="tbltop"><?=$T['Mode'];?></td>
	</tr>
        <?

	// print out parent directory
	echo '<tr class="row1"><td></td><td><img src="'.$icon_dir.$icons['sparent'].'" width="'.$s_icon_size.
		'" height="'.$s_icon_size.'"></td><td colspan="6"><a href="./?url='.$url;
        if (isset($cdup)) {
		echo '&cp='.$cp.'&user='.$user.'&dir='.$cdup.'">'.$T['Parent Folder'].'</a></td></tr>'."\n";
        } elseif (isset($dir)) {
  		echo '&cp='.$cp.'&user='.$user.'">'.$T['Parent Folder'].'</a></td></tr>'."\n";
        }

	$i = 0;

	// print out list of directories
        if (isset($dirlist)) {
                sort($dirlist);
                foreach ($dirlist as $key => $nextfile) {
			// wordwrap dirname
                        $wrappedfile = wordwrap($nextfile, 25, '<br>', 1);

			?>
			<tr class="<?=(($i % 2) == 0 ? 'row2' : 'row1')?>" valign="top">
			<td><input type="checkbox" name="files[<?=$nextfile?>]" value="d"></td>
			<td><img src="<?=$icon_dir?><?=$icons['sfolder']?>" width="<?=$s_icon_size?>" height="<?=$s_icon_size?>"></td>
			<td style="font-size:12"><a href="./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&dir=<?=trim($dir.'/'.$nextfile, '/')?>"><?=$wrappedfile?></a></td>
			<td align="right" style="font-size:12" valign="top"><?=convert_bytes($dirstats[$key]['s'], $metric).strtoupper($metric)?></td>
			<td align="center" style="font-size:12"><?=$dirstats[$key]['m']?></td>
 			<td style="font-size:12"><?=$dirstats[$key]['o']?></td>
 			<td style="font-size:12"><?=$dirstats[$key]['g']?></td>
			<td style="font-size:12"><?=$dirstats[$key]['p']?></td>
			</tr>
			<?
			$i++;
                }
        }

	// print out list of files
        $totalfsize = 0;
        if (isset($filelist)) {
                sort($filelist);
                foreach ($filelist as $key => $nextfile) {
			// wordwrap filename
                        $wrappedfile = wordwrap($nextfile, 25, '<br>', 1);
			$icon = 's'.ftp_file_type($nextfile, $file_types);

                        ?>
			<tr class="<?=(($i % 2) == 0 ? 'row2' : 'row1')?>" valign="top">
			<td><input type="checkbox" name="files[<?=$nextfile?>]" value="f"></td>
			<td><img src="<?=$icon_dir?><?=$icons[$icon]?>" width="<?=$s_icon_size?>" height="<?=$s_icon_size?>"></td>
			<td style="font-size:12"><a href='./?url=<?=$url?>&cp=<?=$cp?>&user=<?=$user?>&act=view&dir=<?=$dir?>&file=<?=$nextfile?>'><?=$wrappedfile?></a></td>
			<td align="right" style="font-size:12"><?=convert_bytes($filestats[$key]['s'], $metric).strtoupper($metric)?></td>
			<td align="center" style="font-size:12"><?=$filestats[$key]['m']?></td>
			<td style="font-size:12"><?=$filestats[$key]['o']?></td>
			<td style="font-size:12"><?=$filestats[$key]['g']?></td>
			<td style="font-size:12"><?=$filestats[$key]['p']?></td>
			</tr>
			<?

                        $totalfsize += $filestats[$key]['s'];
			$i++;
                }
        }

	?>
	</table></td></tr></table>

        <p><?=convert_bytes($totalfsize, $metric).strtoupper($metric)?> <?=$T['allocated in'];?> <?=count($filelist)?> <?=$T['files'];?></p>

	<!--<input type="submit" name="move" value="<?=$T['Move'];?>">-->
	<input type="submit" name="chmod" value="<?=$T['Chmod']?>">
	<input type="submit" name="rename" value="<?=$T['Rename']?>">
	<input type="submit" name="delete" value="<?=$T['Delete']?>">
	</form>

	<?
}

error_reporting($error_level);
Return current item: web-cp - Web Hosting Control Panel