ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilDAVContainer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 use Sabre\DAV\INode;
25 
29 class ilDAVContainer implements ICollection
30 {
35 
36  public function __construct(
37  protected ilObject $obj,
38  protected ilObjUser $current_user,
39  protected RequestInterface $request,
40  protected ilWebDAVObjFactory $dav_factory,
41  protected ilWebDAVRepositoryHelper $repository_helper
42  ) {
43  }
44 
45  public function getName(): string
46  {
47  return $this->obj->getTitle();
48  }
49 
54  public function getChild($name): INode
55  {
56  return $this->getChildByParentRefId(
57  $this->repository_helper,
58  $this->dav_factory,
59  $this->obj->getRefId(),
60  $name
61  );
62  }
63 
67  public function getChildren(): array
68  {
69  return $this->getChildrenByParentRefId(
70  $this->repository_helper,
71  $this->dav_factory,
72  $this->obj->getRefId()
73  );
74  }
75 
80  public function childExists($name): bool
81  {
82  return $this->checkIfChildExistsByParentRefId(
83  $this->repository_helper,
84  $this->dav_factory,
85  $this->obj->getRefId(),
86  $name
87  );
88  }
89 
94  public function setName($name): void
95  {
96  if (!$this->repository_helper->checkAccess("write", $this->obj->getRefId())) {
97  throw new Forbidden('Permission denied');
98  }
99 
100  if ($this->isDAVableObjTitle($name)) {
101  $this->obj->setTitle($name);
102  $this->obj->update();
103  } else {
104  throw new Forbidden('Forbidden characters in title');
105  }
106  }
107 
112  public function createFile($name, $data = null): ?string
113  {
114  if (!$this->repository_helper->checkCreateAccessForType($this->obj->getRefId(), 'file')) {
115  throw new Forbidden('Permission denied');
116  }
117 
118  $size = 0;
119  if ($this->request->hasHeader("Content-Length")) {
120  $size = (int) $this->request->getHeader("Content-Length")[0];
121  }
122  if ($size === 0 && $this->request->hasHeader('X-Expected-Entity-Length')) {
123  $size = (int) $this->request->getHeader('X-Expected-Entity-Length')[0];
124  }
126  throw new Forbidden('File is too big');
127  }
128 
129  $name = $this->ensureSuffix($name, $this->extractSuffixFromFilename($name));
130 
131  if ($this->childExists($name)) {
132  $file_dav = $this->getChild($name);
133  } else {
134  try {
135  $file_obj = new ilObjFile();
136  $file_obj->setTitle($name);
137 
138  $file_dav = $this->dav_factory->createDAVObject($file_obj, $this->obj->getRefId());
139  } catch (ilWebDAVNotDavableException) {
140  throw new Forbidden('Forbidden characters in title');
141  }
142  }
143 
144  try {
145  return $file_dav->put($data, $name);
146  } catch (Forbidden $f) {
147  throw $f;
148  } catch (Throwable) {
149  return null;
150  }
151  }
152 
157  public function createDirectory($name): void
158  {
159  $new_obj = $this->getChildCollection();
160 
161  if (!$this->repository_helper->checkCreateAccessForType($this->obj->getRefId(), $new_obj->getType())) {
162  throw new Forbidden('Permission denied');
163  }
164 
165  try {
166  $new_obj->setOwner($this->current_user->getId());
167  $new_obj->setTitle($name);
168  $this->dav_factory->createDAVObject($new_obj, $this->obj->getRefId());
169  } catch (ilWebDAVNotDavableException) {
170  throw new Forbidden('Forbidden characters in title');
171  }
172  }
173 
174  public function delete(): void
175  {
176  $this->repository_helper->deleteObject($this->obj->getRefId());
177  }
178 
179  public function getLastModified(): ?int
180  {
181  return $this->retrieveLastModifiedAsIntFromObjectLastUpdateString($this->obj->getLastUpdateDate());
182  }
183 
184  protected function getChildCollection(): ilContainer
185  {
186  if ($this->obj::class === 'ilObjCategory') {
187  return new ilObjCategory();
188  }
189 
190  return new ilObjFolder();
191  }
192 }
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
createFile($name, $data=null)
Class ilObjFile.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
extractSuffixFromFilename(string $filename)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
trait ilObjFileSecureString
Trait ilObjFileSecureString.
static getPhpUploadSizeLimitInBytes()
__construct(protected ilObject $obj, protected ilObjUser $current_user, protected RequestInterface $request, protected ilWebDAVObjFactory $dav_factory, protected ilWebDAVRepositoryHelper $repository_helper)
ensureSuffix(string $title, ?string $suffix=null)