1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("./Services/Repository/classes/class.ilObjectPlugin.php");
5include_once("class.ilCloudUtil.php");
6include_once("class.ilCloudConnector.php");
7
8/**
9 * Class ilObjCloud
10 *
11 * @author Timon Amstutz <timon.amstutz@ilub.unibe.ch>
12 * $Id$
13 *
14 * @extends ilObject2
15 */
16class ilObjCloud extends ilObject2
17{
18    /**
19     * @var bool
20     */
21    protected $online = false;
22
23    /**
24     * @var string
25     */
26    protected $service = "";
27
28    /**
29     * @var string
30     */
31    protected $root_folder = "";
32
33    /**
34     * @var int
35     */
36    protected $owner_id = 0;
37
38    /**
39     * @var bool
40     */
41    protected $auth_complete = false;
42
43
44    /**
45     * Constructor
46     *
47     * @access    public
48     */
49    public function __construct($a_ref_id = 0, $a_reference = true)
50    {
51        parent::__construct($a_ref_id, $a_reference);
52    }
53
54    /*
55 * initType
56 */
57    public function initType()
58    {
59        $this->type = "cld";
60    }
61
62    /**
63     * Create object
64     */
65    public function doCreate()
66    {
67        global $DIC;
68        $ilDB = $DIC['ilDB'];
69        $ilUser = $DIC['ilUser'];
70
71        /**
72         * The owner of the cloud Object is only set once, when the object is created. This prevents, that users
73         * access data of a cloud folder which they are not authorized to through changing the root_path of the cloud_folder
74         */
75        $this->setOwnerId($ilUser->getId());
76        $ilDB->manipulate("INSERT INTO il_cld_data " .
77            "(id, is_online, service, root_folder, root_id, owner_id, auth_complete) VALUES (" .
78            $ilDB->quote($this->getId(), "integer") . "," .
79            $ilDB->quote("", "integer") . "," .
80            $ilDB->quote("", "text") . "," .
81            $ilDB->quote("", "text") . "," .
82            $ilDB->quote("", "text") . "," .
83            $ilDB->quote($this->getOwnerId(), "integer") . "," .
84            $ilDB->quote("", "integer") .
85            ")");
86    }
87
88    /**
89     * Read data from db
90     */
91    public function doRead()
92    {
93        global $DIC;
94        $ilDB = $DIC['ilDB'];
95
96        $set = $ilDB->query("SELECT * FROM il_cld_data " .
97            " WHERE id = " . $ilDB->quote($this->getId(), "integer"));
98
99        while ($rec = $ilDB->fetchAssoc($set)) {
100            $this->setOnline($rec["is_online"]);
101            $this->setRootFolder($rec["root_folder"]);
102            $this->setRootId($rec["root_id"]);
103            $this->setServiceName($rec["service"]);
104            $this->setOwnerId($rec["owner_id"]);
105            $this->setAuthComplete($rec["auth_complete"]);
106        }
107    }
108
109    /**
110     * Update data
111     */
112    public function doUpdate()
113    {
114        global $DIC;
115        $ilDB = $DIC['ilDB'];
116
117        $ilDB->manipulate(
118            $up = "UPDATE il_cld_data SET " .
119                " is_online = " . $ilDB->quote($this->getOnline(), "integer") . "," .
120                " service = " . $ilDB->quote($this->getServiceName(), "text") . "," .
121                " root_folder = " . $ilDB->quote($this->getRootFolder(), "text") . "," .
122                " root_id = " . $ilDB->quote($this->getRootId(), "text") . "," .
123                " owner_id = " . $ilDB->quote($this->getOwnerId(), "integer") . "," .
124                " auth_complete = " . $ilDB->quote($this->getAuthComplete(), "integer") .
125                " WHERE id = " . $ilDB->quote($this->getId(), "integer")
126        );
127    }
128
129    /**
130     * Delete data from db
131     */
132    public function doDelete()
133    {
134        global $DIC;
135        $ilDB = $DIC['ilDB'];
136
137        if ($this->getServiceName() != null) {
138            $plugin_class = ilCloudConnector::getPluginClass($this->getServiceName(), $this->getId());
139            if ($plugin_class) {
140                $plugin_class->doDelete($this->getId());
141            }
142        }
143
144        $ilDB->manipulate(
145            "DELETE FROM il_cld_data WHERE " .
146            " id = " . $ilDB->quote($this->getId(), "integer")
147        );
148    }
149
150    /**
151     * Do Cloning
152     */
153    public function doClone($a_target_id, $a_copy_id, $new_obj)
154    {
155        global $DIC;
156        $ilDB = $DIC['ilDB'];
157
158        //copy online status if object is not the root copy object
159        $cp_options = ilCopyWizardOptions::_getInstance($a_copy_id);
160
161        if (!$cp_options->isRootNode($this->getRefId())) {
162            $new_obj->setOnline($this->getOnline());
163        }
164
165        $new_obj->setRootFolder($this->getRootFolder());
166        $new_obj->getRootId($this->getRootId());
167        $new_obj->setServiceName($this->getServiceName());
168        $new_obj->serOwnerId($this->getOwnerId());
169        $new_obj->setAuthComplete($this->getAuthComplete());
170        $new_obj->update();
171    }
172
173//
174    // Set/Get Methods for our example properties
175//
176
177    /**
178     * Set online
179     *
180     * @param    boolean        online
181     */
182    public function setOnline($a_val)
183    {
184        $this->online = $a_val;
185    }
186
187    /**
188     * Get online
189     *
190     * @return    boolean        online
191     */
192    public function getOnline()
193    {
194        return $this->online;
195    }
196
197    /**
198     * Set root_folder, this may only be changed by the owner of the object. This is due too security constraints.
199     * The parameter $no_check could be used by plugins for which these constraint is not an issue.
200     *
201     * @param    string        root_folder
202     */
203    public function setRootFolder($a_val, $no_check = false)
204    {
205        if ($this->currentUserIsOwner() || $no_check || $this->getRootFolder() == null) {
206            $a_val = ilCloudUtil::normalizePath($a_val);
207            $this->root_folder = $a_val;
208        } else {
209            throw new ilCloudException(ilCloudException::PERMISSION_TO_CHANGE_ROOT_FOLDER_DENIED);
210        }
211    }
212
213    /**
214     * Get root_folder
215     *
216     * @return    string        root_folder
217     */
218    public function getRootFolder()
219    {
220        return $this->root_folder;
221    }
222
223    /**
224     * Set root_id
225     *
226     * @param    string        root_id
227     */
228    public function setRootId($a_val, $no_check = false)
229    {
230        if ($this->currentUserIsOwner() || $no_check || $this->getRootId() == null) {
231            $this->root_id = $a_val;
232        } else {
233            throw new ilCloudException(ilCloudException::PERMISSION_TO_CHANGE_ROOT_FOLDER_DENIED);
234        }
235    }
236
237    /**
238     * Get root_id
239     *
240     * @return    string        root_id
241     */
242    public function getRootId()
243    {
244        return $this->root_id;
245    }
246
247    /**
248     * Set service
249     *
250     * @param    string        service
251     */
252    public function setServiceName($a_val)
253    {
254        $this->service_name = $a_val;
255    }
256
257    /**
258     * Get service
259     *
260     * @return    string        service
261     */
262    public function getServiceName()
263    {
264        return $this->service_name;
265    }
266
267    /**
268     * @param int $owner_id
269     */
270    public function setOwnerId($owner_id)
271    {
272        $this->owner_id = $owner_id;
273    }
274
275    /**
276     * @return int
277     */
278    public function getOwnerId()
279    {
280        return $this->owner_id;
281    }
282
283    /**
284     * @return bool
285     */
286    public function currentUserIsOwner()
287    {
288        global $DIC;
289        $ilUser = $DIC['ilUser'];
290        return $ilUser->getId() == $this->getOwnerId();
291    }
292
293    /**
294     * @param boolean $auth_complete
295     */
296    public function setAuthComplete($auth_complete)
297    {
298        $this->auth_complete = $auth_complete;
299    }
300
301    /**
302     * @return boolean
303     */
304    public function getAuthComplete()
305    {
306        return $this->auth_complete;
307    }
308}
309