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