"._("Uncompressing through TAR")."
";
      passthru("tar -xf ".escapeshellarg($file)." -C ".escapeshellarg($dest)." 2>&1", $ret);
    }
    if (substr($lfile,-4)==".zip") {
      echo ""._("Uncompressing through UNZIP")."
";
      $cmd="unzip -o ".escapeshellarg($file)." -d ".escapeshellarg($dest)." 2>&1";
      passthru($cmd, $ret);
    }
    if (substr($lfile,-3)==".gz") {
      echo ""._("Uncompressing through GUNZIP")."
";
      $cmd="gunzip ".escapeshellarg($file)." 2>&1";
      passthru($cmd, $ret);
    }
    echo "";
    if ($ret) {
      $err->raise("bro",_("I cannot find a way to extract the file %s, it is an unsupported compressed format"), $file);
    }
    // fix the perms of the extracted archive TODO: does it work???
    $action->fix_dir($dest_to_fix);
    return $ret;
  }
  /**
   * Copy many files from point A to point B
   * 
   * @global m_err $err
   * @param array $d List of files to move
   * @param string $old 
   * @param string $new 
   * @return boolean
   */
  function CopyFile($d,$old,$new) {
    global $err;
    $old=$this->convertabsolute($old,false);
    if (!$old) {
      $err->raise("bro",_("File or folder name is incorrect"));
      return false;
    }
    $new=$this->convertabsolute($new,false);
    if (!$new) {
      $err->raise("bro",_("File or folder name is incorrect"));
      return false;
    }
    if ($old == $new) {
      $err->raise("bro",_("You cannot move or copy a file to the same folder"));
      return false;
    }
    for ($i=0;$iCopyOneFile($old."/".$d[$i],$new);
      }
    }
    return true;
  }
  /**
   * Copy a source to a destination by either copying recursively a
   * directory or by downloading a file with a URL (only http:// is
   * supported)
   *
   * Note that we assume that the inputs have been convertabsolute()'d
   * 
   * @global m_err $err
   * @param string $src Path or URL
   * @param string $dest Absolute path inside the users directory
   * @return boolean false on error
   */
  function CopyOneFile($src, $dest) {
    global $err;
    exec("cp -Rpf ".escapeshellarg($src)." ".escapeshellarg($dest), $void, $ret);
    if ($ret) {
      $err->raise("bro","Errors happened while copying the source to destination. cp return value: %d", $ret);
      return false;
    }
    return true;
  }
  /**
   * Affiche le chemin et les liens de la racine au dossier $path
   * Affiche autant de liens HTML (anchor) que le chemin $path contient de
   * niveaux de dossier. Chaque lien est associ la page web $action
   * laquelle on ajoute le paramtre R=+Le nom du dossier courant.
   * 
   * @param string $path Dossier vers lequel on trace le chemin
   * @param string $action Page web de destination des liens
   * @param boolean $justparent
   * @return string Le code HTML ainsi obtenu.
   */
  function PathList($path,$action, $justparent=false) {
    $path=$this->convertabsolute($path,true);
    $a=explode("/",$path);
    if (!is_array($a)) $a=array($a);
    $c='';
    $R='';
    if ($justparent) {
      return "↑";
    }
    for($i=0;$i".$a[$i]." / ";
      }
    }
    return $c;
  }
  /**
   * Affiche le contenu d'un fichier pour un champ VALUE de textarea
   * 
   * Affiche le contenu du fichier $file dans le dossier $R. Le contenu
   * du fichier est reformat pour pouvoir entrer dans un champs TextArea
   * 
   * @global m_err $err
   * @param string $R Dossier dans lequel on cherche le fichier
   * @param string $file Fichier dont on souhaite obtenir le contenu.
   * @return string|false TRUE si le fichier a bien été mis sur
   * echo, ou FALSE si une erreur est survenue.
   */
  function content($R,$file) {
    global $err;
    $absolute=$this->convertabsolute($R,false);
    if (!strpos($file,"/")) {
      $absolute.="/".$file;
      if (file_exists($absolute)) {
	$std=str_replace("<","<",str_replace("&","&",file_get_contents($absolute)));
	return $std;
      } else {
	$err->raise("bro",_("Cannot read the requested file. Please check the permissions"));
	return false;
      }
    } else {
      $err->raise("bro",_("File or folder name is incorrect"));
      return false;
    }
  }
  /**
   * Retourne une url de navigation pour le fichier $name du dossier $dir
   * Les url sont mises en caches. Il se peut qu'aucune url n'existe, ou que
   * celle-ci soit protge par un .htaccess.
   * 
   * Return a browsing url if available.
   * Maintain a url cache (positive AND negative(-) cache)
   * 
   * @global m_mysql $db
   * @global int $cuid
   * 
   * @param string $dir Dossier concerné
   * @param string $name Fichier dont on souhaite obtenir une URL
   * @return string URL concerne, ou FALSE si aucune URL n'est disponible pour ce fichier
   */
  function viewurl($dir,$name) {
    global $db,$cuid;
    // Is it in cache ?
    if (substr($dir,0,1) == "/") $dir=substr($dir,1);
    if (substr($dir,-1) == "/") $dir=substr($dir,0,-1);
    $dir=str_replace("%2F", "/", urlencode($dir));
    $name=urlencode($name);
    if (!@$this->cacheurl["d".$dir]) {
      // On parcours $dir en remontant les /
      $end="";	$beg=$dir;	$tofind=true;
      while ($tofind) {
	$db->query("SELECT sub,domaine FROM sub_domaines WHERE compte='$cuid'
 AND type=0 AND (valeur='/$beg/' or valeur='/$beg');");
	$db->next_record();
	if ($db->num_rows()) {
	  $tofind=false;
	  $this->cacheurl["d".$dir]="http://".$db->f("sub").ife($db->f("sub"),".").$db->f("domaine").$end;
	}
	if (!$beg && $tofind) {
	  $tofind=false;
	  $this->cacheurl["d".$dir]="-";
	  // We did not find it ;(
	}
	if (($tt=strrpos($beg,"/"))!==false) {
	  $end=substr($beg,$tt).$end; //=/topdir$end so $end starts AND ends with /
	  $beg=substr($beg,0,$tt);
	} else {
	  $end="/".$beg.$end;
	  $beg="/";
	}
      }
    }
    if ($this->cacheurl["d".$dir] && $this->cacheurl["d".$dir]!="-") {
      return $this->cacheurl["d".$dir]."/".$name;
    } else {
      return false;
    }
  }
  /**
   * 
   * @global m_mem $mem
   * @global m_err $err
   * @param string $dir
   * @param string $name
   * @return null|boolean
   */
  function can_edit($dir,$name) {
    global $mem,$err;
    $absolute="$dir/$name";
    $absolute=$this->convertabsolute($absolute,false);
    if (!$absolute) {
      $err->raise('bro',_("File not in authorized directory"));
      include('foot.php');
      exit;
    }
    $finfo=finfo_open(FILEINFO_MIME_TYPE); 
    $mime=finfo_file($finfo,$absolute);
    if ( substr($mime,0,5) == "text/" || $mime == "application/x-empty" || $mime == "inode/x-empty") {
      return true;
    }
    return false;
  }
  /**
   * Return a HTML snippet representing an extraction function only if the mimetype of $name is supported
   * 
   * @param string $name
   * @return boolean
   */
  function is_extractable($name) {
    if ($parts=explode(".", $name)) {
      $ext=array_pop($parts);
      switch ($ext) {
      case "gz":
      case "bz":
      case "bz2":
	$ext=array_pop($parts).$ext;
      /* FALLTHROUGH */
      case "tar.gz":
      case "tar.bz":
      case "tar.bz2":
      case "tgz":
      case "tbz":
      case "tbz2":
      case "tar":
      case "Z":
      case "zip":
	return true;
      }
    }
    return false;
  }
  /**
   * return true if file is a sql dump (end with .sql or .sql.gz)
   * 
   * @param type $dir
   * @param type $name
   * @return boolean
   */
  function is_sqlfile($name) {
    if ($parts=explode(".", $name)) {
      $ext=array_pop($parts);
      $ext2=array_pop($parts).'.'.$ext;
      if ( $ext == 'sql' or $ext2 == 'sql.gz') return true;
    }
    return false;
  }
  /**
   * 
   * @global m_err $err
   * @param string $dir
   * @param string $file
   */
  function download_link($dir,$file){
    global $err;
    $err->log("bro","download_link");
    header("Content-Disposition: attachment; filename=$file");
    header("Content-Type: application/force-download");
    header("Content-Transfer-Encoding: binary");
    $this->content_send($dir,$file);
  }
  /**
   * Echoes the content of the file $file located in directory $R
   * 
   * @global m_err $err
   * @param string $R
   * @param string $file
   * @return null|false
   */
  function content_send($R,$file) {
    global $err;
    $absolute=$this->convertabsolute($R,false);
    if (!strpos($file,"/")) {
      $absolute.="/".$file;
      if (file_exists($absolute)) {
	readfile($absolute);
      }
    } else {
      $err->raise("bro",_("File or folder name is incorrect"));
      return false;
    }
  }
  /**
   * Sauve le fichier $file dans le dossier $R avec pour contenu $texte
   * le contenu est issu d'un textarea, et ne DOIT PAS contenir de \ ajouts
   * automatiquement par addslashes
   * 
   * @global m_err $err
   * @param string $file Nom du fichier sauver. S'il existe déjà, il sera
   * écrasé sans confirmation.
   * @param string $R Dossier dans lequel on modifie le fichier
   * @param string $texte Texte du fichier à sauver dedans
   * @return false|null TRUE si tout s'est bien pass, FALSE si une erreur s'est produite.
   */
  function save($file,$R,$texte) {
    global $err;
    $absolute=$this->convertabsolute($R,false);
    if (!strpos($file,"/")) {
      $absolute.="/".$file;
      if (file_exists($absolute)) {
 	if (! file_put_contents($absolute, $texte ) ) {
	  $err->raise("bro",_("Cannot edit the requested file. Please check the permissions"));
	  return false;
	}
      }
    } else {
      $err->raise("bro",_("File or folder name is incorrect"));
      return false;
    }
  }
  /**
   * Echo d'un flux .tar.Z contenant tout le contenu du dossier $dir
   * 
   * @global m_mem $mem
   * @param string $dir Dossier à dumper, relatif la racine du compte du membre.
   * @return void NE RETOURNE RIEN, et il faut Quitter le script immdiatement aprs
   */
  function DownloadZ($dir="") {
    global $mem;
    header("Content-Disposition: attachment; filename=".$mem->user["login"].".Z");
    header("Content-Type: application/x-Z");
    header("Content-Transfer-Encoding: binary");
    $d=escapeshellarg(".".$this->convertabsolute($dir,true));
    set_time_limit(0);
    passthru("/bin/tar -cZ -C ".getuserpath()."/".$mem->user["login"]."/ $d");
  }
  /**
   * Echo d'un flux .tgz contenant tout le contenu du dossier $dir
   * 
   * @global type $mem
   * @param string $dir Dossier à dumper, relatif la racine du compte du membre.
   * @return void NE RETOURNE RIEN, et il faut Quitter le script immdiatement aprs
   */
  function DownloadTGZ($dir="") {
    global $mem;
    header("Content-Disposition: attachment; filename=".$mem->user["login"].".tgz");
    header("Content-Type: application/x-tgz");
    header("Content-Transfer-Encoding: binary");
    $d=escapeshellarg(".".$this->convertabsolute($dir,true));
    set_time_limit(0);
    passthru("/bin/tar -cz -C ".getuserpath()."/ $d");
  }
 
 
  /**
   * Echo d'un flux .tar.bz2 contenant tout le contenu du dossier $dir
   * 
   * @global type $mem
   * @param string $dir Dossier à dumper, relatif la racine du compte du membre.
   * @return void NE RETOURNE RIEN, et il faut Quitter le script immdiatement aprs
   */
  function DownloadTBZ($dir="") {
    global $mem;
    header("Content-Disposition: attachment; filename=".$mem->user["login"].".tar.bz2");
    header("Content-Type: application/x-bzip2");
    header("Content-Transfer-Encoding: binary");
    $d=escapeshellarg(".".$this->convertabsolute($dir,true));
    set_time_limit(0);
    passthru("/bin/tar -cj -C ".getuserpath()."/ $d");
  }
 
  /**
   * Echo d'un flux .ZIP contenant tout le contenu du dossier $dir
   * 
   * @global type $mem
   * @param string $dir Dossier à dumper, relatif la racine du compte du membre.
   * @return void NE RETOURNE RIEN, et il faut Quitter le script immdiatement aprs
   */
  function DownloadZIP($dir="") {
    global $mem;
    header("Content-Disposition: attachment; filename=".$mem->user["login"].".zip");
    header("Content-Type: application/x-zip");
    header("Content-Transfer-Encoding: binary");
    $d=escapeshellarg($this->convertabsolute($dir,false));
    set_time_limit(0);
    passthru("/usr/bin/zip -r - $d");
  }
 
  /**
   * Fonction de tri perso utilis par filelist.
   * 
   * @access private
   * @param string $a
   * @param string $b
   * @return int
   */
  function _sort_filelist_name($a,$b) {
    if ($a["type"] && !$b["type"]) return 1;
    if ($b["type"] && !$a["type"]) return -1;
    return $a["name"]>$b["name"];
  }
  /**
   * Efface $file et tous ses sous-dossiers s'il s'agit d'un dossier
   * A UTILISER AVEC PRECAUTION !!!
   * @param string $file Fichier ou dossier supprimer.
   * @access private
   */
  function _delete($file) {
    global $err;
    // permet d'effacer de nombreux fichiers
    @set_time_limit(0);
    //chmod($file,0777);
    $err->log("bro", "_delete($file)");
    if (is_dir($file)) {
      $handle=opendir($file);
      while (($filename=readdir($handle)) !== false) {
	if ($filename != "." && $filename != "..") {
	  $this->_delete($file."/".$filename);
	}
      }
      closedir($handle);
      rmdir($file);
    } else {
      unlink($file);
    }
  }
  /**
   * Function d'exportation de configuration appelé par la classe m_export via un hooks
   * Produit en sorti un tableau formatté ( pour le moment) en HTML
   * 
   * @global m_mysql $db
   * @global m_err $err
   * @return string
   */
  function alternc_export_conf() {
    global $db,$err;
    $err->log("bro","export_conf");
    $str=" Browser  \n";
    $str.=" \n";
    $pref=$this->GetPrefs();
 
    $i=1;
    foreach ($pref as $k=>$v) {
      if (($i % 2) == 0){
	$str.=" <$k>$v$k>\n"; 
      }
      $i++;
    }
    $str.="  \n";
 
    return $str;
  }
  /**
   * Function d'exportation des données appelé par la classe m_export via un hooks
   * 
   * @global m_mem $mem
   * @global m_err $err
   * @param string $dir Le chemin destination du tarball produit
   * @return boolean|null
   */
  function alternc_export_data($dir){
    global $mem,$err;
    $err->log("bro","export_data");
    $dir.="html/";
    if(!is_dir($dir)){ 
      if(!mkdir($dir))
	$err->raise("bro",_("Cannot create the requested directory. Please check the permissions"));
    }
    $timestamp=date("H:i:s");
    if(exec("/bin/tar cvf - ".escapeshellarg(getuserpath()."/")."| gzip -9c > ".escapeshellarg($dir."/".$mem->user['login']."_html_".$timestamp.".tar.gz"))) {
      $err->log("bro","export_data_succes");
    } else {
      $err->log("bro","export_data_failed");
    }
  }
  function getMaxAllowedUploadSize() {
    return min(ini_get('post_max_size'), ini_get('upload_max_filesize'));
  }
} /* Class Browser */