PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/modules/webdav/vendor/Sabre/DAV/Locks/Backend/FS.php

https://github.com/wrlee/gallery3-contrib
PHP | 180 lines | 74 code | 44 blank | 62 comment | 10 complexity | 5d524bb20366fbbf186d51e72ee3f2c9 MD5 | raw file
  1. <?php
  2. /**
  3. * The Lock manager allows you to handle all file-locks centrally.
  4. *
  5. * This Lock Manager stores all its data in the filesystem. By default it will do this in PHP's standard temporary session directory,
  6. * but this can be overriden by specifiying an alternative path in the contructor
  7. *
  8. * @package Sabre
  9. * @subpackage DAV
  10. * @copyright Copyright (C) 2007-2010 Rooftop Solutions. All rights reserved.
  11. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  12. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  13. */
  14. class Sabre_DAV_Locks_Backend_FS extends Sabre_DAV_Locks_Backend_Abstract {
  15. /**
  16. * The default data directory
  17. *
  18. * @var string
  19. */
  20. private $dataDir;
  21. public function __construct($dataDir) {
  22. $this->dataDir = $dataDir;
  23. }
  24. protected function getFileNameForUri($uri) {
  25. return $this->dataDir . '/sabredav_' . md5($uri) . '.locks';
  26. }
  27. /**
  28. * Returns a list of Sabre_DAV_Locks_LockInfo objects
  29. *
  30. * This method should return all the locks for a particular uri, including
  31. * locks that might be set on a parent uri.
  32. *
  33. * @param string $uri
  34. * @return array
  35. */
  36. public function getLocks($uri) {
  37. $lockList = array();
  38. $currentPath = '';
  39. foreach(explode('/',$uri) as $uriPart) {
  40. // weird algorithm that can probably be improved, but we're traversing the path top down
  41. if ($currentPath) $currentPath.='/';
  42. $currentPath.=$uriPart;
  43. $uriLocks = $this->getData($currentPath);
  44. foreach($uriLocks as $uriLock) {
  45. // Unless we're on the leaf of the uri-tree we should ingore locks with depth 0
  46. if($uri==$currentPath || $uriLock->depth!=0) {
  47. $uriLock->uri = $currentPath;
  48. $lockList[] = $uriLock;
  49. }
  50. }
  51. }
  52. // Checking if we can remove any of these locks
  53. foreach($lockList as $k=>$lock) {
  54. if (time() > $lock->timeout + $lock->created) unset($lockList[$k]);
  55. }
  56. return $lockList;
  57. }
  58. /**
  59. * Locks a uri
  60. *
  61. * @param string $uri
  62. * @param Sabre_DAV_Locks_LockInfo $lockInfo
  63. * @return bool
  64. */
  65. public function lock($uri,Sabre_DAV_Locks_LockInfo $lockInfo) {
  66. // We're making the lock timeout 30 minutes
  67. $lockInfo->timeout = 1800;
  68. $lockInfo->created = time();
  69. $locks = $this->getLocks($uri);
  70. foreach($locks as $k=>$lock) {
  71. if ($lock->token == $lockInfo->token) unset($locks[$k]);
  72. }
  73. $locks[] = $lockInfo;
  74. $this->putData($uri,$locks);
  75. return true;
  76. }
  77. /**
  78. * Removes a lock from a uri
  79. *
  80. * @param string $uri
  81. * @param Sabre_DAV_Locks_LockInfo $lockInfo
  82. * @return bool
  83. */
  84. public function unlock($uri,Sabre_DAV_Locks_LockInfo $lockInfo) {
  85. $locks = $this->getLocks($uri);
  86. foreach($locks as $k=>$lock) {
  87. if ($lock->token == $lockInfo->token) {
  88. unset($locks[$k]);
  89. $this->putData($uri,$locks);
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. /**
  96. * Returns the stored data for a uri
  97. *
  98. * @param string $uri
  99. * @return array
  100. */
  101. protected function getData($uri) {
  102. $path = $this->getFilenameForUri($uri);
  103. if (!file_exists($path)) return array();
  104. // opening up the file, and creating a shared lock
  105. $handle = fopen($path,'r');
  106. flock($handle,LOCK_SH);
  107. $data = '';
  108. // Reading data until the eof
  109. while(!feof($handle)) {
  110. $data.=fread($handle,8192);
  111. }
  112. // We're all good
  113. fclose($handle);
  114. // Unserializing and checking if the resource file contains data for this file
  115. $data = unserialize($data);
  116. if (!$data) return array();
  117. return $data;
  118. }
  119. /**
  120. * Updates the lock information
  121. *
  122. * @param string $uri
  123. * @param array $newData
  124. * @return void
  125. */
  126. protected function putData($uri,array $newData) {
  127. $path = $this->getFileNameForUri($uri);
  128. // opening up the file, and creating a shared lock
  129. $handle = fopen($path,'a+');
  130. flock($handle,LOCK_EX);
  131. ftruncate($handle,0);
  132. rewind($handle);
  133. fwrite($handle,serialize($newData));
  134. fclose($handle);
  135. }
  136. }