]>
git.p6c8.net - jirafeau_project.git/blob - script.php
17173e949928af2e4cff84deec2c02adc19bdd89
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 />";
95 echo "<b>upload_password=</b>your_upload_password<i> (" . t('Optional') . ")</i> <br />";
97 echo '<p>' . t('This will return brut text content.') . ' ' .
98 t('First line is the download reference and the second line the delete code.') . '<br /></p>';
100 echo '<h3>' . t('Get a file') . ':</h3>';
102 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
104 echo t('Parameters') . ':<br />';
105 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
108 echo t('If a password has been set, send a POST request with it.');
110 echo t('Parameters') . ':<br />';
111 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
114 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU</a> ";
117 echo '<h3>' . t('Delete a file') . ':</h3>';
119 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
121 echo t('Parameters') . ':<br />';
122 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
123 echo "<b>d=</b>yout_delete_code<i> (" . t('Required') . ")</i> <br />";
125 echo '<p>' . t('This will return "Ok" if succeded, "Error" otherwhise.') . '<br /></p>';
127 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&d=0d210a952\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&d=0d210a952</a> ";
130 echo '<h3>' . t('Get a generated scripts') . ':</h3>';
132 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
134 echo t('Parameters') . ':<br />';
135 echo "<b>lang=</b>[";
136 foreach ($script_langages as $lang => $name)
138 echo "]<i> (" . t('Required') . ")</i> <br />";
140 echo '<p>' . t('This will return brut text content of the code.') . '<br /></p>';
142 echo t('Example') . ": <br />";
143 foreach ($script_langages as $lang => $name)
144 echo "$name: <a href=\"" . $web_root . "script.php?lang=$lang\">" . $web_root . "script.php?lang=$lang</a> ";
147 echo '<h3>' . t('Initalize an asynchronous transfert') . ':</h3>';
149 echo t('The goal is to permit to transfert big file, chunk by chunk.') . ' ';
150 echo t('Chunks of data must be sent in order.');
153 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?init_async</i><br />';
155 echo t('Parameters') . ':<br />';
156 echo "<b>filename=</b>file_name.ext<i> (" . t('Required') . ")</i> <br />";
157 echo "<b>type=</b>MIME_TYPE<i> (" . t('Optional') . ")</i> <br />";
158 echo "<b>time=</b>[minute|hour|day|week|month|none]<i> (" . t('Optional') . ', '. t('default: none') . ")</i> <br />";
159 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
160 echo "<b>one_time_download=</b>1<i> (" . t('Optional') . ")</i> <br />";
161 echo "<b>upload_password=</b>your_upload_password<i> (" . t('Optional') . ")</i> <br />";
163 echo '<p>' . t('This will return brut text content.') . ' ' .
164 t('First line is the asynchronous transfert reference and the second line the code to use in the next operation.') . '<br /></p>';
166 echo '<h3>' . t('Push data during asynchronous transfert') . ':</h3>';
168 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?push_async</i><br />';
170 echo t('Parameters') . ':<br />';
171 echo "<b>ref=</b>async_reference<i> (" . t('Required') . ")</i> <br />";
172 echo "<b>data=</b>data_chunk<i> (" . t('Required') . ")</i> <br />";
173 echo "<b>code=</b>last_provided_code<i> (" . t('Required') . ")</i> <br />";
175 echo '<p>' . t('This will return brut text content.') . ' ' .
176 t('Returns the next code to use.') . '<br /></p>';
178 echo '<h3>' . t('Finalize asynchronous transfert') . ':</h3>';
180 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?end_async</i><br />';
182 echo t('Parameters') . ':<br />';
183 echo "<b>ref=</b>async_reference<i> (" . t('Required') . ")</i> <br />";
184 echo "<b>code=</b>last_provided_code<i> (" . t('Required') . ")</i> <br />";
186 echo '<p>' . t('This will return brut text content.') . ' ' .
187 t('First line is the download reference and the second line the delete code.') . '<br /></p>';
190 require (JIRAFEAU_ROOT
. 'lib/template/footer.php');
194 /* Lets use interface now. */
195 header('Content-Type: text; charset=utf-8');
205 if (isset ($_FILES['file']) && is_writable (VAR_FILES
)
206 && is_writable (VAR_LINKS
))
208 if (jirafeau_has_upload_password ($cfg) &&
209 (!isset ($_POST['upload_password']) ||
210 !jirafeau_challenge_upload_password ($cfg, $_POST['upload_password'])))
217 if (isset ($_POST['key']))
218 $key = $_POST['key'];
221 if (!isset ($_POST['time']))
222 $time = JIRAFEAU_INFINITY
;
224 switch ($_POST['time'])
227 $time +
= JIRAFEAU_MINUTE
;
230 $time +
= JIRAFEAU_HOUR
;
233 $time +
= JIRAFEAU_DAY
;
236 $time +
= JIRAFEAU_WEEK
;
239 $time +
= JIRAFEAU_MONTH
;
242 $time = JIRAFEAU_INFINITY
;
245 $res = jirafeau_upload ($_FILES['file'],
246 isset ($_POST['one_time_download']),
247 $key, $time, $_SERVER['REMOTE_ADDR'],
248 $cfg['enable_crypt'], $cfg['link_name_lenght']);
250 if (empty($res) ||
$res['error']['has_error'])
255 /* Print direct link. */
257 /* Print delete link. */
259 echo $res['delete_link'];
260 /* Print decrypt key. */
262 echo urlencode($res['crypt_key']);
264 elseif (isset ($_GET['h']))
266 $link_name = $_GET['h'];
268 if (isset ($_POST['key']))
269 $key = $_POST['key'];
271 if (isset ($_GET['d']))
274 if (!preg_match ('/[0-9a-zA-Z_-]+$/', $link_name))
280 $link = jirafeau_get_link ($link_name);
281 if (count ($link) == 0)
286 if (strlen ($d) > 0 && $d == $link['link_code'])
288 jirafeau_delete_link ($link_name);
292 if ($link['time'] != JIRAFEAU_INFINITY
&& time () > $link['time'])
294 jirafeau_delete_link ($link_name);
298 if (strlen ($link['key']) > 0 && md5 ($key) != $link['key'])
303 $p = s2p ($link['md5']);
304 if (!file_exists (VAR_FILES
. $p . $link['md5']))
311 header ('Content-Length: ' . $link['file_size']);
312 header ('Content-Type: ' . $link['mime_type']);
313 header ('Content-Disposition: attachment; filename="' .
314 $link['file_name'] . '"');
316 $r = fopen (VAR_FILES
. $p . $link['md5'], 'r');
319 print fread ($r, 1024);
324 if ($link['onetime'] == 'O')
325 jirafeau_delete_link ($link_name);
328 elseif (isset ($_GET['get_capacity']))
330 echo min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
331 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
333 elseif (isset ($_GET['get_version']))
335 echo JIRAFEAU_VERSION
;
337 elseif (isset ($_GET['lang']))
345 # This script has been auto-generated by Jirafeau but you can still edit
349 proxy
='' # ex: proxy='proxysever.test.com:3128' or set JIRAFEAU_PROXY global variable
350 url
='<?php echo $cfg['web_root
'] . 'script
.php
'; ?>' # or set JIRAFEAU_URL ex: url='http://mysite/jirafeau/script.php'
351 time
='none' # minute, hour, day, week, month or none. Or set JIRAFEAU_TIME.
352 one_time
='' # ex: one_time="1" or set JIRAFEAU_ONE_TIME.
353 curl
='' # curl path to download or set JIRAFEAU_CURL_PATH.
356 if [ -n
"$JIRAFEAU_PROXY" ]; then
357 proxy
="$JIRAFEAU_PROXY"
360 if [ -n
"$JIRAFEAU_URL" ]; then
364 if [ -z
"$url" ]; then
365 echo "Please set url in script parameters or export JIRAFEAU_URL"
368 if [ -n
"$JIRAFEAU_TIME" ]; then
369 time
="$JIRAFEAU_TIME"
372 if [ -n
"$JIRAFEAU_ONE_TIME" ]; then
376 if [ -z
"$curl" ]; then
377 curl
="$JIRAFEAU_CURL_PATH"
380 if [ -z
"$curl" ] && [ -e
"/usr/bin/curl" ]; then
384 if [ -z
"$curl" ] && [ -e
"/bin/curl.exe" ]; then
388 if [ -z
"$curl" ]; then
389 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
395 echo " $0 send PATH [PASSWORD]"
396 echo " $0 get URL [PASSWORD]"
397 echo " $0 delete URL"
399 echo "Global variables to export:"
400 echo " JIRAFEAU_PROXY : example: proxysever.test.com:3128"
401 echo " JIRAFEAU_URL : example: http://mysite/jirafeau/script.php"
402 echo " JIRAFEAU_TIME : minute, hour, day, week, month or none"
403 echo " JIRAFEAU_ONE_TIME : set anything or set empty"
404 echo " JIRAFEAU_CURL : path to your curl binary"
409 if [ -n
"$proxy" ]; then
414 if [ -n
"$one_time" ]; then
415 options
="$options -F one_time_download=1"
421 options
="$options -F key=$password"
424 if [ "$1" == "send" ]; then
425 if [ ! -f
"$2" ]; then
426 echo "File \"$2\" does not exists."
431 res
=$
($curl -X POST
--http1
.0
$proxy $options \
436 if [[ "$res" == "Error" ]]; then
437 echo "Error while uploading."
441 # Not using head or tail to minimise command dependencies
442 code
=$
(cnt
=0; echo "$res" |
while read l
; do
443 if [[ "$cnt" == "0" ]]; then
448 del_code
=$
(cnt
=0; echo "$res" |
while read l
; do
449 if [[ "$cnt" == "1" ]]; then
454 echo "${url}?h=$code"
455 echo "${url}?h=$code&d=$del_code"
456 elif
[ "$1" == "get" ]; then
457 if [ -z
"$password" ]; then
458 $curl $proxy -OJ
"$2"
460 $curl $proxy -OJ
-X POST
-F key
=$password "$2"
462 elif
[ "$1" == "delete" ]; then
473 /* Initialize an asynchronous upload. */
474 elseif (isset ($_GET['init_async']))
476 if (jirafeau_has_upload_password ($cfg) &&
477 (!isset ($_POST['upload_password']) ||
478 !jirafeau_challenge_upload_password ($cfg, $_POST['upload_password'])))
484 if (!isset ($_POST['filename']))
491 if (isset ($_POST['type']))
492 $type = $_POST['type'];
495 if (isset ($_POST['key']))
496 $key = $_POST['key'];
499 if (!isset ($_POST['time']))
500 $time = JIRAFEAU_INFINITY
;
502 switch ($_POST['time'])
505 $time +
= JIRAFEAU_MINUTE
;
508 $time +
= JIRAFEAU_HOUR
;
511 $time +
= JIRAFEAU_DAY
;
514 $time +
= JIRAFEAU_WEEK
;
517 $time +
= JIRAFEAU_MONTH
;
520 $time = JIRAFEAU_INFINITY
;
523 echo jirafeau_async_init ($_POST['filename'],
525 isset ($_POST['one_time_download']),
528 $_SERVER['REMOTE_ADDR']);
530 /* Continue an asynchronous upload. */
531 elseif (isset ($_GET['push_async']))
533 if ((!isset ($_POST['ref']))
534 ||
(!isset ($_FILES['data']))
535 ||
(!isset ($_POST['code'])))
538 echo jirafeau_async_push ($_POST['ref'], $_FILES['data'], $_POST['code']);
540 /* Finalize an asynchronous upload. */
541 elseif (isset ($_GET['end_async']))
543 if (!isset ($_POST['ref'])
544 ||
!isset ($_POST['code']))
547 echo jirafeau_async_end ($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_lenght']);
patrick-canterino.de