Code

Added a new function that allows updating the accessTo attribute
[gosa.git] / gosa-core / include / functions.inc
index a8e8c29f05774f2ee21af840c47a2c6793baf3a7..7675ff23e7d502e8c01a834a67a6a615eb9915ef 100644 (file)
@@ -22,9 +22,8 @@
 
 /* Configuration file location */
 define ("CONFIG_DIR", "/etc/gosa");
-define ("CONFIG_FILE", "gosa.conf-trunk");
+define ("CONFIG_FILE", "gosa.conf");
 define ("CONFIG_TEMPLATE_DIR", "../contrib/");
-define ("HELP_BASEDIR", "/var/www/doc/");
 
 /* Define get_list flags */
 define("GL_NONE",         0);
@@ -617,7 +616,7 @@ function add_lock ($object, $user)
     $attrs["cn"] = "$name";
     $ldap->add($attrs);
     if (!$ldap->success()){
-      msg_dialog::display(_("Internal error"), sprintf(_("Adding a lock failed.")."<br><br>"._('LDAP server returned: %s'), "<br><br><i>".$ldap->get_error()."</i>"), ERROR_DIALOG);
+      msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), "cn=$name,".$config->current['CONFIG'], 0, ERROR_DIALOG));
       return;
     }
   }
@@ -1153,8 +1152,8 @@ function get_ou($name)
                 "mimetypeou"    => "ou=mime,");
 
   /* Preset ou... */
-  if (isset($config->current[$name])){
-    $ou= $config->current[$name];
+  if (isset($config->current[strtoupper($name)])){
+    $ou= $config->current[strtoupper($name)];
   } elseif (isset($map[$name])) {
     $ou = $map[$name];
     return($ou);
@@ -1166,10 +1165,17 @@ function get_ou($name)
  
   if ($ou != ""){
     if (!preg_match('/^[^=]+=[^=]+/', $ou)){
-      return @LDAP::convert("ou=$ou,");
+      $ou = @LDAP::convert("ou=$ou");
     } else {
-      return @LDAP::convert("$ou,");
+      $ou = @LDAP::convert("$ou");
     }
+
+    if(preg_match("/".normalizePreg($config->current['BASE'])."$/",$ou)){
+      return($ou);
+    }else{
+      return("$ou,");
+    }
+  
   } else {
     return "";
   }
@@ -1230,48 +1236,6 @@ function get_uid_regexp()
 }
 
 
-function print_red()
-{
-  trigger_error("Use of obsolete print_red");
-  /* Check number of arguments */
-  if (func_num_args() < 1){
-    return;
-  }
-
-  /* Get arguments, save string */
-  $array = func_get_args();
-  $string= $array[0];
-
-  /* Step through arguments */
-  for ($i= 1; $i<count($array); $i++){
-    $string= preg_replace ("/%s/", $array[$i], $string, 1);
-  }
-
-  /* If DEBUGLEVEL is set, we're in web mode, use textual output in
-     the other case... */
-  if($string !== NULL){
-    if (preg_match("/"._("LDAP error:")."/", $string)){
-      $addmsg= _("Problems with the LDAP server mean that you probably lost the last changes. Please check your LDAP setup for possible errors and try again.");
-    } else {
-      if (!preg_match('/[.!?]$/', $string)){
-        $string.= ".";
-      }
-      $string= preg_replace('/<br>/', ' ', $string);
-      $addmsg= _("Please check your input and fix the error. Press 'OK' to close this message box.");
-      $addmsg = "";
-    }
-    if(empty($addmsg)){
-      $addmsg = _("Error");
-    }
-    msg_dialog::display($addmsg, $string,ERROR_DIALOG);
-    return;
-  }else{
-    return;
-  }
-
-}
-
-
 function gen_locked_message($user, $dn)
 {
   global $plug, $config;
@@ -1326,7 +1290,7 @@ function gen_locked_message($user, $dn)
   } else {
     $smarty->assign ("action", _("Edit anyway"));
   }
-  $smarty->assign ("message", sprintf(_("You're going to edit the LDAP entry/entries '%s'"), "<b>".$msg."</b>", ""));
+  $smarty->assign ("message", sprintf(_("You're going to edit the LDAP entry/entries %s"), "<b>".$msg."</b>", ""));
 
   return ($smarty->fetch (get_template_path('islocked.tpl')));
 }
@@ -1547,7 +1511,7 @@ function apply_filter()
 function back_to_main()
 {
   $string= '<br><p class="plugbottom"><input type=submit name="password_back" value="'.
-    _("Back").'"></p><input type="hidden" name="ignore">';
+    msgPool::backButton().'"></p><input type="hidden" name="ignore">';
 
   return ($string);
 }
@@ -2209,17 +2173,17 @@ function get_base_from_hook($dn, $attrib)
         if (preg_match("/^[0-9]+$/", $output[0])){
           return ($output[0]);
         } else {
-          msg_dialog::display(_("Warning"), _("'base_hook' is not available. Using default base."), WARNING_DIALOG);
+          msg_dialog::display(_("Warning"), _("'base_hook' is not available. Using default base!"), WARNING_DIALOG);
           return ($config->current['UIDBASE']);
         }
       } else {
-        msg_dialog::display(_("Warning"), _("'base_hook' is not available. Using default base."), WARNING_DIALOG);
+        msg_dialog::display(_("Warning"), _("'base_hook' is not available. Using default base!"), WARNING_DIALOG);
         return ($config->current['UIDBASE']);
       }
 
     } else {
 
-      msg_dialog::display(_("Warning"), _("'base_hook' is not available. Using default base."), WARNING_DIALOG);
+      msg_dialog::display(_("Warning"), _("'base_hook' is not available. Using default base!"), WARNING_DIALOG);
       return ($config->current['UIDBASE']);
 
     }
