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
24
25  /**
26   * Soap file administration methods
27   *
28   * @author Roland Küstermann <roland@kuestermann.com>
29   * @version $Id: class.ilSoapFileAdministration.php 12992 2007-01-25 10:04:26Z rkuester $
30   *
31   * @package ilias
32   */
33include_once './webservice/soap/classes/class.ilSoapAdministration.php';
34
35class ilSoapFileAdministration extends ilSoapAdministration
36{
37
38    /**
39     * add an File with id.
40     *
41     * @param string $session_id    current session
42     * @param int $target_id refid of parent in repository
43     * @param string $file_xml   qti xml description of test
44     *
45     * @return int reference id in the tree, 0 if not successful
46     */
47    public function addFile($sid, $target_id, $file_xml)
48    {
49        $this->initAuth($sid);
50        $this->initIlias();
51
52        if (!$this->__checkSession($sid)) {
53            return $this->__raiseError($this->__getMessage(), $this->__getMessageCode());
54        }
55        global $DIC;
56
57        $ilAccess   = $DIC['ilAccess'];
58
59        if (!$target_obj = ilObjectFactory::getInstanceByRefId($target_id, false)) {
60            return $this->__raiseError('No valid target given.', 'Client');
61        }
62
63        if (ilObject::_isInTrash($target_id)) {
64            return $this->__raiseError("Parent with ID $target_id has been deleted.", 'CLIENT_TARGET_DELETED');
65        }
66
67        // Check access
68        $allowed_types = array('cat', 'grp', 'crs', 'fold', 'root');
69        if (!in_array($target_obj->getType(), $allowed_types)) {
70            return $this->__raiseError('No valid target type. Target must be reference id of "course, group, category or folder"', 'Client');
71        }
72
73        if (!$ilAccess->checkAccess('create', '', $target_id, "file")) {
74            return $this->__raiseError('No permission to create Files in target  ' . $target_id . '!', 'Client');
75        }
76
77        // create object, put it into the tree and use the parser to update the settings
78        include_once './Modules/File/classes/class.ilFileXMLParser.php';
79        include_once './Modules/File/classes/class.ilFileException.php';
80        include_once './Modules/File/classes/class.ilObjFile.php';
81
82        $file = new ilObjFile();
83        try {
84            $fileXMLParser = new ilFileXMLParser($file, $file_xml);
85
86            if ($fileXMLParser->start()) {
87                global $DIC;
88
89                $ilLog = $DIC['ilLog'];
90
91                $ilLog->write(__METHOD__ . ': File type: ' . $file->getFileType());
92
93                $file->create();
94                $file->createReference();
95                $file->putInTree($target_id);
96                $file->setPermissions($target_id);
97
98                // we now can save the file contents since we know the obj id now.
99                $fileXMLParser->setFileContents();
100                #$file->update();
101
102                return $file->getRefId();
103            } else {
104                return $this->__raiseError("Could not add file", "Server");
105            }
106        } catch (ilFileException $exception) {
107            return $this->__raiseError($exception->getMessage(), $exception->getCode() == ilFileException::$ID_MISMATCH ? "Client" : "Server");
108        }
109    }
110
111
112    /**
113     * update a File with id.
114     *
115     * @param string $session_id    current session
116     * @param int $ref_id   refid id of File in repository
117     * @param string $file_xml   qti xml description of test
118     *
119     * @return boolean true, if update successful, false otherwise
120     */
121    public function updateFile($sid, $ref_id, $file_xml)
122    {
123        $this->initAuth($sid);
124        $this->initIlias();
125
126        if (!$this->__checkSession($sid)) {
127            return $this->__raiseError($this->__getMessage(), $this->__getMessageCode());
128        }
129        global $DIC;
130
131        $rbacsystem = $DIC['rbacsystem'];
132        $tree = $DIC['tree'];
133        $ilLog = $DIC['ilLog'];
134        $ilAccess = $DIC['ilAccess'];
135
136        if (ilObject::_isInTrash($ref_id)) {
137            return $this->__raiseError('Cannot perform update since file has been deleted.', 'CLIENT_OBJECT_DELETED');
138        }
139        // get obj_id
140        if (!$obj_id = ilObject::_lookupObjectId($ref_id)) {
141            return $this->__raiseError(
142                'No File found for id: ' . $ref_id,
143                'Client'
144            );
145        }
146
147        // Check access
148        $permission_ok = false;
149        foreach ($ref_ids = ilObject::_getAllReferences($obj_id) as $ref_id) {
150            if ($ilAccess->checkAccess('write', '', $ref_id)) {
151                $permission_ok = true;
152                break;
153            }
154        }
155
156        if (!$permission_ok) {
157            return $this->__raiseError(
158                'No permission to edit the File with id: ' . $ref_id,
159                'Server'
160            );
161        }
162
163
164        $file = ilObjectFactory::getInstanceByObjId($obj_id, false);
165
166        if (!is_object($file) || $file->getType()!= "file") {
167            return $this->__raiseError(
168                'Wrong obj id or type for File with id ' . $ref_id,
169                'Server'
170            );
171        }
172
173        include_once './Modules/File/classes/class.ilFileXMLParser.php';
174        include_once './Modules/File/classes/class.ilFileException.php';
175        $fileXMLParser = new ilFileXMLParser($file, $file_xml, $obj_id);
176
177        try {
178            if ($fileXMLParser->start()) {
179                $fileXMLParser->updateFileContents();
180
181                return  $file->update();
182            }
183        } catch (ilFileException $exception) {
184            return $this->__raiseError(
185               $exception->getMessage(),
186               $exception->getCode() == ilFileException::$ID_MISMATCH ? "Client" : "Server"
187           );
188        }
189        return false;
190    }
191
192    /**
193     * get File xml
194     *
195     * @param string $sid
196     * @param int $ref_id
197     * @param boolean $attachFileContentsMode
198     *
199     * @return xml following ilias_file_x.dtd
200     */
201
202    public function getFileXML($sid, $ref_id, $attachFileContentsMode)
203    {
204        $this->initAuth($sid);
205        $this->initIlias();
206
207        if (!$this->__checkSession($sid)) {
208            return $this->__raiseError($this->__getMessage(), $this->__getMessageCode());
209        }
210        if (!strlen($ref_id)) {
211            return $this->__raiseError(
212                'No ref id given. Aborting!',
213                'Client'
214            );
215        }
216        global $DIC;
217
218        $rbacsystem = $DIC['rbacsystem'];
219        $tree = $DIC['tree'];
220        $ilLog = $DIC['ilLog'];
221        $ilAccess = $DIC['ilAccess'];
222
223
224        // get obj_id
225        if (!$obj_id = ilObject::_lookupObjectId($ref_id)) {
226            return $this->__raiseError(
227                'No File found for id: ' . $ref_id,
228                'Client'
229            );
230        }
231
232        if (ilObject::_isInTrash($ref_id)) {
233            return $this->__raiseError("Object with ID $ref_id has been deleted.", 'Client');
234        }
235
236        // Check access
237        $permission_ok = false;
238        foreach ($ref_ids = ilObject::_getAllReferences($obj_id) as $ref_id) {
239            if ($ilAccess->checkAccess('read', '', $ref_id)) {
240                $permission_ok = true;
241                break;
242            }
243        }
244
245        if (!$permission_ok) {
246            return $this->__raiseError(
247                'No permission to edit the object with id: ' . $ref_id,
248                'Server'
249            );
250        }
251
252        $file = ilObjectFactory::getInstanceByObjId($obj_id, false);
253
254        if (!is_object($file) || $file->getType()!= "file") {
255            return $this->__raiseError(
256                'Wrong obj id or type for File with id ' . $ref_id,
257                'Server'
258            );
259        }
260        // store into xml result set
261        include_once './Modules/File/classes/class.ilFileXMLWriter.php';
262
263        // create writer
264        $xmlWriter = new ilFileXMLWriter();
265        $xmlWriter->setFile($file);
266        $xmlWriter->setAttachFileContents($attachFileContentsMode);
267        $xmlWriter->start();
268
269        return $xmlWriter->getXML();
270    }
271}
272