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);
204 if ($link['onetime'] == 'O') {
205 jirafeau_delete_link($link_name);
208 } elseif (isset($_GET['get_capacity'])) {
209 echo jirafeau_get_max_upload_size_bytes();
210 } elseif (isset($_GET['get_maximal_upload_size'])) {
211 echo $cfg['maximal_upload_size'];
212 } elseif (isset($_GET['get_version'])) {
213 echo JIRAFEAU_VERSION
;
214 } elseif (isset($_GET['lang'])) {
220 # This script has been auto-generated by Jirafeau but you can still edit options below.
223 proxy
='' # Or set JIRAFEAU_PROXY.
224 url
='<?php echo $cfg['web_root
']; ?>' # Or set JIRAFEAU_URL.
225 time
='<?php echo $cfg['availability_default
']; ?>' # Or set JIRAFEAU_TIME.
226 one_time
='' # Or set JIRAFEAU_ONE_TIME.
227 curl
='' # Or set JIRAFEAU_CURL_PATH.
228 upload_password
='' # Or set JIRAFEAU_UPLOAD_PASSWD
231 if [ -n
"$JIRAFEAU_PROXY" ]; then
232 proxy
="$JIRAFEAU_PROXY"
235 if [ -n
"$JIRAFEAU_URL" ]; then
239 if [ -z
"$url" ]; then
240 echo "Please set url in script parameters or export JIRAFEAU_URL"
243 if [ -n
"$JIRAFEAU_TIME" ]; then
244 time
="$JIRAFEAU_TIME"
247 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
251 if [ -n
"$UPLOAD_PASSWD" ]; then
252 upload_password
="$JIRAFEAU_UPLOAD_PASSWORD"
255 if [ -z
"$curl" ]; then
256 curl
="$JIRAFEAU_CURL_PATH"
259 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
263 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
267 if [ -z
"$curl" ]; then
268 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
273 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
274 echo "--------------------------"
279 echo " $0 send FILE [PASSWORD]"
280 echo " $0 get URL [PASSWORD]"
281 echo " $0 delete URL"
283 echo "Global variables to export:"
284 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxysever.example.com:3128«"
285 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
286 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, »month«, »quarter«, »year« or »none«"
287 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
288 echo " JIRAFEAU_CURL : alternative path to curl binary"
289 echo " JIRAFEAU_UPLOAD_PASSWD : upload password"
294 if [ -n
"$proxy" ]; then
299 if [ -n
"$one_time" ]; then
300 options
="$options -F one_time_download=1"
303 if [ -n
"$upload_password" ]; then
304 options
="$options -F upload_password=$upload_password"
310 options
="$options -F key=$password"
316 if [ "$1" == "send" ]; then
317 if [ ! -f
"$2" ]; then
318 echo "File \"$2\" does not exists."
323 res
=$
($curl -X POST
--http1
.0
$proxy $options \
328 if [[ "$res" == Error
* ]]; then
329 echo "Error while uploading."
334 # Not using head or tail to minimise command dependencies
335 code
=$
(cnt
=0; echo "$res" |
while read l
; do
336 if [[ "$cnt" == "0" ]]; then
341 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
342 if [[ "$cnt" == "1" ]]; then
347 key_code
=$
(cnt
=0; echo "$res" |
while read l
; do
348 if [[ "$cnt" == "2" ]]; then
355 echo "Download page:"
356 if [[ $key_code ]]; then
357 echo " ${url}${downloadpage}?h=$code&k=$key_code"
359 echo " ${url}${downloadpage}?h=$code"
361 echo "Direct download:"
362 if [[ $key_code ]]; then
363 echo " ${url}${downloadpage}?h=$code&k=$key_code&d=1"
365 echo " ${url}${downloadpage}?h=$code&d=1"
368 echo " ${url}${downloadpage}?h=$code&d=$del_code"
370 echo "Download via API:"
371 if [[ $key_code ]]; then
372 echo " ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD}"
374 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD}"
376 echo "Delete via API:"
377 echo " ${0} delete ${url}${downloadpage}?h=$code&d=$del_code"
379 elif
[ "$1" == "get" ]; then
380 if [ -z
"$password" ]; then
381 $curl $proxy -OJ
"$2"
383 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
385 elif
[ "$1" == "delete" ]; then
395 /* Initialize an asynchronous upload. */
396 elseif (isset($_GET['init_async'])) {
397 if (isset($_POST['upload_password'])){
398 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
399 echo 'Error 20: Invalid password';
403 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
404 echo 'Error 19: No password nor allowed IP';
409 if (!isset($_POST['filename'])) {
415 if (isset($_POST['type'])) {
416 $type = $_POST['type'];
420 if (isset($_POST['key'])) {
421 $key = $_POST['key'];
424 // Check if one time download is enabled
425 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
426 echo 'Error 26: One time download is disabled.';
431 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
435 switch ($_POST['time']) {
437 $time +
= JIRAFEAU_MINUTE
;
440 $time +
= JIRAFEAU_HOUR
;
443 $time +
= JIRAFEAU_DAY
;
446 $time +
= JIRAFEAU_WEEK
;
449 $time +
= JIRAFEAU_MONTH
;
452 $time +
= JIRAFEAU_QUARTER
;
455 $time +
= JIRAFEAU_YEAR
;
458 $time = JIRAFEAU_INFINITY
;
462 echo jirafeau_async_init($_POST['filename'],
464 isset($_POST['one_time_download']),
467 get_ip_address($cfg));
469 /* Continue an asynchronous upload. */
470 elseif (isset($_GET['push_async'])) {
471 if ((!isset($_POST['ref']))
472 ||
(!isset($_FILES['data']))
473 ||
(!isset($_POST['code']))) {
476 echo jirafeau_async_push($_POST['ref'],
479 $cfg['maximal_upload_size']);
482 /* Finalize an asynchronous upload. */
483 elseif (isset($_GET['end_async'])) {
484 if (!isset($_POST['ref'])
485 ||
!isset($_POST['code'])) {
488 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length'], $cfg['file_hash']);