Skip to content

Commit

Permalink
easy-wi#30 MySQL server and databases
Browse files Browse the repository at this point in the history
  • Loading branch information
ulrichblock committed Feb 22, 2015
1 parent 57aab96 commit bf9a70a
Show file tree
Hide file tree
Showing 19 changed files with 1,648 additions and 1,356 deletions.
16 changes: 16 additions & 0 deletions web/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@

require_once(EASYWIDIR . '/stuff/ajax/datatable_tsdnsmasterserver.php');

// MySQL server
} else if ($ui->smallletters('d', 11, 'get') == 'mysqlserver' and isset($admin_id) and isset($reseller_id) and isset($resellerLockupID) and $pa['mysql_settings']) {

require_once(EASYWIDIR . '/stuff/ajax/datatable_mysqlserver.php');

// Code wise it seems odd, but this way we can get plausible userIDs for following queries up front without having to repeat ourselves
} else {

Expand Down Expand Up @@ -177,6 +182,10 @@

require_once(EASYWIDIR . '/stuff/ajax/datatable_tsdns.php');

} else if ($ui->smallletters('d', 7, 'get') == 'mysqldb' and isset($admin_id) and isset($reseller_id) and isset($resellerLockupID) and $pa['mysql']) {

require_once(EASYWIDIR . '/stuff/ajax/datatable_mysqldb.php');

}
}

