Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like StreamWrapper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StreamWrapper, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class StreamWrapper implements StreamWrapperIface |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * Scheme / protocol used for this stream-wrapper |
||
| 31 | */ |
||
| 32 | const SCHEME = 'vfs'; |
||
| 33 | /** |
||
| 34 | * Mime type of directories, the old vfs used 'Directory', while eg. WebDAV uses 'httpd/unix-directory' |
||
| 35 | */ |
||
| 36 | const DIR_MIME_TYPE = 'httpd/unix-directory'; |
||
| 37 | /** |
||
| 38 | * Should unreadable entries in a not writable directory be hidden, default yes |
||
| 39 | */ |
||
| 40 | const HIDE_UNREADABLES = true; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * optional context param when opening the stream, null if no context passed |
||
| 44 | * |
||
| 45 | * @var mixed |
||
| 46 | */ |
||
| 47 | var $context; |
||
| 48 | /** |
||
| 49 | * mode-bits, which have to be set for links |
||
| 50 | */ |
||
| 51 | const MODE_LINK = 0120000; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * How much should be logged to the apache error-log |
||
| 55 | * |
||
| 56 | * 0 = Nothing |
||
| 57 | * 1 = only errors |
||
| 58 | * 2 = all function calls and errors (contains passwords too!) |
||
| 59 | */ |
||
| 60 | const LOG_LEVEL = 1; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Maximum depth of symlinks, if exceeded url_stat will return false |
||
| 64 | * |
||
| 65 | * Used to prevent infinit recursion by circular links |
||
| 66 | */ |
||
| 67 | const MAX_SYMLINK_DEPTH = 10; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Our fstab in the form mount-point => url |
||
| 71 | * |
||
| 72 | * The entry for root has to be the first, or more general if you mount into subdirs the parent has to be before! |
||
| 73 | * |
||
| 74 | * @var array |
||
| 75 | */ |
||
| 76 | protected static $fstab = array( |
||
| 77 | '/' => 'sqlfs://$host/', |
||
| 78 | '/apps' => 'links://$host/apps', |
||
| 79 | ); |
||
| 80 | |||
| 81 | /** |
||
| 82 | * stream / ressouce this class is opened for by stream_open |
||
| 83 | * |
||
| 84 | * @var ressource |
||
| 85 | */ |
||
| 86 | private $opened_stream; |
||
| 87 | /** |
||
| 88 | * Mode of opened stream, eg. "r" or "w" |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | private $opened_stream_mode; |
||
| 93 | /** |
||
| 94 | * Path of opened stream |
||
| 95 | * |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | private $opened_stream_path; |
||
| 99 | /** |
||
| 100 | * URL of opened stream |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | private $opened_stream_url; |
||
| 105 | /** |
||
| 106 | * Opened stream is a new file, false for existing files |
||
| 107 | * |
||
| 108 | * @var boolean |
||
| 109 | */ |
||
| 110 | private $opened_stream_is_new; |
||
| 111 | /** |
||
| 112 | * directory-ressouce this class is opened for by dir_open |
||
| 113 | * |
||
| 114 | * @var ressource |
||
| 115 | */ |
||
| 116 | private $opened_dir; |
||
| 117 | /** |
||
| 118 | * URL of the opened dir, used to build the complete URL of files in the dir |
||
| 119 | * |
||
| 120 | * @var string |
||
| 121 | */ |
||
| 122 | private $opened_dir_url; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Options for the opened directory |
||
| 126 | * (backup, etc.) |
||
| 127 | */ |
||
| 128 | protected $dir_url_params = array(); |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Flag if opened dir is writable, in which case we return un-readable entries too |
||
| 132 | * |
||
| 133 | * @var boolean |
||
| 134 | */ |
||
| 135 | private $opened_dir_writable; |
||
| 136 | /** |
||
| 137 | * Extra dirs from our fstab in the current opened dir |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | private $extra_dirs; |
||
| 142 | /** |
||
| 143 | * Pointer in the extra dirs |
||
| 144 | * |
||
| 145 | * @var int |
||
| 146 | */ |
||
| 147 | private $extra_dir_ptr; |
||
| 148 | |||
| 149 | private static $wrappers; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Resolve the given path according to our fstab AND symlinks |
||
| 153 | * |
||
| 154 | * @param string $_path |
||
| 155 | * @param boolean $file_exists =true true if file needs to exists, false if not |
||
| 156 | * @param boolean $resolve_last_symlink =true |
||
| 157 | * @param array|boolean &$stat=null on return: stat of existing file or false for non-existing files |
||
| 158 | * @return string|boolean false if the url cant be resolved, should not happen if fstab has a root entry |
||
| 159 | */ |
||
| 160 | function resolve_url_symlinks($_path,$file_exists=true,$resolve_last_symlink=true,&$stat=null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Cache of already resolved urls |
||
| 185 | * |
||
| 186 | * @var array with path => target |
||
| 187 | */ |
||
| 188 | private static $resolve_url_cache = array(); |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Resolve the given path according to our fstab |
||
| 192 | * |
||
| 193 | * @param string $_path |
||
| 194 | * @param boolean $do_symlink =true is a direct match allowed, default yes (must be false for a lstat or readlink!) |
||
| 195 | * @param boolean $use_symlinkcache =true |
||
| 196 | * @param boolean $replace_user_pass_host =true replace $user,$pass,$host in url, default true, if false result is not cached |
||
| 197 | * @param boolean $fix_url_query =false true append relativ path to url query parameter, default not |
||
| 198 | * @return string|boolean false if the url cant be resolved, should not happen if fstab has a root entry |
||
| 199 | */ |
||
| 200 | static function resolve_url($_path,$do_symlink=true,$use_symlinkcache=true,$replace_user_pass_host=true,$fix_url_query=false) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Returns mount url of a full url returned by resolve_url |
||
| 277 | * |
||
| 278 | * @param string $fullurl full url returned by resolve_url |
||
| 279 | * @return string|NULL mount url or null if not found |
||
| 280 | */ |
||
| 281 | static function mount_url($fullurl) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * This method is called immediately after your stream object is created. |
||
| 296 | * |
||
| 297 | * @param string $path URL that was passed to fopen() and that this object is expected to retrieve |
||
| 298 | * @param string $mode mode used to open the file, as detailed for fopen() |
||
| 299 | * @param int $options additional flags set by the streams API (or'ed together): |
||
| 300 | * - STREAM_USE_PATH If path is relative, search for the resource using the include_path. |
||
| 301 | * - STREAM_REPORT_ERRORS If this flag is set, you are responsible for raising errors using trigger_error() during opening of the stream. |
||
| 302 | * If this flag is not set, you should not raise any errors. |
||
| 303 | * @param string $opened_path full path of the file/resource, if the open was successfull and STREAM_USE_PATH was set |
||
| 304 | * @return boolean true if the ressource was opened successful, otherwise false |
||
| 305 | */ |
||
| 306 | function stream_open ( $path, $mode, $options, &$opened_path ) |
||
| 307 | { |
||
| 308 | unset($options,$opened_path); // not used but required by function signature |
||
| 309 | $this->opened_stream = null; |
||
| 310 | |||
| 311 | $stat = null; |
||
| 312 | if (!($url = $this->resolve_url_symlinks($path,$mode[0]=='r',true,$stat))) |
||
| 313 | { |
||
| 314 | return false; |
||
| 315 | } |
||
| 316 | if (str_replace('b', '', $mode) != 'r' && self::url_is_readonly($url)) |
||
| 317 | { |
||
| 318 | return false; |
||
| 319 | } |
||
| 320 | if (!($this->opened_stream = $this->context ? |
||
| 321 | fopen($url, $mode, false, $this->context) : fopen($url, $mode, false))) |
||
| 322 | { |
||
| 323 | return false; |
||
| 324 | } |
||
| 325 | $this->opened_stream_mode = $mode; |
||
| 326 | $this->opened_stream_path = $path[0] == '/' ? $path : Vfs::parse_url($path, PHP_URL_PATH); |
||
| 327 | $this->opened_stream_url = $url; |
||
| 328 | $this->opened_stream_is_new = !$stat; |
||
| 329 | |||
| 330 | // are we requested to treat the opened file as new file (only for files opened NOT for reading) |
||
| 331 | if ($mode[0] != 'r' && !$this->opened_stream_is_new && $this->context && |
||
| 332 | ($opts = stream_context_get_params($this->context)) && |
||
| 333 | $opts['options'][self::SCHEME]['treat_as_new']) |
||
| 334 | { |
||
| 335 | $this->opened_stream_is_new = true; |
||
| 336 | //error_log(__METHOD__."($path,$mode,...) stat=$stat, context=".array2string($opts)." --> ".array2string($this->opened_stream_is_new)); |
||
| 337 | } |
||
| 338 | return true; |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * This method is called when the stream is closed, using fclose(). |
||
| 343 | * |
||
| 344 | * You must release any resources that were locked or allocated by the stream. |
||
| 345 | * |
||
| 346 | * VFS calls either "vfs_read", "vfs_added" or "vfs_modified" hook |
||
| 347 | */ |
||
| 348 | function stream_close ( ) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * This method is called in response to fread() and fgets() calls on the stream. |
||
| 372 | * |
||
| 373 | * You must return up-to count bytes of data from the current read/write position as a string. |
||
| 374 | * If there are less than count bytes available, return as many as are available. |
||
| 375 | * If no more data is available, return either FALSE or an empty string. |
||
| 376 | * You must also update the read/write position of the stream by the number of bytes that were successfully read. |
||
| 377 | * |
||
| 378 | * @param int $count |
||
| 379 | * @return string/false up to count bytes read or false on EOF |
||
| 380 | */ |
||
| 381 | function stream_read ( $count ) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * This method is called in response to fwrite() calls on the stream. |
||
| 388 | * |
||
| 389 | * You should store data into the underlying storage used by your stream. |
||
| 390 | * If there is not enough room, try to store as many bytes as possible. |
||
| 391 | * You should return the number of bytes that were successfully stored in the stream, or 0 if none could be stored. |
||
| 392 | * You must also update the read/write position of the stream by the number of bytes that were successfully written. |
||
| 393 | * |
||
| 394 | * @param string $data |
||
| 395 | * @return integer |
||
| 396 | */ |
||
| 397 | function stream_write ( $data ) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * This method is called in response to feof() calls on the stream. |
||
| 404 | * |
||
| 405 | * Important: PHP 5.0 introduced a bug that wasn't fixed until 5.1: the return value has to be the oposite! |
||
| 406 | * |
||
| 407 | * if(version_compare(PHP_VERSION,'5.0','>=') && version_compare(PHP_VERSION,'5.1','<')) |
||
| 408 | * { |
||
| 409 | * $eof = !$eof; |
||
| 410 | * } |
||
| 411 | * |
||
| 412 | * @return boolean true if the read/write position is at the end of the stream and no more data availible, false otherwise |
||
| 413 | */ |
||
| 414 | function stream_eof ( ) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * This method is called in response to ftell() calls on the stream. |
||
| 421 | * |
||
| 422 | * @return integer current read/write position of the stream |
||
| 423 | */ |
||
| 424 | function stream_tell ( ) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * This method is called in response to fseek() calls on the stream. |
||
| 431 | * |
||
| 432 | * You should update the read/write position of the stream according to offset and whence. |
||
| 433 | * See fseek() for more information about these parameters. |
||
| 434 | * |
||
| 435 | * @param integer $offset |
||
| 436 | * @param integer $whence SEEK_SET - 0 - Set position equal to offset bytes |
||
| 437 | * SEEK_CUR - 1 - Set position to current location plus offset. |
||
| 438 | * SEEK_END - 2 - Set position to end-of-file plus offset. (To move to a position before the end-of-file, you need to pass a negative value in offset.) |
||
| 439 | * @return boolean TRUE if the position was updated, FALSE otherwise. |
||
| 440 | */ |
||
| 441 | function stream_seek ( $offset, $whence ) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * This method is called in response to fflush() calls on the stream. |
||
| 448 | * |
||
| 449 | * If you have cached data in your stream but not yet stored it into the underlying storage, you should do so now. |
||
| 450 | * |
||
| 451 | * @return booelan TRUE if the cached data was successfully stored (or if there was no data to store), or FALSE if the data could not be stored. |
||
| 452 | */ |
||
| 453 | function stream_flush ( ) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * This method is called in response to fstat() calls on the stream. |
||
| 460 | * |
||
| 461 | * If you plan to use your wrapper in a require_once you need to define stream_stat(). |
||
| 462 | * If you plan to allow any other tests like is_file()/is_dir(), you have to define url_stat(). |
||
| 463 | * stream_stat() must define the size of the file, or it will never be included. |
||
| 464 | * url_stat() must define mode, or is_file()/is_dir()/is_executable(), and any of those functions affected by clearstatcache() simply won't work. |
||
| 465 | * It's not documented, but directories must be a mode like 040777 (octal), and files a mode like 0100666. |
||
| 466 | * If you wish the file to be executable, use 7s instead of 6s. |
||
| 467 | * The last 3 digits are exactly the same thing as what you pass to chmod. |
||
| 468 | * 040000 defines a directory, and 0100000 defines a file. |
||
| 469 | * |
||
| 470 | * @return array containing the same values as appropriate for the stream. |
||
| 471 | */ |
||
| 472 | function stream_stat ( ) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * StreamWrapper method (PHP 5.4+) for touch, chmod, chown and chgrp |
||
| 479 | * |
||
| 480 | * @param string $path |
||
| 481 | * @param int $option STREAM_META_(TOUCH|ACCESS|((OWNER|GROUP)(_NAME)?)) |
||
| 482 | * @param array|int|string $value |
||
| 483 | * - STREAM_META_TOUCH array($time, $atime) |
||
| 484 | * - STREAM_META_ACCESS int |
||
| 485 | * - STREAM_(OWNER|GROUP) int |
||
| 486 | * - STREAM_(OWNER|GROUP)_NAME string |
||
| 487 | * @return boolean true on success, false on failure |
||
| 488 | */ |
||
| 489 | function stream_metadata($path, $option, $value) |
||
| 490 | { |
||
| 491 | if (!($url = $this->resolve_url_symlinks($path, $option != STREAM_META_TOUCH, false))) // true,false file need to exist, but do not resolve last component |
||
| 492 | { |
||
| 493 | return false; |
||
| 494 | } |
||
| 495 | if (self::url_is_readonly($url)) |
||
| 496 | { |
||
| 497 | return false; |
||
| 498 | } |
||
| 499 | View Code Duplication | if (self::LOG_LEVEL > 1) error_log(__METHOD__."('$path', $option, ".array2string($value).") url=$url"); |
|
| 500 | |||
| 501 | switch($option) |
||
| 502 | { |
||
| 503 | case STREAM_META_TOUCH: |
||
| 504 | return touch($url, $value[0]); // atime is not supported |
||
| 505 | |||
| 506 | case STREAM_META_ACCESS: |
||
| 507 | return chmod($url, $value); |
||
| 508 | |||
| 509 | View Code Duplication | case STREAM_META_OWNER_NAME: |
|
| 510 | if (($value = $GLOBALS['egw']->accounts->name2id($value, 'account_lid', 'u')) === false) |
||
| 511 | return false; |
||
| 512 | // fall through |
||
| 513 | case STREAM_META_OWNER: |
||
| 514 | return chown($url, $value); |
||
| 515 | |||
| 516 | View Code Duplication | case STREAM_META_GROUP_NAME: |
|
| 517 | if (($value = $GLOBALS['egw']->accounts->name2id($value, 'account_lid', 'g')) === false) |
||
| 518 | return false; |
||
| 519 | // fall through |
||
| 520 | case STREAM_META_GROUP: |
||
| 521 | return chgrp($url, $value); |
||
| 522 | } |
||
| 523 | return false; |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * This method is called in response to unlink() calls on URL paths associated with the wrapper. |
||
| 528 | * |
||
| 529 | * It should attempt to delete the item specified by path. |
||
| 530 | * In order for the appropriate error message to be returned, do not define this method if your wrapper does not support unlinking! |
||
| 531 | * |
||
| 532 | * @param string $path |
||
| 533 | * @return boolean TRUE on success or FALSE on failure |
||
| 534 | */ |
||
| 535 | function unlink ( $path ) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * This method is called in response to rename() calls on URL paths associated with the wrapper. |
||
| 565 | * |
||
| 566 | * It should attempt to rename the item specified by path_from to the specification given by path_to. |
||
| 567 | * In order for the appropriate error message to be returned, do not define this method if your wrapper does not support renaming. |
||
| 568 | * |
||
| 569 | * The regular filesystem stream-wrapper returns an error, if $url_from and $url_to are not either both files or both dirs! |
||
| 570 | * |
||
| 571 | * @param string $path_from |
||
| 572 | * @param string $path_to |
||
| 573 | * @return boolean TRUE on success or FALSE on failure |
||
| 574 | * @throws Exception\ProtectedDirectory if trying to delete a protected directory, see Vfs::isProtected() |
||
| 575 | */ |
||
| 576 | function rename ( $path_from, $path_to ) |
||
| 626 | |||
| 627 | /** |
||
| 628 | * This method is called in response to mkdir() calls on URL paths associated with the wrapper. |
||
| 629 | * |
||
| 630 | * Not all wrappers, eg. smb(client) support recursive directory creation. |
||
| 631 | * Therefore we handle that here instead of passing the options to underlaying wrapper. |
||
| 632 | * |
||
| 633 | * @param string $path |
||
| 634 | * @param int $mode |
||
| 635 | * @param int $options Posible values include STREAM_REPORT_ERRORS and STREAM_MKDIR_RECURSIVE |
||
| 636 | * @return boolean TRUE on success or FALSE on failure |
||
| 637 | */ |
||
| 638 | function mkdir ( $path, $mode, $options ) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * This method is called in response to rmdir() calls on URL paths associated with the wrapper. |
||
| 676 | * |
||
| 677 | * It should attempt to remove the directory specified by path. |
||
| 678 | * In order for the appropriate error message to be returned, do not define this method if your wrapper does not support removing directories. |
||
| 679 | * |
||
| 680 | * @param string $path |
||
| 681 | * @param int $options Possible values include STREAM_REPORT_ERRORS. |
||
| 682 | * @return boolean TRUE on success or FALSE on failure. |
||
| 683 | * @throws Exception\ProtectedDirectory if trying to delete a protected directory, see Vfs::isProtected() |
||
| 684 | */ |
||
| 685 | function rmdir ( $path, $options ) |
||
| 717 | |||
| 718 | /** |
||
| 719 | * This method is called immediately when your stream object is created for examining directory contents with opendir(). |
||
| 720 | * |
||
| 721 | * @param string $path URL that was passed to opendir() and that this object is expected to explore. |
||
| 722 | * @return booelan |
||
| 723 | */ |
||
| 724 | function dir_opendir ( $path, $options ) |
||
| 759 | |||
| 760 | /** |
||
| 761 | * This method is called in response to stat() calls on the URL paths associated with the wrapper. |
||
| 762 | * |
||
| 763 | * It should return as many elements in common with the system function as possible. |
||
| 764 | * Unknown or unavailable values should be set to a rational value (usually 0). |
||
| 765 | * |
||
| 766 | * If you plan to use your wrapper in a require_once you need to define stream_stat(). |
||
| 767 | * If you plan to allow any other tests like is_file()/is_dir(), you have to define url_stat(). |
||
| 768 | * stream_stat() must define the size of the file, or it will never be included. |
||
| 769 | * url_stat() must define mode, or is_file()/is_dir()/is_executable(), and any of those functions affected by clearstatcache() simply won't work. |
||
| 770 | * It's not documented, but directories must be a mode like 040777 (octal), and files a mode like 0100666. |
||
| 771 | * If you wish the file to be executable, use 7s instead of 6s. |
||
| 772 | * The last 3 digits are exactly the same thing as what you pass to chmod. |
||
| 773 | * 040000 defines a directory, and 0100000 defines a file. |
||
| 774 | * |
||
| 775 | * @param string $path |
||
| 776 | * @param int $flags holds additional flags set by the streams API. It can hold one or more of the following values OR'd together: |
||
| 777 | * - STREAM_URL_STAT_LINK For resources with the ability to link to other resource (such as an HTTP Location: forward, |
||
| 778 | * or a filesystem symlink). This flag specified that only information about the link itself should be returned, |
||
| 779 | * not the resource pointed to by the link. |
||
| 780 | * This flag is set in response to calls to lstat(), is_link(), or filetype(). |
||
| 781 | * - STREAM_URL_STAT_QUIET If this flag is set, your wrapper should not raise any errors. If this flag is not set, |
||
| 782 | * you are responsible for reporting errors using the trigger_error() function during stating of the path. |
||
| 783 | * stat triggers it's own warning anyway, so it makes no sense to trigger one by our stream-wrapper! |
||
| 784 | * @param boolean $try_create_home =false should a user home-directory be created automatic, if it does not exist |
||
| 785 | * @param boolean $check_symlink_components =true check if path contains symlinks in path components other then the last one |
||
| 786 | * @return array |
||
| 787 | */ |
||
| 788 | function url_stat ( $path, $flags, $try_create_home=false, $check_symlink_components=true, $check_symlink_depth=self::MAX_SYMLINK_DEPTH, $try_reconnect=true ) |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Check if path (which fails the stat call) contains symlinks in path-components other then the last one |
||
| 903 | * |
||
| 904 | * @param string $path |
||
| 905 | * @param int $flags =0 see url_stat |
||
| 906 | * @param string &$url=null already resolved path |
||
| 907 | * @return array|boolean stat array or false if not found |
||
| 908 | */ |
||
| 909 | private function check_symlink_components($path,$flags=0,&$url=null) |
||
| 944 | |||
| 945 | /** |
||
| 946 | * Cache of already resolved symlinks |
||
| 947 | * |
||
| 948 | * @var array with path => target |
||
| 949 | */ |
||
| 950 | private static $symlink_cache = array(); |
||
| 951 | |||
| 952 | /** |
||
| 953 | * Add a resolved symlink to cache |
||
| 954 | * |
||
| 955 | * @param string $_path vfs path |
||
| 956 | * @param string $target target path |
||
| 957 | */ |
||
| 958 | static protected function symlinkCache_add($_path,$target) |
||
| 975 | |||
| 976 | /** |
||
| 977 | * Remove a resolved symlink from cache |
||
| 978 | * |
||
| 979 | * @param string $_path vfs path |
||
| 980 | */ |
||
| 981 | static public function symlinkCache_remove($_path) |
||
| 988 | |||
| 989 | /** |
||
| 990 | * Resolve a path from our symlink cache |
||
| 991 | * |
||
| 992 | * The cache is sorted from longer to shorter pathes. |
||
| 993 | * |
||
| 994 | * @param string $_path |
||
| 995 | * @param boolean $do_symlink =true is a direct match allowed, default yes (must be false for a lstat or readlink!) |
||
| 996 | * @return string target or path, if path not found |
||
| 997 | */ |
||
| 998 | static public function symlinkCache_resolve($_path,$do_symlink=true) |
||
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Clears our internal stat and symlink cache |
||
| 1026 | * |
||
| 1027 | * Normaly not necessary, as it is automatically cleared/updated, UNLESS Vfs::$user changes! |
||
| 1028 | */ |
||
| 1029 | static function clearstatcache() |
||
| 1033 | |||
| 1034 | /** |
||
| 1035 | * This method is called in response to readdir(). |
||
| 1036 | * |
||
| 1037 | * It should return a string representing the next filename in the location opened by dir_opendir(). |
||
| 1038 | * |
||
| 1039 | * Unless other filesystem, we only return files readable by the user, if the dir is not writable for him. |
||
| 1040 | * This is done to hide files and dirs not accessible by the user (eg. other peoples home-dirs in /home). |
||
| 1041 | * |
||
| 1042 | * @return string |
||
| 1043 | */ |
||
| 1044 | function dir_readdir ( ) |
||
| 1064 | |||
| 1065 | /** |
||
| 1066 | * This method is called in response to rewinddir(). |
||
| 1067 | * |
||
| 1068 | * It should reset the output generated by dir_readdir(). i.e.: |
||
| 1069 | * The next call to dir_readdir() should return the first entry in the location returned by dir_opendir(). |
||
| 1070 | * |
||
| 1071 | * @return boolean |
||
| 1072 | */ |
||
| 1073 | function dir_rewinddir ( ) |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * This method is called in response to closedir(). |
||
| 1082 | * |
||
| 1083 | * You should release any resources which were locked or allocated during the opening and use of the directory stream. |
||
| 1084 | * |
||
| 1085 | * @return boolean |
||
| 1086 | */ |
||
| 1087 | function dir_closedir ( ) |
||
| 1095 | |||
| 1096 | /** |
||
| 1097 | * Load stream wrapper for a given schema |
||
| 1098 | * |
||
| 1099 | * @param string $scheme |
||
| 1100 | * @return boolean |
||
| 1101 | */ |
||
| 1102 | static function load_wrapper($scheme) |
||
| 1130 | |||
| 1131 | /** |
||
| 1132 | * Return already loaded stream wrappers |
||
| 1133 | * |
||
| 1134 | * @return array |
||
| 1135 | */ |
||
| 1136 | static function get_wrappers() |
||
| 1144 | |||
| 1145 | /** |
||
| 1146 | * Get the class-name for a scheme |
||
| 1147 | * |
||
| 1148 | * A scheme is not allowed to contain an underscore, but allows a dot and a class names only allow or need underscores, but no dots |
||
| 1149 | * --> we replace dots in scheme with underscored to get the class-name |
||
| 1150 | * |
||
| 1151 | * @param string $scheme eg. vfs |
||
| 1152 | * @return string |
||
| 1153 | */ |
||
| 1154 | static function scheme2class($scheme) |
||
| 1167 | |||
| 1168 | /** |
||
| 1169 | * Getting the path from an url (or path) AND removing trailing slashes |
||
| 1170 | * |
||
| 1171 | * @param string $path url or path (might contain trailing slash from WebDAV!) |
||
| 1172 | * @param string $only_remove_scheme =self::SCHEME if given only that scheme get's removed |
||
| 1173 | * @return string path without training slash |
||
| 1174 | */ |
||
| 1175 | static protected function get_path($path,$only_remove_scheme=self::SCHEME) |
||
| 1191 | |||
| 1192 | /** |
||
| 1193 | * Check if url contains ro=1 parameter to mark mount readonly |
||
| 1194 | * |
||
| 1195 | * @param string $url |
||
| 1196 | * @return boolean |
||
| 1197 | */ |
||
| 1198 | static function url_is_readonly($url) |
||
| 1209 | |||
| 1210 | /** |
||
| 1211 | * Mounts $url under $path in the vfs, called without parameter it returns the fstab |
||
| 1212 | * |
||
| 1213 | * The fstab is stored in the eGW configuration and used for all eGW users. |
||
| 1214 | * |
||
| 1215 | * @param string $url =null url of the filesystem to mount, eg. oldvfs://default/ |
||
| 1216 | * @param string $path =null path to mount the filesystem in the vfs, eg. / |
||
| 1217 | * @param boolean $check_url =null check if url is an existing directory, before mounting it |
||
| 1218 | * default null only checks if url does not contain a $ as used in $user or $pass |
||
| 1219 | * @param boolean $persitent_mount =true create a persitent mount, or only a temprary for current request |
||
| 1220 | * @param boolean $clear_fstab =false true clear current fstab, false (default) only add given mount |
||
| 1221 | * @return array|boolean array with fstab, if called without parameter or true on successful mount |
||
| 1222 | */ |
||
| 1223 | static function mount($url=null,$path=null,$check_url=null,$persitent_mount=true,$clear_fstab=false) |
||
| 1289 | |||
| 1290 | /** |
||
| 1291 | * Unmounts a filesystem part of the vfs |
||
| 1292 | * |
||
| 1293 | * @param string $path url or path of the filesystem to unmount |
||
| 1294 | */ |
||
| 1295 | static function umount($path) |
||
| 1296 | { |
||
| 1297 | View Code Duplication | if (!Vfs::$is_root) |
|
| 1298 | { |
||
| 1299 | if (self::LOG_LEVEL > 0) error_log(__METHOD__.'('.array2string($path).','.array2string($path).') permission denied, you are NOT root!'); |
||
| 1300 | return false; // only root can mount |
||
| 1301 | } |
||
| 1302 | if (!isset(self::$fstab[$path]) && ($path = array_search($path,self::$fstab)) === false) |
||
| 1303 | { |
||
| 1304 | if (self::LOG_LEVEL > 0) error_log(__METHOD__.'('.array2string($path).') NOT mounted!'); |
||
| 1305 | return false; // $path not mounted |
||
| 1306 | } |
||
| 1307 | unset(self::$fstab[$path]); |
||
| 1308 | |||
| 1309 | Api\Config::save_value('vfs_fstab',self::$fstab,'phpgwapi'); |
||
| 1310 | $GLOBALS['egw_info']['server']['vfs_fstab'] = self::$fstab; |
||
| 1311 | // invalidate session cache |
||
| 1312 | if (method_exists($GLOBALS['egw'],'invalidate_session_cache')) // egw object in setup is limited |
||
| 1313 | { |
||
| 1314 | $GLOBALS['egw']->invalidate_session_cache(); |
||
| 1315 | } |
||
| 1316 | if (self::LOG_LEVEL > 1) error_log(__METHOD__.'('.array2string($path).') returns true (successful unmount).'); |
||
| 1317 | return true; |
||
| 1318 | } |
||
| 1319 | |||
| 1320 | /** |
||
| 1321 | * Init our static properties and register this wrapper |
||
| 1322 | * |
||
| 1323 | */ |
||
| 1324 | static function init_static() |
||
| 1333 | } |
||
| 1334 | |||
| 1336 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.