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

function addInfoBoxMsg($langText,$find,$replace) {
  global $InfoBoxMsg;
  if($find && $replace) {
    $find = explode("|",$find);
    $replace = explode("|",$replace);
    for($i=0;$i<count($find);$i++) {
      $langText = str_replace($find[$i],$replace[$i],$langText);
    }
  }
  $InfoBoxMsg .= "<tr><td>".$langText."</td></tr>";
}

function showInfoBox() {
  global $InfoBoxMsg;
  if($InfoBoxMsg) {
    echo "<table class='tblbgcolor' width='100%' border='1' cellpadding='3' cellspacing='0' bordercolorlight='#CCCCCC' bordercolordark='#FFFFFF'><tr><td>
    <table border='0' cellpadding='0' cellspacing='0'>
    $InfoBoxMsg
    </table>
    </td></tr></table><br>";
  }
}

function getoctalperms($permstring) {
  $perms = str_replace("r","1",$permstring);
  $perms = str_replace("w","1",$perms);
  $perms = str_replace("x","1",$perms);
  $perms = str_replace("-","0",$perms);
  $perm1 = bindec(substr($perms,1,3));
  $perm2 = bindec(substr($perms,4,3));
  $perm3 = bindec(substr($perms,7,3));
  $perms = $perm1.$perm2.$perm3;
  return $perms;
}

function parse_ftp_rawlist($list, $type="UNIX") {
  GLOBAL $show_dotfiles;
  if ($type == "UNIX") {
  
		$regexp = "([-ldrwxs]{10})[ ]+([0-9]+)[ ]+(.*)[ ]+([0-9]+)[ ]+([A-Za-z]{3}[ ]+[0-9]{1,2}[ ]+[0-9:]{4,5})[ ](.*)";
    $i=0;
    foreach ($list as $line) {
      $is_dir = $is_link = FALSE;
      $target = "";
      if (eregi($regexp, $line, $regs)) {
				$fileName = $regs[6];
				$fileMode = $regs[1];
        if (eregi("^[.]", $fileName ) && !$show_dotfiles) //hide dot files (.)
					continue;
				if ( eregi("^[.][.]?$", $fileName ) )  // hide dirs '..' , '.'
					continue;

				$fileDate = $regs[5];
				$i++;
				if ( eregi("^[d]", $fileMode ) ) {
					$is_dir = TRUE;
				} elseif ( eregi("^[l]", $fileMode ) ) {
					$is_link = TRUE;
					list($fileName, $target) = split(" -> ", $fileName);
				}
          //Get extension from file name
				$regs_ex = explode( ".",$fileName );
				if ( (!$is_dir) && (count($regs_ex) > 1) )
					$extension = $regs_ex[count($regs_ex)-1];
				else $extension = "";
				//$size = ceil($regs[5]/1024);
				$size = $regs[4];
				
				$perms = str_replace("r","1",$regs[1]);
				$perms = str_replace("w","1",$perms);
				$perms = str_replace("x","1",$perms);
				$perms = str_replace("-","0",$perms);
				$perm1 = bindec(substr($perms,1,3));
				$perm2 = bindec(substr($perms,4,3));
				$perm3 = bindec(substr($perms,7,3));
				$perms = $perm1.$perm2.$perm3;

				//				echo "owner: $regs[3]<br>\n";
				
				eregi("([-a-zA-Z0-9_]+)[ ]+(@[ ])?([-a-zA-Z0-9_]+)",$regs[3],$owner);
					 

				$user=$owner[1];
		
				//				echo "user: $user<br>\n";

				if ( $user == "0" ) {
					$user = "root";
				} else {
					if ( is_numeric( $user ) ) {
						if ( $Info = posix_getpwuid( $user ) ) {
							$user = $Info["name"];
						}
					}
				}

				$group=$owner[3];
  
				//				echo "group: $group<br>\n";
				
				if ( $group == "0" ) {
					$group = "root";
				} else {
					if ( is_numeric($group) ) {
						if ($Info=posix_getgrgid( $group ) ) {
							$group = $Info["name"];
						}
					}
				}
				
				$files[$i] = array (
														"is_dir"    => $is_dir,
														"extension" => $extension,
														"name"      => $fileName,
														"perms"     => $fileMode,
														"num"       => $regs[2],
														"user"      => $user,
														"group"     => $group,
														"size"      => $size,
														"date"      => $fileDate,
														"is_link"   => $is_link,
														"target"    => $target,
														"octal"     => $perms
														);
				
			}
		}

	} else {
		//
		// non Unix
		//
		$regexp = "([0-9\-]{8})[ ]+([0-9:]{5}[APM]{2})[ ]+([0-9|<DIR>]+)[ ]+(.*)";
      foreach ($list as $line) {
        $is_dir = false;
        if (eregi($regexp, $line, $regs)) {
          if (!eregi("^[.]", $regs[4])) {
            if($regs[3] == "<DIR>") {
              $is_dir = true;
              $regs[3] = '';
            }
            $i++;

            // Get extension from filename
            $regs_ex = explode(".",$regs[4]);
            if ((!$is_dir)&&(count($regs_ex) > 1))
            $extension = $regs_ex[count($regs_ex)-1];
            else $extension = "";
            if ($regs[3] == "")$regs[3]=0;

            $files[$i] = array (
              "is_dir"    => $is_dir,
              "extension" => $extension,
              "name"      => $regs[4],
              "date"      => $regs[1],
              "time"      => $regs[2],
              "size"      => $size,
              "is_link"   => 0,
              "target"    => "",
              "num"       => "" );
            }
          }
        }
}
if ( is_array($files)  AND count($files) > 0) {
	asort($files);
	reset($files);
}
return $files;
}
    
function getPath()
{
	global $PHP_AUTH_USER;
	if(is_array($_SESSION['ftp_path']) && array_key_exists($PHP_AUTH_USER, $_SESSION['ftp_path']))
	{
		return $_SESSION['ftp_path'][$PHP_AUTH_USER];
	}
	return '';
}

function setPath($path)
{
	global $PHP_AUTH_USER;
	if(!session_is_registered('ftp_path'))	
	{
		session_register('ftp_path');
	}
	$_SESSION['ftp_path'][$PHP_AUTH_USER] = $path;
}
 
?>
