| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@author alex@kingleeds.com |
|---|
| 8 |
@author will@imarc.net |
|---|
| 9 |
@license http://flourishlib.com/license |
|---|
| 10 |
|
|---|
| 11 |
@package |
|---|
| 12 |
@link http://flourishlib.com/fFilesystem |
|---|
| 13 |
|
|---|
| 14 |
@version |
|---|
| 15 |
@changes |
|---|
| 16 |
@changes |
|---|
| 17 |
@changes |
|---|
| 18 |
@changes |
|---|
| 19 |
@changes |
|---|
| 20 |
@changes |
|---|
| 21 |
@changes |
|---|
| 22 |
@changes |
|---|
| 23 |
@changes |
|---|
| 24 |
@changes |
|---|
| 25 |
@changes |
|---|
| 26 |
@changes |
|---|
| 27 |
@changes |
|---|
| 28 |
|
|---|
| 29 |
class fFilesystem |
|---|
| 30 |
{ |
|---|
| 31 |
|
|---|
| 32 |
const addWebPathTranslation = 'fFilesystem::addWebPathTranslation'; |
|---|
| 33 |
const begin = 'fFilesystem::begin'; |
|---|
| 34 |
const commit = 'fFilesystem::commit'; |
|---|
| 35 |
const convertToBytes = 'fFilesystem::convertToBytes'; |
|---|
| 36 |
const createObject = 'fFilesystem::createObject'; |
|---|
| 37 |
const formatFilesize = 'fFilesystem::formatFilesize'; |
|---|
| 38 |
const getPathInfo = 'fFilesystem::getPathInfo'; |
|---|
| 39 |
const hookDeletedMap = 'fFilesystem::hookDeletedMap'; |
|---|
| 40 |
const hookFilenameMap = 'fFilesystem::hookFilenameMap'; |
|---|
| 41 |
const isInsideTransaction = 'fFilesystem::isInsideTransaction'; |
|---|
| 42 |
const makeUniqueName = 'fFilesystem::makeUniqueName'; |
|---|
| 43 |
const recordCreate = 'fFilesystem::recordCreate'; |
|---|
| 44 |
const recordDelete = 'fFilesystem::recordDelete'; |
|---|
| 45 |
const recordDuplicate = 'fFilesystem::recordDuplicate'; |
|---|
| 46 |
const recordRename = 'fFilesystem::recordRename'; |
|---|
| 47 |
const recordWrite = 'fFilesystem::recordWrite'; |
|---|
| 48 |
const reset = 'fFilesystem::reset'; |
|---|
| 49 |
const rollback = 'fFilesystem::rollback'; |
|---|
| 50 |
const translateToWebPath = 'fFilesystem::translateToWebPath'; |
|---|
| 51 |
const updateDeletedMap = 'fFilesystem::updateDeletedMap'; |
|---|
| 52 |
const updateFilenameMap = 'fFilesystem::updateFilenameMap'; |
|---|
| 53 |
const updateFilenameMapForDirectory = 'fFilesystem::updateFilenameMapForDirectory'; |
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
@var |
|---|
| 60 |
|
|---|
| 61 |
static private $commit_operations = NULL; |
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
@var |
|---|
| 67 |
|
|---|
| 68 |
static private $deleted_map = array(); |
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
@var |
|---|
| 74 |
|
|---|
| 75 |
static private $filename_map = array(); |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 |
@var |
|---|
| 81 |
|
|---|
| 82 |
static private $rollback_operations = NULL; |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
@var |
|---|
| 88 |
|
|---|
| 89 |
static private $web_path_translations = array(); |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
@param |
|---|
| 103 |
@param |
|---|
| 104 |
@return |
|---|
| 105 |
|
|---|
| 106 |
static public function addWebPathTranslation($search_path, $replace_path) |
|---|
| 107 |
{ |
|---|
| 108 |
|
|---|
| 109 |
$search_path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $search_path); |
|---|
| 110 |
$replace_path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $replace_path); |
|---|
| 111 |
self::$web_path_translations[$search_path] = $replace_path; |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
@return |
|---|
| 126 |
|
|---|
| 127 |
static public function begin() |
|---|
| 128 |
{ |
|---|
| 129 |
if (self::$commit_operations !== NULL) { |
|---|
| 130 |
throw new fProgrammerException( |
|---|
| 131 |
'There is already a filesystem transaction in progress' |
|---|
| 132 |
); |
|---|
| 133 |
} |
|---|
| 134 |
self::$commit_operations = array(); |
|---|
| 135 |
self::$rollback_operations = array(); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
@return |
|---|
| 143 |
|
|---|
| 144 |
static public function commit() |
|---|
| 145 |
{ |
|---|
| 146 |
if (!self::isInsideTransaction()) { |
|---|
| 147 |
throw new fProgrammerException( |
|---|
| 148 |
'There is no filesystem transaction in progress to commit' |
|---|
| 149 |
); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
$commit_operations = self::$commit_operations; |
|---|
| 153 |
|
|---|
| 154 |
self::$commit_operations = NULL; |
|---|
| 155 |
self::$rollback_operations = NULL; |
|---|
| 156 |
|
|---|
| 157 |
$commit_operations = array_reverse($commit_operations); |
|---|
| 158 |
|
|---|
| 159 |
foreach ($commit_operations as $operation) { |
|---|
| 160 |
|
|---|
| 161 |
if (isset($operation['filename'])) { |
|---|
| 162 |
unlink($operation['filename']); |
|---|
| 163 |
} else { |
|---|
| 164 |
$operation['object']->delete(); |
|---|
| 165 |
} |
|---|
| 166 |
} |
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 |
@param |
|---|
| 177 |
@return |
|---|
| 178 |
|
|---|
| 179 |
static public function convertToBytes($size) |
|---|
| 180 |
{ |
|---|
| 181 |
if (!preg_match('#^(\d+(?:\.\d+)?)\s*(k|m|g|t)?(ilo|ega|era|iga)?( )?b?(yte(s)?)?$#D', strtolower(trim($size)), $matches)) { |
|---|
| 182 |
throw new fProgrammerException( |
|---|
| 183 |
'The size specified, %s, does not appears to be a valid size', |
|---|
| 184 |
$size |
|---|
| 185 |
); |
|---|
| 186 |
} |
|---|
| 187 |
|
|---|
| 188 |
if (empty($matches[2])) { |
|---|
| 189 |
$matches[2] = 'b'; |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
$size_map = array('b' => 1, |
|---|
| 193 |
'k' => 1024, |
|---|
| 194 |
'm' => 1048576, |
|---|
| 195 |
'g' => 1073741824, |
|---|
| 196 |
't' => 1099511627776); |
|---|
| 197 |
return round($matches[1] * $size_map[$matches[2]]); |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
@throws |
|---|
| 205 |
|
|---|
| 206 |
@param |
|---|
| 207 |
@return |
|---|
| 208 |
|
|---|
| 209 |
static public function createObject($path) |
|---|
| 210 |
{ |
|---|
| 211 |
if (empty($path)) { |
|---|
| 212 |
throw new fValidationException( |
|---|
| 213 |
'No path was specified' |
|---|
| 214 |
); |
|---|
| 215 |
} |
|---|
| 216 |
|
|---|
| 217 |
if (!is_readable($path)) { |
|---|
| 218 |
throw new fValidationException( |
|---|
| 219 |
'The path specified, %s, does not exist or is not readable', |
|---|
| 220 |
$path |
|---|
| 221 |
); |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
if (is_dir($path)) { |
|---|
| 225 |
return new fDirectory($path, TRUE); |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
if (fImage::isImageCompatible($path)) { |
|---|
| 229 |
return new fImage($path, TRUE); |
|---|
| 230 |
} |
|---|
| 231 |
|
|---|
| 232 |
return new fFile($path, TRUE); |
|---|
| 233 |
} |
|---|
| 234 |
|
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
@param |
|---|
| 240 |
@param |
|---|
| 241 |
@return |
|---|
| 242 |
|
|---|
| 243 |
static public function formatFilesize($bytes, $decimal_places=1) |
|---|
| 244 |
{ |
|---|
| 245 |
if ($bytes < 0) { |
|---|
| 246 |
$bytes = 0; |
|---|
| 247 |
} |
|---|
| 248 |
$suffixes = array('B', 'K', 'M', 'G', 'T'); |
|---|
| 249 |
$sizes = array(1, 1024, 1048576, 1073741824, 1099511627776); |
|---|
| 250 |
$suffix = (!$bytes) ? 0 : floor(log($bytes)/6.9314718); |
|---|
| 251 |
return number_format($bytes/$sizes[$suffix], ($suffix == 0) ? 0 : $decimal_places) . ' ' . $suffixes[$suffix]; |
|---|
| 252 |
} |
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
@param |
|---|
| 259 |
@param |
|---|
| 260 |
@return |
|---|
| 261 |
|
|---|
| 262 |
static public function getPathInfo($path, $element=NULL) |
|---|
| 263 |
{ |
|---|
| 264 |
$valid_elements = array('dirname', 'basename', 'extension', 'filename'); |
|---|
| 265 |
if ($element !== NULL && !in_array($element, $valid_elements)) { |
|---|
| 266 |
throw new fProgrammerException( |
|---|
| 267 |
'The element specified, %1$s, is invalid. Must be one of: %2$s.', |
|---|
| 268 |
$element, |
|---|
| 269 |
join(', ', $valid_elements) |
|---|
| 270 |
); |
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
$path_info = pathinfo($path); |
|---|
| 274 |
|
|---|
| 275 |
if (!isset($path_info['extension'])) { |
|---|
| 276 |
$path_info['extension'] = NULL; |
|---|
| 277 |
} |
|---|
| 278 |
|
|---|
| 279 |
if (!isset($path_info['filename'])) { |
|---|
| 280 |
$path_info['filename'] = preg_replace('#\.' . preg_quote($path_info['extension'], '#') . '$#D', '', $path_info['basename']); |
|---|
| 281 |
} |
|---|
| 282 |
$path_info['dirname'] .= DIRECTORY_SEPARATOR; |
|---|
| 283 |
|
|---|
| 284 |
if ($element) { |
|---|
| 285 |
return $path_info[$element]; |
|---|
| 286 |
} |
|---|
| 287 |
|
|---|
| 288 |
return $path_info; |
|---|
| 289 |
} |
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
@internal |
|---|
| 299 |
|
|---|
| 300 |
@param |
|---|
| 301 |
@return |
|---|
| 302 |
|
|---|
| 303 |
static public function &hookDeletedMap($file) |
|---|
| 304 |
{ |
|---|
| 305 |
if (!isset(self::$deleted_map[$file])) { |
|---|
| 306 |
self::$deleted_map[$file] = NULL; |
|---|
| 307 |
} |
|---|
| 308 |
return self::$deleted_map[$file]; |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
@internal |
|---|
| 319 |
|
|---|
| 320 |
@param |
|---|
| 321 |
@return |
|---|
| 322 |
|
|---|
| 323 |
static public function &hookFilenameMap($file) |
|---|
| 324 |
{ |
|---|
| 325 |
if (!isset(self::$filename_map[$file])) { |
|---|
| 326 |
self::$filename_map[$file] = $file; |
|---|
| 327 |
} |
|---|
| 328 |
return self::$filename_map[$file]; |
|---|
| 329 |
} |
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
@return |
|---|
| 336 |
|
|---|
| 337 |
static public function isInsideTransaction() |
|---|
| 338 |
{ |
|---|
| 339 |
return is_array(self::$commit_operations); |
|---|
| 340 |
} |
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
@param |
|---|
| 347 |
@return |
|---|
| 348 |
|
|---|
| 349 |
static public function makeURLSafe($filename) |
|---|
| 350 |
{ |
|---|
| 351 |
$filename = strtolower(trim($filename)); |
|---|
| 352 |
$filename = str_replace("'", '', $filename); |
|---|
| 353 |
return preg_replace('#[^a-z0-9\-\.]+#', '_', $filename); |
|---|
| 354 |
} |
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
@param |
|---|
| 361 |
@param |
|---|
| 362 |
@return |
|---|
| 363 |
|
|---|
| 364 |
static public function makeUniqueName($file, $new_extension=NULL) |
|---|
| 365 |
{ |
|---|
| 366 |
$info = self::getPathInfo($file); |
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
if ($new_extension !== NULL) { |
|---|
| 370 |
$new_extension = ($new_extension) ? '.' . $new_extension : $new_extension; |
|---|
| 371 |
$file = $info['dirname'] . $info['filename'] . $new_extension; |
|---|
| 372 |
$info = self::getPathInfo($file); |
|---|
| 373 |
} |
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
$extension = (!empty($info['extension'])) ? '.' . $info['extension'] : ''; |
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
$file = preg_replace('#_copy(\d+)' . preg_quote($extension, '#') . '$#D', $extension, $file); |
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
while (file_exists($file)) { |
|---|
| 383 |
$info = self::getPathInfo($file); |
|---|
| 384 |
if (preg_match('#_copy(\d+)' . preg_quote($extension, '#') . '$#D', $file, $match)) { |
|---|
| 385 |
$file = preg_replace('#_copy(\d+)' . preg_quote($extension, '#') . '$#D', '_copy' . ($match[1]+1) . $extension, $file); |
|---|
| 386 |
} else { |
|---|
| 387 |
$file = $info['dirname'] . $info['filename'] . '_copy1' . $extension; |
|---|
| 388 |
} |
|---|
| 389 |
} |
|---|
| 390 |
|
|---|
| 391 |
return $file; |
|---|
| 392 |
} |
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
@internal |
|---|
| 399 |
|
|---|
| 400 |
@param |
|---|
| 401 |
@param |
|---|
| 402 |
@return |
|---|
| 403 |
|
|---|
| 404 |
static public function updateDeletedMap($file, $backtrace) |
|---|
| 405 |
{ |
|---|
| 406 |
self::$deleted_map[$file] = $backtrace; |
|---|
| 407 |
} |
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
@internal |
|---|
| 414 |
|
|---|
| 415 |
@param |
|---|
| 416 |
@param |
|---|
| 417 |
@return |
|---|
| 418 |
|
|---|
| 419 |
static public function updateFilenameMap($existing_filename, $new_filename) |
|---|
| 420 |
{ |
|---|
| 421 |
if ($existing_filename == $new_filename) { |
|---|
| 422 |
return; |
|---|
| 423 |
} |
|---|
| 424 |
|
|---|
| 425 |
self::$filename_map[$new_filename] =& self::$filename_map[$existing_filename]; |
|---|
| 426 |
self::$deleted_map[$new_filename] =& self::$deleted_map[$existing_filename]; |
|---|
| 427 |
|
|---|
| 428 |
unset(self::$filename_map[$existing_filename]); |
|---|
| 429 |
unset(self::$deleted_map[$existing_filename]); |
|---|
| 430 |
|
|---|
| 431 |
self::$filename_map[$new_filename] = $new_filename; |
|---|
| 432 |
} |
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
@internal |
|---|
| 441 |
|
|---|
| 442 |
@param |
|---|
| 443 |
@param |
|---|
| 444 |
@return |
|---|
| 445 |
|
|---|
| 446 |
static public function updateFilenameMapForDirectory($existing_dirname, $new_dirname) |
|---|
| 447 |
{ |
|---|
| 448 |
if ($existing_dirname == $new_dirname) { |
|---|
| 449 |
return; |
|---|
| 450 |
} |
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
self::$filename_map[$new_dirname] =& self::$filename_map[$existing_dirname]; |
|---|
| 454 |
self::$deleted_map[$new_dirname] =& self::$deleted_map[$existing_dirname]; |
|---|
| 455 |
|
|---|
| 456 |
unset(self::$filename_map[$existing_dirname]); |
|---|
| 457 |
unset(self::$deleted_map[$existing_dirname]); |
|---|
| 458 |
|
|---|
| 459 |
self::$filename_map[$new_dirname] = $new_dirname; |
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
foreach (self::$filename_map as $filename => $ignore) { |
|---|
| 463 |
if (preg_match('#^' . preg_quote($existing_dirname, '#') . '#', $filename)) { |
|---|
| 464 |
$new_filename = preg_replace( |
|---|
| 465 |
'#^' . preg_quote($existing_dirname, '#') . '#', |
|---|
| 466 |
strtr($new_dirname, array('\\' => '\\\\', '$' => '\\$')), |
|---|
| 467 |
$filename |
|---|
| 468 |
); |
|---|
| 469 |
|
|---|
| 470 |
self::$filename_map[$new_filename] =& self::$filename_map[$filename]; |
|---|
| 471 |
self::$deleted_map[$new_filename] =& self::$deleted_map[$filename]; |
|---|
| 472 |
|
|---|
| 473 |
unset(self::$filename_map[$filename]); |
|---|
| 474 |
unset(self::$deleted_map[$filename]); |
|---|
| 475 |
|
|---|
| 476 |
self::$filename_map[$new_filename] = $new_filename; |
|---|
| 477 |
|
|---|
| 478 |
} |
|---|
| 479 |
} |
|---|
| 480 |
} |
|---|
| 481 |
|
|---|
| 482 |
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 |
|
|---|
| 486 |
@internal |
|---|
| 487 |
|
|---|
| 488 |
@param |
|---|
| 489 |
@return |
|---|
| 490 |
|
|---|
| 491 |
static public function recordCreate($object) |
|---|
| 492 |
{ |
|---|
| 493 |
self::$rollback_operations[] = array( |
|---|
| 494 |
'action' => 'delete', |
|---|
| 495 |
'object' => $object |
|---|
| 496 |
); |
|---|
| 497 |
} |
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
@internal |
|---|
| 504 |
|
|---|
| 505 |
@param |
|---|
| 506 |
@return |
|---|
| 507 |
|
|---|
| 508 |
static public function recordDelete($object) |
|---|
| 509 |
{ |
|---|
| 510 |
self::$commit_operations[] = array( |
|---|
| 511 |
'action' => 'delete', |
|---|
| 512 |
'object' => $object |
|---|
| 513 |
); |
|---|
| 514 |
} |
|---|
| 515 |
|
|---|
| 516 |
|
|---|
| 517 |
|
|---|
| 518 |
|
|---|
| 519 |
|
|---|
| 520 |
@internal |
|---|
| 521 |
|
|---|
| 522 |
@param |
|---|
| 523 |
@return |
|---|
| 524 |
|
|---|
| 525 |
static public function recordDuplicate($file) |
|---|
| 526 |
{ |
|---|
| 527 |
self::$rollback_operations[] = array( |
|---|
| 528 |
'action' => 'delete', |
|---|
| 529 |
'filename' => $file->getPath() |
|---|
| 530 |
); |
|---|
| 531 |
} |
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 |
|
|---|
| 537 |
@internal |
|---|
| 538 |
|
|---|
| 539 |
@param |
|---|
| 540 |
@param |
|---|
| 541 |
@return |
|---|
| 542 |
|
|---|
| 543 |
static public function recordRename($old_name, $new_name) |
|---|
| 544 |
{ |
|---|
| 545 |
self::$rollback_operations[] = array( |
|---|
| 546 |
'action' => 'rename', |
|---|
| 547 |
'old_name' => $old_name, |
|---|
| 548 |
'new_name' => $new_name |
|---|
| 549 |
); |
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 |
file_put_contents($old_name, ''); |
|---|
| 553 |
|
|---|
| 554 |
self::$commit_operations[] = array( |
|---|
| 555 |
'action' => 'delete', |
|---|
| 556 |
'filename' => $old_name |
|---|
| 557 |
); |
|---|
| 558 |
} |
|---|
| 559 |
|
|---|
| 560 |
|
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
@internal |
|---|
| 565 |
|
|---|
| 566 |
@param |
|---|
| 567 |
@return |
|---|
| 568 |
|
|---|
| 569 |
static public function recordWrite($file) |
|---|
| 570 |
{ |
|---|
| 571 |
self::$rollback_operations[] = array( |
|---|
| 572 |
'action' => 'write', |
|---|
| 573 |
'filename' => $file->getPath(), |
|---|
| 574 |
'old_data' => file_get_contents($file->getPath()) |
|---|
| 575 |
); |
|---|
| 576 |
} |
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
|
|---|
| 580 |
|
|---|
| 581 |
|
|---|
| 582 |
@internal |
|---|
| 583 |
|
|---|
| 584 |
@return |
|---|
| 585 |
|
|---|
| 586 |
static public function reset() |
|---|
| 587 |
{ |
|---|
| 588 |
self::rollback(); |
|---|
| 589 |
self::$deleted_map = array(); |
|---|
| 590 |
self::$filename_map = array(); |
|---|
| 591 |
self::$web_path_translations = array(); |
|---|
| 592 |
} |
|---|
| 593 |
|
|---|
| 594 |
|
|---|
| 595 |
|
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 |
@return |
|---|
| 599 |
|
|---|
| 600 |
static public function rollback() |
|---|
| 601 |
{ |
|---|
| 602 |
self::$rollback_operations = array_reverse(self::$rollback_operations); |
|---|
| 603 |
|
|---|
| 604 |
foreach (self::$rollback_operations as $operation) { |
|---|
| 605 |
switch($operation['action']) { |
|---|
| 606 |
|
|---|
| 607 |
case 'delete': |
|---|
| 608 |
self::updateDeletedMap( |
|---|
| 609 |
$operation['filename'], |
|---|
| 610 |
debug_backtrace() |
|---|
| 611 |
); |
|---|
| 612 |
unlink($operation['filename']); |
|---|
| 613 |
fFilesystem::updateFilenameMap($operation['filename'], '*DELETED at ' . time() . ' with token ' . uniqid('', TRUE) . '* ' . $operation['filename']); |
|---|
| 614 |
break; |
|---|
| 615 |
|
|---|
| 616 |
case 'write': |
|---|
| 617 |
file_put_contents($operation['filename'], $operation['old_data']); |
|---|
| 618 |
break; |
|---|
| 619 |
|
|---|
| 620 |
case 'rename': |
|---|
| 621 |
fFilesystem::updateFilenameMap($operation['new_name'], $operation['old_name']); |
|---|
| 622 |
rename($operation['new_name'], $operation['old_name']); |
|---|
| 623 |
break; |
|---|
| 624 |
|
|---|
| 625 |
} |
|---|
| 626 |
} |
|---|
| 627 |
|
|---|
| 628 |
|
|---|
| 629 |
foreach (self::$commit_operations as $operation) { |
|---|
| 630 |
if (isset($operation['object'])) { |
|---|
| 631 |
self::updateDeletedMap($operation['object']->getPath(), NULL); |
|---|
| 632 |
fFilesystem::updateFilenameMap($operation['object']->getPath(), preg_replace('#*DELETED at \d+ with token [\w.]+* #', '', $operation['filename'])); |
|---|
| 633 |
} |
|---|
| 634 |
} |
|---|
| 635 |
|
|---|
| 636 |
self::$commit_operations = NULL; |
|---|
| 637 |
self::$rollback_operations = NULL; |
|---|
| 638 |
} |
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
@param |
|---|
| 645 |
@return |
|---|
| 646 |
|
|---|
| 647 |
static public function translateToWebPath($path) |
|---|
| 648 |
{ |
|---|
| 649 |
$translations = array(realpath($_SERVER['DOCUMENT_ROOT']) => '') + self::$web_path_translations; |
|---|
| 650 |
|
|---|
| 651 |
foreach ($translations as $search => $replace) { |
|---|
| 652 |
$path = preg_replace( |
|---|
| 653 |
'#^' . preg_quote($search, '#') . '#', |
|---|
| 654 |
strtr($replace, array('\\' => '\\\\', '$' => '\\$')), |
|---|
| 655 |
$path |
|---|
| 656 |
); |
|---|
| 657 |
} |
|---|
| 658 |
|
|---|
| 659 |
return $path; |
|---|
| 660 |
} |
|---|
| 661 |
|
|---|
| 662 |
|
|---|
| 663 |
|
|---|
| 664 |
|
|---|
| 665 |
|
|---|
| 666 |
@return |
|---|
| 667 |
|
|---|
| 668 |
private function __construct() { } |
|---|
| 669 |
} |
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 |
will@flourishlib.com |
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 |
|
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
|
|---|
| 683 |
|
|---|
| 684 |
|
|---|
| 685 |
|
|---|
| 686 |
|
|---|
| 687 |
|
|---|
| 688 |
|
|---|
| 689 |
|
|---|
| 690 |
|
|---|
| 691 |
|
|---|
| 692 |
|
|---|
| 693 |
|
|---|