<?php
session_start();
class basic_encryption {
private $key = "deinKeyAKAmd5hash";
private $iv = "deinIvAKAmd5hash";
public function encrypt($plain){
$crypted = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $this->key, $plain, MCRYPT_MODE_CBC, $this->iv);
$crypted = base64_encode($crypted);
$crypted = preg_replace('/[+]/', '-',$crypted);
return $crypted;
}
public function decrypt($crypted){
$crypted = preg_replace('/[-]/', '+',$crypted);
$crypted = base64_decode($crypted);
$plain = mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $this->key, $crypted, MCRYPT_MODE_CBC, $this->iv);
return $plain;
}
}
class sanitize{
private $regex;
// set regex to cleanup invalid characters
public function clPM($string) {
$this->regex = "/[^\p{L}\d_!,;:.\s\/\-\"\'@®+&§]/ui";
return $this->{'add_selection'}($string);
}
private function add_selection($string){
$std = preg_replace($this->regex, "", $string);
$clean = preg_replace('/[\x80-\xFF]/', '?', $std);
$clean = str_replace(chr(0), '', $std);
return $clean;
}
}
if( empty($_POST['password']) || empty($_POST['mail']) ){
header( 'Location: /index.php?login=failed&reason=empty.values' );
exit();
}
// sessiontoken auf der loginseite erzeugen
if( !isset($_SESSION['token']) ){
header( 'Location: /index.php?denied=true' );
exit();
}
else{
unset($_SESSION['token']);
$clean = new sanitize;
$passwd = $clean->{'clPM'}($_POST['password']);
$mail = $clean->{'clPM'}($_POST['mail']);
$aes = new basic_encryption;
$user = 'unprivilegeduser';
$keeper = trim($aes->{'decrypt'}('VerschlüsseltesDBpasswd'));
$dbh = new PDO("mysql:host=127.0.0.1;dbname=DeineDB", $user, $passwd);
$sth = $dbh->prepare("SELECT COUNT(*) FROM users WHERE Email = ? AND Password = ? ");
$sth->execute(array($mail,$passwd));
$r = $sth->fetch();
$r = $r[0];
if( $r > 0 ){
$sth = $dbh->prepare("SELECT id,username FROM users WHERE Email = ? AND Password = ? ");
$sth->execute(array($mail,$passwd));
$r = $sth->fetch();
$_SESSION['UserDetails'] = array('UserID' => $r['0'],'Username' => $r[1]);
$_SESSION['Admin'] = $r[1] == 'Admin' ? true : false;
$dbh = NULL;
header( "Location: /index.php?session=started" );
exit();
}
else{
$dbh = NULL;
die(header( "Location: /index.php?login=failed&reason=some.reason" ));
}
}
?>