ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
28 include_once './webservice/soap/classes/class.ilSoapAdministration.php';
29 
31 {
35  public function addFile(string $sid, int $target_id, string $file_xml)
36  {
37  $this->initAuth($sid);
38  $this->initIlias();
39 
40  if (!$this->checkSession($sid)) {
41  return $this->raiseError($this->getMessage(), $this->getMessageCode());
42  }
43  global $DIC;
44 
45  $ilAccess = $DIC['ilAccess'];
46 
47  if (!$target_obj = ilObjectFactory::getInstanceByRefId($target_id, false)) {
48  return $this->raiseError('No valid target given.', 'Client');
49  }
50 
51  if (ilObject::_isInTrash($target_id)) {
52  return $this->raiseError("Parent with ID $target_id has been deleted.", 'CLIENT_TARGET_DELETED');
53  }
54 
55  $allowed_types = array('cat', 'grp', 'crs', 'fold', 'root');
56  if (!in_array($target_obj->getType(), $allowed_types)) {
57  return $this->raiseError(
58  'No valid target type. Target must be reference id of "course, group, category or folder"',
59  'Client'
60  );
61  }
62 
63  if (!$ilAccess->checkAccess('create', '', $target_id, "file")) {
64  return $this->raiseError('No permission to create Files in target ' . $target_id . '!', 'Client');
65  }
66 
67  // create object, put it into the tree and use the parser to update the settings
68  include_once './Modules/File/classes/class.ilFileXMLParser.php';
69  include_once './Modules/File/classes/class.ilFileException.php';
70  include_once './Modules/File/classes/class.ilObjFile.php';
71 
72  $file = new ilObjFile();
73  try {
74  $fileXMLParser = new ilFileXMLParser($file, $file_xml);
75 
76  if ($fileXMLParser->start()) {
77  global $DIC;
78 
79  $ilLog = $DIC['ilLog'];
80 
81  $ilLog->write(__METHOD__ . ': File type: ' . $file->getFileType());
82 
83  $file->create();
84  $file->createReference();
85  $file->putInTree($target_id);
86  $file->setPermissions($target_id);
87 
88  // we now can save the file contents since we know the obj id now.
89  $fileXMLParser->setFileContents();
90  #$file->update();
91 
92  return $file->getRefId();
93  }
94 
95  return $this->raiseError("Could not add file", "Server");
96  } catch (ilFileException $exception) {
97  return $this->raiseError(
98  $exception->getMessage(),
99  $exception->getCode() == ilFileException::$ID_MISMATCH ? "Client" : "Server"
100  );
101  }
102  }
103 
107  public function updateFile(string $sid, int $requested_ref_id, string $file_xml)
108  {
109  $this->initAuth($sid);
110  $this->initIlias();
111 
112  if (!$this->checkSession($sid)) {
113  return $this->raiseError($this->getMessage(), $this->getMessageCode());
114  }
115  global $DIC;
116 
117  $rbacsystem = $DIC['rbacsystem'];
118  $tree = $DIC['tree'];
119  $ilLog = $DIC['ilLog'];
120  $ilAccess = $DIC['ilAccess'];
121 
122  if (ilObject::_isInTrash($requested_ref_id)) {
123  return $this->raiseError('Cannot perform update since file has been deleted.', 'CLIENT_OBJECT_DELETED');
124  }
125 
126  if (!$obj_id = ilObject::_lookupObjectId($requested_ref_id)) {
127  return $this->raiseError(
128  'No File found for id: ' . $requested_ref_id,
129  'Client'
130  );
131  }
132 
133  $permission_ok = false;
134  foreach ($ref_ids = ilObject::_getAllReferences($obj_id) as $ref_id) {
135  if ($ilAccess->checkAccess('write', '', $ref_id)) {
136  $permission_ok = true;
137  break;
138  }
139  }
140 
141  if (!$permission_ok) {
142  return $this->raiseError(
143  'No permission to edit the File with id: ' . $requested_ref_id,
144  'Server'
145  );
146  }
147 
149  $file = ilObjectFactory::getInstanceByObjId($obj_id, false);
150 
151  if (!is_object($file) || $file->getType() !== "file") {
152  return $this->raiseError(
153  'Wrong obj id or type for File with id ' . $requested_ref_id,
154  'Server'
155  );
156  }
157 
158  include_once './Modules/File/classes/class.ilFileXMLParser.php';
159  include_once './Modules/File/classes/class.ilFileException.php';
160  $fileXMLParser = new ilFileXMLParser($file, $file_xml, $obj_id);
161 
162  try {
163  if ($fileXMLParser->start()) {
164  $fileXMLParser->updateFileContents();
165 
166  return $file->update();
167  }
168  } catch (ilFileException $exception) {
169  return $this->raiseError(
170  $exception->getMessage(),
171  $exception->getCode() == ilFileException::$ID_MISMATCH ? "Client" : "Server"
172  );
173  }
174  return false;
175  }
176 
180  public function getFileXML(string $sid, int $requested_ref_id, int $attachFileContentsMode)
181  {
182  $this->initAuth($sid);
183  $this->initIlias();
184 
185  if (!$this->checkSession($sid)) {
186  return $this->raiseError($this->getMessage(), $this->getMessageCode());
187  }
188 
189  if (!($requested_ref_id > 0)) {
190  return $this->raiseError(
191  'No ref id given. Aborting!',
192  'Client'
193  );
194  }
195 
196  global $DIC;
197 
198  $rbacsystem = $DIC['rbacsystem'];
199  $tree = $DIC['tree'];
200  $ilLog = $DIC['ilLog'];
201  $ilAccess = $DIC['ilAccess'];
202 
203  if (!$obj_id = ilObject::_lookupObjectId($requested_ref_id)) {
204  return $this->raiseError(
205  'No File found for id: ' . $requested_ref_id,
206  'Client'
207  );
208  }
209 
210  if (ilObject::_isInTrash($requested_ref_id)) {
211  return $this->raiseError("Object with ID $requested_ref_id has been deleted.", 'Client');
212  }
213 
214  $permission_ok = false;
215  foreach ($ref_ids = ilObject::_getAllReferences($obj_id) as $ref_id) {
216  if ($ilAccess->checkAccess('read', '', $ref_id)) {
217  $permission_ok = true;
218  break;
219  }
220  }
221 
222  if (!$permission_ok) {
223  return $this->raiseError(
224  'No permission to edit the object with id: ' . $requested_ref_id,
225  'Server'
226  );
227  }
228 
230  $file = ilObjectFactory::getInstanceByObjId($obj_id, false);
231 
232  if (!is_object($file) || $file->getType() !== "file") {
233  return $this->raiseError(
234  'Wrong obj id or type for File with id ' . $requested_ref_id,
235  'Server'
236  );
237  }
238 
239  include_once './Modules/File/classes/class.ilFileXMLWriter.php';
240 
241  $xmlWriter = new ilFileXMLWriter();
242  $xmlWriter->setFile($file);
243  $xmlWriter->setAttachFileContents($attachFileContentsMode);
244  $xmlWriter->start();
245 
246  return $xmlWriter->getXML();
247  }
248 }
static _getAllReferences(int $id)
get all reference ids for object ID
raiseError(string $a_message, $a_code)
$target_id
Definition: goto.php:52
global $DIC
Definition: feed.php:28
Soap file administration methods.
$ref_id
Definition: ltiauth.php:67
$requested_ref_id
Definition: feed.php:40
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.
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)