]> git.p6c8.net - jirafeau.git/blob - f.php
add support for http_X_forwarded_for header, refs #36
[jirafeau.git] / f.php
1 <?php
2 /*
3 * Jirafeau, your web file repository
4 * Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
5 * Copyright (C) 2015 Jerome Jutteau <j.jutteau@gmail.com>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 define ('JIRAFEAU_ROOT', dirname (__FILE__) . '/');
21
22 require (JIRAFEAU_ROOT . 'lib/lang.php');
23 require (JIRAFEAU_ROOT . 'lib/config.original.php');
24 require (JIRAFEAU_ROOT . 'lib/settings.php');
25 require (JIRAFEAU_ROOT . 'lib/functions.php');
26
27 if (!isset ($_GET['h']) || empty ($_GET['h']))
28 {
29 header ('Location: ' . $cfg['web_root']);
30 exit;
31 }
32
33 /* Operations may take a long time.
34 * Be sure PHP's safe mode is off.
35 */
36 @set_time_limit(0);
37 /* Remove errors. */
38 @error_reporting(0);
39
40 $link_name = $_GET['h'];
41
42 if (!preg_match ('/[0-9a-zA-Z_-]+$/', $link_name))
43 {
44 require (JIRAFEAU_ROOT.'lib/template/header.php');
45 echo '<div class="error"><p>' . t('Sorry, the requested file is not found') . '</p></div>';
46 require (JIRAFEAU_ROOT.'lib/template/footer.php');
47 exit;
48 }
49
50 $link = jirafeau_get_link ($link_name);
51 if (count ($link) == 0)
52 {
53 require (JIRAFEAU_ROOT.'lib/template/header.php');
54 echo '<div class="error"><p>' . t('Sorry, the requested file is not found') .
55 '</p></div>';
56 require (JIRAFEAU_ROOT.'lib/template/footer.php');
57 exit;
58 }
59
60 $delete_code = '';
61 if (isset ($_GET['d']) && !empty ($_GET['d']) && $_GET['d'] != '1')
62 $delete_code = $_GET['d'];
63
64 $crypt_key = '';
65 if (isset ($_GET['k']) && !empty ($_GET['k']))
66 $crypt_key = $_GET['k'];
67
68 $do_download = false;
69 if (isset ($_GET['d']) && $_GET['d'] == '1')
70 $do_download = true;
71
72 $do_preview = false;
73 if (isset ($_GET['p']) && !empty ($_GET['p']))
74 $do_preview = true;
75
76 $p = s2p ($link['md5']);
77 if (!file_exists (VAR_FILES . $p . $link['md5']))
78 {
79 jirafeau_delete_link ($link_name);
80 require (JIRAFEAU_ROOT.'lib/template/header.php');
81 echo '<div class="error"><p>'.t('File not available.').
82 '</p></div>';
83 require (JIRAFEAU_ROOT.'lib/template/footer.php');
84 exit;
85 }
86
87 if (!empty ($delete_code) && $delete_code == $link['link_code'])
88 {
89 jirafeau_delete_link ($link_name);
90 require (JIRAFEAU_ROOT.'lib/template/header.php');
91 echo '<div class="message"><p>'.t('File has been deleted.').
92 '</p></div>';
93 require (JIRAFEAU_ROOT.'lib/template/footer.php');
94 exit;
95 }
96
97 if ($link['time'] != JIRAFEAU_INFINITY && time () > $link['time'])
98 {
99 jirafeau_delete_link ($link_name);
100 require (JIRAFEAU_ROOT.'lib/template/header.php');
101 echo '<div class="error"><p>'.
102 t('The time limit of this file has expired.') . ' ' .
103 t('File has been deleted.') .
104 '</p></div>';
105 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
106 exit;
107 }
108
109 if (empty ($crypt_key) && $link['crypted'])
110 {
111 require (JIRAFEAU_ROOT.'lib/template/header.php');
112 echo '<div class="error"><p>' . t('Sorry, the requested file is not found') .
113 '</p></div>';
114 require (JIRAFEAU_ROOT.'lib/template/footer.php');
115 exit;
116 }
117
118 $password_challenged = false;
119 if (!empty ($link['key']))
120 {
121 if (!isset ($_POST['key']))
122 {
123 require (JIRAFEAU_ROOT.'lib/template/header.php');
124 echo '<div>' .
125 '<form action = "';
126 echo $cfg['web_root'] . '/f.php';
127 echo '" ' .
128 'method = "post" id = "submit_post">'; ?>
129 <input type = "hidden" name = "jirafeau" value = "<?php echo JIRAFEAU_VERSION ?>"/><?php
130 echo '<fieldset>' .
131 '<legend>' . t('Password protection') .
132 '</legend><table><tr><td>' .
133 t('Give the password of this file') . ' : ' .
134 '<input type = "password" name = "key" />' .
135 '</td></tr>' .
136 '<tr><td>' .
137 t('By using our services, you accept our'). ' <a href="' . $cfg['web_root'] . '/tos.php' . '">' . t('Term Of Service') . '</a>' .
138 '</td></tr>';
139
140 if ($link['onetime'] == 'O')
141 {
142 echo '<tr><td id="self_destruct">' .
143 t('Warning, this file will self-destruct after being read') .
144 '</td></tr>';
145 }
146
147 ?><tr><td><input type="submit" id = "submit_download" value="<?php echo t('Download'); ?>"
148 onclick="document.getElementById('submit_post').action='
149 <?php
150 echo $cfg['web_root'] . '/f.php?h=' . $link_name . '&amp;d=1';
151 if (!empty($crypt_key))
152 echo '&amp;k=' . urlencode($crypt_key);
153 ?>';
154 document.getElementById('submit_download').submit ();"/><?php
155 if ($cfg['preview'] && jirafeau_is_viewable($link['mime_type']))
156 {
157 ?><input type="submit" id = "submit_preview" value="<?php echo t('Preview'); ?>"
158 onclick="document.getElementById('submit_post').action='
159 <?php
160 echo $cfg['web_root'] . '/f.php?h=' . $link_name . '&amp;p=1';
161 if (!empty($crypt_key))
162 echo '&amp;k=' . urlencode($crypt_key);
163 ?>';
164 document.getElementById('submit_preview').submit ();"/><?php
165 }
166 echo '</td></tr></table></fieldset></form></div>';
167 require (JIRAFEAU_ROOT.'lib/template/footer.php');
168 exit;
169 }
170 else
171 {
172 if ($link['key'] == md5 ($_POST['key']))
173 $password_challenged = true;
174 else
175 {
176 require (JIRAFEAU_ROOT.'lib/template/header.php');
177 echo '<div class="error"><p>' . t('Access denied') .
178 '</p></div>';
179 require (JIRAFEAU_ROOT.'lib/template/footer.php');
180 exit;
181 }
182 }
183 }
184
185 if (!$password_challenged && !$do_download && !$do_preview)
186 {
187 require (JIRAFEAU_ROOT.'lib/template/header.php');
188 echo '<div>' .
189 '<form action = "';
190 echo $cfg['web_root'] . '/f.php';
191 echo '" ' .
192 'method = "post" id = "submit_post">'; ?>
193 <input type = "hidden" name = "jirafeau" value = "<?php echo JIRAFEAU_VERSION ?>"/><?php
194 echo '<fieldset><legend>' . htmlspecialchars($link['file_name']) . '</legend><table>' .
195 '<tr><td>' .
196 t('You are about to download') . ' "' . htmlspecialchars($link['file_name']) . '" (' . jirafeau_human_size($link['file_size']) . ')' .
197 '</td></tr>' .
198 '<tr><td>' .
199 t('By using our services, you accept our'). ' <a href="' . $cfg['web_root'] . '/tos.php' . '">' . t('Term Of Service') . '</a>' .
200 '</td></tr>';
201
202 if ($link['onetime'] == 'O')
203 {
204 echo '<tr><td id="self_destruct">' .
205 t('Warning, this file will self-destruct after being read') .
206 '</td></tr>';
207 }
208
209 ?>
210 <tr><td><input type="submit" id = "submit_download" value="<?php echo t('Download'); ?>"
211 onclick="document.getElementById('submit_post').action='
212 <?php
213 echo $cfg['web_root'] . '/f.php?h=' . $link_name . '&amp;d=1';
214 if (!empty($crypt_key))
215 echo '&amp;k=' . urlencode($crypt_key);
216 ?>';
217 document.getElementById('submit_post').submit ();"/><?php
218
219 if ($cfg['preview'] && jirafeau_is_viewable($link['mime_type']))
220 {
221 ?><input type="submit" id = "submit_preview" value="<?php echo t('Preview'); ?>"
222 onclick="document.getElementById('submit_post').action='
223 <?php
224 echo $cfg['web_root'] . '/f.php?h=' . $link_name . '&amp;p=1';
225 if (!empty($crypt_key))
226 echo '&amp;k=' . urlencode($crypt_key);
227 ?>';
228 document.getElementById('submit_post').submit ();"/><?php
229 }
230 echo '</td></tr>';
231 echo '</table></fieldset></form></div>';
232 require (JIRAFEAU_ROOT.'lib/template/footer.php');
233 exit;
234 }
235
236 header ('HTTP/1.0 200 OK');
237 header ('Content-Length: ' . $link['file_size']);
238 if (!jirafeau_is_viewable ($link['mime_type']) || !$cfg['preview'] || $do_download)
239 header ('Content-Disposition: attachment; filename="' . $link['file_name'] . '"');
240 else
241 header ('Content-Disposition: filename="' . $link['file_name'] . '"');
242 header ('Content-Type: ' . $link['mime_type']);
243 header ('Content-MD5: ' . hex_to_base64($link['md5']));
244
245 /* Read encrypted file. */
246 if ($link['crypted'])
247 {
248 /* Init module */
249 $m = mcrypt_module_open('rijndael-256', '', 'ofb', '');
250 /* Extract key and iv. */
251 $md5_key = md5 ($crypt_key);
252 $iv = jirafeau_crypt_create_iv ($md5_key, mcrypt_enc_get_iv_size($m));
253 /* Init module. */
254 mcrypt_generic_init ($m, $md5_key, $iv);
255 /* Decrypt file. */
256 $r = fopen (VAR_FILES . $p . $link['md5'], 'r');
257 while (!feof ($r))
258 {
259 $dec = mdecrypt_generic($m, fread ($r, 1024));
260 print $dec;
261 ob_flush();
262 }
263 fclose ($r);
264 /* Cleanup. */
265 mcrypt_generic_deinit($m);
266 mcrypt_module_close($m);
267 }
268 /* Read file. */
269 else
270 {
271 $r = fopen (VAR_FILES . $p . $link['md5'], 'r');
272 while (!feof ($r))
273 {
274 print fread ($r, 1024);
275 ob_flush();
276 }
277 fclose ($r);
278 }
279
280 if ($link['onetime'] == 'O')
281 jirafeau_delete_link ($link_name);
282 exit;
283
284 ?>

patrick-canterino.de