[dev] Today's little ldap schema witch project and a
horde.schema diff.
Jon Parise
jon@horde.org
Thu, 11 Apr 2002 00:39:48 -0400
---------------------- multipart/mixed attachment
On Thu, Apr 11, 2002 at 12:24:35AM -0400, Jon Parise wrote:
> Because we can't save an empty string, the kludgy solution would
> be to define some new string constant (e.g. 'HORDE-EMPTY-STRING')
> and store that as the equivalent of an empty preference. Upon
> reading the preferences back in, we'd check for that special
> string and convert it into an empty string value in PHP code.
To back this up with code, see the attached patch. It's
completely untested (my PHP installation is broken at the
moment), but it should at least convey my idea.
--
Jon Parise (jon@csh.rit.edu) . Information Technology (2001)
http://www.csh.rit.edu/~jon/ : Computer Science House Member
---------------------- multipart/mixed attachment
Index: ldap.php
===================================================================
RCS file: /repository/horde/lib/Prefs/ldap.php,v
retrieving revision 1.24
diff -u -r1.24 ldap.php
--- ldap.php 2 Apr 2002 01:31:52 -0000 1.24
+++ ldap.php 11 Apr 2002 04:26:50 -0000
@@ -6,6 +6,21 @@
Horde::functionCheck('ldap_connect', true,
'Prefs_ldap: Required LDAP functions were not found.');
+/*
+ * Apparently, it is not possible to store an empty string in an LDAP
+ * directory. Attempting to do so under OpenLDAP 1.x results in the attribute
+ * being deleted, and under OpenLDAP 2.x, a syntax error is raised.
+ *
+ * Because we require the ability to set preferences to empty strings, we
+ * introduce the following kludge. When we detect a preference is set to
+ * an empty string (as determined by empty()), we store the string associated
+ * with _EMPTY_STRING instead. When the preferences are retrieved, instances
+ * of _EMPTY_STRING are converted back to a real empty string ('').
+ *
+ * @constant _EMPTY_STRING Kludge for support empty string values in LDAP
+ */
+define('_EMPTY_STRING', 'HORDE-EMPTY-STRING');
+
/**
* Preferences storage implementation for PHP's LDAP extention.
*
@@ -244,7 +259,20 @@
*/
foreach ($prefs as $pref) {
if (isset($result[0][$this->map($pref)][0])) {
- $this->prefs[$pref]['val'] = utf8_decode($result[0][$this->map($pref)][0]);
+ $value = utf8_decode($result[0][$this->map($pref)][0]);
+
+ /* Check for the existence of the empty string kludge. */
+ if ($value == _EMPTY_STRING) {
+ $this->prefs[$pref]['val'] = '';
+ } else {
+ $this->prefs[$pref]['val'] = $value;
+ }
+
+ /*
+ * Turn off the "default" flag for this preference now
+ * that the default value has been replaced with the
+ * stored value.
+ */
$this->prefs[$pref]['default'] = false;
}
}
@@ -306,13 +334,18 @@
* Build a hash of the preferences and their values that need to be
* stored in the LDAP server.
*/
- $new_values = array();
+ $entry = array();
foreach($dirty_prefs as $pref) {
- $new_values[$this->map($pref)] = utf8_encode(Prefs::getValue($pref));
+ $value = Pref::getValue($pref);
+ if (empty($value)) {
+ $entry[$this->map($pref)] = _EMPTY_STRING;
+ }
+ $entry[$this->map($pref)] = utf8_encode($value);
+ }
}
/* Send the hash to the LDAP server. */
- if (!@ldap_modify($this->connection, $this->dn, $new_values)) {
+ if (!@ldap_modify($this->connection, $this->dn, $entry)) {
Horde::fatal(new PEAR_Error(sprintf(_("Unable to modify preferences: [%d] %s"), ldap_errno($this->connection), ldap_error($this->connection))), __FILE__, __LINE__);
}
---------------------- multipart/mixed attachment--