Code

Some backport from trunk
[gosa.git] / gosa-core / include / functions.inc
index b431063863966261ed028a83fab0b8fc0773b760..d14308bcb45988897ccb484c7606655a47c14ebe 100644 (file)
 /*! \file
  * Common functions and named definitions. */
 
+/* Define globals for revision comparing */
+$svn_path = '$HeadURL$';
+$svn_revision = '$Revision$';
+
 /* Configuration file location */
 if(!isset($_SERVER['CONFIG_DIR'])){
   define ("CONFIG_DIR", "/etc/gosa");
@@ -68,12 +72,8 @@ define('DES_CBC_MD5',3);
 define('DES3_CBC_MD5',5);
 define('DES3_CBC_SHA1',16);
 
-/* Define globals for revision comparing */
-$svn_path = '$HeadURL$';
-$svn_revision = '$Revision$';
-
 /* Include required files */
-require_once("class_location.inc");
+include_once("class_location.inc");
 require_once ("functions_debug.inc");
 require_once ("accept-to-gettext.inc");
 
@@ -701,17 +701,27 @@ function ldap_expired_account($config, $userdn, $uid)
 
     // Fetch required attributes
     foreach(array('shadowExpire','shadowLastChange','shadowMax','shadowMin',
-                'shadowInactive','shadowWarning') as $attr){
+                'shadowInactive','shadowWarning','sambaKickoffTime') as $attr){
         $$attr = (isset($attrs[$attr][0]))? $attrs[$attr][0] : null;
     }
 
 
+    // Check if the account has reached its kick off limitations.
+    // ---------------------------------------------------------
+    // Once the accout reaches the kick off limit it has expired.
+    if($sambaKickoffTime !== null){
+        if(time() >= $sambaKickoffTime){
+            return(POSIX_ACCOUNT_EXPIRED);
+        }
+    }
+
+
     // Check if the account has expired.
     // ---------------------------------
     // An account is locked/expired once its expiration date has reached (shadowExpire).
     // If the optional attribute (shadowInactive) is set, we've to postpone
     //  the account expiration by the amount of days specified in (shadowInactive).
