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

patrick-canterino.de