PageRenderTime 59ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/script.php

https://gitlab.com/sidneywebba/Jirafeau
PHP | 548 lines | 449 code | 52 blank | 47 comment | 88 complexity | 49c81031245190eefc97f0f41f6f2e49 MD5 | raw file
  1. <?php
  2. /*
  3. * Jirafeau, your web file repository
  4. * Copyright (C) 2015 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 <https://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * This file permits to easyly script file sending, receiving, deleting, ...
  21. * If you don't want this feature, you can simply delete this file from your
  22. * web directory.
  23. */
  24. define ('JIRAFEAU_ROOT', dirname (__FILE__) . '/');
  25. require (JIRAFEAU_ROOT . 'lib/config.original.php');
  26. require (JIRAFEAU_ROOT . 'lib/settings.php');
  27. require (JIRAFEAU_ROOT . 'lib/functions.php');
  28. require (JIRAFEAU_ROOT . 'lib/lang.php');
  29. global $script_langages;
  30. $script_langages = array ('bash' => 'Bash');
  31. /* Operations may take a long time.
  32. * Be sure PHP's safe mode is off.
  33. */
  34. @set_time_limit(0);
  35. /* Remove errors. */
  36. @error_reporting(0);
  37. if ($_SERVER['REQUEST_METHOD'] == "GET" && count ($_GET) == 0)
  38. {
  39. require (JIRAFEAU_ROOT . 'lib/template/header.php');
  40. check_errors ($cfg);
  41. if (has_error ())
  42. {
  43. show_errors ();
  44. require (JIRAFEAU_ROOT . 'lib/template/footer.php');
  45. exit;
  46. }
  47. ?>
  48. <div class="info">
  49. <h2>Scripting interface</h2>
  50. <p>This interface permits to script your uploads and downloads.</p>
  51. <p>See <a href="https://gitlab.com/mojo42/Jirafeau/blob/master/script.php">source code</a> of this interface to get available calls :)</p>
  52. <p>Alternatively, go to <a href="<?php echo $cfg['web_root'] . 'script.php?lang=bash'; ?>">this page</a> to download a bash script.</p>
  53. </div>
  54. <br />
  55. <?php
  56. require (JIRAFEAU_ROOT . 'lib/template/footer.php');
  57. exit;
  58. }
  59. /* Lets use interface now. */
  60. header('Content-Type: text; charset=utf-8');
  61. check_errors ($cfg);
  62. if (has_error ())
  63. {
  64. echo 'Error 1';
  65. exit;
  66. }
  67. /* Upload file */
  68. if (isset ($_FILES['file']) && is_writable (VAR_FILES)
  69. && is_writable (VAR_LINKS))
  70. {
  71. if (!jirafeau_challenge_upload_ip ($cfg, get_ip_address($cfg)))
  72. {
  73. echo 'Error 2';
  74. exit;
  75. }
  76. if (jirafeau_has_upload_password ($cfg) &&
  77. (!isset ($_POST['upload_password']) ||
  78. !jirafeau_challenge_upload_password ($cfg, $_POST['upload_password'])))
  79. {
  80. echo 'Error 3';
  81. exit;
  82. }
  83. $key = '';
  84. if (isset ($_POST['key']))
  85. $key = $_POST['key'];
  86. $time = time ();
  87. if (!isset ($_POST['time']) || !$cfg['availabilities'][$_POST['time']])
  88. {
  89. echo 'Error 4: The parameter time is invalid.';
  90. exit;
  91. }
  92. else
  93. switch ($_POST['time'])
  94. {
  95. case 'minute':
  96. $time += JIRAFEAU_MINUTE;
  97. break;
  98. case 'hour':
  99. $time += JIRAFEAU_HOUR;
  100. break;
  101. case 'day':
  102. $time += JIRAFEAU_DAY;
  103. break;
  104. case 'week':
  105. $time += JIRAFEAU_WEEK;
  106. break;
  107. case 'month':
  108. $time += JIRAFEAU_MONTH;
  109. break;
  110. case 'year':
  111. $time += JIRAFEAU_YEAR;
  112. break;
  113. default:
  114. $time = JIRAFEAU_INFINITY;
  115. break;
  116. }
  117. // Check file size
  118. if ($cfg['maximal_upload_size'] > 0 &&
  119. $_FILES['file']['size'] > $cfg['maximal_upload_size'] * 1024 * 1024)
  120. {
  121. echo 'Error 5: Your file exceeds the maximum authorized file size.';
  122. exit;
  123. }
  124. $res = jirafeau_upload ($_FILES['file'],
  125. isset ($_POST['one_time_download']),
  126. $key, $time, get_ip_address($cfg),
  127. $cfg['enable_crypt'], $cfg['link_name_length']);
  128. if (empty($res) || $res['error']['has_error'])
  129. {
  130. echo 'Error 6 ' . $res['error']['why'];
  131. exit;
  132. }
  133. /* Print direct link. */
  134. echo $res['link'];
  135. /* Print delete link. */
  136. echo NL;
  137. echo $res['delete_link'];
  138. /* Print decrypt key. */
  139. echo NL;
  140. echo urlencode($res['crypt_key']);
  141. }
  142. elseif (isset ($_GET['h']))
  143. {
  144. $link_name = $_GET['h'];
  145. $key = '';
  146. if (isset ($_POST['key']))
  147. $key = $_POST['key'];
  148. $d = '';
  149. if (isset ($_GET['d']))
  150. $d = $_GET['d'];
  151. if (!preg_match ('/[0-9a-zA-Z_-]+$/', $link_name))
  152. {
  153. echo 'Error 7';
  154. exit;
  155. }
  156. $link = jirafeau_get_link ($link_name);
  157. if (count ($link) == 0)
  158. {
  159. echo 'Error 8';
  160. exit;
  161. }
  162. if (strlen ($d) > 0 && $d == $link['link_code'])
  163. {
  164. jirafeau_delete_link ($link_name);
  165. echo "Ok";
  166. exit;
  167. }
  168. if ($link['time'] != JIRAFEAU_INFINITY && time () > $link['time'])
  169. {
  170. jirafeau_delete_link ($link_name);
  171. echo 'Error 9';
  172. exit;
  173. }
  174. if (strlen ($link['key']) > 0 && md5 ($key) != $link['key'])
  175. {
  176. sleep (2);
  177. echo 'Error 10';
  178. exit;
  179. }
  180. $p = s2p ($link['md5']);
  181. if (!file_exists (VAR_FILES . $p . $link['md5']))
  182. {
  183. echo 'Error 11';
  184. exit;
  185. }
  186. /* Read file. */
  187. header ('Content-Length: ' . $link['file_size']);
  188. header ('Content-Type: ' . $link['mime_type']);
  189. header ('Content-Disposition: attachment; filename="' .
  190. $link['file_name'] . '"');
  191. $r = fopen (VAR_FILES . $p . $link['md5'], 'r');
  192. while (!feof ($r))
  193. {
  194. print fread ($r, 1024);
  195. ob_flush();
  196. }
  197. fclose ($r);
  198. if ($link['onetime'] == 'O')
  199. jirafeau_delete_link ($link_name);
  200. exit;
  201. }
  202. elseif (isset ($_GET['get_capacity']))
  203. {
  204. echo min (jirafeau_ini_to_bytes (ini_get ('post_max_size')),
  205. jirafeau_ini_to_bytes (ini_get ('upload_max_filesize')));
  206. }
  207. elseif (isset ($_GET['get_maximal_upload_size']))
  208. {
  209. echo $cfg['maximal_upload_size'];
  210. }
  211. elseif (isset ($_GET['get_version']))
  212. {
  213. echo JIRAFEAU_VERSION;
  214. }
  215. elseif (isset ($_GET['lang']))
  216. {
  217. $l=$_GET['lang'];
  218. if ($l == "bash")
  219. {
  220. ?>
  221. #!/bin/bash
  222. # This script has been auto-generated by Jirafeau but you can still edit
  223. # options below.
  224. # Config
  225. proxy='' # ex: proxy='proxysever.test.com:3128' or set JIRAFEAU_PROXY global variable
  226. url='<?php echo $cfg['web_root'] . 'script.php'; ?>' # or set JIRAFEAU_URL ex: url='http://mysite/jirafeau/script.php'
  227. time='none' # minute, hour, day, week, month, year or none. Or set JIRAFEAU_TIME.
  228. one_time='' # ex: one_time="1" or set JIRAFEAU_ONE_TIME.
  229. curl='' # curl path to download or set JIRAFEAU_CURL_PATH.
  230. # End of config
  231. if [ -n "$JIRAFEAU_PROXY" ]; then
  232. proxy="$JIRAFEAU_PROXY"
  233. fi
  234. if [ -n "$JIRAFEAU_URL" ]; then
  235. url="$JIRAFEAU_URL"
  236. fi
  237. if [ -z "$url" ]; then
  238. echo "Please set url in script parameters or export JIRAFEAU_URL"
  239. fi
  240. if [ -n "$JIRAFEAU_TIME" ]; then
  241. time="$JIRAFEAU_TIME"
  242. fi
  243. if [ -n "$JIRAFEAU_ONE_TIME" ]; then
  244. one_time='1'
  245. fi
  246. if [ -z "$curl" ]; then
  247. curl="$JIRAFEAU_CURL_PATH"
  248. fi
  249. if [ -z "$curl" ] && [ -e "/usr/bin/curl" ]; then
  250. curl="/usr/bin/curl"
  251. fi
  252. if [ -z "$curl" ] && [ -e "/bin/curl.exe" ]; then
  253. curl="/bin/curl.exe"
  254. fi
  255. if [ -z "$curl" ]; then
  256. echo "Please set your curl binary path (by editing this script or export JIRAFEAU_CURL_PATH global variable)."
  257. exit
  258. fi
  259. if [ -z "$2" ]; then
  260. echo "man:"
  261. echo " $0 send PATH [PASSWORD]"
  262. echo " $0 get URL [PASSWORD]"
  263. echo " $0 delete URL"
  264. echo ""
  265. echo "Global variables to export:"
  266. echo " JIRAFEAU_PROXY : example: proxysever.test.com:3128"
  267. echo " JIRAFEAU_URL : example: http://mysite/jirafeau/script.php"
  268. echo " JIRAFEAU_TIME : minute, hour, day, week, year, month or none"
  269. echo " JIRAFEAU_ONE_TIME : set anything or set empty"
  270. echo " JIRAFEAU_CURL : path to your curl binary"
  271. exit 0
  272. fi
  273. if [ -n "$proxy" ]; then
  274. proxy="-x $proxy"
  275. fi
  276. options=''
  277. if [ -n "$one_time" ]; then
  278. options="$options -F one_time_download=1"
  279. fi
  280. password=''
  281. if [ -n "$3" ]; then
  282. password="$3"
  283. options="$options -F key=$password"
  284. fi
  285. if [ "$1" == "send" ]; then
  286. if [ ! -f "$2" ]; then
  287. echo "File \"$2\" does not exists."
  288. exit
  289. fi
  290. # Ret result
  291. res=$($curl -X POST --http1.0 $proxy $options \
  292. -F "time=$time" \
  293. -F "file=@$2" \
  294. $url)
  295. if [[ "$res" == Error* ]]; then
  296. echo "Error while uploading."
  297. echo $res
  298. exit
  299. fi
  300. # Not using head or tail to minimise command dependencies
  301. code=$(cnt=0; echo "$res" | while read l; do
  302. if [[ "$cnt" == "0" ]]; then
  303. echo "$l"
  304. fi
  305. cnt=$(( cnt + 1 ))
  306. done)
  307. del_code=$(cnt=0; echo "$res" | while read l; do
  308. if [[ "$cnt" == "1" ]]; then
  309. echo "$l"
  310. fi
  311. cnt=$(( cnt + 1 ))
  312. done)
  313. echo "Download link:"
  314. echo "${url}?h=$code"
  315. echo "Direct download link:"
  316. echo "${url}?h=$code&d=1"
  317. echo "Delete link:"
  318. echo "${url}?h=$code&d=$del_code"
  319. elif [ "$1" == "get" ]; then
  320. if [ -z "$password" ]; then
  321. $curl $proxy -OJ "$2"
  322. else
  323. $curl $proxy -OJ -X POST -F key=$password "$2"
  324. fi
  325. elif [ "$1" == "delete" ]; then
  326. $curl $proxy "$2"
  327. fi
  328. <?php
  329. }
  330. else
  331. {
  332. echo 'Error 12';
  333. exit;
  334. }
  335. }
  336. /* Create alias. */
  337. elseif (isset ($_GET['alias_create']))
  338. {
  339. $ip = get_ip_address($cfg);
  340. if (!jirafeau_challenge_upload_ip ($cfg, $ip))
  341. {
  342. echo 'Error 13';
  343. exit;
  344. }
  345. if (jirafeau_has_upload_password ($cfg) &&
  346. (!isset ($_POST['upload_password']) ||
  347. !jirafeau_challenge_upload_password ($cfg, $_POST['upload_password'])))
  348. {
  349. echo 'Error 14';
  350. exit;
  351. }
  352. if (!isset ($_POST['alias']) ||
  353. !isset ($_POST['destination']) ||
  354. !isset ($_POST['password']))
  355. {
  356. echo 'Error 15';
  357. exit;
  358. }
  359. echo jirafeau_alias_create ($_POST['alias'],
  360. $_POST['destination'],
  361. $_POST['password'],
  362. $ip);
  363. }
  364. /* Get alias. */
  365. elseif (isset ($_GET['alias_get']))
  366. {
  367. if (!isset ($_POST['alias']))
  368. {
  369. echo 'Error 16';
  370. exit;
  371. }
  372. echo jirafeau_alias_get ($_POST['alias']);
  373. }
  374. /* Update alias. */
  375. elseif (isset ($_GET['alias_update']))
  376. {
  377. if (!isset ($_POST['alias']) ||
  378. !isset ($_POST['destination']) ||
  379. !isset ($_POST['password']))
  380. {
  381. echo 'Error 17';
  382. exit;
  383. }
  384. $new_password = '';
  385. if (isset ($_POST['new_password']))
  386. $new_password = $_POST['new_password'];
  387. echo jirafeau_alias_update ($_POST['alias'],
  388. $_POST['destination'],
  389. $_POST['password'],
  390. $new_password,
  391. get_ip_address($cfg));
  392. }
  393. /* Delete alias. */
  394. elseif (isset ($_GET['alias_delete']))
  395. {
  396. if (!isset ($_POST['alias']) ||
  397. !isset ($_POST['password']))
  398. {
  399. echo 'Error 18';
  400. exit;
  401. }
  402. echo jirafeau_alias_delete ($_POST['alias'],
  403. $_POST['password']);
  404. }
  405. /* Initialize an asynchronous upload. */
  406. elseif (isset ($_GET['init_async']))
  407. {
  408. if (!jirafeau_challenge_upload_ip ($cfg, get_ip_address($cfg)))
  409. {
  410. echo 'Error 19';
  411. exit;
  412. }
  413. if (jirafeau_has_upload_password ($cfg) &&
  414. (!isset ($_POST['upload_password']) ||
  415. !jirafeau_challenge_upload_password ($cfg, $_POST['upload_password'])))
  416. {
  417. echo 'Error 20';
  418. exit;
  419. }
  420. if (!isset ($_POST['filename']))
  421. {
  422. echo 'Error 21';
  423. exit;
  424. }
  425. $type = '';
  426. if (isset ($_POST['type']))
  427. $type = $_POST['type'];
  428. $key = '';
  429. if (isset ($_POST['key']))
  430. $key = $_POST['key'];
  431. $time = time ();
  432. if (!isset ($_POST['time']) || !$cfg['availabilities'][$_POST['time']])
  433. {
  434. echo 'Error 22';
  435. exit;
  436. }
  437. else
  438. switch ($_POST['time'])
  439. {
  440. case 'minute':
  441. $time += JIRAFEAU_MINUTE;
  442. break;
  443. case 'hour':
  444. $time += JIRAFEAU_HOUR;
  445. break;
  446. case 'day':
  447. $time += JIRAFEAU_DAY;
  448. break;
  449. case 'week':
  450. $time += JIRAFEAU_WEEK;
  451. break;
  452. case 'month':
  453. $time += JIRAFEAU_MONTH;
  454. break;
  455. case 'year':
  456. $time += JIRAFEAU_YEAR;
  457. break;
  458. default:
  459. $time = JIRAFEAU_INFINITY;
  460. break;
  461. }
  462. echo jirafeau_async_init ($_POST['filename'],
  463. $type,
  464. isset ($_POST['one_time_download']),
  465. $key,
  466. $time,
  467. get_ip_address($cfg));
  468. }
  469. /* Continue an asynchronous upload. */
  470. elseif (isset ($_GET['push_async']))
  471. {
  472. if ((!isset ($_POST['ref']))
  473. || (!isset ($_FILES['data']))
  474. || (!isset ($_POST['code'])))
  475. echo 'Error 23';
  476. else
  477. {
  478. echo jirafeau_async_push ($_POST['ref'],
  479. $_FILES['data'],
  480. $_POST['code'],
  481. $cfg['maximal_upload_size']);
  482. }
  483. }
  484. /* Finalize an asynchronous upload. */
  485. elseif (isset ($_GET['end_async']))
  486. {
  487. if (!isset ($_POST['ref'])
  488. || !isset ($_POST['code']))
  489. echo 'Error 24';
  490. else
  491. echo jirafeau_async_end ($_POST['ref'], $_POST['code'], $cfg['enable_crypt'], $cfg['link_name_length']);
  492. }
  493. else
  494. echo 'Error 25';
  495. exit;
  496. ?>