-
Notifications
You must be signed in to change notification settings - Fork 1
/
Filesystem.php
executable file
·249 lines (189 loc) · 7.02 KB
/
Filesystem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php
/**
* PHP Version 5
* Helper for filesystem operations
* @package Polyfony
* @link https://github.com/SIB-FRANCE/Polyfony
* @license http://www.gnu.org/licenses/lgpl.txt GNU General Public License
* @note This program is distributed in the hope that it will be useful - WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*/
namespace Polyfony;
class Filesystem {
// this will restrict a path to the data storage folder
public static function chroot($path='/', $override = false) {
// if chrooting is enabled in the configuration
if(Config::get('filesystem', 'chroot') && !$override) {
// if the path already has the proper (ch)root
if(strpos($path, Config::get('filesystem', 'data_path')) === 0) {
// remove the root, remove double dots
$path = str_replace(array(Config::get('filesystem', 'data_path'), '..'), '', $path);
// re-add the root
return(Config::get('filesystem', 'data_path') . trim($path, '/') . '/');
}
// the path doesn't start with the (ch)root
else {
// remove all double dots and add the root path
return(Config::get('filesystem', 'data_path') . trim(str_replace('..','',$path), '/') . '/');
}
}
// return the path (altered or not)
return($path);
}
public static function isDirectory($path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// check if the path is a directory
return(is_dir($path));
}
public static function isFile($path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// check if the path is a file
return(is_file($path));
}
public static function isSymbolic($path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// check if it is symbolic
return(is_link($path));
}
public static function isWritable($path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// check if it is writable
return(is_writable($path));
}
public static function isNormalName($string) {
// check if the name starts with a dot
return((substr($string,0,1) != '.' ) ? true : false);
}
public static function exists($path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// check for the existence
return(file_exists($path));
}
public static function ls($path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// prepare the results
$filtered = array();
// clean the path and add a trailing slash
$path = trim($path, '/') . '/';
// scan the folder
$files_and_folders = scandir($path);
// for each found result
foreach($files_and_folders as $file_or_folder) {
// ignore filesystem internals
if(!self::isNormalName($file_or_folder)) {continue;}
// build the fullpath
$full_path = $path . $file_or_folder;
// add a trailing slash if it is a folder
$full_path .= self::isDirectory($full_path, $override_chroot) ? '/' : '';
// add the file or folder if it has passed the filter(s)
$filtered[$full_path] = $file_or_folder;
}
// return the filetered content
return($filtered);
}
public static function symlink($existing_file, $link, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$existing_file = self::chroot($existing_file, $override_chroot);
$link = self::chroot($link, $override_chroot);
// return false if the target does not exist
return(symlink($existing_file, $link));
}
public static function mkdir($path, $mask = 0777, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// if the file already exists
if(self::exists($path, $override_chroot) && self::isDirectory($path, $override_chroot)) {
// we also return true
return(true);
}
// return the creation status
return(mkdir($path, $mask, true));
}
public static function remove($path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// if path exists and is a file
return(unlink($path));
}
public static function get($path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// if the file or folder exists
return(file_get_contents($path));
}
public static function put($path, $content=null, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// put the content in a file
return(file_put_contents($path, $content));
}
public static function chmod($path, $mask, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// apply the chmod
return(chmod($path, $mask));
}
public static function chown($path, $user, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
}
public static function touch($path, $timestamp=null, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// touch it
return(touch($path, $timestamp));
}
public static function copy($source_path, $destination_path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$source_path = self::chroot($source_path, $override_chroot);
$destination_path = self::chroot($destination_path, $override_chroot);
// copy the file
return(copy($source_path, $destination_path));
}
public static function info($path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// return informations about the file
return(array(
'size' => filesize($path),
'modification' => filemtime($path),
'type' => self::type($path, $override_chroot)
));
}
public static function type($path, $override_chroot = false) {
// if chroot is enabled, restrict the path to the chroot
$path = self::chroot($path, $override_chroot);
// get a new fileinfo object
$info = new \finfo(FILEINFO_MIME);
// if the fileinfo failed to instanciate
if(!$info) {
// return an error
return(false);
}
// get the mimetype
$type = $info->file($path);
// if failed to get a type
if(!$type) {
// return an error
return(false);
}
// we got a mimetype
else {
// if it has a ; in it
if(strstr($type,';')) {
// only keep the first part
list($type) = explode(';',$type);
}
}
// return the type
return($type);
}
}
?>