X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/3f5685912e89eb3b0534acd85aa0946b1ca2bbe3..7f1521bf193b382565eb753043c161f4cb3fcda7:/wp-admin/includes/class-wp-filesystem-direct.php diff --git a/wp-admin/includes/class-wp-filesystem-direct.php b/wp-admin/includes/class-wp-filesystem-direct.php index 88e4a902..f8979c2a 100644 --- a/wp-admin/includes/class-wp-filesystem-direct.php +++ b/wp-admin/includes/class-wp-filesystem-direct.php @@ -9,98 +9,110 @@ /** * WordPress Filesystem Class for direct PHP file and folder manipulation. * - * @since 2.5 + * @since 2.5.0 * @package WordPress * @subpackage Filesystem * @uses WP_Filesystem_Base Extends class */ class WP_Filesystem_Direct extends WP_Filesystem_Base { - var $errors = null; + /** * constructor * - * @param $arg mixed ingored argument + * @param mixed $arg ignored argument */ - function WP_Filesystem_Direct($arg) { + public function __construct($arg) { $this->method = 'direct'; $this->errors = new WP_Error(); } - /** - * connect filesystem. - * - * @return bool Returns true on success or false on failure (always true for WP_Filesystem_Direct). - */ - function connect() { - return true; - } + /** * Reads entire file into a string * - * @param $file string Name of the file to read. + * @param string $file Name of the file to read. * @return string|bool The function returns the read data or false on failure. */ - function get_contents($file) { + public function get_contents($file) { return @file_get_contents($file); } + /** * Reads entire file into an array * - * @param $file string Path to the file. + * @param string $file Path to the file. * @return array|bool the file contents in an array or false on failure. */ - function get_contents_array($file) { + public function get_contents_array($file) { return @file($file); } + /** * Write a string to a file * - * @param $file string Path to the file where to write the data. - * @param $contents string The data to write. - * @param $mode int (optional) The file permissions as octal number, usually 0644. - * @param $type string (optional) Specifies additional type of access you require to the file. - * @return bool False upon failure. + * @param string $file Remote path to the file where to write the data. + * @param string $contents The data to write. + * @param int $mode Optional. The file permissions as octal number, usually 0644. + * Default false. + * @return bool False upon failure, true otherwise. */ - function put_contents($file, $contents, $mode = false, $type = '') { - if ( ! ($fp = @fopen($file, 'w' . $type)) ) + public function put_contents( $file, $contents, $mode = false ) { + $fp = @fopen( $file, 'wb' ); + if ( ! $fp ) return false; - @fwrite($fp, $contents); - @fclose($fp); - $this->chmod($file, $mode); + + mbstring_binary_safe_encoding(); + + $data_length = strlen( $contents ); + + $bytes_written = fwrite( $fp, $contents ); + + reset_mbstring_encoding(); + + fclose( $fp ); + + if ( $data_length !== $bytes_written ) + return false; + + $this->chmod( $file, $mode ); + return true; } + /** * Gets the current working directory * * @return string|bool the current working directory on success, or false on failure. */ - function cwd() { + public function cwd() { return @getcwd(); } + /** * Change directory * - * @param $dir string The new current directory. + * @param string $dir The new current directory. * @return bool Returns true on success or false on failure. */ - function chdir($dir) { + public function chdir($dir) { return @chdir($dir); } + /** * Changes file group * - * @param $file string Path to the file. - * @param $group mixed A group name or number. - * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False. + * @param string $file Path to the file. + * @param mixed $group A group name or number. + * @param bool $recursive Optional. If set True changes file group recursively. Default false. * @return bool Returns true on success or false on failure. */ - function chgrp($file, $group, $recursive = false) { + public function chgrp($file, $group, $recursive = false) { if ( ! $this->exists($file) ) return false; if ( ! $recursive ) return @chgrp($file, $group); if ( ! $this->is_dir($file) ) return @chgrp($file, $group); - //Is a directory, and we want recursive + // Is a directory, and we want recursive $file = trailingslashit($file); $filelist = $this->dirlist($file); foreach ($filelist as $filename) @@ -108,18 +120,17 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { return true; } + /** * Changes filesystem permissions * - * @param $file string Path to the file. - * @param $mode int (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs. - * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False. + * @param string $file Path to the file. + * @param int $mode Optional. The permissions as octal number, usually 0644 for files, + * 0755 for dirs. Default false. + * @param bool $recursive Optional. If set True changes file group recursively. Default false. * @return bool Returns true on success or false on failure. */ - function chmod($file, $mode = false, $recursive = false) { - if ( ! $this->exists($file) ) - return false; - + public function chmod($file, $mode = false, $recursive = false) { if ( ! $mode ) { if ( $this->is_file($file) ) $mode = FS_CHMOD_FILE; @@ -129,47 +140,48 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { return false; } - if ( ! $recursive ) + if ( ! $recursive || ! $this->is_dir($file) ) return @chmod($file, $mode); - if ( ! $this->is_dir($file) ) - return @chmod($file, $mode); - //Is a directory, and we want recursive + // Is a directory, and we want recursive $file = trailingslashit($file); $filelist = $this->dirlist($file); - foreach ($filelist as $filename) + foreach ( (array)$filelist as $filename => $filemeta) $this->chmod($file . $filename, $mode, $recursive); return true; } + /** * Changes file owner * - * @param $file string Path to the file. - * @param $owner mixed A user name or number. - * @param $recursive bool (optional) If set True changes file owner recursivly. Defaults to False. + * @param string $file Path to the file. + * @param mixed $owner A user name or number. + * @param bool $recursive Optional. If set True changes file owner recursively. + * Default false. * @return bool Returns true on success or false on failure. */ - function chown($file, $owner, $recursive = false) { + public function chown($file, $owner, $recursive = false) { if ( ! $this->exists($file) ) return false; if ( ! $recursive ) return @chown($file, $owner); if ( ! $this->is_dir($file) ) return @chown($file, $owner); - //Is a directory, and we want recursive + // Is a directory, and we want recursive $filelist = $this->dirlist($file); foreach ($filelist as $filename) { $this->chown($file . '/' . $filename, $owner, $recursive); } return true; } + /** * Gets file owner * - * @param $file string Path to the file. - * @return string Username of the user. + * @param string $file Path to the file. + * @return string|bool Username of the user or false on error. */ - function owner($file) { + public function owner($file) { $owneruid = @fileowner($file); if ( ! $owneruid ) return false; @@ -178,18 +190,24 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { $ownerarray = posix_getpwuid($owneruid); return $ownerarray['name']; } + /** * Gets file permissions * * FIXME does not handle errors in fileperms() * - * @param $file string Path to the file. - * @return string Mode of the file (last 4 digits). + * @param string $file Path to the file. + * @return string Mode of the file (last 3 digits). */ - function getchmod($file) { - return substr(decoct(@fileperms($file)),3); + public function getchmod($file) { + return substr( decoct( @fileperms( $file ) ), -3 ); } - function group($file) { + + /** + * @param string $file + * @return string|false + */ + public function group($file) { $gid = @filegroup($file); if ( ! $gid ) return false; @@ -199,14 +217,37 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { return $grouparray['name']; } - function copy($source, $destination, $overwrite = false) { + /** + * @param string $source + * @param string $destination + * @param bool $overwrite + * @param int $mode + * @return bool + */ + public function copy($source, $destination, $overwrite = false, $mode = false) { if ( ! $overwrite && $this->exists($destination) ) return false; - return copy($source, $destination); + + $rtval = copy($source, $destination); + if ( $mode ) + $this->chmod($destination, $mode); + return $rtval; } - function move($source, $destination, $overwrite = false) { - //Possible to use rename()? + /** + * @param string $source + * @param string $destination + * @param bool $overwrite + * @return bool + */ + public function move($source, $destination, $overwrite = false) { + if ( ! $overwrite && $this->exists($destination) ) + return false; + + // Try using rename first. if that fails (for example, source is read only) try copy. + if ( @rename($source, $destination) ) + return true; + if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) { $this->delete($source); return true; @@ -215,63 +256,108 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { } } - function delete($file, $recursive = false) { - if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. + /** + * @param string $file + * @param bool $recursive + * @param string $type + * @return bool + */ + public function delete($file, $recursive = false, $type = false) { + if ( empty( $file ) ) // Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem. return false; - $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise + $file = str_replace( '\\', '/', $file ); // for win32, occasional problems deleting files otherwise - if ( $this->is_file($file) ) + if ( 'f' == $type || $this->is_file($file) ) return @unlink($file); if ( ! $recursive && $this->is_dir($file) ) return @rmdir($file); - //At this point its a folder, and we're in recursive mode + // At this point it's a folder, and we're in recursive mode $file = trailingslashit($file); $filelist = $this->dirlist($file, true); $retval = true; - if ( is_array($filelist) ) //false if no files, So check first. - foreach ($filelist as $filename => $fileinfo) - if ( ! $this->delete($file . $filename, $recursive) ) + if ( is_array( $filelist ) ) { + foreach ( $filelist as $filename => $fileinfo ) { + if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) ) $retval = false; + } + } if ( file_exists($file) && ! @rmdir($file) ) $retval = false; + return $retval; } - - function exists($file) { + /** + * @param string $file + * @return bool + */ + public function exists($file) { return @file_exists($file); } - - function is_file($file) { + /** + * @param string $file + * @return bool + */ + public function is_file($file) { return @is_file($file); } - - function is_dir($path) { + /** + * @param string $path + * @return bool + */ + public function is_dir($path) { return @is_dir($path); } - function is_readable($file) { + /** + * @param string $file + * @return bool + */ + public function is_readable($file) { return @is_readable($file); } - function is_writable($file) { + /** + * @param string $file + * @return bool + */ + public function is_writable($file) { return @is_writable($file); } - function atime($file) { + /** + * @param string $file + * @return int + */ + public function atime($file) { return @fileatime($file); } - function mtime($file) { + /** + * @param string $file + * @return int + */ + public function mtime($file) { return @filemtime($file); } - function size($file) { + + /** + * @param string $file + * @return int + */ + public function size($file) { return @filesize($file); } - function touch($file, $time = 0, $atime = 0) { + /** + * @param string $file + * @param int $time + * @param int $atime + * @return bool + */ + public function touch($file, $time = 0, $atime = 0) { if ($time == 0) $time = time(); if ($atime == 0) @@ -279,7 +365,19 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { return @touch($file, $time, $atime); } - function mkdir($path, $chmod = false, $chown = false, $chgrp = false) { + /** + * @param string $path + * @param mixed $chmod + * @param mixed $chown + * @param mixed $chgrp + * @return bool + */ + public function mkdir($path, $chmod = false, $chown = false, $chgrp = false) { + // Safe mode fails with a trailing slash under certain PHP versions. + $path = untrailingslashit($path); + if ( empty($path) ) + return false; + if ( ! $chmod ) $chmod = FS_CHMOD_DIR; @@ -293,21 +391,22 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { return true; } - function rmdir($path, $recursive = false) { - //Currently unused and untested, Use delete() instead. - if ( ! $recursive ) - return @rmdir($path); - //recursive: - $filelist = $this->dirlist($path); - foreach ($filelist as $filename => $det) { - if ( '/' == substr($filename, -1, 1) ) - $this->rmdir($path . '/' . $filename, $recursive); - @rmdir($filename); - } - return @rmdir($path); + /** + * @param string $path + * @param bool $recursive + * @return bool + */ + public function rmdir($path, $recursive = false) { + return $this->delete($path, $recursive); } - function dirlist($path, $include_hidden = true, $recursive = false) { + /** + * @param string $path + * @param bool $include_hidden + * @param bool $recursive + * @return bool|array + */ + public function dirlist($path, $include_hidden = true, $recursive = false) { if ( $this->is_file($path) ) { $limit_file = basename($path); $path = dirname($path); @@ -362,4 +461,3 @@ class WP_Filesystem_Direct extends WP_Filesystem_Base { return $ret; } } -?>