ILIAS  release_8 Revision v8.24
imgupload.php
Go to the documentation of this file.
1<?php declare(strict_types=1);
2
3/******************************************************************************
4 *
5 * This file is part of ILIAS, a powerful learning management system.
6 *
7 * ILIAS is licensed with the GPL-3.0, you should have received a copy
8 * of said license along with the source code.
9 *
10 * If this is not the case or you just want to try ILIAS, you'll find
11 * us at:
12 * https://www.ilias.de
13 * https://github.com/ILIAS-eLearning
14 *
15 *****************************************************************************/
16
18
19chdir('../../../../');
20
21require_once 'Services/Init/classes/class.ilInitialisation.php';
23
30global $DIC;
31
32$ilIliasIniFile = $DIC['ilIliasIniFile'];
33$lng = $DIC['lng'];
34$ilUser = $DIC['ilUser'];
35$https = $DIC['https'];
36
37$lng->loadLanguageModule('form');
38
39$htdocs = $ilIliasIniFile->readVariable('server', 'absolute_path') . '/';
40$weburl = $ilIliasIniFile->readVariable('server', 'absolute_path') . '/';
41if (defined('ILIAS_HTTP_PATH')) {
42 $weburl = substr(ILIAS_HTTP_PATH, 0, strrpos(ILIAS_HTTP_PATH, '/node_modules')) . '/';
43}
44
46
47// directory where tinymce files are located
48$iliasMobPath = 'data/' . CLIENT_ID . '/mobs/';
51
52// base url for images
55
56// allowed extentions for uploaded image files
57$tinyMCE_valid_imgs = ['gif', 'jpg', 'jpeg', 'png'];
58
59// allow upload in image library
61
62// allow delete in image library
64
65$errors = new stdClass();
66$errors->general = [];
67$errors->fields = [];
68
69include_once 'webservice/soap/include/inc.soap_functions.php';
71 session_id() . '::' . CLIENT_ID,
72 $DIC->http()->wrapper()->query()->retrieve(
73 'obj_type',
74 $DIC->refinery()->kindlyTo()->string()
75 ) . ':html',
76 $DIC->http()->wrapper()->query()->retrieve(
77 'obj_id',
78 $DIC->refinery()->kindlyTo()->int()
79 )
80);
83$img = '';
84if ($DIC->http()->wrapper()->post()->has('imglist')) {
85 $img = $DIC->http()->wrapper()->post()->retrieve(
86 'imglist',
87 $DIC->refinery()->kindlyTo()->string()
88 );
89}
91
92$update = false;
93if ($DIC->http()->wrapper()->query()->has('update')) {
94 $update = $DIC->http()->wrapper()->query()->retrieve(
95 'update',
96 $DIC->refinery()->kindlyTo()->bool()
97 );
98}
99
100// upload images
102if (isset($_FILES['img_file']) && is_array($_FILES['img_file'])) {
103 while (substr($_FILES['img_file']['name'], -1) === '/') {
104 $_FILES['img_file']['name'] = substr($_FILES['img_file']['name'], 0, -1);
105 }
106
107 $error = $_FILES['img_file']['error'];
108 switch ($error) {
109 case UPLOAD_ERR_INI_SIZE:
110 $errors->fields[] = ['name' => 'img_file', 'message' => $lng->txt('form_msg_file_size_exceeds')];
111 break;
112
113 case UPLOAD_ERR_FORM_SIZE:
114 $errors->fields[] = ['name' => 'img_file', 'message' => $lng->txt("form_msg_file_size_exceeds")];
115 break;
116
117 case UPLOAD_ERR_PARTIAL:
118 $errors->fields[] = ['name' => 'img_file', 'message' => $lng->txt("form_msg_file_partially_uploaded")];
119 break;
120
121 case UPLOAD_ERR_NO_FILE:
122 $errors->fields[] = ['name' => 'img_file', 'message' => $lng->txt("form_msg_file_no_upload")];
123 break;
124
125 case UPLOAD_ERR_NO_TMP_DIR:
126 $errors->fields[] = ['name' => 'img_file', 'message' => $lng->txt("form_msg_file_missing_tmp_dir")];
127 break;
128
129 case UPLOAD_ERR_CANT_WRITE:
130 $errors->fields[] = ['name' => 'img_file', 'message' => $lng->txt("form_msg_file_cannot_write_to_disk")];
131 break;
132
133 case UPLOAD_ERR_EXTENSION:
134 $errors->fields[] = ['name' => 'img_file', 'message' => $lng->txt("form_msg_file_upload_stopped_ext")];
135 break;
136 }
137
138 // check suffixes
139 if (!$errors->fields && !$errors->general) {
140 $finfo = pathinfo($_FILES['img_file']['name']);
142 $_FILES['img_file']['tmp_name'],
143 $_FILES['img_file']['name'],
144 $_FILES['img_file']['type']
145 );
146 if (
147 !in_array($mime_type, ['image/gif', 'image/jpeg', 'image/png'], true) ||
148 !in_array(strtolower($finfo['extension']), $tinyMCE_valid_imgs, true)
149 ) {
150 $errors->fields[] = ['name' => 'img_file', 'message' => $lng->txt("form_msg_file_wrong_file_type")];
151 }
152 }
153
154 // virus handling
155 if (
156 !$errors->fields &&
157 !$errors->general &&
158 $_FILES['img_file']['tmp_name'] !== ''
159 ) {
160 $vir = ilVirusScanner::virusHandling($_FILES['img_file']['tmp_name'], $_FILES['img_file']['name']);
161 if ($vir[0] === false) {
162 $errors->fields[] = [
163 'name' => 'img_file',
164 'message' => $lng->txt('form_msg_file_virus_found') . '<br />' . $vir[1]
165 ];
166 }
167 }
168 if (!$errors->fields && !$errors->general) {
169 $safefilename = preg_replace('/[^a-zA-Z0-9_\.]/', '', $_FILES['img_file']['name']);
171 session_id() . '::' . CLIENT_ID,
172 $safefilename,
173 $_FILES['img_file']['tmp_name']
174 );
175 if (file_exists($iliasAbsolutePath . $iliasMobPath . 'mm_' . $media_object->getId() . '/' . $media_object->getTitle())) {
176 // only save usage if the file was uploaded
177 $media_object::_saveUsage(
178 $media_object->getId(),
179 $DIC->http()->wrapper()->query()->retrieve(
180 'obj_type',
181 $DIC->refinery()->kindlyTo()->string()
182 ) . ':html',
183 $DIC->http()->wrapper()->query()->retrieve(
184 'obj_id',
185 $DIC->refinery()->kindlyTo()->int()
186 )
187 );
188
189 // Append file to array of existings mobs of this context (obj_type and obj_id)
190 $mobs[$media_object->getId()] = $media_object->getId();
191
192 $uploadedFile = $media_object->getId();
193 $update = true;
194 }
195 }
196}
197
198$panel = ['img_insert_command' => "ilimgupload.insert"];
199if ($update) {
200 $panel["img_url_tab_desc"] = "ilimgupload.edit_image";
201 $panel["img_from_url_desc"] = "ilimgupload.edit_image_desc";
202} else {
203 $panel["img_url_tab_desc"] = "ilimgupload.upload_image_from_url";
204 $panel["img_from_url_desc"] = "ilimgupload.upload_image_from_url_desc";
205}
206
207$mob_details = [];
208foreach ($mobs as $mob) {
209 $mobdir = $iliasAbsolutePath . $iliasMobPath . 'mm_' . $mob . '/';
210 if (is_dir($mobdir) && ($d = dir($mobdir))) {
211 $i = 0;
212 while (false !== ($entry = $d->read())) {
213 $ext = strtolower(substr(strrchr($entry, '.'), 1));
214 if (is_file($mobdir . $entry) && in_array($ext, $tinyMCE_valid_imgs)) {
215 $mob_details[$uploadedFile]['file_name'] = $entry;
216 $mob_details[$uploadedFile]['file_dir'] = $mobdir;
217 $mob_details[$uploadedFile]['http_dir'] = $iliasHttpPath . $iliasMobPath . 'mm_' . $mob . '/';
218 }
219 }
220 $d->close();
221 }
222}
223
226if ($errors->fields || $errors->general) {
227 $response[] = $errors;
229 $location = $mob_details[$uploadedFile]['http_dir'] . $mob_details[$uploadedFile]['file_name'];
230 $uploaded_file_desc['width'] = 0;
231 $uploaded_file_desc['height'] = 0;
232 $uploaded_file_desc['location'] = $location;
233}
234$response = [
235 'uploaded_file' => $uploaded_file_desc,
236 'errors' => $errors,
237 'panel' => $panel
238];
239
240$DIC->http()->saveResponse(
241 $DIC->http()->response()
242 ->withHeader(ResponseHeader::CONTENT_TYPE, 'application/json')
243 ->withBody(\ILIAS\Filesystem\Stream\Streams::ofString(json_encode(['response' => $response], JSON_THROW_ON_ERROR)))
244);
245$DIC->http()->sendResponse();
246$DIC->http()->close();
$location
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: buildRTE.php:22
static getMimeType(string $a_file='', string $a_filename='', string $a_mime='')
Definition: MimeType.php:530
static initILIAS()
ilias initialisation
static saveTempFileAsMediaObject(string $sid, string $name, string $tmp_name)
static getMobsOfObject(string $sid, string $a_type, int $a_id)
static virusHandling(string $a_file, string $a_orig_name='', bool $a_clean=true)
const CLIENT_ID
Definition: constants.php:41
for( $i=6;$i< 13;$i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
$preview
Definition: imgupload.php:81
$iliasAbsolutePath
Definition: imgupload.php:49
$update
Definition: imgupload.php:92
if($DIC->http() ->wrapper() ->query() ->has('update')) $uploadedFile
Definition: imgupload.php:101
foreach($mobs as $mob) $response
Definition: imgupload.php:224
$weburl
Definition: imgupload.php:40
$uploaded_file_desc
Definition: imgupload.php:225
global $DIC
Definition: imgupload.php:30
$img
Definition: imgupload.php:83
$mobs
Definition: imgupload.php:70
$mob_details
Definition: imgupload.php:82
if($DIC->http() ->wrapper() ->post() ->has('imglist')) $_root
Definition: imgupload.php:90
$https
Definition: imgupload.php:35
$tinyMCE_img_delete_allowed
Definition: imgupload.php:63
if(isset($_FILES['img_file']) &&is_array($_FILES['img_file'])) $panel
Definition: imgupload.php:198
$tinyMCE_base_url
Definition: imgupload.php:53
$tinyMCE_upload_allowed
Definition: imgupload.php:60
$errors
Definition: imgupload.php:65
$lng
Definition: imgupload.php:33
$iliasHttpPath
Definition: imgupload.php:50
$ilIliasIniFile
Definition: imgupload.php:32
$ilUser
Definition: imgupload.php:34
$htdocs
Definition: imgupload.php:39
$tinyMCE_valid_imgs
Definition: imgupload.php:57
if(defined('ILIAS_HTTP_PATH')) $installpath
Definition: imgupload.php:45
$iliasMobPath
Definition: imgupload.php:48
$tinyMCE_DOC_url
Definition: imgupload.php:54
Interface ResponseHeader.
$i
Definition: metadata.php:41
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
Class ChatMainBarProvider \MainMenu\Provider.