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/pcanterino/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'])) {
84 if ($cfg['download_password_requirement'] !== 'generated' && $cfg['download_password_policy'] === 'regex'){
85 if (!preg_match($cfg['download_password_policy_regex'], $key)){
86 echo 'Error 14: The download password is not complying to the security standards.';
90 }elseif ($cfg['download_password_requirement'] !== 'optional'){
91 echo 'Error 13: The parameter password is required.';
96 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
97 echo 'Error 4: The parameter time is invalid.';
100 switch ($_POST['time']) {
102 $time +
= JIRAFEAU_MINUTE
;
105 $time +
= JIRAFEAU_HOUR
;
108 $time +
= JIRAFEAU_DAY
;
111 $time +
= JIRAFEAU_WEEK
;
114 $time +
= JIRAFEAU_FORTNIGHT
;
117 $time +
= JIRAFEAU_MONTH
;
120 $time +
= JIRAFEAU_QUARTER
;
123 $time +
= JIRAFEAU_YEAR
;
126 $time = JIRAFEAU_INFINITY
;
132 if ($cfg['maximal_upload_size'] > 0 &&
133 $_FILES['file']['size'] > $cfg['maximal_upload_size'] * 1024 * 1024) {
134 echo 'Error 5: Your file exceeds the maximum authorized file size.';
138 // Check if one time download is enabled
139 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
140 echo 'Error 26: One time download is disabled.';
144 if ($cfg['store_uploader_ip']) {
145 $ip = get_ip_address($cfg);
150 $res = jirafeau_upload(
152 isset($_POST['one_time_download']),
156 $cfg['enable_crypt'],
157 $cfg['link_name_length'],
161 if (empty($res) ||
$res['error']['has_error']) {
162 echo 'Error 6 ' . $res['error']['why'];
165 /* Print direct link. */
167 /* Print delete link. */
169 echo $res['delete_link'];
170 /* Print decrypt key. */
172 echo urlencode($res['crypt_key']);
173 } elseif (isset($_GET['h'])) {
174 $link_name = $_GET['h'];
176 if (isset($_POST['key'])) {
177 $key = $_POST['key'];
178 if ($cfg['download_password_requirement'] !== 'generated' && $cfg['download_password_policy'] === 'regex'){
179 if (!preg_match($cfg['download_password_policy_regex'], $key)){
180 echo 'Error 14: The download password is not complying to the security standards.';
184 }elseif ($cfg['download_password_requirement'] !== 'optional'){
185 echo 'Error 13: The parameter password is required.';
189 if (isset($_GET['d'])) {
193 if (!preg_match('/[0-9a-zA-Z_-]+$/', $link_name)) {
198 $link = jirafeau_get_link($link_name);
199 if (count($link) == 0) {
203 if (strlen($d) > 0 && $d == $link['link_code']) {
204 jirafeau_delete_link($link_name);
208 if ($link['time'] != JIRAFEAU_INFINITY
&& time() > $link['time']) {
209 jirafeau_delete_link($link_name);
213 if (strlen($link['key']) > 0 && md5($key) != $link['key']) {
218 $p = s2p($link['hash']);
219 if (!file_exists(VAR_FILES
. $p . $link['hash'])) {
225 header('Content-Length: ' . $link['file_size']);
226 header('Content-Type: ' . $link['mime_type']);
227 header('Content-Disposition: attachment; filename="' .
228 $link['file_name'] . '"');
230 $r = fopen(VAR_FILES
. $p . $link['hash'], 'r');
232 print fread($r, 1024);
236 if ($link['onetime'] == 'O') {
237 jirafeau_delete_link($link_name);
240 } elseif (isset($_GET['get_capacity'])) {
241 echo jirafeau_get_max_upload_size_bytes();
242 } elseif (isset($_GET['get_maximal_upload_size'])) {
243 echo $cfg['maximal_upload_size'];
244 } elseif (isset($_GET['get_version'])) {
245 echo JIRAFEAU_VERSION
;
246 } elseif (isset($_GET['lang'])) {
252 # This script has been auto-generated by Jirafeau but you can still edit options below.
255 proxy
='' # Or set JIRAFEAU_PROXY.
256 url
='<?php echo $cfg['web_root
']; ?>' # Or set JIRAFEAU_URL.
257 time
='<?php echo $cfg['availability_default
']; ?>' # Or set JIRAFEAU_TIME.
258 one_time
='' # Or set JIRAFEAU_ONE_TIME.
259 curl
='' # Or set JIRAFEAU_CURL_PATH.
260 upload_password
='' # Or set JIRAFEAU_UPLOAD_PASSWD
263 if [ -n
"$JIRAFEAU_PROXY" ]; then
264 proxy
="$JIRAFEAU_PROXY"
267 if [ -n
"$JIRAFEAU_URL" ]; then
271 if [ -z
"$url" ]; then
272 echo "Please set url in script parameters or export JIRAFEAU_URL"
275 if [ -n
"$JIRAFEAU_TIME" ]; then
276 time
="$JIRAFEAU_TIME"
279 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
283 if [ -n
"$JIRAFEAU_UPLOAD_PASSWD" ]; then
284 upload_password
="$JIRAFEAU_UPLOAD_PASSWD"
287 if [ -z
"$curl" ]; then
288 curl
="$JIRAFEAU_CURL_PATH"
291 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
295 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
299 if [ -z
"$curl" ]; then
300 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
305 echo "Jirafeau Bash Script <?php echo JIRAFEAU_VERSION; ?>"
306 echo "--------------------------"
311 echo " $0 send FILE [PASSWORD]"
312 echo " $0 get URL [PASSWORD]"
313 echo " $0 delete URL"
315 echo "Global variables to export:"
316 echo " JIRAFEAU_PROXY: Domain and port of proxy server, eg. »proxyserver.example.com:3128«"
317 echo " JIRAFEAU_URL : URI to Jirafeau installation with trailing slash, eg. »https://example.com/jirafeau/«"
318 echo " JIRAFEAU_TIME : expiration time, eg. »minute«, »hour«, »day«, »week«, fortnight, »month«, »quarter«, »year« or »none«"
319 echo " JIRAFEAU_ONE_TIME : self-destroy after first download, eg. »1« to enable or »« (empty) to disable"
320 echo " JIRAFEAU_CURL : alternative path to curl binary"
321 echo " JIRAFEAU_UPLOAD_PASSWD : upload password"
326 if [ -n
"$proxy" ]; then
331 if [ -n
"$one_time" ]; then
332 options
="$options -F one_time_download=1"
335 if [ -n
"$upload_password" ]; then
336 options
="$options -F upload_password=$upload_password"
342 options
="$options -F key=$password"
348 if [ "$1" == "send" ]; then
349 if [ ! -f
"$2" ]; then
350 echo "File \"$2\" does not exists."
355 res
=$
($curl -X POST
--http1
.0
$proxy $options \
360 if [[ "$res" == Error
* ]]; then
361 echo "Error while uploading."
366 # Not using head or tail to minimise command dependencies
367 code
=$
(cnt
=0; echo "$res" |
while read l
; do
368 if [[ "$cnt" == "0" ]]; then
373 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
374 if [[ "$cnt" == "1" ]]; then
379 key_code
=$
(cnt
=0; echo "$res" |
while read l
; do
380 if [[ "$cnt" == "2" ]]; then
387 echo "Download page:"
388 if [[ $key_code ]]; then
389 echo " ${url}${downloadpage}?h=$code&k=$key_code"
391 echo " ${url}${downloadpage}?h=$code"
393 echo "Direct download:"
394 if [[ $key_code ]]; then
395 echo " ${url}${downloadpage}?h=$code&k=$key_code&d=1"
397 echo " ${url}${downloadpage}?h=$code&d=1"
400 echo " ${url}${downloadpage}?h=$code&d=$del_code"
402 echo "Download via API:"
403 if [[ $key_code ]]; then
404 echo " ${0} get ${url}${apipage}?h=$code&k=$key_code [PASSWORD]"
406 echo " ${0} get ${url}${apipage}?h=$code [PASSWORD]"
408 echo "Delete via API:"
409 echo " ${0} delete \"${url}${downloadpage}?h=$code&d=$del_code\""
411 elif
[ "$1" == "get" ]; then
412 if [ -z
"$password" ]; then
413 $curl $proxy -OJ
"$2"
415 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
417 elif
[ "$1" == "delete" ]; then
418 $curl $proxy "$2" --data
-raw
"do_delete=1%2F" | grep
"div class" |sed
-e
"s/<[^>]\+>//g"
426 /* Initialize an asynchronous upload. */
427 elseif (isset($_GET['init_async'])) {
428 if (jirafeau_user_session_logged()) {
429 } elseif (isset($_POST['upload_password'])) {
430 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), $_POST['upload_password'])) {
431 echo 'Error 20: Invalid password';
435 if (!jirafeau_challenge_upload($cfg, get_ip_address($cfg), null)) {
436 echo 'Error 19: No password nor allowed IP';
441 if (!isset($_POST['filename'])) {
447 if (isset($_POST['type'])) {
448 $type = $_POST['type'];
452 if (isset($_POST['key'])) {
453 $key = $_POST['key'];
454 if ($cfg['download_password_requirement'] !== 'generated' && $cfg['download_password_policy'] === 'regex'){
455 if (!preg_match($cfg['download_password_policy_regex'], $key)){
456 echo 'Error 14: The download password is not complying to the security standards.';
460 }elseif ($cfg['download_password_requirement'] !== 'optional'){
461 echo 'Error 13: The parameter password is required.';
465 // Check if one time download is enabled
466 if (!$cfg['one_time_download'] && isset($_POST['one_time_download'])) {
467 echo 'Error 26: One time download is disabled.';
472 if (!isset($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']]) {
476 switch ($_POST['time']) {
478 $time +
= JIRAFEAU_MINUTE
;
481 $time +
= JIRAFEAU_HOUR
;
484 $time +
= JIRAFEAU_DAY
;
487 $time +
= JIRAFEAU_WEEK
;
490 $time +
= JIRAFEAU_FORTNIGHT
;
493 $time +
= JIRAFEAU_MONTH
;
496 $time +
= JIRAFEAU_QUARTER
;
499 $time +
= JIRAFEAU_YEAR
;
502 $time = JIRAFEAU_INFINITY
;
507 if ($cfg['store_uploader_ip']) {
508 $ip = get_ip_address($cfg);
513 echo jirafeau_async_init(
516 isset($_POST['one_time_download']),
522 /* Continue an asynchronous upload. */
523 elseif (isset($_GET['push_async'])) {
524 if ((!isset($_POST['ref']))
525 ||
(!isset($_FILES['data']))
526 ||
(!isset($_POST['code']))) {
529 echo jirafeau_async_push(
533 $cfg['maximal_upload_size']
537 /* Finalize an asynchronous upload. */
538 elseif (isset($_GET['end_async'])) {
539 if (!isset($_POST['ref'])
540 ||
!isset($_POST['code'])) {
543 echo jirafeau_async_end($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length'], $cfg['file_hash']);