00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 if (!class_exists('Object')) {
00030 uses('object');
00031 }
00032 if (!class_exists('Folder')) {
00033 require LIBS . 'folder.php';
00034 }
00035
00036
00037
00038
00039
00040
00041 class File extends Object {
00042
00043
00044
00045
00046
00047
00048 var $Folder = null;
00049
00050
00051
00052
00053
00054
00055 var $name = null;
00056
00057
00058
00059
00060
00061
00062 var $info = array();
00063
00064
00065
00066
00067
00068
00069 var $handle = null;
00070
00071
00072
00073
00074
00075
00076 var $lock = null;
00077
00078
00079
00080
00081
00082
00083
00084
00085 var $path = null;
00086
00087
00088
00089
00090
00091
00092
00093
00094 function __construct($path, $create = false, $mode = 0755) {
00095 parent::__construct();
00096 $this->Folder =& new Folder(dirname($path), $create, $mode);
00097 if (!is_dir($path)) {
00098 $this->name = basename($path);
00099 }
00100 $this->pwd();
00101
00102 if (!$this->exists()) {
00103 if ($create === true) {
00104 if ($this->safe($path) && $this->create() === false) {
00105 return false;
00106 }
00107 } else {
00108 return false;
00109 }
00110 }
00111 }
00112
00113
00114
00115
00116
00117 function __destruct() {
00118 $this->close();
00119 }
00120
00121
00122
00123
00124
00125
00126 function create() {
00127 $dir = $this->Folder->pwd();
00128 if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
00129 $old = umask(0);
00130 if (touch($this->path)) {
00131 umask($old);
00132 return true;
00133 }
00134 }
00135 return false;
00136 }
00137
00138
00139
00140
00141
00142
00143
00144
00145 function open($mode = 'r', $force = false) {
00146 if (!$force && is_resource($this->handle)) {
00147 return true;
00148 }
00149 clearstatcache();
00150 if ($this->exists() === false) {
00151 if ($this->create() === false) {
00152 return false;
00153 }
00154 }
00155
00156 $this->handle = fopen($this->path, $mode);
00157 if (is_resource($this->handle)) {
00158 return true;
00159 }
00160 return false;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 function read($bytes = false, $mode = 'rb', $force = false) {
00172 if ($bytes === false && $this->lock === null) {
00173 return file_get_contents($this->path);
00174 }
00175 if ($this->open($mode, $force) === false) {
00176 return false;
00177 }
00178 if ($this->lock !== null && flock($this->handle, LOCK_SH) === false) {
00179 return false;
00180 }
00181 if (is_int($bytes)) {
00182 return fread($this->handle, $bytes);
00183 }
00184
00185 $data = '';
00186 while (!feof($this->handle)) {
00187 $data .= fgets($this->handle, 4096);
00188 }
00189 $data = trim($data);
00190
00191 if ($this->lock !== null) {
00192 flock($this->handle, LOCK_UN);
00193 }
00194 if ($bytes === false) {
00195 $this->close();
00196 }
00197 return $data;
00198 }
00199
00200
00201
00202
00203
00204
00205
00206
00207 function offset($offset = false, $seek = SEEK_SET) {
00208 if ($offset === false) {
00209 if (is_resource($this->handle)) {
00210 return ftell($this->handle);
00211 }
00212 } elseif ($this->open() === true) {
00213 return fseek($this->handle, $offset, $seek) === 0;
00214 }
00215 return false;
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225 function prepare($data, $forceWindows = false) {
00226 $lineBreak = "\n";
00227 if (DIRECTORY_SEPARATOR == '\\' || $forceWindows === true) {
00228 $lineBreak = "\r\n";
00229 }
00230 return strtr($data, array("\r\n" => $lineBreak, "\n" => $lineBreak, "\r" => $lineBreak));
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 function write($data, $mode = 'w', $force = false) {
00243 $success = false;
00244 if ($this->open($mode, $force) === true) {
00245 if ($this->lock !== null) {
00246 if (flock($this->handle, LOCK_EX) === false) {
00247 return false;
00248 }
00249 }
00250
00251 if (fwrite($this->handle, $data) !== false) {
00252 $success = true;
00253 }
00254 if ($this->lock !== null) {
00255 flock($this->handle, LOCK_UN);
00256 }
00257 }
00258 return $success;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268 function append($data, $force = false) {
00269 return $this->write($data, 'a', $force);
00270 }
00271
00272
00273
00274
00275
00276
00277 function close() {
00278 if (!is_resource($this->handle)) {
00279 return true;
00280 }
00281 return fclose($this->handle);
00282 }
00283
00284
00285
00286
00287
00288
00289 function delete() {
00290 clearstatcache();
00291 if ($this->exists()) {
00292 return unlink($this->path);
00293 }
00294 return false;
00295 }
00296
00297
00298
00299
00300
00301
00302 function info() {
00303 if ($this->info == null) {
00304 $this->info = pathinfo($this->path);
00305 }
00306 if (!isset($this->info['filename'])) {
00307 $this->info['filename'] = $this->name();
00308 }
00309 return $this->info;
00310 }
00311
00312
00313
00314
00315
00316
00317 function ext() {
00318 if ($this->info == null) {
00319 $this->info();
00320 }
00321 if (isset($this->info['extension'])) {
00322 return $this->info['extension'];
00323 }
00324 return false;
00325 }
00326
00327
00328
00329
00330
00331
00332 function name() {
00333 if ($this->info == null) {
00334 $this->info();
00335 }
00336 if (isset($this->info['extension'])) {
00337 return basename($this->name, '.'.$this->info['extension']);
00338 } elseif ($this->name) {
00339 return $this->name;
00340 }
00341 return false;
00342 }
00343
00344
00345
00346
00347
00348
00349
00350 function safe($name = null, $ext = null) {
00351 if (!$name) {
00352 $name = $this->name;
00353 }
00354 if (!$ext) {
00355 $ext = $this->ext();
00356 }
00357 return preg_replace( "/(?:[^\w\.-]+)/", "_", basename($name, $ext));
00358 }
00359
00360
00361
00362
00363
00364
00365
00366 function md5($maxsize = 5) {
00367 if ($maxsize === true) {
00368 return md5_file($this->path);
00369 }
00370
00371 $size = $this->size();
00372 if ($size && $size < ($maxsize * 1024) * 1024) {
00373 return md5_file($this->path);
00374 }
00375
00376 return false;
00377 }
00378
00379
00380
00381
00382
00383
00384 function pwd() {
00385 if (is_null($this->path)) {
00386 $this->path = $this->Folder->slashTerm($this->Folder->pwd()) . $this->name;
00387 }
00388 return $this->path;
00389 }
00390
00391
00392
00393
00394
00395
00396 function exists() {
00397 return (file_exists($this->path) && is_file($this->path));
00398 }
00399
00400
00401
00402
00403
00404
00405 function perms() {
00406 if ($this->exists()) {
00407 return substr(sprintf('%o', fileperms($this->path)), -4);
00408 }
00409 return false;
00410 }
00411
00412
00413
00414
00415
00416
00417 function size() {
00418 if ($this->exists()) {
00419 return filesize($this->path);
00420 }
00421 return false;
00422 }
00423
00424
00425
00426
00427
00428
00429 function writable() {
00430 return is_writable($this->path);
00431 }
00432
00433
00434
00435
00436
00437
00438 function executable() {
00439 return is_executable($this->path);
00440 }
00441
00442
00443
00444
00445
00446
00447 function readable() {
00448 return is_readable($this->path);
00449 }
00450
00451
00452
00453
00454
00455
00456 function owner() {
00457 if ($this->exists()) {
00458 return fileowner($this->path);
00459 }
00460 return false;
00461 }
00462
00463
00464
00465
00466
00467
00468 function group() {
00469 if ($this->exists()) {
00470 return filegroup($this->path);
00471 }
00472 return false;
00473 }
00474
00475
00476
00477
00478
00479
00480 function lastAccess() {
00481 if ($this->exists()) {
00482 return fileatime($this->path);
00483 }
00484 return false;
00485 }
00486
00487
00488
00489
00490
00491
00492 function lastChange() {
00493 if ($this->exists()) {
00494 return filemtime($this->path);
00495 }
00496 return false;
00497 }
00498
00499
00500
00501
00502
00503
00504 function &Folder() {
00505 return $this->Folder;
00506 }
00507 }
00508 ?>