refactor: OOP code movements started :)

This commit is contained in:
Martin Ambrus
2017-10-25 13:38:38 +02:00
parent ab461e5945
commit 30d3b9c4fa
20 changed files with 484 additions and 76 deletions
+6 -9
View File
@@ -1,5 +1,8 @@
<?php
include_once("src/Entity/User.php");
use App\Entity\User;
#################################################################################
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
## --------------------------------------------------------------------------- ##
@@ -62,10 +65,7 @@ class Account {
else if(USRNM_SPECIAL && preg_match("/[:,\\. \\n\\r\\t\\s\\<\\>]+/", $_POST['name'])) {
$form->addError("name",USRNM_CHAR);
}
else if($database->checkExist($_POST['name'],0)) {
$form->addError("name",USRNM_TAKEN);
}
else if($database->checkExist_activate($_POST['name'],0)) {
else if(User::exists($database,$_POST['name'])) {
$form->addError("name",USRNM_TAKEN);
}
@@ -89,10 +89,7 @@ class Account {
if(!$this->validEmail($_POST['email'])) {
$form->addError("email",EMAIL_INVALID);
}
else if($database->checkExist($_POST['email'],1)) {
$form->addError("email",EMAIL_TAKEN);
}
else if($database->checkExist_activate($_POST['email'],1)) {
else if(User::exists($database,$_POST['email'])) {
$form->addError("email",EMAIL_TAKEN);
}
}
@@ -190,7 +187,7 @@ class Account {
if(!isset($_POST['user']) || $_POST['user'] == "") {
$form->addError("user",$user);
}
else if(!$database->checkExist($_POST['user'],0)) {
else if(!User::exists($database,$_POST['user'])) {
$form->addError("user",USR_NT_FOUND);
}
if(!isset($_POST['pw']) || $_POST['pw'] == "") {
+4 -1
View File
@@ -8,6 +8,9 @@
## Copyright: TravianZ (c) 2014. All rights reserved. ##
#########################################################
include_once("../../../src/Entity/User.php");
use App\Entity\User;
include_once("../../config.php");
include_once("../../Session.php");
include_once("../../Automation.php");
@@ -78,7 +81,7 @@ else
$act = "";
// Check username not already registered
if($database->checkExist($userName,0))
if(User::exists($database, $userName))
{
// Name already used, do nothing except update $skipped
$skipped ++;
+3 -3
View File
@@ -38,7 +38,7 @@ class adm_DB {
var $connection;
function __construct(){
global $database;
$database = new MYSQLi_DB;
$database = new MYSQLi_DB(SQL_SERVER, SQL_USER, SQL_PASS, SQL_DB);
$this->connection = $database->return_link();
}
@@ -763,8 +763,8 @@ class adm_DB {
for($i=0;$i<=count($cropholder)-1;$i++) { $basecrop+= $bid4[$fdata[$cropholder[$i]]]['prod']; }
$crop = $basecrop + $basecrop * 0.25 * $ocounter[3];
$jcrop=0;
if($grainmill >= 1) $jcrop=$bid8[$grainmill]['attri'];
if($bakery >= 1) $jcrop+=$bid9[$bakery]['attri'];
if($grainmill >= 1) $jcrop=(isset($bid8[$grainmill]['attri']) ? $bid8[$grainmill]['attri'] : 0);
if($bakery >= 1) $jcrop+=(isset($bid9[$bakery]['attri']) ? $bid9[$bakery]['attri'] : 0);
$crop += $basecrop /100 * $jcrop;
if($b4 > time()) {
$crop *= 1.25;
+4 -4
View File
@@ -20,7 +20,7 @@
class funct {
function CheckLogin(){
if($_SESSION['access'] >= MULTIHUNTER and $_SESSION['id']){
if(isset($_SESSION['access']) && $_SESSION['access'] >= MULTIHUNTER and $_SESSION['id']){
return true;
}else{
return false;
@@ -212,14 +212,14 @@ class funct {
$funct = new funct;
if($funct->CheckLogin()){
if($_GET['action']){
if(isset($_GET['action']) && $_GET['action']){
$funct->Act($_GET);
}
if($_POST['action']){
if(isset($_POST['action']) && $_POST['action']){
$funct->Act2($_POST);
}
}
if($_POST['action']=='login'){
if(isset($_POST['action']) && $_POST['action']=='login'){
$funct->LogIN($_POST['name'],$_POST['pw']);
}
?>
+5 -2
View File
@@ -16,7 +16,10 @@
## ##
#################################################################################
class Alliance {
include_once("src/Entity/User.php");
use App\Entity\User;
class Alliance {
public $gotInvite = false;
public $inviteArray = array();
@@ -114,7 +117,7 @@
$form->addError("perm", NO_PERMISSION);
}elseif(!isset($post['a_name']) || $post['a_name'] == "") {
$form->addError("name1", NAME_EMPTY);
}elseif(!$database->checkExist(stripslashes($post['a_name']), 0)) {
}elseif(!User::exists($database, $post['a_name'])) {
$form->addError("name2", NAME_NO_EXIST."".stripslashes(stripslashes($post['a_name'])));
}elseif($UserData['id'] == $session->uid) {
$form->addError("name3", SAME_NAME);
+2 -2
View File
@@ -3891,10 +3891,10 @@ $wallimg = "<img src=\"".GP_LOCATE."img/g/g3".$targettribe."Icon.gif\" height=\"
}
for($i=0;$i<=count($cropholder)-1;$i++) { $crop+= $bid4[$this->bountyresarray[$cropholder[$i]]]['prod']; }
if($grainmill >= 1) {
$crop += $crop /100 * $bid8[$grainmill]['attri'];
$crop += $crop /100 * (isset($bid8[$grainmill]['attri']) ? $bid8[$grainmill]['attri'] : 0);
}
if($bakery >= 1) {
$crop += $crop /100 * $bid9[$bakery]['attri'];
$crop += $crop /100 * (isset($bid9[$bakery]['attri']) ? $bid9[$bakery]['attri'] : 0);
}
if($this->bountyocounter[3] != 0) {
$crop += $crop*0.25*$this->bountyocounter[3];
+1 -1
View File
@@ -110,7 +110,7 @@ class Building {
public function canBuild($id,$tid) {
global $village,$session,$database;
$demolition = $database->getDemolition($village->wid);
if($demolition[0]['buildnumber']==$id) { return 11; }
if((isset($demolition[0])) && $demolition[0]['buildnumber']==$id) { return 11; }
if($this->isMax($tid,$id)) {
return 1;
} else if($this->isMax($tid,$id,1) && ($this->isLoop($id) || $this->isCurrent($id))) {
+209 -42
View File
@@ -1,6 +1,4 @@
<?php
#################################################################################
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
## --------------------------------------------------------------------------- ##
@@ -17,15 +15,216 @@
## ##
#################################################################################
$src_prefix = '';
if (substr(getcwd(), -5) === 'Admin') {
$src_prefix = '../';
}
if (substr(getcwd(), -4) === 'Mods') {
$src_prefix = '../../../';
}
include_once("config.php");
include_once($src_prefix."src/Database/IDbConnection.php");
include_once($src_prefix."src/Utils/Math.php");
class MYSQLi_DB {
use App\Database\IDbConnection;
use App\Utils\Math;
var $dblink;
function __construct() {
$this->dblink = mysqli_connect(SQL_SERVER, SQL_USER, SQL_PASS) or die(mysqli_error($this->dblink));
mysqli_select_db($this->dblink, SQL_DB);
mysqli_query($this->dblink,"SET NAMES 'UTF8'");
class MYSQLi_DB implements IDbConnection {
private
/**
* @var string MySQL server hostname to connect to.
*/
$hostname = 'localhost',
/**
* @var int MySQL server port to connect to.
*/
$port = 3306,
/**
* @var string Username to authenticate with to the MySQL connection.
*/
$username = 'root',
/**
* @var string Password to authenticate with to the MySQL connection.
*/
$password = '',
/**
* @var string Database to use with TravianZ.
*/
$dbname = 'travian',
/**
* @var int Counter of all SELECT queries performed.
*/
$selectQueryCount = 0,
/**
* @var int Counter of all INSERT queries performed.
*/
$insertQueryCount = 0,
/**
* @var int Counter of all UPDATE queries performed.
*/
$updateQueryCount = 0,
/**
* @var int Counter of all DELETE queries performed.
*/
$deleteQueryCount = 0,
/**
* @var int Counter of all REPLACE queries performed.
*/
$replaceQueryCount = 0;
public $dblink;
/**
*
* Constructor.
* Will initialize the connection to MySQL
* and die on any error it would encounter.
*
* @example $db = new MYSQLi_DB(SQL_SERVER, SQL_USER, SQL_PASS, SQL_DB);
*
* @param string $hostname Hostname of the MySQL server.
* @param string $username Username to be used to to connect.
* @param string $password Password to be used to to connect.
* @param string $dbname Name of the database to use.
* @param int $port [Optional] server port to connect to. Default: 3306
* @return void This method doesn't have a return value.
*/
public function __construct(string $hostname, string $username, string $password, string $dbname, int $port = 3306) {
$this->hostname = $hostname;
$this->port = $port;
$this->username = $username;
$this->password = $password;
$this->dbname = $dbname;
// connect to the DB
if (!$this->connect()) {
die(mysqli_errno($this->dblink));
}
// we will operate in UTF8
mysqli_query($this->dblink,"SET NAMES 'UTF8'");
}
/**
* {@inheritDoc}
* @see \App\Database\IDbConnection::connect()
*/
public function connect(): bool {
// try to connect
$this->dblink = mysqli_connect($this->hostname, $this->username, $this->password);
// return on error
if (mysqli_error($this->dblink)) {
return false;
}
// select the DB to use
mysqli_select_db($this->dblink, $this->dbname);
// return on error
if (mysqli_error($this->dblink)) {
return false;
} else {
// connected and DB exists, we're good to go
return true;
}
}
/**
* {@inheritDoc}
* @see \App\Database\IDbConnection::disconnect()
*/
public function disconnect(): bool {
if ($this->dblink) {
if (!$this->dblink->close()) {
return false;
}
$this->dblink = null;
}
return true;
}
/**
* {@inheritDoc}
* @see \App\Database\IDbConnection::reconnect()
*/
public function reconnect(): bool {
$this->disconnect();
return $this->connect();
}
/**
* {@inheritDoc}
* @see \App\Database\IDbConnection::query()
*/
public function query_new(string $statement, ...$params) {
// check for SELECT
preg_match('/[^AZ-az]*(\()?[^AZ-az]*SELECT/i', $statement, $matches);
// SELECT statement it is...
if (count($matches)) {
if ($prep = mysqli_prepare($this->dblink, $statement)) {
// prepare all parameter types
$types = [];
foreach ($params as $param) {
// default to string, change if neccessary
$paramType = 's';
if (Math::isInt($param)) {
$paramType = 'i';
} else if (Math::isFloat($param)) {
$paramType = 'd';
}
$types[] = $paramType;
}
// dynamically bind parameters
$bind_names = [implode('', $types)];
for ($i=0; $i<count($params); $i++){
$bind_name = 'bind' . $i;
$$bind_name = $params[$i];
$bind_names[] = &$$bind_name;
}
call_user_func_array(array($prep, 'bind_param'),$bind_names);
// execute the statement to get its value back
if (mysqli_stmt_execute($prep)) {
$this->selectQueryCount++;
return mysqli_stmt_get_result($prep);
} else {
throw new Exception('Failed to execute an SQL statement!');
}
} else {
throw new Exception('Failed to prepare an SQL statement!');
}
}
return false;
}
/**
* {@inheritDoc}
* @see \App\Database\IDbConnection::is_connected()
*/
public function is_connected(): bool {
return ($this->dblink ? true : false);
}
function escape($value) {
@@ -111,38 +310,6 @@ class MYSQLi_DB {
return mysqli_query($this->dblink,$q);
}
function checkExist($ref, $mode) {
list($ref, $mode) = $this->escape_input($ref, $mode);
if(!$mode) {
$q = "SELECT username FROM " . TB_PREFIX . "users where username = '$ref' LIMIT 1";
} else {
$q = "SELECT email FROM " . TB_PREFIX . "users where email = '$ref' LIMIT 1";
}
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
return true;
} else {
return false;
}
}
function checkExist_activate($ref, $mode) {
list($ref, $mode) = $this->escape_input($ref, $mode);
if(!$mode) {
$q = "SELECT username FROM " . TB_PREFIX . "activate where username = '$ref' LIMIT 1";
} else {
$q = "SELECT email FROM " . TB_PREFIX . "activate where email = '$ref' LIMIT 1";
}
$result = mysqli_query($this->dblink,$q);
if(mysqli_num_rows($result)) {
return true;
} else {
return false;
}
}
public function hasBeginnerProtection($vid) {
list($vid) = $this->escape_input($vid);
@@ -4205,7 +4372,7 @@ class MYSQLi_DB {
for($i=0;$i<=count($cropholder)-1;$i++) { $basecrop+= $bid4[$buildarray[$cropholder[$i]]]['prod']; }
$crop = $basecrop + $basecrop * 0.25 * $cropo;
if($grainmill >= 1 || $bakery >= 1) {
$crop += $basecrop /100 * ($bid8[$grainmill]['attri'] + $bid9[$bakery]['attri']);
$crop += $basecrop /100 * ((isset($bid8[$grainmill]['attri']) ? $bid8[$grainmill]['attri'] : 0) + (isset($bid9[$bakery]['attri']) ? $bid9[$bakery]['attri'] : 0));
}
if($bonus > time()) {
$crop *= 1.25;
@@ -4635,7 +4802,7 @@ References:
// database is not needed if we're displaying static pages
$req_file = basename($_SERVER['PHP_SELF']);
if (!in_array($req_file, ['tutorial.php', 'anleitung.php'])) {
$database = new MYSQLi_DB;
$database = new MYSQLi_DB(SQL_SERVER, SQL_USER, SQL_PASS, SQL_DB);
$link = $database->return_link();
$GLOBALS['db'] = $database;
$GLOBALS['link'] = $database->return_link();
+6
View File
@@ -1,4 +1,6 @@
<?php
use App\Entity\User;
ob_start(); // Enesure, that no more header already been sent error not showing up again
mb_internal_encoding("UTF-8"); // Add for utf8 varriables.
@@ -282,6 +284,10 @@ $form = new Form;
// if there is no user, we'd try to load messages for user with ID 0, which is wrong
if (!empty($_SESSION['id_user'])) {
$message = new Message;
// create a global user variable which will later be removed from here
// and created + retrieved either via Service Locator or other DI concept
$user = new User((int) $_SESSION['id_user'], $database);
}
?>
+1 -1
View File
@@ -229,7 +229,7 @@ class Village {
for($i=0;$i<=count($cropholder)-1;$i++) { $basecrop+= $bid4[$this->resarray[$cropholder[$i]]]['prod']; }
$crop = $basecrop + $basecrop * 0.25 * $this->ocounter[3];
if($grainmill >= 1 || $bakery >= 1) {
$crop += $basecrop /100 * ($bid8[$grainmill]['attri'] + $bid9[$bakery]['attri']);
$crop += $basecrop /100 * (isset($bid8[$grainmill]['attri']) ? $bid8[$grainmill]['attri'] : 0) + (isset($bid9[$bakery]['attri']) ? $bid9[$bakery]['attri'] : 0);
}
if($session->bonus4 == 1) {
$crop *= 1.25;