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

patrick-canterino.de