/** * @brief creates a hopefully unique name for owncloud based on the display name and the dn of the LDAP object * @param $name the display name of the object * @param $dn the dn of the object * @returns string with with the name to use in ownCloud * * creates a hopefully unique name for owncloud based on the display name and the dn of the LDAP object */ private static function alternateOwnCloudName($name, $dn) { $ufn = ldap_dn2ufn($dn); $name = $name . '@' . trim(substr_replace($ufn, '', 0, strpos($ufn, ','))); $name = self::sanitizeUsername($name); return $name; }
<?php /* Convert valid DN */ var_dump(ldap_dn2ufn("cn=bob,dc=example,dc=com")); /* Convert valid DN */ var_dump(ldap_dn2ufn("cn=bob,ou=users,dc=example,dc=com")); /* Convert DN with < > characters */ var_dump(ldap_dn2ufn("cn=<bob>,dc=example,dc=com")); /* Too many parameters */ ldap_dn2ufn("cn=bob,dc=example,dc=com", 1); /* Bad DN value */ var_dump(ldap_dn2ufn("bob,dc=example,dc=com")); echo "Done\n";
/** * Convert DN to User Friendly Naming format * * @param string $dn The distinguished name of an LDAP entity * @return string The user friendly name */ public static function dnToUfn($dn) { return ldap_dn2ufn($dn); }
/** * @link http://php.net/manual/en/function.ldap-dn2fn.php * @param $dn * @return string */ public function dn2fn($dn) { return ldap_dn2ufn($dn); }
/** * @brief creates a hopefully unique name for owncloud based on the display name and the dn of the LDAP object * @param $name the display name of the object * @param $dn the dn of the object * @returns string with with the name to use in ownCloud * * creates a hopefully unique name for owncloud based on the display name and the dn of the LDAP object */ private function alternateOwnCloudName($name, $dn) { $ufn = ldap_dn2ufn($dn); $name = $name . '@' . trim(\OCP\Util::mb_substr_replace($ufn, '', 0, mb_strpos($ufn, ',', 0, 'UTF-8'), 'UTF-8')); $name = $this->sanitizeUsername($name); return $name; }
private static function _create_ldap_groups($groups, $username) { db_update("simple_sys_groups", array("members" => "replace(members,'|" . sql_quote($username) . "|','|')"), array("members like @username@", "createdby='auth_ldap'"), array("username" => "%|" . $username . "|%"), array("quote" => false)); foreach ($groups as $group) { $group = ldap_dn2ufn($group); $group = substr($group, 0, strpos($group, ",")); if (empty($group)) { continue; } // decode 2-byte unicode characters $group = preg_replace("/\\\\([A-F0-9]{2})/e", 'chr(hexdec("\\1"))', $group); self::creategroup($group); self::addgroupmember(0, array("username" => $username), array($group)); } }
<?php // Define the dn $dn = "OU=People, OU=staff, DC=ad, DC=example, DC=com"; // Convert the DN to a user-friendly format echo ldap_dn2ufn($dn); ?>