|
You last visited: Today at 10:36
Advertisement
PHP Datenbankklasse mit Escapefunktion
Discussion on PHP Datenbankklasse mit Escapefunktion within the Flyff PServer Guides & Releases forum part of the Flyff Private Server category.
08/22/2015, 13:55
|
#1
|
elite*gold: 0
Join Date: May 2012
Posts: 871
Received Thanks: 642
|
PHP Datenbankklasse mit Escapefunktion
Datenbankklasse:
PHP Code:
<?php
class Database { private $sql; private $database; function __construct($customString = null, $mssqlCredentialsArray = false) { if($mssqlCredentialsArray){ $this->sql = odbc_connect('Driver={SQL Server};Server=' . $mssqlCredentialsArray['Host'] . '\SQLEXPRESS;Port=1433', $mssqlCredentialsArray['User'], $mssqlCredentialsArray['Pass']); }else{ $this->sql = $customString; } $this->database = array( 'Account' => 'ACCOUNT_DBF', 'Character' => 'CHARACTER_01_DBF', 'Item' => 'ITEM_DBF', 'Ranking' => 'RANKING_DBF', 'Log' => 'LOGGING_01_DBF', 'Website' => 'WEBSITE_DBF' ); } private function InputCheck($mixedInput){ foreach($mixedInput as $key => $value){ if(gettype($value) != 'integer'){ $mixedInput[$key] = htmlspecialchars($value); $mixedInput[$key] = htmlentities($value); $mixedInput[$key] = strip_tags($value); $mixedInput[$key] = '0x'.bin2hex($value); }else { $mixedInput[$key] = $value; } } return $mixedInput; } private function QueryConstruction($escapedInput, $queryType){ $queryRows = null; $whereRows = false; foreach($escapedInput as $key => $value){ $queryRows .= '[' . $key . '], '; } switch($queryType){ case 'rows': if(gettype(key($escapedInput)) == 'integer'){ foreach($escapedInput as $key => $value){ $queryRows .= '[' . $value . '], '; } }else{ foreach($escapedInput as $key => $value){ $queryRows .= '[' . $key . '], '; } } $subQueryRows = substr($queryRows, 0, strlen($queryRows) - 2); break; case 'dual': foreach($escapedInput as $key => $value){ if(gettype($value) == 'integer'){ $queryRows .= '[' . $key . '] = ' . $value . ' AND '; }else { $queryRows .= '[' . $key . '] = \'' . $value . '\' AND '; } } $subQueryRows = substr($queryRows, 0, strlen($queryRows) - 5); $subQueryRows = ' WHERE ' . $subQueryRows; case 'values': foreach($escapedInput as $key => $value){ if(gettype($value) == 'integer'){ $queryRows .= $value . ', '; }else { $queryRows .= '\'' . $value . '\', '; } } $subQueryRows = substr($queryRows, 0, strlen($queryRows) - 2); break; } return $subQueryRows; } private function Exec($preparedQuery){ return @odbc_exec($preparedQuery); } private function Fetch($result){ return @odbc_fetch_array($result, 0); } private function Rows($result){ return @odbc_num_rows($result); }
/************************************************************************************************************************************************************************** Query Functions *************************************************************************************************************************************************************************/ protected function Update($mixedInput, $usedDatabase, $table){ $queryCredentials = self::QueryConstruction(self::InputCheck($mixedInput, 'dual')); $exec = 'UPDATE [' . $this->database[$usedDatabase] . '].[dbo].[' . $table . '] SET '; } protected function Insert($mixedInput, $usedDatabase, $table){ $queryCredentials = self::QueryConstruction(self::InputCheck($mixedInput, 'rows')); $valueCredentials = self::QueryConstruction(self::InputCheck($mixedInput, 'values')); $exec = 'INSERT INTO [' . $this->database[$usedDatabase] . '].[dbo].[' . $table . '] (' . $queryCredentials . ') VALUES (' . $valueCredentials . ')'; } protected function Select($mixedInput, $usedDatabase, $table, $mixedWhere = false){ $queryCredentials = self::QueryConstruction(self::InputCheck($mixedInput, 'rows')); if($mixedWhere){ $whereCredentials = self::QueryConstruction(self::InputCheck($mixedWhere, 'dual')); }else { $whereCredentials = null; } $craft = 'SELECT ' . $queryCredentials . ' FROM [' . $this->database[$usedDatabase] . '].[dbo].[' . $table . ']' . $whereCredentials . ''; $exec = self::Exec($craft); $fetch = self::Fetch($exec); $rows = self::Rows($exec); return array('Result' => $fetch, 'Rows' => $rows); } }
Features:
- automatisierte Query Construction
- automatisierte Escape Funktion
- dynamische Verwaltung
- sehr simple Anwendung
Anwendung:
Allgemein:
PHP Code:
require_once('class.Database.php');
PHP Code:
Man übergibt ein Array ähnlich wie bei Timmy welches auf dem Standart Port 1433 läuft und nur über SQLEXPRESS
new Database($config['mssqlCredentials']);
oder für fortgeschrittene Entwickler auch einen ganzen Connectionstring:
new Database(odbc_connect('Driver={SQL Server};Server=' . HOSTNAME;Port=PORT', 'USERNAME', 'PASSWORT', ...));
PHP Code:
class YourClass extends Database {}
________________________________________________
Update:
PHP Code:
//Ihr könnt einfach bei einem Formular das $_POST Array übergeben.
self::Update($_POST, 'Account', 'account_tbl);
//Update($mixedInput, $usedDatabase, $table)
Insert:
PHP Code:
//Ihr könnt einfach bei einem Formular das $_POST Array übergeben.
self::Insert($_POST, 'Account', 'account_tbl);
//Insert($mixedInput, $usedDatabase, $table)
Select:
PHP Code:
$search = array('account', 'password', 'dcoins');
self::Select($search, 'Account', 'account_tbl') !!! Wenn ihr KEINEN Vergleichswert benötigt lasst $mixedWhere einfach weg, ansonsten: !!!
$where = array('id' => 1,'character' => 'AMAZEN');
self::Select($search, 'Account', 'account_tbl', $_POST); oder self::Select($search, 'Account', 'account_tbl'); //Select($mixedInput, $usedDatabase, $table, $mixedWhere = false)
Credits:
- Datenbankklasse: AMAZEN
|
|
|
08/22/2015, 14:41
|
#2
|
elite*gold: 18
Join Date: Sep 2009
Posts: 20,174
Received Thanks: 14,475
|
Die Validierungen sind ok, aber ich würde die Werte alle, ausgenommen integer, als Hex an die Datenbank weiterleiten.
In PHP folgend:
Code:
<?php
'0x'.bin2hex( $input );
?>
Dann interpritiert der SQL Server nichts mehr als Befehl und du kannst sogar den Shutdown Befehl abschicken, was zwar sinnfrei wäre aber möglich.
Absolutes Escapen halt in einem Befehl
|
|
|
08/22/2015, 14:50
|
#3
|
elite*gold: 0
Join Date: May 2012
Posts: 871
Received Thanks: 642
|
Muss jetzt leider los. Werde es mir bei Gelegenheit mal ansehen, danke
Das:
PHP Code:
private function InputCheck($mixedInput){ foreach($mixedInput as $key => $value){ $mixedInput[$key] = self::Secure($value); } return $mixedInput; }
wurde zu dem:
PHP Code:
private function InputCheck($mixedInput){ foreach($mixedInput as $key => $value){ if(gettype($value) != 'integer'){ $mixedInput[$key] = htmlspecialchars($value); $mixedInput[$key] = htmlentities($value); $mixedInput[$key] = strip_tags($value); $mixedInput[$key] = '0x'.bin2hex($value); }else { $mixedInput[$key] = $value; } } return $mixedInput; }
Außerdem wurden die Escape Funktionen entfernt da diese nun nicht mehr benötigt werden.
|
|
|
All times are GMT +1. The time now is 10:37.
|
|