forked from websvnphp/websvn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl.php
326 lines (283 loc) · 7.95 KB
/
dl.php
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
// WebSVN - Subversion repository viewing via the web using PHP
// Copyright (C) 2004-2006 Tim Armes
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// --
//
// dl.php
//
// Allow for file and directory downloads, creating zip/tar/gz files if needed.
require_once 'include/setup.php';
require_once 'include/svnlook.php';
require_once 'include/utils.php';
if (!defined('USE_AUTOLOADER')) {
@include_once 'Archive/Tar.php';
}
function setDirectoryTimestamp($dir, $timestamp) {
global $config;
// Changing the timestamp of a directory in Windows is only supported in PHP 5.3.0+
touch($dir, $timestamp);
if (is_dir($dir)) {
// Set timestamp for all contents, recursing into subdirectories
$handle = opendir($dir);
if ($handle) {
while (($file = readdir($handle)) !== false) {
if ($file == '.' || $file == '..') {
continue;
}
$f = $dir.DIRECTORY_SEPARATOR.$file;
if (is_dir($f)) {
setDirectoryTimestamp($f, $timestamp);
}
}
closedir($handle);
}
}
}
// Make sure that downloading the specified file/directory is permitted
if (!$rep->isDownloadAllowed($path)) {
http_response_code(403);
error_log('Unable to download resource at path: '.$path);
print 'Unable to download resource at path: '.xml_entities($path);
exit;
}
if (!$rep)
{
http_response_code(404);
exit;
}
$svnrep = new SVNRepository($rep);
// Fetch information about a revision (if unspecified, the latest) for this path
if (empty($rev))
{
$history = $svnrep->getLog($path, 'HEAD', '', true, 1, $peg);
}
else if ($rev == $peg)
{
$history = $svnrep->getLog($path, '', 1, true, 1, $peg);
}
else
{
$history = $svnrep->getLog($path, $rev, $rev - 1, true, 1, $peg);
}
$logEntry = ($history) ? $history->entries[0] : null;
if (!$logEntry)
{
http_response_code(404);
error_log('Unable to download resource at path: '.$path);
print 'Unable to download resource at path: '.xml_entities($path);
exit(0);
}
if (empty($rev))
{
$rev = $logEntry->rev;
}
// Create a temporary filename to be used for a directory to archive a download.
// Here we have an unavoidable but highly unlikely to occur race condition.
$tempDir = tempnamWithCheck($config->getTempDir(), 'websvn');
@unlink($tempDir);
mkdir($tempDir);
// Create the name of the directory being archived
$archiveName = $path;
$isDir = (substr($archiveName, -1) == '/');
if ($isDir)
{
$archiveName = substr($archiveName, 0, -1);
}
$archiveName = basename($archiveName);
if ($archiveName == '')
{
$archiveName = $rep->name;
}
$plainfilename = $archiveName;
$archiveName .= '.r'.$rev;
// Export the requested path from SVN repository to the temp directory
$svnExportResult = $svnrep->exportRepositoryPath($path, $tempDir.DIRECTORY_SEPARATOR.$archiveName, $rev, $peg);
if ($svnExportResult != 0)
{
http_response_code(500);
error_log('svn export failed for: '.$archiveName);
print 'svn export failed for "'.xml_entities($archiveName).'".';
removeDirectory($tempDir);
exit(0);
}
// For security reasons, disallow direct downloads of filenames that
// are a symlink, since they may be a symlink to anywhere (/etc/passwd)
// Deciding whether the symlink is relative and legal within the
// repository would be nice but seems to error prone at this moment.
if ( is_link($tempDir.DIRECTORY_SEPARATOR.$archiveName) )
{
http_response_code(500);
error_log('to be downloaded file is symlink, aborting: '.$archiveName);
print 'Download of symlinks disallowed: "'.xml_entities($archiveName).'".';
removeDirectory($tempDir);
exit(0);
}
// Set timestamp of exported directory (and subdirectories) to timestamp of
// the revision so every archive of a given revision has the same timestamp.
$revDate = $logEntry->date;
$timestamp = mktime(substr($revDate, 11, 2), // hour
substr($revDate, 14, 2), // minute
substr($revDate, 17, 2), // second
substr($revDate, 5, 2), // month
substr($revDate, 8, 2), // day
substr($revDate, 0, 4)); // year
setDirectoryTimestamp($tempDir, $timestamp);
// Change to temp directory so that only relative paths are stored in archive.
$oldcwd = getcwd();
chdir($tempDir);
if ($isDir)
{
$downloadMode = $config->getDefaultDirectoryDlMode();
}
else
{
$downloadMode = $config->getDefaultFileDlMode();
}
// $_REQUEST parameter can override dlmode
if (!empty($_REQUEST['dlmode']))
{
$downloadMode = $_REQUEST['dlmode'];
if (substr($logEntry->path, -1) == '/')
{
if (!in_array($downloadMode, $config->validDirectoryDlModes))
{
$downloadMode = $config->getDefaultDirectoryDlMode();
}
}
else
{
if (!in_array($downloadMode, $config->validFileDlModes))
{
$downloadMode = $config->getDefaultFileDlMode();
}
}
}
$downloadArchive = $archiveName;
if ($downloadMode == 'plain')
{
$downloadMimeType = 'application/octet-stream';
}
else if ($downloadMode == 'zip')
{
$downloadMimeType = 'application/zip';
$downloadArchive .= '.zip';
// Create zip file
$cmd = $config->zip.' --symlinks -r '.quote($downloadArchive).' '.quote($archiveName);
execCommand($cmd, $retcode);
if ($retcode != 0)
{
error_log('Unable to call zip command: '.$cmd);
print 'Unable to call zip command. See webserver error log for details.';
}
}
else
{
$downloadMimeType = 'application/gzip';
$downloadArchive .= '.tar.gz';
$tarArchive = $archiveName.'.tar';
// Create the tar file
$retcode = 0;
if (class_exists('Archive_Tar'))
{
$tar = new Archive_Tar($tarArchive);
$created = $tar->create(array($archiveName));
if (!$created)
{
$retcode = 1;
http_response_code(500);
print 'Unable to create tar archive.';
}
}
else
{
$cmd = $config->tar.' -cf '.quote($tarArchive).' '.quote($archiveName);
execCommand($cmd, $retcode);
if ($retcode != 0)
{
http_response_code(500);
error_log('Unable to call tar command: '.$cmd);
print 'Unable to call tar command. See webserver error log for details.';
}
}
if ($retcode != 0)
{
chdir($oldcwd);
removeDirectory($tempDir);
exit(0);
}
// Set timestamp of tar file to timestamp of revision
touch($tarArchive, $timestamp);
// GZIP it up
if (function_exists('gzopen'))
{
$srcHandle = fopen($tarArchive, 'rb');
$dstHandle = gzopen($downloadArchive, 'wb');
if (!$srcHandle || !$dstHandle)
{
http_response_code(500);
print 'Unable to open file for gz-compression.';
chdir($oldcwd);
removeDirectory($tempDir);
exit(0);
}
while (!feof($srcHandle))
{
gzwrite($dstHandle, fread($srcHandle, 1024 * 512));
}
fclose($srcHandle);
gzclose($dstHandle);
}
else
{
$cmd = $config->gzip.' '.quote($tarArchive);
$retcode = 0;
execCommand($cmd, $retcode);
if ($retcode != 0)
{
http_response_code(500);
error_log('Unable to call gzip command: '.$cmd);
print 'Unable to call gzip command. See webserver error log for details.';
chdir($oldcwd);
removeDirectory($tempDir);
exit(0);
}
}
}
// Give the file to the browser
if (is_readable($downloadArchive))
{
if ($downloadMode == 'plain')
{
$downloadFilename = $plainfilename;
}
else
{
$downloadFilename = $rep->name.'-'.$downloadArchive;
}
header('Content-Type: '.$downloadMimeType);
header('Content-Length: '.filesize($downloadArchive));
header('Content-Disposition: attachment; filename="'. $downloadFilename .'"');
readfile($downloadArchive);
}
else
{
http_response_code(404);
print 'Unable to open file: '.xml_entities($downloadArchive);
}
chdir($oldcwd);
removeDirectory($tempDir);