ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
imgupload.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 chdir('../../../../');
5 
6 require_once 'Services/Init/classes/class.ilInitialisation.php';
8 
15 global $DIC;
16 $ilIliasIniFile = $DIC['ilIliasIniFile'];
17 $lng = $DIC['lng'];
18 $ilUser = $DIC['ilUser'];
19 $https = $DIC['https'];
20 
21 $lng->loadLanguageModule("form");
22 
23 $htdocs = $ilIliasIniFile->readVariable('server', 'absolute_path') . '/';
24 $weburl = $ilIliasIniFile->readVariable('server', 'absolute_path') . '/';
25 if (defined('ILIAS_HTTP_PATH')) {
26  $weburl = substr(ILIAS_HTTP_PATH, 0, strrpos(ILIAS_HTTP_PATH, '/node_modules')) . '/';
27 }
28 
30 
31 // directory where tinymce files are located
32 $iliasMobPath = 'data/' . CLIENT_ID . '/mobs/';
35 
36 // base url for images
39 
40 // allowed extentions for uploaded image files
41 $tinyMCE_valid_imgs = array('gif', 'jpg', 'jpeg', 'png');
42 
43 // allow upload in image library
45 
46 // allow delete in image library
48 
49 $errors = new stdClass();
50 $errors->general = array();
51 $errors->fields = array();
52 
53 include_once 'webservice/soap/include/inc.soap_functions.php';
54 $mobs = ilSoapFunctions::getMobsOfObject(session_id() . '::' . CLIENT_ID, $_GET['obj_type'] . ':html', (int) $_GET['obj_id']);
55 $preview = '';
56 $mob_details = array();
57 $img = isset($_POST['imglist']) ? $_POST['imglist'] : '';
59 
60 // upload images
61 $uploadedFile = false;
62 if (isset($_FILES['img_file']) && is_array($_FILES['img_file'])) {
63  // remove trailing '/'
64  while (substr($_FILES['img_file']['name'], -1) == '/') {
65  $_FILES['img_file']['name'] = substr($_FILES['img_file']['name'], 0, -1);
66  }
67 
68  $error = $_FILES['img_file']['error'];
69  switch ($error) {
70  case UPLOAD_ERR_INI_SIZE:
71  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt('form_msg_file_size_exceeds'));
72  break;
73 
74  case UPLOAD_ERR_FORM_SIZE:
75  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_size_exceeds"));
76  break;
77 
78  case UPLOAD_ERR_PARTIAL:
79  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_partially_uploaded"));
80  break;
81 
82  case UPLOAD_ERR_NO_FILE:
83  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_no_upload"));
84  break;
85 
86  case UPLOAD_ERR_NO_TMP_DIR:
87  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_missing_tmp_dir"));
88  break;
89 
90  case UPLOAD_ERR_CANT_WRITE:
91  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_cannot_write_to_disk"));
92  break;
93 
94  case UPLOAD_ERR_EXTENSION:
95  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_upload_stopped_ext"));
96  break;
97  }
98 
99  // check suffixes
100  if (!$errors->fields && !$errors->general) {
101  $finfo = pathinfo($_FILES['img_file']['name']);
102  require_once 'Services/Utilities/classes/class.ilMimeTypeUtil.php';
103  $mime_type = ilMimeTypeUtil::getMimeType($_FILES['img_file']['tmp_name'], $_FILES['img_file']['name'], $_FILES['img_file']['type']);
104  if (!in_array(strtolower($finfo['extension']), $tinyMCE_valid_imgs) || !in_array($mime_type, array(
105  'image/gif',
106  'image/jpeg',
107  'image/png'
108  ))) {
109  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_wrong_file_type"));
110  }
111  }
112 
113  // virus handling
114  if (!$errors->fields && !$errors->general) {
115  if ($_FILES['img_file']["tmp_name"] != "") {
116  $vir = ilUtil::virusHandling($_FILES['img_file']["tmp_name"], $_FILES['img_file']["name"]);
117  if ($vir[0] == false) {
118  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
119  }
120  }
121  }
122  if (!$errors->fields && !$errors->general) {
123  include_once 'webservice/soap/include/inc.soap_functions.php';
124  $safefilename = preg_replace('/[^a-zA-z0-9_\.]/', '', $_FILES['img_file']['name']);
125  $media_object = ilSoapFunctions::saveTempFileAsMediaObject(session_id() . '::' . CLIENT_ID, $safefilename, $_FILES['img_file']['tmp_name']);
126  if (file_exists($iliasAbsolutePath . $iliasMobPath . 'mm_' . $media_object->getId() . '/' . $media_object->getTitle())) {
127  // only save usage if the file was uploaded
128  $media_object->_saveUsage($media_object->getId(), $_GET['obj_type'] . ':html', (int) $_GET['obj_id']);
129 
130  // Append file to array of existings mobs of this context (obj_type and obj_id)
131  $mobs[$media_object->getId()] = $media_object->getId();
132 
133  $uploadedFile = $media_object->getId();
134  $_GET['update'] = 1;
135  }
136  }
137 }
138 $panel =array();
139 if ($_GET["update"] == 1) {
140  $panel["img_url_tab_desc"]= "ilimgupload.edit_image";
141  $panel["img_from_url_desc"]="ilimgupload.edit_image_desc";
142  $panel['img_insert_command'] = "ilimgupload.insert";
143 
144 } else {
145  $panel["img_url_tab_desc"]= "ilimgupload.upload_image_from_url";
146  $panel["img_from_url_desc"]="ilimgupload.upload_image_from_url_desc";
147  $panel['img_insert_command'] = "ilimgupload.insert";
148 }
149 
150 $mob_details = array();
151 foreach ($mobs as $mob) {
152  $mobdir = $iliasAbsolutePath . $iliasMobPath . 'mm_' . $mob . '/';
153  $d = @dir($mobdir);
154  if ($d) {
155  $i = 0;
156  while (false !== ($entry = $d->read())) {
157  $ext = strtolower(substr(strrchr($entry, '.'), 1));
158  if (is_file($mobdir . $entry) && in_array($ext, $tinyMCE_valid_imgs)) {
159  $mob_details[$uploadedFile]['file_name'] = $entry;
160  $mob_details[$uploadedFile]['file_dir'] = $mobdir;
161  $mob_details[$uploadedFile]['http_dir'] = $iliasHttpPath . $iliasMobPath . 'mm_' . $mob . '/';
162  }
163  }
164  $d->close();
165  }
166 }
167 
170 if ($errors->fields || $errors->general) {
171  $response[] = $errors;
173  $location= $mob_details[$uploadedFile]['http_dir'] . $mob_details[$uploadedFile]['file_name'];
174  $uploaded_file_desc['width']= (int) $img_size[0];
175  $uploaded_file_desc['height']= (int) $img_size[1];
176  $uploaded_file_desc['location']= $location;
177 }
178 $response = array(
179  'uploaded_file'=> $uploaded_file_desc,
180  'errors' => $errors,
181  'panel'=> $panel
182 );
183 
184 echo json_encode(array('response' => $response));
$tinyMCE_DOC_url
Definition: imgupload.php:38
$https
Definition: imgupload.php:19
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible
$mobs
Definition: imgupload.php:54
$errors
Definition: imgupload.php:49
if(isset($_FILES['img_file']) &&is_array($_FILES['img_file'])) $panel
Definition: imgupload.php:138
$_GET["client_id"]
$location
Definition: buildRTE.php:44
$tinyMCE_valid_imgs
Definition: imgupload.php:41
$iliasAbsolutePath
Definition: imgupload.php:33
$mob_details
Definition: imgupload.php:56
$tinyMCE_base_url
Definition: imgupload.php:37
$ilIliasIniFile
Definition: imgupload.php:16
$lng
Definition: imgupload.php:17
$iliasMobPath
Definition: imgupload.php:32
static getMimeType($a_file='', $a_filename='', $a_mime='')
static initILIAS()
ilias initialisation
foreach($mobs as $mob) $response
Definition: imgupload.php:168
$preview
Definition: imgupload.php:55
static getMobsOfObject($sid, $a_type, $a_id)
$iliasHttpPath
Definition: imgupload.php:34
$weburl
Definition: imgupload.php:24
const CLIENT_ID
Definition: constants.php:39
$htdocs
Definition: imgupload.php:23
$img
Definition: imgupload.php:57
$tinyMCE_upload_allowed
Definition: imgupload.php:44
$uploaded_file_desc
Definition: imgupload.php:169
$uploadedFile
Definition: imgupload.php:61
$ilUser
Definition: imgupload.php:18
$tinyMCE_img_delete_allowed
Definition: imgupload.php:47
global $DIC
Definition: imgupload.php:15
$_root
Definition: imgupload.php:58
if(defined('ILIAS_HTTP_PATH')) $installpath
Definition: imgupload.php:29
$_POST["username"]
for($i=6; $i< 13; $i++) for($i=1; $i< 13; $i++) $d
Definition: date.php:296
static saveTempFileAsMediaObject($sid, $name, $tmp_name)
$i
Definition: metadata.php:24