/
gsutils.php
346 lines (291 loc) · 8.06 KB
/
gsutils.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
// Prevent this class from being included if another plugin
// has already loaded it
if (class_exists('GSUtils')) return;
class GSUtils {
// == CONSTANTS ==
const SDK_VERSION = 0.1;
const EXCEPTION_MKDIR = 'mkDirErr';
const EXCEPTION_RMDIR = 'rmDirErr';
const EXCEPTION_MKFILE = 'mkFileErr';
const EXCEPTION_PUTFILE = 'putFileErr';
const EXCEPTION_GETFILE = 'getFileErr';
const EXCEPTION_RMFILE = 'rmFileErr';
const EXCEPTION_MOVE = 'moveErr';
const EXCEPTION_MOVE_SOURCE = 'moveSrcErr';
const EXCEPTION_MOVE_DEST = 'moveDestErr';
// == PROPERTIES ==
protected $options;
protected $defaults = array(
'htaccess' => 'Deny from all',
);
// == METHODS ==
// Constructor
public function __construct($options = array()) {
$this->options = array_merge(array(
'basepath' => GSDATAOTHERPATH,
'adminurl' => $GLOBALS['SITEURL'] . $GLOBALS['GSADMIN'] . '/',
'pluginid' => null,
'siteurl' => $GLOBALS['SITEURL'],
), $options);
}
// Get a path (from the basepath)
public function path($path = null) {
if (strpos($path, $this->options['basepath']) !== 0) {
$path = $this->options['basepath'] . $path;
}
return $path;
}
// Get file info
public function fileinfo($file) {
$file = $this->path($file);
$info = pathinfo($file);
$info = array_merge(array(
'extension' => false,
), $info);
return $info;
}
// Make a directory
public function mkdir($path, $htaccess = true, $mode = 0755) {
$path = $this->path($path);
$mkdir = file_exists($path) || @mkdir($path, $mode, $recursive = true);
if ($mkdir) {
if ($htaccess === true) {
$htaccess = $this->defaults['htaccess'];
}
if ($htaccess) {
return $this->putfile($path . '/.htaccess', $htaccess);
} else {
return $mkdir;
}
} else {
throw new Exception(static::EXCEPTION_MKDIR);
}
}
// List files in a directory
public function scandir($path, $fullpath = true, $parents = false) {
$path = $this->path($path);
$files = scandir($path);
if (!$parents) {
$files = array_diff($files, array('.', '..'));
}
if ($fullpath) {
foreach ($files as $i => $file) {
$files[$i] = $path . '/' . $file;
}
}
return $files;
}
// Remove a directory
public function rmdir($path, $force = false) {
$path = $this->path($path);
if ($force) {
$files = $this->scandir($path);
foreach ($files as $file) {
if (is_dir($file)) {
$rmdir = $this->rmdir($file, true);
} else {
$rmdir = @unlink($file);
}
if (!$rmdir) {
throw new Exception(static::EXCEPTION_RMDIR);
}
}
}
$rmdir = @rmdir($path);
if ($rmdir) {
return $rmdir;
} else {
throw new Exception(static::EXCEPTION_RMDIR);
}
}
// Make a file
public function mkfile($file, $data) {
if ($this->exists($file)) {
throw new Exception(static::EXCEPTION_MKFILE);
} else {
return $this->putfile($file, $data);
}
}
// Put contents in a file
public function putfile($file, $data) {
$file = $this->path($file);
$info = $this->fileinfo($file);
if ($info['extension'] == 'json') {
$json = json_encode($data);
$putfile = @file_put_contents($file, $json);
} elseif ($info['extension'] == 'xml') {
// TODO
$putfile = false;
} else {
$putfile = @file_put_contents($file, $data);
}
if ($putfile) {
return $putfile;
} else {
throw new Exception(static::EXCEPTION_PUTFILE);
}
}
// Get file contents
public function getfile($file) {
$file = $this->path($file);
$info = $this->fileinfo($file);
if ($info['extension'] == 'json') {
$contents = @file_get_contents($file);
if ($contents !== false) {
$data = @json_decode($contents);
if ($data) {
$data = (array) $data;
}
} else {
$data = false;
}
} elseif ($info['extension'] == 'xml') {
// TODO
// Currently doesn't read xml files
$data = false;
} else {
$data = @file_get_contents($file);
}
if ($data !== false) {
return $data;
} else {
throw new Exception(static::EXCEPTION_GETFILE);
}
}
// Get a list of files
public function getfiles($query) {
$files = array();
if (is_string($query)) {
$path = $this->path($query);
$list = glob($path);
} elseif (is_array($query)) {
$list = $query;
}
foreach ($list as $file) {
$file = $this->path($file);
$files[$file] = $this->getfile($file);
}
return $files;
}
// Remove a file
public function rmfile($file) {
$file = $this->path($file);
$rmfile = @unlink($file);
if ($rmfile) {
return $rmfile;
} else {
throw new Exception(static::EXCEPTION_RMFILE);
}
}
// Move a directory/file
public function move($source, $dest) {
$source = $this->path($source);
$dest = $this->path($dest);
$sourceExists = $this->exists($source);
$destExists = $this->exists($dest);
if (!$sourceExists) {
throw new Exception(static::EXCEPTION_MOVE_SOURCE);
} elseif ($destExists) {
throw new Exception(static::EXCEPTION_MOVE_DEST);
} else {
$move = @rename($source, $dest);
if (!$move) {
throw new Exception(static::EXCEPTION_MOVE);
} else {
return $move;
}
}
}
// Copy a file/directory
public function copy($source, $dest, $permission = 0755) {
$source = $this->path($source);
$dest = $this->path($dest);
if (is_file($source)) {
$copy = @copy($source, $dest);
if (!$copy) {
throw new Exception(static::EXCEPTION_COPY);
}
} else {
$files = $this->scandir($source);
foreach ($files as $file) {
$f = '/' . basename($file);
if (!is_dir($file)) {
$copy = @copy($source . $f, $dest . $f);
if (!$copy) {
throw new Exception(static::EXCEPTION_COPY);
}
} else {
$mkdir = $this->mkdir($source . $f, $dest . $f);
$copy = $mkdir && $this->copy($source . $f, $dest . $f, $permission);
}
}
}
return $copy;
}
// File/folder exists
public function exists($resource) {
return file_exists($this->path($resource));
}
// Print
public function dump() {
$args = func_get_args();
echo '<pre><code>';
call_user_func_array('var_dump', $args);
echo '</code></pre>';
}
// Slug
public function slug($string, $default = 'temp') {
if (function_exists('prepareSlug')) {
// GS 3.4
return prepareSlug($string, $default);
} else {
$slug = $string;
// Need to implement
// Copied from slugging done on 3.4
//$slug = truncate($slug,GSFILENAMEMAX);
$slug = $this->translit($slug);
$slug = to7bit($slug, "UTF-8");
$slug = clean_url($slug); //old way @todo what does that mean ?
if(trim($slug) == '' && $default) return $default;
return $slug;
}
}
// Transliteration
public function translit() {
$args = func_get_args();
if (count($args) === 0) {
return i18n_r('TRANSLITERATION', null);
} else {
$string = $args[0];
if (function_exists('doTransliteration')) {
// GS 3.4
$string = doTransliteration($string);
} else {
$translit = $this->translit();
if (is_array($translit) && count($translit > 0)) {
$string = str_replace(array_keys($translit), array_values($translit), $string);
}
}
return $string;
}
}
// Clean strings
public function clean($string) {
return cl($string);
}
// Admin url
public function adminurl($path = null) {
$url = $this->options['adminurl'];
if ($this->options['pluginid']) {
$url = $url . 'load.php?id=' . $this->options['pluginid'];
}
if ($path && $this->options['pluginid']) {
$url = $url . '&';
}
return $url . $path;
}
public function siteurl($path = null) {
return $this->options['siteurl'] . $path;
}
}