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

patrick-canterino.de