ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilWebDAVUriPathResolver.php
Go to the documentation of this file.
1 <?php
2 
12 {
14  protected $repo_helper;
15 
22  {
23  $this->repo_helper = $repo_helper;
24  }
25 
41  public function getRefIdForWebDAVPath(string $a_uri) : int
42  {
43  $a_uri = trim($a_uri, '/');
44 
45  /* After this funciton, the array SHOULD look like this:
46  * $splitted_path[0] = '<client_name>'
47  * $splitted_path[1] = 'ref_<ref_id>' or <ilias_root_name>
48  * $splitted_path[2] = '<rest>/<of>/<the>/<path>
49  */
50  $splitted_path = explode('/', $a_uri, 3);
51 
52  // Early exit for bad request
53  if (count($splitted_path) < 2) {
54  throw new \Sabre\DAV\Exception\BadRequest('Path too short');
55  }
56 
57  $repository_mountpoint = $splitted_path[1];
58  $path_inside_of_mountpoint = isset($splitted_path[2]) ? $splitted_path[2] : '';
59 
60  // Since we already know our client, we only have to check the requested root for our path
61  // if second string = 'ilias', the request was for the ilias root
62  if ($repository_mountpoint == 'ILIAS') {
63  return $this->getRefIdFromPathInRepositoryMount($path_inside_of_mountpoint);
64  } // if the first 4 letters are 'ref_', we are searching for a ref ID in the tree
65  elseif (substr($repository_mountpoint, 0, 4) == 'ref_') {
66  return $this->getRefIdFromPathInRefMount($repository_mountpoint, $path_inside_of_mountpoint);
67  }
68 
69  // if there was no 'ilias' and no 'ref_' in the second string, then its an invalid request
70  throw new \Sabre\DAV\Exception\BadRequest('Invalid mountpoint given');
71  }
72 
85  protected function getRefIdFromPathInRepositoryMount(string $path_inside_of_mountpoint) : int
86  {
87  if ($path_inside_of_mountpoint != '') {
88  return $this->getRefIdFromGivenParentRefAndTitlePath(ROOT_FOLDER_ID, explode('/', $path_inside_of_mountpoint));
89  } else {
90  return ROOT_FOLDER_ID;
91  }
92  }
93 
108  protected function getRefIdFromPathInRefMount(string $repository_mountpoint, string $path_inside_of_mountpoint) : int
109  {
110  // Make a 'ref_1234' to a '1234'
111  // Since we already tested for 'ref_', we can be sure there is at least one '_' character
112  $relative_mountpoint_ref_id = (int) explode('_', $repository_mountpoint)[1];
113 
114  // Case 1: Path to an object given and a ref_id is given
115  if ($path_inside_of_mountpoint != '' && $relative_mountpoint_ref_id > 0) {
116  return $this->getRefIdFromGivenParentRefAndTitlePath($relative_mountpoint_ref_id, explode('/', $path_inside_of_mountpoint));
117  }
118  // Case 2: No path is given, but a ref_id. This means, the searched object is actually the given ref_id
119  // This happens for an URI like "<client_id>/ref_1234/" (the part after the '/' is actually an empty string)
120  elseif ($path_inside_of_mountpoint == '' && $relative_mountpoint_ref_id > 0) {
121  return $relative_mountpoint_ref_id;
122  }
123  // Case 3: Given ref_id is invalid (no number or 0 given). Throw an exception since there is no object like this
124  else {
125  throw new \Sabre\DAV\Exception\NotFound('Mount point not found');
126  }
127  }
128 
138  protected function getRefIdFromGivenParentRefAndTitlePath(int $a_parent_ref, array $a_current_path_array) : int
139  {
140  $current_ref_id = $a_parent_ref;
141  while (count($a_current_path_array) >= 1) {
142  // Pops out first element (respectively object title)
143  $next_searched_title = array_shift($a_current_path_array);
144  if ($next_searched_title != '') {
145  try {
146  $current_ref_id = $this->getChildRefIdByGivenTitle($current_ref_id, $next_searched_title);
147  } catch (\Sabre\DAV\Exception\NotFound $e) {
148  if (count($a_current_path_array) == 0) {
149  /* This is a really special case. It occurs, if the lock is meant for an object that does not
150  exist yet (so called NullRessources) since we can't ant won't lock non existing objects, we
151  set the Exception code to -1. The receiving class SHOULD handle what to do with this value */
152  throw new \Sabre\DAV\Exception\NotFound('Last node not found', -1);
153  } else {
154  // Set Exception code to 0, so their won't be any conflicts with default values
155  throw new \Sabre\DAV\Exception\NotFound('Node not found', 0);
156  }
157  }
158  }
159  }
160  return $current_ref_id;
161  }
162 
174  protected function getChildRefIdByGivenTitle(int $a_parent_ref_id, string $a_searched_title) : int
175  {
176  // Search if any child of the given ref has the name of the given searched element
177  foreach ($this->repo_helper->getChildrenOfRefId($a_parent_ref_id) as $child_ref) {
178  $child_title = $this->repo_helper->getObjectTitleFromRefId($child_ref, true);
179  if ($a_searched_title == $child_title) {
180  return $child_ref;
181  }
182  }
183 
184  throw new \Sabre\DAV\Exception\NotFound('Node not found');
185  }
186 }
getRefIdFromPathInRefMount(string $repository_mountpoint, string $path_inside_of_mountpoint)
Gets the ref_id from a searched object in the repository.
getChildRefIdByGivenTitle(int $a_parent_ref_id, string $a_searched_title)
Searches for a an object with a specific title inside an other object (identified by ref_id) ...
Class ilWebDAVRepositoryHelper.
getRefIdFromGivenParentRefAndTitlePath(int $a_parent_ref, array $a_current_path_array)
Searches an object inside the given path, starting at the given reference id.
getRefIdFromPathInRepositoryMount(string $path_inside_of_mountpoint)
Gets the ref_id from a searched object in the repository.
__construct(ilWebDAVRepositoryHelper $repo_helper)
ilWebDAVUriPathResolver constructor.
getRefIdForWebDAVPath(string $a_uri)
Returns the ref_id of the given webdav path.
Class ilWebDAVUriPathResolver.