| version 1.24, 2023/12/19 15:35:42 |
version 1.25, 2023/12/23 22:04:13 |
|
|
| * 2007-03-09 - created |
* 2007-03-09 - created |
| * 2017-09-12 - implemented new SK-NIC-EPP/CentralNic export |
* 2017-09-12 - implemented new SK-NIC-EPP/CentralNic export |
| * 2017-10-07 - added import of expired domains (droplist) |
* 2017-10-07 - added import of expired domains (droplist) |
| |
* 2023-12-23 - use constructor debug option on multiple places |
| */ |
*/ |
| |
|
| /* $Platon: scripts/php/sk-nic/SK-NIC.php,v 1.23 2023/12/14 00:21:13 nepto Exp $ */ |
/* $Platon: scripts/php/sk-nic/SK-NIC.php,v 1.24 2023/12/19 15:35:42 nepto Exp $ */ |
| |
|
| require_once 'DB.php'; |
require_once 'DB.php'; |
| |
|
| class SK_NIC |
class SK_NIC |
| { |
{ |
| var $input_dt_format_sql = '%d.%m.%Y'; |
var $input_dt_format_sql = '%d.%m.%Y'; |
| var $_dsn = null; |
var $_debug = null; |
| var $_db = null; |
var $_dsn = null; |
| |
var $_db = null; |
| |
|
| |
public function __construct($dsn, $debug = false) // {{{ |
| |
{ |
| |
$this->_dsn = $dsn; |
| |
$this->_debug = intval($debug); |
| |
$pear_db_opts = array('debug' => ($debug ? 10 : 0)); |
| |
|
| public function __construct($dsn, $options = array('debug' => 10)) // {{{ |
$this->_db = DB::connect($this->_dsn, $pear_db_opts); |
| { |
if (DB::isError($this->_db)) { |
| $this->_dsn = $dsn; |
|
| |
|
| if (DB::isError($this->_db = DB::connect($this->_dsn, $options))) { |
|
| $this->error($this->_db); |
$this->error($this->_db); |
| } |
} |
| |
|
| $this->_db->setFetchMode(DB_FETCHMODE_ASSOC); |
$this->_db->setFetchMode(DB_FETCHMODE_ASSOC); |
| mysqli_options($this->_db->connection, MYSQLI_OPT_LOCAL_INFILE, 1); |
mysqli_options($this->_db->connection, MYSQLI_OPT_LOCAL_INFILE, 1); |
| |
|
| DB::isError($res = $this->_db->query('SET NAMES UTF8')) && $this->error($res); |
$res = $this->_db->query('SET NAMES UTF8'); |
| DB::isError($res = $this->_db->query('SET COLLATION_CONNECTION=UTF8_GENERAL_CI')) |
if (DB::isError($res)) { |
| && $this->error($res); |
$this->error($res); |
| |
} |
| |
|
| |
$res = $this->_db->query('SET COLLATION_CONNECTION=UTF8_GENERAL_CI'); |
| |
if (DB::isError($res)) { |
| |
$this->error($res); |
| |
} |
| } // }}} |
} // }}} |
| |
|
| function SK_NIC($dsn, $options = array('debug' => 10)) // {{{ |
function SK_NIC($dsn, $debug = false) // {{{ |
| { |
{ |
| self::__construct(); |
self::__construct(); |
| } // }}} |
} // }}} |
|
|
| $this->pk_header_del = 'Reg ID'; |
$this->pk_header_del = 'Reg ID'; |
| break; |
break; |
| default: |
default: |
| die(sprintf("%s() ERROR: unknown context: %s\n", __FUNCTION__, $context)); |
$this->error(sprintf("%s() ERROR: unknown context: %s", __FUNCTION__, $context)); |
| } |
} |
| } /* }}} */ |
} /* }}} */ |
| |
|
|
|
| return $this->setContext('registrar'); |
return $this->setContext('registrar'); |
| } |
} |
| |
|
| die(sprintf("%s() ERROR: unable to detect context from file: %s\n", __FUNCTION__, $basefile)); |
$this->error(sprintf("%s() ERROR: unable to detect context from file: %s", __FUNCTION__, $basefile)); |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function _tableColumns($table_name) /* {{{ */ |
function _tableColumns($table_name) /* {{{ */ |
|
|
| return $ar; |
return $ar; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function error($err_obj, $debug = 1) // {{{ |
function error($err_obj) // {{{ |
| { |
{ |
| if (is_object($err_obj)) { |
if (is_object($err_obj)) { |
| $error_string = $err_obj->getMessage(); |
$error_string = $err_obj->getMessage(); |
| if (! empty($debug)) { |
if (! empty($this->_debug)) { |
| $error_string .= "\n"; |
$error_string .= "\n"; |
| $error_string .= $err_obj->getDebugInfo(); |
$error_string .= $err_obj->getDebugInfo(); |
| } |
} |
|
|
| { |
{ |
| $query = 'SELECT MAX(dt_changed) FROM '.$this->table_current; |
$query = 'SELECT MAX(dt_changed) FROM '.$this->table_current; |
| $date = $this->_db->getOne($query); |
$date = $this->_db->getOne($query); |
| DB::isError($date) && $this->error($date); |
if (DB::isError($date)) { |
| |
$this->error($date); |
| |
} |
| return $date; |
return $date; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function doImport($filename, $changed_date = null) /* {{{ */ |
function doImport($filename, $changed_date = null) /* {{{ */ |
| { |
{ |
| |
$max_changed_date = $this->_getMaxChangedDate(); |
| if ($changed_date == null) { |
if ($changed_date == null) { |
| $changed_date = $this->_getChangedDateFromFilename($filename); |
$changed_date = $this->_getChangedDateFromFilename($filename); |
| |
if ($this->_debug) { |
| |
echo "INFO: max changed date in DB: $max_changed_date\n"; |
| |
echo " changed date from filename: $changed_date\n"; |
| |
} |
| |
} else { |
| |
if ($this->_debug) { |
| |
echo "INFO: max changed date in DB: $max_changed_date\n"; |
| |
echo " changed date as argument: $changed_date\n"; |
| |
} |
| } |
} |
| if (strncmp($changed_date, $this->_getMaxChangedDate(), 10) <= 0) { |
if (strncmp($changed_date, $max_changed_date, 10) <= 0) { |
| return false; |
return false; |
| } |
} |
| $this->_cleanImport(); |
$this->_cleanImport(); |
| $this->_writeImportWithLoadData($filename, $changed_date); |
$this->_writeImportWithLoadData($filename, $changed_date); |
| return true; |
return $changed_date; |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function checkImport() /* {{{ */ |
function checkImport() /* {{{ */ |
| { |
{ |
| $query = 'SELECT COUNT(*) FROM '.$this->table_import; |
$query = 'SELECT COUNT(*) FROM '.$this->table_import; |
| $count = $this->_db->getOne($query); |
$count = $this->_db->getOne($query); |
| DB::isError($count) && $this->error($count); |
if (DB::isError($count)) { |
| |
$this->error($count); |
| |
} |
| // here droplist shares limits with registrars (1000/9900) |
// here droplist shares limits with registrars (1000/9900) |
| $min_count = ($this->context == 'domain') ? 100000 : 1000; |
$min_count = ($this->context == 'domain') ? 100000 : 1000; |
| $max_count = ($this->context == 'domain') ? 900000 : 9900; |
$max_count = ($this->context == 'domain') ? 900000 : 9900; |
| if ($count < $min_count) { |
if ($count < $min_count) { |
| die(sprintf("%s() ERROR: too less imported %s records: %d (must be min %d)\n", |
if ($this->_debug) { |
| __FUNCTION__, $this->context, $count, $min_count)); |
echo sprintf("%s() ERROR: too less imported %s records: %d (must be min %d)\n", |
| |
__FUNCTION__, $this->context, $count, $min_count); |
| |
} |
| |
return false; |
| } |
} |
| if ($count > $max_count) { |
if ($count > $max_count) { |
| die(sprintf("%s() ERROR: too much imported %s records: %d (must be max 600000)\n", |
if ($this->_debug) { |
| __FUNCTION__, $this->context, $count, $max_count)); |
echo sprintf("%s() ERROR: too much imported %s records: %d (must be max 600000)\n", |
| |
__FUNCTION__, $this->context, $count, $max_count); |
| |
} |
| |
return false; |
| } |
} |
| return true; |
return true; |
| } /* }}} */ |
} /* }}} */ |
|
|
| function _cleanImport() /* {{{ */ |
function _cleanImport() /* {{{ */ |
| { |
{ |
| $result = $this->_db->query('TRUNCATE TABLE '.$this->table_import); |
$result = $this->_db->query('TRUNCATE TABLE '.$this->table_import); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function _importedSetStatusOK() /* {{{ */ |
function _importedSetStatusOK() /* {{{ */ |
| { |
{ |
| $query = 'UPDATE sknic_domains_import SET status="OK" WHERE status IS NULL'; |
$query = 'UPDATE sknic_domains_import SET status="OK" WHERE status IS NULL'; |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function _importedNameserversReorder() /* {{{ */ |
function _importedNameserversReorder() /* {{{ */ |
|
|
| $this->_db->quote($row[3]), |
$this->_db->quote($row[3]), |
| $this->_db->quote($domain)); |
$this->_db->quote($domain)); |
| $result2 = $this->_db->query($query); |
$result2 = $this->_db->query($query); |
| DB::isError($result2) && $this->error($result2); |
if (DB::isError($result2)) { |
| |
$this->error($result2); |
| |
} |
| } |
} |
| } /* }}} */ |
} /* }}} */ |
| |
|
|
|
| $this->_db->quote($dt_valid), $this->_db->quote($changed_date)); |
$this->_db->quote($dt_valid), $this->_db->quote($changed_date)); |
| |
|
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| } |
} |
| |
|
| fclose($f); |
fclose($f); |
|
|
| $this->_db->quote($this->input_dt_format_sql), |
$this->_db->quote($this->input_dt_format_sql), |
| $this->_db->quote($changed_date)); |
$this->_db->quote($changed_date)); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| // fixing duplicate of NS |
// fixing duplicate of NS |
| $query = sprintf('UPDATE %s SET ns4 = NULL WHERE ns4 IN (ns1, ns2, ns3)', |
$query = sprintf('UPDATE %s SET ns4 = NULL WHERE ns4 IN (ns1, ns2, ns3)', |
| $this->table_import); |
$this->table_import); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| $query = sprintf('UPDATE %s SET ns3 = NULL WHERE ns3 IN (ns1, ns2)', |
$query = sprintf('UPDATE %s SET ns3 = NULL WHERE ns3 IN (ns1, ns2)', |
| $this->table_import); |
$this->table_import); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| $query = sprintf('UPDATE %s SET ns2 = NULL WHERE ns2 = ns1', |
$query = sprintf('UPDATE %s SET ns2 = NULL WHERE ns2 = ns1', |
| $this->table_import); |
$this->table_import); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| $query = sprintf('UPDATE %s SET ns1 = NULL WHERE LENGTH(ns1) <= 0', |
$query = sprintf('UPDATE %s SET ns1 = NULL WHERE LENGTH(ns1) <= 0', |
| $this->table_import); |
$this->table_import); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| } elseif ($this->context == 'droplist') { |
} elseif ($this->context == 'droplist') { |
| $query = sprintf('LOAD DATA LOCAL INFILE %s' |
$query = sprintf('LOAD DATA LOCAL INFILE %s' |
| .' INTO TABLE %s' |
.' INTO TABLE %s' |
|
|
| $this->table_import, |
$this->table_import, |
| $this->_db->quote($changed_date)); |
$this->_db->quote($changed_date)); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| } elseif ($this->context == 'registrar') { |
} elseif ($this->context == 'registrar') { |
| $this->_db->query('SET NAMES UTF8'); |
$this->_db->query('SET NAMES UTF8'); |
| $query = sprintf('LOAD DATA LOCAL INFILE %s' |
$query = sprintf('LOAD DATA LOCAL INFILE %s' |
|
|
| $this->table_import, |
$this->table_import, |
| $this->_db->quote($changed_date)); |
$this->_db->quote($changed_date)); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| } else { |
} else { |
| die(sprintf("%s() ERROR: unknown context: %s\n", __FUNCTION__, $context)); |
$this->error(sprintf("%s() ERROR: unknown context: %s", __FUNCTION__, $context)); |
| } |
} |
| |
|
| // Remove all lines started with "--" |
// Remove all lines started with "--" |
| $query = sprintf('DELETE FROM %s WHERE %s LIKE "-- %%"', |
$query = sprintf('DELETE FROM %s WHERE %s LIKE "-- %%"', |
| $this->table_import, $this->primary_key); |
$this->table_import, $this->primary_key); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| // Remove table headers (we want just data) |
// Remove table headers (we want just data) |
| $query = sprintf('DELETE FROM %s WHERE %s = %s LIMIT 1', |
$query = sprintf('DELETE FROM %s WHERE %s = %s LIMIT 1', |
| $this->table_import, $this->primary_key, |
$this->table_import, $this->primary_key, |
| $this->_db->quote($this->pk_header_del)); |
$this->_db->quote($this->pk_header_del)); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function _writeHistory($rec, $deleted = 0) /* {{{ */ |
function _writeHistory($rec, $deleted = 0) /* {{{ */ |
|
|
| $this->_db->popExpect(); |
$this->_db->popExpect(); |
| if (DB::isError($result)) { |
if (DB::isError($result)) { |
| if ($result->getCode() == DB_ERROR_ALREADY_EXISTS) { |
if ($result->getCode() == DB_ERROR_ALREADY_EXISTS) { |
| printf("%s WARNING: %s entry already exists\n", |
printf("%s() WARNING: %s entry already exists\n", |
| __FUNCTION__, $this->table_history); |
__FUNCTION__, $this->table_history); |
| } else { |
} else { |
| $this->error($result); |
$this->error($result); |
|
|
| $this->primary_key, |
$this->primary_key, |
| $this->_db->quote($rec["c_$this->primary_key"])); |
$this->_db->quote($rec["c_$this->primary_key"])); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function _insertRecord($rec) /* {{{ */ |
function _insertRecord($rec) /* {{{ */ |
|
|
| $query = sprintf('INSERT INTO %s (%s) VALUES (%s)', |
$query = sprintf('INSERT INTO %s (%s) VALUES (%s)', |
| $this->table_current, $cols, $vals); |
$this->table_current, $cols, $vals); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function doComparison($debug = false) /* {{{ */ |
function doComparison() /* {{{ */ |
| { |
{ |
| |
$new = 0; |
| |
$deleted = 0; |
| $changed = 0; |
$changed = 0; |
| $import_cols = $this->_tableColumns($this->table_import); |
$import_cols = $this->_tableColumns($this->table_import); |
| $current_cols = $this->_tableColumns($this->table_current); |
$current_cols = $this->_tableColumns($this->table_current); |
|
|
| $subqryres = $this->_db->query(sprintf($query, $subquery)); |
$subqryres = $this->_db->query(sprintf($query, $subquery)); |
| while ($subqryres->fetchInto($rec)) { |
while ($subqryres->fetchInto($rec)) { |
| if (@strlen($rec["c_$this->primary_key"]) <= 0) { // new domain |
if (@strlen($rec["c_$this->primary_key"]) <= 0) { // new domain |
| if ($debug) { |
if ($this->_debug >= 2) { |
| echo 'NEW: ',$rec["i_domain"],"\n"; |
echo 'NEW: ',$rec["i_domain"],"\n"; |
| } |
} |
| $changed++; |
$new++; |
| $this->_insertRecord($rec); |
$this->_insertRecord($rec); |
| continue; |
continue; |
| } |
} |
| if (@strlen($rec["i_$this->primary_key"]) <= 0) { // deleted domain |
if (@strlen($rec["i_$this->primary_key"]) <= 0) { // deleted domain |
| if ($debug) { |
if ($this->_debug >= 2) { |
| echo 'DELETED: ',$rec["c_domain"],"\n"; |
echo 'DELETED: ',$rec["c_domain"],"\n"; |
| } |
} |
| $changed++; |
$deleted++; |
| $this->_writeHistory($rec, 1); |
$this->_writeHistory($rec, 1); |
| $query = sprintf('DELETE FROM %s WHERE %s = %s', |
$query = sprintf('DELETE FROM %s WHERE %s = %s', |
| $this->table_current, |
$this->table_current, |
| $this->primary_key, |
$this->primary_key, |
| $this->_db->quote($rec["c_$this->primary_key"])); |
$this->_db->quote($rec["c_$this->primary_key"])); |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| continue; |
continue; |
| } |
} |
| $changed_cols = array(); |
$changed_cols = array(); |
|
|
| if (count($changed_cols) == 1 && $changed_cols[0] = 'dt_changed') { |
if (count($changed_cols) == 1 && $changed_cols[0] = 'dt_changed') { |
| // do nothing |
// do nothing |
| } elseif (count($changed_cols) > 0) { |
} elseif (count($changed_cols) > 0) { |
| if ($debug) { |
if ($this->_debug >= 2) { |
| echo 'CHANGED: ',$rec['i_domain']; |
echo 'CHANGED: ',$rec['i_domain']; |
| echo ' [', join('][',$changed_cols),"]\n"; |
echo ' [', join('][',$changed_cols),"]\n"; |
| } |
} |
|
|
| } |
} |
| $subqryres->free(); |
$subqryres->free(); |
| } |
} |
| return $changed; |
if ($this->_debug) { |
| |
echo sprintf("INFO: %s() stats: %d new, %d changed, %d deleted\n", |
| |
__FUNCTION__, $new, $changed, $deleted); |
| |
} |
| |
return ($new + $changed + $deleted); |
| } /* }}} */ |
} /* }}} */ |
| |
|
| function writeStats() /* {{{ */ |
function writeStats() /* {{{ */ |
|
|
| |
|
| foreach ($queries as $query) { |
foreach ($queries as $query) { |
| $result = $this->_db->query($query); |
$result = $this->_db->query($query); |
| DB::isError($result) && $this->error($result); |
if (DB::isError($result)) { |
| |
$this->error($result); |
| |
} |
| } |
} |
| } /* }}} */ |
} /* }}} */ |
| } |
} |