Expand Down Expand Up @@ -232,6 +241,13 @@
require_once(EASYWIDIR . '/stuff/ajax/tsdns_master_usage.php');
die;

} else if (isset($admin_id) and $ui->smallletters('d', 16, 'get') == 'mysqlmasterusage' and $pa['mysql']) {

if ($ui->id('id', 10, 'get')) {
require_once(EASYWIDIR . '/stuff/ajax/mysql_master_usage.php');
}
die;

} else if (isset($admin_id) and $pa['dedicatedServer'] and $ui->smallletters('d', 7, 'get') == 'freeips' and $reseller_id == 0) {

if ($ui->id('userID', 10, 'get')) {
Expand Down
1 change: 1 addition & 0 deletions web/stuff/admin/init_admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
}

if ($easywiModules['my'] === true) {
$what_to_be_included_array['md'] = 'mysql_db.php';
$what_to_be_included_array['my'] = 'mysql_server.php';
}

Expand Down
321 changes: 321 additions & 0 deletions web/stuff/admin/mysql_db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
<?php

/**
* File: mysql_db.php.
* Author: Ulrich Block
* Date: 22.02.15
* Contact: <[email protected]>
*
* This file is part of Easy-WI.
*
* Easy-WI is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Easy-WI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Easy-WI. If not, see <http://www.gnu.org/licenses/>.
*
* Diese Datei ist Teil von Easy-WI.
*
* Easy-WI ist Freie Software: Sie koennen es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder spaeteren
* veroeffentlichten Version, weiterverbreiten und/oder modifizieren.
*
* Easy-WI wird in der Hoffnung, dass es nuetzlich sein wird, aber
* OHNE JEDE GEWAEHELEISTUNG, bereitgestellt; sogar ohne die implizite
* Gewaehrleistung der MARKTFAEHIGKEIT oder EIGNUNG FUER EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License fuer weitere Details.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
*/

if ((!isset($admin_id) or $main != 1) or (isset($admin_id) and !$pa['mysql'])) {
header('Location: login.php');
die;
}

include(EASYWIDIR . '/stuff/methods/class_mysql.php');
include(EASYWIDIR . '/stuff/keyphrasefile.php');

$sprache = getlanguagefile('mysql', $user_language, $reseller_id);
$loguserid = $admin_id;
$logusername = getusername($admin_id);
$logusertype = 'admin';

if ($reseller_id == 0) {
$logreseller = 0;
$logsubuser = 0;
} else {
$logsubuser = (isset($_SESSION['oldid'])) ? $_SESSION['oldid']: 0;
$logreseller = 0;
}

// Define the ID variable which will be used at the form and SQLs
$id = $ui->id('id', 10, 'get');
$rootID = $ui->id('rootID', 10, 'post');
$userID = $ui->id('userID', 10, 'post');
$externalID = $ui->externalID('externalID', 'post');
$active = $ui->active('active', 'post');
$manage_host_table = $ui->active('manage_host_table', 'post');
$description = $ui->description('description', 'post');
$password = ($ui->password('password', 255, 'post')) ? $ui->password('password', 255, 'post') : passwordgenerate(10);
$ips = $ui->ips('ips', 'post');
$max_queries_per_hour = ($ui->id('max_queries_per_hour', 255, 'post')) ? $ui->id('max_queries_per_hour', 255, 'post') : 0;
$max_updates_per_hour = ($ui->id('max_updates_per_hour', 255, 'post')) ? $ui->id('max_updates_per_hour', 255, 'post') : 0;
$max_connections_per_hour = ($ui->id('max_connections_per_hour', 255, 'post')) ? $ui->id('max_connections_per_hour', 255, 'post') : 0;
$max_userconnections_per_hour = ($ui->id('max_userconnections_per_hour', 255, 'post')) ? $ui->id('max_userconnections_per_hour', 255, 'post') : 0;

// At this point all variables are defined that can come from the user

$table = array();

// CSFR protection with hidden tokens. If token(true) returns false, we likely have an attack
if ($ui->w('action', 4, 'post') and !token(true)) {

unset($header, $text);

$errors = array('token' => $spracheResponse->token);

} else {
$errors = array();
}

if ($ui->st('d', 'get') == 'ad' or $ui->st('d', 'get') == 'md') {

// Add jQuery plugin chosen to the header
$htmlExtraInformation['css'][] = '<link href="css/default/chosen/chosen.min.css" rel="stylesheet" type="text/css">';
$htmlExtraInformation['js'][] = '<script src="js/default/plugins/chosen/chosen.jquery.min.js" type="text/javascript"></script>';

if (count($errors) == 0 and ($ui->st('action', 'post') == 'md' or $ui->st('action', 'post') == 'ad')) {

if (!$active) {
$errors['active'] = $sprache->active;
}

if ($ui->st('action', 'post') == 'ad') {

if (!$userID) {

$errors['userID'] = $sprache->user;

} else {

$query = $sql->prepare("SELECT 1 FROM `userdata` WHERE `id`=? AND `resellerid`=? AND `accounttype`='u' LIMIT 1");
$query->execute(array($userID, $resellerLockupID));

if ($query->rowCount() == 0) {
$errors['userID'] = $sprache->user;
}
}
}

if ($ui->st('action', 'post') == 'md') {
$query = $sql->prepare("SELECT `sid` FROM `mysql_external_dbs` WHERE `id`=? AND `resellerid`=? LIMIT 1");
$query->execute(array($id, $resellerLockupID));
$rootID = $query->fetchColumn();
}

if (!isid($rootID, 10)) {

$errors['rootID'] = 'MySQL Server';

} else {

$query = $sql->prepare("SELECT `ip`,`port`,`user`,AES_DECRYPT(`password`,?) AS `decryptedpassword` FROM `mysql_external_servers` WHERE `id`=? AND `resellerid`=? LIMIT 1");
$query->execute(array($aeskey, $rootID, $reseller_id));
foreach ($query->fetchAll(PDO::FETCH_ASSOC) as $mysqlServer) {
$mysqlServerFount = true;
}

if (!isset($mysqlServerFount)) {
$errors['rootID'] = 'MySQL Server';
}
}

$rowCount = 0;

if (count($errors) == 0 and isset($mysqlServer)) {

if ($ui->st('action', 'post') == 'ad') {

$query = $sql->prepare("INSERT INTO `mysql_external_dbs` (`externalID`,`active`,`sid`,`uid`,`description`,`password`,`ips`,`manage_host_table`,`max_queries_per_hour`,`max_updates_per_hour`,`max_connections_per_hour`,`max_userconnections_per_hour`,`resellerid`) VALUES (?,?,?,?,?,AES_ENCRYPT(?,?),?,?,?,?,?,?,?)");
$query->execute(array($externalID, $active, $rootID, $userID, $description, $password, $aeskey, $ips, $manage_host_table, $max_queries_per_hour, $max_updates_per_hour, $max_connections_per_hour, $max_userconnections_per_hour, $resellerLockupID));

$rowCount = $query->rowCount();

$id = $sql->lastInsertId();

$dbName = 'sql' . $id;

$query = $sql->prepare("UPDATE `mysql_external_dbs` SET `dbname`=? WHERE `id`=? AND `resellerid`=? LIMIT 1");
$query->execute(array($dbName, $id, $resellerLockupID));

$loguseraction = '%add% MySQL DB ' . $dbName . ' (' . $mysqlServer['ip'] .')';

} else if ($ui->st('action', 'post') == 'md' and $id and isset($rootServer)) {

$dbName = 'sql' . $id;

$query = $sql->prepare("UPDATE `mysql_external_dbs` SET `externalID`=?,`active`=?,`ips`=?,`manage_host_table`=?,`description`=?,`password`=AES_ENCRYPT(?,?),`max_queries_per_hour`=?,`max_updates_per_hour`=?,`max_connections_per_hour`=?,`max_userconnections_per_hour`=? WHERE `id`=? AND `resellerid`=? LIMIT 1");
$query->execute(array($externalID, $active, $ips, $manage_host_table, $description, $password, $aeskey, $max_queries_per_hour, $max_updates_per_hour, $max_connections_per_hour, $max_userconnections_per_hour, $id, $resellerLockupID));
$rowCount = $query->rowCount();

$loguseraction = '%mod% MySQL DB ' . $dbName . ' (' . $mysqlServer['ip'] .')';
}

$rowCount += customColumns('D', $id, 'save');

if ($rowCount > 0) {

$remotesql = new ExternalSQL($mysqlServer['ip'], $mysqlServer['port'], $mysqlServer['user'], $mysqlServer['decryptedpassword']);

if ($remotesql->error == 'ok') {

if ($active == 'N') {
$password = passwordgenerate(10);
}

if ($ui->st('action', 'post') == 'ad') {
$remotesql->AddDB($dbName, $password, $ips, $max_queries_per_hour, $max_connections_per_hour, $max_updates_per_hour, $max_userconnections_per_hour);
}

if ($ui->st('action', 'post') == 'md') {
$remotesql->ModDB($dbName, $password, $ips, $max_queries_per_hour, $max_connections_per_hour, $max_updates_per_hour, $max_userconnections_per_hour);
}
}

$insertlog->execute();
$template_file = $spracheResponse->table_add;

// No update or insert failed
} else {
$template_file = $spracheResponse->error_table;
}
}
}

// An error occurred during validation
// unset the redirect information and display the form again
if (!$ui->smallletters('action', 2, 'post') or count($errors) != 0) {

unset($header, $text);

// Gather data for adding if needed and define add template
if ($ui->st('d', 'get') == 'ad') {

$table = getUserList($resellerLockupID);
$table2 = array();

$query = $sql->prepare("SELECT s.`id`,s.`ip`,(s.`max_databases`/100)*COUNT(d.`id`) AS `usedpercent` FROM `mysql_external_servers` s LEFT JOIN `mysql_external_dbs` d ON s.`id`=d.`sid` WHERE s.`active`='Y' AND s.`resellerid`=? GROUP BY s.`ip` HAVING `usedpercent`<100 ORDER BY `usedpercent` ASC");
$query->execute(array($reseller_id));
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$table2[$row['id']] = $row['ip'];
}

$template_file = 'admin_mysql_db_add.tpl';

// Gather data for modding in case we have an ID and define mod template
} else if ($ui->st('d', 'get') == 'md' and $id) {

$query = $sql->prepare("SELECT e.*,AES_DECRYPT(e.`password`,?) AS `decryptedpassword`,s.`ip`,u.`cname`,CONCAT(u.`vname`,' ',u.`name`) AS `full_name` FROM `mysql_external_dbs` AS e LEFT JOIN `mysql_external_servers` s ON e.`sid`=s.`id` LEFT JOIN `userdata` u ON e.`uid`=u.`id` WHERE e.`id`=? AND e.`resellerid`=? LIMIT 1");
$query->execute(array($aeskey, $id, $resellerLockupID));
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {

$externalID = $row['externalID'];
$ip = $row['ip'];
$manage_host_table = $row['manage_host_table'];
$ips = $row['ips'];
$active = $row['active'];
$description = $row['description'];
$dbName = $row['dbname'];
$password = $row['decryptedpassword'];

$userName = trim($row['cname'] . ' ' . $row['full_name']);
$table2[$row['sid']] = $row['ip'];
}

// Check if database entry exists and if not display 404 page
$template_file = ($query->rowCount() > 0) ? 'admin_mysql_db_md.tpl' : 'admin_404.tpl';

// Show 404 if GET parameters did not add up or no ID was given with mod
} else {
$template_file = 'admin_404.tpl';
}
}

} else if (($ui->st('d', 'get') == 'ri' or $ui->st('d', 'get') == 'dl') and $id) {

$query = $sql->prepare("SELECT `dbname`,`description` FROM `mysql_external_dbs` WHERE `id`=? AND `resellerid`=? LIMIT 1");
$query->execute(array($id, $resellerLockupID));
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$dbName = $row['dbname'];
$description = $row['description'];
}

