PageRenderTime 29ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/admin.php

https://gitlab.com/Poorchop/Jirafeau
PHP | 304 lines | 259 code | 13 blank | 32 comment | 43 complexity | b374a213b5103e6cc507a662b764ae37 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 <http://www.gnu.org/licenses/>.
  18. */
  19. define ('JIRAFEAU_ROOT', dirname (__FILE__) . '/');
  20. require (JIRAFEAU_ROOT . 'lib/config.original.php');
  21. require (JIRAFEAU_ROOT . 'lib/settings.php');
  22. require (JIRAFEAU_ROOT . 'lib/functions.php');
  23. require (JIRAFEAU_ROOT . 'lib/lang.php');
  24. /* Check if installation is OK. */
  25. if (file_exists (JIRAFEAU_ROOT . 'install.php')
  26. && !file_exists (JIRAFEAU_ROOT . 'lib/config.local.php'))
  27. {
  28. header('Location: install.php');
  29. exit;
  30. }
  31. /* If called from CLI, no password or graphical interface */
  32. if (php_sapi_name() == "cli") {
  33. if ((count($argv)>1) && $argv[1]=="clean_expired") {
  34. $total = jirafeau_admin_clean ();
  35. echo "$total expired files deleted.";
  36. }
  37. elseif ((count($argv)>1) && $argv[1]=="clean_async") {
  38. $total = jirafeau_admin_clean_async ();
  39. echo "$total old unfinished transfers deleted.";
  40. }
  41. else
  42. {
  43. die("No command found. Should be admin.php <clean_expired|clean_async>.");
  44. }
  45. }
  46. else
  47. {
  48. /* Disable admin interface if we have a empty admin password. */
  49. if (empty($cfg['admin_password']) && empty($cfg['admin_http_auth_user']))
  50. {
  51. require (JIRAFEAU_ROOT . 'lib/template/header.php');
  52. echo '<div class="error"><p>'.
  53. t('Sorry, the admin interface is not enabled.') .
  54. '</p></div>';
  55. require (JIRAFEAU_ROOT.'lib/template/footer.php');
  56. exit;
  57. }
  58. /* Check session. */
  59. session_start();
  60. /* Unlog if asked. */
  61. if (isset ($_POST['action']) && (strcmp ($_POST['action'], 'logout') == 0))
  62. $_SESSION['admin_auth'] = false;
  63. /* Check classic admin password authentification. */
  64. if (isset ($_POST['admin_password']) && empty($cfg['admin_http_auth_user']))
  65. {
  66. if ($cfg['admin_password'] === $_POST['admin_password'])
  67. $_SESSION['admin_auth'] = true;
  68. else
  69. {
  70. $_SESSION['admin_auth'] = false;
  71. require (JIRAFEAU_ROOT . 'lib/template/header.php');
  72. echo '<div class="error"><p>'.
  73. t('Wrong password.') . '</p></div>';
  74. require (JIRAFEAU_ROOT.'lib/template/footer.php');
  75. exit;
  76. }
  77. }
  78. /* Ask for classic admin password authentification. */
  79. elseif ((!isset ($_SESSION['admin_auth']) || $_SESSION['admin_auth'] != true)
  80. && empty($cfg['admin_http_auth_user']))
  81. {
  82. require (JIRAFEAU_ROOT . 'lib/template/header.php'); ?>
  83. <form action = "<?php echo basename(__FILE__); ?>" method = "post">
  84. <fieldset>
  85. <table>
  86. <tr>
  87. <td class = "label"><label for = "enter_password">
  88. <?php echo t('Administration password') . ':';?></label>
  89. </td>
  90. <td class = "field"><input type = "password"
  91. name = "admin_password" id = "admin_password"
  92. size = "40" />
  93. </td>
  94. </tr>
  95. <tr class = "nav">
  96. <td></td>
  97. <td class = "nav next">
  98. <input type = "submit" name = "key" value =
  99. "<?php echo t('Login'); ?>" />
  100. </td>
  101. </tr>
  102. </table>
  103. </fieldset>
  104. </form>
  105. <?php
  106. require (JIRAFEAU_ROOT.'lib/template/footer.php');
  107. exit;
  108. }
  109. /* Check authenticated user if HTTP authentification is enable. */
  110. elseif ((!isset ($_SESSION['admin_auth']) || $_SESSION['admin_auth'] != true)
  111. && !empty($cfg['admin_http_auth_user']))
  112. {
  113. if ($cfg['admin_http_auth_user'] == $_SERVER['PHP_AUTH_USER'])
  114. $_SESSION['admin_auth'] = true;
  115. }
  116. /* Be sure that no one can access further without admin_auth. */
  117. if (!isset ($_SESSION['admin_auth']) || $_SESSION['admin_auth'] != true)
  118. {
  119. $_SESSION['admin_auth'] = false;
  120. require (JIRAFEAU_ROOT . 'lib/template/header.php');
  121. echo '<div class="error"><p>'.
  122. t('Sorry, you are not authenticated on admin interface.') .
  123. '</p></div>';
  124. require (JIRAFEAU_ROOT.'lib/template/footer.php');
  125. exit;
  126. }
  127. /* Operations may take a long time.
  128. * Be sure PHP's safe mode is off.
  129. */
  130. @set_time_limit(0);
  131. /* Remove errors. */
  132. @error_reporting(0);
  133. /* Show admin interface if not downloading a file. */
  134. if (!(isset ($_POST['action']) && strcmp ($_POST['action'], 'download') == 0))
  135. {
  136. require (JIRAFEAU_ROOT . 'lib/template/header.php');
  137. ?><h2><?php echo t('Admin interface'); ?></h2><?php
  138. ?><h2>(version <?php echo JIRAFEAU_VERSION ?>)</h2><?php
  139. ?><div id = "admin">
  140. <fieldset><legend><?php echo t('Actions');?></legend>
  141. <table>
  142. <form action = "<?php echo basename(__FILE__); ?>" method = "post">
  143. <tr>
  144. <input type = "hidden" name = "action" value = "clean"/>
  145. <td class = "info">
  146. <?php echo t('Clean expired files'); ?>
  147. </td>
  148. <td></td>
  149. <td>
  150. <input type = "submit" value = "<?php echo t('Clean'); ?>" />
  151. </td>
  152. </tr>
  153. </form>
  154. <form action = "<?php echo basename(__FILE__); ?>" method = "post">
  155. <tr>
  156. <input type = "hidden" name = "action" value = "clean_async"/>
  157. <td class = "info">
  158. <?php echo t('Clean old unfinished transfers'); ?>
  159. </td>
  160. <td></td>
  161. <td>
  162. <input type = "submit" value = "<?php echo t('Clean'); ?>" />
  163. </td>
  164. </tr>
  165. </form>
  166. <form action = "<?php echo basename(__FILE__); ?>" method = "post">
  167. <tr>
  168. <input type = "hidden" name = "action" value = "list"/>
  169. <td class = "info">
  170. <?php echo t('List all files'); ?>
  171. </td>
  172. <td></td>
  173. <td>
  174. <input type = "submit" value = "<?php echo t('List'); ?>" />
  175. </td>
  176. </tr>
  177. </form>
  178. <form action = "<?php echo basename(__FILE__); ?>" method = "post">
  179. <tr>
  180. <input type = "hidden" name = "action" value = "search_by_name"/>
  181. <td class = "info">
  182. <?php echo t('Search files by name'); ?>
  183. </td>
  184. <td>
  185. <input type = "text" name = "name" id = "name"/>
  186. </td>
  187. <td>
  188. <input type = "submit" value = "<?php echo t('Search'); ?>" />
  189. </td>
  190. </tr>
  191. </form>
  192. <form action = "<?php echo basename(__FILE__); ?>" method = "post">
  193. <tr>
  194. <input type = "hidden" name = "action" value = "search_by_file_hash"/>
  195. <td class = "info">
  196. <?php echo t('Search files by file hash'); ?>
  197. </td>
  198. <td>
  199. <input type = "text" name = "hash" id = "hash"/>
  200. </td>
  201. <td>
  202. <input type = "submit" value = "<?php echo t('Search'); ?>" />
  203. </td>
  204. </tr>
  205. </form>
  206. <form action = "<?php echo basename(__FILE__); ?>" method = "post">
  207. <tr>
  208. <input type = "hidden" name = "action" value = "search_link"/>
  209. <td class = "info">
  210. <?php echo t('Search a specific link'); ?>
  211. </td>
  212. <td>
  213. <input type = "text" name = "link" id = "link"/>
  214. </td>
  215. <td>
  216. <input type = "submit" value = "<?php echo t('Search'); ?>" />
  217. </td>
  218. </tr>
  219. </form>
  220. </table>
  221. <form action = "<?php echo basename(__FILE__); ?>" method = "post">
  222. <input type = "hidden" name = "action" value = "logout" />
  223. <input type = "submit" value = "<?php echo t('Logout'); ?>" />
  224. </form>
  225. </fieldset></div><?php
  226. }
  227. /* Check for actions */
  228. if (isset ($_POST['action']))
  229. {
  230. if (strcmp ($_POST['action'], 'clean') == 0)
  231. {
  232. $total = jirafeau_admin_clean ();
  233. echo '<div class="message">' . NL;
  234. echo '<p>';
  235. echo t('Number of cleaned files') . ' : ' . $total;
  236. echo '</p></div>';
  237. }
  238. elseif (strcmp ($_POST['action'], 'clean_async') == 0)
  239. {
  240. $total = jirafeau_admin_clean_async ();
  241. echo '<div class="message">' . NL;
  242. echo '<p>';
  243. echo t('Number of cleaned files') . ' : ' . $total;
  244. echo '</p></div>';
  245. }
  246. elseif (strcmp ($_POST['action'], 'list') == 0)
  247. {
  248. jirafeau_admin_list ("", "", "");
  249. }
  250. elseif (strcmp ($_POST['action'], 'search_by_name') == 0)
  251. {
  252. jirafeau_admin_list ($_POST['name'], "", "");
  253. }
  254. elseif (strcmp ($_POST['action'], 'search_by_file_hash') == 0)
  255. {
  256. jirafeau_admin_list ("", $_POST['hash'], "");
  257. }
  258. elseif (strcmp ($_POST['action'], 'search_link') == 0)
  259. {
  260. jirafeau_admin_list ("", "", $_POST['link']);
  261. }
  262. elseif (strcmp ($_POST['action'], 'delete_link') == 0)
  263. {
  264. jirafeau_delete_link ($_POST['link']);
  265. echo '<div class="message">' . NL;
  266. echo '<p>' . t('Link deleted') . '</p></div>';
  267. }
  268. elseif (strcmp ($_POST['action'], 'delete_file') == 0)
  269. {
  270. $count = jirafeau_delete_file ($_POST['md5']);
  271. echo '<div class="message">' . NL;
  272. echo '<p>' . t('Deleted links') . ' : ' . $count . '</p></div>';
  273. }
  274. elseif (strcmp ($_POST['action'], 'download') == 0)
  275. {
  276. $l = jirafeau_get_link ($_POST['link']);
  277. if (!count ($l))
  278. return;
  279. $p = s2p ($l['md5']);
  280. header ('Content-Length: ' . $l['file_size']);
  281. header ('Content-Type: ' . $l['mime_type']);
  282. header ('Content-Disposition: attachment; filename="' .
  283. $l['file_name'] . '"');
  284. if (file_exists(VAR_FILES . $p . $l['md5']))
  285. readfile (VAR_FILES . $p . $l['md5']);
  286. exit;
  287. }
  288. }
  289. require (JIRAFEAU_ROOT.'lib/template/footer.php');
  290. }
  291. ?>