<?php

/* liste des dossiers */
function list_dir($base, $cur, $level=0) {

	global $PHP_SELF, $BASE;

	if ($dir = opendir($base)) {
    $tab = array();
    while($entry = readdir($dir)) {
      if(is_dir($base."/".$entry) && !in_array($entry, array(".",".."))) {
        $tab[] = $entry;
      }
    }

   	if(ereg("([0-9]{1,2})[_]([0-9]{1,2})[_]([0-9]{4})",$tab[0])){
   		$timestamp=array();
   		foreach($tab as $val):
   			list($day, $month, $year) = explode('_',$val);
   			$timestamp[] = mktime(0,0,0,$month, $day, $year);
   		endforeach;

   		sort($timestamp,SORT_NUMERIC);

   		$tab=array();
   		foreach($timestamp as $ts):
   			$tab[] = date('d_m_Y', $ts);
   		endforeach;


   	}else{

	    natcasesort($tab);
   	}
    //natsort($tab);

    foreach($tab as $entry) {
      /* chemin relatif à la racine */
      $file = $base."/".$entry;

      /* marge gauche
      for($i=1; $i<=(4*$level); $i++) {
        echo "&nbsp;";
      }
      */

      /* l'entrée est-elle le dossier courant */
      if($file == $cur) {
        echo "<img src=\"http://www.iri.centrepompidou.fr/privatedoc/img/folder.png\" />&nbsp;".strtoupper($entry)."<br />\n";
      } else {

      	$display = deInterCaps($entry);
        $display = UtoWS($display);

      	echo "<img src=\"http://www.iri.centrepompidou.fr/privatedoc/img/folder.png\" />&nbsp; <a href=\"$PHP_SELF?dir=".rawurlencode($file)."\">".trim(strtoupper($display))."</a><br/>\n";



      }
      /* l'entrée est-elle dans la branche dont le dossier courant est la feuille */
      if(ereg($file."/",$cur."/")) {
        list_dir($file, $cur, $level+1);
      }
    }
    closedir($dir);
  }
}

/* liste des fichiers */
function list_file($cur) {
global $PHP_SELF, $BASE;
  if ($dir = opendir($cur)) {
    /* tableaux */
    $tab_dir = array();
    $tab_file = array();
    /* extraction */
    while($file = readdir($dir)) {
      if(is_dir($cur."/".$file)) {
          $tab_dir[] = $file;
      } else {
          $tab_file[] = $file;
      }
    }


    /* tri */

    sort($tab_dir);

    sort($tab_file);

    /* affichage */
    if(!empty($tab_file)):
    foreach($tab_dir as $elem) {
     if($elem == ".." && isset($_GET['dir'])):
        echo "<img src=\"http://www.iri.centrepompidou.fr/privatedoc/img/folder.png\" />&nbsp;<a href='javascript:history.back()' title='retour'>".$elem."</a><br />\n";
     else:
        //echo "<img src=\"dir-close.gif\" />&nbsp;".$elem."<br />\n";
     endif;
    }
    endif;

    $i=0;
    $tab=array();
    $path='';
    $tab = explode("/",$BASE);

    echo '<table width="100%" cellspacing="0" cellpadding="0">';
    foreach($tab_file as $elem) {


        $path='';
        for($j=5;$j<count($tab);$j++){
            $path .= $tab[$j]."/";
        }

        $path .= $elem;

        $temppath = substr($BASE,26);
      	$temppath .= "/";

        $displayElem = UtoWS($elem);
      	$iconeInfo = (strstr(strtolower($BASE),"regards"))?'<a href="javascript:void(0)" onclick="document.getElementById(\'moreinfo\').style.display=\'block\'"><img src="http://www.iri.centrepompidou.fr/privatedoc/img/information.png" alt="information" title="plus d\'information" class="image"/></a>&nbsp;&nbsp;':'';

      	//gestion du type de fichier
      	//si mp3 : affichage lien download et ecoute
      	//si pdf : uniquement download
      	//si zip : uniquement download
      	list($filename, $ext) = explode('.',$elem);

      	if($ext == 'mp3'){
	      	echo "<tr>";
	     	echo "<td width='80%'><img src=\"http://www.iri.centrepompidou.fr/privatedoc/img/page_white_cd.png\" border='0'/>&nbsp;<a href=\"javascript:void(0)\" onclick=\"switchMedia('".trim($temppath.$elem)."')\" >".$displayElem."</a></td>";
	      	echo "<td>".$iconeInfo."<a href=\"javascript:void(0)\" onclick=\"switchMedia('".trim($temppath.$elem)."')\">&nbsp;&nbsp;<img src='http://www.iri.centrepompidou.fr/privatedoc/img/sound.png' border='0' title='ecouter' alt='ecouter' class='image'/></a>&nbsp;&nbsp;";
            echo "<a href=\"http://www.iri.centrepompidou.fr/documents/audio/".$path."\">&nbsp;&nbsp;<img src='http://www.iri.centrepompidou.fr/privatedoc/img/disk.png' border='0' title='télécharger' alt='télécharger' class='image'></a><br/></td>";
	      	echo '</tr>';
      	}
      	elseif($ext == 'pdf' || $ext == 'zip'){

      		echo "<tr>";
	      	echo "<td width='80%'>";
	      	echo "<img src=\"http://www.iri.centrepompidou.fr/privatedoc/img/$ext.png\" border='0'/>&nbsp;<a href=\"http://www.iri.centrepompidou.fr/documents/audio/$path\" >".$displayElem."</a></td>";
	      	echo "<td>".$iconeInfo."<a href=\"http://www.iri.centrepompidou.fr/documents/audio/".$path."\">&nbsp;&nbsp;<img src='http://www.iri.centrepompidou.fr/privatedoc/img/disk.png' border='0' title='télécharger' alt='télécharger' class='image'></a><br/></td>";
	      	echo '</tr>';

      	}
    $i++;
    }
    echo '</table>';
    closedir($dir);
  }
}

/*remplace une majuscule par un espace*/
function deInterCaps($var){
   return ucfirst(strtolower(ereg_replace("[A-Z]"," \\0",$var)));
}

/*remplace un underscore par un espace*/
function UtoWS($text){

	return ucfirst(strtolower(ereg_replace("[_]"," ",$text)));
}
?>
