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.
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
="https://gitlab.com/mojo42/Jirafeau/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');
68 if (isset($_FILES['file']) && is_writable(VAR_FILES
)
69 && is_writable(VAR_LINKS
)) {
70 if (isset ($_POST['upload_password'])) {
71 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
72 echo 'Error 3: Invalid password';
76 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
77 echo 'Error 2: No password nor allowed IP';
82 if (isset($_POST['key'])) {
87 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
88 echo 'Error 4: The parameter time is invalid.';
91 switch ($_POST['time']) {
93 $time +
= JIRAFEAU_MINUTE
;
96 $time +
= JIRAFEAU_HOUR
;
99 $time +
= JIRAFEAU_DAY
;
102 $time +
= JIRAFEAU_WEEK
;
105 $time +
= JIRAFEAU_MONTH
;
108 $time +
= JIRAFEAU_QUARTER
;
111 $time +
= JIRAFEAU_YEAR
;
114 $time = JIRAFEAU_INFINITY
;
120 if ($cfg['maximal_upload_size'] > 0 &&
121 $_FILES['file']['size'] > $cfg['maximal_upload_size'] * 1024 * 1024) {
122 echo 'Error 5: Your file exceeds the maximum authorized file size.';
126 // Check if one time download is enabled
127 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
128 echo 'Error 26: One time download is disabled.';
132 $res = jirafeau_upload($_FILES['file'],
133 isset($_POST['one_time_download']),
134 $key, $time, get_ip_address($cfg),
135 $cfg['enable_crypt'], $cfg['link_name_length'],
138 if (empty($res) ||
$res['error']['has_error']) {
139 echo 'Error 6 ' . $res['error']['why'];
142 /* Print direct link. */
144 /* Print delete link. */
146 echo $res['delete_link'];
147 /* Print decrypt key. */
149 echo urlencode($res['crypt_key']);
150 } elseif (isset($_GET['h'])) {
151 $link_name = $_GET['h'];
153 if (isset($_POST['key'])) {
154 $key = $_POST['key'];
157 if (isset($_GET['d'])) {
161 if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
166 $link = jirafeau_get_link($link_name);
167 if (count($link) == 0) {
171 if (strlen($d) > 0 && $d == $link['link_code']) {
172 jirafeau_delete_link($link_name);
176 if ($link['time'] != JIRAFEAU_INFINITY
&& time() > $link['time']) {
177 jirafeau_delete_link($link_name);
181 if (strlen($link['key']) > 0 && md5($key) != $link['key']) {
186 $p = s2p($link['hash']);
187 if (!file_exists(VAR_FILES
. $p . $link['hash'])) {
193 header('Content-Length: ' . $link['file_size']);
194 header('Content-Type: ' . $link['mime_type']);
195 header('Content-Disposition: attachment; filename="' .
196 $link['file_name'] . '"');
198 $r = fopen(VAR_FILES
. $p . $link['hash'], 'r');
200 print fread($r, 1024);
205 if ($link['onetime'] == 'O') {
206 jirafeau_delete_link($link_name);
209 } elseif (isset($_GET['get_capacity'])) {
210 echo jirafeau_get_max_upload_size_bytes();
211 } elseif (isset($_GET['get_maximal_upload_size'])) {
212 echo $cfg['maximal_upload_size'];
213 } elseif (isset($_GET['get_version'])) {
214 echo JIRAFEAU_VERSION
;
215 } elseif (isset($_GET['lang'])) {
221 # This script has been auto-generated by Jirafeau but you can still edit options below.
224 proxy
='' # Or set JIRAFEAU_PROXY.
225 url
='<?php echo $cfg['web_root
']; ?>' # Or set JIRAFEAU_URL.
226 time
='<?php echo $cfg['availability_default
']; ?>' # Or set JIRAFEAU_TIME.
227 one_time
='' # Or set JIRAFEAU_ONE_TIME.
228 curl
='' # Or set JIRAFEAU_CURL_PATH.
229 upload_password
='' # Or set JIRAFEAU_UPLOAD_PASSWD
232 if [ -n
"$JIRAFEAU_PROXY" ]; then
233 proxy
="$JIRAFEAU_PROXY"
236 if [ -n
"$JIRAFEAU_URL" ]; then
240 if [ -z
"$url" ]; then
241 echo "Please set url in script parameters or export JIRAFEAU_URL"
244 if [ -n
"$JIRAFEAU_TIME" ]; then
245 time
="$JIRAFEAU_TIME"
248 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
252 if [ -n
"$UPLOAD_PASSWD" ]; then
253 upload_password
="$JIRAFEAU_UPLOAD_PASSWORD"
256 if [ -z
"$curl" ]; then
257 curl
="$JIRAFEAU_CURL_PATH"
260 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
264 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
268 if [ -z
"$curl" ]; then
269 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
274 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
275 echo "--------------------------"
280 echo " $0 send FILE [PASSWORD]"
281 echo " $0 get URL [PASSWORD]"
282 echo " $0 delete URL"
284 echo "Global variables to export:"
285 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxysever.example.com:3128«"
286 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
287 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, »month«, »quarter«, »year« or »none«"
288 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
289 echo " JIRAFEAU_CURL : alternative path to curl binary"
290 echo " JIRAFEAU_UPLOAD_PASSWD : upload password"
295 if [ -n
"$proxy" ]; then
300 if [ -n
"$one_time" ]; then
301 options
="$options -F one_time_download=1"
304 if [ -n
"$upload_password" ]; then
305 options
="$options -F upload_password=$upload_password"
311 options
="$options -F key=$password"
317 if [ "$1" == "send" ]; then
318 if [ ! -f
"$2" ]; then
319 echo "File \"$2\" does not exists."
324 res
=$
($curl -X POST
--http1
.0
$proxy $options \
329 if [[ "$res" == Error
* ]]; then
330 echo "Error while uploading."
335 # Not using head or tail to minimise command dependencies
336 code
=$
(cnt
=0; echo "$res" |
while read l
; do
337 if [[ "$cnt" == "0" ]]; then
342 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
343 if [[ "$cnt" == "1" ]]; then
348 key_code
=$
(cnt
=0; echo "$res" |
while read l
; do
349 if [[ "$cnt" == "2" ]]; then
356 echo "Download page:"
357 if [[ $key_code ]]; then
358 echo " ${url}${downloadpage}?h=$code&k=$key_code"
360 echo " ${url}${downloadpage}?h=$code"
362 echo "Direct download:"
363 if [[ $key_code ]]; then
364 echo " ${url}${downloadpage}?h=$code&k=$key_code&d=1"
366 echo " ${url}${downloadpage}?h=$code&d=1"
369 echo " ${url}${downloadpage}?h=$code&d=$del_code"
371 echo "Download via API:"
372 if [[ $key_code ]]; then
373 echo " ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD}"
375 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD}"
377 echo "Delete via API:"
378 echo " ${0} delete ${url}${downloadpage}?h=$code&d=$del_code"
380 elif
[ "$1" == "get" ]; then
381 if [ -z
"$password" ]; then
382 $curl $proxy -OJ
"$2"
384 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
386 elif
[ "$1" == "delete" ]; then
396 /* Initialize an asynchronous upload. */
397 elseif (isset($_GET['init_async'])) {
398 if (isset($_POST['upload_password'])){
399 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
400 echo 'Error 20: Invalid password';
404 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
405 echo 'Error 19: No password nor allowed IP';
410 if (!isset($_POST['filename'])) {
416 if (isset($_POST['type'])) {
417 $type = $_POST['type'];
421 if (isset($_POST['key'])) {
422 $key = $_POST['key'];
425 // Check if one time download is enabled
426 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
427 echo 'Error 26: One time download is disabled.';
432 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
436 switch ($_POST['time']) {
438 $time +
= JIRAFEAU_MINUTE
;
441 $time +
= JIRAFEAU_HOUR
;
444 $time +
= JIRAFEAU_DAY
;
447 $time +
= JIRAFEAU_WEEK
;
450 $time +
= JIRAFEAU_MONTH
;
453 $time +
= JIRAFEAU_QUARTER
;
456 $time +
= JIRAFEAU_YEAR
;
459 $time = JIRAFEAU_INFINITY
;
463 echo jirafeau_async_init($_POST['filename'],
465 isset($_POST['one_time_download']),
468 get_ip_address($cfg));
470 /* Continue an asynchronous upload. */
471 elseif (isset($_GET['push_async'])) {
472 if ((!isset($_POST['ref']))
473 ||
(!isset($_FILES['data']))
474 ||
(!isset($_POST['code']))) {
477 echo jirafeau_async_push($_POST['ref'],
480 $cfg['maximal_upload_size']);
483 /* Finalize an asynchronous upload. */
484 elseif (isset($_GET['end_async'])) {
485 if (!isset($_POST['ref'])
486 ||
!isset($_POST['code'])) {
489 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length'], $cfg['file_hash']);