Public Member Functions | Static Public Member Functions

ilFileUtils Class Reference

fileUtils class various functions for zip-archive handling More...

Public Member Functions

 processZipFile ($a_directory, $a_file, $structure, $ref_id=null, $containerType=null)
 unzips in given directory and processes uploaded zip for use as single files
 recursive_dirscan ($dir, &$arr)
 Recursively scans a given directory and writes path and filename into referenced array.
 createObjects ($dir, $structure, $ref_id, $containerType)
 Recursively scans a given directory and creates file and folder/category objects.
 createContainer ($name, $ref_id, $containerType)
 Creates and inserts container object (folder/category) into tree.
 createFile ($filename, $path, $ref_id)
 Creates and inserts file object into tree.
 fastBase64Decode ($filein, $fileout)
 decodes base encoded file row by row to prevent memory exhaust
 fastBase64Encode ($filein, $fileout)
 decodes base encoded file row by row to prevent memory exhaust
 fastGZip ($in, $out, $level="9")
 fast compressing the file with the zlib-extension without memory consumption
 fastGunzip ($in, $out)
 fast uncompressing the file with the zlib-extension without memory consumption

Static Public Member Functions

static _lookupMimeType ($a_file)

Detailed Description

fileUtils class various functions for zip-archive handling

Author:
Jan Hippchen <janhippchen@gmx.de>

Definition at line 38 of file class.ilFileUtils.php.


Member Function Documentation

static ilFileUtils::_lookupMimeType ( a_file  )  [static]
Parameters:
string file absolute path to file

Definition at line 390 of file class.ilFileUtils.php.

Referenced by ilFileXMLParser::handlerEndTag().

        {
                if(!file_exists($a_file) or !is_readable($a_file))
                {
                        return false;
                }
                
                if(class_exists('finfo'))
                {
                        $finfo = new finfo(FILEINFO_MIME);
                        return $finfo->buffer(file_get_contents($a_file));
                }
                if(function_exists('mime_content_type'))
                {
                        return mime_content_type($a_file);
                }
                return 'application/octet-stream';
        }

Here is the caller graph for this function:

ilFileUtils::createContainer ( name,
ref_id,
containerType 
)

Creates and inserts container object (folder/category) into tree.

Author:
Jan Hippchen
Version:
1.6.9.07
Parameters:
string $name Name of the object
integer $ref_id ref_id of parent
string $containerType Fold or Cat
Returns:
integer ref_id of containerobject

Definition at line 212 of file class.ilFileUtils.php.

References $lng, and $ref_id.

Referenced by createObjects().

        {
                if ($containerType == "Category") 
                {
                        include_once("./Modules/Category/classes/class.ilObjCategory.php");
                        $newObj = new ilObjCategory();
                        $newObj->setType("cat");
                }
                if ($containerType == "Folder")
                {
                        include_once("./classes/class.ilObjFolder.php");
                        $newObj = new ilObjFolder();
                        $newObj->setType("fold");               
                }

                $newObj->setTitle($name);
                $newObj->create();
                $newObj->createReference();
                $newObj->putInTree($ref_id);
                $newObj->setPermissions($ref_id);
                $newObj->initDefaultRoles();
                
                if ($newObj->getType() == "cat") 
                {
                        global $lng;
                        $newObj->addTranslation($name,"", $lng->getLangKey(), $lng->getLangKey());
                }
                
                return $newObj->getRefId();
        }

Here is the caller graph for this function:

ilFileUtils::createFile ( filename,
path,
ref_id 
)

Creates and inserts file object into tree.

Author:
Jan Hippchen
Version:
1.6.9.07
Parameters:
string $filename Name of the object
string $path Path to file
integer $ref_id ref_id of parent

Definition at line 252 of file class.ilFileUtils.php.

References $filename, $ref_id, ilObjMediaObject::getMimeType(), and ilUtil::stripSlashes().

Referenced by createObjects().

        {
                // create and insert file in grp_tree
                include_once("./Modules/File/classes/class.ilObjFile.php");
                $fileObj = new ilObjFile();
                $fileObj->setType($this->type);
                $fileObj->setTitle(ilUtil::stripSlashes($filename));
                $fileObj->setFileName(ilUtil::stripSlashes($filename));
                
                // better use this, mime_content_type is deprecated
                include_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
                $fileObj->setFileType(ilObjMediaObject::getMimeType($path. "/" . $filename));
                
                $fileObj->setFileSize(filesize($path. "/" . $filename));
                $fileObj->create();
                $fileObj->createReference();

                $fileObj->putInTree($ref_id);
                $fileObj->setPermissions($ref_id);
        
                // upload file to filesystem

                $fileObj->createDirectory();

                $fileObj->storeUnzipedFile($path. "/" . $filename,ilUtil::stripSlashes($filename));

        }

