]> git.p6c8.net - jirafeau_project.git/blob - docker/docker_config.php
aa5777233878179c190a1f60970b1bb0c43476e3
[jirafeau_project.git] / docker / docker_config.php
1 <?php
2 /*
3 * Jirafeau, your web file repository
4 * Copyright (C) 2020 Jérôme 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 <https://www.gnu.org/licenses/>.
18 */
19 define('JIRAFEAU_ROOT', '/www/');
20 define('JIRAFEAU_CFG', JIRAFEAU_ROOT . 'lib/config.local.php');
21
22 require(JIRAFEAU_ROOT . 'lib/settings.php');
23 require(JIRAFEAU_ROOT . 'lib/functions.php');
24 require(JIRAFEAU_ROOT . 'lib/lang.php');
25
26 function env_2_cfg_string(&$cfg, $config_name, $env_name, $default = null)
27 {
28 $r = getenv($env_name);
29 if ($r === false) {
30 if (is_null($default)) {
31 return false;
32 } else {
33 $r = $default;
34 }
35 }
36 echo("setting $config_name to '$r'\n");
37 $cfg[$config_name] = $r;
38 return true;
39 }
40
41 function setup_admin_password(&$cfg)
42 {
43 if (strlen($cfg['admin_password']) > 0) {
44 return true;
45 }
46 echo("setting up admin password\n");
47 $p = getenv('ADMIN_PASSWORD');
48 if ($p === false) {
49 $p = jirafeau_gen_random(20);
50 echo("auto-generated admin password: $p\n");
51 }
52 $cfg['admin_password'] = hash('sha256', $p);
53 return true;
54 }
55
56 function set_rights($path)
57 {
58 $uid = getenv('USER_ID');
59 if ($uid === false) {
60 $uid = 100;
61 }
62 $gid = getenv('GROUP_ID');
63 if ($gid === false) {
64 $gid = 82;
65 }
66 if (!chown($path, $uid)) {
67 echo("setting up user $uid for $path: failed\n");
68 return false;
69 }
70 if (!chgrp($path, $gid)) {
71 echo("setting up group $gid for $path: failed\n");
72 return false;
73 }
74 if (!chmod($path, 0770)) {
75 echo("setting up permissions $path: failed\n");
76 return false;
77 }
78 return true;
79 }
80
81 function setup_var_folder(&$cfg)
82 {
83 env_2_cfg_string($cfg, 'var_root', 'VAR_ROOT', '/data/');
84 $var_root = $cfg['var_root'];
85 if (!is_dir($var_root)) {
86 mkdir($var_root, 0770, true);
87 }
88 $err = jirafeau_check_var_dir($var_root);
89 if ($err['has_error']) {
90 echo("error: cannot create $var_root folder\n");
91 return false;
92 }
93 return set_rights($var_root) &&
94 set_rights($var_root . 'async') &&
95 set_rights($var_root . 'files') &&
96 set_rights($var_root . 'links');
97 }
98
99 // TODO: lots of other options to implement
100 $setup_ok = setup_admin_password($cfg) &&
101 setup_var_folder($cfg);
102 env_2_cfg_string($cfg, 'web_root', 'WEB_ROOT', '');
103 env_2_cfg_string($cfg, 'file_hash', 'FILE_HASH', 'md5');
104
105 if ($setup_ok) {
106 $cfg['installation_done'] = true;
107 jirafeau_export_cfg($cfg);
108 echo("You can now connect to your Jirafeau instance\n");
109 exit(0);
110 } else {
111 echo("Some Jirafeau options failed");
112 exit(1);
113 }

patrick-canterino.de