mirror of
https://github.com/Shadowss/TravianZ.git
synced 2026-07-08 21:56:09 +00:00
refactor: OOP code movements started :)
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
#################################################################################
|
||||
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
|
||||
## --------------------------------------------------------------------------- ##
|
||||
## Project: TravianZ ##
|
||||
## Filename User.php ##
|
||||
## Developed by: martinambrus ##
|
||||
## License: TravianZ Project ##
|
||||
## Copyright: TravianZ (c) 2010-2017. All rights reserved. ##
|
||||
## URLs: https://travian.martinambrus.com ##
|
||||
## Source code: https://github.com/Shadowss/TravianZ ##
|
||||
## ##
|
||||
#################################################################################
|
||||
namespace App\Database;
|
||||
|
||||
/**
|
||||
* Defines database connection class structure
|
||||
* and all required methods for it to work.
|
||||
*
|
||||
* @author martinambrus
|
||||
*/
|
||||
interface IDbConnection {
|
||||
|
||||
/**
|
||||
* Method used to connect to the database
|
||||
* using the data provided via the DB class' constructor.
|
||||
*
|
||||
* @return bool Returns true if the connection was made and the chosen database exists, false otherwise.
|
||||
*/
|
||||
public function connect(): bool;
|
||||
|
||||
/**
|
||||
* Method used to disconnect from the database.
|
||||
*
|
||||
* @return bool Returns true if the disconnect was successful, false otherwise.
|
||||
*/
|
||||
public function disconnect(): bool;
|
||||
|
||||
/**
|
||||
* Method used to reconnect to the database
|
||||
* using the data provided via the DB class' constructor.
|
||||
*
|
||||
* @return bool Returns true if the reconnect was successful, false otherwise.
|
||||
*/
|
||||
public function reconnect(): bool;
|
||||
|
||||
/**
|
||||
* Method to check whether or not we are connected
|
||||
* to the database.
|
||||
*
|
||||
* @return bool Returns true if a connection exists, false otherwise.
|
||||
*/
|
||||
public function is_connected(): bool;
|
||||
|
||||
/**
|
||||
* Prepares and executes a MySQL query and returns the result.
|
||||
* -> SELECT statements will return a mysqli_result
|
||||
* -> INSERT, UPDATE, DELETE, REPLACE statements will return an integer
|
||||
* (last insert ID for INSERTs, number of affected rows for everything else)
|
||||
*
|
||||
* @example $dbConnection->query("SELECT id FROM ".TB_PREFIX."users WHERE email = ? AND activated = ?", "my@mail.com", 1);
|
||||
* @example $dbConnection->query("UPDATE ".TB_PREFIX."users SET name = ? WHERE id = ?", "John Doe", 1);
|
||||
* @example $dbConnection->query("INSERT INTO ".TB_PREFIX."users (name, email) VALUES (?, ?)", "John Doe", "john@doe.com");
|
||||
* @example $dbConnection->query("REPLACE INTO ".TB_PREFIX."users (name, email) VALUES (?, ?)", "John Doe", "john@doe.com");
|
||||
* @example $dbConnection->query("DELETE FROM ".TB_PREFIX."users WHERE id IN(?, ?, ?)", 1, 2 3);
|
||||
*
|
||||
* @param string $statement The query to prepare and execute.
|
||||
* @param mixed ...$params Parameters for the query. These usually come from user via POST or GET requests.
|
||||
* @return mixed Returns either a mysqli_result or a number. If number is returned, it will be last insert ID
|
||||
* for INSERTs or number of affected rows for anything else.
|
||||
*/
|
||||
public function query_new(string $statement, ...$params);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
#################################################################################
|
||||
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
|
||||
## --------------------------------------------------------------------------- ##
|
||||
## Project: TravianZ ##
|
||||
## Filename User.php ##
|
||||
## Developed by: martinambrus ##
|
||||
## License: TravianZ Project ##
|
||||
## Copyright: TravianZ (c) 2010-2017. All rights reserved. ##
|
||||
## URLs: https://travian.martinambrus.com ##
|
||||
## Source code: https://github.com/Shadowss/TravianZ ##
|
||||
## ##
|
||||
#################################################################################
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
$src_prefix = '';
|
||||
|
||||
if (substr(getcwd(), -5) === 'Admin') {
|
||||
$src_prefix = '../';
|
||||
}
|
||||
|
||||
if (substr(getcwd(), -4) === 'Mods') {
|
||||
$src_prefix = '../../../';
|
||||
}
|
||||
|
||||
include_once($src_prefix."src/Database/IDbConnection.php");
|
||||
include_once($src_prefix."src/Utils/Math.php");
|
||||
|
||||
use App\Database\IDbConnection;
|
||||
use App\Utils\Math;
|
||||
|
||||
/**
|
||||
* Defines the properties of a user, e.g. player entity
|
||||
* connected to their profile and other personal and account-specific
|
||||
* information.
|
||||
*
|
||||
* @author martinambrus
|
||||
*/
|
||||
class User {
|
||||
|
||||
/**
|
||||
* @var int Database ID of the user.
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var string A unique username for this user.
|
||||
*/
|
||||
private $username;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var IDbConnection Database connection to perform queries on.
|
||||
*/
|
||||
private $db;
|
||||
|
||||
/**
|
||||
* Constructor for the User class.
|
||||
* Depending on the parameter input, a User class with
|
||||
* database ID or username will be instantiated.
|
||||
*
|
||||
* @example $user = new User(1);
|
||||
* @example $user = new User("martinambrus");
|
||||
*
|
||||
* @param int|string $identifier ID or username for this user.
|
||||
* @param IDbConnection $database Instance of the database class to use to perform queries.
|
||||
*
|
||||
* @return void This method doesn't have a return value.
|
||||
*/
|
||||
public function __construct($identifier, IDbConnection $database) {
|
||||
// check if we passed an ID or a username
|
||||
if (Math::isInt($identifier)) {
|
||||
$this->id = $identifier;
|
||||
} else {
|
||||
$this->username = $identifier;
|
||||
}
|
||||
|
||||
$this->db = $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether username or e-mail already exists in the database.
|
||||
*
|
||||
* @param IDbConnection $db The current database connection.
|
||||
* @param string $value Value to check names and emails for.
|
||||
* @return boolean Returns true if the value exists in database,
|
||||
* false otherwise.
|
||||
*/
|
||||
public static function exists(IDbConnection $db, string $value) {
|
||||
$sql = '(
|
||||
SELECT
|
||||
Count(*) AS in_users
|
||||
FROM
|
||||
'.TB_PREFIX.'users
|
||||
WHERE
|
||||
username = ? OR email = ?
|
||||
)
|
||||
UNION ALL
|
||||
(
|
||||
SELECT
|
||||
Count(*) AS in_act
|
||||
FROM
|
||||
'.TB_PREFIX.'activate
|
||||
WHERE
|
||||
username = ? OR email = ?
|
||||
)';
|
||||
|
||||
$res = $db->query_new($sql, $value, $value, $value, $value);
|
||||
|
||||
// convert result into an array
|
||||
$res = mysqli_fetch_array($res, MYSQLI_NUM);
|
||||
|
||||
return ($res[0] > 0 || $res[1] > 0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
#################################################################################
|
||||
## -= YOU MAY NOT REMOVE OR CHANGE THIS NOTICE =- ##
|
||||
## --------------------------------------------------------------------------- ##
|
||||
## Project: TravianZ ##
|
||||
## Filename User.php ##
|
||||
## Developed by: martinambrus ##
|
||||
## License: TravianZ Project ##
|
||||
## Copyright: TravianZ (c) 2010-2017. All rights reserved. ##
|
||||
## URLs: https://travian.martinambrus.com ##
|
||||
## Source code: https://github.com/Shadowss/TravianZ ##
|
||||
## ##
|
||||
#################################################################################
|
||||
|
||||
namespace App\Utils;
|
||||
|
||||
/**
|
||||
*
|
||||
* Mathematics-related helpers.
|
||||
*
|
||||
* @author martinambrus
|
||||
*
|
||||
*/
|
||||
class Math {
|
||||
|
||||
public static function isInt($val) {
|
||||
return (is_numeric($val) && intval($val) === $val);
|
||||
}
|
||||
|
||||
public static function isFloat($val) {
|
||||
return (is_numeric($val) && floatval($val) === $val);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user