wp/wp-admin/includes/class-wp-filesystem-direct.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * WordPress Filesystem Class for direct PHP file and folder manipulation.
    10  * WordPress Filesystem Class for direct PHP file and folder manipulation.
    11  *
    11  *
    12  * @since 2.5
    12  * @since 2.5.0
    13  * @package WordPress
    13  * @package WordPress
    14  * @subpackage Filesystem
    14  * @subpackage Filesystem
    15  * @uses WP_Filesystem_Base Extends class
    15  * @uses WP_Filesystem_Base Extends class
    16  */
    16  */
    17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
    17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
    19 	/**
    19 	/**
    20 	 * constructor
    20 	 * constructor
    21 	 *
    21 	 *
    22 	 * @param mixed $arg ignored argument
    22 	 * @param mixed $arg ignored argument
    23 	 */
    23 	 */
    24 	function __construct($arg) {
    24 	public function __construct($arg) {
    25 		$this->method = 'direct';
    25 		$this->method = 'direct';
    26 		$this->errors = new WP_Error();
    26 		$this->errors = new WP_Error();
    27 	}
    27 	}
    28 
    28 
    29 	/**
    29 	/**
    30 	 * Reads entire file into a string
    30 	 * Reads entire file into a string
    31 	 *
    31 	 *
    32 	 * @param string $file Name of the file to read.
    32 	 * @param string $file Name of the file to read.
    33 	 * @return string|bool The function returns the read data or false on failure.
    33 	 * @return string|bool The function returns the read data or false on failure.
    34 	 */
    34 	 */
    35 	function get_contents($file) {
    35 	public function get_contents($file) {
    36 		return @file_get_contents($file);
    36 		return @file_get_contents($file);
    37 	}
    37 	}
    38 
    38 
    39 	/**
    39 	/**
    40 	 * Reads entire file into an array
    40 	 * Reads entire file into an array
    41 	 *
    41 	 *
    42 	 * @param string $file Path to the file.
    42 	 * @param string $file Path to the file.
    43 	 * @return array|bool the file contents in an array or false on failure.
    43 	 * @return array|bool the file contents in an array or false on failure.
    44 	 */
    44 	 */
    45 	function get_contents_array($file) {
    45 	public function get_contents_array($file) {
    46 		return @file($file);
    46 		return @file($file);
    47 	}
    47 	}
    48 
    48 
    49 	/**
    49 	/**
    50 	 * Write a string to a file
    50 	 * Write a string to a file
    51 	 *
    51 	 *
    52 	 * @param string $file Remote path to the file where to write the data.
    52 	 * @param string $file     Remote path to the file where to write the data.
    53 	 * @param string $contents The data to write.
    53 	 * @param string $contents The data to write.
    54 	 * @param int $mode (optional) The file permissions as octal number, usually 0644.
    54 	 * @param int    $mode     Optional. The file permissions as octal number, usually 0644.
    55 	 * @return bool False upon failure.
    55 	 *                         Default false.
    56 	 */
    56 	 * @return bool False upon failure, true otherwise.
    57 	function put_contents( $file, $contents, $mode = false ) {
    57 	 */
       
    58 	public function put_contents( $file, $contents, $mode = false ) {
    58 		$fp = @fopen( $file, 'wb' );
    59 		$fp = @fopen( $file, 'wb' );
    59 		if ( ! $fp )
    60 		if ( ! $fp )
    60 			return false;
    61 			return false;
    61 
    62 
    62 		mbstring_binary_safe_encoding();
    63 		mbstring_binary_safe_encoding();
    80 	/**
    81 	/**
    81 	 * Gets the current working directory
    82 	 * Gets the current working directory
    82 	 *
    83 	 *
    83 	 * @return string|bool the current working directory on success, or false on failure.
    84 	 * @return string|bool the current working directory on success, or false on failure.
    84 	 */
    85 	 */
    85 	function cwd() {
    86 	public function cwd() {
    86 		return @getcwd();
    87 		return @getcwd();
    87 	}
    88 	}
    88 
    89 
    89 	/**
    90 	/**
    90 	 * Change directory
    91 	 * Change directory
    91 	 *
    92 	 *
    92 	 * @param string $dir The new current directory.
    93 	 * @param string $dir The new current directory.
    93 	 * @return bool Returns true on success or false on failure.
    94 	 * @return bool Returns true on success or false on failure.
    94 	 */
    95 	 */
    95 	function chdir($dir) {
    96 	public function chdir($dir) {
    96 		return @chdir($dir);
    97 		return @chdir($dir);
    97 	}
    98 	}
    98 
    99 
    99 	/**
   100 	/**
   100 	 * Changes file group
   101 	 * Changes file group
   101 	 *
   102 	 *
   102 	 * @param string $file Path to the file.
   103 	 * @param string $file      Path to the file.
   103 	 * @param mixed $group A group name or number.
   104 	 * @param mixed  $group     A group name or number.
   104 	 * @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
   105 	 * @param bool   $recursive Optional. If set True changes file group recursively. Default false.
   105 	 * @return bool Returns true on success or false on failure.
   106 	 * @return bool Returns true on success or false on failure.
   106 	 */
   107 	 */
   107 	function chgrp($file, $group, $recursive = false) {
   108 	public function chgrp($file, $group, $recursive = false) {
   108 		if ( ! $this->exists($file) )
   109 		if ( ! $this->exists($file) )
   109 			return false;
   110 			return false;
   110 		if ( ! $recursive )
   111 		if ( ! $recursive )
   111 			return @chgrp($file, $group);
   112 			return @chgrp($file, $group);
   112 		if ( ! $this->is_dir($file) )
   113 		if ( ! $this->is_dir($file) )
   121 	}
   122 	}
   122 
   123 
   123 	/**
   124 	/**
   124 	 * Changes filesystem permissions
   125 	 * Changes filesystem permissions
   125 	 *
   126 	 *
   126 	 * @param string $file Path to the file.
   127 	 * @param string $file      Path to the file.
   127 	 * @param int $mode (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs.
   128 	 * @param int    $mode      Optional. The permissions as octal number, usually 0644 for files,
   128 	 * @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
   129 	 *                          0755 for dirs. Default false.
       
   130 	 * @param bool   $recursive Optional. If set True changes file group recursively. Default false.
   129 	 * @return bool Returns true on success or false on failure.
   131 	 * @return bool Returns true on success or false on failure.
   130 	 */
   132 	 */
   131 	function chmod($file, $mode = false, $recursive = false) {
   133 	public function chmod($file, $mode = false, $recursive = false) {
   132 		if ( ! $mode ) {
   134 		if ( ! $mode ) {
   133 			if ( $this->is_file($file) )
   135 			if ( $this->is_file($file) )
   134 				$mode = FS_CHMOD_FILE;
   136 				$mode = FS_CHMOD_FILE;
   135 			elseif ( $this->is_dir($file) )
   137 			elseif ( $this->is_dir($file) )
   136 				$mode = FS_CHMOD_DIR;
   138 				$mode = FS_CHMOD_DIR;
   150 	}
   152 	}
   151 
   153 
   152 	/**
   154 	/**
   153 	 * Changes file owner
   155 	 * Changes file owner
   154 	 *
   156 	 *
   155 	 * @param string $file Path to the file.
   157 	 * @param string $file      Path to the file.
   156 	 * @param mixed $owner A user name or number.
   158 	 * @param mixed  $owner     A user name or number.
   157 	 * @param bool $recursive (optional) If set True changes file owner recursively. Defaults to False.
   159 	 * @param bool   $recursive Optional. If set True changes file owner recursively.
       
   160 	 *                          Default false.
   158 	 * @return bool Returns true on success or false on failure.
   161 	 * @return bool Returns true on success or false on failure.
   159 	 */
   162 	 */
   160 	function chown($file, $owner, $recursive = false) {
   163 	public function chown($file, $owner, $recursive = false) {
   161 		if ( ! $this->exists($file) )
   164 		if ( ! $this->exists($file) )
   162 			return false;
   165 			return false;
   163 		if ( ! $recursive )
   166 		if ( ! $recursive )
   164 			return @chown($file, $owner);
   167 			return @chown($file, $owner);
   165 		if ( ! $this->is_dir($file) )
   168 		if ( ! $this->is_dir($file) )
   176 	 * Gets file owner
   179 	 * Gets file owner
   177 	 *
   180 	 *
   178 	 * @param string $file Path to the file.
   181 	 * @param string $file Path to the file.
   179 	 * @return string|bool Username of the user or false on error.
   182 	 * @return string|bool Username of the user or false on error.
   180 	 */
   183 	 */
   181 	function owner($file) {
   184 	public function owner($file) {
   182 		$owneruid = @fileowner($file);
   185 		$owneruid = @fileowner($file);
   183 		if ( ! $owneruid )
   186 		if ( ! $owneruid )
   184 			return false;
   187 			return false;
   185 		if ( ! function_exists('posix_getpwuid') )
   188 		if ( ! function_exists('posix_getpwuid') )
   186 			return $owneruid;
   189 			return $owneruid;
   192 	 * Gets file permissions
   195 	 * Gets file permissions
   193 	 *
   196 	 *
   194 	 * FIXME does not handle errors in fileperms()
   197 	 * FIXME does not handle errors in fileperms()
   195 	 *
   198 	 *
   196 	 * @param string $file Path to the file.
   199 	 * @param string $file Path to the file.
   197 	 * @return string Mode of the file (last 4 digits).
   200 	 * @return string Mode of the file (last 3 digits).
   198 	 */
   201 	 */
   199 	function getchmod($file) {
   202 	public function getchmod($file) {
   200 		return substr(decoct(@fileperms($file)),3);
   203 		return substr( decoct( @fileperms( $file ) ), -3 );
   201 	}
   204 	}
   202 
   205 
   203 	function group($file) {
   206 	/**
       
   207 	 * @param string $file
       
   208 	 * @return string|false
       
   209 	 */
       
   210 	public function group($file) {
   204 		$gid = @filegroup($file);
   211 		$gid = @filegroup($file);
   205 		if ( ! $gid )
   212 		if ( ! $gid )
   206 			return false;
   213 			return false;
   207 		if ( ! function_exists('posix_getgrgid') )
   214 		if ( ! function_exists('posix_getgrgid') )
   208 			return $gid;
   215 			return $gid;
   209 		$grouparray = posix_getgrgid($gid);
   216 		$grouparray = posix_getgrgid($gid);
   210 		return $grouparray['name'];
   217 		return $grouparray['name'];
   211 	}
   218 	}
   212 
   219 
   213 	function copy($source, $destination, $overwrite = false, $mode = false) {
   220 	/**
       
   221 	 * @param string $source
       
   222 	 * @param string $destination
       
   223 	 * @param bool   $overwrite
       
   224 	 * @param int    $mode
       
   225 	 * @return bool
       
   226 	 */
       
   227 	public function copy($source, $destination, $overwrite = false, $mode = false) {
   214 		if ( ! $overwrite && $this->exists($destination) )
   228 		if ( ! $overwrite && $this->exists($destination) )
   215 			return false;
   229 			return false;
   216 
   230 
   217 		$rtval = copy($source, $destination);
   231 		$rtval = copy($source, $destination);
   218 		if ( $mode )
   232 		if ( $mode )
   219 			$this->chmod($destination, $mode);
   233 			$this->chmod($destination, $mode);
   220 		return $rtval;
   234 		return $rtval;
   221 	}
   235 	}
   222 
   236 
   223 	function move($source, $destination, $overwrite = false) {
   237 	/**
       
   238 	 * @param string $source
       
   239 	 * @param string $destination
       
   240 	 * @param bool $overwrite
       
   241 	 * @return bool
       
   242 	 */
       
   243 	public function move($source, $destination, $overwrite = false) {
   224 		if ( ! $overwrite && $this->exists($destination) )
   244 		if ( ! $overwrite && $this->exists($destination) )
   225 			return false;
   245 			return false;
   226 
   246 
   227 		// try using rename first. if that fails (for example, source is read only) try copy
   247 		// Try using rename first. if that fails (for example, source is read only) try copy.
   228 		if ( @rename($source, $destination) )
   248 		if ( @rename($source, $destination) )
   229 			return true;
   249 			return true;
   230 
   250 
   231 		if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
   251 		if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
   232 			$this->delete($source);
   252 			$this->delete($source);
   234 		} else {
   254 		} else {
   235 			return false;
   255 			return false;
   236 		}
   256 		}
   237 	}
   257 	}
   238 
   258 
   239 	function delete($file, $recursive = false, $type = false) {
   259 	/**
       
   260 	 * @param string $file
       
   261 	 * @param bool $recursive
       
   262 	 * @param string $type
       
   263 	 * @return bool
       
   264 	 */
       
   265 	public function delete($file, $recursive = false, $type = false) {
   240 		if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
   266 		if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
   241 			return false;
   267 			return false;
   242 		$file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
   268 		$file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise
   243 
   269 
   244 		if ( 'f' == $type || $this->is_file($file) )
   270 		if ( 'f' == $type || $this->is_file($file) )
   261 		if ( file_exists($file) && ! @rmdir($file) )
   287 		if ( file_exists($file) && ! @rmdir($file) )
   262 			$retval = false;
   288 			$retval = false;
   263 
   289 
   264 		return $retval;
   290 		return $retval;
   265 	}
   291 	}
   266 
   292 	/**
   267 	function exists($file) {
   293 	 * @param string $file
       
   294 	 * @return bool
       
   295 	 */
       
   296 	public function exists($file) {
   268 		return @file_exists($file);
   297 		return @file_exists($file);
   269 	}
   298 	}
   270 
   299 	/**
   271 	function is_file($file) {
   300 	 * @param string $file
       
   301 	 * @return bool
       
   302 	 */
       
   303 	public function is_file($file) {
   272 		return @is_file($file);
   304 		return @is_file($file);
   273 	}
   305 	}
   274 
   306 	/**
   275 	function is_dir($path) {
   307 	 * @param string $path
       
   308 	 * @return bool
       
   309 	 */
       
   310 	public function is_dir($path) {
   276 		return @is_dir($path);
   311 		return @is_dir($path);
   277 	}
   312 	}
   278 
   313 
   279 	function is_readable($file) {
   314 	/**
       
   315 	 * @param string $file
       
   316 	 * @return bool
       
   317 	 */
       
   318 	public function is_readable($file) {
   280 		return @is_readable($file);
   319 		return @is_readable($file);
   281 	}
   320 	}
   282 
   321 
   283 	function is_writable($file) {
   322 	/**
       
   323 	 * @param string $file
       
   324 	 * @return bool
       
   325 	 */
       
   326 	public function is_writable($file) {
   284 		return @is_writable($file);
   327 		return @is_writable($file);
   285 	}
   328 	}
   286 
   329 
   287 	function atime($file) {
   330 	/**
       
   331 	 * @param string $file
       
   332 	 * @return int
       
   333 	 */
       
   334 	public function atime($file) {
   288 		return @fileatime($file);
   335 		return @fileatime($file);
   289 	}
   336 	}
   290 
   337 
   291 	function mtime($file) {
   338 	/**
       
   339 	 * @param string $file
       
   340 	 * @return int
       
   341 	 */
       
   342 	public function mtime($file) {
   292 		return @filemtime($file);
   343 		return @filemtime($file);
   293 	}
   344 	}
   294 
   345 
   295 	function size($file) {
   346 	/**
       
   347 	 * @param string $file
       
   348 	 * @return int
       
   349 	 */
       
   350 	public function size($file) {
   296 		return @filesize($file);
   351 		return @filesize($file);
   297 	}
   352 	}
   298 
   353 
   299 	function touch($file, $time = 0, $atime = 0) {
   354 	/**
       
   355 	 * @param string $file
       
   356 	 * @param int $time
       
   357 	 * @param int $atime
       
   358 	 * @return bool
       
   359 	 */
       
   360 	public function touch($file, $time = 0, $atime = 0) {
   300 		if ($time == 0)
   361 		if ($time == 0)
   301 			$time = time();
   362 			$time = time();
   302 		if ($atime == 0)
   363 		if ($atime == 0)
   303 			$atime = time();
   364 			$atime = time();
   304 		return @touch($file, $time, $atime);
   365 		return @touch($file, $time, $atime);
   305 	}
   366 	}
   306 
   367 
   307 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
   368 	/**
   308 		// safe mode fails with a trailing slash under certain PHP versions.
   369 	 * @param string $path
       
   370 	 * @param mixed  $chmod
       
   371 	 * @param mixed  $chown
       
   372 	 * @param mixed  $chgrp
       
   373 	 * @return bool
       
   374 	 */
       
   375 	public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
       
   376 		// Safe mode fails with a trailing slash under certain PHP versions.
   309 		$path = untrailingslashit($path);
   377 		$path = untrailingslashit($path);
   310 		if ( empty($path) )
   378 		if ( empty($path) )
   311 			return false;
   379 			return false;
   312 
   380 
   313 		if ( ! $chmod )
   381 		if ( ! $chmod )
   321 		if ( $chgrp )
   389 		if ( $chgrp )
   322 			$this->chgrp($path, $chgrp);
   390 			$this->chgrp($path, $chgrp);
   323 		return true;
   391 		return true;
   324 	}
   392 	}
   325 
   393 
   326 	function rmdir($path, $recursive = false) {
   394 	/**
       
   395 	 * @param string $path
       
   396 	 * @param bool $recursive
       
   397 	 * @return bool
       
   398 	 */
       
   399 	public function rmdir($path, $recursive = false) {
   327 		return $this->delete($path, $recursive);
   400 		return $this->delete($path, $recursive);
   328 	}
   401 	}
   329 
   402 
   330 	function dirlist($path, $include_hidden = true, $recursive = false) {
   403 	/**
       
   404 	 * @param string $path
       
   405 	 * @param bool $include_hidden
       
   406 	 * @param bool $recursive
       
   407 	 * @return bool|array
       
   408 	 */
       
   409 	public function dirlist($path, $include_hidden = true, $recursive = false) {
   331 		if ( $this->is_file($path) ) {
   410 		if ( $this->is_file($path) ) {
   332 			$limit_file = basename($path);
   411 			$limit_file = basename($path);
   333 			$path = dirname($path);
   412 			$path = dirname($path);
   334 		} else {
   413 		} else {
   335 			$limit_file = false;
   414 			$limit_file = false;