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

patrick-canterino.de