// BROWSE CLASS
class Browser {
function Browser($dir='', $extensions='') {
// CD
$this->cd = $_GET["cd"];
if ($this->cd == "." || strstr($this->cd, "..") || $this->cd == "/") {
$this->cd = "";
}
$this->cd = urldecode(stripslashes($this->cd));
// PAGE
$this->query_a = $this->GetQueryString();
$this->extensions = $extensions;
$this->dir= stripslashes($dir.$this->cd);
} // END FUNCTION
###############################
# DisplayNav()
###############################
function DisplayNav(){
$this->query_a['cd'] = '/';
echo 'racine';
if (strlen($this->cd) == 0 || urldecode($this->cd) == "/"){
// BOA
} else { // Not @ fs root
$folders = explode("/", $this->cd);
foreach ($folders as $key=>$val) {
if (strlen($val) > 0) {
$dadir.= "/".$val;
$this->query_a['cd'] = urlencode($dadir);
$lnk = $this->MakeDaLink();
echo ' - '.$val.''."\n";
}
}
}
print "
\n";
}
###############################
# DisplayDir()
###############################
function DisplayDir(){
$da_dirlist = Array();
$da_filelist = Array();
// SPE
echo '
';
echo <<
EOF;
if (is_dir($this->dir)){
$lst = $this->GetFileList($this->dir);
$da_dirlist = $lst["dirlist"];
$da_filelist = $lst["filelist"];
$da_thumblist = $lst["thumblist"];
}
if (count($da_dirlist)){ksort($da_dirlist);}
if (count($da_filelist)){ksort($da_filelist);}
if (count($da_thumblist)){ksort($da_thumblist);}
echo '';
### DISPLAY
if (count($da_dirlist)){
foreach ($da_dirlist as $key=>$val){
$this->query_a['cd'] = urlencode($this->CleanSlashs($this->cd)).$val;
$lnk = $this->MakeDaLink();
echo '
'.$key.'';
}
}
echo "
";
if (count($da_filelist)){
foreach ($da_filelist as $key=>$val) {
echo "";
echo "| dir.$val."\">".$val." | ";
### HAVE THUMB ?
if (array_key_exists($key, $da_thumblist)) {
// if (in_array($key, $da_thumblist)) {
echo "(dir.$da_thumblist[$key]."\">thumb) | ";
$DA_thumb = $da_thumblist[$key];
}
### IMAGE SIZE
$sz = getimagesize($this->dir."/".$key);
echo "".$sz[0]."x".$sz[1]." px | ";
### FILE SIZE
echo "".$this->FormatSize(filesize($this->dir.$key))." | ";
### ADD IMAGE SIMPLE
$goodpath = str_replace("../images", "images", $this->dir);
$addit = "((".$goodpath.$val."|".$goodpath.$val."))";
echo 'ajouter | ';
### ADD IMAGE COMPLEX :)
if ($DA_thumb) {
$add_withtb = "[((".$goodpath.$DA_thumb."|".$goodpath.$val."))|".$goodpath.$val."]";
echo '+thumb | ';
}
echo "
";
}
}
echo "
";
echo '
';
}
###############################
# GetFileList($rep)
###############################
function GetFileList($rep){
if (is_dir($rep)){
$dh = opendir($rep);
while (false !== ($filename = readdir($dh))) {
if ($filename == '.' || $filename == '..') continue;
// IS A DIR
if (is_dir($rep."/".$filename) && !ereg("\.", $filename)) { // hide dot files
$dirlist[$filename] = urlencode($cfg->cd."/".$filename);
} else {
// IS A FILE
$ext = substr($filename, -4, 4);
$is_thumb = substr($filename, -10, 6);
if (in_array($ext, $this->extensions)){
if ($is_thumb == "_thumb") {
$orig_name = str_replace("_thumb", "", $filename);
$thumblist[$orig_name] = urlencode($filename);
// echo "YOUHOU".$orig_name;
} else {
$filelist[$filename] = urlencode($filename);
}
}
}
}
}
$ret['dirlist'] = $dirlist;
$ret['filelist'] = $filelist;
$ret['thumblist'] = $thumblist;
// print_r($ret);
return $ret;
}
################
# TOOLS
################
Function CleanSlashs($str) {
while (ereg("//", $str)) {
$str = str_replace("//", "/", $str);
}
Return $str;
}
function GetQueryString(){
$ar = explode('&', $_SERVER['QUERY_STRING']);
foreach($ar as $key=>$val){
if (ereg('=', $val)){
$tmp = explode('=', $val);
$qstr[$tmp[0]] = $tmp[1];
} else {
$qstr[$val] = '';
}
}
return $qstr;
}
function MakeDaLink(){
$ret = $_SERVER['PHP_SELF']."?";
$last1 = array_slice ($this->query_a, -1, 1);
foreach($this->query_a as $key=>$val){
$ret.= $key.'='.$val;
if (key($last1) != $key && $last1[key($last1)] != $val){
$ret.= "&";
}
}
return $ret;
}
function FormatSize($dsize) {
/*
1 Byte = 8 Bit
1 Kilobyte = 1024 Bytes
1 Megabyte = 1048576 Bytes
1 Gigabyte = 1073741824 Bytes
*/
// Calculate
if (strlen($dsize) <= 6) {
$dsize = number_format($dsize);
return "$dsize Kb";
} else if (strlen($dsize) > 6 && strlen($dsize) <= 9) {
$dsize = number_format($dsize / 1048576,2);
return "$dsize Mb";
} else {
$dsize = number_format($dsize / 1073741824,2);
return "$dsize Gb";
}
}
} // END CLASS
?>