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

patrick-canterino.de