ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilCloudPluginUploadGUI.php
Go to the documentation of this file.
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
18{
22 protected $form;
26 public function executeCommand()
27 {
28 global $DIC;
29 $ilCtrl = $DIC['ilCtrl'];
30
31 $cmd = $ilCtrl->getCmd();
32
33 switch ($cmd) {
34 default:
35 $this->$cmd();
36 break;
37 }
38 }
39
40 public function asyncUploadFile()
41 {
42 global $DIC;
43 $ilTabs = $DIC['ilTabs'];
44
45 $ilTabs->activateTab("content");
46 $this->initUploadForm();
47 echo $this->form->getHTML();
48
49
50 $options = new stdClass();
51 $options->dropZone = "#ilFileUploadDropZone_1";
52 $options->fileInput = "#ilFileUploadInput_1";
53 $options->submitButton = "uploadFiles";
54 $options->cancelButton = "cancelAll";
55 $options->dropArea = "#ilFileUploadDropArea_1";
56 $options->fileList = "#ilFileUploadList_1";
57 $options->fileSelectButton = "#ilFileUploadFileSelect_1";
58 echo "<script language='javascript' type='text/javascript'>var fileUpload1 = new ilFileUpload(1, " . ilJsonUtil::encode($options) . ");</script>";
59
60 $_SESSION["cld_folder_id"] = $_POST["folder_id"];
61
62 exit;
63 }
64
65 public function initUploadForm()
66 {
67 global $DIC;
68 $ilCtrl = $DIC['ilCtrl'];
69 $lng = $DIC['lng'];
70
71 include_once("./Services/Form/classes/class.ilDragDropFileInputGUI.php");
72 include_once("./Services/jQuery/classes/class.iljQueryUtil.php");
73
74 $this->form = new ilPropertyFormGUI();
75 $this->form->setId("upload");
76 $this->form->setMultipart(true);
77 $this->form->setHideLabels();
78
79 $file = new ilDragDropFileInputGUI($lng->txt("cld_upload_files"), "upload_files");
80 $file->setRequired(true);
81 $this->form->addItem($file);
82
83 $this->form->addCommandButton("uploadFiles", $lng->txt("upload"));
84 $this->form->addCommandButton("cancelAll", $lng->txt("cancel"));
85
86 $this->form->setTableWidth("100%");
87 $this->form->setTitle($lng->txt("upload_files_title"));
88// $this->form->setTitleIcon(ilUtil::getImagePath('icon_file.gif'), $lng->txt('obj_file'));
89 $this->form->setTitleIcon(ilUtil::getImagePath('icon_dcl_file.svg'), $lng->txt('obj_file'));
90
91 $this->form->setTitle($lng->txt("upload_files"));
92 $this->form->setFormAction($ilCtrl->getFormAction($this, "uploadFiles"));
93 $this->form->setTarget("cld_blank_target");
94 }
95
96 public function cancelAll()
97 {
98 echo "<script language='javascript' type='text/javascript'>window.parent.il.CloudFileList.afterUpload('cancel');</script>";
99 exit;
100 }
101
106 public function uploadFiles()
107 {
108 $response = new stdClass();
109 $response->error = null;
110 $response->debug = null;
111
112 $this->initUploadForm();
113 if ($this->form->checkInput()) {
114 try {
115 $fileresult = $this->handleFileUpload($this->form->getInput("upload_files"));
116 if ($fileresult) {
117 $response = (object) array_merge((array) $response, (array) $fileresult);
118 }
119 } catch (ilException $e) {
120 $response->error = $e->getMessage();
121 }
122 } else {
124 $response->error = $error->getMessage();
125 }
126
127 // send response object (don't use 'application/json' as IE wants to download it!)
128 header('Vary: Accept');
129 header('Content-type: text/plain');
131 exit;
132 }
133
134 public function handleFileUpload($file_upload)
135 {
136 // create answer object
137 $response = new stdClass();
138 $response->fileName = $_POST["title"];
139 $response->fileSize = intval($file_upload["size"]);
140 $response->fileType = $file_upload["type"];
141 $response->fileUnzipped = $file_upload["extract"];
142 $response->error = null;
143
145
146 if ($file_upload["extract"]) {
147 $newdir = ilUtil::ilTempnam();
148 ilUtil::makeDir($newdir);
149
150 include_once './Services/Utilities/classes/class.ilFileUtils.php';
151 try {
152 ilFileUtils::processZipFile($newdir, $file_upload["tmp_name"], $file_upload["keep_structure"]);
153 } catch (Exception $e) {
154 $response->error = $e->getMessage();
155 ilUtil::delDir($newdir);
156 exit;
157 }
158
159 try {
160 $this->uploadDirectory($newdir, $_SESSION["cld_folder_id"], $file_tree, $file_upload["keep_structure"]);
161 } catch (Exception $e) {
162 $response->error = $e->getMessage();
163 ilUtil::delDir($newdir);
164 exit;
165 }
166
167 ilUtil::delDir($newdir);
168 return $response;
169 } else {
170 $file_tree->uploadFileToService($_SESSION["cld_folder_id"], $file_upload["tmp_name"], $_POST["title"]);
171 return $response;
172 }
173 }
174
184 protected function uploadDirectory($dir, $parent_id, $file_tree, $keep_structure = true)
185 {
186 $dirlist = opendir($dir);
187
188 while (false !== ($file = readdir($dirlist))) {
189 if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file)) {
190 global $DIC;
191 $lng = $DIC['lng'];
192 throw new ilCloudException($lng->txt("filenames_not_supported"), ilFileUtilsException::$BROKEN_FILE);
193 }
194 if ($file != '.' && $file != '..') {
195 $newpath = $dir . '/' . $file;
196 if (is_dir($newpath)) {
197 if ($keep_structure) {
198 $newnode = $file_tree->addFolderToService($parent_id, basename($newpath));
199 $this->uploadDirectory($newpath, $newnode->getId(), $file_tree);
200 } else {
201 $this->uploadDirectory($newpath, $parent_id, $file_tree, false);
202 }
203 } else {
204 $file_tree->uploadFileToService($parent_id, $newpath, basename($newpath));
205 }
206 }
207 }
208 closedir($dirlist);
209 }
210}
if(!isset( $_REQUEST[ 'ReturnTo'])) if(!isset($_REQUEST['AuthId'])) $options
Definition: as_login.php:20
$_POST["username"]
$_SESSION["AccountId"]
An exception for terminatinating execution or to throw for unit testing.
Class ilCloudException.
Class ilCloudPluginGUI.
Class ilCloudPluginUploadGUI.
uploadDirectory($dir, $parent_id, $file_tree, $keep_structure=true)
Recursive Method to upload a directory.
This class represents a file input property where multiple files can be dopped in a property form.
Base class for ILIAS Exception handling.
static processZipFile($a_directory, $a_file, $structure, $ref_id=null, $containerType=null, $tree=null, $access_handler=null)
unzips in given directory and processes uploaded zip for use as single files
static encode($mixed, $suppress_native=false)
This class represents a property form user interface.
static delDir($a_dir, $a_clean_only=false)
removes a dir and all its content (subdirs and files) recursively
static ilTempnam($a_temp_path=null)
Create a temporary file in an ILIAS writable directory.
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
static makeDir($a_dir)
creates a new directory and inherits all filesystem permissions of the parent directory You may pass ...
global $ilCtrl
Definition: ilias.php:18
$error
Definition: Error.php:17
global $lng
Definition: privfeed.php:17
$response
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file
global $DIC
Definition: saml.php:7