@@ -18,7 +18,13 @@ public static function is_logged_in(){
1818 return true ;
1919 }
2020
21- return !empty ($ _SESSION [User::SESSION_NAME ]) && $ _SESSION [User::SESSION_NAME ] === hash ("crc32 " , Config::get ("nick " ).Config::get_safe ("pass " , "" ), false );
21+ if (Config::get_safe ("ldap_enabled " , false )){
22+ return !empty ($ _SESSION [User::SESSION_NAME ]) &&
23+ $ _SESSION [User::SESSION_NAME ] === 'admin ' ;
24+ }
25+
26+ return !empty ($ _SESSION [User::SESSION_NAME ]) &&
27+ $ _SESSION [User::SESSION_NAME ] === hash ("crc32 " , Config::get ("nick " ).Config::get_safe ("pass " , "" ), false );
2228 }
2329
2430 public static function login ($ nick , $ pass ){
@@ -30,6 +36,14 @@ public static function login($nick, $pass){
3036 throw new Exception (__ ("You are already logged in. " ));
3137 }
3238
39+ if (Config::get_safe ("ldap_enabled " , false )){
40+ return static ::LDAP_login ($ nick , $ pass );
41+ } else {
42+ return static ::config_login ($ nick , $ pass );
43+ }
44+ }
45+
46+ private static function config_login ($ nick , $ pass ){
3347 if (Config::get ("nick " ) === $ nick && Config::get_safe ("pass " , "" ) === $ pass ){
3448 $ _SESSION [User::SESSION_NAME ] = hash ("crc32 " , $ nick .$ pass , false );
3549 return ["logged_in " => true , "is_visitor " => false ];
@@ -45,6 +59,34 @@ public static function login($nick, $pass){
4559 throw new Exception (__ ("The nick or password is incorrect. " ));
4660 }
4761
62+ private static function LDAP_login ($ nick , $ pass ){
63+ $ ldap_host = Config::get ("ldap_host " );
64+ $ ldap_port = Config::get_safe ("ldap_port " , 389 );
65+ $ ldap_admin_dn = Config::get_safe ("ldap_admin_dn " , false );
66+ $ ldap_visitor_dn = Config::get_safe ("ldap_visitor_dn " , false );
67+
68+ if (!($ ds = ldap_connect ($ ldap_host , $ ldap_port ))) {
69+ throw new Exception (__ ("Could not connect to LDAP server. " ));
70+ }
71+
72+ ldap_set_option ($ ds , LDAP_OPT_PROTOCOL_VERSION , 3 );
73+ ldap_set_option ($ ds , LDAP_OPT_REFERRALS , 0 );
74+ ldap_set_option ($ ds , LDAP_OPT_NETWORK_TIMEOUT , 10 );
75+
76+ if ($ ldap_admin_dn !== false && ldap_bind ($ ds , "cn= " .$ nick .", " .$ ldap_admin_dn , $ pass )) {
77+ $ _SESSION [User::SESSION_NAME ] = 'admin ' ;
78+ return ["logged_in " => true , "is_visitor " => false ];
79+ }
80+
81+ if ($ ldap_visitor_dn !== false && ldap_bind ($ ds , "cn= " .$ nick .", " .$ ldap_visitor_dn , $ pass )) {
82+ $ _SESSION [User::SESSION_NAME ] = 'visitor ' ;
83+ return ["logged_in " => false , "is_visitor " => true ];
84+ }
85+
86+ Log::put ("login_fails " , $ nick );
87+ throw new Exception (__ ("The nick or password is incorrect. " ));
88+ }
89+
4890 public static function logout (){
4991 if (!Config::get_safe ("force_login " , false )){
5092 throw new Exception (__ ("You can't log out. There is no account. " ));
0 commit comments