mailcow/data/web/inc/prerequisites.inc.php

316 lines
12 KiB
PHP
Raw Normal View History

2017-03-02 11:23:23 +01:00
<?php
2020-01-29 10:37:55 +01:00
// check for development mode
$DEV_MODE = (getenv('DEV_MODE') == 'y');
2020-01-29 10:37:55 +01:00
// Slave does not serve UI
2020-03-28 19:51:42 +01:00
/* if (!preg_match('/y|yes/i', getenv('MASTER'))) {
2020-01-29 10:37:55 +01:00
header('Location: /SOGo', true, 307);
exit;
2020-03-28 19:51:42 +01:00
}*/
2020-01-29 10:37:55 +01:00
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.inc.php';
Jan(moo)uary Update 2022 - Revision A (2022-01a) (#4445) * [API] Fix minor issue in api docs * [GH-Actions][stale] Add neverstale label to exempt list * [Web] add github version tag * [Web] add github version tag error handling * Passwordless SOGo auth: support for calendar invitations and calendar/contacts subscriptions Inviting someone to a calendar event triggers a request to /SOGo/so/otheruser@example.com/freebusy.ifb/ajaxRead. Subscribing to someone's calendar/contacts triggers a request to /SOGo/so/otheruser@example.com/foldersSearch. The email address in the URL is different from the logged-in user, which needs to be handled appropriately by sogo-auth.php. * [Web] add github version tag - adjust css * [Compose] Update SOGo Autoreply Schedule to 5m Based on the advice of inverse (SOGo developer). Thanks to https://github.com/jmber Closes: https://github.com/mailcow/mailcow-dockerized/issues/4436 * [Web] add github version tag - move twig globals * [Web] add github version tag - missing </div> * Passwordless SOGo auth: improvements for when accessing other users * [WebAuthn] fido2 passwordless auth - fix (#4440) * [WebAuthn] fido2 revert * [WebAuthn] set UV flags to 'discouraged' * [WebAuthn] revert - set UV flags to 'discouraged' Co-authored-by: ntimo <git@nowitzki.me> Co-authored-by: Peter <magic@kthx.at> Co-authored-by: FreddleSpl0it <patschul@posteo.de> Co-authored-by: FreddleSpl0it <75116288+FreddleSpl0it@users.noreply.github.com> Co-authored-by: Michael Kuron <mkuron@users.noreply.github.com>
2022-02-01 15:26:48 +01:00
$default_autodiscover_config = $autodiscover_config;
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php')) {
include_once $_SERVER['DOCUMENT_ROOT'] . '/inc/vars.local.inc.php';
2017-03-02 11:23:23 +01:00
}
Jan(moo)uary Update 2022 - Revision A (2022-01a) (#4445) * [API] Fix minor issue in api docs * [GH-Actions][stale] Add neverstale label to exempt list * [Web] add github version tag * [Web] add github version tag error handling * Passwordless SOGo auth: support for calendar invitations and calendar/contacts subscriptions Inviting someone to a calendar event triggers a request to /SOGo/so/otheruser@example.com/freebusy.ifb/ajaxRead. Subscribing to someone's calendar/contacts triggers a request to /SOGo/so/otheruser@example.com/foldersSearch. The email address in the URL is different from the logged-in user, which needs to be handled appropriately by sogo-auth.php. * [Web] add github version tag - adjust css * [Compose] Update SOGo Autoreply Schedule to 5m Based on the advice of inverse (SOGo developer). Thanks to https://github.com/jmber Closes: https://github.com/mailcow/mailcow-dockerized/issues/4436 * [Web] add github version tag - move twig globals * [Web] add github version tag - missing </div> * Passwordless SOGo auth: improvements for when accessing other users * [WebAuthn] fido2 passwordless auth - fix (#4440) * [WebAuthn] fido2 revert * [WebAuthn] set UV flags to 'discouraged' * [WebAuthn] revert - set UV flags to 'discouraged' Co-authored-by: ntimo <git@nowitzki.me> Co-authored-by: Peter <magic@kthx.at> Co-authored-by: FreddleSpl0it <patschul@posteo.de> Co-authored-by: FreddleSpl0it <75116288+FreddleSpl0it@users.noreply.github.com> Co-authored-by: Michael Kuron <mkuron@users.noreply.github.com>
2022-02-01 15:26:48 +01:00
// auto-generated by generate-config.sh and update.sh
if (file_exists($_SERVER['DOCUMENT_ROOT'] . '/inc/app_info.inc.php')) {
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/app_info.inc.php';
}
unset($https_port);
$autodiscover_config = array_merge($default_autodiscover_config, $autodiscover_config);
2017-03-02 11:23:23 +01:00
2017-06-06 21:58:48 +02:00
header_remove("X-Powered-By");
2017-03-02 11:23:23 +01:00
// Yubi OTP API
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/Yubico.php';
2017-03-02 11:23:23 +01:00
// WebAuthn
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/WebAuthn.php';
// Autoload composer
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/vendor/autoload.php';
// Load Sieve
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/sieve/SieveParser.php';
// minifierExtended
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/JSminifierExtended.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/CSSminifierExtended.php';
introduce json-files for translations (#3120) * introduce json-files for translations to structure data and add ability using translation managements * Translated using Weblate (Italian) Currently translated at 40.1% (342 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/it/ * Translated using Weblate (Catalan) Currently translated at 58.3% (497 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/ca/ * Translated using Weblate (Polish) Currently translated at 45.3% (386 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/pl/ * Translated using Weblate (Latvian) Currently translated at 59.0% (503 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/lv/ * Translated using Weblate (Portuguese) Currently translated at 27.5% (234 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/pt/ * Translated using Weblate (Russian) Currently translated at 45.4% (387 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/ru/ * Translated using Weblate (German) Currently translated at 99.9% (851 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/de/ * removed empty and unused text user_change_fn * removed empty and unused text user_change_fn II
2019-11-09 10:09:23 +01:00
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/lib/array_merge_real.php';
// Minify JS
use MatthiasMullie\Minify;
$js_minifier = new JSminifierExtended();
$js_dir = array_diff(scandir('/web/js/build'), array('..', '.'));
foreach ($js_dir as $js_file) {
$js_minifier->add('/web/js/build/' . $js_file);
}
// Minify CSS
$css_minifier = new CSSminifierExtended();
$css_dir = array_diff(scandir('/web/css/build'), array('..', '.'));
foreach ($css_dir as $css_file) {
$css_minifier->add('/web/css/build/' . $css_file);
}
// U2F API + T/HOTP API
2022-01-12 21:09:18 +01:00
// u2f - deprecated, should be removed
$u2f = new u2flib_server\U2F('https://' . $_SERVER['HTTP_HOST']);
$qrprovider = new RobThree\Auth\Providers\Qr\QRServerProvider();
$tfa = new RobThree\Auth\TwoFactorAuth($OTP_LABEL, 6, 30, 'sha1', $qrprovider);
2017-03-02 11:23:23 +01:00
// FIDO2
$server_name = parse_url('https://' . $_SERVER['HTTP_HOST'], PHP_URL_HOST);
$formats = $GLOBALS['FIDO2_FORMATS'];
$WebAuthn = new lbuchs\WebAuthn\WebAuthn('WebAuthn Library', $server_name, $formats);
// only include root ca's when needed
2022-01-20 14:37:20 +01:00
if (getenv('WEBAUTHN_ONLY_TRUSTED_VENDORS') == 'y') $WebAuthn->addRootCertificates($_SERVER['DOCUMENT_ROOT'] . '/inc/lib/WebAuthn/rootCertificates');
// Redis
$redis = new Redis();
try {
if (!empty(getenv('REDIS_SLAVEOF_IP'))) {
$redis->connect(getenv('REDIS_SLAVEOF_IP'), getenv('REDIS_SLAVEOF_PORT'));
}
else {
$redis->connect('redis-mailcow', 6379);
}
}
catch (Exception $e) {
?>
<center style='font-family:sans-serif;'>Connection to Redis failed.<br /><br />The following error was reported:<br/><?=$e->getMessage();?></center>
<?php
exit;
}
2017-03-02 11:23:23 +01:00
// PDO
2017-04-21 10:20:31 +02:00
// Calculate offset
// $now = new DateTime();
// $mins = $now->getOffset() / 60;
// $sgn = ($mins < 0 ? -1 : 1);
// $mins = abs($mins);
// $hrs = floor($mins / 60);
// $mins -= $hrs * 60;
// $offset = sprintf('%+d:%02d', $hrs*$sgn, $mins);
2017-04-21 10:20:31 +02:00
$dsn = $database_type . ":unix_socket=" . $database_sock . ";dbname=" . $database_name;
2017-03-02 11:23:23 +01:00
$opt = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
//PDO::MYSQL_ATTR_INIT_COMMAND => "SET time_zone = '" . $offset . "', group_concat_max_len = 3423543543;",
2017-03-02 11:23:23 +01:00
];
try {
$pdo = new PDO($dsn, $database_user, $database_pass, $opt);
2017-03-02 11:23:23 +01:00
}
catch (PDOException $e) {
// Stop when SQL connection fails
2017-03-02 11:23:23 +01:00
?>
<center style='font-family:sans-serif;'>Connection to database failed.<br /><br />The following error was reported:<br/> <?=$e->getMessage();?></center>
2017-03-02 11:23:23 +01:00
<?php
exit;
}
// Stop when dockerapi is not available
if (fsockopen("tcp://dockerapi", 443, $errno, $errstr) === false) {
?>
<center style='font-family:sans-serif;'>Connection to dockerapi container failed.<br /><br />The following error was reported:<br/><?=$errno;?> - <?=$errstr;?></center>
<?php
exit;
}
// OAuth2
class mailcowPdo extends OAuth2\Storage\Pdo {
public function __construct($connection, $config = array()) {
parent::__construct($connection, $config);
$this->config['user_table'] = 'mailbox';
}
public function checkUserCredentials($username, $password) {
if (check_login($username, $password) == 'user') {
return true;
}
return false;
}
public function getUserDetails($username) {
return $this->getUser($username);
}
}
$oauth2_scope_storage = new OAuth2\Storage\Memory(array('default_scope' => 'profile', 'supported_scopes' => array('profile')));
$oauth2_storage = new mailcowPdo(array('dsn' => $dsn, 'username' => $database_user, 'password' => $database_pass));
$oauth2_server = new OAuth2\Server($oauth2_storage, array(
'refresh_token_lifetime' => $REFRESH_TOKEN_LIFETIME,
'access_lifetime' => $ACCESS_TOKEN_LIFETIME,
));
$oauth2_server->setScopeUtil(new OAuth2\Scope($oauth2_scope_storage));
$oauth2_server->addGrantType(new OAuth2\GrantType\AuthorizationCode($oauth2_storage));
$oauth2_server->addGrantType(new OAuth2\GrantType\UserCredentials($oauth2_storage));
$oauth2_server->addGrantType(new OAuth2\GrantType\RefreshToken($oauth2_storage, array(
'always_issue_new_refresh_token' => true
)));
function exception_handler($e) {
if ($e instanceof PDOException) {
$_SESSION['return'][] = array(
'type' => 'danger',
'log' => array(__FUNCTION__),
'msg' => array('mysql_error', $e)
);
return false;
}
else {
$_SESSION['return'][] = array(
'type' => 'danger',
'log' => array(__FUNCTION__),
'msg' => 'An unknown error occured: ' . print_r($e, true)
);
return false;
}
}
if(!$DEV_MODE) {
set_exception_handler('exception_handler');
}
2017-03-02 11:23:23 +01:00
// TODO: Move function
function get_remote_ip() {
$remote = $_SERVER['REMOTE_ADDR'];
if (filter_var($remote, FILTER_VALIDATE_IP) === false) {
return '0.0.0.0';
}
return $remote;
}
// Load core functions first
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.inc.php';
2017-12-09 13:17:15 +01:00
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/sessions.inc.php';
// IMAP lib
// use Ddeboer\Imap\Server;
// $imap_server = new Server('dovecot', 143, '/imap/tls/novalidate-cert');
// Set language
if (!isset($_SESSION['mailcow_locale']) && !isset($_COOKIE['mailcow_locale'])) {
2017-12-09 13:17:15 +01:00
if ($DETECT_LANGUAGE && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
// regex inspired from @GabrielAnderson on http://stackoverflow.com/questions/6038236/http-accept-language
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);
2022-10-06 16:21:12 +02:00
$langs = $lang_parse[1];
$ranks = $lang_parse[4];
2022-10-06 16:21:12 +02:00
// (create an associative array 'language' => 'preference')
$lang2pref = array();
for ($i=0; $i<count($langs); $i++) {
$lang2pref[strtolower($langs[$i])] = (float) (!empty($ranks[$i]) ? $ranks[$i] : 1);
2017-12-09 13:17:15 +01:00
}
2022-10-06 16:21:12 +02:00
// (comparison function for uksort)
$cmpLangs = function ($a, $b) use ($lang2pref) {
if ($lang2pref[$a] > $lang2pref[$b])
return -1;
elseif ($lang2pref[$a] < $lang2pref[$b])
return 1;
elseif (strlen($a) > strlen($b))
return -1;
elseif (strlen($a) < strlen($b))
return 1;
else
return 0;
};
// sort the languages by prefered language and by the most specific region
uksort($lang2pref, $cmpLangs);
// generate language array without the region part
$AVAILABLE_BASE_LANGUAGES=array();
foreach ($AVAILABLE_LANGUAGES as $code => $lang) {
$base_code = substr($code, 0, 2);
if (!array_key_exists($base_code, $AVAILABLE_BASE_LANGUAGES)) {
$AVAILABLE_BASE_LANGUAGES[$base_code] = $code;
}
}
// Find a perfect match or partial match
// Match en-gb or en
2022-10-06 16:21:12 +02:00
foreach ($lang2pref as $lang => $q) {
if (array_key_exists($lang, $AVAILABLE_LANGUAGES)) {
$_SESSION['mailcow_locale'] = $lang;
break;
} elseif (array_key_exists($lang, $AVAILABLE_BASE_LANGUAGES)) {
$_SESSION['mailcow_locale'] = $AVAILABLE_BASE_LANGUAGES[$lang];
break;
2022-10-06 16:21:12 +02:00
}
}
// Try suggest match
// e.g. suggest en-gb when only en-us is provided
if (!isset($_COOKIE['mailcow_locale'])) {
foreach ($lang2pref as $lang => $q) {
if (array_key_exists(substr($lang, 0, 2), $AVAILABLE_BASE_LANGUAGES)) {
$_SESSION['mailcow_locale'] = $AVAILABLE_BASE_LANGUAGES[substr($lang, 0, 2)];
break;
}
}
}
2017-12-09 13:17:15 +01:00
}
else {
$_SESSION['mailcow_locale'] = strtolower(trim($DEFAULT_LANG));
}
2017-05-23 09:36:59 +02:00
}
if (isset($_COOKIE['mailcow_locale'])) {
(preg_match('/^[a-z]{2}$/', $_COOKIE['mailcow_locale'])) ? $_SESSION['mailcow_locale'] = $_COOKIE['mailcow_locale'] : setcookie("mailcow_locale", "", time() - 300);
}
if (isset($_GET['lang']) && array_key_exists($_GET['lang'], $AVAILABLE_LANGUAGES)) {
$_SESSION['mailcow_locale'] = $_GET['lang'];
setcookie("mailcow_locale", $_GET['lang'], time()+30758400); // one year
}
introduce json-files for translations (#3120) * introduce json-files for translations to structure data and add ability using translation managements * Translated using Weblate (Italian) Currently translated at 40.1% (342 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/it/ * Translated using Weblate (Catalan) Currently translated at 58.3% (497 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/ca/ * Translated using Weblate (Polish) Currently translated at 45.3% (386 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/pl/ * Translated using Weblate (Latvian) Currently translated at 59.0% (503 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/lv/ * Translated using Weblate (Portuguese) Currently translated at 27.5% (234 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/pt/ * Translated using Weblate (Russian) Currently translated at 45.4% (387 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/ru/ * Translated using Weblate (German) Currently translated at 99.9% (851 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/de/ * removed empty and unused text user_change_fn * removed empty and unused text user_change_fn II
2019-11-09 10:09:23 +01:00
/*
* load language
*/
2022-10-06 16:21:12 +02:00
$lang = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . '/lang/lang.en-gb.json'), true);
introduce json-files for translations (#3120) * introduce json-files for translations to structure data and add ability using translation managements * Translated using Weblate (Italian) Currently translated at 40.1% (342 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/it/ * Translated using Weblate (Catalan) Currently translated at 58.3% (497 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/ca/ * Translated using Weblate (Polish) Currently translated at 45.3% (386 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/pl/ * Translated using Weblate (Latvian) Currently translated at 59.0% (503 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/lv/ * Translated using Weblate (Portuguese) Currently translated at 27.5% (234 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/pt/ * Translated using Weblate (Russian) Currently translated at 45.4% (387 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/ru/ * Translated using Weblate (German) Currently translated at 99.9% (851 of 852 strings) Translation: test/mailcow Translate-URL: http://192.168.178.22:8080/projects/test/mailcow/de/ * removed empty and unused text user_change_fn * removed empty and unused text user_change_fn II
2019-11-09 10:09:23 +01:00
$langFile = $_SERVER['DOCUMENT_ROOT'] . '/lang/lang.'.$_SESSION['mailcow_locale'].'.json';
if(file_exists($langFile)) {
$lang = array_merge_real($lang, json_decode(file_get_contents($langFile), true));
}
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.acl.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.address_rewriting.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.admin.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.app_passwd.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.customize.inc.php';
2017-05-26 23:02:04 +02:00
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.dkim.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.docker.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.domain_admin.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fail2ban.inc.php';
2017-05-26 23:02:04 +02:00
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.fwdhost.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailbox.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.mailq.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.oauth2.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.policy.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.presets.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.pushover.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quarantine.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.quota_notification.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.ratelimit.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.rspamd.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.tls_policy_maps.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/functions.transports.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/init_db.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/triggers.inc.php';
require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/twig.inc.php';
2017-04-21 10:20:31 +02:00
init_db_schema();
if (isset($_SESSION['mailcow_cc_role'])) {
// if ($_SESSION['mailcow_cc_role'] == 'user') {
// list($master_user, $master_passwd) = explode(':', file_get_contents('/etc/sogo/sieve.creds'));
// $imap_connection = $imap_server->authenticate($_SESSION['mailcow_cc_username'] . '*' . trim($master_user), trim($master_passwd));
// $master_user = $master_passwd = null;
// }
acl('to_session');
2017-10-01 20:34:37 +02:00
}
2017-12-09 13:17:15 +01:00
$UI_TEXTS = customize('get', 'ui_texts');