enhance domain-importer, fixes #1512

Signed-off-by: Michael Kaufmann (d00p) <d00p@froxlor.org>
This commit is contained in:
Michael Kaufmann (d00p)
2015-03-06 20:28:41 +01:00
parent 5dce0db661
commit c150d559c7

View File

@@ -24,7 +24,8 @@
* @author Michael Kaufmann (d00p) <d00p@froxlor.org>
*
*/
class DomainBulkAction {
class DomainBulkAction
{
/**
* complete path including filename of file to be imported
@@ -74,19 +75,25 @@ class DomainBulkAction {
* @var array
*/
private $_required_fields = array (
'domain',
'documentroot',
'isbinddomain',
'isemaildomain',
'email_only',
'iswildcarddomain',
'subcanemaildomain',
'caneditdomain',
'wwwserveralias',
'specialsettings',
'ssl_redirect',
'registration_date',
'ips',
/* 1 */ 'domain',
/* 2 */ 'documentroot',
/* 3 */ 'aliasdomain',
/* 4 */ 'isbinddomain',
/* 5 */ 'isemaildomain',
/* 6 */ 'email_only',
/* 7 */ 'iswildcarddomain',
/* 8 */ 'subcanemaildomain',
/* 9 */ 'caneditdomain',
/* 10 */ 'zonefile',
/* 11 */ 'wwwserveralias',
/* 12 */ 'openbasedir',
/* 13 */ 'speciallogfile',
/* 14 */ 'specialsettings',
/* 15 */ 'ssl_redirect',
/* 16 */ 'use_ssl',
/* 17 */ 'registration_date',
/* 18 */ 'ips',
/* automatically added */
'adminid',
'customerid',
'add_date'
@@ -98,6 +105,7 @@ class DomainBulkAction {
* @var PDOStatement
*/
private $_ins_stmt = null;
private $_ipp_ins_stmt = null;
/**
@@ -108,13 +116,12 @@ class DomainBulkAction {
*
* @return object DomainBulkAction instance
*/
public function __construct($import_file = null, $customer_id = 0) {
if (!empty($import_file)) {
public function __construct($import_file = null, $customer_id = 0)
{
if (! empty($import_file)) {
$this->_impFile = makeCorrectFile($import_file);
}
$this->_custId = $customer_id;
}
/**
@@ -126,7 +133,8 @@ class DomainBulkAction {
*
* @return array 'all' => amount of records processed, 'imported' => number of imported records
*/
public function doImport($separator = ";", $offset = 0) {
public function doImport($separator = ";", $offset = 0)
{
// get the admins userinfo to check for domains_used, etc.
global $userinfo;
@@ -137,8 +145,8 @@ class DomainBulkAction {
$dom_unlimited = false;
}
$domains_used = (int)$userinfo['domains_used'];
$domains_avail = (int)$userinfo['domains'];
$domains_used = (int) $userinfo['domains_used'];
$domains_avail = (int) $userinfo['domains'];
if (empty($separator) || strlen($separator) != 1) {
throw new Exception("Invalid separator specified: '" . $separator . "'");
@@ -178,13 +186,17 @@ class DomainBulkAction {
`adminid` = :adminid,
`customerid` = :customerid,
`documentroot` = :documentroot,
`aliasdomain` = :aliasdomain,
`isbinddomain` = :isbinddomain,
`isemaildomain` = :isemaildomain,
`email_only` = :email_only,
`iswildcarddomain` = :iswildcarddomain,
`subcanemaildomain` = :subcanemaildomain,
`caneditdomain` = :caneditdomain,
`zonefile` = :zonefile,
`wwwserveralias` = :wwwserveralias,
`openbasedir` = :openbasedir,
`speciallogfile` = :speciallogfile,
`specialsettings` = :specialsettings,
`ssl_redirect` = :ssl_redirect,
`registration_date` = :registration_date,
@@ -217,12 +229,11 @@ class DomainBulkAction {
$global_counter ++;
}
return array (
return array(
'all' => $global_counter,
'imported' => $import_counter,
'notice' => $note
);
}
/**
@@ -232,10 +243,9 @@ class DomainBulkAction {
*
* @return void
*/
public function setImportFile($import_file = null) {
public function setImportFile($import_file = null)
{
$this->_impFile = makeCorrectFile($import_file);
}
/**
@@ -245,10 +255,9 @@ class DomainBulkAction {
*
* @return void
*/
public function setCustomer($customer_id = 0) {
public function setCustomer($customer_id = 0)
{
$this->_custId = $customer_id;
}
/**
@@ -258,11 +267,12 @@ class DomainBulkAction {
*
* @return int last-inserted id or false on error
*/
private function _addSingleDomainToDatabase($domain_data = array()) {
private function _addSingleDomainToDatabase($domain_data = array())
{
// format domain
$idna_convert = new idna_convert_wrapper();
$domain_data['domain'] = $idna_convert->encode(preg_replace(array (
$domain_data['domain'] = $idna_convert->encode(preg_replace(array(
'/\:(\d)+$/',
'/^https?\:\/\//'
), '', $domain_data['domain']));
@@ -277,11 +287,54 @@ class DomainBulkAction {
return false;
}
// no existing domains
// no existing domains can be imported
if (in_array($domain_data['domain'], $this->_knownDomains)) {
return false;
}
// check for alias-domain
if (! empty($domain_data['aliasdomain'])) {
// format
$domain_data['aliasdomain'] = $idna_convert->encode(preg_replace(array(
'/\:(\d)+$/',
'/^https?\:\/\//'
), '', $domain_data['aliasdomain']));
// validate alias-domain
if (! validateDomain($domain_data['aliasdomain'])) {
// invalid-domain lol - skip to be sure we dont add anything weird
return false;
}
// does the domain we want to be an alias of exists?
if (! in_array($domain_data['aliasdomain'], $this->_knownDomains)) {
// it does not - User should respect the order of import so if the domain
// he wants to alias is also part of the import is ABOVE this one
// - we'd better skip
return false;
}
}
// check for use_ssl and ssl_redirect
if (!isset($domain_data['use_ssl']) || $domain_data['use_ssl'] == 1) {
// if not set: default is whatever the system says
// if set to 1: set to 0 if system has no ssl enabled
$domain_data['use_ssl'] = (Settings::get('system.use_ssl') == 1 ? 1 : 0);
}
// use_ssl flag
if ($domain_data['use_ssl'] != 1) {
$domain_data['use_ssl'] = 0;
}
// ssl_redirect flag
if ($domain_data['ssl_redirect'] != 1) {
$domain_data['ssl_redirect'] = 0;
}
// if use_ssl is 0 ssl_redirect must be too (no ssl-ip => no ssl-redirect)
if ($domain_data['use_ssl'] == 0 && $domain_data['ssl_redirect'] == 1) {
$domain_data['ssl_redirect'] = 0;
}
// add to known domains
$this->_knownDomains[] = $domain_data['domain'];
@@ -297,15 +350,40 @@ class DomainBulkAction {
$domain_data['isbinddomain'] = 0;
}
// zonefile
if (!isset($domain_data['zonefile'])) {
$domain_data['zonefile'] = "";
} else {
if (!empty($domain_data['zonefile']) && Settings::Get('system.bind_enable') == '1') {
$domain_data['zonefile'] = makeCorrectFile($domain_data['zonefile']);
} else {
$domain_data['zonefile'] = "";
}
}
// openbasedir flag
if (! isset($domain_data['openbasedir'])) {
$domain_data['openbasedir'] = 1;
} elseif ($domain_data['openbasedir'] != 1) {
$domain_data['openbasedir'] = 0;
}
// speciallogfile flag
if (! isset($domain_data['speciallogfile'])) {
$domain_data['speciallogfile'] = 0;
} elseif ($domain_data['speciallogfile'] != 1) {
$domain_data['speciallogfile'] = 0;
}
/*
* automatically set values (not from the file)
*/
// add date
$domain_data['add_date'] = time();
// set adminid
$domain_data['adminid'] = $this->_custData['adminid'];
$domain_data['adminid'] = (int)$this->_custData['adminid'];
// set customerid
$domain_data['customerid'] = $this->_custId;
$domain_data['customerid'] = (int)$this->_custId;
// check for required fields
foreach ($this->_required_fields as $rfld) {
@@ -334,6 +412,11 @@ class DomainBulkAction {
// dont need that for the domain-insert-statement
unset($domain_data['ips']);
// remember use_ssl value
$use_ssl = (bool)$domain_data['use_ssl'];
// dont need that for the domain-insert-statement
unset($domain_data['use_ssl']);
// finally ADD the domain to panel_domains
Database::pexecute($this->_ins_stmt, $domain_data);
@@ -351,8 +434,12 @@ class DomainBulkAction {
// if we know the ip, at all variants (different ports, ssl and non-ssl) of it!
if (isset($this->_knownIpPort[$ip])) {
foreach ($this->_knownIpPort[$ip] as $ipdata) {
// no ssl ip/ports should be used for this domain
if ($use_ssl == false && $ipdata['ssl'] == 1) {
continue;
}
// add domain->ip reference
Database::pexecute($this->_ipp_ins_stmt, array (
Database::pexecute($this->_ipp_ins_stmt, array(
'domid' => $domain_id,
'ipid' => $ipdata['id']
));
@@ -361,7 +448,6 @@ class DomainBulkAction {
}
return $domain_id;
}
/**
@@ -372,8 +458,8 @@ class DomainBulkAction {
*
* @return array
*/
private function _parseImportFile($separator = ";") {
private function _parseImportFile($separator = ";")
{
if (empty($this->_impFile)) {
throw new Exception("No file was given for import");
}
@@ -386,16 +472,16 @@ class DomainBulkAction {
throw new Exception("Unable to read file '" . $this->_impFile . "'");
}
$file_data = array ();
$file_data = array();
$fh = @fopen($this->_impFile, "r");
if ($fh) {
while (($line = fgets($fh)) !== false) {
$tmp_arr = explode($separator, $line);
$data_arr = array ();
$data_arr = array();
foreach ($tmp_arr as $idx => $data) {
// dont include more fields that the 13 we use
if ($idx > 12)
// dont include more fields that the ones we use
if ($idx > (count($this->_required_fields) - 4)) // off-by-one + 3 auto-values
break;
$data_arr[$this->_required_fields[$idx]] = $data;
}
@@ -407,7 +493,6 @@ class DomainBulkAction {
fclose($fh);
return $file_data;
}
/**
@@ -415,10 +500,10 @@ class DomainBulkAction {
*
* @return bool
*/
private function _readCustomerData() {
private function _readCustomerData()
{
$cust_stmt = Database::prepare("SELECT * FROM `" . TABLE_PANEL_CUSTOMERS . "` WHERE `customerid` = :cid");
$this->_custData = Database::pexecute_first($cust_stmt, array (
$this->_custData = Database::pexecute_first($cust_stmt, array(
'cid' => $this->_custId
));
if (is_array($this->_custData) && isset($this->_custData['customerid']) && $this->_custData['customerid'] == $this->_custId) {
@@ -426,7 +511,6 @@ class DomainBulkAction {
}
$this->_custData = null;
return false;
}
/**
@@ -434,15 +518,14 @@ class DomainBulkAction {
*
* @return void
*/
private function _readDomainData() {
private function _readDomainData()
{
$knowndom_stmt = Database::prepare("SELECT `domain` FROM `" . TABLE_PANEL_DOMAINS . "` ORDER BY `domain` ASC");
Database::pexecute($knowndom_stmt);
$this->_knownDomains = array ();
$this->_knownDomains = array();
while ($dom = $knowndom_stmt->fetch()) {
$this->_knownDomains[] = $dom['domain'];
}
}
/**
@@ -450,16 +533,14 @@ class DomainBulkAction {
*
* @return void
*/
private function _readIpPortData() {
private function _readIpPortData()
{
$knownip_stmt = Database::prepare("SELECT * FROM `" . TABLE_PANEL_IPSANDPORTS . "`");
Database::pexecute($knownip_stmt);
$this->_knownIpPort = array ();
$this->_knownIpPort = array();
while ($ipp = $knownip_stmt->fetch()) {
$this->_knownIpPort[$ipp['ip']][] = $ipp;
$this->_knownIpPortChk[] = $ipp['ip'];
}
}
}