Here is the call graph for this function:

Here is the caller graph for this function:

ilFileUtils::createObjects ( dir,
structure,
ref_id,
containerType 
)

Recursively scans a given directory and creates file and folder/category objects.

Calls createContainer & createFile to store objects in tree

Author:
Jan Hippchen
Version:
1.6.9.07
Parameters:
string $dir Directory to start from
boolean structure True if archive structure is to be overtaken (otherwise flat inclusion)
integer $ref_id ref_id of parent object, if null, files won�t be included in system (just checked)
string containerType object type of created containerobjects (folder or category)
Returns:
integer errorcode

Definition at line 171 of file class.ilFileUtils.php.

References $dir, $file, $ref_id, createContainer(), and createFile().

Referenced by processZipFile().

        {
                $dirlist = opendir($dir);
                while ($file = readdir ($dirlist))
                {
                        if ($file != '.' && $file != '..')
                        {
                                $newpath = $dir.'/'.$file;
                                $level = explode('/',$newpath);
                                if (is_dir($newpath))
                                {
                                        if ($structure) 
                                        {
                                                $new_ref_id = ilFileUtils::createContainer($file, $ref_id, $containerType);
                                                ilFileUtils::createObjects($newpath, $structure, $new_ref_id, $containerType);
                                        }
                                        else 
                                        {
                                                ilFileUtils::createObjects($newpath, $structure, $ref_id, $containerType);
                                        }
                                }
                                else
                                {
                                        ilFileUtils::createFile (end($level),$dir,$ref_id);
                                }
                        }
                }
                closedir($dirlist);             
        }

Here is the call graph for this function:

Here is the caller graph for this function:

ilFileUtils::fastBase64Decode ( filein,
fileout 
)

decodes base encoded file row by row to prevent memory exhaust

Parameters:
string $filename name of file to read
string $fileout name where to put decoded file

Definition at line 286 of file class.ilFileUtils.php.

Referenced by ilFileXMLParser::handlerEndTag().

        {
                $fh = fopen($filein, 'rb');
                $fh2= fopen($fileout, 'wb');
                stream_filter_append($fh2, 'convert.base64-decode');

                while (!feof($fh)){
                    $chunk = fgets($fh);
                    if ($chunk === false)
                        break;
                    fwrite ($fh2, $chunk);
                }
                fclose ($fh);
                fclose ($fh2);
                return true;
        }

Here is the caller graph for this function:

ilFileUtils::fastBase64Encode ( filein,
fileout 
)

decodes base encoded file row by row to prevent memory exhaust

Parameters:
string $filename name of file to read
Returns:
string base decoded content

Definition at line 308 of file class.ilFileUtils.php.

        {
                $fh = fopen($filein, 'rb');
                $fh2= fopen($fileout, 'wb');
                stream_filter_append($fh2, 'convert.base64-encode');
                
                while (feof ($fh)) {
                        $chunk = fgets($fh,76);
                        if ($chunk === false) 
                        {
                                break;
                        }
                        fwrite ($fh2, $chunk);                  
                }
                fclose ($fh);
                fclose ($fh2);
        }                       

ilFileUtils::fastGunzip ( in,
out 
)

fast uncompressing the file with the zlib-extension without memory consumption

Parameters:
string $in filename
string $out filename
Returns:
bool

Definition at line 366 of file class.ilFileUtils.php.

References $out.

