Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PCLZIP alternative to ZipArchive #140

Merged
merged 1 commit into from
Mar 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 51 additions & 5 deletions Classes/PHPWord/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,33 @@
*/
class PHPWord_Settings
{
/** constants */

/** Available Zip library classes */
const PCLZIP = 'PHPWord_Shared_ZipArchive';
const ZIPARCHIVE = 'ZipArchive';

/**
* Compatibility option for XMLWriter
*
* @var boolean
*/
private static $_xmlWriterCompatibility = true;

/**
* Name of the class used for Zip file management
* e.g.
* ZipArchive
*
* @var string
*/
private static $_zipClass = self::ZIPARCHIVE;

/**
* Set the compatibility option used by the XMLWriter
*
* @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility
* @return boolean Success or failure
* @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility
* @return boolean Success or failure
*/
public static function setCompatibility($compatibility)
{
Expand All @@ -50,7 +65,7 @@ public static function setCompatibility($compatibility)
return true;
}
return false;
}
} // function setCompatibility()

/**
* Return the compatibility option used by the XMLWriter
Expand All @@ -60,5 +75,36 @@ public static function setCompatibility($compatibility)
public static function getCompatibility()
{
return self::$_xmlWriterCompatibility;
}
}
} // function getCompatibility()

/**
* Set the Zip handler Class that PHPWord should use for Zip file management (PCLZip or ZipArchive)
*
* @param string $zipClass The Zip handler class that PHPWord should use for Zip file management
* e.g. PHPWord_Settings::PCLZip or PHPWord_Settings::ZipArchive
* @return boolean Success or failure
*/
public static function setZipClass($zipClass)
{
if (($zipClass === self::PCLZIP) ||
($zipClass === self::ZIPARCHIVE)) {
self::$_zipClass = $zipClass;
return TRUE;
}
return FALSE;
} // function setZipClass()

/**
* Return the name of the Zip handler Class that PHPWord is configured to use (PCLZip or ZipArchive)
* or Zip file management
*
* @return string Name of the Zip handler Class that PHPWord is configured to use
* for Zip file management
* e.g. PHPWord_Settings::PCLZip or PHPWord_Settings::ZipArchive
*/
public static function getZipClass()
{
return self::$_zipClass;
} // function getZipClass()
}

100 changes: 97 additions & 3 deletions Classes/PHPWord/Shared/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,39 @@
*/
class PHPWord_Shared_File
{
/*
* Use Temp or File Upload Temp for temporary files
*
* @protected
* @var boolean
*/
protected static $_useUploadTempDirectory = FALSE;


/**
* Set the flag indicating whether the File Upload Temp directory should be used for temporary files
*
* @param boolean $useUploadTempDir Use File Upload Temporary directory (true or false)
*/
public static function setUseUploadTempDirectory($useUploadTempDir = FALSE) {
self::$_useUploadTempDirectory = (boolean) $useUploadTempDir;
} // function setUseUploadTempDirectory()


/**
* Get the flag indicating whether the File Upload Temp directory should be used for temporary files
*
* @return boolean Use File Upload Temporary directory (true or false)
*/
public static function getUseUploadTempDirectory() {
return self::$_useUploadTempDirectory;
} // function getUseUploadTempDirectory()

/**
* Verify if a file exists
*
* @param string $pFilename Filename
* @return bool
* @param string $pFilename Filename
* @return boolean
*/
public static function file_exists($pFilename)
{
Expand All @@ -45,7 +73,7 @@ public static function file_exists($pFilename)
/**
* Returns canonicalized absolute pathname, also for ZIP archives
*
* @param string $pFilename
* @param string $pFilename
* @return string
*/
public static function realpath($pFilename)
Expand Down Expand Up @@ -74,4 +102,70 @@ public static function realpath($pFilename)
// Return
return $returnValue;
}

/**
* Return the Image Type from a file
*
* @param string $filename
* @return return
*/
public static function imagetype($filename) {
if (function_exists('exif_imagetype')) {
return exif_imagetype($filename);
} else {
if ((list($width, $height, $type, $attr) = getimagesize( $filename )) !== false) {
return $type;
}
}
return false;
}

/**
* Get the systems temporary directory.
*
* @return string
*/
public static function sys_get_temp_dir()
{
if (self::$_useUploadTempDirectory) {
// use upload-directory when defined to allow running on environments having very restricted
// open_basedir configs
if (ini_get('upload_tmp_dir') !== FALSE) {
if ($temp = ini_get('upload_tmp_dir')) {
if (file_exists($temp))
return realpath($temp);
}
}
}

// sys_get_temp_dir is only available since PHP 5.2.1
// http://php.net/manual/en/function.sys-get-temp-dir.php#94119
if ( !function_exists('sys_get_temp_dir')) {
if ($temp = getenv('TMP') ) {
if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
}
if ($temp = getenv('TEMP') ) {
if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
}
if ($temp = getenv('TMPDIR') ) {
if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
}

// trick for creating a file in system's temporary dir
// without knowing the path of the system's temporary dir
$temp = tempnam(__FILE__, '');
if (file_exists($temp)) {
unlink($temp);
return realpath(dirname($temp));
}

return null;
}

// use ordinary built-in PHP function
// There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
// be called if we're running 5.2.1 or earlier
return realpath(sys_get_temp_dir());
}

}
Loading