Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Thursday, October 15, 2026

Diff for scripts/php/sk-nic/SK-NIC.php between version 1.24 and 1.25

version 1.24, 2023/12/19 15:35:42 version 1.25, 2023/12/23 22:04:13
Line 12 
Line 12 
  * 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();
         } // }}}          } // }}}
Line 77  class SK_NIC
Line 89  class SK_NIC
                                 $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));
                 }                  }
         } /* }}} */          } /* }}} */
   
Line 92  class SK_NIC
Line 104  class SK_NIC
                         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) /* {{{ */
Line 105  class SK_NIC
Line 117  class SK_NIC
                 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();
                         }                          }
Line 136  class SK_NIC
Line 148  class SK_NIC
         {          {
                 $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;
         } /* }}} */          } /* }}} */
Line 175  class SK_NIC
Line 207  class SK_NIC
         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() /* {{{ */
Line 208  class SK_NIC
Line 244  class SK_NIC
                                         $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);
                           }
                 }                  }
         } /* }}} */          } /* }}} */
   
Line 265  class SK_NIC
Line 303  class SK_NIC
                                         $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);
Line 302  class SK_NIC
Line 342  class SK_NIC
                                         $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'
Line 331  class SK_NIC
Line 381  class SK_NIC
                                         $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'
Line 344  class SK_NIC
Line 396  class SK_NIC
                                         $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) /* {{{ */
Line 390  class SK_NIC
Line 448  class SK_NIC
                 $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);
Line 411  class SK_NIC
Line 469  class SK_NIC
                                 $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) /* {{{ */
Line 431  class SK_NIC
Line 491  class SK_NIC
                 $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);
Line 454  class SK_NIC
Line 518  class SK_NIC
                         $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();
Line 484  class SK_NIC
Line 550  class SK_NIC
                                 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";
                                         }                                          }
Line 495  class SK_NIC
Line 561  class SK_NIC
                         }                          }
                         $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() /* {{{ */
Line 578  class SK_NIC
Line 648  class SK_NIC
   
                 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);
                           }
                 }                  }
         } /* }}} */          } /* }}} */
 }  }

Legend:
Removed from v.1.24  
changed lines
  Added in v.1.25

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top