]> git.p6c8.net - jirafeau.git/blob - admin.php
Show a warning in admin interface if Sodium is not available
[jirafeau.git] / admin.php
1 <?php
2 /*
3 * Jirafeau, your web file repository
4 * Copyright (C) 2015 Jerome Jutteau <jerome@jutteau.fr>
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 session_start();
20 define('JIRAFEAU_ROOT', dirname(__FILE__) . '/');
21
22 require(JIRAFEAU_ROOT . 'lib/settings.php');
23 require(JIRAFEAU_ROOT . 'lib/functions.php');
24 require(JIRAFEAU_ROOT . 'lib/lang.php');
25
26 /* Check if installation is OK. */
27 if (file_exists(JIRAFEAU_ROOT . 'install.php')
28 && !file_exists(JIRAFEAU_ROOT . 'lib/config.local.php')) {
29 header('Location: install.php');
30 exit;
31 }
32
33 /* If called from CLI, no password or graphical interface */
34 if (php_sapi_name() == "cli") {
35 if ($cfg['installation_done'] == false) {
36 die("Installation not completed yet.\n");
37 }
38 if ((count($argv)>1) && $argv[1]=="clean_expired") {
39 $total = jirafeau_admin_clean();
40 echo "$total expired files deleted.\n";
41 } elseif ((count($argv)>1) && $argv[1]=="clean_async") {
42 $total = jirafeau_admin_clean_async();
43 echo "$total old unfinished transfers deleted.\n";
44 } else {
45 die("No command found. Should be admin.php <clean_expired|clean_async>.\n");
46 }
47 // Second check: Challenge by IP
48 } elseif (true === jirafeau_challenge_admin_ip($cfg, get_ip_address($cfg))) {
49 /* Disable admin interface if we have a empty admin password. */
50 if (empty($cfg['admin_password']) && empty($cfg['admin_http_auth_user'])) {
51 require(JIRAFEAU_ROOT . 'lib/template/header.php');
52 echo '<div class="error"><p>'.
53 t('NO_ADMIN') .
54 '</p></div>';
55 require(JIRAFEAU_ROOT.'lib/template/footer.php');
56 exit;
57 }
58
59 /* Logout if requested. */
60 if (jirafeau_admin_session_logged() && isset($_POST['action']) && (strcmp($_POST['action'], 'logout') == 0)) {
61 jirafeau_session_end();
62 }
63
64 if (!jirafeau_admin_session_logged()) {
65 /* Test HTTP authentication. */
66 if (!empty($cfg['admin_http_auth_user']) &&
67 $cfg['admin_http_auth_user'] == $_SERVER['PHP_AUTH_USER']) {
68 jirafeau_admin_session_start();
69 }
70 /* Test web password authentication. */
71 elseif (!empty($cfg['admin_password']) && isset($_POST['admin_password'])) {
72 if ($cfg['admin_password'] === hash('sha256', $_POST['admin_password'])) {
73 jirafeau_admin_session_start();
74 } else {
75 require(JIRAFEAU_ROOT . 'lib/template/header.php');
76 echo '<div class="error"><p>'. t('BAD_PSW') . '</p></div>';
77 require(JIRAFEAU_ROOT.'lib/template/footer.php');
78 exit;
79 }
80 }
81 /* Admin password prompt form. */
82 else {
83 require(JIRAFEAU_ROOT . 'lib/template/header.php'); ?>
84 <form method="post" class="form login">
85 <fieldset>
86 <table>
87 <tr>
88 <td class = "label"><label for = "enter_password">
89 <?php echo t('ADMIN_PSW') . ':'; ?></label>
90 </td>
91 </tr>
92 <tr>
93 <td class = "field"><input type = "password"
94 name = "admin_password" id = "admin_password"
95 size = "40" autocomplete = "current-password" />
96 </td>
97 </tr>
98 <tr class = "nav">
99 <td class = "nav next">
100 <input type = "submit" name = "key" value =
101 "<?php echo t('LOGIN'); ?>" />
102 </td>
103 </tr>
104 </table>
105 </fieldset>
106 </form>
107 <?php
108 require(JIRAFEAU_ROOT.'lib/template/footer.php');
109 exit;
110 }
111 }
112
113 /* Operations may take a long time.
114 * Be sure PHP's safe mode is off.
115 */
116 @set_time_limit(0);
117
118 /* Show admin interface if not downloading a file. */
119 if (!(isset($_POST['action']) && strcmp($_POST['action'], 'download') == 0)) {
120 require(JIRAFEAU_ROOT . 'lib/template/header.php'); ?><h2><?php echo t('ADMIN_INTERFACE'); ?></h2><?php
121 ?><h2>(version <?php echo JIRAFEAU_VERSION ?>)</h2><?php
122
123 if ($cfg['enable_crypt'] && !(extension_loaded('sodium'))) {
124 echo '<div class="error"><p>'.t('SODIUM_UNAVAILABLE').'</p></div>';
125 }
126
127 ?><div id = "admin">
128 <fieldset><legend><?php echo t('ACTIONS'); ?></legend>
129 <table>
130 <form method="post">
131 <tr>
132 <input type = "hidden" name = "action" value = "clean"/>
133 <?php echo jirafeau_admin_csrf_field() ?>
134 <td class = "info">
135 <?php echo t('CLEAN_EXPIRED'); ?>
136 </td>
137 <td></td>
138 <td>
139 <input type = "submit" value = "<?php echo t('CLEAN'); ?>" />
140 </td>
141 </tr>
142 </form>
143 <form method="post">
144 <tr>
145 <input type = "hidden" name = "action" value = "clean_async"/>
146 <?php echo jirafeau_admin_csrf_field() ?>
147 <td class = "info">
148 <?php echo t('CLEAN_INCOMPLETE'); ?>
149 </td>
150 <td></td>
151 <td>
152 <input type = "submit" value = "<?php echo t('CLEAN'); ?>" />
153 </td>
154 </tr>
155 </form>
156 <form method="post">
157 <tr>
158 <input type = "hidden" name = "action" value = "list"/>
159 <?php echo jirafeau_admin_csrf_field() ?>
160 <td class = "info">
161 <?php echo t('LS_FILES'); ?>
162 </td>
163 <td></td>
164 <td>
165 <input type = "submit" value = "<?php echo t('LIST'); ?>" />
166 </td>
167 </tr>
168 </form>
169 <form method="post">
170 <tr>
171 <input type = "hidden" name = "action" value = "size"/>
172 <?php echo jirafeau_admin_csrf_field() ?>
173 <td class = "info">
174 <?php echo t('SIZE_DATA'); ?>
175 </td>
176 <td></td>
177 <td>
178 <input type = "submit" value = "<?php echo t('SIZE'); ?>" />
179 </td>
180 </tr>
181 </form>
182 <form method="post">
183 <tr>
184 <input type = "hidden" name = "action" value = "search_by_name"/>
185 <?php echo jirafeau_admin_csrf_field() ?>
186 <td class = "info">
187 <?php echo t('SEARCH_NAME'); ?>
188 </td>
189 <td>
190 <input type = "text" name = "name" id = "name"/>
191 </td>
192 <td>
193 <input type = "submit" value = "<?php echo t('SEARCH'); ?>" />
194 </td>
195 </tr>
196 </form>
197 <form method="post">
198 <tr>
199 <input type = "hidden" name = "action" value = "search_by_file_hash"/>
200 <?php echo jirafeau_admin_csrf_field() ?>
201 <td class = "info">
202 <?php echo t('SEARH_BY_HASH'); ?>
203 </td>
204 <td>
205 <input type = "text" name = "hash" id = "hash"/>
206 </td>
207 <td>
208 <input type = "submit" value = "<?php echo t('SEARCH'); ?>" />
209 </td>
210 </tr>
211 </form>
212 <form method="post">
213 <tr>
214 <input type = "hidden" name = "action" value = "search_link"/>
215 <?php echo jirafeau_admin_csrf_field() ?>
216 <td class = "info">
217 <?php echo t('SEARCH_LINK'); ?>
218 </td>
219 <td>
220 <input type = "text" name = "link" id = "link"/>
221 </td>
222 <td>
223 <input type = "submit" value = "<?php echo t('SEARCH'); ?>" />
224 </td>
225 </tr>
226 </form>
227 <form method="post">
228 <tr>
229 <input type = "hidden" name = "action" value = "bug_report_info"/>
230 <?php echo jirafeau_admin_csrf_field() ?>
231 <td class = "info">
232 <?php echo t('REPORTING_AN_ISSUE'); ?>
233 </td>
234 <td></td>
235 <td>
236 <input type = "submit" value = "<?php echo t('INFO'); ?>" />
237 </td>
238 </tr>
239 </form>
240 </table>
241 <form method="post">
242 <input type = "hidden" name = "action" value = "logout" />
243 <?php echo jirafeau_admin_csrf_field() ?>
244 <input type = "submit" value = "<?php echo t('LOGOUT'); ?>" />
245 </form>
246 </fieldset></div><?php
247 }
248
249 /* Check for actions */
250 if (isset($_POST['action'])) {
251 if (strcmp($_POST['action'], 'clean') == 0) {
252 $total = jirafeau_admin_clean();
253 echo '<div class="message">' . NL;
254 echo '<p>';
255 echo t('CLEANED_FILES_CNT') . ' : ' . $total;
256 echo '</p></div>';
257 } elseif (strcmp($_POST['action'], 'clean_async') == 0) {
258 $total = jirafeau_admin_clean_async();
259 echo '<div class="message">' . NL;
260 echo '<p>';
261 echo t('CLEANED_FILES_CNT') . ' : ' . $total;
262 echo '</p></div>';
263 } elseif (strcmp($_POST['action'], 'size') == 0) {
264 $size = jirafeau_dir_size($cfg['var_root']);
265 $human_size = jirafeau_human_size($size);
266 echo '<div class="message">' . NL;
267 echo '<p>' . t('SIZE_DATA') . ': ' . $human_size .'</p>';
268 echo '</div>';
269 } elseif (strcmp($_POST['action'], 'list') == 0) {
270 jirafeau_admin_list("", "", "");
271 } elseif (strcmp($_POST['action'], 'search_by_name') == 0) {
272 jirafeau_admin_list($_POST['name'], "", "");
273 } elseif (strcmp($_POST['action'], 'search_by_file_hash') == 0) {
274 jirafeau_admin_list("", $_POST['hash'], "");
275 } elseif (strcmp($_POST['action'], 'search_link') == 0) {
276 jirafeau_admin_list("", "", $_POST['link']);
277 } elseif (strcmp($_POST['action'], 'delete_link') == 0) {
278 jirafeau_delete_link($_POST['link']);
279 echo '<div class="message">' . NL;
280 echo '<p>' . t('LINK_DELETED') . '</p></div>';
281 } elseif (strcmp($_POST['action'], 'delete_file') == 0) {
282 $count = jirafeau_delete_file($_POST['hash']);
283 echo '<div class="message">' . NL;
284 echo '<p>' . t('DELETED_LINKS') . ' : ' . $count . '</p></div>';
285 } elseif (strcmp($_POST['action'], 'download') == 0) {
286 $l = jirafeau_get_link($_POST['link']);
287 if (!count($l)) {
288 return;
289 }
290 $p = s2p($l['hash']);
291 header('Content-Length: ' . $l['file_size']);
292 header('Content-Type: ' . $l['mime_type']);
293 header('Content-Disposition: attachment; filename="' .
294 $l['file_name'] . '"');
295 if (file_exists(VAR_FILES . $p . $l['hash'])) {
296 $r = fopen(VAR_FILES . $p . $l['hash'], 'r');
297 while (!feof($r)) {
298 print fread($r, 1024);
299 }
300 fclose($r);
301 }
302 exit;
303 } elseif (strcmp($_POST['action'], 'bug_report_info') == 0) {
304 echo jirafeau_admin_bug_report($cfg);
305 }
306 }
307
308 require(JIRAFEAU_ROOT.'lib/template/footer.php');
309 } else {
310 require(JIRAFEAU_ROOT . 'lib/template/header.php');
311 jirafeau_fatal_error(t('ACCESS_KO'), $cfg);
312 }
313 ?>

patrick-canterino.de