]> git.p6c8.net - jirafeau_project.git/blob - docker/docker_config.php
Fixed every error detected by php-cs-fixer (except the single_space_around_construct...
[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 setup_admin_password(&$cfg)
95 {
96 if (strlen($cfg['admin_password']) > 0) {
97 return true;
98 }
99 echo("setting up admin password\n");
100 $p = getenv('ADMIN_PASSWORD');
101 if ($p === false) {
102 $p = jirafeau_gen_random(20);
103 echo("auto-generated admin password: $p\n");
104 }
105 $cfg['admin_password'] = hash('sha256', $p);
106 return true;
107 }
108
109 function set_rights($path)
110 {
111 $uid = getenv('USER_ID');
112 if ($uid === false) {
113 $uid = 100;
114 }
115 $gid = getenv('GROUP_ID');
116 if ($gid === false) {
117 $gid = 82;
118 }
119 if (!chown($path, $uid)) {
120 echo("setting up user $uid for $path: failed\n");
121 return false;
122 }
123 if (!chgrp($path, $gid)) {
124 echo("setting up group $gid for $path: failed\n");
125 return false;
126 }
127 if (!chmod($path, 0770)) {
128 echo("setting up permissions $path: failed\n");
129 return false;
130 }
131 return true;
132 }
133
134 function setup_var_folder(&$cfg)
135 {
136 env_2_cfg_string($cfg, 'var_root', '/data/');
137 $var_root = $cfg['var_root'];
138 if (!is_dir($var_root)) {
139 mkdir($var_root, 0770, true);
140 }
141 $err = jirafeau_check_var_dir($var_root);
142 if ($err['has_error']) {
143 echo("error: cannot create $var_root folder\n");
144 return false;
145 }
146 return set_rights($var_root) &&
147 set_rights($var_root . 'async') &&
148 set_rights($var_root . 'files') &&
149 set_rights($var_root . 'links');
150 }
151
152 function setup_webroot(&$cfg)
153 {
154 if (!env_2_cfg_string($cfg, 'web_root')) {
155 echo("warning: you may want to have set WEB_ROOT to your website URL (like 'jirafeau.mydomain.tld/')\n");
156 }
157 }
158
159 function run_setup(&$cfg)
160 {
161 $setup_ok = setup_admin_password($cfg) &&
162 setup_var_folder($cfg);
163 setup_webroot($cfg);
164 env_2_cfg_string($cfg, 'file_hash');
165 env_2_cfg_bool($cfg, 'preview');
166 env_2_cfg_string($cfg, 'title', false);
167 env_2_cfg_string($cfg, 'organisation');
168 env_2_cfg_string($cfg, 'contactperson');
169 env_2_cfg_string($cfg, 'style');
170 env_2_cfg_string($cfg, 'availability_default');
171 env_2_cfg_string($cfg, 'dark_style');
172 env_2_cfg_bool($cfg, 'one_time_download');
173 env_2_cfg_bool($cfg, 'enable_crypt');
174 env_2_cfg_bool($cfg, 'debug');
175 env_2_cfg_int($cfg, 'maximal_upload_size');
176 env_2_cfg_string_array($cfg, 'upload_password');
177 env_2_cfg_string_array($cfg, 'upload_ip');
178 env_2_cfg_string_array($cfg, 'admin_ip');
179 env_2_cfg_string_array($cfg, 'upload_ip_nopassword');
180 env_2_cfg_string_array($cfg, 'proxy_ip');
181 env_2_cfg_bool($cfg, 'store_uploader_ip');
182 env_2_cfg_string($cfg, 'download_password_requirement');
183 env_2_cfg_int($cfg, 'download_password_gen_len');
184 env_2_cfg_string($cfg, 'download_password_gen_chars');
185 env_2_cfg_string($cfg, 'download_password_policy');
186 env_2_cfg_string($cfg, 'download_password_policy_regex');
187
188 if ($setup_ok) {
189 $cfg['installation_done'] = true;
190 jirafeau_export_cfg($cfg);
191 echo("You can now connect to your Jirafeau instance\n");
192 exit(0);
193 } else {
194 echo("Some Jirafeau options failed");
195 exit(1);
196 }
197 }
198
199 run_setup($cfg);

patrick-canterino.de