15 if (isset($params[
'scheme'])) $this->url[
'scheme'] = $params[
'scheme'];
16 if (isset($params[
'user'])) $this->url[
'user'] = $params[
'user'];
17 if (isset($params[
'pass'])) $this->url[
'pass'] = $params[
'pass'];
18 if (isset($params[
'host'])) $this->url[
'host'] = $params[
'host'];
19 if (isset($params[
'port'])) $this->url[
'port'] = $params[
'port'];
21 $this->hidden = (isset($params[
'hidden'])) ? $params[
'hidden'] :
false;
22 $this->path = (isset($params[
'path'])) ? $params[
'path'] :
'.';
24 $this->streamContext = stream_context_create($this->streamContextOptions);
29 if (is_array($parsed)) {
30 $params = array_merge($parsed, $params);
32 $scheme = isset($params[
'scheme']) ? $params[
'scheme'] :
null;
35 $schemeClass =
'\Depage\Fs\Fs' . ucfirst($alias[
'class']);
36 $params[
'scheme'] = $alias[
'scheme'];
38 return new $schemeClass($params);
43 '' => array(
'class' =>
'file',
'scheme' =>
'file'),
44 'file' => array(
'class' =>
'file',
'scheme' =>
'file'),
45 'ftp' => array(
'class' =>
'ftp',
'scheme' =>
'ftp'),
46 'ftps' => array(
'class' =>
'ftp',
'scheme' =>
'ftps'),
47 'ssh2.sftp' => array(
'class' =>
'ssh',
'scheme' =>
'ssh2.sftp'),
48 'ssh' => array(
'class' =>
'ssh',
'scheme' =>
'ssh2.sftp'),
49 'sftp' => array(
'class' =>
'ssh',
'scheme' =>
'ssh2.sftp'),
52 if (array_key_exists($alias, $aliases)) {
53 $translation = $aliases[$alias];
55 $translation = array(
'class' =>
'',
'scheme' => $alias);
77 $path = str_replace($this->
pwd(),
'', $cleanUrl);
83 public function lsDir($path =
'')
87 $lsDir = $this->
lsFilter($path,
'is_dir');
96 $lsFiles = $this->
lsFilter($path,
'is_file');
105 $remote = $this->
cleanUrl($remotePath);
106 $exists = file_exists($remote);
115 $remote = $this->
cleanUrl($remotePath);
116 $fileInfo = new \SplFileInfo($remote);
128 if (is_dir($cleanUrl) && is_readable($cleanUrl .
'/.')) {
129 $this->currentPath = str_replace($this->
pwd(),
'', $cleanUrl) .
'/';
136 public function mkdir($pathName, $mode = 0777, $recursive =
true)
140 $cleanUrl = $this->
cleanUrl($pathName);
141 if (!is_dir($cleanUrl)) {
142 $success =
mkdir($cleanUrl, $mode, $recursive, $this->streamContext);
144 if (!$success && !is_dir($cleanUrl)) {
158 if (preg_match(
'/^' . preg_quote($cleanUrl,
'/') .
'\//', $pwd .
'/')) {
165 public function copy($sourcePath, $targetPath)
169 $source = $this->
cleanUrl($sourcePath);
170 $target = $this->
cleanUrl($targetPath);
172 if (file_exists($source)) {
173 if(file_exists($target) && is_dir($target)) {
176 \copy($source, $target, $this->streamContext);
183 public function mv($sourcePath, $targetPath)
187 $source = $this->
cleanUrl($sourcePath);
188 $target = $this->
cleanUrl($targetPath);
190 if (file_exists($source)) {
191 if(file_exists($target) && is_dir($target)) {
194 $this->
rename($source, $target);
202 public function get($remotePath, $local =
null)
206 if ($local ===
null) {
210 $remote = $this->
cleanUrl($remotePath);
211 copy($remote, $local, $this->streamContext);
215 public function put($local, $remotePath)
219 $remote = $this->
cleanUrl($remotePath);
220 copy($local, $remote, $this->streamContext);
228 $remote = $this->
cleanUrl($remotePath);
229 $string = file_get_contents($remote,
false, $this->streamContext);
238 $remote = $this->
cleanUrl($remotePath);
244 public function test(&$error =
null)
246 $testFile =
'depage-fs-test-file.tmp';
247 $testString =
'depage-fs-test-string';
251 if (!$this->
exists($testFile)) {
252 $this->
putString($testFile, $testString);
253 if ($this->
getString($testFile) === $testString) {
254 $this->
rm($testFile);
255 $success = !$this->
exists($testFile);
259 $error = $exception->getMessage();
276 if (!isset($this->base)) {
282 $cleanPath = $this->
cleanPath(
'/' . $path);
283 $this->base = (substr($cleanPath, -1) ==
'/') ? $cleanPath : $cleanPath .
'/';
288 restore_error_handler();
294 set_error_handler(array($this,
'depageFsErrorHandler'));
296 restore_error_handler();
302 $parsed = parse_url(
$url);
305 if (isset($parsed[
'fragment'])) {
306 $urlParts = explode(
'/',
$url);
308 if (isset($urlParts[2]) && preg_match(
'/^Resource id \#([0-9]+)$/', $urlParts[2], $matches)) {
309 $urlParts[2] = $matches[1];
310 $parsed = parse_url(implode(
'/', $urlParts));
311 $parsed[
'host'] =
'Resource id #' . $parsed[
'host'];
316 $path = (isset($parsed[
'path'])) ? $parsed[
'path'] :
'';
317 $query = (isset($parsed[
'query'])) ? $parsed[
'query'] :
'';
318 if (!empty($query) || preg_match(
'/\?$/',
$url)) {
319 $parsed[
'path'] = $path .
'?' . $query;
320 unset($parsed[
'query']);
328 $scheme = (isset($parsed[
'scheme'])) ? $parsed[
'scheme'] :
null;
329 $path = (isset($parsed[
'path'])) ? $parsed[
'path'] :
null;
336 if (substr(
$url, 0, 1) ==
'/') {
340 $newPath .= (substr($path, 0, 1) ==
'/') ?
'' :
'/';
345 $newUrl[
'path'] = $this->
cleanPath($newPath);
347 if (!preg_match(
';^' . preg_quote($this->
cleanPath($this->base)) .
'(.*)$;', $newUrl[
'path'])) {
351 return $this->
buildUrl($newUrl, $showPass);
355 $dirs = explode(
'/', $path);
358 foreach ($dirs as $dir) {
361 } elseif ($dir !=
'.' && $dir !=
'') {
366 $newPath = (substr($path, 0, 1) ==
'/') ?
'/' :
'';
367 $newPath .= implode(
'/', $newDirs);
371 protected function buildUrl($parsed, $showPass =
true)
373 $path = $parsed[
'scheme'] .
'://';
374 $path .= !empty($parsed[
'user']) ? $parsed[
'user'] :
'';
376 if (!empty($parsed[
'pass'])) {
377 $path .= ($showPass) ?
':' . $parsed[
'pass'] :
':...';
380 $path .= !empty($parsed[
'user']) ?
'@' :
'';
381 $path .= !empty($parsed[
'host']) ? $parsed[
'host'] :
'';
382 $path .= !empty($parsed[
'port']) ?
':' . $parsed[
'port'] :
'';
383 $path .= !empty($parsed[
'path']) ? $parsed[
'path'] :
'/';
389 $pathInfo = pathinfo($path);
390 $fileName = $pathInfo[
'filename'];
392 if (isset($pathInfo[
'extension'])) {
393 $fileName .=
'.' . $pathInfo[
'extension'];
401 $ls = $this->
ls($path);
403 $lsFiltered = array_filter(
405 function ($element) use ($function, $pwd) {
406 return $function($pwd . $element);
409 natcasesort($lsFiltered);
410 $sorted = array_values($lsFiltered);
416 if (preg_match(
'/[' . preg_quote(
'*?[]') .
']/', $pattern)) {
417 $matches = array_filter(
419 function ($node) use ($pattern) {
return fnmatch($pattern, $node); }
422 $matches = array($pattern);
429 $patterns = explode(
'/', $path);
430 $count = count($patterns);
434 $pattern = array_shift($patterns);
437 foreach ($matches as $match) {
438 $next = ($current) ? $current .
'/' . $match : $match;
442 } elseif (is_dir($pwd . $next)) {
443 $nodes = array_merge(
455 if (!file_exists($cleanUrl)) {
457 } elseif (is_dir($cleanUrl)) {
458 foreach ($this->
scandir($cleanUrl,
true) as $nested) {
461 $this->
rmdir($cleanUrl);
462 } elseif (is_file($cleanUrl)) {
463 unlink($cleanUrl, $this->streamContext);
466 clearstatcache(
true, $cleanUrl);
475 $scanDir =
\scandir($cleanUrl, 0, $this->streamContext);
476 $filtered = array_diff($scanDir, array(
'.',
'..'));
479 $filtered = array_filter(
481 function ($node) {
return ($node[0] !=
'.'); }
485 natcasesort($filtered);
486 $sorted = array_values($filtered);
495 return \rmdir(
$url, $this->streamContext);
500 protected function rename($source, $target)
502 return \rename($source, $target, $this->streamContext);
509 return \file_put_contents($filename, $data, $flags, $context);
buildUrl($parsed, $showPass=true)
static schemeAlias($alias='')
lsFilter($path='', $function)
putString($remotePath, $string)
__construct($params=array())
rename($source, $target)
Hook, allows overriding of rename function.
static factory($url, $params=array())
lsRecursive($path, $current)
copy($sourcePath, $targetPath)
rmdir($url)
Hook, allows overriding of rmdir function.
file_put_contents($filename, $data, $flags=0, $context=null)
Hook, allows overriding of file_put_contents function.
scandir($cleanUrl='', $hidden=null)
mkdir($pathName, $mode=0777, $recursive=true)
depageFsErrorHandler($errno, $errstr, $errfile, $errline, array $errcontext)
mv($sourcePath, $targetPath)
cleanUrl($url, $showPass=true)
matchNodesInPath($path, $pattern)