<?
########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

include_once( "../include/class.SQLString.php" );?><?include_once("auth.php");
include("ftp_header.php");

global $DEBUG;
#$DEBUG=1;

if ($DEBUG){
  $ftp->debug=1;
}

/* START - MKDIR */
if(strlen($mkdir)) {
  if($ftp->ftp_chdir($in_dir)) {
	if($ftp->ftp_mkdir($mkdir)) {
       addInfoBoxMsg($lang_text["wftp_mkdir_ok"],"|%inDir%|%mkdir%","|$in_dir|$mkdir");
    } else {
       addInfoBoxMsg($lang_text["wftp_mkdir_error"],"|%inDir%|%mkdir%","|$in_dir|$mkdir");
    }
    if($in_dir) $path = $in_dir;
  } else {
    addInfoBoxMsg($lang_text["wftp_chdir_failed"],"|%fullpath%","|$in_dir");
  }
}

/* END - MKDIR */

/* START - FILE-UPLOAD */
if($upld && $filename && $in_dir && is_uploaded_file($filename) ) {
  if($upld != $in_dir) {
    $in_dir = $in_dir;
  } else {
    $in_dir = $upld;
  }
  $path = $in_dir;
  if(substr($in_dir,-1) != "/") $in_dir .= "/";
  if($ftp->ftp_chdir($in_dir)) {
    if(strlen($new_name) && $new_name != $filename_name) {
      $filename_name = $new_name;
    }
    $tmpfile = $filename_name;
    $remotefile = $in_dir.$filename_name;
	
if ($DEBUG)	{
  echo "remotefile: $remotefile<br>\n";
}
    if($force != "1" && $ftp->ftp_file_exists($filename_name)) {
      addInfoBoxMsg($lang_text["wftp_file_exists"],"|%filename%","|$filename_name");
    } else {
      @copy($filename, $tmpfile);
	  $fd = fopen ($filename, "rb");
	  $contents = fread ($fd, filesize ($filename));
	  fclose ($fd);
      if($ftp->ftp_put($remotefile, $contents)) {
        addInfoBoxMsg($lang_text["wftp_upload_ok"],"|%filename%|%inDir%","|$filename_name|$in_dir");
      } else {
        addInfoBoxMsg($lang_text["wftp_upload_error"],"|%filename%|%inDir%","|$filename_name|$in_dir");
      }
      @unlink($tmpfile);
    }
  }
}
/* END - FILE-UPLOAD */

/* START - EDIT/NEW-FILE */
if($edit && $path && strlen($editnew_filename)) {
    /*
    if($edit != "new")
    {
	$trans = get_html_translation_table (HTML_ENTITIES);
        $trans = array_flip ($trans);
        $filecontent = preg_replace("/(\&[a-zA-Z0-9\#]+;)/es", "strtr('\\1',\$trans)", $filecontent);
    }
    //*/
    /*
    if( get_magic_quotes_gpc() )
    {
	//$filecontent = stripslashes( $filecontent );
	$filecontent = str_replace("\\\\", "\\", $filecontent);    
	$filecontent = str_replace('\"', '"', $filecontent);    
	$filecontent = str_replace("\\'", "'", $filecontent);    
	$filecontent = str_replace("\\".chr(0), chr(0), $filecontent);    
    } 
    //*/ 
  if(substr($path,-1) != "/") {
    $editnew_path = $path."/";
  } else {
    $editnew_path = $path;
  }
  
  if($ftp->ftp_chdir($path)) {
  	$editnew_fullpath = $editnew_path.$editnew_filename;
  	$tmpfile = $editnew_filename;
  	$fp = @fopen($tmpfile,"w");
  	$line = explode("\n",$filecontent);
  	for($i=0;$i<count($line);$i++) {
    	@fputs($fp,$line[$i]);
  	}
  	@fclose($fp);
  	if($force != "1" && $ftp->ftp_file_exists($editnew_filename)) {  	
    	addInfoBoxMsg($lang_text["wftp_file_exists"],"|%filename%","|$editnew_filename");
  	} 
  	else {
    	if($ftp->ftp_put($editnew_fullpath, $filecontent, 0)) {
      		addInfoBoxMsg($lang_text["wftp_upload_ok"],"|%filename%|%inDir%","|$editnew_filename|$path");
    	} 
    	else {
      		addInfoBoxMsg($lang_text["wftp_upload_error"],"|%filename%|%inDir%","|$editnew_filename|$path");
    	}
  	}
  	@unlink($tmpfile);
  }
}
/* END - EDIT/NEW-FILE */

