ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilSoapFileAdministration.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
30 {
34  public function addFile(string $sid, int $target_id, string $file_xml)
35  {
36  $this->initAuth($sid);
37  $this->initIlias();
38 
39  if (!$this->checkSession($sid)) {
40  return $this->raiseError($this->getMessage(), $this->getMessageCode());
41  }
42  global $DIC;
43 
44  $ilAccess = $DIC['ilAccess'];
45 
46  if (!$target_obj = ilObjectFactory::getInstanceByRefId($target_id, false)) {
47  return $this->raiseError('No valid target given.', 'Client');
48  }
49 
50  if (ilObject::_isInTrash($target_id)) {
51  return $this->raiseError("Parent with ID $target_id has been deleted.", 'CLIENT_TARGET_DELETED');
52  }
53 
54  $allowed_types = array('cat', 'grp', 'crs', 'fold', 'root');
55  if (!in_array($target_obj->getType(), $allowed_types)) {
56  return $this->raiseError(
57  'No valid target type. Target must be reference id of "course, group, category or folder"',
58  'Client'
59  );
60  }
61 
62  if (!$ilAccess->checkAccess('create', '', $target_id, "file")) {
63  return $this->raiseError('No permission to create Files in target ' . $target_id . '!', 'Client');
64  }
65 
66  // create object, put it into the tree and use the parser to update the settings
67 
68  $file = new ilObjFile();
69  try {
70  $fileXMLParser = new ilFileXMLParser($file, $file_xml);
71 
72  if ($fileXMLParser->start()) {
73  global $DIC;
74 
75  $ilLog = $DIC['ilLog'];
76 
77  $ilLog->write(__METHOD__ . ': File type: ' . $file->getFileType());
78 
79  $file->create();
80  $file->createReference();
81  $file->putInTree($target_id);
82  $file->setPermissions($target_id);
83 
84  // we now can save the file contents since we know the obj id now.
85  $fileXMLParser->setFileContents();
86  #$file->update();
87 
88  return $file->getRefId();
89  }
90 
91  return $this->raiseError("Could not add file", "Server");
92  } catch (ilFileException $exception) {
93  return $this->raiseError(
94  $exception->getMessage(),
95  $exception->getCode() == ilFileException::$ID_MISMATCH ? "Client" : "Server"
96  );
97  }
98  }
99 
103  public function updateFile(string $sid, int $requested_ref_id, string $file_xml)
104  {
105  $this->initAuth($sid);
106  $this->initIlias();
107 
108  if (!$this->checkSession($sid)) {
109  return $this->raiseError($this->getMessage(), $this->getMessageCode());
110  }
111  global $DIC;
112 
113  $rbacsystem = $DIC['rbacsystem'];
114  $tree = $DIC['tree'];
115  $ilLog = $DIC['ilLog'];
116  $ilAccess = $DIC['ilAccess'];
117 
118  if (ilObject::_isInTrash($requested_ref_id)) {
119  return $this->raiseError('Cannot perform update since file has been deleted.', 'CLIENT_OBJECT_DELETED');
120  }
121 
122  if (!$obj_id = ilObject::_lookupObjectId($requested_ref_id)) {
123  return $this->raiseError(
124  'No File found for id: ' . $requested_ref_id,
125  'Client'
126  );
127  }
128 
129  $permission_ok = false;
130  foreach ($ref_ids = ilObject::_getAllReferences($obj_id) as $ref_id) {
131  if ($ilAccess->checkAccess('write', '', $ref_id)) {
132  $permission_ok = true;
133  break;
134  }
135  }
136 
137  if (!$permission_ok) {
138  return $this->raiseError(
139  'No permission to edit the File with id: ' . $requested_ref_id,
140  'Server'
141  );
142  }
143 
145  $file = ilObjectFactory::getInstanceByObjId($obj_id, false);
146 
147  if (!is_object($file) || $file->getType() !== "file") {
148  return $this->raiseError(
149  'Wrong obj id or type for File with id ' . $requested_ref_id,
150  'Server'
151  );
152  }
153 
154  $fileXMLParser = new ilFileXMLParser($file, $file_xml, $obj_id);
155 
156  try {
157  if ($fileXMLParser->start()) {
158  $fileXMLParser->updateFileContents();
159 
160  return $file->update();
161  }
162  } catch (ilFileException $exception) {
163  return $this->raiseError(
164  $exception->getMessage(),
165  $exception->getCode() == ilFileException::$ID_MISMATCH ? "Client" : "Server"
166  );
167  }
168  return false;
169  }
170 
174  public function getFileXML(string $sid, int $requested_ref_id, int $attachFileContentsMode)
175  {
176  $this->initAuth($sid);
177  $this->initIlias();
178 
179  if (!$this->checkSession($sid)) {
180  return $this->raiseError($this->getMessage(), $this->getMessageCode());
181  }
182 
183  if (!($requested_ref_id > 0)) {
184  return $this->raiseError(
185  'No ref id given. Aborting!',
186  'Client'
187  );
188  }
189 
190  global $DIC;
191 
192  $rbacsystem = $DIC['rbacsystem'];
193  $tree = $DIC['tree'];
194  $ilLog = $DIC['ilLog'];
195  $ilAccess = $DIC['ilAccess'];
196 
197  if (!$obj_id = ilObject::_lookupObjectId($requested_ref_id)) {
198  return $this->raiseError(
199  'No File found for id: ' . $requested_ref_id,
200  'Client'
201  );
202  }
203 
204  if (ilObject::_isInTrash($requested_ref_id)) {
205  return $this->raiseError("Object with ID $requested_ref_id has been deleted.", 'Client');
206  }
207 
208  $permission_ok = false;
209  foreach ($ref_ids = ilObject::_getAllReferences($obj_id) as $ref_id) {
210  if ($ilAccess->checkAccess('read', '', $ref_id)) {
211  $permission_ok = true;
212  break;
213  }
214  }
215 
216  if (!$permission_ok) {
217  return $this->raiseError(
218  'No permission to edit the object with id: ' . $requested_ref_id,
219  'Server'
220  );
221  }
222 
224  $file = ilObjectFactory::getInstanceByObjId($obj_id, false);
225 
226  if (!is_object($file) || $file->getType() !== "file") {
227  return $this->raiseError(
228  'Wrong obj id or type for File with id ' . $requested_ref_id,
229  'Server'
230  );
231  }
232 
233 
234  $xmlWriter = new ilFileXMLWriter();
235  $xmlWriter->setFile($file);
236  $xmlWriter->setAttachFileContents($attachFileContentsMode);
237  $xmlWriter->start();
238 
239  return $xmlWriter->getXML();
240  }
241 }
static _getAllReferences(int $id)
get all reference ids for object ID
raiseError(string $a_message, $a_code)
Soap file administration methods.
$ref_id
Definition: ltiauth.php:66
$requested_ref_id
Definition: feed.php:41
static _isInTrash(int $ref_id)
static getInstanceByRefId(int $ref_id, bool $stop_on_error=true)
get an instance of an Ilias object by reference id
Class ilObjFile.
global $DIC
Definition: shib_login.php:25
static _lookupObjectId(int $ref_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getInstanceByObjId(?int $obj_id, bool $stop_on_error=true)
get an instance of an Ilias object by object id
addFile(string $sid, int $target_id, string $file_xml)