Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[REFACTORING/IMPROVEMENT] 1-minimal deep rewrite #98

Closed
wants to merge 33 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
366eaec
Update Registration.php
nyamsprod Jun 30, 2013
41c4947
Update Registration.php
nyamsprod Jun 30, 2013
a5abd2e
Update Registration.php
nyamsprod Jun 30, 2013
fe42bc0
Update Registration.php
nyamsprod Jun 30, 2013
51a7210
Update db.php
nyamsprod Jun 30, 2013
9bbf08c
Update Registration.php
nyamsprod Jun 30, 2013
11be254
typo fix
nyamsprod Jun 30, 2013
5302c6f
Update Registration.php
nyamsprod Jun 30, 2013
0c45054
Update Registration.php
nyamsprod Jun 30, 2013
cdd481c
Registration is extended from Auth.php
nyamsprod Jul 1, 2013
9216378
Login/php extends Auth.php
nyamsprod Jul 1, 2013
2f6b2f3
Create Auth.php
nyamsprod Jul 1, 2013
04b8800
Added function to validate submitted data
nyamsprod Jul 1, 2013
4009d4e
Added PHPDocs comments
nyamsprod Jul 1, 2013
7189e45
Adding check to see if the session is not corrupted
nyamsprod Jul 1, 2013
ba8166f
Added Methods and PHPDocs comments
nyamsprod Jul 1, 2013
fe524eb
Update Login.php
nyamsprod Jul 1, 2013
35c41db
Update Registration.php
nyamsprod Jul 1, 2013
6fa71f8
Update Registration.php
nyamsprod Jul 1, 2013
cbff6b8
Update Auth.php
nyamsprod Jul 1, 2013
f3c8693
Update Registration.php
nyamsprod Jul 1, 2013
54abd4e
Update Registration.php
nyamsprod Jul 1, 2013
4d67fc1
Change method visibility
nyamsprod Jul 1, 2013
1903f8e
Update Login.php
nyamsprod Jul 1, 2013
51c8c7b
Update register.php
nyamsprod Jul 1, 2013
1400bf1
Update not_logged_in.php
nyamsprod Jul 1, 2013
25dca9c
Update not_logged_in.php
nyamsprod Jul 1, 2013
34aa25b
Update register.php
nyamsprod Jul 1, 2013
c5332c4
Update index.php
nyamsprod Jul 1, 2013
b858f08
Update register.php
nyamsprod Jul 1, 2013
0d8d0b8
Bug and indentation fixes
ignace-dev Jul 1, 2013
937125a
Bug fixes and example rewrite to help understand the new code for 1-m…
ignace-dev Jul 2, 2013
d1edbde
Update db.php
nyamsprod Jul 2, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
Update Auth.php
  • Loading branch information
nyamsprod committed Jul 1, 2013
commit cbff6b80717fe128c2ebb010baa32397500380af
96 changes: 85 additions & 11 deletions 1-minimal/classes/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,42 @@
*/
class Auth
{
private $conn; // database connection
private $errors = array(); // collection of error messages
/**
* Database connection
* @var MySQLi
*/
private $conn;

/**
* Collection of error messages
* @var array
*/
private $errors = array();

/**
* Collection of regular expressions to validate user data
* @var array
*/
public static final $regexp = array(
'user_name' => '^[a-zA-Z0-9]{2,64}$',
'user_password' => '^.{6,}$'
);

const DATA_MISSING = 1;
const DATA_INVALID = 2;
const DATA_MISMATCH = 3;
const REGISTRATION_FAILED = 1;
const USER_EXISTS = 1;
const USER_UNKNOWN = 2;
/********************************************************
* Possible Error using Constants to enable localization
********************************************************/
const DATA_MISSING = 1; //data is missing
const DATA_INVALID = 2; //data is invalid
const DATA_MISMATCH = 3; //string mismatch between 2 string
const REGISTRATION_FAILED = 1; //registration failed (db error)
const USER_EXISTS = 1; //user submitted already exists in database
const USER_UNKNOWN = 2; //user unknown (user name OR password Error)

/**
* Used to generated a unique token for each user
* @var string
*/
private $secretKey = 'This is my hidden secret key'; //you should change this phrase

/**
* The Constructor initialize the db connection
Expand All @@ -35,7 +58,7 @@ public function __construct()
}

/**
* Return the errors
* Return the regular expressions (can be use to match PHP and HTML5 regular expression)
* @param string $name an specified regular expression
* @return mixed
*/
Expand Down Expand Up @@ -99,7 +122,7 @@ public static function isValidPassword($str = null)
FILTER_VALIDATE_REGEXP,
array(
'options' => array(
'regexp' => '/'.self::regexp['user_password'].'/'
'regexp' => '/'.self::$regexp['user_password'].'/'
)
)
);
Expand All @@ -125,7 +148,7 @@ public static function isValidUserName($str = null)
FILTER_VALIDATE_REGEXP,
array(
'options' => array(
'regexp' => '/'.self::regexp['user_name'].'/'
'regexp' => '/'.self::$regexp['user_name'].'/'
)
)
);
Expand All @@ -151,4 +174,55 @@ private function getUserByName($login)
}
return $res->fetch_assoc();
}

/**
* is a user already with the given login OR email exists in the database
* @param str $login the user name
* @param str $email the user email
*
* @return boolean
*/
private function isUserExists($login, $email)
{
$login = $this->conn->real_escape_string($login);
$email = $this->conn->real_escape_string($email);
$res = $this->conn->query(
"SELECT COUNT(user_id) AS nb FROM users WHERE user_name = '$login' OR user_email = '$email'"
);
$nb = $res->fetch_assoc();
return (bool) $nb['nb'];
}

/**
* generate a unique token
* @param string $login a string to generate the token with
* @return string the generated token
*/
private function generateToken($login)
{
$userAgent = (isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : '';
$timestamp = time();
$secret = sha1($login.'|'.$this->secretKey.'|'.$userAgent.'|'.$timestamp);
return $login.'|'.$timestamp.'|'.$secret;
}

/**
* validate a token against itself and against time
* which makes session timeout possible
* @param string $str the token to be validated
* @return boolean
*/
private function isValidateToken($str)
{
list($login, $timestamp, $secret) = explode('|', $str);
$userAgent = (isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : '';
if (
sha1($login.'|'.$this->secretKey.'|'.$userAgent.'|'.$timestamp) != $secret ||
strtotime('NOW - 30 MINUTES') > $timestamp
) {
return false;
}
return true;
}

}