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