1 | <?php
|
---|
2 | /**
|
---|
3 | * WordPress Direct Filesystem.
|
---|
4 | *
|
---|
5 | * @package WordPress
|
---|
6 | * @subpackage Filesystem
|
---|
7 | */
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * WordPress Filesystem Class for direct PHP file and folder manipulation.
|
---|
11 | *
|
---|
12 | * @since 2.5
|
---|
13 | * @package WordPress
|
---|
14 | * @subpackage Filesystem
|
---|
15 | * @uses WP_Filesystem_Base Extends class
|
---|
16 | */
|
---|
17 | class WP_Filesystem_Direct extends WP_Filesystem_Base {
|
---|
18 | var $permission = null;
|
---|
19 | var $errors = null;
|
---|
20 | function WP_Filesystem_Direct($arg) {
|
---|
21 | $this->method = 'direct';
|
---|
22 | $this->errors = new WP_Error();
|
---|
23 | }
|
---|
24 | function connect() {
|
---|
25 | return true;
|
---|
26 | }
|
---|
27 | function setDefaultPermissions($perm) {
|
---|
28 | $this->permission = $perm;
|
---|
29 | }
|
---|
30 | function get_contents($file) {
|
---|
31 | return @file_get_contents($file);
|
---|
32 | }
|
---|
33 | function get_contents_array($file) {
|
---|
34 | return @file($file);
|
---|
35 | }
|
---|
36 | function put_contents($file, $contents, $mode = false, $type = '') {
|
---|
37 | if ( ! ($fp = @fopen($file, 'w' . $type)) )
|
---|
38 | return false;
|
---|
39 | @fwrite($fp, $contents);
|
---|
40 | @fclose($fp);
|
---|
41 | $this->chmod($file,$mode);
|
---|
42 | return true;
|
---|
43 | }
|
---|
44 | function cwd() {
|
---|
45 | return @getcwd();
|
---|
46 | }
|
---|
47 | function chdir($dir) {
|
---|
48 | return @chdir($dir);
|
---|
49 | }
|
---|
50 | function chgrp($file, $group, $recursive = false) {
|
---|
51 | if ( ! $this->exists($file) )
|
---|
52 | return false;
|
---|
53 | if ( ! $recursive )
|
---|
54 | return @chgrp($file, $group);
|
---|
55 | if ( ! $this->is_dir($file) )
|
---|
56 | return @chgrp($file, $group);
|
---|
57 | //Is a directory, and we want recursive
|
---|
58 | $file = trailingslashit($file);
|
---|
59 | $filelist = $this->dirlist($file);
|
---|
60 | foreach ($filelist as $filename)
|
---|
61 | $this->chgrp($file . $filename, $group, $recursive);
|
---|
62 |
|
---|
63 | return true;
|
---|
64 | }
|
---|
65 | function chmod($file, $mode = false, $recursive = false) {
|
---|
66 | if ( ! $this->exists($file) )
|
---|
67 | return false;
|
---|
68 |
|
---|
69 | if ( ! $mode ) {
|
---|
70 | if ( $this->permission )
|
---|
71 | $mode = $this->permission;
|
---|
72 | elseif ( $this->is_file($file) )
|
---|
73 | $mode = FS_CHMOD_FILE;
|
---|
74 | elseif ( $this->is_dir($file) )
|
---|
75 | $mode = FS_CHMOD_DIR;
|
---|
76 | else
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 |
|
---|
80 | if ( ! $recursive )
|
---|
81 | return @chmod($file, $mode);
|
---|
82 | if ( ! $this->is_dir($file) )
|
---|
83 | return @chmod($file, $mode);
|
---|
84 | //Is a directory, and we want recursive
|
---|
85 | $file = trailingslashit($file);
|
---|
86 | $filelist = $this->dirlist($file);
|
---|
87 | foreach ($filelist as $filename)
|
---|
88 | $this->chmod($file . $filename, $mode, $recursive);
|
---|
89 |
|
---|
90 | return true;
|
---|
91 | }
|
---|
92 | function chown($file, $owner, $recursive = false) {
|
---|
93 | if ( ! $this->exists($file) )
|
---|
94 | return false;
|
---|
95 | if ( ! $recursive )
|
---|
96 | return @chown($file, $owner);
|
---|
97 | if ( ! $this->is_dir($file) )
|
---|
98 | return @chown($file, $owner);
|
---|
99 | //Is a directory, and we want recursive
|
---|
100 | $filelist = $this->dirlist($file);
|
---|
101 | foreach ($filelist as $filename){
|
---|
102 | $this->chown($file . '/' . $filename, $owner, $recursive);
|
---|
103 | }
|
---|
104 | return true;
|
---|
105 | }
|
---|
106 | function owner($file) {
|
---|
107 | $owneruid = @fileowner($file);
|
---|
108 | if ( ! $owneruid )
|
---|
109 | return false;
|
---|
110 | if ( ! function_exists('posix_getpwuid') )
|
---|
111 | return $owneruid;
|
---|
112 | $ownerarray = posix_getpwuid($owneruid);
|
---|
113 | return $ownerarray['name'];
|
---|
114 | }
|
---|
115 | function getchmod($file) {
|
---|
116 | return substr(decoct(@fileperms($file)),3);
|
---|
117 | }
|
---|
118 | function group($file) {
|
---|
119 | $gid = @filegroup($file);
|
---|
120 | if ( ! $gid )
|
---|
121 | return false;
|
---|
122 | if ( ! function_exists('posix_getgrgid') )
|
---|
123 | return $gid;
|
---|
124 | $grouparray = posix_getgrgid($gid);
|
---|
125 | return $grouparray['name'];
|
---|
126 | }
|
---|
127 |
|
---|
128 | function copy($source, $destination, $overwrite = false) {
|
---|
129 | if ( ! $overwrite && $this->exists($destination) )
|
---|
130 | return false;
|
---|
131 | return copy($source, $destination);
|
---|
132 | }
|
---|
133 |
|
---|
134 | function move($source, $destination, $overwrite = false) {
|
---|
135 | //Possible to use rename()?
|
---|
136 | if ( $this->copy($source, $destination, $overwrite) && $this->exists($destination) ){
|
---|
137 | $this->delete($source);
|
---|
138 | return true;
|
---|
139 | } else {
|
---|
140 | return false;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | function delete($file, $recursive = false) {
|
---|
145 | if ( empty($file) ) //Some filesystems report this as /, which can cause non-expected recursive deletion of all files in the filesystem.
|
---|
146 | return false;
|
---|
147 | $file = str_replace('\\', '/', $file); //for win32, occasional problems deleteing files otherwise
|
---|
148 |
|
---|
149 | if ( $this->is_file($file) )
|
---|
150 | return @unlink($file);
|
---|
151 | if ( ! $recursive && $this->is_dir($file) )
|
---|
152 | return @rmdir($file);
|
---|
153 |
|
---|
154 | //At this point its a folder, and we're in recursive mode
|
---|
155 | $file = trailingslashit($file);
|
---|
156 | $filelist = $this->dirlist($file, true);
|
---|
157 |
|
---|
158 | $retval = true;
|
---|
159 | if ( is_array($filelist) ) //false if no files, So check first.
|
---|
160 | foreach ($filelist as $filename => $fileinfo)
|
---|
161 | if ( ! $this->delete($file . $filename, $recursive) )
|
---|
162 | $retval = false;
|
---|
163 |
|
---|
164 | if ( file_exists($file) && ! @rmdir($file) )
|
---|
165 | $retval = false;
|
---|
166 | return $retval;
|
---|
167 | }
|
---|
168 |
|
---|
169 | function exists($file) {
|
---|
170 | return @file_exists($file);
|
---|
171 | }
|
---|
172 |
|
---|
173 | function is_file($file) {
|
---|
174 | return @is_file($file);
|
---|
175 | }
|
---|
176 |
|
---|
177 | function is_dir($path) {
|
---|
178 | return @is_dir($path);
|
---|
179 | }
|
---|
180 |
|
---|
181 | function is_readable($file) {
|
---|
182 | return @is_readable($file);
|
---|
183 | }
|
---|
184 |
|
---|
185 | function is_writable($file) {
|
---|
186 | return @is_writable($file);
|
---|
187 | }
|
---|
188 |
|
---|
189 | function atime($file) {
|
---|
190 | return @fileatime($file);
|
---|
191 | }
|
---|
192 |
|
---|
193 | function mtime($file) {
|
---|
194 | return @filemtime($file);
|
---|
195 | }
|
---|
196 | function size($file) {
|
---|
197 | return @filesize($file);
|
---|
198 | }
|
---|
199 |
|
---|
200 | function touch($file, $time = 0, $atime = 0){
|
---|
201 | if ($time == 0)
|
---|
202 | $time = time();
|
---|
203 | if ($atime == 0)
|
---|
204 | $atime = time();
|
---|
205 | return @touch($file, $time, $atime);
|
---|
206 | }
|
---|
207 |
|
---|
208 | function mkdir($path, $chmod = false, $chown = false, $chgrp = false){
|
---|
209 | if ( ! @mkdir($path) )
|
---|
210 | return false;
|
---|
211 | $this->chmod($path, $chmod);
|
---|
212 | if ( $chown )
|
---|
213 | $this->chown($path, $chown);
|
---|
214 | if ( $chgrp )
|
---|
215 | $this->chgrp($path, $chgrp);
|
---|
216 | return true;
|
---|
217 | }
|
---|
218 |
|
---|
219 | function rmdir($path, $recursive = false) {
|
---|
220 | //Currently unused and untested, Use delete() instead.
|
---|
221 | if ( ! $recursive )
|
---|
222 | return @rmdir($path);
|
---|
223 | //recursive:
|
---|
224 | $filelist = $this->dirlist($path);
|
---|
225 | foreach ($filelist as $filename => $det) {
|
---|
226 | if ( '/' == substr($filename, -1, 1) )
|
---|
227 | $this->rmdir($path . '/' . $filename, $recursive);
|
---|
228 | @rmdir($filename);
|
---|
229 | }
|
---|
230 | return @rmdir($path);
|
---|
231 | }
|
---|
232 |
|
---|
233 | function dirlist($path, $incdot = false, $recursive = false) {
|
---|
234 | if ( $this->is_file($path) ) {
|
---|
235 | $limitFile = basename($path);
|
---|
236 | $path = dirname($path);
|
---|
237 | } else {
|
---|
238 | $limitFile = false;
|
---|
239 | }
|
---|
240 | if ( ! $this->is_dir($path) )
|
---|
241 | return false;
|
---|
242 |
|
---|
243 | $ret = array();
|
---|
244 | $dir = @dir($path);
|
---|
245 | if ( ! $dir )
|
---|
246 | return false;
|
---|
247 | while (false !== ($entry = $dir->read()) ) {
|
---|
248 | $struc = array();
|
---|
249 | $struc['name'] = $entry;
|
---|
250 |
|
---|
251 | if ( '.' == $struc['name'] || '..' == $struc['name'] )
|
---|
252 | continue; //Do not care about these folders.
|
---|
253 | if ( '.' == $struc['name'][0] && !$incdot)
|
---|
254 | continue;
|
---|
255 | if ( $limitFile && $struc['name'] != $limitFile)
|
---|
256 | continue;
|
---|
257 |
|
---|
258 | $struc['perms'] = $this->gethchmod($path.'/'.$entry);
|
---|
259 | $struc['permsn'] = $this->getnumchmodfromh($struc['perms']);
|
---|
260 | $struc['number'] = false;
|
---|
261 | $struc['owner'] = $this->owner($path.'/'.$entry);
|
---|
262 | $struc['group'] = $this->group($path.'/'.$entry);
|
---|
263 | $struc['size'] = $this->size($path.'/'.$entry);
|
---|
264 | $struc['lastmodunix']= $this->mtime($path.'/'.$entry);
|
---|
265 | $struc['lastmod'] = date('M j',$struc['lastmodunix']);
|
---|
266 | $struc['time'] = date('h:i:s',$struc['lastmodunix']);
|
---|
267 | $struc['type'] = $this->is_dir($path.'/'.$entry) ? 'd' : 'f';
|
---|
268 |
|
---|
269 | if ( 'd' == $struc['type'] ) {
|
---|
270 | if ( $recursive )
|
---|
271 | $struc['files'] = $this->dirlist($path . '/' . $struc['name'], $incdot, $recursive);
|
---|
272 | else
|
---|
273 | $struc['files'] = array();
|
---|
274 | }
|
---|
275 |
|
---|
276 | $ret[ $struc['name'] ] = $struc;
|
---|
277 | }
|
---|
278 | $dir->close();
|
---|
279 | unset($dir);
|
---|
280 | return $ret;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | ?>
|
---|