blob: b3b1cc13d9481ae679485a99f4bef186733fa71e [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01003// check for development mode
4$DEV_MODE = (getenv('DEV_MODE') == 'y');
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +01005// check for demo mode
6$DEMO_MODE = (getenv('DEMO_MODE') == 'y');
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +01007
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01008// Slave does not serve UI
9/* if (!preg_match('/y|yes/i', getenv('MASTER'))) {
10 header('Location: /SOGo', true, 307);
11 exit;
12}*/
13
14require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.inc.php';
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010015
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010016$default_autodiscover_config = $autodiscover_config;
17
18if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php')) {
19 include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php';
20}
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010021
22// auto-generated by generate-config.sh and update.sh
23if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/inc/app_info.inc.php')) {
24 require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/app_info.inc.php';
25}
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010026unset($https_port);
27$autodiscover_config = array_merge($default_autodiscover_config, $autodiscover_config);
28
29header_remove("X-Powered-By");
30
31// Yubi OTP API
32require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/Yubico.php';
33
34// WebAuthn
35require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/WebAuthn.php';
36
37// Autoload composer
38require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/vendor/autoload.php';
39
40// Load Sieve
41require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/sieve/SieveParser.php';
42
43// minifierExtended
44require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/JSminifierExtended.php';
45require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/CSSminifierExtended.php';
46
47require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/array_merge_real.php';
48
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010049// U2F API + T/HOTP API
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010050// u2f - deprecated, should be removed
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010051$u2f = new u2flib_server\U2F('https://' . $_SERVER['HTTP_HOST']);
52$qrprovider = new RobThree\Auth\Providers\Qr\QRServerProvider();
53$tfa = new RobThree\Auth\TwoFactorAuth($OTP_LABEL, 6, 30, 'sha1', $qrprovider);
54
55// FIDO2
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010056$server_name = parse_url('https://' . $_SERVER['HTTP_HOST'], PHP_URL_HOST);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010057$formats = $GLOBALS['FIDO2_FORMATS'];
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010058$WebAuthn = new lbuchs\WebAuthn\WebAuthn('WebAuthn Library', $server_name, $formats);
59// only include root ca's when needed
60if (getenv('WEBAUTHN_ONLY_TRUSTED_VENDORS') == 'y') $WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates');
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010061
62// Redis
63$redis = new Redis();
64try {
65 if (!empty(getenv('REDIS_SLAVEOF_IP'))) {
66 $redis->connect(getenv('REDIS_SLAVEOF_IP'), getenv('REDIS_SLAVEOF_PORT'));
67 }
68 else {
69 $redis->connect('redis-mailcow', 6379);
70 }
71}
72catch (Exception $e) {
73?>
74<center style='font-family:sans-serif;'>Connection to Redis failed.<br /><br />The following error was reported:<br/><?=$e->getMessage();?></center>
75<?php
76exit;
77}
78
79// PDO
80// Calculate offset
81// $now = new DateTime();
82// $mins = $now->getOffset() / 60;
83// $sgn = ($mins < 0 ? -1 : 1);
84// $mins = abs($mins);
85// $hrs = floor($mins / 60);
86// $mins -= $hrs * 60;
87// $offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
88
89$dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
90$opt = [
91 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
92 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
93 PDO::ATTR_EMULATE_PREPARES => false,
94 //PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '" . $offset . "', group_concat_max_len = 3423543543;",
95];
96try {
97 $pdo = new PDO($dsn, $database_user, $database_pass, $opt);
98}
99catch (PDOException $e) {
100// Stop when SQL connection fails
101?>
102<center style='font-family:sans-serif;'>Connection to database failed.<br /><br />The following error was reported:<br/> <?=$e->getMessage();?></center>
103<?php
104exit;
105}
106// Stop when dockerapi is not available
107if (fsockopen("tcp://dockerapi", 443, $errno, $errstr) === false) {
108?>
109<center style='font-family:sans-serif;'>Connection to dockerapi container failed.<br /><br />The following error was reported:<br/><?=$errno;?> - <?=$errstr;?></center>
110<?php
111exit;
112}
113
114// OAuth2
115class mailcowPdo extends OAuth2\Storage\Pdo {
116 public function __construct($connection, $config = array()) {
117 parent::__construct($connection, $config);
118 $this->config['user_table'] = 'mailbox';
119 }
120 public function checkUserCredentials($username, $password) {
121 if (check_login($username, $password) == 'user') {
122 return true;
123 }
124 return false;
125 }
126 public function getUserDetails($username) {
127 return $this->getUser($username);
128 }
129}
130$oauth2_scope_storage = new OAuth2\Storage\Memory(array('default_scope' => 'profile', 'supported_scopes' => array('profile')));
131$oauth2_storage = new mailcowPdo(array('dsn' => $dsn, 'username' => $database_user, 'password' => $database_pass));
132$oauth2_server = new OAuth2\Server($oauth2_storage, array(
133 'refresh_token_lifetime' => $REFRESH_TOKEN_LIFETIME,
134 'access_lifetime' => $ACCESS_TOKEN_LIFETIME,
135));
136$oauth2_server->setScopeUtil(new OAuth2\Scope($oauth2_scope_storage));
137$oauth2_server->addGrantType(new OAuth2\GrantType\AuthorizationCode($oauth2_storage));
138$oauth2_server->addGrantType(new OAuth2\GrantType\UserCredentials($oauth2_storage));
139$oauth2_server->addGrantType(new OAuth2\GrantType\RefreshToken($oauth2_storage, array(
140 'always_issue_new_refresh_token' => true
141)));
142
143function exception_handler($e) {
144 if ($e instanceof PDOException) {
145 $_SESSION['return'][] = array(
146 'type' => 'danger',
147 'log' => array(__FUNCTION__),
148 'msg' => array('mysql_error', $e)
149 );
150 return false;
151 }
152 else {
153 $_SESSION['return'][] = array(
154 'type' => 'danger',
155 'log' => array(__FUNCTION__),
156 'msg' => 'An unknown error occured: ' . print_r($e, true)
157 );
158 return false;
159 }
160}
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +0100161if(!$DEV_MODE) {
162 set_exception_handler('exception_handler');
163}
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100164
165// TODO: Move function
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200166function get_remote_ip() {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100167 $remote = $_SERVER['REMOTE_ADDR'];
168 if (filter_var($remote, FILTER_VALIDATE_IP) === false) {
169 return '0.0.0.0';
170 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200171 return $remote;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100172}
173
174// Load core functions first
175require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.inc.php';
176require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/sessions.inc.php';
177
178// IMAP lib
179// use Ddeboer\Imap\Server;
180// $imap_server = new Server('dovecot', 143, '/imap/tls/novalidate-cert');
181
182// Set language
183if (!isset($_SESSION['mailcow_locale']) && !isset($_COOKIE['mailcow_locale'])) {
184 if ($DETECT_LANGUAGE && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100185 // regex inspired from @GabrielAnderson on http://stackoverflow.com/questions/6038236/http-accept-language
186 preg_match_all('/([a-z]{1,8}(-[a-z]{1,8})*)\s*(;\s*q\s*=\s*(1|0\.[0-9]+))?/i', $_SERVER['HTTP_ACCEPT_LANGUAGE'], $lang_parse);
187
188 $langs = $lang_parse[1];
189 $ranks = $lang_parse[4];
190
191 // (create an associative array 'language' => 'preference')
192 $lang2pref = array();
193 for ($i=0; $i<count($langs); $i++) {
194 $lang2pref[strtolower($langs[$i])] = (float) (!empty($ranks[$i]) ? $ranks[$i] : 1);
195 }
196
197 // (comparison function for uksort)
198 $cmpLangs = function ($a, $b) use ($lang2pref) {
199 if ($lang2pref[$a] > $lang2pref[$b])
200 return -1;
201 elseif ($lang2pref[$a] < $lang2pref[$b])
202 return 1;
203 elseif (strlen($a) > strlen($b))
204 return -1;
205 elseif (strlen($a) < strlen($b))
206 return 1;
207 else
208 return 0;
209 };
210
211 // sort the languages by prefered language and by the most specific region
212 uksort($lang2pref, $cmpLangs);
213
214 // generate language array without the region part
215 $AVAILABLE_BASE_LANGUAGES=array();
216 foreach ($AVAILABLE_LANGUAGES as $code => $lang) {
217 $base_code = substr($code, 0, 2);
218 if (!array_key_exists($base_code, $AVAILABLE_BASE_LANGUAGES)) {
219 $AVAILABLE_BASE_LANGUAGES[$base_code] = $code;
220 }
221 }
222
223 // Find a perfect match or partial match
224 // Match en-gb or en
225 foreach ($lang2pref as $lang => $q) {
226 if (array_key_exists($lang, $AVAILABLE_LANGUAGES)) {
227 $_SESSION['mailcow_locale'] = $lang;
228 break;
229 } elseif (array_key_exists($lang, $AVAILABLE_BASE_LANGUAGES)) {
230 $_SESSION['mailcow_locale'] = $AVAILABLE_BASE_LANGUAGES[$lang];
231 break;
232 }
233 }
234
235 // Try suggest match
236 // e.g. suggest en-gb when only en-us is provided
237 if (!isset($_SESSION['mailcow_locale'])) {
238 foreach ($lang2pref as $lang => $q) {
239 if (array_key_exists(substr($lang, 0, 2), $AVAILABLE_BASE_LANGUAGES)) {
240 $_SESSION['mailcow_locale'] = $AVAILABLE_BASE_LANGUAGES[substr($lang, 0, 2)];
241 break;
242 }
243 }
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100244 }
245 }
246 else {
247 $_SESSION['mailcow_locale'] = strtolower(trim($DEFAULT_LANG));
248 }
249}
250if (isset($_COOKIE['mailcow_locale'])) {
251 (preg_match('/^[a-z]{2}$/', $_COOKIE['mailcow_locale'])) ? $_SESSION['mailcow_locale'] = $_COOKIE['mailcow_locale'] : setcookie("mailcow_locale", "", time() - 300);
252}
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200253if (isset($_GET['lang']) && array_key_exists($_GET['lang'], $AVAILABLE_LANGUAGES)) {
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100254 $_SESSION['mailcow_locale'] = $_GET['lang'];
255 setcookie("mailcow_locale", $_GET['lang'], time()+30758400); // one year
256}
257
258/*
259 * load language
260 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100261$lang = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/lang/lang.en-gb.json'), true);
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100262
263$langFile = $_SERVER['DOCUMENT_ROOT'] . '/lang/lang.'.$_SESSION['mailcow_locale'].'.json';
264if(file_exists($langFile)) {
265 $lang = array_merge_real($lang, json_decode(file_get_contents($langFile), true));
266}
267
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100268
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100269require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.acl.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100270require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.address_rewriting.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100271require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.admin.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200272require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.app_passwd.inc.php';
273require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.customize.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100274require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.dkim.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200275require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.docker.inc.php';
276require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.domain_admin.inc.php';
277require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fail2ban.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100278require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fwdhost.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200279require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailbox.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100280require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailq.inc.php';
281require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.oauth2.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200282require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.policy.inc.php';
283require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.presets.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100284require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.pushover.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200285require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quarantine.inc.php';
286require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quota_notification.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100287require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.ratelimit.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100288require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.rspamd.inc.php';
289require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.tls_policy_maps.inc.php';
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200290require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.transports.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100291require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/init_db.inc.php';
292require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/triggers.inc.php';
Matthias Andreas Benkard12a57352021-12-28 18:02:04 +0100293require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/twig.inc.php';
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100294init_db_schema();
295if (isset($_SESSION['mailcow_cc_role'])) {
296 // if ($_SESSION['mailcow_cc_role'] == 'user') {
297 // list($master_user, $master_passwd) = explode(':', file_get_contents('/etc/sogo/sieve.creds'));
298 // $imap_connection = $imap_server->authenticate($_SESSION['mailcow_cc_username'] . '*' . trim($master_user), trim($master_passwd));
299 // $master_user = $master_passwd = null;
300 // }
301 acl('to_session');
302}
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100303
304// init frontend
305// Minify JS
306use MatthiasMullie\Minify;
307$js_minifier = new JSminifierExtended();
308$js_dir = array_diff(scandir('/web/js/build'), array('..', '.'));
309// Minify CSS
310$css_minifier = new CSSminifierExtended();
311$css_dir = array_diff(scandir('/web/css/build'), array('..', '.'));
312// get customized ui data
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +0100313$UI_TEXTS = customize('get', 'ui_texts');
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100314
315
316// minify bootstrap theme
317if (file_exists('/web/css/themes/'.$UI_THEME.'-bootstrap.css'))
318 $css_minifier->add('/web/css/themes/'.$UI_THEME.'-bootstrap.css');
319else
320 $css_minifier->add('/web/css/themes/lumen-bootstrap.css');
321// minify css build files
322foreach ($css_dir as $css_file) {
323 $css_minifier->add('/web/css/build/' . $css_file);
324}
325// minify js build files
326foreach ($js_dir as $js_file) {
327 $js_minifier->add('/web/js/build/' . $js_file);
328}