Skip to content

Commit b9244d6

Browse files
committed
Add PCLZIP alternative to ZipArchive
Resolves #106
1 parent f4d7fa4 commit b9244d6

File tree

6 files changed

+6110
-11
lines changed

6 files changed

+6110
-11
lines changed

Classes/PHPWord/Settings.php

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,33 @@
3030
*/
3131
class PHPWord_Settings
3232
{
33+
/** constants */
34+
35+
/** Available Zip library classes */
36+
const PCLZIP = 'PHPWord_Shared_ZipArchive';
37+
const ZIPARCHIVE = 'ZipArchive';
38+
3339
/**
3440
* Compatibility option for XMLWriter
3541
*
3642
* @var boolean
3743
*/
3844
private static $_xmlWriterCompatibility = true;
3945

46+
/**
47+
* Name of the class used for Zip file management
48+
* e.g.
49+
* ZipArchive
50+
*
51+
* @var string
52+
*/
53+
private static $_zipClass = self::ZIPARCHIVE;
54+
4055
/**
4156
* Set the compatibility option used by the XMLWriter
4257
*
43-
* @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility
44-
* @return boolean Success or failure
58+
* @param boolean $compatibility This sets the setIndent and setIndentString for better compatibility
59+
* @return boolean Success or failure
4560
*/
4661
public static function setCompatibility($compatibility)
4762
{
@@ -50,7 +65,7 @@ public static function setCompatibility($compatibility)
5065
return true;
5166
}
5267
return false;
53-
}
68+
} // function setCompatibility()
5469

5570
/**
5671
* Return the compatibility option used by the XMLWriter
@@ -60,5 +75,36 @@ public static function setCompatibility($compatibility)
6075
public static function getCompatibility()
6176
{
6277
return self::$_xmlWriterCompatibility;
63-
}
64-
}
78+
} // function getCompatibility()
79+
80+
/**
81+
* Set the Zip handler Class that PHPWord should use for Zip file management (PCLZip or ZipArchive)
82+
*
83+
* @param string $zipClass The Zip handler class that PHPWord should use for Zip file management
84+
* e.g. PHPWord_Settings::PCLZip or PHPWord_Settings::ZipArchive
85+
* @return boolean Success or failure
86+
*/
87+
public static function setZipClass($zipClass)
88+
{
89+
if (($zipClass === self::PCLZIP) ||
90+
($zipClass === self::ZIPARCHIVE)) {
91+
self::$_zipClass = $zipClass;
92+
return TRUE;
93+
}
94+
return FALSE;
95+
} // function setZipClass()
96+
97+
/**
98+
* Return the name of the Zip handler Class that PHPWord is configured to use (PCLZip or ZipArchive)
99+
* or Zip file management
100+
*
101+
* @return string Name of the Zip handler Class that PHPWord is configured to use
102+
* for Zip file management
103+
* e.g. PHPWord_Settings::PCLZip or PHPWord_Settings::ZipArchive
104+
*/
105+
public static function getZipClass()
106+
{
107+
return self::$_zipClass;
108+
} // function getZipClass()
109+
}
110+

Classes/PHPWord/Shared/File.php

Lines changed: 97 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,39 @@
3030
*/
3131
class PHPWord_Shared_File
3232
{
33+
/*
34+
* Use Temp or File Upload Temp for temporary files
35+
*
36+
* @protected
37+
* @var boolean
38+
*/
39+
protected static $_useUploadTempDirectory = FALSE;
40+
41+
42+
/**
43+
* Set the flag indicating whether the File Upload Temp directory should be used for temporary files
44+
*
45+
* @param boolean $useUploadTempDir Use File Upload Temporary directory (true or false)
46+
*/
47+
public static function setUseUploadTempDirectory($useUploadTempDir = FALSE) {
48+
self::$_useUploadTempDirectory = (boolean) $useUploadTempDir;
49+
} // function setUseUploadTempDirectory()
50+
51+
52+
/**
53+
* Get the flag indicating whether the File Upload Temp directory should be used for temporary files
54+
*
55+
* @return boolean Use File Upload Temporary directory (true or false)
56+
*/
57+
public static function getUseUploadTempDirectory() {
58+
return self::$_useUploadTempDirectory;
59+
} // function getUseUploadTempDirectory()
60+
3361
/**
3462
* Verify if a file exists
3563
*
36-
* @param string $pFilename Filename
37-
* @return bool
64+
* @param string $pFilename Filename
65+
* @return boolean
3866
*/
3967
public static function file_exists($pFilename)
4068
{
@@ -45,7 +73,7 @@ public static function file_exists($pFilename)
4573
/**
4674
* Returns canonicalized absolute pathname, also for ZIP archives
4775
*
48-
* @param string $pFilename
76+
* @param string $pFilename
4977
* @return string
5078
*/
5179
public static function realpath($pFilename)
@@ -74,4 +102,70 @@ public static function realpath($pFilename)
74102
// Return
75103
return $returnValue;
76104
}
105+
106+
/**
107+
* Return the Image Type from a file
108+
*
109+
* @param string $filename
110+
* @return return
111+
*/
112+
public static function imagetype($filename) {
113+
if (function_exists('exif_imagetype')) {
114+
return exif_imagetype($filename);
115+
} else {
116+
if ((list($width, $height, $type, $attr) = getimagesize( $filename )) !== false) {
117+
return $type;
118+
}
119+
}
120+
return false;
121+
}
122+
123+
/**
124+
* Get the systems temporary directory.
125+
*
126+
* @return string
127+
*/
128+
public static function sys_get_temp_dir()
129+
{
130+
if (self::$_useUploadTempDirectory) {
131+
// use upload-directory when defined to allow running on environments having very restricted
132+
// open_basedir configs
133+
if (ini_get('upload_tmp_dir') !== FALSE) {
134+
if ($temp = ini_get('upload_tmp_dir')) {
135+
if (file_exists($temp))
136+
return realpath($temp);
137+
}
138+
}
139+
}
140+
141+
// sys_get_temp_dir is only available since PHP 5.2.1
142+
// http://php.net/manual/en/function.sys-get-temp-dir.php#94119
143+
if ( !function_exists('sys_get_temp_dir')) {
144+
if ($temp = getenv('TMP') ) {
145+
if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
146+
}
147+
if ($temp = getenv('TEMP') ) {
148+
if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
149+
}
150+
if ($temp = getenv('TMPDIR') ) {
151+
if ((!empty($temp)) && (file_exists($temp))) { return realpath($temp); }
152+
}
153+
154+
// trick for creating a file in system's temporary dir
155+
// without knowing the path of the system's temporary dir
156+
$temp = tempnam(__FILE__, '');
157+
if (file_exists($temp)) {
158+
unlink($temp);
159+
return realpath(dirname($temp));
160+
}
161+
162+
return null;
163+
}
164+
165+
// use ordinary built-in PHP function
166+
// There should be no problem with the 5.2.4 Suhosin realpath() bug, because this line should only
167+
// be called if we're running 5.2.1 or earlier
168+
return realpath(sys_get_temp_dir());
169+
}
170+
77171
}

0 commit comments

Comments
 (0)