]> git.p6c8.net - jirafeau_project.git/blob - docker/docker_config.php
140149e4794213d9694dc177fb2be9a035ad7e9d
[jirafeau_project.git] / docker / docker_config.php
1 <?php
2 /*
3 * Jirafeau, your web file repository
4 * Copyright (C) 2008 Julien "axolotl" BERNARD <axolotl@magieeternelle.org>
5 * Copyright (C) 2015 Jerome Jutteau <jerome@jutteau.fr>
6 * Copyright (C) 2024 Jirafeau project <https://gitlab.com/jirafeau> (see AUTHORS.md)
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21 define('JIRAFEAU_ROOT', '/www/');
22 define('JIRAFEAU_CFG', JIRAFEAU_ROOT . 'lib/config.local.php');
23
24 require(JIRAFEAU_ROOT . 'lib/settings.php');
25 require(JIRAFEAU_ROOT . 'lib/functions.php');
26 require(JIRAFEAU_ROOT . 'lib/lang.php');
27
28 function env_2_cfg_string(&$cfg, $config_name, $default = null)
29 {
30 $env_name = strtoupper($config_name);
31 $r = getenv($env_name);
32 if ($r === false) {
33 if (is_null($default)) {
34 return false;
35 } else {
36 $r = $default;
37 }
38 }
39 echo("setting $config_name to '$r'\n");
40 $cfg[$config_name] = $r;
41 return true;
42 }
43
44 function env_2_cfg_bool(&$cfg, $config_name, $default = null)
45 {
46 $env_name = strtoupper($config_name);
47 $r = getenv($env_name);
48 if ($r === false) {
49 if (is_null($default)) {
50 return false;
51 } else {
52 $r = $default;
53 }
54 } else {
55 $r = ($r == "1") ? true : false;
56 }
57 echo("setting $config_name to " . ($r ? "true" : "false") . "\n");
58 $cfg[$config_name] = $r;
59 return true;
60 }
61
62 function env_2_cfg_int(&$cfg, $config_name, $default = null)
63 {
64 $env_name = strtoupper($config_name);
65 $r = getenv($env_name);
66 if ($r === false) {
67 if (is_null($default)) {
68 return false;
69 } else {
70 $r = $default;
71 }
72 } else {
73 $r = intval($r);
74 }
75 echo("setting $config_name to $r\n");
76 $cfg[$config_name] = $r;
77 return true;
78 }
79
80 function env_2_cfg_string_array(&$cfg, $config_name)
81 {
82 $env_name = strtoupper($config_name);
83 $r = getenv($env_name);
84 if ($r === false) {
85 return;
86 }
87 $r = explode(",", $r);
88 $c = count($r);
89 echo("setting $config_name array with $c value(s)n\n");
90 $cfg[$config_name] = $r;
91 return true;
92 }
93
94 function env_2_cfg_string_array_key_value(&$cfg, $config_name)
95 {
96 $env_name = strtoupper($config_name);
97 $env_string = getenv($env_name);
98 if ($env_string === false) {
99 return;
100 }
101 $result = json_decode($env_string, true);
102 if (json_last_error() === JSON_ERROR_NONE) {
103 // JSON is valid
104 $c = count($result);
105 echo ("setting $config_name array with $c value(s)n\n");
106 } else {
107 echo ("ERROR - invalid json for environment key $config_name \n");
108 }
109
110 $cfg[$config_name] = $result;
111 return true;
112 }
113
114
115
116 function setup_admin_password(&$cfg)
117 {
118 if (strlen($cfg['admin_password']) > 0) {
119 return true;
120 }
121 echo("setting up admin password\n");
122 $p = getenv('ADMIN_PASSWORD');
123 if ($p === false) {
124 $p = jirafeau_gen_random(20);
125 echo("auto-generated admin password: $p\n");
126 }
127 $cfg['admin_password'] = hash('sha256', $p);
128 return true;
129 }
130
131 function set_rights($path)
132 {
133 $uid = getenv('USER_ID');
134 if ($uid === false) {
135 $uid = 100;
136 }
137 $gid = getenv('GROUP_ID');
138 if ($gid === false) {
139 $gid = 82;
140 }
141 if (!chown($path, $uid)) {
142 echo("setting up user $uid for $path: failed\n");
143 return false;
144 }
145 if (!chgrp($path, $gid)) {
146 echo("setting up group $gid for $path: failed\n");
147 return false;
148 }
149 if (!chmod($path, 0770)) {
150 echo("setting up permissions $path: failed\n");
151 return false;
152 }
153 return true;
154 }
155
156 function setup_var_folder(&$cfg)
157 {
158 env_2_cfg_string($cfg, 'var_root', '/data/');
159 $var_root = $cfg['var_root'];
160 if (!is_dir($var_root)) {
161 mkdir($var_root, 0770, true);
162 }
163 $err = jirafeau_check_var_dir($var_root);
164 if ($err['has_error']) {
165 echo("error: cannot create $var_root folder\n");
166 return false;
167 }
168 return set_rights($var_root) &&
169 set_rights($var_root . 'async') &&
170 set_rights($var_root . 'files') &&
171 set_rights($var_root . 'links');
172 }
173
174 function setup_webroot(&$cfg)
175 {
176 if (!env_2_cfg_string($cfg, 'web_root')) {
177 echo("warning: you may want to have set WEB_ROOT to your website URL (like 'jirafeau.mydomain.tld/')\n");
178 }
179 }
180
181 function run_setup(&$cfg)
182 {
183 $setup_ok = setup_admin_password($cfg) &&
184 setup_var_folder($cfg);
185 setup_webroot($cfg);
186 env_2_cfg_string($cfg, 'file_hash');
187 env_2_cfg_bool($cfg, 'preview');
188 env_2_cfg_string($cfg, 'title', false);
189 env_2_cfg_string($cfg, 'organisation');
190 env_2_cfg_string($cfg, 'lang');
191 env_2_cfg_string($cfg, 'contactperson');
192 env_2_cfg_string($cfg, 'style');
193 env_2_cfg_string($cfg, 'availability_default');
194 env_2_cfg_string($cfg, 'dark_style');
195 env_2_cfg_bool($cfg, 'one_time_download');
196 env_2_cfg_bool($cfg, 'one_time_download_preselected');
197 env_2_cfg_bool($cfg, 'enable_crypt');
198 env_2_cfg_bool($cfg, 'debug');
199 env_2_cfg_int($cfg, 'maximal_upload_size');
200 env_2_cfg_string_array($cfg, 'upload_password');
201 env_2_cfg_string_array($cfg, 'upload_ip');
202 env_2_cfg_string_array($cfg, 'admin_ip');
203 env_2_cfg_string_array($cfg, 'upload_ip_nopassword');
204 env_2_cfg_string_array($cfg, 'proxy_ip');
205 // this is a key value based value
206 env_2_cfg_string_array_key_value($cfg, 'availabilities');
207 env_2_cfg_bool($cfg, 'store_uploader_ip');
208 env_2_cfg_string($cfg, 'download_password_requirement');
209 env_2_cfg_int($cfg, 'download_password_gen_len');
210 env_2_cfg_string($cfg, 'download_password_gen_chars');
211 env_2_cfg_string($cfg, 'download_password_policy');
212 env_2_cfg_string($cfg, 'download_password_policy_regex');
213
214 if ($setup_ok) {
215 $cfg['installation_done'] = true;
216 jirafeau_export_cfg($cfg);
217 echo("You can now connect to your Jirafeau instance\n");
218 exit(0);
219 } else {
220 echo("Some Jirafeau options failed");
221 exit(1);
222 }
223 }
224
225 run_setup($cfg);

patrick-canterino.de