3 * Jirafeau, your web file repository
4 * Copyright (C) 2015 Jerome Jutteau <jerome@jutteau.fr>
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.
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.
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/>.
20 /* This file offer a kind of API for jirafeau. */
22 define('JIRAFEAU_ROOT', dirname(__FILE__
) . '/');
24 require(JIRAFEAU_ROOT
. 'lib/settings.php');
25 require(JIRAFEAU_ROOT
. 'lib/functions.php');
26 require(JIRAFEAU_ROOT
. 'lib/lang.php');
28 global $script_langages;
29 $script_langages = array('bash' => 'Bash');
31 /* Operations may take a long time.
32 * Be sure PHP's safe mode is off.
36 if ($_SERVER['REQUEST_METHOD'] == "GET" && count($_GET) == 0) {
37 require(JIRAFEAU_ROOT
. 'lib/template/header.php');
41 require(JIRAFEAU_ROOT
. 'lib/template/footer.php');
45 <h2
>Scripting
interface</h2
>
46 <p
>This
interface permits to script your uploads
and downloads
.</p
>
47 <p
>See
<a href
="https://gitlab.com/mojo42/Jirafeau/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
>
52 require(JIRAFEAU_ROOT
. 'lib/template/footer.php');
56 /* Lets use interface now. */
57 header('Content-Type: text/plain; charset=utf-8');
66 if (isset($_FILES['file']) && is_writable(VAR_FILES
)
67 && is_writable(VAR_LINKS
)) {
68 if (isset($_POST['upload_password'])) {
69 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
70 echo 'Error 3: Invalid password';
74 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
75 echo 'Error 2: No password nor allowed IP';
80 if (isset($_POST['key'])) {
85 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
86 echo 'Error 4: The parameter time is invalid.';
89 switch ($_POST['time']) {
91 $time +
= JIRAFEAU_MINUTE
;
94 $time +
= JIRAFEAU_HOUR
;
97 $time +
= JIRAFEAU_DAY
;
100 $time +
= JIRAFEAU_WEEK
;
103 $time +
= JIRAFEAU_FORTNIGHT
;
106 $time +
= JIRAFEAU_MONTH
;
109 $time +
= JIRAFEAU_QUARTER
;
112 $time +
= JIRAFEAU_YEAR
;
115 $time = JIRAFEAU_INFINITY
;
121 if ($cfg['maximal_upload_size'] > 0 &&
122 $_FILES['file']['size'] > $cfg['maximal_upload_size'] * 1024 * 1024) {
123 echo 'Error 5: Your file exceeds the maximum authorized file size.';
127 // Check if one time download is enabled
128 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
129 echo 'Error 26: One time download is disabled.';
133 if ($cfg['store_uploader_ip']) {
134 $ip = get_ip_address($cfg);
139 $res = jirafeau_upload(
141 isset($_POST['one_time_download']),
145 $cfg['enable_crypt'],
146 $cfg['link_name_length'],
150 if (empty($res) ||
$res['error']['has_error']) {
151 echo 'Error 6 ' . $res['error']['why'];
154 /* Print direct link. */
156 /* Print delete link. */
158 echo $res['delete_link'];
159 /* Print decrypt key. */
161 echo urlencode($res['crypt_key']);
162 } elseif (isset($_GET['h'])) {
163 $link_name = $_GET['h'];
165 if (isset($_POST['key'])) {
166 $key = $_POST['key'];
169 if (isset($_GET['d'])) {
173 if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
178 $link = jirafeau_get_link($link_name);
179 if (count($link) == 0) {
183 if (strlen($d) > 0 && $d == $link['link_code']) {
184 jirafeau_delete_link($link_name);
188 if ($link['time'] != JIRAFEAU_INFINITY
&& time() > $link['time']) {
189 jirafeau_delete_link($link_name);
193 if (strlen($link['key']) > 0 && md5($key) != $link['key']) {
198 $p = s2p($link['hash']);
199 if (!file_exists(VAR_FILES
. $p . $link['hash'])) {
205 header('Content-Length: ' . $link['file_size']);
206 header('Content-Type: ' . $link['mime_type']);
207 header('Content-Disposition: attachment; filename="' .
208 $link['file_name'] . '"');
210 $r = fopen(VAR_FILES
. $p . $link['hash'], 'r');
212 print fread($r, 1024);
216 if ($link['onetime'] == 'O') {
217 jirafeau_delete_link($link_name);
220 } elseif (isset($_GET['get_capacity'])) {
221 echo jirafeau_get_max_upload_size_bytes();
222 } elseif (isset($_GET['get_maximal_upload_size'])) {
223 echo $cfg['maximal_upload_size'];
224 } elseif (isset($_GET['get_version'])) {
225 echo JIRAFEAU_VERSION
;
226 } elseif (isset($_GET['lang'])) {
232 # This script has been auto-generated by Jirafeau but you can still edit options below.
235 proxy
='' # Or set JIRAFEAU_PROXY.
236 url
='<?php echo $cfg['web_root
']; ?>' # Or set JIRAFEAU_URL.
237 time
='<?php echo $cfg['availability_default
']; ?>' # Or set JIRAFEAU_TIME.
238 one_time
='' # Or set JIRAFEAU_ONE_TIME.
239 curl
='' # Or set JIRAFEAU_CURL_PATH.
240 upload_password
='' # Or set JIRAFEAU_UPLOAD_PASSWD
243 if [ -n
"$JIRAFEAU_PROXY" ]; then
244 proxy
="$JIRAFEAU_PROXY"
247 if [ -n
"$JIRAFEAU_URL" ]; then
251 if [ -z
"$url" ]; then
252 echo "Please set url in script parameters or export JIRAFEAU_URL"
255 if [ -n
"$JIRAFEAU_TIME" ]; then
256 time
="$JIRAFEAU_TIME"
259 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
263 if [ -n
"$JIRAFEAU_UPLOAD_PASSWD" ]; then
264 upload_password
="$JIRAFEAU_UPLOAD_PASSWD"
267 if [ -z
"$curl" ]; then
268 curl
="$JIRAFEAU_CURL_PATH"
271 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
275 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
279 if [ -z
"$curl" ]; then
280 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
285 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
286 echo "--------------------------"
291 echo " $0 send FILE [PASSWORD]"
292 echo " $0 get URL [PASSWORD]"
293 echo " $0 delete URL"
295 echo "Global variables to export:"
296 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxyserver.example.com:3128«"
297 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
298 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, fortnight, »month«, »quarter«, »year« or »none«"
299 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
300 echo " JIRAFEAU_CURL : alternative path to curl binary"
301 echo " JIRAFEAU_UPLOAD_PASSWD : upload password"
306 if [ -n
"$proxy" ]; then
311 if [ -n
"$one_time" ]; then
312 options
="$options -F one_time_download=1"
315 if [ -n
"$upload_password" ]; then
316 options
="$options -F upload_password=$upload_password"
322 options
="$options -F key=$password"
328 if [ "$1" == "send" ]; then
329 if [ ! -f
"$2" ]; then
330 echo "File \"$2\" does not exists."
335 res
=$
($curl -X POST
--http1
.0
$proxy $options \
340 if [[ "$res" == Error
* ]]; then
341 echo "Error while uploading."
346 # Not using head or tail to minimise command dependencies
347 code
=$
(cnt
=0; echo "$res" |
while read l
; do
348 if [[ "$cnt" == "0" ]]; then
353 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
354 if [[ "$cnt" == "1" ]]; then
359 key_code
=$
(cnt
=0; echo "$res" |
while read l
; do
360 if [[ "$cnt" == "2" ]]; then
367 echo "Download page:"
368 if [[ $key_code ]]; then
369 echo " ${url}${downloadpage}?h=$code&k=$key_code"
371 echo " ${url}${downloadpage}?h=$code"
373 echo "Direct download:"
374 if [[ $key_code ]]; then
375 echo " ${url}${downloadpage}?h=$code&k=$key_code&d=1"
377 echo " ${url}${downloadpage}?h=$code&d=1"
380 echo " ${url}${downloadpage}?h=$code&d=$del_code"
382 echo "Download via API:"
383 if [[ $key_code ]]; then
384 echo " ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD]"
386 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD]"
388 echo "Delete via API:"
389 echo " ${0} delete \"${url}${downloadpage}?h=$code&d=$del_code\""
391 elif
[ "$1" == "get" ]; then
392 if [ -z
"$password" ]; then
393 $curl $proxy -OJ
"$2"
395 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
397 elif
[ "$1" == "delete" ]; then
398 $curl $proxy "$2" --data
-raw
"do_delete=1%2F" | grep
"div class" |sed
-e
"s/<[^>]\+>//g"
406 /* Initialize an asynchronous upload. */
407 elseif (isset($_GET['init_async'])) {
408 if (isset($_POST['upload_password'])) {
409 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
410 echo 'Error 20: Invalid password';
414 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
415 echo 'Error 19: No password nor allowed IP';
420 if (!isset($_POST['filename'])) {
426 if (isset($_POST['type'])) {
427 $type = $_POST['type'];
431 if (isset($_POST['key'])) {
432 $key = $_POST['key'];
435 // Check if one time download is enabled
436 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
437 echo 'Error 26: One time download is disabled.';
442 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
446 switch ($_POST['time']) {
448 $time +
= JIRAFEAU_MINUTE
;
451 $time +
= JIRAFEAU_HOUR
;
454 $time +
= JIRAFEAU_DAY
;
457 $time +
= JIRAFEAU_WEEK
;
460 $time +
= JIRAFEAU_FORTNIGHT
;
463 $time +
= JIRAFEAU_MONTH
;
466 $time +
= JIRAFEAU_QUARTER
;
469 $time +
= JIRAFEAU_YEAR
;
472 $time = JIRAFEAU_INFINITY
;
477 if ($cfg['store_uploader_ip']) {
478 $ip = get_ip_address($cfg);
483 echo jirafeau_async_init(
486 isset($_POST['one_time_download']),
492 /* Continue an asynchronous upload. */
493 elseif (isset($_GET['push_async'])) {
494 if ((!isset($_POST['ref']))
495 ||
(!isset($_FILES['data']))
496 ||
(!isset($_POST['code']))) {
499 echo jirafeau_async_push(
503 $cfg['maximal_upload_size']
507 /* Finalize an asynchronous upload. */
508 elseif (isset($_GET['end_async'])) {
509 if (!isset($_POST['ref'])
510 ||
!isset($_POST['code'])) {
513 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length'], $cfg['file_hash']);