]> git.p6c8.net - jirafeau.git/blob - script.php
Fix error checks
[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 if ($_SERVER['REQUEST_METHOD'] == "GET" && count ($_GET) == 0)
37 {
38 require (JIRAFEAU_ROOT . 'lib/template/header.php');
39 check_errors ();
40 if (has_error ())
41 {
42 show_errors ();
43 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
44 exit;
45 }
46 echo '<div class="info">';
47 echo '<h2>' . t('Welcome to Jirafeau\'s query interface') . '</h2>';
48 echo '<p>';
49 echo t('This interface permits to script your uploads and downloads.') .
50 ' ' . t('The instructions above show how to query this interface.');
51 echo '</p>';
52
53 echo '<h3>' . t('Get Jirafeau\'s version') . ':</h3>';
54 echo '<p>';
55 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
56 echo '<br />';
57 echo t('Parameters') . ':<br />';
58 echo "<b>get_version=</b>1<i> (" . t('Required') . ")</i> <br />";
59 echo '</p>';
60 echo '<p>' . t('This will return brut text content.') . ' ' .
61 t('First line is the version number.') . '<br /></p>';
62 echo '<p>';
63 echo t('Example') . ": <a href=\"" . $web_root . "script.php?get_version=1\">" . $web_root . "script.php?get_version=1</a> ";
64 echo '</p>';
65
66 echo '<h3>' . t('Get server capacity') . ':</h3>';
67 echo '<p>';
68 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
69 echo '<br />';
70 echo t('Parameters') . ':<br />';
71 echo "<b>get_capacity=</b>1<i> (" . t('Required') . ")</i> <br />";
72 echo '</p>';
73 echo '<p>' . t('This will return brut text content.') . ' ' .
74 t('First line is the server capacity (in Bytes).') . '<br /></p>';
75 echo '<p>';
76 echo t('Example') . ": <a href=\"" . $web_root . "script.php?get_capacity=1\">" . $web_root . "script.php?get_capacity=1</a> ";
77 echo '</p>';
78
79 echo '<h3>' . t('Upload a file') . ':</h3>';
80 echo '<p>';
81 echo t('Send a POST query to') . ': <i>' . $web_root . 'script.php</i><br />';
82 echo '<br />';
83 echo t('Parameters') . ':<br />';
84 echo "<b>file=</b>C:\\your\\file\\path<i> (" . t('Required') . ")</i> <br />";
85 echo "<b>time=</b>[minute|hour|day|week|month|none]<i> (" . t('Optional') . ', '. t('default: none') . ")</i> <br />";
86 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
87 echo "<b>one_time_download=</b>1<i> (" . t('Optional') . ")</i> <br />";
88 echo '</p>';
89 echo '<p>' . t('This will return brut text content.') . ' ' .
90 t('First line is the download reference and the second line the delete code.') . '<br /></p>';
91
92 echo '<h3>' . t('Get a file') . ':</h3>';
93 echo '<p>';
94 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
95 echo '<br />';
96 echo t('Parameters') . ':<br />';
97 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
98 echo '</p>';
99 echo '<p>';
100 echo t('If a password has been set, send a POST request with it.');
101 echo '<br />';
102 echo t('Parameters') . ':<br />';
103 echo "<b>password=</b>your_password<i> (" . t('Optional') . ")</i> <br />";
104 echo '</p>';
105 echo '<p>';
106 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU</a> ";
107 echo '</p>';
108
109 echo '<h3>' . t('Delete a file') . ':</h3>';
110 echo '<p>';
111 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
112 echo '<br />';
113 echo t('Parameters') . ':<br />';
114 echo "<b>h=</b>your_download_reference<i> (" . t('Required') . ")</i> <br />";
115 echo "<b>d=</b>yout_delete_code<i> (" . t('Required') . ")</i> <br />";
116 echo '</p>';
117 echo '<p>' . t('This will return "Ok" if succeded, "Error" otherwhise.') . '<br /></p>';
118 echo '<p>';
119 echo t('Example') . ": <a href=\"" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&amp;d=0d210a952\">" . $web_root . "script.php?h=30ngy0hsDcpfrF8zR7x9iU&amp;d=0d210a952</a> ";
120 echo '</p>';
121
122 echo '<h3>' . t('Get a generated scripts') . ':</h3>';
123 echo '<p>';
124 echo t('Send a GET query to') . ': <i>' . $web_root . 'script.php</i><br />';
125 echo '<br />';
126 echo t('Parameters') . ':<br />';
127 echo "<b>lang=</b>[";
128 foreach ($script_langages as $lang => $name)
129 echo $lang;
130 echo "]<i> (" . t('Required') . ")</i> <br />";
131 echo '</p>';
132 echo '<p>' . t('This will return brut text content of the code.') . '<br /></p>';
133 echo '<p>';
134 echo t('Example') . ": <br />";
135 foreach ($script_langages as $lang => $name)
136 echo "$name: <a href=\"" . $web_root . "script.php?lang=$lang\">" . $web_root . "script.php?lang=$lang</a> ";
137 echo '</p>';
138
139 echo '</div><br />';
140 require (JIRAFEAU_ROOT . 'lib/template/footer.php');
141 exit;
142 }
143
144 /* Lets use interface now. */
145 header('Content-Type: text; charset=utf-8');
146
147 /* Upload file */
148 if (isset ($_FILES['file']) && is_writable (VAR_FILES)
149 && is_writable (VAR_LINKS))
150 {
151 $key = '';
152 if (isset ($_POST['key']))
153 $key = $_POST['key'];
154
155 $time = time ();
156 if (!isset ($_POST['time']))
157 $time = JIRAFEAU_INFINITY;
158 else
159 switch ($_POST['time'])
160 {
161 case 'minute':
162 $time += JIRAFEAU_MINUTE;
163 break;
164 case 'hour':
165 $time += JIRAFEAU_HOUR;
166 break;
167 case 'day':
168 $time += JIRAFEAU_DAY;
169 break;
170 case 'week':
171 $time += JIRAFEAU_WEEK;
172 break;
173 case 'month':
174 $time += JIRAFEAU_MONTH;
175 break;
176 default:
177 $time = JIRAFEAU_INFINITY;
178 break;
179 }
180 $res = jirafeau_upload ($_FILES['file'],
181 isset ($_POST['one_time_download']),
182 $key, $time, $_SERVER['REMOTE_ADDR']);
183
184 if (empty($res) || $res['error']['has_error'])
185 {
186 echo "Error";
187 exit;
188 }
189 /* Print direct link. */
190 echo $res['link'];
191 echo NL;
192 /* Print delete link. */
193 echo $res['delete_link'];
194 }
195 elseif (isset ($_GET['h']))
196 {
197 $link_name = $_GET['h'];
198 $key = '';
199 if (isset ($_POST['key']))
200 $key = $_POST['key'];
201 $d = '';
202 if (isset ($_GET['d']))
203 $d = $_GET['d'];
204
205 if (!preg_match ('/[0-9a-zA-Z_-]{22}$/', $link_name))
206 {
207 echo "Error";
208 exit;
209 }
210
211 $link = jirafeau_get_link ($link_name);
212 if (count ($link) == 0)
213 {
214 echo "Error";
215 exit;
216 }
217 if (strlen ($d) > 0 && $d == $link['link_code'])
218 {
219 jirafeau_delete_link ($link_name);
220 echo "Ok";
221 exit;
222 }
223 if ($link['time'] != JIRAFEAU_INFINITY && time () > $link['time'])
224 {
225 jirafeau_delete_link ($link_name);
226 echo "Error";
227 exit;
228 }
229 if (strlen ($link['key']) > 0 && md5 ($key) != $link['key'])
230 {
231 echo "Error";
232 exit;
233 }
234 $p = s2p ($link['md5']);
235 if (!file_exists (VAR_FILES . $p . $link['md5']))
236 {
237 echo "Error";
238 exit;
239 }
240
241 header ('Content-Length: ' . $link['file_size']);
242 header ('Content-Type: ' . $link['mime_type']);
243 header ('Content-Disposition: attachment; filename="' .
244 $link['file_name'] . '"');
245 readfile (VAR_FILES . $p . $link['md5']);
246
247 if ($link['onetime'] == 'O')
248 jirafeau_delete_link ($link_name);
249 exit;
250 }
251 elseif (isset ($_GET['get_capacity']))
252 {
253 echo min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
254 jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
255 }
256 elseif (isset ($_GET['get_version']))
257 {
258 echo JIRAFEAU_VERSION;
259 }
260 elseif (isset ($_GET['lang']))
261 {
262 $l=$_GET['lang'];
263 if ($l == "bash")
264 {
265 ?>
266 #!/bin/bash
267
268 # This script has been auto-generated by Jirafeau but you can still edit
269 # options below.
270
271 # Config
272 proxy='' # ex: proxy='proxysever.test.com:3128' or set JIRAFEAU_PROXY global variable
273 url='<?php echo $cfg['web_root'] . 'script.php'; ?>' # or set JIRAFEAU_URL ex: url='http://mysite/jirafeau/script.php'
274 time='none' # minute, hour, day, week, month or none. Or set JIRAFEAU_TIME.
275 one_time='' # ex: one_time="1" or set JIRAFEAU_ONE_TIME.
276 curl='' # curl path to download or set JIRAFEAU_CURL_PATH.
277 # End of config
278
279 if [ -n "$JIRAFEAU_PROXY" ]; then
280 proxy="$JIRAFEAU_PROXY"
281 fi
282
283 if [ -n "$JIRAFEAU_URL" ]; then
284 url="$JIRAFEAU_URL"
285 fi
286
287 if [ -z "$url" ]; then
288 echo "Please set url in script parameters or export JIRAFEAU_URL"
289 fi
290
291 if [ -n "$JIRAFEAU_TIME" ]; then
292 time="$JIRAFEAU_TIME"
293 fi
294
295 if [ -n "$JIRAFEAU_ONE_TIME" ]; then
296 one_time='1'
297 fi
298
299 if [ -z "$curl" ]; then
300 curl="$JIRAFEAU_CURL_PATH"
301 fi
302
303 if [ -z "$curl" ] && [ -e "/usr/bin/curl" ]; then
304 curl="/usr/bin/curl"
305 fi
306
307 if [ -z "$curl" ] && [ -e "/bin/curl.exe" ]; then
308 curl="/bin/curl.exe"
309 fi
310
311 if [ -z "$curl" ]; then
312 echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
313 exit
314 fi
315
316 if [ -z "$2" ]; then
317 echo "man:"
318 echo " $0 send PATH [PASSWORD]"
319 echo " $0 get URL [PASSWORD]"
320 echo " $0 delete URL"
321 echo ""
322 echo "Global variables to export:"
323 echo " JIRAFEAU_PROXY : example: proxysever.test.com:3128"
324 echo " JIRAFEAU_URL : example: http://mysite/jirafeau/script.php"
325 echo " JIRAFEAU_TIME : minute, hour, day, week, month or none"
326 echo " JIRAFEAU_ONE_TIME : set anything or set empty"
327 echo " JIRAFEAU_CURL : path to your curl binary"
328
329 exit 0
330 fi
331
332 if [ -n "$proxy" ]; then
333 proxy="-x $proxy"
334 fi
335
336 options=''
337 if [ -n "$one_time" ]; then
338 options="$options -F one_time_download=1"
339 fi
340
341 password=''
342 if [ -n "$3" ]; then
343 password="$3"
344 options="$options -F key=$password"
345 fi
346
347 if [ "$1" == "send" ]; then
348 if [ ! -f "$2" ]; then
349 echo "File \"$2\" does not exists."
350 exit
351 fi
352
353 # Ret result
354 res=$($curl -X POST --http1.0 $proxy $options \
355 -F "time=$time" \
356 -F "file=@$2" \
357 $url)
358
359 if [[ "$res" == "Error" ]]; then
360 echo "Error while uploading."
361 exit
362 fi
363
364 # Not using head or tail to minimise command dependencies
365 code=$(cnt=0; echo "$res" | while read l; do
366 if [[ "$cnt" == "0" ]]; then
367 echo "$l"
368 fi
369 cnt=$(( cnt + 1 ))
370 done)
371 del_code=$(cnt=0; echo "$res" | while read l; do
372 if [[ "$cnt" == "1" ]]; then
373 echo "$l"
374 fi
375 cnt=$(( cnt + 1 ))
376 done)
377 echo "${url}?h=$code"
378 echo "${url}?h=$code&d=$del_code"
379 elif [ "$1" == "get" ]; then
380 if [ -z "$password" ]; then
381 $curl $proxy -OJ "$2"
382 else
383 $curl $proxy -OJ -X POST -F key=$password "$2"
384 fi
385 elif [ "$1" == "delete" ]; then
386 $curl $proxy "$2"
387 fi
388 <?php
389 }
390 else
391 {
392 echo "Error";
393 exit;
394 }
395 }
396 else
397 echo "Error";
398 exit;
399 ?>

patrick-canterino.de