| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
@copyright |
|---|
| 6 |
@author will@flourishlib.com |
|---|
| 7 |
@author will@imarc.net |
|---|
| 8 |
@license http://flourishlib.com/license |
|---|
| 9 |
|
|---|
| 10 |
@package |
|---|
| 11 |
@link http://flourishlib.com/fFile |
|---|
| 12 |
|
|---|
| 13 |
@version |
|---|
| 14 |
@changes |
|---|
| 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 |
@changes |
|---|
| 29 |
@changes |
|---|
| 30 |
@changes |
|---|
| 31 |
@changes |
|---|
| 32 |
@changes |
|---|
| 33 |
@changes |
|---|
| 34 |
@changes |
|---|
| 35 |
@changes |
|---|
| 36 |
@changes |
|---|
| 37 |
@changes |
|---|
| 38 |
@changes |
|---|
| 39 |
@changes |
|---|
| 40 |
@changes |
|---|
| 41 |
@changes |
|---|
| 42 |
@changes |
|---|
| 43 |
@changes |
|---|
| 44 |
|
|---|
| 45 |
class fFile implements Iterator |
|---|
| 46 |
{ |
|---|
| 47 |
|
|---|
| 48 |
const create = 'fFile::create'; |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
@throws |
|---|
| 57 |
|
|---|
| 58 |
@param |
|---|
| 59 |
@param |
|---|
| 60 |
@return |
|---|
| 61 |
|
|---|
| 62 |
static public function create($file_path, $contents) |
|---|
| 63 |
{ |
|---|
| 64 |
if (empty($file_path)) { |
|---|
| 65 |
throw new fValidationException( |
|---|
| 66 |
'No filename was specified' |
|---|
| 67 |
); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
if (file_exists($file_path)) { |
|---|
| 71 |
throw new fValidationException( |
|---|
| 72 |
'The file specified, %s, already exists', |
|---|
| 73 |
$file_path |
|---|
| 74 |
); |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
$directory = fFilesystem::getPathInfo($file_path, 'dirname'); |
|---|
| 78 |
if (!is_writable($directory)) { |
|---|
| 79 |
throw new fEnvironmentException( |
|---|
| 80 |
'The file path specified, %s, is inside of a directory that is not writable', |
|---|
| 81 |
$file_path |
|---|
| 82 |
); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
file_put_contents($file_path, $contents); |
|---|
| 86 |
|
|---|
| 87 |
$file = new fFile($file_path); |
|---|
| 88 |
|
|---|
| 89 |
fFilesystem::recordCreate($file); |
|---|
| 90 |
|
|---|
| 91 |
return $file; |
|---|
| 92 |
} |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
@internal |
|---|
| 102 |
|
|---|
| 103 |
@param |
|---|
| 104 |
@param |
|---|
| 105 |
@return |
|---|
| 106 |
|
|---|
| 107 |
static public function determineMimeType($file, $contents=NULL) |
|---|
| 108 |
{ |
|---|
| 109 |
|
|---|
| 110 |
if ($contents === NULL) { |
|---|
| 111 |
if (!file_exists($file)) { |
|---|
| 112 |
throw new fValidationException( |
|---|
| 113 |
'The file specified, %s, does not exist', |
|---|
| 114 |
$file |
|---|
| 115 |
); |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
$handle = fopen($file, 'r'); |
|---|
| 120 |
$contents = fread($handle, 4096); |
|---|
| 121 |
fclose($handle); |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
$extension = strtolower(fFilesystem::getPathInfo($file, 'extension')); |
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 |
if (!preg_match('#[\x00-\x08\x0B\x0C\x0E-\x1F]|%PDF-|<\?php|\%\!PS-Adobe-3|<\?xml|\{\\\\rtf|<\?=|<html|<\!doctype|<rss|\#\![/a-z0-9]+(python|ruby|perl|php)\b#i', $contents)) { |
|---|
| 128 |
return self::determineMimeTypeByExtension($extension); |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
return self::determineMimeTypeByContents($contents, $extension); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
@param |
|---|
| 139 |
@param |
|---|
| 140 |
@return |
|---|
| 141 |
|
|---|
| 142 |
static private function determineMimeTypeByContents($content, $extension) |
|---|
| 143 |
{ |
|---|
| 144 |
$length = strlen($content); |
|---|
| 145 |
$_0_8 = substr($content, 0, 8); |
|---|
| 146 |
$_0_6 = substr($content, 0, 6); |
|---|
| 147 |
$_0_5 = substr($content, 0, 5); |
|---|
| 148 |
$_0_4 = substr($content, 0, 4); |
|---|
| 149 |
$_0_3 = substr($content, 0, 3); |
|---|
| 150 |
$_0_2 = substr($content, 0, 2); |
|---|
| 151 |
$_8_4 = substr($content, 8, 4); |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
if ($_0_4 == "MM\x00\x2A" || $_0_4 == "II\x2A\x00") { |
|---|
| 155 |
return 'image/tiff'; |
|---|
| 156 |
} |
|---|
| 157 |
|
|---|
| 158 |
if ($_0_8 == "\x89PNG\x0D\x0A\x1A\x0A") { |
|---|
| 159 |
return 'image/png'; |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
if ($_0_4 == 'GIF8') { |
|---|
| 163 |
return 'image/gif'; |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
if ($_0_2 == 'BM' && strlen($content) > 14 && array($content[14], array("\x0C", "\x28", "\x40", "\x80"))) { |
|---|
| 167 |
return 'image/x-ms-bmp'; |
|---|
| 168 |
} |
|---|
| 169 |
|
|---|
| 170 |
$normal_jpeg = $length > 10 && in_array(substr($content, 6, 4), array('JFIF', 'Exif')); |
|---|
| 171 |
$photoshop_jpeg = $length > 24 && $_0_4 == "\xFF\xD8\xFF\xED" && substr($content, 20, 4) == '8BIM'; |
|---|
| 172 |
if ($normal_jpeg || $photoshop_jpeg) { |
|---|
| 173 |
return 'image/jpeg'; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
if (preg_match('#^[^\n\r]*\%\!PS-Adobe-3#', $content)) { |
|---|
| 177 |
return 'application/postscript'; |
|---|
| 178 |
} |
|---|
| 179 |
|
|---|
| 180 |
if ($_0_4 == "\x00\x00\x01\x00") { |
|---|
| 181 |
return 'application/vnd.microsoft.icon'; |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
if ($_0_4 == 'MOVI') { |
|---|
| 187 |
if (in_array($_4_4, array('moov', 'mdat'))) { |
|---|
| 188 |
return 'video/quicktime'; |
|---|
| 189 |
} |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
if ($length > 8 && substr($content, 4, 4) == 'ftyp') { |
|---|
| 193 |
|
|---|
| 194 |
$_8_3 = substr($content, 8, 3); |
|---|
| 195 |
$_8_2 = substr($content, 8, 2); |
|---|
| 196 |
|
|---|
| 197 |
if (in_array($_8_4, array('isom', 'iso2', 'mp41', 'mp42'))) { |
|---|
| 198 |
return 'video/mp4'; |
|---|
| 199 |
} |
|---|
| 200 |
|
|---|
| 201 |
if ($_8_3 == 'M4A') { |
|---|
| 202 |
return 'audio/mp4'; |
|---|
| 203 |
} |
|---|
| 204 |
|
|---|
| 205 |
if ($_8_3 == 'M4V') { |
|---|
| 206 |
return 'video/mp4'; |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
if ($_8_3 == 'M4P' || $_8_3 == 'M4B' || $_8_2 == 'qt') { |
|---|
| 210 |
return 'video/quicktime'; |
|---|
| 211 |
} |
|---|
| 212 |
} |
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 |
if (($_0_2 & "\xFF\xF6") == "\xFF\xF2") { |
|---|
| 216 |
if (($content[2] & "\xF0") != "\xF0" && ($content[2] & "\x0C") != "\x0C") { |
|---|
| 217 |
return 'audio/mpeg'; |
|---|
| 218 |
} |
|---|
| 219 |
} |
|---|
| 220 |
if ($_0_3 == 'ID3') { |
|---|
| 221 |
return 'audio/mpeg'; |
|---|
| 222 |
} |
|---|
| 223 |
|
|---|
| 224 |
if ($_0_8 == "\x30\x26\xB2\x75\x8E\x66\xCF\x11") { |
|---|
| 225 |
if ($content[24] == "\x07") { |
|---|
| 226 |
return 'audio/x-ms-wma'; |
|---|
| 227 |
} |
|---|
| 228 |
if ($content[24] == "\x08") { |
|---|
| 229 |
return 'video/x-ms-wmv'; |
|---|
| 230 |
} |
|---|
| 231 |
return 'video/x-ms-asf'; |
|---|
| 232 |
} |
|---|
| 233 |
|
|---|
| 234 |
if ($_0_4 == 'RIFF' && $_8_4 == 'AVI ') { |
|---|
| 235 |
return 'video/x-msvideo'; |
|---|
| 236 |
} |
|---|
| 237 |
|
|---|
| 238 |
if ($_0_4 == 'RIFF' && $_8_4 == 'WAVE') { |
|---|
| 239 |
return 'audio/x-wav'; |
|---|
| 240 |
} |
|---|
| 241 |
|
|---|
| 242 |
if ($_0_4 == 'OggS') { |
|---|
| 243 |
$_28_5 = substr($content, 28, 5); |
|---|
| 244 |
if ($_28_5 == "\x01\x76\x6F\x72\x62") { |
|---|
| 245 |
return 'audio/vorbis'; |
|---|
| 246 |
} |
|---|
| 247 |
if ($_28_5 == "\x07\x46\x4C\x41\x43") { |
|---|
| 248 |
return 'audio/x-flac'; |
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
if ($_28_5 == "\x80\x74\x68\x65\x6F" || $_28_5 == "\x76\x69\x64\x65") { |
|---|
| 252 |
return 'video/ogg'; |
|---|
| 253 |
} |
|---|
| 254 |
} |
|---|
| 255 |
|
|---|
| 256 |
if ($_0_3 == 'FWS' || $_0_3 == 'CWS') { |
|---|
| 257 |
return 'application/x-shockwave-flash'; |
|---|
| 258 |
} |
|---|
| 259 |
|
|---|
| 260 |
if ($_0_3 == 'FLV') { |
|---|
| 261 |
return 'video/x-flv'; |
|---|
| 262 |
} |
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
if ($_0_5 == '%PDF-') { |
|---|
| 267 |
return 'application/pdf'; |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
if ($_0_5 == '{\rtf') { |
|---|
| 271 |
return 'text/rtf'; |
|---|
| 272 |
} |
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
if ($_0_8 == "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1" || $_0_8 == "PK\x03\x04\x14\x00\x06\x00") { |
|---|
| 276 |
if (in_array($extension, array('xlsx', 'xls', 'csv', 'tab'))) { |
|---|
| 277 |
return 'application/vnd.ms-excel'; |
|---|
| 278 |
} |
|---|
| 279 |
if (in_array($extension, array('pptx', 'ppt'))) { |
|---|
| 280 |
return 'application/vnd.ms-powerpoint'; |
|---|
| 281 |
} |
|---|
| 282 |
|
|---|
| 283 |
return 'application/msword'; |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
if ($_0_8 == "\x09\x04\x06\x00\x00\x00\x10\x00") { |
|---|
| 287 |
return 'application/vnd.ms-excel'; |
|---|
| 288 |
} |
|---|
| 289 |
|
|---|
| 290 |
if ($_0_6 == "\xDB\xA5\x2D\x00\x00\x00" || $_0_5 == "\x50\x4F\x5E\x51\x60" || $_0_4 == "\xFE\x37\x0\x23" || $_0_3 == "\x94\xA6\x2E") { |
|---|
| 291 |
return 'application/msword'; |
|---|
| 292 |
} |
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
if ($_0_4 == "PK\x03\x04") { |
|---|
| 297 |
return 'application/zip'; |
|---|
| 298 |
} |
|---|
| 299 |
|
|---|
| 300 |
if ($length > 257) { |
|---|
| 301 |
if (substr($content, 257, 6) == "ustar\x00") { |
|---|
| 302 |
return 'application/x-tar'; |
|---|
| 303 |
} |
|---|
| 304 |
if (substr($content, 257, 8) == "ustar\x40\x40\x00") { |
|---|
| 305 |
return 'application/x-tar'; |
|---|
| 306 |
} |
|---|
| 307 |
} |
|---|
| 308 |
|
|---|
| 309 |
if ($_0_4 == 'Rar!') { |
|---|
| 310 |
return 'application/x-rar-compressed'; |
|---|
| 311 |
} |
|---|
| 312 |
|
|---|
| 313 |
if ($_0_2 == "\x1F\x9D") { |
|---|
| 314 |
return 'application/x-compress'; |
|---|
| 315 |
} |
|---|
| 316 |
|
|---|
| 317 |
if ($_0_2 == "\x1F\x8B") { |
|---|
| 318 |
return 'application/x-gzip'; |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
if ($_0_3 == 'BZh') { |
|---|
| 322 |
return 'application/x-bzip2'; |
|---|
| 323 |
} |
|---|
| 324 |
|
|---|
| 325 |
if ($_0_4 == "SIT!" || $_0_4 == "SITD" || substr($content, 0, 7) == 'StuffIt') { |
|---|
| 326 |
return 'application/x-stuffit'; |
|---|
| 327 |
} |
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
if (strpos($content, '<?xml') !== FALSE) { |
|---|
| 332 |
if (stripos($content, '<!DOCTYPE') !== FALSE) { |
|---|
| 333 |
return 'application/xhtml+xml'; |
|---|
| 334 |
} |
|---|
| 335 |
if (strpos($content, '<svg') !== FALSE) { |
|---|
| 336 |
return 'image/svg+xml'; |
|---|
| 337 |
} |
|---|
| 338 |
if (strpos($content, '<rss') !== FALSE) { |
|---|
| 339 |
return 'application/rss+xml'; |
|---|
| 340 |
} |
|---|
| 341 |
return 'application/xml'; |
|---|
| 342 |
} |
|---|
| 343 |
|
|---|
| 344 |
if (strpos($content, '<?php') !== FALSE || strpos($content, '<?=') !== FALSE) { |
|---|
| 345 |
return 'application/x-httpd-php'; |
|---|
| 346 |
} |
|---|
| 347 |
|
|---|
| 348 |
if (preg_match('#^\#\![/a-z0-9]+(python|perl|php|ruby)$#mi', $content, $matches)) { |
|---|
| 349 |
switch (strtolower($matches[1])) { |
|---|
| 350 |
case 'php': |
|---|
| 351 |
return 'application/x-httpd-php'; |
|---|
| 352 |
case 'python': |
|---|
| 353 |
return 'application/x-python'; |
|---|
| 354 |
case 'perl': |
|---|
| 355 |
return 'application/x-perl'; |
|---|
| 356 |
case 'ruby': |
|---|
| 357 |
return 'application/x-ruby'; |
|---|
| 358 |
} |
|---|
| 359 |
} |
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
return 'application/octet-stream'; |
|---|
| 364 |
} |
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
@param |
|---|
| 371 |
@return |
|---|
| 372 |
|
|---|
| 373 |
static private function determineMimeTypeByExtension($extension) |
|---|
| 374 |
{ |
|---|
| 375 |
switch ($extension) { |
|---|
| 376 |
case 'css': |
|---|
| 377 |
return 'text/css'; |
|---|
| 378 |
|
|---|
| 379 |
case 'csv': |
|---|
| 380 |
return 'text/csv'; |
|---|
| 381 |
|
|---|
| 382 |
case 'htm': |
|---|
| 383 |
case 'html': |
|---|
| 384 |
case 'xhtml': |
|---|
| 385 |
return 'text/html'; |
|---|
| 386 |
|
|---|
| 387 |
case 'ics': |
|---|
| 388 |
return 'text/calendar'; |
|---|
| 389 |
|
|---|
| 390 |
case 'js': |
|---|
| 391 |
return 'application/javascript'; |
|---|
| 392 |
|
|---|
| 393 |
case 'php': |
|---|
| 394 |
case 'php3': |
|---|
| 395 |
case 'php4': |
|---|
| 396 |
case 'php5': |
|---|
| 397 |
case 'inc': |
|---|
| 398 |
return 'application/x-httpd-php'; |
|---|
| 399 |
|
|---|
| 400 |
case 'pl': |
|---|
| 401 |
case 'cgi': |
|---|
| 402 |
return 'application/x-perl'; |
|---|
| 403 |
|
|---|
| 404 |
case 'py': |
|---|
| 405 |
return 'application/x-python'; |
|---|
| 406 |
|
|---|
| 407 |
case 'rb': |
|---|
| 408 |
case 'rhtml': |
|---|
| 409 |
return 'application/x-ruby'; |
|---|
| 410 |
|
|---|
| 411 |
case 'rss': |
|---|
| 412 |
return 'application/rss+xml'; |
|---|
| 413 |
|
|---|
| 414 |
case 'tab': |
|---|
| 415 |
return 'text/tab-separated-values'; |
|---|
| 416 |
|
|---|
| 417 |
case 'vcf': |
|---|
| 418 |
return 'text/x-vcard'; |
|---|
| 419 |
|
|---|
| 420 |
case 'xml': |
|---|
| 421 |
return 'application/xml'; |
|---|
| 422 |
|
|---|
| 423 |
default: |
|---|
| 424 |
return 'text/plain'; |
|---|
| 425 |
} |
|---|
| 426 |
} |
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
@var |
|---|
| 433 |
|
|---|
| 434 |
private $current_line = NULL; |
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
@var |
|---|
| 440 |
|
|---|
| 441 |
private $current_line_number = NULL; |
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
@var |
|---|
| 447 |
|
|---|
| 448 |
protected $deleted = NULL; |
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
@var |
|---|
| 454 |
|
|---|
| 455 |
protected $file; |
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
@var |
|---|
| 461 |
|
|---|
| 462 |
private $file_handle = NULL; |
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
@internal |
|---|
| 469 |
|
|---|
| 470 |
@return |
|---|
| 471 |
|
|---|
| 472 |
public function __clone() |
|---|
| 473 |
{ |
|---|
| 474 |
$this->tossIfDeleted(); |
|---|
| 475 |
|
|---|
| 476 |
$directory = $this->getParent(); |
|---|
| 477 |
|
|---|
| 478 |
if (!$directory->isWritable()) { |
|---|
| 479 |
throw new fEnvironmentException( |
|---|
| 480 |
'The file count not be cloned because the containing directory, %s, is not writable', |
|---|
| 481 |
$directory |
|---|
| 482 |
); |
|---|
| 483 |
} |
|---|
| 484 |
|
|---|
| 485 |
$file = fFilesystem::makeUniqueName($directory->getPath() . $this->getName()); |
|---|
| 486 |
|
|---|
| 487 |
copy($this->getPath(), $file); |
|---|
| 488 |
chmod($file, fileperms($this->getPath())); |
|---|
| 489 |
|
|---|
| 490 |
$this->file =& fFilesystem::hookFilenameMap($file); |
|---|
| 491 |
$this->deleted =& fFilesystem::hookDeletedMap($file); |
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
if (fFilesystem::isInsideTransaction()) { |
|---|
| 495 |
fFilesystem::recordDuplicate($this); |
|---|
| 496 |
} |
|---|
| 497 |
} |
|---|
| 498 |
|
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
|
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
@throws |
|---|
| 507 |
|
|---|
| 508 |
@param |
|---|
| 509 |
@param |
|---|
| 510 |
@return |
|---|
| 511 |
|
|---|
| 512 |
public function __construct($file, $skip_checks=FALSE) |
|---|
| 513 |
{ |
|---|
| 514 |
if (!$skip_checks) { |
|---|
| 515 |
if (empty($file)) { |
|---|
| 516 |
throw new fValidationException( |
|---|
| 517 |
'No filename was specified' |
|---|
| 518 |
); |
|---|
| 519 |
} |
|---|
| 520 |
|
|---|
| 521 |
if (!is_readable($file)) { |
|---|
| 522 |
throw new fValidationException( |
|---|
| 523 |
'The file specified, %s, does not exist or is not readable', |
|---|
| 524 |
$file |
|---|
| 525 |
); |
|---|
| 526 |
} |
|---|
| 527 |
if (is_dir($file)) { |
|---|
| 528 |
throw new fValidationException( |
|---|
| 529 |
'The file specified, %s, is actually a directory', |
|---|
| 530 |
$file |
|---|
| 531 |
); |
|---|
| 532 |
} |
|---|
| 533 |
} |
|---|
| 534 |
|
|---|
| 535 |
|
|---|
| 536 |
$file = realpath($file); |
|---|
| 537 |
|
|---|
| 538 |
$this->file =& fFilesystem::hookFilenameMap($file); |
|---|
| 539 |
$this->deleted =& fFilesystem::hookDeletedMap($file); |
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
if ($this->deleted !== NULL && !fFilesystem::isInsideTransaction()) { |
|---|
| 544 |
fFilesystem::updateDeletedMap($file, NULL); |
|---|
| 545 |
} |
|---|
| 546 |
} |
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 |
@internal |
|---|
| 553 |
|
|---|
| 554 |
@param |
|---|
| 555 |
@return |
|---|
| 556 |
|
|---|
| 557 |
public function __get($method) |
|---|
| 558 |
{ |
|---|
| 559 |
return array($this, $method); |
|---|
| 560 |
} |
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 |
@internal |
|---|
| 567 |
|
|---|
| 568 |
@return |
|---|
| 569 |
|
|---|
| 570 |
public function __sleep() |
|---|
| 571 |
{ |
|---|
| 572 |
return array('deleted', 'file'); |
|---|
| 573 |
} |
|---|
| 574 |
|
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
@return |
|---|
| 580 |
|
|---|
| 581 |
public function __toString() |
|---|
| 582 |
{ |
|---|
| 583 |
try { |
|---|
| 584 |
return $this->getName(); |
|---|
| 585 |
} catch (Exception $e) { |
|---|
| 586 |
return ''; |
|---|
| 587 |
} |
|---|
| 588 |
} |
|---|
| 589 |
|
|---|
| 590 |
|
|---|
| 591 |
|
|---|
| 592 |
|
|---|
| 593 |
|
|---|
| 594 |
@internal |
|---|
| 595 |
|
|---|
| 596 |
@return |
|---|
| 597 |
|
|---|
| 598 |
public function __wakeup() |
|---|
| 599 |
{ |
|---|
| 600 |
$file = $this->file; |
|---|
| 601 |
$deleted = $this->deleted; |
|---|
| 602 |
|
|---|
| 603 |
$this->file =& fFilesystem::hookFilenameMap($file); |
|---|
| 604 |
$this->deleted =& fFilesystem::hookDeletedMap($file); |
|---|
| 605 |
|
|---|
| 606 |
if ($deleted !== NULL) { |
|---|
| 607 |
fFilesystem::updateDeletedMap($file, $deleted); |
|---|
| 608 |
} |
|---|
| 609 |
} |
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 |
|
|---|
| 613 |
|
|---|
| 614 |
|
|---|
| 615 |
@throws |
|---|
| 616 |
@internal |
|---|
| 617 |
|
|---|
| 618 |
@return |
|---|
| 619 |
|
|---|
| 620 |
public function current() |
|---|
| 621 |
{ |
|---|
| 622 |
$this->tossIfDeleted(); |
|---|
| 623 |
|
|---|
| 624 |
|
|---|
| 625 |
if ($this->file_handle === NULL) { |
|---|
| 626 |
$this->next(); |
|---|
| 627 |
|
|---|
| 628 |
} elseif (!$this->valid()) { |
|---|
| 629 |
throw new fNoRemainingException('There are no remaining lines'); |
|---|
| 630 |
} |
|---|
| 631 |
|
|---|
| 632 |
return $this->current_line; |
|---|
| 633 |
} |
|---|
| 634 |
|
|---|
| 635 |
|
|---|
| 636 |
|
|---|
| 637 |
|
|---|
| 638 |
|
|---|
| 639 |
|
|---|
| 640 |
|
|---|
| 641 |
|
|---|
| 642 |
|
|---|
| 643 |
|
|---|
| 644 |
@return |
|---|
| 645 |
|
|---|
| 646 |
public function delete() |
|---|
| 647 |
{ |
|---|
| 648 |
if ($this->deleted) { |
|---|
| 649 |
return; |
|---|
| 650 |
} |
|---|
| 651 |
|
|---|
| 652 |
if (!$this->getParent()->isWritable()) { |
|---|
| 653 |
throw new fEnvironmentException( |
|---|
| 654 |
'The file, %s, can not be deleted because the directory containing it is not writable', |
|---|
| 655 |
$this->file |
|---|
| 656 |
); |
|---|
| 657 |
} |
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
if (fFilesystem::isInsideTransaction()) { |
|---|
| 661 |
return fFilesystem::recordDelete($this); |
|---|
| 662 |
} |
|---|
| 663 |
|
|---|
| 664 |
unlink($this->file); |
|---|
| 665 |
|
|---|
| 666 |
fFilesystem::updateDeletedMap($this->file, debug_backtrace()); |
|---|
| 667 |
fFilesystem::updateFilenameMap($this->file, '*DELETED at ' . time() . ' with token ' . uniqid('', TRUE) . '* ' . $this->file); |
|---|
| 668 |
} |
|---|
| 669 |
|
|---|
| 670 |
|
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
|
|---|
| 676 |
|
|---|
| 677 |
|
|---|
| 678 |
|
|---|
| 679 |
|
|---|
| 680 |
|
|---|
| 681 |
|
|---|
| 682 |
@param |
|---|
| 683 |
@param |
|---|
| 684 |
@return |
|---|
| 685 |
|
|---|
| 686 |
public function duplicate($new_directory=NULL, $overwrite=NULL) |
|---|
| 687 |
{ |
|---|
| 688 |
$this->tossIfDeleted(); |
|---|
| 689 |
|
|---|
| 690 |
if ($new_directory === NULL) { |
|---|
| 691 |
$new_directory = $this->getParent(); |
|---|
| 692 |
} |
|---|
| 693 |
|
|---|
| 694 |
if (!is_object($new_directory)) { |
|---|
| 695 |
$new_directory = new fDirectory($new_directory); |
|---|
| 696 |
} |
|---|
| 697 |
|
|---|
| 698 |
$new_filename = $new_directory->getPath() . $this->getName(); |
|---|
| 699 |
|
|---|
| 700 |
$check_dir_permissions = FALSE; |
|---|
| 701 |
|
|---|
| 702 |
if (file_exists($new_filename)) { |
|---|
| 703 |
if (!$overwrite) { |
|---|
| 704 |
$new_filename = fFilesystem::makeUniqueName($new_filename); |
|---|
| 705 |
$check_dir_permissions = TRUE; |
|---|
| 706 |
|
|---|
| 707 |
} elseif (!is_writable($new_filename)) { |
|---|
| 708 |
throw new fEnvironmentException( |
|---|
| 709 |
'The new directory specified, %1$s, already contains a file with the name %2$s, but it is not writable', |
|---|
| 710 |
$new_directory->getPath(), |
|---|
| 711 |
$this->getName() |
|---|
| 712 |
); |
|---|
| 713 |
} |
|---|
| 714 |
|
|---|
| 715 |
} else { |
|---|
| 716 |
$check_dir_permissions = TRUE; |
|---|
| 717 |
} |
|---|
| 718 |
|
|---|
| 719 |
if ($check_dir_permissions) { |
|---|
| 720 |
if (!$new_directory->isWritable()) { |
|---|
| 721 |
throw new fEnvironmentException( |
|---|
| 722 |
'The new directory specified, %s, is not writable', |
|---|
| 723 |
$new_directory |
|---|
| 724 |
); |
|---|
| 725 |
} |
|---|
| 726 |
} |
|---|
| 727 |
|
|---|
| 728 |
copy($this->getPath(), $new_filename); |
|---|
| 729 |
chmod($new_filename, fileperms($this->getPath())); |
|---|
| 730 |
|
|---|
| 731 |
$class = get_class($this); |
|---|
| 732 |
$file = new $class($new_filename); |
|---|
| 733 |
|
|---|
| 734 |
|
|---|
| 735 |
if (fFilesystem::isInsideTransaction()) { |
|---|
| 736 |
fFilesystem::recordDuplicate($file); |
|---|
| 737 |
} |
|---|
| 738 |
|
|---|
| 739 |
return $file; |
|---|
| 740 |
} |
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
|
|---|
| 748 |
|
|---|
| 749 |
|
|---|
| 750 |
|
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
|
|---|
| 754 |
|
|---|
| 755 |
|
|---|
| 756 |
|
|---|
| 757 |
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
|
|---|
| 764 |
|
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
|
|---|
| 769 |
|
|---|
| 770 |
|
|---|
| 771 |
|
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
|
|---|
| 776 |
|
|---|
| 777 |
|
|---|
| 778 |
|
|---|
| 779 |
|
|---|
| 780 |
|
|---|
| 781 |
|
|---|
| 782 |
|
|---|
| 783 |
|
|---|
| 784 |
|
|---|
| 785 |
|
|---|
| 786 |
|
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
|
|---|
| 791 |
|
|---|
| 792 |
|
|---|
| 793 |
|
|---|
| 794 |
|
|---|
| 795 |
|
|---|
| 796 |
|
|---|
| 797 |
|
|---|
| 798 |
|
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
|
|---|
| 802 |
|
|---|
| 803 |
|
|---|
| 804 |
|
|---|
| 805 |
|
|---|
| 806 |
|
|---|
| 807 |
|
|---|
| 808 |
|
|---|
| 809 |
|
|---|
| 810 |
|
|---|
| 811 |
|
|---|
| 812 |
|
|---|
| 813 |
|
|---|
| 814 |
|
|---|
| 815 |
|
|---|
| 816 |
|
|---|
| 817 |
|
|---|
| 818 |
|
|---|
| 819 |
|
|---|
| 820 |
|
|---|
| 821 |
|
|---|
| 822 |
@return |
|---|
| 823 |
|
|---|
| 824 |
public function getMimeType() |
|---|
| 825 |
{ |
|---|
| 826 |
$this->tossIfDeleted(); |
|---|
| 827 |
|
|---|
| 828 |
return self::determineMimeType($this->file); |
|---|
| 829 |
} |
|---|
| 830 |
|
|---|
| 831 |
|
|---|
| 832 |
|
|---|
| 833 |
|
|---|
| 834 |
|
|---|
| 835 |
@return |
|---|
| 836 |
|
|---|
| 837 |
public function getMTime() |
|---|
| 838 |
{ |
|---|
| 839 |
$this->tossIfDeleted(); |
|---|
| 840 |
|
|---|
| 841 |
return new fTimestamp(filemtime($this->file)); |
|---|
| 842 |
} |
|---|
| 843 |
|
|---|
| 844 |
|
|---|
| 845 |
|
|---|
| 846 |
|
|---|
| 847 |
|
|---|
| 848 |
@return |
|---|
| 849 |
|
|---|
| 850 |
public function getName() |
|---|
| 851 |
{ |
|---|
| 852 |
|
|---|
| 853 |
return fFilesystem::getPathInfo($this->file, 'basename'); |
|---|
| 854 |
} |
|---|
| 855 |
|
|---|
| 856 |
|
|---|
| 857 |
|
|---|
| 858 |
|
|---|
| 859 |
|
|---|
| 860 |
@return |
|---|
| 861 |
|
|---|
| 862 |
public function getParent() |
|---|
| 863 |
{ |
|---|
| 864 |
return new fDirectory(fFilesystem::getPathInfo($this->file, 'dirname')); |
|---|
| 865 |
} |
|---|
| 866 |
|
|---|
| 867 |
|
|---|
| 868 |
|
|---|
| 869 |
|
|---|
| 870 |
|
|---|
| 871 |
|
|---|
| 872 |
|
|---|
| 873 |
|
|---|
| 874 |
@param |
|---|
| 875 |
@return |
|---|
| 876 |
|
|---|
| 877 |
public function getPath($translate_to_web_path=FALSE) |
|---|
| 878 |
{ |
|---|
| 879 |
if ($translate_to_web_path) { |
|---|
| 880 |
return fFilesystem::translateToWebPath($this->file); |
|---|
| 881 |
} |
|---|
| 882 |
return $this->file; |
|---|
| 883 |
} |
|---|
| 884 |
|
|---|
| 885 |
|
|---|
| 886 |
|
|---|
| 887 |
|
|---|
| 888 |
|
|---|
| 889 |
|
|---|
| 890 |
|
|---|
| 891 |
@param |
|---|
| 892 |
@param |
|---|
| 893 |
@return |
|---|
| 894 |
|
|---|
| 895 |
public function getSize($format=FALSE, $decimal_places=1) |
|---|
| 896 |
{ |
|---|
| 897 |
$this->tossIfDeleted(); |
|---|
| 898 |
|
|---|
| 899 |
|
|---|
| 900 |
$size = sprintf("%u", filesize($this->file)); |
|---|
| 901 |
|
|---|
| 902 |
if (!$format) { |
|---|
| 903 |
return $size; |
|---|
| 904 |
} |
|---|
| 905 |
|
|---|
| 906 |
return fFilesystem::formatFilesize($size, $decimal_places); |
|---|
| 907 |
} |
|---|
| 908 |
|
|---|
| 909 |
|
|---|
| 910 |
|
|---|
| 911 |
|
|---|
| 912 |
|
|---|
| 913 |
@return |
|---|
| 914 |
|
|---|
| 915 |
public function isWritable() |
|---|
| 916 |
{ |
|---|
| 917 |
$this->tossIfDeleted(); |
|---|
| 918 |
|
|---|
| 919 |
return is_writable($this->file); |
|---|
| 920 |
} |
|---|
| 921 |
|
|---|
| 922 |
|
|---|
| 923 |
|
|---|
| 924 |
|
|---|
| 925 |
|
|---|
| 926 |
@throws |
|---|
| 927 |
@internal |
|---|
| 928 |
|
|---|
| 929 |
@return |
|---|
| 930 |
|
|---|
| 931 |
public function key() |
|---|
| 932 |
{ |
|---|
| 933 |
$this->tossIfDeleted(); |
|---|
| 934 |
|
|---|
| 935 |
if ($this->file_handle === NULL) { |
|---|
| 936 |
$this->next(); |
|---|
| 937 |
|
|---|
| 938 |
} elseif (!$this->valid()) { |
|---|
| 939 |
throw new fNoRemainingException('There are no remaining lines'); |
|---|
| 940 |
} |
|---|
| 941 |
|
|---|
| 942 |
return $this->current_line_number; |
|---|
| 943 |
} |
|---|
| 944 |
|
|---|
| 945 |
|
|---|
| 946 |
|
|---|
| 947 |
|
|---|
| 948 |
|
|---|
| 949 |
|
|---|
| 950 |
|
|---|
| 951 |
|
|---|
| 952 |
|
|---|
| 953 |
|
|---|
| 954 |
|
|---|
| 955 |
|
|---|
| 956 |
|
|---|
| 957 |
|
|---|
| 958 |
|
|---|
| 959 |
@throws |
|---|
| 960 |
|
|---|
| 961 |
@param |
|---|
| 962 |
@param |
|---|
| 963 |
@return |
|---|
| 964 |
|
|---|
| 965 |
public function move($new_directory, $overwrite) |
|---|
| 966 |
{ |
|---|
| 967 |
if (!$new_directory instanceof fDirectory) { |
|---|
| 968 |
$new_directory = new fDirectory($new_directory); |
|---|
| 969 |
} |
|---|
| 970 |
|
|---|
| 971 |
return $this->rename($new_directory->getPath() . $this->getName(), $overwrite); |
|---|
| 972 |
} |
|---|
| 973 |
|
|---|
| 974 |
|
|---|
| 975 |
|
|---|
| 976 |
|
|---|
| 977 |
|
|---|
| 978 |
@throws |
|---|
| 979 |
@internal |
|---|
| 980 |
|
|---|
| 981 |
@return |
|---|
| 982 |
|
|---|
| 983 |
public function next() |
|---|
| 984 |
{ |
|---|
| 985 |
$this->tossIfDeleted(); |
|---|
| 986 |
|
|---|
| 987 |
if ($this->file_handle === NULL) { |
|---|
| 988 |
$this->file_handle = fopen($this->file, 'r'); |
|---|
| 989 |
$this->current_line = ''; |
|---|
| 990 |
$this->current_line_number = 0; |
|---|
| 991 |
|
|---|
| 992 |
} elseif (!$this->valid()) { |
|---|
| 993 |
throw new fNoRemainingException('There are no remaining lines'); |
|---|
| 994 |
} |
|---|
| 995 |
|
|---|
| 996 |
$this->current_line = fgets($this->file_handle); |
|---|
| 997 |
$this->current_line_number++; |
|---|
| 998 |
} |
|---|
| 999 |
|
|---|
| 1000 |
|
|---|
| 1001 |
|
|---|
| 1002 |
|
|---|
| 1003 |
|
|---|
| 1004 |
|
|---|
| 1005 |
|
|---|
| 1006 |
|
|---|
| 1007 |
|
|---|
| 1008 |
|
|---|
| 1009 |
|
|---|
| 1010 |
@param |
|---|
| 1011 |
@param |
|---|
| 1012 |
@return |
|---|
| 1013 |
|
|---|
| 1014 |
public function output($headers, $filename=NULL) |
|---|
| 1015 |
{ |
|---|
| 1016 |
$this->tossIfDeleted(); |
|---|
| 1017 |
|
|---|
| 1018 |
if (ob_get_level() > 0) { |
|---|
| 1019 |
throw new fProgrammerException( |
|---|
| 1020 |
'The method requested, %1$s, can not be used when output buffering is turned on, due to potential memory issues. Please call %2$s, %3$s and %4$s, or %5$s as appropriate to turn off output buffering.', |
|---|
| 1021 |
'output()', |
|---|
| 1022 |
'ob_end_clean()', |
|---|
| 1023 |
'fBuffer::erase()', |
|---|
| 1024 |
'fBuffer::stop()', |
|---|
| 1025 |
'fTemplating::destroy()' |
|---|
| 1026 |
); |
|---|
| 1027 |
} |
|---|
| 1028 |
|
|---|
| 1029 |
if ($headers) { |
|---|
| 1030 |
if ($filename !== NULL) { |
|---|
| 1031 |
if ($filename === TRUE) { $filename = $this->getName(); } |
|---|
| 1032 |
header('Content-Disposition: attachment; filename="' . $filename . '"'); |
|---|
| 1033 |
} |
|---|
| 1034 |
header('Cache-Control: '); |
|---|
| 1035 |
header('Content-Length: ' . $this->getSize()); |
|---|
| 1036 |
header('Content-Type: ' . $this->getMimeType()); |
|---|
| 1037 |
header('Expires: '); |
|---|
| 1038 |
header('Last-Modified: ' . $this->getMTime()->format('D, d M Y H:i:s')); |
|---|
| 1039 |
header('Pragma: '); |
|---|
| 1040 |
} |
|---|
| 1041 |
|
|---|
| 1042 |
readfile($this->file); |
|---|
| 1043 |
|
|---|
| 1044 |
return $this; |
|---|
| 1045 |
} |
|---|
| 1046 |
|
|---|
| 1047 |
|
|---|
| 1048 |
|
|---|
| 1049 |
|
|---|
| 1050 |
|
|---|
| 1051 |
|
|---|
| 1052 |
|
|---|
| 1053 |
|
|---|
| 1054 |
|
|---|
| 1055 |
|
|---|
| 1056 |
@param |
|---|
| 1057 |
@return |
|---|
| 1058 |
|
|---|
| 1059 |
public function read() |
|---|
| 1060 |
{ |
|---|
| 1061 |
$this->tossIfDeleted(); |
|---|
| 1062 |
|
|---|
| 1063 |
return file_get_contents($this->file); |
|---|
| 1064 |
} |
|---|
| 1065 |
|
|---|
| 1066 |
|
|---|
| 1067 |
|
|---|
| 1068 |
|
|---|
| 1069 |
|
|---|
| 1070 |
|
|---|
| 1071 |
|
|---|
| 1072 |
|
|---|
| 1073 |
|
|---|
| 1074 |
|
|---|
| 1075 |
|
|---|
| 1076 |
@param |
|---|
| 1077 |
@param |
|---|
| 1078 |
@return |
|---|
| 1079 |
|
|---|
| 1080 |
public function rename($new_filename, $overwrite) |
|---|
| 1081 |
{ |
|---|
| 1082 |
$this->tossIfDeleted(); |
|---|
| 1083 |
|
|---|
| 1084 |
if (!$this->getParent()->isWritable()) { |
|---|
| 1085 |
throw new fEnvironmentException( |
|---|
| 1086 |
'The file, %s, can not be renamed because the directory containing it is not writable', |
|---|
| 1087 |
$this->file |
|---|
| 1088 |
); |
|---|
| 1089 |
} |
|---|
| 1090 |
|
|---|
| 1091 |
|
|---|
| 1092 |
if (preg_match('#^[^/\\\\]+$#D', $new_filename)) { |
|---|
| 1093 |
$new_filename = $this->getParent()->getPath() . $new_filename; |
|---|
| 1094 |
} |
|---|
| 1095 |
|
|---|
| 1096 |
$info = fFilesystem::getPathInfo($new_filename); |
|---|
| 1097 |
|
|---|
| 1098 |
if (!file_exists($info['dirname'])) { |
|---|
| 1099 |
throw new fProgrammerException( |
|---|
| 1100 |
'The new filename specified, %s, is inside of a directory that does not exist', |
|---|
| 1101 |
$new_filename |
|---|
| 1102 |
); |
|---|
| 1103 |
} |
|---|
| 1104 |
|
|---|
| 1105 |
|
|---|
| 1106 |
$new_filename = fDirectory::makeCanonical(realpath($info['dirname'])) . $info['basename']; |
|---|
| 1107 |
|
|---|
| 1108 |
if (file_exists($new_filename)) { |
|---|
| 1109 |
if (!is_writable($new_filename)) { |
|---|
| 1110 |
throw new fEnvironmentException( |
|---|
| 1111 |
'The new filename specified, %s, already exists, but is not writable', |
|---|
| 1112 |
$new_filename |
|---|
| 1113 |
); |
|---|
| 1114 |
} |
|---|
| 1115 |
|
|---|
| 1116 |
if (!$overwrite) { |
|---|
| 1117 |
$new_filename = fFilesystem::makeUniqueName($new_filename); |
|---|
| 1118 |
|
|---|
| 1119 |
} else { |
|---|
| 1120 |
if (fFilesystem::isInsideTransaction()) { |
|---|
| 1121 |
fFilesystem::recordWrite(new fFile($new_filename)); |
|---|
| 1122 |
} |
|---|
| 1123 |
|
|---|
| 1124 |
unlink($new_filename); |
|---|
| 1125 |
} |
|---|
| 1126 |
|
|---|
| 1127 |
} else { |
|---|
| 1128 |
$new_dir = new fDirectory($info['dirname']); |
|---|
| 1129 |
if (!$new_dir->isWritable()) { |
|---|
| 1130 |
throw new fEnvironmentException( |
|---|
| 1131 |
'The new filename specified, %s, is inside of a directory that is not writable', |
|---|
| 1132 |
$new_filename |
|---|
| 1133 |
); |
|---|
| 1134 |
} |
|---|
| 1135 |
} |
|---|
| 1136 |
|
|---|
| 1137 |
rename($this->file, $new_filename); |
|---|
| 1138 |
|
|---|
| 1139 |
|
|---|
| 1140 |
if (fFilesystem::isInsideTransaction()) { |
|---|
| 1141 |
fFilesystem::recordRename($this->file, $new_filename); |
|---|
| 1142 |
} |
|---|
| 1143 |
|
|---|
| 1144 |
fFilesystem::updateFilenameMap($this->file, $new_filename); |
|---|
| 1145 |
|
|---|
| 1146 |
return $this; |
|---|
| 1147 |
} |
|---|
| 1148 |
|
|---|
| 1149 |
|
|---|
| 1150 |
|
|---|
| 1151 |
|
|---|
| 1152 |
|
|---|
| 1153 |
@internal |
|---|
| 1154 |
|
|---|
| 1155 |
@return |
|---|
| 1156 |
|
|---|
| 1157 |
public function rewind() |
|---|
| 1158 |
{ |
|---|
| 1159 |
$this->tossIfDeleted(); |
|---|
| 1160 |
|
|---|
| 1161 |
if ($this->file_handle !== NULL) { |
|---|
| 1162 |
rewind($this->file_handle); |
|---|
| 1163 |
} |
|---|
| 1164 |
} |
|---|
| 1165 |
|
|---|
| 1166 |
|
|---|
| 1167 |
|
|---|
| 1168 |
|
|---|
| 1169 |
|
|---|
| 1170 |
@return |
|---|
| 1171 |
|
|---|
| 1172 |
protected function tossIfDeleted() |
|---|
| 1173 |
{ |
|---|
| 1174 |
if ($this->deleted) { |
|---|
| 1175 |
throw new fProgrammerException( |
|---|
| 1176 |
"The action requested can not be performed because the file has been deleted\n\nBacktrace for fFile::delete() call:\n%s", |
|---|
| 1177 |
fCore::backtrace(0, $this->deleted) |
|---|
| 1178 |
); |
|---|
| 1179 |
} |
|---|
| 1180 |
} |
|---|
| 1181 |
|
|---|
| 1182 |
|
|---|
| 1183 |
|
|---|
| 1184 |
|
|---|
| 1185 |
|
|---|
| 1186 |
@internal |
|---|
| 1187 |
|
|---|
| 1188 |
@return |
|---|
| 1189 |
|
|---|
| 1190 |
public function valid() |
|---|
| 1191 |
{ |
|---|
| 1192 |
$this->tossIfDeleted(); |
|---|
| 1193 |
|
|---|
| 1194 |
if ($this->file_handle === NULL) { |
|---|
| 1195 |
return TRUE; |
|---|
| 1196 |
} |
|---|
| 1197 |
|
|---|
| 1198 |
return $this->current_line !== FALSE; |
|---|
| 1199 |
} |
|---|
| 1200 |
|
|---|
| 1201 |
|
|---|
| 1202 |
|
|---|
| 1203 |
|
|---|
| 1204 |
|
|---|
| 1205 |
|
|---|
| 1206 |
|
|---|
| 1207 |
|
|---|
| 1208 |
|
|---|
| 1209 |
|
|---|
| 1210 |
|
|---|
| 1211 |
@param |
|---|
| 1212 |
@return |
|---|
| 1213 |
|
|---|
| 1214 |
public function write($data) |
|---|
| 1215 |
{ |
|---|
| 1216 |
$this->tossIfDeleted(); |
|---|
| 1217 |
|
|---|
| 1218 |
if (!$this->isWritable()) { |
|---|
| 1219 |
throw new fEnvironmentException( |
|---|
| 1220 |
'This file, %s, can not be written to because it is not writable', |
|---|
| 1221 |
$this->file |
|---|
| 1222 |
); |
|---|
| 1223 |
} |
|---|
| 1224 |
|
|---|
| 1225 |
|
|---|
| 1226 |
if (fFilesystem::isInsideTransaction()) { |
|---|
| 1227 |
fFilesystem::recordWrite($this); |
|---|
| 1228 |
} |
|---|
| 1229 |
|
|---|
| 1230 |
file_put_contents($this->file, $data); |
|---|
| 1231 |
|
|---|
| 1232 |
return $this; |
|---|
| 1233 |
} |
|---|
| 1234 |
} |
|---|
| 1235 |
|
|---|
| 1236 |
|
|---|
| 1237 |
|
|---|
| 1238 |
|
|---|
| 1239 |
will@flourishlib.com |
|---|
| 1240 |
|
|---|
| 1241 |
|
|---|
| 1242 |
|
|---|
| 1243 |
|
|---|
| 1244 |
|
|---|
| 1245 |
|
|---|
| 1246 |
|
|---|
| 1247 |
|
|---|
| 1248 |
|
|---|
| 1249 |
|
|---|
| 1250 |
|
|---|
| 1251 |
|
|---|
| 1252 |
|
|---|
| 1253 |
|
|---|
| 1254 |
|
|---|
| 1255 |
|
|---|
| 1256 |
|
|---|
| 1257 |
|
|---|
| 1258 |
|
|---|