]> git.p6c8.net - jirafeau_mojo42.git/blob - script.php
09c7a8c1dd3ff030e99348d21679c3445209adb2
[jirafeau_mojo42.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.original.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 /* Remove errors. */
41 @error_reporting(0);
42
43 if ($_SERVER['REQUEST_METHOD'] == "GET" && count ($_GET) == 0)
44 {
45 require (JIRAFEAU_ROOT . 'lib/template/header.php');
46 check_errors ($cfg);
47 if (has_error ())
48 {
49 show_errors ();
50 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
51 exit;
52 }
53 echo '<div class="info">';
54 echo '<h2>' . t('Welcome to Jirafeau\'s query interface') . '</h2>';
55 echo '<p>';
56 echo t('This interface permits to script your uploads and downloads.') .
57 ' ' . t('The instructions above show how to query this interface.');
58 echo '</p>';
59
60 echo '<h3>' . t('Get Jirafeau\'s version') . ':</h3>';
61 echo '<p>';
62 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
63 echo '<br />';
64 echo t('Parameters') . ':<br />';
65 echo "<b>get_version=</b>1<i> (" . t('Required') . ")</i> <br />";
66 echo '</p>';
67 echo '<p>' . t('This will return brut text content.') . ' ' .
68 t('First line is the version number.') . '<br /></p>';
69 echo '<p>';
70 echo t('Example') . ": <a href=\"" . $web_root . "script.php?get_version=1\">" . $web_root . "script.php?get_version=1</a> ";
71 echo '</p>';
72
73 echo '<h3>' . t('Get server capacity') . ':</h3>';
74 echo '<p>';
75 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
76 echo '<br />';
77 echo t('Parameters') . ':<br />';
78 echo "<b>get_capacity=</b>1<i> (" . t('Required') . ")</i> <br />";
79 echo '</p>';
80 echo '<p>' . t('This will return brut text content.') . ' ' .
81 t('First line is the server capacity (in Bytes).') . '<br /></p>';
82 echo '<p>';
83 echo t('Example') . ": <a href=\"" . $web_root . "script.php?get_capacity=1\">" . $web_root . "script.php?get_capacity=1</a> ";
84 echo '</p>';
85
86 echo '<h3>' . t('Upload a file') . ':</h3>';
87 echo '<p>';
88 echo t('Send a POST query to') . ': <i>' . $web_root . 'script.php</i><br />';
89 echo '<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 '</p>';
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>';
98
99 echo '<h3>' . t('Get a file') . ':</h3>';
100 echo '<p>';
101 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
102 echo '<br />';
103 echo t('Parameters') . ':<br />';
104 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
105 echo '</p>';
106 echo '<p>';
107 echo t('If a password has been set, send a POST request with it.');
108 echo '<br />';
109 echo t('Parameters') . ':<br />';
110 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
111 echo '</p>';
112 echo '<p>';
113 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU</a> ";
114 echo '</p>';
115
116 echo '<h3>' . t('Delete a file') . ':</h3>';
117 echo '<p>';
118 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
119 echo '<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 />";
123 echo '</p>';
124 echo '<p>' . t('This will return "Ok" if succeded, "Error" otherwhise.') . '<br /></p>';
125 echo '<p>';
126 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&amp;d=0d210a952\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&amp;d=0d210a952</a> ";
127 echo '</p>';
128
129 echo '<h3>' . t('Get a generated scripts') . ':</h3>';
130 echo '<p>';
131 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
132 echo '<br />';
133 echo t('Parameters') . ':<br />';
134 echo "<b>lang=</b>[";
135 foreach ($script_langages as $lang => $name)
136 echo $lang;
137 echo "]<i> (" . t('Required') . ")</i> <br />";
138 echo '</p>';
139 echo '<p>' . t('This will return brut text content of the code.') . '<br /></p>';
140 echo '<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> ";
144 echo '</p>';
145
146 echo '<h3>' . t('Initalize a asynchronous transfert') . ':</h3>';
147 echo '<p>';
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.');
150 echo '</p>';
151 echo '<p>';
152 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?init_async</i><br />';
153 echo '<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 />";
160 echo '</p>';
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>';
163
164 echo '<h3>' . t('Push data during asynchronous transfert') . ':</h3>';
165 echo '<p>';
166 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?push_async</i><br />';
167 echo '<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 />";
172 echo '</p>';
173 echo '<p>' . t('This will return brut text content.') . ' ' .
174 t('Returns the next code to use.') . '<br /></p>';
175
176 echo '<h3>' . t('Finalize asynchronous transfert') . ':</h3>';
177 echo '<p>';
178 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php?end_async</i><br />';
179 echo '<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 />";
183 echo '</p>';
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>';
186
187 echo '</div><br />';
188 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
189 exit;
190 }
191
192 /* Lets use interface now. */
193 header('Content-Type: text; charset=utf-8');
194
195 check_errors ($cfg);
196 if (has_error ())
197 {
198 echo "Error";
199 exit;
200 }
201
202 /* Upload file */
203 if (isset ($_FILES['file']) && is_writable (VAR_FILES)
204 && is_writable (VAR_LINKS))
205 {
206 if (jirafeau_has_upload_password ($cfg) &&
207 (!isset ($_POST['upload_password']) ||
208 !jirafeau_challenge_upload_password ($cfg, $_POST['upload_password'])))
209 {
210 echo "Error";
211 exit;
212 }
213
214 $key = '';
215 if (isset ($_POST['key']))
216 $key = $_POST['key'];
217
218 $time = time ();
219 if (!isset ($_POST['time']))
220 $time = JIRAFEAU_INFINITY;
221 else
222 switch ($_POST['time'])
223 {
224 case 'minute':
225 $time += JIRAFEAU_MINUTE;
226 break;
227 case 'hour':
228 $time += JIRAFEAU_HOUR;
229 break;
230 case 'day':
231 $time += JIRAFEAU_DAY;
232 break;
233 case 'week':
234 $time += JIRAFEAU_WEEK;
235 break;
236 case 'month':
237 $time += JIRAFEAU_MONTH;
238 break;
239 default:
240 $time = JIRAFEAU_INFINITY;
241 break;
242 }
243 $res = jirafeau_upload ($_FILES['file'],
244 isset ($_POST['one_time_download']),
245 $key, $time, $_SERVER['REMOTE_ADDR'],
246 $cfg['enable_crypt'], $cfg['link_name_lenght']);
247
248 if (empty($res) || $res['error']['has_error'])
249 {
250 echo "Error";
251 exit;
252 }
253 /* Print direct link. */
254 echo $res['link'];
255 /* Print delete link. */
256 echo NL;
257 echo $res['delete_link'];
258 /* Print decrypt key. */
259 echo NL;
260 echo urlencode($res['crypt_key']);
261 }
262 elseif (isset ($_GET['h']))
263 {
264 $link_name = $_GET['h'];
265 $key = '';
266 if (isset ($_POST['key']))
267 $key = $_POST['key'];
268 $d = '';
269 if (isset ($_GET['d']))
270 $d = $_GET['d'];
271
272 if (!preg_match ('/[0-9a-zA-Z_-]+$/', $link_name))
273 {
274 echo "Error";
275 exit;
276 }
277
278 $link = jirafeau_get_link ($link_name);
279 if (count ($link) == 0)
280 {
281 echo "Error";
282 exit;
283 }
284 if (strlen ($d) > 0 && $d == $link['link_code'])
285 {
286 jirafeau_delete_link ($link_name);
287 echo "Ok";
288 exit;
289 }
290 if ($link['time'] != JIRAFEAU_INFINITY && time () > $link['time'])
291 {
292 jirafeau_delete_link ($link_name);
293 echo "Error";
294 exit;
295 }
296 if (strlen ($link['key']) > 0 && md5 ($key) != $link['key'])
297 {
298 echo "Error";
299 exit;
300 }
301 $p = s2p ($link['md5']);
302 if (!file_exists (VAR_FILES . $p . $link['md5']))
303 {
304 echo "Error";
305 exit;
306 }
307
308 /* Read file. */
309 header ('Content-Length: ' . $link['file_size']);
310 header ('Content-Type: ' . $link['mime_type']);
311 header ('Content-Disposition: attachment; filename="' .
312 $link['file_name'] . '"');
313
314 $r = fopen (VAR_FILES . $p . $link['md5'], 'r');
315 while (!feof ($r))
316 {
317 print fread ($r, 1024);
318 ob_flush();
319 }
320 fclose ($r);
321
322 if ($link['onetime'] == 'O')
323 jirafeau_delete_link ($link_name);
324 exit;
325 }
326 elseif (isset ($_GET['get_capacity']))
327 {
328 echo min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
329 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
330 }
331 elseif (isset ($_GET['get_version']))
332 {
333 echo JIRAFEAU_VERSION;
334 }
335 elseif (isset ($_GET['lang']))
336 {
337 $l=$_GET['lang'];
338 if ($l == "bash")
339 {
340 ?>
341 #!/bin/bash
342
343 # This script has been auto-generated by Jirafeau but you can still edit
344 # options below.
345
346 # Config
347 proxy='' # ex: proxy='proxysever.test.com:3128' or set JIRAFEAU_PROXY global variable
348 url='<?php echo $cfg['web_root'] . 'script.php'; ?>' # or set JIRAFEAU_URL ex: url='http://mysite/jirafeau/script.php'
349 time='none' # minute, hour, day, week, month or none. Or set JIRAFEAU_TIME.
350 one_time='' # ex: one_time="1" or set JIRAFEAU_ONE_TIME.
351 curl='' # curl path to download or set JIRAFEAU_CURL_PATH.
352 # End of config
353
354 if [ -n "$JIRAFEAU_PROXY" ]; then
355 proxy="$JIRAFEAU_PROXY"
356 fi
357
358 if [ -n "$JIRAFEAU_URL" ]; then
359 url="$JIRAFEAU_URL"
360 fi
361
362 if [ -z "$url" ]; then
363 echo "Please set url in script parameters or export JIRAFEAU_URL"
364 fi
365
366 if [ -n "$JIRAFEAU_TIME" ]; then
367 time="$JIRAFEAU_TIME"
368 fi
369
370 if [ -n "$JIRAFEAU_ONE_TIME" ]; then
371 one_time='1'
372 fi
373
374 if [ -z "$curl" ]; then
375 curl="$JIRAFEAU_CURL_PATH"
376 fi
377
378 if [ -z "$curl" ] && [ -e "/usr/bin/curl" ]; then
379 curl="/usr/bin/curl"
380 fi
381
382 if [ -z "$curl" ] && [ -e "/bin/curl.exe" ]; then
383 curl="/bin/curl.exe"
384 fi
385
386 if [ -z "$curl" ]; then
387 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
388 exit
389 fi
390
391 if [ -z "$2" ]; then
392 echo "man:"
393 echo " $0 send PATH [PASSWORD]"
394 echo " $0 get URL [PASSWORD]"
395 echo " $0 delete URL"
396 echo ""
397 echo "Global variables to export:"
398 echo " JIRAFEAU_PROXY : example: proxysever.test.com:3128"
399 echo " JIRAFEAU_URL : example: http://mysite/jirafeau/script.php"
400 echo " JIRAFEAU_TIME : minute, hour, day, week, month or none"
401 echo " JIRAFEAU_ONE_TIME : set anything or set empty"
402 echo " JIRAFEAU_CURL : path to your curl binary"
403
404 exit 0
405 fi
406
407 if [ -n "$proxy" ]; then
408 proxy="-x $proxy"
409 fi
410
411 options=''
412 if [ -n "$one_time" ]; then
413 options="$options -F one_time_download=1"
414 fi
415
416 password=''
417 if [ -n "$3" ]; then
418 password="$3"
419 options="$options -F key=$password"
420 fi
421
422 if [ "$1" == "send" ]; then
423 if [ ! -f "$2" ]; then
424 echo "File \"$2\" does not exists."
425 exit
426 fi
427
428 # Ret result
429 res=$($curl -X POST --http1.0 $proxy $options \
430 -F "time=$time" \
431 -F "file=@$2" \
432 $url)
433
434 if [[ "$res" == "Error" ]]; then
435 echo "Error while uploading."
436 exit
437 fi
438
439 # Not using head or tail to minimise command dependencies
440 code=$(cnt=0; echo "$res" | while read l; do
441 if [[ "$cnt" == "0" ]]; then
442 echo "$l"
443 fi
444 cnt=$(( cnt + 1 ))
445 done)
446 del_code=$(cnt=0; echo "$res" | while read l; do
447 if [[ "$cnt" == "1" ]]; then
448 echo "$l"
449 fi
450 cnt=$(( cnt + 1 ))
451 done)
452 echo "${url}?h=$code"
453 echo "${url}?h=$code&d=$del_code"
454 elif [ "$1" == "get" ]; then
455 if [ -z "$password" ]; then
456 $curl $proxy -OJ "$2"
457 else
458 $curl $proxy -OJ -X POST -F key=$password "$2"
459 fi
460 elif [ "$1" == "delete" ]; then
461 $curl $proxy "$2"
462 fi
463 <?php
464 }
465 else
466 {
467 echo "Error";
468 exit;
469 }
470 }
471 /* Initialize an asynchronous upload. */
472 elseif (isset ($_GET['init_async']))
473 {
474 if (jirafeau_has_upload_password ($cfg) &&
475 (!isset ($_POST['upload_password']) ||
476 !jirafeau_challenge_upload_password ($cfg, $_POST['upload_password'])))
477 {
478 echo "Error";
479 exit;
480 }
481
482 if (!isset ($_POST['filename']))
483 {
484 echo "Error";
485 exit;
486 }
487
488 $type = '';
489 if (isset ($_POST['type']))
490 $type = $_POST['type'];
491
492 $key = '';
493 if (isset ($_POST['key']))
494 $key = $_POST['key'];
495
496 $time = time ();
497 if (!isset ($_POST['time']))
498 $time = JIRAFEAU_INFINITY;
499 else
500 switch ($_POST['time'])
501 {
502 case 'minute':
503 $time += JIRAFEAU_MINUTE;
504 break;
505 case 'hour':
506 $time += JIRAFEAU_HOUR;
507 break;
508 case 'day':
509 $time += JIRAFEAU_DAY;
510 break;
511 case 'week':
512 $time += JIRAFEAU_WEEK;
513 break;
514 case 'month':
515 $time += JIRAFEAU_MONTH;
516 break;
517 default:
518 $time = JIRAFEAU_INFINITY;
519 break;
520 }
521 echo jirafeau_async_init ($_POST['filename'],
522 $type,
523 isset ($_POST['one_time_download']),
524 $key,
525 $time,
526 $_SERVER['REMOTE_ADDR']);
527 }
528 /* Continue an asynchronous upload. */
529 elseif (isset ($_GET['push_async']))
530 {
531 if ((!isset ($_POST['ref']))
532 || (!isset ($_FILES['data']))
533 || (!isset ($_POST['code'])))
534 echo "Error";
535 else
536 echo jirafeau_async_push ($_POST['ref'], $_FILES['data'], $_POST['code']);
537 }
538 /* Finalize an asynchronous upload. */
539 elseif (isset ($_GET['end_async']))
540 {
541 if (!isset ($_POST['ref'])
542 || !isset ($_POST['code']))
543 echo "Error";
544 else
545 echo jirafeau_async_end ($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_lenght']);
546 }
547 else
548 echo "Error";
549 exit;
550 ?>

patrick-canterino.de