/* START - SET PROPERTIES */
if($setProps) {

  $fullpath = $shProps;
  $path = dirname($fullpath);
  if($owner1){ $owner1 = "r"; }else{ $owner1 = "-"; }
  if($owner2){ $owner2 = "w"; }else{ $owner2 = "-"; }
  if($owner3){ $owner3 = "x"; }else{ $owner3 = "-"; }
  if($group1){ $group1 = "r"; }else{ $group1 = "-"; }
  if($group2){ $group2 = "w"; }else{ $group2 = "-"; }
  if($group3){ $group3 = "x"; }else{ $group3 = "-"; }
  if($other1){ $other1 = "r"; }else{ $other1 = "-"; }
  if($other2){ $other2 = "w"; }else{ $other2 = "-"; }
  if($other3){ $other3 = "x"; }else{ $other3 = "-"; }

  if($isDir) { $isDir = "d"; }else{ $isDir = "-"; }

  $newPerms = $isDir.$owner1.$owner2.$owner3.$group1.$group2.$group3.$other1.$other2.$other3;
  $newPerms = getoctalperms($newPerms);
  if($newPerms != $oldPerms && $newPerms != "" || $directPerms != $oldPerms && $directPerms != "") {
    if($newPerms != $oldPerms && $directPerms == $oldPerms) {
      $setNewPerms = $newPerms;
    }
    if($directPerms != $oldPerms && $newPerms == $oldPerms) {
      $setNewPerms = $directPerms;
    }
  }

  if($setNewPerms) {
    if($setNewPerms != "000") {
      $permcommand = "chmod $setNewPerms $fullpath";
      if($ftp->ftp_site($permcommand)) {
        addInfoBoxMsg($lang_text["wftp_setperms_ok"],"|%name%|%newPerms%","|$newName|$setNewPerms");
      } else {
        addInfoBoxMsg($lang_text["wftp_setperms_error"],"|%name%","|$newName");
      }
    } else {
      addInfoBoxMsg($lang_text["wftp_setperms_emtpy"],"","");
    }
  }

  if($oldName != $newName) {
    if(substr_count($newName,"/") >= 1 && $fullpath != $newName) {
      if($ftp->ftp_rename($fullpath, $newName)) {
        addInfoBoxMsg($lang_text["wftp_rename_ok"],"|%oldName%|%newName%","|$oldName|$newName");
      } else {
        addInfoBoxMsg($lang_text["wftp_rename_error"],"|%oldName%|%newName%","|$oldName|$newName");
      }
    } else {
      if($ftp->ftp_rename($fullpath, ($oldPath."/".$newName))) {
        addInfoBoxMsg($lang_text["wftp_rename_ok"],"|%oldName%|%newName%","|$oldName|$newName");
      } else {
        addInfoBoxMsg($lang_text["wftp_rename_error"],"|%oldName%|%newName%","|$oldName|$newName");
      }
    }
  }
}
/* END - SET PROPERTIES */

 if($path == "") {

 $path = $ftp->ftp_pwd();
 if(substr_count($path,"\"")>=1) {
   $path = explode("\"",$path);
   for($i=0;$i<count($path);$i++) {
     if(substr_count($path[$i],"/")>=1) {
      $path = $path[$i];
      end;
     }
   }
 }

}

$ok=0;
if ($ftp->ftp_chdir($path)) {
	$ok=1;
}else{	
	if ($path != "/") {
		addInfoBoxMsg($lang_text["wftp_chdir_failed"],"|%fullpath%","|$path");
		$split_path = explode("/",$path);
		array_pop($split_path); 
		$path = join("/",$split_path);
		if ($path == "") $path= "/";
		if(!session_is_registered('ftp_path')) {
			session_register('ftp_path');
		}
		$_SESSION['ftp_path'][$PHP_AUTH_USER] = $path;
	}
}

