changes to how testactions work

This commit is contained in:
Andreas 2019-09-19 12:08:16 +02:00
parent 7c009bcd96
commit 6121e31459
9 changed files with 303 additions and 231 deletions

View File

@ -33,7 +33,7 @@ class MVCController{
if(isset($_POST['testaction'])){ if(isset($_POST['testaction'])){
$this->testaction = "./model/testactions/model_".$_POST['testaction'].".php"; $this->testaction = "./model/testactions/TA_".$_POST['testaction'].".php";
} }
@ -67,12 +67,10 @@ class MVCController{
//check if testaction is valid //check if testaction is valid
if(file_exists($this->testaction)){ if(file_exists($this->testaction)){
echo('{"output": [');
//execute testaction //execute testaction
include_once($this->testaction); require_once($this->testaction);
TestUtils::log('End of output', "OK", false); $testactionClassname = "TA_".$_POST['testaction'];
echo("]}"); $testactionInstance = new $testactionClassname();
} }
} }

View File

@ -1,21 +0,0 @@
<?php
class TestUtils{
public static $log = [];
public static $status;
public static function log($output, $status = "OK",$addComma = true){
$loginput = [];
$loginput['message'] = $output;
$loginput['status'] = $status;
echo(json_encode($loginput));
if($addComma){
echo(',');
echo("\n");
}
return;
}
public static function returnLog(){
echo(json_encode(self::$log));
}
}

View File

@ -1,6 +1,5 @@
<?php <?php
require_once('./controller/TestUtils.php'); require_once('./controller/TestUtils.php');
//date_default_timezone_set('Europe/Amsterdam'); //date_default_timezone_set('Europe/Amsterdam');
require_once('./controller/MVCController.php'); require_once('./controller/MVCController.php');
require_once('./controller/UserSession.php'); require_once('./controller/UserSession.php');
@ -10,5 +9,4 @@ $mvcController->executeModel();
if(!isset($_POST['testaction'])){ if(!isset($_POST['testaction'])){
include_once("./view/content_pagetemplate.php"); include_once("./view/content_pagetemplate.php");
} }
?> ?>

View File

