lib.files.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 files
  23. {
  24. function scandir($d,$order=0)
  25. {
  26. $res = array();
  27. if (($dh = @opendir($d)) !== false)
  28. {
  29. while (($f = readdir($dh)) !== false) {
  30. $res[] = $f;
  31. }
  32. closedir($dh);
  33. sort($res);
  34. if ($order == 1) {
  35. rsort($res);
  36. }
  37. return $res;
  38. }
  39. else
  40. {
  41. return false;
  42. }
  43. }
  44. function isDeletable($f)
  45. {
  46. if (is_file($f)) {
  47. return is_writable(dirname($f));
  48. } elseif (is_dir($f)) {
  49. return (is_writable(dirname($f)) && count(files::scandir($f)) <= 2);
  50. }
  51. }
  52. # Suppression récursive d'un répertoire (rm -rf)
  53. function deltree($dir)
  54. {
  55. $current_dir = opendir($dir);
  56. while($entryname = readdir($current_dir))
  57. {
  58. if (is_dir($dir.'/'.$entryname) and ($entryname != '.' and $entryname!='..'))
  59. {
  60. if (!files::deltree($dir.'/'.$entryname)) {
  61. return false;
  62. }
  63. }
  64. elseif ($entryname != '.' and $entryname!='..')
  65. {
  66. if (!@unlink($dir.'/'.$entryname)) {
  67. return false;
  68. }
  69. }
  70. }
  71. closedir($current_dir);
  72. return @rmdir($dir);
  73. }
  74. function touch($f)
  75. {
  76. if (is_writable($f)) {
  77. $c = implode('',file($f));
  78. if ($fp = @fopen($f,'w')) {
  79. fwrite($fp,$c,strlen($c));
  80. fclose($fp);
  81. }
  82. }
  83. }
  84. function secureFile($f)
  85. {
  86. if (is_file($f))
  87. {
  88. @chmod($f,0600);
  89. if (is_readable($f)) {
  90. return true;
  91. } else {
  92. @chmod($f,0660);
  93. if (is_readable($f)) {
  94. return true;
  95. } else {
  96. @chmod($f,0666);
  97. }
  98. }
  99. }
  100. }
  101. function makeDir($f)
  102. {
  103. if (@mkdir($f,fileperms(dirname($f))) === false) {
  104. return false;
  105. }
  106. @chmod($f,fileperms(dirname($f)));
  107. }
  108. function putContent($f, $f_content)
  109. {
  110. if (is_writable($f))
  111. {
  112. if ($fp = @fopen($f, 'w'))
  113. {
  114. fwrite($fp,$f_content,strlen($f_content));
  115. fclose($fp);
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. function size($size)
  122. {
  123. $kb = 1024;
  124. $mb = 1024 * $kb;
  125. $gb = 1024 * $mb;
  126. $tb = 1024 * $gb;
  127. if($size < $kb) {
  128. return $size." B";
  129. }
  130. else if($size < $mb) {
  131. return round($size/$kb,2)." KB";
  132. }
  133. else if($size < $gb) {
  134. return round($size/$mb,2)." MB";
  135. }
  136. else if($size < $tb) {
  137. return round($size/$gb,2)." GB";
  138. }
  139. else {
  140. return round($size/$tb,2)." TB";
  141. }
  142. }
  143. # Copier d'un fichier binaire distant
  144. function copyRemote($src,$dest)
  145. {
  146. if (($fp1 = @fopen($src,'r')) === false)
  147. {
  148. return __('An error occured while downloading the file.');
  149. }
  150. else
  151. {
  152. if (($fp2 = @fopen($dest,'w')) === false)
  153. {
  154. fclose($fp1);
  155. return __('An error occured while writing the file.');
  156. }
  157. else
  158. {
  159. while (($buffer = fgetc($fp1)) !== false) {
  160. fwrite($fp2,$buffer);
  161. }
  162. fclose($fp1);
  163. fclose($fp2);
  164. return true;
  165. }
  166. }
  167. }
  168. # Fonctions de création de packages
  169. #
  170. function getDirList($dirName)
  171. {
  172. static $filelist = array();
  173. static $dirlist = array();
  174. $exclude_list=array('.','..','.svn');
  175. if (empty($res)) {
  176. $res = array();
  177. }
  178. $dirName = preg_replace('|/$|','',$dirName);
  179. if (!is_dir($dirName)) {
  180. return false;
  181. }
  182. $dirlist[] = $dirName;
  183. $d = dir($dirName);
  184. while($entry = $d->read())
  185. {
  186. if (!in_array($entry,$exclude_list))
  187. {
  188. if (is_dir($dirName.'/'.$entry))
  189. {
  190. if ($entry != 'CVS')
  191. {
  192. files::getDirList($dirName.'/'.$entry);
  193. }
  194. }
  195. else
  196. {
  197. $filelist[] = $dirName.'/'.$entry;
  198. }
  199. }
  200. }
  201. $d->close();
  202. return array('dirs'=>$dirlist, 'files'=>$filelist);
  203. }
  204. function makePackage($name,$dir,$remove_path='',$gzip=true)
  205. {
  206. if ($gzip && !function_exists('gzcompress')) {
  207. return false;
  208. }
  209. if (($filelist = files::getDirList($dir)) === false) {
  210. return false;
  211. }
  212. $res = array ('name' => $name, 'dirs' => array(), 'files' => array());
  213. foreach ($filelist['dirs'] as $v) {
  214. $res['dirs'][] = preg_replace('/^'.preg_quote($remove_path,'/').'/','',$v);
  215. }
  216. foreach ($filelist['files'] as $v) {
  217. $f_content = base64_encode(file_get_contents($v));
  218. $v = preg_replace('/^'.preg_quote($remove_path,'/').'/','',$v);
  219. $res['files'][$v] = $f_content;
  220. }
  221. $res = serialize($res);
  222. if ($gzip) {
  223. $res = gzencode($res);
  224. }
  225. return $res;
  226. }
  227. }
  228. class path
  229. {
  230. function real($p,$strict=true)
  231. {
  232. $os = (DIRECTORY_SEPARATOR == '\\') ? 'win' : 'nix';
  233. # Chemin absolu ou non ?
  234. if ($os == 'win') {
  235. $_abs = preg_match('/^\w+:/',$p);
  236. } else {
  237. $_abs = substr($p,0,1) == '/';
  238. }
  239. # Transformation du chemin, forme std
  240. if ($os == 'win') {
  241. $p = str_replace('\\','/',$p);
  242. }
  243. # Ajout de la racine du fichier appelant si
  244. if (!$_abs) {
  245. $p = dirname($_SERVER['SCRIPT_FILENAME']).'/'.$p;
  246. }
  247. # Nettoyage
  248. $p = preg_replace('|/+|','/',$p);
  249. if (strlen($p) > 1) {
  250. $p = preg_replace('|/$|','',$p);
  251. }
  252. $_start = '';
  253. if ($os == 'win') {
  254. list($_start,$p) = explode(':',$p);
  255. $_start .= ':/';
  256. } else {
  257. $_start = '/';
  258. }
  259. $p = substr($p,1);
  260. # Parcours
  261. $P = explode('/',$p);
  262. $res = array();
  263. for ($i=0;$i<count($P);$i++)
  264. {
  265. if ($P[$i] == '.') {
  266. continue;
  267. }
  268. if ($P[$i] == '..') {
  269. if (count($res) > 0) {
  270. array_pop($res);
  271. }
  272. } else {
  273. array_push($res,$P[$i]);
  274. }
  275. }
  276. $p = $_start.implode('/',$res);
  277. if ($strict && !@file_exists($p)) {
  278. return false;
  279. }
  280. return $p;
  281. }
  282. }
  283. ?>