PropFind.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV;
  4. /**
  5. * This class holds all the information about a PROPFIND request.
  6. *
  7. * It contains the type of PROPFIND request, which properties were requested
  8. * and also the returned items.
  9. */
  10. class PropFind
  11. {
  12. /**
  13. * A normal propfind.
  14. */
  15. const NORMAL = 0;
  16. /**
  17. * An allprops request.
  18. *
  19. * While this was originally intended for instructing the server to really
  20. * fetch every property, because it was used so often and it's so heavy
  21. * this turned into a small list of default properties after a while.
  22. *
  23. * So 'all properties' now means a hardcoded list.
  24. */
  25. const ALLPROPS = 1;
  26. /**
  27. * A propname request. This just returns a list of properties that are
  28. * defined on a node, without their values.
  29. */
  30. const PROPNAME = 2;
  31. /**
  32. * Creates the PROPFIND object.
  33. *
  34. * @param string $path
  35. * @param int $depth
  36. * @param int $requestType
  37. */
  38. public function __construct($path, array $properties, $depth = 0, $requestType = self::NORMAL)
  39. {
  40. $this->path = $path;
  41. $this->properties = $properties;
  42. $this->depth = $depth;
  43. $this->requestType = $requestType;
  44. if (self::ALLPROPS === $requestType) {
  45. $this->properties = [
  46. '{DAV:}getlastmodified',
  47. '{DAV:}getcontentlength',
  48. '{DAV:}resourcetype',
  49. '{DAV:}quota-used-bytes',
  50. '{DAV:}quota-available-bytes',
  51. '{DAV:}getetag',
  52. '{DAV:}getcontenttype',
  53. ];
  54. }
  55. foreach ($this->properties as $propertyName) {
  56. // Seeding properties with 404's.
  57. $this->result[$propertyName] = [404, null];
  58. }
  59. $this->itemsLeft = count($this->result);
  60. }
  61. /**
  62. * Handles a specific property.
  63. *
  64. * This method checks whether the specified property was requested in this
  65. * PROPFIND request, and if so, it will call the callback and use the
  66. * return value for it's value.
  67. *
  68. * Example:
  69. *
  70. * $propFind->handle('{DAV:}displayname', function() {
  71. * return 'hello';
  72. * });
  73. *
  74. * Note that handle will only work the first time. If null is returned, the
  75. * value is ignored.
  76. *
  77. * It's also possible to not pass a callback, but immediately pass a value
  78. *
  79. * @param string $propertyName
  80. * @param mixed $valueOrCallBack
  81. */
  82. public function handle($propertyName, $valueOrCallBack)
  83. {
  84. if ($this->itemsLeft && isset($this->result[$propertyName]) && 404 === $this->result[$propertyName][0]) {
  85. if (is_callable($valueOrCallBack)) {
  86. $value = $valueOrCallBack();
  87. } else {
  88. $value = $valueOrCallBack;
  89. }
  90. if (!is_null($value)) {
  91. --$this->itemsLeft;
  92. $this->result[$propertyName] = [200, $value];
  93. }
  94. }
  95. }
  96. /**
  97. * Sets the value of the property.
  98. *
  99. * If status is not supplied, the status will default to 200 for non-null
  100. * properties, and 404 for null properties.
  101. *
  102. * @param string $propertyName
  103. * @param mixed $value
  104. * @param int $status
  105. */
  106. public function set($propertyName, $value, $status = null)
  107. {
  108. if (is_null($status)) {
  109. $status = is_null($value) ? 404 : 200;
  110. }
  111. // If this is an ALLPROPS request and the property is
  112. // unknown, add it to the result; else ignore it:
  113. if (!isset($this->result[$propertyName])) {
  114. if (self::ALLPROPS === $this->requestType) {
  115. $this->result[$propertyName] = [$status, $value];
  116. }
  117. return;
  118. }
  119. if (404 !== $status && 404 === $this->result[$propertyName][0]) {
  120. --$this->itemsLeft;
  121. } elseif (404 === $status && 404 !== $this->result[$propertyName][0]) {
  122. ++$this->itemsLeft;
  123. }
  124. $this->result[$propertyName] = [$status, $value];
  125. }
  126. /**
  127. * Returns the current value for a property.
  128. *
  129. * @param string $propertyName
  130. *
  131. * @return mixed
  132. */
  133. public function get($propertyName)
  134. {
  135. return isset($this->result[$propertyName]) ? $this->result[$propertyName][1] : null;
  136. }
  137. /**
  138. * Returns the current status code for a property name.
  139. *
  140. * If the property does not appear in the list of requested properties,
  141. * null will be returned.
  142. *
  143. * @param string $propertyName
  144. *
  145. * @return int|null
  146. */
  147. public function getStatus($propertyName)
  148. {
  149. return isset($this->result[$propertyName]) ? $this->result[$propertyName][0] : null;
  150. }
  151. /**
  152. * Updates the path for this PROPFIND.
  153. *
  154. * @param string $path
  155. */
  156. public function setPath($path)
  157. {
  158. $this->path = $path;
  159. }
  160. /**
  161. * Returns the path this PROPFIND request is for.
  162. *
  163. * @return string
  164. */
  165. public function getPath()
  166. {
  167. return $this->path;
  168. }
  169. /**
  170. * Returns the depth of this propfind request.
  171. *
  172. * @return int
  173. */
  174. public function getDepth()
  175. {
  176. return $this->depth;
  177. }
  178. /**
  179. * Updates the depth of this propfind request.
  180. *
  181. * @param int $depth
  182. */
  183. public function setDepth($depth)
  184. {
  185. $this->depth = $depth;
  186. }
  187. /**
  188. * Returns all propertynames that have a 404 status, and thus don't have a
  189. * value yet.
  190. *
  191. * @return array
  192. */
  193. public function get404Properties()
  194. {
  195. if (0 === $this->itemsLeft) {
  196. return [];
  197. }
  198. $result = [];
  199. foreach ($this->result as $propertyName => $stuff) {
  200. if (404 === $stuff[0]) {
  201. $result[] = $propertyName;
  202. }
  203. }
  204. return $result;
  205. }
  206. /**
  207. * Returns the full list of requested properties.
  208. *
  209. * This returns just their names, not a status or value.
  210. *
  211. * @return array
  212. */
  213. public function getRequestedProperties()
  214. {
  215. return $this->properties;
  216. }
  217. /**
  218. * Returns true if this was an '{DAV:}allprops' request.
  219. *
  220. * @return bool
  221. */
  222. public function isAllProps()
  223. {
  224. return self::ALLPROPS === $this->requestType;
  225. }
  226. /**
  227. * Returns a result array that's often used in multistatus responses.
  228. *
  229. * The array uses status codes as keys, and property names and value pairs
  230. * as the value of the top array.. such as :
  231. *
  232. * [
  233. * 200 => [ '{DAV:}displayname' => 'foo' ],
  234. * ]
  235. *
  236. * @return array
  237. */
  238. public function getResultForMultiStatus()
  239. {
  240. $r = [
  241. 200 => [],
  242. 404 => [],
  243. ];
  244. foreach ($this->result as $propertyName => $info) {
  245. if (!isset($r[$info[0]])) {
  246. $r[$info[0]] = [$propertyName => $info[1]];
  247. } else {
  248. $r[$info[0]][$propertyName] = $info[1];
  249. }
  250. }
  251. // Removing the 404's for multi-status requests.
  252. if (self::ALLPROPS === $this->requestType) {
  253. unset($r[404]);
  254. }
  255. return $r;
  256. }
  257. /**
  258. * The path that we're fetching properties for.
  259. *
  260. * @var string
  261. */
  262. protected $path;
  263. /**
  264. * The Depth of the request.
  265. *
  266. * 0 means only the current item. 1 means the current item + its children.
  267. * It can also be DEPTH_INFINITY if this is enabled in the server.
  268. *
  269. * @var int
  270. */
  271. protected $depth = 0;
  272. /**
  273. * The type of request. See the TYPE constants.
  274. */
  275. protected $requestType;
  276. /**
  277. * A list of requested properties.
  278. *
  279. * @var array
  280. */
  281. protected $properties = [];
  282. /**
  283. * The result of the operation.
  284. *
  285. * The keys in this array are property names.
  286. * The values are an array with two elements: the http status code and then
  287. * optionally a value.
  288. *
  289. * Example:
  290. *
  291. * [
  292. * "{DAV:}owner" : [404],
  293. * "{DAV:}displayname" : [200, "Admin"]
  294. * ]
  295. *
  296. * @var array
  297. */
  298. protected $result = [];
  299. /**
  300. * This is used as an internal counter for the number of properties that do
  301. * not yet have a value.
  302. *
  303. * @var int
  304. */
  305. protected $itemsLeft;
  306. }