Move include/user to src #462

Merged
MrPetovan merged 3 commits from task/3942-move-user-to-src into master 2017-12-04 10:33:02 +01:00

View file

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Name: LDAP Authenticate * Name: LDAP Authenticate
* Description: Authenticate a user against an LDAP directory * Description: Authenticate a user against an LDAP directory
@ -51,147 +52,145 @@
* *
* ...etc. * ...etc.
*/ */
require_once('include/user.php');
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Model\User;
function ldapauth_install()
function ldapauth_install() { {
register_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate'); register_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
} }
function ldapauth_uninstall()
function ldapauth_uninstall() { {
unregister_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate'); unregister_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
} }
function ldapauth_hook_authenticate($a, &$b)
function ldapauth_hook_authenticate($a,&$b) { {
if(ldapauth_authenticate($b['username'],$b['password'])) { if (ldapauth_authenticate($b['username'], $b['password'])) {
$results = get_existing_account($b['username']); $results = get_existing_account($b['username']);
if(! empty($results)){ if (!empty($results)) {
$b['user_record'] = $results[0]; $b['user_record'] = $results[0];
$b['authenticated'] = 1; $b['authenticated'] = 1;
} }
} }
return; return;
} }
function ldapauth_authenticate($username,$password) { function ldapauth_authenticate($username, $password)
{
$ldap_server = Config::get('ldapauth', 'ldap_server');
$ldap_binddn = Config::get('ldapauth', 'ldap_binddn');
$ldap_bindpw = Config::get('ldapauth', 'ldap_bindpw');
$ldap_searchdn = Config::get('ldapauth', 'ldap_searchdn');
$ldap_userattr = Config::get('ldapauth', 'ldap_userattr');
$ldap_group = Config::get('ldapauth', 'ldap_group');
$ldap_autocreateaccount = Config::get('ldapauth', 'ldap_autocreateaccount');
$ldap_autocreateaccount_emailattribute = Config::get('ldapauth', 'ldap_autocreateaccount_emailattribute');
$ldap_autocreateaccount_nameattribute = Config::get('ldapauth', 'ldap_autocreateaccount_nameattribute');
$ldap_server = Config::get('ldapauth','ldap_server'); if (!(strlen($password) && function_exists('ldap_connect') && strlen($ldap_server))) {
$ldap_binddn = Config::get('ldapauth','ldap_binddn'); logger("ldapauth: not configured or missing php-ldap module");
$ldap_bindpw = Config::get('ldapauth','ldap_bindpw'); return false;
$ldap_searchdn = Config::get('ldapauth','ldap_searchdn'); }
$ldap_userattr = Config::get('ldapauth','ldap_userattr');
$ldap_group = Config::get('ldapauth','ldap_group');
$ldap_autocreateaccount = Config::get('ldapauth','ldap_autocreateaccount');
$ldap_autocreateaccount_emailattribute = Config::get('ldapauth','ldap_autocreateaccount_emailattribute');
$ldap_autocreateaccount_nameattribute = Config::get('ldapauth','ldap_autocreateaccount_nameattribute');
if(! ((strlen($password)) $connect = @ldap_connect($ldap_server);
&& (function_exists('ldap_connect'))
&& (strlen($ldap_server)))) {
logger("ldapauth: not configured or missing php-ldap module");
return false;
}
$connect = @ldap_connect($ldap_server); if ($connect === false) {
logger("ldapauth: could not connect to $ldap_server");
return false;
}
if($connect === false) { @ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION, 3);
logger("ldapauth: could not connect to $ldap_server"); @ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
return false; if ((@ldap_bind($connect, $ldap_binddn, $ldap_bindpw)) === false) {
} logger("ldapauth: could not bind $ldap_server as $ldap_binddn");
return false;
}
@ldap_set_option($connect, LDAP_OPT_PROTOCOL_VERSION,3); $res = @ldap_search($connect, $ldap_searchdn, $ldap_userattr . '=' . $username);
@ldap_set_option($connect, LDAP_OPT_REFERRALS, 0);
if((@ldap_bind($connect,$ldap_binddn,$ldap_bindpw)) === false) {
logger("ldapauth: could not bind $ldap_server as $ldap_binddn");
return false;
}
$res = @ldap_search($connect,$ldap_searchdn, $ldap_userattr . '=' . $username); if (!$res) {
logger("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
return false;
}
if(! $res) { $id = @ldap_first_entry($connect, $res);
logger("ldapauth: $ldap_userattr=$username,$ldap_searchdn not found");
return false;
}
$id = @ldap_first_entry($connect,$res); if (!$id) {
return false;
}
if(! $id) { $dn = @ldap_get_dn($connect, $id);
return false;
}
$dn = @ldap_get_dn($connect,$id); if (!@ldap_bind($connect, $dn, $password)) {
return false;
}
if(! @ldap_bind($connect,$dn,$password)) $emailarray = [];
return false; $namearray = [];
if ($ldap_autocreateaccount == "true") {
if (!strlen($ldap_autocreateaccount_emailattribute)) {
$ldap_autocreateaccount_emailattribute = "mail";
}
if (!strlen($ldap_autocreateaccount_nameattribute)) {
$ldap_autocreateaccount_nameattribute = "givenName";
}
$emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
$namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
}
$emailarray = []; if (!strlen($ldap_group)) {
$namearray = []; ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
if($ldap_autocreateaccount == "true"){ return true;
if(! strlen($ldap_autocreateaccount_emailattribute)) }
$ldap_autocreateaccount_emailattribute = "mail";
if(! strlen($ldap_autocreateaccount_nameattribute))
$ldap_autocreateaccount_nameattribute = "givenName";
$emailarray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_emailattribute);
$namearray = @ldap_get_values($connect, $id, $ldap_autocreateaccount_nameattribute);
}
if(! strlen($ldap_group)){ $r = @ldap_compare($connect, $ldap_group, 'member', $dn);
ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]); if ($r === -1) {
return true; $err = @ldap_error($connect);
} $eno = @ldap_errno($connect);
@ldap_close($connect);
$r = @ldap_compare($connect,$ldap_group,'member',$dn); if ($eno === 32) {
if ($r === -1) { logger("ldapauth: access control group Does Not Exist");
$err = @ldap_error($connect); return false;
$eno = @ldap_errno($connect); } elseif ($eno === 16) {
@ldap_close($connect); logger('ldapauth: membership attribute does not exist in access control group');
return false;
} else {
logger('ldapauth: error: ' . $err);
return false;
}
} elseif ($r === false) {
@ldap_close($connect);
return false;
}
if ($eno === 32) { ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $emailarray[0], $namearray[0]);
logger("ldapauth: access control group Does Not Exist"); return true;
return false;
}
elseif ($eno === 16) {
logger('ldapauth: membership attribute does not exist in access control group');
return false;
}
else {
logger('ldapauth: error: ' . $err);
return false;
}
}
elseif ($r === false) {
@ldap_close($connect);
return false;
}
ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$emailarray[0],$namearray[0]);
return true;
} }
function ldap_autocreateaccount($ldap_autocreateaccount,$username,$password,$email,$name) { function ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $email, $name)
if($ldap_autocreateaccount == "true"){ {
$results = get_existing_account($username); if ($ldap_autocreateaccount == "true") {
if(empty($results)){ $results = get_existing_account($username);
if (strlen($email) > 0 && strlen($name) > 0){ if (empty($results)) {
$arr = array('username'=>$name,'nickname'=>$username,'email'=>$email,'password'=>$password,'verified'=>1); if (strlen($email) > 0 && strlen($name) > 0) {
$result = create_user($arr); $arr = array('username' => $name, 'nickname' => $username, 'email' => $email, 'password' => $password, 'verified' => 1);
if ($result['success']){ $result = User::create($arr);
logger("ldapauth: account " . $username . " created"); if ($result['success']) {
}else{ logger("ldapauth: account " . $username . " created");
logger("ldapauth: account " . $username . " was not created ! : " . implode($result)); } else {
} logger("ldapauth: account " . $username . " was not created ! : " . implode($result));
}else{ }
logger("ldapauth: unable to create account, no email or nickname found"); } else {
} logger("ldapauth: unable to create account, no email or nickname found");
} }
} }
}
} }
function get_existing_account($username){ function get_existing_account($username)
return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1",$username); {
return q("SELECT * FROM `user` WHERE `nickname` = '%s' AND `blocked` = 0 AND `verified` = 1 LIMIT 1", $username);
} }