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

patrick-canterino.de