* @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) {
+ 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) {
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) {
return @file($file);
}
+
/**
* Write a string to a file
*
- * @param $file string Remote 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 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.
* @return bool False upon failure.
*/
- function put_contents($file, $contents, $mode = false ) {
- if ( ! ($fp = @fopen($file, 'w')) )
+ function put_contents( $file, $contents, $mode = false ) {
+ $fp = @fopen( $file, 'wb' );
+ if ( ! $fp )
+ return false;
+
+ 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;
- @fwrite($fp, $contents);
- @fclose($fp);
- $this->chmod($file, $mode);
+
+ $this->chmod( $file, $mode );
+
return true;
}
+
/**
* Gets the current working directory
*
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) {
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. Defaults to False.
* @return bool Returns true on success or false on failure.
*/
function chgrp($file, $group, $recursive = false) {
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)
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.
+ * @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
* @return bool Returns true on success or false on failure.
*/
function chmod($file, $mode = false, $recursive = false) {
if ( ! $recursive || ! $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 ( (array)$filelist as $filename => $filemeta)
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. Defaults to False.
* @return bool Returns true on success or false on failure.
*/
function chown($file, $owner, $recursive = false) {
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) {
$owneruid = @fileowner($file);
$ownerarray = posix_getpwuid($owneruid);
return $ownerarray['name'];
}
+
/**
* Gets file permissions
*
* FIXME does not handle errors in fileperms()
*
- * @param $file string Path to the file.
+ * @param string $file Path to the file.
* @return string Mode of the file (last 4 digits).
*/
function getchmod($file) {
return substr(decoct(@fileperms($file)),3);
}
+
function group($file) {
$gid = @filegroup($file);
if ( ! $gid )
return $grouparray['name'];
}
- function copy($source, $destination, $overwrite = false) {
+ 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) {
if ( ! $overwrite && $this->exists($destination) )
return false;
- // try using rename first. if that fails (for example, source is read only) try copy
+ // try using rename first. if that fails (for example, source is read only) try copy
if ( @rename($source, $destination) )
return true;
}
}
- 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.
+ 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 mtime($file) {
return @filemtime($file);
}
+
function size($file) {
return @filesize($file);
}
// safe mode fails with a trailing slash under certain PHP versions.
$path = untrailingslashit($path);
if ( empty($path) )
- $path = '/';
+ return false;
if ( ! $chmod )
$chmod = FS_CHMOD_DIR;
return $ret;
}
}
-?>