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

patrick-canterino.de