<? /*
// File: sendmail.sys.phps
// Purpose: sendmail system update
// Author: Felix <hide@address.com>
*/
// Domain Update
if ($type == "domain") {
// Load current files
$sysfile['sendmail.cw'] = implode("",file($cfg['mail_sendmail']));
$sysfile['virtusertable'] = implode("",file($cfg['mail_virtuser']));
$sysfile['access'] = implode("",file($cfg['mail_access']));
// Load file headers
$sysfile['sendmail.cw-header'] = implode("",file($cfg['basedir']."/headers/localhosts"));
$sysfile['access-header'] = implode("",file($cfg['basedir']."/headers/access"));
$sysfile['access-deny'] = implode("",file($cfg['basedir']."/headers/access-deny"));
$temp = explode("\n### DO NOT MODIFY ###\n",$sysfile['virtusertable']);
if (is_array($temp))
$sysfile['virtusertable-header'] = $temp[0];
else $sysfile['virtusertable-header'] = "";
// New files
$newfile['sendmail.cw'] = '# This file is automatically generated by web://cp
# Please make custom changes to '.$cfg['basedir'].'/headers/localhosts
# ================================================
'.$sysfile['sendmail.cw-header'].'
';
$newfile['access'] = '# This file is automatically generated by web://cp
# Please make custom changes to '.$cfg['basedir'].'/headers/access
# ================================================
'.$sysfile['access-header'].'
'.$sysfile['access-deny'].'
';
$newfile['virtusertable'] = $sysfile['virtusertable-header']."\n### DO NOT MODIFY ###\n";
// Initialize reseller ID
unset($oldowner);
// make mail config
$dbp = mysql_query("SELECT * FROM domains WHERE type='domain' ORDER BY priority, owner, domain") or die(mysql_error());
while ($data = mysql_fetch_array($dbp)) {
// Prepare sendmail.cw, access, virtusertable
$newfile['access'] .= $data['host'].".".$data['domain']." RELAY\n";
$newfile['sendmail.cw'] .= $data['host'].".".$data['domain']."\n";
// if domain is an alias, include it
// GYRBO - always make @domain.com work
//if ($data['nohost'] == "true") {
$newfile['sendmail.cw'] .= $data['domain']."\n";
$newfile['access'] .= $data['domain']." RELAY\n";
$newfile['virtusertable'] .= "@".$data['host'].".".$data['domain']." %1@".$data['domain']."\n";
//}
// If no user has catch-all, set a catch-all error (Unknown Address)
// GYRBO - remove host
$dbp1 = mysql_query("SELECT username FROM users WHERE id = '".$data['id']."' AND catchall = 'true'");
if (!mysql_num_rows($dbp1))
$newfile['virtusertable'] .= "@".$data['domain']." error:nouser Unknown E-Mail Address\n";
// if there is aliases, include them
$data['aliases'] = trim($data['aliases']);
if (strstr($data['aliases']," ")) {
$aliases = explode(" ",$data['aliases']);
while (current($aliases)) {
$newfile['sendmail.cw'] .= current($aliases).".".$data['domain']."\n";
$newfile['access'] .= current($aliases).".".$data['domain']." RELAY\n";
$newfile['virtusertable'] .= "@".current($aliases).".".$data['domain']." %1@".$data['domain']."\n";
next($aliases);
}
}
elseif ($data['aliases']) {
$newfile['sendmail.cw'] .= $data['aliases'].".".$data['domain']."\n";
$newfile['access'] .= $data['aliases'].".".$data['domain']." RELAY\n";
$newfile['virtusertable'] .= "@".current($aliases).".".$data['domain']." %1@".$data['domain']."\n";
}
// Check if domain has pointers / sub-domains, set them here
$dbp2 = mysql_query("SELECT * FROM domains WHERE owner='".$data['id']."' ORDER BY domain");
while ($subdata = mysql_fetch_array($dbp2)) {
// :: point @domain-pointer.com to >> @main-domain.com
// GYRBO - always make this work
// @domain.com
$newfile['virtusertable'] .= "@".$subdata['domain']." %1@".$data['domain']."\n";
$newfile['access'] .= $subdata['domain']." RELAY\n";
$newfile['sendmail.cw'] .= $subdata['domain']."\n";
// @www.domain.com
$newfile['virtusertable'] .= "@".$subdata['host'].".".$subdata['domain']." %1@".$data['domain']."\n";
$newfile['access'] .= $subdata['host'].".".$subdata['domain']." RELAY\n";
$newfile['sendmail.cw'] .= $subdata['host'].".".$subdata['domain']."\n";
}
}
// Check if there was really any update and signal which service to restart
if ($newfile['sendmail.cw'] != $sysfile['sendmail.cw']) {
// backup first:
if (!file_exists($cfg['mail_sendmail'].".wcp.bak"))
copy($cfg['mail_sendmail'], $cfg['mail_sendmail'].".wcp.bak");
$fp = fopen($cfg['mail_sendmail'],"w+");
fwrite($fp,$newfile['sendmail.cw']);
$restartmail = TRUE;
fclose($fp);
}
if ($newfile['virtusertable'] != $sysfile['virtusertable']) {
// backup first:
if (!file_exists($cfg['mail_virtuser'].".wcp.bak"))
copy($cfg['mail_virtuser'], $cfg['mail_virtuser'].".wcp.bak");
$fp = fopen($cfg['mail_virtuser'],"w+");
fwrite($fp,$newfile['virtusertable']);
$remapmail = TRUE;
fclose($fp);
}
if ($newfile['access'] != $sysfile['access']) {
// backup first:
if (!file_exists($cfg['mail_access'].".wcp.bak"))
copy($cfg['mail_access'], $cfg['mail_access'].".wcp.bak");
$fp = fopen($cfg['mail_access'],"w+");
fwrite($fp,$newfile['access']);
$remapmail = TRUE;
fclose($fp);
}
}
// end of updatesys("domain");
///
// User Update
elseif ($type == "user") {
// Load current files
$sysfile['alias'] = implode("",file($cfg['mail_aliases']));
$sysfile['virtusertable'] = implode("",file($cfg['mail_virtuser']));
// Load file headers
$sysfile['alias-header'] = implode("",file($cfg['basedir']."/headers/aliases"));
$temp = explode("\n### DO NOT MODIFY ###\n",$sysfile['virtusertable']);
if (is_array($temp))
$sysfile['virtusertable-footer'] = $temp[1];
else $sysfile['virtusertable-footer'] = "";
// New files
$newfile['alias'] = '# This file is automatically generated by web://cp
# Please make custom changes to '.$cfg['basedir'].'/headers/alias
# ================================================
'.$sysfile['alias-header'].'
';
$newfile['virtusertable'] = '# This file is automatically generated by web://cp
# Please make custom changes with the web panel
# ================================================
';
// get domain names to map virtual mail
unset($userdom);
unset($domalias);
$dbp = mysql_query("SELECT id, host, domain FROM domains");
while ($data = mysql_fetch_array($dbp)) {
//GYRBO - without the host. part
$userdom[$data['id']] = $data['domain'];
//GYRBO - prevent double aliases, etc
$domalias[$data['id']] = array();
}
// Generate sorted virtusertable file
$newfile['virtusertable'] .= "## Virtual Users Mapping\n";
$dbp = mysql_query("SELECT aliases,username,id FROM users WHERE type != 'demo' ORDER BY username");
while ($data = mysql_fetch_array($dbp)) {
// write entry for the main user
$newfile['virtusertable'] .= $data['username']."@".$userdom[$data['id']]." ".$data['username']."\n";
// GYRBO - make sure username isn't being used as an alias
$domalias[$data['id']][] = $data['username'];
// if there are aliases, include them
$data['aliases'] = trim($data['aliases']);
if (strstr($data['aliases']," ")) {
$aliases = explode(" ",$data['aliases']);
while (current($aliases)) {
//GYRBO - fairly extreme code to prevent double aliases
if(!in_array(current($aliases), $domalias[$data['id']])) {
//GYRBO - this may be pretty redudant, be it's also the most dangerous part
$dbpu = mysql_query("SELECT id FROM users WHERE type != 'demo' AND username = '".current($aliases)."' AND id = '".$data['id']."' LIMIT 1");
if(mysql_num_rows($dbpu)==0)
$newfile['virtusertable'] .= current($aliases)."@".$userdom[$data['id']]." ".$data['username']."\n";
//GYRBO - add alias to list
$domalias[$data['id']][] = current($aliases);
}
next($aliases);
}
}elseif ($data['aliases']) {
//GYRBO - fairly extreme code to prevent double aliases
if(!in_array($data['aliases'], $domalias[$data['id']])) {
//GYRBO - this may be pretty redudant, be it's also the most dangerous part
$dbpu = mysql_query("SELECT id FROM users WHERE type != 'demo' AND username = '".$data['aliases']."' AND id = '".$data['id']."' LIMIT 1");
if(mysql_num_rows($dbpu)==0)
$newfile['virtusertable'] .= $data['aliases']."@".$userdom[$data['id']]." ".$data['username']."\n";
//GYRBO - add alias to list
$domalias[$data['id']][] = $data['aliases'];
}
}
}
// Generate Catch-All
$newfile['virtusertable'] .= "\n\n## Catch-All to Users Mapping\n";
$dbp = mysql_query("SELECT id,username FROM users WHERE catchall = 'true' and type != 'demo' ORDER BY username");
while ($data = mysql_fetch_array($dbp))
$newfile['virtusertable'] .= "@".$userdom[$data['id']]." ".$data['username']."\n";
// finalize virtusertable
$newfile['virtusertable'] .= "\n### DO NOT MODIFY ###\n".$sysfile['virtusertable-footer'];
// Generate alias file
$dbp = mysql_query("SELECT autoreply,forward,username,aliases,catchall FROM users WHERE type != 'demo' ORDER BY username");
while ($data = mysql_fetch_array($dbp)) {
$data['autoreply'] = trim($data['autoreply']);
$data['forward'] = trim($data['forward']);
// Check for autoreply or forward, else don't do anything
if ($data['autoreply'] OR $data['forward']) {
$newfile['alias'] .= $data['username'].":";
if ($data['autoreply']) {
$newfile['alias'] .= ' "|'.$cfg['basedir'].'/server/autoreply.php '.$data['username'];
if ($data['catchall'] == 'true')
$newfile['alias'] .= " *";
elseif (trim($data['aliases']));
$newfile['alias'] .= ' '.str_replace(" ",",",trim($data['aliases']));
$newfile['alias'] .= '", \\'.$data['username'];
}
if ($data['autoreply'] AND $data['forward'])
$newfile['alias'] .= ", ";
if ($data['forward'])
$newfile['alias'] .= " ".str_replace(" ",", ",$data['forward']);
$newfile['alias'] .= "\n";
}
}
// Mailman Code
if ($cfg['maillist'] == 'mailman') {
$newfile['alias'] .= "## Mailman Mailing lists\n";
$dbp = mysql_query('SELECT listname,domain FROM mailman WHERE action = ""');
while($data = mysql_fetch_array($dbp)) {
$name = strtolower($data['listname']);
$newfile['alias'] .= $name.": \"|".$cfg['mm_wrap']." post $name\"\n";
$newfile['alias'] .= $name."-admin: \"|".$cfg['mm_wrap']."mailowner $name\"\n";
$newfile['alias'] .= $name."-request: \"|".$cfg['mm_wrap']." mailcmd $name\"\n";
// $newfile['alias'] .= $name."-owner: ".$name."-admin\n";
//GYRBO - add proper virtusertable entries, I should have done this
//for the inital release
if(!in_array($name, $domalias[$data['domain']])) {
//GYRBO - this may be pretty redudant, be it's also the most dangerous part
$dbpu = mysql_query("SELECT id FROM users WHERE type != 'demo' AND username = '".$name."' LIMIT 1");
if(mysql_num_rows($dbpu)==0)
$newfile['virtusertable'] .= $name."@".$userdom[$data['id']]." ".$name."\n";
//GYRBO - add alias to list
$domalias[$data['id']][] = $name;
}
if(!in_array($name.'-admin', $domalias[$data['domain']])) {
//GYRBO - this may be pretty redudant, be it's also the most dangerous part
$dbpu = mysql_query("SELECT id FROM users WHERE type != 'demo' AND username = '".$name.'-admin'."' LIMIT 1");
if(mysql_num_rows($dbpu)==0)
$newfile['virtusertable'] .= $name.'-admin'."@".$userdom[$data['id']]." ".$name.'-admin'."\n";
//GYRBO - add alias to list
$domalias[$data['id']][] = $name.'-admin';
}
if(!in_array($name.'-request', $domalias[$data['domain']])) {
//GYRBO - this may be pretty redudant, be it's also the most dangerous part
$dbpu = mysql_query("SELECT id FROM users WHERE type != 'demo' AND username = '".$name.'-request'."' LIMIT 1");
if(mysql_num_rows($dbpu)==0)
$newfile['virtusertable'] .= $name.'-request'."@".$userdom[$data['id']]." ".$name.'-request'."\n";
//GYRBO - add alias to list
$domalias[$data['id']][] = $name.'-request';
}
if(!in_array($name.'-owner', $domalias[$data['domain']])) {
//GYRBO - this may be pretty redudant, be it's also the most dangerous part
$dbpu = mysql_query("SELECT id FROM users WHERE type != 'demo' AND username = '".$name.'-owner'."' LIMIT 1");
if(mysql_num_rows($dbpu)==0)
$newfile['virtusertable'] .= $name.'-owner'."@".$userdom[$data['id']]." ".$name.'-request@'.$userdom[$data['id']]."\n";
//GYRBO - add alias to list
$domalias[$data['id']][] = $name.'-owner';
}
}
}
// Compare system new & current system files
if ($newfile['virtusertable'] != $sysfile['virtusertable']) {
// backup first:
if (!file_exists($cfg['mail_sendmail'].".wcp.bak"))
copy($cfg['mail_sendmail'], $cfg['mail_sendmail'].".wcp.bak");
$fp = fopen($cfg['mail_virtuser'],"w+");
fwrite($fp,$newfile['virtusertable']);
$remapmail = TRUE;
fclose($fp);
}
if ($newfile['alias'] != $sysfile['alias']) {
// backup first:
if (!file_exists($cfg['mail_aliases'].".wcp.bak"))
copy($cfg['mail_aliases'], $cfg['mail_aliases'].".wcp.bak");
$fp = fopen($cfg['mail_aliases'],"w+");
fwrite($fp,$newfile['alias']);
$remapmail = TRUE;
fclose($fp);
}
}
// end of updatesys("user");
///
?>