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

patrick-canterino.de