Code

Updated read only handling for locks.
[gosa.git] / gosa-core / include / functions.inc
index b635eb88f101efd8950d6188c2d6dfe1922af24a..ba46e840db640eba6469a22e0dd6418e9ac46c74 100644 (file)
@@ -120,8 +120,8 @@ function __autoload($class_name) {
            exit;
     }
 
-    if (isset($class_mapping[$class_name])){
-      require_once($BASE_DIR."/".$class_mapping[$class_name]);
+    if (isset($class_mapping["$class_name"])){
+      require_once($BASE_DIR."/".$class_mapping["$class_name"]);
     } else {
       echo sprintf(_("Fatal error: cannot instantiate class '%s' - try running '%s' to fix this"), $class_name, "<b>update-gosa</b>");
       exit;
@@ -281,31 +281,14 @@ function get_template_path($filename= '', $plugin= FALSE, $path= "")
 
 function array_remove_entries($needles, $haystack)
 {
-  $tmp= array();
-
-  /* Loop through entries to be removed */
-  foreach ($haystack as $entry){
-    if (!in_array($entry, $needles)){
-      $tmp[]= $entry;
-    }
-  }
-
-  return ($tmp);
+  return (array_merge(array_diff($haystack, $needles)));
 }
 
 
 function array_remove_entries_ics($needles, $haystack)
 {
-  $tmp= array();
-
-  /* Loop through entries to be removed */
-  foreach ($haystack as $entry){
-    if (!in_array_ics($entry, $needles)){
-      $tmp[]= $entry;
-    }
-  }
-
-  return ($tmp);
+  // strcasecmp will work, because we only compare ASCII values here
+  return (array_merge(array_udiff($haystack, $needles, 'strcasecmp')));
 }
 
 
@@ -612,10 +595,28 @@ function ldap_expired_account($config, $userdn, $username)
 }
 
 
-function add_lock ($object, $user)
+function add_lock($object, $user)
 {
   global $config;
 
+  /* Remember which entries were opened as read only, because we 
+      don't need to remove any locks for them later.
+   */
+  if(!session::is_set("LOCK_CACHE")){
+    session::set("LOCK_CACHE",array(""));
+  }
+  $cache = &session::get("LOCK_CACHE");
+  if(isset($_POST['open_readonly'])){
+    $cache['READ_ONLY'][$object] = TRUE;
+    echo "ADDED : {$user}:{$object}<br>";
+    return;
+  }
+  if(isset($cache['READ_ONLY'][$object])){
+    echo "Removed lock entry $object <br>";
+    unset($cache['READ_ONLY'][$object]);
+  }
+
+
   if(is_array($object)){
     foreach($object as $obj){
       add_lock($obj,$user);
@@ -673,6 +674,21 @@ function del_lock ($object)
     return;
   }
 
+  /* If this object was opened in read only mode then 
+      skip removing the lock entry, there wasn't any lock created.
+    */
+  if(session::is_set("LOCK_CACHE")){
+    $cache = &session::get("LOCK_CACHE");
+    if(isset($cache['READ_ONLY'][$object])){
+      if(isset($_POST['delete_lock'])){
+        unset($cache['READ_ONLY'][$object]);
+      }else{
+        echo "Skipped: $object <br>";
+        return;
+      }
+    }
+  }
+
   /* Check for existance and remove the entry */
   $ldap= $config->get_ldap_link();
   $ldap->cd ($config->get_cfg_value("config"));
@@ -715,6 +731,9 @@ function get_lock ($object)
     return("");
   }
 
+  /* Allow readonly access, the plugin::plugin will restrict the acls */
+  if(isset($_POST['open_readonly'])) return("");
+
   /* Get LDAP link, check for presence of the lock entry */
   $user= "";
   $ldap= $config->get_ldap_link();
@@ -870,7 +889,7 @@ function get_sub_list($filter, $category,$sub_deps, $base= "", $attributes= arra
        *  departments like this "ou=servers,ou=blafasel,..."
        * Here we filter out those blafasel departments.
        */
-      if(preg_match("/".normalizePreg($sub_dep)."/",$attrs['dn'])){
+      if(preg_match("/".preg_quote($sub_dep, '/')."/",$attrs['dn'])){
         $departments[$attrs['dn']] = $attrs['dn'];
         break;
       }
@@ -1088,7 +1107,7 @@ function getMenuCache()
       if(session::is_set('maxC')){
         $b= session::get('maxC');
         $q= "";
-        for ($m=0;$m<strlen($b);$m++) {
+        for ($m=0, $l= strlen($b);$m<$l;$m++) {
           $q.= $b[$m++];
         }
         msg_dialog::display(_("Internal error"), base64_decode($q), ERROR_DIALOG);
@@ -1124,7 +1143,7 @@ function convert_department_dn($dn, $base = NULL)
 
   /* Build a sub-directory style list of the tree level
      specified in $dn */
-  $dn = preg_replace("/".normalizePreg($base)."$/i","",$dn);
+  $dn = preg_replace("/".preg_quote($base, '/')."$/i","",$dn);
   if(empty($dn)) return("/");
 
 
@@ -1142,7 +1161,7 @@ function convert_department_dn($dn, $base = NULL)
  * style value. It removes the trailing '/', too. */
 function get_sub_department($value)
 {
-  return (@LDAP::fix(preg_replace("%^.*/([^/]+)/?$%", "\\1", $value)));
+  return (LDAP::fix(preg_replace("%^.*/([^/]+)/?$%", "\\1", $value)));
 }
 
 
@@ -1150,37 +1169,37 @@ function get_ou($name)
 {
   global $config;
 
-  $name= strtolower($name);
-
   $map = array( 
-                "ogroupou"      => "ou=groups,",
-                "applicationou" => "ou=apps,",
-                "systemsou"     => "ou=systems,",
+                "ogroupRDN"      => "ou=groups,",
+                "applicationRDN" => "ou=apps,",
+                "systemRDN"     => "ou=systems,",
                 "serverRDN"      => "ou=servers,ou=systems,",
                 "terminalRDN"    => "ou=terminals,ou=systems,",
-                "workstationou" => "ou=workstations,ou=systems,",
-                "printerou"     => "ou=printers,ou=systems,",
-                "phoneou"       => "ou=phones,ou=systems,",
-                "componentou"   => "ou=netdevices,ou=systems,",
+                "workstationRDN" => "ou=workstations,ou=systems,",
+                "printerRDN"     => "ou=printers,ou=systems,",
+                "phoneRDN"       => "ou=phones,ou=systems,",
+                "componentRDN"   => "ou=netdevices,ou=systems,",
                 "sambaMachineAccountRDN"   => "ou=winstation,",
 
-                "blocklistou"   => "ou=gofax,ou=systems,",
-                "incomingou"    => "ou=incoming,",
-                "aclroleou"     => "ou=aclroles,",
-                "macroou"       => "ou=macros,ou=asterisk,ou=configs,ou=systems,",
-                "conferenceou"  => "ou=conferences,ou=asterisk,ou=configs,ou=systems,",
+                "faxBlocklistRDN"   => "ou=gofax,ou=systems,",
+                "systemIncomingRDN"    => "ou=incoming,",
+                "aclRoleRDN"     => "ou=aclroles,",
+                "phoneMacroRDN"       => "ou=macros,ou=asterisk,ou=configs,ou=systems,",
+                "phoneConferenceRDN"  => "ou=conferences,ou=asterisk,ou=configs,ou=systems,",
 
-                "faiou"         => "ou=fai,ou=configs,ou=systems,",
-                "faiscriptou"   => "ou=scripts,",
-                "faihookou"     => "ou=hooks,",
-                "faitemplateou" => "ou=templates,",
-                "faivariableou" => "ou=variables,",
-                "faiprofileou"  => "ou=profiles,",
-                "faipackageou"  => "ou=packages,",
-                "faipartitionou"=> "ou=disk,",
+                "faiBaseRDN"         => "ou=fai,ou=configs,ou=systems,",
+                "faiScriptRDN"   => "ou=scripts,",
+                "faiHookRDN"     => "ou=hooks,",
+                "faiTemplateRDN" => "ou=templates,",
+                "faiVariableRDN" => "ou=variables,",
+                "faiProfileRDN"  => "ou=profiles,",
+                "faiPackageRDN"  => "ou=packages,",
+                "faiPartitionRDN"=> "ou=disk,",
 
-                "deviceou"      => "ou=devices,",
-                "mimetypeou"    => "ou=mime,");
+                "sudoRDN"       => "ou=sudoers,",
+
+                "deviceRDN"      => "ou=devices,",
+                "mimetypeRDN"    => "ou=mime,");
 
   /* Preset ou... */
   if ($config->get_cfg_value($name, "_not_set_") != "_not_set_"){
@@ -1201,7 +1220,7 @@ function get_ou($name)
       $ou = @LDAP::convert("$ou");
     }
 
-    if(preg_match("/".normalizePreg($config->current['BASE'])."$/",$ou)){
+    if(preg_match("/".preg_quote($config->current['BASE'], '/')."$/",$ou)){
       return($ou);
     }else{
       return("$ou,");
@@ -1215,19 +1234,19 @@ function get_ou($name)
 
 function get_people_ou()
 {
-  return (get_ou("USERRDN"));
+  return (get_ou("userRDN"));
 }
 
 
 function get_groups_ou()
 {
-  return (get_ou("GROUPRDN"));
+  return (get_ou("groupRDN"));
 }
 
 
 function get_winstations_ou()
 {
-  return (get_ou("SAMBAMACHINEACCOUNTRDN"));
+  return (get_ou("sambaMachineAccountRDN"));
 }
 
 
@@ -1235,7 +1254,7 @@ function get_base_from_people($dn)
 {
   global $config;
 
-  $pattern= "/^[^,]+,".preg_quote(get_people_ou())."/i";
+  $pattern= "/^[^,]+,".preg_quote(get_people_ou(), '/')."/i";
   $base= preg_replace($pattern, '', $dn);
 
   /* Set to base, if we're not on a correct subtree */
@@ -1362,7 +1381,7 @@ function rewrite($s)
   global $REWRITE;
 
   foreach ($REWRITE as $key => $val){
-    $s= preg_replace("/$key/", "$val", $s);
+    $s= str_replace("$key", "$val", $s);
   }
 
   return ($s);
@@ -1613,7 +1632,7 @@ function recurse($rule, $variables)
   unset ($variables[$key]);
 
   foreach($val as $possibility){
-    $nrule= preg_replace("/\{$key\}/", $possibility, $rule);
+    $nrule= str_replace("{$key}", $possibility, $rule);
     $result= array_merge($result, recurse($nrule, $variables));
   }
 
@@ -1631,7 +1650,7 @@ function expand_id($rule, $attributes)
   /* Check for clean attribute */
   if (preg_match('/^%[a-zA-Z0-9]+$/', $rule)){
     $rule= preg_replace('/^%/', '', $rule);
-    $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$rule])));
+    $val= rewrite(str_replace(' ', '', strtolower($attributes[$rule])));
     return (array($val));
   }
 
@@ -1639,7 +1658,7 @@ function expand_id($rule, $attributes)
   if (preg_match('/^%[a-zA-Z0-9]+\[[0-9-]+\]$/', $rule)){
     $param= preg_replace('/^[^[]+\[([^]]+)]$/', '\\1', $rule);
     $part= preg_replace('/^%/', '', preg_replace('/\[.*$/', '', $rule));
-    $val= rewrite(preg_replace('/ /', '', strtolower($attributes[$part])));
+    $val= rewrite(str_replace(' ', '', strtolower($attributes[$part])));
     $start= preg_replace ('/-.*$/', '', $param);
     $stop = preg_replace ('/^[^-]+-/', '', $param);
 
@@ -1667,7 +1686,7 @@ function gen_uids($rule, $attributes)
   $stripped= "";
   $variables= array();
 
-  for ($pos= 0; $pos < strlen($rule); $pos++){
+  for ($pos= 0, $l= strlen($rule); $pos < $l; $pos++){
 
     if ($rule[$pos] == "{" ){
       $trigger= true;
@@ -1710,7 +1729,7 @@ function gen_uids($rule, $attributes)
     if(preg_match('/\{id:\d+}/',$uid)){
       $size= preg_replace('/^.*{id:(\d+)}.*$/', '\\1', $uid);
 
-      for ($i= 0; $i < pow(10,$size); $i++){
+      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)){
@@ -1720,27 +1739,27 @@ function gen_uids($rule, $attributes)
       }
     }
 
-  if(preg_match('/\{id#\d+}/',$uid)){
-    $size= preg_replace('/^.*{id#(\d+)}.*$/', '\\1', $uid);
+    if(preg_match('/\{id#\d+}/',$uid)){
+      $size= preg_replace('/^.*{id#(\d+)}.*$/', '\\1', $uid);
 
-    while (true){
-      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)){
-        $uid= $res;
-        break;
+      while (true){
+        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)){
+          $uid= $res;
+          break;
+        }
       }
     }
-  }
 
-/* Don't assign used ones */
-if (!in_array($uid, $used)){
-  $ret[]= $uid;
-}
-}
+    /* Don't assign used ones */
+    if (!in_array($uid, $used)){
+      $ret[]= $uid;
+    }
+  }
 
-return(array_unique($ret));
+  return(array_unique($ret));
 }
 
 
@@ -1772,18 +1791,8 @@ function to_byte($value) {
 
 function in_array_ics($value, $items)
 {
-  if (!is_array($items)){
-    return (FALSE);
-  }
-
-  foreach ($items as $item){
-    if (strcasecmp($item, $value) == 0) {
-      return (TRUE);
-    }
-  }
-
-  return (FALSE);
-} 
+       return preg_grep('/^'.preg_quote($value, '/').'$/i', $items);
+}
 
 
 function generate_alphabet($count= 10)
@@ -1793,7 +1802,7 @@ function generate_alphabet($count= 10)
   $c= 0;
 
   /* Fill cells with charaters */
-  for ($i= 0; $i<mb_strlen($characters, 'UTF8'); $i++){
+  for ($i= 0, $l= mb_strlen($characters, 'UTF8'); $i<$l; $i++){
     if ($c == 0){
       $alphabet.= "<tr>";
     }
@@ -1819,7 +1828,7 @@ function generate_alphabet($count= 10)
 
 function validate($string)
 {
-  return (strip_tags(preg_replace('/\0/', '', $string)));
+  return (strip_tags(str_replace('\0', '', $string)));
 }
 
 
@@ -2016,17 +2025,13 @@ function progressbar($percentage,$width=100,$height=15,$showvalue=false)
 
 function array_key_ics($ikey, $items)
 {
-  /* Gather keys, make them lowercase */
-  $tmp= array();
-  foreach ($items as $key => $value){
-    $tmp[strtolower($key)]= $key;
-  }
-
-  if (isset($tmp[strtolower($ikey)])){
-    return($tmp[strtolower($ikey)]);
+  $tmp= array_change_key_case($items, CASE_LOWER);
+  $ikey= strtolower($ikey);
+  if (isset($tmp[$ikey])){
+    return($tmp[$ikey]);
   }
 
-  return ("");
+  return ('');
 }
 
 
@@ -2037,15 +2042,7 @@ function array_differs($src, $dst)
     return (TRUE);
   }
 
-  /* So the count is the same - lets check the contents */
-  $differs= FALSE;
-  foreach($src as $value){
-    if (!in_array($value, $dst)){
-      $differs= TRUE;
-    }
-  }
-
-  return ($differs);
+  return (count(array_diff($src, $dst)) != 0);
 }
 
 
@@ -2076,13 +2073,6 @@ function saveFilter($a_filter, $values)
 }
 
 
-/* Escape all preg_* relevant characters */
-function normalizePreg($input)
-{
-  return (addcslashes($input, '[]()|/.*+-'));
-}
-
-
 /* Escape all LDAP filter relevant characters */
 function normalizeLdap($input)
 {
@@ -2192,10 +2182,10 @@ function get_base_from_hook($dn, $attrib)
 {
   global $config;
 
-  if ($config->get_cfg_value("nextIdHook") != ""){
+  if ($config->get_cfg_value("baseIdHook") != ""){
     
     /* Call hook script - if present */
-    $command= $config->get_cfg_value("nextIdHook");
+    $command= $config->get_cfg_value("baseIdHook");
 
     if ($command != ""){
       $command.= " '".LDAP::fix($dn)."' $attrib";
@@ -2205,17 +2195,17 @@ function get_base_from_hook($dn, $attrib)
         if (preg_match("/^[0-9]+$/", $output[0])){
           return ($output[0]);
         } else {
-          msg_dialog::display(_("Warning"), _("'nextIdHook' is not available. Using default base!"), WARNING_DIALOG);
+          msg_dialog::display(_("Warning"), _("'baseIdHook' is not available. Using default base!"), WARNING_DIALOG);
           return ($config->get_cfg_value("uidNumberBase"));
         }
       } else {
-        msg_dialog::display(_("Warning"), _("'nextIdHook' is not available. Using default base!"), WARNING_DIALOG);
+        msg_dialog::display(_("Warning"), _("'baseIdHook' is not available. Using default base!"), WARNING_DIALOG);
         return ($config->get_cfg_value("uidNumberBase"));
       }
 
     } else {
 
-      msg_dialog::display(_("Warning"), _("'nextIdHook' is not available. Using default base!"), WARNING_DIALOG);
+      msg_dialog::display(_("Warning"), _("'baseIdHook' is not available. Using default base!"), WARNING_DIALOG);
       return ($config->get_cfg_value("uidNumberBase"));
 
     }
@@ -2373,7 +2363,7 @@ function get_languages($languages_in_own_language = FALSE,$strip_region_tag = FA
         "en_US" => "English",
         "nl_NL" => "Dutch",
         "pl_PL" => "Polish",
-        "sv_SE" => "Swedish",
+        #"sv_SE" => "Swedish",
         "zh_CN" => "Chinese",
         "vi_VN" => "Vietnamese",
         "ru_RU" => "Russian");
@@ -2386,7 +2376,7 @@ function get_languages($languages_in_own_language = FALSE,$strip_region_tag = FA
         "en_US" => _("English"),
         "nl_NL" => _("Dutch"),
         "pl_PL" => _("Polish"),
-        "sv_SE" => _("Swedish"),
+        #"sv_SE" => _("Swedish"),
         "zh_CN" => _("Chinese"),
         "vi_VN" => _("Vietnamese"),
         "ru_RU" => _("Russian"));