web/wp-admin/includes/class-wp-filesystem-direct.php
branchwordpress
changeset 132 4d4862461b8d
parent 109 03b0d1493584
equal deleted inserted replaced
131:a4642baaf829 132:4d4862461b8d
    13  * @package WordPress
    13  * @package WordPress
    14  * @subpackage Filesystem
    14  * @subpackage Filesystem
    15  * @uses WP_Filesystem_Base Extends class
    15  * @uses WP_Filesystem_Base Extends class
    16  */
    16  */
    17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
    17 class WP_Filesystem_Direct extends WP_Filesystem_Base {
    18 	var $permission = null;
       
    19 	var $errors = null;
    18 	var $errors = null;
       
    19 	/**
       
    20 	 * constructor
       
    21 	 *
       
    22 	 * @param $arg mixed ingored argument
       
    23 	 */
    20 	function WP_Filesystem_Direct($arg) {
    24 	function WP_Filesystem_Direct($arg) {
    21 		$this->method = 'direct';
    25 		$this->method = 'direct';
    22 		$this->errors = new WP_Error();
    26 		$this->errors = new WP_Error();
    23 	}
    27 	}
       
    28 	/**
       
    29 	 * connect filesystem.
       
    30 	 *
       
    31 	 * @return bool Returns true on success or false on failure (always true for WP_Filesystem_Direct).
       
    32 	 */
    24 	function connect() {
    33 	function connect() {
    25 		return true;
    34 		return true;
    26 	}
    35 	}
    27 	function setDefaultPermissions($perm) {
    36 	/**
    28 		$this->permission = $perm;
    37 	 * Reads entire file into a string
    29 	}
    38 	 *
       
    39 	 * @param $file string Name of the file to read.
       
    40 	 * @return string|bool The function returns the read data or false on failure.
       
    41 	 */
    30 	function get_contents($file) {
    42 	function get_contents($file) {
    31 		return @file_get_contents($file);
    43 		return @file_get_contents($file);
    32 	}
    44 	}
       
    45 	/**
       
    46 	 * Reads entire file into an array
       
    47 	 *
       
    48 	 * @param $file string Path to the file.
       
    49 	 * @return array|bool the file contents in an array or false on failure.
       
    50 	 */
    33 	function get_contents_array($file) {
    51 	function get_contents_array($file) {
    34 		return @file($file);
    52 		return @file($file);
    35 	}
    53 	}
       
    54 	/**
       
    55 	 * Write a string to a file
       
    56 	 *
       
    57 	 * @param $file string Path to the file where to write the data.
       
    58 	 * @param $contents string The data to write.
       
    59 	 * @param $mode int (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.
       
    62 	 */
    36 	function put_contents($file, $contents, $mode = false, $type = '') {
    63 	function put_contents($file, $contents, $mode = false, $type = '') {
    37 		if ( ! ($fp = @fopen($file, 'w' . $type)) )
    64 		if ( ! ($fp = @fopen($file, 'w' . $type)) )
    38 			return false;
    65 			return false;
    39 		@fwrite($fp, $contents);
    66 		@fwrite($fp, $contents);
    40 		@fclose($fp);
    67 		@fclose($fp);
    41 		$this->chmod($file,$mode);
    68 		$this->chmod($file, $mode);
    42 		return true;
    69 		return true;
    43 	}
    70 	}
       
    71 	/**
       
    72 	 * Gets the current working directory
       
    73 	 *
       
    74 	 * @return string|bool the current working directory on success, or false on failure.
       
    75 	 */
    44 	function cwd() {
    76 	function cwd() {
    45 		return @getcwd();
    77 		return @getcwd();
    46 	}
    78 	}
       
    79 	/**
       
    80 	 * Change directory
       
    81 	 *
       
    82 	 * @param $dir string The new current directory.
       
    83 	 * @return bool Returns true on success or false on failure.
       
    84 	 */
    47 	function chdir($dir) {
    85 	function chdir($dir) {
    48 		return @chdir($dir);
    86 		return @chdir($dir);
    49 	}
    87 	}
       
    88 	/**
       
    89 	 * Changes file group
       
    90 	 *
       
    91 	 * @param $file string Path to the file.
       
    92 	 * @param $group mixed A group name or number.
       
    93 	 * @param $recursive bool (optional) If set True changes file group recursivly. Defaults to False.
       
    94 	 * @return bool Returns true on success or false on failure.
       
    95 	 */
    50 	function chgrp($file, $group, $recursive = false) {
    96 	function chgrp($file, $group, $recursive = false) {
    51 		if ( ! $this->exists($file) )
    97 		if ( ! $this->exists($file) )
    52 			return false;
    98 			return false;
    53 		if ( ! $recursive )
    99 		if ( ! $recursive )
    54 			return @chgrp($file, $group);
   100 			return @chgrp($file, $group);
    60 		foreach ($filelist as $filename)
   106 		foreach ($filelist as $filename)
    61 			$this->chgrp($file . $filename, $group, $recursive);
   107 			$this->chgrp($file . $filename, $group, $recursive);
    62 
   108 
    63 		return true;
   109 		return true;
    64 	}
   110 	}
       
   111 	/**
       
   112 	 * Changes filesystem permissions
       
   113 	 *
       
   114 	 * @param $file string Path to the file.
       
   115 	 * @param $mode int (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.
       
   117 	 * @return bool Returns true on success or false on failure.
       
   118 	 */
    65 	function chmod($file, $mode = false, $recursive = false) {
   119 	function chmod($file, $mode = false, $recursive = false) {
    66 		if ( ! $this->exists($file) )
   120 		if ( ! $this->exists($file) )
    67 			return false;
   121 			return false;
    68 
   122 
    69 		if ( ! $mode ) {
   123 		if ( ! $mode ) {
    70 			if ( $this->permission )
   124 			if ( $this->is_file($file) )
    71 				$mode = $this->permission;
       
    72 			elseif ( $this->is_file($file) )
       
    73 				$mode = FS_CHMOD_FILE;
   125 				$mode = FS_CHMOD_FILE;
    74 			elseif ( $this->is_dir($file) )
   126 			elseif ( $this->is_dir($file) )
    75 				$mode = FS_CHMOD_DIR;
   127 				$mode = FS_CHMOD_DIR;
    76 			else
   128 			else
    77 				return false;	
   129 				return false;
    78 		}
   130 		}
    79 
   131 
    80 		if ( ! $recursive )
   132 		if ( ! $recursive )
    81 			return @chmod($file, $mode);
   133 			return @chmod($file, $mode);
    82 		if ( ! $this->is_dir($file) )
   134 		if ( ! $this->is_dir($file) )
    87 		foreach ($filelist as $filename)
   139 		foreach ($filelist as $filename)
    88 			$this->chmod($file . $filename, $mode, $recursive);
   140 			$this->chmod($file . $filename, $mode, $recursive);
    89 
   141 
    90 		return true;
   142 		return true;
    91 	}
   143 	}
       
   144 	/**
       
   145 	 * Changes file owner
       
   146 	 *
       
   147 	 * @param $file string Path to the file.
       
   148 	 * @param $owner mixed A user name or number.
       
   149 	 * @param $recursive bool (optional) If set True changes file owner recursivly. Defaults to False.
       
   150 	 * @return bool Returns true on success or false on failure.
       
   151 	 */
    92 	function chown($file, $owner, $recursive = false) {
   152 	function chown($file, $owner, $recursive = false) {
    93 		if ( ! $this->exists($file) )
   153 		if ( ! $this->exists($file) )
    94 			return false;
   154 			return false;
    95 		if ( ! $recursive )
   155 		if ( ! $recursive )
    96 			return @chown($file, $owner);
   156 			return @chown($file, $owner);
    97 		if ( ! $this->is_dir($file) )
   157 		if ( ! $this->is_dir($file) )
    98 			return @chown($file, $owner);
   158 			return @chown($file, $owner);
    99 		//Is a directory, and we want recursive
   159 		//Is a directory, and we want recursive
   100 		$filelist = $this->dirlist($file);
   160 		$filelist = $this->dirlist($file);
   101 		foreach ($filelist as $filename){
   161 		foreach ($filelist as $filename) {
   102 			$this->chown($file . '/' . $filename, $owner, $recursive);
   162 			$this->chown($file . '/' . $filename, $owner, $recursive);
   103 		}
   163 		}
   104 		return true;
   164 		return true;
   105 	}
   165 	}
       
   166 	/**
       
   167 	 * Gets file owner
       
   168 	 *
       
   169 	 * @param $file string Path to the file.
       
   170 	 * @return string Username of the user.
       
   171 	 */
   106 	function owner($file) {
   172 	function owner($file) {
   107 		$owneruid = @fileowner($file);
   173 		$owneruid = @fileowner($file);
   108 		if ( ! $owneruid )
   174 		if ( ! $owneruid )
   109 			return false;
   175 			return false;
   110 		if ( ! function_exists('posix_getpwuid') )
   176 		if ( ! function_exists('posix_getpwuid') )
   111 			return $owneruid;
   177 			return $owneruid;
   112 		$ownerarray = posix_getpwuid($owneruid);
   178 		$ownerarray = posix_getpwuid($owneruid);
   113 		return $ownerarray['name'];
   179 		return $ownerarray['name'];
   114 	}
   180 	}
       
   181 	/**
       
   182 	 * Gets file permissions
       
   183 	 *
       
   184 	 * FIXME does not handle errors in fileperms()
       
   185 	 *
       
   186 	 * @param $file string Path to the file.
       
   187 	 * @return string Mode of the file (last 4 digits).
       
   188 	 */
   115 	function getchmod($file) {
   189 	function getchmod($file) {
   116 		return substr(decoct(@fileperms($file)),3);
   190 		return substr(decoct(@fileperms($file)),3);
   117 	}
   191 	}
   118 	function group($file) {
   192 	function group($file) {
   119 		$gid = @filegroup($file);
   193 		$gid = @filegroup($file);
   131 		return copy($source, $destination);
   205 		return copy($source, $destination);
   132 	}
   206 	}
   133 
   207 
   134 	function move($source, $destination, $overwrite = false) {
   208 	function move($source, $destination, $overwrite = false) {
   135 		//Possible to use rename()?
   209 		//Possible to use rename()?
   136 		if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){
   210 		if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ) {
   137 			$this->delete($source);
   211 			$this->delete($source);
   138 			return true;
   212 			return true;
   139 		} else {
   213 		} else {
   140 			return false;
   214 			return false;
   141 		}
   215 		}
   195 	}
   269 	}
   196 	function size($file) {
   270 	function size($file) {
   197 		return @filesize($file);
   271 		return @filesize($file);
   198 	}
   272 	}
   199 
   273 
   200 	function touch($file, $time = 0, $atime = 0){
   274 	function touch($file, $time = 0, $atime = 0) {
   201 		if ($time == 0)
   275 		if ($time == 0)
   202 			$time = time();
   276 			$time = time();
   203 		if ($atime == 0)
   277 		if ($atime == 0)
   204 			$atime = time();
   278 			$atime = time();
   205 		return @touch($file, $time, $atime);
   279 		return @touch($file, $time, $atime);
   206 	}
   280 	}
   207 
   281 
   208 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
   282 	function mkdir($path, $chmod = false, $chown = false, $chgrp = false) {
       
   283 		if ( ! $chmod )
       
   284 			$chmod = FS_CHMOD_DIR;
       
   285 
   209 		if ( ! @mkdir($path) )
   286 		if ( ! @mkdir($path) )
   210 			return false;
   287 			return false;
   211 		$this->chmod($path, $chmod);
   288 		$this->chmod($path, $chmod);
   212 		if ( $chown )
   289 		if ( $chown )
   213 			$this->chown($path, $chown);
   290 			$this->chown($path, $chown);
   228 			@rmdir($filename);
   305 			@rmdir($filename);
   229 		}
   306 		}
   230 		return @rmdir($path);
   307 		return @rmdir($path);
   231 	}
   308 	}
   232 
   309 
   233 	function dirlist($path, $incdot = false, $recursive = false) {
   310 	function dirlist($path, $include_hidden = true, $recursive = false) {
   234 		if ( $this->is_file($path) ) {
   311 		if ( $this->is_file($path) ) {
   235 			$limitFile = basename($path);
   312 			$limit_file = basename($path);
   236 			$path = dirname($path);
   313 			$path = dirname($path);
   237 		} else {
   314 		} else {
   238 			$limitFile = false;
   315 			$limit_file = false;
   239 		}
   316 		}
       
   317 
   240 		if ( ! $this->is_dir($path) )
   318 		if ( ! $this->is_dir($path) )
   241 			return false;
   319 			return false;
   242 
   320 
   243 		$ret = array();
       
   244 		$dir = @dir($path);
   321 		$dir = @dir($path);
   245 		if ( ! $dir )
   322 		if ( ! $dir )
   246 			return false;
   323 			return false;
       
   324 
       
   325 		$ret = array();
       
   326 
   247 		while (false !== ($entry = $dir->read()) ) {
   327 		while (false !== ($entry = $dir->read()) ) {
   248 			$struc = array();
   328 			$struc = array();
   249 			$struc['name'] = $entry;
   329 			$struc['name'] = $entry;
   250 
   330 
   251 			if ( '.' == $struc['name'] || '..' == $struc['name'] )
   331 			if ( '.' == $struc['name'] || '..' == $struc['name'] )
   252 				continue; //Do not care about these folders.
       
   253 			if ( '.' == $struc['name'][0] && !$incdot)
       
   254 				continue;
   332 				continue;
   255 			if ( $limitFile && $struc['name'] != $limitFile)
   333 
       
   334 			if ( ! $include_hidden && '.' == $struc['name'][0] )
       
   335 				continue;
       
   336 
       
   337 			if ( $limit_file && $struc['name'] != $limit_file)
   256 				continue;
   338 				continue;
   257 
   339 
   258 			$struc['perms'] 	= $this->gethchmod($path.'/'.$entry);
   340 			$struc['perms'] 	= $this->gethchmod($path.'/'.$entry);
   259 			$struc['permsn']	= $this->getnumchmodfromh($struc['perms']);
   341 			$struc['permsn']	= $this->getnumchmodfromh($struc['perms']);
   260 			$struc['number'] 	= false;
   342 			$struc['number'] 	= false;
   266 			$struc['time']    	= date('h:i:s',$struc['lastmodunix']);
   348 			$struc['time']    	= date('h:i:s',$struc['lastmodunix']);
   267 			$struc['type']		= $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
   349 			$struc['type']		= $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
   268 
   350 
   269 			if ( 'd' == $struc['type'] ) {
   351 			if ( 'd' == $struc['type'] ) {
   270 				if ( $recursive )
   352 				if ( $recursive )
   271 					$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
   353 					$struc['files'] = $this->dirlist($path . '/' . $struc['name'], $include_hidden, $recursive);
   272 				else
   354 				else
   273 					$struc['files'] = array();
   355 					$struc['files'] = array();
   274 			}
   356 			}
   275 
   357 
   276 			$ret[ $struc['name'] ] = $struc;
   358 			$ret[ $struc['name'] ] = $struc;