3 * Jirafeau, your web file repository
4 * Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
5 * Copyright (C) 2015 Jerome Jutteau <jerome@jutteau.fr>
6 * Copyright (C) 2024 Jirafeau project <https://gitlab.com/jirafeau> (see AUTHORS.md)
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 /* This file offer a kind of API for jirafeau. */
24 define('JIRAFEAU_ROOT', dirname(__FILE__
) . '/');
26 require(JIRAFEAU_ROOT
. 'lib/settings.php');
27 require(JIRAFEAU_ROOT
. 'lib/functions.php');
28 require(JIRAFEAU_ROOT
. 'lib/lang.php');
30 global $script_langages;
31 $script_langages = array('bash' => 'Bash');
33 /* Operations may take a long time.
34 * Be sure PHP's safe mode is off.
38 if ($_SERVER['REQUEST_METHOD'] == "GET" && count($_GET) == 0) {
39 require(JIRAFEAU_ROOT
. 'lib/template/header.php');
43 require(JIRAFEAU_ROOT
. 'lib/template/footer.php');
47 <h2
>Scripting
interface</h2
>
48 <p
>This
interface permits to script your uploads
and downloads
.</p
>
49 <p
>See
<a href
="<?php echo JIRAFEAU_WEBSITE ?>/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
>
54 require(JIRAFEAU_ROOT
. 'lib/template/footer.php');
58 /* Lets use interface now. */
59 header('Content-Type: text/plain; charset=utf-8');
70 if (isset($_FILES['file']) && is_writable(VAR_FILES
)
71 && is_writable(VAR_LINKS
)) {
72 if (!jirafeau_user_session_logged()) {
73 if (isset($_POST['upload_password']) &&
74 !jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
75 echo 'Error 3: Invalid password';
77 } elseif (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
78 echo 'Error 2: No password nor allowed IP';
84 if (isset($_POST['key'])) {
86 if ($cfg['download_password_requirement'] !== 'generated' && $cfg['download_password_policy'] === 'regex') {
87 if (!preg_match($cfg['download_password_policy_regex'], $key)) {
88 echo 'Error 14: The download password is not complying to the security standards.';
92 } elseif ($cfg['download_password_requirement'] !== 'optional') {
93 echo 'Error 13: The parameter password is required.';
97 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
98 echo 'Error 4: The parameter time is invalid.';
101 $time = jirafeau_datestr_to_int($_POST['time']);
105 if ($cfg['maximal_upload_size'] > 0 &&
106 $_FILES['file']['size'] > $cfg['maximal_upload_size'] * 1024 * 1024) {
107 echo 'Error 5: Your file exceeds the maximum authorized file size.';
111 // Check if one time download is enabled
112 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
113 echo 'Error 26: One time download is disabled.';
117 if ($cfg['store_uploader_ip']) {
118 $ip = get_ip_address($cfg);
123 $res = jirafeau_upload(
125 isset($_POST['one_time_download']),
129 $cfg['enable_crypt'],
130 $cfg['link_name_length'],
134 if (empty($res) ||
$res['error']['has_error']) {
135 echo 'Error 6 ' . $res['error']['why'];
138 /* Print direct link. */
140 /* Print delete link. */
142 echo $res['delete_link'];
143 /* Print decrypt key. */
145 echo urlencode($res['crypt_key']);
146 } elseif (isset($_GET['h'])) {
147 $link_name = $_GET['h'];
149 if (isset($_POST['key'])) {
150 $key = $_POST['key'];
151 if ($cfg['download_password_requirement'] !== 'generated' && $cfg['download_password_policy'] === 'regex') {
152 if (!preg_match($cfg['download_password_policy_regex'], $key)) {
153 echo 'Error 14: The download password is not complying to the security standards.';
157 } elseif ($cfg['download_password_requirement'] !== 'optional') {
158 echo 'Error 13: The parameter password is required.';
162 if (isset($_GET['d'])) {
166 if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
171 $link = jirafeau_get_link($link_name);
172 if (count($link) == 0) {
176 if (strlen($d) > 0 && $d == $link['link_code']) {
177 jirafeau_delete_link($link_name);
181 if ($link['time'] != JIRAFEAU_INFINITY
&& time() > $link['time']) {
182 jirafeau_delete_link($link_name);
186 if (strlen($link['key']) > 0 && md5($key) != $link['key']) {
191 $p = s2p($link['hash']);
192 if (!file_exists(VAR_FILES
. $p . $link['hash'])) {
198 header('Content-Length: ' . $link['file_size']);
199 header('Content-Type: ' . $link['mime_type']);
200 header('Content-Disposition: attachment; filename="' .
201 $link['file_name'] . '"');
203 $r = fopen(VAR_FILES
. $p . $link['hash'], 'r');
205 print fread($r, 1024);
209 if ($link['onetime'] == 'O') {
210 jirafeau_delete_link($link_name);
213 } elseif (isset($_GET['get_capacity'])) {
214 echo jirafeau_get_max_upload_size_bytes();
215 } elseif (isset($_GET['get_maximal_upload_size'])) {
216 echo $cfg['maximal_upload_size'];
217 } elseif (isset($_GET['get_version'])) {
218 echo JIRAFEAU_VERSION
;
219 } elseif (isset($_GET['lang'])) {
225 # This script has been auto-generated by Jirafeau but you can still edit options below.
228 proxy
='' # Or set JIRAFEAU_PROXY.
229 url
='<?php echo $cfg['web_root
']; ?>' # Or set JIRAFEAU_URL.
230 time
='<?php echo $cfg['availability_default
']; ?>' # Or set JIRAFEAU_TIME.
231 one_time
='' # Or set JIRAFEAU_ONE_TIME.
232 curl
='' # Or set JIRAFEAU_CURL_PATH.
233 upload_password
='' # Or set JIRAFEAU_UPLOAD_PASSWD
236 if [ -n
"$JIRAFEAU_PROXY" ]; then
237 proxy
="$JIRAFEAU_PROXY"
240 if [ -n
"$JIRAFEAU_URL" ]; then
244 if [ -z
"$url" ]; then
245 echo "Please set url in script parameters or export JIRAFEAU_URL"
248 if [ -n
"$JIRAFEAU_TIME" ]; then
249 time
="$JIRAFEAU_TIME"
252 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
256 if [ -n
"$JIRAFEAU_UPLOAD_PASSWD" ]; then
257 upload_password
="$JIRAFEAU_UPLOAD_PASSWD"
260 if [ -z
"$curl" ]; then
261 curl
="$JIRAFEAU_CURL_PATH"
264 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
268 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
272 if [ -z
"$curl" ]; then
273 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
278 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
279 echo "--------------------------"
284 echo " $0 send FILE [PASSWORD]"
285 echo " $0 get URL [PASSWORD]"
286 echo " $0 delete URL"
288 echo "Global variables to export:"
289 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxyserver.example.com:3128«"
290 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
291 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, fortnight, »month«, »quarter«, »year« or »none«"
292 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
293 echo " JIRAFEAU_CURL : alternative path to curl binary"
294 echo " JIRAFEAU_UPLOAD_PASSWD : upload password"
299 if [ -n
"$proxy" ]; then
304 if [ -n
"$one_time" ]; then
305 options
="$options -F one_time_download=1"
308 if [ -n
"$upload_password" ]; then
309 options
="$options -F upload_password=$upload_password"
315 options
="$options -F key=$password"
321 if [ "$1" == "send" ]; then
322 if [ ! -f
"$2" ]; then
323 echo "File \"$2\" does not exists."
328 res
=$
($curl -X POST
--http1
.0
$proxy $options \
333 if [[ "$res" == Error
* ]]; then
334 echo "Error while uploading."
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
346 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
347 if [[ "$cnt" == "1" ]]; then
352 key_code
=$
(cnt
=0; echo "$res" |
while read l
; do
353 if [[ "$cnt" == "2" ]]; then
360 echo "Download page:"
361 if [[ $key_code ]]; then
362 echo " ${url}${downloadpage}?h=$code&k=$key_code"
364 echo " ${url}${downloadpage}?h=$code"
366 echo "Direct download:"
367 if [[ $key_code ]]; then
368 echo " ${url}${downloadpage}?h=$code&k=$key_code&d=1"
370 echo " ${url}${downloadpage}?h=$code&d=1"
373 echo " ${url}${downloadpage}?h=$code&d=$del_code"
375 echo "Download via API:"
376 if [[ $key_code ]]; then
377 echo " ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD]"
379 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD]"
381 echo "Delete via API:"
382 echo " ${0} delete \"${url}${downloadpage}?h=$code&d=$del_code\""
384 elif
[ "$1" == "get" ]; then
385 if [ -z
"$password" ]; then
386 $curl $proxy -OJ
"$2"
388 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
390 elif
[ "$1" == "delete" ]; then
391 $curl $proxy "$2" --data
-raw
"do_delete=1%2F" | grep
"div class" |sed
-e
"s/<[^>]\+>//g"
399 /* Initialize an asynchronous upload. */
400 elseif (isset($_GET['init_async'])) {
401 if (jirafeau_user_session_logged()) {
402 } elseif (isset($_POST['upload_password'])) {
403 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
404 echo 'Error 20: Invalid password';
408 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
409 echo 'Error 19: No password nor allowed IP';
414 if (!isset($_POST['filename'])) {
420 if (isset($_POST['type'])) {
421 $type = $_POST['type'];
425 if (isset($_POST['key'])) {
426 $key = $_POST['key'];
427 if ($cfg['download_password_requirement'] !== 'generated' && $cfg['download_password_policy'] === 'regex') {
428 if (!preg_match($cfg['download_password_policy_regex'], $key)) {
429 echo 'Error 14: The download password is not complying to the security standards.';
433 } elseif ($cfg['download_password_requirement'] !== 'optional') {
434 echo 'Error 13: The parameter password is required.';
438 // Check if one time download is enabled
439 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
440 echo 'Error 26: One time download is disabled.';
444 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
448 $time = jirafeau_datestr_to_int($_POST['time']);
451 if ($cfg['store_uploader_ip']) {
452 $ip = get_ip_address($cfg);
457 echo jirafeau_async_init(
460 isset($_POST['one_time_download']),
466 /* Continue an asynchronous upload. */
467 elseif (isset($_GET['push_async'])) {
468 if ((!isset($_POST['ref']))
469 ||
(!isset($_FILES['data']))
470 ||
(!isset($_POST['code']))) {
473 echo jirafeau_async_push(
477 $cfg['maximal_upload_size']
481 /* Finalize an asynchronous upload. */
482 elseif (isset($_GET['end_async'])) {
483 if (!isset($_POST['ref'])
484 ||
!isset($_POST['code'])) {
487 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length'], $cfg['file_hash']);