• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/Utilities/classes/class.ilFileUtils.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003 +-----------------------------------------------------------------------------+
00004 | ILIAS open source                                                           |
00005 +-----------------------------------------------------------------------------+
00006 | Copyright (c) 1998-2006 ILIAS open source, University of Cologne            |
00007 |                                                                             |
00008 | This program is free software; you can redistribute it and/or               |
00009 | modify it under the terms of the GNU General Public License                 |
00010 | as published by the Free Software Foundation; either version 2              |
00011 | of the License, or (at your option) any later version.                      |
00012 |                                                                             |
00013 | This program is distributed in the hope that it will be useful,             |
00014 | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016 | GNU General Public License for more details.                                |
00017 |                                                                             |
00018 | You should have received a copy of the GNU General Public License           |
00019 | along with this program; if not, write to the Free Software                 |
00020 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
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                 // Copy zip-file to new directory, unzip and remove it
00062                 // TODO: check archive for broken file
00063                 copy ($a_file, $a_directory . "/" . $file);
00064                 ilUtil::unzip($a_directory . "/" . $file);
00065                 unlink ($a_directory . "/" . $file);
00066 
00067                 // Stores filename and paths into $filearray to check for viruses and 
00068                 ilFileUtils::recursive_dirscan($a_directory, $filearray);
00069 
00070                 // if there are no files unziped (->broken file!)
00071                 if (empty($filearray)) {
00072                         throw new ilFileUtilsException($lng->txt("archive_broken"), ilFileUtilsException::$BROKEN_FILE);
00073                         break;
00074                 }
00075 
00076                 // virus handling
00077                 foreach ($filearray["file"] as $key => $value)
00078                 {
00079                         $vir = ilUtil::virusHandling($filearray[path][$key], $value);
00080                         if (!$vir[0])
00081                         {
00082                                 // Unlink file and throw exception
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                 // If archive is to be used "flat"
00099                 if (!$structure) 
00100                 {       
00101                         foreach (array_count_values($filearray["file"]) as $key => $value)
00102                         {
00103                                 // Archive contains same filenames in different directories 
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                 // Everything fine since we got here; so we can store files and folders into the system (if ref_id is given)
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                 // create and insert file in grp_tree
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                 // better use this, mime_content_type is deprecated
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                 // upload file to filesystem
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 } // END class.ilFileUtils
00410 
00411 
00412 ?>

Generated on Fri Dec 13 2013 17:57:02 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1