]> git.p6c8.net - jirafeau_project.git/blob - script.php
Add support for the X-Sendfile header/module to offload downloads.
[jirafeau_project.git] / script.php
1 <?php
2 /*
3 * Jirafeau, your web file repository
4 * Copyright (C) 2015 Jerome Jutteau <jerome@jutteau.fr>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 /* This file offer a kind of API for jirafeau. */
21
22 define('JIRAFEAU_ROOT', dirname(__FILE__) . '/');
23
24 require(JIRAFEAU_ROOT . 'lib/settings.php');
25 require(JIRAFEAU_ROOT . 'lib/functions.php');
26 require(JIRAFEAU_ROOT . 'lib/lang.php');
27
28 global $script_langages;
29 $script_langages = array('bash' => 'Bash');
30
31 /* Operations may take a long time.
32 * Be sure PHP's safe mode is off.
33 */
34 @set_time_limit(0);
35
36 if ($_SERVER['REQUEST_METHOD'] == "GET" && count($_GET) == 0) {
37 require(JIRAFEAU_ROOT . 'lib/template/header.php');
38 check_errors($cfg);
39 if (has_error()) {
40 show_errors();
41 require(JIRAFEAU_ROOT . 'lib/template/footer.php');
42 exit;
43 } ?>
44 <div class="info">
45 <h2>Scripting interface</h2>
46 <p>This interface permits to script your uploads and downloads.</p>
47 <p>See <a href="https://gitlab.com/mojo42/Jirafeau/blob/master/script.php">source code</a> of this interface to get available calls :)</p>
48 <p>You may download a preconfigured <a href="script.php?lang=bash">Bash Script</a> to easily send to and get files from the API via command line.</p>
49 </div>
50 <br />
51 <?php
52 require(JIRAFEAU_ROOT . 'lib/template/footer.php');
53 exit;
54 }
55
56 /* Lets use interface now. */
57 header('Content-Type: text/plain; charset=utf-8');
58
59 check_errors($cfg);
60 if (has_error()) {
61 echo 'Error 1';
62 exit;
63 }
64
65 /* Upload file */
66 if (isset($_FILES['file']) && is_writable(VAR_FILES)
67 && is_writable(VAR_LINKS)) {
68 if (isset($_POST['upload_password'])) {
69 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
70 echo 'Error 3: Invalid password';
71 exit;
72 }
73 } else {
74 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
75 echo 'Error 2: No password nor allowed IP';
76 exit;
77 }
78 }
79 $key = '';
80 if (isset($_POST['key'])) {
81 $key = $_POST['key'];
82 }
83
84 $time = time();
85 if (!isset($_POST['time']) || !$cfg['availabilities'][$_POST['time']]) {
86 echo 'Error 4: The parameter time is invalid.';
87 exit;
88 } else {
89 switch ($_POST['time']) {
90 case 'minute':
91 $time += JIRAFEAU_MINUTE;
92 break;
93 case 'hour':
94 $time += JIRAFEAU_HOUR;
95 break;
96 case 'day':
97 $time += JIRAFEAU_DAY;
98 break;
99 case 'week':
100 $time += JIRAFEAU_WEEK;
101 break;
102 case 'fortnight':
103 $time += JIRAFEAU_FORTNIGHT;
104 break;
105 case 'month':
106 $time += JIRAFEAU_MONTH;
107 break;
108 case 'quarter':
109 $time += JIRAFEAU_QUARTER;
110 break;
111 case 'year':
112 $time += JIRAFEAU_YEAR;
113 break;
114 default:
115 $time = JIRAFEAU_INFINITY;
116 break;
117 }
118 }
119
120 // Check file size
121 if ($cfg['maximal_upload_size'] > 0 &&
122 $_FILES['file']['size'] > $cfg['maximal_upload_size'] * 1024 * 1024) {
123 echo 'Error 5: Your file exceeds the maximum authorized file size.';
124 exit;
125 }
126
127 // Check if one time download is enabled
128 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
129 echo 'Error 26: One time download is disabled.';
130 exit;
131 }
132
133 if ($cfg['store_uploader_ip']) {
134 $ip = get_ip_address($cfg);
135 } else {
136 $ip = "";
137 }
138
139 $res = jirafeau_upload(
140 $_FILES['file'],
141 isset($_POST['one_time_download']),
142 $key,
143 $time,
144 $ip,
145 $cfg['enable_crypt'],
146 $cfg['link_name_length'],
147 $cfg['file_hash']
148 );
149
150 if (empty($res) || $res['error']['has_error']) {
151 echo 'Error 6 ' . $res['error']['why'];
152 exit;
153 }
154 /* Print direct link. */
155 echo $res['link'];
156 /* Print delete link. */
157 echo NL;
158 echo $res['delete_link'];
159 /* Print decrypt key. */
160 echo NL;
161 echo urlencode($res['crypt_key']);
162 } elseif (isset($_GET['h'])) {
163 $link_name = $_GET['h'];
164 $key = '';
165 if (isset($_POST['key'])) {
166 $key = $_POST['key'];
167 }
168 $d = '';
169 if (isset($_GET['d'])) {
170 $d = $_GET['d'];
171 }
172
173 if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
174 echo 'Error 7';
175 exit;
176 }
177
178 $link = jirafeau_get_link($link_name);
179 if (count($link) == 0) {
180 echo 'Error 8';
181 exit;
182 }
183 if (strlen($d) > 0 && $d == $link['link_code']) {
184 jirafeau_delete_link($link_name);
185 echo "Ok";
186 exit;
187 }
188 if ($link['time'] != JIRAFEAU_INFINITY && time() > $link['time']) {
189 jirafeau_delete_link($link_name);
190 echo 'Error 9';
191 exit;
192 }
193 if (strlen($link['key']) > 0 && md5($key) != $link['key']) {
194 sleep(2);
195 echo 'Error 10';
196 exit;
197 }
198 $p = s2p($link['hash']);
199 if (!file_exists(VAR_FILES . $p . $link['hash'])) {
200 echo 'Error 11';
201 exit;
202 }
203
204 /* Read file. */
205 header('Content-Length: ' . $link['file_size']);
206 header('Content-Type: ' . $link['mime_type']);
207 header('Content-Disposition: attachment; filename="' .
208 $link['file_name'] . '"');
209
210 $r = fopen(VAR_FILES . $p . $link['hash'], 'r');
211 while (!feof($r)) {
212 print fread($r, 1024);
213 }
214 fclose($r);
215
216 if ($link['onetime'] == 'O') {
217 jirafeau_delete_link($link_name);
218 }
219 exit;
220 } elseif (isset($_GET['get_capacity'])) {
221 echo jirafeau_get_max_upload_size_bytes();
222 } elseif (isset($_GET['get_maximal_upload_size'])) {
223 echo $cfg['maximal_upload_size'];
224 } elseif (isset($_GET['get_version'])) {
225 echo JIRAFEAU_VERSION;
226 } elseif (isset($_GET['lang'])) {
227 $l=$_GET['lang'];
228 if ($l == "bash") {
229 ?>
230 #!/bin/bash
231
232 # This script has been auto-generated by Jirafeau but you can still edit options below.
233
234 # Config begin
235 proxy='' # Or set JIRAFEAU_PROXY.
236 url='<?php echo $cfg['web_root']; ?>' # Or set JIRAFEAU_URL.
237 time='<?php echo $cfg['availability_default']; ?>' # Or set JIRAFEAU_TIME.
238 one_time='' # Or set JIRAFEAU_ONE_TIME.
239 curl='' # Or set JIRAFEAU_CURL_PATH.
240 upload_password='' # Or set JIRAFEAU_UPLOAD_PASSWD
241 # Config end
242
243 if [ -n "$JIRAFEAU_PROXY" ]; then
244 proxy="$JIRAFEAU_PROXY"
245 fi
246
247 if [ -n "$JIRAFEAU_URL" ]; then
248 url="$JIRAFEAU_URL"
249 fi
250
251 if [ -z "$url" ]; then
252 echo "Please set url in script parameters or export JIRAFEAU_URL"
253 fi
254
255 if [ -n "$JIRAFEAU_TIME" ]; then
256 time="$JIRAFEAU_TIME"
257 fi
258
259 if [ -n "$JIRAFEAU_ONE_TIME" ]; then
260 one_time='1'
261 fi
262
263 if [ -n "$UPLOAD_PASSWD" ]; then
264 upload_password="$JIRAFEAU_UPLOAD_PASSWORD"
265 fi
266
267 if [ -z "$curl" ]; then
268 curl="$JIRAFEAU_CURL_PATH"
269 fi
270
271 if [ -z "$curl" ] && [ -e "/usr/bin/curl" ]; then
272 curl="/usr/bin/curl"
273 fi
274
275 if [ -z "$curl" ] && [ -e "/bin/curl.exe" ]; then
276 curl="/bin/curl.exe"
277 fi
278
279 if [ -z "$curl" ]; then
280 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
281 exit
282 fi
283
284 if [ -z "$2" ]; then
285 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
286 echo "--------------------------"
287 echo "Usage:"
288 echo " $0 OPTIONS"
289 echo
290 echo "Options:"
291 echo " $0 send FILE [PASSWORD]"
292 echo " $0 get URL [PASSWORD]"
293 echo " $0 delete URL"
294 echo
295 echo "Global variables to export:"
296 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxyserver.example.com:3128«"
297 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
298 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, fortnight, »month«, »quarter«, »year« or »none«"
299 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
300 echo " JIRAFEAU_CURL : alternative path to curl binary"
301 echo " JIRAFEAU_UPLOAD_PASSWD : upload password"
302
303 exit 0
304 fi
305
306 if [ -n "$proxy" ]; then
307 proxy="-x $proxy"
308 fi
309
310 options=''
311 if [ -n "$one_time" ]; then
312 options="$options -F one_time_download=1"
313 fi
314
315 if [ -n "$upload_password" ]; then
316 options="$options -F upload_password=$upload_password"
317 fi
318
319 password=''
320 if [ -n "$3" ]; then
321 password="$3"
322 options="$options -F key=$password"
323 fi
324
325 apipage='script.php'
326 downloadpage='f.php'
327
328 if [ "$1" == "send" ]; then
329 if [ ! -f "$2" ]; then
330 echo "File \"$2\" does not exists."
331 exit
332 fi
333
334 # Ret result
335 res=$($curl -X POST --http1.0 $proxy $options \
336 -F "time=$time" \
337 -F "file=@$2" \
338 $url$apipage)
339
340 if [[ "$res" == Error* ]]; then
341 echo "Error while uploading."
342 echo $res
343 exit
344 fi
345
346 # Not using head or tail to minimise command dependencies
347 code=$(cnt=0; echo "$res" | while read l; do
348 if [[ "$cnt" == "0" ]]; then
349 echo "$l"
350 fi
351 cnt=$(( cnt + 1 ))
352 done)
353 del_code=$(cnt=0; echo "$res" | while read l; do
354 if [[ "$cnt" == "1" ]]; then
355 echo "$l"
356 fi
357 cnt=$(( cnt + 1 ))
358 done)
359 key_code=$(cnt=0; echo "$res" | while read l; do
360 if [[ "$cnt" == "2" ]]; then
361 echo "$l"
362 fi
363 cnt=$(( cnt + 1 ))
364 done)
365
366 echo
367 echo "Download page:"
368 if [[ $key_code ]]; then
369 echo " ${url}${downloadpage}?h=$code&k=$key_code"
370 else
371 echo " ${url}${downloadpage}?h=$code"
372 fi
373 echo "Direct download:"
374 if [[ $key_code ]]; then
375 echo " ${url}${downloadpage}?h=$code&k=$key_code&d=1"
376 else
377 echo " ${url}${downloadpage}?h=$code&d=1"
378 fi
379 echo "Delete link:"
380 echo " ${url}${downloadpage}?h=$code&d=$del_code"
381 echo
382 echo "Download via API:"
383 if [[ $key_code ]]; then
384 echo " ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD]"
385 else
386 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD]"
387 fi
388 echo "Delete via API:"
389 echo " ${0} delete \"${url}${downloadpage}?h=$code&d=$del_code\""
390
391 elif [ "$1" == "get" ]; then
392 if [ -z "$password" ]; then
393 $curl $proxy -OJ "$2"
394 else
395 $curl $proxy -OJ -X POST -F key=$password "$2"
396 fi
397 elif [ "$1" == "delete" ]; then
398 $curl $proxy "$2" --data-raw "do_delete=1%2F" | grep "div class" |sed -e "s/<[^>]\+>//g"
399 fi
400 <?php
401 } else {
402 echo 'Error 12';
403 exit;
404 }
405 }
406 /* Initialize an asynchronous upload. */
407 elseif (isset($_GET['init_async'])) {
408 if (isset($_POST['upload_password'])) {
409 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
410 echo 'Error 20: Invalid password';
411 exit;
412 }
413 } else {
414 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
415 echo 'Error 19: No password nor allowed IP';
416 exit;
417 }
418 }
419
420 if (!isset($_POST['filename'])) {
421 echo 'Error 21';
422 exit;
423 }
424
425 $type = '';
426 if (isset($_POST['type'])) {
427 $type = $_POST['type'];
428 }
429
430 $key = '';
431 if (isset($_POST['key'])) {
432 $key = $_POST['key'];
433 }
434
435 // Check if one time download is enabled
436 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
437 echo 'Error 26: One time download is disabled.';
438 exit;
439 }
440
441 $time = time();
442 if (!isset($_POST['time']) || !$cfg['availabilities'][$_POST['time']]) {
443 echo 'Error 22';
444 exit;
445 } else {
446 switch ($_POST['time']) {
447 case 'minute':
448 $time += JIRAFEAU_MINUTE;
449 break;
450 case 'hour':
451 $time += JIRAFEAU_HOUR;
452 break;
453 case 'day':
454 $time += JIRAFEAU_DAY;
455 break;
456 case 'week':
457 $time += JIRAFEAU_WEEK;
458 break;
459 case 'fortnight':
460 $time += JIRAFEAU_FORTNIGHT;
461 break;
462 case 'month':
463 $time += JIRAFEAU_MONTH;
464 break;
465 case 'quarter':
466 $time += JIRAFEAU_QUARTER;
467 break;
468 case 'year':
469 $time += JIRAFEAU_YEAR;
470 break;
471 default:
472 $time = JIRAFEAU_INFINITY;
473 break;
474 }
475 }
476
477 if ($cfg['store_uploader_ip']) {
478 $ip = get_ip_address($cfg);
479 } else {
480 $ip = "";
481 }
482
483 echo jirafeau_async_init(
484 $_POST['filename'],
485 $type,
486 isset($_POST['one_time_download']),
487 $key,
488 $time,
489 $ip
490 );
491 }
492 /* Continue an asynchronous upload. */
493 elseif (isset($_GET['push_async'])) {
494 if ((!isset($_POST['ref']))
495 || (!isset($_FILES['data']))
496 || (!isset($_POST['code']))) {
497 echo 'Error 23';
498 } else {
499 echo jirafeau_async_push(
500 $_POST['ref'],
501 $_FILES['data'],
502 $_POST['code'],
503 $cfg['maximal_upload_size']
504 );
505 }
506 }
507 /* Finalize an asynchronous upload. */
508 elseif (isset($_GET['end_async'])) {
509 if (!isset($_POST['ref'])
510 || !isset($_POST['code'])) {
511 echo 'Error 24';
512 } else {
513 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length'], $cfg['file_hash']);
514 }
515 } else {
516 echo 'Error 25';
517 }
518 exit;
519 ?>

patrick-canterino.de