]> git.p6c8.net - jirafeau_mojo42.git/blob - script.php
[BUGFIX] lower POST size when ̀PHP's upload_max_filesize is too low.
[jirafeau_mojo42.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 'month':
103 $time += JIRAFEAU_MONTH;
104 break;
105 case 'quarter':
106 $time += JIRAFEAU_QUARTER;
107 break;
108 case 'year':
109 $time += JIRAFEAU_YEAR;
110 break;
111 default:
112 $time = JIRAFEAU_INFINITY;
113 break;
114 }
115 }
116
117 // Check file size
118 if ($cfg['maximal_upload_size'] > 0 &&
119 $_FILES['file']['size'] > $cfg['maximal_upload_size'] * 1024 * 1024) {
120 echo 'Error 5: Your file exceeds the maximum authorized file size.';
121 exit;
122 }
123
124 // Check if one time download is enabled
125 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
126 echo 'Error 26: One time download is disabled.';
127 exit;
128 }
129
130 $res = jirafeau_upload($_FILES['file'],
131 isset($_POST['one_time_download']),
132 $key, $time, get_ip_address($cfg),
133 $cfg['enable_crypt'], $cfg['link_name_length'],
134 $cfg['file_hash']);
135
136 if (empty($res) || $res['error']['has_error']) {
137 echo 'Error 6 ' . $res['error']['why'];
138 exit;
139 }
140 /* Print direct link. */
141 echo $res['link'];
142 /* Print delete link. */
143 echo NL;
144 echo $res['delete_link'];
145 /* Print decrypt key. */
146 echo NL;
147 echo urlencode($res['crypt_key']);
148 } elseif (isset($_GET['h'])) {
149 $link_name = $_GET['h'];
150 $key = '';
151 if (isset($_POST['key'])) {
152 $key = $_POST['key'];
153 }
154 $d = '';
155 if (isset($_GET['d'])) {
156 $d = $_GET['d'];
157 }
158
159 if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
160 echo 'Error 7';
161 exit;
162 }
163
164 $link = jirafeau_get_link($link_name);
165 if (count($link) == 0) {
166 echo 'Error 8';
167 exit;
168 }
169 if (strlen($d) > 0 && $d == $link['link_code']) {
170 jirafeau_delete_link($link_name);
171 echo "Ok";
172 exit;
173 }
174 if ($link['time'] != JIRAFEAU_INFINITY && time() > $link['time']) {
175 jirafeau_delete_link($link_name);
176 echo 'Error 9';
177 exit;
178 }
179 if (strlen($link['key']) > 0 && md5($key) != $link['key']) {
180 sleep(2);
181 echo 'Error 10';
182 exit;
183 }
184 $p = s2p($link['hash']);
185 if (!file_exists(VAR_FILES . $p . $link['hash'])) {
186 echo 'Error 11';
187 exit;
188 }
189
190 /* Read file. */
191 header('Content-Length: ' . $link['file_size']);
192 header('Content-Type: ' . $link['mime_type']);
193 header('Content-Disposition: attachment; filename="' .
194 $link['file_name'] . '"');
195
196 $r = fopen(VAR_FILES . $p . $link['hash'], 'r');
197 while (!feof($r)) {
198 print fread($r, 1024);
199 }
200 fclose($r);
201
202 if ($link['onetime'] == 'O') {
203 jirafeau_delete_link($link_name);
204 }
205 exit;
206 } elseif (isset($_GET['get_capacity'])) {
207 echo jirafeau_get_max_upload_size_bytes();
208 } elseif (isset($_GET['get_maximal_upload_size'])) {
209 echo $cfg['maximal_upload_size'];
210 } elseif (isset($_GET['get_version'])) {
211 echo JIRAFEAU_VERSION;
212 } elseif (isset($_GET['lang'])) {
213 $l=$_GET['lang'];
214 if ($l == "bash") {
215 ?>
216 #!/bin/bash
217
218 # This script has been auto-generated by Jirafeau but you can still edit options below.
219
220 # Config begin
221 proxy='' # Or set JIRAFEAU_PROXY.
222 url='<?php echo $cfg['web_root']; ?>' # Or set JIRAFEAU_URL.
223 time='<?php echo $cfg['availability_default']; ?>' # Or set JIRAFEAU_TIME.
224 one_time='' # Or set JIRAFEAU_ONE_TIME.
225 curl='' # Or set JIRAFEAU_CURL_PATH.
226 upload_password='' # Or set JIRAFEAU_UPLOAD_PASSWD
227 # Config end
228
229 if [ -n "$JIRAFEAU_PROXY" ]; then
230 proxy="$JIRAFEAU_PROXY"
231 fi
232
233 if [ -n "$JIRAFEAU_URL" ]; then
234 url="$JIRAFEAU_URL"
235 fi
236
237 if [ -z "$url" ]; then
238 echo "Please set url in script parameters or export JIRAFEAU_URL"
239 fi
240
241 if [ -n "$JIRAFEAU_TIME" ]; then
242 time="$JIRAFEAU_TIME"
243 fi
244
245 if [ -n "$JIRAFEAU_ONE_TIME" ]; then
246 one_time='1'
247 fi
248
249 if [ -n "$UPLOAD_PASSWD" ]; then
250 upload_password="$JIRAFEAU_UPLOAD_PASSWORD"
251 fi
252
253 if [ -z "$curl" ]; then
254 curl="$JIRAFEAU_CURL_PATH"
255 fi
256
257 if [ -z "$curl" ] && [ -e "/usr/bin/curl" ]; then
258 curl="/usr/bin/curl"
259 fi
260
261 if [ -z "$curl" ] && [ -e "/bin/curl.exe" ]; then
262 curl="/bin/curl.exe"
263 fi
264
265 if [ -z "$curl" ]; then
266 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
267 exit
268 fi
269
270 if [ -z "$2" ]; then
271 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
272 echo "--------------------------"
273 echo "Usage:"
274 echo " $0 OPTIONS"
275 echo
276 echo "Options:"
277 echo " $0 send FILE [PASSWORD]"
278 echo " $0 get URL [PASSWORD]"
279 echo " $0 delete URL"
280 echo
281 echo "Global variables to export:"
282 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxysever.example.com:3128«"
283 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
284 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, »month«, »quarter«, »year« or »none«"
285 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
286 echo " JIRAFEAU_CURL : alternative path to curl binary"
287 echo " JIRAFEAU_UPLOAD_PASSWD : upload password"
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 if [ -n "$upload_password" ]; then
302 options="$options -F upload_password=$upload_password"
303 fi
304
305 password=''
306 if [ -n "$3" ]; then
307 password="$3"
308 options="$options -F key=$password"
309 fi
310
311 apipage='script.php'
312 downloadpage='f.php'
313
314 if [ "$1" == "send" ]; then
315 if [ ! -f "$2" ]; then
316 echo "File \"$2\" does not exists."
317 exit
318 fi
319
320 # Ret result
321 res=$($curl -X POST --http1.0 $proxy $options \
322 -F "time=$time" \
323 -F "file=@$2" \
324 $url$apipage)
325
326 if [[ "$res" == Error* ]]; then
327 echo "Error while uploading."
328 echo $res
329 exit
330 fi
331
332 # Not using head or tail to minimise command dependencies
333 code=$(cnt=0; echo "$res" | while read l; do
334 if [[ "$cnt" == "0" ]]; then
335 echo "$l"
336 fi
337 cnt=$(( cnt + 1 ))
338 done)
339 del_code=$(cnt=0; echo "$res" | while read l; do
340 if [[ "$cnt" == "1" ]]; then
341 echo "$l"
342 fi
343 cnt=$(( cnt + 1 ))
344 done)
345 key_code=$(cnt=0; echo "$res" | while read l; do
346 if [[ "$cnt" == "2" ]]; then
347 echo "$l"
348 fi
349 cnt=$(( cnt + 1 ))
350 done)
351
352 echo
353 echo "Download page:"
354 if [[ $key_code ]]; then
355 echo " ${url}${downloadpage}?h=$code&k=$key_code"
356 else
357 echo " ${url}${downloadpage}?h=$code"
358 fi
359 echo "Direct download:"
360 if [[ $key_code ]]; then
361 echo " ${url}${downloadpage}?h=$code&k=$key_code&d=1"
362 else
363 echo " ${url}${downloadpage}?h=$code&d=1"
364 fi
365 echo "Delete link:"
366 echo " ${url}${downloadpage}?h=$code&d=$del_code"
367 echo
368 echo "Download via API:"
369 if [[ $key_code ]]; then
370 echo " ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD}"
371 else
372 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD}"
373 fi
374 echo "Delete via API:"
375 echo " ${0} delete ${url}${downloadpage}?h=$code&d=$del_code"
376
377 elif [ "$1" == "get" ]; then
378 if [ -z "$password" ]; then
379 $curl $proxy -OJ "$2"
380 else
381 $curl $proxy -OJ -X POST -F key=$password "$2"
382 fi
383 elif [ "$1" == "delete" ]; then
384 $curl $proxy "$2"
385 fi
386 <?php
387
388 } else {
389 echo 'Error 12';
390 exit;
391 }
392 }
393 /* Initialize an asynchronous upload. */
394 elseif (isset($_GET['init_async'])) {
395 if (isset($_POST['upload_password'])){
396 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
397 echo 'Error 20: Invalid password';
398 exit;
399 }
400 } else {
401 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
402 echo 'Error 19: No password nor allowed IP';
403 exit;
404 }
405 }
406
407 if (!isset($_POST['filename'])) {
408 echo 'Error 21';
409 exit;
410 }
411
412 $type = '';
413 if (isset($_POST['type'])) {
414 $type = $_POST['type'];
415 }
416
417 $key = '';
418 if (isset($_POST['key'])) {
419 $key = $_POST['key'];
420 }
421
422 // Check if one time download is enabled
423 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
424 echo 'Error 26: One time download is disabled.';
425 exit;
426 }
427
428 $time = time();
429 if (!isset($_POST['time']) || !$cfg['availabilities'][$_POST['time']]) {
430 echo 'Error 22';
431 exit;
432 } else {
433 switch ($_POST['time']) {
434 case 'minute':
435 $time += JIRAFEAU_MINUTE;
436 break;
437 case 'hour':
438 $time += JIRAFEAU_HOUR;
439 break;
440 case 'day':
441 $time += JIRAFEAU_DAY;
442 break;
443 case 'week':
444 $time += JIRAFEAU_WEEK;
445 break;
446 case 'month':
447 $time += JIRAFEAU_MONTH;
448 break;
449 case 'quarter':
450 $time += JIRAFEAU_QUARTER;
451 break;
452 case 'year':
453 $time += JIRAFEAU_YEAR;
454 break;
455 default:
456 $time = JIRAFEAU_INFINITY;
457 break;
458 }
459 }
460 echo jirafeau_async_init($_POST['filename'],
461 $type,
462 isset($_POST['one_time_download']),
463 $key,
464 $time,
465 get_ip_address($cfg));
466 }
467 /* Continue an asynchronous upload. */
468 elseif (isset($_GET['push_async'])) {
469 if ((!isset($_POST['ref']))
470 || (!isset($_FILES['data']))
471 || (!isset($_POST['code']))) {
472 echo 'Error 23';
473 } else {
474 echo jirafeau_async_push($_POST['ref'],
475 $_FILES['data'],
476 $_POST['code'],
477 $cfg['maximal_upload_size']);
478 }
479 }
480 /* Finalize an asynchronous upload. */
481 elseif (isset($_GET['end_async'])) {
482 if (!isset($_POST['ref'])
483 || !isset($_POST['code'])) {
484 echo 'Error 24';
485 } else {
486 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length'], $cfg['file_hash']);
487 }
488 } else {
489 echo 'Error 25';
490 }
491 exit;
492 ?>

patrick-canterino.de