Referenced by ilFileXMLParser::handlerEndTag().

        {
    if (!file_exists ($in) || !is_readable ($in))
        return false;
    if ((!file_exists ($out) && !is_writable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
        return false;

    $in_file = gzopen ($in, "rb");
    $out_file = fopen ($out, "wb");

    while (!gzeof ($in_file)) {
        $buffer = gzread ($in_file, 4096);
        fwrite ($out_file, $buffer, 4096);
    }
 
    gzclose ($in_file);
    fclose ($out_file);
    
    return true;
        }

Here is the caller graph for this function:

ilFileUtils::fastGZip ( in,
out,
level = "9" 
)

fast compressing the file with the zlib-extension without memory consumption

Parameters:
string $in filename
string $out filename
string $level compression level from 1 to 9
Returns:
bool

Definition at line 335 of file class.ilFileUtils.php.

References $out.

        {
    if (!file_exists ($in) || !is_readable ($in))
        return false;
    if ((!file_exists ($out) && !is_writable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
        return false;
    
    $in_file = fopen ($in, "rb");
    if (!$out_file = gzopen ($out, "wb".$param)) {
        return false;
    }
    
    while (!feof ($in_file)) {
        $buffer = fgets ($in_file, 4096);
        gzwrite ($out_file, $buffer, 4096);
    }

    fclose ($in_file);
    gzclose ($out_file);
    
    return true;
        }

ilFileUtils::processZipFile ( a_directory,
a_file,
structure,
ref_id = null,
containerType = null 
)

unzips in given directory and processes uploaded zip for use as single files

Author:
Jan Hippchen
Version:
1.6.9.07
Parameters:
string $a_directory Directory to unzip
string $a_file Filename of archive
boolean structure True if archive structure is to be overtaken
integer $ref_id ref_id of parent object, if null, files wont be included in system (just checked)
string containerType object type of created containerobjects (folder or category)
Exceptions:
ilFileUtilsException 

Definition at line 53 of file class.ilFileUtils.php.

References ilFileUtilsException::$BROKEN_FILE, ilFileUtilsException::$DOUBLETTES_FOUND, $file, ilFileUtilsException::$INFECTED_FILE, $lng, $ref_id, createObjects(), recursive_dirscan(), ilUtil::unzip(), and ilUtil::virusHandling().

Referenced by ilObjExercise::processUploadedFile(), and ilObjFileGUI::saveUnzipObject().

                                                                                                           {

                global $lng;
                include_once("Services/Utilities/classes/class.ilUtil.php");
                                
                $pathinfo = pathinfo($a_file);
                $file = $pathinfo["basename"];

                // Copy zip-file to new directory, unzip and remove it
                // TODO: check archive for broken file
                copy ($a_file, $a_directory . "/" . $file);
                ilUtil::unzip($a_directory . "/" . $file);
                unlink ($a_directory . "/" . $file);

                // Stores filename and paths into $filearray to check for viruses and 
                ilFileUtils::recursive_dirscan($a_directory, $filearray);

                // if there are no files unziped (->broken file!)
                if (empty($filearray)) {
                        throw new ilFileUtilsException($lng->txt("archive_broken"), ilFileUtilsException::$BROKEN_FILE);
                        break;
                }

                // virus handling
                foreach ($filearray["file"] as $key => $value)
                {
                        $vir = ilUtil::virusHandling($filearray[path][$key], $value);
                        if (!$vir[0])
                        {
                                // Unlink file and throw exception
                                unlink($a_file);
                                throw new ilFileUtilsException($lng->txt("file_is_infected")."<br />".$vir[1], ilFileUtilsException::$INFECTED_FILE);
                                break;
                        }
                        else
                        {
                                if ($vir[1] != "")
                                {
                                        throw new ilFileUtilsException($vir[1], ilFileUtilsException::$INFECTED_FILE);
                                        break;
                                }
                        }
                        
                }
                
                // If archive is to be used "flat"
                if (!$structure) 
                {       
                        foreach (array_count_values($filearray["file"]) as $key => $value)
                        {
                                // Archive contains same filenames in different directories 
                                if ($value != "1") 
                                {       
                                        $doublettes .= " '" . $key . "'";
                                        
                                }       
                        }
                        if (isset($doublettes))
                        {
                                throw new ilFileUtilsException($lng->txt("exc_upload_error") . "<br />" . $lng->txt("zip_structure_error") . $doublettes , 
                                                                ilFileUtilsException::$DOUBLETTES_FOUND);
                                break;
                        }
                }

                // Everything fine since we got here; so we can store files and folders into the system (if ref_id is given)
                if ($ref_id != null)
                {
                        ilFileUtils::createObjects ($a_directory, $structure, $ref_id, $containerType);
                }
                
        }

Here is the call graph for this function:

Here is the caller graph for this function:

ilFileUtils::recursive_dirscan ( dir,
&$  arr 
)

Recursively scans a given directory and writes path and filename into referenced array.

Author:
Jan Hippchen
Version:
1.6.9.07
Parameters:
string $dir Directory to start from
array &$arr Referenced array which is filled with Filename and path

Definition at line 134 of file class.ilFileUtils.php.

References $dir, and $file.

Referenced by ilObjExercise::processUploadedFile(), and processZipFile().

        {
                $dirlist = opendir($dir);
                while ($file = readdir ($dirlist))
                {
                        if ($file != '.' && $file != '..')
                        {
                                $newpath = $dir.'/'.$file;
                                $level = explode('/',$newpath);
                                if (is_dir($newpath))
                                {
                                        ilFileUtils::recursive_dirscan($newpath, $arr);
                                }
                                else
                                {
                                        $arr["path"][] = $dir . "/";
                                        $arr["file"][] = end($level);
                                }
                        }
                }
                closedir($dirlist);             
        }

Here is the caller graph for this function:


The documentation for this class was generated from the following file: