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

patrick-canterino.de