[ Index ]
 

Code source de Dotclear 2.0-beta6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/clearbricks/filemanager/ -> class.filemanager.php (source)

   1  <?php
   2  # ***** BEGIN LICENSE BLOCK *****
   3  # This file is part of Clearbricks.
   4  # Copyright (c) 2006 Olivier Meunier and contributors. All rights
   5  # reserved.
   6  #
   7  # Clearbricks is free software; you can redistribute it and/or modify
   8  # it under the terms of the GNU General Public License as published by
   9  # the Free Software Foundation; either version 2 of the License, or
  10  # (at your option) any later version.
  11  # 
  12  # Clearbricks is distributed in the hope that it will be useful,
  13  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15  # GNU General Public License for more details.
  16  # 
  17  # You should have received a copy of the GNU General Public License
  18  # along with Clearbricks; if not, write to the Free Software
  19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  20  #
  21  # ***** END LICENSE BLOCK *****
  22  
  23  class filemanager
  24  {
  25      public $root;
  26      public $root_url;
  27      protected $pwd;
  28      protected $exclude_list = array();
  29      
  30      public $dir = array('dirs'=>array(),'files'=>array());
  31      
  32  	function __construct($root,$root_url='')
  33      {
  34          $this->root = $this->pwd = path::real($root);
  35          $this->root_url = $root_url;
  36          
  37          if (!preg_match('#/$#',$this->root_url)) {
  38              $this->root_url = $this->root_url.'/';
  39          }
  40          
  41          if (!$this->root) {
  42              throw new Exception('Invalid root directory.');
  43          }
  44      }
  45      
  46  	function chdir($dir)
  47      {
  48          $realdir = path::real($this->root.'/'.path::clean($dir));
  49          if (!$realdir || !is_dir($realdir)) {
  50              throw new Exception('Invalid directory.');
  51          }
  52          
  53          if ($this->isExclude($realdir)) {
  54              throw new Exception('Directory is excluded.');
  55          }
  56          
  57          $this->pwd = $realdir;
  58      }
  59      
  60  	public function addExclusion($f)
  61      {
  62          if (is_array($f))
  63          {
  64              foreach ($f as $v) {
  65                  if (($V = path::real($v)) !== false) {
  66                      $this->exclude_list[] = $V;
  67                  }
  68              }
  69          }
  70          elseif (($F = path::real($f)) !== false)
  71          {
  72              $this->exclude_list[] = $F;
  73          }
  74      }
  75      
  76  	private function isExclude($f)
  77      {
  78          foreach ($this->exclude_list as $v)
  79          {
  80              if (strpos($f,$v) === 0) {
  81                  return true;
  82              }
  83          }
  84          
  85          return false;
  86      }
  87      
  88  	private function inJail($f)
  89      {
  90          $f = path::real($f);
  91          
  92          if ($f !== false) {
  93              return preg_match('|^'.preg_quote($this->root,'|').'|',$f);
  94          }
  95          
  96          return false;
  97      }
  98      
  99  	public function inFiles($f)
 100      {
 101          foreach ($this->dir['files'] as $v) {
 102              if ($v->relname == $f) {
 103                  return true;
 104              }
 105          }
 106          return false;
 107      }
 108      
 109  	public function getDir()
 110      {
 111          $dir = path::clean($this->pwd);
 112          
 113          $dh = @opendir($dir);
 114          
 115          if ($dh === false) {
 116              throw new Exception('Unable to read directory.');
 117          }
 118          
 119          $d_res = $f_res = array();
 120              
 121          while (($file = readdir($dh)) !== false)
 122          {
 123              $fname = $dir.'/'.$file;
 124              
 125              if ($this->inJail($fname) && !$this->isExclude($fname))
 126              {
 127                  if (is_dir($fname) && $file != '.') {
 128                      $tmp = new fileItem($fname,$this->root,$this->root_url);
 129                      if ($file == '..') {
 130                          $tmp->parent = true;
 131                      }
 132                      $d_res[] = $tmp;
 133                  }
 134                  
 135                  if (is_file($fname) && strpos($file,'.') !== 0) {
 136                      $f_res[] = new fileItem($fname,$this->root,$this->root_url);
 137                  }
 138              }
 139          }
 140          closedir($dh);
 141          
 142          ksort($d_res);
 143          ksort($f_res);
 144          
 145          $this->dir = array('dirs'=>$d_res,'files'=>$f_res);
 146      }
 147      
 148  	public function getRootDirs()
 149      {
 150          $d = files::getDirList($this->root);
 151          
 152          $dir = array();
 153          
 154          foreach ($d['dirs'] as $v) {
 155              $dir[] = new fileItem($v,$this->root,$this->root_url);
 156          }
 157          
 158          return $dir;
 159      }
 160      
 161  	public function uploadFile($tmp,$dest)
 162      {
 163          $dest = $this->pwd.'/'.path::clean($dest);
 164          
 165          if (!$this->inJail(dirname($dest))) {
 166              throw new Exception(__('Destination directory is not in jail.'));
 167          }
 168          
 169          if (!is_writable(dirname($dest))) {
 170              throw new Exception(__('Cannot write in this directory.'));
 171          }
 172          
 173          if (@move_uploaded_file($tmp,$dest) === false) {
 174              throw new Exception(__('An error occured while writing the file.'));
 175          }
 176          
 177          return path::real($dest);
 178      }
 179      
 180  	public function uploadBits($name,$bits)
 181      {
 182          $dest = $this->pwd.'/'.path::clean($name);
 183          
 184          if (!$this->inJail(dirname($dest))) {
 185              throw new Exception(__('Destination directory is not in jail.'));
 186          }
 187          
 188          if (!is_writable(dirname($dest))) {
 189              throw new Exception(__('Cannot write in this directory.'));
 190          }
 191          
 192          $fp = @fopen($dest,'wb');
 193          if ($fp === false) {
 194              throw new Exception(__('An error occured while writing the file.'));
 195          }
 196          
 197          fwrite($fp,$bits);
 198          fclose($fp);
 199          
 200          return path::real($dest);
 201      }
 202      
 203  	public function makeDir($d)
 204      {
 205          files::makeDir($this->pwd.'/'.path::clean($d)); 
 206      }
 207      
 208  	public function moveFile($s,$d)
 209      {
 210          $s = $this->root.'/'.path::clean($s);
 211          $d = $this->root.'/'.path::clean($d);
 212          
 213          $s = path::real($s);
 214          $dest_dir = path::real(dirname($d));
 215          
 216          if (!$this->inJail($s)) {
 217              throw new Exception(__('File is not in jail.'));
 218          }
 219          if (!$this->inJail($dest_dir)) {
 220              throw new Exception(__('File is not in jail.'));
 221          }
 222          
 223          if (!is_writable($dest_dir)) {
 224              throw new Exception(__('Destination directory is not writable.'));
 225          }
 226          
 227          if (@rename($s,$d) === false) {
 228              throw new Exception(__('Unable to rename file.'));
 229          }
 230      }
 231      
 232  	public function removeItem($f)
 233      {
 234          $file = path::real($this->pwd.'/'.path::clean($f));
 235          
 236          if (is_file($file)) {
 237              $this->removeFile($f);
 238          } elseif (is_dir($file)) {
 239              $this->removeDir($f);
 240          }
 241      }
 242      
 243  	public function removeFile($f)
 244      {
 245          $f = path::real($this->pwd.'/'.path::clean($f));
 246          
 247          if (!$this->inJail($f)) {
 248              throw new Exception(__('File is not in jail.'));
 249          }
 250          
 251          if (!files::isDeletable($f)) {
 252              throw new Exception(__('File cannot be removed.'));
 253          }
 254          
 255          if (@unlink($f) === false) {
 256              throw new Exception(__('File cannot be removed.'));
 257          }
 258      }
 259      
 260  	public function removeDir($d)
 261      {
 262          $d = path::real($this->pwd.'/'.path::clean($d));
 263          
 264          if (!$this->inJail($d)) {
 265              throw new Exception(__('Directory is not in jail.'));
 266          }
 267          
 268          if (!files::isDeletable($d)) {
 269              throw new Exception(__('Directory cannot be removed.'));
 270          }
 271          
 272          if (@rmdir($d) === false) {
 273              throw new Exception(__('Directory cannot be removed.'));
 274          }
 275      }
 276  }
 277  
 278  class fileItem
 279  {
 280      public $file;
 281      public $basename;
 282      public $dir;
 283      public $file_url;
 284      public $dir_url;
 285      public $extension;
 286      public $relname;
 287      public $parent = false;
 288      
 289      public $type;
 290      public $mtime;
 291      public $size;
 292      public $mode;
 293      public $uid;
 294      public $gid;
 295      public $w;
 296      public $d;
 297      public $x;
 298      public $f;
 299      public $del;
 300      
 301  	public function __construct($file,$root,$root_url='')
 302      {
 303          $file = path::real($file);
 304          $stat = stat($file);
 305          $path = path::info($file);
 306          
 307          $rel = preg_replace('/^'.preg_quote($root,'/').'\/?/','',$file);
 308          
 309          $this->file = $file;
 310          $this->basename = $path['basename'];
 311          $this->dir = $path['dirname'];
 312          $this->relname = $rel;
 313          
 314          $this->file_url = str_replace('%2F','/',rawurlencode($rel));
 315          $this->file_url = $root_url.$this->file_url;
 316          
 317          $this->dir_url = dirname($this->file_url);
 318          $this->extension = $path['extension'];
 319          
 320          $this->mtime = $stat[9];
 321          $this->size = $stat[7];
 322          $this->mode = $stat[2];
 323          $this->uid = $stat[4];
 324          $this->gid = $stat[5];
 325          $this->w = is_writable($file);
 326          $this->d = is_dir($file);
 327          $this->f = is_file($file);
 328          $this->x = file_exists($file.'/.');
 329          $this->del = files::isDeletable($file);
 330          
 331          $this->type = $this->d ? null : files::getMimeType($file);
 332          $this->type_prefix = preg_replace('/^(.+?)\/.+$/','$1',$this->type);
 333      }
 334  }
 335  ?>


Généré le : Fri Feb 23 22:16:06 2007 par Balluche grâce à PHPXref 0.7