Dump from SVN
This commit is contained in:
315
config/lib/PasswordStorage.php
Normal file
315
config/lib/PasswordStorage.php
Normal file
@ -0,0 +1,315 @@
|
||||
<?php
|
||||
class InvalidHashException extends Exception {}
|
||||
class CannotPerformOperationException extends Exception {}
|
||||
class PasswordStorage
|
||||
{
|
||||
// These constants may be changed without breaking existing hashes.
|
||||
const PBKDF2_HASH_ALGORITHM = "sha1";
|
||||
const PBKDF2_ITERATIONS = 64000;
|
||||
const PBKDF2_SALT_BYTES = 24;
|
||||
const PBKDF2_OUTPUT_BYTES = 18;
|
||||
// These constants define the encoding and may not be changed.
|
||||
const HASH_SECTIONS = 5;
|
||||
const HASH_ALGORITHM_INDEX = 0;
|
||||
const HASH_ITERATION_INDEX = 1;
|
||||
const HASH_SIZE_INDEX = 2;
|
||||
const HASH_SALT_INDEX = 3;
|
||||
const HASH_PBKDF2_INDEX = 4;
|
||||
/**
|
||||
* Hash a password with PBKDF2
|
||||
*
|
||||
* @param string $password
|
||||
* @return string
|
||||
*/
|
||||
public static function create_hash($password)
|
||||
{
|
||||
// format: algorithm:iterations:outputSize:salt:pbkdf2output
|
||||
if (!\is_string($password)) {
|
||||
throw new InvalidArgumentException(
|
||||
"create_hash(): Expected a string"
|
||||
);
|
||||
}
|
||||
if (\function_exists('random_bytes')) {
|
||||
try {
|
||||
$salt_raw = \random_bytes(self::PBKDF2_SALT_BYTES);
|
||||
} catch (Error $e) {
|
||||
$salt_raw = false;
|
||||
} catch (Exception $e) {
|
||||
$salt_raw = false;
|
||||
} catch (TypeError $e) {
|
||||
$salt_raw = false;
|
||||
}
|
||||
} else {
|
||||
$salt_raw = \mcrypt_create_iv(self::PBKDF2_SALT_BYTES, MCRYPT_DEV_URANDOM);
|
||||
}
|
||||
if ($salt_raw === false) {
|
||||
throw new CannotPerformOperationException(
|
||||
"Random number generator failed. Not safe to proceed."
|
||||
);
|
||||
}
|
||||
$PBKDF2_Output = self::pbkdf2(
|
||||
self::PBKDF2_HASH_ALGORITHM,
|
||||
$password,
|
||||
$salt_raw,
|
||||
self::PBKDF2_ITERATIONS,
|
||||
self::PBKDF2_OUTPUT_BYTES,
|
||||
true
|
||||
);
|
||||
return self::PBKDF2_HASH_ALGORITHM .
|
||||
":" .
|
||||
self::PBKDF2_ITERATIONS .
|
||||
":" .
|
||||
self::PBKDF2_OUTPUT_BYTES .
|
||||
":" .
|
||||
\base64_encode($salt_raw) .
|
||||
":" .
|
||||
\base64_encode($PBKDF2_Output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify that a password matches the stored hash
|
||||
*
|
||||
* @param string $password
|
||||
* @param string $hash
|
||||
* @return bool
|
||||
*/
|
||||
public static function verify_password($password, $hash)
|
||||
{
|
||||
if (!\is_string($password) || !\is_string($hash)) {
|
||||
throw new InvalidArgumentException(
|
||||
"verify_password(): Expected two strings"
|
||||
);
|
||||
}
|
||||
$params = \explode(":", $hash);
|
||||
if (\count($params) !== self::HASH_SECTIONS) {
|
||||
throw new InvalidHashException(
|
||||
"Fields are missing from the password hash."
|
||||
);
|
||||
}
|
||||
$pbkdf2 = \base64_decode($params[self::HASH_PBKDF2_INDEX], true);
|
||||
if ($pbkdf2 === false) {
|
||||
throw new InvalidHashException(
|
||||
"Base64 decoding of pbkdf2 output failed."
|
||||
);
|
||||
}
|
||||
$salt_raw = \base64_decode($params[self::HASH_SALT_INDEX], true);
|
||||
if ($salt_raw === false) {
|
||||
throw new InvalidHashException(
|
||||
"Base64 decoding of salt failed."
|
||||
);
|
||||
}
|
||||
$storedOutputSize = (int) $params[self::HASH_SIZE_INDEX];
|
||||
if (self::ourStrlen($pbkdf2) !== $storedOutputSize) {
|
||||
throw new InvalidHashException(
|
||||
"PBKDF2 output length doesn't match stored output length."
|
||||
);
|
||||
}
|
||||
$iterations = (int) $params[self::HASH_ITERATION_INDEX];
|
||||
if ($iterations < 1) {
|
||||
throw new InvalidHashException(
|
||||
"Invalid number of iterations. Must be >= 1."
|
||||
);
|
||||
}
|
||||
|
||||
return self::slow_equals(
|
||||
$pbkdf2,
|
||||
self::pbkdf2(
|
||||
$params[self::HASH_ALGORITHM_INDEX],
|
||||
$password,
|
||||
$salt_raw,
|
||||
$iterations,
|
||||
self::ourStrlen($pbkdf2),
|
||||
true
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two strings $a and $b in length-constant time.
|
||||
*
|
||||
* @param string $a
|
||||
* @param string $b
|
||||
* @return bool
|
||||
*/
|
||||
public static function slow_equals($a, $b)
|
||||
{
|
||||
if (!\is_string($a) || !\is_string($b)) {
|
||||
throw new InvalidArgumentException(
|
||||
"slow_equals(): expected two strings"
|
||||
);
|
||||
}
|
||||
if (\function_exists('hash_equals')) {
|
||||
return \hash_equals($a, $b);
|
||||
}
|
||||
|
||||
// PHP < 5.6 polyfill:
|
||||
$diff = self::ourStrlen($a) ^ self::ourStrlen($b);
|
||||
for($i = 0; $i < self::ourStrlen($a) && $i < self::ourStrlen($b); $i++) {
|
||||
$diff |= \ord($a[$i]) ^ \ord($b[$i]);
|
||||
}
|
||||
return $diff === 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* PBKDF2 key derivation function as defined by RSA's PKCS #5: https://www.ietf.org/rfc/rfc2898.txt
|
||||
* $algorithm - The hash algorithm to use. Recommended: SHA256
|
||||
* $password - The password.
|
||||
* $salt - A salt that is unique to the password.
|
||||
* $count - Iteration count. Higher is better, but slower. Recommended: At least 1000.
|
||||
* $key_length - The length of the derived key in bytes.
|
||||
* $raw_output - If true, the key is returned in raw binary format. Hex encoded otherwise.
|
||||
* Returns: A $key_length-byte key derived from the password and salt.
|
||||
*
|
||||
* Test vectors can be found here: https://www.ietf.org/rfc/rfc6070.txt
|
||||
*
|
||||
* This implementation of PBKDF2 was originally created by https://defuse.ca
|
||||
* With improvements by http://www.variations-of-shadow.com
|
||||
*/
|
||||
public static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
|
||||
{
|
||||
// Type checks:
|
||||
if (!\is_string($algorithm)) {
|
||||
throw new InvalidArgumentException(
|
||||
"pbkdf2(): algorithm must be a string"
|
||||
);
|
||||
}
|
||||
if (!\is_string($password)) {
|
||||
throw new InvalidArgumentException(
|
||||
"pbkdf2(): password must be a string"
|
||||
);
|
||||
}
|
||||
if (!\is_string($salt)) {
|
||||
throw new InvalidArgumentException(
|
||||
"pbkdf2(): salt must be a string"
|
||||
);
|
||||
}
|
||||
// Coerce strings to integers with no information loss or overflow
|
||||
$count += 0;
|
||||
$key_length += 0;
|
||||
$algorithm = \strtolower($algorithm);
|
||||
if (!\in_array($algorithm, \hash_algos(), true)) {
|
||||
throw new CannotPerformOperationException(
|
||||
"Invalid or unsupported hash algorithm."
|
||||
);
|
||||
}
|
||||
// Whitelist, or we could end up with people using CRC32.
|
||||
$ok_algorithms = array(
|
||||
"sha1", "sha224", "sha256", "sha384", "sha512",
|
||||
"ripemd160", "ripemd256", "ripemd320", "whirlpool"
|
||||
);
|
||||
if (!\in_array($algorithm, $ok_algorithms, true)) {
|
||||
throw new CannotPerformOperationException(
|
||||
"Algorithm is not a secure cryptographic hash function."
|
||||
);
|
||||
}
|
||||
if ($count <= 0 || $key_length <= 0) {
|
||||
throw new CannotPerformOperationException(
|
||||
"Invalid PBKDF2 parameters."
|
||||
);
|
||||
}
|
||||
|
||||
if (\function_exists("hash_pbkdf2")) {
|
||||
// The output length is in NIBBLES (4-bits) if $raw_output is false!
|
||||
if (!$raw_output) {
|
||||
$key_length = $key_length * 2;
|
||||
}
|
||||
return \hash_pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output);
|
||||
}
|
||||
|
||||
$hash_length = self::ourStrlen(\hash($algorithm, "", true));
|
||||
$block_count = \ceil($key_length / $hash_length);
|
||||
|
||||
$output = "";
|
||||
for($i = 1; $i <= $block_count; $i++) {
|
||||
// $i encoded as 4 bytes, big endian.
|
||||
$last = $salt . \pack("N", $i);
|
||||
// first iteration
|
||||
$last = $xorsum = \hash_hmac($algorithm, $last, $password, true);
|
||||
// perform the other $count - 1 iterations
|
||||
for ($j = 1; $j < $count; $j++) {
|
||||
$xorsum ^= ($last = \hash_hmac($algorithm, $last, $password, true));
|
||||
}
|
||||
$output .= $xorsum;
|
||||
}
|
||||
|
||||
if($raw_output) {
|
||||
return self::ourSubstr($output, 0, $key_length);
|
||||
} else {
|
||||
return \bin2hex(self::ourSubstr($output, 0, $key_length));
|
||||
}
|
||||
}
|
||||
/*
|
||||
* We need these strlen() and substr() functions because when
|
||||
* 'mbstring.func_overload' is set in php.ini, the standard strlen() and
|
||||
* substr() are replaced by mb_strlen() and mb_substr().
|
||||
*/
|
||||
/**
|
||||
* Calculate the length of a string
|
||||
*
|
||||
* @param string $str
|
||||
* @return int
|
||||
*/
|
||||
private static function ourStrlen($str)
|
||||
{
|
||||
static $exists = null;
|
||||
if ($exists === null) {
|
||||
$exists = \function_exists('mb_strlen');
|
||||
}
|
||||
|
||||
if (!\is_string($str)) {
|
||||
throw new InvalidArgumentException(
|
||||
"ourStrlen() expects a string"
|
||||
);
|
||||
}
|
||||
|
||||
if ($exists) {
|
||||
$length = \mb_strlen($str, '8bit');
|
||||
if ($length === false) {
|
||||
throw new CannotPerformOperationException();
|
||||
}
|
||||
return $length;
|
||||
} else {
|
||||
return \strlen($str);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Substring
|
||||
*
|
||||
* @param string $str
|
||||
* @param int $start
|
||||
* @param int $length
|
||||
* @return string
|
||||
*/
|
||||
private static function ourSubstr($str, $start, $length = null)
|
||||
{
|
||||
static $exists = null;
|
||||
if ($exists === null) {
|
||||
$exists = \function_exists('mb_substr');
|
||||
}
|
||||
// Type validation:
|
||||
if (!\is_string($str)) {
|
||||
throw new InvalidArgumentException(
|
||||
"ourSubstr() expects a string"
|
||||
);
|
||||
}
|
||||
|
||||
if ($exists) {
|
||||
// mb_substr($str, 0, NULL, '8bit') returns an empty string on PHP
|
||||
// 5.3, so we have to find the length ourselves.
|
||||
if (!isset($length)) {
|
||||
if ($start >= 0) {
|
||||
$length = self::ourStrlen($str) - $start;
|
||||
} else {
|
||||
$length = -$start;
|
||||
}
|
||||
}
|
||||
return \mb_substr($str, $start, $length, '8bit');
|
||||
}
|
||||
// Unlike mb_substr(), substr() doesn't accept NULL for length
|
||||
if (isset($length)) {
|
||||
return \substr($str, $start, $length);
|
||||
} else {
|
||||
return \substr($str, $start);
|
||||
}
|
||||
}
|
||||
}
|
274
config/lib/functions.php
Normal file
274
config/lib/functions.php
Normal file
@ -0,0 +1,274 @@
|
||||
<?php
|
||||
/**
|
||||
* functions.php
|
||||
* @version 2.4
|
||||
* @desc General issued php function library for me
|
||||
* @author Fándly Gergő Zoltán
|
||||
* @copy 2017 Fándly Gergő Zoltán
|
||||
*/
|
||||
|
||||
class functions{
|
||||
const STR_SAME=0;
|
||||
const STR_LOWERCASE=1;
|
||||
const STR_RACCENT=2;
|
||||
const STR_RACCLOW=3;
|
||||
const RAND_SMALL=0;
|
||||
const RAND_LARGE=1;
|
||||
const RAND_SPEC=2;
|
||||
const COOKIE_LIFETIME=3;
|
||||
|
||||
public static function setError($code){
|
||||
global $errcode;
|
||||
if(isset($errcode)){
|
||||
array_push($errcode, $code);
|
||||
}
|
||||
else{
|
||||
$errcode=array($code);
|
||||
}
|
||||
setcookie("errcode", serialize($errcode), time()+functions::COOKIE_LIFETIME);
|
||||
}
|
||||
|
||||
public static function isError(){
|
||||
global $errcode;
|
||||
if(isset($errcode) || isset($_COOKIE['errcode'])){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getErrorArray(){
|
||||
global $errcode;
|
||||
if(functions::isError()){
|
||||
if(isset($errcode)){
|
||||
return $errcode;
|
||||
}
|
||||
if(isset($_COOKIE['errcode'])){
|
||||
return unserialize($_COOKIE['errcode']);
|
||||
}
|
||||
}
|
||||
else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static function setMessage($code){
|
||||
global $msgcode;
|
||||
if(isset($msgcode)){
|
||||
array_push($msgcode, $code);
|
||||
}
|
||||
else{
|
||||
$msgcode=array($code);
|
||||
}
|
||||
setcookie("msgcode", serialize($msgcode), time()+functions::COOKIE_LIFETIME);
|
||||
}
|
||||
|
||||
public static function isMessage(){
|
||||
global $msgcode;
|
||||
if(isset($msgcode) || isset($_COOKIE['msgcode'])){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function getMessageArray(){
|
||||
global $msgcode;
|
||||
if(functions::isMessage()){
|
||||
if(isset($msgcode)){
|
||||
return $msgcode;
|
||||
}
|
||||
if(isset($_COOKIE['msgcode'])){
|
||||
return unserialize($_COOKIE['msgcode']);
|
||||
}
|
||||
}
|
||||
else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static function clearError(){
|
||||
global $errcode;
|
||||
if(isset($errcode)){
|
||||
unset($errcode);
|
||||
}
|
||||
setcookie("errcode", null, -1);
|
||||
}
|
||||
|
||||
public static function clearMessage(){
|
||||
global $msgcode;
|
||||
if(isset($msgcode)){
|
||||
unset($msgcode);
|
||||
}
|
||||
setcookie("msgcode", null, -1);
|
||||
}
|
||||
|
||||
public static function safeReload(){
|
||||
header("Location: ".explode("?", $_SERVER['REQUEST_URI'])[0]);
|
||||
}
|
||||
|
||||
public static function randomString($length, $char=functions::RAND_SMALL){
|
||||
if($char==0){
|
||||
$charset="0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
}
|
||||
else if($char==1){
|
||||
$charset="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
}
|
||||
else if($char==2){
|
||||
$charset="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_-=+\?/.>,<";
|
||||
}
|
||||
$charsetlength=strlen($charset);
|
||||
$string="";
|
||||
for($i=0; $i<$length; $i++){
|
||||
$string=$string . $charset[rand(0, $charsetlength-1)];
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
public static function get_string_between($string, $start, $end){
|
||||
$string=' ' . $string;
|
||||
$ini=strpos($string, $start);
|
||||
if($ini==0) return '';
|
||||
$ini+=strlen($start);
|
||||
$len=strpos($string, $end, $ini) - $ini;
|
||||
return substr($string, $ini, $len);
|
||||
}
|
||||
|
||||
public static function process_string($str, $dep){
|
||||
global $functions_accent_convert;
|
||||
switch($dep){
|
||||
case 0:
|
||||
{
|
||||
return $str;
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
return strtolower($str);
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
return strtr($str, $functions_accent_convert);
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
return strtolower(strtr($str, $functions_accent_convert));
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function validate_captcha($secretkey, $response){
|
||||
$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretkey."&response=".$response);
|
||||
$data=json_decode($verify);
|
||||
if($data->success){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
$functions_accent_convert=array(
|
||||
// Decompositions for Latin-1 Supplement
|
||||
chr(195).chr(128) => 'A', chr(195).chr(129) => 'A',
|
||||
chr(195).chr(130) => 'A', chr(195).chr(131) => 'A',
|
||||
chr(195).chr(132) => 'A', chr(195).chr(133) => 'A',
|
||||
chr(195).chr(135) => 'C', chr(195).chr(136) => 'E',
|
||||
chr(195).chr(137) => 'E', chr(195).chr(138) => 'E',
|
||||
chr(195).chr(139) => 'E', chr(195).chr(140) => 'I',
|
||||
chr(195).chr(141) => 'I', chr(195).chr(142) => 'I',
|
||||
chr(195).chr(143) => 'I', chr(195).chr(145) => 'N',
|
||||
chr(195).chr(146) => 'O', chr(195).chr(147) => 'O',
|
||||
chr(195).chr(148) => 'O', chr(195).chr(149) => 'O',
|
||||
chr(195).chr(150) => 'O', chr(195).chr(153) => 'U',
|
||||
chr(195).chr(154) => 'U', chr(195).chr(155) => 'U',
|
||||
chr(195).chr(156) => 'U', chr(195).chr(157) => 'Y',
|
||||
chr(195).chr(159) => 's', chr(195).chr(160) => 'a',
|
||||
chr(195).chr(161) => 'a', chr(195).chr(162) => 'a',
|
||||
chr(195).chr(163) => 'a', chr(195).chr(164) => 'a',
|
||||
chr(195).chr(165) => 'a', chr(195).chr(167) => 'c',
|
||||
chr(195).chr(168) => 'e', chr(195).chr(169) => 'e',
|
||||
chr(195).chr(170) => 'e', chr(195).chr(171) => 'e',
|
||||
chr(195).chr(172) => 'i', chr(195).chr(173) => 'i',
|
||||
chr(195).chr(174) => 'i', chr(195).chr(175) => 'i',
|
||||
chr(195).chr(177) => 'n', chr(195).chr(178) => 'o',
|
||||
chr(195).chr(179) => 'o', chr(195).chr(180) => 'o',
|
||||
chr(195).chr(181) => 'o', chr(195).chr(182) => 'o',
|
||||
chr(195).chr(182) => 'o', chr(195).chr(185) => 'u',
|
||||
chr(195).chr(186) => 'u', chr(195).chr(187) => 'u',
|
||||
chr(195).chr(188) => 'u', chr(195).chr(189) => 'y',
|
||||
chr(195).chr(191) => 'y',
|
||||
// Decompositions for Latin Extended-A
|
||||
chr(196).chr(128) => 'A', chr(196).chr(129) => 'a',
|
||||
chr(196).chr(130) => 'A', chr(196).chr(131) => 'a',
|
||||
chr(196).chr(132) => 'A', chr(196).chr(133) => 'a',
|
||||
chr(196).chr(134) => 'C', chr(196).chr(135) => 'c',
|
||||
chr(196).chr(136) => 'C', chr(196).chr(137) => 'c',
|
||||
chr(196).chr(138) => 'C', chr(196).chr(139) => 'c',
|
||||
chr(196).chr(140) => 'C', chr(196).chr(141) => 'c',
|
||||
chr(196).chr(142) => 'D', chr(196).chr(143) => 'd',
|
||||
chr(196).chr(144) => 'D', chr(196).chr(145) => 'd',
|
||||
chr(196).chr(146) => 'E', chr(196).chr(147) => 'e',
|
||||
chr(196).chr(148) => 'E', chr(196).chr(149) => 'e',
|
||||
chr(196).chr(150) => 'E', chr(196).chr(151) => 'e',
|
||||
chr(196).chr(152) => 'E', chr(196).chr(153) => 'e',
|
||||
chr(196).chr(154) => 'E', chr(196).chr(155) => 'e',
|
||||
chr(196).chr(156) => 'G', chr(196).chr(157) => 'g',
|
||||
chr(196).chr(158) => 'G', chr(196).chr(159) => 'g',
|
||||
chr(196).chr(160) => 'G', chr(196).chr(161) => 'g',
|
||||
chr(196).chr(162) => 'G', chr(196).chr(163) => 'g',
|
||||
chr(196).chr(164) => 'H', chr(196).chr(165) => 'h',
|
||||
chr(196).chr(166) => 'H', chr(196).chr(167) => 'h',
|
||||
chr(196).chr(168) => 'I', chr(196).chr(169) => 'i',
|
||||
chr(196).chr(170) => 'I', chr(196).chr(171) => 'i',
|
||||
chr(196).chr(172) => 'I', chr(196).chr(173) => 'i',
|
||||
chr(196).chr(174) => 'I', chr(196).chr(175) => 'i',
|
||||
chr(196).chr(176) => 'I', chr(196).chr(177) => 'i',
|
||||
chr(196).chr(178) => 'IJ',chr(196).chr(179) => 'ij',
|
||||
chr(196).chr(180) => 'J', chr(196).chr(181) => 'j',
|
||||
chr(196).chr(182) => 'K', chr(196).chr(183) => 'k',
|
||||
chr(196).chr(184) => 'k', chr(196).chr(185) => 'L',
|
||||
chr(196).chr(186) => 'l', chr(196).chr(187) => 'L',
|
||||
chr(196).chr(188) => 'l', chr(196).chr(189) => 'L',
|
||||
chr(196).chr(190) => 'l', chr(196).chr(191) => 'L',
|
||||
chr(197).chr(128) => 'l', chr(197).chr(129) => 'L',
|
||||
chr(197).chr(130) => 'l', chr(197).chr(131) => 'N',
|
||||
chr(197).chr(132) => 'n', chr(197).chr(133) => 'N',
|
||||
chr(197).chr(134) => 'n', chr(197).chr(135) => 'N',
|
||||
chr(197).chr(136) => 'n', chr(197).chr(137) => 'N',
|
||||
chr(197).chr(138) => 'n', chr(197).chr(139) => 'N',
|
||||
chr(197).chr(140) => 'O', chr(197).chr(141) => 'o',
|
||||
chr(197).chr(142) => 'O', chr(197).chr(143) => 'o',
|
||||
chr(197).chr(144) => 'O', chr(197).chr(145) => 'o',
|
||||
chr(197).chr(146) => 'OE',chr(197).chr(147) => 'oe',
|
||||
chr(197).chr(148) => 'R',chr(197).chr(149) => 'r',
|
||||
chr(197).chr(150) => 'R',chr(197).chr(151) => 'r',
|
||||
chr(197).chr(152) => 'R',chr(197).chr(153) => 'r',
|
||||
chr(197).chr(154) => 'S',chr(197).chr(155) => 's',
|
||||
chr(197).chr(156) => 'S',chr(197).chr(157) => 's',
|
||||
chr(197).chr(158) => 'S',chr(197).chr(159) => 's',
|
||||
chr(197).chr(160) => 'S', chr(197).chr(161) => 's',
|
||||
chr(197).chr(162) => 'T', chr(197).chr(163) => 't',
|
||||
chr(197).chr(164) => 'T', chr(197).chr(165) => 't',
|
||||
chr(197).chr(166) => 'T', chr(197).chr(167) => 't',
|
||||
chr(197).chr(168) => 'U', chr(197).chr(169) => 'u',
|
||||
chr(197).chr(170) => 'U', chr(197).chr(171) => 'u',
|
||||
chr(197).chr(172) => 'U', chr(197).chr(173) => 'u',
|
||||
chr(197).chr(174) => 'U', chr(197).chr(175) => 'u',
|
||||
chr(197).chr(176) => 'U', chr(197).chr(177) => 'u',
|
||||
chr(197).chr(178) => 'U', chr(197).chr(179) => 'u',
|
||||
chr(197).chr(180) => 'W', chr(197).chr(181) => 'w',
|
||||
chr(197).chr(182) => 'Y', chr(197).chr(183) => 'y',
|
||||
chr(197).chr(184) => 'Y', chr(197).chr(185) => 'Z',
|
||||
chr(197).chr(186) => 'z', chr(197).chr(187) => 'Z',
|
||||
chr(197).chr(188) => 'z', chr(197).chr(189) => 'Z',
|
||||
chr(197).chr(190) => 'z', chr(197).chr(191) => 's');
|
||||
|
||||
?>
|
82
config/lib/loginManager/lmConfig.php
Normal file
82
config/lib/loginManager/lmConfig.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* loginManager/lmConfig.php
|
||||
* @version 1.3
|
||||
* @desc config class
|
||||
* @author Fándly Gergő Zoltán
|
||||
* @copy 2017 Fándly Gergő Zoltán
|
||||
*/
|
||||
|
||||
class lmConfig{
|
||||
public function __construct($_pdo, $_session_lifetime, $_captcha_enable, $_captcha_after, $_captcha_sitekey, $_captcha_secretkey, $_ban_enable, $_ban_after, $_ban_time, $_look, $_remember_enable, $_remember_time, $_auth_type){
|
||||
$this->pdo=$_pdo;
|
||||
$this->session_lifetime=$_session_lifetime;
|
||||
$this->captcha_enable=$_captcha_enable;
|
||||
$this->captcha_after=$_captcha_after;
|
||||
$this->captcha_sitekey=$_captcha_sitekey;
|
||||
$this->captcha_secretkey=$_captcha_secretkey;
|
||||
$this->ban_enable=$_ban_enable;
|
||||
$this->ban_after=$_ban_after;
|
||||
$this->ban_time=$_ban_time;
|
||||
$this->look=$_look;
|
||||
$this->remember_enable=$_remember_enable;
|
||||
$this->remember_time=$_remember_time;
|
||||
$this->auth_type=$_auth_type;
|
||||
}
|
||||
|
||||
private $pdo;
|
||||
private $session_lifetime;
|
||||
private $captcha_enable;
|
||||
private $captcha_after;
|
||||
private $captcha_sitekey;
|
||||
private $captcha_secretkey;
|
||||
private $ban_enable;
|
||||
private $ban_after;
|
||||
private $ban_time;
|
||||
private $look;
|
||||
private $remember_enable; //NOT SAFE AT ALL!!!
|
||||
private $remember_time;
|
||||
private $auth_type;
|
||||
|
||||
public function getPDO(){
|
||||
return $this->pdo;
|
||||
}
|
||||
public function getSessionLifetime(){
|
||||
return $this->session_lifetime;
|
||||
}
|
||||
public function isCaptchaEnabled(){
|
||||
return $this->captcha_enable;
|
||||
}
|
||||
public function getCaptchaAfter(){
|
||||
return $this->captcha_after;
|
||||
}
|
||||
public function getCaptchaSitekey(){
|
||||
return $this->captcha_sitekey;
|
||||
}
|
||||
public function getCaptchaSecretkey(){
|
||||
return $this->captcha_secretkey;
|
||||
}
|
||||
public function isBanEnabled(){
|
||||
return $this->ban_enable;
|
||||
}
|
||||
public function getBanAfter(){
|
||||
return $this->ban_after;
|
||||
}
|
||||
public function getBanTime(){
|
||||
return $this->ban_time;
|
||||
}
|
||||
public function getLook(){
|
||||
return $this->look;
|
||||
}
|
||||
public function isRememberEnabled(){
|
||||
return $this->remember_enable;
|
||||
}
|
||||
public function getRememberTime(){
|
||||
return $this->remember_time;
|
||||
}
|
||||
public function getAuthType(){
|
||||
return $this->auth_type;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
14
config/lib/loginManager/lmHandler.php
Normal file
14
config/lib/loginManager/lmHandler.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* loginManager/lmHandler.php
|
||||
* @version 1.1
|
||||
* @desc Event handler for login manager
|
||||
* @author Fándly Gergő Zoltán
|
||||
* @copy 2017 Fándly Gergő Zoltán
|
||||
*/
|
||||
|
||||
interface lmHandler{
|
||||
public function handle($state, $target=0);
|
||||
}
|
||||
|
||||
?>
|
14
config/lib/loginManager/lmPassword.php
Normal file
14
config/lib/loginManager/lmPassword.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* loginManager/lmPassword.php
|
||||
* @version 1.0
|
||||
* @desc interface for function verifying password
|
||||
* @author Fándly Gergő Zoltán
|
||||
* @copy 2017 Fándly Gergő Zoltán
|
||||
*/
|
||||
|
||||
interface lmPassword{
|
||||
public function verifyPassword($cleartext, $database);
|
||||
}
|
||||
|
||||
?>
|
24
config/lib/loginManager/lmStates.php
Normal file
24
config/lib/loginManager/lmStates.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
/**
|
||||
* loginManager/lmStates.php
|
||||
* @version 1.2
|
||||
* @desc States of login manager
|
||||
* @author Fándly Gergő Zoltán
|
||||
* @copy 2017 Fándly Gergő Zoltán
|
||||
*/
|
||||
|
||||
class lmStates{
|
||||
const LOGIN_FAILED=0;
|
||||
const LOGIN_OK=1;
|
||||
const CAPTCHA_FAILED=2;
|
||||
const BANNED=3;
|
||||
const FORGET_DONE=4;
|
||||
const LOGOUT_DONE=5;
|
||||
|
||||
const AUTH_ID=10;
|
||||
const AUTH_UNAME=11;
|
||||
|
||||
const NOUSER=1;
|
||||
}
|
||||
|
||||
?>
|
14
config/lib/loginManager/lmTwoFactor.php
Normal file
14
config/lib/loginManager/lmTwoFactor.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
/**
|
||||
* loginManager/lmTwoFactor.php
|
||||
* @version 1.0
|
||||
* @desc second factor auth to LM
|
||||
* @author Fándly Gergő Zoltán 2017
|
||||
* @copy 2017 Fándly Gergő Zoltán
|
||||
*/
|
||||
|
||||
interface lmTwoFactor{
|
||||
public function secondFactor($uid);
|
||||
}
|
||||
|
||||
?>
|
44
config/lib/loginManager/lmUtils.php
Normal file
44
config/lib/loginManager/lmUtils.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* loginManager/lmUtils.php
|
||||
* @desc utilities for correct functioning
|
||||
* @version 1.0
|
||||
* @author Fándly Gergő Zoltán
|
||||
* @copy 2017 Fándly Gergő Zoltán
|
||||
*/
|
||||
|
||||
class lmUtils{
|
||||
/**
|
||||
* generate a random string with special character
|
||||
* @param int $length length of the requested string
|
||||
* @return string
|
||||
*/
|
||||
public static function randomString($length){
|
||||
$charset="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ~!@#$%^&*()_-=+\?/.>,<";
|
||||
$charsetLength=strlen($charset);
|
||||
$string="";
|
||||
for($i=0; $i<$length; $i++){
|
||||
$string.=$charset[rand(0, $charsetLength-1)];
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* validate google ReCaptcha
|
||||
* @param string $secretkey secret key to captcha API
|
||||
* @param string $response response of API
|
||||
* @return bool
|
||||
*/
|
||||
public static function validateCaptcha($secretkey, $response){
|
||||
$verify=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretkey."&response=".$response);
|
||||
$data=json_decode($verify);
|
||||
if($data->success){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
393
config/lib/loginManager/loginManager.php
Normal file
393
config/lib/loginManager/loginManager.php
Normal file
@ -0,0 +1,393 @@
|
||||
<?php
|
||||
/**
|
||||
* loginManager/loginManager.php
|
||||
* @version 1.1
|
||||
* @desc Easily manage authentication to your system
|
||||
* @author Fándly Gergő Zoltán
|
||||
* @copy 2017 Fándly Gergő Zoltán
|
||||
*/
|
||||
|
||||
/**
|
||||
* NEEDED Database structure:
|
||||
*
|
||||
<?sql
|
||||
CREATE TABLE `users`(
|
||||
`id` int(4) UNSIGNED NOT NULL auto_increment,
|
||||
`username` varchar(65) NOT NULL default '', /* optional
|
||||
`password` varchar(255) NOT NULL default '',
|
||||
PRIMARY KEY (`id`)
|
||||
) CHARACTER SET utf8 COLLATE utf8_general_ci;
|
||||
|
||||
CREATE TABLE `login_history`(
|
||||
`id` int(4) UNSIGNED NOT NULL auto_increment,
|
||||
`user` int(4) UNSIGNED NOT NULL default 1, /* id of nouser
|
||||
`date` timestamp NOT NULL default current_timestamp,
|
||||
`ip` varchar(45) NOT NULL default '0.0.0.0',
|
||||
`auth_token` varchar(65) NOT NULL default '',
|
||||
`user_agent` varchar(500) NOT NULL default '',
|
||||
`success` tinyint(1) NOT NULL default 0,
|
||||
PRIMARY KEY (`id`),
|
||||
FOREIGN KEY (`user`) REFERENCES users(`id`) ON DELETE CASCADE
|
||||
) CHARACTER SET utf8 COLLATE utf8_general_ci;
|
||||
|
||||
CREATE TABLE `login_remember`(
|
||||
`id` int(4) UNSIGNED NOT NULL auto_increment,
|
||||
`user` int(4) UNSIGNED NOT NULL default 0,
|
||||
`remember_token` varchar(65) NOT NULL default '',
|
||||
`until` timestamp NOT NULL default current_timestamp,
|
||||
PRIMARY KEY (`id`),
|
||||
FOREIGN KEY (`user`) REFERENCES users(`id`) ON DELETE CASCADE
|
||||
) CHARACTER SET utf8 COLLATE utf8_general_ci;
|
||||
|
||||
CREATE TABLE `login_bans`(
|
||||
`id` int(4) UNSIGNED NOT NULL auto_increment,
|
||||
`ip` varchar(45) NOT NULL default '0.0.0.0',
|
||||
`until` timestamp NOT NULL default current_timestamp,
|
||||
PRIMARY KEY (`id`)
|
||||
) CHARACTER SET utf8 COLLATE utf8_general_ci;
|
||||
|
||||
INSERT INTO users (`id`, `username`) VALUES (1, 'nouser');
|
||||
?>
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
* Includes
|
||||
*/
|
||||
require("lmStates.php");
|
||||
require("lmConfig.php");
|
||||
require("lmHandler.php");
|
||||
require("lmPassword.php");
|
||||
require("lmTwoFactor.php");
|
||||
require("lmUtils.php");
|
||||
|
||||
|
||||
/*
|
||||
* Class
|
||||
*/
|
||||
class loginManager{
|
||||
//constructor
|
||||
|
||||
/**
|
||||
* building...
|
||||
* @param lmConfig $_config configuration for login Manager
|
||||
* @param lmHandler $_eventHandler handler of events
|
||||
* @param lmPassword $_passwordEngine engine for verifying passwords
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($_config, $_eventHandler, $_passwordEngine, $_twoFactor){
|
||||
$this->config=$_config;
|
||||
$this->eventHandler=$_eventHandler;
|
||||
$this->passwordEngine=$_passwordEngine;
|
||||
$this->twoFactor=$_twoFactor;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//settings
|
||||
|
||||
private $config;
|
||||
private $eventHandler;
|
||||
private $passwordEngine;
|
||||
private $twoFactor;
|
||||
|
||||
|
||||
|
||||
//frontend functions
|
||||
|
||||
/**
|
||||
* initialize session and set its lifetime
|
||||
* @return bool
|
||||
*/
|
||||
public function init(){
|
||||
session_set_cookie_params($this->config->getSessionLifetime());
|
||||
return session_start();
|
||||
}
|
||||
|
||||
/**
|
||||
* prepare for login. Run this on the top of your login page!
|
||||
* @return void
|
||||
*/
|
||||
public function loginPrepare(){
|
||||
$this->passFailedAttempts();
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* lets start here!
|
||||
* @param int/string @identifier id or username of user
|
||||
* @param string @password cleartext password from input
|
||||
* @param bool $remember save user fot further use
|
||||
* @return void
|
||||
*/
|
||||
public function login($identifier, $password, $remember=false){
|
||||
global $lm_force_captcha;
|
||||
|
||||
if($this->passFailedAttempts()){ //not banned
|
||||
if(isset($lm_force_captcha)){ //check captcha
|
||||
if(!isset($_POST['g-recaptcha-response'])){
|
||||
$captcha_failed=true;
|
||||
$this->addLoginHistory(lmStates::NOUSER, lmStates::LOGIN_FAILED);
|
||||
$this->eventHandler->handle(lmStates::CAPTCHA_FAILED);
|
||||
return;
|
||||
}
|
||||
else{
|
||||
if(!lmUtils::validateCaptcha($this->config->getCaptchaSecretkey(), $_POST['g-recaptcha-response'])){
|
||||
$captcha_failed=true;
|
||||
$this->addLoginHistory(lmStates::NOUSER, lmStates::LOGIN_FAILED);
|
||||
$this->eventHandler->handle(lmStates::CAPTCHA_FAILED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!isset($captcha_failed)){
|
||||
if($this->config->isRememberEnabled()){ //check if remembering is enabled
|
||||
if($this->isRememberingUser() && $this->twoFactor->secondFactor($this->isRememberingUser())){ //remembering.
|
||||
$this->permitLogin($this->isRememberingUser()); //good to go!
|
||||
return;
|
||||
}
|
||||
}
|
||||
//proceed with normal login
|
||||
if($this->config->getAuthType()==lmStates::AUTH_UNAME){ //username based authentication
|
||||
$sql=$this->config->getPDO()->prepare("SELECT COUNT(id) AS count, id, password FROM users WHERE username=:identifier and id<>1");
|
||||
}
|
||||
else{
|
||||
$sql=$this->config->getPDO()->prepare("SELECT COUNT(id) AS count, id, password FROM users WHERE id=:identifier and id<>1");
|
||||
}
|
||||
$sql->execute(array(":identifier"=>$identifier));
|
||||
$res=$sql->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if($res['count']==0){ //user not existing
|
||||
$this->addLoginHistory(lmStates::NOUSER, lmStates::LOGIN_FAILED);
|
||||
$this->eventHandler->handle(lmStates::LOGIN_FAILED);
|
||||
return;
|
||||
}
|
||||
else{
|
||||
if($this->passwordEngine->verifyPassword($password, $res['password']) && $this->twoFactor->secondFactor($res['id'])){
|
||||
if($this->config->isRememberEnabled()){ //remember... if he wants to be insecure
|
||||
if($remember){
|
||||
$this->rememberUser($res['id']);
|
||||
}
|
||||
}
|
||||
$this->permitLogin($res['id']); //good to go!
|
||||
return;
|
||||
}
|
||||
else{
|
||||
$this->addLoginHistory($res['id'], lmStates::LOGIN_FAILED);
|
||||
$this->eventHandler->handle(lmStates::LOGIN_FAILED);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* finish it up!
|
||||
* @return void
|
||||
*/
|
||||
public function logout(){
|
||||
$_SESSION=array();
|
||||
session_destroy();
|
||||
setcookie("lm_login_random", NULL, -1);
|
||||
$this->eventHandler->handle(lmStates::LOGOUT_DONE);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* just some formal checking
|
||||
* @return bool
|
||||
*/
|
||||
public function validateLogin(){
|
||||
if(!isset($_SESSION['lm_id'])){
|
||||
return false;
|
||||
}
|
||||
else{
|
||||
$sql=$this->config->getPDO()->prepare("SELECT auth_token FROM login_history WHERE user=:id and success=1 ORDER BY id DESC LIMIT 1");
|
||||
$sql->execute(array(":id"=>$_SESSION['lm_id']));
|
||||
$res=$sql->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if($res['auth_token']==$this->getSessionKey()){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* do i know you?
|
||||
* @return int
|
||||
*/
|
||||
public function isRememberingUser(){
|
||||
if(!$this->config->isRememberEnabled()){
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(is_null($this->getRememberKey())){
|
||||
return NULL;
|
||||
}
|
||||
else{
|
||||
$sql=$this->config->getPDO()->prepare("SELECT COUNT(id) AS count, user FROM login_remember WHERE remember_token=:token and until>:until");
|
||||
$sql->execute(array(":token"=>$this->getRememberKey(), ":until"=>date("Y-m-d H:i:s")));
|
||||
$res=$sql->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if($res['count']!=1){
|
||||
$this->addLoginHistory(lmStates::NOUSER, lmStates::LOGIN_FAILED);
|
||||
return NULL;
|
||||
}
|
||||
else{
|
||||
return $res['user'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* i don't know you anymore!
|
||||
* @return void
|
||||
*/
|
||||
public function forgetUser(){
|
||||
$sql=$this->config->getPDO()->prepare("UPDATE login_remember SET until=0 WHERE remember_token=:token");
|
||||
$sql->execute(array(":token"=>$this->getRememberKey()));
|
||||
|
||||
setcookie("lm_login_remember", NULL, -1);
|
||||
|
||||
$this->eventHandler->handle(lmStates::FORGET_DONE);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* print captcha html code if needed
|
||||
* @param bool $dark use the dark theme, default false
|
||||
* @return void
|
||||
*/
|
||||
public function printCaptcha($dark=false){
|
||||
if($this->config->isCaptchaEnabled()){
|
||||
global $lm_force_captcha;
|
||||
if(isset($lm_force_captcha)){
|
||||
if($dark){
|
||||
echo "<div class=\"g-recaptcha\" data-sitekey=\"".$this->config->getCaptchaSitekey()."\" data-theme=\"dark\"></div>";
|
||||
}
|
||||
else{
|
||||
echo "<div class=\"g-recaptcha\" data-sitekey=\"".$this->config->getCaptchaSitekey()."\"></div>";
|
||||
}
|
||||
return;
|
||||
}
|
||||
else{
|
||||
return;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//backend functions
|
||||
|
||||
protected function generateSessionKey(){
|
||||
$random=lmUtils::randomString(32);
|
||||
setcookie("lm_login_random", $random, time()+$this->config->getSessionLifetime());
|
||||
$hash=hash("sha256", $_SERVER['REMOTE_ADDR']."***".$_SERVER['HTTP_USER_AGENT']."***".$random);
|
||||
return $hash;
|
||||
}
|
||||
|
||||
protected function getSessionKey(){
|
||||
if(!isset($_COOKIE['lm_login_random'])){
|
||||
return NULL;
|
||||
}
|
||||
else{
|
||||
$hash=hash("sha256", $_SERVER['REMOTE_ADDR']."***".$_SERVER['HTTP_USER_AGENT']."***".$_COOKIE['lm_login_random']);
|
||||
return $hash;
|
||||
}
|
||||
}
|
||||
|
||||
protected function passFailedAttempts(){
|
||||
//check if no limitations are enabled
|
||||
if(!$this->config->isCaptchaEnabled() && !$this->config->isBanEnabled()){
|
||||
return true; //nothing to do
|
||||
}
|
||||
|
||||
//check if is already banned
|
||||
if($this->config->isBanEnabled()){
|
||||
$sql=$this->config->getPDO()->prepare("SELECT COUNT(id) AS count FROM login_bans WHERE id=:ip and until>:until");
|
||||
$sql->execute(array(":ip"=>$_SERVER['REMOTE_ADDR'], ":until"=>date("Y-m-d H:i:s")));
|
||||
$res=$sql->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if($res['count']!=0){
|
||||
$this->eventHandler->handle(lmStates::BANNED);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//count failed attempts
|
||||
$sql=$this->config->getPDO()->prepare("SELECT COUNT(id) AS count FROM login_history WHERE ip=:ip and date>:date and success=0");
|
||||
$sql->execute(array(":ip"=>$_SERVER['REMOTE_ADDR'], ":date"=>date("Y-m-d H:i:s", time()-$this->config->getLook())));
|
||||
$res=$sql->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
//force captcha if case
|
||||
if($res['count']>=$this->config->getCaptchaAfter() && $this->config->isCaptchaEnabled()){
|
||||
global $lm_force_captcha;
|
||||
$lm_force_captcha=true;
|
||||
}
|
||||
|
||||
//bann if case
|
||||
if($res['count']>=$this->config->getBanAfter() && $this->config->isBanEnabled()){
|
||||
$sql=$this->config->getPDO()->prepare("INSERT INTO login_bans (ip, until) VALUES (:ip, :until)");
|
||||
$sql->execute(array(":ip"=>$_SERVER['REMOTE_ADDR'], ":until"=>date("Y-m-d H:i:s", time()+$this->config->getBanTime())));
|
||||
global $lm_banned;
|
||||
$lm_banned=true;
|
||||
$this->eventHandler->handle(lmStates::BANNED);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function addLoginHistory($uid, $success=lmStates::LOGIN_FAILED, $token=""){
|
||||
$sql=$this->config->getPDO()->prepare("INSERT INTO login_history (user, date, ip, auth_token, user_agent, success) VALUES (:user, :date, :ip, :auth_token, :user_agent, :success)");
|
||||
$sql->execute(array(":user"=>$uid, ":date"=>date("Y-m-d H:i:s"), ":ip"=>$_SERVER['REMOTE_ADDR'], ":auth_token"=>$token, ":user_agent"=>$_SERVER['HTTP_USER_AGENT'], ":success"=>$success));
|
||||
return;
|
||||
}
|
||||
|
||||
protected function permitLogin($uid){
|
||||
$token=$this->generateSessionKey();
|
||||
$this->addLoginHistory($uid, lmStates::LOGIN_OK, $token);
|
||||
|
||||
$_SESSION=array();
|
||||
$_SESSION['lm_id']=$uid;
|
||||
|
||||
$this->eventHandler->handle(lmStates::LOGIN_OK, $uid);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//functions for remembering
|
||||
protected function generateRememberKey(){
|
||||
$random=lmUtils::randomString(32);
|
||||
setcookie("lm_login_remember", $random, time()+(86000*$this->config->getRememberTime()));
|
||||
$hash=hash("sha256", $_SERVER['REMOTE_ADDR']."***".$_SERVER['HTTP_USER_AGENT']."***".$random);
|
||||
return $hash;
|
||||
}
|
||||
|
||||
protected function getRememberKey(){
|
||||
if(!isset($_COOKIE['lm_login_remember'])){
|
||||
return NULL;
|
||||
}
|
||||
else{
|
||||
$hash=hash("sha256", $_SERVER['REMOTE_ADDR']."***".$_SERVER['HTTP_USER_AGENT']."***".$_COOKIE['lm_login_remember']);
|
||||
return $hash;
|
||||
}
|
||||
}
|
||||
|
||||
protected function rememberUser($uid){
|
||||
$sql=$this->config->getPDO()->prepare("INSERT INTO login_remember (user, remember_token, until) VALUES (:user, :token, :until)");
|
||||
$sql->execute(array(":user"=>$uid, ":token"=>$this->generateRememberKey(), ":until"=>date("Y-m-d H:i:s", time()+(86400*$this->config->getRememberTime()))));
|
||||
return;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user