]> git.mxchange.org Git - friendica-addons.git/blobdiff - ldapauth/ldapauth.php
Avoid a notice in twitter.php
[friendica-addons.git] / ldapauth / ldapauth.php
index a0b270e38842adbc1cae6322bf7efe82c12ee230..514fb1d1a89ade5565a827ce1292409e7cfec57e 100644 (file)
  * Note when using with Windows Active Directory: you may need to set TLS_CACERT in your site
  * ldap.conf file to the signing cert for your LDAP server.
  *
- * The configuration options for this module may be set in the .htconfig.php file
+ * The configuration options for this module may be set in the config/addon.ini.php file
  * e.g.:
  *
- * // ldap hostname server - required
- * $a->config['ldapauth']['ldap_server'] = 'host.example.com';
- * // dn to search users - required
- * $a->config['ldapauth']['ldap_searchdn'] = 'ou=users,dc=example,dc=com';
- * // attribute to find username - required
- * $a->config['ldapauth']['ldap_userattr'] = 'uid';
+ * [ldapauth]
+ * ; ldap hostname server - required
+ * ldap_server = host.example.com
+ * ; dn to search users - required
+ * ldap_searchdn = ou=users,dc=example,dc=com
+ * ; attribute to find username - required
+ * ldap_userattr = uid
  *
- * // admin dn - optional - only if ldap server dont have anonymous access
- * $a->config['ldapauth']['ldap_binddn'] = 'cn=admin,dc=example,dc=com';
- * // admin password - optional - only if ldap server dont have anonymous access
- * $a->config['ldapauth']['ldap_bindpw'] = 'password';
+ * ; admin dn - optional - only if ldap server dont have anonymous access
+ * ldap_binddn = cn=admin,dc=example,dc=com
+ * ; admin password - optional - only if ldap server dont have anonymous access
+ * ldap_bindpw = password
  *
- * // for create Friendica account if user exist in ldap
- * //     required an email and a simple (beautiful) nickname on user ldap object
- * //   active account creation - optional - default none
- * $a->config['ldapauth']['ldap_autocreateaccount'] = 'true';
- * //   attribute to get email - optional - default : 'mail'
- * $a->config['ldapauth']['ldap_autocreateaccount_emailattribute'] = 'mail';
- * //   attribute to get nickname - optional - default : 'givenName'
- * $a->config['ldapauth']['ldap_autocreateaccount_nameattribute'] = 'cn';
+ * ; for create Friendica account if user exist in ldap
+ * ;     required an email and a simple (beautiful) nickname on user ldap object
+ * ;   active account creation - optional - default none
+ * ldap_autocreateaccount = true
+ * ;   attribute to get email - optional - default : 'mail'
+ * ldap_autocreateaccount_emailattribute = mail
+ * ;   attribute to get nickname - optional - default : 'givenName'
+ * ldap_autocreateaccount_nameattribute = cn
  *
  * ...etc.
  */
+use Friendica\Core\Addon;
 use Friendica\Core\Config;
 use Friendica\Model\User;
 
 function ldapauth_install()
 {
-       register_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
+       Addon::registerHook('load_config',  'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
+       Addon::registerHook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
 }
 
 function ldapauth_uninstall()
 {
-       unregister_hook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
+       Addon::unregisterHook('load_config',  'addon/ldapauth/ldapauth.php', 'ldapauth_load_config');
+       Addon::unregisterHook('authenticate', 'addon/ldapauth/ldapauth.php', 'ldapauth_hook_authenticate');
+}
+
+function ldapauth_load_config(\Friendica\App $a)
+{
+       $a->loadConfigFile(__DIR__. '/config/ldapauth.ini.php');
 }
 
 function ldapauth_hook_authenticate($a, &$b)
@@ -176,12 +185,13 @@ function ldap_autocreateaccount($ldap_autocreateaccount, $username, $password, $
                $results = get_existing_account($username);
                if (empty($results)) {
                        if (strlen($email) > 0 && strlen($name) > 0) {
-                               $arr = array('username' => $name, 'nickname' => $username, 'email' => $email, 'password' => $password, 'verified' => 1);
-                               $result = User::create($arr);
-                               if ($result['success']) {
+                               $arr = ['username' => $name, 'nickname' => $username, 'email' => $email, 'password' => $password, 'verified' => 1];
+
+                               try {
+                                       User::create($arr);
                                        logger("ldapauth: account " . $username . " created");
-                               } else {
-                                       logger("ldapauth: account " . $username . " was not created ! : " . implode($result));
+                               } catch (Exception $ex) {
+                                       logger("ldapauth: account " . $username . " was not created ! : " . $ex->getMessage());
                                }
                        } else {
                                logger("ldapauth: unable to create account, no email or nickname found");