File.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Locks\Backend;
  4. use Sabre\DAV\Locks\LockInfo;
  5. /**
  6. * This Locks backend stores all locking information in a single file.
  7. *
  8. * Note that this is not nearly as robust as a database. If you are considering
  9. * using this backend, keep in mind that the PDO backend can work with SqLite,
  10. * which is designed to be a good file-based database.
  11. *
  12. * It literally solves the problem this class solves as well, but much better.
  13. *
  14. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  15. * @author Evert Pot (http://evertpot.com/)
  16. * @license http://sabre.io/license/ Modified BSD License
  17. */
  18. class File extends AbstractBackend
  19. {
  20. /**
  21. * The storage file.
  22. *
  23. * @var string
  24. */
  25. private $locksFile;
  26. /**
  27. * Constructor.
  28. *
  29. * @param string $locksFile path to file
  30. */
  31. public function __construct($locksFile)
  32. {
  33. $this->locksFile = $locksFile;
  34. }
  35. /**
  36. * Returns a list of Sabre\DAV\Locks\LockInfo objects.
  37. *
  38. * This method should return all the locks for a particular uri, including
  39. * locks that might be set on a parent uri.
  40. *
  41. * If returnChildLocks is set to true, this method should also look for
  42. * any locks in the subtree of the uri for locks.
  43. *
  44. * @param string $uri
  45. * @param bool $returnChildLocks
  46. *
  47. * @return array
  48. */
  49. public function getLocks($uri, $returnChildLocks)
  50. {
  51. $newLocks = [];
  52. $locks = $this->getData();
  53. foreach ($locks as $lock) {
  54. if ($lock->uri === $uri ||
  55. //deep locks on parents
  56. (0 != $lock->depth && 0 === strpos($uri, $lock->uri.'/')) ||
  57. // locks on children
  58. ($returnChildLocks && (0 === strpos($lock->uri, $uri.'/')))) {
  59. $newLocks[] = $lock;
  60. }
  61. }
  62. // Checking if we can remove any of these locks
  63. foreach ($newLocks as $k => $lock) {
  64. if (time() > $lock->timeout + $lock->created) {
  65. unset($newLocks[$k]);
  66. }
  67. }
  68. return $newLocks;
  69. }
  70. /**
  71. * Locks a uri.
  72. *
  73. * @param string $uri
  74. *
  75. * @return bool
  76. */
  77. public function lock($uri, LockInfo $lockInfo)
  78. {
  79. // We're making the lock timeout 30 minutes
  80. $lockInfo->timeout = 1800;
  81. $lockInfo->created = time();
  82. $lockInfo->uri = $uri;
  83. $locks = $this->getData();
  84. foreach ($locks as $k => $lock) {
  85. if (
  86. ($lock->token == $lockInfo->token) ||
  87. (time() > $lock->timeout + $lock->created)
  88. ) {
  89. unset($locks[$k]);
  90. }
  91. }
  92. $locks[] = $lockInfo;
  93. $this->putData($locks);
  94. return true;
  95. }
  96. /**
  97. * Removes a lock from a uri.
  98. *
  99. * @param string $uri
  100. *
  101. * @return bool
  102. */
  103. public function unlock($uri, LockInfo $lockInfo)
  104. {
  105. $locks = $this->getData();
  106. foreach ($locks as $k => $lock) {
  107. if ($lock->token == $lockInfo->token) {
  108. unset($locks[$k]);
  109. $this->putData($locks);
  110. return true;
  111. }
  112. }
  113. return false;
  114. }
  115. /**
  116. * Loads the lockdata from the filesystem.
  117. *
  118. * @return array
  119. */
  120. protected function getData()
  121. {
  122. if (!file_exists($this->locksFile)) {
  123. return [];
  124. }
  125. // opening up the file, and creating a shared lock
  126. $handle = fopen($this->locksFile, 'r');
  127. flock($handle, LOCK_SH);
  128. // Reading data until the eof
  129. $data = stream_get_contents($handle);
  130. // We're all good
  131. flock($handle, LOCK_UN);
  132. fclose($handle);
  133. // Unserializing and checking if the resource file contains data for this file
  134. $data = unserialize($data);
  135. if (!$data) {
  136. return [];
  137. }
  138. return $data;
  139. }
  140. /**
  141. * Saves the lockdata.
  142. */
  143. protected function putData(array $newData)
  144. {
  145. // opening up the file, and creating an exclusive lock
  146. $handle = fopen($this->locksFile, 'a+');
  147. flock($handle, LOCK_EX);
  148. // We can only truncate and rewind once the lock is acquired.
  149. ftruncate($handle, 0);
  150. rewind($handle);
  151. fwrite($handle, serialize($newData));
  152. flock($handle, LOCK_UN);
  153. fclose($handle);
  154. }
  155. }