[ Index ] |
|
Code source de Plume CMS 1.2.2 |
1 <?php 2 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 3 /* 4 # ***** BEGIN LICENSE BLOCK ***** 5 # This file is part of Plume CMS, a website management application. 6 # Copyright (C) 2001-2005 Loic d'Anterroches and contributors. 7 # 8 # Plume CMS is free software; you can redistribute it and/or modify 9 # it under the terms of the GNU General Public License as published by 10 # the Free Software Foundation; either version 2 of the License, or 11 # (at your option) any later version. 12 # 13 # Plume CMS 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 16 # GNU General Public License for more details. 17 # 18 # You should have received a copy of the GNU General Public License 19 # along with this program; if not, write to the Free Software 20 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 # 22 # ***** END LICENSE BLOCK ***** */ 23 24 /** @class files 25 * 26 * - Set of file utilities to copy, move files and folders. 27 * - Some of the functions can be used directly without the new to instance 28 * an object. 29 */ 30 31 //param constants 32 define('PX_FILES_OVERWRITE_TRUE', 1); 33 define('PX_FILES_OVERWRITE_FALSE', 2); 34 define('PX_FILES_OVERWRITE_IF_NEWER', 3); 35 36 //success codes > 0 37 define('PX_FILES_SUCCESS', 1); 38 define('PX_FILES_SUCCESS_NO_CHANGE', 2); 39 40 //error codes <= 0 41 define('PX_FILES_ERROR', 0); 42 define('PX_FILES_ERROR_NO_RIGHTS', -1); 43 define('PX_FILES_ERROR_NO_SOURCE', -2); 44 define('PX_FILES_ERROR_BAD_DEST', -3); 45 define('PX_FILES_ERROR_CHMOD', -4); 46 47 define('PX_OS_SEP', '/'); 48 define('PX_CUR_OS', (strtoupper(substr(PHP_OS,0,3) == 'WIN')) ? 'Win' : 'Nix'); 49 50 51 class files 52 { 53 var $job = array(); 54 var $log = array(); 55 /** 56 * Add a job. 57 */ 58 59 /** 60 * Copy the content of one folder into another. 61 * The folder is copied recursively. 62 * By default the files are not overwritten in the destination folder. 63 * The destination folder must exist. 64 * If storage is asked, the function must be called through an object, 65 * else the $log array is not available! 66 * The completely empty folders are not created. 67 * 68 * @param string Source folder 69 * @param string Destination folder 70 * @param int Overwrite (PX_FILES_OVERWRITE_FALSE) 71 * @param octal Default chmod for the files (0666) 72 * @param octal Default chmod for the folders (0777) 73 * @param bool Store the status of each file in the log (false) 74 * @return int Global error success code 75 */ 76 function copyfolder($src, $dest, $overwrite=PX_FILES_OVERWRITE_FALSE, 77 $chmodfiles=0666, $chmodfolders=0777, $log=false) 78 { 79 if (!file_exists($src) or !@is_dir($src)) 80 return PX_FILES_ERROR_NO_SOURCE; 81 if (!file_exists($dest) or !@is_dir($dest)) 82 return PX_FILES_ERROR_BAD_DEST; 83 $src = files::real_path($src); 84 $dest = files::real_path($dest); 85 $folders = array(); //will get the list of created folders 86 $files = array(); 87 $r = PX_FILES_SUCCESS; 88 files::listfiles($src, $files); 89 reset($files); 90 foreach ($files as $file) { 91 $folder = dirname($dest.substr($file, strlen($src))); 92 if (empty($folders[$folder])) { 93 $folders[$folder] = files::createfolder($folder, $chmodfolders); 94 } 95 $res = files::copyfile($file, $dest.substr($file, strlen($src)), 96 $chmodfiles, $overwrite); 97 if ($log) { 98 $this->log[$file] = $res; 99 } 100 if (!files::is_success($res)) 101 $r = PX_FILES_ERROR; 102 } 103 return $r; 104 } 105 106 /** 107 * List all the files of a folder recursively by default. 108 * 109 * @param string Folder 110 * @param array Reference to the array to contain the files 111 * @param string Regex to catch only some files, comparison only on the file name ('') 112 * @param bool (true) do the recursive listing. 113 * @return int Error/success code 114 */ 115 function listfiles($folder, &$files, $regex='', $rec=true) 116 { 117 if (!@file_exists($folder) or !@is_dir($folder)) 118 return PX_FILES_ERROR_NO_SOURCE; 119 $r = PX_FILES_SUCCESS; 120 if ($dirstream = @opendir($folder)) { 121 while (false !== ($file = readdir($dirstream))) { 122 if ($file != '.' && $file != '..') { 123 if (is_file($folder.'/'.$file) 124 && (empty($regex) or preg_match($regex, $file))) { 125 $files[] = files::unifypath($folder.'/'.$file); 126 } 127 if ($rec && @is_dir($folder.'/'.$file)) 128 $r = files::listfiles($folder.'/'.$file, $files, 129 $regex, $rec); 130 } 131 } 132 } 133 return $r; 134 } 135 136 /** 137 * Copy and chmod a file. Can overwrite the file if asked. 138 * No try to create the needed folders if not available. 139 * 140 * @param string Source file 141 * @param string Destination file 142 * @param int (octal) chmod the file (0666) 143 * @param int Overwrite the file (PX_FILES_OVERWRITE_FALSE) 144 * @return int Success/error code 145 */ 146 function copyfile($src, $dest, $chmod=0666, 147 $overwrite=PX_FILES_OVERWRITE_FALSE) 148 { 149 $src_exists = @file_exists($src); 150 $dest_exists = @file_exists($dest); 151 152 if (!$src_exists or @is_dir($src)) 153 return PX_FILES_ERROR_NO_SOURCE; 154 if ($dest_exists && @is_dir($dest)) 155 return PX_FILES_ERROR_BAD_DEST; 156 if ($dest_exists && $overwrite == PX_FILES_OVERWRITE_FALSE) 157 return PX_FILES_SUCCESS_NO_CHANGE; 158 //Do not overwrite if dest newer and PX_FILES_OVERWRITE_IF_NEWER asked 159 if ($dest_exists && $overwrite == PX_FILES_OVERWRITE_IF_NEWER 160 && (@filemtime($src) < @filemtime($dest))) 161 return PX_FILES_SUCCESS_NO_CHANGE; 162 $success = @copy($src, $dest); 163 if (!$success) 164 return PX_FILES_ERROR; 165 $success = @chmod($dest, $chmod); 166 if (!$success) 167 return PX_FILES_ERROR_CHMOD; 168 return PX_FILES_SUCCESS; 169 } 170 171 /** 172 * Create folder recursively if needed. 173 * 174 * @param string Folder to create absolute path needed 175 * @param int (octal) Chmod for the newly created folders (0777) 176 * @return int Success/error code 177 */ 178 function createfolder($folder, $chmod=0777) 179 { 180 $folder = files::real_path($folder); 181 //windows cleaning 182 if (preg_match('#^[A-Za-z]+\:/#', $folder)) { 183 list($_startPoint, $_path) = explode(':', $folder, 2); 184 $folder = $_path; 185 } 186 // First pop from the top to the bottom "/" to see when a directory 187 // exist. Then start to create them from this point. 188 // This is to avoid problems in safe_mod. 189 $dirs = split('/', $folder); 190 if (@is_dir($folder)) { 191 return PX_FILES_SUCCESS; 192 } 193 $offset = 1; 194 for ($i=count($dirs); $i>-1; $i--) { 195 $path = implode('/', array_slice($dirs, 0, $i)); 196 if (@is_dir($path)) { 197 $offset = $i; 198 break; 199 } 200 } 201 $path .= '/'; 202 for ($i=$offset; $i<count($dirs); $i++) { 203 $path .= $dirs[$i]; 204 if (!@is_dir($path)) { 205 @mkdir($path, $chmod); 206 @chmod($path, $chmod); 207 } 208 $path.='/'; 209 } 210 if (@is_dir($folder)) { 211 return PX_FILES_SUCCESS; 212 } else { 213 return PX_FILES_ERROR; 214 } 215 } 216 217 /** 218 * Convert a success/error code into a BOOL 219 * 220 * @param int error/success code 221 * @return BOOL error or success 222 */ 223 function is_success($code) 224 { 225 return ($code > 0); 226 } 227 228 229 function is_pathrelative($dir) 230 { 231 if (strcmp('Win', PX_CUR_OS) == 0) { 232 return (preg_match('/^\w+:/', $dir) <= 0); 233 } else { 234 return (preg_match('/^\//', $dir) <= 0); 235 } 236 } 237 238 function unifypath($path) 239 { 240 if (strcmp('Win', PX_CUR_OS) == 0) { 241 return str_replace('\\', PX_OS_SEP, $path); 242 } 243 return $path; 244 } 245 246 function real_path($path) 247 { 248 $_path = files::unifypath($path); 249 if (files::is_pathrelative($path)) { 250 $_curdir = files::unifypath(realpath('.').PX_OS_SEP); 251 $_path = $_curdir.$_path; 252 } 253 $_startPoint = ''; 254 if (strcmp('Win', PX_CUR_OS) == 0) { 255 list($_startPoint, $_path) = explode(':', $_path, 2); 256 $_startPoint .= ':'; 257 } 258 // From now processing is the same for WIndows and Unix, 259 // and hopefully for others. 260 $_realparts = array(); 261 $_parts = explode(PX_OS_SEP, $_path); 262 for ($i=0; $i<count($_parts); $i++) { 263 if (strlen($_parts[$i]) == 0 || $_parts[$i] == '.') { 264 continue; 265 } 266 if ($_parts[$i] == '..') { 267 if (count($_realparts) > 0) { 268 array_pop($_realparts); 269 } 270 } else { 271 array_push($_realparts, $_parts[$i]); 272 } 273 } 274 return $_startPoint.PX_OS_SEP.implode(PX_OS_SEP, $_realparts); 275 } 276 277 } 278 279 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 11:57:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |