includes/dao.php
// Data Access Objects
class DaoManager
{
private $daos = array(
'UserDao' => 'SqlUserDao'
);
private $inst = array();
public function getDao($iface)
{
if(!isset($this->inst[$iface])) {
$this->inst[$iface] = new $this->daos[$iface]();
}
return $this->inst[$iface];
}
}
// User Data Access Object START
interface UserDao
{
public function getById($id);
public function getByLogin($name, $pass);
public function checkUser(User &$user);
public function createUser(User &$user);
public function updateUser(User &$user);
public function deleteUser(User &$user);
}
class SqlUserDao implements UserDao
{
private $mysqli;
public function __construct()
{
$this->mysqli = new mysqli('mysql*', 'username', 'password', 'username0*', 3306, '/tmp/mysql_*.sock');
if(mysqli_connect_errno()) {
die(sprintf('#%d %s', mysqli_connect_errno(), mysqli_connect_error()));
}
}
public function __destruct()
{
$this->mysqli->close();
}
public function getById($id)
{
$sql = "SELECT user_id, user_name, user_pass, user_mail
FROM users
WHERE user_id = %d";
$sql = sprintf($sql,
intval($id)
);
$result = $this->mysqli->query($sql)
or sql_die($sql, $this->mysqli->errno, $this->mysqli->error, __LINE__, __FILE__);
return $this->extractResult($result);
}
public function getByLogin($name, $pass)
{
$sql = "SELECT user_id, user_name, user_pass, user_mail
FROM users
WHERE user_name = '%s'
AND user_pass = '%s'";
$sql = sprintf($sql,
$this->mysqli->real_escape_string($name),
$this->mysqli->real_escape_string($pass)
);
$result = $this->mysqli->query($sql)
or sql_die($sql, $this->mysqli->errno, $this->mysqli->error, __LINE__, __FILE__);
return $this->extractResult($result);
}
public function checkUser(User &$user)
{
$ec = 0;
if($user->getId() != 0) {
$ec = 1;
}
$sqlName = "SELECT user_id
FROM users
WHERE user_name = '%s'";
$sqlName = sprintf($sqlName,
$this->mysqli->real_escape_string($user->getName())
);
$resultName = $this->mysqli->query($sqlName)
or sql_die($sql1, $this->mysqli->errno, $this->mysqli->error, __LINE__, __FILE__);
if($resultName->num_rows != 0) {
$ec += 2;
}
$sqlMail = "SELECT user_id
FROM users
WHERE user_mail = '%s'";
$sqlMail = sprintf($sqlMail,
$this->mysqli->real_escape_string($user->getMail())
);
$resultMail = $this->mysqli->query($sqlMail)
or sql_die($sqlMail, $this->mysqli->errno, $this->mysqli->error, __LINE__, __FILE__);
if($resultMail->num_rows != 0) {
$ec += 4;
}
return $ec;
}
public function createUser(User &$user)
{
$sql = "INSERT INTO users
SET user_name = '%s', user_pass = '%s', user_mail = '%s'";
$sql = sprintf($sql,
$this->mysqli->real_escape_string($user->getName()),
$this->mysqli->real_escape_string($user->getPass()),
$this->mysqli->real_escape_string($user->getMail())
);
$this->mysqli->query($sql)
or sql_die($sql, $this->mysqli_errno, $this->mysqli->error, __LINE__, __FILE__);
$user->setId($this->mysqli->insert_id);
}
public function updateUser(User &$user)
{
$sql = "UPDATE users
SET user_name = '%s', user_pass = '%s', user_mail = '%s'
WHERE user_id = %d";
$sql = sprintf($sql,
$this->mysqli->real_escape_string($user->getName()),
$this->mysqli->real_escape_string($user->getPass()),
$this->mysqli->real_escape_string($user->getMail()),
$user->getId()
);
$this->mysqli->query($sql)
or sql_die($sql, $this->mysqli->errno, $this->mysqli->error, __LINE__, __FILE__);
}
public function deleteUser(User &$user)
{
$sql = "DELETE FROM users
WHERE user_id = %d";
$sql = sprintf($sql,
$user->getId()
);
$this->mysqli->query($sql)
or sql_die($sql, $this->mysqli->errno, $this->mysqli->error, __LINE__, __FILE__);
$user->destroy();
}
private function extractResult($result)
{
if($result->num_rows != 1) {
return false;
}
$user = new User();
$row = $result->fetch_assoc();
$user->setId($row['user_id']);
$user->setName($row['user_name']);
$user->setPass($row['user_pass']);
$user->setMail($row['user_mail']);
return $user;
}
}
class User
{
private $id = 0;
private $name = '';
private $pass = '';
private $mail = '';
public function __call($method, $parameters)
{
$what = substr($method, 0, 3);
$var = strtolower(substr($method, 3));
if($what == 'set') {
$this->{$var} = $parameters[0];
} elseif($what == 'get') {
return $this->{$var};
}
}
public function setId($id)
{
if($this->id == 0)
{
$this->id = intval($id);
}
}
public function destroy()
{
$this->id = 0;
$this->name = '';
$this->pass = '';
$this->mail = '';
}
}
// User Data Access Object END
?>
includes/functions.php
// SQL die() Message
function sql_die($sql, $errno, $error, $line, $file)
{
$message = sprintf("%s\n#%d %s\n#%d in %s", $sql, $errno, $error, $line, $file);
$message = nl2br($message);
die($message);
}
?>
use.php
include 'includes/functions.php';
include 'includes/dao.php';
$daoManager = new DaoManager(); // intialisiert den DaoManager
$userDao = $daoManager->getDao('UserDao'); // ein UserDao wird erstellt.
$user = $userDao->getById(1); // oder $user = $userDao->getByLogin('username', 'password'); womit der Login Fall auch gelöst ist. Bei einem nich existiernden User wird False zurückgegeben.
echo $user->getName(); // gibt den Benutzernamen aus.
$user->setPass(md5($user->getPass())); // mache aus einem Plaintext (in diesem Beispiel) ein MD5 verschlüsseltes Passwort.
$userDao->updateUser($user); // dies nicht vergessen, da sonst die Änderungen über die get/set Klasse $user nicht übernommen werden.
$userFoo = new User(); // Ein neuer Benutzer wird erstellt. Id darf nicht gesetzt werden, da sonst die $userFoo Instantz ungültig ist.
$userFoo->setName('Foo'); // Name setzen.
$userFoo->setPass(md5('foo')); // Passwort setzen.
$userFoo->setMail('[email protected]'); // E-Mail Adresse setzen.
if($userDao->checkUser($userFoo) != 0) { // User überprüfen. Werte können adiert zürückgegeb werden.
die('Ungültig'); // 0: Alles OK, 1: Id gesetzt, 2: Name existiert, 3: 1+2, 4: E-Mail-Adresse exisitiert, 5: 1+4, 6: 2+4, 7: 1+2+4;
}
$userDao->createUser($userFoo); // Benutzer Foo erstellen. Hier wird auch die Id erzeugt.
echo $userFoo->getId(); // Erzeugte Id ausgeben.
$userDao->deleteUser($userFoo); // Foo wieder löschen.
?>