quota.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Files\Stream;
  26. /**
  27. * stream wrapper limits the amount of data that can be written to a stream
  28. *
  29. * usage: void \OC\Files\Stream\Quota::register($id, $stream, $limit)
  30. * or: resource \OC\Files\Stream\Quota::wrap($stream, $limit)
  31. */
  32. class Quota {
  33. private static $streams = array();
  34. /**
  35. * @var resource $source
  36. */
  37. private $source;
  38. /**
  39. * @var int $limit
  40. */
  41. private $limit;
  42. /**
  43. * @param string $id
  44. * @param resource $stream
  45. * @param int $limit
  46. */
  47. public static function register($id, $stream, $limit) {
  48. self::$streams[$id] = array($stream, $limit);
  49. }
  50. /**
  51. * remove all registered streams
  52. */
  53. public static function clear() {
  54. self::$streams = array();
  55. }
  56. /**
  57. * @param resource $stream
  58. * @param int $limit
  59. * @return resource
  60. */
  61. static public function wrap($stream, $limit) {
  62. $id = uniqid();
  63. self::register($id, $stream, $limit);
  64. $meta = stream_get_meta_data($stream);
  65. return fopen('quota://' . $id, $meta['mode']);
  66. }
  67. public function stream_open($path, $mode, $options, &$opened_path) {
  68. $id = substr($path, strlen('quota://'));
  69. if (isset(self::$streams[$id])) {
  70. list($this->source, $this->limit) = self::$streams[$id];
  71. return true;
  72. } else {
  73. return false;
  74. }
  75. }
  76. public function stream_seek($offset, $whence = SEEK_SET) {
  77. if ($whence === SEEK_END){
  78. // go to the end to find out last position's offset
  79. $oldOffset = $this->stream_tell();
  80. if (fseek($this->source, 0, $whence) !== 0){
  81. return false;
  82. }
  83. $whence = SEEK_SET;
  84. $offset = $this->stream_tell() + $offset;
  85. $this->limit += $oldOffset - $offset;
  86. }
  87. else if ($whence === SEEK_SET) {
  88. $this->limit += $this->stream_tell() - $offset;
  89. } else {
  90. $this->limit -= $offset;
  91. }
  92. // this wrapper needs to return "true" for success.
  93. // the fseek call itself returns 0 on succeess
  94. return fseek($this->source, $offset, $whence) === 0;
  95. }
  96. public function stream_tell() {
  97. return ftell($this->source);
  98. }
  99. public function stream_read($count) {
  100. $this->limit -= $count;
  101. return fread($this->source, $count);
  102. }
  103. public function stream_write($data) {
  104. $size = strlen($data);
  105. if ($size > $this->limit) {
  106. $data = substr($data, 0, $this->limit);
  107. $size = $this->limit;
  108. }
  109. $this->limit -= $size;
  110. return fwrite($this->source, $data);
  111. }
  112. public function stream_set_option($option, $arg1, $arg2) {
  113. switch ($option) {
  114. case STREAM_OPTION_BLOCKING:
  115. stream_set_blocking($this->source, $arg1);
  116. break;
  117. case STREAM_OPTION_READ_TIMEOUT:
  118. stream_set_timeout($this->source, $arg1, $arg2);
  119. break;
  120. case STREAM_OPTION_WRITE_BUFFER:
  121. stream_set_write_buffer($this->source, $arg1, $arg2);
  122. }
  123. }
  124. public function stream_stat() {
  125. return fstat($this->source);
  126. }
  127. public function stream_lock($mode) {
  128. return flock($this->source, $mode);
  129. }
  130. public function stream_flush() {
  131. return fflush($this->source);
  132. }
  133. public function stream_eof() {
  134. return feof($this->source);
  135. }
  136. public function stream_close() {
  137. fclose($this->source);
  138. }
  139. }