[turba] Re: [core] Webmail and IMSP

Martin Wellard veg at gold.ac.uk
Tue Mar 4 12:15:31 PST 2003


Hello,
> Sure. However: someone who actually needs IMSP support and is going to use
> and test it really needs to step up and take the lead on this. I'm sure some
> of the core developers will help with code or even do the basic driver if
> someone gets a codebase, etc. But someone who wants this needs to get it
> rolling.
OK -  I need it so I'd be happy to get stuck in if people are agreeable.
As a starting point, attached is my IMSP driver for turba. I'm happy to
switch to working on HEAD. The coding
style is probably off-message too, but it seems to work ok.

As I mentioned before it relies on
<http://sourceforge.net/projects/phpimspclass/> and I'm about to contact
the author to see about possibilities for hijacking it/patching it. I've
already had to hack it a bit to get it to support literals in names
properly - but hey, it's GPLed.

The stanza I'm using in config/sources.php follows.

BTW - this is my first attempt at contributing so please go easy on me if
I've got the wrong end of the stick about anything...or everything :)

Regards,
Martin


$cfgSources['imsp'] = array(
    'title' => 'Mulberry (IMSP)',
    'type' => 'imsp',
    'params' => array(),
    'map' => array(
        '__key' => 'name',
        '__owner' => 'owner_id',
        'name' => 'name',
        'alias' => 'alias',
        'email' => 'email',
        'homeAddress' => 'address',
        'workAddress' => 'company',
        'homePhone' => 'phone-home',
        'workPhone' => 'phone-work',
        'fax' => 'fax',
        'notes' => 'notes'
    ),
    'search' => array(
        'name'
    ),
    'strict' => array(
        'name'
    ),
    'public' => false,
    'readonly' => false,
    'admin' => array(),
    'export' => true
);
-------------- next part --------------
<?php
// $Horde: turba/lib/Driver/ldap.php,v 1.11.2.5 2002/04/12 16:10:09 jan Exp $

class Turba_Driver_imsp extends Turba_Driver {

    var $imsp;
    var $username;
    var $abookname;

    /**
     * Constructs a new Turba IMSP driver object.
     *
     * @param $params       Hash containing additional configuration parameters.
     */
    function Turba_Driver_imsp($params)
    {
        $this->type = 'imsp';
        $this->params = $params;

        include_once 'cIMSPABook.php';
        $password = Auth::getCredential('password');
        $this->username = Auth::getAuth();
        $loginname = preg_replace('/@gold.ac.uk/', '', $this->username);
        $this->abookname = $loginname;

        if (! $this->imsp = new IMSPAddressBook()) {
            $this->errno = -1;
            $this->errstr = "Can't create IMSP object";
            return false;
        }

        $this->imsp->escape_special_characters = 0;

        if (! $this->imsp->login($loginname, $password)) {
            $this->errno = -1;
            $this->errstr = "Can't login";
            return false;
        }

        Horde::logMessage(sprintf('IMSP Login %s ', $loginname), 
                    __FILE__, __LINE__, LOG_NOTICE);

    }


    /**
     * Searches the IMSP database with the given criteria and returns a
     * filtered list of results. If the criteria parameter is an empty
     * array, all records will be returned.
     *
     * @param $criteria      Array containing the search criteria.
     * @param $fields        List of fields to return.
     * @param $strict_fields (optional) Array of fields which must be matched
     *                       exactly.
     * @param const $match   Do an 'and' or an 'or' search 
     *                       (defaults to TURBA_SEARCH_AND).
     *
     * @return               Hash containing the search results.
     */
    function search($criteria, $fields, $strict_fields = array(), $match = TURBA_SEARCH_AND)
    {
       $searchcrit = $criteria['name'] ? $criteria['name'].'*' : '*';
       $entries = $this->imsp->searchAddressBook($this->abookname, $searchcrit);

       for($i = 0 ; $i < count($entries) ; $i++) {
            $entry = $this->imsp->getAddressBookEntry($this->abookname, 
                                                             $entries[$i]);
            $entry['object_id'] = $entries[$i];
            $entry['name'] = $entries[$i];
            $entry['owner_id'] = $this->username;
            $results[] = $entry;
        }

        Horde::logMessage(sprintf('IMSP search results %d:%s:%d', $i, 
                              $this->username,count($entries))
                              ,__FILE__, __LINE__, LOG_NOTICE);
        return $i ? $results : false;
    }

    /**
     * Read the given data from the IMSP addressbook
     * and returns the result's fields.
     *
     * @param $criteria      Search criteria.
     * @param $id            Data identifier.
     * @param $fields        List of fields to return.
     *
     * @return               Hash containing the search results.
     */
    function read($criteria, $id, $fields)
    {
        $entry = $this->imsp->getAddressBookEntry($this->abookname, $id);
        foreach($entry as $key  => $val) {
            $entry[$key] = preg_replace('/"/', '', $val);
            Horde::logMessage(sprintf('IMSP read %s:%s', $val, $entry[$key]), 
                          __FILE__, __LINE__, LOG_NOTICE);
        }
        $results[] = $entry;
        return $results;
    }

    /**
     * Closes the current imsp connection.
     *
     * @return          The result of the disconnect() call.
     */
    function close()
    {
        $this->imsp->logout();
        return;
    }


    /**
     * Adds the specified object to the IMSP database.
     */
    function addObject($attributes)
    {
        if (!$attributes['name']) {
            return false;
        }
        foreach($attributes as $key => $val) {
            if ($key && $val) {
                $newentry[$key] = "$val";
                Horde::logMessage(sprintf('IMSP entries %s:%s', $val, $key), 
                          __FILE__, __LINE__, LOG_NOTICE);
            }
        }
        $this->imsp->addAddress($this->abookname, $newentry);
        return true;
    }

    /**
     * Deletes the specified object from the IMSP database.
     */
    function removeObject($object_key, $object_id)
    {
        Horde::logMessage(sprintf('IMSP removeObject %s:%s', 
                         $object_key, $object_id), 
                          __FILE__, __LINE__, LOG_NOTICE);
        return $this->imsp->deleteAddress($this->abookname,$object_id);
    }

    /**
     * Saves the specified object in the IMSP database.
     */
    function setObject($object_key, $object_id, $attributes)
    {
        Horde::logMessage(sprintf('IMSP setObject %s:%s', 
                         $object_key, $object_id), 
                          __FILE__, __LINE__, LOG_NOTICE);
         return $this->addObject($attributes);
    }

    /**
     * Create an object key for a new object.
     *
     * @param array $attributes  The attributes (in driver keys) of the
     *                           object being added.
     *
     * @return string  A unique ID for the new object.
     */
    function makeKey($attributes)
    {
        return ($attributes['name'] ? $attributes['name'] : "The Man With No Name");
    }
}
?>


More information about the turba mailing list