1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
5include_once("./Services/JSON/classes/class.ilJsonUtil.php");
6
7/**
8 * Class ilCloudPluginUploadGUI
9 *
10 * Standard class for uploading files. Can be overwritten if needed.
11 *
12 * @author  Timon Amstutz <timon.amstutz@ilub.unibe.ch>
13 * @version $Id:
14 * @extends ilCloudPluginGUI
15 * @ingroup ModulesCloud
16 */
17class ilCloudPluginUploadGUI extends ilCloudPluginGUI
18{
19
20    /**
21     * @var ilPropertyFormGUI
22     */
23    protected $form;
24
25
26    /**
27     * execute command
28     */
29    public function executeCommand()
30    {
31        global $DIC;
32        $ilCtrl = $DIC['ilCtrl'];
33
34        $cmd = $ilCtrl->getCmd();
35
36        switch ($cmd) {
37            default:
38                $this->$cmd();
39                break;
40        }
41    }
42
43
44    public function asyncUploadFile()
45    {
46        global $DIC;
47        $ilTabs = $DIC['ilTabs'];
48
49        $ilTabs->activateTab("content");
50        $this->initUploadForm();
51        echo $this->form->getHTML();
52
53        $options = new stdClass();
54        $options->dropZone = "#ilFileUploadDropZone_1";
55        $options->fileInput = "#ilFileUploadInput_1";
56        $options->submitButton = "uploadFiles";
57        $options->cancelButton = "cancelAll";
58        $options->dropArea = "#ilFileUploadDropArea_1";
59        $options->fileList = "#ilFileUploadList_1";
60        $options->fileSelectButton = "#ilFileUploadFileSelect_1";
61        echo "<script language='javascript' type='text/javascript'>var fileUpload1 = new ilFileUpload(1, " . ilJsonUtil::encode($options) . ");</script>";
62
63        $_SESSION["cld_folder_id"] = $_POST["folder_id"];
64
65        exit;
66    }
67
68
69    public function initUploadForm()
70    {
71        global $DIC;
72        $ilCtrl = $DIC['ilCtrl'];
73        $lng = $DIC['lng'];
74
75        include_once("./Services/Form/classes/class.ilDragDropFileInputGUI.php");
76        include_once("./Services/jQuery/classes/class.iljQueryUtil.php");
77
78        $this->form = new ilPropertyFormGUI();
79        $this->form->setId("upload");
80        $this->form->setMultipart(true);
81        $this->form->setHideLabels();
82
83        $file = new ilDragDropFileInputGUI($lng->txt("cld_upload_files"), "upload_files");
84        $file->setRequired(true);
85        $this->form->addItem($file);
86
87        $this->form->addCommandButton("uploadFiles", $lng->txt("upload"));
88        $this->form->addCommandButton("cancelAll", $lng->txt("cancel"));
89
90        $this->form->setTableWidth("100%");
91        $this->form->setTitle($lng->txt("upload_files_title"));
92        //        $this->form->setTitleIcon(ilUtil::getImagePath('icon_file.gif'), $lng->txt('obj_file'));
93        $this->form->setTitleIcon(ilUtil::getImagePath('icon_dcl_file.svg'), $lng->txt('obj_file'));
94
95        $this->form->setTitle($lng->txt("upload_files"));
96        $this->form->setFormAction($ilCtrl->getFormAction($this, "uploadFiles"));
97        $this->form->setTarget("cld_blank_target");
98    }
99
100
101    public function cancelAll()
102    {
103        echo "<script language='javascript' type='text/javascript'>window.parent.il.CloudFileList.afterUpload('cancel');</script>";
104        exit;
105    }
106
107
108    /**
109     * Update properties
110     */
111
112    public function uploadFiles()
113    {
114        $response = new stdClass();
115        $response->error = null;
116        $response->debug = null;
117
118        $this->initUploadForm();
119        if ($this->form->checkInput()) {
120            try {
121                $fileresult = $this->handleFileUpload($this->form->getInput("upload_files"));
122                if ($fileresult) {
123                    $response = (object) array_merge((array) $response, (array) $fileresult);
124                }
125            } catch (ilException $e) {
126                $response->error = $e->getMessage();
127            }
128        } else {
129            $error = new ilCloudException(ilCloudException::UPLOAD_FAILED);
130            $response->error = $error->getMessage();
131        }
132
133        // send response object (don't use 'application/json' as IE wants to download it!)
134        header('Vary: Accept');
135        header('Content-type: text/plain');
136        echo ilJsonUtil::encode($response);
137        exit;
138    }
139
140
141    public function handleFileUpload($file_upload)
142    {
143        // create answer object
144        $response = new stdClass();
145        $response->fileName = $_POST["title"];
146        $response->fileSize = intval($file_upload["size"]);
147        $response->fileType = $file_upload["type"];
148        $response->fileUnzipped = $file_upload["extract"];
149        $response->error = null;
150
151        $file_tree = ilCloudFileTree::getFileTreeFromSession();
152
153        if ($file_upload["extract"]) {
154            $newdir = ilUtil::ilTempnam();
155            ilUtil::makeDir($newdir);
156
157            include_once './Services/Utilities/classes/class.ilFileUtils.php';
158            try {
159                ilFileUtils::processZipFile($newdir, $file_upload["tmp_name"], $file_upload["keep_structure"]);
160            } catch (Exception $e) {
161                $response->error = $e->getMessage();
162                ilUtil::delDir($newdir);
163                exit;
164            }
165
166            try {
167                $this->uploadDirectory($newdir, $_SESSION["cld_folder_id"], $file_tree, $file_upload["keep_structure"]);
168            } catch (Exception $e) {
169                $response->error = $e->getMessage();
170                ilUtil::delDir($newdir);
171                exit;
172            }
173
174            ilUtil::delDir($newdir);
175
176            return $response;
177        } else {
178            $file_tree->uploadFileToService($_SESSION["cld_folder_id"], $file_upload["tmp_name"], $_POST["title"]);
179
180            return $response;
181        }
182    }
183
184
185    /**
186     * Recursive Method to upload a directory
187     *
188     * @param string          $dir            path to directory
189     * @param int             $parent_id      id of parent folder
190     * @param ilCloudFileTree $file_tree
191     * @param bool            $keep_structure if false, only files will be extracted, without folder structure
192     *
193     * @throws ilCloudException
194     */
195    protected function uploadDirectory($dir, $parent_id, $file_tree, $keep_structure = true)
196    {
197        $dirlist = opendir($dir);
198
199        while (false !== ($file = readdir($dirlist))) {
200            if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file)) {
201                global $DIC;
202                $lng = $DIC['lng'];
203                throw new ilCloudException($lng->txt("filenames_not_supported"), ilFileUtilsException::$BROKEN_FILE);
204            }
205            if ($file != '.' && $file != '..') {
206                $newpath = $dir . '/' . $file;
207                if (is_dir($newpath)) {
208                    if ($keep_structure) {
209                        $newnode = $file_tree->addFolderToService($parent_id, basename($newpath));
210                        $this->uploadDirectory($newpath, $newnode->getId(), $file_tree);
211                    } else {
212                        $this->uploadDirectory($newpath, $parent_id, $file_tree, false);
213                    }
214                } else {
215                    $file_tree->uploadFileToService($parent_id, $newpath, basename($newpath));
216                }
217            }
218        }
219        closedir($dirlist);
220    }
221}
222