web/wp-admin/includes/class-wp-filesystem-direct.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
    17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
    18 	var $errors = null;
    18 	var $errors = null;
    19 	/**
    19 	/**
    20 	 * constructor
    20 	 * constructor
    21 	 *
    21 	 *
    22 	 * @param $arg mixed ingored argument
    22 	 * @param mixed $arg ignored argument
    23 	 */
    23 	 */
    24 	function WP_Filesystem_Direct($arg) {
    24 	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 	 * connect filesystem.
    29 	 * connect filesystem.
    34 		return true;
    34 		return true;
    35 	}
    35 	}
    36 	/**
    36 	/**
    37 	 * Reads entire file into a string
    37 	 * Reads entire file into a string
    38 	 *
    38 	 *
    39 	 * @param $file string Name of the file to read.
    39 	 * @param string $file Name of the file to read.
    40 	 * @return string|bool The function returns the read data or false on failure.
    40 	 * @return string|bool The function returns the read data or false on failure.
    41 	 */
    41 	 */
    42 	function get_contents($file) {
    42 	function get_contents($file) {
    43 		return @file_get_contents($file);
    43 		return @file_get_contents($file);
    44 	}
    44 	}
    45 	/**
    45 	/**
    46 	 * Reads entire file into an array
    46 	 * Reads entire file into an array
    47 	 *
    47 	 *
    48 	 * @param $file string Path to the file.
    48 	 * @param string $file Path to the file.
    49 	 * @return array|bool the file contents in an array or false on failure.
    49 	 * @return array|bool the file contents in an array or false on failure.
    50 	 */
    50 	 */
    51 	function get_contents_array($file) {
    51 	function get_contents_array($file) {
    52 		return @file($file);
    52 		return @file($file);
    53 	}
    53 	}
    54 	/**
    54 	/**
    55 	 * Write a string to a file
    55 	 * Write a string to a file
    56 	 *
    56 	 *
    57 	 * @param $file string Path to the file where to write the data.
    57 	 * @param string $file Remote path to the file where to write the data.
    58 	 * @param $contents string The data to write.
    58 	 * @param string $contents The data to write.
    59 	 * @param $mode int (optional) The file permissions as octal number, usually 0644.
    59 	 * @param int $mode (optional) The file permissions as octal number, usually 0644.
    60 	 * @param $type string (optional) Specifies additional type of access you require to the file.
       
    61 	 * @return bool False upon failure.
    60 	 * @return bool False upon failure.
    62 	 */
    61 	 */
    63 	function put_contents($file, $contents, $mode = false, $type = '') {
    62 	function put_contents($file, $contents, $mode = false ) {
    64 		if ( ! ($fp = @fopen($file, 'w' . $type)) )
    63 		if ( ! ($fp = @fopen($file, 'w')) )
    65 			return false;
    64 			return false;
    66 		@fwrite($fp, $contents);
    65 		@fwrite($fp, $contents);
    67 		@fclose($fp);
    66 		@fclose($fp);
    68 		$this->chmod($file, $mode);
    67 		$this->chmod($file, $mode);
    69 		return true;
    68 		return true;
    77 		return @getcwd();
    76 		return @getcwd();
    78 	}
    77 	}
    79 	/**
    78 	/**
    80 	 * Change directory
    79 	 * Change directory
    81 	 *
    80 	 *
    82 	 * @param $dir string The new current directory.
    81 	 * @param string $dir The new current directory.
    83 	 * @return bool Returns true on success or false on failure.
    82 	 * @return bool Returns true on success or false on failure.
    84 	 */
    83 	 */
    85 	function chdir($dir) {
    84 	function chdir($dir) {
    86 		return @chdir($dir);
    85 		return @chdir($dir);
    87 	}
    86 	}
    88 	/**
    87 	/**
    89 	 * Changes file group
    88 	 * Changes file group
    90 	 *
    89 	 *
    91 	 * @param $file string Path to the file.
    90 	 * @param string $file Path to the file.
    92 	 * @param $group mixed A group name or number.
    91 	 * @param mixed $group A group name or number.
    93 	 * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False.
    92 	 * @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
    94 	 * @return bool Returns true on success or false on failure.
    93 	 * @return bool Returns true on success or false on failure.
    95 	 */
    94 	 */
    96 	function chgrp($file, $group, $recursive = false) {
    95 	function chgrp($file, $group, $recursive = false) {
    97 		if ( ! $this->exists($file) )
    96 		if ( ! $this->exists($file) )
    98 			return false;
    97 			return false;
   109 		return true;
   108 		return true;
   110 	}
   109 	}
   111 	/**
   110 	/**
   112 	 * Changes filesystem permissions
   111 	 * Changes filesystem permissions
   113 	 *
   112 	 *
   114 	 * @param $file string Path to the file.
   113 	 * @param string $file Path to the file.
   115 	 * @param $mode int (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs.
   114 	 * @param int $mode (optional) The permissions as octal number, usually 0644 for files, 0755 for dirs.
   116 	 * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False.
   115 	 * @param bool $recursive (optional) If set True changes file group recursively. Defaults to False.
   117 	 * @return bool Returns true on success or false on failure.
   116 	 * @return bool Returns true on success or false on failure.
   118 	 */
   117 	 */
   119 	function chmod($file, $mode = false, $recursive = false) {
   118 	function chmod($file, $mode = false, $recursive = false) {
   120 		if ( ! $this->exists($file) )
       
   121 			return false;
       
   122 
       
   123 		if ( ! $mode ) {
   119 		if ( ! $mode ) {
   124 			if ( $this->is_file($file) )
   120 			if ( $this->is_file($file) )
   125 				$mode = FS_CHMOD_FILE;
   121 				$mode = FS_CHMOD_FILE;
   126 			elseif ( $this->is_dir($file) )
   122 			elseif ( $this->is_dir($file) )
   127 				$mode = FS_CHMOD_DIR;
   123 				$mode = FS_CHMOD_DIR;
   128 			else
   124 			else
   129 				return false;
   125 				return false;
   130 		}
   126 		}
   131 
   127 
   132 		if ( ! $recursive )
   128 		if ( ! $recursive || ! $this->is_dir($file) )
   133 			return @chmod($file, $mode);
       
   134 		if ( ! $this->is_dir($file) )
       
   135 			return @chmod($file, $mode);
   129 			return @chmod($file, $mode);
   136 		//Is a directory, and we want recursive
   130 		//Is a directory, and we want recursive
   137 		$file = trailingslashit($file);
   131 		$file = trailingslashit($file);
   138 		$filelist = $this->dirlist($file);
   132 		$filelist = $this->dirlist($file);
   139 		foreach ($filelist as $filename)
   133 		foreach ( (array)$filelist as $filename => $filemeta)
   140 			$this->chmod($file . $filename, $mode, $recursive);
   134 			$this->chmod($file . $filename, $mode, $recursive);
   141 
   135 
   142 		return true;
   136 		return true;
   143 	}
   137 	}
   144 	/**
   138 	/**
   145 	 * Changes file owner
   139 	 * Changes file owner
   146 	 *
   140 	 *
   147 	 * @param $file string Path to the file.
   141 	 * @param string $file Path to the file.
   148 	 * @param $owner mixed A user name or number.
   142 	 * @param mixed $owner A user name or number.
   149 	 * @param $recursive bool (optional) If set True changes file owner recursivly. Defaults to False.
   143 	 * @param bool $recursive (optional) If set True changes file owner recursively. Defaults to False.
   150 	 * @return bool Returns true on success or false on failure.
   144 	 * @return bool Returns true on success or false on failure.
   151 	 */
   145 	 */
   152 	function chown($file, $owner, $recursive = false) {
   146 	function chown($file, $owner, $recursive = false) {
   153 		if ( ! $this->exists($file) )
   147 		if ( ! $this->exists($file) )
   154 			return false;
   148 			return false;
   164 		return true;
   158 		return true;
   165 	}
   159 	}
   166 	/**
   160 	/**
   167 	 * Gets file owner
   161 	 * Gets file owner
   168 	 *
   162 	 *
   169 	 * @param $file string Path to the file.
   163 	 * @param string $file Path to the file.
   170 	 * @return string Username of the user.
   164 	 * @return string Username of the user.
   171 	 */
   165 	 */
   172 	function owner($file) {
   166 	function owner($file) {
   173 		$owneruid = @fileowner($file);
   167 		$owneruid = @fileowner($file);
   174 		if ( ! $owneruid )
   168 		if ( ! $owneruid )
   181 	/**
   175 	/**
   182 	 * Gets file permissions
   176 	 * Gets file permissions
   183 	 *
   177 	 *
   184 	 * FIXME does not handle errors in fileperms()
   178 	 * FIXME does not handle errors in fileperms()
   185 	 *
   179 	 *
   186 	 * @param $file string Path to the file.
   180 	 * @param string $file Path to the file.
   187 	 * @return string Mode of the file (last 4 digits).
   181 	 * @return string Mode of the file (last 4 digits).
   188 	 */
   182 	 */
   189 	function getchmod($file) {
   183 	function getchmod($file) {
   190 		return substr(decoct(@fileperms($file)),3);
   184 		return substr(decoct(@fileperms($file)),3);
   191 	}
   185 	}
   197 			return $gid;
   191 			return $gid;
   198 		$grouparray = posix_getgrgid($gid);
   192 		$grouparray = posix_getgrgid($gid);
   199 		return $grouparray['name'];
   193 		return $grouparray['name'];
   200 	}
   194 	}
   201 
   195 
   202 	function copy($source, $destination, $overwrite = false) {
   196 	function copy($source, $destination, $overwrite = false, $mode = false) {
   203 		if ( ! $overwrite && $this->exists($destination) )
   197 		if ( ! $overwrite && $this->exists($destination) )
   204 			return false;
   198 			return false;
   205 		return copy($source, $destination);
   199 
       
   200 		$rtval = copy($source, $destination);
       
   201 		if ( $mode )
       
   202 			$this->chmod($destination, $mode);
       
   203 		return $rtval;
   206 	}
   204 	}
   207 
   205 
   208 	function move($source, $destination, $overwrite = false) {
   206 	function move($source, $destination, $overwrite = false) {
   209 		//Possible to use rename()?
   207 		if ( ! $overwrite && $this->exists($destination) )
       
   208 			return false;
       
   209 
       
   210 		// try using rename first. if that fails (for example, source is read only) try copy
       
   211 		if ( @rename($source, $destination) )
       
   212 			return true;
       
   213 
   210 		if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
   214 		if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
   211 			$this->delete($source);
   215 			$this->delete($source);
   212 			return true;
   216 			return true;
   213 		} else {
   217 		} else {
   214 			return false;
   218 			return false;
   215 		}
   219 		}
   216 	}
   220 	}
   217 
   221 
   218 	function delete($file, $recursive = false) {
   222 	function delete($file, $recursive = false, $type = false) {
   219 		if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
   223 		if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
   220 			return false;
   224 			return false;
   221 		$file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
   225 		$file = str_replace('\\', '/', $file); //for win32, occasional problems deleting files otherwise
   222 
   226 
   223 		if ( $this->is_file($file) )
   227 		if ( 'f' == $type || $this->is_file($file) )
   224 			return @unlink($file);
   228 			return @unlink($file);
   225 		if ( ! $recursive && $this->is_dir($file) )
   229 		if ( ! $recursive && $this->is_dir($file) )
   226 			return @rmdir($file);
   230 			return @rmdir($file);
   227 
   231 
   228 		//At this point its a folder, and we're in recursive mode
   232 		//At this point its a folder, and we're in recursive mode
   230 		$filelist = $this->dirlist($file, true);
   234 		$filelist = $this->dirlist($file, true);
   231 
   235 
   232 		$retval = true;
   236 		$retval = true;
   233 		if ( is_array($filelist) ) //false if no files, So check first.
   237 		if ( is_array($filelist) ) //false if no files, So check first.
   234 			foreach ($filelist as $filename => $fileinfo)
   238 			foreach ($filelist as $filename => $fileinfo)
   235 				if ( ! $this->delete($file . $filename, $recursive) )
   239 				if ( ! $this->delete($file . $filename, $recursive, $fileinfo['type']) )
   236 					$retval = false;
   240 					$retval = false;
   237 
   241 
   238 		if ( file_exists($file) && ! @rmdir($file) )
   242 		if ( file_exists($file) && ! @rmdir($file) )
   239 			$retval = false;
   243 			$retval = false;
   240 		return $retval;
   244 		return $retval;
   278 			$atime = time();
   282 			$atime = time();
   279 		return @touch($file, $time, $atime);
   283 		return @touch($file, $time, $atime);
   280 	}
   284 	}
   281 
   285 
   282 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
   286 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
       
   287 		// safe mode fails with a trailing slash under certain PHP versions.
       
   288 		$path = untrailingslashit($path);
       
   289 		if ( empty($path) )
       
   290 			return false;
       
   291 
   283 		if ( ! $chmod )
   292 		if ( ! $chmod )
   284 			$chmod = FS_CHMOD_DIR;
   293 			$chmod = FS_CHMOD_DIR;
   285 
   294 
   286 		if ( ! @mkdir($path) )
   295 		if ( ! @mkdir($path) )
   287 			return false;
   296 			return false;
   292 			$this->chgrp($path, $chgrp);
   301 			$this->chgrp($path, $chgrp);
   293 		return true;
   302 		return true;
   294 	}
   303 	}
   295 
   304 
   296 	function rmdir($path, $recursive = false) {
   305 	function rmdir($path, $recursive = false) {
   297 		//Currently unused and untested, Use delete() instead.
   306 		return $this->delete($path, $recursive);
   298 		if ( ! $recursive )
       
   299 			return @rmdir($path);
       
   300 		//recursive:
       
   301 		$filelist = $this->dirlist($path);
       
   302 		foreach ($filelist as $filename => $det) {
       
   303 			if ( '/' == substr($filename, -1, 1) )
       
   304 				$this->rmdir($path . '/' . $filename, $recursive);
       
   305 			@rmdir($filename);
       
   306 		}
       
   307 		return @rmdir($path);
       
   308 	}
   307 	}
   309 
   308 
   310 	function dirlist($path, $include_hidden = true, $recursive = false) {
   309 	function dirlist($path, $include_hidden = true, $recursive = false) {
   311 		if ( $this->is_file($path) ) {
   310 		if ( $this->is_file($path) ) {
   312 			$limit_file = basename($path);
   311 			$limit_file = basename($path);
   360 		$dir->close();
   359 		$dir->close();
   361 		unset($dir);
   360 		unset($dir);
   362 		return $ret;
   361 		return $ret;
   363 	}
   362 	}
   364 }
   363 }
   365 ?>