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