if($ok || $ftp->ftp_chdir($path)) {
/* START - DELETE-Action */
	$numFilesToDel = count($del_name);
	if($numFilesToDel) {
  //for($i=0;$i<=$numFilesToDel-1;$i++) {
		foreach ($del_name as $i => $dell) {
			if(substr_count($del_name[$i], "DIR:")>=1) {
				$del_dir[$i] = str_replace("DIR:","",$del_name[$i]);
				if($ftp->ftp_rmdir($del_dir[$i])) {
					$dir_oklist = $dir_oklist.$del_dir[$i].", ";
         
					$id = safe_query2("SELECT * FROM kunden WHERE kunde=?", $PHP_AUTH_USER);
					if (db_num_rows($id) == 1)	{
						$pfad = str_replace("/html/","",$del_dir[$i]);
						$id = safe_query2("SELECT * FROM ftp WHERE pfad=? AND kunde=?", $pfad, $PHP_AUTH_USER);
						$row = db_fetch_array($id);
						if ($row) {
							safe_query2( "delete from ftp where account = ?  and kunde = ? ", $row["account"], $PHP_AUTH_USER );
							$id = safe_query2( "select account from delaccs where account = ? ", $row["account"] );
							if (db_num_rows($id) == 0)	{
								safe_query2( "insert into delaccs (account, type, pfad, kunde) VALUES (  ? ,  'ftp',  ? ,  ?  )", $row["account"], $row["pfad"], $PHP_AUTH_USER );
							}
							safe_query2("update allgemein set delftp='1'");       	 		
						}	         
					}
					
				} else {
					addInfoBoxMsg($lang_text["wftp_dir_error"],"|%dir%","|$del_dir[$i]");
				}
			} else {
				$del_file = explode("/",$del_name[$i]);
				$del_file = $del_file[(count($del_file)-1)];
				if($ftp->ftp_delete($del_name[$i])) {
					$file_oklist = $file_oklist.$del_file.", ";
				} else {
					addInfoBoxMsg($lang_text["wftp_file_error"],"|%file%","|$del_file");
				}
			}
		}
		
		if($dir_oklist) {
			$dir_oklist = $lang_text["wftp_dir_ok"].substr($dir_oklist, 0, -2)."<br>";
			addInfoBoxMsg($dir_oklist,"","");
		}
		if($file_oklist) {
			$file_oklist = $lang_text["wftp_file_ok"].substr($file_oklist, 0, -2);
			addInfoBoxMsg($file_oklist,"","");
		}
	}
	/* END - DELETE-Action */

	showInfoBox();

	$PHP_SELF = 'ftp_index.php';
?>
<form ACTION="<? ECHO $PHP_SELF ?>?path=<?= urlencode( $path ) ?>" METHOD="POST" name="del_form">
<? 
	if($path != "/") { 
?>
  <div align="right"><nobr>
  <a class="data" href="<? echo $PHP_SELF ?>?path=/">/</a>&nbsp;
<?
    $pathnavi = explode("/",$path);
    for($z=0;$z<count($pathnavi);$z++) {
			if($pathnavi[$z] != "") {
				$pathlink .= "/".$pathnavi[$z];
				if($z == count($pathnavi)-1) {
					$pathfullnavi .= $pathnavi[$z];
				} else {
					$pathfullnavi .= "<a class=\"data\" href=\"$PHP_SELF?path=" . urlencode($pathlink) . "\">". htmlspecialchars($pathnavi[$z])."</a> / ";
				}
			}
		}
    echo $pathfullnavi;
?>
  </nobr></div>
<? 
	} 
?>

<input type="hidden" name="path" value="<? echo $path ?>">
<table class="tblbgcolor" width="100%" border="1" cellpadding="3" cellspacing="0" bordercolorlight="#CCCCCC" bordercolordark="#FFFFFF">
<tr>
<td class="titel-3" align="center" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg">&nbsp;</td>
<td class="titel-3" align="center" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo $lang_text["wftp_dircontent"] ?></td>
<td class="titel-3" align="center" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo $lang_text["wftp_size"] ?></td>
<td class="titel-3" align="center" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo $lang_text["wftp_date"] ?></td>
<td class="titel-3" align="center" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo $lang_text["wftp_owner"] ?></td>
<td class="titel-3" align="center" background="<?=$strSkinName?>/pics/bg_tbl_header.jpg"><? echo $lang_text["wftp_actions"] ?></td>
</tr>
<?
if($path != "/") {
  $updir = dirname($path);
?>
  <td><a class="data" href="<? echo $PHP_SELF ?>?path=<?= urlencode($updir) ?>"><img src="<?=$strSkinName?>/file_icons/back.gif" border="0"></a></td>
  <td><a class="data" href="<? echo $PHP_SELF ?>?path=<?= urlencode($updir) ?>">..</a></td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
<?
}

$ftp_dirlist = $ftp->ftp_rawlist("$path");
if($ftp_dirlist != "") {
	$dirlist = parse_ftp_rawlist($ftp_dirlist);
}
$ind = 0;
 
$sqlUser = new SQLString( $PHP_AUTH_USER );

#
# calculate list of dirs with ftp-accounts
#

$ftpHome = array();
if ( strstr($path,'/html') ) {
	$pfad = preg_replace('/^\/html(\/|$)/','',$path);
	if ($pfad == '/' ) {
		$pfad = '';
	}  
	if ( $pfad ) {
		$pfad .= '/';
	}

	$sql = "SELECT pfad FROM ftp WHERE kunde=? AND pfad like ?";

	$id = safe_query( $sql, TRUE, $sqlUser, new SQLString($pfad.'%'));

	$chunks = split('/',$pfad);
	$cnt = $pfad?count($chunks)-1:0;

	while ( list($pathHome) = db_fetch_array($id) ){
		$chunks = split('/',$pathHome);
		$ftpHome[] = $chunks[$cnt];
	}
}

#
# claculate list of the path
#
$listFtpDir='';
$listFtpFile='';
for($i=1;$i<=count($dirlist);$i++) {
  $name = $dirlist[$i]['name'];
  if($path != "/") {
    $showpath = $path."/".$name;
  } else {
    $showpath = $path.$name;
  }
  if(strlen($name) > 30) {
    $showname = substr($name, 0, 25)."...";
  } else {
    $showname = $name;
  }
  if($dirlist[$i]['is_dir'] == 1) {
    $fileimg = "<img src=$strSkinName/file_icons/file_dir.gif border=0>";    
    $showname = "<a class=data href=$PHP_SELF?path=" . urlencode($showpath) . ">" . htmlspecialchars($showname) . "&nbsp;</a>";
    $del_name = "DIR:".$showpath;
  } else {
    $extension = $dirlist[$i]['extension'];
	
    if($MimeIcon["$extension"]) {
      $fileimg = "<img src=$strSkinName/file_icons/".$MimeIcon["$extension"]." border=0>";
    } else {
      $fileimg = "<img src=$strSkinName/file_icons/file_generic.gif border=0>";
    }
    $name = $dirlist[$i]['name'];
    $del_name = $showpath;
  }
  
  
  $size = $dirlist[$i]['size'];
  $date = $dirlist[$i]['date'];
  if($dirlist[$i]['user'] == "0") {
//    $user = "&nbsp;";
	$user = "root";
  } else {
    $user = $dirlist[$i]['user'];
    if (0+$user>0){
      if ($userCache[$user]){
	$user = $userCache[$user];
      }else{
        if ($userInfo=posix_getpwuid($user)){
	  $userCache[$user]=$userInfo["name"];
	  $user = $userInfo["name"];
	}
      }
    }	
  }

  $query_standard_dom = safe_query( "SELECT domain FROM domains WHERE pfad = '/' AND kunde = ?  AND richtigedomain = 3", TRUE, new SQLString( $PHP_AUTH_USER ) );
  $res_standard_dom = db_fetch_array($query_standard_dom);
  $standard_dom = $res_standard_dom["domain"];
  $standard_dom = $standard_dom."/";
  if((strlen( $standard_dom ) > 1) && !$dirlist[$i]['is_dir'] && substr($showpath,0,strlen($docRootPath)) == $docRootPath) {
    $link = $standard_dom.substr($showpath,strlen($docRootPath),strlen($showpath));
    $link = "<a class=\"data\" href=\"http://".$link."\" target=\"_blank\">";
    $fileimg = $link.$fileimg."</a>";    
    $showname = $link.$showname."&nbsp;</a>";
  }

  if(!$dirlist[$i]['is_dir']) {
    $list_files .="<tr>
    <td>$fileimg</td>
    <td>$showname</td>
    <td>$size</td>
    <td><nobr>$date</nobr></td>
    <td>$user</td>
    <td nowrap><input type=\"checkbox\" name=\"del_name[$ind]\" id=\"del_name\" value=\"" . htmlspecialchars($del_name) . "\">
    <a class=\"data\" href=\"ftp_props.php?shProps=" . urlencode($showpath) . "\" title=\"".$lang_text["wftp_props"]."\">
      <img src=\"$strSkinName/file_icons/props.gif\" alt=\"".$lang_text["wftp_props"]."\" border=\"0\"></a>
    <a class=\"data\" href=\"ftp_editnew.php?edit=" . urlencode($showpath) . "\" title=\"".$lang_text["wftp_edit"]."\">
      <img src=\"$strSkinName/file_icons/edit.gif\" alt=\"".$lang_text["wftp_edit"]."\" border=\"0\"></a>
    <a class=\"data\" href=\"ftp_download.php?filesize=$size&download=" . urlencode($showpath) . "\" title=\"".$lang_text["wftp_download"]."\">
      <img src=\"$strSkinName/file_icons/download.gif\" target=\"_blank\" alt=\"".$lang_text["wftp_download"]."\" border=\"0\"></a></td>
    </tr>";
		$listFtpFile .= "<input type=\"hidden\" name=\"ftp_user[$ind]\" id=\"ftp_user\" value=\"0\">\n";

  } else {
		$list_dirs .="<tr>
    <td class=\"filetype_column\">$fileimg</td>
    <td>$showname</td>
    <td>$size</td>
    <td><nobr>$date</nobr></td>
    <td>$user</td>
    <td><input type=\"checkbox\" name=\"del_name[$ind]\" id=\"del_name\" value=\"" . htmlspecialchars($del_name) . "\"><a class=\"data\" href=\"ftp_props.php?shProps=" . urlencode($showpath) . "\" title=\"".$lang_text["wftp_props"]."\"><img src=\"$strSkinName/file_icons/props.gif\" alt=\"".$lang_text["wftp_props"]."\" border=\"0\"></a></td>
    </tr>";
		$listFtpDir .= "<input type=\"hidden\" name=\"ftp_user[$ind]\" id=\"ftp_user\" value=\"".(in_array($name,$ftpHome)?1:0)."\">\n";

  }

  $ind++;
}
echo $list_dirs;
echo $list_files;
?>
</table>
<? 
echo $listFtpDir;
echo $listFtpFile;
?>
<table width="100%"><tr>
<td valign="top">
  <? echo $lang_text["wftp_info"] ?><br>
  <img src="<?=$strSkinName?>/file_icons/props.gif"><? echo $lang_text["wftp_props"] ?><br>
  <img src="<?=$strSkinName?>/file_icons/edit.gif"><? echo $lang_text["wftp_edit"] ?><br>
  <img src="<?=$strSkinName?>/file_icons/download.gif"><? echo $lang_text["wftp_download"] ?>
</td>
<td align="right" valign="top">
  <a class="data" href="javascript:deActivateAll()"><? echo $lang_text["wftp_activate"] ?></a>
  <br>&nbsp;<br>
  <? echo $lang_text["wftp_active"] ?><br>
  <? $id = safe_query2("SELECT * FROM kunden WHERE kunde=?", $PHP_AUTH_USER); 
  	 if (db_num_rows($id) == 1)	echo std_button(ltext("wftp_delete"), 'javascript:performDeleteWithUsers()'); // end user 
     else echo std_button(ltext("wftp_delete"), 'javascript:performDelete()'); // additional ftp user 
  ?>
  </td>
</tr></table>
</form>
<?
} else {
  addInfoBoxMsg($lang_text["wftp_chdir_failed"],"|%fullpath%","|$path");
  showInfoBox();
}

include("ftp_footer.php");
?>
