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
="<?php echo JIRAFEAU_ABSPREFIX . '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 (!jirafeau_challenge_upload_ip($cfg, get_ip_address($cfg))) {
78 if (jirafeau_has_upload_password($cfg) &&
79 (!isset($_POST['upload_password']) ||
80 !jirafeau_challenge_upload_password($cfg, $_POST['upload_password']))) {
86 if (isset($_POST['key'])) {
91 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
92 echo 'Error 4: The parameter time is invalid.';
95 switch ($_POST['time']) {
97 $time +
= JIRAFEAU_MINUTE
;
100 $time +
= JIRAFEAU_HOUR
;
103 $time +
= JIRAFEAU_DAY
;
106 $time +
= JIRAFEAU_WEEK
;
109 $time +
= JIRAFEAU_MONTH
;
112 $time +
= JIRAFEAU_QUARTER
;
115 $time +
= JIRAFEAU_YEAR
;
118 $time = JIRAFEAU_INFINITY
;
124 if ($cfg['maximal_upload_size'] > 0 &&
125 $_FILES['file']['size'] > $cfg['maximal_upload_size'] * 1024 * 1024) {
126 echo 'Error 5: Your file exceeds the maximum authorized file size.';
130 $res = jirafeau_upload($_FILES['file'],
131 isset($_POST['one_time_download']),
132 $key, $time, get_ip_address($cfg),
133 $cfg['enable_crypt'], $cfg['link_name_length']);
135 if (empty($res) ||
$res['error']['has_error']) {
136 echo 'Error 6 ' . $res['error']['why'];
139 /* Print direct link. */
141 /* Print delete link. */
143 echo $res['delete_link'];
144 /* Print decrypt key. */
146 echo urlencode($res['crypt_key']);
147 } elseif (isset($_GET['h'])) {
148 $link_name = $_GET['h'];
150 if (isset($_POST['key'])) {
151 $key = $_POST['key'];
154 if (isset($_GET['d'])) {
158 if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
163 $link = jirafeau_get_link($link_name);
164 if (count($link) == 0) {
168 if (strlen($d) > 0 && $d == $link['link_code']) {
169 jirafeau_delete_link($link_name);
173 if ($link['time'] != JIRAFEAU_INFINITY
&& time() > $link['time']) {
174 jirafeau_delete_link($link_name);
178 if (strlen($link['key']) > 0 && md5($key) != $link['key']) {
183 $p = s2p($link['md5']);
184 if (!file_exists(VAR_FILES
. $p . $link['md5'])) {
190 header('Content-Length: ' . $link['file_size']);
191 header('Content-Type: ' . $link['mime_type']);
192 header('Content-Disposition: attachment; filename="' .
193 $link['file_name'] . '"');
195 $r = fopen(VAR_FILES
. $p . $link['md5'], 'r');
197 print fread($r, 1024);
202 if ($link['onetime'] == 'O') {
203 jirafeau_delete_link($link_name);
206 } elseif (isset($_GET['get_capacity'])) {
207 echo min(jirafeau_ini_to_bytes(ini_get('post_max_size')),
208 jirafeau_ini_to_bytes(ini_get('upload_max_filesize')));
209 } elseif (isset($_GET['get_maximal_upload_size'])) {
210 echo $cfg['maximal_upload_size'];
211 } elseif (isset($_GET['get_version'])) {
212 echo JIRAFEAU_VERSION
;
213 } elseif (isset($_GET['lang'])) {
219 # This script has been auto-generated by Jirafeau but you can still edit options below.
222 proxy
='' # Or set JIRAFEAU_PROXY.
223 url
='<?php echo $cfg['web_root
']; ?>' # Or set JIRAFEAU_URL.
224 time
='<?php echo $cfg['availability_default
']; ?>' # Or set JIRAFEAU_TIME.
225 one_time
='' # Or set JIRAFEAU_ONE_TIME.
226 curl
='' # Or set JIRAFEAU_CURL_PATH.
229 if [ -n
"$JIRAFEAU_PROXY" ]; then
230 proxy
="$JIRAFEAU_PROXY"
233 if [ -n
"$JIRAFEAU_URL" ]; then
237 if [ -z
"$url" ]; then
238 echo "Please set url in script parameters or export JIRAFEAU_URL"
241 if [ -n
"$JIRAFEAU_TIME" ]; then
242 time
="$JIRAFEAU_TIME"
245 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
249 if [ -z
"$curl" ]; then
250 curl
="$JIRAFEAU_CURL_PATH"
253 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
257 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
261 if [ -z
"$curl" ]; then
262 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
267 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
268 echo "--------------------------"
273 echo " $0 send FILE [PASSWORD]"
274 echo " $0 get URL [PASSWORD]"
275 echo " $0 delete URL"
277 echo "Global variables to export:"
278 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxysever.example.com:3128«"
279 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
280 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, »month«, »quarter«, »year« or »none«"
281 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
282 echo " JIRAFEAU_CURL : alternative path to curl binary"
287 if [ -n
"$proxy" ]; then
292 if [ -n
"$one_time" ]; then
293 options
="$options -F one_time_download=1"
299 options
="$options -F key=$password"
305 if [ "$1" == "send" ]; then
306 if [ ! -f
"$2" ]; then
307 echo "File \"$2\" does not exists."
312 res
=$
($curl -X POST
--http1
.0
$proxy $options \
317 if [[ "$res" == Error
* ]]; then
318 echo "Error while uploading."
323 # Not using head or tail to minimise command dependencies
324 code
=$
(cnt
=0; echo "$res" |
while read l
; do
325 if [[ "$cnt" == "0" ]]; then
330 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
331 if [[ "$cnt" == "1" ]]; then
338 echo "Download page:"
339 echo " ${url}${downloadpage}?h=$code"
340 echo "Direct download:"
341 echo " ${url}${downloadpage}?h=$code&d=1"
343 echo " ${url}${downloadpage}?h=$code&d=$del_code"
345 echo "Download via API:"
346 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD}"
347 echo "Delete via API:"
348 echo " ${0} delete ${url}${downloadpage}?h=$code&d=$del_code"
350 elif
[ "$1" == "get" ]; then
351 if [ -z
"$password" ]; then
352 $curl $proxy -OJ
"$2"
354 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
356 elif
[ "$1" == "delete" ]; then
367 elseif (isset($_GET['alias_create'])) {
368 $ip = get_ip_address($cfg);
369 if (!jirafeau_challenge_upload_ip($cfg, $ip)) {
374 if (jirafeau_has_upload_password($cfg) &&
375 (!isset($_POST['upload_password']) ||
376 !jirafeau_challenge_upload_password($cfg, $_POST['upload_password']))) {
381 if (!isset($_POST['alias']) ||
382 !isset($_POST['destination']) ||
383 !isset($_POST['password'])) {
388 echo jirafeau_alias_create($_POST['alias'],
389 $_POST['destination'],
394 elseif (isset($_GET['alias_get'])) {
395 if (!isset($_POST['alias'])) {
400 echo jirafeau_alias_get($_POST['alias']);
403 elseif (isset($_GET['alias_update'])) {
404 if (!isset($_POST['alias']) ||
405 !isset($_POST['destination']) ||
406 !isset($_POST['password'])) {
412 if (isset($_POST['new_password'])) {
413 $new_password = $_POST['new_password'];
416 echo jirafeau_alias_update($_POST['alias'],
417 $_POST['destination'],
420 get_ip_address($cfg));
423 elseif (isset($_GET['alias_delete'])) {
424 if (!isset($_POST['alias']) ||
425 !isset($_POST['password'])) {
430 echo jirafeau_alias_delete($_POST['alias'],
433 /* Initialize an asynchronous upload. */
434 elseif (isset($_GET['init_async'])) {
435 if (!jirafeau_challenge_upload_ip($cfg, get_ip_address($cfg))) {
440 if (jirafeau_has_upload_password($cfg) &&
441 (!isset($_POST['upload_password']) ||
442 !jirafeau_challenge_upload_password($cfg, $_POST['upload_password']))) {
447 if (!isset($_POST['filename'])) {
453 if (isset($_POST['type'])) {
454 $type = $_POST['type'];
458 if (isset($_POST['key'])) {
459 $key = $_POST['key'];
463 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
467 switch ($_POST['time']) {
469 $time +
= JIRAFEAU_MINUTE
;
472 $time +
= JIRAFEAU_HOUR
;
475 $time +
= JIRAFEAU_DAY
;
478 $time +
= JIRAFEAU_WEEK
;
481 $time +
= JIRAFEAU_MONTH
;
484 $time +
= JIRAFEAU_QUARTER
;
487 $time +
= JIRAFEAU_YEAR
;
490 $time = JIRAFEAU_INFINITY
;
494 echo jirafeau_async_init($_POST['filename'],
496 isset($_POST['one_time_download']),
499 get_ip_address($cfg));
501 /* Continue an asynchronous upload. */
502 elseif (isset($_GET['push_async'])) {
503 if ((!isset($_POST['ref']))
504 ||
(!isset($_FILES['data']))
505 ||
(!isset($_POST['code']))) {
508 echo jirafeau_async_push($_POST['ref'],
511 $cfg['maximal_upload_size']);
514 /* Finalize an asynchronous upload. */
515 elseif (isset($_GET['end_async'])) {
516 if (!isset($_POST['ref'])
517 ||
!isset($_POST['code'])) {
520 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length']);