- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-admin/includes/class-wp-filesystem-direct.php
r41161 r42343 19 19 * constructor 20 20 * 21 *22 21 * @param mixed $arg ignored argument 23 22 */ 24 public function __construct( $arg) {23 public function __construct( $arg ) { 25 24 $this->method = 'direct'; 26 25 $this->errors = new WP_Error(); … … 30 29 * Reads entire file into a string 31 30 * 32 *33 31 * @param string $file Name of the file to read. 34 32 * @return string|bool The function returns the read data or false on failure. 35 33 */ 36 public function get_contents( $file) {37 return @file_get_contents( $file);34 public function get_contents( $file ) { 35 return @file_get_contents( $file ); 38 36 } 39 37 40 38 /** 41 39 * Reads entire file into an array 42 *43 40 * 44 41 * @param string $file Path to the file. 45 42 * @return array|bool the file contents in an array or false on failure. 46 43 */ 47 public function get_contents_array( $file) {48 return @file( $file);44 public function get_contents_array( $file ) { 45 return @file( $file ); 49 46 } 50 47 51 48 /** 52 49 * Write a string to a file 53 *54 50 * 55 51 * @param string $file Remote path to the file where to write the data. … … 61 57 public function put_contents( $file, $contents, $mode = false ) { 62 58 $fp = @fopen( $file, 'wb' ); 63 if ( ! $fp ) 64 return false; 59 if ( ! $fp ) { 60 return false; 61 } 65 62 66 63 mbstring_binary_safe_encoding(); … … 74 71 fclose( $fp ); 75 72 76 if ( $data_length !== $bytes_written ) 77 return false; 73 if ( $data_length !== $bytes_written ) { 74 return false; 75 } 78 76 79 77 $this->chmod( $file, $mode ); … … 84 82 /** 85 83 * Gets the current working directory 86 *87 84 * 88 85 * @return string|bool the current working directory on success, or false on failure. … … 95 92 * Change directory 96 93 * 97 *98 94 * @param string $dir The new current directory. 99 95 * @return bool Returns true on success or false on failure. 100 96 */ 101 public function chdir( $dir) {102 return @chdir( $dir);97 public function chdir( $dir ) { 98 return @chdir( $dir ); 103 99 } 104 100 105 101 /** 106 102 * Changes file group 107 *108 103 * 109 104 * @param string $file Path to the file. … … 112 107 * @return bool Returns true on success or false on failure. 113 108 */ 114 public function chgrp($file, $group, $recursive = false) { 115 if ( ! $this->exists($file) ) 116 return false; 117 if ( ! $recursive ) 118 return @chgrp($file, $group); 119 if ( ! $this->is_dir($file) ) 120 return @chgrp($file, $group); 109 public function chgrp( $file, $group, $recursive = false ) { 110 if ( ! $this->exists( $file ) ) { 111 return false; 112 } 113 if ( ! $recursive ) { 114 return @chgrp( $file, $group ); 115 } 116 if ( ! $this->is_dir( $file ) ) { 117 return @chgrp( $file, $group ); 118 } 121 119 // Is a directory, and we want recursive 122 $file = trailingslashit($file); 123 $filelist = $this->dirlist($file); 124 foreach ($filelist as $filename) 125 $this->chgrp($file . $filename, $group, $recursive); 120 $file = trailingslashit( $file ); 121 $filelist = $this->dirlist( $file ); 122 foreach ( $filelist as $filename ) { 123 $this->chgrp( $file . $filename, $group, $recursive ); 124 } 126 125 127 126 return true; … … 130 129 /** 131 130 * Changes filesystem permissions 132 *133 131 * 134 132 * @param string $file Path to the file. … … 138 136 * @return bool Returns true on success or false on failure. 139 137 */ 140 public function chmod( $file, $mode = false, $recursive = false) {138 public function chmod( $file, $mode = false, $recursive = false ) { 141 139 if ( ! $mode ) { 142 if ( $this->is_file( $file) )140 if ( $this->is_file( $file ) ) { 143 141 $mode = FS_CHMOD_FILE; 144 elseif ( $this->is_dir($file) )142 } elseif ( $this->is_dir( $file ) ) { 145 143 $mode = FS_CHMOD_DIR; 146 else144 } else { 147 145 return false; 148 } 149 150 if ( ! $recursive || ! $this->is_dir($file) ) 151 return @chmod($file, $mode); 146 } 147 } 148 149 if ( ! $recursive || ! $this->is_dir( $file ) ) { 150 return @chmod( $file, $mode ); 151 } 152 152 // Is a directory, and we want recursive 153 $file = trailingslashit($file); 154 $filelist = $this->dirlist($file); 155 foreach ( (array)$filelist as $filename => $filemeta) 156 $this->chmod($file . $filename, $mode, $recursive); 153 $file = trailingslashit( $file ); 154 $filelist = $this->dirlist( $file ); 155 foreach ( (array) $filelist as $filename => $filemeta ) { 156 $this->chmod( $file . $filename, $mode, $recursive ); 157 } 157 158 158 159 return true; … … 161 162 /** 162 163 * Changes file owner 163 *164 164 * 165 165 * @param string $file Path to the file. … … 169 169 * @return bool Returns true on success or false on failure. 170 170 */ 171 public function chown($file, $owner, $recursive = false) { 172 if ( ! $this->exists($file) ) 173 return false; 174 if ( ! $recursive ) 175 return @chown($file, $owner); 176 if ( ! $this->is_dir($file) ) 177 return @chown($file, $owner); 171 public function chown( $file, $owner, $recursive = false ) { 172 if ( ! $this->exists( $file ) ) { 173 return false; 174 } 175 if ( ! $recursive ) { 176 return @chown( $file, $owner ); 177 } 178 if ( ! $this->is_dir( $file ) ) { 179 return @chown( $file, $owner ); 180 } 178 181 // Is a directory, and we want recursive 179 $filelist = $this->dirlist( $file);180 foreach ( $filelist as $filename) {181 $this->chown( $file . '/' . $filename, $owner, $recursive);182 $filelist = $this->dirlist( $file ); 183 foreach ( $filelist as $filename ) { 184 $this->chown( $file . '/' . $filename, $owner, $recursive ); 182 185 } 183 186 return true; … … 186 189 /** 187 190 * Gets file owner 188 *189 191 * 190 192 * @param string $file Path to the file. 191 193 * @return string|bool Username of the user or false on error. 192 194 */ 193 public function owner($file) { 194 $owneruid = @fileowner($file); 195 if ( ! $owneruid ) 196 return false; 197 if ( ! function_exists('posix_getpwuid') ) 195 public function owner( $file ) { 196 $owneruid = @fileowner( $file ); 197 if ( ! $owneruid ) { 198 return false; 199 } 200 if ( ! function_exists( 'posix_getpwuid' ) ) { 198 201 return $owneruid; 199 $ownerarray = posix_getpwuid($owneruid); 202 } 203 $ownerarray = posix_getpwuid( $owneruid ); 200 204 return $ownerarray['name']; 201 205 } … … 205 209 * 206 210 * FIXME does not handle errors in fileperms() 207 *208 211 * 209 212 * @param string $file Path to the file. 210 213 * @return string Mode of the file (last 3 digits). 211 214 */ 212 public function getchmod( $file) {215 public function getchmod( $file ) { 213 216 return substr( decoct( @fileperms( $file ) ), -3 ); 214 217 } 215 218 216 219 /** 217 *218 220 * @param string $file 219 221 * @return string|false 220 222 */ 221 public function group($file) { 222 $gid = @filegroup($file); 223 if ( ! $gid ) 224 return false; 225 if ( ! function_exists('posix_getgrgid') ) 223 public function group( $file ) { 224 $gid = @filegroup( $file ); 225 if ( ! $gid ) { 226 return false; 227 } 228 if ( ! function_exists( 'posix_getgrgid' ) ) { 226 229 return $gid; 227 $grouparray = posix_getgrgid($gid); 230 } 231 $grouparray = posix_getgrgid( $gid ); 228 232 return $grouparray['name']; 229 233 } 230 234 231 235 /** 232 *233 236 * @param string $source 234 237 * @param string $destination … … 237 240 * @return bool 238 241 */ 239 public function copy($source, $destination, $overwrite = false, $mode = false) { 240 if ( ! $overwrite && $this->exists($destination) ) 241 return false; 242 243 $rtval = copy($source, $destination); 244 if ( $mode ) 245 $this->chmod($destination, $mode); 242 public function copy( $source, $destination, $overwrite = false, $mode = false ) { 243 if ( ! $overwrite && $this->exists( $destination ) ) { 244 return false; 245 } 246 247 $rtval = copy( $source, $destination ); 248 if ( $mode ) { 249 $this->chmod( $destination, $mode ); 250 } 246 251 return $rtval; 247 252 } 248 253 249 254 /** 250 *251 255 * @param string $source 252 256 * @param string $destination … … 254 258 * @return bool 255 259 */ 256 public function move($source, $destination, $overwrite = false) { 257 if ( ! $overwrite && $this->exists($destination) ) 258 return false; 260 public function move( $source, $destination, $overwrite = false ) { 261 if ( ! $overwrite && $this->exists( $destination ) ) { 262 return false; 263 } 259 264 260 265 // Try using rename first. if that fails (for example, source is read only) try copy. 261 if ( @rename( $source, $destination) )266 if ( @rename( $source, $destination ) ) { 262 267 return true; 263 264 if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) { 265 $this->delete($source); 268 } 269 270 if ( $this->copy( $source, $destination, $overwrite ) && $this->exists( $destination ) ) { 271 $this->delete( $source ); 266 272 return true; 267 273 } else { … … 271 277 272 278 /** 273 *274 279 * @param string $file 275 280 * @param bool $recursive … … 277 282 * @return bool 278 283 */ 279 public function delete($file, $recursive = false, $type = false) { 280 if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. 281 return false; 284 public function delete( $file, $recursive = false, $type = false ) { 285 if ( empty( $file ) ) { // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. 286 return false; 287 } 282 288 $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise 283 289 284 if ( 'f' == $type || $this->is_file($file) ) 285 return @unlink($file); 286 if ( ! $recursive && $this->is_dir($file) ) 287 return @rmdir($file); 290 if ( 'f' == $type || $this->is_file( $file ) ) { 291 return @unlink( $file ); 292 } 293 if ( ! $recursive && $this->is_dir( $file ) ) { 294 return @rmdir( $file ); 295 } 288 296 289 297 // At this point it's a folder, and we're in recursive mode 290 $file = trailingslashit($file);291 $filelist = $this->dirlist( $file, true);298 $file = trailingslashit( $file ); 299 $filelist = $this->dirlist( $file, true ); 292 300 293 301 $retval = true; 294 302 if ( is_array( $filelist ) ) { 295 303 foreach ( $filelist as $filename => $fileinfo ) { 296 if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type']) )304 if ( ! $this->delete( $file . $filename, $recursive, $fileinfo['type'] ) ) { 297 305 $retval = false; 306 } 298 307 } 299 308 } 300 309 301 if ( file_exists( $file) && ! @rmdir($file) )310 if ( file_exists( $file ) && ! @rmdir( $file ) ) { 302 311 $retval = false; 312 } 303 313 304 314 return $retval; 305 315 } 306 316 /** 307 * 308 * @param string $file 309 * @return bool 310 */ 311 public function exists($file) { 312 return @file_exists($file); 313 } 314 /** 315 * 316 * @param string $file 317 * @return bool 318 */ 319 public function is_file($file) { 320 return @is_file($file); 321 } 322 /** 323 * 317 * @param string $file 318 * @return bool 319 */ 320 public function exists( $file ) { 321 return @file_exists( $file ); 322 } 323 /** 324 * @param string $file 325 * @return bool 326 */ 327 public function is_file( $file ) { 328 return @is_file( $file ); 329 } 330 /** 324 331 * @param string $path 325 332 * @return bool 326 333 */ 327 public function is_dir($path) { 328 return @is_dir($path); 329 } 330 331 /** 332 * 333 * @param string $file 334 * @return bool 335 */ 336 public function is_readable($file) { 337 return @is_readable($file); 338 } 339 340 /** 341 * 342 * @param string $file 343 * @return bool 344 */ 345 public function is_writable($file) { 346 return @is_writable($file); 347 } 348 349 /** 350 * 334 public function is_dir( $path ) { 335 return @is_dir( $path ); 336 } 337 338 /** 339 * @param string $file 340 * @return bool 341 */ 342 public function is_readable( $file ) { 343 return @is_readable( $file ); 344 } 345 346 /** 347 * @param string $file 348 * @return bool 349 */ 350 public function is_writable( $file ) { 351 return @is_writable( $file ); 352 } 353 354 /** 351 355 * @param string $file 352 356 * @return int 353 357 */ 354 public function atime($file) { 355 return @fileatime($file); 356 } 357 358 /** 359 * 358 public function atime( $file ) { 359 return @fileatime( $file ); 360 } 361 362 /** 360 363 * @param string $file 361 364 * @return int 362 365 */ 363 public function mtime($file) { 364 return @filemtime($file); 365 } 366 367 /** 368 * 366 public function mtime( $file ) { 367 return @filemtime( $file ); 368 } 369 370 /** 369 371 * @param string $file 370 372 * @return int 371 373 */ 372 public function size($file) { 373 return @filesize($file); 374 } 375 376 /** 377 * 374 public function size( $file ) { 375 return @filesize( $file ); 376 } 377 378 /** 378 379 * @param string $file 379 380 * @param int $time … … 381 382 * @return bool 382 383 */ 383 public function touch( $file, $time = 0, $atime = 0) {384 if ( $time == 0)384 public function touch( $file, $time = 0, $atime = 0 ) { 385 if ( $time == 0 ) { 385 386 $time = time(); 386 if ($atime == 0) 387 } 388 if ( $atime == 0 ) { 387 389 $atime = time(); 388 return @touch($file, $time, $atime);389 }390 391 /** 392 390 } 391 return @touch( $file, $time, $atime ); 392 } 393 394 /** 393 395 * @param string $path 394 396 * @param mixed $chmod … … 397 399 * @return bool 398 400 */ 399 public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false) {401 public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { 400 402 // Safe mode fails with a trailing slash under certain PHP versions. 401 $path = untrailingslashit($path); 402 if ( empty($path) ) 403 return false; 404 405 if ( ! $chmod ) 403 $path = untrailingslashit( $path ); 404 if ( empty( $path ) ) { 405 return false; 406 } 407 408 if ( ! $chmod ) { 406 409 $chmod = FS_CHMOD_DIR; 407 408 if ( ! @mkdir($path) ) 409 return false; 410 $this->chmod($path, $chmod); 411 if ( $chown ) 412 $this->chown($path, $chown); 413 if ( $chgrp ) 414 $this->chgrp($path, $chgrp); 410 } 411 412 if ( ! @mkdir( $path ) ) { 413 return false; 414 } 415 $this->chmod( $path, $chmod ); 416 if ( $chown ) { 417 $this->chown( $path, $chown ); 418 } 419 if ( $chgrp ) { 420 $this->chgrp( $path, $chgrp ); 421 } 415 422 return true; 416 423 } 417 424 418 425 /** 419 *420 426 * @param string $path 421 427 * @param bool $recursive 422 428 * @return bool 423 429 */ 424 public function rmdir($path, $recursive = false) { 425 return $this->delete($path, $recursive); 426 } 427 428 /** 429 * 430 public function rmdir( $path, $recursive = false ) { 431 return $this->delete( $path, $recursive ); 432 } 433 434 /** 430 435 * @param string $path 431 436 * @param bool $include_hidden … … 433 438 * @return bool|array 434 439 */ 435 public function dirlist( $path, $include_hidden = true, $recursive = false) {436 if ( $this->is_file( $path) ) {437 $limit_file = basename( $path);438 $path = dirname($path);440 public function dirlist( $path, $include_hidden = true, $recursive = false ) { 441 if ( $this->is_file( $path ) ) { 442 $limit_file = basename( $path ); 443 $path = dirname( $path ); 439 444 } else { 440 445 $limit_file = false; 441 446 } 442 447 443 if ( ! $this->is_dir($path) ) 444 return false; 445 446 $dir = @dir($path); 447 if ( ! $dir ) 448 return false; 448 if ( ! $this->is_dir( $path ) ) { 449 return false; 450 } 451 452 $dir = @dir( $path ); 453 if ( ! $dir ) { 454 return false; 455 } 449 456 450 457 $ret = array(); 451 458 452 while ( false !== ($entry = $dir->read()) ) {453 $struc = array();459 while ( false !== ( $entry = $dir->read() ) ) { 460 $struc = array(); 454 461 $struc['name'] = $entry; 455 462 456 if ( '.' == $struc['name'] || '..' == $struc['name'] ) 463 if ( '.' == $struc['name'] || '..' == $struc['name'] ) { 457 464 continue; 458 459 if ( ! $include_hidden && '.' == $struc['name'][0] ) 465 } 466 467 if ( ! $include_hidden && '.' == $struc['name'][0] ) { 460 468 continue; 461 462 if ( $limit_file && $struc['name'] != $limit_file) 469 } 470 471 if ( $limit_file && $struc['name'] != $limit_file ) { 463 472 continue; 464 465 $struc['perms'] = $this->gethchmod($path.'/'.$entry); 466 $struc['permsn'] = $this->getnumchmodfromh($struc['perms']); 467 $struc['number'] = false; 468 $struc['owner'] = $this->owner($path.'/'.$entry); 469 $struc['group'] = $this->group($path.'/'.$entry); 470 $struc['size'] = $this->size($path.'/'.$entry); 471 $struc['lastmodunix']= $this->mtime($path.'/'.$entry); 472 $struc['lastmod'] = date('M j',$struc['lastmodunix']); 473 $struc['time'] = date('h:i:s',$struc['lastmodunix']); 474 $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f'; 473 } 474 475 $struc['perms'] = $this->gethchmod( $path . '/' . $entry ); 476 $struc['permsn'] = $this->getnumchmodfromh( $struc['perms'] ); 477 $struc['number'] = false; 478 $struc['owner'] = $this->owner( $path . '/' . $entry ); 479 $struc['group'] = $this->group( $path . '/' . $entry ); 480 $struc['size'] = $this->size( $path . '/' . $entry ); 481 $struc['lastmodunix'] = $this->mtime( $path . '/' . $entry ); 482 $struc['lastmod'] = date( 'M j', $struc['lastmodunix'] ); 483 $struc['time'] = date( 'h:i:s', $struc['lastmodunix'] ); 484 $struc['type'] = $this->is_dir( $path . '/' . $entry ) ? 'd' : 'f'; 475 485 476 486 if ( 'd' == $struc['type'] ) { 477 if ( $recursive ) 478 $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive);479 else487 if ( $recursive ) { 488 $struc['files'] = $this->dirlist( $path . '/' . $struc['name'], $include_hidden, $recursive ); 489 } else { 480 490 $struc['files'] = array(); 491 } 481 492 } 482 493 … … 484 495 } 485 496 $dir->close(); 486 unset( $dir);497 unset( $dir ); 487 498 return $ret; 488 499 }
Note: See TracChangeset
for help on using the changeset viewer.