]>
git.p6c8.net - jirafeau_mojo42.git/blob - script.php
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 <http://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
26 define ('JIRAFEAU_ROOT', dirname (__FILE__
) . '/');
28 require (JIRAFEAU_ROOT
. 'lib/config.original.php');
29 require (JIRAFEAU_ROOT
. 'lib/settings.php');
30 require (JIRAFEAU_ROOT
. 'lib/functions.php');
31 require (JIRAFEAU_ROOT
. 'lib/lang.php');
33 global $script_langages;
34 $script_langages = array ('bash' => 'Bash');
36 /* Operations may take a long time.
37 * Be sure PHP's safe mode is off.
43 if ($_SERVER['REQUEST_METHOD'] == "GET" && count ($_GET) == 0)
45 require (JIRAFEAU_ROOT
. 'lib/template/header.php');
50 require (JIRAFEAU_ROOT
. 'lib/template/footer.php');
53 echo '<div class="info">';
54 echo '<h2>' . t('Welcome to Jirafeau\'s query interface') . '</h2>';
56 echo t('This interface permits to script your uploads and downloads.') .
57 ' ' . t('The instructions above show how to query this interface.');
60 echo '<h3>' . t('Get Jirafeau\'s version') . ':</h3>';
62 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
64 echo t('Parameters') . ':<br />';
65 echo "<b>get_version=</b>1<i> (" . t('Required') . ")</i> <br />";
67 echo '<p>' . t('This will return brut text content.') . ' ' .
68 t('First line is the version number.') . '<br /></p>';
70 echo t('Example') . ": <a href=\"" . $web_root . "script.php?get_version=1\">" . $web_root . "script.php?get_version=1</a> ";
73 echo '<h3>' . t('Get server capacity') . ':</h3>';
75 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
77 echo t('Parameters') . ':<br />';
78 echo "<b>get_capacity=</b>1<i> (" . t('Required') . ")</i> <br />";
80 echo '<p>' . t('This will return brut text content.') . ' ' .
81 t('First line is the server capacity (in Bytes).') . '<br /></p>';
83 echo t('Example') . ": <a href=\"" . $web_root . "script.php?get_capacity=1\">" . $web_root . "script.php?get_capacity=1</a> ";
86 echo '<h3>' . t('Maximal allowed size of an uploaded file') . ':</h3>';
88 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
90 echo t('Parameters') . ':<br />';
91 echo "<b>get_maximal_upload_size=</b>1<i> (" . t('Required') . ")</i> <br />";
93 echo '<p>' . t('This will return brut text content.') . ' ' .
94 t('First line returns size (in MB).') . '<br /></p>';
96 echo t('Example') . ": <a href=\"" . $web_root . "script.php?get_maximal_upload_size=1\">" . $web_root . "script.php?get_maximal_upload_size=1</a> ";
99 echo '<h3>' . t('Upload a file') . ':</h3>';
101 echo t('Send a POST query to') . ': <i>' . $web_root . 'script.php</i><br />';
103 echo t('Parameters') . ':<br />';
104 echo "<b>file=</b>C:\\your\\file\\path<i> (" . t('Required') . ")</i> <br />";
105 echo "<b>time=</b>[minute|hour|day|week|month|year|none]<i> (" . t('Optional') . ', '. t('default: none') . ")</i> <br />";
106 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
107 echo "<b>one_time_download=</b>1<i> (" . t('Optional') . ")</i> <br />";
108 echo "<b>upload_password=</b>your_upload_password<i> (" . t('Optional') . ")</i> <br />";
110 echo '<p>' . t('This will return brut text content.') . ' ' .
111 t('First line is the download reference and the second line the delete code.') . '<br /></p>';
113 echo '<h3>' . t('Get a file') . ':</h3>';
115 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
117 echo t('Parameters') . ':<br />';
118 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
121 echo t('If a password has been set, send a POST request with it.');
123 echo t('Parameters') . ':<br />';
124 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
127 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU</a> ";
130 echo '<h3>' . t('Delete a file') . ':</h3>';
132 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
134 echo t('Parameters') . ':<br />';
135 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
136 echo "<b>d=</b>yout_delete_code<i> (" . t('Required') . ")</i> <br />";
138 echo '<p>' . t('This will return "Ok" if succeeded, "Error" otherwhise.') . '<br /></p>';
140 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&d=0d210a952\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&d=0d210a952</a> ";
143 echo '<h3>' . t('Get a generated scripts') . ':</h3>';
145 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
147 echo t('Parameters') . ':<br />';
148 echo "<b>lang=</b>[";
149 foreach ($script_langages as $lang => $name)
151 echo "]<i> (" . t('Required') . ")</i> <br />";
153 echo '<p>' . t('This will return brut text content of the code.') . '<br /></p>';
155 echo t('Example') . ": <br />";
156 foreach ($script_langages as $lang => $name)
157 echo "$name: <a href=\"" . $web_root . "script.php?lang=$lang\">" . $web_root . "script.php?lang=$lang</a> ";
160 echo '<h3>' . t('Initalize an asynchronous transfert') . ':</h3>';
162 echo t('The goal is to permit to transfert big file, chunk by chunk.') . ' ';
163 echo t('Chunks of data must be sent in order.');
166 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?init_async</i><br />';
168 echo t('Parameters') . ':<br />';
169 echo "<b>filename=</b>file_name.ext<i> (" . t('Required') . ")</i> <br />";
170 echo "<b>type=</b>MIME_TYPE<i> (" . t('Optional') . ")</i> <br />";
171 echo "<b>time=</b>[minute|hour|day|week|month|year|none]<i> (" . t('Optional') . ', '. t('default: none') . ")</i> <br />";
172 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
173 echo "<b>one_time_download=</b>1<i> (" . t('Optional') . ")</i> <br />";
174 echo "<b>upload_password=</b>your_upload_password<i> (" . t('Optional') . ")</i> <br />";
176 echo '<p>' . t('This will return brut text content.') . ' ' .
177 t('First line is the asynchronous transfert reference and the second line the code to use in the next operation.') . '<br /></p>';
179 echo '<h3>' . t('Push data during asynchronous transfert') . ':</h3>';
181 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?push_async</i><br />';
183 echo t('Parameters') . ':<br />';
184 echo "<b>ref=</b>async_reference<i> (" . t('Required') . ")</i> <br />";
185 echo "<b>data=</b>data_chunk<i> (" . t('Required') . ")</i> <br />";
186 echo "<b>code=</b>last_provided_code<i> (" . t('Required') . ")</i> <br />";
188 echo '<p>' . t('This will return brut text content.') . ' ' .
189 t('Returns the next code to use.') . '<br /></p>';
191 echo '<h3>' . t('Finalize asynchronous transfert') . ':</h3>';
193 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?end_async</i><br />';
195 echo t('Parameters') . ':<br />';
196 echo "<b>ref=</b>async_reference<i> (" . t('Required') . ")</i> <br />";
197 echo "<b>code=</b>last_provided_code<i> (" . t('Required') . ")</i> <br />";
199 echo '<p>' . t('This will return brut text content.') . ' ' .
200 t('First line is the download reference and the second line the delete code.') . '<br /></p>';
203 require (JIRAFEAU_ROOT
. 'lib/template/footer.php');
207 /* Lets use interface now. */
208 header('Content-Type: text; charset=utf-8');
218 if (isset ($_FILES['file']) && is_writable (VAR_FILES
)
219 && is_writable (VAR_LINKS
))
221 if (jirafeau_has_upload_password ($cfg) &&
222 (!isset ($_POST['upload_password']) ||
223 !jirafeau_challenge_upload_password ($cfg, $_POST['upload_password'])))
230 if (isset ($_POST['key']))
231 $key = $_POST['key'];
234 if (!isset ($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']])
240 switch ($_POST['time'])
243 $time +
= JIRAFEAU_MINUTE
;
246 $time +
= JIRAFEAU_HOUR
;
249 $time +
= JIRAFEAU_DAY
;
252 $time +
= JIRAFEAU_WEEK
;
255 $time +
= JIRAFEAU_MONTH
;
258 $time +
= JIRAFEAU_YEAR
;
261 $time = JIRAFEAU_INFINITY
;
266 if ($cfg['maximal_upload_size'] > 0 &&
267 $_FILES['file']['size'] > $cfg['maximal_upload_size'] * 1024 * 1024)
273 $res = jirafeau_upload ($_FILES['file'],
274 isset ($_POST['one_time_download']),
275 $key, $time, $_SERVER['REMOTE_ADDR'],
276 $cfg['enable_crypt'], $cfg['link_name_length']);
278 if (empty($res) ||
$res['error']['has_error'])
283 /* Print direct link. */
285 /* Print delete link. */
287 echo $res['delete_link'];
288 /* Print decrypt key. */
290 echo urlencode($res['crypt_key']);
292 elseif (isset ($_GET['h']))
294 $link_name = $_GET['h'];
296 if (isset ($_POST['key']))
297 $key = $_POST['key'];
299 if (isset ($_GET['d']))
302 if (!preg_match ('/[0-9a-zA-Z_-]+$/', $link_name))
308 $link = jirafeau_get_link ($link_name);
309 if (count ($link) == 0)
314 if (strlen ($d) > 0 && $d == $link['link_code'])
316 jirafeau_delete_link ($link_name);
320 if ($link['time'] != JIRAFEAU_INFINITY
&& time () > $link['time'])
322 jirafeau_delete_link ($link_name);
326 if (strlen ($link['key']) > 0 && md5 ($key) != $link['key'])
331 $p = s2p ($link['md5']);
332 if (!file_exists (VAR_FILES
. $p . $link['md5']))
339 header ('Content-Length: ' . $link['file_size']);
340 header ('Content-Type: ' . $link['mime_type']);
341 header ('Content-Disposition: attachment; filename="' .
342 $link['file_name'] . '"');
344 $r = fopen (VAR_FILES
. $p . $link['md5'], 'r');
347 print fread ($r, 1024);
352 if ($link['onetime'] == 'O')
353 jirafeau_delete_link ($link_name);
356 elseif (isset ($_GET['get_capacity']))
358 echo min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
359 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
361 elseif (isset ($_GET['get_maximal_upload_size']))
363 echo $cfg['maximal_upload_size'];
365 elseif (isset ($_GET['get_version']))
367 echo JIRAFEAU_VERSION
;
369 elseif (isset ($_GET['lang']))
377 # This script has been auto-generated by Jirafeau but you can still edit
381 proxy
='' # ex: proxy='proxysever.test.com:3128' or set JIRAFEAU_PROXY global variable
382 url
='<?php echo $cfg['web_root
'] . 'script
.php
'; ?>' # or set JIRAFEAU_URL ex: url='http://mysite/jirafeau/script.php'
383 time
='none' # minute, hour, day, week, month, year or none. Or set JIRAFEAU_TIME.
384 one_time
='' # ex: one_time="1" or set JIRAFEAU_ONE_TIME.
385 curl
='' # curl path to download or set JIRAFEAU_CURL_PATH.
388 if [ -n
"$JIRAFEAU_PROXY" ]; then
389 proxy
="$JIRAFEAU_PROXY"
392 if [ -n
"$JIRAFEAU_URL" ]; then
396 if [ -z
"$url" ]; then
397 echo "Please set url in script parameters or export JIRAFEAU_URL"
400 if [ -n
"$JIRAFEAU_TIME" ]; then
401 time
="$JIRAFEAU_TIME"
404 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
408 if [ -z
"$curl" ]; then
409 curl
="$JIRAFEAU_CURL_PATH"
412 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
416 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
420 if [ -z
"$curl" ]; then
421 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
427 echo " $0 send PATH [PASSWORD]"
428 echo " $0 get URL [PASSWORD]"
429 echo " $0 delete URL"
431 echo "Global variables to export:"
432 echo " JIRAFEAU_PROXY : example: proxysever.test.com:3128"
433 echo " JIRAFEAU_URL : example: http://mysite/jirafeau/script.php"
434 echo " JIRAFEAU_TIME : minute, hour, day, week, year, month or none"
435 echo " JIRAFEAU_ONE_TIME : set anything or set empty"
436 echo " JIRAFEAU_CURL : path to your curl binary"
441 if [ -n
"$proxy" ]; then
446 if [ -n
"$one_time" ]; then
447 options
="$options -F one_time_download=1"
453 options
="$options -F key=$password"
456 if [ "$1" == "send" ]; then
457 if [ ! -f
"$2" ]; then
458 echo "File \"$2\" does not exists."
463 res
=$
($curl -X POST
--http1
.0
$proxy $options \
468 if [[ "$res" == "Error" ]]; then
469 echo "Error while uploading."
473 # Not using head or tail to minimise command dependencies
474 code
=$
(cnt
=0; echo "$res" |
while read l
; do
475 if [[ "$cnt" == "0" ]]; then
480 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
481 if [[ "$cnt" == "1" ]]; then
486 echo "${url}?h=$code"
487 echo "${url}?h=$code&d=$del_code"
488 elif
[ "$1" == "get" ]; then
489 if [ -z
"$password" ]; then
490 $curl $proxy -OJ
"$2"
492 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
494 elif
[ "$1" == "delete" ]; then
505 /* Initialize an asynchronous upload. */
506 elseif (isset ($_GET['init_async']))
508 if (jirafeau_has_upload_password ($cfg) &&
509 (!isset ($_POST['upload_password']) ||
510 !jirafeau_challenge_upload_password ($cfg, $_POST['upload_password'])))
516 if (!isset ($_POST['filename']))
523 if (isset ($_POST['type']))
524 $type = $_POST['type'];
527 if (isset ($_POST['key']))
528 $key = $_POST['key'];
531 if (!isset ($_POST['time']) ||
!$cfg['availabilities'][$_POST['time']])
537 switch ($_POST['time'])
540 $time +
= JIRAFEAU_MINUTE
;
543 $time +
= JIRAFEAU_HOUR
;
546 $time +
= JIRAFEAU_DAY
;
549 $time +
= JIRAFEAU_WEEK
;
552 $time +
= JIRAFEAU_MONTH
;
555 $time +
= JIRAFEAU_YEAR
;
558 $time = JIRAFEAU_INFINITY
;
561 echo jirafeau_async_init ($_POST['filename'],
563 isset ($_POST['one_time_download']),
566 $_SERVER['REMOTE_ADDR']);
568 /* Continue an asynchronous upload. */
569 elseif (isset ($_GET['push_async']))
571 if ((!isset ($_POST['ref']))
572 ||
(!isset ($_FILES['data']))
573 ||
(!isset ($_POST['code'])))
577 echo jirafeau_async_push ($_POST['ref'],
580 $cfg['maximal_upload_size']);
583 /* Finalize an asynchronous upload. */
584 elseif (isset ($_GET['end_async']))
586 if (!isset ($_POST['ref'])
587 ||
!isset ($_POST['code']))
590 echo jirafeau_async_end ($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length']);
patrick-canterino.de