@@ -85,10 +85,20 @@ public function login($username, $password, $client_login = false)
8585 //* Delete old remoting sessions
8686 $ sql = "DELETE FROM remote_session WHERE tstamp < UNIX_TIMESTAMP() " ;
8787 $ app ->db ->query ($ sql );
88+
89+ //* Check for max. login attempts
90+ $ ip_md5 = md5 ($ _SERVER ['REMOTE_ADDR ' ]);
91+ $ sql = "SELECT * FROM `attempts_login` WHERE `ip`= ? AND `login_time` > (NOW() - INTERVAL 5 MINUTE) LIMIT 1 " ;
92+ $ alreadyfailed = $ app ->db ->queryOneRecord ($ sql , $ ip_md5 );
93+
94+ if ($ alreadyfailed ['times ' ] > 10 ) {
95+ throw new SoapFault ('login_failure_limit ' , 'The login failure limit has been reached. ' );
96+ return false ;
97+ }
8898
8999 if ($ client_login == true ) {
90100 $ sql = "SELECT * FROM sys_user WHERE USERNAME = ? " ;
91- $ user = $ app ->db ->queryOneRecord ($ sql , $ username );
101+ $ user = $ app ->db ->queryOneRecord ($ sql , ( string ) $ username );
92102 if ($ user ) {
93103 $ saved_password = stripslashes ($ user ['passwort ' ]);
94104
@@ -104,6 +114,16 @@ public function login($username, $password, $client_login = false)
104114 }
105115 }
106116 } else {
117+ if (!$ alreadyfailed ['times ' ] )
118+ {
119+ //* user login the first time wrong
120+ $ sql = "INSERT INTO `attempts_login` (`ip`, `times`, `login_time`) VALUES (?, 1, NOW()) " ;
121+ $ app ->db ->query ($ sql , $ ip_md5 );
122+ } elseif ($ alreadyfailed ['times ' ] >= 1 ) {
123+ //* update times wrong
124+ $ sql = "UPDATE `attempts_login` SET `times`=`times`+1, `login_time`=NOW() WHERE `ip` = ? AND `login_time` < NOW() ORDER BY `login_time` DESC LIMIT 1 " ;
125+ $ app ->db ->query ($ sql , $ ip_md5 );
126+ }
107127 throw new SoapFault ('client_login_failed ' , 'The login failed. Username or password wrong. ' );
108128 }
109129 if ($ user ['active ' ] != 1 ) {
@@ -119,17 +139,23 @@ public function login($username, $password, $client_login = false)
119139
120140 //* Create a remote user session
121141 //srand ((double)microtime()*1000000);
122- $ remote_session = md5 ( mt_rand ().uniqid ('ispco ' ));
142+ $ remote_session = substr ( str_shuffle ( ' abcdefghijklmnopqrstuvwxyz ' ), 0 , 1 ). sha1 ( mt_rand ().uniqid ('ispco ' , true ));
123143 $ remote_userid = $ user ['userid ' ];
124144 $ remote_functions = '' ;
125145 $ tstamp = time () + $ this ->session_timeout ;
126- $ sql = 'INSERT INTO remote_session (remote_session,remote_userid,remote_functions,client_login,tstamp '
127- .') VALUES (?, ?, ?, 1, ?) ' ;
128- $ app ->db ->query ($ sql , $ remote_session ,$ remote_userid ,$ remote_functions ,$ tstamp );
146+ $ ip = $ _SERVER ['REMOTE_ADDR ' ];
147+ $ sql = 'INSERT INTO remote_session (remote_session,remote_userid,remote_functions,client_login,tstamp,remote_ip '
148+ .') VALUES (?, ?, ?, 1, ?, ?) ' ;
149+ $ app ->db ->query ($ sql , $ remote_session ,$ remote_userid ,$ remote_functions ,$ tstamp ,$ ip );
150+
151+ //* Delete login attempts after successful login
152+ $ sql = "DELETE FROM `attempts_login` WHERE `ip`=? " ;
153+ $ app ->db ->query ($ sql , $ ip_md5 );
154+
129155 return $ remote_session ;
130156 } else {
131157 $ sql = "SELECT * FROM remote_user WHERE remote_username = ? " ;
132- $ remote_user = $ app ->db ->queryOneRecord ($ sql , $ username );
158+ $ remote_user = $ app ->db ->queryOneRecord ($ sql , ( string ) $ username );
133159 if ($ remote_user ) {
134160 if (substr ($ remote_user ['remote_password ' ], 0 , 1 ) === '$ ' ) {
135161 if (crypt (stripslashes ($ password ), $ remote_user ['remote_password ' ]) != $ remote_user ['remote_password ' ]) {
@@ -138,7 +164,7 @@ public function login($username, $password, $client_login = false)
138164 } elseif (md5 ($ password ) == $ remote_user ['remote_password ' ]) {
139165 // update hash algo
140166 $ sql = 'UPDATE `remote_user` SET `remote_password` = ? WHERE `remote_username` = ? ' ;
141- $ app ->db ->query ($ sql , $ app ->auth ->crypt_password ($ password ), $ username );
167+ $ app ->db ->query ($ sql , $ app ->auth ->crypt_password ($ password ), ( string ) $ username );
142168 } else {
143169 $ remote_user = null ;
144170 }
@@ -185,15 +211,32 @@ public function login($username, $password, $client_login = false)
185211 }
186212 //* Create a remote user session
187213 //srand ((double)microtime()*1000000);
188- $ remote_session = md5 ( mt_rand ().uniqid ('ispco ' ));
214+ $ remote_session = substr ( str_shuffle ( ' abcdefghijklmnopqrstuvwxyz ' ), 0 , 1 ). sha1 ( mt_rand ().uniqid ('ispco ' , true ));
189215 $ remote_userid = $ remote_user ['remote_userid ' ];
190216 $ remote_functions = $ remote_user ['remote_functions ' ];
191217 $ tstamp = time () + $ this ->session_timeout ;
192- $ sql = 'INSERT INTO remote_session (remote_session,remote_userid,remote_functions,tstamp '
193- .') VALUES (?, ?, ?, ?) ' ;
194- $ app ->db ->query ($ sql , $ remote_session ,$ remote_userid ,$ remote_functions ,$ tstamp );
218+ $ sql = 'INSERT INTO remote_session (remote_session,remote_userid,remote_functions,tstamp,remote_ip '
219+ .') VALUES (?, ?, ?, ?, ?) ' ;
220+ $ app ->db ->query ($ sql , $ remote_session ,$ remote_userid ,$ remote_functions ,$ tstamp , $ ip );
221+
222+ //* Delete login attempts after successful login
223+ $ sql = "DELETE FROM `attempts_login` WHERE `ip`=? " ;
224+ $ app ->db ->query ($ sql , $ ip_md5 );
225+
195226 return $ remote_session ;
196227 } else {
228+
229+ if (!$ alreadyfailed ['times ' ] )
230+ {
231+ //* user login the first time wrong
232+ $ sql = "INSERT INTO `attempts_login` (`ip`, `times`, `login_time`) VALUES (?, 1, NOW()) " ;
233+ $ app ->db ->query ($ sql , $ ip_md5 );
234+ } elseif ($ alreadyfailed ['times ' ] >= 1 ) {
235+ //* update times wrong
236+ $ sql = "UPDATE `attempts_login` SET `times`=`times`+1, `login_time`=NOW() WHERE `ip` = ? AND `login_time` < NOW() ORDER BY `login_time` DESC LIMIT 1 " ;
237+ $ app ->db ->query ($ sql , $ ip_md5 );
238+ }
239+
197240 throw new SoapFault ('login_failed ' , 'The login failed. Username or password wrong. ' );
198241 return false ;
199242 }
@@ -212,7 +255,7 @@ public function logout($session_id)
212255 }
213256
214257 $ sql = "DELETE FROM remote_session WHERE remote_session = ? " ;
215- if ($ app ->db ->query ($ sql , $ session_id ) != false ) {
258+ if ($ app ->db ->query ($ sql , ( string ) $ session_id ) != false ) {
216259 return true ;
217260 } else {
218261 return false ;
@@ -522,12 +565,61 @@ protected function getSession($session_id)
522565 throw new SoapFault ('session_id_empty ' , 'The SessionID is empty. ' );
523566 return false ;
524567 }
568+
569+ if (!is_string ($ session_id )) {
570+ throw new SoapFault ('session_id_nostring ' , 'Wrong SessionID datatype. ' );
571+ return false ;
572+ }
573+
574+ $ ip_md5 = md5 ($ _SERVER ['REMOTE_ADDR ' ]);
575+ $ sql = "SELECT * FROM `attempts_login` WHERE `ip`= ? AND `login_time` > (NOW() - INTERVAL 5 MINUTE) LIMIT 1 " ;
576+ $ alreadyfailed = $ app ->db ->queryOneRecord ($ sql , $ ip_md5 );
577+
578+ if ($ alreadyfailed ['times ' ] > 10 ) {
579+ throw new SoapFault ('session_failure_limit ' , 'The Session failure limit has been reached. ' );
580+ return false ;
581+ }
525582
526583 $ sql = "SELECT * FROM remote_session WHERE remote_session = ? AND tstamp >= UNIX_TIMESTAMP() " ;
527- $ session = $ app ->db ->queryOneRecord ($ sql , $ session_id );
584+ $ session = $ app ->db ->queryOneRecord ($ sql , (string )$ session_id );
585+
586+ if (!is_array ($ session )) {
587+ if (!$ alreadyfailed ['times ' ] )
588+ {
589+ //* user login the first time wrong
590+ $ sql = "INSERT INTO `attempts_login` (`ip`, `times`, `login_time`) VALUES (?, 1, NOW()) " ;
591+ $ app ->db ->query ($ sql , $ ip_md5 );
592+ } elseif ($ alreadyfailed ['times ' ] >= 1 ) {
593+ //* update times wrong
594+ $ sql = "UPDATE `attempts_login` SET `times`=`times`+1, `login_time`=NOW() WHERE `ip` = ? AND `login_time` < NOW() ORDER BY `login_time` DESC LIMIT 1 " ;
595+ $ app ->db ->query ($ sql , $ ip_md5 );
596+ }
597+
598+ throw new SoapFault ('session_does_not_exist ' , 'The Session is expired or does not exist. ' );
599+ return false ;
600+ }
601+
602+ $ ip = $ _SERVER ['REMOTE_ADDR ' ];
603+ if ($ session ['remote_ip ' ] != $ ip ) {
604+ throw new SoapFault ('session_ip_mismatch ' , 'Session IP mismatch. ' );
605+ return false ;
606+ }
607+
528608 if ($ session ['remote_userid ' ] > 0 ) {
529609 return $ session ;
530610 } else {
611+
612+ if (!$ alreadyfailed ['times ' ] )
613+ {
614+ //* user login the first time wrong
615+ $ sql = "INSERT INTO `attempts_login` (`ip`, `times`, `login_time`) VALUES (?, 1, NOW()) " ;
616+ $ app ->db ->query ($ sql , $ ip_md5 );
617+ } elseif ($ alreadyfailed ['times ' ] >= 1 ) {
618+ //* update times wrong
619+ $ sql = "UPDATE `attempts_login` SET `times`=`times`+1, `login_time`=NOW() WHERE `ip` = ? AND `login_time` < NOW() ORDER BY `login_time` DESC LIMIT 1 " ;
620+ $ app ->db ->query ($ sql , $ ip_md5 );
621+ }
622+
531623 throw new SoapFault ('session_does_not_exist ' , 'The Session is expired or does not exist. ' );
532624 return false ;
533625 }
0 commit comments