diff --git a/main.inc.php b/main.inc.php new file mode 100644 index 0000000..0e11945 --- /dev/null +++ b/main.inc.php @@ -0,0 +1,115 @@ +load_config(); +set_plugin_data($plugin['id'], $ldap); +unset($ldap); + +// +-----------------------------------------------------------------------+ +// | functions | +// +-----------------------------------------------------------------------+ + +function random_password( $length = 8 ) { + $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?"; + $password = substr( str_shuffle( $chars ), 0, $length ); + return $password; +} + +function ld_init(){ + load_language('plugin.lang', LDAP_LOGIN_PATH); +} + + +function login($success, $username, $password, $remember_me){ + + global $conf; + + $obj = new Ldap(); + $obj->load_config(); + $obj->ldap_conn() or die("Unable to connect LDAP server : ".$ldap->getErrorString()); + + //if (!$obj->ldap_bind_as($username,$password)){ // bind with userdn + if (!$obj->ldap_search_dn($username) || !$obj->ldap_bind_as($obj->ldap_search_dn($username),$password)){ // bind with userdn + trigger_notify('login_failure', stripslashes($username)); + return false; // wrong password + } + + // search user in piwigo database + $query = 'SELECT '.$conf['user_fields']['id'].' AS id FROM '.USERS_TABLE.' WHERE '.$conf['user_fields']['username'].' = \''.pwg_db_real_escape_string($username).'\' ;'; + + $row = pwg_db_fetch_assoc(pwg_query($query)); + + // if query is not empty, it means everything is ok and we can continue, auth is done ! + if (!empty($row['id'])) { + log_user($row['id'], $remember_me); + trigger_notify('login_success', stripslashes($username)); + return true; + } + + // if query is empty but ldap auth is done we can create a piwigo user if it's said so ! + else { + // this is where we check we are allowed to create new users upon that. + if ($obj->config['allow_newusers']) { + + // we got the email address + if ($obj->ldap_mail($username)) { + $mail = $obj->ldap_mail($username); + } + else { + $mail = NULL; + } + + // we actually register the new user + $new_id = register_user($username,random_password(8),$mail); + + // now we fetch again his id in the piwigo db, and we get them, as we just created him ! + //$query = 'SELECT '.$conf['user_fields']['id'].' AS id FROM '.USERS_TABLE.' WHERE '.$conf['user_fields']['username'].' = \''.pwg_db_real_escape_string($username).'\' ;'; + //$row = pwg_db_fetch_assoc(pwg_query($query)); + + log_user($new_id, False); + trigger_notify('login_success', stripslashes($username)); + redirect('profile.php'); + return true; + } + // else : this is the normal behavior ! user is not created. + else { + trigger_notify('login_failure', stripslashes($username)); + return false; + } + } +} + +?>