[sork] Re: ldap.php

alain alain.defrance at univ-evry.fr
Fri Apr 8 02:06:07 PDT 2005


hello,

i'am using the latest file vacation/lib/Driver/ldap.php,v 1.6 2005/01/03 
14:35:41

and i've the follwing error message :

*Fatal error*: Call to undefined function: mb_convert_encoding() in 
*/usr/apache2/htdocs/horde/vacation/lib/Driver/ldap.php* on line *313

*is it a problem about php Version ? (PHP Version: 4.3.4, Horde: 
3.0.3,Imp: H3 (4.0.2))

if i comment the lines concerning function mb_convert i can display the 
vacation form, but when i submit the form i've another error :

*Fatal error*: Call to undefined function: mb_detect_encoding() in 
*/usr/apache2/htdocs/horde/vacation/lib/Driver/ldap.php* on line *230

*if i comment the lines concerning mb_detect_encoding() then the form 
works fine...

question :  what's the purspose of these encoding function, and how may 
i resolved the problem ????

thanks for reading...

Alain DEFRANCE

>Hi,
>
>  
>
>>i try to use /vacation/lib/Driver/ldap.php file but i'm a little lost
>>with the correction :
>>
>>http://bugs.horde.org/ticket/?id=1648&action=
>>
>>is it possible to send me the good ldap.php file because even with the
>>good correction i've still errors... ??
>>    
>>
>here it goes...
>
>what kind of errors do you have?
>
>Do you mind to post further conversations also to the sork list?
>
>regards,
>Christian
>  
>
>------------------------------------------------------------------------
>
>Index: lib/Driver/ldap.php
>===================================================================
>RCS file: /repository/vacation/lib/Driver/ldap.php,v
>retrieving revision 1.6
>diff -r1.6 ldap.php
>50a51
>  
>
>>        $this->params = array_merge($this->params, $this->_params);
>>    
>>
>141d141
>< 
>191c191,196
><         $message = mb_convert_encoding($message, 'UTF-8', 'ISO-8859-1');
>---
>  
>
>>        $messageEncoding = $this->_detectEncoding($message);
>>        
>>        // encode if necessary
>>        if ($messageEncoding != 'UTF-8') {
>>            $message = mb_convert_encoding($message, 'UTF-8', $messageEncoding);
>>        }
>>    
>>
>195c200
><         $newDetails[$this->_params[$realm]['active']] = 'Y';
>---
>  
>
>>        $newDetails[$this->_params[$realm]['active']] = $this->_params[$realm]['enabled'];
>>    
>>
>197,198d201
><         $attribs = array($this->_params[$realm]['vacation'], $this->_params[$realm]['active']);
><         $value = $this->_getVacation($userdn, $attribs);
>200a204,205
>  
>
>>            $this->_disconnect();
>>            return false; 
>>    
>>
>202a208,209
>  
>
>>        $res_alias = $this->_setVacationAlias($user, $realm, $userdn);
>>
>>    
>>
>205a213,273
>  
>
>>        return $res_alias;
>>    }
>>
>>    /**
>>     * Try to detect the character encoding of a string.
>>     *
>>     * @access private
>>     *
>>     * @param string $string   Input string for detection
>>     *  
>>     *
>>     * @return string  Character encoding (see link below)
>>     * @link http://www.php.net/manual/en/ref.mbstring.php
>>     */
>>    function _detectEncoding($string)
>>    {
>>        // try popular defaults first
>>        if (!$encoding = mb_detect_encoding($string, 'UTF-8, ISO-8859-1', true)) {
>>            // no UTF-8 nor ISO-8859-1, try whatever it's defined as 'auto'
>>            if (!$encoding = mb_detect_encoding($string, 'auto', true)) {
>>                // still unrecognised encoding, use the charset for the current language
>>                $encoding = NLS::getCharset();
>>            }
>>        }
>>        return $encoding; 
>>    }
>>
>>
>>    /**
>>     * Set/create vacation mail alias
>>     *
>>     * Some mta/ldap/vacation implementations requires an extra mail alias
>>     * (ex. user at example.org -> user at example.org, user at autoreply.exmaple.org)
>>     *
>>     * You should override this method in your extended ldap driver class,
>>     * if you need this feature.
>>     *
>>     * @access private
>>     *     
>>     * @param string $user   The username to enable vacation for
>>     * @param string $realm  The realm for the current user
>>     * @param string $userdn The LDAP DN for the current user
>>     *
>>     * @return boolean  true on success, otherwise false :)
>>     */    
>>    function _setVacationAlias($user, $realm, $userdn)
>>    {
>>        return true;    
>>    }
>>
>>
>>    /**
>>     * Unset/remove vacation mail alias
>>     *
>>     * @access private
>>     *
>>     * @return boolean  true on success, otherwise false :)
>>     * @see _setVacationAlias()
>>     */    
>>    function _unsetVacationAlias($user, $realm, $userdn)
>>    {
>>    
>>
>208a277
>  
>
>236,237c305
><         $attribs = array($this->_params[$realm]['vacation'], $this->_params[$realm]['active']);
><         $vac = $this->_getVacation($userdn, $attribs);
>---
>  
>
>>        $vac = $this->_getVacation($userdn, $realm);
>>    
>>
>239c307
><         // Prepare the message. \n->\n\n and UTF-8 encode.
>---
>  
>
>>        // Prepare the message. \n->\n\n and UTF-8 decode.
>>    
>>
>241c309
><         $vac['message'] = mb_convert_encoding($vac['message'], 'ISO-8859-1', 'UTF-8');
>---
>  
>
>>        $outputEncoding = NLS::getCharset();
>>    
>>
>242a311,314
>  
>
>>        // decode if necessary
>>        if ($outputEncoding != 'UTF-8') {
>>            $vac['message'] = mb_convert_encoding($vac['message'], $outputEncoding, 'UTF-8');
>>        }
>>    
>>
>246c318
><     function _getVacation($userdn, $attribs)
>---
>  
>
>>    function _getVacation($userdn, $realm)
>>    
>>
>248c320,323
><         $sr = ldap_search($this->_ds, $userdn, 'uid=*', $attribs);
>---
>  
>
>>        // get the vacation message and the vacation status
>>        $searchAttrs = array($this->_params[$realm]['vacation'], $this->_params[$realm]['active']);
>>        $sr = ldap_search($this->_ds, $userdn, $this->_params[$realm]['uid'].'=*', $searchAttrs);
>>
>>    
>>
>253,254c328,330
><         $value = ldap_get_values($this->_ds, $entry, $attribs[0]);
><         if ($value['count'] == 0) {
>---
>  
>
>>        $retAttrs = ldap_get_attributes($this->_ds, $entry);
>>        if (!$retAttrs) {
>>    
>>
>257,260c333,340
><         $retval['message'] = $value[0];
><         $value = ldap_get_values($this->_ds, $entry, $attribs[1]);
><         if ($value['count'] == 0) {
><             return false;
>---
>  
>
>>        // set default values 
>>        $vacationInfo['message']  = '';
>>        $vacationInfo['vacation'] = $this->_params[$realm]['disabled'];
>>
>>        // are there any returned attributes / values?
>>        if (isset($retAttrs[$this->_params[$realm]['vacation']])) {
>>            $vacationInfo['message'] = $retAttrs[$this->_params[$realm]['vacation']][0];
>>    
>>
>262,263c342,347
><         $retval['vacation'] = $value[0];
><         return $retval;
>---
>  
>
>>        if (isset($retAttrs[$this->_params[$realm]['active']])) {
>>            $vacationInfo['vacation'] = $retAttrs[$this->_params[$realm]['active']][0];    
>>        }
>>
>>        return $vacationInfo;	
>>    
>>
>305c389
><         $newDetails[$this->_params[$realm]['active']] = 'N';
>---
>  
>
>>        $newDetails[$this->_params[$realm]['active']] = $this->_params[$realm]['disabled'];
>>    
>>
>310a395,396
>  
>
>>        $res_alias = $this->_unsetVacationAlias($user, $realm, $userdn);
>>
>>    
>>
>313,314c399
>< 
><         return true;
>---
>  
>
>>        return $res_alias;
>>    
>>
>>------------------------------------------------------------------------
>>
>><?php
>>/**
>> * $Horde: vacation/lib/Driver/ldap.php,v 1.6 2005/01/03 14:35:41 jan Exp $
>> *
>> * Copyright 2001-2005 Eric Rostetter <eric.rostetter at physics.utexas.edu>
>> *
>> * See the enclosed file LICENSE for license information (BSD). If you
>> * did not receive this file, see http://www.horde.org/bsdl.php.
>> *
>> * Vacation_Driver_ldap:: implements the Vacation_Driver API for LDAP-compliant
>> * mail servers (such as Exim).
>> *
>> * Parameters:
>> * (required)
>> *   host       - hostname of the LDAP server
>> *   port       - port number of the LDAP service
>> *   basedn     - base DN of the user directory
>> *   uid        - attribute to use for uid
>> *   vacation   - attribute to use for storing the vacation message
>> *   active     - attribute which determines if the vacation message is active
>> * (optional)
>> *   userdn     - another way of specifying the user DN (instead of
>> *                constructing it from uid+basedn).
>> *   version    - Protocol version for the LDAP server (PHP defaults
>> *                to version 2. OpenLDAP >= 2.1.4 uses version 3, and
>> *                so must be set explicitly).
>> *
>> * @author  Eric Rostetter <eric.rostetter at physics.utexas.edu>
>> * @version $Revision: 1.6 $
>> * @package Vacation
>> */
>>class Vacation_Driver_ldap extends Vacation_Driver {
>>
>>    /** Pointer to the ldap connection. */
>>    var $_ds;
>>
>>    /** Hash containing connection parameters. */
>>    var $_params;
>>
>>    /** The error string returned to the user if an error occurs. */
>>    var $err_str;
>>
>>    /**
>>     * Constructs a new ftp dot-forward Vacation_Driber object.
>>     *
>>     * @param array  $params    A hash containing connection parameters.
>>     */
>>    function Vacation_Driver_ldap($params = array())
>>    {
>>        $this->_params = $params;
>>        $this->params = array_merge($this->params, $this->_params);
>>    }
>>
>>    /**
>>     * Do an ldap connect and bind as the guest user or as the optional userdn.
>>     *
>>     * @param    $userdn      The optional dn to use when binding non-anonymously.
>>     * @param    $oldpassword The optional password for $userdn.
>>     *
>>     * @return   boolean   True or False based on success of connect and bind.
>>     *
>>     */
>>    function _connect($userdn = null, $password = null, $realm = 'default')
>>    {
>>        $this->_ds = ldap_connect($this->_params[$realm]['host'], $this->_params[$realm]['port']);
>>        if (!$this->_ds) {
>>            return PEAR::raiseError(_("Could not connect to ldap server"));
>>        }
>>        if (array_key_exists('version', $this->_params[$realm])) {
>>             ldap_set_option($this->_ds, LDAP_OPT_PROTOCOL_VERSION,
>>					$this->_params[$realm]['version']);
>>        }
>>
>>        if (!is_null($userdn)) {
>>            $result = @ldap_bind($this->_ds, $userdn, $password);
>>        } else {
>>            $result = @ldap_bind($this->_ds);
>>        }
>>
>>        if (!$result) {
>>            return PEAR::raiseError(_("Could not bind to ldap server"));
>>        }
>>
>>        return true;
>>    }
>>
>>    /**
>>     * Close the ldap connection.
>>     */
>>    function _disconnect()
>>    {
>>        @ldap_close($this->_ds);
>>    }
>>
>>    /**
>>     * Check if the realm has a specific configuration.  If not, try to fall
>>     * back on the default configuration.  If still not a valid configuration
>>     * then exit with an error.
>>     *
>>     * @param string    $realm      The realm of the user, or "default" if none.
>>     *                              Note: passed by reference so we can change
>>     *                              it's value!
>>     *
>>     */
>>    function checkConfig(&$realm)
>>    {
>>        // If no realm passed in, or no host config for the realm passed in,
>>        // then we fall back to the default realm
>>        if (empty($realm) || empty($this->_params[$realm]['server'])) {
>>            $realm = 'default';
>>        }
>>
>>        // If still no host/port, then we have a misconfigured module.
>>        if (empty($this->_params[$realm]['host']) ||
>>            empty($this->_params[$realm]['port']) ) {
>>            $this->err_str = _("The module is not properly configured!");
>>            return false;
>>        }
>>        return true;
>>    }
>>
>>    /**
>>     * Lookup and return the user's dn.
>>     *
>>     * @param  $user     The username of the user.
>>     * @param  $realm    The realm (domain) name of the user.
>>     * @param  $basedn   The ldap basedn.
>>     * @param  $uid      The ldap uid.
>>     *
>>     * @return string    The ldap dn for the user.
>>     */
>>    function _lookupdn($user, $realm)
>>    {
>>        // bind as guest
>>        $this->_connect();
>>
>>        // construct search
>>        $search = $this->_params[$realm]['uid'] . '=' . $user;
>>        if (!empty($this->_params[$realm]['realm'])) {
>>            $search .= '@' . $this->_params[$realm]['realm'];
>>        }
>>        // get userdn
>>        $result = ldap_search($this->_ds, $this->_params[$realm]['basedn'], $search);
>>        $entry = ldap_first_entry($this->_ds, $result);
>>        if ($entry === false) {
>>            $this->_disconnect();
>>            return PEAR::raiseError(_("User not found."));
>>        }
>>        $userdn = ldap_get_dn($this->_ds, $entry);
>>
>>        // disconnect from ldap server
>>        $this->_disconnect();
>>
>>        return $userdn;
>>    }
>>
>>    /**
>>     * Set the vacation notice up.
>>     * @param string    $user       The username to enable vacation for.
>>     * @param string    $realm      The realm of the user.
>>     * @param string    $target     The message to install.
>>     * @return boolean  Returns true on success, false on error.
>>     */
>>    function setVacation($user, $realm, $pass, $message)
>>    {
>>        // Make sure the configuration file is correct
>>        if (!$this->checkConfig($realm)) {
>>            return false;
>>        }
>>
>>        // get the user's dn
>>        if (array_key_exists('userdn', $this->_params[$realm])) {
>>            $userdn = $this->_params[$realm]['userdn'];
>>        } else {
>>            $userdn = $this->_lookupdn($user, $realm);
>>            if (is_a($userdn, 'PEAR_Error')) {
>>                return $userdn;
>>            }
>>        }
>>
>>        // connect as the user
>>        $res = $this->_connect($userdn, $pass, $realm);
>>        if (is_a($res, 'PEAR_Error')) {
>>            $this->err_str = $res->getMessage();
>>            $this->err_str .= ' - ' .  _("Check your password");
>>            return false;
>>        }
>>
>>        // Prepare the message. \n->\n\n and UTF-8 encode.
>>        $message = str_replace("\r\n", "\\n", $message);
>>        $messageEncoding = $this->_detectEncoding($message);
>>        
>>        // encode if necessary
>>        if ($messageEncoding != 'UTF-8') {
>>            $message = mb_convert_encoding($message, 'UTF-8', $messageEncoding);
>>        }
>>
>>        // change the user's vacation.
>>        $newDetails[$this->_params[$realm]['vacation']] = $message;
>>        $newDetails[$this->_params[$realm]['active']] = $this->_params[$realm]['enabled'];
>>        $res = ldap_mod_replace($this->_ds, $userdn, $newDetails);
>>        if (!$res) {
>>            $res = PEAR::raiseError(ldap_error($this->_ds));
>>            $this->_disconnect();
>>            return false; 
>>        }
>>
>>        $res_alias = $this->_setVacationAlias($user, $realm, $userdn);
>>
>>        // disconnect from ldap server
>>        $this->_disconnect();
>>
>>        return $res_alias;
>>    }
>>
>>    /**
>>     * Try to detect the character encoding of a string.
>>     *
>>     * @access private
>>     *
>>     * @param string $string   Input string for detection
>>     *  
>>     *
>>     * @return string  Character encoding (see link below)
>>     * @link http://www.php.net/manual/en/ref.mbstring.php
>>     */
>>    function _detectEncoding($string)
>>    {
>>        // try popular defaults first
>>        if (!$encoding = mb_detect_encoding($string, 'UTF-8, ISO-8859-1', true)) {
>>            // no UTF-8 nor ISO-8859-1, try whatever it's defined as 'auto'
>>            if (!$encoding = mb_detect_encoding($string, 'auto', true)) {
>>                // still unrecognised encoding, use the charset for the current language
>>                $encoding = NLS::getCharset();
>>            }
>>        }
>>        return $encoding; 
>>    }
>>
>>
>>    /**
>>     * Set/create vacation mail alias
>>     *
>>     * Some mta/ldap/vacation implementations requires an extra mail alias
>>     * (ex. user at example.org -> user at example.org, user at autoreply.exmaple.org)
>>     *
>>     * You should override this method in your extended ldap driver class,
>>     * if you need this feature.
>>     *
>>     * @access private
>>     *     
>>     * @param string $user   The username to enable vacation for
>>     * @param string $realm  The realm for the current user
>>     * @param string $userdn The LDAP DN for the current user
>>     *
>>     * @return boolean  true on success, otherwise false :)
>>     */    
>>    function _setVacationAlias($user, $realm, $userdn)
>>    {
>>        return true;    
>>    }
>>
>>
>>    /**
>>     * Unset/remove vacation mail alias
>>     *
>>     * @access private
>>     *
>>     * @return boolean  true on success, otherwise false :)
>>     * @see _setVacationAlias()
>>     */    
>>    function _unsetVacationAlias($user, $realm, $userdn)
>>    {
>>        return true;
>>    }
>>
>>
>>    function _getUserDetails($user, $realm = 'default', $pass)
>>    {
>>        // Make sure the configuration file is correct.
>>        if (!$this->checkConfig($realm)) {
>>            return false;
>>        }
>>
>>        // Get the user's dn.
>>        if (array_key_exists('userdn', $this->_params[$realm])) {
>>            $userdn = $this->_params[$realm]['userdn'];
>>        } else {
>>            $userdn = $this->_lookupdn($user, $realm);
>>            if (is_a($userdn, 'PEAR_Error')) {
>>                return $userdn;
>>            }
>>        }
>>
>>        // Connect as the user.
>>        $res = $this->_connect($userdn, $pass, $realm);
>>        if (is_a($res, 'PEAR_Error')) {
>>            $this->_disconnect();
>>            if ($res->getMessage() == _("Could not bind to ldap server")) {
>>                return PEAR::raiseError(_("Incorrect Password"));
>>            }
>>            return $res;
>>        }
>>
>>        $vac = $this->_getVacation($userdn, $realm);
>>
>>        // Prepare the message. \n->\n\n and UTF-8 decode.
>>        $vac['message'] = str_replace("\\\\n", "\r\n", $vac['message']);
>>        $outputEncoding = NLS::getCharset();
>>
>>        // decode if necessary
>>        if ($outputEncoding != 'UTF-8') {
>>            $vac['message'] = mb_convert_encoding($vac['message'], $outputEncoding, 'UTF-8');
>>        }
>>        return $vac;
>>    }
>>
>>    function _getVacation($userdn, $realm)
>>    {
>>        // get the vacation message and the vacation status
>>        $searchAttrs = array($this->_params[$realm]['vacation'], $this->_params[$realm]['active']);
>>        $sr = ldap_search($this->_ds, $userdn, $this->_params[$realm]['uid'].'=*', $searchAttrs);
>>
>>        $entry = ldap_first_entry($this->_ds, $sr);
>>        if (!$entry) {
>>            return false;
>>        }
>>
>>        $retAttrs = ldap_get_attributes($this->_ds, $entry);
>>        if (!$retAttrs) {
>>            return false;
>>        }
>>
>>        // set default values 
>>        $vacationInfo['message']  = '';
>>        $vacationInfo['vacation'] = $this->_params[$realm]['disabled'];
>>
>>        // are there any returned attributes / values?
>>        if (isset($retAttrs[$this->_params[$realm]['vacation']])) {
>>            $vacationInfo['message'] = $retAttrs[$this->_params[$realm]['vacation']][0];
>>        }
>>
>>        if (isset($retAttrs[$this->_params[$realm]['active']])) {
>>            $vacationInfo['vacation'] = $retAttrs[$this->_params[$realm]['active']][0];    
>>        }
>>
>>        return $vacationInfo;	
>>    }
>>
>>    /**
>>     * Deactivate the vacation notice.
>>     * NB: This does not delete the vacation message, just marks it as
>>     * disabled.
>>     *
>>     * @param string    $user       The user to disable vacation notices for.
>>     * @param string    $realm      The realm of the user.
>>     * @param string    $pass       The password of the user.
>>     *
>>     * @return boolean  Returns true on success, false on error.
>>     */
>>    function unsetVacation($user, $realm, $pass)
>>    {
>>        // Make sure the configuration file is correct.
>>        if (!$this->checkConfig($realm)) {
>>            return false;
>>        }
>>
>>        // Get the user's dn.
>>        if (isset($this->_params[$realm]['userdn'])) {
>>            $userdn = $this->_params[$realm]['userdn'];
>>        } else {
>>            $userdn = $this->_lookupdn($user, $realm);
>>            if (is_a($userdn, 'PEAR_Error')) {
>>                return $userdn;
>>            }
>>        }
>>
>>        // Connect as the user.
>>        $res = $this->_connect($userdn, $pass, $realm);
>>        if (is_a($res, 'PEAR_Error')) {
>>            $this->_disconnect();
>>            if ($res->getMessage() == _("Could not bind to ldap server")) {
>>                return PEAR::raiseError(_("Incorrect Password"));
>>            }
>>            return $res;
>>        }
>>
>>        // Set the vacation message to inactive.
>>        $newDetails[$this->_params[$realm]['active']] = $this->_params[$realm]['disabled'];
>>        $res = ldap_mod_replace($this->_ds, $userdn, $newDetails);
>>        if (!$res) {
>>            $res = PEAR::raiseError(ldap_error($this->_ds));
>>        }
>>
>>        $res_alias = $this->_unsetVacationAlias($user, $realm, $userdn);
>>
>>        // Disconnect from LDAP server.
>>        $this->_disconnect();
>>        return $res_alias;
>>    }
>>
>>}
>>    
>>



More information about the sork mailing list