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 $res = jirafeau_upload($_FILES['file'],
130 isset($_POST['one_time_download']),
131 $key, $time, get_ip_address($cfg),
132 $cfg['enable_crypt'], $cfg['link_name_length']);
134 if (empty($res) ||
$res['error']['has_error']) {
135 echo 'Error 6 ' . $res['error']['why'];
138 /* Print direct link. */
140 /* Print delete link. */
142 echo $res['delete_link'];
143 /* Print decrypt key. */
145 echo urlencode($res['crypt_key']);
146 } elseif (isset($_GET['h'])) {
147 $link_name = $_GET['h'];
149 if (isset($_POST['key'])) {
150 $key = $_POST['key'];
153 if (isset($_GET['d'])) {
157 if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
162 $link = jirafeau_get_link($link_name);
163 if (count($link) == 0) {
167 if (strlen($d) > 0 && $d == $link['link_code']) {
168 jirafeau_delete_link($link_name);
172 if ($link['time'] != JIRAFEAU_INFINITY
&& time() > $link['time']) {
173 jirafeau_delete_link($link_name);
177 if (strlen($link['key']) > 0 && md5($key) != $link['key']) {
182 $p = s2p($link['hash']);
183 if (!file_exists(VAR_FILES
. $p . $link['hash'])) {
189 header('Content-Length: ' . $link['file_size']);
190 header('Content-Type: ' . $link['mime_type']);
191 header('Content-Disposition: attachment; filename="' .
192 $link['file_name'] . '"');
194 $r = fopen(VAR_FILES
. $p . $link['hash'], 'r');
196 print fread($r, 1024);
201 if ($link['onetime'] == 'O') {
202 jirafeau_delete_link($link_name);
205 } elseif (isset($_GET['get_capacity'])) {
206 echo jirafeau_get_max_upload_size_bytes();
207 } elseif (isset($_GET['get_maximal_upload_size'])) {
208 echo $cfg['maximal_upload_size'];
209 } elseif (isset($_GET['get_version'])) {
210 echo JIRAFEAU_VERSION
;
211 } elseif (isset($_GET['lang'])) {
217 # This script has been auto-generated by Jirafeau but you can still edit options below.
220 proxy
='' # Or set JIRAFEAU_PROXY.
221 url
='<?php echo $cfg['web_root
']; ?>' # Or set JIRAFEAU_URL.
222 time
='<?php echo $cfg['availability_default
']; ?>' # Or set JIRAFEAU_TIME.
223 one_time
='' # Or set JIRAFEAU_ONE_TIME.
224 curl
='' # Or set JIRAFEAU_CURL_PATH.
227 if [ -n
"$JIRAFEAU_PROXY" ]; then
228 proxy
="$JIRAFEAU_PROXY"
231 if [ -n
"$JIRAFEAU_URL" ]; then
235 if [ -z
"$url" ]; then
236 echo "Please set url in script parameters or export JIRAFEAU_URL"
239 if [ -n
"$JIRAFEAU_TIME" ]; then
240 time
="$JIRAFEAU_TIME"
243 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
247 if [ -z
"$curl" ]; then
248 curl
="$JIRAFEAU_CURL_PATH"
251 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
255 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
259 if [ -z
"$curl" ]; then
260 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
265 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
266 echo "--------------------------"
271 echo " $0 send FILE [PASSWORD]"
272 echo " $0 get URL [PASSWORD]"
273 echo " $0 delete URL"
275 echo "Global variables to export:"
276 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxysever.example.com:3128«"
277 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
278 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, »month«, »quarter«, »year« or »none«"
279 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
280 echo " JIRAFEAU_CURL : alternative path to curl binary"
285 if [ -n
"$proxy" ]; then
290 if [ -n
"$one_time" ]; then
291 options
="$options -F one_time_download=1"
297 options
="$options -F key=$password"
303 if [ "$1" == "send" ]; then
304 if [ ! -f
"$2" ]; then
305 echo "File \"$2\" does not exists."
310 res
=$
($curl -X POST
--http1
.0
$proxy $options \
315 if [[ "$res" == Error
* ]]; then
316 echo "Error while uploading."
321 # Not using head or tail to minimise command dependencies
322 code
=$
(cnt
=0; echo "$res" |
while read l
; do
323 if [[ "$cnt" == "0" ]]; then
328 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
329 if [[ "$cnt" == "1" ]]; then
334 key_code
=$
(cnt
=0; echo "$res" |
while read l
; do
335 if [[ "$cnt" == "2" ]]; then
342 echo "Download page:"
343 if [[ $key_code ]]; then
344 echo " ${url}${downloadpage}?h=$code&k=$key_code"
346 echo " ${url}${downloadpage}?h=$code"
348 echo "Direct download:"
349 if [[ $key_code ]]; then
350 echo " ${url}${downloadpage}?h=$code&k=$key_code&d=1"
352 echo " ${url}${downloadpage}?h=$code&d=1"
355 echo " ${url}${downloadpage}?h=$code&d=$del_code"
357 echo "Download via API:"
358 if [[ $key_code ]]; then
359 echo " ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD}"
361 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD}"
363 echo "Delete via API:"
364 echo " ${0} delete ${url}${downloadpage}?h=$code&d=$del_code"
366 elif
[ "$1" == "get" ]; then
367 if [ -z
"$password" ]; then
368 $curl $proxy -OJ
"$2"
370 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
372 elif
[ "$1" == "delete" ]; then
382 /* Initialize an asynchronous upload. */
383 elseif (isset($_GET['init_async'])) {
384 if (isset($_POST['upload_password'])){
385 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
386 echo 'Error 20: Invalid password';
390 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
391 echo 'Error 19: No password nor allowed IP';
396 if (!isset($_POST['filename'])) {
402 if (isset($_POST['type'])) {
403 $type = $_POST['type'];
407 if (isset($_POST['key'])) {
408 $key = $_POST['key'];
412 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
416 switch ($_POST['time']) {
418 $time +
= JIRAFEAU_MINUTE
;
421 $time +
= JIRAFEAU_HOUR
;
424 $time +
= JIRAFEAU_DAY
;
427 $time +
= JIRAFEAU_WEEK
;
430 $time +
= JIRAFEAU_MONTH
;
433 $time +
= JIRAFEAU_QUARTER
;
436 $time +
= JIRAFEAU_YEAR
;
439 $time = JIRAFEAU_INFINITY
;
443 echo jirafeau_async_init($_POST['filename'],
445 isset($_POST['one_time_download']),
448 get_ip_address($cfg));
450 /* Continue an asynchronous upload. */
451 elseif (isset($_GET['push_async'])) {
452 if ((!isset($_POST['ref']))
453 ||
(!isset($_FILES['data']))
454 ||
(!isset($_POST['code']))) {
457 echo jirafeau_async_push($_POST['ref'],
460 $cfg['maximal_upload_size']);
463 /* Finalize an asynchronous upload. */
464 elseif (isset($_GET['end_async'])) {
465 if (!isset($_POST['ref'])
466 ||
!isset($_POST['code'])) {
469 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length']);