]> git.p6c8.net - jirafeau_mojo42.git/blob - admin.php
cfdfe3010dc26a12fab2b644329aa33b585eb7a4
[jirafeau_mojo42.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 ((count($argv)>1) && $argv[1]=="clean_expired") {
36 $total = jirafeau_admin_clean();
37 echo "$total expired files deleted.";
38 } elseif ((count($argv)>1) && $argv[1]=="clean_async") {
39 $total = jirafeau_admin_clean_async();
40 echo "$total old unfinished transfers deleted.";
41 } else {
42 die("No command found. Should be admin.php <clean_expired|clean_async>.");
43 }
44 } else {
45 /* Disable admin interface if we have a empty admin password. */
46 if (empty($cfg['admin_password']) && empty($cfg['admin_http_auth_user'])) {
47 require(JIRAFEAU_ROOT . 'lib/template/header.php');
48 echo '<div class="error"><p>'.
49 t('NO_ADMIN') .
50 '</p></div>';
51 require(JIRAFEAU_ROOT.'lib/template/footer.php');
52 exit;
53 }
54
55 /* Unlog if asked. */
56 if (jirafeau_admin_session_logged() && isset($_POST['action']) && (strcmp($_POST['action'], 'logout') == 0)) {
57 jirafeau_admin_session_end();
58 }
59
60 if (!jirafeau_admin_session_logged()) {
61 /* Test HTTP authentification. */
62 if (!empty($cfg['admin_http_auth_user']) &&
63 $cfg['admin_http_auth_user'] == $_SERVER['PHP_AUTH_USER']) {
64 jirafeau_admin_session_start();
65 }
66 /* Test web password authentification. */
67 elseif (!empty($cfg['admin_password']) && isset($_POST['admin_password'])) {
68 if ($cfg['admin_password'] === hash('sha256', $_POST['admin_password'])) {
69 jirafeau_admin_session_start();
70 } else {
71 require(JIRAFEAU_ROOT . 'lib/template/header.php');
72 echo '<div class="error"><p>'. t('BAD_PSW') . '</p></div>';
73 require(JIRAFEAU_ROOT.'lib/template/footer.php');
74 exit;
75 }
76 }
77 /* Admin password prompt form. */
78 else {
79 require(JIRAFEAU_ROOT . 'lib/template/header.php'); ?>
80 <form method="post" class="form login">
81 <fieldset>
82 <table>
83 <tr>
84 <td class = "label"><label for = "enter_password">
85 <?php echo t('ADMIN_PSW') . ':'; ?></label>
86 </td>
87 </tr>
88 <tr>
89 <td class = "field"><input type = "password"
90 name = "admin_password" id = "admin_password"
91 size = "40" />
92 </td>
93 </tr>
94 <tr class = "nav">
95 <td class = "nav next">
96 <input type = "submit" name = "key" value =
97 "<?php echo t('LOGIN'); ?>" />
98 </td>
99 </tr>
100 </table>
101 </fieldset>
102 </form>
103 <?php
104 require(JIRAFEAU_ROOT.'lib/template/footer.php');
105 exit;
106 }
107 }
108
109 /* Operations may take a long time.
110 * Be sure PHP's safe mode is off.
111 */
112 @set_time_limit(0);
113
114 /* Show admin interface if not downloading a file. */
115 if (!(isset($_POST['action']) && strcmp($_POST['action'], 'download') == 0)) {
116 require(JIRAFEAU_ROOT . 'lib/template/header.php'); ?><h2><?php echo t('ADMIN_INTERFACE'); ?></h2><?php
117 ?><h2>(version <?php echo JIRAFEAU_VERSION ?>)</h2><?php
118
119 ?><div id = "admin">
120 <fieldset><legend><?php echo t('ACTIONS'); ?></legend>
121 <table>
122 <form method="post">
123 <tr>
124 <input type = "hidden" name = "action" value = "clean"/>
125 <?php echo jirafeau_admin_csrf_field() ?>
126 <td class = "info">
127 <?php echo t('CLEAN_EXPIRED'); ?>
128 </td>
129 <td></td>
130 <td>
131 <input type = "submit" value = "<?php echo t('CLEAN'); ?>" />
132 </td>
133 </tr>
134 </form>
135 <form method="post">
136 <tr>
137 <input type = "hidden" name = "action" value = "clean_async"/>
138 <?php echo jirafeau_admin_csrf_field() ?>
139 <td class = "info">
140 <?php echo t('CLEAN_INCOMPLETE'); ?>
141 </td>
142 <td></td>
143 <td>
144 <input type = "submit" value = "<?php echo t('CLEAN'); ?>" />
145 </td>
146 </tr>
147 </form>
148 <form method="post">
149 <tr>
150 <input type = "hidden" name = "action" value = "list"/>
151 <?php echo jirafeau_admin_csrf_field() ?>
152 <td class = "info">
153 <?php echo t('LS_FILES'); ?>
154 </td>
155 <td></td>
156 <td>
157 <input type = "submit" value = "<?php echo t('LIST'); ?>" />
158 </td>
159 </tr>
160 </form>
161 <form method="post">
162 <tr>
163 <input type = "hidden" name = "action" value = "size"/>
164 <?php echo jirafeau_admin_csrf_field() ?>
165 <td class = "info">
166 <?php echo t('SIZE_DATA'); ?>
167 </td>
168 <td></td>
169 <td>
170 <input type = "submit" value = "<?php echo t('SIZE'); ?>" />
171 </td>
172 </tr>
173 </form>
174 <form method="post">
175 <tr>
176 <input type = "hidden" name = "action" value = "search_by_name"/>
177 <?php echo jirafeau_admin_csrf_field() ?>
178 <td class = "info">
179 <?php echo t('SEARCH_NAME'); ?>
180 </td>
181 <td>
182 <input type = "text" name = "name" id = "name"/>
183 </td>
184 <td>
185 <input type = "submit" value = "<?php echo t('SEARCH'); ?>" />
186 </td>
187 </tr>
188 </form>
189 <form method="post">
190 <tr>
191 <input type = "hidden" name = "action" value = "search_by_file_hash"/>
192 <?php echo jirafeau_admin_csrf_field() ?>
193 <td class = "info">
194 <?php echo t('SEARH_BY_HASH'); ?>
195 </td>
196 <td>
197 <input type = "text" name = "hash" id = "hash"/>
198 </td>
199 <td>
200 <input type = "submit" value = "<?php echo t('SEARCH'); ?>" />
201 </td>
202 </tr>
203 </form>
204 <form method="post">
205 <tr>
206 <input type = "hidden" name = "action" value = "search_link"/>
207 <?php echo jirafeau_admin_csrf_field() ?>
208 <td class = "info">
209 <?php echo t('SEARCH_LINK'); ?>
210 </td>
211 <td>
212 <input type = "text" name = "link" id = "link"/>
213 </td>
214 <td>
215 <input type = "submit" value = "<?php echo t('SEARCH'); ?>" />
216 </td>
217 </tr>
218 </form>
219 </table>
220 <form method="post">
221 <input type = "hidden" name = "action" value = "logout" />
222 <?php echo jirafeau_admin_csrf_field() ?>
223 <input type = "submit" value = "<?php echo t('LOGOUT'); ?>" />
224 </form>
225 </fieldset></div><?php
226 }
227
228 /* Check for actions */
229 if (isset($_POST['action'])) {
230 if (strcmp($_POST['action'], 'clean') == 0) {
231 $total = jirafeau_admin_clean();
232 echo '<div class="message">' . NL;
233 echo '<p>';
234 echo t('CLEANED_FILES_CNT') . ' : ' . $total;
235 echo '</p></div>';
236 } elseif (strcmp($_POST['action'], 'clean_async') == 0) {
237 $total = jirafeau_admin_clean_async();
238 echo '<div class="message">' . NL;
239 echo '<p>';
240 echo t('CLEANED_FILES_CNT') . ' : ' . $total;
241 echo '</p></div>';
242 } elseif (strcmp($_POST['action'], 'size') == 0) {
243 $size = jirafeau_dir_size($cfg['var_root']);
244 $human_size = jirafeau_human_size($size);
245 echo '<div class="message">' . NL;
246 echo '<p>' . t('SIZE_DATA') . ': ' . $human_size .'</p>';
247 echo '</div>';
248 } elseif (strcmp($_POST['action'], 'list') == 0) {
249 jirafeau_admin_list("", "", "");
250 } elseif (strcmp($_POST['action'], 'search_by_name') == 0) {
251 jirafeau_admin_list($_POST['name'], "", "");
252 } elseif (strcmp($_POST['action'], 'search_by_file_hash') == 0) {
253 jirafeau_admin_list("", $_POST['hash'], "");
254 } elseif (strcmp($_POST['action'], 'search_link') == 0) {
255 jirafeau_admin_list("", "", $_POST['link']);
256 } elseif (strcmp($_POST['action'], 'delete_link') == 0) {
257 jirafeau_delete_link($_POST['link']);
258 echo '<div class="message">' . NL;
259 echo '<p>' . t('LINK_DELETED') . '</p></div>';
260 } elseif (strcmp($_POST['action'], 'delete_file') == 0) {
261 $count = jirafeau_delete_file($_POST['hash']);
262 echo '<div class="message">' . NL;
263 echo '<p>' . t('DELETED_LINKS') . ' : ' . $count . '</p></div>';
264 } elseif (strcmp($_POST['action'], 'download') == 0) {
265 $l = jirafeau_get_link($_POST['link']);
266 if (!count($l)) {
267 return;
268 }
269 $p = s2p($l['hash']);
270 header('Content-Length: ' . $l['file_size']);
271 header('Content-Type: ' . $l['mime_type']);
272 header('Content-Disposition: attachment; filename="' .
273 $l['file_name'] . '"');
274 if (file_exists(VAR_FILES . $p . $l['hash'])) {
275 $r = fopen(VAR_FILES . $p . $l['hash'], 'r');
276 while (!feof($r)) {
277 print fread($r, 1024);
278 }
279 fclose($r);
280 }
281 exit;
282 }
283 }
284
285 require(JIRAFEAU_ROOT.'lib/template/footer.php');
286 }
287 ?>

patrick-canterino.de