00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00035 include_once 'Services/Utilities/classes/class.ilFileUtilsException.php';
00036
00037
00038 class ilFileUtils
00039 {
00053 function processZipFile ($a_directory, $a_file, $structure, $ref_id = null, $containerType = null) {
00054
00055 global $lng;
00056 include_once("Services/Utilities/classes/class.ilUtil.php");
00057
00058 $pathinfo = pathinfo($a_file);
00059 $file = $pathinfo["basename"];
00060
00061
00062
00063 copy ($a_file, $a_directory . "/" . $file);
00064 ilUtil::unzip($a_directory . "/" . $file);
00065 unlink ($a_directory . "/" . $file);
00066
00067
00068 ilFileUtils::recursive_dirscan($a_directory, $filearray);
00069
00070
00071 if (empty($filearray)) {
00072 throw new ilFileUtilsException($lng->txt("archive_broken"), ilFileUtilsException::$BROKEN_FILE);
00073 break;
00074 }
00075
00076
00077 foreach ($filearray["file"] as $key => $value)
00078 {
00079 $vir = ilUtil::virusHandling($filearray[path][$key], $value);
00080 if (!$vir[0])
00081 {
00082
00083 unlink($a_file);
00084 throw new ilFileUtilsException($lng->txt("file_is_infected")."<br />".$vir[1], ilFileUtilsException::$INFECTED_FILE);
00085 break;
00086 }
00087 else
00088 {
00089 if ($vir[1] != "")
00090 {
00091 throw new ilFileUtilsException($vir[1], ilFileUtilsException::$INFECTED_FILE);
00092 break;
00093 }
00094 }
00095
00096 }
00097
00098
00099 if (!$structure)
00100 {
00101 foreach (array_count_values($filearray["file"]) as $key => $value)
00102 {
00103
00104 if ($value != "1")
00105 {
00106 $doublettes .= " '" . $key . "'";
00107
00108 }
00109 }
00110 if (isset($doublettes))
00111 {
00112 throw new ilFileUtilsException($lng->txt("exc_upload_error") . "<br />" . $lng->txt("zip_structure_error") . $doublettes ,
00113 ilFileUtilsException::$DOUBLETTES_FOUND);
00114 break;
00115 }
00116 }
00117
00118
00119 if ($ref_id != null)
00120 {
00121 ilFileUtils::createObjects ($a_directory, $structure, $ref_id, $containerType);
00122 }
00123
00124 }
00125
00134 function recursive_dirscan($dir, &$arr)
00135 {
00136 $dirlist = opendir($dir);
00137 while ($file = readdir ($dirlist))
00138 {
00139 if ($file != '.' && $file != '..')
00140 {
00141 $newpath = $dir.'/'.$file;
00142 $level = explode('/',$newpath);
00143 if (is_dir($newpath))
00144 {
00145 ilFileUtils::recursive_dirscan($newpath, $arr);
00146 }
00147 else
00148 {
00149 $arr["path"][] = $dir . "/";
00150 $arr["file"][] = end($level);
00151 }
00152 }
00153 }
00154 closedir($dirlist);
00155 }
00156
00157
00171 function createObjects($dir, $structure, $ref_id, $containerType)
00172 {
00173 $dirlist = opendir($dir);
00174 while ($file = readdir ($dirlist))
00175 {
00176 if ($file != '.' && $file != '..')
00177 {
00178 $newpath = $dir.'/'.$file;
00179 $level = explode('/',$newpath);
00180 if (is_dir($newpath))
00181 {
00182 if ($structure)
00183 {
00184 $new_ref_id = ilFileUtils::createContainer($file, $ref_id, $containerType);
00185 ilFileUtils::createObjects($newpath, $structure, $new_ref_id, $containerType);
00186 }
00187 else
00188 {
00189 ilFileUtils::createObjects($newpath, $structure, $ref_id, $containerType);
00190 }
00191 }
00192 else
00193 {
00194 ilFileUtils::createFile (end($level),$dir,$ref_id);
00195 }
00196 }
00197 }
00198 closedir($dirlist);
00199 }
00200
00201
00212 function createContainer($name, $ref_id, $containerType)
00213 {
00214 if ($containerType == "Category")
00215 {
00216 include_once("./Modules/Category/classes/class.ilObjCategory.php");
00217 $newObj = new ilObjCategory();
00218 $newObj->setType("cat");
00219 }
00220 if ($containerType == "Folder")
00221 {
00222 include_once("./classes/class.ilObjFolder.php");
00223 $newObj = new ilObjFolder();
00224 $newObj->setType("fold");
00225 }
00226
00227 $newObj->setTitle($name);
00228 $newObj->create();
00229 $newObj->createReference();
00230 $newObj->putInTree($ref_id);
00231 $newObj->setPermissions($ref_id);
00232 $newObj->initDefaultRoles();
00233
00234 if ($newObj->getType() == "cat")
00235 {
00236 global $lng;
00237 $newObj->addTranslation($name,"", $lng->getLangKey(), $lng->getLangKey());
00238 }
00239
00240 return $newObj->getRefId();
00241 }
00242
00252 function createFile ($filename, $path, $ref_id)
00253 {
00254
00255 include_once("./Modules/File/classes/class.ilObjFile.php");
00256 $fileObj = new ilObjFile();
00257 $fileObj->setType($this->type);
00258 $fileObj->setTitle(ilUtil::stripSlashes($filename));
00259 $fileObj->setFileName(ilUtil::stripSlashes($filename));
00260
00261
00262 include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
00263 $fileObj->setFileType(ilObjMediaObject::getMimeType($path. "/" . $filename));
00264
00265 $fileObj->setFileSize(filesize($path. "/" . $filename));
00266 $fileObj->create();
00267 $fileObj->createReference();
00268
00269 $fileObj->putInTree($ref_id);
00270 $fileObj->setPermissions($ref_id);
00271
00272
00273
00274 $fileObj->createDirectory();
00275
00276 $fileObj->storeUnzipedFile($path. "/" . $filename,ilUtil::stripSlashes($filename));
00277
00278 }
00279
00280
00286 function fastBase64Decode ($filein, $fileout)
00287 {
00288 $fh = fopen($filein, 'rb');
00289 $fh2= fopen($fileout, 'wb');
00290 stream_filter_append($fh2, 'convert.base64-decode');
00291
00292 while (!feof($fh)){
00293 $chunk = fgets($fh);
00294 if ($chunk === false)
00295 break;
00296 fwrite ($fh2, $chunk);
00297 }
00298 fclose ($fh);
00299 fclose ($fh2);
00300 return true;
00301 }
00302
00308 function fastBase64Encode ($filein, $fileout)
00309 {
00310 $fh = fopen($filein, 'rb');
00311 $fh2= fopen($fileout, 'wb');
00312 stream_filter_append($fh2, 'convert.base64-encode');
00313
00314 while (feof ($fh)) {
00315 $chunk = fgets($fh,76);
00316 if ($chunk === false)
00317 {
00318 break;
00319 }
00320 fwrite ($fh2, $chunk);
00321 }
00322 fclose ($fh);
00323 fclose ($fh2);
00324 }
00325
00335 function fastGZip ($in, $out, $level="9")
00336 {
00337 if (!file_exists ($in) || !is_readable ($in))
00338 return false;
00339 if ((!file_exists ($out) && !is_writable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
00340 return false;
00341
00342 $in_file = fopen ($in, "rb");
00343 if (!$out_file = gzopen ($out, "wb".$param)) {
00344 return false;
00345 }
00346
00347 while (!feof ($in_file)) {
00348 $buffer = fgets ($in_file, 4096);
00349 gzwrite ($out_file, $buffer, 4096);
00350 }
00351
00352 fclose ($in_file);
00353 gzclose ($out_file);
00354
00355 return true;
00356 }
00357
00366 function fastGunzip ($in, $out)
00367 {
00368 if (!file_exists ($in) || !is_readable ($in))
00369 return false;
00370 if ((!file_exists ($out) && !is_writable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
00371 return false;
00372
00373 $in_file = gzopen ($in, "rb");
00374 $out_file = fopen ($out, "wb");
00375
00376 while (!gzeof ($in_file)) {
00377 $buffer = gzread ($in_file, 4096);
00378 fwrite ($out_file, $buffer, 4096);
00379 }
00380
00381 gzclose ($in_file);
00382 fclose ($out_file);
00383
00384 return true;
00385 }
00386
00390 public static function _lookupMimeType($a_file)
00391 {
00392 if(!file_exists($a_file) or !is_readable($a_file))
00393 {
00394 return false;
00395 }
00396
00397 if(class_exists('finfo'))
00398 {
00399 $finfo = new finfo(FILEINFO_MIME);
00400 return $finfo->buffer(file_get_contents($a_file));
00401 }
00402 if(function_exists('mime_content_type'))
00403 {
00404 return mime_content_type($a_file);
00405 }
00406 return 'application/octet-stream';
00407 }
00408
00409 }
00410
00411
00412 ?>