-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFSnode.php
More file actions
executable file
·685 lines (642 loc) · 35.1 KB
/
FSnode.php
File metadata and controls
executable file
·685 lines (642 loc) · 35.1 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
<?php
/****************** DO NOT REMOVE OR ALTER THIS HEADER ******************************
* *
* Product: FSnode *
* FSnode is a node-based Uniform FileSystem handler, written in PHP. It allows *
* you to access all kind of file systems in the exact same manner, with the same *
* simple commands. You can write your web application once and let users switch *
* file system/platform; mount through URI. *
* *
* Latest version to download: *
* https://github.com/sentfanwyaerda/FSnode *
* *
* Documentation: *
* http://sent.wyaerda.org/FSnode/ *
* https://github.com/sentfanwyaerda/FSnode/blob/master/manual/Introduction.md *
* *
* Authors: *
* Sent fan Wyærda (fsnode@sent.wyaerda.org) [creator, main] *
* *
* License: cc-by-nd *
* Creative Commons, Attribution-No Derivative Works 3.0 Unported *
* http://creativecommons.org/licenses/by-nd/3.0/ *
* http://creativecommons.org/licenses/by-nd/3.0/legalcode *
* *
****************** CHANGES IN THE CODE ARE AT OWN RISK *****************************/
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'FSnode.settings.php');
if(!class_exists('Xnode')){
class Xnode { }
}
#for use of hooks:
define('PREFIX', 'prefix');
define('POSTFIX', 'postfix');
define('ITTERATION', 'itteration');
define('FAIL', 'fail');
define('MIXED', 'mixed');
if(!defined('FSnode_ALLOW_CODE_EXECUTE')){ define('FSnode_ALLOW_CODE_EXECUTE', FALSE); }
if(!defined('FSnode_DEBUG')){ define('FSnode_DEBUG', FALSE); }
function FSnode($a=NULL, $b=FALSE, $c=FALSE, $d=FALSE){
$URI = FSnode::parse_url($a);
if(is_array($URI) && count($URI) >= 2){ #assume $a is an URI with more then only a path, then $b is (bool ? get result/document : connected $FSnode object). Ignore $c and $d.
$fs = FSnode::URI_load($a);
if(is_bool($b)){ return ($b ? $fs->read() : $fs); }
else{ return /*error*/ FALSE; }
}
}
class FSnode extends Xnode {
public function Version($f=FALSE){ return '0.4.2'; }
public function Product_url($u=FALSE){ return ($u === TRUE ? "https://github.com/sentfanwyaerda/FSnode" : "http://sent.wyaerda.org/FSnode/?version=".self::Version(TRUE).'&license='.str_replace(' ', '+', self::License()) );}
public function Product($full=FALSE){ return "FSnode".(!($full===FALSE) ? (is_array($full) ? '(extended with '.preg_replace('#(, )([A-Z]+)$#i', ' and \\2', strtoupper(implode(', ', self::list_FSnode_extensions()))).') ' : NULL)." ".self::version(TRUE).(class_exists('Xnode') && method_exists('Xnode', 'Product') ? '/'.Xnode::Product(TRUE) : NULL) : NULL); }
public function License($with_link=FALSE){ return ($with_link ? '<a href="'.self::License_url().'">' : NULL).'cc-by-nd 3.0'.($with_link ? '</a>' : NULL); }
public function License_url(){ return 'http://creativecommons.org/licenses/by-nd/3.0/'; }
public function Product_base(){ return dirname(__FILE__).DIRECTORY_SEPARATOR; }
public function Product_file($full=FALSE){ return ($full ? self::Product_base() : NULL).basename(__FILE__); }
public function AutoUpdate(){
if(FSnode_ALLOW_CODE_EXECUTE){
$fsnode = FSnode('file:'.FSnode::Product_base());
$fsnode->add_hook('git');
#backup FSnode.settings.php
$fsnode->refresh('origin');
#restore FSnode.settings.php
}
else{
return FALSE;
}
}
function FSnode($a=NULL, $b=NULL, $c=NULL, $d=NULL){
self::_initialize($a, $b, $c, $d);
}
private function _initialize($a=NULL, $b=NULL, $c=NULL, $d=NULL){
#This will do some magic, later on
}
private /*array*/ function _allowed_methods(){
return array('chmod','chgrp','chown','copy','delete','disk_free_space','disk_total_space','file_exists','file_get_contents','file_put_contents','file','fileatime','filectime','filegroup','fileinode','filemtime','fileowner','fileperms','filesize','filetype','is_dir','is_executable','is_file','is_readable','is_writable','is_writeable','mkdir','rename','rndir','stat','touch','unlink','read','write');
}
public /*bool*/ function method_exists($str=NULL){ return (in_array(strtolower($str), self::_allowed_methods() )); }
public /*string*/ function __toString(){ return (string) NULL; }
private $hooks = array();
private function _hook($_m_, $vars=array(), $placeholder='default', $add_default=FALSE){
$bool = TRUE;
#use like: $this->_hook(__METHOD__, $args, PREFIX);
$placeholder = strtolower($placeholder); if(!in_array($placeholder, array('default','prefix','postfix','itteration','fail') )){ $placeholder = 'default'; }
$method = $placeholder.'_hook_'.strtolower($_m_);
$def_method = 'default_hook_'.strtolower($_m_);
foreach($this->hooks as $h){
$hook = 'FSnode_'.$h;
if(class_exists($hook) && method_exists($hook, $method)){ $bool = ( $bool && $hook::$method($vars) ); }
if(!($add_default===FALSE) && $placeholder != 'default' && class_exists($hook) && method_exists($hook, $def_method )){ $bool = ( $bool && $hook::$def_method($vars) ); }
}
return $bool;
}
public function add_hook($hook, $auto_load=FALSE){
if(!($auto_load===FALSE) && !class_exists( 'FSnode_'.$hook )){ self::load_FSnode_extension($hook); }
if(class_exists( 'FSnode_'.$hook) && !in_array($hook, $this->hooks)){ $this->hooks[] = (string) $hook; return TRUE; }
else{ return FALSE; }
}
public function load_extension($ext=FALSE){ return self::load_FSnode_extension($ext); }
public function load_FSnode_extension($ext=FALSE){
if($ext === TRUE){ /*load all extensions*/
foreach(scandir(FSnode_EXTENSION_DIRECTORY) as $f){
if(!in_array($f, array('.', '..', 'all.php')) && preg_match("#(.*).php$#i", $f, $buffer)){
FSnode::load_FSnode_extension($buffer[1]);
}
}
}
elseif($ext === FALSE){ return FALSE; }
elseif(is_array($ext)){
$bool = TRUE;
foreach($ext as $buffer){
$bool = ($bool && FSnode::load_FSnode_extension($buffer) );
}
return $bool;
}
else{
$p = FSnode_EXTENSION_DIRECTORY.preg_replace("#[^a-z0-9_]#i", "", $ext).'.php';
if(file_exists($p)){
require_once($p);
} else{ return FALSE; }
}
return TRUE;
}
public /*array*/ function list_FSnode_extensions(){
$set = array();
$classlist = get_declared_classes();
foreach($classlist as $d){
if(preg_match("#^FSnode_(.*)$#i", $d, $buffer) && class_exists("FSnode_".strtolower($buffer[1])) && (defined('FSnode_'.strtoupper($buffer[1]).'_URI_PREFIX') || defined('FSnode_'.strtoupper($buffer[1]).'_SCHEME')) ){
$set[] = strtolower($buffer[1]);
}
}
return $set;
}
var /*private*/ $URI = NULL;
public /*FSnode*/ function URI_load($URI, $auto_connect=TRUE){
$extension = FSnode::get_FSnode_extension_by_URI($URI);
//*debug*/ print '<!-- FSnode extension of "'.$URI.'" is '.print_r($extension, TRUE).' -->'."\n";
if($extension === FALSE){ return FALSE; }
#if(isset($this)){ $o =& $this; } else {
$ext = "FSnode_".strtolower($extension);
$FSnode = new $ext();
#}
$FSnode->URI = $URI;
$hooks = FSnode::get_FSnode_hooks_by_URI($URI);
//*debug*/ print '<!-- FSnode hooks of "'.$URI.'" are '.print_r($hooks, TRUE).' -->'."\n";
if(is_array($hooks)){
foreach($hooks as $hook){ $FSnode->add_hook($hook); }
}
if($auto_connect === TRUE){ $FSnode->connect($URI); }
return $FSnode;
}
public /*string|FALSE*/ function get_FSnode_extension_by_URI($URI){
$set = self::parse_url($URI);
foreach(self::list_FSnode_extensions() as $extension){
if(defined('FSnode_'.strtoupper($extension).'_SCHEME') && isset($set['scheme'])){
$schemes = explode(' ', constant('FSnode_'.strtoupper($extension).'_SCHEME'));
$s = explode('+', $set['scheme']);
if(in_array(end($s), $schemes)){ return $extension; }
}
if(defined('FSnode_'.strtoupper($extension).'_URI_PREFIX')){
$pattern = explode(' ', constant('FSnode_'.strtoupper($extension).'_URI_PREFIX'));
foreach($pattern as $prefix){
if(preg_match("#^(".str_replace(array('\s', '\\'), array('[a-z]', '\\\\'), $prefix).")#i", $URI)){ return $extension; }
}
}
}
return FALSE;
}
public /*array*/ function get_FSnode_hooks_by_URI($URI){
$hooks = array();
$set = self::parse_url($URI);
if(isset($set['scheme'])){
$hooks = explode('+', $set['scheme']);
/*untested*/ array_pop($hooks);
}
return $hooks;
}
public function parse_url($url, $component=-1){
if(!($component === -1) && is_string($component) && defined('PHP_URL_'.strtoupper($component))){ $component = constant('PHP_URL_'.strtoupper($component));}
$cfext = array('zip',/*tar.*/'gz','tar','bz','rar','iso','gzip','7z'); #compressed file extensions
$buffer = explode('://', $url); $scheme = (isset($buffer[0]) && isset($buffer[1]) ? $buffer[0] : NULL);
if(preg_match("#[\.](".implode('|', $cfext).")[/]#i", $url)){ #in case you request a file from an compressed archive
$set = parse_url($url);
if(preg_match("#^((.*)[\.](".implode('|', $cfext)."))([/](.*))$#", $set['path'], $buffer)){
$set['fullpath'] = $set['path'];
$set['path'] = $buffer[1];
$set['path-query'] = $buffer[4];
if(!preg_match("#".$buffer[3]."#", $set['scheme'])){ $set['scheme'] = $buffer[3].'+'.$set['scheme']; }
$set['pathtype'] = 'archive';
}
//return $set;
}
elseif(substr($url, 0, 2) == '\\\\'){
$set = parse_url($url);
if(preg_match("#^//([^/]+)(.*)$#i", str_replace('\\', '/', $set['path']), $buffer)){
$set['scheme'] = 'smb';
$set['host'] = $buffer[1];
$set['path'] = /*str_replace('/', '\\',*/ $buffer[2] /*)*/;
}
}
elseif(preg_match("#^([0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}|localhost)$#i", $url)){ $set = array('host'=>$url); }
elseif(preg_match("#[:]#", $scheme)){
$set = parse_url(str_replace($scheme, str_replace(':', '+', $scheme), $url));
}
elseif($set = parse_url($url /*, $component*/)){ #in all regular cases #component will be selected by FSnode::parse_url not its PHP-counterpart
//return $set;
}
elseif(preg_match("#^([a-z0-9+.-]+)[\:]([/]+)([^\?\#]+)([\?]([^\#]+))?([\#](.*))?$#i", $url, $buffer_one)){ #in failures of php::parse_url(), like email-address-usernames, or 'postgres:///'
$set = array();
$set['scheme'] = $buffer_one[1];
$hierarchical_prefix = $buffer_one[2];
$hierarchical = $buffer_one[3];
#/*debug*/ $set['b_one'] = print_r($buffer_one, TRUE);
if(strlen($hierarchical_prefix) == 2){
preg_match("#^([^/]+)(.*)$#i", $hierarchical, $buffer_two);
$authority = $buffer_two[1];
#scenario user:pass@domain:port gets filtered by parse_url, assume scheme based authentication on central server with an (emailaddress) user.
switch($set['scheme']){
#extend for more cases!!!
case 'dropbox': $set['host'] = 'dropbox.com'; break;
case 'imap+gmail': case 'gmail': $set['host'] = 'gmail.com'; break;
default: #do nothing
}
if(preg_match("#^([^:]+)[:](.*)#i", $authority, $buffer_three) && /*is emailaddress*/ preg_match("#^([a-z0-9_-]+)@([a-z0-9.-]+)$#i", $buffer_three[1]) ){
$set['user'] = $buffer_three[1];
$set['pass'] = $buffer_three[2];
} else {
$set['user'] = $authority;
}
$set['path'] = $buffer_two[2];
}
elseif($set['scheme'] == 'postgres' && strlen($hierarchical_prefix) == 3){
$set['host'] = 'localhost';
$set['path'] = $hierarchical;
}
else{
$set['path'] = $hierarchical_prefix.$hierarchical;
}
if(isset($buffer_one[5])){ $set['query'] = $buffer_one[5]; }
if(isset($buffer_one[7])){ $set['fragment'] = $buffer_one[7]; }
//return $set;
}
else { return FALSE; }
/*fix*/ if(!isset($set) || !is_array($set)){ $set = array(); }
/*debug*/ $set = array_merge(array('original' => $url, 'pathtype' => NULL), $set);
#analyses of $set['path']:
if(isset($set['scheme']) && in_array($set['scheme'], array('mysql','postgres') )){ $set['pathtype'] = 'database'; }
elseif(preg_match("#[/\\\/]#i", $url)){ $set['pathtype'] = 'directory'; }
else { $set['pathtype'] = 'urn'; } #mixed,urn,directory,archive,database,email,..
$set['separator'] = ($set['pathtype'] == 'urn' ? ':' : '/');
switch($set['pathtype']){
case 'urn':
if(isset($set['path']) && preg_match("#^[".$set['separator']."]?(([^".$set['separator']."]+)[".$set['separator']."])?(.*)$#i", $set['path'], $buffer)){
if(isset($buffer[2]) && strlen($buffer[2]) > 0){ $set['namespace'] = $buffer[2]; }
/*redundancy*/ elseif(isset($set['scheme']) && $set['scheme'] != 'urn'){ $set['namespace'] = $set['scheme'];}
$set['resource'] = $buffer[3];
}
break;
case 'database':
if(preg_match("#^[".$set['separator']."]?([^".$set['separator']."]+)([".$set['separator']."](.*))?$#i", $set['path'], $buffer)){
$set['database'] = $buffer[1];
if(isset($buffer[3])){ $set['table'] = $buffer[3]; }
}
break;
default:
$set['pathtype'] = 'directory';
foreach(array('.zip','.tgz','.tar.gz','.bz','.7z') as $ext){
if(isset($set['path']) && preg_match("#".$ext."#i", $set['path'])){ $rawpath = preg_replace("#".$ext."#i", "×", $set['path']); $rawext = $ext; }
}
if(isset($rawpath) && preg_match("#^([^×]+)[×][".$set['separator']."]?(.*)$#i", $rawpath, $buffer)){
$set['archive'] = $buffer[1];
$set['archivetype'] = substr($rawext, 1);
$set['fullarchive'] = $set['archive'].$rawext;
#/*fix*/ if(substr($set['fullarchive'], 0,1) == $set['separator']){ $set['fullarchive'] = substr($set['fullarchive'],1); }
/*fix*/ if(substr($set['fullarchive'], -1) == $set['separator']){ $set['fullarchive'] = substr($set['fullarchive'],0,-1); }
$remainingpath = (isset($set['path-query']) ? $set['path-query'] : $buffer[2]);
$set['pathtype'] = 'archive';
}
/*fix*/ if(!isset($remainingpath)){ $remainingpath = (isset($set['path']) ? $set['path'] : NULL); }
/*fix*/ $remainingpath = str_replace('\\','/', $remainingpath);
if(substr($remainingpath, -1) == '/'){ $set['directory'] = substr($remainingpath, 0, -1); }
else{
$set['directory'] = dirname($remainingpath);
$set['filename'] = basename($remainingpath);
}
/*fix*/ if(isset($set['path']) && preg_match("#[\\\]#", $set['path'])){ $set['directory'] = str_replace('/', '\\', $set['directory']); $set['separator'] = '\\'; }
if(isset($set['filename']) && preg_match("#^(.*)[.]([^.]+)$#i", $set['filename'], $buffer)){
$set['filetype'] = $buffer[2]; #extension
//$set['filemime'] = #?
}
}
#analyses of $set['query']:
//$set['assigner'] = (=|:);
//$set['divider'] = (&|;)
//$set['masterdivider'] = ($set['pathtype'] == 'urn' ? ';' : '?');
if(isset($set['query'])){
parse_str($set['query'], $set['queryexpanded']);
$set['queryamount'] = count($set['queryexpanded']);
}
#analyses of $set['user']
//$set['anonymous'] = (yes|no);
//$set['emailaddress'] = (yes|no);
if($component === -1 || (is_array($component) && count($component) == 0) || !is_array($set)){ return $set; }
elseif(is_array($component)){
$subset = array();
foreach($component as $key){
switch($key){
case PHP_URL_SCHEME: $subset['scheme'] = (string) $set['scheme']; break;
case PHP_URL_HOST: $subset['host'] = (string) $set['host']; break;
case PHP_URL_PORT: $subset['port'] = (int) $set['port']; break;
case PHP_URL_USER: $subset['user'] = (string) $set['user']; break;
case PHP_URL_PASS: $subset['pass'] = (string) $set['pass']; break;
case PHP_URL_PATH: $subset['path'] = (string) $set['path']; break;
case PHP_URL_QUERY: $subset['query'] = (string) $set['query']; break;
case PHP_URL_FRAGMENT: $subset['fragment'] = (string) $set['fragment']; break;
default:
if(isset($set[$key])){ $subset[strtolower($key)] = $set[$key]; }
#else ignores $key
}
}
return $subset;
}
else{
/*fix*/ foreach(array('scheme','host','port','user','pass','path','query','fragment') as $c){ if(!isset($set[$c])){ $set[$c] = NULL; }}
if(isset($set[strtolower($component)])){ return $set[strtolower($component)]; }
switch($component){
case PHP_URL_SCHEME: return (string) $set['scheme']; break;
case PHP_URL_HOST: return (string) $set['host']; break;
case PHP_URL_PORT: return (int) $set['port']; break;
case PHP_URL_USER: return (string) $set['user']; break;
case PHP_URL_PASS: return (string) $set['pass']; break;
case PHP_URL_PATH: return (string) $set['path']; break;
case PHP_URL_QUERY: return (string) $set['query']; break;
case PHP_URL_FRAGMENT: return (string) $set['fragment']; break;
default:
if(isset($set[strtolower($component)])){ return $set[strtolower($component)]; }
}
}
return $set;
}
public function rebuild_url($arg=array()){
if(isset($arg['user']) && /*is_emailaddress*/ preg_match("#^[a-z0-9_-]+[@][a-z0-9.-]+$#i", $arg['user']) && in_array($arg['scheme'], array('dropbox','imap+gmail'))){
#extend for more cases!!!
$hierarchal = $arg['scheme'].'://'.$arg['user'].(isset($arg['pass']) ? ':'.$arg['pass'] : NULL).(substr($arg['path'], 0, 1) != '/' ? '/' : NULL).$arg['path'];
}
elseif(isset($arg['host'])){
$hierarchal = (isset($arg['scheme']) ? $arg['scheme'] : 'unknown').'://'.(isset($arg['user']) ? $arg['user'].(isset($arg['pass']) ? ':'.$arg['pass'] : NULL).'@' : NULL).$arg['host'].(isset($arg['port']) ? ':'.$arg['port'] : NULL).(isset($arg['path']) ? (substr($arg['path'], 0, 1) != '/' ? '/' : NULL).$arg['path'] : NULL).(isset($arg['path-query']) ? $arg['path-query'] : NULL);
}
else{ $hierarchal = (isset($arg['scheme']) ? $arg['scheme'].':' : NULL).$arg['path']; }
$str = $hierarchal.(isset($arg['query']) ? '?'.(is_array($arg['query']) ? http_build_query($arg['query']) : $arg['query']) : NULL).(isset($arg['fragment']) ? '#'.$arg['fragment'] : NULL);
return $str;
}
/* ignores the following Filesystem&Directory Functions ( http://php.net/manual/en/ref.filesystem.php & http://php.net/manual/en/ref.dir.php ) functions: basename, clearstatcace, dirname, diskfreespace*, fclose, feof, fflush, fgetc, fgetcsv, fgets, fgetss, flock, fnmatch, fopen, fpassthru, fputcsv, fputs, fread, fscanf, fseek, fstat, ftell, ftruncate, fwrite, glob, is_link, is_uploaded_file, lchgrp,, lchown, link, linkinfo, lstat, move_uploaded_file, parse_ini_file, parse_ini_string, pathinfo, pclose, popen, readfile, readlink, realpath_cache_get, realpath_cache_size, realpath, rewind, set_file_buffer, symlink, tempnam, umask & chdir, chroot, closedir, dir, getcwd, opendir, readdir, rewinddir */
#Filesystem Handlers
public /*bool*/ function chmod($filename, $mode){ return @chmod( $this->_filename_attach_prefix( (string) $filename ), (int) $mode ); }
public /*bool*/ function chgrp($filename, $group){ return chgrp( $this->_filename_attach_prefix( (string) $filename ), $group ); }
public /*bool*/ function chown($filename, $user){ return chown( $this->_filename_attach_prefix( (string) $filename ), $user ); }
public /*bool*/ function copy($source, $dest /*, (resource) $context */ ){ return copy( $this->_filename_attach_prefix( (string) $source ), $this->_filename_attach_prefix( (string) $dest ) /*, (resource) $context */ ); }
public /*float*/ function disk_free_space($directory=NULL){ if($directory === NULL){ $directory = './'; } return disk_free_space( $this->_filename_attach_prefix( (string) $directory ) ); }
public /*float*/ function disk_total_space($directory=NULL){ if($directory === NULL){ $directory = './'; } return disk_total_space( $this->_filename_attach_prefix( (string) $directory ) ); }
public /*bool*/ function file_exists($filename){ return file_exists( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*string*/ function file_get_contents($filename, $use_include_path=FALSE /*, (resource) $context, $offset=-1, $maxlen=FALSE */){ return file_get_contents( $this->_filename_attach_prefix( (string) $filename ), (bool) $use_include_path /*, (resource) $context, (int) $offset, (int) $maxlen */ ); }
public /*int*/ function file_put_contents($filename, $data, $flags=0 /*, (resource) $context */ ){ return file_put_contents( $this->_filename_attach_prefix( (string) $filename ), $data, (int) $flags /*, (resource) $context */ ); }
public /*array*/ function file($filename, $flags=0 /*, (resource) $context */ ){ return file( $this->_filename_attach_prefix( (string) $filename ), (int) $flags /*, (resource) $context */ ); }
public /*int*/ function fileatime($filename){ return fileatime( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*int*/ function filectime($filename){ return filectime( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*int*/ function filegroup($filename){ return filegroup( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*int*/ function fileinode($filename){ return fileinode( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*int*/ function filemtime($filename){ return filemtime( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*int*/ function fileowner($filename){ return fileowner( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*int*/ function fileperms($filename, $UNIX=FALSE){
$perms = fileperms( $this->_filename_attach_prefix( (string) $filename ) );
if($UNIX == TRUE){ return preg_replace("#^([0-7])([0-7]{3})$#", "\\1x\\2", substr(sprintf('%o', $perms), -4)); }
else{return '0x'.decoct($perms);}
}
public /*string*/ function filerights($filename, $UNIX=FALSE){
return self::fileperms2rights(octdec(self::fileperms($filename, $UNIX)));
}
public /*int*/ function filesize($filename){ return filesize( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*int*/ function filetype($filename){ return filetype( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*bool*/ function is_dir($filename){ return is_dir( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*bool*/ function is_executable($filename){ return is_executable( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*bool*/ function is_file($filename){ return is_file( $this->_filename_attach_prefix( (string) $filename ) ); }
#public /*bool*/ function is_link($filename){ return is_link( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*bool*/ function is_readable($filename){ return is_readable( $this->_filename_attach_prefix( (string) $filename ) ); }
#public /*bool*/ function is_uploaded_file($filename){ return is_uploaded_file( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*bool*/ function is_writable($filename){ return is_writable( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*bool*/ function is_writeable($filename){ return self::is_writable( $this->_filename_attach_prefix( (string) $filename ) ); } #alias
public /*bool*/ function is_connected(){ return FALSE; }
public /*bool*/ function mkdir($pathname, $mode=0777, $recursive=FALSE /*, (resource) $context */ ){ return @mkdir( $this->_filename_attach_prefix( (string) $pathname ), (int) $mode, (bool) $recursive /*, (resource) $context */ ); }
public /*bool*/ function rename($oldname, $newname /*, (resource) $context */ ){ return rename( $this->_filename_attach_prefix( (string) $oldname ), $this->_filename_attach_prefix( (string) $newname ) /*, (resource) $context */ ); }
public /*bool*/ function rmdir($dirname /*, (resource) $context */ ){ return rmdir( $this->_filename_attach_prefix( (string) $dirname ) /*, (resource) $context */ ); }
public /*array*/ function stat($filename){ return stat( $this->_filename_attach_prefix( (string) $filename ) ); }
public /*bool*/ function touch($filename, $time=NULL, $atime=0){
if($time === NULL){ $time = time(); }
if($atime === 0){ $atime = time(); }
return @touch( $this->_filename_attach_prefix( (string) $filename ), (int) $time, (int) $atime );
}
public /*bool*/ function unlink($filename /*, (resource) $context */ ){ return unlink( $this->_filename_attach_prefix( (string) $filename ) /*, (resource) $context */ ); }
#Directory Handlers
public /*directory*/ function scandir($directory=NULL, $sorting_order=SCANDIR_SORT_ASCENDING /*, (resource) $context */ ){
/*fix*/ if(!$this->is_dir('/')){ return array(); }
if($directory === NULL){ $directory = './'; }
$d = $this->_filename_attach_prefix( (string) $directory );
if($d && is_dir($d)){
return scandir( $d, (int) $sorting_order /*, (resource) $context */ );
} else { return array(); }
}
/***************************************
* self::realpath: /chroot/dir/ect/ory/file.ext
* self::realpath_URI: file:/chroot/dir/ect/ory/file.ext or ftp://user@server/dir/ect/ory/file.ext
* self::relativepath: ./dir/ect/ory/file.ext
***************************************/
public /*string*/ function realpath($filename=NULL){
$f = $this->_filename_attach_prefix( (string) $filename );
if( !($filename===NULL) && $f ){
$f = realpath( $f );
/*fix*/ $f = preg_replace('#[/]+#', '/', $f.($this->is_dir($f) ? DIRECTORY_SEPARATOR : NULL));
return $f;
} else{ #returns an empty $filename to be an error
return FALSE;
}
}
public /*string*/ function realpath_URI($filename=NULL){
$URI = $this->URI.'/'.$filename;
/*fix*/ $URI = preg_replace('#[/]+#', '/', $URI);
return $URI;
}
public /*string*/ function relativepath($URI=NULL){
return preg_replace('#[/]+#', '/', './'.$URI);
}
/*public|private*/ /*string*/ function _filename_attach_prefix($filename=NULL){
if(isset($this->URI)){
//*debug*/ print '<!-- '.$filename.' @('.$this->URI.') = ';
$chroot = self::parse_url($this->URI, 'path');
/*fix*/ if(substr($chroot, 0, 2) == './'){ $chroot = self::Product_base().substr($chroot, 2);}
if(file_exists($chroot) /*&& is_dir($chroot)*/){
/*fix*/ if(substr($chroot, -1) == '/'){ $chroot = substr($chroot, 0, -1); }
if(!is_dir($chroot)){ $chroot = dirname($chroot); }
if(!preg_match("#^".str_replace(array('[',']','(',')'), array('\[','\]','\(','\)'), $chroot)."#i", $filename)){ #check for prefix: do not double prefix
$filename = $chroot.(!preg_match("#^[/\\/]#i", $filename) ? DIRECTORY_SEPARATOR : NULL).$filename;
}
#/*debug*/ print "<!-- \n\t".$filename."\n=\t".realpath($chroot)."\n=\t".realpath($filename)."\n -->\n";
//if(!(substr(realpath($filename), 0, strlen(realpath($chroot))) == realpath($chroot))){ return FALSE; /*out of chroot*/ }
}
else{ return ($filename != '/' ? FALSE : $chroot); }
//*debug*/ print $filename.' -->';
}
return (string) $filename;
}
#Server Handlers
public /*bool*/ function close(){
$this->_hook(__METHOD__, array(), PREFIX, TRUE);
$this->_hook(__METHOD__, array(), POSTFIX);
return TRUE;
}
public /*bool*/ function connect($a=NULL, $b=NULL, $c=NULL, $d=NULL, $timeout=90, $secure=FALSE){
$this->_hook(__METHOD__, array(), PREFIX, TRUE);
//*debug*/ print 'CONNECT '.$this->URI.' ('.$this->_filename_attach_prefix( '/' ).') <b>'.(self::is_dir('/') && self::file_exists('/') ? 'exists' : 'unable')."</b>\n";
//*fix*/ if(!( self::is_dir('/') ) ){ self::mkdir('/'); }
$this->_hook(__METHOD__, array(), POSTFIX);
return TRUE;
}
#Basic
public /*string*/ function read($filename){
$this->_hook(__METHOD__, array('filename'=>$filename), PREFIX, TRUE);
$result = self::file_get_contents( $this->_filename_attach_prefix( (string) $filename ) );
$this->_hook(__METHOD__, array('filename'=>$filename, 'result'=>$result), POSTFIX);
return $result;
}
public /*int*/ function write($filename, $data){
$this->_hook(__METHOD__, array('filename'=>$filename, 'data'=>$data), PREFIX, TRUE);
$result = self::file_put_contents( $this->_filename_attach_prefix( (string) $filename ), $data);
$this->_hook(__METHOD__, array('filename'=>$filename, 'data'=>$data, 'result'=>$result), POSTFIX);
return $result;
}
public /*FSfile*/ function get($filename){
$FSfile = new FSfile(self::realpath_URI($filename), $this);
return $FSfile;
}
public /*bool*/ function put(/*(FSfile)*/ $FSfile, $filename=FALSE){
if(!is_object($FSfile) && get_class($FSfile) != 'FSfile'){ return /*error*/ FALSE; }
if($filename===FALSE){
return $this->write(self::relativepath($FSfile->URI), (string) $FSfile);
}
else{
return $this->write($filename, (string) $FSfile);
}
}
public /*bool*/ function delete($filename /*, (resource) $context */ ){
$this->_hook(__METHOD__, array('filename'=>$filename), PREFIX, TRUE);
if(!$this->file_exists($filename)){ $result = FALSE; }
if($this->is_dir($filename)){
$result = self::rmdir( (string) $filename );
}
else{
$result = self::unlink( (string) $filename /*, (resource) $context */ );
}
$this->_hook(__METHOD__, array('filename'=>$filename, 'result'=>$result), POSTFIX);
return $result;
}
public /*array*/ function scan($directory=NULL, $sorting_order=SCANDIR_SORT_ASCENDING){
$this->_hook(__METHOD__, array('directory'=>$directory, 'sorting_order'=>$sorting_order), PREFIX, TRUE);
$result = self::scandir($directory, $sorting_order);
$this->_hook(__METHOD__, array('directory'=>$directory, 'sorting_order'=>$sorting_order, 'result'=>$result), POSTFIX);
return $result;
}
#Basic extended
public /*mixed*/ function execute($line=NULL){
$this->_hook(__METHOD__, array('line'=>$line, 'data'=>$data), PREFIX, TRUE);
if(FSnode_ALLOW_CODE_EXECUTE){
exec($line);
}
$this->_hook(__METHOD__, array('line'=>$line, 'data'=>$data), POSTFIX);
}
public /*bool*/ function refresh($tag=NULL){
$this->_hook(__METHOD__, array('tag'=>$tag), PREFIX, TRUE);
$this->_hook(__METHOD__, array('tag'=>$tag), POSTFIX);
return TRUE;
}
public /*string*/ function mime_content_type($filename){
if(self::is_connected() && function_exists('finfo_open') && function_exists('finfo_file') && function_exists('finfo_close') ){
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $this->_filename_attach_prefix( (string) $filename ) );
finfo_close($finfo);
return $mime;
}
return NULL;
}
#Additional functionality
function rights2fileperms($rights=NULL){ //accepts strings like: "-rw-r--r--"
if(preg_match("#^([slbdcpu-])?([r-])([w-])([xsS-])([r-])([w-])([xsS-])([r-])([w-])([xtT-])$#", (string) $rights, $buffer)){
$perms = 0x0000;
if(isset($buffer[1])){
switch($buffer[1]){ //ignore/default: u
case 's': $perms += 0xC000; break;
case 'l': $perms += 0xA000; break;
case '-': $perms += 0x8000; break;
case 'b': $perms += 0x6000; break;
case 'd': $perms += 0x4000; break;
case 'c': $perms += 0x2000; break;
case 'p': $perms += 0x1000; break;
}
} else { $perms += 0x8000; }
if($buffer[2] == 'r'){ $perms += 0x0100; }
if($buffer[3] == 'w'){ $perms += 0x0080; }
switch($buffer[4]){ //ignore/default: -
case 's': $perms += 0x0040 + 0x0800; break;
case 'x': $perms += 0x0040; break;
case 'S': $perms += 0x0800; break;
}
if($buffer[5] == 'r'){ $perms += 0x0020; }
if($buffer[6] == 'w'){ $perms += 0x0010; }
switch($buffer[7]){ //ignore/default: -
case 's': $perms += 0x0008 + 0x0400; break;
case 'x': $perms += 0x0008; break;
case 'S': $perms += 0x0400; break;
}
if($buffer[8] == 'r'){ $perms += 0x0004; }
if($buffer[9] == 'w'){ $perms += 0x0002; }
switch($buffer[10]){ //ignore/default: -
case 't': $perms += 0x0001 + 0x0200; break;
case 'x': $perms += 0x0001; break;
case 'T': $perms += 0x0200; break;
}
return $perms; //decoct($perms);
}
else{ return decoct(0); }
}
function stroct($right=NULL, $asdec=FALSE){
return (int) $right;
//if(is_string($right) && preg_match("#^([0-7])x([0-7]+)$#", $right, $buffer)){ return ($asdec === FALSE ? decoct($buffer[1].$buffer[2]) : octdec($buffer[1].$buffer[2]) ); }
//return ($asdec === FALSE ? $right : octdec($right) );
}
function fileperms2rights($perms=NULL){
//source: http://php.net/manual/en/function.fileperms.php
if (($perms & 0xC000) == 0xC000) {
// Socket
$info = 's';
} elseif (($perms & 0xA000) == 0xA000) {
// Symbolic Link
$info = 'l';
} elseif (($perms & 0x8000) == 0x8000) {
// Regular
$info = '-';
} elseif (($perms & 0x6000) == 0x6000) {
// Block special
$info = 'b';
} elseif (($perms & 0x4000) == 0x4000) {
// Directory
$info = 'd';
} elseif (($perms & 0x2000) == 0x2000) {
// Character special
$info = 'c';
} elseif (($perms & 0x1000) == 0x1000) {
// FIFO pipe
$info = 'p';
} else {
// Unknown
$info = 'u';
}
// Owner
$info .= (($perms & 0x0100) ? 'r' : '-');
$info .= (($perms & 0x0080) ? 'w' : '-');
$info .= (($perms & 0x0040) ?
(($perms & 0x0800) ? 's' : 'x' ) :
(($perms & 0x0800) ? 'S' : '-'));
// Group
$info .= (($perms & 0x0020) ? 'r' : '-');
$info .= (($perms & 0x0010) ? 'w' : '-');
$info .= (($perms & 0x0008) ?
(($perms & 0x0400) ? 's' : 'x' ) :
(($perms & 0x0400) ? 'S' : '-'));
// World
$info .= (($perms & 0x0004) ? 'r' : '-');
$info .= (($perms & 0x0002) ? 'w' : '-');
$info .= (($perms & 0x0001) ?
(($perms & 0x0200) ? 't' : 'x' ) :
(($perms & 0x0200) ? 'T' : '-'));
return $info;
}
var $debugging = array();
/* how to use: self::log(NULL, __METHOD__, __FILE__.':'.__LINE__); */
function log($description=NULL, $method=__METHOD__, $location=NULL, $result=MIXED, $input=MIXED, $class=__CLASS__, $other=array()){
if(FSnode_DEBUG){
$timestamp = microtime(TRUE);
/*extra*/ $last = json_decode(end($this->debugging), TRUE); if(isset($last['timestamp'])){ $duration = ($timestamp - $last['timestamp']); }
$entry = array();
foreach(array('timestamp'=>0,'duration'=>0,'method'=>__METHOD__,'location'=>NULL,'description'=>NULL,'result'=>MIXED,'input'=>MIXED,'class'=>__CLASS__) as $key=>$defval){
switch($key){
case 'method': case 'class': case 'description': case 'location':
if(isset($$key) && $$key !== $defval && strlen($$key) > 0){ $entry[$key] = $$key; }
break;
default:
if(isset($$key) && $$key !== $defval){ $entry[$key] = $$key; }
}
}
if($other !== array()){ $entry['other'] = $other; }
$this->debugging[] = json_encode($entry);
}
}
}
/*To make sure FSnode_local is loaded*/
if(file_exists(FSnode_EXTENSION_DIRECTORY.'local.php')){ require_once(FSnode_EXTENSION_DIRECTORY.'local.php'); }
?>