PageRenderTime 26ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Sabre/DAV/FSExt/Directory.php

https://bitbucket.org/freshflow/sabredav-1.8.5-fork
PHP | 159 lines | 49 code | 37 blank | 73 comment | 15 complexity | 1aece05de12e9732651adcb576df7509 MD5 | raw file
  1. <?php
  2. namespace Sabre\DAV\FSExt;
  3. use Sabre\DAV;
  4. /**
  5. * Directory class
  6. *
  7. * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
  8. * @author Evert Pot (http://evertpot.com/)
  9. * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
  10. */
  11. class Directory extends Node implements DAV\ICollection, DAV\IQuota {
  12. /**
  13. * Creates a new file in the directory
  14. *
  15. * Data will either be supplied as a stream resource, or in certain cases
  16. * as a string. Keep in mind that you may have to support either.
  17. *
  18. * After successful creation of the file, you may choose to return the ETag
  19. * of the new file here.
  20. *
  21. * The returned ETag must be surrounded by double-quotes (The quotes should
  22. * be part of the actual string).
  23. *
  24. * If you cannot accurately determine the ETag, you should not return it.
  25. * If you don't store the file exactly as-is (you're transforming it
  26. * somehow) you should also not return an ETag.
  27. *
  28. * This means that if a subsequent GET to this new file does not exactly
  29. * return the same contents of what was submitted here, you are strongly
  30. * recommended to omit the ETag.
  31. *
  32. * @param string $name Name of the file
  33. * @param resource|string $data Initial payload
  34. * @return null|string
  35. */
  36. public function createFile($name, $data = null) {
  37. // We're not allowing dots
  38. if ($name=='.' || $name=='..') throw new DAV\Exception\Forbidden('Permission denied to . and ..');
  39. $newPath = $this->path . '/' . $name;
  40. file_put_contents($newPath,$data);
  41. return '"' . md5_file($newPath) . '"';
  42. }
  43. /**
  44. * Creates a new subdirectory
  45. *
  46. * @param string $name
  47. * @return void
  48. */
  49. public function createDirectory($name) {
  50. // We're not allowing dots
  51. if ($name=='.' || $name=='..') throw new DAV\Exception\Forbidden('Permission denied to . and ..');
  52. $newPath = $this->path . '/' . $name;
  53. mkdir($newPath);
  54. }
  55. /**
  56. * Returns a specific child node, referenced by its name
  57. *
  58. * This method must throw Sabre\DAV\Exception\NotFound if the node does not
  59. * exist.
  60. *
  61. * @param string $name
  62. * @throws DAV\Exception\NotFound
  63. * @return DAV\INode
  64. */
  65. public function getChild($name) {
  66. $path = $this->path . '/' . $name;
  67. if (!file_exists($path)) throw new DAV\Exception\NotFound('File could not be located');
  68. if ($name=='.' || $name=='..') throw new DAV\Exception\Forbidden('Permission denied to . and ..');
  69. if (is_dir($path)) {
  70. return new Directory($path);
  71. } else {
  72. return new File($path);
  73. }
  74. }
  75. /**
  76. * Checks if a child exists.
  77. *
  78. * @param string $name
  79. * @return bool
  80. */
  81. public function childExists($name) {
  82. if ($name=='.' || $name=='..')
  83. throw new DAV\Exception\Forbidden('Permission denied to . and ..');
  84. $path = $this->path . '/' . $name;
  85. return file_exists($path);
  86. }
  87. /**
  88. * Returns an array with all the child nodes
  89. *
  90. * @return DAV\INode[]
  91. */
  92. public function getChildren() {
  93. $nodes = array();
  94. foreach(scandir($this->path) as $node) if($node!='.' && $node!='..' && $node!='.sabredav') $nodes[] = $this->getChild($node);
  95. return $nodes;
  96. }
  97. /**
  98. * Deletes all files in this directory, and then itself
  99. *
  100. * @return bool
  101. */
  102. public function delete() {
  103. // Deleting all children
  104. foreach($this->getChildren() as $child) $child->delete();
  105. // Removing resource info, if its still around
  106. if (file_exists($this->path . '/.sabredav')) unlink($this->path . '/.sabredav');
  107. // Removing the directory itself
  108. rmdir($this->path);
  109. return parent::delete();
  110. }
  111. /**
  112. * Returns available diskspace information
  113. *
  114. * @return array
  115. */
  116. public function getQuotaInfo() {
  117. return array(
  118. disk_total_space($this->path)-disk_free_space($this->path),
  119. disk_free_space($this->path)
  120. );
  121. }
  122. }