@ -0,0 +1,182 @@
<?php
class TA_CreateDB extends TestAction{
function TA_CreateDB(){
parent();
}
function execute(){
try{
if(getenv("SQL_CREDENTIALS") !== false){
$sql_server = getenv("SQL_SERVER");
$sql_username = getenv("SQL_USERNAME");
$sql_password = getenv("SQL_PASSWORD");
$sql_database = getenv("SQL_DATABASE");
}
else{
$sql_server = "localhost";
$sql_username = "root";
$sql_password = "kankerlow";
$sql_database = "webforum";
}
$host = $sql_server;
$db = $sql_database;
$user = $sql_username;
$pass = $sql_password;
//connect to sql server
$con = new PDO( "mysql:host=$host;charset=utf8", $user, $pass );
//check if db exists
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db'");
//db exists
if($query->fetchColumn() != 1){
$query = $con->query("CREATE DATABASE $db");
self::logMessage('db doesnt exist');
}
//db doesn't exist
else{
self::logMessage('db already exists, skipping');
}
//select db
$con->exec("USE $db");
//test if table exists
$table = 'users';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
self::logMessage($query->fetchColumn());
//table doesn't exist
if($query->fetchColumn() != 4){
self::logMessage('table doesnt exist');
$query = $con->query(
" CREATE TABLE `users` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(256) NOT NULL,
`email` varchar(256) NOT NULL,
`password` varchar(256) NOT NULL,
`reg_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`logMessagein_date` datetime NOT NULL,
`reg_ip` varchar(256) NOT NULL,
`permissions` int(11) NOT NULL DEFAULT '-1',
`active` tinyint(1) DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
self::logMessage("created table $table");
}
//table exists
else{
self::logMessage("table $table already exists, skipping");
}
$table = 'usersessions';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
if($query->fetchColumn() != 4){
self::logMessage('table doesnt exist');
$query = $con->query(
" CREATE TABLE `usersessions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`token` varchar(256) NOT NULL,
`expires` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
self::logMessage("created table $table");
}
//table exists
else{
self::logMessage("table $table already exists, skipping");
}
$table = 'email_activation_keys';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
if($query->fetchColumn() != 4){
self::logMessage('table doesnt exist');
$query = $con->query(
" CREATE TABLE `email_activation_keys` (
`id` int(16) NOT NULL AUTO_INCREMENT,
`users_id` int(16) NOT NULL,
`activationkey` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
self::logMessage("created table $table");
}
//table exists
else{
self::logMessage("table $table already exists, skipping");
}
$table = 'board';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
if($query->fetchColumn() != 4){
self::logMessage('table doesnt exist');
$query = $con->query(
" CREATE TABLE `board` (
`ID` int(16) NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`description` text NOT NULL,
`permLevel` int(16) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
self::logMessage("created table $table");
}
//table exists
else{
self::logMessage("table $table already exists, skipping");
}
$table = 'thread';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
if($query->fetchColumn() != 4){
self::logMessage('table doesnt exist');
$query = $con->query(
" CREATE TABLE `thread` (
`ID` int(16) NOT NULL AUTO_INCREMENT,
`users_ID` int(16) NOT NULL,
`board_ID` int(16) NOT NULL,
`title` varchar(256) NOT NULL,
`text` text NOT NULL,
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
self::logMessage("created table $table");
}
//table exists
else{
self::logMessage("table $table already exists, skipping");
}
$table = 'reply';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
if($query->fetchColumn() != 4){
self::logMessage('table doesnt exist');
$query = $con->query(
" CREATE TABLE `reply` (
`ID` int(16) NOT NULL AUTO_INCREMENT,
`thread_ID` int(16) NOT NULL,
`users_ID` int(16) NOT NULL,
`content` text NOT NULL,
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
self::logMessage("created table $table");
}
//table exists
else{
self::logMessage("table $table already exists, skipping");
}
}
catch(PDOException $e){
self::logMessage('PDO ERROR', "FAILURE");
die("pdo exception, cannot connect to sql:<br> $e");
}
}
}
?>

View File

@ -0,0 +1,63 @@
<?php
class TA_PopulateDB extends TestAction{
function TA_PopulateDB(){
parent();
}
function execute(){
try{
if(getenv("SQL_CREDENTIALS") !== false){
$sql_server = getenv("SQL_SERVER");
$sql_username = getenv("SQL_USERNAME");
$sql_password = getenv("SQL_PASSWORD");
$sql_database = getenv("SQL_DATABASE");
}
else{
$sql_server = "localhost";
$sql_username = "root";
$sql_password = "kankerlow";
$sql_database = "webforum";
}
$host = $sql_server;
$db = $sql_database;
$user = $sql_username;
$pass = $sql_password;
//connect to sql server
$con = new PDO( "mysql:host=$host;charset=utf8", $user, $pass );
$con->exec("USE $db");
self::logMessage('table doesnt exist', "OK");
$query = $con->query("INSERT INTO users (username, email, password, login_date, reg_ip, active) VALUES ( 'andreas', 'andreas@andreas.nl', 'jenk', '2019-01-01 14:35:33', '192.168.0.2', 1),
( 'bram', 'bram@bram.nl', 'jenk', '2019-01-01 14:35:33', '192.168.0.1', 1)");
self::logMessage("created test users", "OK");
$query = $con->query("INSERT INTO `board` (`name`, `description`, `permLevel`) VALUES ('General Discussion', 'Plek om algemene discussie te voeren.', '0'),
('Off Topic', 'Voor alle irrelevante zooi.', '0')");
self::logMessage("created test boards", "OK");
$query = $con->query("INSERT INTO `thread` (`users_ID`, `board_ID`, `title`, `text`, `date_created`) VALUES ('1', '1', 'Test thread', 'Deze thread is een test.', '2019-06-20 13:55:37'),
('1', '2', 'Waa', 'Frist niffo', '2019-06-20 13:56:42')");
self::logMessage("created test threads", "OK");
$query = $con->query("INSERT INTO `reply` (`thread_ID`, `users_ID`, `content`, `date_created`) VALUES ('1', '1', 'heehee eks dee', '2019-06-21 11:01:57'),
('1', '1', 'hoi\r\n', '2019-06-21 11:07:25'),
('2', '2', 'fristi niBBa', '2019-06-21 11:08:08'),
('1', '1', 'was jouw prebleem', '2019-06-21 14:41:00'),
('1', '2', 'Mijn naam is bram', '2019-06-21 17:58:12'),
('1', '2', 'huh wuddufuq', '2019-06-21 17:58:29'),
('1', '1', 'huts a neef', '2019-06-21 17:59:27')");
self::logMessage("created test replies", "OK");
}
catch(PDOException $e){
self::logMessage("created test replies", "FAILURE");
die("pdo exception, cannot connect to sql:<br> $e");
}
}
}
?>

View File

@ -0,0 +1,36 @@
<?php
class TestAction{
function TestAction(){
if(isset($_POST['auth'])){
if($_POST['auth'] == getenv('ADMIN_ACTION_KEY')){
execute();
}else{
self::logMessage('you have no authorization to do that', 'FAILURE');
}
}else{
self::logMessage('you have no authorization to do that', 'FAILURE');
}
self::returnLogAsText();
}
function execute(){
self::logMessage('Unoverridden execute called on TestAction: '.$this, 'FAILURE');
}
public static $log = [];
public static $status;
public static function logMessage($message, $status = "OK"){
$loginput = [];
$loginput['message'] = $message;
$loginput['status'] = $status;
arr_push(self::log, $loginput);
return;
}
public static function returnLogAsJson(){
echo(json_encode(self::$log));
return;
}
public static function returnLogAsText(){
for($i = 0; $i<sizeof(self::$log); $i++){
echo("[".self::log[i]['stats']."] ".self::log[i]['message']);
}
}
}

View File

@ -1,196 +0,0 @@
<?php
if(isset($_POST['auth'])){
if($_POST['auth'] == getenv('ADMIN_ACTION_KEY')){
createDB();
}
}else{
TestUtils::log('you have no authorization to do that', 'FAILURE');
}
function createDB(){
try{
if(getenv("SQL_CREDENTIALS") !== false){
$sql_server = getenv("SQL_SERVER");
$sql_username = getenv("SQL_USERNAME");
$sql_password = getenv("SQL_PASSWORD");
$sql_database = getenv("SQL_DATABASE");
}
else{
$sql_server = "localhost";
$sql_username = "root";
$sql_password = "kankerlow";
$sql_database = "webforum";
}
$host = $sql_server;
$db = $sql_database;
$user = $sql_username;
$pass = $sql_password;
//connect to sql server
$con = new PDO( "mysql:host=$host;charset=utf8", $user, $pass );
//check if db exists
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$db'");
//db exists
if($query->fetchColumn() != 1){
$query = $con->query("CREATE DATABASE $db");
TestUtils::log('db doesnt exist');
}
//db doesn't exist
else{
TestUtils::log('db already exists, skipping');
}
//select db
$con->exec("USE $db");
//test if table exists
$table = 'users';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
TestUtils::log($query->fetchColumn());
//table doesn't exist
if($query->fetchColumn() != 4){
TestUtils::log('table doesnt exist');
$query = $con->query(
" CREATE TABLE `users` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(256) NOT NULL,
`email` varchar(256) NOT NULL,
`password` varchar(256) NOT NULL,
`reg_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`login_date` datetime NOT NULL,
`reg_ip` varchar(256) NOT NULL,
`permissions` int(11) NOT NULL DEFAULT '-1',
`active` tinyint(1) DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
TestUtils::log("created table $table");
}
//table exists
else{
TestUtils::log("table $table already exists, skipping");
}
$table = 'usersessions';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
if($query->fetchColumn() != 4){
TestUtils::log('table doesnt exist');
$query = $con->query(
" CREATE TABLE `usersessions` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL,
`token` varchar(256) NOT NULL,
`expires` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
TestUtils::log("created table $table");
}
//table exists
else{
TestUtils::log("table $table already exists, skipping");
}
$table = 'email_activation_keys';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
if($query->fetchColumn() != 4){
TestUtils::log('table doesnt exist');
$query = $con->query(
" CREATE TABLE `email_activation_keys` (
`id` int(16) NOT NULL AUTO_INCREMENT,
`users_id` int(16) NOT NULL,
`activationkey` varchar(256) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
TestUtils::log("created table $table");
}
//table exists
else{
TestUtils::log("table $table already exists, skipping");
}
$table = 'board';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
if($query->fetchColumn() != 4){
TestUtils::log('table doesnt exist');
$query = $con->query(
" CREATE TABLE `board` (
`ID` int(16) NOT NULL AUTO_INCREMENT,
`name` varchar(256) NOT NULL,
`description` text NOT NULL,
`permLevel` int(16) NOT NULL DEFAULT '0',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
TestUtils::log("created table $table");
}
//table exists
else{
TestUtils::log("table $table already exists, skipping");
}
$table = 'thread';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
if($query->fetchColumn() != 4){
TestUtils::log('table doesnt exist');
$query = $con->query(
" CREATE TABLE `thread` (
`ID` int(16) NOT NULL AUTO_INCREMENT,
`users_ID` int(16) NOT NULL,
`board_ID` int(16) NOT NULL,
`title` varchar(256) NOT NULL,
`text` text NOT NULL,
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
TestUtils::log("created table $table");
}
//table exists
else{
TestUtils::log("table $table already exists, skipping");
}
$table = 'reply';
$query = $con->query("SELECT COUNT(*) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '$table'");
if($query->fetchColumn() != 4){
TestUtils::log('table doesnt exist');
$query = $con->query(
" CREATE TABLE `reply` (
`ID` int(16) NOT NULL AUTO_INCREMENT,
`thread_ID` int(16) NOT NULL,
`users_ID` int(16) NOT NULL,
`content` text NOT NULL,
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1");
TestUtils::log("created table $table");
}
//table exists
else{
TestUtils::log("table $table already exists, skipping");
}
}
catch(PDOException $e){
TestUtils::log('PDO ERROR', "FAILURE");
die("pdo exception, cannot connect to sql:<br> $e");
}
}
?>

View File

@ -4,7 +4,7 @@ if(isset($_POST['auth'])){
populateDB(); populateDB();
} }
}else{ }else{
echol('you have no authorization to do that'); self::logMessage('you have no authorization to do that', "OK");
} }
function populateDB(){ function populateDB(){
@ -32,16 +32,16 @@ function populateDB(){
echol('table doesnt exist'); self::logMessage('table doesnt exist', "OK");
$query = $con->query("INSERT INTO users (username, email, password, login_date, reg_ip, active) VALUES ( 'andreas', 'andreas@andreas.nl', 'jenk', '2019-01-01 14:35:33', '192.168.0.2', 1), $query = $con->query("INSERT INTO users (username, email, password, login_date, reg_ip, active) VALUES ( 'andreas', 'andreas@andreas.nl', 'jenk', '2019-01-01 14:35:33', '192.168.0.2', 1),
( 'bram', 'bram@bram.nl', 'jenk', '2019-01-01 14:35:33', '192.168.0.1', 1)"); ( 'bram', 'bram@bram.nl', 'jenk', '2019-01-01 14:35:33', '192.168.0.1', 1)");
echol("created test users"); self::logMessage("created test users", "OK");
$query = $con->query("INSERT INTO `board` (`name`, `description`, `permLevel`) VALUES ('General Discussion', 'Plek om algemene discussie te voeren.', '0'), $query = $con->query("INSERT INTO `board` (`name`, `description`, `permLevel`) VALUES ('General Discussion', 'Plek om algemene discussie te voeren.', '0'),
('Off Topic', 'Voor alle irrelevante zooi.', '0')"); ('Off Topic', 'Voor alle irrelevante zooi.', '0')");
echol("created test boards"); self::logMessage("created test boards", "OK");
$query = $con->query("INSERT INTO `thread` (`users_ID`, `board_ID`, `title`, `text`, `date_created`) VALUES ('1', '1', 'Test thread', 'Deze thread is een test.', '2019-06-20 13:55:37'), $query = $con->query("INSERT INTO `thread` (`users_ID`, `board_ID`, `title`, `text`, `date_created`) VALUES ('1', '1', 'Test thread', 'Deze thread is een test.', '2019-06-20 13:55:37'),
('1', '2', 'Waa', 'Frist niffo', '2019-06-20 13:56:42')"); ('1', '2', 'Waa', 'Frist niffo', '2019-06-20 13:56:42')");
echol("created test threads"); self::logMessage("created test threads", "OK");
$query = $con->query("INSERT INTO `reply` (`thread_ID`, `users_ID`, `content`, `date_created`) VALUES ('1', '1', 'heehee eks dee', '2019-06-21 11:01:57'), $query = $con->query("INSERT INTO `reply` (`thread_ID`, `users_ID`, `content`, `date_created`) VALUES ('1', '1', 'heehee eks dee', '2019-06-21 11:01:57'),
('1', '1', 'hoi\r\n', '2019-06-21 11:07:25'), ('1', '1', 'hoi\r\n', '2019-06-21 11:07:25'),
('2', '2', 'fristi niBBa', '2019-06-21 11:08:08'), ('2', '2', 'fristi niBBa', '2019-06-21 11:08:08'),
@ -49,9 +49,10 @@ function populateDB(){
('1', '2', 'Mijn naam is bram', '2019-06-21 17:58:12'), ('1', '2', 'Mijn naam is bram', '2019-06-21 17:58:12'),
('1', '2', 'huh wuddufuq', '2019-06-21 17:58:29'), ('1', '2', 'huh wuddufuq', '2019-06-21 17:58:29'),
('1', '1', 'huts a neef', '2019-06-21 17:59:27')"); ('1', '1', 'huts a neef', '2019-06-21 17:59:27')");
echol("created test replies"); self::logMessage("created test replies", "OK");
} }
catch(PDOException $e){ catch(PDOException $e){
self::logMessage("created test replies", "FAILURE");
die("pdo exception, cannot connect to sql:<br> $e"); die("pdo exception, cannot connect to sql:<br> $e");
} }
} }

View File

@ -0,0 +1,11 @@
<?php
if(isset($_POST['auth'])){
if($_POST['auth'] == getenv('ADMIN_ACTION_KEY')){
execute();
}
}else{
echol('you have no authorization to do that');
}
function execute(){
}