streamwrapper.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @author Bart Visscher <bartv@thisnet.nl>
  4. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @copyright Copyright (c) 2016, ownCloud, Inc.
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Files\Storage;
  28. abstract class StreamWrapper extends Common {
  29. /**
  30. * @param string $path
  31. * @return string|null
  32. */
  33. abstract public function constructUrl($path);
  34. public function mkdir($path) {
  35. return mkdir($this->constructUrl($path));
  36. }
  37. public function rmdir($path) {
  38. if ($this->is_dir($path) && $this->isDeletable($path)) {
  39. $dh = $this->opendir($path);
  40. if (!is_resource($dh)) {
  41. return false;
  42. }
  43. while (($file = readdir($dh)) !== false) {
  44. if ($this->is_dir($path . '/' . $file)) {
  45. $this->rmdir($path . '/' . $file);
  46. } else {
  47. $this->unlink($path . '/' . $file);
  48. }
  49. }
  50. $url = $this->constructUrl($path);
  51. $success = rmdir($url);
  52. clearstatcache(false, $url);
  53. return $success;
  54. } else {
  55. return false;
  56. }
  57. }
  58. public function opendir($path) {
  59. return opendir($this->constructUrl($path));
  60. }
  61. public function filetype($path) {
  62. return @filetype($this->constructUrl($path));
  63. }
  64. public function file_exists($path) {
  65. return file_exists($this->constructUrl($path));
  66. }
  67. public function unlink($path) {
  68. $url = $this->constructUrl($path);
  69. $success = unlink($url);
  70. // normally unlink() is supposed to do this implicitly,
  71. // but doing it anyway just to be sure
  72. clearstatcache(false, $url);
  73. return $success;
  74. }
  75. public function fopen($path, $mode) {
  76. return fopen($this->constructUrl($path), $mode);
  77. }
  78. public function touch($path, $mtime = null) {
  79. if ($this->file_exists($path)) {
  80. if (is_null($mtime)) {
  81. $fh = $this->fopen($path, 'a');
  82. fwrite($fh, '');
  83. fclose($fh);
  84. return true;
  85. } else {
  86. return false; //not supported
  87. }
  88. } else {
  89. $this->file_put_contents($path, '');
  90. return true;
  91. }
  92. }
  93. /**
  94. * @param string $path
  95. * @param string $target
  96. */
  97. public function getFile($path, $target) {
  98. return copy($this->constructUrl($path), $target);
  99. }
  100. /**
  101. * @param string $target
  102. */
  103. public function uploadFile($path, $target) {
  104. return copy($path, $this->constructUrl($target));
  105. }
  106. public function rename($path1, $path2) {
  107. return rename($this->constructUrl($path1), $this->constructUrl($path2));
  108. }
  109. public function stat($path) {
  110. return stat($this->constructUrl($path));
  111. }
  112. }