@@ -38,14 +38,12 @@ class CUserManager
3838 * @param $password the current user's password
3939 * @return noreturn.
4040 */
41- public function __construct ($ aid, $ password )
41+ public function __construct ($ aid )
4242 {
4343 $ this ->dbh = new Database (DB_HOST , DB_PORT , DB_NAME , DB_USER , DB_PASS , DB_PREFIX );
4444
45- if ($ this ->CheckLogin ($ password , $ aid )) {
46- $ this ->aid = $ aid ;
47- $ this ->GetUserArray ($ aid );
48- }
45+ $ this ->aid = $ aid ;
46+ $ this ->GetUserArray ($ aid );
4947 }
5048
5149
@@ -63,7 +61,7 @@ public function GetUserArray($aid = null)
6361 }
6462 // Invalid aid
6563 if ($ aid < 0 || empty ($ aid )) {
66- return 0 ;
64+ return false ;
6765 }
6866
6967 // We already got the data from the DB, and its saved in the manager
@@ -82,7 +80,7 @@ public function GetUserArray($aid = null)
8280 $ res = $ this ->dbh ->single ();
8381
8482 if (!$ res ) {
85- return 0 ; // ohnoes some type of db error
83+ return false ; // ohnoes some type of db error
8684 }
8785
8886 $ user = array ();
@@ -212,30 +210,24 @@ public function CheckLogin($password, $aid)
212210
213211 public function login ($ aid , $ password , $ save = true )
214212 {
215- if ($ this ->CheckLogin ($ this ->encrypt_password ($ password ), $ aid )) {
213+ if ($ this ->CheckLogin ($ this ->encrypt_password ($ password ), $ aid ) || $ this -> CheckLogin ( $ this -> hash ( $ password ), $ aid ) ) {
216214 //Old password hash detected update it.
217215 $ this ->dbh ->query ('UPDATE `:prefix_admins` SET password = :password WHERE aid = :aid ' );
218- $ this ->dbh ->bind (':password ' , $ this -> hash ($ password ));
216+ $ this ->dbh ->bind (':password ' , password_hash ($ password, PASSWORD_BCRYPT ));
219217 $ this ->dbh ->bind (':aid ' , $ aid );
220218 $ this ->dbh ->execute ();
221219
222- setcookie ("aid " , $ aid );
223- setcookie ("password " , $ this ->hash ($ password ));
224- setcookie ("user " , $ _SESSION ['user ' ]['user ' ]);
220+ \SessionManager::sessionStart ('login ' , 604800 , 0 );
221+ $ _SESSION ['aid ' ] = $ aid ;
225222 return true ;
226223 }
227224
228- if ($ this ->CheckLogin ($ this ->hash ($ password ), $ aid )) {
229- if ($ save ) {
230- //Sets cookies
231- setcookie ("aid " , $ aid , time ()+LOGIN_COOKIE_LIFETIME );
232- setcookie ("password " , $ this ->hash ($ password ), time ()+LOGIN_COOKIE_LIFETIME );
233- setcookie ("user " , isset ($ _SESSION ['user ' ]['user ' ])?$ _SESSION ['user ' ]['user ' ]:null , time ()+LOGIN_COOKIE_LIFETIME );
234- return true ;
235- }
236- setcookie ("aid " , $ aid );
237- setcookie ("password " , $ this ->hash ($ password ));
238- setcookie ("user " , $ _SESSION ['user ' ]['user ' ]);
225+ $ this ->dbh ->query ('SELECT password FROM `:prefix_admins` WHERE aid = :aid ' );
226+ $ this ->dbh ->bind (':aid ' , $ aid );
227+ $ hash = $ this ->dbh ->single ();
228+ if (password_verify ($ password , $ hash ['password ' ])) {
229+ \SessionManager::sessionStart ('login ' , 604800 , 0 );
230+ $ _SESSION ['aid ' ] = $ aid ;
239231 return true ;
240232 }
241233 return false ;
@@ -332,19 +324,13 @@ public function AddAdmin($name, $steam, $password, $email, $web_group, $web_flag
332324 throw new RuntimeException ('Password must be at least ' . MIN_PASS_LENGTH . ' characters long. ' );
333325 }
334326 if (empty ($ password )) {
335- // Silently generate a token for account if there is no password set
336- // the token is required in Steam OAuth routines.
337- // Due to ugly codebase and lack of migrations we store the token as password hash.
338- // Also we use a prefix here to prevent any possible collisions with `encrypt_password` implementation.
339- $ password_hash = '$token$ ' . $ this ->random_string ();
340- } else {
341- $ password_hash = $ this ->hash ($ password );
327+ throw new RuntimeException ('Password must not be empty! ' );
342328 }
343329 $ this ->dbh ->query ('INSERT INTO `:prefix_admins` (user, authid, password, gid, email, extraflags, immunity, srv_group, srv_flags, srv_password)
344330 VALUES (:user, :authid, :password, :gid, :email, :extraflags, :immunity, :srv_group, :srv_flags, :srv_password) ' );
345331 $ this ->dbh ->bind (':user ' , $ name );
346332 $ this ->dbh ->bind (':authid ' , $ steam );
347- $ this ->dbh ->bind (':password ' , $ password_hash );
333+ $ this ->dbh ->bind (':password ' , password_hash ( $ password , PASSWORD_BCRYPT ) );
348334 $ this ->dbh ->bind (':gid ' , $ web_group );
349335 $ this ->dbh ->bind (':email ' , $ email );
350336 $ this ->dbh ->bind (':extraflags ' , $ web_flags );
0 commit comments