From: Michael M Slusarz Date: Thu, 9 Dec 2010 21:39:14 +0000 (-0700) Subject: Clean up prefs_init hook examples - make sure username is null if no authenticated... X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=e352fbb3940078e0097b2f5f8f508dc961250b63;p=horde.git Clean up prefs_init hook examples - make sure username is null if no authenticated user --- diff --git a/framework/Core/lib/Horde/Core/Prefs/Storage/Hooks.php b/framework/Core/lib/Horde/Core/Prefs/Storage/Hooks.php index d275b85f0..f0f20c18f 100644 --- a/framework/Core/lib/Horde/Core/Prefs/Storage/Hooks.php +++ b/framework/Core/lib/Horde/Core/Prefs/Storage/Hooks.php @@ -27,7 +27,7 @@ class Horde_Core_Prefs_Storage_Hooks extends Horde_Prefs_Storage_Base foreach ($conf_ob->hooks[$scope_ob->scope] as $name) { try { - $scope_ob->set($name, Horde::callHook('prefs_init', array($name, $scope_ob->get($name), $this->_params['user']), $scope_ob->scope)); + $scope_ob->set($name, Horde::callHook('prefs_init', array($name, $scope_ob->get($name), strlen($this->_params['user']) ? $this->_params['user'] : null), $scope_ob->scope)); } catch (Horde_Exception_HookNotSet $e) {} } diff --git a/horde/config/hooks.php.dist b/horde/config/hooks.php.dist index 0c757d171..db1283acc 100644 --- a/horde/config/hooks.php.dist +++ b/horde/config/hooks.php.dist @@ -28,6 +28,8 @@ * as parameters and uses the return value from the hook as the new preference * value. * + * Username will be null if the user is not authenticated. + * * This hook is ONLY executed on login and preferences are cached during a * users' session. * @@ -149,7 +151,7 @@ class Horde_Hooks { // // PREFERENCES INIT: See above for documentation. -// public function prefs_init($pref, $value, $username = null) +// public function prefs_init($pref, $value, $username) // { // switch ($pref) { // case 'from_addr': @@ -165,50 +167,45 @@ class Horde_Hooks // // address. // // // Example #1 -// if (is_null($name)) { -// $name = $GLOBALS['registry']->getAuth(); +// if (is_null($username)) { +// return $value; // } // -// if (!empty($name)) { -// $base_context = 'o=myorg'; -// $scope = 'sub'; -// -// // You will probably need to replace cd= with uid=; this -// // syntax is for Netware 5.1 nldap. -// $cmd = '/usr/bin/ldapsearch -b ' . $base_context . ' -s ' . $scope . ' cn=' . -// escapeShellCmd($GLOBALS['registry']->getAuth()) . -// ' | /usr/bin/grep mail | /usr/bin/awk \'{print $2}\''; -// $mails = `$cmd`; -// $mail_array = explode("\n", $mails); +// $base_context = 'o=myorg'; +// $scope = 'sub'; // -// // Send back the first email found, not the whole list. -// $mail = $mail_array['0']; +// // You will probably need to replace cd= with uid=; this +// // syntax is for Netware 5.1 nldap. +// $cmd = '/usr/bin/ldapsearch -b ' . $base_context . ' -s ' . $scope . ' cn=' . +// escapeshellcmd($username) . +// ' | /usr/bin/grep mail | /usr/bin/awk \'{print $2}\''; +// $mails = `$cmd`; +// $mail_array = explode("\n", $mails); // -// // If no email address is found, then the login name will -// // be used. -// return (empty($mail) ? '' : $mail); -// } +// // Send back the first email found, not the whole list. +// $mail = $mail_array['0']; // -// return ''; +// // If no email address is found, then the login name will +// // be used. +// return empty($mail) +// ? '' +// : $mail; // // // // Example #2 +// if (is_null($username)) { +// return $value; +// } +// // $ldapServer = '172.31.0.236'; // $ldapPort = '389'; // $searchBase = 'o=myorg'; // // $ds = @ldap_connect($ldapServer, $ldapPort); // -// if (is_null($name)) { -// $name = $GLOBALS['registry']->getAuth(); -// if ($name === false) { -// return ''; -// } -// } -// // // You will probably need to replace cn= with uid=; this syntax // // is for Netware 5.1 nldap. -// $searchResult = @ldap_search($ds, $searchBase, 'cn=' . $name); +// $searchResult = @ldap_search($ds, $searchBase, 'cn=' . $username); // $information = @ldap_get_entries($ds, $searchResult); // if (($information === false) || ($information['count'] == 0)) { // $user = ''; @@ -220,7 +217,9 @@ class Horde_Hooks // // ldap_close($ds); // -// return empty($user) ? $name : $user; +// return empty($user) +// ? $username +// : $user; // // // case 'fullname': @@ -228,35 +227,33 @@ class Horde_Hooks // // // Example #1: Set the fullname from the GECOS information in // // the passwd file. -// if (is_null($user)) { -// $user = $GLOBALS['registry']->getAuth('bare'); -// if ($user === false) { -// return ''; -// } +// if (is_null($username)) { +// return $value; // } // +// $user = $GLOBALS['registry']->getAuth('bare'); +// // $array = posix_getpwnam($user); // $gecos_array = explode(',', $array['gecos']); -// return empty($gecos_array) ? $user : $gecos_array[0]; +// return empty($gecos_array) +// ? $user +// : $gecos_array[0]; // // // // Example #2: Set the fullname from LDAP information. In this // // example we look if a Spanish name exists and return this or // // the standard 'cn' entry if not. +// if (is_null($username)) { +// return $value; +// } +// // $ldapServer = 'ldap.example.com'; // $ldapPort = '389'; // $searchBase = 'ou=people,o=example.com'; // // $ds = @ldap_connect($ldapServer, $ldapPort); // -// if (is_null($user)) { -// $user = $GLOBALS['registry']->getAuth(); -// if ($user === false) { -// return ''; -// } -// } -// -// $searchResult = @ldap_search($ds, $searchBase, 'uid=' . $user); +// $searchResult = @ldap_search($ds, $searchBase, 'uid=' . $username); // $information = @ldap_get_entries($ds, $searchResult); // if (($information === false) || ($information['count'] == 0)) { // $name = ''; @@ -268,16 +265,14 @@ class Horde_Hooks // // ldap_close($ds); // -// return empty($name) ? $user : $name; +// return empty($user) +// ? $username +// : $user; // // // case 'theme': -// // Example theme init. This shows how you can access things -// // like the currently logged in user, global variables, server -// // config, etc. It isn't, however, something you probably want -// // to actually use in production, by virtue of demonstrating all -// // those. :) -// if ($GLOBALS['registry']->getAuth() != 'foo') { +// // Example theme init. +// if ($username != 'foo') { // return 'mozilla'; // } // diff --git a/imp/config/hooks.php.dist b/imp/config/hooks.php.dist index 0a82af70c..2deab44fb 100644 --- a/imp/config/hooks.php.dist +++ b/imp/config/hooks.php.dist @@ -50,19 +50,17 @@ class IMP_Hooks * * See horde/config/hooks.php.dist for more information. */ -// public function prefs_init($pref, $value, $username = null) +// public function prefs_init($pref, $value, $username) // { -// if (!$username) { -// return; -// } -// // switch ($pref) { // case 'add_source': // // Dynamically set the add_source preference. // -// // Example #1: Useful hook when using a Turba source with shares +// // Example: Useful hook when using a Turba source with shares // // enabled (i.e. the example localsql configuration). -// return $GLOBALS['registry']->call('contacts/getDefaultShare'); +// return is_null($username) +// ? $value +// : $GLOBALS['registry']->call('contacts/getDefaultShare'); // // // case 'search_sources': @@ -71,7 +69,8 @@ class IMP_Hooks // // Example #1: Use the list of sources defined in the contacts // // application (e.g. Turba) to populate the search_sources // // value. -// if ($GLOBALS['registry']->hasMethod('contacts/sources')) { +// if (!is_null($username) && +// $GLOBALS['registry']->hasMethod('contacts/sources')) { // $sources = $GLOBALS['registry']->call('contacts/sources'); // return json_encode(array_keys($sources)); // }