Example #1
0
/**
 * Run SQL queries
 * 
 * @param string $file Used configuration file
 * @param string $name Server name
 * 
 * @return void
 */
function runSQLQueries($file, $name)
{
    // Create a new DOMDocument object
    $dom = new DOMDocument();
    // Load the XML file
    check_errs($dom->load($file), false, "File " . $file . " not correctly formed.", "File " . $file . " is OK!");
    // Get the root node
    $root = $dom->documentElement;
    echo "\n";
    // Get the list of servers already in file
    $serverList = $dom->getElementsByTagName('serveur');
    switch ($serverList->length) {
        // If two, we can continue
        case 2:
            // Get the server
            $myServer = getServerByName($name, $file);
            // Get the other server
            $myOtherServer = getOtherServerByName($name, $file);
            // Get the SHOW MASTER STATUS of the other server because we need for CHANGE MASTER TO...
            if ($myOtherServer->getAttribute('localhost') == "y") {
                $masterStatus = getFileAndPosition("localhost:" . $myOtherServer->getAttribute('dbport'), $myOtherServer->getAttribute('dbusername'), $myOtherServer->getAttribute('dbpassword'));
            } else {
                $masterStatus = getFileAndPosition($myOtherServer->getAttribute('ip') . ":" . $myOtherServer->getAttribute('dbport'), $myOtherServer->getAttribute('dbusername'), $myOtherServer->getAttribute('dbpassword'));
            }
            check_errs($masterStatus, false, "Unable to get information.", "Information collected!");
            // SQL first query to execute on server
            $sql1 = "GRANT REPLICATION SLAVE ON *.* TO '" . $myServer->getElementsByTagName('esclave')->item(0)->getAttribute('username') . "'@'%' IDENTIFIED BY '" . $myServer->getElementsByTagName('esclave')->item(0)->getAttribute('password') . "';\nFLUSH PRIVILEGES;";
            // SQL second query
            $sql2 = "SLAVE STOP;\nCHANGE MASTER TO MASTER_HOST='" . $myOtherServer->getAttribute('ip') . "', MASTER_USER='******'esclave')->item(0)->getAttribute('username') . "', MASTER_PASSWORD='******'esclave')->item(0)->getAttribute('password') . "', MASTER_LOG_FILE='" . $masterStatus['File'] . "', MASTER_LOG_POS=" . $masterStatus['Position'] . ";\nSTART SLAVE;";
            // Ask confirmation for the first query
            echo "\n";
            cecho("Do you want to execute these queries on " . $myServer->getAttribute('ip') . " ?", "red");
            echo "\n\n";
            echo $sql1 . "\n\n";
            $answerSQL = recup("y or n [default y]: ", "y");
            if ($answerSQL != "n") {
                if ($myServer->getAttribute('localhost') == "y") {
                    // Execute query as "localhost"
                    executeSQLRequest("localhost:" . $myServer->getAttribute('dbport'), $myServer->getAttribute('dbusername'), $myServer->getAttribute('dbpassword'), $sql1);
                } else {
                    // Execute distant query
                    executeSQLRequest($myServer->getAttribute('ip') . ":" . $myServer->getAttribute('dbport'), $myServer->getAttribute('dbusername'), $myServer->getAttribute('dbpassword'), $sql1);
                }
            }
            // Ask confirmation for the second query
            echo "\n";
            cecho("Do you want to execute these queries on " . $myServer->getAttribute('ip') . " ?", "red");
            echo "\n\n";
            echo $sql2 . "\n\n";
            $answerSQL = recup("y or n [default y]: ", "y");
            if ($answerSQL != "n") {
                if ($myServer->getAttribute('localhost') == "y") {
                    // Execute query as "localhost"
                    executeSQLRequest("localhost:" . $myServer->getAttribute('dbport'), $myServer->getAttribute('dbusername'), $myServer->getAttribute('dbpassword'), $sql2);
                } else {
                    // Execute distant query
                    executeSQLRequest($myServer->getAttribute('ip') . ":" . $myServer->getAttribute('dbport'), $myServer->getAttribute('dbusername'), $myServer->getAttribute('dbpassword'), $sql2);
                }
            }
            // Ask confirmation for mysqldump query
            echo "\n";
            // If server if the "first", we do the mysqldump
            if ($myServer->getAttribute('first') == "y") {
                cecho("Do you want to execute this command on " . $myServer->getAttribute('ip') . " ?", "red");
                echo "\n\n";
                echo "mysqldump -u " . $myServer->getAttribute('dbusername') . " -p --databases " . $root->getAttribute('db') . " > /tmp/mysqldump.sql" . "\n\n";
                $answerSQL = recup("y or n [default y]: ", "y");
                if ($answerSQL != "n") {
                    // If localhost, local command
                    if ($myServer->getAttribute('localhost') == "y") {
                        exec("mysqldump -u " . $myServer->getAttribute('dbusername') . " -P " . $myServer->getAttribute('dbport') . " -p" . $myServer->getAttribute('dbpassword') . " --databases " . $root->getAttribute('db') . " > /tmp/mysqldump.sql", $result, $returnVar);
                    } else {
                        $sshPort = recup("SSH port [default 22]? ", "22");
                        // User allowed
                        $sshUser = recup("Username allowed to connect to " . $myServer->getAttribute('ip') . ": ");
                        // Command via SSH
                        exec("ssh -p " . $sshPort . " " . $sshUser . "@" . $myServer->getAttribute('ip') . " mysqldump -u " . $myServer->getAttribute('dbusername') . " -P " . $myServer->getAttribute('dbport') . " -p" . $myServer->getAttribute('dbpassword') . " --databases " . $root->getAttribute('db') . " > /tmp/mysqldump.sql", $result, $returnVar);
                    }
                    check_errs($returnVar, true, "Unable to perform MySQLDump.", "MySQLDump performed!");
                }
                // If server is the "second", we get the mysqldump of the "first"
            } else {
                cecho("Do you want to execute this command on " . $myServer->getAttribute('ip') . " ?", "red");
                echo "\n\n";
                // /tmp/mysqldump.sql is a local file
                echo "mysql -u " . $myServer->getAttribute('dbusername') . " -p " . $root->getAttribute('db') . " < /tmp/mysqldump.sql" . "\n\n";
                $answerSQL = recup("y or n [default y]: ", "y");
                if ($answerSQL != "n") {
                    if ($myServer->getAttribute('localhost') == "y") {
                        exec("mysql -u " . $myServer->getAttribute('dbusername') . " -P " . $myServer->getAttribute('dbport') . " -p" . $myServer->getAttribute('dbpassword') . " < /tmp/mysqldump.sql", $result, $returnVar);
                    } else {
                        $sshPort = recup("SSH port [default 22]? ", "22");
                        // User allowed
                        $sshUser = recup("Username allowed to connect to " . $myServer->getAttribute('ip') . ": ");
                        // Command via SSH
                        exec("ssh -p " . $sshPort . " " . $sshUser . "@" . $myServer->getAttribute('ip') . " mysql -u " . $myServer->getAttribute('dbusername') . " -P " . $myServer->getAttribute('dbport') . " -p" . $myServer->getAttribute('dbpassword') . " < /tmp/mysqldump.sql", $result, $returnVar);
                    }
                    check_errs($returnVar, true, "Failed.", "Success!");
                }
            }
            break;
            // Else, quit
        // Else, quit
        default:
            echo exec("clear");
            cecho("You must have two servers in your XML file.", "red");
            echo "\n\n";
            menu();
    }
}
Example #2
0
        $replacingText = '';
        $defaultLink = "ADSL";
        $displayLink = "0";
        if (isset($_POST['displayMode'])) {
            $displayMode = $_POST['displayMode'];
            if (isset($_POST['substitutiveText'])) {
                $replacingText = $_POST['substitutiveText'];
            }
        }
        if (isset($_POST['defaultLink'])) {
            $defaultLink = $_POST['defaultLink'];
        }
        if (isset($_POST['displayLink'])) {
            $displayLink = $_POST['displayLink'];
        }
        if (getServerByName($_POST['serverName'])) {
            $messageType = "ErrorServerNameExist";
        } elseif (getServerByHost($_POST['hostName'])) {
            $messageType = "ErrorServerHostExist";
        } elseif (InsertServer($_POST['serverName'], $_POST['hostName'], $_POST['port'], $_POST['platform'], $_POST['cpu'], $_POST['memory'], $_POST['disk'], $_POST['description'], $icon, $displayMode, $replacingText, $defaultLink, $displayLink)) {
            $messageType = "AddedServer";
            header('Location: ServerListAdmin.php');
        } else {
            $messageType = "ErrorServerUnknown";
        }
    }
    //end if(!error)
} elseif (isset($_POST['uploadFile_x'])) {
    $directory = 'images/servers/';
    $path1 = $_FILES['file1']['tmp_name'];
    $name = $_FILES['file1']['name'];