ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups 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 
16 
17 $lng->loadLanguageModule("form");
18 
19 $htdocs = $ilIliasIniFile->readVariable('server', 'absolute_path') . '/';
20 $weburl = $ilIliasIniFile->readVariable('server', 'http_path') . '/';
22 
23 // directory where tinymce files are located
24 $iliasMobPath = 'data/' . CLIENT_ID . '/mobs/';
27 if($iliasHttpPath && strpos($iliasHttpPath, 'https://') === false && $https->isDetected())
28 {
29  $iliasHttpPath = str_replace('http://', 'https://', $iliasHttpPath);
30 }
31 
32 // base url for images
35 
36 // allowed extentions for uploaded image files
37 $tinyMCE_valid_imgs = array('gif', 'jpg', 'jpeg', 'png');
38 
39 // allow upload in image library
41 
42 // allow delete in image library
44 
45 $errors = new stdClass();
46 $errors->general = array();
47 $errors->fields = array();
48 
49 include_once 'webservice/soap/include/inc.soap_functions.php';
50 $mobs = ilSoapFunctions::getMobsOfObject(session_id() . '::' . CLIENT_ID, $_GET['obj_type'] . ':html', (int)$_GET['obj_id']);
51 $preview = '';
52 $mob_details = array();
53 $img = isset($_POST['imglist']) ? $_POST['imglist'] : '';
55 
56 // upload images
57 $uploadedFile = false;
58 if(isset($_FILES['img_file']) && is_array($_FILES['img_file']))
59 {
60  // remove trailing '/'
61  while(substr($_FILES['img_file']['name'], -1) == '/')
62  {
63  $_FILES['img_file']['name'] = substr($_FILES['img_file']['name'], 0, -1);
64  }
65 
66  $error = $_FILES['img_file']['error'];
67  switch ($error)
68  {
69  case UPLOAD_ERR_INI_SIZE:
70  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt('form_msg_file_size_exceeds'));
71  break;
72 
73  case UPLOAD_ERR_FORM_SIZE:
74  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_size_exceeds"));
75  break;
76 
77  case UPLOAD_ERR_PARTIAL:
78  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_partially_uploaded"));
79  break;
80 
81  case UPLOAD_ERR_NO_FILE:
82  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_no_upload"));
83  break;
84 
85  case UPLOAD_ERR_NO_TMP_DIR:
86  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_missing_tmp_dir"));
87  break;
88 
89  case UPLOAD_ERR_CANT_WRITE:
90  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_cannot_write_to_disk"));
91  break;
92 
93  case UPLOAD_ERR_EXTENSION:
94  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_upload_stopped_ext"));
95  break;
96  }
97 
98  // check suffixes
99  if(!$errors->fields && !$errors->general)
100  {
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  {
110  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_wrong_file_type"));
111  }
112  }
113 
114  // virus handling
115  if(!$errors->fields && !$errors->general)
116  {
117  if($_FILES['img_file']["tmp_name"] != "")
118  {
119  $vir = ilUtil::virusHandling($_FILES['img_file']["tmp_name"], $_FILES['img_file']["name"]);
120  if($vir[0] == false)
121  {
122  $errors->fields[] = array('name' => 'img_file', 'message' => $lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
123  }
124  }
125  }
126 
127  if(!$errors->fields && !$errors->general)
128  {
129  include_once 'webservice/soap/include/inc.soap_functions.php';
130  $safefilename = preg_replace('/[^a-zA-z0-9_\.]/', '', $_FILES['img_file']['name']);
131  $media_object = ilSoapFunctions::saveTempFileAsMediaObject(session_id() . '::' . CLIENT_ID, $safefilename, $_FILES['img_file']['tmp_name']);
132  if(file_exists($iliasAbsolutePath . $iliasMobPath . 'mm_' . $media_object->getId() . '/' . $media_object->getTitle()))
133  {
134  // only save usage if the file was uploaded
135  $media_object->_saveUsage($media_object->getId(), $_GET['obj_type'] . ':html', (int)$_GET['obj_id']);
136 
137  // Append file to array of existings mobs of this context (obj_type and obj_id)
138  $mobs[$media_object->getId()] = $media_object->getId();
139 
140  $uploadedFile = $media_object->getId();
141  $_GET['update'] = 1;
142  }
143  }
144 }
145 
146 $tpl = new ilTemplate(dirname(__FILE__) . "/tpl.img_upload.html", true, true);
147 
148 $tpl->setVariable("OBJ_ID", (int)$_GET["obj_id"]);
149 $tpl->setVariable("OBJ_TYPE", $_GET["obj_type"]);
150 $tpl->setVariable("VALUE_UPDATE", (int)$_GET["update"]);
151 $tpl->setVariable("ILIAS_INST_PATH", $iliasHttpPath);
152 $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
153 $tpl->setVariable(
154  "TXT_ALLOWED_FILE_EXTENSIONS",
155  $lng->txt("file_allowed_suffixes")." ".
156  implode(', ', array_map(create_function('$value', 'return ".".$value;'), $tinyMCE_valid_imgs))
157 );
158 
159 if($ilUser->getLanguage() == 'de')
160 {
161  $tpl->touchBlock('validation_engine_lang_de');
162 }
163 else
164 {
165  $tpl->touchBlock('validation_engine_lang_default');
166 }
167 
168 if($_GET["update"] == 1)
169 {
170  $tpl->setVariable("IMG_FROM_URL_TAB_DESC", "{#ilimgupload.edit_image}");
171  $tpl->setVariable("IMG_FROM_URL_DESC", "{#ilimgupload.edit_image_desc}");
172  $tpl->setVariable("INSERT_COMMAND", "{#ilimgupload.insert}");
173 }
174 else
175 {
176  $tpl->setVariable("IMG_FROM_URL_TAB_DESC", "{#ilimgupload.upload_image_from_url}");
177  $tpl->setVariable("IMG_FROM_URL_DESC", "{#ilimgupload.upload_image_from_url_desc}");
178  $tpl->setVariable("INSERT_COMMAND", "{#ilimgupload.insert}");
179 }
180 
181 $mob_details = array();
182 foreach($mobs as $mob)
183 {
184  $mobdir = $iliasAbsolutePath . $iliasMobPath . 'mm_' . $mob . '/';
185  $d = @dir($mobdir);
186  if($d)
187  {
188  $i = 0;
189  while(false !== ($entry = $d->read()))
190  {
191  $ext = strtolower(substr(strrchr($entry, '.'), 1));
192  if(is_file($mobdir . $entry) && in_array($ext, $tinyMCE_valid_imgs))
193  {
194  $mob_details[$uploadedFile]['file_name'] = $entry;
195  $mob_details[$uploadedFile]['file_dir'] = $mobdir;
196  $mob_details[$uploadedFile]['http_dir'] = $iliasHttpPath . $iliasMobPath . 'mm_' . $mob . '/';
197  }
198  }
199  $d->close();
200  }
201 }
202 if($errors->fields || $errors->general)
203 {
204  foreach($errors->fields as $field)
205  {
206  $tpl->setCurrentBlock('errors');
207  $tpl->setVariable('ERRORS_FIELDNAME', $field['name']);
208  $tpl->setVariable('ERRORS_MESSAGE', $field['message']);
209  $tpl->parseCurrentBlock();
210  }
211 }
213 {
214  $img_size = getimagesize($mob_details[$uploadedFile]['file_dir'] . $mob_details[$uploadedFile]['file_name']);
215  $tpl->setVariable('UPLOADED_FILE_WIDTH', (int)$img_size[0]);
216  $tpl->setVariable('UPLOADED_FILE_HEIGHT', (int)$img_size[1]);
217  $tpl->setVariable('UPLOADED_FILE_SRC', $mob_details[$uploadedFile]['http_dir'] . $mob_details[$uploadedFile]['file_name']);
218 }
219 
220 $tpl->show();