fileglobal.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Thomas Tanghus <thomas@tanghus.net>
  9. *
  10. * @copyright Copyright (c) 2015, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Cache;
  27. class FileGlobal {
  28. static protected function getCacheDir() {
  29. $cache_dir = get_temp_dir().'/owncloud-' . \OC_Util::getInstanceId().'/';
  30. if (!is_dir($cache_dir)) {
  31. mkdir($cache_dir);
  32. }
  33. return $cache_dir;
  34. }
  35. protected function fixKey($key) {
  36. return str_replace('/', '_', $key);
  37. }
  38. /**
  39. * @param string $key
  40. */
  41. public function get($key) {
  42. $key = $this->fixKey($key);
  43. if ($this->hasKey($key)) {
  44. $cache_dir = self::getCacheDir();
  45. return file_get_contents($cache_dir.$key);
  46. }
  47. return null;
  48. }
  49. /**
  50. * @param string $key
  51. * @param string $value
  52. */
  53. public function set($key, $value, $ttl=0) {
  54. $key = $this->fixKey($key);
  55. $cache_dir = self::getCacheDir();
  56. if ($cache_dir and file_put_contents($cache_dir.$key, $value)) {
  57. if ($ttl === 0) {
  58. $ttl = 86400; // 60*60*24
  59. }
  60. return touch($cache_dir.$key, time() + $ttl);
  61. }
  62. return false;
  63. }
  64. public function hasKey($key) {
  65. $key = $this->fixKey($key);
  66. $cache_dir = self::getCacheDir();
  67. if ($cache_dir && is_file($cache_dir.$key) && is_readable($cache_dir.$key)) {
  68. $mtime = filemtime($cache_dir.$key);
  69. if ($mtime < time()) {
  70. unlink($cache_dir.$key);
  71. return false;
  72. }
  73. return true;
  74. }
  75. return false;
  76. }
  77. public function remove($key) {
  78. $cache_dir = self::getCacheDir();
  79. if(!$cache_dir) {
  80. return false;
  81. }
  82. $key = $this->fixKey($key);
  83. return unlink($cache_dir.$key);
  84. }
  85. public function clear($prefix='') {
  86. $cache_dir = self::getCacheDir();
  87. $prefix = $this->fixKey($prefix);
  88. if($cache_dir and is_dir($cache_dir)) {
  89. $dh=opendir($cache_dir);
  90. if(is_resource($dh)) {
  91. while (($file = readdir($dh)) !== false) {
  92. if($file!='.' and $file!='..' and ($prefix==='' || strpos($file, $prefix) === 0)) {
  93. unlink($cache_dir.$file);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }