Directory.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Directory class
  4. *
  5. * @package Sabre
  6. * @subpackage DAV
  7. * @copyright Copyright (C) 2007-2012 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. class Sabre_DAV_FSExt_Directory extends Sabre_DAV_FSExt_Node implements Sabre_DAV_ICollection, Sabre_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 succesful 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 Sabre_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 Sabre_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. * @param string $name
  59. * @throws Sabre_DAV_Exception_NotFound
  60. * @return Sabre_DAV_INode
  61. */
  62. public function getChild($name) {
  63. $path = $this->path . '/' . $name;
  64. if (!file_exists($path)) throw new Sabre_DAV_Exception_NotFound('File could not be located');
  65. if ($name=='.' || $name=='..') throw new Sabre_DAV_Exception_Forbidden('Permission denied to . and ..');
  66. if (is_dir($path)) {
  67. return new Sabre_DAV_FSExt_Directory($path);
  68. } else {
  69. return new Sabre_DAV_FSExt_File($path);
  70. }
  71. }
  72. /**
  73. * Checks if a child exists.
  74. *
  75. * @param string $name
  76. * @return bool
  77. */
  78. public function childExists($name) {
  79. if ($name=='.' || $name=='..')
  80. throw new Sabre_DAV_Exception_Forbidden('Permission denied to . and ..');
  81. $path = $this->path . '/' . $name;
  82. return file_exists($path);
  83. }
  84. /**
  85. * Returns an array with all the child nodes
  86. *
  87. * @return Sabre_DAV_INode[]
  88. */
  89. public function getChildren() {
  90. $nodes = array();
  91. foreach(scandir($this->path) as $node) if($node!='.' && $node!='..' && $node!='.sabredav') $nodes[] = $this->getChild($node);
  92. return $nodes;
  93. }
  94. /**
  95. * Deletes all files in this directory, and then itself
  96. *
  97. * @return bool
  98. */
  99. public function delete() {
  100. // Deleting all children
  101. foreach($this->getChildren() as $child) $child->delete();
  102. // Removing resource info, if its still around
  103. if (file_exists($this->path . '/.sabredav')) unlink($this->path . '/.sabredav');
  104. // Removing the directory itself
  105. rmdir($this->path);
  106. return parent::delete();
  107. }
  108. /**
  109. * Returns available diskspace information
  110. *
  111. * @return array
  112. */
  113. public function getQuotaInfo() {
  114. return array(
  115. disk_total_space($this->path)-disk_free_space($this->path),
  116. disk_free_space($this->path)
  117. );
  118. }
  119. }