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

patrick-canterino.de