@@ -2462,8 +2426,8 @@ function change_password ($dn, $password, $mode=0, $hash= "")
   // Get all available encryption Methods
 
   // NON STATIC CALL :)
-  $tmp = new passwordMethod(session::get('config'));
-  $available = $tmp->get_available_methods();
+  $methods = new passwordMethod(session::get('config'));
+  $available = $methods->get_available_methods();
 
   // read current password entry for $dn, to detect the encryption Method
   $ldap       = $config->get_ldap_link();
@@ -2490,11 +2454,12 @@ function change_password ($dn, $password, $mode=0, $hash= "")
 
     /* Extract used hash */
     if ($hash == ""){
-      $hash= strtolower($matches[1]);
+      $test = passwordMethod::get_method($attrs['userPassword'][0]);
+    } else {
+      $test = new $available[$hash]($config);
+      $test->set_hash($hash);
     }
 
-    $test = new  $available[$hash]($config);
-
   } else {
     // User MD5 by default
     $hash= "md5";
@@ -2528,7 +2493,7 @@ function change_password ($dn, $password, $mode=0, $hash= "")
     $attrs= generate_smb_nt_hash($password);
   }
 
- /* Readd ! if user was deactivated */
+ /* Read ! if user was deactivated */
   if($deactivated){
     $newpass = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$newpass);
   }
@@ -2574,8 +2539,12 @@ function generate_smb_nt_hash($password)
 
   # Try to use gosa-si?
   if (isset($config->current['GOSA_SI'])){
-       $res= gosaSupportDaemon::send("gosa_gen_smb_hash", "GOSA", array("password" => $password), TRUE);
-       $hash= $res['XML']['HASH'];
+       $res= gosaSupportDaemon::send("gosa_gen_smb_hash", "GOSA", array("password" => $password), TRUE);
+    if (isset($res['XML']['HASH'])){
+       $hash= $res['XML']['HASH'];
+    } else {
+      $hash= "";
+    }
   } else {
          $tmp= $config->data['MAIN']['SMBHASH']." ".escapeshellarg($password);
          @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $tmp, "Execute");
@@ -2608,12 +2577,6 @@ function generate_smb_nt_hash($password)
 }
 
 
-function crypt_single($string,$enc_type )
-{
-  return( passwordMethod::crypt_single_str($string,$enc_type));
-}
-
-
 function getEntryCSN($dn)
 {
   global $config;
@@ -2707,11 +2670,66 @@ function send_binary_content($data,$name,$type = "application/octet-stream")
            HTML output, without breaking quotes.
     @param  The String we want to encode.
     @return The encoded String
-*/
+ */
 function xmlentities($str)
+{ 
+  if(is_string($str)){
+    return(htmlentities($str,ENT_QUOTES));
+  }elseif(is_array($str)){
+    foreach($str as $name => $value){
+      $str[$name] = xmlentities($value);
+    }
+  }
+  return($str);
+}
+
+
+/*! \brief  Updates all accessTo attributes from a given value to a new one.
+            For example if a host is renamed.
+    @param  String  $from The source accessTo name.
+    @param  String  $to   The destination accessTo name.
+*/
+function update_accessTo($from,$to)
 {
-  return (htmlentities($str,ENT_QUOTES));
+  global $config;
+  $ldap = $config->get_ldap_link();
+  $ldap->cd($config->current['BASE']);
+  $ldap->search("(&(objectClass=trustAccount)(accessTo=".$from."))",array("objectClass","accessTo"));
+  while($attrs = $ldap->fetch()){
+    $new_attrs = array();
+    $dn = $attrs['dn'];
+    for($i = 0 ; $i < $attrs['objectClass']['count']; $i++){
+      $new_attrs['objectClass'][] =  $attrs['objectClass'][$i];
+    }
+    for($i = 0 ; $i < $attrs['accessTo']['count']; $i++){
+      if($attrs['accessTo'][$i] == $from){
+        if(!empty($to)){
+          $new_attrs['accessTo'][] =  $to;
+        }
+      }else{
+        $new_attrs['accessTo'][] =  $attrs['accessTo'][$i]; 
+      }
+    }
+    $ldap->cd($dn);
+    $ldap->modify($new_attrs);
+    if (!$ldap->success()){
+      msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_MOD, "update_accessTo($from,$to)"));
+    }
+    new log("modify","update_accessTo($from,$to)",$dn,array_keys($new_attrs),$ldap->get_error());
+  }
 }
 
+
+function get_random_char () {
+     $randno = rand (0, 63);
+     if ($randno < 12) {
+         return (chr ($randno + 46)); // Digits, '/' and '.'
+     } else if ($randno < 38) {
+         return (chr ($randno + 53)); // Uppercase
+     } else {
+         return (chr ($randno + 59)); // Lowercase
+     }
+  }
+
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>