| 1: | <?php
|
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: |
|
| 19: | defined('XOOPS_ROOT_PATH') || exit('Restricted access');
|
| 20: |
|
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: |
|
| 44: |
|
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: |
|
| 51: | class XoopsFileHandler
|
| 52: | {
|
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: |
|
| 59: | public $folder;
|
| 60: |
|
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: |
|
| 67: | public $name;
|
| 68: |
|
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: |
|
| 75: | public $info = array();
|
| 76: |
|
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: |
|
| 83: | public $handle;
|
| 84: |
|
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: |
|
| 91: | public $lock;
|
| 92: |
|
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: |
|
| 101: | public function __construct($path, $create = false, $mode = 0755)
|
| 102: | {
|
| 103: | XoopsLoad::load('XoopsFile');
|
| 104: | $this->folder = XoopsFile::getHandler('folder', dirname($path), $create, $mode);
|
| 105: | if (!is_dir($path)) {
|
| 106: | $this->name = basename($path);
|
| 107: | }
|
| 108: | if (!$this->exists()) {
|
| 109: | if ($create === true) {
|
| 110: | if ($this->safe($path) && $this->create() === false) {
|
| 111: | return false;
|
| 112: | }
|
| 113: | } else {
|
| 114: | return false;
|
| 115: | }
|
| 116: | }
|
| 117: | return null;
|
| 118: | }
|
| 119: |
|
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: |
|
| 125: | public function __destruct()
|
| 126: | {
|
| 127: | $this->close();
|
| 128: | }
|
| 129: |
|
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: |
|
| 136: | public function create()
|
| 137: | {
|
| 138: | $dir = $this->folder->pwd();
|
| 139: | if (is_dir($dir) && is_writable($dir) && !$this->exists()) {
|
| 140: | if (touch($this->pwd())) {
|
| 141: | return true;
|
| 142: | }
|
| 143: | }
|
| 144: |
|
| 145: | return false;
|
| 146: | }
|
| 147: |
|
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: |
|
| 156: | public function open($mode = 'r', $force = false)
|
| 157: | {
|
| 158: | if (!$force && is_resource($this->handle)) {
|
| 159: | return true;
|
| 160: | }
|
| 161: | if ($this->exists() === false) {
|
| 162: | if ($this->create() === false) {
|
| 163: | return false;
|
| 164: | }
|
| 165: | }
|
| 166: | $this->handle = fopen($this->pwd(), $mode);
|
| 167: | if (is_resource($this->handle)) {
|
| 168: | return true;
|
| 169: | }
|
| 170: |
|
| 171: | return false;
|
| 172: | }
|
| 173: |
|
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: |
|
| 184: | public function read($bytes = false, $mode = 'rb', $force = false)
|
| 185: | {
|
| 186: | $success = false;
|
| 187: | if ($this->lock !== null) {
|
| 188: | if (flock($this->handle, LOCK_SH) === false) {
|
| 189: | return false;
|
| 190: | }
|
| 191: | }
|
| 192: | if ($bytes === false) {
|
| 193: | $success = file_get_contents($this->pwd());
|
| 194: | } elseif ($this->open($mode, $force) === true) {
|
| 195: | if (is_int($bytes)) {
|
| 196: | $success = fread($this->handle, $bytes);
|
| 197: | } else {
|
| 198: | $data = '';
|
| 199: | while (!feof($this->handle)) {
|
| 200: | $data .= fgets($this->handle, 4096);
|
| 201: | }
|
| 202: | $success = trim($data);
|
| 203: | }
|
| 204: | }
|
| 205: | if ($this->lock !== null) {
|
| 206: | flock($this->handle, LOCK_UN);
|
| 207: | }
|
| 208: |
|
| 209: | return $success;
|
| 210: | }
|
| 211: |
|
| 212: | |
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: |
|
| 220: | public function offset($offset = false, $seek = SEEK_SET)
|
| 221: | {
|
| 222: | if ($offset === false) {
|
| 223: | if (is_resource($this->handle)) {
|
| 224: | return ftell($this->handle);
|
| 225: | }
|
| 226: | } elseif ($this->open() === true) {
|
| 227: | return fseek($this->handle, $offset, $seek) === 0;
|
| 228: | }
|
| 229: |
|
| 230: | return false;
|
| 231: | }
|
| 232: |
|
| 233: | |
| 234: | |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: |
|
| 241: | public function prepare($data)
|
| 242: | {
|
| 243: | $lineBreak = "\n";
|
| 244: | if (substr(PHP_OS, 0, 3) === 'WIN') {
|
| 245: | $lineBreak = "\r\n";
|
| 246: | }
|
| 247: |
|
| 248: | return strtr($data, array(
|
| 249: | "\r\n" => $lineBreak,
|
| 250: | "\n" => $lineBreak,
|
| 251: | "\r" => $lineBreak));
|
| 252: | }
|
| 253: |
|
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | |
| 261: | |
| 262: |
|
| 263: | public function write($data, $mode = 'w', $force = false)
|
| 264: | {
|
| 265: | $success = false;
|
| 266: | if ($this->open($mode, $force) === true) {
|
| 267: | if ($this->lock !== null) {
|
| 268: | if (flock($this->handle, LOCK_EX) === false) {
|
| 269: | return false;
|
| 270: | }
|
| 271: | }
|
| 272: | if (fwrite($this->handle, $data) !== false) {
|
| 273: | $success = true;
|
| 274: | }
|
| 275: | if ($this->lock !== null) {
|
| 276: | flock($this->handle, LOCK_UN);
|
| 277: | }
|
| 278: | }
|
| 279: |
|
| 280: | return $success;
|
| 281: | }
|
| 282: |
|
| 283: | |
| 284: | |
| 285: | |
| 286: | |
| 287: | |
| 288: | |
| 289: | |
| 290: |
|
| 291: | public function append($data, $force = false)
|
| 292: | {
|
| 293: | return $this->write($data, 'a', $force);
|
| 294: | }
|
| 295: |
|
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: |
|
| 302: | public function close()
|
| 303: | {
|
| 304: | if (!is_resource($this->handle)) {
|
| 305: | return true;
|
| 306: | }
|
| 307: |
|
| 308: | return fclose($this->handle);
|
| 309: | }
|
| 310: |
|
| 311: | |
| 312: | |
| 313: | |
| 314: | |
| 315: | |
| 316: |
|
| 317: | public function delete()
|
| 318: | {
|
| 319: | if ($this->exists()) {
|
| 320: | return unlink($this->pwd());
|
| 321: | }
|
| 322: |
|
| 323: | return false;
|
| 324: | }
|
| 325: |
|
| 326: | |
| 327: | |
| 328: | |
| 329: | |
| 330: | |
| 331: |
|
| 332: | public function info()
|
| 333: | {
|
| 334: | if ($this->info == null) {
|
| 335: | $this->info = pathinfo($this->pwd());
|
| 336: | }
|
| 337: | if (!isset($this->info['filename'])) {
|
| 338: | $this->info['filename'] = $this->name();
|
| 339: | }
|
| 340: |
|
| 341: | return $this->info;
|
| 342: | }
|
| 343: |
|
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: |
|
| 350: | public function ext()
|
| 351: | {
|
| 352: | if ($this->info == null) {
|
| 353: | $this->info();
|
| 354: | }
|
| 355: | if (isset($this->info['extension'])) {
|
| 356: | return $this->info['extension'];
|
| 357: | }
|
| 358: |
|
| 359: | return false;
|
| 360: | }
|
| 361: |
|
| 362: | |
| 363: | |
| 364: | |
| 365: | |
| 366: | |
| 367: |
|
| 368: | public function name()
|
| 369: | {
|
| 370: | if ($this->info == null) {
|
| 371: | $this->info();
|
| 372: | }
|
| 373: | if (isset($this->info['extension'])) {
|
| 374: | return basename($this->name, '.' . $this->info['extension']);
|
| 375: | } elseif ($this->name) {
|
| 376: | return $this->name;
|
| 377: | }
|
| 378: |
|
| 379: | return false;
|
| 380: | }
|
| 381: |
|
| 382: | |
| 383: | |
| 384: | |
| 385: | |
| 386: | |
| 387: | |
| 388: | |
| 389: |
|
| 390: | public function safe($name = null, $ext = null)
|
| 391: | {
|
| 392: | if (!$name) {
|
| 393: | $name = $this->name;
|
| 394: | }
|
| 395: | if (!$ext) {
|
| 396: | $ext = $this->ext();
|
| 397: | }
|
| 398: |
|
| 399: | return preg_replace('/[^\w\.-]+/', '_', basename($name, $ext));
|
| 400: | }
|
| 401: |
|
| 402: | |
| 403: | |
| 404: | |
| 405: | |
| 406: | |
| 407: | |
| 408: |
|
| 409: | public function md5($maxsize = 5)
|
| 410: | {
|
| 411: | if ($maxsize === true) {
|
| 412: | return md5_file($this->pwd());
|
| 413: | } else {
|
| 414: | $size = $this->size();
|
| 415: | if ($size && $size < ($maxsize * 1024) * 1024) {
|
| 416: | return md5_file($this->pwd());
|
| 417: | }
|
| 418: | }
|
| 419: |
|
| 420: | return false;
|
| 421: | }
|
| 422: |
|
| 423: | |
| 424: | |
| 425: | |
| 426: | |
| 427: | |
| 428: |
|
| 429: | public function pwd()
|
| 430: | {
|
| 431: | return $this->folder->slashTerm($this->folder->pwd()) . $this->name;
|
| 432: | }
|
| 433: |
|
| 434: | |
| 435: | |
| 436: | |
| 437: | |
| 438: | |
| 439: |
|
| 440: | public function exists()
|
| 441: | {
|
| 442: | $exists = (file_exists($this->pwd()) && is_file($this->pwd()));
|
| 443: |
|
| 444: | return $exists;
|
| 445: | }
|
| 446: |
|
| 447: | |
| 448: | |
| 449: | |
| 450: | |
| 451: | |
| 452: |
|
| 453: | public function perms()
|
| 454: | {
|
| 455: | if ($this->exists()) {
|
| 456: | return substr(sprintf('%o', fileperms($this->pwd())), -4);
|
| 457: | }
|
| 458: |
|
| 459: | return false;
|
| 460: | }
|
| 461: |
|
| 462: | |
| 463: | |
| 464: | |
| 465: | |
| 466: | |
| 467: |
|
| 468: | public function size()
|
| 469: | {
|
| 470: | if ($this->exists()) {
|
| 471: | return filesize($this->pwd());
|
| 472: | }
|
| 473: |
|
| 474: | return false;
|
| 475: | }
|
| 476: |
|
| 477: | |
| 478: | |
| 479: | |
| 480: | |
| 481: | |
| 482: |
|
| 483: | public function writable()
|
| 484: | {
|
| 485: | return is_writable($this->pwd());
|
| 486: | }
|
| 487: |
|
| 488: | |
| 489: | |
| 490: | |
| 491: | |
| 492: | |
| 493: |
|
| 494: | public function executable()
|
| 495: | {
|
| 496: | return is_executable($this->pwd());
|
| 497: | }
|
| 498: |
|
| 499: | |
| 500: | |
| 501: | |
| 502: | |
| 503: | |
| 504: |
|
| 505: | public function readable()
|
| 506: | {
|
| 507: | return is_readable($this->pwd());
|
| 508: | }
|
| 509: |
|
| 510: | |
| 511: | |
| 512: | |
| 513: | |
| 514: |
|
| 515: | public function owner()
|
| 516: | {
|
| 517: | if ($this->exists()) {
|
| 518: | return fileowner($this->pwd());
|
| 519: | }
|
| 520: |
|
| 521: | return false;
|
| 522: | }
|
| 523: |
|
| 524: | |
| 525: | |
| 526: | |
| 527: | |
| 528: | |
| 529: |
|
| 530: | public function group()
|
| 531: | {
|
| 532: | if ($this->exists()) {
|
| 533: | return filegroup($this->pwd());
|
| 534: | }
|
| 535: |
|
| 536: | return false;
|
| 537: | }
|
| 538: |
|
| 539: | |
| 540: | |
| 541: | |
| 542: | |
| 543: | |
| 544: |
|
| 545: | public function lastAccess()
|
| 546: | {
|
| 547: | if ($this->exists()) {
|
| 548: | return fileatime($this->pwd());
|
| 549: | }
|
| 550: |
|
| 551: | return false;
|
| 552: | }
|
| 553: |
|
| 554: | |
| 555: | |
| 556: | |
| 557: | |
| 558: | |
| 559: |
|
| 560: | public function lastChange()
|
| 561: | {
|
| 562: | if ($this->exists()) {
|
| 563: | return filemtime($this->pwd());
|
| 564: | }
|
| 565: |
|
| 566: | return false;
|
| 567: | }
|
| 568: |
|
| 569: | |
| 570: | |
| 571: | |
| 572: | |
| 573: | |
| 574: |
|
| 575: | public function &folder()
|
| 576: | {
|
| 577: | return $this->folder;
|
| 578: | }
|
| 579: | }
|
| 580: | |