Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
interface/web/tools/dns_import_tupa.php
@@ -27,8 +27,8 @@
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
require_once('../../lib/config.inc.php');
require_once('../../lib/app.inc.php');
require_once '../../lib/config.inc.php';
require_once '../../lib/app.inc.php';
//* Check permissions for module
$app->auth->check_module_permissions('admin');
@@ -46,49 +46,52 @@
// Resyncing dns zones
if(isset($_POST['start']) && $_POST['start'] == 1) {
   
   //* CSRF Check
   $app->auth->csrf_token_check();
   //* Set variable sin template
   $app->tpl->setVar('dbhost',$_POST['dbhost']);
   $app->tpl->setVar('dbname',$_POST['dbname']);
   $app->tpl->setVar('dbuser',$_POST['dbuser']);
   $app->tpl->setVar('dbpassword',$_POST['dbpassword']);
   $app->tpl->setVar('dbhost', $_POST['dbhost']);
   $app->tpl->setVar('dbname', $_POST['dbname']);
   $app->tpl->setVar('dbuser', $_POST['dbuser']);
   $app->tpl->setVar('dbpassword', $_POST['dbpassword']);
   //* Establish connection to external database
   $msg .= 'Connecting to external database...<br />';
   //* Backup DB login details
   /*$conf_bak['db_host'] = $conf['db_host'];
   $conf_bak['db_database'] = $conf['db_database'];
   $conf_bak['db_user'] = $conf['db_user'];
   $conf_bak['db_password'] = $conf['db_password'];*/
   //* Set external Login details
   $conf['imp_db_host'] = $_POST['dbhost'];
   $conf['imp_db_database'] = $_POST['dbname'];
   $conf['imp_db_user'] = $_POST['dbuser'];
   $conf['imp_db_password'] = $_POST['dbpassword'];
    $conf['imp_db_charset'] = $conf['db_charset'];
    $conf['imp_db_new_link'] = $conf['db_new_link'];
    $conf['imp_db_client_flags'] = $conf['db_client_flags'];
   $conf['imp_db_charset'] = $conf['db_charset'];
   $conf['imp_db_new_link'] = $conf['db_new_link'];
   $conf['imp_db_client_flags'] = $conf['db_client_flags'];
   //* create new db object
   $exdb = new db('imp');
   $server_id = 1;
   $sys_userid = 1;
   $sys_groupid = 1;
   function addot($text) {
      return trim($text) . '.';
   }
   //* Connect to DB
   if($exdb->connect()) {
   if($exdb !== false) {
      $domains = $exdb->queryAllRecords("SELECT * FROM domains WHERE type = 'MASTER'");
      if(is_array($domains)) {
         foreach($domains as $domain) {
            $soa = $exdb->queryOneRecord("SELECT * FROM records WHERE type = 'SOA' AND domain_id = ".$domain['id']);
            $soa = $exdb->queryOneRecord("SELECT * FROM records WHERE type = 'SOA' AND domain_id = ?", $domain['id']);
            if(is_array($soa)) {
               $parts = explode(' ',$soa['content']);
               $parts = explode(' ', $soa['content']);
               $origin = addot($soa['name']);
               $ns = addot($parts[0]);
               $mbox = addot($parts[1]);
@@ -96,64 +99,98 @@
               $refresh = 7200;
               $retry =  540;
               $expire = 604800;
               $minimum = 86400;
               $minimum = 3600;
               $ttl = $soa['ttl'];
               $insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `origin`, `ns`, `mbox`, `serial`, `refresh`, `retry`, `expire`, `minimum`, `ttl`, `active`, `xfer`) VALUES
               ('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$origin', '$ns', '$mbox', '$serial', '$refresh', '$retry', '$expire', '$minimum', '$ttl', 'Y', '')";
               $insert_data = array(
                  "sys_userid" => $sys_userid,
                  "sys_groupid" => $sys_groupid,
                  "sys_perm_user" => 'riud',
                  "sys_perm_group" => 'riud',
                  "sys_perm_other" => '',
                  "server_id" => $server_id,
                  "origin" => $origin,
                  "ns" => $ns,
                  "mbox" => $mbox,
                  "serial" => $serial,
                  "refresh" => $refresh,
                  "retry" => $retry,
                  "expire" => $expire,
                  "minimum" => $minimum,
                  "ttl" => $ttl,
                  "active" => 'Y',
                  "xfer" => ''
               );
               $dns_soa_id = $app->db->datalogInsert('dns_soa', $insert_data, 'id');
               unset($parts);
               $msg .= 'Import Zone: '.$soa['name'].'<br />';
               //* Process the other records
               $records = $exdb->queryAllRecords("SELECT * FROM records WHERE type != 'SOA' AND domain_id = ".$domain['id']);
               $records = $exdb->queryAllRecords("SELECT * FROM records WHERE type != 'SOA' AND domain_id = ?", $domain['id']);
               if(is_array($records)) {
                  foreach($records as $rec) {
                     $rr = array();
                     $rr['name'] = addot($rec['name']);
                     $rr['type'] = $rec['type'];
                     $rr['aux'] = $rec['prio'];
                     $rr['ttl'] = $rec['ttl'];
                     if($rec['type'] == 'NS' || $rec['type'] == 'MX' || $rec['type'] == 'CNAME') {
                        $rr['data'] = addot($rec['content']);
                     } else {
                        $rr['data'] = $rec['content'];
                     }
                     $insert_data = "(`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `server_id`, `zone`, `name`, `type`, `data`, `aux`, `ttl`, `active`) VALUES
                     ('$sys_userid', '$sys_groupid', 'riud', 'riud', '', '$server_id', '$dns_soa_id', '$rr[name]', '$rr[type]', '$rr[data]', '$rr[aux]', '$rr[ttl]', 'Y')";
                     $insert_data = array(
                        "sys_userid" => $sys_userid,
                        "sys_groupid" => $sys_groupid,
                        "sys_perm_user" => 'riud',
                        "sys_perm_group" => 'riud',
                        "sys_perm_other" => '',
                        "server_id" => $server_id,
                        "zone" => $dns_soa_id,
                        "name" => $rr['name'],
                        "type" => $rr['type'],
                        "data" => $rr['data'],
                        "aux" => $rr['aux'],
                        "ttl" => $rr['ttl'],
                        "active" => 'Y'
                     );
                     $dns_rr_id = $app->db->datalogInsert('dns_rr', $insert_data, 'id');
                     //$msg .= $insert_data.'<br />';
                  }
               }
            }
         }
      }
   } else {
      $error .= $exdb->errorMessage;
   }
   //* restore db login details
   /*$conf['db_host'] = $conf_bak['db_host'];
   $conf['db_database'] = $conf_bak['db_database'];
   $conf['db_user'] = $conf_bak['db_user'];
   $conf['db_password'] = $conf_bak['db_password'];*/
}
$app->tpl->setVar('msg',$msg);
$app->tpl->setVar('error',$error);
$app->tpl->setVar('msg', $msg);
$app->tpl->setVar('error', $error);
//* SET csrf token
$csrf_token = $app->auth->csrf_token_get('dns_import');
$app->tpl->setVar('_csrf_id',$csrf_token['csrf_id']);
$app->tpl->setVar('_csrf_key',$csrf_token['csrf_key']);
$app->tpl_defaults();
$app->tpl->pparse();
?>
?>