wp/wp-admin/includes/class-wp-filesystem-direct.php
changeset 7 cf61fcea0001
parent 5 5e2f62d02dcd
child 9 177826044cd9
equal deleted inserted replaced
6:490d5cc509ed 7:cf61fcea0001
     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.0
    12  * @since 2.5.0
    13  * @package WordPress
    13  *
    14  * @subpackage Filesystem
    14  * @see WP_Filesystem_Base
    15  * @uses WP_Filesystem_Base Extends class
       
    16  */
    15  */
    17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
    16 class WP_Filesystem_Direct extends WP_Filesystem_Base {
    18 
    17 
    19 	/**
    18 	/**
    20 	 * constructor
    19 	 * constructor
       
    20 	 *
    21 	 *
    21 	 *
    22 	 * @param mixed $arg ignored argument
    22 	 * @param mixed $arg ignored argument
    23 	 */
    23 	 */
    24 	public function __construct($arg) {
    24 	public function __construct($arg) {
    25 		$this->method = 'direct';
    25 		$this->method = 'direct';
    27 	}
    27 	}
    28 
    28 
    29 	/**
    29 	/**
    30 	 * Reads entire file into a string
    30 	 * Reads entire file into a string
    31 	 *
    31 	 *
       
    32 	 *
    32 	 * @param string $file Name of the file to read.
    33 	 * @param string $file Name of the file to read.
    33 	 * @return string|bool The function returns the read data or false on failure.
    34 	 * @return string|bool The function returns the read data or false on failure.
    34 	 */
    35 	 */
    35 	public function get_contents($file) {
    36 	public function get_contents($file) {
    36 		return @file_get_contents($file);
    37 		return @file_get_contents($file);
    37 	}
    38 	}
    38 
    39 
    39 	/**
    40 	/**
    40 	 * Reads entire file into an array
    41 	 * Reads entire file into an array
    41 	 *
    42 	 *
       
    43 	 *
    42 	 * @param string $file Path to the file.
    44 	 * @param string $file Path to the file.
    43 	 * @return array|bool the file contents in an array or false on failure.
    45 	 * @return array|bool the file contents in an array or false on failure.
    44 	 */
    46 	 */
    45 	public function get_contents_array($file) {
    47 	public function get_contents_array($file) {
    46 		return @file($file);
    48 		return @file($file);
    47 	}
    49 	}
    48 
    50 
    49 	/**
    51 	/**
    50 	 * Write a string to a file
    52 	 * Write a string to a file
       
    53 	 *
    51 	 *
    54 	 *
    52 	 * @param string $file     Remote path to the file where to write the data.
    55 	 * @param string $file     Remote path to the file where to write the data.
    53 	 * @param string $contents The data to write.
    56 	 * @param string $contents The data to write.
    54 	 * @param int    $mode     Optional. The file permissions as octal number, usually 0644.
    57 	 * @param int    $mode     Optional. The file permissions as octal number, usually 0644.
    55 	 *                         Default false.
    58 	 *                         Default false.
    79 	}
    82 	}
    80 
    83 
    81 	/**
    84 	/**
    82 	 * Gets the current working directory
    85 	 * Gets the current working directory
    83 	 *
    86 	 *
       
    87 	 *
    84 	 * @return string|bool the current working directory on success, or false on failure.
    88 	 * @return string|bool the current working directory on success, or false on failure.
    85 	 */
    89 	 */
    86 	public function cwd() {
    90 	public function cwd() {
    87 		return @getcwd();
    91 		return @getcwd();
    88 	}
    92 	}
    89 
    93 
    90 	/**
    94 	/**
    91 	 * Change directory
    95 	 * Change directory
    92 	 *
    96 	 *
       
    97 	 *
    93 	 * @param string $dir The new current directory.
    98 	 * @param string $dir The new current directory.
    94 	 * @return bool Returns true on success or false on failure.
    99 	 * @return bool Returns true on success or false on failure.
    95 	 */
   100 	 */
    96 	public function chdir($dir) {
   101 	public function chdir($dir) {
    97 		return @chdir($dir);
   102 		return @chdir($dir);
    98 	}
   103 	}
    99 
   104 
   100 	/**
   105 	/**
   101 	 * Changes file group
   106 	 * Changes file group
       
   107 	 *
   102 	 *
   108 	 *
   103 	 * @param string $file      Path to the file.
   109 	 * @param string $file      Path to the file.
   104 	 * @param mixed  $group     A group name or number.
   110 	 * @param mixed  $group     A group name or number.
   105 	 * @param bool   $recursive Optional. If set True changes file group recursively. Default false.
   111 	 * @param bool   $recursive Optional. If set True changes file group recursively. Default false.
   106 	 * @return bool Returns true on success or false on failure.
   112 	 * @return bool Returns true on success or false on failure.
   122 	}
   128 	}
   123 
   129 
   124 	/**
   130 	/**
   125 	 * Changes filesystem permissions
   131 	 * Changes filesystem permissions
   126 	 *
   132 	 *
       
   133 	 *
   127 	 * @param string $file      Path to the file.
   134 	 * @param string $file      Path to the file.
   128 	 * @param int    $mode      Optional. The permissions as octal number, usually 0644 for files,
   135 	 * @param int    $mode      Optional. The permissions as octal number, usually 0644 for files,
   129 	 *                          0755 for dirs. Default false.
   136 	 *                          0755 for dirs. Default false.
   130 	 * @param bool   $recursive Optional. If set True changes file group recursively. Default false.
   137 	 * @param bool   $recursive Optional. If set True changes file group recursively. Default false.
   131 	 * @return bool Returns true on success or false on failure.
   138 	 * @return bool Returns true on success or false on failure.
   152 	}
   159 	}
   153 
   160 
   154 	/**
   161 	/**
   155 	 * Changes file owner
   162 	 * Changes file owner
   156 	 *
   163 	 *
       
   164 	 *
   157 	 * @param string $file      Path to the file.
   165 	 * @param string $file      Path to the file.
   158 	 * @param mixed  $owner     A user name or number.
   166 	 * @param mixed  $owner     A user name or number.
   159 	 * @param bool   $recursive Optional. If set True changes file owner recursively.
   167 	 * @param bool   $recursive Optional. If set True changes file owner recursively.
   160 	 *                          Default false.
   168 	 *                          Default false.
   161 	 * @return bool Returns true on success or false on failure.
   169 	 * @return bool Returns true on success or false on failure.
   176 	}
   184 	}
   177 
   185 
   178 	/**
   186 	/**
   179 	 * Gets file owner
   187 	 * Gets file owner
   180 	 *
   188 	 *
       
   189 	 *
   181 	 * @param string $file Path to the file.
   190 	 * @param string $file Path to the file.
   182 	 * @return string|bool Username of the user or false on error.
   191 	 * @return string|bool Username of the user or false on error.
   183 	 */
   192 	 */
   184 	public function owner($file) {
   193 	public function owner($file) {
   185 		$owneruid = @fileowner($file);
   194 		$owneruid = @fileowner($file);
   194 	/**
   203 	/**
   195 	 * Gets file permissions
   204 	 * Gets file permissions
   196 	 *
   205 	 *
   197 	 * FIXME does not handle errors in fileperms()
   206 	 * FIXME does not handle errors in fileperms()
   198 	 *
   207 	 *
       
   208 	 *
   199 	 * @param string $file Path to the file.
   209 	 * @param string $file Path to the file.
   200 	 * @return string Mode of the file (last 3 digits).
   210 	 * @return string Mode of the file (last 3 digits).
   201 	 */
   211 	 */
   202 	public function getchmod($file) {
   212 	public function getchmod($file) {
   203 		return substr( decoct( @fileperms( $file ) ), -3 );
   213 		return substr( decoct( @fileperms( $file ) ), -3 );
   204 	}
   214 	}
   205 
   215 
   206 	/**
   216 	/**
       
   217 	 *
   207 	 * @param string $file
   218 	 * @param string $file
   208 	 * @return string|false
   219 	 * @return string|false
   209 	 */
   220 	 */
   210 	public function group($file) {
   221 	public function group($file) {
   211 		$gid = @filegroup($file);
   222 		$gid = @filegroup($file);
   216 		$grouparray = posix_getgrgid($gid);
   227 		$grouparray = posix_getgrgid($gid);
   217 		return $grouparray['name'];
   228 		return $grouparray['name'];
   218 	}
   229 	}
   219 
   230 
   220 	/**
   231 	/**
       
   232 	 *
   221 	 * @param string $source
   233 	 * @param string $source
   222 	 * @param string $destination
   234 	 * @param string $destination
   223 	 * @param bool   $overwrite
   235 	 * @param bool   $overwrite
   224 	 * @param int    $mode
   236 	 * @param int    $mode
   225 	 * @return bool
   237 	 * @return bool
   233 			$this->chmod($destination, $mode);
   245 			$this->chmod($destination, $mode);
   234 		return $rtval;
   246 		return $rtval;
   235 	}
   247 	}
   236 
   248 
   237 	/**
   249 	/**
       
   250 	 *
   238 	 * @param string $source
   251 	 * @param string $source
   239 	 * @param string $destination
   252 	 * @param string $destination
   240 	 * @param bool $overwrite
   253 	 * @param bool $overwrite
   241 	 * @return bool
   254 	 * @return bool
   242 	 */
   255 	 */
   255 			return false;
   268 			return false;
   256 		}
   269 		}
   257 	}
   270 	}
   258 
   271 
   259 	/**
   272 	/**
       
   273 	 *
   260 	 * @param string $file
   274 	 * @param string $file
   261 	 * @param bool $recursive
   275 	 * @param bool $recursive
   262 	 * @param string $type
   276 	 * @param string $type
   263 	 * @return bool
   277 	 * @return bool
   264 	 */
   278 	 */
   288 			$retval = false;
   302 			$retval = false;
   289 
   303 
   290 		return $retval;
   304 		return $retval;
   291 	}
   305 	}
   292 	/**
   306 	/**
       
   307 	 *
   293 	 * @param string $file
   308 	 * @param string $file
   294 	 * @return bool
   309 	 * @return bool
   295 	 */
   310 	 */
   296 	public function exists($file) {
   311 	public function exists($file) {
   297 		return @file_exists($file);
   312 		return @file_exists($file);
   298 	}
   313 	}
   299 	/**
   314 	/**
       
   315 	 *
   300 	 * @param string $file
   316 	 * @param string $file
   301 	 * @return bool
   317 	 * @return bool
   302 	 */
   318 	 */
   303 	public function is_file($file) {
   319 	public function is_file($file) {
   304 		return @is_file($file);
   320 		return @is_file($file);
   305 	}
   321 	}
   306 	/**
   322 	/**
       
   323 	 *
   307 	 * @param string $path
   324 	 * @param string $path
   308 	 * @return bool
   325 	 * @return bool
   309 	 */
   326 	 */
   310 	public function is_dir($path) {
   327 	public function is_dir($path) {
   311 		return @is_dir($path);
   328 		return @is_dir($path);
   312 	}
   329 	}
   313 
   330 
   314 	/**
   331 	/**
       
   332 	 *
   315 	 * @param string $file
   333 	 * @param string $file
   316 	 * @return bool
   334 	 * @return bool
   317 	 */
   335 	 */
   318 	public function is_readable($file) {
   336 	public function is_readable($file) {
   319 		return @is_readable($file);
   337 		return @is_readable($file);
   320 	}
   338 	}
   321 
   339 
   322 	/**
   340 	/**
       
   341 	 *
   323 	 * @param string $file
   342 	 * @param string $file
   324 	 * @return bool
   343 	 * @return bool
   325 	 */
   344 	 */
   326 	public function is_writable($file) {
   345 	public function is_writable($file) {
   327 		return @is_writable($file);
   346 		return @is_writable($file);
   328 	}
   347 	}
   329 
   348 
   330 	/**
   349 	/**
       
   350 	 *
   331 	 * @param string $file
   351 	 * @param string $file
   332 	 * @return int
   352 	 * @return int
   333 	 */
   353 	 */
   334 	public function atime($file) {
   354 	public function atime($file) {
   335 		return @fileatime($file);
   355 		return @fileatime($file);
   336 	}
   356 	}
   337 
   357 
   338 	/**
   358 	/**
       
   359 	 *
   339 	 * @param string $file
   360 	 * @param string $file
   340 	 * @return int
   361 	 * @return int
   341 	 */
   362 	 */
   342 	public function mtime($file) {
   363 	public function mtime($file) {
   343 		return @filemtime($file);
   364 		return @filemtime($file);
   344 	}
   365 	}
   345 
   366 
   346 	/**
   367 	/**
       
   368 	 *
   347 	 * @param string $file
   369 	 * @param string $file
   348 	 * @return int
   370 	 * @return int
   349 	 */
   371 	 */
   350 	public function size($file) {
   372 	public function size($file) {
   351 		return @filesize($file);
   373 		return @filesize($file);
   352 	}
   374 	}
   353 
   375 
   354 	/**
   376 	/**
       
   377 	 *
   355 	 * @param string $file
   378 	 * @param string $file
   356 	 * @param int $time
   379 	 * @param int $time
   357 	 * @param int $atime
   380 	 * @param int $atime
   358 	 * @return bool
   381 	 * @return bool
   359 	 */
   382 	 */
   364 			$atime = time();
   387 			$atime = time();
   365 		return @touch($file, $time, $atime);
   388 		return @touch($file, $time, $atime);
   366 	}
   389 	}
   367 
   390 
   368 	/**
   391 	/**
       
   392 	 *
   369 	 * @param string $path
   393 	 * @param string $path
   370 	 * @param mixed  $chmod
   394 	 * @param mixed  $chmod
   371 	 * @param mixed  $chown
   395 	 * @param mixed  $chown
   372 	 * @param mixed  $chgrp
   396 	 * @param mixed  $chgrp
   373 	 * @return bool
   397 	 * @return bool
   390 			$this->chgrp($path, $chgrp);
   414 			$this->chgrp($path, $chgrp);
   391 		return true;
   415 		return true;
   392 	}
   416 	}
   393 
   417 
   394 	/**
   418 	/**
       
   419 	 *
   395 	 * @param string $path
   420 	 * @param string $path
   396 	 * @param bool $recursive
   421 	 * @param bool $recursive
   397 	 * @return bool
   422 	 * @return bool
   398 	 */
   423 	 */
   399 	public function rmdir($path, $recursive = false) {
   424 	public function rmdir($path, $recursive = false) {
   400 		return $this->delete($path, $recursive);
   425 		return $this->delete($path, $recursive);
   401 	}
   426 	}
   402 
   427 
   403 	/**
   428 	/**
       
   429 	 *
   404 	 * @param string $path
   430 	 * @param string $path
   405 	 * @param bool $include_hidden
   431 	 * @param bool $include_hidden
   406 	 * @param bool $recursive
   432 	 * @param bool $recursive
   407 	 * @return bool|array
   433 	 * @return bool|array
   408 	 */
   434 	 */