/
download.php
256 lines (224 loc) · 9.1 KB
/
download.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
<?php
/**************************************************************************
* *
* 4images - A Web Based Image Gallery Management System *
* ---------------------------------------------------------------- *
* *
* File: download.php *
* Copyright: (C) 2002-2023 4homepages.de *
* Email: 4images@4homepages.de *
* Web: http://www.4homepages.de *
* Scriptversion: 1.10 *
* *
**************************************************************************
* *
* Dieses Script ist KEINE Freeware. Bitte lesen Sie die Lizenz- *
* bedingungen (Lizenz.txt) für weitere Informationen. *
* --------------------------------------------------------------- *
* This script is NOT freeware! Please read the Copyright Notice *
* (Licence.txt) for further information. *
* *
*************************************************************************/
$main_template = 0;
$nozip = 1;
define('GET_CACHES', 1);
define('ROOT_PATH', './');
include(ROOT_PATH.'global.php');
require(ROOT_PATH.'includes/sessions.php');
$user_access = get_permission();
if (!function_exists('file_get_contents')) {
function file_get_contents($filename, $incpath = false, $resource_context = null) {
if (false === $fh = fopen($filename, 'rb', $incpath)) {
user_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING);
return false;
}
clearstatcache();
if ($fsize = @filesize($filename)) {
$data = fread($fh, $fsize);
} else {
$data = '';
while (!feof($fh)) {
$data .= fread($fh, 8192);
}
}
fclose($fh);
return $data;
}
}
function fix_file_path($file_path) {
if (!is_remote_file($file_path) && !file_exists($file_path)) {
$file_path = preg_replace("/\/{2,}/", "/", get_document_root()."/".$file_path);
}
return $file_path;
}
function send_file($file_name, $file_path) {
@session_write_close();
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
if (get_user_os() == "MAC") {
header("Content-Type: application/x-unknown\n");
header("Content-Disposition: attachment; filename=\"".$file_name."\"\n");
}
elseif (get_browser_info() == "MSIE") {
$disposition = (!preg_match("/\.zip$/i", $file_name)) ? 'attachment' : 'inline';
header("Content-Disposition: $disposition; filename=\"".$file_name."\"\n");
header("Content-Type: application/x-ms-download\n");
}
else {
header("Content-Disposition: attachment; filename=\"".$file_name."\"\n");
header("Content-Type: application/octet-stream\n");
}
$file_path = fix_file_path($file_path);
if (!is_remote_file($file_path) && ($filesize = filesize($file_path)) > 0 && !@ini_get('zlib.output_compression') && !@ini_get('output_handler')) {
header("Content-Length: ".$filesize."\n\n");
}
@readfile($file_path);
}
$file = array();
$file_path = null;
$file_name = null;
if ($action == "lightbox") {
if (empty($user_info['lightbox_image_ids']) || !function_exists("gzcompress") || !function_exists("crc32")) {
redirect("lightbox.php");
}
if (!check_download_token($user_info['lightbox_image_ids'])) {
redirect("lightbox.php");
}
$image_id_sql = str_replace(" ", ", ", trim($user_info['lightbox_image_ids']));
$image_ids = array();
$sql = "SELECT image_id, cat_id, image_media_file, image_download_url
FROM ".IMAGES_TABLE."
WHERE image_active = 1 AND image_id IN ($image_id_sql) AND cat_id NOT IN (".get_auth_cat_sql("auth_viewimage", "NOTIN").", ".get_auth_cat_sql("auth_viewcat", "NOTIN").", ".get_auth_cat_sql("auth_download", "NOTIN").")";
$result = $site_db->query($sql);
if ($result) {
include(ROOT_PATH."includes/zip.php");
$zipfile = new zipfile();
$file_added = 0;
while ($image_row = $site_db->fetch_array($result)) {
$file_path = null;
$file_name = null;
if (!empty($image_row['image_download_url'])) {
if (is_remote_file($image_row['image_download_url']) || is_local_file($image_row['image_download_url'])) {
$file_path = $image_row['image_download_url'];
$file_name = basename($image_row['image_download_url']);
}
}
elseif (is_remote($image_row['image_media_file'])) {
$file_path = $image_row['image_media_file'];
$file_name = get_basefile($image_row['image_media_file']);
}
else {
$file_path = MEDIA_PATH."/".$image_row['cat_id']."/".$image_row['image_media_file'];
$file_name = $image_row['image_media_file'];
}
if (!empty($file_path)) {
@set_time_limit(120);
$file_path = fix_file_path($file_path);
if (!$file_data = @file_get_contents($file_path)) {
continue;
}
$zipfile->add_file($file_data, $file_name);
$file_added = 1;
unset($file_data);
$image_ids[] = $image_row['image_id'];
}
}
if ($file_added) {
if ($user_info['user_level'] != ADMIN) {
$sql = "UPDATE ".IMAGES_TABLE."
SET image_downloads = image_downloads + 1
WHERE image_id IN (".trim(implode(", ", $image_ids)).")";
$site_db->query($sql);
}
$zipfile->send(time().".zip");
exit;
}
else {
redirect("lightbox.php?empty=1");
}
}
}
elseif ($image_id) {
if (isset($HTTP_GET_VARS['size']) || isset($HTTP_POST_VARS['size'])) {
$size = (isset($HTTP_GET_VARS['size'])) ? intval($HTTP_GET_VARS['size']) : intval($HTTP_POST_VARS['size']);
}
else {
$size = 0;
}
$sql = "SELECT image_id, cat_id, user_id, image_media_file, image_download_url, image_downloads
FROM ".IMAGES_TABLE."
WHERE image_id = $image_id AND image_active = 1";
$image_row = $site_db->query_firstrow($sql);
if (!$image_row || !check_permission("auth_viewcat", $image_row['cat_id']) || !check_permission("auth_viewimage", $image_row['cat_id'])) {
redirect($url);
}
else {
if (!check_permission("auth_download", $image_row['cat_id'])) {
redirect($url);
}
if (!check_download_token($image_row['image_id'])) {
echo "Hotlinking is not allowed";
exit;
redirect("index.php");
}
}
$remote_url = 0;
if (!empty($image_row['image_download_url'])) {
if (is_remote_file($image_row['image_download_url']) || is_local_file($image_row['image_download_url'])) {
preg_match("/(.+)\.(.+)/", basename($image_row['image_download_url']), $regs);
$file_name = $regs[1];
$file_extension = $regs[2];
$file['file_name'] = $file_name.(($size) ? "_".$size : "").".".$file_extension;
$file['file_path'] = dirname($image_row['image_download_url'])."/".$file['file_name'];
}
else {
$file['file_path'] = $image_row['image_download_url'];
$remote_url = 1;
}
}
elseif (is_remote_file($image_row['image_media_file'])) {
preg_match("/(.+)\.(.+)/", get_basefile($image_row['image_media_file']), $regs);
$file_name = $regs[1];
$file_extension = $regs[2];
$file['file_name'] = $file_name.(($size) ? "_".$size : "").".".$file_extension;
$file['file_path'] = dirname($image_row['image_media_file'])."/".$file['file_name'];
}
else {
preg_match("/(.+)\.(.+)/", get_basefile($image_row['image_media_file']), $regs);
$file_name = $regs[1];
$file_extension = $regs[2];
$file['file_name'] = $file_name.(($size) ? "_".$size : "").".".$file_extension;
$file['file_path'] = (is_local_file($image_row['image_media_file'])) ? dirname($image_row['image_media_file'])."/".$file['file_name'] : MEDIA_PATH."/".$image_row['cat_id']."/".$file['file_name'];
}
if ($user_info['user_level'] != ADMIN) {
$sql = "UPDATE ".IMAGES_TABLE."
SET image_downloads = image_downloads + 1
WHERE image_id = $image_id";
$site_db->query($sql);
}
if (!empty($file['file_path'])) {
@set_time_limit(120);
if ($remote_url) {
redirect($file['file_path']);
}
if ($action == "zip" && !preg_match("/\.zip$/i", $file['file_name']) && function_exists("gzcompress") && function_exists("crc32")) {
include(ROOT_PATH."includes/zip.php");
$zipfile = new zipfile();
$zipfile->add_file(file_get_contents($file['file_path']), $file['file_name']);
$zipfile->send(get_file_name($file['file_name']).".zip");
} else {
send_file($file['file_name'], $file['file_path']);
}
exit;
}
else {
echo $lang['download_error']."\n<!-- EMPTY FILE PATH //-->";
exit;
}
}
else {
echo $lang['download_error']."\n<!-- NO ACTION SPECIFIED //-->";
exit;
}
exit;
?>