2018-04-07 02:31:11 +08:00
< ? php
//
2018-04-07 02:36:23 +08:00
// ZoneMinder auth library, $Date$, $Revision$
2018-04-07 02:31:11 +08:00
// Copyright (C) 2001-2008 Philip Coombes
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
2018-05-01 01:02:53 +08:00
function userLogin ( $username , $password = '' , $passwordHashed = false ) {
2018-07-11 23:45:19 +08:00
global $user ;
2018-04-07 02:31:11 +08:00
$sql = 'SELECT * FROM Users WHERE Enabled=1' ;
$sql_values = NULL ;
if ( ZM_AUTH_TYPE == 'builtin' ) {
if ( $passwordHashed ) {
$sql .= ' AND Username=? AND Password=?' ;
} else {
$sql .= ' AND Username=? AND Password=password(?)' ;
}
2018-05-01 01:02:53 +08:00
$sql_values = array ( $username , $password );
2018-04-07 02:31:11 +08:00
} else {
$sql .= ' AND Username=?' ;
2018-05-01 01:02:53 +08:00
$sql_values = array ( $username );
2018-04-07 02:31:11 +08:00
}
2018-07-11 23:45:19 +08:00
$close_session = 0 ;
if ( ! is_session_started () ) {
Logger :: Debug ( " Starting session in userLogin " );
session_start ();
$close_session = 1 ;
}
2018-04-07 02:31:11 +08:00
$_SESSION [ 'username' ] = $username ;
if ( ZM_AUTH_RELAY == 'plain' ) {
// Need to save this in session
$_SESSION [ 'password' ] = $password ;
}
$_SESSION [ 'remoteAddr' ] = $_SERVER [ 'REMOTE_ADDR' ]; // To help prevent session hijacking
2018-05-01 01:02:53 +08:00
if ( $dbUser = dbFetchOne ( $sql , NULL , $sql_values ) ) {
Info ( " Login successful for user \" $username\ " " );
2018-04-07 02:31:11 +08:00
$_SESSION [ 'user' ] = $user = $dbUser ;
unset ( $_SESSION [ 'loginFailed' ]);
if ( ZM_AUTH_TYPE == 'builtin' ) {
$_SESSION [ 'passwordHash' ] = $user [ 'Password' ];
}
session_regenerate_id ();
} else {
2018-05-01 01:02:53 +08:00
Warning ( " Login denied for user \" $username\ " " );
2018-04-07 02:31:11 +08:00
$_SESSION [ 'loginFailed' ] = true ;
2018-05-01 01:02:53 +08:00
unset ( $user );
2018-04-07 02:31:11 +08:00
}
2018-07-11 23:45:19 +08:00
if ( $close_session )
session_write_close ();
2018-07-16 09:17:35 +08:00
return isset ( $user ) ? $user : null ;
2018-05-01 01:02:53 +08:00
} # end function userLogin
2018-04-07 02:31:11 +08:00
function userLogout () {
global $user ;
2018-05-01 01:02:53 +08:00
Info ( 'User "' . $user [ 'Username' ] . '" logged out' );
2018-04-07 02:31:11 +08:00
session_start ();
2018-05-01 01:02:53 +08:00
unset ( $_SESSION [ 'user' ]);
unset ( $user );
2018-04-07 02:31:11 +08:00
session_destroy ();
}
2018-05-01 01:02:53 +08:00
function getAuthUser ( $auth ) {
2018-04-07 02:31:11 +08:00
if ( ZM_OPT_USE_AUTH && ZM_AUTH_RELAY == 'hashed' && ! empty ( $auth ) ) {
$remoteAddr = '' ;
if ( ZM_AUTH_HASH_IPS ) {
$remoteAddr = $_SERVER [ 'REMOTE_ADDR' ];
if ( ! $remoteAddr ) {
2018-05-01 01:02:53 +08:00
Error ( " Can't determine remote address for authentication, using empty string " );
2018-04-07 02:31:11 +08:00
$remoteAddr = '' ;
}
}
2018-04-13 06:43:57 +08:00
if ( isset ( $_SESSION [ 'username' ]) ) {
2018-04-07 02:31:11 +08:00
# Most of the time we will be logged in already and the session will have our username, so we can significantly speed up our hash testing by only looking at our user.
# Only really important if you have a lot of users.
$sql = " SELECT * FROM Users WHERE Enabled = 1 AND Username=' " . $_SESSION [ 'username' ] . " ' " ;
} else {
$sql = 'SELECT * FROM Users WHERE Enabled = 1' ;
}
2018-04-13 06:43:57 +08:00
foreach ( dbFetchAll ( $sql ) as $user ) {
2018-04-07 02:31:11 +08:00
$now = time ();
for ( $i = 0 ; $i < ZM_AUTH_HASH_TTL ; $i ++ , $now -= ( 3600 ) ) { // Try for last two hours
2018-04-13 06:43:57 +08:00
$time = localtime ( $now );
2018-04-07 02:31:11 +08:00
$authKey = ZM_AUTH_HASH_SECRET . $user [ 'Username' ] . $user [ 'Password' ] . $remoteAddr . $time [ 2 ] . $time [ 3 ] . $time [ 4 ] . $time [ 5 ];
2018-04-13 06:43:57 +08:00
$authHash = md5 ( $authKey );
2018-04-07 02:31:11 +08:00
if ( $auth == $authHash ) {
return $user ;
}
} // end foreach hour
} // end foreach user
} // end if using auth hash
2018-04-13 06:43:57 +08:00
Error ( " Unable to authenticate user from auth hash ' $auth ' " );
return false ;
2018-04-07 02:31:11 +08:00
} // end getAuthUser($auth)
2018-06-26 01:43:08 +08:00
function generateAuthHash ( $useRemoteAddr , $force = false ) {
2018-04-07 02:31:11 +08:00
if ( ZM_OPT_USE_AUTH and ZM_AUTH_RELAY == 'hashed' and isset ( $_SESSION [ 'username' ]) and $_SESSION [ 'passwordHash' ] ) {
# regenerate a hash at half the liftetime of a hash, an hour is 3600 so half is 1800
$time = time ();
$mintime = $time - ( ZM_AUTH_HASH_TTL * 1800 );
2018-10-20 01:39:37 +08:00
if ( $force or ( ! isset ( $_SESSION [ 'AuthHash' . $_SESSION [ 'remoteAddr' ]]) ) or ( $_SESSION [ 'AuthHashGeneratedAt' ] < $mintime ) ) {
2018-04-07 02:31:11 +08:00
# Don't both regenerating Auth Hash if an hour hasn't gone by yet
$local_time = localtime ();
$authKey = '' ;
if ( $useRemoteAddr ) {
$authKey = ZM_AUTH_HASH_SECRET . $_SESSION [ 'username' ] . $_SESSION [ 'passwordHash' ] . $_SESSION [ 'remoteAddr' ] . $local_time [ 2 ] . $local_time [ 3 ] . $local_time [ 4 ] . $local_time [ 5 ];
} else {
$authKey = ZM_AUTH_HASH_SECRET . $_SESSION [ 'username' ] . $_SESSION [ 'passwordHash' ] . $local_time [ 2 ] . $local_time [ 3 ] . $local_time [ 4 ] . $local_time [ 5 ];
}
#Logger::Debug("Generated using hour:".$local_time[2] . ' mday:' . $local_time[3] . ' month:'.$local_time[4] . ' year: ' . $local_time[5] );
2018-05-01 01:02:53 +08:00
$auth = md5 ( $authKey );
2018-06-26 02:50:54 +08:00
if ( ! $force ) {
2018-07-11 22:34:45 +08:00
$close_session = 0 ;
if ( ! is_session_started () ) {
session_start ();
$close_session = 1 ;
}
2018-10-20 01:39:37 +08:00
$_SESSION [ 'AuthHash' . $_SESSION [ 'remoteAddr' ]] = $auth ;
2018-06-26 01:43:08 +08:00
$_SESSION [ 'AuthHashGeneratedAt' ] = $time ;
session_write_close ();
2018-06-26 02:50:54 +08:00
} else {
return $auth ;
2018-06-26 01:43:08 +08:00
}
2018-04-07 02:31:11 +08:00
#Logger::Debug("Generated new auth $auth at " . $_SESSION['AuthHashGeneratedAt']. " using $authKey" );
2018-06-26 01:43:08 +08:00
#} else {
2018-04-07 02:31:11 +08:00
#Logger::Debug("Using cached auth " . $_SESSION['AuthHash'] ." beacuse generatedat:" . $_SESSION['AuthHashGeneratedAt'] . ' < now:'. $time . ' - ' . ZM_AUTH_HASH_TTL . ' * 1800 = '. $mintime);
} # end if AuthHash is not cached
2018-10-20 01:39:37 +08:00
return $_SESSION [ 'AuthHash' . $_SESSION [ 'remoteAddr' ]];
2018-06-26 02:50:54 +08:00
} # end if using AUTH and AUTH_RELAY
return '' ;
2018-04-07 02:31:11 +08:00
}
2018-05-01 01:02:53 +08:00
function visibleMonitor ( $mid ) {
2018-04-07 02:31:11 +08:00
global $user ;
2018-05-01 01:02:53 +08:00
return ( empty ( $user [ 'MonitorIds' ]) || in_array ( $mid , explode ( ',' , $user [ 'MonitorIds' ])) );
2018-04-07 02:31:11 +08:00
}
2018-05-01 01:02:53 +08:00
function canView ( $area , $mid = false ) {
2018-04-07 02:31:11 +08:00
global $user ;
2018-05-01 01:02:53 +08:00
return ( ( $user [ $area ] == 'View' || $user [ $area ] == 'Edit' ) && ( ! $mid || visibleMonitor ( $mid ) ) );
2018-04-07 02:31:11 +08:00
}
2018-05-01 01:02:53 +08:00
function canEdit ( $area , $mid = false ) {
2018-04-07 02:31:11 +08:00
global $user ;
2018-05-01 01:02:53 +08:00
return ( $user [ $area ] == 'Edit' && ( ! $mid || visibleMonitor ( $mid ) ));
2018-04-07 02:31:11 +08:00
}
2018-07-11 22:34:45 +08:00
function is_session_started () {
if ( php_sapi_name () !== 'cli' ) {
if ( version_compare ( phpversion (), '5.4.0' , '>=' ) ) {
return session_status () === PHP_SESSION_ACTIVE ? TRUE : FALSE ;
} else {
return session_id () === '' ? FALSE : TRUE ;
}
2018-07-11 23:45:19 +08:00
} else {
Warning ( " php_sapi_name === 'cli' " );
2018-07-11 22:34:45 +08:00
}
return FALSE ;
}
2018-04-07 02:31:11 +08:00
?>