| 431 | 
           mathias | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           /*vim: set expandtab tabstop=4 shiftwidth=4: */
  | 
        
        
            | 
            | 
           3 | 
           // +------------------------------------------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           4 | 
           // | PHP version 4.1                                                                                      |
  | 
        
        
            | 
            | 
           5 | 
           // +------------------------------------------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           6 | 
           // | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org)                                         |
  | 
        
        
            | 
            | 
           7 | 
           // +------------------------------------------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           8 | 
           // | This library is free software; you can redistribute it and/or                                        |
  | 
        
        
            | 
            | 
           9 | 
           // | modify it under the terms of the GNU General Public                                                  |
  | 
        
        
            | 
            | 
           10 | 
           // | License as published by the Free Software Foundation; either                                         |
  | 
        
        
            | 
            | 
           11 | 
           // | version 2.1 of the License, or (at your option) any later version.                                   |
  | 
        
        
            | 
            | 
           12 | 
           // |                                                                                                      |
  | 
        
        
            | 
            | 
           13 | 
           // | This library is distributed in the hope that it will be useful,                                      |
  | 
        
        
            | 
            | 
           14 | 
           // | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
  | 
        
        
            | 
            | 
           15 | 
           // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                                    |
  | 
        
        
            | 
            | 
           16 | 
           // | General Public License for more details.                                                             |
  | 
        
        
            | 
            | 
           17 | 
           // |                                                                                                      |
  | 
        
        
            | 
            | 
           18 | 
           // | You should have received a copy of the GNU General Public                                            |
  | 
        
        
            | 
            | 
           19 | 
           // | License along with this library; if not, write to the Free Software                                  |
  | 
        
        
            | 
            | 
           20 | 
           // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
  | 
        
        
            | 
            | 
           21 | 
           // +------------------------------------------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           22 | 
           // CVS : $Id: fichier.class.php,v 1.7 2007-06-25 12:15:06 alexandre_tb Exp $
  | 
        
        
            | 
            | 
           23 | 
           /**
  | 
        
        
            | 
            | 
           24 | 
           * Application projet
  | 
        
        
            | 
            | 
           25 | 
           *
  | 
        
        
            | 
            | 
           26 | 
           * La classe fichier
  | 
        
        
            | 
            | 
           27 | 
           *
  | 
        
        
            | 
            | 
           28 | 
           *@package projet
  | 
        
        
            | 
            | 
           29 | 
           //Auteur original :
  | 
        
        
            | 
            | 
           30 | 
           *@author        Alexandre Granier <alexandre@tela-botanica.org>
  | 
        
        
            | 
            | 
           31 | 
           //Autres auteurs :
  | 
        
        
            | 
            | 
           32 | 
           *@author        Aucun
  | 
        
        
            | 
            | 
           33 | 
           *@copyright     Tela-Botanica 2000-2004
  | 
        
        
            | 
            | 
           34 | 
           *@version       $Revision: 1.7 $
  | 
        
        
            | 
            | 
           35 | 
           // +------------------------------------------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           36 | 
           */
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
              | 
        
        
            | 
            | 
           39 | 
           // +------------------------------------------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           40 | 
           // |                                            ENTETE du PROGRAMME                                       |
  | 
        
        
            | 
            | 
           41 | 
           // +------------------------------------------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
           include_once PROJET_CHEMIN_CLASSES.'type_fichier_mime.class.php' ;
  | 
        
        
            | 
            | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
           // +------------------------------------------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           46 | 
           // |                                            ENTETE du PROGRAMME                                       |
  | 
        
        
            | 
            | 
           47 | 
           // +------------------------------------------------------------------------------------------------------+
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
           /**
  | 
        
        
            | 
            | 
           51 | 
            * class fichier
  | 
        
        
            | 
            | 
           52 | 
            * Cette classe représente un fichier au sens physique du terme. Elle fonctionne
  | 
        
        
            | 
            | 
           53 | 
            * pour les système UNIX.  A faire : adaptation selon les système. L'objectif est de
  | 
        
        
            | 
            | 
           54 | 
            * gérer correctement l'upload de fichier.
  | 
        
        
            | 
            | 
           55 | 
            */
  | 
        
        
            | 
            | 
           56 | 
           class fichier
  | 
        
        
            | 
            | 
           57 | 
           {
  | 
        
        
            | 
            | 
           58 | 
               /*** Attributes: ***/
  | 
        
        
            | 
            | 
           59 | 
               /**
  | 
        
        
            | 
            | 
           60 | 
                * Le nom du fichier, avec son extension.
  | 
        
        
            | 
            | 
           61 | 
                * @access private
  | 
        
        
            | 
            | 
           62 | 
                */
  | 
        
        
            | 
            | 
           63 | 
               var $_nom;
  | 
        
        
            | 
            | 
           64 | 
               /**
  | 
        
        
            | 
            | 
           65 | 
                * Le chemin UNIX ou Windows pour accéder au fichier sur le serveur (par ex;
  | 
        
        
            | 
            | 
           66 | 
                * /var/www/fichier.txt
  | 
        
        
            | 
            | 
           67 | 
                * @access private
  | 
        
        
            | 
            | 
           68 | 
                */
  | 
        
        
            | 
            | 
           69 | 
               var $_chemin;
  | 
        
        
            | 
            | 
           70 | 
               /**
  | 
        
        
            | 
            | 
           71 | 
                * Cet attribut contient une valeur du type 755, indiquant les droits afférent à un
  | 
        
        
            | 
            | 
           72 | 
                * fichier selon le système UNIX (propriétaire, groupe, autres)
  | 
        
        
            | 
            | 
           73 | 
                * @access private
  | 
        
        
            | 
            | 
           74 | 
                */
  | 
        
        
            | 
            | 
           75 | 
               var $_droits_unix;
  | 
        
        
            | 
            | 
           76 | 
               /**
  | 
        
        
            | 
            | 
           77 | 
                * Le type indique si le fichier est un répertoire, un lien ou un fichier.
  | 
        
        
            | 
            | 
           78 | 
                * @access private
  | 
        
        
            | 
            | 
           79 | 
                */
  | 
        
        
            | 
            | 
           80 | 
               var $_type = 'fichier';
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
               /**
  | 
        
        
            | 
            | 
           83 | 
                * l'identifiant du type mime.
  | 
        
        
            | 
            | 
           84 | 
                * @access private
  | 
        
        
            | 
            | 
           85 | 
                */
  | 
        
        
            | 
            | 
           86 | 
               var $_type_mime;
  | 
        
        
            | 
            | 
           87 | 
              | 
        
        
            | 
            | 
           88 | 
               /**
  | 
        
        
            | 
            | 
           89 | 
                * Le chemin vers le fichier, en partant du répertoire de travail.
  | 
        
        
            | 
            | 
           90 | 
                * @access private
  | 
        
        
            | 
            | 
           91 | 
                */
  | 
        
        
            | 
            | 
           92 | 
               var $_prefixe_chemin;
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
               /**
  | 
        
        
            | 
            | 
           95 | 
                *
  | 
        
        
            | 
            | 
           96 | 
                *
  | 
        
        
            | 
            | 
           97 | 
                * @return void
  | 
        
        
            | 
            | 
           98 | 
                * @access public
  | 
        
        
            | 
            | 
           99 | 
                */
  | 
        
        
            | 
            | 
           100 | 
               function fichier( $chemin, &$objetDB)
  | 
        
        
            | 
            | 
           101 | 
               {
  | 
        
        
            | 
            | 
           102 | 
                   $this->_chemin = $chemin ;
  | 
        
        
            | 
            | 
           103 | 
                   // On analyse l'extension pour découvrir le type mime
  | 
        
        
            | 
            | 
           104 | 
                   $partie_chemin = pathinfo ($this->_chemin) ;
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                   if (is_object($objetDB) && isset($partie_chemin['extension'])) {
  | 
        
        
            | 
            | 
           107 | 
                       $this->_type_mime = type_fichier_mime::factory ($partie_chemin['extension']) ;
  | 
        
        
            | 
            | 
           108 | 
                   }
  | 
        
        
            | 
            | 
           109 | 
                   // calcul du type
  | 
        
        
            | 
            | 
           110 | 
                   if (is_dir ($this->_chemin)) $this->_type = 'repertoire' ;
  | 
        
        
            | 
            | 
           111 | 
               } // end of member function fichier
  | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
               /**
  | 
        
        
            | 
            | 
           114 | 
                * Le constructeur de la classe.
  | 
        
        
            | 
            | 
           115 | 
                *
  | 
        
        
            | 
            | 
           116 | 
                * @param string chemin Le chemin du fichier sur le serveur.
  | 
        
        
            | 
            | 
           117 | 
                * @return fichier
  | 
        
        
            | 
            | 
           118 | 
                * @access public
  | 
        
        
            | 
            | 
           119 | 
                */
  | 
        
        
            | 
            | 
           120 | 
               function __construct( $chemin, &$objetDB)
  | 
        
        
            | 
            | 
           121 | 
               {
  | 
        
        
            | 
            | 
           122 | 
                   $this->fichier($chemin, $objetDB);
  | 
        
        
            | 
            | 
           123 | 
              | 
        
        
            | 
            | 
           124 | 
               } // end of member function __construct
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
               /**
  | 
        
        
            | 
            | 
           127 | 
                *
  | 
        
        
            | 
            | 
           128 | 
                *
  | 
        
        
            | 
            | 
           129 | 
                * @return void
  | 
        
        
            | 
            | 
           130 | 
                * @access public
  | 
        
        
            | 
            | 
           131 | 
                */
  | 
        
        
            | 
            | 
           132 | 
               function suppression()
  | 
        
        
            | 
            | 
           133 | 
               {
  | 
        
        
            | 
            | 
           134 | 
                   if ($this->_type == 'repertoire') rmdir ($this->_chemin) ;
  | 
        
        
            | 
            | 
           135 | 
                   if ($this->_type == 'fichier') unlink ($this->_chemin) ;
  | 
        
        
            | 
            | 
           136 | 
               } // end of member function suppression
  | 
        
        
            | 
            | 
           137 | 
              | 
        
        
            | 
            | 
           138 | 
               /**
  | 
        
        
            | 
            | 
           139 | 
                * Réalise l'upload d'un fichier vers chemin_destination.
  | 
        
        
            | 
            | 
           140 | 
                *
  | 
        
        
            | 
            | 
           141 | 
                * @param string chemin_destination Il s'agit du chemin UNIX de destination du fichier. Le script doit avoir les
  | 
        
        
            | 
            | 
           142 | 
                * droits en écriture sur le répertoire.
  | 
        
        
            | 
            | 
           143 | 
                * @global    mixed une référence vers un objet HTML_QuickForm_File
  | 
        
        
            | 
            | 
           144 | 
                * @return void
  | 
        
        
            | 
            | 
           145 | 
                * @access public
  | 
        
        
            | 
            | 
           146 | 
                */
  | 
        
        
            | 
            | 
           147 | 
               function upload( $chemin_destination )
  | 
        
        
            | 
            | 
           148 | 
               {
  | 
        
        
            | 
            | 
           149 | 
                   if (move_uploaded_file($_FILES['fichier']['tmp_name'], $chemin_destination)) {
  | 
        
        
            | 
            | 
           150 | 
                       return true ;
  | 
        
        
            | 
            | 
           151 | 
                   } else {
  | 
        
        
            | 
            | 
           152 | 
                       return false ;
  | 
        
        
            | 
            | 
           153 | 
                   }
  | 
        
        
            | 
            | 
           154 | 
               } // end of member function upload
  | 
        
        
            | 
            | 
           155 | 
              | 
        
        
            | 
            | 
           156 | 
               /**
  | 
        
        
            | 
            | 
           157 | 
                * Déplace un fichier, renvoie vrai en cas de succès.
  | 
        
        
            | 
            | 
           158 | 
                *
  | 
        
        
            | 
            | 
           159 | 
                * @param string origine L'emplacement de départ du fichier.
  | 
        
        
            | 
            | 
           160 | 
                * @param string destination Le répertoire d'arrivé.
  | 
        
        
            | 
            | 
           161 | 
                * @return bool
  | 
        
        
            | 
            | 
           162 | 
                * @access public
  | 
        
        
            | 
            | 
           163 | 
                */
  | 
        
        
            | 
            | 
           164 | 
               function deplace( $origine,  $destination )
  | 
        
        
            | 
            | 
           165 | 
               {
  | 
        
        
            | 
            | 
           166 | 
                   if (rename ($origine, $destination )) return true ;
  | 
        
        
            | 
            | 
           167 | 
                   return false ;
  | 
        
        
            | 
            | 
           168 | 
               } // end of member function deplace
  | 
        
        
            | 
            | 
           169 | 
              | 
        
        
            | 
            | 
           170 | 
               /**
  | 
        
        
            | 
            | 
           171 | 
                *
  | 
        
        
            | 
            | 
           172 | 
                *
  | 
        
        
            | 
            | 
           173 | 
                * @param int id_type_mime L'identifiant du type mime du document.
  | 
        
        
            | 
            | 
           174 | 
                * @return type_fichier_mime
  | 
        
        
            | 
            | 
           175 | 
                * @access public
  | 
        
        
            | 
            | 
           176 | 
                */
  | 
        
        
            | 
            | 
           177 | 
               function getTypeMime(  )
  | 
        
        
            | 
            | 
           178 | 
               {
  | 
        
        
            | 
            | 
           179 | 
                   return $this->_type_mime;
  | 
        
        
            | 
            | 
           180 | 
              | 
        
        
            | 
            | 
           181 | 
               } // end of member function getTypeMime
  | 
        
        
            | 
            | 
           182 | 
              | 
        
        
            | 
            | 
           183 | 
               /**
  | 
        
        
            | 
            | 
           184 | 
                * Renvoie vrai si le document est un répertoire.
  | 
        
        
            | 
            | 
           185 | 
                *
  | 
        
        
            | 
            | 
           186 | 
                * @return bool
  | 
        
        
            | 
            | 
           187 | 
                * @access public
  | 
        
        
            | 
            | 
           188 | 
                */
  | 
        
        
            | 
            | 
           189 | 
               function isRepertoire( )
  | 
        
        
            | 
            | 
           190 | 
               {	$isRep = is_dir ($this->_chemin) ;
  | 
        
        
            | 
            | 
           191 | 
                   return $isRep ;
  | 
        
        
            | 
            | 
           192 | 
               } // end of member function isRepertoire
  | 
        
        
            | 
            | 
           193 | 
              | 
        
        
            | 
            | 
           194 | 
              | 
        
        
            | 
            | 
           195 | 
               /**
  | 
        
        
            | 
            | 
           196 | 
                *
  | 
        
        
            | 
            | 
           197 | 
                *
  | 
        
        
            | 
            | 
           198 | 
                * @param int id_type_mime L'identifiant du type mime.
  | 
        
        
            | 
            | 
           199 | 
                * @return void
  | 
        
        
            | 
            | 
           200 | 
                * @access public
  | 
        
        
            | 
            | 
           201 | 
                */
  | 
        
        
            | 
            | 
           202 | 
               function setTypeMime( $id_type_mime )
  | 
        
        
            | 
            | 
           203 | 
               {
  | 
        
        
            | 
            | 
           204 | 
                   $this->_type_mime = $id_type_mime ;
  | 
        
        
            | 
            | 
           205 | 
               } // end of member function set TypeMime
  | 
        
        
            | 
            | 
           206 | 
              | 
        
        
            | 
            | 
           207 | 
              | 
        
        
            | 
            | 
           208 | 
               /**
  | 
        
        
            | 
            | 
           209 | 
                * Renvoie la taille du fichier en octet. Nécessite un accès au disque.
  | 
        
        
            | 
            | 
           210 | 
                *
  | 
        
        
            | 
            | 
           211 | 
                * @return int
  | 
        
        
            | 
            | 
           212 | 
                * @access public
  | 
        
        
            | 
            | 
           213 | 
                */
  | 
        
        
            | 
            | 
           214 | 
               function getTaille( )
  | 
        
        
            | 
            | 
           215 | 
               {
  | 
        
        
            | 
            | 
           216 | 
                   if ($this->isRepertoire()) {
  | 
        
        
            | 
            | 
           217 | 
                       return $this->_tailleRepertoire($this->_chemin) ;
  | 
        
        
            | 
            | 
           218 | 
                   }
  | 
        
        
            | 
            | 
           219 | 
                   return @filesize ($this->_chemin) ;
  | 
        
        
            | 
            | 
           220 | 
               } // end of member function getTaille
  | 
        
        
            | 
            | 
           221 | 
              | 
        
        
            | 
            | 
           222 | 
               /**
  | 
        
        
            | 
            | 
           223 | 
                * Renovie le nom du fichier, sur le disque.
  | 
        
        
            | 
            | 
           224 | 
                *
  | 
        
        
            | 
            | 
           225 | 
                * @return string
  | 
        
        
            | 
            | 
           226 | 
                * @access public
  | 
        
        
            | 
            | 
           227 | 
                */
  | 
        
        
            | 
            | 
           228 | 
               function getNom( )
  | 
        
        
            | 
            | 
           229 | 
               {
  | 
        
        
            | 
            | 
           230 | 
              | 
        
        
            | 
            | 
           231 | 
               } // end of member function getNom
  | 
        
        
            | 
            | 
           232 | 
              | 
        
        
            | 
            | 
           233 | 
               /**
  | 
        
        
            | 
            | 
           234 | 
                * Renvoie le chemin du fichier.
  | 
        
        
            | 
            | 
           235 | 
                *
  | 
        
        
            | 
            | 
           236 | 
                * @return string
  | 
        
        
            | 
            | 
           237 | 
                * @access public
  | 
        
        
            | 
            | 
           238 | 
                */
  | 
        
        
            | 
            | 
           239 | 
               function getChemin( )
  | 
        
        
            | 
            | 
           240 | 
               {
  | 
        
        
            | 
            | 
           241 | 
                   return $this->_chemin ;
  | 
        
        
            | 
            | 
           242 | 
               } // end of member function getChemin
  | 
        
        
            | 
            | 
           243 | 
              | 
        
        
            | 
            | 
           244 | 
               /**
  | 
        
        
            | 
            | 
           245 | 
                * Permet de calculer la taille en octet du repertoire courrant
  | 
        
        
            | 
            | 
           246 | 
                *
  | 
        
        
            | 
            | 
           247 | 
                * @return int
  | 
        
        
            | 
            | 
           248 | 
                * @access protected
  | 
        
        
            | 
            | 
           249 | 
                */
  | 
        
        
            | 
            | 
           250 | 
               function _tailleRepertoire($rep)
  | 
        
        
            | 
            | 
           251 | 
               {
  | 
        
        
            | 
            | 
           252 | 
                   $taille = 0 ;
  | 
        
        
            | 
            | 
           253 | 
                   $liste_fichier = scandir ($rep) ;
  | 
        
        
            | 
            | 
           254 | 
                   foreach ($liste_fichier as $key => $value) {
  | 
        
        
            | 
            | 
           255 | 
                       if (is_dir ($rep."/".$value) && $value != ".." && $value != ".") {
  | 
        
        
            | 
            | 
           256 | 
              | 
        
        
            | 
            | 
           257 | 
                           $taille += $this->_tailleRepertoire ($rep.$value."/") ;
  | 
        
        
            | 
            | 
           258 | 
                       } else {
  | 
        
        
            | 
            | 
           259 | 
                           if ($value != '..' && $value != '.') $taille += @filesize ($rep.$value) ;
  | 
        
        
            | 
            | 
           260 | 
                       }
  | 
        
        
            | 
            | 
           261 | 
                   }
  | 
        
        
            | 
            | 
           262 | 
                   return $taille ;
  | 
        
        
            | 
            | 
           263 | 
               } // end of member function _tailleRepertoire
  | 
        
        
            | 
            | 
           264 | 
              | 
        
        
            | 
            | 
           265 | 
              | 
        
        
            | 
            | 
           266 | 
               /**
  | 
        
        
            | 
            | 
           267 | 
                * initAttributes sets all fichier attributes to its default                    value make
  | 
        
        
            | 
            | 
           268 | 
                * sure to call this method within your class constructor
  | 
        
        
            | 
            | 
           269 | 
                */
  | 
        
        
            | 
            | 
           270 | 
               function initAttributes( )
  | 
        
        
            | 
            | 
           271 | 
               {
  | 
        
        
            | 
            | 
           272 | 
                   $this->_type = 'fichier';
  | 
        
        
            | 
            | 
           273 | 
               }
  | 
        
        
            | 
            | 
           274 | 
              | 
        
        
            | 
            | 
           275 | 
              | 
        
        
            | 
            | 
           276 | 
           } // end of fichier
  | 
        
        
            | 
            | 
           277 | 
              | 
        
        
            | 
            | 
           278 | 
           if(!function_exists("scandir"))
  | 
        
        
            | 
            | 
           279 | 
           {
  | 
        
        
            | 
            | 
           280 | 
             function scandir($dirstr)
  | 
        
        
            | 
            | 
           281 | 
             {
  | 
        
        
            | 
            | 
           282 | 
              // php.net/scandir (PHP5)
  | 
        
        
            | 
            | 
           283 | 
              $files = array();
  | 
        
        
            | 
            | 
           284 | 
              $fh = opendir($dirstr);
  | 
        
        
            | 
            | 
           285 | 
              while (false !== ($filename = readdir($fh)))
  | 
        
        
            | 
            | 
           286 | 
              {
  | 
        
        
            | 
            | 
           287 | 
                array_push($files, $filename);
  | 
        
        
            | 
            | 
           288 | 
              }
  | 
        
        
            | 
            | 
           289 | 
              closedir($fh);
  | 
        
        
            | 
            | 
           290 | 
              return $files;
  | 
        
        
            | 
            | 
           291 | 
             }
  | 
        
        
            | 
            | 
           292 | 
           }
  | 
        
        
            | 
            | 
           293 | 
           ?>
  |