tidied up some code, i.e. new autoconf function

This commit is contained in:
Paul Wilde 2021-08-12 14:55:54 +01:00
parent 30302dfb84
commit b0327c0a4e
3 changed files with 55 additions and 72 deletions

View file

@ -33,6 +33,7 @@ class Init {
} }
// merge the default config with the custom config // merge the default config with the custom config
Core::$Config = array_merge($default_config,$config); Core::$Config = array_merge($default_config,$config);
Core::$Config["PrimaryDomain"] = Core::$Config["Domain"][0];
// parse the default services file for default values // parse the default services file for default values
$default_services = parse_ini_file(Core::root_dir()."/default-config/services.default.ini", true); $default_services = parse_ini_file(Core::root_dir()."/default-config/services.default.ini", true);

View file

@ -65,6 +65,19 @@ class Responder {
} }
return $resp; return $resp;
} }
private function get_username($service,$email_address) {
$username = "%EMAILADDRESS%";
if(!$service["UsernameIsFQDN"]) {
preg_match("/^[^@]+/",$email_address,$matches);
if (count($matches) > 0){
$username = $matches[0];
}
} else if ($service["RequireLogonDomain"]) {
$username = preg_replace("/[^@]+$/",Core::$Config["LogonDomain"],$email_address,1);
}
return $username;
}
private function all_urls(){ private function all_urls(){
$response = new Response(); $response = new Response();
// Not really useful, unless some lovely app developers want to parse it for their app :) // Not really useful, unless some lovely app developers want to parse it for their app :)

View file

@ -1,95 +1,64 @@
<?php <?php
// Get some core information to help with generation.
// Errrrrrrrrgh, this is sooooo messy, I'm going to tidy this up $services = Core::$Config["Services"];
// It's basically configuring the format of the email address dependent on
// variables set in the config file.
// i.e. if the domain isn't required for authentication then it strips the
// username back to just the pre-@ part. Or, if the username requires a different
// logon domain, then it replaces the email domain with the localdomain
//
// TODO: TIDY THIS UP!!
$conf = Core::$Config["Services"];
$data = Core::get_get_data(); $data = Core::get_get_data();
$email_provided = false;
$display_name = false;
$emailaddress = false;
if (isset($data["emailaddress"])) { if (isset($data["emailaddress"])) {
$email_address = $data["emailaddress"]; $email_address = $data["emailaddress"];
$display_name = $email_address;
$email_provided = true;
} else if ($data["path"]) {
$query = parse_url($data["path"]);
if (isset($query["query"])){
$email_address = explode("=",$query["query"]);
if ($email_address[0] == "emailaddress") {
$email_address = $email_address[1];
$email_provided = true;
$display_name = $email_address;
} }
}
}
if ($email_provided) {
if(!Core::$Config["RequireAuthDomain"]) {
$email_address = str_ireplace("@".Core::$Config["Domain"],"",$email_address);
} else if (Core::$Config["LogonDomain"]) {
$email_address = str_ireplace(Core::$Config["Domain"],Core::$Config["LogonDomain"],$email_address);
}
}
// The below link has config-v1.1.xml information // The below link has config-v1.1.xml information
// https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat // https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat
?> ?>
<clientConfig version="1.1"> <clientConfig version="1.1">
<emailProvider id="<?php echo Core::$Config["Domain"][0]?>"> <emailProvider id="<?php echo Core::$Config["PrimaryDomain"]?>">
<?php foreach (Core::$Config["Domain"] as $domain){ ?> <?php foreach (Core::$Config["Domain"] as $domain){ ?>
<domain><?php echo $domain; ?></domain> <domain><?php echo $domain; ?></domain>
<?php } ?> <?php } ?>
<displayName><?php echo $email_provided ? $display_name : "%EMAILADDRESS%" ;?></displayName> <displayName>%EMAILADDRESS%</displayName>
<?php if($conf["InMail"]&& $conf["InMail"]["Enabled"]){ <?php if($services["InMail"]&& $services["InMail"]["Enabled"]){
$in = $conf["InMail"]; ?> $service = $services["InMail"]; ?>
<incomingServer type="<?php echo strtolower($in["Type"]);?>"> <incomingServer type="<?php echo strtolower($service["Type"]);?>">
<hostname><?php echo $in["Server"];?></hostname> <hostname><?php echo $service["Server"];?></hostname>
<port><?php echo $in["Port"];?></port> <port><?php echo $service["Port"];?></port>
<socketType><?php echo $in["SocketType"];?></socketType> <socketType><?php echo $service["SocketType"];?></socketType>
<username><?php echo $email_provided ? $email_address : "%EMAILADDRESS%";?></username> <username><?php echo $this->get_username($service,$email_address); ?></username>
<authentication><?php echo $in["Authentication"];?></authentication> <authentication><?php echo $service["Authentication"];?></authentication>
</incomingServer> </incomingServer>
<?php } <?php }
if($conf["OutMail"]&& $conf["OutMail"]["Enabled"]){ if($services["OutMail"]&& $services["OutMail"]["Enabled"]){
$out = $conf["OutMail"]; ?> $service = $services["OutMail"]; ?>
<outgoingServer type="<?php echo strtolower($out["Type"]);?>"> <outgoingServer type="<?php echo strtolower($service["Type"]);?>">
<hostname><?php echo $out["Server"];?></hostname> <hostname><?php echo $service["Server"];?></hostname>
<port><?php echo $out["Port"];?></port> <port><?php echo $service["Port"];?></port>
<socketType><?php echo $out["SocketType"];?></socketType> <socketType><?php echo $service["SocketType"];?></socketType>
<username><?php echo $email_provided ? $email_address : "%EMAILADDRESS%";?></username> <username><?php echo $this->get_username($service,$email_address);?></username>
<authentication><?php echo $out["Authentication"];?></authentication> <authentication><?php echo $service["Authentication"];?></authentication>
</outgoingServer> </outgoingServer>
<?php } <?php }
if ($conf["AddressBook"] && $conf["AddressBook"]["Enabled"]) { if ($services["AddressBook"] && $services["AddressBook"]["Enabled"]) {
$card = $conf["AddressBook"]; ?> $service = $services["AddressBook"]; ?>
<addressBook type="<?php echo strtolower($card["Type"]); ?>"> <addressBook type="<?php echo strtolower($service["Type"]); ?>">
<username><?php echo $email_provided ? $email_address : "%EMAILADDRESS%";?></username> <username><?php echo $this->get_username($service,$email_address);?></username>
<authentication><?php echo $card["Authentication"] ? $card["Authentication"] : "http-basic" ;?></authentication> <authentication><?php echo $service["Authentication"];?></authentication>
<serverURL><?php echo $card["Server"];?></serverURL> <serverURL><?php echo $service["Server"];?></serverURL>
</addressBook> </addressBook>
<?php } <?php }
if ($conf["Calendar"] && $conf["Calendar"]["Enabled"]){ if ($services["Calendar"] && $services["Calendar"]["Enabled"]){
$cal = $conf["Calendar"] ;?> $service = $services["Calendar"] ;?>
<calendar type="<?php echo strtolower($cal["Type"]);?>"> <calendar type="<?php echo strtolower($service["Type"]);?>">
<username><?php echo $email_provided ? $email_address : "%EMAILADDRESS%";?></username> <username><?php echo $this->get_username($service,$email_address);?></username>
<authentication><?php echo $card["Authentication"] ? $card["Authentication"] : "http-basic" ;?></authentication> <authentication><?php echo $service["Authentication"];?></authentication>
<serverURL><?php echo $card["Server"];?></serverURL> <serverURL><?php echo $service["Server"];?></serverURL>
</calendar> </calendar>
<?php } <?php }
if ($conf["WebMail"] && $conf["WebMail"]["Enabled"]) { if ($services["WebMail"] && $services["WebMail"]["Enabled"]) {
$wm = $conf["WebMail"]; ?> $service = $services["WebMail"]; ?>
<webMail> <webMail>
<loginPage url="<?php echo $wm["Server"];?>" /> <loginPage url="<?php echo $service["Server"];?>" />
<loginPageInfo url="<?php echo $wm["Server"];?>"> <loginPageInfo url="<?php echo $service["Server"];?>">
<username><?php echo $email_provided ? $email_address : "%EMAILADDRESS%";?></username> <username><?php echo $this->get_username($service,$email_address);?></username>
<usernameField id="<?php echo $wm["UsernameDivID"];?>" name="<?php echo $wm["UsernameDivName"];?>" /> <usernameField id="<?php echo $service["UsernameDivID"];?>" name="<?php echo $service["UsernameDivName"];?>" />
<passwordField name="<?php echo $wm["PasswordDivName"];?>" /> <passwordField name="<?php echo $service["PasswordDivName"];?>" />
<loginButton id="<?php echo $wm["SubmitButtonID"];?>" name="<?php echo $wm["SubmitButtonName"];?>"/> <loginButton id="<?php echo $service["SubmitButtonID"];?>" name="<?php echo $service["SubmitButtonName"];?>"/>
</loginPageInfo> </loginPageInfo>
</webMail> </webMail>
<?php } ?> <?php } ?>