]> git.p6c8.net - jirafeau.git/blob - script.php
New data block feature
[jirafeau.git] / script.php
1 <?php
2 /*
3 * Jirafeau, your web file repository
4 * Copyright (C) 2012 Jerome Jutteau <j.jutteau@gmail.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 /*
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
23 * web directory.
24 */
25
26 define ('JIRAFEAU_ROOT', dirname (__FILE__) . '/');
27
28 require (JIRAFEAU_ROOT . 'lib/config.php');
29 require (JIRAFEAU_ROOT . 'lib/settings.php');
30 require (JIRAFEAU_ROOT . 'lib/functions.php');
31 require (JIRAFEAU_ROOT . 'lib/lang.php');
32
33 global $script_langages;
34 $script_langages = array ('bash' => 'Bash');
35
36 /* Operations may take a long time.
37 * Be sure PHP's safe mode is off.
38 */
39 set_time_limit(0);
40
41 if ($_SERVER['REQUEST_METHOD'] == "GET" && count ($_GET) == 0)
42 {
43 require (JIRAFEAU_ROOT . 'lib/template/header.php');
44 check_errors ();
45 if (has_error ())
46 {
47 show_errors ();
48 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
49 exit;
50 }
51 echo '<div class="info">';
52 echo '<h2>' . t('Welcome to Jirafeau\'s query interface') . '</h2>';
53 echo '<p>';
54 echo t('This interface permits to script your uploads and downloads.') .
55 ' ' . t('The instructions above show how to query this interface.');
56 echo '</p>';
57
58 echo '<h3>' . t('Get Jirafeau\'s version') . ':</h3>';
59 echo '<p>';
60 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
61 echo '<br />';
62 echo t('Parameters') . ':<br />';
63 echo "<b>get_version=</b>1<i> (" . t('Required') . ")</i> <br />";
64 echo '</p>';
65 echo '<p>' . t('This will return brut text content.') . ' ' .
66 t('First line is the version number.') . '<br /></p>';
67 echo '<p>';
68 echo t('Example') . ": <a href=\"" . $web_root . "script.php?get_version=1\">" . $web_root . "script.php?get_version=1</a> ";
69 echo '</p>';
70
71 echo '<h3>' . t('Get server capacity') . ':</h3>';
72 echo '<p>';
73 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
74 echo '<br />';
75 echo t('Parameters') . ':<br />';
76 echo "<b>get_capacity=</b>1<i> (" . t('Required') . ")</i> <br />";
77 echo '</p>';
78 echo '<p>' . t('This will return brut text content.') . ' ' .
79 t('First line is the server capacity (in Bytes).') . '<br /></p>';
80 echo '<p>';
81 echo t('Example') . ": <a href=\"" . $web_root . "script.php?get_capacity=1\">" . $web_root . "script.php?get_capacity=1</a> ";
82 echo '</p>';
83
84 echo '<h3>' . t('Upload a file') . ':</h3>';
85 echo '<p>';
86 echo t('Send a POST query to') . ': <i>' . $web_root . 'script.php</i><br />';
87 echo '<br />';
88 echo t('Parameters') . ':<br />';
89 echo "<b>file=</b>C:\\your\\file\\path<i> (" . t('Required') . ")</i> <br />";
90 echo "<b>time=</b>[minute|hour|day|week|month|none]<i> (" . t('Optional') . ', '. t('default: none') . ")</i> <br />";
91 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
92 echo "<b>one_time_download=</b>1<i> (" . t('Optional') . ")</i> <br />";
93 echo '</p>';
94 echo '<p>' . t('This will return brut text content.') . ' ' .
95 t('First line is the download reference and the second line the delete code.') . '<br /></p>';
96
97 echo '<h3>' . t('Get a file') . ':</h3>';
98 echo '<p>';
99 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
100 echo '<br />';
101 echo t('Parameters') . ':<br />';
102 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
103 echo '</p>';
104 echo '<p>';
105 echo t('If a password has been set, send a POST request with it.');
106 echo '<br />';
107 echo t('Parameters') . ':<br />';
108 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
109 echo '</p>';
110 echo '<p>';
111 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU</a> ";
112 echo '</p>';
113
114 echo '<h3>' . t('Delete a file') . ':</h3>';
115 echo '<p>';
116 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
117 echo '<br />';
118 echo t('Parameters') . ':<br />';
119 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
120 echo "<b>d=</b>yout_delete_code<i> (" . t('Required') . ")</i> <br />";
121 echo '</p>';
122 echo '<p>' . t('This will return "Ok" if succeded, "Error" otherwhise.') . '<br /></p>';
123 echo '<p>';
124 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&amp;d=0d210a952\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&amp;d=0d210a952</a> ";
125 echo '</p>';
126
127 echo '<h3>' . t('Get a generated scripts') . ':</h3>';
128 echo '<p>';
129 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
130 echo '<br />';
131 echo t('Parameters') . ':<br />';
132 echo "<b>lang=</b>[";
133 foreach ($script_langages as $lang => $name)
134 echo $lang;
135 echo "]<i> (" . t('Required') . ")</i> <br />";
136 echo '</p>';
137 echo '<p>' . t('This will return brut text content of the code.') . '<br /></p>';
138 echo '<p>';
139 echo t('Example') . ": <br />";
140 foreach ($script_langages as $lang => $name)
141 echo "$name: <a href=\"" . $web_root . "script.php?lang=$lang\">" . $web_root . "script.php?lang=$lang</a> ";
142 echo '</p>';
143
144 echo '<h3>' . t('Initalize a asynchronous transfert') . ':</h3>';
145 echo '<p>';
146 echo t('The goal is to permit to transfert big file, chunk by chunk.') . ' ';
147 echo t('Chunks of data must be sent in order.');
148 echo '</p>';
149 echo '<p>';
150 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?init_async</i><br />';
151 echo '<br />';
152 echo t('Parameters') . ':<br />';
153 echo "<b>filename=</b>file_name.ext<i> (" . t('Required') . ")</i> <br />";
154 echo "<b>type=</b>MIME_TYPE<i> (" . t('Optional') . ")</i> <br />";
155 echo "<b>time=</b>[minute|hour|day|week|month|none]<i> (" . t('Optional') . ', '. t('default: none') . ")</i> <br />";
156 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
157 echo "<b>one_time_download=</b>1<i> (" . t('Optional') . ")</i> <br />";
158 echo '</p>';
159 echo '<p>' . t('This will return brut text content.') . ' ' .
160 t('First line is the asynchronous transfert reference and the second line the code to use in the next operation.') . '<br /></p>';
161
162 echo '<h3>' . t('Push data during asynchronous transfert') . ':</h3>';
163 echo '<p>';
164 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?push_async</i><br />';
165 echo '<br />';
166 echo t('Parameters') . ':<br />';
167 echo "<b>ref=</b>async_reference<i> (" . t('Required') . ")</i> <br />";
168 echo "<b>data=</b>data_chunk<i> (" . t('Required') . ")</i> <br />";
169 echo "<b>code=</b>last_provided_code<i> (" . t('Required') . ")</i> <br />";
170 echo '</p>';
171 echo '<p>' . t('This will return brut text content.') . ' ' .
172 t('Returns the next code to use.') . '<br /></p>';
173
174 echo '<h3>' . t('Finalize asynchronous transfert') . ':</h3>';
175 echo '<p>';
176 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?end_async</i><br />';
177 echo '<br />';
178 echo t('Parameters') . ':<br />';
179 echo "<b>ref=</b>async_reference<i> (" . t('Required') . ")</i> <br />";
180 echo "<b>code=</b>last_provided_code<i> (" . t('Required') . ")</i> <br />";
181 echo '</p>';
182 echo '<p>' . t('This will return brut text content.') . ' ' .
183 t('First line is the download reference and the second line the delete code.') . '<br /></p>';
184
185 if ($cfg['enable_blocks'])
186 {
187 echo '<h3>' . t('Create a data block') . ':</h3>';
188 echo '<p>';
189 echo t('This interface permits to create a block of data filled with zeros.') .
190 ' ' . t('You can read selected parts, write (using a code) and delete the block.') .
191 ' ' . t('Blocks may be removed after a month of non usage.');
192 echo '</p>';
193 echo '<p>';
194 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?init_block</i><br />';
195 echo '<br />';
196 echo t('Parameters') . ':<br />';
197 echo "<b>size=</b>size_in_bytes<i> (" . t('Required') . ")</i> <br />";
198 echo '</p>';
199 echo '<p>' . t('This will return brut text content.') . ' ' .
200 t('First line is a block id the second line the edit/delete code.') . '<br /></p>';
201
202 echo '<h3>' . t('Read data in a block') . ':</h3>';
203 echo '<p>';
204 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?read_block</i><br />';
205 echo '<br />';
206 echo t('Parameters') . ':<br />';
207 echo "<b>id=</b>block_id<i> (" . t('Required') . ")</i> <br />";
208 echo "<b>start=</b>byte_position_starting_from_zero<i> (" . t('Required') . ")</i> <br />";
209 echo "<b>length=</b>length_to_read_in_bytes<i> (" . t('Required') . ")</i> <br />";
210 echo '</p>';
211 echo '<p>' . t('This will return asked data or "Error" string.') . '<br /></p>';
212
213 echo '<h3>' . t('Write data in a block') . ':</h3>';
214 echo '<p>';
215 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?write_block</i><br />';
216 echo '<br />';
217 echo t('Parameters') . ':<br />';
218 echo "<b>id=</b>block_id<i> (" . t('Required') . ")</i> <br />";
219 echo "<b>code=</b>block_code<i> (" . t('Required') . ")</i> <br />";
220 echo "<b>start=</b>byte_position_starting_from_zero<i> (" . t('Required') . ")</i> <br />";
221 echo "<b>data=</b>data_to_write<i> (" . t('Required') . ")</i> <br />";
222 echo '</p>';
223 echo '<p>' . t('This will return "Ok" or "Error" string.') . '<br /></p>';
224
225 echo '<h3>' . t('Delete a block') . ':</h3>';
226 echo '<p>';
227 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?delete_block</i><br />';
228 echo '<br />';
229 echo t('Parameters') . ':<br />';
230 echo "<b>id=</b>block_id<i> (" . t('Required') . ")</i> <br />";
231 echo "<b>code=</b>block_code<i> (" . t('Required') . ")</i> <br />";
232 echo '</p>';
233 echo '<p>' . t('This will return "Ok" or "Error" string.') . '<br /></p>';
234 }
235
236 echo '</div><br />';
237 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
238 exit;
239 }
240
241 /* Lets use interface now. */
242 header('Content-Type: text; charset=utf-8');
243
244 /* Upload file */
245 if (isset ($_FILES['file']) && is_writable (VAR_FILES)
246 && is_writable (VAR_LINKS))
247 {
248 $key = '';
249 if (isset ($_POST['key']))
250 $key = $_POST['key'];
251
252 $time = time ();
253 if (!isset ($_POST['time']))
254 $time = JIRAFEAU_INFINITY;
255 else
256 switch ($_POST['time'])
257 {
258 case 'minute':
259 $time += JIRAFEAU_MINUTE;
260 break;
261 case 'hour':
262 $time += JIRAFEAU_HOUR;
263 break;
264 case 'day':
265 $time += JIRAFEAU_DAY;
266 break;
267 case 'week':
268 $time += JIRAFEAU_WEEK;
269 break;
270 case 'month':
271 $time += JIRAFEAU_MONTH;
272 break;
273 default:
274 $time = JIRAFEAU_INFINITY;
275 break;
276 }
277 $res = jirafeau_upload ($_FILES['file'],
278 isset ($_POST['one_time_download']),
279 $key, $time, $_SERVER['REMOTE_ADDR']);
280
281 if (empty($res) || $res['error']['has_error'])
282 {
283 echo "Error";
284 exit;
285 }
286 /* Print direct link. */
287 echo $res['link'];
288 echo NL;
289 /* Print delete link. */
290 echo $res['delete_link'];
291 }
292 elseif (isset ($_GET['h']))
293 {
294 $link_name = $_GET['h'];
295 $key = '';
296 if (isset ($_POST['key']))
297 $key = $_POST['key'];
298 $d = '';
299 if (isset ($_GET['d']))
300 $d = $_GET['d'];
301
302 if (!preg_match ('/[0-9a-zA-Z_-]{22}$/', $link_name))
303 {
304 echo "Error";
305 exit;
306 }
307
308 $link = jirafeau_get_link ($link_name);
309 if (count ($link) == 0)
310 {
311 echo "Error";
312 exit;
313 }
314 if (strlen ($d) > 0 && $d == $link['link_code'])
315 {
316 jirafeau_delete_link ($link_name);
317 echo "Ok";
318 exit;
319 }
320 if ($link['time'] != JIRAFEAU_INFINITY && time () > $link['time'])
321 {
322 jirafeau_delete_link ($link_name);
323 echo "Error";
324 exit;
325 }
326 if (strlen ($link['key']) > 0 && md5 ($key) != $link['key'])
327 {
328 echo "Error";
329 exit;
330 }
331 $p = s2p ($link['md5']);
332 if (!file_exists (VAR_FILES . $p . $link['md5']))
333 {
334 echo "Error";
335 exit;
336 }
337
338 /* Read file. */
339 header ('Content-Length: ' . $link['file_size']);
340 header ('Content-Type: ' . $link['mime_type']);
341 header ('Content-Disposition: attachment; filename="' .
342 $link['file_name'] . '"');
343
344 $r = fopen (VAR_FILES . $p . $link['md5'], 'r');
345 while (!feof ($r))
346 {
347 print fread ($r, 1024);
348 ob_flush();
349 }
350 fclose ($r);
351
352 if ($link['onetime'] == 'O')
353 jirafeau_delete_link ($link_name);
354 exit;
355 }
356 elseif (isset ($_GET['get_capacity']))
357 {
358 echo min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
359 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
360 }
361 elseif (isset ($_GET['get_version']))
362 {
363 echo JIRAFEAU_VERSION;
364 }
365 elseif (isset ($_GET['lang']))
366 {
367 $l=$_GET['lang'];
368 if ($l == "bash")
369 {
370 ?>
371 #!/bin/bash
372
373 # This script has been auto-generated by Jirafeau but you can still edit
374 # options below.
375
376 # Config
377 proxy='' # ex: proxy='proxysever.test.com:3128' or set JIRAFEAU_PROXY global variable
378 url='<?php echo $cfg['web_root'] . 'script.php'; ?>' # or set JIRAFEAU_URL ex: url='http://mysite/jirafeau/script.php'
379 time='none' # minute, hour, day, week, month or none. Or set JIRAFEAU_TIME.
380 one_time='' # ex: one_time="1" or set JIRAFEAU_ONE_TIME.
381 curl='' # curl path to download or set JIRAFEAU_CURL_PATH.
382 # End of config
383
384 if [ -n "$JIRAFEAU_PROXY" ]; then
385 proxy="$JIRAFEAU_PROXY"
386 fi
387
388 if [ -n "$JIRAFEAU_URL" ]; then
389 url="$JIRAFEAU_URL"
390 fi
391
392 if [ -z "$url" ]; then
393 echo "Please set url in script parameters or export JIRAFEAU_URL"
394 fi
395
396 if [ -n "$JIRAFEAU_TIME" ]; then
397 time="$JIRAFEAU_TIME"
398 fi
399
400 if [ -n "$JIRAFEAU_ONE_TIME" ]; then
401 one_time='1'
402 fi
403
404 if [ -z "$curl" ]; then
405 curl="$JIRAFEAU_CURL_PATH"
406 fi
407
408 if [ -z "$curl" ] && [ -e "/usr/bin/curl" ]; then
409 curl="/usr/bin/curl"
410 fi
411
412 if [ -z "$curl" ] && [ -e "/bin/curl.exe" ]; then
413 curl="/bin/curl.exe"
414 fi
415
416 if [ -z "$curl" ]; then
417 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
418 exit
419 fi
420
421 if [ -z "$2" ]; then
422 echo "man:"
423 echo " $0 send PATH [PASSWORD]"
424 echo " $0 get URL [PASSWORD]"
425 echo " $0 delete URL"
426 echo ""
427 echo "Global variables to export:"
428 echo " JIRAFEAU_PROXY : example: proxysever.test.com:3128"
429 echo " JIRAFEAU_URL : example: http://mysite/jirafeau/script.php"
430 echo " JIRAFEAU_TIME : minute, hour, day, week, month or none"
431 echo " JIRAFEAU_ONE_TIME : set anything or set empty"
432 echo " JIRAFEAU_CURL : path to your curl binary"
433
434 exit 0
435 fi
436
437 if [ -n "$proxy" ]; then
438 proxy="-x $proxy"
439 fi
440
441 options=''
442 if [ -n "$one_time" ]; then
443 options="$options -F one_time_download=1"
444 fi
445
446 password=''
447 if [ -n "$3" ]; then
448 password="$3"
449 options="$options -F key=$password"
450 fi
451
452 if [ "$1" == "send" ]; then
453 if [ ! -f "$2" ]; then
454 echo "File \"$2\" does not exists."
455 exit
456 fi
457
458 # Ret result
459 res=$($curl -X POST --http1.0 $proxy $options \
460 -F "time=$time" \
461 -F "file=@$2" \
462 $url)
463
464 if [[ "$res" == "Error" ]]; then
465 echo "Error while uploading."
466 exit
467 fi
468
469 # Not using head or tail to minimise command dependencies
470 code=$(cnt=0; echo "$res" | while read l; do
471 if [[ "$cnt" == "0" ]]; then
472 echo "$l"
473 fi
474 cnt=$(( cnt + 1 ))
475 done)
476 del_code=$(cnt=0; echo "$res" | while read l; do
477 if [[ "$cnt" == "1" ]]; then
478 echo "$l"
479 fi
480 cnt=$(( cnt + 1 ))
481 done)
482 echo "${url}?h=$code"
483 echo "${url}?h=$code&d=$del_code"
484 elif [ "$1" == "get" ]; then
485 if [ -z "$password" ]; then
486 $curl $proxy -OJ "$2"
487 else
488 $curl $proxy -OJ -X POST -F key=$password "$2"
489 fi
490 elif [ "$1" == "delete" ]; then
491 $curl $proxy "$2"
492 fi
493 <?php
494 }
495 else
496 {
497 echo "Error";
498 exit;
499 }
500 }
501 /* Initialize an asynchronous upload. */
502 elseif (isset ($_GET['init_async']))
503 {
504 if (!isset ($_POST['filename']))
505 {
506 echo "Error";
507 exit;
508 }
509
510 $type = '';
511 if (isset ($_POST['type']))
512 $type = $_POST['type'];
513
514 $key = '';
515 if (isset ($_POST['password']))
516 $key = $_POST['password'];
517
518 $time = time ();
519 if (!isset ($_POST['time']))
520 $time = JIRAFEAU_INFINITY;
521 else
522 switch ($_POST['time'])
523 {
524 case 'minute':
525 $time += JIRAFEAU_MINUTE;
526 break;
527 case 'hour':
528 $time += JIRAFEAU_HOUR;
529 break;
530 case 'day':
531 $time += JIRAFEAU_DAY;
532 break;
533 case 'week':
534 $time += JIRAFEAU_WEEK;
535 break;
536 case 'month':
537 $time += JIRAFEAU_MONTH;
538 break;
539 default:
540 $time = JIRAFEAU_INFINITY;
541 break;
542 }
543 echo jirafeau_async_init ($_POST['filename'],
544 $type,
545 isset ($_POST['one_time_download']),
546 $key,
547 $time,
548 $_SERVER['REMOTE_ADDR']);
549 }
550 /* Continue an asynchronous upload. */
551 elseif (isset ($_GET['push_async']))
552 {
553 if ((!isset ($_POST['ref']))
554 || (!isset ($_FILES['data']))
555 || (!isset ($_POST['code'])))
556 echo "Error";
557 else
558 echo jirafeau_async_push ($_POST['ref'], $_FILES['data'], $_POST['code']);
559 }
560 /* Finalize an asynchronous upload. */
561 elseif (isset ($_GET['end_async']))
562 {
563 if (!isset ($_POST['ref'])
564 || !isset ($_POST['code']))
565 echo "Error";
566 else
567 echo jirafeau_async_end ($_POST['ref'], $_POST['code']);
568 }
569 /* Initialize block. */
570 elseif (isset ($_GET['init_block']) && $cfg['enable_blocks'])
571 {
572 if (!isset ($_POST['size']))
573 echo "Error";
574 else
575 echo jirafeau_block_init ($_POST['size']);
576 }
577 /* Read data in block. */
578 elseif (isset ($_GET['read_block']) && $cfg['enable_blocks'])
579 {
580 if (!isset ($_POST['id'])
581 || !isset ($_POST['start'])
582 || !isset ($_POST['length']))
583 echo "Error";
584 else
585 jirafeau_block_read ($_POST['id'], $_POST['start'], $_POST['length']);
586 }
587 /* Write data in block. */
588 elseif (isset ($_GET['write_block']) && $cfg['enable_blocks'])
589 {
590 if (!isset ($_POST['id'])
591 || !isset ($_POST['start'])
592 || !isset ($_FILES['data'])
593 || !isset ($_POST['code']))
594 echo "Error";
595 else
596 echo jirafeau_block_write ($_POST['id'], $_POST['start'], $_FILES['data'], $_POST['code']);
597 }
598 /* Delete block. */
599 elseif (isset ($_GET['delete_block']) && $cfg['enable_blocks'])
600 {
601 if (!isset ($_POST['id'])
602 || !isset ($_POST['code']))
603 echo "Error";
604 else
605 echo jirafeau_block_delete ($_POST['id'], $_POST['code']);
606 }
607 else
608 echo "Error";
609 exit;
610 ?>

patrick-canterino.de