streamwrapper.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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) 2015, 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->file_exists($path) && $this->isDeletable($path)) {
  39. $dh = $this->opendir($path);
  40. while (($file = readdir($dh)) !== false) {
  41. if ($this->is_dir($path . '/' . $file)) {
  42. $this->rmdir($path . '/' . $file);
  43. } else {
  44. $this->unlink($path . '/' . $file);
  45. }
  46. }
  47. $url = $this->constructUrl($path);
  48. $success = rmdir($url);
  49. clearstatcache(false, $url);
  50. return $success;
  51. } else {
  52. return false;
  53. }
  54. }
  55. public function opendir($path) {
  56. return opendir($this->constructUrl($path));
  57. }
  58. public function filetype($path) {
  59. return @filetype($this->constructUrl($path));
  60. }
  61. public function file_exists($path) {
  62. return file_exists($this->constructUrl($path));
  63. }
  64. public function unlink($path) {
  65. $url = $this->constructUrl($path);
  66. $success = unlink($url);
  67. // normally unlink() is supposed to do this implicitly,
  68. // but doing it anyway just to be sure
  69. clearstatcache(false, $url);
  70. return $success;
  71. }
  72. public function fopen($path, $mode) {
  73. return fopen($this->constructUrl($path), $mode);
  74. }
  75. public function touch($path, $mtime = null) {
  76. if ($this->file_exists($path)) {
  77. if (is_null($mtime)) {
  78. $fh = $this->fopen($path, 'a');
  79. fwrite($fh, '');
  80. fclose($fh);
  81. return true;
  82. } else {
  83. return false; //not supported
  84. }
  85. } else {
  86. $this->file_put_contents($path, '');
  87. return true;
  88. }
  89. }
  90. /**
  91. * @param string $path
  92. * @param string $target
  93. */
  94. public function getFile($path, $target) {
  95. return copy($this->constructUrl($path), $target);
  96. }
  97. /**
  98. * @param string $target
  99. */
  100. public function uploadFile($path, $target) {
  101. return copy($path, $this->constructUrl($target));
  102. }
  103. public function rename($path1, $path2) {
  104. return rename($this->constructUrl($path1), $this->constructUrl($path2));
  105. }
  106. public function stat($path) {
  107. return stat($this->constructUrl($path));
  108. }
  109. }