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');
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';
75 } elseif (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
76 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_FORTNIGHT
;
108 $time +
= JIRAFEAU_MONTH
;
111 $time +
= JIRAFEAU_QUARTER
;
114 $time +
= JIRAFEAU_YEAR
;
117 $time = JIRAFEAU_INFINITY
;
123 if ($cfg['maximal_upload_size'] > 0 &&
124 $_FILES['file']['size'] > $cfg['maximal_upload_size'] * 1024 * 1024) {
125 echo 'Error 5: Your file exceeds the maximum authorized file size.';
129 // Check if one time download is enabled
130 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
131 echo 'Error 26: One time download is disabled.';
135 if ($cfg['store_uploader_ip']) {
136 $ip = get_ip_address($cfg);
141 $res = jirafeau_upload(
143 isset($_POST['one_time_download']),
147 $cfg['enable_crypt'],
148 $cfg['link_name_length'],
152 if (empty($res) ||
$res['error']['has_error']) {
153 echo 'Error 6 ' . $res['error']['why'];
156 /* Print direct link. */
158 /* Print delete link. */
160 echo $res['delete_link'];
161 /* Print decrypt key. */
163 echo urlencode($res['crypt_key']);
164 } elseif (isset($_GET['h'])) {
165 $link_name = $_GET['h'];
167 if (isset($_POST['key'])) {
168 $key = $_POST['key'];
171 if (isset($_GET['d'])) {
175 if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
180 $link = jirafeau_get_link($link_name);
181 if (count($link) == 0) {
185 if (strlen($d) > 0 && $d == $link['link_code']) {
186 jirafeau_delete_link($link_name);
190 if ($link['time'] != JIRAFEAU_INFINITY
&& time() > $link['time']) {
191 jirafeau_delete_link($link_name);
195 if (strlen($link['key']) > 0 && md5($key) != $link['key']) {
200 $p = s2p($link['hash']);
201 if (!file_exists(VAR_FILES
. $p . $link['hash'])) {
207 header('Content-Length: ' . $link['file_size']);
208 header('Content-Type: ' . $link['mime_type']);
209 header('Content-Disposition: attachment; filename="' .
210 $link['file_name'] . '"');
212 $r = fopen(VAR_FILES
. $p . $link['hash'], 'r');
214 print fread($r, 1024);
218 if ($link['onetime'] == 'O') {
219 jirafeau_delete_link($link_name);
222 } elseif (isset($_GET['get_capacity'])) {
223 echo jirafeau_get_max_upload_size_bytes();
224 } elseif (isset($_GET['get_maximal_upload_size'])) {
225 echo $cfg['maximal_upload_size'];
226 } elseif (isset($_GET['get_version'])) {
227 echo JIRAFEAU_VERSION
;
228 } elseif (isset($_GET['lang'])) {
234 # This script has been auto-generated by Jirafeau but you can still edit options below.
237 proxy
='' # Or set JIRAFEAU_PROXY.
238 url
='<?php echo $cfg['web_root
']; ?>' # Or set JIRAFEAU_URL.
239 time
='<?php echo $cfg['availability_default
']; ?>' # Or set JIRAFEAU_TIME.
240 one_time
='' # Or set JIRAFEAU_ONE_TIME.
241 curl
='' # Or set JIRAFEAU_CURL_PATH.
242 upload_password
='' # Or set JIRAFEAU_UPLOAD_PASSWD
245 if [ -n
"$JIRAFEAU_PROXY" ]; then
246 proxy
="$JIRAFEAU_PROXY"
249 if [ -n
"$JIRAFEAU_URL" ]; then
253 if [ -z
"$url" ]; then
254 echo "Please set url in script parameters or export JIRAFEAU_URL"
257 if [ -n
"$JIRAFEAU_TIME" ]; then
258 time
="$JIRAFEAU_TIME"
261 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
265 if [ -n
"$JIRAFEAU_UPLOAD_PASSWD" ]; then
266 upload_password
="$JIRAFEAU_UPLOAD_PASSWD"
269 if [ -z
"$curl" ]; then
270 curl
="$JIRAFEAU_CURL_PATH"
273 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
277 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
281 if [ -z
"$curl" ]; then
282 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
287 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
288 echo "--------------------------"
293 echo " $0 send FILE [PASSWORD]"
294 echo " $0 get URL [PASSWORD]"
295 echo " $0 delete URL"
297 echo "Global variables to export:"
298 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxyserver.example.com:3128«"
299 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
300 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, fortnight, »month«, »quarter«, »year« or »none«"
301 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
302 echo " JIRAFEAU_CURL : alternative path to curl binary"
303 echo " JIRAFEAU_UPLOAD_PASSWD : upload password"
308 if [ -n
"$proxy" ]; then
313 if [ -n
"$one_time" ]; then
314 options
="$options -F one_time_download=1"
317 if [ -n
"$upload_password" ]; then
318 options
="$options -F upload_password=$upload_password"
324 options
="$options -F key=$password"
330 if [ "$1" == "send" ]; then
331 if [ ! -f
"$2" ]; then
332 echo "File \"$2\" does not exists."
337 res
=$
($curl -X POST
--http1
.0
$proxy $options \
342 if [[ "$res" == Error
* ]]; then
343 echo "Error while uploading."
348 # Not using head or tail to minimise command dependencies
349 code
=$
(cnt
=0; echo "$res" |
while read l
; do
350 if [[ "$cnt" == "0" ]]; then
355 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
356 if [[ "$cnt" == "1" ]]; then
361 key_code
=$
(cnt
=0; echo "$res" |
while read l
; do
362 if [[ "$cnt" == "2" ]]; then
369 echo "Download page:"
370 if [[ $key_code ]]; then
371 echo " ${url}${downloadpage}?h=$code&k=$key_code"
373 echo " ${url}${downloadpage}?h=$code"
375 echo "Direct download:"
376 if [[ $key_code ]]; then
377 echo " ${url}${downloadpage}?h=$code&k=$key_code&d=1"
379 echo " ${url}${downloadpage}?h=$code&d=1"
382 echo " ${url}${downloadpage}?h=$code&d=$del_code"
384 echo "Download via API:"
385 if [[ $key_code ]]; then
386 echo " ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD]"
388 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD]"
390 echo "Delete via API:"
391 echo " ${0} delete \"${url}${downloadpage}?h=$code&d=$del_code\""
393 elif
[ "$1" == "get" ]; then
394 if [ -z
"$password" ]; then
395 $curl $proxy -OJ
"$2"
397 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
399 elif
[ "$1" == "delete" ]; then
400 $curl $proxy "$2" --data
-raw
"do_delete=1%2F" | grep
"div class" |sed
-e
"s/<[^>]\+>//g"
408 /* Initialize an asynchronous upload. */
409 elseif (isset($_GET['init_async'])) {
410 if (jirafeau_user_session_logged()) {
411 } elseif (isset($_POST['upload_password'])) {
412 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
413 echo 'Error 20: Invalid password';
417 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
418 echo 'Error 19: No password nor allowed IP';
423 if (!isset($_POST['filename'])) {
429 if (isset($_POST['type'])) {
430 $type = $_POST['type'];
434 if (isset($_POST['key'])) {
435 $key = $_POST['key'];
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.';
445 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
449 switch ($_POST['time']) {
451 $time +
= JIRAFEAU_MINUTE
;
454 $time +
= JIRAFEAU_HOUR
;
457 $time +
= JIRAFEAU_DAY
;
460 $time +
= JIRAFEAU_WEEK
;
463 $time +
= JIRAFEAU_FORTNIGHT
;
466 $time +
= JIRAFEAU_MONTH
;
469 $time +
= JIRAFEAU_QUARTER
;
472 $time +
= JIRAFEAU_YEAR
;
475 $time = JIRAFEAU_INFINITY
;
480 if ($cfg['store_uploader_ip']) {
481 $ip = get_ip_address($cfg);
486 echo jirafeau_async_init(
489 isset($_POST['one_time_download']),
495 /* Continue an asynchronous upload. */
496 elseif (isset($_GET['push_async'])) {
497 if ((!isset($_POST['ref']))
498 ||
(!isset($_FILES['data']))
499 ||
(!isset($_POST['code']))) {
502 echo jirafeau_async_push(
506 $cfg['maximal_upload_size']
510 /* Finalize an asynchronous upload. */
511 elseif (isset($_GET['end_async'])) {
512 if (!isset($_POST['ref'])
513 ||
!isset($_POST['code'])) {
516 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length'], $cfg['file_hash']);