]>
git.p6c8.net - jirafeau.git/blob - script.php
3 * Jirafeau, your web file repository
4 * Copyright (C) 2012 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('Upload a file') . ':</h3>';
88 echo t('Send a POST query to') . ': <i>' . $web_root . 'script.php</i><br />';
90 echo t('Parameters') . ':<br />';
91 echo "<b>file=</b>C:\\your\\file\\path<i> (" . t('Required') . ")</i> <br />";
92 echo "<b>time=</b>[minute|hour|day|week|month|none]<i> (" . t('Optional') . ', '. t('default: none') . ")</i> <br />";
93 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
94 echo "<b>one_time_download=</b>1<i> (" . t('Optional') . ")</i> <br />";
96 echo '<p>' . t('This will return brut text content.') . ' ' .
97 t('First line is the download reference and the second line the delete code.') . '<br /></p>';
99 echo '<h3>' . t('Get a file') . ':</h3>';
101 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
103 echo t('Parameters') . ':<br />';
104 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
107 echo t('If a password has been set, send a POST request with it.');
109 echo t('Parameters') . ':<br />';
110 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
113 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU</a> ";
116 echo '<h3>' . t('Delete a file') . ':</h3>';
118 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
120 echo t('Parameters') . ':<br />';
121 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
122 echo "<b>d=</b>yout_delete_code<i> (" . t('Required') . ")</i> <br />";
124 echo '<p>' . t('This will return "Ok" if succeded, "Error" otherwhise.') . '<br /></p>';
126 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&d=0d210a952\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&d=0d210a952</a> ";
129 echo '<h3>' . t('Get a generated scripts') . ':</h3>';
131 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
133 echo t('Parameters') . ':<br />';
134 echo "<b>lang=</b>[";
135 foreach ($script_langages as $lang => $name)
137 echo "]<i> (" . t('Required') . ")</i> <br />";
139 echo '<p>' . t('This will return brut text content of the code.') . '<br /></p>';
141 echo t('Example') . ": <br />";
142 foreach ($script_langages as $lang => $name)
143 echo "$name: <a href=\"" . $web_root . "script.php?lang=$lang\">" . $web_root . "script.php?lang=$lang</a> ";
146 echo '<h3>' . t('Initalize a asynchronous transfert') . ':</h3>';
148 echo t('The goal is to permit to transfert big file, chunk by chunk.') . ' ';
149 echo t('Chunks of data must be sent in order.');
152 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?init_async</i><br />';
154 echo t('Parameters') . ':<br />';
155 echo "<b>filename=</b>file_name.ext<i> (" . t('Required') . ")</i> <br />";
156 echo "<b>type=</b>MIME_TYPE<i> (" . t('Optional') . ")</i> <br />";
157 echo "<b>time=</b>[minute|hour|day|week|month|none]<i> (" . t('Optional') . ', '. t('default: none') . ")</i> <br />";
158 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
159 echo "<b>one_time_download=</b>1<i> (" . t('Optional') . ")</i> <br />";
161 echo '<p>' . t('This will return brut text content.') . ' ' .
162 t('First line is the asynchronous transfert reference and the second line the code to use in the next operation.') . '<br /></p>';
164 echo '<h3>' . t('Push data during asynchronous transfert') . ':</h3>';
166 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?push_async</i><br />';
168 echo t('Parameters') . ':<br />';
169 echo "<b>ref=</b>async_reference<i> (" . t('Required') . ")</i> <br />";
170 echo "<b>data=</b>data_chunk<i> (" . t('Required') . ")</i> <br />";
171 echo "<b>code=</b>last_provided_code<i> (" . t('Required') . ")</i> <br />";
173 echo '<p>' . t('This will return brut text content.') . ' ' .
174 t('Returns the next code to use.') . '<br /></p>';
176 echo '<h3>' . t('Finalize asynchronous transfert') . ':</h3>';
178 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?end_async</i><br />';
180 echo t('Parameters') . ':<br />';
181 echo "<b>ref=</b>async_reference<i> (" . t('Required') . ")</i> <br />";
182 echo "<b>code=</b>last_provided_code<i> (" . t('Required') . ")</i> <br />";
184 echo '<p>' . t('This will return brut text content.') . ' ' .
185 t('First line is the download reference and the second line the delete code.') . '<br /></p>';
187 if ($cfg['enable_blocks'])
189 echo '<h3>' . t('Create a data block') . ':</h3>';
191 echo t('This interface permits to create a block of data filled with zeros.') .
192 ' ' . t('You can read selected parts, write (using a code) and delete the block.') .
193 ' ' . t('Blocks may be removed after a month of non usage.');
196 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?init_block</i><br />';
198 echo t('Parameters') . ':<br />';
199 echo "<b>size=</b>size_in_bytes<i> (" . t('Required') . ")</i> <br />";
201 echo '<p>' . t('This will return brut text content.') . ' ' .
202 t('First line is a block id the second line the edit/delete code.') . '<br /></p>';
204 echo '<h3>' . t('Get block size') . ':</h3>';
206 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?get_block_size</i><br />';
208 echo t('Parameters') . ':<br />';
209 echo "<b>id=</b>block_id<i> (" . t('Required') . ")</i> <br />";
211 echo '<p>' . t('This will return asked data or "Error" string.') . '<br /></p>';
213 echo '<h3>' . t('Read data in a block') . ':</h3>';
215 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?read_block</i><br />';
217 echo t('Parameters') . ':<br />';
218 echo "<b>id=</b>block_id<i> (" . t('Required') . ")</i> <br />";
219 echo "<b>start=</b>byte_position_starting_from_zero<i> (" . t('Required') . ")</i> <br />";
220 echo "<b>length=</b>length_to_read_in_bytes<i> (" . t('Required') . ")</i> <br />";
222 echo '<p>' . t('This will return asked data or "Error" string.') . '<br /></p>';
224 echo '<h3>' . t('Write data in a block') . ':</h3>';
226 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?write_block</i><br />';
228 echo t('Parameters') . ':<br />';
229 echo "<b>id=</b>block_id<i> (" . t('Required') . ")</i> <br />";
230 echo "<b>code=</b>block_code<i> (" . t('Required') . ")</i> <br />";
231 echo "<b>start=</b>byte_position_starting_from_zero<i> (" . t('Required') . ")</i> <br />";
232 echo "<b>data=</b>data_to_write<i> (" . t('Required') . ")</i> <br />";
234 echo '<p>' . t('This will return "Ok" or "Error" string.') . '<br /></p>';
236 echo '<h3>' . t('Delete a block') . ':</h3>';
238 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?delete_block</i><br />';
240 echo t('Parameters') . ':<br />';
241 echo "<b>id=</b>block_id<i> (" . t('Required') . ")</i> <br />";
242 echo "<b>code=</b>block_code<i> (" . t('Required') . ")</i> <br />";
244 echo '<p>' . t('This will return "Ok" or "Error" string.') . '<br /></p>';
248 require (JIRAFEAU_ROOT
. 'lib/template/footer.php');
252 /* Lets use interface now. */
253 header('Content-Type: text; charset=utf-8');
263 if (isset ($_FILES['file']) && is_writable (VAR_FILES
)
264 && is_writable (VAR_LINKS
))
267 if (isset ($_POST['key']))
268 $key = $_POST['key'];
271 if (!isset ($_POST['time']))
272 $time = JIRAFEAU_INFINITY
;
274 switch ($_POST['time'])
277 $time +
= JIRAFEAU_MINUTE
;
280 $time +
= JIRAFEAU_HOUR
;
283 $time +
= JIRAFEAU_DAY
;
286 $time +
= JIRAFEAU_WEEK
;
289 $time +
= JIRAFEAU_MONTH
;
292 $time = JIRAFEAU_INFINITY
;
295 $res = jirafeau_upload ($_FILES['file'],
296 isset ($_POST['one_time_download']),
297 $key, $time, $_SERVER['REMOTE_ADDR'],
298 $cfg['enable_crypt'], $cfg['link_name_lenght']);
300 if (empty($res) ||
$res['error']['has_error'])
305 /* Print direct link. */
307 /* Print delete link. */
309 echo $res['delete_link'];
310 /* Print decrypt key. */
312 echo urlencode($res['crypt_key']);
314 elseif (isset ($_GET['h']))
316 $link_name = $_GET['h'];
318 if (isset ($_POST['key']))
319 $key = $_POST['key'];
321 if (isset ($_GET['d']))
324 if (!preg_match ('/[0-9a-zA-Z_-]+$/', $link_name))
330 $link = jirafeau_get_link ($link_name);
331 if (count ($link) == 0)
336 if (strlen ($d) > 0 && $d == $link['link_code'])
338 jirafeau_delete_link ($link_name);
342 if ($link['time'] != JIRAFEAU_INFINITY
&& time () > $link['time'])
344 jirafeau_delete_link ($link_name);
348 if (strlen ($link['key']) > 0 && md5 ($key) != $link['key'])
353 $p = s2p ($link['md5']);
354 if (!file_exists (VAR_FILES
. $p . $link['md5']))
361 header ('Content-Length: ' . $link['file_size']);
362 header ('Content-Type: ' . $link['mime_type']);
363 header ('Content-Disposition: attachment; filename="' .
364 $link['file_name'] . '"');
366 $r = fopen (VAR_FILES
. $p . $link['md5'], 'r');
369 print fread ($r, 1024);
374 if ($link['onetime'] == 'O')
375 jirafeau_delete_link ($link_name);
378 elseif (isset ($_GET['get_capacity']))
380 echo min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
381 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
383 elseif (isset ($_GET['get_version']))
385 echo JIRAFEAU_VERSION
;
387 elseif (isset ($_GET['lang']))
395 # This script has been auto-generated by Jirafeau but you can still edit
399 proxy
='' # ex: proxy='proxysever.test.com:3128' or set JIRAFEAU_PROXY global variable
400 url
='<?php echo $cfg['web_root
'] . 'script
.php
'; ?>' # or set JIRAFEAU_URL ex: url='http://mysite/jirafeau/script.php'
401 time
='none' # minute, hour, day, week, month or none. Or set JIRAFEAU_TIME.
402 one_time
='' # ex: one_time="1" or set JIRAFEAU_ONE_TIME.
403 curl
='' # curl path to download or set JIRAFEAU_CURL_PATH.
406 if [ -n
"$JIRAFEAU_PROXY" ]; then
407 proxy
="$JIRAFEAU_PROXY"
410 if [ -n
"$JIRAFEAU_URL" ]; then
414 if [ -z
"$url" ]; then
415 echo "Please set url in script parameters or export JIRAFEAU_URL"
418 if [ -n
"$JIRAFEAU_TIME" ]; then
419 time
="$JIRAFEAU_TIME"
422 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
426 if [ -z
"$curl" ]; then
427 curl
="$JIRAFEAU_CURL_PATH"
430 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
434 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
438 if [ -z
"$curl" ]; then
439 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
445 echo " $0 send PATH [PASSWORD]"
446 echo " $0 get URL [PASSWORD]"
447 echo " $0 delete URL"
449 echo "Global variables to export:"
450 echo " JIRAFEAU_PROXY : example: proxysever.test.com:3128"
451 echo " JIRAFEAU_URL : example: http://mysite/jirafeau/script.php"
452 echo " JIRAFEAU_TIME : minute, hour, day, week, month or none"
453 echo " JIRAFEAU_ONE_TIME : set anything or set empty"
454 echo " JIRAFEAU_CURL : path to your curl binary"
459 if [ -n
"$proxy" ]; then
464 if [ -n
"$one_time" ]; then
465 options
="$options -F one_time_download=1"
471 options
="$options -F key=$password"
474 if [ "$1" == "send" ]; then
475 if [ ! -f
"$2" ]; then
476 echo "File \"$2\" does not exists."
481 res
=$
($curl -X POST
--http1
.0
$proxy $options \
486 if [[ "$res" == "Error" ]]; then
487 echo "Error while uploading."
491 # Not using head or tail to minimise command dependencies
492 code
=$
(cnt
=0; echo "$res" |
while read l
; do
493 if [[ "$cnt" == "0" ]]; then
498 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
499 if [[ "$cnt" == "1" ]]; then
504 echo "${url}?h=$code"
505 echo "${url}?h=$code&d=$del_code"
506 elif
[ "$1" == "get" ]; then
507 if [ -z
"$password" ]; then
508 $curl $proxy -OJ
"$2"
510 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
512 elif
[ "$1" == "delete" ]; then
523 /* Initialize an asynchronous upload. */
524 elseif (isset ($_GET['init_async']))
526 if (!isset ($_POST['filename']))
533 if (isset ($_POST['type']))
534 $type = $_POST['type'];
537 if (isset ($_POST['password']))
538 $key = $_POST['password'];
541 if (!isset ($_POST['time']))
542 $time = JIRAFEAU_INFINITY
;
544 switch ($_POST['time'])
547 $time +
= JIRAFEAU_MINUTE
;
550 $time +
= JIRAFEAU_HOUR
;
553 $time +
= JIRAFEAU_DAY
;
556 $time +
= JIRAFEAU_WEEK
;
559 $time +
= JIRAFEAU_MONTH
;
562 $time = JIRAFEAU_INFINITY
;
565 echo jirafeau_async_init ($_POST['filename'],
567 isset ($_POST['one_time_download']),
570 $_SERVER['REMOTE_ADDR']);
572 /* Continue an asynchronous upload. */
573 elseif (isset ($_GET['push_async']))
575 if ((!isset ($_POST['ref']))
576 ||
(!isset ($_FILES['data']))
577 ||
(!isset ($_POST['code'])))
580 echo jirafeau_async_push ($_POST['ref'], $_FILES['data'], $_POST['code']);
582 /* Finalize an asynchronous upload. */
583 elseif (isset ($_GET['end_async']))
585 if (!isset ($_POST['ref'])
586 ||
!isset ($_POST['code']))
589 echo jirafeau_async_end ($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_lenght']);
591 /* Initialize block. */
592 elseif (isset ($_GET['init_block']) && $cfg['enable_blocks'])
594 if (!isset ($_POST['size']))
597 echo jirafeau_block_init ($_POST['size']);
599 /* Get block size. */
600 elseif (isset ($_GET['get_block_size']) && $cfg['enable_blocks'])
602 if (!isset ($_POST['id']))
605 echo jirafeau_block_get_size ($_POST['id']);
607 /* Read data in block. */
608 elseif (isset ($_GET['read_block']) && $cfg['enable_blocks'])
610 if (!isset ($_POST['id'])
611 ||
!isset ($_POST['start'])
612 ||
!isset ($_POST['length']))
615 jirafeau_block_read ($_POST['id'], $_POST['start'], $_POST['length']);
617 /* Write data in block. */
618 elseif (isset ($_GET['write_block']) && $cfg['enable_blocks'])
620 if (!isset ($_POST['id'])
621 ||
!isset ($_POST['start'])
622 ||
!isset ($_FILES['data'])
623 ||
!isset ($_POST['code']))
626 echo jirafeau_block_write ($_POST['id'], $_POST['start'], $_FILES['data'], $_POST['code']);
629 elseif (isset ($_GET['delete_block']) && $cfg['enable_blocks'])
631 if (!isset ($_POST['id'])
632 ||
!isset ($_POST['code']))
635 echo jirafeau_block_delete ($_POST['id'], $_POST['code']);
patrick-canterino.de