locks.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Jakob Sack
  6. * @copyright 2011 Jakob Sack kde@jakobsack.de
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. class OC_Connector_Sabre_Locks extends Sabre_DAV_Locks_Backend_Abstract {
  23. /**
  24. * Returns a list of Sabre_DAV_Locks_LockInfo objects
  25. *
  26. * This method should return all the locks for a particular uri, including
  27. * locks that might be set on a parent uri.
  28. *
  29. * If returnChildLocks is set to true, this method should also look for
  30. * any locks in the subtree of the uri for locks.
  31. *
  32. * @param string $uri
  33. * @param bool $returnChildLocks
  34. * @return array
  35. */
  36. public function getLocks($uri, $returnChildLocks) {
  37. // NOTE: the following 10 lines or so could be easily replaced by
  38. // pure sql. MySQL's non-standard string concatination prevents us
  39. // from doing this though.
  40. // NOTE: SQLite requires time() to be inserted directly. That's ugly
  41. // but otherwise reading locks from SQLite Databases will return
  42. // nothing
  43. $query = 'SELECT * FROM `*PREFIX*locks`'
  44. .' WHERE `userid` = ? AND (`created` + `timeout`) > '.time().' AND (( `uri` = ?)';
  45. $params = array(OC_User::getUser(), $uri);
  46. // We need to check locks for every part in the uri.
  47. $uriParts = explode('/', $uri);
  48. // We already covered the last part of the uri
  49. array_pop($uriParts);
  50. $currentPath='';
  51. foreach($uriParts as $part) {
  52. if ($currentPath) $currentPath.='/';
  53. $currentPath.=$part;
  54. $query.=' OR (`depth` != 0 AND `uri` = ?)';
  55. $params[] = $currentPath;
  56. }
  57. if ($returnChildLocks) {
  58. $query.=' OR (`uri` LIKE ?)';
  59. $params[] = $uri . '/%';
  60. }
  61. $query.=')';
  62. $stmt = OC_DB::prepare( $query );
  63. $result = $stmt->execute( $params );
  64. $lockList = array();
  65. while( $row = $result->fetchRow()) {
  66. $lockInfo = new Sabre_DAV_Locks_LockInfo();
  67. $lockInfo->owner = $row['owner'];
  68. $lockInfo->token = $row['token'];
  69. $lockInfo->timeout = $row['timeout'];
  70. $lockInfo->created = $row['created'];
  71. $lockInfo->scope = $row['scope'];
  72. $lockInfo->depth = $row['depth'];
  73. $lockInfo->uri = $row['uri'];
  74. $lockList[] = $lockInfo;
  75. }
  76. return $lockList;
  77. }
  78. /**
  79. * Locks a uri
  80. *
  81. * @param string $uri
  82. * @param Sabre_DAV_Locks_LockInfo $lockInfo
  83. * @return bool
  84. */
  85. public function lock($uri, Sabre_DAV_Locks_LockInfo $lockInfo) {
  86. // We're making the lock timeout 5 minutes
  87. $lockInfo->timeout = 300;
  88. $lockInfo->created = time();
  89. $lockInfo->uri = $uri;
  90. $locks = $this->getLocks($uri, false);
  91. $exists = false;
  92. foreach($locks as $lock) {
  93. if ($lock->token == $lockInfo->token) $exists = true;
  94. }
  95. if ($exists) {
  96. $query = OC_DB::prepare( 'UPDATE `*PREFIX*locks`'
  97. .' SET `owner` = ?, `timeout` = ?, `scope` = ?, `depth` = ?, `uri` = ?, `created` = ?'
  98. .' WHERE `userid` = ? AND `token` = ?' );
  99. $result = $query->execute( array(
  100. $lockInfo->owner,
  101. $lockInfo->timeout,
  102. $lockInfo->scope,
  103. $lockInfo->depth,
  104. $uri,
  105. $lockInfo->created,
  106. OC_User::getUser(),
  107. $lockInfo->token)
  108. );
  109. } else {
  110. $query = OC_DB::prepare( 'INSERT INTO `*PREFIX*locks`'
  111. .' (`userid`,`owner`,`timeout`,`scope`,`depth`,`uri`,`created`,`token`)'
  112. .' VALUES (?,?,?,?,?,?,?,?)' );
  113. $result = $query->execute( array(
  114. OC_User::getUser(),
  115. $lockInfo->owner,
  116. $lockInfo->timeout,
  117. $lockInfo->scope,
  118. $lockInfo->depth,
  119. $uri,
  120. $lockInfo->created,
  121. $lockInfo->token)
  122. );
  123. }
  124. return true;
  125. }
  126. /**
  127. * Removes a lock from a uri
  128. *
  129. * @param string $uri
  130. * @param Sabre_DAV_Locks_LockInfo $lockInfo
  131. * @return bool
  132. */
  133. public function unlock($uri, Sabre_DAV_Locks_LockInfo $lockInfo) {
  134. $query = OC_DB::prepare( 'DELETE FROM `*PREFIX*locks` WHERE `userid` = ? AND `uri` = ? AND `token` = ?' );
  135. $result = $query->execute( array(OC_User::getUser(), $uri, $lockInfo->token));
  136. return $result->numRows() === 1;
  137. }
  138. }