From b63628ebd06d7732f451e7fd52e2d284f1757b82 Mon Sep 17 00:00:00 2001 From: hickert Date: Tue, 5 Oct 2010 06:09:05 +0000 Subject: [PATCH] Updated Hook execution -Allow to use replacement slicing by using replacements like %var{n:m} git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@19912 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-core/include/class_plugin.inc | 5 +-- gosa-core/include/functions.inc | 37 +++++++++++++++++++ .../class_password-methods.inc | 28 ++++++++++---- 3 files changed, 59 insertions(+), 11 deletions(-) diff --git a/gosa-core/include/class_plugin.inc b/gosa-core/include/class_plugin.inc index 3c003145e..d7a97a60e 100644 --- a/gosa-core/include/class_plugin.inc +++ b/gosa-core/include/class_plugin.inc @@ -1636,10 +1636,7 @@ class plugin arsort($tmp); // Now replace the placeholder - foreach ($tmp as $name => $len){ - $value = $addAttrs[$name]; - $command= str_replace("%$name", "$value", $command); - } + $command = fillReplacements($command, $addAttrs, TRUE); // If there are still some %.. in our command, try to fill these with some other class vars if(preg_match("/%/",$command)){ diff --git a/gosa-core/include/functions.inc b/gosa-core/include/functions.inc index 6b1ad16a8..ae3624984 100644 --- a/gosa-core/include/functions.inc +++ b/gosa-core/include/functions.inc @@ -3859,5 +3859,42 @@ function detectLdapSpecialCharHandling() return(NULL); } + +/*! \brief Replaces placeholder in a given string. + * For example: + * '%uid@gonicus.de' Replaces '%uid' with 'uid'. + * '%uid{0}@gonicus.de' Replaces '%uid{0}' with the first char of 'uid'. + * '%uid{2:4}@gonicus.de' Replaces '%uid{2:4}' with three chars from 'uid' starting from the second. + * + * @param String The string to perform the action on. + * @param Array An array of replacements. + * @return The resulting string. + */ +function fillReplacements($str, $attrs, $shellArg = FALSE) +{ + // Search for '%...{...} + // Get all matching parts of the given string and sort them by + // length, to avoid replacing strings like '%uidNumber' with 'uid' + // instead of 'uidNumber'; The longest tring at first. + preg_match_all('/(%([a-z0-9]+)(\{(([0-9]+)(:([0-9]+))?)\})?)/i', $str ,$matches, PREG_SET_ORDER); + $hits = array(); + foreach($matches as $match){ + $hits[strlen($match[2]).$match[0]] = $match; + } + krsort($hits); + + // Replace the placeholder in the given string now. + foreach($hits as $match){ + $name = $match[2]; + $start = (isset($match[5])) ? $match[5] : 0; + $end = (isset($match[7])) ? $match[7] : strlen($attrs[$name]); + $value = substr($attrs[$name], $start, $end); + if($shellArg) $value = escapeshellarg($value); + $str = preg_replace("/".preg_quote($match[0],'/')."/", $value, $str); + } + return($str); +} + + // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: ?> diff --git a/gosa-core/include/password-methods/class_password-methods.inc b/gosa-core/include/password-methods/class_password-methods.inc index 9d1260bf2..695f32e71 100644 --- a/gosa-core/include/password-methods/class_password-methods.inc +++ b/gosa-core/include/password-methods/class_password-methods.inc @@ -108,13 +108,20 @@ class passwordMethod $userPassword = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$userPassword); $sambaLMPassword = preg_replace("/^[!]*(.*$)/","!\\1",$sambaLMPassword); $sambaNTPassword = preg_replace("/^[!]*(.*$)/","!\\1",$sambaNTPassword); - $ldap->cd($dn); - // Lock the account by modifying the password hash. + // Call external lock hook + $res = $ldap->cat($dn); + $hookAttrs = array(); + foreach($ldap->fetch() as $name => $value){ + if(is_numeric($name)) continue; + if(isset($value[0])) $hookAttrs[$name] = $value[0]; + if(isset($value) && is_string($value)) $hookAttrs[$name] = $value; + } $pwdClass = new password($config, $dn); - $pwdClass->callHook($pwdClass, 'PRELOCK',array(), $ret); + $pwdClass->callHook($pwdClass, 'PRELOCK',$hookAttrs, $ret); // Update the ldap entry + $ldap->cd($dn); $ldap->modify( array( "userPassword" => $userPassword, @@ -124,7 +131,7 @@ class passwordMethod if($ldap->success()){ // Call the password post-lock hook, if defined. - $pwdClass->callHook($pwdClass, 'POSTLOCK',array(), $ret); + $pwdClass->callHook($pwdClass, 'POSTLOCK',$hookAttrs, $ret); } return($ldap->success()); @@ -171,9 +178,16 @@ class passwordMethod $sambaLMPassword = preg_replace("/^[!]*(.*$)/","\\1",$sambaLMPassword); $sambaNTPassword = preg_replace("/^[!]*(.*$)/","\\1",$sambaNTPassword); - // Call a pre-lock hook if defined. + // Call external lock hook + $res = $ldap->cat($dn); + $hookAttrs = array(); + foreach($ldap->fetch() as $name => $value){ + if(is_numeric($name)) continue; + if(isset($value[0])) $hookAttrs[$name] = $value[0]; + if(isset($value) && is_string($value)) $hookAttrs[$name] = $value; + } $pwdClass = new password($config, $dn); - $pwdClass->callHook($pwdClass, 'PREUNLOCK',array(), $ret); + $pwdClass->callHook($pwdClass, 'PREUNLOCK',$hookAttrs, $ret); // Lock the account by modifying the password hash. $ldap->cd($dn); @@ -186,7 +200,7 @@ class passwordMethod // Call the password post-lock hook, if defined. $pwdClass = new password($config, $dn); - $pwdClass->callHook($pwdClass, 'POSTUNLOCK',array(), $ret); + $pwdClass->callHook($pwdClass, 'POSTUNLOCK',$hookAttrs, $ret); } return($ldap->success()); } -- 2.30.2