This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
/
MetaStreamWrapper.php
634 lines (564 loc) · 18 KB
/
MetaStreamWrapper.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
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
<?php
/*
* Copyright 2007-2015 Abstrium <contact (at) pydio.com>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
namespace Pydio\Access\Core;
use Normalizer;
use Pydio\Access\Core\Filter\ContentFilter;
use Pydio\Access\Core\Model\AJXP_Node;
use Pydio\Core\Model\ContextInterface;
use Pydio\Core\PluginFramework\PluginsService;
use Pydio\Core\Services\RepositoryService;
defined('AJXP_EXEC') or die('Access not allowed');
/**
* Class MetaStreamWrapper
*
* Global streamWrapper that encapsulates all wrappers to access a driver's resource.
* Registered under the "pydio" protocol, it should replace all the old ajxp.XX direct calls.
* The static "appendMetaWrapper" method allows to add additional wrapper that will be sequentially called until
* reaching the driver ajxp.XX wrapper.
*/
class MetaStreamWrapper implements IAjxpWrapper
{
/**
* @var resource
*/
protected $handle;
/**
* @var string
*/
protected $currentDirPath;
protected static $metaWrappers = [
'core' => [
'pydio' => 'Pydio\Access\Core\MetaStreamWrapper'
]
];
protected static $cachedRepositoriesWrappers = array();
protected $currentUniquePath;
/**
* Register the stack of protocols/wrappers.
* @param null $registered_wrappers
*/
public static function register($registered_wrappers = null){
if($registered_wrappers == null){
$registered_wrappers = stream_get_wrappers();
}
$it = new \RecursiveIteratorIterator(new \RecursiveArrayIterator(self::$metaWrappers));
foreach($it as $protocol => $className){
if(!in_array($protocol, $registered_wrappers)){
stream_wrapper_register($protocol, $className);
}
}
}
/**
* Check if a repository accessDriver declares a streamWrapper, and eventually register it.
* @param AJXP_Node $node
* @param bool $register
* @param array|null $streams
* @param array|bool $streamData
* @return bool
*/
public static function detectWrapperForNode(AJXP_Node $node, $register = false, &$streams = null, &$streamData = false){
$driverInstance = $node->getDriver();
if(!empty($driverInstance) && $driverInstance instanceof AbstractAccessDriver){
$plugin = $driverInstance;
}
if(empty($plugin)) {
return false;
}
$streamData = $plugin->detectStreamWrapper($register);
if (!$register && $streamData !== false && is_array($streams)) {
$accessType = $node->getRepository()->getAccessType();
$streams[$accessType] = $accessType;
}
return ($streamData !== false);
}
/**
* Register an addition protocol/wrapper in the stack
* @param $name string
* @param $className string
*/
public static function appendMetaWrapper($name, $className, $parent = "core"){
self::$metaWrappers[$parent][$name] = $className;
self::register();
}
protected static function getMetaWrappers($scheme) {
return array_merge((array) self::$metaWrappers['core'], (array) self::$metaWrappers[$scheme]);
}
protected static function getNextScheme($url, $context='core'){
$parts = parse_url($url);
$metaWrapperKeys = array_keys(self::getMetaWrappers($context));
$key = array_search($parts["scheme"], $metaWrapperKeys);
if($key < count($metaWrapperKeys) - 1){
// Return next registered meta wrapper
return $metaWrapperKeys[$key + 1];
}else{
// Otherwise return repository wrapper
$data = self::actualRepositoryWrapperData(new AJXP_Node($url));
return $data["protocol"];
}
}
/**
* @param string $url
* @param MetaStreamWrapper $crtInstance
* @return string
* @throws \Exception
*/
public static function translateScheme($url, $crtInstance = null){
$node = new AJXP_Node($url);
$currentScheme = $node->getScheme();
$context = self::actualRepositoryWrapperProtocol($node);
$newScheme = self::getNextScheme($url, $context);
$repository = $node->getRepository();
if($currentScheme == "pydio" && $repository->hasContentFilter()){
$contentFilter = $repository->getContentFilter();
if ($contentFilter instanceof ContentFilter) {
$baseDir = $contentFilter->getBaseDir();
if ($crtInstance != null) {
$crtInstance->currentUniquePath = $contentFilter->getUniquePath();
}
if (!empty($baseDir) || $baseDir != "/") {
$crtPath = $node->getPath();
if(empty($crtPath)){
$crtPath = "/";
}
$crtBase = basename($crtPath);
if (!empty($crtPath) && $crtPath != "/" && $crtBase != $contentFilter->getUniquePath() && $crtBase != ".ajxp_meta") {
throw new \Exception("Cannot find file " . $crtBase);
}
$url = $node->getContext()->getUrlBase().rtrim($baseDir.$crtPath, "/");
}
}
}
$newUrl = str_replace($currentScheme."://", $newScheme."://", $url);
self::applyInitPathHook($newUrl, $context);
return $newUrl;
}
protected static function findWrapperClassName(ContextInterface $ctx, $scheme, $context = "core"){
$metaWrappers = self::getMetaWrappers($context);
if(isSet($metaWrappers[$scheme])){
$wrapper = $metaWrappers[$scheme];
}else{
$wrapper = PluginsService::getInstance($ctx)->getWrapperClassName($scheme);
}
if(empty($wrapper)) {
throw new \Exception("Cannot find any wrapper for the scheme " . $scheme . " in context " . $context);
}
return $wrapper;
}
protected static function findSubWrapperClassName($url){
$context = self::actualRepositoryWrapperProtocol(new AJXP_Node($url));
$nextScheme = self::getNextScheme($url, $context);
return self::findWrapperClassName(AJXP_Node::contextFromUrl($url), $nextScheme, $context);
}
/** @var $node AJXP_Node */
protected static function actualRepositoryWrapperData(AJXP_Node $node){
$repositoryId = $node->getRepositoryId();
if(isSet(self::$cachedRepositoriesWrappers[$repositoryId])){
return self::$cachedRepositoriesWrappers[$repositoryId];
}
$repository = RepositoryService::getRepositoryById($repositoryId);
if(!is_a($repository, "Repository")){
throw new \Exception("Cannot find repository with this id!");
}
if(self::detectWrapperForNode($node, false, $streams, $streamData)){
self::$cachedRepositoriesWrappers[$repositoryId] = $streamData;
return $streamData;
}else{
throw new \Exception("Repository does not provide a stream wrapper!");
}
}
/**
* Return the final ajxp.XX wrapper class name.
* @param $node AJXP_Node
* @return string mixed
* @throws \Exception
*/
public static function actualRepositoryWrapperClass($node){
$data = self::actualRepositoryWrapperData($node);
return $data["classname"];
}
/**
* Return the final ajxp.XX wrapper protocol.
* @param $node AJXP_Node
* @return string mixed
* @throws \Exception
*/
public static function actualRepositoryWrapperProtocol($node){
$data = self::actualRepositoryWrapperData($node);
return $data["protocol"];
}
/**
* Call Init function for a translated Path if defined
*
* @param string $path
*/
public static function applyInitPathHook($path, $context = 'core') {
$currentScheme = parse_url($path, PHP_URL_SCHEME);
$wrapper = self::findWrapperClassName(AJXP_Node::contextFromUrl($path), $currentScheme, $context);
if (is_callable(array($wrapper, "applyInitPathHook"))) {
call_user_func(array($wrapper, "applyInitPathHook"), $path);
}
}
/**
* Get a "usable" reference to a file : the real file or a tmp copy.
*
* @param string $path
* @param bool $persistent
* @return string
* @throws \Exception
*/
public static function getRealFSReference($path, $persistent = false)
{
$wrapper = self::findSubWrapperClassName($path);
return call_user_func(array($wrapper, "getRealFSReference"), self::translateScheme($path), $persistent);
}
/**
* Read a file (by chunks) and copy the data directly inside the given stream.
*
* @param string $path
* @param resource $stream
*/
public static function copyFileInStream($path, $stream)
{
$wrapper = self::findSubWrapperClassName($path);
call_user_func(array($wrapper, "copyFileInStream"), self::translateScheme($path), $stream);
}
/**
* Chmod implementation for this type of access.
*
* @param string $path
* @param number $chmodValue
*/
public static function changeMode($path, $chmodValue)
{
$wrapper = self::findSubWrapperClassName($path);
call_user_func(array($wrapper, "changeMode"), self::translateScheme($path), $chmodValue);
}
/**
* Describe whether the current wrapper operates on a remote server or not.
* @static
* @return boolean
* @throws \Exception
*/
public static function isRemote()
{
throw new \Exception("Do not call this method directly, but MetaStreamWrapper::wrapperIsRemote() instead");
}
/**
* Describe whether the current wrapper operates on a remote server or not.
* @param String $url Url of the resource
* @static
* @return boolean
* @throws \Exception
*/
public static function isSeekable($url)
{
throw new \Exception("Do not call this method directly, but MetaStreamWrapper::wrapperIsSeekable() instead");
}
/**
* @param string $url
* @return boolean
*/
public static function wrapperIsRemote($url){
return call_user_func(array(self::actualRepositoryWrapperClass(new AJXP_Node($url)), "isRemote"));
}
/**
* @param string $url
* @return boolean
*/
public static function wrapperIsSeekable($url){
return call_user_func(array(self::actualRepositoryWrapperClass(new AJXP_Node($url)), "isSeekable"), $url);
}
public static function nodesUseSameWrappers($url1, $url2){
$w1 = self::actualRepositoryWrapperClass(new AJXP_Node($url1));
$w2 = self::actualRepositoryWrapperClass(new AJXP_Node($url2));
return $w1 == $w2;
}
/**
*
*
* @return bool
*/
public function dir_closedir()
{
if(isSet($this->handle) && is_resource($this->handle)){
closedir($this->handle);
}
}
/**
* Enter description here...
*
* @param string $path
* @param int $options
* @return bool
*/
public function dir_opendir($path, $options)
{
$newPath = self::translateScheme($path, $this);
$this->handle = opendir($newPath);
if($this->handle !== false){
$this->currentDirPath = parse_url($path, PHP_URL_PATH);
return true;
}else{
return false;
}
}
/**
* Standard readdir() implementation
*
* @return string
*/
public function dir_readdir()
{
if(isSet($this->handle) && is_resource($this->handle)){
if($this->currentUniquePath != null){
return $this->innerReadDirFiltered($this->handle);
}else{
return readdir($this->handle);
}
}
return false;
}
/**
* Skip values until correct one is found
* @param Resource $resource
* @return string
*/
protected function innerReadDirFiltered($resource){
$test = readdir($resource);
if($test === false || $test == "." || $test == ".."){
return $test;
}
if (class_exists("Normalizer")) {
$test = Normalizer::normalize($test);
}
if($this->currentUniquePath == $test) {
return $test;
}
// Return next one
return $this->innerReadDirFiltered($resource);
}
/**
* Enter description here...
*
* @return bool
*/
public function dir_rewinddir()
{
$this->currentDirPath = null;
if(isSet($this->handle) && is_resource($this->handle)){
return rewind($this->handle);
}
return false;
}
/**
* Enter description here...
*
* @param string $path
* @param int $mode
* @param int $options
* @return bool
*/
public function mkdir($path, $mode, $options)
{
return mkdir($this->translateScheme($path), $mode, $options);
}
/**
* Enter description here...
*
* @param string $path_from
* @param string $path_to
* @return bool
*/
public function rename($path_from, $path_to)
{
return rename($this->translateScheme($path_from), $this->translateScheme($path_to));
}
/**
* Enter description here...
*
* @param string $path
* @param int $options
* @return bool
*/
public function rmdir($path, $options)
{
if(is_resource($options)){
return rmdir(MetaStreamWrapper::translateScheme($path), $options);
}else{
return rmdir(MetaStreamWrapper::translateScheme($path));
}
}
/**
* Enter description here...
*
*/
public function stream_close()
{
if(isSet($this->handle) && is_resource($this->handle)){
return fclose($this->handle);
}
return false;
}
/**
* Enter description here...
*
* @return bool
*/
public function stream_eof()
{
if(isSet($this->handle) && is_resource($this->handle)){
return feof($this->handle);
}
return false;
}
/**
* Enter description here...
*
* @return bool
*/
public function stream_flush()
{
if(isSet($this->handle) && is_resource($this->handle)){
return fflush($this->handle);
}
return false;
}
/**
* Enter description here...
*
* @param string $path
* @param string $mode
* @param int $options
* @param string &$context
* @return bool
*/
public function stream_open($path, $mode, $options, &$context)
{
if(is_resource($context)){
$this->handle = fopen($this->translateScheme($path), $mode, $options, $context);
}else{
$this->handle = fopen($this->translateScheme($path), $mode, $options);
}
return ($this->handle !== false);
}
/**
* Enter description here...
*
* @param int $count
* @return string
*/
public function stream_read($count)
{
if(isSet($this->handle) && is_resource($this->handle)){
return fread($this->handle, $count);
}
return null;
}
/**
* Enter description here...
*
* @param int $offset
* @param int $whence = SEEK_SET
* @return bool
*/
public function stream_seek($offset, $whence = SEEK_SET)
{
if(isSet($this->handle) && is_resource($this->handle)){
return fseek($this->handle, $offset, $whence);
}
return false;
}
/**
* Enter description here...
*
* @return array
*/
public function stream_stat()
{
if(isSet($this->handle) && is_resource($this->handle)){
return fstat($this->handle);
}
return false;
}
/**
* Enter description here...
*
* @return int
*/
public function stream_tell()
{
if(isSet($this->handle) && is_resource($this->handle)){
return ftell($this->handle);
}
return false;
}
/**
* Enter description here...
*
* @param string $data
* @return int
*/
public function stream_write($data)
{
if(isSet($this->handle) && is_resource($this->handle)){
return fwrite($this->handle, $data);
}
return false;
}
/**
* Enter description here...
*
* @param string $path
* @return bool
*/
public function unlink($path)
{
return unlink($this->translateScheme($path));
}
/**
* Enter description here...
*
* @param string $path
* @param int $flags
* @return array
*/
public function url_stat($path, $flags)
{
$path = $this->translateScheme($path);
$stat = @stat($path);
if($stat === false) {
return null;
}
$bytesize = $stat["size"];
$wrapper = self::actualRepositoryWrapperClass(new AJXP_Node($path));
if(method_exists($wrapper, "getLastRealSize")){
$custom = call_user_func(array($wrapper, "getLastRealSize"));
if ($custom !== false) {
$bytesize = $custom;
}
}
if ($bytesize < 0) {
$bytesize = sprintf("%u", $bytesize);
}
$stat["size"] = $stat[7] = $bytesize;
return $stat;
}
}