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

patrick-canterino.de