]> git.p6c8.net - jirafeau_project.git/blob - script.php
Fix some bugs due to PHP's max_execution_time
[jirafeau_project.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 echo '</div><br />';
186 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
187 exit;
188 }
189
190 /* Lets use interface now. */
191 header('Content-Type: text; charset=utf-8');
192
193 /* Upload file */
194 if (isset ($_FILES['file']) && is_writable (VAR_FILES)
195 && is_writable (VAR_LINKS))
196 {
197 $key = '';
198 if (isset ($_POST['key']))
199 $key = $_POST['key'];
200
201 $time = time ();
202 if (!isset ($_POST['time']))
203 $time = JIRAFEAU_INFINITY;
204 else
205 switch ($_POST['time'])
206 {
207 case 'minute':
208 $time += JIRAFEAU_MINUTE;
209 break;
210 case 'hour':
211 $time += JIRAFEAU_HOUR;
212 break;
213 case 'day':
214 $time += JIRAFEAU_DAY;
215 break;
216 case 'week':
217 $time += JIRAFEAU_WEEK;
218 break;
219 case 'month':
220 $time += JIRAFEAU_MONTH;
221 break;
222 default:
223 $time = JIRAFEAU_INFINITY;
224 break;
225 }
226 $res = jirafeau_upload ($_FILES['file'],
227 isset ($_POST['one_time_download']),
228 $key, $time, $_SERVER['REMOTE_ADDR']);
229
230 if (empty($res) || $res['error']['has_error'])
231 {
232 echo "Error";
233 exit;
234 }
235 /* Print direct link. */
236 echo $res['link'];
237 echo NL;
238 /* Print delete link. */
239 echo $res['delete_link'];
240 }
241 elseif (isset ($_GET['h']))
242 {
243 $link_name = $_GET['h'];
244 $key = '';
245 if (isset ($_POST['key']))
246 $key = $_POST['key'];
247 $d = '';
248 if (isset ($_GET['d']))
249 $d = $_GET['d'];
250
251 if (!preg_match ('/[0-9a-zA-Z_-]{22}$/', $link_name))
252 {
253 echo "Error";
254 exit;
255 }
256
257 $link = jirafeau_get_link ($link_name);
258 if (count ($link) == 0)
259 {
260 echo "Error";
261 exit;
262 }
263 if (strlen ($d) > 0 && $d == $link['link_code'])
264 {
265 jirafeau_delete_link ($link_name);
266 echo "Ok";
267 exit;
268 }
269 if ($link['time'] != JIRAFEAU_INFINITY && time () > $link['time'])
270 {
271 jirafeau_delete_link ($link_name);
272 echo "Error";
273 exit;
274 }
275 if (strlen ($link['key']) > 0 && md5 ($key) != $link['key'])
276 {
277 echo "Error";
278 exit;
279 }
280 $p = s2p ($link['md5']);
281 if (!file_exists (VAR_FILES . $p . $link['md5']))
282 {
283 echo "Error";
284 exit;
285 }
286
287 /* Read file. */
288 header ('Content-Length: ' . $link['file_size']);
289 header ('Content-Type: ' . $link['mime_type']);
290 header ('Content-Disposition: attachment; filename="' .
291 $link['file_name'] . '"');
292
293 $r = fopen (VAR_FILES . $p . $link['md5'], 'r');
294 while (!feof ($r))
295 {
296 print fread ($r, 1024);
297 ob_flush();
298 }
299 fclose ($r);
300
301 if ($link['onetime'] == 'O')
302 jirafeau_delete_link ($link_name);
303 exit;
304 }
305 elseif (isset ($_GET['get_capacity']))
306 {
307 echo min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
308 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
309 }
310 elseif (isset ($_GET['get_version']))
311 {
312 echo JIRAFEAU_VERSION;
313 }
314 elseif (isset ($_GET['lang']))
315 {
316 $l=$_GET['lang'];
317 if ($l == "bash")
318 {
319 ?>
320 #!/bin/bash
321
322 # This script has been auto-generated by Jirafeau but you can still edit
323 # options below.
324
325 # Config
326 proxy='' # ex: proxy='proxysever.test.com:3128' or set JIRAFEAU_PROXY global variable
327 url='<?php echo $cfg['web_root'] . 'script.php'; ?>' # or set JIRAFEAU_URL ex: url='http://mysite/jirafeau/script.php'
328 time='none' # minute, hour, day, week, month or none. Or set JIRAFEAU_TIME.
329 one_time='' # ex: one_time="1" or set JIRAFEAU_ONE_TIME.
330 curl='' # curl path to download or set JIRAFEAU_CURL_PATH.
331 # End of config
332
333 if [ -n "$JIRAFEAU_PROXY" ]; then
334 proxy="$JIRAFEAU_PROXY"
335 fi
336
337 if [ -n "$JIRAFEAU_URL" ]; then
338 url="$JIRAFEAU_URL"
339 fi
340
341 if [ -z "$url" ]; then
342 echo "Please set url in script parameters or export JIRAFEAU_URL"
343 fi
344
345 if [ -n "$JIRAFEAU_TIME" ]; then
346 time="$JIRAFEAU_TIME"
347 fi
348
349 if [ -n "$JIRAFEAU_ONE_TIME" ]; then
350 one_time='1'
351 fi
352
353 if [ -z "$curl" ]; then
354 curl="$JIRAFEAU_CURL_PATH"
355 fi
356
357 if [ -z "$curl" ] && [ -e "/usr/bin/curl" ]; then
358 curl="/usr/bin/curl"
359 fi
360
361 if [ -z "$curl" ] && [ -e "/bin/curl.exe" ]; then
362 curl="/bin/curl.exe"
363 fi
364
365 if [ -z "$curl" ]; then
366 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
367 exit
368 fi
369
370 if [ -z "$2" ]; then
371 echo "man:"
372 echo " $0 send PATH [PASSWORD]"
373 echo " $0 get URL [PASSWORD]"
374 echo " $0 delete URL"
375 echo ""
376 echo "Global variables to export:"
377 echo " JIRAFEAU_PROXY : example: proxysever.test.com:3128"
378 echo " JIRAFEAU_URL : example: http://mysite/jirafeau/script.php"
379 echo " JIRAFEAU_TIME : minute, hour, day, week, month or none"
380 echo " JIRAFEAU_ONE_TIME : set anything or set empty"
381 echo " JIRAFEAU_CURL : path to your curl binary"
382
383 exit 0
384 fi
385
386 if [ -n "$proxy" ]; then
387 proxy="-x $proxy"
388 fi
389
390 options=''
391 if [ -n "$one_time" ]; then
392 options="$options -F one_time_download=1"
393 fi
394
395 password=''
396 if [ -n "$3" ]; then
397 password="$3"
398 options="$options -F key=$password"
399 fi
400
401 if [ "$1" == "send" ]; then
402 if [ ! -f "$2" ]; then
403 echo "File \"$2\" does not exists."
404 exit
405 fi
406
407 # Ret result
408 res=$($curl -X POST --http1.0 $proxy $options \
409 -F "time=$time" \
410 -F "file=@$2" \
411 $url)
412
413 if [[ "$res" == "Error" ]]; then
414 echo "Error while uploading."
415 exit
416 fi
417
418 # Not using head or tail to minimise command dependencies
419 code=$(cnt=0; echo "$res" | while read l; do
420 if [[ "$cnt" == "0" ]]; then
421 echo "$l"
422 fi
423 cnt=$(( cnt + 1 ))
424 done)
425 del_code=$(cnt=0; echo "$res" | while read l; do
426 if [[ "$cnt" == "1" ]]; then
427 echo "$l"
428 fi
429 cnt=$(( cnt + 1 ))
430 done)
431 echo "${url}?h=$code"
432 echo "${url}?h=$code&d=$del_code"
433 elif [ "$1" == "get" ]; then
434 if [ -z "$password" ]; then
435 $curl $proxy -OJ "$2"
436 else
437 $curl $proxy -OJ -X POST -F key=$password "$2"
438 fi
439 elif [ "$1" == "delete" ]; then
440 $curl $proxy "$2"
441 fi
442 <?php
443 }
444 else
445 {
446 echo "Error";
447 exit;
448 }
449 }
450 /* Initialize an asynchronous upload. */
451 elseif (isset ($_GET['init_async']))
452 {
453 if (!isset ($_POST['filename']))
454 {
455 echo "Error";
456 exit;
457 }
458
459 $type = '';
460 if (isset ($_POST['type']))
461 $type = $_POST['type'];
462
463 $key = '';
464 if (isset ($_POST['password']))
465 $key = $_POST['password'];
466
467 $time = time ();
468 if (!isset ($_POST['time']))
469 $time = JIRAFEAU_INFINITY;
470 else
471 switch ($_POST['time'])
472 {
473 case 'minute':
474 $time += JIRAFEAU_MINUTE;
475 break;
476 case 'hour':
477 $time += JIRAFEAU_HOUR;
478 break;
479 case 'day':
480 $time += JIRAFEAU_DAY;
481 break;
482 case 'week':
483 $time += JIRAFEAU_WEEK;
484 break;
485 case 'month':
486 $time += JIRAFEAU_MONTH;
487 break;
488 default:
489 $time = JIRAFEAU_INFINITY;
490 break;
491 }
492 echo jirafeau_async_init ($_POST['filename'],
493 $type,
494 isset ($_POST['one_time_download']),
495 $key,
496 $time,
497 $_SERVER['REMOTE_ADDR']);
498 }
499 /* Continue an asynchronous upload. */
500 elseif (isset ($_GET['push_async']))
501 {
502 if ((!isset ($_POST['ref']))
503 || (!isset ($_FILES['data']))
504 || (!isset ($_POST['code'])))
505 echo "Error";
506 else
507 echo jirafeau_async_push ($_POST['ref'], $_FILES['data'], $_POST['code']);
508 }
509 /* Finalize an asynchronous upload. */
510 elseif (isset ($_GET['end_async']))
511 {
512 if (!isset ($_POST['ref'])
513 || !isset ($_POST['code']))
514 echo "Error";
515 else
516 echo jirafeau_async_end ($_POST['ref'], $_POST['code']);
517 }
518 else
519 echo "Error";
520 exit;
521 ?>

patrick-canterino.de