file.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Björn Schießle <schiessle@owncloud.com>
  6. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  7. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <icewind@owncloud.com>
  10. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Thomas Tanghus <thomas@tanghus.net>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @copyright Copyright (c) 2015, ownCloud, Inc.
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\Cache;
  32. use OC\Files\Filesystem;
  33. use OC\Files\View;
  34. use OCP\Security\ISecureRandom;
  35. class File {
  36. protected $storage;
  37. /**
  38. * Returns the cache storage for the logged in user
  39. *
  40. * @return \OC\Files\View cache storage
  41. */
  42. protected function getStorage() {
  43. if (isset($this->storage)) {
  44. return $this->storage;
  45. }
  46. if (\OC_User::isLoggedIn()) {
  47. $rootView = new View();
  48. $user = \OC::$server->getUserSession()->getUser();
  49. Filesystem::initMountPoints($user->getUID());
  50. if (!$rootView->file_exists('/' . $user->getUID() . '/cache')) {
  51. $rootView->mkdir('/' . $user->getUID() . '/cache');
  52. }
  53. $this->storage = new View('/' . $user->getUID() . '/cache');
  54. return $this->storage;
  55. } else {
  56. \OC_Log::write('core', 'Can\'t get cache storage, user not logged in', \OC_Log::ERROR);
  57. throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in');
  58. }
  59. }
  60. /**
  61. * @param string $key
  62. */
  63. public function get($key) {
  64. $result = null;
  65. if ($this->hasKey($key)) {
  66. $storage = $this->getStorage();
  67. $result = $storage->file_get_contents($key);
  68. }
  69. return $result;
  70. }
  71. /**
  72. * Returns the size of the stored/cached data
  73. *
  74. * @param string $key
  75. * @return int
  76. */
  77. public function size($key) {
  78. $result = 0;
  79. if ($this->hasKey($key)) {
  80. $storage = $this->getStorage();
  81. $result = $storage->filesize($key);
  82. }
  83. return $result;
  84. }
  85. /**
  86. * @param string $key
  87. */
  88. public function set($key, $value, $ttl = 0) {
  89. $storage = $this->getStorage();
  90. $result = false;
  91. // unique id to avoid chunk collision, just in case
  92. $uniqueId = \OC::$server->getSecureRandom()->getLowStrengthGenerator()->generate(
  93. 16,
  94. ISecureRandom::CHAR_DIGITS . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER
  95. );
  96. // use part file to prevent hasKey() to find the key
  97. // while it is being written
  98. $keyPart = $key . '.' . $uniqueId . '.part';
  99. if ($storage and $storage->file_put_contents($keyPart, $value)) {
  100. if ($ttl === 0) {
  101. $ttl = 86400; // 60*60*24
  102. }
  103. $result = $storage->touch($keyPart, time() + $ttl);
  104. $result &= $storage->rename($keyPart, $key);
  105. }
  106. return $result;
  107. }
  108. public function hasKey($key) {
  109. $storage = $this->getStorage();
  110. if ($storage && $storage->is_file($key) && $storage->isReadable($key)) {
  111. return true;
  112. }
  113. return false;
  114. }
  115. /**
  116. * @param string $key
  117. */
  118. public function remove($key) {
  119. $storage = $this->getStorage();
  120. if (!$storage) {
  121. return false;
  122. }
  123. return $storage->unlink($key);
  124. }
  125. public function clear($prefix = '') {
  126. $storage = $this->getStorage();
  127. if ($storage and $storage->is_dir('/')) {
  128. $dh = $storage->opendir('/');
  129. if (is_resource($dh)) {
  130. while (($file = readdir($dh)) !== false) {
  131. if ($file != '.' and $file != '..' and ($prefix === '' || strpos($file, $prefix) === 0)) {
  132. $storage->unlink('/' . $file);
  133. }
  134. }
  135. }
  136. }
  137. return true;
  138. }
  139. public function gc() {
  140. $storage = $this->getStorage();
  141. if ($storage and $storage->is_dir('/')) {
  142. $now = time();
  143. $dh = $storage->opendir('/');
  144. if (!is_resource($dh)) {
  145. return null;
  146. }
  147. while (($file = readdir($dh)) !== false) {
  148. if ($file != '.' and $file != '..') {
  149. $mtime = $storage->filemtime('/' . $file);
  150. if ($mtime < $now) {
  151. $storage->unlink('/' . $file);
  152. }
  153. }
  154. }
  155. }
  156. }
  157. public static function loginListener() {
  158. $c = new self();
  159. $c->gc();
  160. }
  161. }