3 * Jirafeau, your web file repository
4 * Copyright (C) 2015 Jerome Jutteau <j.jutteau@gmail.com>
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/>.
21 * This file permits to easyly script file sending, receiving, deleting, ...
22 * If you don't want this feature, you can simply delete this file from your
25 define('JIRAFEAU_ROOT', dirname(__FILE__
) . '/');
27 require(JIRAFEAU_ROOT
. 'lib/settings.php');
28 require(JIRAFEAU_ROOT
. 'lib/functions.php');
29 require(JIRAFEAU_ROOT
. 'lib/lang.php');
31 global $script_langages;
32 $script_langages = array('bash' => 'Bash');
34 /* Operations may take a long time.
35 * Be sure PHP's safe mode is off.
41 if ($_SERVER['REQUEST_METHOD'] == "GET" && count($_GET) == 0) {
42 require(JIRAFEAU_ROOT
. 'lib/template/header.php');
46 require(JIRAFEAU_ROOT
. 'lib/template/footer.php');
50 <h2
>Scripting
interface</h2
>
51 <p
>This
interface permits to script your uploads
and downloads
.</p
>
52 <p
>See
<a href
="https://gitlab.com/mojo42/Jirafeau/blob/master/script.php">source code
</a
> of this
interface to get available calls
:)</p
>
53 <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
>
57 require(JIRAFEAU_ROOT
. 'lib/template/footer.php');
61 /* Lets use interface now. */
62 header('Content-Type: text/plain; charset=utf-8');
71 if (isset($_FILES['file']) && is_writable(VAR_FILES
)
72 && is_writable(VAR_LINKS
)) {
73 if (isset ($_POST['upload_password'])) {
74 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
75 echo 'Error 3: Invalid password';
79 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
80 echo 'Error 2: No password nor allowed IP';
85 if (isset($_POST['key'])) {
90 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
91 echo 'Error 4: The parameter time is invalid.';
94 switch ($_POST['time']) {
96 $time +
= JIRAFEAU_MINUTE
;
99 $time +
= JIRAFEAU_HOUR
;
102 $time +
= JIRAFEAU_DAY
;
105 $time +
= JIRAFEAU_WEEK
;
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 $res = jirafeau_upload($_FILES['file'],
136 isset($_POST['one_time_download']),
137 $key, $time, get_ip_address($cfg),
138 $cfg['enable_crypt'], $cfg['link_name_length'],
141 if (empty($res) ||
$res['error']['has_error']) {
142 echo 'Error 6 ' . $res['error']['why'];
145 /* Print direct link. */
147 /* Print delete link. */
149 echo $res['delete_link'];
150 /* Print decrypt key. */
152 echo urlencode($res['crypt_key']);
153 } elseif (isset($_GET['h'])) {
154 $link_name = $_GET['h'];
156 if (isset($_POST['key'])) {
157 $key = $_POST['key'];
160 if (isset($_GET['d'])) {
164 if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
169 $link = jirafeau_get_link($link_name);
170 if (count($link) == 0) {
174 if (strlen($d) > 0 && $d == $link['link_code']) {
175 jirafeau_delete_link($link_name);
179 if ($link['time'] != JIRAFEAU_INFINITY
&& time() > $link['time']) {
180 jirafeau_delete_link($link_name);
184 if (strlen($link['key']) > 0 && md5($key) != $link['key']) {
189 $p = s2p($link['hash']);
190 if (!file_exists(VAR_FILES
. $p . $link['hash'])) {
196 header('Content-Length: ' . $link['file_size']);
197 header('Content-Type: ' . $link['mime_type']);
198 header('Content-Disposition: attachment; filename="' .
199 $link['file_name'] . '"');
201 $r = fopen(VAR_FILES
. $p . $link['hash'], 'r');
203 print fread($r, 1024);
208 if ($link['onetime'] == 'O') {
209 jirafeau_delete_link($link_name);
212 } elseif (isset($_GET['get_capacity'])) {
213 echo jirafeau_get_max_upload_size_bytes();
214 } elseif (isset($_GET['get_maximal_upload_size'])) {
215 echo $cfg['maximal_upload_size'];
216 } elseif (isset($_GET['get_version'])) {
217 echo JIRAFEAU_VERSION
;
218 } elseif (isset($_GET['lang'])) {
224 # This script has been auto-generated by Jirafeau but you can still edit options below.
227 proxy
='' # Or set JIRAFEAU_PROXY.
228 url
='<?php echo $cfg['web_root
']; ?>' # Or set JIRAFEAU_URL.
229 time
='<?php echo $cfg['availability_default
']; ?>' # Or set JIRAFEAU_TIME.
230 one_time
='' # Or set JIRAFEAU_ONE_TIME.
231 curl
='' # Or set JIRAFEAU_CURL_PATH.
234 if [ -n
"$JIRAFEAU_PROXY" ]; then
235 proxy
="$JIRAFEAU_PROXY"
238 if [ -n
"$JIRAFEAU_URL" ]; then
242 if [ -z
"$url" ]; then
243 echo "Please set url in script parameters or export JIRAFEAU_URL"
246 if [ -n
"$JIRAFEAU_TIME" ]; then
247 time
="$JIRAFEAU_TIME"
250 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
254 if [ -z
"$curl" ]; then
255 curl
="$JIRAFEAU_CURL_PATH"
258 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
262 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
266 if [ -z
"$curl" ]; then
267 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
272 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
273 echo "--------------------------"
278 echo " $0 send FILE [PASSWORD]"
279 echo " $0 get URL [PASSWORD]"
280 echo " $0 delete URL"
282 echo "Global variables to export:"
283 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxysever.example.com:3128«"
284 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
285 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, »month«, »quarter«, »year« or »none«"
286 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
287 echo " JIRAFEAU_CURL : alternative path to curl binary"
292 if [ -n
"$proxy" ]; then
297 if [ -n
"$one_time" ]; then
298 options
="$options -F one_time_download=1"
304 options
="$options -F key=$password"
310 if [ "$1" == "send" ]; then
311 if [ ! -f
"$2" ]; then
312 echo "File \"$2\" does not exists."
317 res
=$
($curl -X POST
--http1
.0
$proxy $options \
322 if [[ "$res" == Error
* ]]; then
323 echo "Error while uploading."
328 # Not using head or tail to minimise command dependencies
329 code
=$
(cnt
=0; echo "$res" |
while read l
; do
330 if [[ "$cnt" == "0" ]]; then
335 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
336 if [[ "$cnt" == "1" ]]; then
341 key_code
=$
(cnt
=0; echo "$res" |
while read l
; do
342 if [[ "$cnt" == "2" ]]; then
349 echo "Download page:"
350 if [[ $key_code ]]; then
351 echo " ${url}${downloadpage}?h=$code&k=$key_code"
353 echo " ${url}${downloadpage}?h=$code"
355 echo "Direct download:"
356 if [[ $key_code ]]; then
357 echo " ${url}${downloadpage}?h=$code&k=$key_code&d=1"
359 echo " ${url}${downloadpage}?h=$code&d=1"
362 echo " ${url}${downloadpage}?h=$code&d=$del_code"
364 echo "Download via API:"
365 if [[ $key_code ]]; then
366 echo " ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD}"
368 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD}"
370 echo "Delete via API:"
371 echo " ${0} delete ${url}${downloadpage}?h=$code&d=$del_code"
373 elif
[ "$1" == "get" ]; then
374 if [ -z
"$password" ]; then
375 $curl $proxy -OJ
"$2"
377 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
379 elif
[ "$1" == "delete" ]; then
389 /* Initialize an asynchronous upload. */
390 elseif (isset($_GET['init_async'])) {
391 if (isset($_POST['upload_password'])){
392 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
393 echo 'Error 20: Invalid password';
397 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
398 echo 'Error 19: No password nor allowed IP';
403 if (!isset($_POST['filename'])) {
409 if (isset($_POST['type'])) {
410 $type = $_POST['type'];
414 if (isset($_POST['key'])) {
415 $key = $_POST['key'];
418 // Check if one time download is enabled
419 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
420 echo 'Error 26: One time download is disabled.';
425 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
429 switch ($_POST['time']) {
431 $time +
= JIRAFEAU_MINUTE
;
434 $time +
= JIRAFEAU_HOUR
;
437 $time +
= JIRAFEAU_DAY
;
440 $time +
= JIRAFEAU_WEEK
;
443 $time +
= JIRAFEAU_MONTH
;
446 $time +
= JIRAFEAU_QUARTER
;
449 $time +
= JIRAFEAU_YEAR
;
452 $time = JIRAFEAU_INFINITY
;
456 echo jirafeau_async_init($_POST['filename'],
458 isset($_POST['one_time_download']),
461 get_ip_address($cfg));
463 /* Continue an asynchronous upload. */
464 elseif (isset($_GET['push_async'])) {
465 if ((!isset($_POST['ref']))
466 ||
(!isset($_FILES['data']))
467 ||
(!isset($_POST['code']))) {
470 echo jirafeau_async_push($_POST['ref'],
473 $cfg['maximal_upload_size']);
476 /* Finalize an asynchronous upload. */
477 elseif (isset($_GET['end_async'])) {
478 if (!isset($_POST['ref'])
479 ||
!isset($_POST['code'])) {
482 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length'], $cfg['file_hash']);