1 | <?php |
---|
2 | /* |
---|
3 | * Definitions |
---|
4 | */ |
---|
5 | if(!isset($GLOBALS['phpgw_info'])) |
---|
6 | { |
---|
7 | $GLOBALS['phpgw_info']['flags'] = array( |
---|
8 | 'currentapp' => 'expressoMail1_2', |
---|
9 | 'nonavbar' => true, |
---|
10 | 'noheader' => true |
---|
11 | ); |
---|
12 | } |
---|
13 | |
---|
14 | $GLOBALS['phpgw_info']['server']['download_temp_dir'] = '/tmp'; |
---|
15 | //-----------------------// |
---|
16 | |
---|
17 | /* |
---|
18 | * Requires |
---|
19 | */ |
---|
20 | require_once 'class.attachment.inc.php'; |
---|
21 | require_once '../../header.inc.php'; |
---|
22 | //-----------------------// |
---|
23 | |
---|
24 | /* |
---|
25 | * Get variables |
---|
26 | */ |
---|
27 | $newFilename = urldecode($_GET['newFilename']); |
---|
28 | $indexFile = $_GET['idx_file']; |
---|
29 | $indexPart = $_GET['indexPart']; |
---|
30 | $msgNumber = $_GET['msgNumber']; |
---|
31 | $msgFolder = $_GET['msgFolder']; |
---|
32 | //----------------------------------------------// |
---|
33 | |
---|
34 | /* |
---|
35 | * Functions |
---|
36 | */ |
---|
37 | function downloadFile($strFileType, $strFileName, $newFileName, $fileContent) { |
---|
38 | |
---|
39 | $ContentType = "application/octet-stream"; |
---|
40 | |
---|
41 | if ($strFileType == ".asf") |
---|
42 | $ContentType = "video/x-ms-asf"; |
---|
43 | if ($strFileType == ".avi") |
---|
44 | $ContentType = "video/avi"; |
---|
45 | if ($strFileType == ".doc") |
---|
46 | $ContentType = "application/msword"; |
---|
47 | if ($strFileType == ".zip") |
---|
48 | $ContentType = "application/zip"; |
---|
49 | if ($strFileType == ".xls") |
---|
50 | $ContentType = "application/vnd.ms-excel"; |
---|
51 | if ($strFileType == ".gif") |
---|
52 | $ContentType = "image/gif"; |
---|
53 | if ($strFileType == ".jpg" || $strFileType == "jpeg") |
---|
54 | $ContentType = "image/jpeg"; |
---|
55 | if ($strFileType == ".wav") |
---|
56 | $ContentType = "audio/wav"; |
---|
57 | if ($strFileType == ".mp3") |
---|
58 | $ContentType = "audio/mpeg3"; |
---|
59 | if ($strFileType == ".mpg" || $strFileType == "mpeg") |
---|
60 | $ContentType = "video/mpeg"; |
---|
61 | if ($strFileType == ".rtf") |
---|
62 | $ContentType = "application/rtf"; |
---|
63 | if ($strFileType == ".htm" || $strFileType == "html") |
---|
64 | $ContentType = "text/html"; |
---|
65 | if ($strFileType == ".xml") |
---|
66 | $ContentType = "text/xml"; |
---|
67 | if ($strFileType == ".xsl") |
---|
68 | $ContentType = "text/xsl"; |
---|
69 | if ($strFileType == ".css") |
---|
70 | $ContentType = "text/css"; |
---|
71 | if ($strFileType == ".php") |
---|
72 | $ContentType = "text/php"; |
---|
73 | if ($strFileType == ".asp") |
---|
74 | $ContentType = "text/asp"; |
---|
75 | if ($strFileType == ".pdf") |
---|
76 | $ContentType = "application/pdf"; |
---|
77 | if ($strFileType == ".txt") |
---|
78 | $ContentType = "text/plain"; |
---|
79 | if ($strFileType == ".log") |
---|
80 | $ContentType = "text/plain"; |
---|
81 | if ($strFileType == ".wmv") |
---|
82 | $ContentType = "video/x-ms-wmv"; |
---|
83 | if ($strFileType == ".sxc") |
---|
84 | $ContentType = "application/vnd.sun.xml.calc"; |
---|
85 | if ($strFileType == ".odt") |
---|
86 | $ContentType = "application/vnd.oasis.opendocument.text"; |
---|
87 | if ($strFileType == ".stc") |
---|
88 | $ContentType = "application/vnd.sun.xml.calc.template"; |
---|
89 | if ($strFileType == ".sxd") |
---|
90 | $ContentType = "application/vnd.sun.xml.draw"; |
---|
91 | if ($strFileType == ".std") |
---|
92 | $ContentType = "application/vnd.sun.xml.draw.template"; |
---|
93 | if ($strFileType == ".sxi") |
---|
94 | $ContentType = "application/vnd.sun.xml.impress"; |
---|
95 | if ($strFileType == ".sti") |
---|
96 | $ContentType = "application/vnd.sun.xml.impress.template"; |
---|
97 | if ($strFileType == ".sxm") |
---|
98 | $ContentType = "application/vnd.sun.xml.math"; |
---|
99 | if ($strFileType == ".sxw") |
---|
100 | $ContentType = "application/vnd.sun.xml.writer"; |
---|
101 | if ($strFileType == ".sxq") |
---|
102 | $ContentType = "application/vnd.sun.xml.writer.global"; |
---|
103 | if ($strFileType == ".stw") |
---|
104 | $ContentType = "application/vnd.sun.xml.writer.template"; |
---|
105 | if ($strFileType == ".ps") |
---|
106 | $ContentType = "application/postscript"; |
---|
107 | if ($strFileType == ".pps") |
---|
108 | $ContentType = "application/vnd.ms-powerpoint"; |
---|
109 | if ($strFileType == ".odt") |
---|
110 | $ContentType = "application/vnd.oasis.opendocument.text"; |
---|
111 | if ($strFileType == ".ott") |
---|
112 | $ContentType = "application/vnd.oasis.opendocument.text-template"; |
---|
113 | if ($strFileType == ".oth") |
---|
114 | $ContentType = "application/vnd.oasis.opendocument.text-web"; |
---|
115 | if ($strFileType == ".odm") |
---|
116 | $ContentType = "application/vnd.oasis.opendocument.text-master"; |
---|
117 | if ($strFileType == ".odg") |
---|
118 | $ContentType = "application/vnd.oasis.opendocument.graphics"; |
---|
119 | if ($strFileType == ".otg") |
---|
120 | $ContentType = "application/vnd.oasis.opendocument.graphics-template"; |
---|
121 | if ($strFileType == ".odp") |
---|
122 | $ContentType = "application/vnd.oasis.opendocument.presentation"; |
---|
123 | if ($strFileType == ".otp") |
---|
124 | $ContentType = "application/vnd.oasis.opendocument.presentation-template"; |
---|
125 | if ($strFileType == ".ods") |
---|
126 | $ContentType = "application/vnd.oasis.opendocument.spreadsheet"; |
---|
127 | if ($strFileType == ".ots") |
---|
128 | $ContentType = "application/vnd.oasis.opendocument.spreadsheet-template"; |
---|
129 | if ($strFileType == ".odc") |
---|
130 | $ContentType = "application/vnd.oasis.opendocument.chart"; |
---|
131 | if ($strFileType == ".odf") |
---|
132 | $ContentType = "application/vnd.oasis.opendocument.formula"; |
---|
133 | if ($strFileType == ".odi") |
---|
134 | $ContentType = "application/vnd.oasis.opendocument.image"; |
---|
135 | if ($strFileType == ".ndl") |
---|
136 | $ContentType = "application/vnd.lotus-notes"; |
---|
137 | if ($strFileType == ".eml") |
---|
138 | $ContentType = "text/plain"; |
---|
139 | |
---|
140 | header ("Content-Type: $ContentType"); |
---|
141 | header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0"); |
---|
142 | header("Pragma: public"); |
---|
143 | header("Expires: 0"); // set expiration time |
---|
144 | header ("Content-Disposition: attachment; filename=\"". addslashes($newFileName)."\""); |
---|
145 | |
---|
146 | if($fileContent) |
---|
147 | { |
---|
148 | echo $fileContent; |
---|
149 | } |
---|
150 | else |
---|
151 | { |
---|
152 | $tempDir = $GLOBALS['phpgw_info']['server']['download_temp_dir']; |
---|
153 | if (preg_match("#^".$tempDir."/(".$GLOBALS['phpgw']->session->sessionid."/)*[A-z0-9_]+_".$GLOBALS['phpgw']->session->sessionid."[A-z0-9]*(\.[A-z]{3,4})?$#",$strFileName)) |
---|
154 | { |
---|
155 | //avoid stuck request |
---|
156 | session_write_close(); |
---|
157 | |
---|
158 | //reset time limit for big files |
---|
159 | set_time_limit(0); |
---|
160 | ob_end_flush(); |
---|
161 | |
---|
162 | if ($fp = fopen ($strFileName, 'rb')) |
---|
163 | { |
---|
164 | $bufferSize=1024; |
---|
165 | for ($i=$bufferSize; $i<=(filesize($strFileName)+$bufferSize); $i+=$bufferSize) |
---|
166 | { |
---|
167 | echo fread($fp, $i); |
---|
168 | flush(); |
---|
169 | } |
---|
170 | fclose ($fp); |
---|
171 | } |
---|
172 | |
---|
173 | //removendo pelo php, garante a suportabilidade cross-platform |
---|
174 | cleanup( dirname( $strFileName ) ); |
---|
175 | |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | if (preg_match("#^".dirname( __FILE__ ) . '/../tmpLclAtt'."/source_#",$strFileName)) { |
---|
180 | readfile($strFileName); |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | /** |
---|
186 | * Delete Diretorio |
---|
187 | * @param string $dir |
---|
188 | */ |
---|
189 | function cleanup( $dir ) |
---|
190 | { |
---|
191 | if ( is_dir( $dir ) ) |
---|
192 | { |
---|
193 | $files = scandir($dir); |
---|
194 | |
---|
195 | foreach( $files as $file ) |
---|
196 | if( $file != "." && $file != ".." ) |
---|
197 | cleanup( $dir.'/'.$file ); |
---|
198 | |
---|
199 | reset( $files ); //? |
---|
200 | |
---|
201 | if(!rmdir( $dir )) |
---|
202 | return; |
---|
203 | } |
---|
204 | else |
---|
205 | { |
---|
206 | if(!unlink( $dir )) |
---|
207 | return; |
---|
208 | } |
---|
209 | |
---|
210 | } |
---|
211 | //-----------------------// |
---|
212 | |
---|
213 | /* |
---|
214 | * Main |
---|
215 | */ |
---|
216 | |
---|
217 | |
---|
218 | if($msgNumber != 'null' && $indexPart !== null && $msgFolder) |
---|
219 | { |
---|
220 | |
---|
221 | $attachment = new attachment(PHPGW_INCLUDE_ROOT); |
---|
222 | $attachment->setStructureFromMail($msgFolder, $msgNumber); |
---|
223 | $fileContent = $attachment->getAttachment($indexPart); |
---|
224 | |
---|
225 | if($newFilename) |
---|
226 | $filename = $newFilename; |
---|
227 | else |
---|
228 | { |
---|
229 | $info = $attachment->getAttachmentInfo($indexPart); |
---|
230 | $filename = $info['name']; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | } |
---|
235 | else |
---|
236 | $filename = $indexFile; |
---|
237 | |
---|
238 | |
---|
239 | $filename = $filename ? $filename : "attachment.bin"; |
---|
240 | $newFilename = $newFilename ? $newFilename : $filename; |
---|
241 | |
---|
242 | |
---|
243 | $strFileType = strrev(substr(strrev(strtolower($filename)),0,4)); |
---|
244 | |
---|
245 | downloadFile($strFileType, $filename, $newFilename, $fileContent) |
---|
246 | //-----------------------// |
---|
247 | |
---|
248 | ?> |
---|