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

/3.0/modules/webdav/vendor/Sabre/DAV/Tree.php

http://github.com/gallery/gallery3-contrib
PHP | 192 lines | 68 code | 45 blank | 79 comment | 7 complexity | e618694f1d04d1c6462242b56aeb9691 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Abstract tree object
  4. *
  5. * @package Sabre
  6. * @subpackage DAV
  7. * @copyright Copyright (C) 2007-2010 Rooftop Solutions. All rights reserved.
  8. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  9. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  10. */
  11. abstract class Sabre_DAV_Tree {
  12. /**
  13. * This function must return an INode object for a path
  14. * If a Path doesn't exist, thrown an Exception_FileNotFound
  15. *
  16. * @param string $path
  17. * @throws Exception_FileNotFound
  18. * @return Sabre_DAV_INode
  19. */
  20. abstract function getNodeForPath($path);
  21. /**
  22. * This function allows you to check if a node exists.
  23. *
  24. * Implementors of this class should override this method to make
  25. * it cheaper.
  26. *
  27. * @param string $path
  28. * @return bool
  29. */
  30. public function nodeExists($path) {
  31. try {
  32. $this->getNodeForPath($path);
  33. return true;
  34. } catch (Sabre_DAV_Exception_FileNotFound $e) {
  35. return false;
  36. }
  37. }
  38. /**
  39. * Copies a file from path to another
  40. *
  41. * @param string $sourcePath The source location
  42. * @param string $destinationPath The full destination path
  43. * @return void
  44. */
  45. public function copy($sourcePath, $destinationPath) {
  46. $sourceNode = $this->getNodeForPath($sourcePath);
  47. // grab the dirname and basename components
  48. list($destinationDir, $destinationName) = Sabre_DAV_URLUtil::splitPath($destinationPath);
  49. $destinationParent = $this->getNodeForPath($destinationDir);
  50. $this->copyNode($sourceNode,$destinationParent,$destinationName);
  51. $this->markDirty($destinationDir);
  52. }
  53. /**
  54. * Moves a file from one location to another
  55. *
  56. * @param string $sourcePath The path to the file which should be moved
  57. * @param string $destinationPath The full destination path, so not just the destination parent node
  58. * @return int
  59. */
  60. public function move($sourcePath, $destinationPath) {
  61. list($sourceDir, $sourceName) = Sabre_DAV_URLUtil::splitPath($sourcePath);
  62. list($destinationDir, $destinationName) = Sabre_DAV_URLUtil::splitPath($destinationPath);
  63. if ($sourceDir===$destinationDir) {
  64. $renameable = $this->getNodeForPath($sourcePath);
  65. $renameable->setName($destinationName);
  66. } else {
  67. $this->copy($sourcePath,$destinationPath);
  68. $this->getNodeForPath($sourcePath)->delete();
  69. }
  70. $this->markDirty($sourceDir);
  71. $this->markDirty($destinationDir);
  72. }
  73. /**
  74. * Deletes a node from the tree
  75. *
  76. * @param string $path
  77. * @return void
  78. */
  79. public function delete($path) {
  80. $node = $this->getNodeForPath($path);
  81. $node->delete();
  82. list($parent) = Sabre_DAV_URLUtil::splitPath($path);
  83. $this->markDirty($parent);
  84. }
  85. /**
  86. * Returns a list of childnodes for a given path.
  87. *
  88. * @param string $path
  89. * @return array
  90. */
  91. public function getChildren($path) {
  92. $node = $this->getNodeForPath($path);
  93. return $node->getChildren();
  94. }
  95. /**
  96. * This method is called with every tree update
  97. *
  98. * Examples of tree updates are:
  99. * * node deletions
  100. * * node creations
  101. * * copy
  102. * * move
  103. * * renaming nodes
  104. *
  105. * If Tree classes implement a form of caching, this will allow
  106. * them to make sure caches will be expired.
  107. *
  108. * If a path is passed, it is assumed that the entire subtree is dirty
  109. *
  110. * @param string $path
  111. * @return void
  112. */
  113. public function markDirty($path) {
  114. }
  115. /**
  116. * copyNode
  117. *
  118. * @param Sabre_DAV_INode $source
  119. * @param Sabre_DAV_ICollection $destination
  120. * @return void
  121. */
  122. protected function copyNode(Sabre_DAV_INode $source,Sabre_DAV_ICollection $destinationParent,$destinationName = null) {
  123. if (!$destinationName) $destinationName = $source->getName();
  124. if ($source instanceof Sabre_DAV_IFile) {
  125. $data = $source->get();
  126. // If the body was a string, we need to convert it to a stream
  127. if (is_string($data)) {
  128. $stream = fopen('php://temp','r+');
  129. fwrite($stream,$data);
  130. rewind($stream);
  131. $data = $stream;
  132. }
  133. $destinationParent->createFile($destinationName,$data);
  134. $destination = $destinationParent->getChild($destinationName);
  135. } elseif ($source instanceof Sabre_DAV_ICollection) {
  136. $destinationParent->createDirectory($destinationName);
  137. $destination = $destinationParent->getChild($destinationName);
  138. foreach($source->getChildren() as $child) {
  139. $this->copyNode($child,$destination);
  140. }
  141. }
  142. if ($source instanceof Sabre_DAV_IProperties && $destination instanceof Sabre_DAV_IProperties) {
  143. $props = $source->getProperties(array());
  144. $destination->updateProperties($props);
  145. }
  146. }
  147. }