class.filemanager.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. # ***** BEGIN LICENSE BLOCK *****
  3. # This file is part of DotClear.
  4. # Copyright (c) 2004 Olivier Meunier and contributors. All rights
  5. # reserved.
  6. #
  7. # DotClear 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. # DotClear 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 DotClear; 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. class filemanager
  23. {
  24. var $root;
  25. var $exclude_list = array();
  26. var $_types = array('img'=>'','txt'=>'');
  27. function filemanager($root_path,$base_path)
  28. {
  29. $this->root = path::real($root_path);
  30. $this->base_path = $this->__cleanPath('/'.$base_path);
  31. # Type de fichiers images
  32. $this->_types['img'] = '.gif|.jpg|.jpeg|.png|.bmp';
  33. # Types de fichier texte (éditables)
  34. $this->_types['txt'] = '.htm|.html|.php|.php3|.inc|.xml|.txt|.xhtml|.css';
  35. }
  36. function addExclusion($f)
  37. {
  38. if (is_array($f))
  39. {
  40. $l = array();
  41. foreach ($f as $v) {
  42. if (($V = path::real($v)) !== false) {
  43. $this->exclude_list[] = $V;
  44. }
  45. }
  46. }
  47. elseif (($F = path::real($f)) !== false)
  48. {
  49. $this->exclude_list[] = $F;
  50. }
  51. }
  52. function isExclude()
  53. {
  54. foreach ($this->exclude_list as $v)
  55. {
  56. if (strpos($this->root.$this->base_path,$v) === 0) {
  57. return true;
  58. }
  59. }
  60. return false;
  61. //return in_array($this->root.$this->base_path,$this->exclude_list);
  62. }
  63. function getDir()
  64. {
  65. $dir = $this->__cleanPath($this->root.$this->base_path);
  66. if ($dh = @opendir($dir))
  67. {
  68. $d_res = $f_res = array();
  69. while (($file = readdir($dh)) !== false)
  70. {
  71. $fname = $dir.'/'.$file;
  72. $jailed = $this->__inJail($fname);
  73. $tmp = array();
  74. $stat = @stat($fname);
  75. $tmp['fname'] = $fname;
  76. $tmp['jail'] = $jailed;
  77. $tmp['type'] = NULL;
  78. $tmp['type'] = @filetype($fname);
  79. $tmp['mtime'] = $stat[9];
  80. $tmp['size'] = $stat[7];
  81. $tmp['mode'] = $stat[2];
  82. $tmp['uid'] = $stat[4];
  83. $tmp['gid'] = $stat[5];
  84. $tmp['w'] = $tmp['r'] = $tmp['x'] = $tmp['f'] = $tmp['del'] = false;
  85. $tmp['d'] = ($file == '..');
  86. $tmp['l'] = NULL;
  87. if ($jailed && !in_array($fname,$this->exclude_list)) {
  88. $tmp['w'] = @is_writable($fname);
  89. $tmp['r'] = @is_readable($fname);
  90. $tmp['x'] = @file_exists($fname.'/.');
  91. $tmp['f'] = @is_file($fname);
  92. $tmp['d'] = @is_dir($fname);
  93. $tmp['l'] = $this->__getRelPath($fname);
  94. $tmp['del'] = ($file != '.') ? files::isDeletable($fname) : false;
  95. $tmp['type'] = $this->__getType($fname);
  96. }
  97. if (@is_dir($fname)) {
  98. $d_res[$file] = $tmp;
  99. } else {
  100. $f_res[$file] = $tmp;
  101. }
  102. }
  103. closedir($dh);
  104. ksort($d_res);
  105. ksort($f_res);
  106. return array('dirs'=>$d_res,'files'=>$f_res);
  107. }
  108. return false;
  109. }
  110. function getContent()
  111. {
  112. if ($this->isFile() && $this->isReadable()) {
  113. return file_get_contents($this->root.$this->base_path);
  114. }
  115. return NULL;
  116. }
  117. function isFile()
  118. {
  119. return is_file($this->root.$this->base_path);
  120. }
  121. function isDir()
  122. {
  123. return is_dir($this->root.$this->base_path);
  124. }
  125. function isWritable()
  126. {
  127. return is_writable($this->root.$this->base_path);
  128. }
  129. function isReadable()
  130. {
  131. return is_readable($this->root.$this->base_path);
  132. }
  133. function isDeletable()
  134. {
  135. return files::isDeletable($this->root.$this->base_path);
  136. }
  137. function isParentWritable()
  138. {
  139. return is_writable(dirname($this->root.$this->base_path));
  140. }
  141. function isImg()
  142. {
  143. return ($this->__getType($this->root.$this->base_path) == 'img');
  144. }
  145. function isTxt()
  146. {
  147. return ($this->__getType($this->root.$this->base_path) == 'txt');
  148. }
  149. function newDir($name)
  150. {
  151. $name = str_replace('/','',$name);
  152. return files::makeDir($this->root.$this->base_path.'/'.$name);
  153. }
  154. function putContent($c)
  155. {
  156. return files::putContent($this->root.$this->base_path,$c);
  157. }
  158. function rename($name)
  159. {
  160. $name = str_replace('/','',$name);
  161. $d = $this->root.$this->base_path;
  162. $n = dirname($d).'/'.$name;
  163. return @rename($d,$n);
  164. }
  165. function delete()
  166. {
  167. $f = $this->root.$this->base_path;
  168. if (is_file($f)) {
  169. if (@unlink($f) === true) {
  170. return true;
  171. }
  172. } elseif (is_dir($f)) {
  173. if (@rmdir($f) === true) {
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. /* Methodes privées
  180. ------------------------------------------------------- */
  181. function __cleanPath($p)
  182. {
  183. $p = str_replace('..','',$p);
  184. $p = preg_replace('|/{2,}|','/',$p);
  185. $p = preg_replace('|/$|','',$p);
  186. return $p;
  187. }
  188. function __inJail($f)
  189. {
  190. if (($f = path::real($f)) !== false) {
  191. if (preg_match('|^'.preg_quote($this->root,'|').'|',$f)) {
  192. return true;
  193. } else {
  194. return false;
  195. }
  196. } else {
  197. return false;
  198. }
  199. }
  200. function __getRelPath($f)
  201. {
  202. if ($this->__inJail($f)) {
  203. $f = path::real($f);
  204. return preg_replace('|^'.preg_quote($this->root,'|').'|','',$f);
  205. } else {
  206. return false;
  207. }
  208. }
  209. function __getType($f)
  210. {
  211. global $fm_cf_types;
  212. if (is_file($f))
  213. {
  214. if (strpos(basename($f),'.') === false) {
  215. return 'txt';
  216. }
  217. elseif (preg_match('/('.$this->_types['img'].')$/i',basename($f))) {
  218. return 'img';
  219. } elseif (preg_match('/('.$this->_types['txt'].')$/i',basename($f))) {
  220. return 'txt';
  221. }
  222. }
  223. else
  224. {
  225. return NULL;
  226. }
  227. }
  228. }
  229. ?>