compressionArchiveUtil.inc.php
8.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
* $Id:
*
* The contents of this file are subject to the KnowledgeTree Public
* License Version 1.1.2 ("License"); You may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.knowledgetree.com/KPL
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
* See the License for the specific language governing rights and
* limitations under the License.
*
* All copies of the Covered Code must include on each user interface screen:
* (i) the "Powered by KnowledgeTree" logo and
* (ii) the KnowledgeTree copyright notice
* in the same form as they appear in the distribution. See the License for
* requirements.
*
* The Original Code is: KnowledgeTree Open Source
*
* The Initial Developer of the Original Code is The Jam Warehouse Software
* (Pty) Ltd, trading as KnowledgeTree.
* Portions created by The Jam Warehouse Software (Pty) Ltd are Copyright
* (C) 2007 The Jam Warehouse Software (Pty) Ltd;
* All Rights Reserved.
* Contributor( s): ______________________________________
*
*/
/**
* Class to create and download a zip file
*/
class ZipFolder {
var $sTmpPath = '';
var $sZipFileName = '';
var $sZipFile = '';
var $aPaths = array();
var $aReplaceKeys = array();
var $aReplaceValues = array();
var $sOutputEncoding = 'UTF-8';
/**
* Constructor
*/
function ZipFolder($sZipFileName) {
$this->oKTConfig =& KTConfig::getSingleton();
$this->oStorage =& KTStorageManagerUtil::getSingleton();
$sBasedir = $this->oKTConfig->get("urls/tmpDirectory");
$sTmpPath = tempnam($sBasedir, 'kt_compress_zip');
unlink($sTmpPath);
mkdir($sTmpPath, 0700);
$this->sTmpPath = $sTmpPath;
$this->sZipFileName = $sZipFileName;
$this->aPaths = array();
$aReplace = array(
"[" => "[[]",
" " => "[ ]",
"*" => "[*]",
"?" => "[?]",
);
$this->aReplaceKeys = array_keys($aReplace);
$this->aReplaceValues = array_values($aReplace);
}
/**
* Add a document to the zip file
*/
function addDocumentToZip($oDocument) {
$sParentFolder = str_replace('<', '', str_replace('</', '', str_replace('>', '', sprintf('%s/%s', $this->sTmpPath, $oDocument->getFullPath()))));
$newDir = $this->sTmpPath;
$sFullPath = str_replace('<', '', str_replace('</', '', str_replace('>', '', $this->_convertEncoding($oDocument->getFullPath(), true))));
foreach (split('/', $sFullPath) as $dirPart) {
$newDir = sprintf("%s/%s", $newDir, $dirPart);
if (!file_exists($newDir)) {
mkdir($newDir, 0700);
}
}
$sOrigFile = str_replace('<', '', str_replace('</', '', str_replace('>', '', $this->oStorage->temporaryFile($oDocument))));
$sFilename = sprintf("%s/%s", $sParentFolder, str_replace('<', '', str_replace('</', '', str_replace('>', '', $oDocument->getFileName()))));
$sFilename = $this->_convertEncoding($sFilename, true);
copy($sOrigFile, $sFilename);
$sPath = str_replace('<', '', str_replace('</', '', str_replace('>', '', sprintf("%s/%s", $oDocument->getFullPath(), $oDocument->getFileName()))));
$sPath = str_replace($this->aReplaceKeys, $this->aReplaceValues, $sPath);
$sPath = $this->_convertEncoding($sPath, true);
$this->aPaths[] = $sPath;
return true;
}
/**
* Add a folder to the zip file
*/
function addFolderToZip($oFolder) {
$sFolderPath = $oFolder->getFullPath().'/'.$oFolder->getName().'/';
$sParentFolder = str_replace('<', '', str_replace('</', '', str_replace('>', '', sprintf('%s/%s', $this->sTmpPath, $sFolderPath))));
$newDir = $this->sTmpPath;
$sFullPath = str_replace('<', '', str_replace('</', '', str_replace('>', '', $this->_convertEncoding($sFolderPath, true))));
foreach (split('/', $sFullPath) as $dirPart) {
$newDir = sprintf("%s/%s", $newDir, $dirPart);
if (!file_exists($newDir)) {
mkdir($newDir, 0700);
}
}
$sPath = str_replace('<', '', str_replace('</', '', str_replace('>', '', sprintf("%s", $sFolderPath))));
$sPath = str_replace($this->aReplaceKeys, $this->aReplaceValues, $sPath);
$sPath = $this->_convertEncoding($sPath, true);
$this->aPaths[] = $sPath;
return true;
}
/**
* Zip the temp folder
*/
function createZipFile($bEchoStatus = FALSE) {
if(empty($this->aPaths)){
return PEAR::raiseError(_kt("No folders or documents found to compress"));
//$this->addErrorMessage(_kt("No folders or documents found to compress"));
//return false;
}
$sManifest = sprintf("%s/%s", $this->sTmpPath, "MANIFEST");
file_put_contents($sManifest, join("\n", $this->aPaths));
$sZipFile = sprintf("%s/%s.zip", $this->sTmpPath, $this->sZipFileName);
$sZipFile = str_replace('<', '', str_replace('</', '', str_replace('>', '', $sZipFile)));
$sZipCommand = KTUtil::findCommand("export/zip", "zip");
$aCmd = array($sZipCommand, "-r", $sZipFile, ".", "-i@MANIFEST");
$sOldPath = getcwd();
chdir($this->sTmpPath);
// Note that the popen means that pexec will return a file descriptor
$aOptions = array('popen' => 'r');
$fh = KTUtil::pexec($aCmd, $aOptions);
if($bEchoStatus){
$last_beat = time();
while(!feof($fh)) {
if ($i % 1000 == 0) {
$this_beat = time();
if ($last_beat + 1 < $this_beat) {
$last_beat = $this_beat;
print " ";
}
}
$contents = fread($fh, 4096);
if ($contents) {
print nl2br($this->_convertEncoding($contents, false));
}
$i++;
}
}
pclose($fh);
// Save the zip file and path into session
$_SESSION['zipcompression'] = KTUtil::arrayGet($_SESSION, 'zipcompression', array());
$sExportCode = KTUtil::randomString();
$_SESSION['zipcompression'][$sExportCode] = array(
'file' => $sZipFile,
'dir' => $this->oZip->sTmpPath,
);
$_SESSION['zipcompression']['exportcode'] = $sExportCode;
$this->sZipFile = $sZipFile;
return $sExportCode;
}
/**
* Download the zip file
*/
function downloadZipFile($exportCode = NULL) {
if(!(isset($exportCode) && !empty($exportCode))) {
$exportCode = KTUtil::arrayGet($_SESSION['zipcompression'], 'exportcode');
}
$aData = KTUtil::arrayGet($_SESSION['zipcompression'], $exportCode);
if(!empty($aData)){
$sZipFile = $aData['file'];
$sTmpPath = $aData['dir'];
}else{
$sZipFile = $this->sZipFile;
$sTmpPath = $this->sTmpPath;
}
if (!file_exists($sZipFile)) {
return PEAR::raiseError(_kt('The ZIP file can only be downloaded once - if you cancel the download, you will need to reload the page.'));
}
header("Content-Type: application/zip");
header("Content-Length: ". filesize($sZipFile));
header("Content-Disposition: attachment; filename=\"" . $this->sZipFileName . ".zip" . "\"");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate");
readfile($sZipFile);
$sTmpDir = $sTmpPath;
KTUtil::deleteDirectory($sTmpDir);
return true;
}
/**
* Check that iconv exists and that the selected encoding is supported.
*/
function checkConvertEncoding() {
if(!function_exists("iconv")) {
return PEAR::raiseError(_kt('IConv PHP extension not installed. The zip file compression could not handle output filename encoding conversion !'));
}
$oKTConfig = $this->oKTConfig;
$this->sOutputEncoding = $oKTConfig->get('export/encoding', 'UTF-8');
// Test the specified encoding
if(iconv("UTF-8", $this->sOutputEncoding, "") === FALSE) {
return PEAR::raiseError(_kt('Specified output encoding for the zip files compression does not exists !'));
}
return true;
}
function _convertEncoding($sMystring, $bEncode) {
if (strcasecmp($this->sOutputEncoding, "UTF-8") === 0) {
return $sMystring;
}
if ($bEncode) {
return iconv("UTF-8", $this->sOutputEncoding, $sMystring);
} else {
return iconv($this->sOutputEncoding, "UTF-8", $sMystring);
}
}
}
?>