ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
ilFileUtils Class Reference

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

+ Collaboration diagram for ilFileUtils:

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.
 utf8_encode ($string)
 utf8-encodes string if it is not a valid utf8-string.
 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 janhi.nosp@m.ppch.nosp@m.en@gm.nosp@m.x.de

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

Member Function Documentation

static ilFileUtils::_lookupMimeType (   $a_file)
static
Parameters
stringfile absolute path to file

Definition at line 433 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$nameName of the object
integer$ref_idref_id of parent
string$containerTypeFold or Cat
Returns
integer ref_id of containerobject

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

References $lng, $name, 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("./Modules/Folder/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$filenameName of the object
string$pathPath to file
integer$ref_idref_id of parent

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

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

Referenced by createObjects().

{
global $rbacsystem;
if ($rbacsystem->checkAccess("create", $ref_id, "file")) {
// create and insert file in grp_tree
include_once("./Modules/File/classes/class.ilObjFile.php");
$fileObj = new ilObjFile();
$fileObj->setType($this->type);
// 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,ilFileUtils::utf8_encode(ilUtil::stripSlashes($filename)));
}
else {
$this->ilErr->raiseError($this->lng->txt("permission_denied"),$this->ilErr->MESSAGE);
}
}

+ 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$dirDirectory to start from
booleanstructure True if archive structure is to be overtaken (otherwise flat inclusion)
integer$ref_idref_id of parent object, if null, files won�t be included in system (just checked)
stringcontainerType object type of created containerobjects (folder or category)
Returns
integer errorcode

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

References ilFileUtilsException\$BROKEN_FILE, $dir, $file, $lng, $ref_id, createContainer(), createFile(), and utf8_encode().

Referenced by processZipFile().

{
$dirlist = opendir($dir);
while (false !== ($file = readdir ($dirlist)))
{
if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file))
{
throw new ilFileUtilsException($lng->txt("filenames_not_supported") , ilFileUtilsException::$BROKEN_FILE);
}
if ($file != '.' && $file != '..')
{
$newpath = $dir.'/'.$file;
$level = explode('/',$newpath);
if (is_dir($newpath))
{
if ($structure)
{
$new_ref_id = ilFileUtils::createContainer(ilFileUtils::utf8_encode($file), $ref_id, $containerType);
ilFileUtils::createObjects($newpath, $structure, $new_ref_id, $containerType);
}
else
{
ilFileUtils::createObjects($newpath, $structure, $ref_id, $containerType);
}
}
else
{
}
}
}
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$filenamename of file to read
string$fileoutname where to put decoded file

Definition at line 329 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$filenamename of file to read
Returns
string base decoded content

Definition at line 351 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$infilename
string$outfilename
Returns
bool

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

References $in, and $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$infilename
string$outfilename
string$levelcompression level from 1 to 9
Returns
bool

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

References $in, $out, and $param.

{
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_directoryDirectory to unzip
string$a_fileFilename of archive
booleanstructure True if archive structure is to be overtaken
integer$ref_idref_id of parent object, if null, files wont be included in system (just checked)
stringcontainerType 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, $key, $lng, $ref_id, createObjects(), recursive_dirscan(), ilUtil\unzip(), utf8_encode(), 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);
move_uploaded_file($a_file, $a_directory . "/" . $file);
ilUtil::unzip($a_directory . "/" . $file);
unlink ($a_directory . "/" . $file);
//echo "-".$a_directory . "/" . $file."-";
// Stores filename and paths into $filearray to check for viruses
// Checks if filenames can be read, else -> throw exception and leave
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($filearray[path][$key]);
throw new ilFileUtilsException($lng->txt("file_is_infected")."<br />".$vir[1], ilFileUtilsException::$INFECTED_FILE);
break;
}
else
{
if ($vir[1] != "")
{
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 .= " '" . ilFileUtils::utf8_encode($key) . "'";
}
}
if (isset($doublettes))
{
throw new ilFileUtilsException($lng->txt("exc_upload_error") . "<br />" . $lng->txt("zip_structure_error") . $doublettes ,
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$dirDirectory to start from
array&$arrReferenced array which is filled with Filename and path

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

References ilFileUtilsException\$BROKEN_FILE, $dir, $file, and $lng.

Referenced by ilObjTest\getCustomStyles(), ilObjExercise\processUploadedFile(), and processZipFile().

{
global $lng;
$dirlist = opendir($dir);
while (false !== ($file = readdir ($dirlist)))
{
if (!is_file($dir . "/" . $file) && !is_dir($dir . "/" . $file))
{
throw new ilFileUtilsException($lng->txt("filenames_not_supported"), ilFileUtilsException::$BROKEN_FILE);
}
if ($file != '.' && $file != '..')
{
$newpath = $dir.'/'.$file;
$level = explode('/',$newpath);
if (is_dir($newpath))
{
}
else
{
$arr["path"][] = $dir . "/";
$arr["file"][] = end($level);
}
}
}
closedir($dirlist);
}

+ Here is the caller graph for this function:

ilFileUtils::utf8_encode (   $string)

utf8-encodes string if it is not a valid utf8-string.

Author
Jan Hippchen
Version
1.12.3.08
Parameters
string$stringString to encode
Returns
string utf-8-encoded string

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

Referenced by createFile(), createObjects(), ilObjExercise\processUploadedFile(), and processZipFile().

{
// From http://w3.org/International/questions/qa-forms-utf-8.html
return (preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string))? $string : utf8_encode($string);
}

+ Here is the caller graph for this function:


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