1
0

file.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  4. * Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. namespace OC\Cache;
  10. use OC\Files\Filesystem;
  11. use OC\Files\View;
  12. class File {
  13. protected $storage;
  14. /**
  15. * Returns the cache storage for the logged in user
  16. *
  17. * @return \OC\Files\View cache storage
  18. */
  19. protected function getStorage() {
  20. if (isset($this->storage)) {
  21. return $this->storage;
  22. }
  23. if (\OC_User::isLoggedIn()) {
  24. $rootView = new View();
  25. $user = \OC::$server->getUserSession()->getUser();
  26. Filesystem::initMountPoints($user->getUID());
  27. if (!$rootView->file_exists('/' . $user->getUID() . '/cache')) {
  28. $rootView->mkdir('/' . $user->getUID() . '/cache');
  29. }
  30. $this->storage = new View('/' . $user->getUID() . '/cache');
  31. return $this->storage;
  32. } else {
  33. \OC_Log::write('core', 'Can\'t get cache storage, user not logged in', \OC_Log::ERROR);
  34. throw new \OC\ForbiddenException('Can\t get cache storage, user not logged in');
  35. }
  36. }
  37. /**
  38. * @param string $key
  39. */
  40. public function get($key) {
  41. $result = null;
  42. $proxyStatus = \OC_FileProxy::$enabled;
  43. \OC_FileProxy::$enabled = false;
  44. if ($this->hasKey($key)) {
  45. $storage = $this->getStorage();
  46. $result = $storage->file_get_contents($key);
  47. }
  48. \OC_FileProxy::$enabled = $proxyStatus;
  49. return $result;
  50. }
  51. /**
  52. * Returns the size of the stored/cached data
  53. *
  54. * @param string $key
  55. * @return int
  56. */
  57. public function size($key) {
  58. $result = 0;
  59. $proxyStatus = \OC_FileProxy::$enabled;
  60. \OC_FileProxy::$enabled = false;
  61. if ($this->hasKey($key)) {
  62. $storage = $this->getStorage();
  63. $result = $storage->filesize($key);
  64. }
  65. \OC_FileProxy::$enabled = $proxyStatus;
  66. return $result;
  67. }
  68. /**
  69. * @param string $key
  70. */
  71. public function set($key, $value, $ttl = 0) {
  72. $storage = $this->getStorage();
  73. $result = false;
  74. $proxyStatus = \OC_FileProxy::$enabled;
  75. \OC_FileProxy::$enabled = false;
  76. if ($storage and $storage->file_put_contents($key, $value)) {
  77. if ($ttl === 0) {
  78. $ttl = 86400; // 60*60*24
  79. }
  80. $result = $storage->touch($key, time() + $ttl);
  81. }
  82. \OC_FileProxy::$enabled = $proxyStatus;
  83. return $result;
  84. }
  85. public function hasKey($key) {
  86. $storage = $this->getStorage();
  87. if ($storage && $storage->is_file($key) && $storage->isReadable($key)) {
  88. return true;
  89. }
  90. return false;
  91. }
  92. /**
  93. * @param string $key
  94. */
  95. public function remove($key) {
  96. $storage = $this->getStorage();
  97. if (!$storage) {
  98. return false;
  99. }
  100. return $storage->unlink($key);
  101. }
  102. public function clear($prefix = '') {
  103. $storage = $this->getStorage();
  104. if ($storage and $storage->is_dir('/')) {
  105. $dh = $storage->opendir('/');
  106. if (is_resource($dh)) {
  107. while (($file = readdir($dh)) !== false) {
  108. if ($file != '.' and $file != '..' and ($prefix === '' || strpos($file, $prefix) === 0)) {
  109. $storage->unlink('/' . $file);
  110. }
  111. }
  112. }
  113. }
  114. return true;
  115. }
  116. public function gc() {
  117. $storage = $this->getStorage();
  118. if ($storage and $storage->is_dir('/')) {
  119. $now = time();
  120. $dh = $storage->opendir('/');
  121. if (!is_resource($dh)) {
  122. return null;
  123. }
  124. while (($file = readdir($dh)) !== false) {
  125. if ($file != '.' and $file != '..') {
  126. $mtime = $storage->filemtime('/' . $file);
  127. if ($mtime < $now) {
  128. $storage->unlink('/' . $file);
  129. }
  130. }
  131. }
  132. }
  133. }
  134. public static function loginListener() {
  135. $c = new self();
  136. $c->gc();
  137. }
  138. }