ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilFileUtils.php
Go to the documentation of this file.
1 <?php
2 /*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22 */
23 
35 include_once 'Services/Utilities/classes/class.ilFileUtilsException.php';
36 
37 
39 {
53  function processZipFile ($a_directory, $a_file, $structure, $ref_id = null, $containerType = null) {
54 
55  global $lng;
56  include_once("Services/Utilities/classes/class.ilUtil.php");
57 
58  $pathinfo = pathinfo($a_file);
59  $file = $pathinfo["basename"];
60 
61  // Copy zip-file to new directory, unzip and remove it
62  // TODO: check archive for broken file
63  //copy ($a_file, $a_directory . "/" . $file);
64  move_uploaded_file($a_file, $a_directory . "/" . $file);
65  ilUtil::unzip($a_directory . "/" . $file);
66  unlink ($a_directory . "/" . $file);
67 //echo "-".$a_directory . "/" . $file."-";
68  // Stores filename and paths into $filearray to check for viruses
69  // Checks if filenames can be read, else -> throw exception and leave
70  ilFileUtils::recursive_dirscan($a_directory, $filearray);
71 
72  // if there are no files unziped (->broken file!)
73  if (empty($filearray)) {
74  throw new ilFileUtilsException($lng->txt("archive_broken"), ilFileUtilsException::$BROKEN_FILE);
75  break;
76  }
77 
78  // virus handling
79  foreach ($filearray["file"] as $key => $value)
80  {
81  $vir = ilUtil::virusHandling($filearray[path][$key], $value);
82  if (!$vir[0])
83  {
84  // Unlink file and throw exception
85  unlink($filearray[path][$key]);
86  throw new ilFileUtilsException($lng->txt("file_is_infected")."<br />".$vir[1], ilFileUtilsException::$INFECTED_FILE);
87  break;
88  }
89  else
90  {
91  if ($vir[1] != "")
92  {
94  break;
95  }
96  }
97 
98  }
99 
100  // If archive is to be used "flat"
101  if (!$structure)
102  {
103  foreach (array_count_values($filearray["file"]) as $key => $value)
104  {
105  // Archive contains same filenames in different directories
106  if ($value != "1")
107  {
108  $doublettes .= " '" . ilFileUtils::utf8_encode($key) . "'";
109 
110  }
111  }
112  if (isset($doublettes))
113  {
114  throw new ilFileUtilsException($lng->txt("exc_upload_error") . "<br />" . $lng->txt("zip_structure_error") . $doublettes ,
116  break;
117  }
118  }
119 
120  // Everything fine since we got here; so we can store files and folders into the system (if ref_id is given)
121  if ($ref_id != null)
122  {
123  ilFileUtils::createObjects ($a_directory, $structure, $ref_id, $containerType);
124  }
125 
126  }
127 
136  function recursive_dirscan($dir, &$arr)
137  {
138  global $lng;
139 
140  $dirlist = opendir($dir);
141  while (false !== ($file = readdir ($dirlist)))
142  {
143  if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file))
144  {
145  throw new ilFileUtilsException($lng->txt("filenames_not_supported"), ilFileUtilsException::$BROKEN_FILE);
146  }
147 
148  if ($file != '.' && $file != '..')
149  {
150  $newpath = $dir.'/'.$file;
151  $level = explode('/',$newpath);
152  if (is_dir($newpath))
153  {
154  ilFileUtils::recursive_dirscan($newpath, $arr);
155  }
156  else
157  {
158  $arr["path"][] = $dir . "/";
159  $arr["file"][] = end($level);
160  }
161  }
162  }
163  closedir($dirlist);
164  }
165 
166 
180  function createObjects($dir, $structure, $ref_id, $containerType)
181  {
182  $dirlist = opendir($dir);
183 
184  while (false !== ($file = readdir ($dirlist)))
185  {
186  if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file))
187  {
188  throw new ilFileUtilsException($lng->txt("filenames_not_supported") , ilFileUtilsException::$BROKEN_FILE);
189  }
190  if ($file != '.' && $file != '..')
191  {
192  $newpath = $dir.'/'.$file;
193  $level = explode('/',$newpath);
194  if (is_dir($newpath))
195  {
196  if ($structure)
197  {
198  $new_ref_id = ilFileUtils::createContainer(ilFileUtils::utf8_encode($file), $ref_id, $containerType);
199  ilFileUtils::createObjects($newpath, $structure, $new_ref_id, $containerType);
200  }
201  else
202  {
203  ilFileUtils::createObjects($newpath, $structure, $ref_id, $containerType);
204  }
205  }
206  else
207  {
208  ilFileUtils::createFile (end($level),$dir,$ref_id);
209  }
210  }
211  }
212  closedir($dirlist);
213  }
214 
215 
226  function createContainer($name, $ref_id, $containerType)
227  {
228  if ($containerType == "Category")
229  {
230  include_once("./Modules/Category/classes/class.ilObjCategory.php");
231  $newObj = new ilObjCategory();
232  $newObj->setType("cat");
233  }
234  if ($containerType == "Folder")
235  {
236  include_once("./Modules/Folder/classes/class.ilObjFolder.php");
237  $newObj = new ilObjFolder();
238  $newObj->setType("fold");
239  }
240 
241  $newObj->setTitle($name);
242  $newObj->create();
243  $newObj->createReference();
244  $newObj->putInTree($ref_id);
245  $newObj->setPermissions($ref_id);
246 
247  if ($newObj->getType() == "cat")
248  {
249  global $lng;
250  $newObj->addTranslation($name,"", $lng->getLangKey(), $lng->getLangKey());
251  }
252 
253  return $newObj->getRefId();
254  }
255 
266  {
267  global $rbacsystem;
268  if ($rbacsystem->checkAccess("create", $ref_id, "file")) {
269 
270  // create and insert file in grp_tree
271  include_once("./Modules/File/classes/class.ilObjFile.php");
272  $fileObj = new ilObjFile();
273  $fileObj->setType($this->type);
276 
277  // better use this, mime_content_type is deprecated
278  include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
279  $fileObj->setFileType(ilObjMediaObject::getMimeType($path. "/" . $filename));
280 
281  $fileObj->setFileSize(filesize($path. "/" . $filename));
282  $fileObj->create();
283  $fileObj->createReference();
284 
285  $fileObj->putInTree($ref_id);
286  $fileObj->setPermissions($ref_id);
287 
288  // upload file to filesystem
289 
290  $fileObj->createDirectory();
291 
292  $fileObj->storeUnzipedFile($path. "/" . $filename,ilFileUtils::utf8_encode(ilUtil::stripSlashes($filename)));
293  }
294  else {
295  $this->ilErr->raiseError($this->lng->txt("permission_denied"),$this->ilErr->MESSAGE);
296  }
297  }
298 
307  function utf8_encode($string) {
308 
309  // From http://w3.org/International/questions/qa-forms-utf-8.html
310  return (preg_match('%^(?:
311  [\x09\x0A\x0D\x20-\x7E] # ASCII
312  | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
313  | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
314  | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
315  | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
316  | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
317  | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
318  | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
319  )*$%xs', $string))? $string : utf8_encode($string);
320  }
321 
322 
328  function fastBase64Decode ($filein, $fileout)
329  {
330  $fh = fopen($filein, 'rb');
331  $fh2= fopen($fileout, 'wb');
332  stream_filter_append($fh2, 'convert.base64-decode');
333 
334  while (!feof($fh)){
335  $chunk = fgets($fh);
336  if ($chunk === false)
337  break;
338  fwrite ($fh2, $chunk);
339  }
340  fclose ($fh);
341  fclose ($fh2);
342  return true;
343  }
344 
350  function fastBase64Encode ($filein, $fileout)
351  {
352  $fh = fopen($filein, 'rb');
353  $fh2= fopen($fileout, 'wb');
354  stream_filter_append($fh2, 'convert.base64-encode');
355 
356  while (feof ($fh)) {
357  $chunk = fgets($fh,76);
358  if ($chunk === false)
359  {
360  break;
361  }
362  fwrite ($fh2, $chunk);
363  }
364  fclose ($fh);
365  fclose ($fh2);
366  }
367 
377  function fastGZip ($in, $out, $level="9")
378  {
379  if (!file_exists ($in) || !is_readable ($in))
380  return false;
381  if ((!file_exists ($out) && !is_writable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
382  return false;
383 
384  $in_file = fopen ($in, "rb");
385  if (!$out_file = gzopen ($out, "wb".$param)) {
386  return false;
387  }
388 
389  while (!feof ($in_file)) {
390  $buffer = fgets ($in_file, 4096);
391  gzwrite ($out_file, $buffer, 4096);
392  }
393 
394  fclose ($in_file);
395  gzclose ($out_file);
396 
397  return true;
398  }
399 
408  function fastGunzip ($in, $out)
409  {
410  if (!file_exists ($in) || !is_readable ($in))
411  return false;
412  if ((!file_exists ($out) && !is_writable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
413  return false;
414 
415  $in_file = gzopen ($in, "rb");
416  $out_file = fopen ($out, "wb");
417 
418  while (!gzeof ($in_file)) {
419  $buffer = gzread ($in_file, 4096);
420  fwrite ($out_file, $buffer, 4096);
421  }
422 
423  gzclose ($in_file);
424  fclose ($out_file);
425 
426  return true;
427  }
428 
432  public static function _lookupMimeType($a_file)
433  {
434  if(!file_exists($a_file) or !is_readable($a_file))
435  {
436  return false;
437  }
438 
439  if(class_exists('finfo'))
440  {
441  $finfo = new finfo(FILEINFO_MIME);
442  return $finfo->buffer(file_get_contents($a_file));
443  }
444  if(function_exists('mime_content_type'))
445  {
446  return mime_content_type($a_file);
447  }
448  return 'application/octet-stream';
449  }
450 
451 } // END class.ilFileUtils
452 
453 
454 ?>