예제 #1
0
function checkMysql()
{
    $q = new mysql();
    if (!$q->DATABASE_EXISTS("powerdns")) {
        echo "Starting......: PowerDNS creating 'powerdns' database\n";
        if (!$q->CREATE_DATABASE("powerdns")) {
            echo "Starting......: PowerDNS creating 'powerdns' database failed\n";
            return;
        }
    }
    echo "Starting......: PowerDNS 'powerdns' database OK\n";
    if (!$q->TABLE_EXISTS("domains", "powerdns")) {
        echo "Starting......: PowerDNS creating 'domains' table\n";
        $sql = "create table domains (\n\t\t\t id\t\t INT auto_increment,\n\t\t\t name\t\t VARCHAR(255) NOT NULL,\n\t\t\t master\t\t VARCHAR(128) DEFAULT NULL,\n\t\t\t last_check\t INT DEFAULT NULL,\n\t\t\t type\t\t VARCHAR(6) NOT NULL,\n\t\t\t notified_serial INT DEFAULT NULL, \n\t\t\t account         VARCHAR(40) DEFAULT NULL,\n\t\t\t primary key (id)\n\t\t\t) Engine=InnoDB;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'domains' table FAILED\n";
        } else {
            $q->QUERY_SQL("CREATE UNIQUE INDEX name_index ON domains(name);", "powerdns");
        }
    }
    if (!$q->TABLE_EXISTS("records", "powerdns")) {
        echo "Starting......: PowerDNS creating 'records' table\n";
        $sql = "CREATE TABLE records (\n\t\t\t  id              INT auto_increment,\n\t\t\t  domain_id       INT DEFAULT NULL,\n\t\t\t  name            VARCHAR(255) DEFAULT NULL,\n\t\t\t  type            VARCHAR(10) DEFAULT NULL,\n\t\t\t  content         VARCHAR(255) DEFAULT NULL,\n\t\t\t  ttl             INT DEFAULT NULL,\n\t\t\t  prio            INT DEFAULT NULL,\n\t\t\t  change_date     INT DEFAULT NULL,\n\t\t\t  primary key(id)\n\t\t\t)Engine=InnoDB;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'records' table FAILED\n";
        }
        $q->QUERY_SQL("CREATE INDEX rec_name_index ON records(name);", "powerdns");
        $q->QUERY_SQL("CREATE INDEX nametype_index ON records(name,type);", "powerdns");
        $q->QUERY_SQL("CREATE INDEX domain_id ON records(domain_id);", "powerdns");
        $q->QUERY_SQL("alter table records add ordername VARCHAR(255);", "powerdns");
        $q->QUERY_SQL("alter table records add auth bool;", "powerdns");
        $q->QUERY_SQL("create index orderindex on records(ordername);", "powerdns");
        $q->QUERY_SQL("alter table records change column type type VARCHAR(10);", "powerdns");
    }
    if (!$q->TABLE_EXISTS("supermasters", "powerdns")) {
        echo "Starting......: PowerDNS creating 'supermasters' table\n";
        $sql = "create table supermasters (\n\t\t\t\t  ip VARCHAR(25) NOT NULL, \n\t\t\t\t  nameserver VARCHAR(255) NOT NULL, \n\t\t\t\t  account VARCHAR(40) DEFAULT NULL\n\t\t\t\t) Engine=InnoDB;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'supermasters' table FAILED\n";
        }
        $q->QUERY_SQL("CREATE INDEX rec_name_index ON records(name);", "powerdns");
        $q->QUERY_SQL("CREATE INDEX nametype_index ON records(name,type);", "powerdns");
        $q->QUERY_SQL("CREATE INDEX domain_id ON records(domain_id);", "powerdns");
    }
    if (!$q->TABLE_EXISTS("domainmetadata", "powerdns")) {
        echo "Starting......: PowerDNS creating 'domainmetadata' table\n";
        $sql = "create table domainmetadata (\n\t\t\t id              INT auto_increment,\n\t\t\t domain_id       INT NOT NULL,\n\t\t\t kind            VARCHAR(16),\n\t\t\t content        TEXT,\n\t\t\t primary key(id)\n\t\t\t);";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'domainmetadata' table FAILED\n";
        } else {
            $q->QUERY_SQL("create index domainmetaidindex on domainmetadata(domain_id);", "powerdns");
        }
    }
    if (!$q->TABLE_EXISTS("cryptokeys", "powerdns")) {
        echo "Starting......: PowerDNS creating 'cryptokeys' table\n";
        $sql = "create table cryptokeys (\n\t\t\t id             INT auto_increment,\n\t\t\t domain_id      INT NOT NULL,\n\t\t\t flags          INT NOT NULL,\n\t\t\t active         BOOL,\n\t\t\t content        TEXT,\n\t\t\t primary key(id)\n\t\t\t); ";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'cryptokeys' table FAILED\n";
        } else {
            $q->QUERY_SQL("create index domainidindex on cryptokeys(domain_id);", "powerdns");
        }
    }
    if (!$q->TABLE_EXISTS("tsigkeys", "powerdns")) {
        echo "Starting......: PowerDNS creating 'tsigkeys' table\n";
        $sql = "create table tsigkeys (\n\t\t\t id             INT auto_increment,\n\t\t\t name           VARCHAR(255), \n\t\t\t algorithm      VARCHAR(255),\n\t\t\t secret         VARCHAR(255),\n\t\t\t primary key(id)\n\t\t\t);";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'tsigkeys' table FAILED\n";
        } else {
            $q->QUERY_SQL("create unique index namealgoindex on tsigkeys(name, algorithm);", "powerdns");
        }
    }
    if (!$q->TABLE_EXISTS("users", "powerdns")) {
        echo "Starting......: PowerDNS creating 'users' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `users` (\n\t\t\t  `id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t  `username` varchar(16) NOT NULL DEFAULT '0',\n\t\t\t  `password` varchar(34) NOT NULL DEFAULT '0',\n\t\t\t  `fullname` varchar(255) NOT NULL DEFAULT '0',\n\t\t\t  `email` varchar(255) NOT NULL DEFAULT '0',\n\t\t\t  `description` varchar(1024) NOT NULL DEFAULT '0',\n\t\t\t  `perm_templ` tinyint(4) NOT NULL DEFAULT '0',\n\t\t\t  `active` tinyint(4) NOT NULL DEFAULT '0',\n\t\t\t  PRIMARY KEY (`id`))";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'users' table FAILED\n";
        }
    }
    if (!$q->TABLE_EXISTS("perm_items", "powerdns")) {
        echo "Starting......: PowerDNS creating 'perm_items' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `perm_items` (\n\t\t  `id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t  `name` varchar(64) NOT NULL DEFAULT '0',\n\t\t  `descr` varchar(1024) NOT NULL DEFAULT '0',\n\t\t  PRIMARY KEY (`id`)\n\t\t) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=62 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'perm_items' table FAILED\n";
        } else {
            $sql = "INSERT INTO `perm_items` (`id`, `name`, `descr`) VALUES\n\t\t\t(41, 'zone_master_add', 'User is allowed to add new master zones.'),\n\t\t\t(42, 'zone_slave_add', 'User is allowed to add new slave zones.'),\n\t\t\t(43, 'zone_content_view_own', 'User is allowed to see the content and meta data of zones he owns.'),\n\t\t\t(44, 'zone_content_edit_own', 'User is allowed to edit the content of zones he owns.'),\n\t\t\t(45, 'zone_meta_edit_own', 'User is allowed to edit the meta data of zones he owns.'),\n\t\t\t(46, 'zone_content_view_others', 'User is allowed to see the content and meta data of zones he does not own.'),\n\t\t\t(47, 'zone_content_edit_others', 'User is allowed to edit the content of zones he does not own.'),\n\t\t\t(48, 'zone_meta_edit_others', 'User is allowed to edit the meta data of zones he does not own.'),\n\t\t\t(49, 'search', 'User is allowed to perform searches.'),\n\t\t\t(50, 'supermaster_view', 'User is allowed to view supermasters.'),\n\t\t\t(51, 'supermaster_add', 'User is allowed to add new supermasters.'),\n\t\t\t(52, 'supermaster_edit', 'User is allowed to edit supermasters.'),\n\t\t\t(53, 'user_is_ueberuser', 'User has full access. God-like. Redeemer.'),\n\t\t\t(54, 'user_view_others', 'User is allowed to see other users and their details.'),\n\t\t\t(55, 'user_add_new', 'User is allowed to add new users.'),\n\t\t\t(56, 'user_edit_own', 'User is allowed to edit their own details.'),\n\t\t\t(57, 'user_edit_others', 'User is allowed to edit other users.'),\n\t\t\t(58, 'user_passwd_edit_others', 'User is allowed to edit the password of other users.'),\n\t\t\t(59, 'user_edit_templ_perm', 'User is allowed to change the permission template that is assigned to a user.'),\n\t\t\t(60, 'templ_perm_add', 'User is allowed to add new permission templates.'),\n\t\t\t(61, 'templ_perm_edit', 'User is allowed to edit existing permission templates.');";
            $q->QUERY_SQL($sql, "powerdns");
        }
    }
    if (!$q->TABLE_EXISTS("perm_templ", "powerdns")) {
        echo "Starting......: PowerDNS creating 'perm_templ' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `perm_templ` (\n\t\t\t  `id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t  `name` varchar(128) NOT NULL DEFAULT '0',\n\t\t\t  `descr` varchar(1024) NOT NULL DEFAULT '0',\n\t\t\t  PRIMARY KEY (`id`)\n\t\t\t) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'perm_templ' table FAILED\n";
        } else {
            $sql = "INSERT INTO `perm_templ` (`id`, `name`, `descr`) VALUES (1, 'Administrator', 'Administrator template with full rights.');";
            $q->QUERY_SQL($sql, "powerdns");
        }
    }
    if (!$q->TABLE_EXISTS("perm_templ_items", "powerdns")) {
        echo "Starting......: PowerDNS creating 'perm_templ_items' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `perm_templ_items` (\n\t\t  `id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t  `templ_id` int(11) NOT NULL DEFAULT '0',\n\t\t  `perm_id` int(11) NOT NULL DEFAULT '0',\n\t\t  PRIMARY KEY (`id`)\n\t\t) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=250 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'perm_templ_items' table FAILED\n";
        } else {
            $sql = "INSERT INTO `perm_templ_items` (`id`, `templ_id`, `perm_id`) VALUES (249, 1, 53);";
            $q->QUERY_SQL($sql, "powerdns");
        }
    }
    if (!$q->TABLE_EXISTS("zones", "powerdns")) {
        echo "Starting......: PowerDNS creating 'zones' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `zones` (\n\t\t  `id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t  `domain_id` int(11) NOT NULL DEFAULT '0',\n\t\t  `owner` int(11) NOT NULL DEFAULT '0',\n\t\t  `comment` varchar(1024) DEFAULT '0',\n\t\t  `zone_templ_id` int(11) NOT NULL,\n\t\t  PRIMARY KEY (`id`)\n\t\t) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'zones' table FAILED\n";
        }
    }
    if (!$q->TABLE_EXISTS("zone_templ", "powerdns")) {
        echo "Starting......: PowerDNS creating 'zone_templ' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `zone_templ` (\n\t\t\t  `id` bigint(20) NOT NULL AUTO_INCREMENT,\n\t\t\t  `name` varchar(128) NOT NULL DEFAULT '0',\n\t\t\t  `descr` varchar(1024) NOT NULL DEFAULT '0',\n\t\t\t  `owner` bigint(20) NOT NULL DEFAULT '0',\n\t\t\t  PRIMARY KEY (`id`)\n\t\t\t) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'zone_templ' table FAILED\n";
        }
    }
    if (!$q->TABLE_EXISTS("zone_templ_records", "powerdns")) {
        echo "Starting......: PowerDNS creating 'zone_templ_records' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `zone_templ_records` (\n\t\t  `id` bigint(20) NOT NULL AUTO_INCREMENT,\n\t\t  `zone_templ_id` bigint(20) NOT NULL DEFAULT '0',\n\t\t  `name` varchar(255) NOT NULL DEFAULT '0',\n\t\t  `type` varchar(6) NOT NULL DEFAULT '0',\n\t\t  `content` varchar(255) NOT NULL DEFAULT '0',\n\t\t  `ttl` bigint(20) NOT NULL DEFAULT '0',\n\t\t  `prio` bigint(20) NOT NULL DEFAULT '0',\n\t\t  PRIMARY KEY (`id`)\n\t\t) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: PowerDNS creating 'zone_templ_records' table FAILED\n";
        }
    }
    echo "Starting......: PowerDNS Mysql done...\n";
    poweradmin();
}
예제 #2
0
function checkMysql($nollop = false)
{
    $unix = new unix();
    $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time";
    if ($unix->file_time_min($timefile) < 1) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS need at least 1mn, aborting\n";
        return;
    }
    @unlink($timefile);
    @file_put_contents($timefile, time());
    $passwdcmdline = null;
    $mysql = $unix->find_program("mysql");
    $q = new mysql();
    if (!$q->TestingConnection(true)) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating, MySQL seems not ready..\n";
        return;
    }
    forward_zones();
    if (!$q->DATABASE_EXISTS("powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'powerdns' database\n";
        if (!$q->CREATE_DATABASE("powerdns")) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'powerdns' database failed\n";
            return;
        }
    }
    echo "Starting......: " . date("H:i:s") . " PowerDNS 'powerdns' database OK\n";
    $f["cryptokeys"] = true;
    $f["domainmetadata"] = true;
    $f["domains"] = true;
    $f["perm_items"] = true;
    $f["perm_templ"] = true;
    $f["perm_templ_items"] = true;
    $f["records"] = true;
    $f["supermasters"] = true;
    $f["tsigkeys"] = true;
    $f["users"] = true;
    $f["zones"] = true;
    $f["zone_templ"] = true;
    $f["zone_templ_records"] = true;
    $resultTables = true;
    while (list($tablename, $line2) = each($f)) {
        if (!$q->TABLE_EXISTS($tablename, "powerdns")) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS Table `{$tablename}` failed...\n";
            $resultTables = false;
            continue;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS Table `{$tablename}` OK...\n";
    }
    if ($resultTables) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS pass tests Success...\n";
        return true;
    }
    $dumpfile = "/usr/share/artica-postfix/bin/install/pdns/powerdns.sql";
    if (!is_file($dumpfile)) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS /usr/share/artica-postfix/bin/install/pdns/powerdns.sql no such file...\n";
        return;
    }
    echo "Starting......: " . date("H:i:s") . " PowerDNS installing database...\n";
    if ($q->mysql_password != null) {
        $passwdcmdline = " -p{$q->mysql_password}";
    }
    $cmd = "{$mysql} -B -u {$q->mysql_admin}{$passwdcmdline} --database=powerdns -E < {$dumpfile} >/dev/null 2>&1";
    shell_exec($cmd);
    reset($f);
    $resultTables = true;
    while (list($tablename, $line2) = each($f)) {
        if (!$q->TABLE_EXISTS($tablename, "powerdns")) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS Table `{$tablename}` failed...\n";
            $resultTables = false;
            continue;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS Table `{$tablename}` OK...\n";
    }
    if ($resultTables) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS Success...\n";
        return true;
    }
    if (!$q->TABLE_EXISTS("domains", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'domains' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS domains (\n\t\t\t id\t\t INT auto_increment,\n\t\t\t name\t\t VARCHAR(255) NOT NULL,\n\t\t\t master\t\t VARCHAR(128) DEFAULT NULL,\n\t\t\t last_check\t INT DEFAULT NULL,\n\t\t\t type\t\t VARCHAR(6) NOT NULL,\n\t\t\t notified_serial INT DEFAULT NULL, \n\t\t\t account         VARCHAR(40) DEFAULT NULL,\n\t\t\t primary key (id)\n\t\t\t) Engine=InnoDB;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'domains' table FAILED\n";
        } else {
            return;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS table 'domains' Success\n";
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS table 'domains' Success\n";
        $q->QUERY_SQL("CREATE UNIQUE INDEX name_index ON domains(name);", "powerdns");
    }
    if (!$q->TABLE_EXISTS("records", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'records' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS records (\n\t\t\t  id              INT auto_increment,\n\t\t\t  domain_id       INT DEFAULT NULL,\n\t\t\t  name            VARCHAR(255) DEFAULT NULL,\n\t\t\t  type            VARCHAR(10) DEFAULT NULL,\n\t\t\t  content         VARCHAR(255) DEFAULT NULL,\n\t\t\t  ttl             INT DEFAULT NULL,\n\t\t\t  prio            INT DEFAULT NULL,\n\t\t\t  change_date     INT DEFAULT NULL,\n\t\t\t  explainthis     VARCHAR(255) DEFAULT NULL,\n\t\t\t  primary key(id)\n\t\t\t)Engine=InnoDB;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'records' table FAILED\n";
            return;
        }
        $q->QUERY_SQL("CREATE INDEX rec_name_index ON records(name);", "powerdns");
        $q->QUERY_SQL("CREATE INDEX nametype_index ON records(name,type);", "powerdns");
        $q->QUERY_SQL("CREATE INDEX domain_id ON records(domain_id);", "powerdns");
        $q->QUERY_SQL("alter table records add ordername VARCHAR(255);", "powerdns");
        $q->QUERY_SQL("alter table records add auth bool;", "powerdns");
        $q->QUERY_SQL("create index orderindex on records(ordername);", "powerdns");
        $q->QUERY_SQL("alter table records change column type type VARCHAR(10);", "powerdns");
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'records' table success\n";
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'records' table success\n";
    }
    if (!$q->TABLE_EXISTS("supermasters", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'supermasters' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS supermasters (\n\t\t\t\t  ip VARCHAR(25) NOT NULL, \n\t\t\t\t  nameserver VARCHAR(255) NOT NULL, \n\t\t\t\t  account VARCHAR(40) DEFAULT NULL\n\t\t\t\t) Engine=InnoDB;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'supermasters' table FAILED\n";
            return;
        }
        $q->QUERY_SQL("CREATE INDEX rec_name_index ON records(name);", "powerdns");
        $q->QUERY_SQL("CREATE INDEX nametype_index ON records(name,type);", "powerdns");
        $q->QUERY_SQL("CREATE INDEX domain_id ON records(domain_id);", "powerdns");
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'supermasters' table success\n";
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'supermasters' table success\n";
    }
    if (!$q->TABLE_EXISTS("domainmetadata", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'domainmetadata' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS domainmetadata (\n\t\t\t id              INT auto_increment,\n\t\t\t domain_id       INT NOT NULL,\n\t\t\t kind            VARCHAR(16),\n\t\t\t content        TEXT,\n\t\t\t primary key(id)\n\t\t\t);";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'domainmetadata' table FAILED\n";
            return;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'domainmetadata' table success\n";
        $q->QUERY_SQL("create index domainmetaidindex on domainmetadata(domain_id);", "powerdns");
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS 'domainmetadata' table success\n";
    }
    if (!$q->TABLE_EXISTS("cryptokeys", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'cryptokeys' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS cryptokeys (\n\t\t\t id             INT auto_increment,\n\t\t\t domain_id      INT NOT NULL,\n\t\t\t flags          INT NOT NULL,\n\t\t\t active         BOOL,\n\t\t\t content        TEXT,\n\t\t\t primary key(id)\n\t\t\t); ";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'cryptokeys' table FAILED\n";
            return;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'cryptokeys' table success\n";
        $q->QUERY_SQL("create index domainidindex on cryptokeys(domain_id);", "powerdns");
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS 'cryptokeys' table success\n";
    }
    if (!$q->TABLE_EXISTS("tsigkeys", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'tsigkeys' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS tsigkeys (\n\t\t\t id             INT auto_increment,\n\t\t\t name           VARCHAR(255), \n\t\t\t algorithm      VARCHAR(255),\n\t\t\t secret         VARCHAR(255),\n\t\t\t primary key(id)\n\t\t\t);";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'tsigkeys' table FAILED\n";
            return;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'tsigkeys' table success\n";
        $q->QUERY_SQL("create unique index namealgoindex on tsigkeys(name, algorithm);", "powerdns");
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS 'tsigkeys' table success\n";
    }
    if (!$q->TABLE_EXISTS("users", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'users' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(16) NOT NULL DEFAULT '0', `password` varchar(34) NOT NULL DEFAULT '0', `fullname` varchar(255) NOT NULL DEFAULT '0', `email` varchar(255) NOT NULL DEFAULT '0', `description` varchar(1024) NOT NULL DEFAULT '0', `perm_templ` tinyint(4) NOT NULL DEFAULT '0', `active` tinyint(4) NOT NULL DEFAULT '0', PRIMARY KEY (`id`))";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'users' table FAILED\n";
            return;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'users' table success\n";
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS 'users' table success\n";
    }
    if (!$q->TABLE_EXISTS("perm_items", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'perm_items' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `perm_items` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(64) NOT NULL DEFAULT '0', `descr` varchar(1024) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=62 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'perm_items' table FAILED\n";
            return;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'perm_items' table success\n";
        $sql = "INSERT INTO `perm_items` (`id`, `name`, `descr`) VALUES (41, 'zone_master_add', 'User is allowed to add new master zones.'), (42, 'zone_slave_add', 'User is allowed to add new slave zones.'), (43, 'zone_content_view_own', 'User is allowed to see the content and meta data of zones he owns.'), (44, 'zone_content_edit_own', 'User is allowed to edit the content of zones he owns.'), (45, 'zone_meta_edit_own', 'User is allowed to edit the meta data of zones he owns.'), (46, 'zone_content_view_others', 'User is allowed to see the content and meta data of zones he does not own.'), (47, 'zone_content_edit_others', 'User is allowed to edit the content of zones he does not own.'), (48, 'zone_meta_edit_others', 'User is allowed to edit the meta data of zones he does not own.'), (49, 'search', 'User is allowed to perform searches.'), (50, 'supermaster_view', 'User is allowed to view supermasters.'), (51, 'supermaster_add', 'User is allowed to add new supermasters.'), (52, 'supermaster_edit', 'User is allowed to edit supermasters.'), (53, 'user_is_ueberuser', 'User has full access. God-like. Redeemer.'), (54, 'user_view_others', 'User is allowed to see other users and their details.'), (55, 'user_add_new', 'User is allowed to add new users.'), (56, 'user_edit_own', 'User is allowed to edit their own details.'), (57, 'user_edit_others', 'User is allowed to edit other users.'), (58, 'user_passwd_edit_others', 'User is allowed to edit the password of other users.'), (59, 'user_edit_templ_perm', 'User is allowed to change the permission template that is assigned to a user.'), (60, 'templ_perm_add', 'User is allowed to add new permission templates.'), (61, 'templ_perm_edit', 'User is allowed to edit existing permission templates.');";
        $q->QUERY_SQL($sql, "powerdns");
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS 'perm_items' table success\n";
    }
    if (!$q->TABLE_EXISTS("perm_templ", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'perm_templ' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `perm_templ` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(128) NOT NULL DEFAULT '0', `descr` varchar(1024) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'perm_templ' table FAILED\n";
        } else {
            $sql = "INSERT INTO `perm_templ` (`id`, `name`, `descr`) VALUES (1, 'Administrator', 'Administrator template with full rights.');";
            $q->QUERY_SQL($sql, "powerdns");
        }
    }
    if (!$q->TABLE_EXISTS("perm_templ_items", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'perm_templ_items' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `perm_templ_items` ( `id` int(11) NOT NULL AUTO_INCREMENT, `templ_id` int(11) NOT NULL DEFAULT '0', `perm_id` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=250 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'perm_templ_items' table FAILED\n";
            return;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'perm_templ_items' table success\n";
        $sql = "INSERT INTO `perm_templ_items` (`id`, `templ_id`, `perm_id`) VALUES (249, 1, 53);";
        $q->QUERY_SQL($sql, "powerdns");
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS 'perm_templ_items' table success\n";
    }
    if (!$q->TABLE_EXISTS("zones", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'zones' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `zones` ( `id` int(11) NOT NULL AUTO_INCREMENT, `domain_id` int(11) NOT NULL DEFAULT '0', `owner` int(11) NOT NULL DEFAULT '0', `comment` varchar(1024) DEFAULT '0', `zone_templ_id` int(11) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'zones' table FAILED\n";
            return;
        }
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS 'zones' table success\n";
    }
    if (!$q->TABLE_EXISTS("zone_templ", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'zone_templ' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `zone_templ` ( \n\t\t\t\t`id` bigint(20) NOT NULL AUTO_INCREMENT, \n\t\t\t\t`name` varchar(128) NOT NULL DEFAULT '0', \n\t\t\t\t`descr` varchar(1024) NOT NULL DEFAULT '0', \n\t\t\t\t`owner` bigint(20) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) \n\t\t\t\t) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'zone_templ' table FAILED\n";
            return;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'zone_templ' table success\n";
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS 'zone_templ' table success\n";
    }
    $q->QUERY_SQL("CREATE TABLE IF NOT EXISTS zone_templ_records (\n\tid            INTEGER      NOT NULL AUTO_INCREMENT,\n\tzone_templ_id INTEGER      NOT NULL,\n\tname          VARCHAR(255) NOT NULL,\n\t`type`        VARCHAR(6)   NOT NULL,\n\tcontent       VARCHAR(255) NOT NULL,\n\tttl           INTEGER      NOT NULL,\n\tprio          INTEGER      NOT NULL,\n\tPRIMARY KEY (id)\n\t) ENGINE=InnoDB DEFAULT CHARSET=latin1;", "powerdns");
    $q->QUERY_SQL("CREATE TABLE IF NOT EXISTS records_zone_templ (\n\tdomain_id INTEGER NOT NULL,\n\trecord_id INTEGER NOT NULL,\n\tzone_templ_id INTEGER NOT NULL\n\t) ENGINE=InnoDB DEFAULT CHARSET=latin1;");
    if (!$q->TABLE_EXISTS("zone_templ_records", "powerdns")) {
        echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'zone_templ_records' table\n";
        $sql = "CREATE TABLE IF NOT EXISTS `zone_templ_records` (\n\t\t  `id` bigint(20) NOT NULL AUTO_INCREMENT,\n\t\t  `zone_templ_id` bigint(20) NOT NULL DEFAULT '0',\n\t\t  `name` varchar(255) NOT NULL DEFAULT '0',\n\t\t  `type` varchar(6) NOT NULL DEFAULT '0',\n\t\t  `content` varchar(255) NOT NULL DEFAULT '0',\n\t\t  `ttl` bigint(20) NOT NULL DEFAULT '0',\n\t\t  `prio` bigint(20) NOT NULL DEFAULT '0',\n\t\t  PRIMARY KEY (`id`)\n\t\t) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
        $q->QUERY_SQL($sql, "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS creating 'zone_templ_records' table FAILED\n";
        }
    }
    if (!$q->TABLE_EXISTS("domainmetadata", "powerdns")) {
        $q->QUERY_SQL("create table domainmetadata ( id INT auto_increment, domain_id INT NOT NULL, kind VARCHAR(16), content TEXT, primary key(id) );", "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/domainmetadata failed {$q->mysql_error}\n";
            return;
        }
        echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/domainmetadata success\n";
        $q->QUERY_SQL("create index domainmetaidindex on domainmetadata(domain_id);", "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/domainmetadata failed {$q->mysql_error}\n";
        }
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/domainmetadata OK\n";
    }
    if (!$q->TABLE_EXISTS("cryptokeys", "powerdns")) {
        $q->QUERY_SQL("create table cryptokeys (\n\tid             INT auto_increment,\n \tdomain_id      INT NOT NULL,\n\tflags          INT NOT NULL,\n \tactive         BOOL,\n \tcontent        TEXT,\n\tprimary key(id)\n\t);", "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/cryptokeys failed {$q->mysql_error}\n";
        } else {
            echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/cryptokeys success\n";
            $q->QUERY_SQL("create index domainidindex on cryptokeys(domain_id);", "powerdns");
            if (!$q->ok) {
                echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/cryptokeys failed {$q->mysql_error}\n";
            }
        }
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/cryptokeys OK\n";
    }
    if ($q->TABLE_EXISTS("records", "powerdns")) {
        if (!$q->FIELD_EXISTS("records", "ordername", "powerdns")) {
            $q->QUERY_SQL("alter table records add ordername  VARCHAR(255)", "powerdns");
            if (!$q->ok) {
                echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/records failed {$q->mysql_error}\n";
            }
            $q->QUERY_SQL("create index orderindex on records(ordername)", "powerdns");
        }
        if (!$q->FIELD_EXISTS("records", "auth", "powerdns")) {
            $q->QUERY_SQL("alter table records add auth bool", "powerdns");
            if (!$q->ok) {
                echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/records failed {$q->mysql_error}\n";
            }
        }
        if (!$q->FIELD_EXISTS("records", "disabled", "powerdns")) {
            $q->QUERY_SQL("alter table records add `disabled` TINYINT(1) DEFAULT 0, ADD INDEX `disabled`(`disabled`)", "powerdns");
            if (!$q->ok) {
                echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/records/disabled failed {$q->mysql_error}\n";
            }
        }
        $q->QUERY_SQL("alter table records change column type type VARCHAR(10);", "powerdns");
    }
    if (!$q->TABLE_EXISTS("tsigkeys", "powerdns")) {
        $q->QUERY_SQL("create table tsigkeys (\n\t\t id             INT auto_increment,\n\t\t name           VARCHAR(255), \n\t\t algorithm      VARCHAR(50),\n\t\t secret         VARCHAR(255),\n\t\t primary key(id)\n\t\t);", "powerdns");
        if (!$q->ok) {
            echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/tsigkeys failed {$q->mysql_error}\n";
        } else {
            echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/tsigkeys success\n";
            $q->QUERY_SQL("create unique index namealgoindex on tsigkeys(name, algorithm);", "powerdns");
            if (!$q->ok) {
                echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/tsigkeys failed {$q->mysql_error}\n";
            }
        }
    } else {
        echo "Starting......: " . date("H:i:s") . " PowerDNS patching database/tsigkeys OK\n";
    }
    echo "Starting......: " . date("H:i:s") . " PowerDNS Mysql done...\n";
    poweradmin();
}