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

patrick-canterino.de