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

patrick-canterino.de