]> git.p6c8.net - jirafeau_project.git/blob - docker/docker_config.php
added run container section
[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_array_from_json(&$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 function setup_admin_password(&$cfg)
115 {
116 if (strlen($cfg['admin_password']) > 0) {
117 return true;
118 }
119 echo("setting up admin password\n");
120 $p = getenv('ADMIN_PASSWORD');
121 if ($p === false) {
122 $p = jirafeau_gen_random(20);
123 echo("auto-generated admin password: $p\n");
124 }
125 $cfg['admin_password'] = hash('sha256', $p);
126 return true;
127 }
128
129 function set_rights($path)
130 {
131 $uid = getenv('USER_ID');
132 if ($uid === false) {
133 $uid = 100;
134 }
135 $gid = getenv('GROUP_ID');
136 if ($gid === false) {
137 $gid = 82;
138 }
139 if (!chown($path, $uid)) {
140 echo("setting up user $uid for $path: failed\n");
141 return false;
142 }
143 if (!chgrp($path, $gid)) {
144 echo("setting up group $gid for $path: failed\n");
145 return false;
146 }
147 if (!chmod($path, 0770)) {
148 echo("setting up permissions $path: failed\n");
149 return false;
150 }
151 return true;
152 }
153
154 function setup_var_folder(&$cfg)
155 {
156 env_2_cfg_string($cfg, 'var_root', '/data/');
157 $var_root = $cfg['var_root'];
158 if (!is_dir($var_root)) {
159 mkdir($var_root, 0770, true);
160 }
161 $err = jirafeau_check_var_dir($var_root);
162 if ($err['has_error']) {
163 echo("error: cannot create $var_root folder\n");
164 return false;
165 }
166 return set_rights($var_root) &&
167 set_rights($var_root . 'async') &&
168 set_rights($var_root . 'files') &&
169 set_rights($var_root . 'links');
170 }
171
172 function setup_webroot(&$cfg)
173 {
174 if (!env_2_cfg_string($cfg, 'web_root')) {
175 echo("warning: you may want to have set WEB_ROOT to your website URL (like 'jirafeau.mydomain.tld/')\n");
176 }
177 }
178
179 function run_setup(&$cfg)
180 {
181 $setup_ok = setup_admin_password($cfg) &&
182 setup_var_folder($cfg);
183 setup_webroot($cfg);
184 env_2_cfg_string($cfg, 'file_hash');
185 env_2_cfg_bool($cfg, 'preview');
186 env_2_cfg_string($cfg, 'title', false);
187 env_2_cfg_string($cfg, 'organisation');
188 env_2_cfg_string($cfg, 'lang');
189 env_2_cfg_string($cfg, 'contactperson');
190 env_2_cfg_string($cfg, 'style');
191 env_2_cfg_string($cfg, 'availability_default');
192 env_2_cfg_string($cfg, 'dark_style');
193 env_2_cfg_bool($cfg, 'one_time_download');
194 env_2_cfg_bool($cfg, 'one_time_download_preselected');
195 env_2_cfg_bool($cfg, 'enable_crypt');
196 env_2_cfg_bool($cfg, 'debug');
197 env_2_cfg_int($cfg, 'maximal_upload_size');
198 env_2_cfg_string_array($cfg, 'upload_password');
199 env_2_cfg_string_array($cfg, 'upload_ip');
200 env_2_cfg_string_array($cfg, 'admin_ip');
201 env_2_cfg_string_array($cfg, 'upload_ip_nopassword');
202 env_2_cfg_string_array($cfg, 'proxy_ip');
203 // this is a key value based value
204 env_2_cfg_array_from_json($cfg, 'availabilities');
205 env_2_cfg_bool($cfg, 'store_uploader_ip');
206 env_2_cfg_string($cfg, 'download_password_requirement');
207 env_2_cfg_int($cfg, 'download_password_gen_len');
208 env_2_cfg_string($cfg, 'download_password_gen_chars');
209 env_2_cfg_string($cfg, 'download_password_policy');
210 env_2_cfg_string($cfg, 'download_password_policy_regex');
211
212 if ($setup_ok) {
213 $cfg['installation_done'] = true;
214 jirafeau_export_cfg($cfg);
215 echo("You can now connect to your Jirafeau instance\n");
216 exit(0);
217 } else {
218 echo("Some Jirafeau options failed");
219 exit(1);
220 }
221 }
222
223 run_setup($cfg);

patrick-canterino.de