-    if($shadowExpire != null && $shadowExpire >= $current){
+    if($shadowExpire != null && $shadowExpire <= $current){
 
         // The account seems to be expired, but we've to check 'shadowInactive' additionally.
         // ShadowInactive specifies an amount of days we've to reprieve the user.
@@ -1440,7 +1450,9 @@ function get_ou($class,$name)
     global $config;
 
     if(!$config->configRegistry->propertyExists($class,$name)){
-        trigger_error("No department mapping found for type ".$name);
+        if($config->boolValueIsTrue("core","developmentMode")){
+            trigger_error("No department mapping found for type ".$name);
+        }
         return "";
     }
 
@@ -1981,72 +1993,6 @@ function netmask_to_bits($netmask)
 }
 
 
-/*! \brief Recursion helper for gen_id() */
-function recurse($rule, $variables)
-{
-  $result= array();
-
-  if (!count($variables)){
-    return array($rule);
-  }
-
-  reset($variables);
-  $key= key($variables);
-  $val= current($variables);
-  unset ($variables[$key]);
-
-  foreach($val as $possibility){
-    $nrule= str_replace("{$key}", $possibility, $rule);
-    $result= array_merge($result, recurse($nrule, $variables));
-  }
-
-  return ($result);
-}
-
-
-/*! \brief Expands user ID based on possible rules
- *
- *  Unroll given rule string by filling in attributes.
- *
- * \param string 'rule' The rule string from gosa.conf.
- * \param array 'attributes' A dictionary of attribute/value mappings
- * \return string Expanded string, still containing the id keyword.
- */
-function expand_id($rule, $attributes)
-{
-  /* Check for id rule */
-  if(preg_match('/^id(:|#|!)\d+$/',$rule)){
-    return (array("{$rule}"));
-  }
-
-  /* Check for clean attribute */
-  if (preg_match('/^%[a-zA-Z0-9]+$/', $rule)){
-    $rule= preg_replace('/^%/', '', $rule);
-    $val= rewrite(str_replace(' ', '', strtolower($attributes[$rule])));
-    return (array($val));
-  }
-
-  /* Check for attribute with parameters */
-  if (preg_match('/^%[a-zA-Z0-9]+\[[0-9-]+\]$/', $rule)){
-    $param= preg_replace('/^[^[]+\[([^]]+)]$/', '\\1', $rule);
-    $part= preg_replace('/^%/', '', preg_replace('/\[.*$/', '', $rule));
-    $val= rewrite(str_replace(' ', '', strtolower($attributes[$part])));
-    $start= preg_replace ('/-.*$/', '', $param);
-    $stop = preg_replace ('/^[^-]+-/', '', $param);
-
-    /* Assemble results */
-    $result= array();
-    for ($i= $start; $i<= $stop; $i++){
-      $result[]= substr($val, 0, $i);
-    }
-    return ($result);
-  }
-
-  echo "Error in idGenerator string: don't know how to handle rule $rule.\n";
-  return (array($rule));
-}
-
-
 /*! \brief Convert various data sizes to bytes
  *
  * Given a certain value in the format n(g|m|k), where n
@@ -2944,9 +2890,14 @@ function change_password ($dn, $password, $mode=FALSE, $hash= "", $old_password
         $attrs= array();
         if (!$mode && !empty($tmp)){
             $attrs= generate_smb_nt_hash($password);
-            $shadow = (isset($attrs["shadowLastChange"][0]))?(int)(date("U") / 86400):0;
-            if ($shadow != 0){
-                $attrs['shadowLastChange']= $shadow;
+            if(!count($attrs) || !is_array($attrs)){
+                msg_dialog::display(tr("Error"),tr("Cannot generate SAMBA hash!"),ERROR_DIALOG);
+                return(FALSE);    
+            }else{
+                $shadow = (isset($attrs["shadowLastChange"][0]))?(int)(date("U") / 86400):0;
+                if ($shadow != 0){
+                    $attrs['shadowLastChange']= $shadow;
+                }
             }
         }
 
@@ -3100,7 +3051,7 @@ function generate_smb_nt_hash($password)
     $hash = $rpc->mksmbhash($password);
     if(!$rpc->success()){
         msg_dialog::display(_("Error"),msgPool::rpcError($rpc->get_error()),ERROR_DIALOG);
-        return("");
+        return(array());
     }
 
   }elseif ($config->get_cfg_value("core","gosaSupportURI") != ""){
@@ -3130,7 +3081,7 @@ function generate_smb_nt_hash($password)
 
     if ($hash == "") {
       msg_dialog::display(_("Configuration error"), sprintf(_("Generating SAMBA hash by running %s failed: check %s!"), bold($config->get_cfg_value("core",'sambaHashHook'), bold("sambaHashHook"))), ERROR_DIALOG);
-      return ("");
+      return(array());
     }
   }
 
@@ -3587,6 +3538,9 @@ function image($path, $action= "", $title= "", $align= "middle")
   $label= null;
 
   // Bail out, if there's no style file
+  if(!class_exists('session')){
+    return "";    
+  }
   if(!session::global_is_set("img-styles")){
 
     // Get theme
@@ -3678,8 +3632,8 @@ function detectLdapSpecialCharHandling()
 
     // In the DN we've to use escaped characters, but the object name (o)
     //  has the be un-escaped.
-    $name = 'GOsaLdapEncoding_,_"_(_)_+';
-    $dnName = 'GOsaLdapEncoding_\,_\"_(_)_\+';
+    $name = 'GOsaLdapEncoding_,_"_(_)_+_/';
+    $dnName = 'GOsaLdapEncoding_\,_\"_(_)_\+_/';
    
     // Prapare name to be useable in filters
     $fixed= normalizeLdap(str_replace('\\\\', '\\\\\\', $name));
@@ -3771,11 +3725,14 @@ function fillReplacements($str, $attrs, $shellArg = FALSE, $default = "")
     }
     krsort($hits);
 
+    // Add lower case placeholders to avoid errors
+    foreach($attrs as $key => $attr) $attrs[strtolower($key)] = $attr;
+
     // Replace the placeholder in the given string now.
     foreach($hits as $match){
 
         // Avoid errors about undefined index.
-        $name = $match[2];
+        $name = strtolower($match[2]);
         if(!isset($attrs[$name])) $attrs[$name] = $default;
 
         // Calculate the replacement
@@ -3833,10 +3790,11 @@ function gen_uids($rule, $attributes)
         if(!isset($match[5])){
             $replacements[$match[0]][] = $attributes[$match[2]];
     
-        // Start given but no end, so just add a simple character
+        // Start given but no end, so just add a single character
         }elseif(!isset($match[7])){
             if(isset($attributes[$match[2]][$match[5]])){
-                $replacements[$match[0]][] = $attributes[$match[2]][$match[5]];
+                $tmp = " ".$attributes[$match[2]];
+                $replacements[$match[0]][] = trim($tmp[$match[5]]);
             }
 
         // Add all values in range
@@ -3844,8 +3802,9 @@ function gen_uids($rule, $attributes)
             $str = "";
             for($i=$match[5]; $i<= $match[7]; $i++){
                 if(isset($attributes[$match[2]][$i])){
-                    $str .= $attributes[$match[2]][$i];
-                    $replacements[$match[0]][] = $str;
+                    $tmp = " ".$attributes[$match[2]];
+                    $str .= $tmp[$i];
+                    $replacements[$match[0]][] = trim($str);
                 }
             }
         }
@@ -3915,5 +3874,53 @@ function gen_uid_proposals(&$rules, $tag, $values)
     return($newRules);
 }
 
+
+function gen_uuid() 
+{
+    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
+        // 32 bits for "time_low"
+        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
+
+        // 16 bits for "time_mid"
+        mt_rand( 0, 0xffff ),
+
+        // 16 bits for "time_hi_and_version",
+        // four most significant bits holds version number 4
+        mt_rand( 0, 0x0fff ) | 0x4000,
+
+        // 16 bits, 8 bits for "clk_seq_hi_res",
+        // 8 bits for "clk_seq_low",
+        // two most significant bits holds zero and one for variant DCE1.1
+        mt_rand( 0, 0x3fff ) | 0x8000,
+
+        // 48 bits for "node"
+        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
+    );
+}
+
+function gosa_file_name($filename)
+{
+    $tempfile = tempnam(sys_get_temp_dir(), 'GOsa'); 
+    if(move_uploaded_file($filename, $tempfile)){ 
+       return( $tempfile);
+    }
+}
+
+function gosa_file($filename)
+{
+    $tempfile = tempnam(sys_get_temp_dir(), 'GOsa'); 
+    if(move_uploaded_file($filename, $tempfile)){ 
+       return file( $tempfile );
+    }
+}
+
+function gosa_fopen($filename, $mode)
+{
+    $tempfile = tempnam(sys_get_temp_dir(), 'GOsa'); 
+    if(move_uploaded_file($filename, $tempfile)){ 
+       return fopen( $tempfile, $mode );
+    }
+}
+
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>