<?php

if (empty($_POST['submit'])) {
    echo '<div class="menu"><img src="images/t-new.png" alt="." />Создание базы данных<br />' . "\n";
    echo '</div>' . "\n";
    echo '<div class="post"><form action="?func=db.edit" method="post">' . "\n";
    echo 'Имя базы:<br />' . "\n";
    echo '<input name="name" /><br />' . "\n";
    echo 'Тип базы данных:<br />' . "\n";
    echo '<select name="dbtype">' . "\n";
    echo '<option value="MySQL">MySQL</option>' . "\n";
    echo '</select><br />' . "\n";
    echo 'Кодировка:<br />' . "\n";
    echo '<select name="dbencoding">' . "\n";
    echo '<option value="default">default</option>' . "\n";
    echo '<option value="big5">big5</option>' . "\n";
    echo '<option value="dec8">dec8</option>' . "\n";
    echo '<option value="cp850">cp850</option>' . "\n";
    echo '<option value="hp8">hp8</option>' . "\n";
    echo '<option value="koi8r">koi8r</option>' . "\n";
    echo '<option value="latin1">latin1</option>' . "\n";
    echo '<option value="latin2">latin2</option>' . "\n";
    echo '<option value="swe7">swe7</option>' . "\n";
    echo '<option value="ascii">ascii</option>' . "\n";
    echo '<option value="ujis">ujis</option>' . "\n";
    echo '<option value="sjis">sjis</option>' . "\n";
    echo '<option value="hebrew">hebrew</option>' . "\n";
    echo '<option value="tis620">tis620</option>' . "\n";
    echo '<option value="euckr">euckr</option>' . "\n";
    echo '<option value="koi8u">koi8u</option>' . "\n";
    echo '<option value="gb2312">gb2312</option>' . "\n";
    echo '<option value="greek">greek</option>' . "\n";
    echo '<option value="cp1250">cp1250</option>' . "\n";
    echo '<option value="gbk">gbk</option>' . "\n";
    echo '<option value="latin5">latin5</option>' . "\n";
    echo '<option value="armscii8">armscii8</option>' . "\n";
    echo '<option value="utf8">utf8</option>' . "\n";
    echo '<option value="ucs2">ucs2</option>' . "\n";
    echo '<option value="cp866">cp866</option>' . "\n";
    echo '<option value="keybcs2">keybcs2</option>' . "\n";
    echo '<option value="macce">macce</option>' . "\n";
    echo '<option value="macroman">macroman</option>' . "\n";
    echo '<option value="cp852">cp852</option>' . "\n";
    echo '<option value="latin7">latin7</option>' . "\n";
    echo '<option value="cp1251">cp1251</option>' . "\n";
    echo '<option value="cp1256">cp1256</option>' . "\n";
    echo '<option value="cp1257">cp1257</option>' . "\n";
    echo '<option value="binary">binary</option>' . "\n";
    echo '<option value="geostd8">geostd8</option>' . "\n";
    echo '<option value="cp932">cp932</option>' . "\n";
    echo '<option value="eucjpms">eucjpms</option>' . "\n";
    echo '</select><br />' . "\n";
    echo 'Пользователь:<br />' . "\n";
    echo '<select name="dbuser">' . "\n";
    echo '<option value="newuser">Создать пользователя*</option>' . "\n";
    $content = api_query('https://' . $server . '/manager/ispmgr?func=db&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
    $parse_xml_db = simplexml_load_string($content);
    $echo_users = array();
    foreach($parse_xml_db->elem as $var_db) {
        $content = api_query('https://' . $server . '/manager/ispmgr?func=db.users&elid=' . urlencode((string)$var_db->dbkey) . '&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
        $parse_xml_users = simplexml_load_string($content);
        foreach($parse_xml_users->elem as $var_users) {
            if (!in_array((string)$var_users->name, $echo_users)) {
                $echo_users[] = (string)$var_users->name;
                echo '<option value="' . htmlentities((string)$var_users->name, ENT_QUOTES, 'UTF-8') . '">' . htmlentities((string)$var_users->name, ENT_QUOTES, 'UTF-8') . '</option>' . "\n";
            }
        }
    }
    unset($echo_users);
    echo '</select><br />' . "\n";
    echo '*Новый пользователь:<br />' . "\n";
    echo '<input name="dbusername" /><br />' . "\n";
    echo '*Пароль:<br />' . "\n";
    echo '<input name="dbpassword" /><br />' . "\n";
    echo '*Подтверждение:<br />' . "\n";
    echo '<input name="dbconfirm" /><br />' . "\n";
    echo '<input name="submit" type="submit" value="Создать" /><br />' . "\n";
    echo '</form>' . "\n";
    echo '</div>' . "\n";
    echo '<div class="menu"><img src="images/t-back.png" alt=".." /><a href="?func=db">Вернуться</a>' . "\n";
    echo '<img src="images/sb-menu.png" alt="." /><a href="?func=menu">Вернуться в меню</a>' . "\n</div>";
} else {
    if ($_POST['dbpassword'] <> $_POST['dbconfirm'] or $_POST['dbpassword'] == '') {
        echo '<div class="post"><div style="text-align:center">' . "\n";
        echo 'Ошибка при создании базы данных!<br />' . "\n";
        echo 'Пароль не подтвержден.<br />' . "\n";
        echo '<a href="?func=db">Продолжить&#187;</a>' . "\n";
        echo '</div></div>' . "\n";
    } else {
        if ($_POST['dbuser'] <> 'newuser') {
            $_POST['dbusername'] = $_POST['dbuser'];
            $_POST['dbpassword'] = '';
            $_POST['dbconfirm'] = '';
            $dbuserhost = '';
        }
        $content = api_query('https://' . $server . '/manager/ispmgr?func=db.edit&dbuserhost=on&dbconfirm=' . urlencode($_POST['dbconfirm']) . '&dbpassword=' . urlencode($_POST['dbpassword']) . '&dbusername=' . urlencode($_POST['dbusername']) . '&dbuser=' . urlencode($_POST['dbuser']) . '&name=' . urlencode($_POST['name']) . '&dbtype=' . urlencode($_POST['dbtype']) . '&dbencoding=' . urlencode($_POST['dbencoding']) . '&sok=ok&out=xml&authinfo=' . urlencode($_SESSION['username']) . ':' . urlencode($_SESSION['password']));
        $parse_xml = simplexml_load_string($content);
        if (isset($parse_xml->ok)) {
            echo '<div class="post"><div style="text-align:center">' . "\n";
            echo 'База данных успешно создана!<br />' . "\n";
            echo '<a href="?func=db">Продолжить&#187;</a>' . "\n";
            echo '</div></div>' . "\n";
        } else {
// 	    print_r($content);
            echo '<div class="post"><div style="text-align:center">' . "\n";
            echo 'Ошибка при создании базы данных!<br />' . "\n";
            echo '<a href="?func=db">Продолжить&#187;</a>' . "\n";
            echo '</div></div>' . "\n";
        }
    }
}

?>