$serverFound = $query->rowCount();

if (($ui->st('action', 'post') == 'ri' or $ui->st('action', 'post') == 'dl') and count($errors) == 0 and $serverFound > 0) {

$query = $sql->prepare("SELECT e.`dbname`,e.`ips`,e.`max_queries_per_hour`,e.`max_connections_per_hour`,e.`max_updates_per_hour`,e.`max_userconnections_per_hour`,AES_DECRYPT(e.`password`,?) AS `decryptedpassword`,s.`ip`,AES_DECRYPT(s.`password`,?) AS `decryptedpassword2`,s.`port`,s.`user` FROM `mysql_external_dbs` e INNER JOIN `mysql_external_servers` s ON e.`sid`=s.`id` WHERE e.`id`=? AND e.`resellerid`=? LIMIT 1");
$query->execute(array($aeskey, $aeskey, $id, $resellerLockupID));
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {

$remotesql = new ExternalSQL ($row['ip'], $row['port'], $row['user'], $row['decryptedpassword2']);

if ($remotesql->error == 'ok') {

$remotesql->DelDB($row['dbname']);
$remotesql->DelUser($row['dbname']);

if ($ui->st('action', 'post') == 'ri') {

$remotesql->AddDB($row['dbname'], $row['decryptedpassword'], $row['ips'], $row['max_queries_per_hour'], $row['max_connections_per_hour'], $row['max_updates_per_hour'], $row['max_userconnections_per_hour']);

$loguseraction = '%ri% MYSQL DB ' . $row['dbname'] . ' (' . $row['ip'] . ')';

$template_file = $spracheResponse->reinstall_success;

} else {

$query2 = $sql->prepare("DELETE FROM `mysql_external_dbs` WHERE `id`=? AND `resellerid`=? LIMIT 1");
$query2->execute(array($id, $resellerLockupID));

customColumns('D', $id, 'del');

$loguseraction = '%del% MySQL DB ' . $row['dbname'] . ' (' . $row['ip'] .')';

$template_file = $spracheResponse->table_del;
}

$insertlog->execute();

} else {
$template_file = $remotesql->error;
}
}
}

// Nothing submitted yet or csfr error, display the delete form
if (!$ui->st('action', 'post') or count($errors) != 0 and $serverFound > 0) {
$template_file = ($ui->st('d', 'get') == 'ri') ? 'admin_mysql_db_ri.tpl' : 'admin_mysql_db_dl.tpl';
}

// Check if we could find an entry and if not display 404 page
if (!isset($template_file)) {
$template_file = 'admin_404.tpl';
}

} else {

configureDateTables('-1', '1, "asc"', 'ajax.php?w=datatable&d=mysqldb');

$template_file = 'admin_mysql_db_list.tpl';
}
Loading

0 comments on commit bf9a70a

Please sign in to comment.