PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/admin.php

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