Code

Updated defaults
[gosa.git] / gosa-core / include / functions.inc
index d02d7d46264fd15083e23f313b2ee7b1a9a8c636..d9d12986fd5aa5ba65846f3604594aa4025f3899 100644 (file)
@@ -1662,8 +1662,8 @@ function recurse($rule, $variables)
 function expand_id($rule, $attributes)
 {
   /* Check for id rule */
-  if(preg_match('/^id(:|#)\d+$/',$rule)){
-    return (array("\{$rule}"));
+  if(preg_match('/^id(:|#|!)\d+$/',$rule)){
+    return (array("{$rule}"));
   }
 
   /* Check for clean attribute */
@@ -1731,27 +1731,28 @@ function gen_uids($rule, $attributes)
   $proposed= recurse($stripped, $variables);
 
   /* Get list of used ID's */
-  $used= array();
   $ldap= $config->get_ldap_link();
   $ldap->cd($config->current['BASE']);
-  $ldap->search('(uid=*)');
-
-  while($attrs= $ldap->fetch()){
-    $used[]= $attrs['uid'][0];
-  }
 
   /* Remove used uids and watch out for id tags */
   $ret= array();
   foreach($proposed as $uid){
 
     /* Check for id tag and modify uid if needed */
-    if(preg_match('/\{id:\d+}/',$uid)){
-      $size= preg_replace('/^.*{id:(\d+)}.*$/', '\\1', $uid);
+    if(preg_match('/\{id(:|!)\d+}/',$uid, $m)){
+      $size= preg_replace('/^.*{id(:|!)(\d+)}.*$/', '\\2', $uid);
+
+      $start= $m[1]==":"?0:-1;
+      for ($i= $start, $p= pow(10,$size); $i < $p; $i++){
+        if ($i == -1) {
+          $number= "";
+        } else {
+          $number= sprintf("%0".$size."d", $i+1);
+        }
+        $res= preg_replace('/{id(:|!)\d+}/', $number, $uid);
 
-      for ($i= 0, $p= pow(10,$size); $i < $p; $i++){
-        $number= sprintf("%0".$size."d", $i);
-        $res= preg_replace('/{id:(\d+)}/', $number, $uid);
-        if (!in_array($res, $used)){
+        $ldap->search("(uid=".preg_replace('/[{}]/', '', $res).")",array('dn'));
+        if($ldap->count() == 0){
           $uid= $res;
           break;
         }
@@ -1765,7 +1766,8 @@ function gen_uids($rule, $attributes)
         mt_srand((double) microtime()*1000000);
         $number= sprintf("%0".$size."d", mt_rand(0, pow(10, $size)-1));
         $res= preg_replace('/{id#(\d+)}/', $number, $uid);
-        if (!in_array($res, $used)){
+        $ldap->search("(uid=".preg_replace('/[{}]/', '', $res).")",array('dn'));
+        if($ldap->count() == 0){
           $uid= $res;
           break;
         }
@@ -1773,7 +1775,8 @@ function gen_uids($rule, $attributes)
     }
 
     /* Don't assign used ones */
-    if (!in_array($uid, $used)){
+    $ldap->search("(uid=".preg_replace('/[{}]/', '', $uid).")",array('dn'));
+    if($ldap->count() == 0){
       /* Add uid, but remove {} first. These are invalid anyway. */
       $ret[]= preg_replace('/[{}]/', '', $uid);
     }