gunnar: server/php-kolab/Kolab_Webadmin/Webadmin user.class.php, NONE, 1.1 sieveutils.class.php, 1.1, 1.2 webadmin.class.php, 1.2, 1.3

cvs at kolab.org cvs at kolab.org
Mon Aug 20 11:43:18 CEST 2007


Author: gunnar

Update of /kolabrepository/server/php-kolab/Kolab_Webadmin/Webadmin
In directory doto:/tmp/cvs-serv12680/Webadmin

Modified Files:
	sieveutils.class.php webadmin.class.php 
Added Files:
	user.class.php 
Log Message:
Started restructuring the users part of the kolab web admin frontend.

--- NEW FILE: user.class.php ---
<?php
/*  
 *  COPYRIGHT
 *  ---------
 *
 *  See ../AUTHORS file
 *
 *
 *  LICENSE
 *  -------
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  $Revision: 1.1 $
 *
 *  ABOUT
 *  -----
 *
 *  A class for Kolab user management.
 *
 */

/** Handle input information. */
require_once('Kolab/Webadmin/form.class.php');

/**
 * This class combines methods common to most pages of the Kolab web
 * admin frontend.
 *
 * $Header: /kolabrepository/server/php-kolab/Kolab_Webadmin/Webadmin/user.class.php,v 1.1 2007/08/20 09:43:15 gunnar Exp $
 *
 * @author  Steffen Hansen <steffen at klaralvdalens-datakonsult.se>
 * @author  Gunnar Wrobel  <wrobel at pardus.de>
 * @package Kolab_Webadmin
 */
class KolabUsers {

    /**
     * A link to the object that handles our ldap connection.
     *
     * @var KolabLDAP
     */
    var $_ldap;

    /**
     * The configuration options for the Kolab web admin
     *
     * @var params
     */
    var $_params;

    /**
     * Initialize the users class
     *
     * @param KolabLDAP $ldap An object that wraps the ldap connection.
     *
     * @return KolabUsers The initialized KolabUsers object
     */
    function KolabUsers($ldap, $params)
    {
        $this->_ldap = $ldap;
        $this->_params = $params;
    }

    /**
     * Return a list of all users
     *
     * @param string $dn The distinguished name of the current user.
     * @param mixed $filterattr  FIXME
     * @param mixed $filtertype  FIXME
     * @param mixed $filtervalue FIXME
     * @param mixed $alphalimit  FIXME
     * @param mixed $alphagroup  FIXME
     *
     * @return KolabUsers The initialized KolabUsers object
     */
    function getVisibleUsers($dn, $filterattr = false, $filtertype = false,
							 $filtervalue = false, $alphalimit = false,
							 $alphagroup = false)
    {
        // Get all entries & dynamically split the letters with growing entries
        $entries = array();
        if (isset($this->_params['base_dn'])) {
            $base_dn = $this->params['base_dn'];
        } else {
            $base_dn = 'k=kolab';
        }

        $privmembers = array_merge( 
            (array)$this->_ldap->groupMembers( "cn=internal,$base_dn", 'admin' ),
            (array)$this->_ldap->groupMembers( "cn=internal,$base_dn", 'maintainer' ) 
        );

        $userfilter = "cn=*";

        if( !in_array( $filterattr, array( 'cn', 'uid', 'mail' ) ) ) {
            $filterattr = 'cn';
        }
        
        if( !empty( $filtervalue ) ) {
            switch( $filtertype ) {
            case 'contains': // contains
                $userfilter = "$filterattr=*" .
                    $this->_ldap->escape($filtervalue) . '*';
                break;
            case 'is': // is
                $userfilter = "$filterattr=" . 
                    $this->_ldap->escape($filtervalue);
                break;
            case 'begins': // begins with
                $userfilter = "$filterattr=" . 
                    $this->_ldap->escape($filtervalue) . '*';
                break;
            case 'ends': // ends with
                $userfilter = "$filterattr=*" . 
                    $this->_ldap->escape($filtervalue);
                break;
            }
        }
        $alphalimit = '';
        $alphagroup = '';
        // Disabled for now
        if( false && !empty($alphalimit)) {
            $ala='sn'; // alpha limit attibute
            if( $alphalimit == "other" ) {
                $alphalimit = "(|($ala=æ*)($ala=ø*)($ala=å*)($ala=ä*)($ala=ö*)($ala=ü*)($ala=0*)($ala=1*)($ala=2*)($ala=3*)($ala=4*)($ala=5*)($ala=6*)($ala=7*)($ala=8*)($ala=9*))";
            } else {
                $alphalimit ="($ala=$alphalimit*)";
            }
        } else if( !empty($alphagroup)) {
            $ala='sn'; // alpha limit attibute
            switch( $alphagroup ) {
            case 'a': 
                $alphalimit = "(|($ala=a*)($ala=b*)($ala=c*)($ala=d*)($ala=e*)($ala=f*))";
                break;
            case 'g': 
                $alphalimit = "(|($ala=g*)($ala=h*)($ala=i*)($ala=j*)($ala=k*)($ala=l*))";
                break;
            case 'm': 
                $alphalimit = "(|($ala=m*)($ala=n*)($ala=o*)($ala=p*)($ala=q*)($ala=r*))";
                break;
            case 's': 
                $alphalimit = "(|($ala=s*)($ala=t*)($ala=u*)($ala=v*)($ala=w*)($ala=x*)($ala=y*)($ala=z*))"; 
                break;
            case 'other': 
                $alphalimit = "(|($ala=æ*)($ala=ø*)($ala=å*)($ala=ä*)($ala=ö*)($ala=ü*)($ala=0*)($ala=1*)($ala=2*)($ala=3*)($ala=4*)($ala=5*)($ala=6*)($ala=7*)($ala=8*)($ala=9*))"; 
                break;
            default: 
                $alphalimit = '';
            }
        }
        $domains = $this->_ldap->domainsForMaintainerDn($smarty->auth->dn());
        if( is_array($domains) ) {
            $domainfilter='';
            foreach( $domains as $dom ) {
                $domainfilter .= '(mail=*@' . $this->_ldap->escape($dom) . ')';
            }
            if( $domainfilter ) {
                $domainfilter = "(|$domainfilter)";
            }
        } else {
            $domainfilter= "";
        }
        $filter = "(&($userfilter)$domainfilter$alphalimit(objectclass=kolabInetOrgPerson)(uid=*)(mail=*)(sn=*))";
        debug("filter is \"$filter\"");
        $result = ldap_search($this->_ldap->connection, $base_dn, $filter, 
                              array( 'uid', 'mail', 'sn', 'cn', 
                                     'kolabDeleteflag' ));
        
        $entries = array();

        if( $result ) {
            ldap_sort($this->_ldap->connection, $result, 'sn');
            $entry = ldap_first_entry($this->_ldap->connection, $result);

            while( $entry ) {
                $attrs = ldap_get_attributes($this->_ldap->connection, $entry);
                $dn = ldap_get_dn($this->_ldap->connection,$entry);
                $deleted = array_key_exists('kolabDeleteflag',$attrs)?$attrs['kolabDeleteflag'][0]:"FALSE";
                $uid = $attrs['uid'][0];
                $mail = $attrs['mail'][0];
                $sn = $attrs['sn'][0];
                $cn = $attrs['cn'][0];
                $a = strlen($sn);
                $b = strlen($cn);
                $fn = substr($cn, 0, $b - $a);
                $dncomp = split( ',', $dn );
                if( in_array('cn=groups',$dncomp) ) {
                    $type = 'G';
                } else if( in_array('cn=resources',$dncomp) ) {
                    $type = 'R';
                } else if( in_array('cn=internal',$dncomp) ) {
                    $type = 'I';
                } else {
                    $type = 'U';
                }

                // skip admins and maintainers
                if( !array_key_exists( $dn, $privmembers ) ) {
                    $entries[] = array( 'dn' => $dn,
                                        'sn' => $sn,
                                        'fn' => $fn,
                                        'type' => $type,
                                        'mail' => $mail,
                                        'uid' => $uid,
                                        'deleted' => $deleted );
                }
                $entry = ldap_next_entry( $this->_ldap->connection,$entry );
            }
        }

        return $entries;
    }

};

/*
  Local variables:
  mode: php
  indent-tabs-mode: f
  tab-width: 4
  buffer-file-coding-system: utf-8
  End:
  vim:encoding=utf-8:
 */
?>

Index: sieveutils.class.php
===================================================================
RCS file: /kolabrepository/server/php-kolab/Kolab_Webadmin/Webadmin/sieveutils.class.php,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- sieveutils.class.php	16 Aug 2007 12:16:51 -0000	1.1
+++ sieveutils.class.php	20 Aug 2007 09:43:15 -0000	1.2
@@ -18,92 +18,288 @@
  *  Project's homepage; see <http://www.gnu.org/licenses/gpl.html>.
  */
 
-class SieveUtils {
-  // Funny multiline string escaping in Sieve
-  /*static*/ function dotstuff( $str ) {
-    return str_replace( "\n.", "\n..", $str );
-  }
-  
-  /*static*/ function undotstuff( $str ) {
-    return str_replace( "\n..", "\n.", $str );
-  }
-  
-  /*static*/ function getDeliverFolder( $script ) {
-    $inbox = false;      
-	if( preg_match("/fileinto \"INBOX\/(.*)\";/", $script, $regs ) ) {
-	  $inbox = $regs[1];
-	}
-    return $inbox;
-  }
+/** We need sieve access. */
+require_once('Net/Sieve.php');
 
-  /*static*/ function getVacationAddresses( $script ) {
-    $addresses = false;
-    if( preg_match("/:addresses \\[([^\\]]*)\\]/s", $script, $regs ) ) {
-      $tmp = split(',', $regs[1] );
-      $addresses = array();
-      foreach( $tmp as $a ) {
-		if( ereg('^ *"(.*)" *$', $a, $regs ) ) $addresses[] = $regs[1];
-		else $addresses[] = $a;
-      }
+/** The name of the forward sieve script */
+define('KOLAB_SIEVE_FORWARD', 'kolab-forward.siv')
+
+/** The name of the vacation sieve script */
+define('KOLAB_SIEVE_VACATION', 'kolab-vacation.siv')
+
+/** The name of the deliver sieve script */
+define('KOLAB_SIEVE_DELIVER', 'kolab-deliver.siv')
+
+class KolabSieve {
+
+    /**
+     * A sieve connection.
+     *
+     * @var NetSieve
+     */
+    var $_sieve;
+
+    // Funny multiline string escaping in Sieve
+    function dotstuff( $str ) {
+        return str_replace( "\n.", "\n..", $str );
     }
-    return $addresses;
-  }
 
-  /*static*/ function getMailDomain( $script ) {
-	$maildomain = false;
-	if( preg_match( '/if not address :domain :contains "From" "(.*)" { keep; stop; }/i', $script, $regs ) ) {
-	  $maildomain = $regs[1];
-	}
-	return $maildomain;
-  }
-  
-  /*static*/ function getReactToSpam( $script ) {
-	$spam = false;
-	if( preg_match('/if header :contains "X-Spam-Flag" "YES" { keep; stop; }/i', $script ) ) {
-	  $spam = true;
-	}
-	return $spam;
-  }
+    function undotstuff( $str ) {
+        return str_replace( "\n..", "\n.", $str );
+    }
+    
+    function getDeliverFolder( $script ) {
+        $inbox = false;      
+        if( preg_match("/fileinto \"INBOX\/(.*)\";/", $script, $regs ) ) {
+            $inbox = $regs[1];
+        }
+        return $inbox;
+    }
 
-  /*static*/ function getVacationDays( $script ) {
-    $days = false;
-    if( preg_match("/:days ([0-9]+)/s", $script, $regs ) ) {
-      $days = $regs[1];
+    function getVacationAddresses( $script ) {
+        $addresses = false;
+        if( preg_match("/:addresses \\[([^\\]]*)\\]/s", $script, $regs ) ) {
+            $tmp = split(',', $regs[1] );
+            $addresses = array();
+            foreach( $tmp as $a ) {
+                if( ereg('^ *"(.*)" *$', $a, $regs ) ) {
+                    $addresses[] = $regs[1];
+                } else {
+                    $addresses[] = $a;
+                }
+            }
+        }
+        return $addresses;
     }
-    return $days;
-  }
 
-  /*static*/ function getVacationText( $script ) {
-    $text = false;
-    if( preg_match("/text:(.*\r\n)\\.\r\n/s", $script, $regs ) ) {
-      $text = $regs[1];
-      $text = str_replace( '\n', "\r\n", $text );
-      $text = SieveUtils::undotstuff($text);
+    function getMailDomain( $script ) {
+        $maildomain = false;
+        if( preg_match( '/if not address :domain :contains "From" "(.*)" { keep; stop; }/i', $script, $regs ) ) {
+            $maildomain = $regs[1];
+        }
+        return $maildomain;
     }
-    return $text;
-  }
 
-  /*static*/ function getForwardAddress( $script ) {
-    $address = false;
-    if( preg_match("/redirect \"([^\"]*)\"/s", $script, $regs ) ) {
-      $address = $regs[1];
+    function getReactToSpam( $script ) {
+        $spam = false;
+        if( preg_match('/if header :contains "X-Spam-Flag" "YES" { keep; stop; }/i', $script ) ) {
+            $spam = true;
+        }
+        return $spam;
     }
-    return $address;
-  }
 
-  /*static*/ function getKeepOnServer( $script ) {
-    return ereg('"; keep;', $script, $regs );    
-  }
+    function getVacationDays( $script ) {
+        $days = false;
+        if( preg_match("/:days ([0-9]+)/s", $script, $regs ) ) {
+            $days = $regs[1];
+        }
+        return $days;
+    }
+
+    function getVacationText( $script ) {
+        $text = false;
+        if( preg_match("/text:(.*\r\n)\\.\r\n/s", $script, $regs ) ) {
+            $text = $regs[1];
+            $text = str_replace( '\n', "\r\n", $text );
+            $text = KolabSieve::undotstuff($text);
+        }
+        return $text;
+    }
+
+    function getForwardAddress( $script ) {
+        $address = false;
+        if( preg_match("/redirect \"([^\"]*)\"/s", $script, $regs ) ) {
+            $address = $regs[1];
+        }
+        return $address;
+    }
+
+    function getKeepOnServer( $script ) {
+        return ereg('"; keep;', $script, $regs );    
+    }
+
+    function createScript( $scriptname ) {
+        // TODO
+    }
+
+    
+    /**
+     * Initialize a sieve connection.
+     *
+     * @param string  $uid    The user id.
+     * @param string  $pass   The user pass.
+     * @param string  $server The server to connect to.
+     */
+    function KolabSieve($uid, $pass, $server)
+    {
+        $this->_sieve =& new Net_Sieve($uid, $pass, $server);
+    }
+
+    /**
+     * Store the forward sieve script.
+     *
+     * @param string  $address The address to forward to.
+     * @param boolean $keep    Keep the forwarded messages on the server?
+     * @param boolean $active  Set the forward script active?
+     *
+     * @return mixed True on success, a PEAR error otherwise
+     */
+    function storeForwardSieve($address, $keep, $active)
+    {
+        $script = "require \"fileinto\";\r\nredirect \"" . addslashes($address)
+            . "\";" . ($keep?" keep;":"");
+        return $this->storeSieve(KOLAB_SIEVE_FORWARD, $script, $active);
+
+    }
+    
+    /**
+     * Store the vacation sieve script.
+     *
+     * @param string  $address The address to forward to.
+     * @param boolean $keep    Keep the forwarded messages on the server?
+     * @param boolean $active  Set the forward script active?
+     *
+     * @return mixed True on success, a PEAR error otherwise
+     */
+    function storeForwardSieve($addresses, $maildomain, $reacttospam, $text, $days, $active)
+    {
+        $script = 
+            "require \"vacation\";\r\n\r\n".
+            (!empty($maildomain)?"if not address :domain :contains \"From\" \"".$maildomain."\" { keep; stop; }\r\n":"").
+            ($reacttospam?"if header :contains \"X-Spam-Flag\" \"YES\" { keep; stop; }\r\n":"").
+            "vacation :addresses [ \"".join('", "', $addresses )."\" ] :days ".
+            $days . " text:\r\n".
+            KolabSieve::dotstuff(trim($_REQUEST['text']))."\r\n.\r\n;\r\n\r\n";
+        return $this->storeSieve(KOLAB_SIEVE_FORWARD, $script, $active);
+    }
+    
+    /**
+     * Store the deliver sieve script.
+     *
+     * @param string  $inbox   Folder to deliver to
+     * @param boolean $active  Set the forward script active?
+     *
+     * @return mixed True on success, a PEAR error otherwise
+     */
+    function storeDeliverSieve($inbox, $active)
+    {
+        $script = 
+            "require \"fileinto\";\r\nif header :contains [\"X-Kolab-Scheduling-Message\"] [\"FALSE\"] {\r\nfileinto \"INBOX/$inbox\";\r\n}\r\n";
+        return $this->storeSieve(KOLAB_SIEVE_DELIVER, $script, $active);
+    }
+    
+    /**
+     * Store a new sieve script.
+     *
+     * @param string  $scriptname The name of the script
+     * @param string  $script     Script content
+     * @param boolean $active     Set the script active?
+     *
+     * @return mixed True on success, a PEAR error otherwise
+     */
+    function storeSieve($scriptname, $script, $active)
+    {
+        $result = $this->_sieve->installScript( $scriptname, $script, $active );
+        if (is_a($result, 'PEAR_Error')) {
+            return $result;
+        }
+        if (!$active && $this->_sieve->getActive() === $scriptname ) {
+            $result = $this->_sieve->setActive( '' );
+            if (is_a($result, 'PEAR_Error')) {
+                return $result;
+            }
+        }
+        return true;
+    }
+
+    /**
+     * Get forwarding information.
+     *
+     * @return array A list of information extracted from the forward
+     * script.
+     */
+    function getForwardSieve()
+    {
+        $scripts = $this->_sieve->listScripts();
+
+        if( in_array(KOLAB_SIEVE_FORWARD, $scripts ) ) {
+            $script = $this->_sieve->getScript(KOLAB_SIEVE_FORWARD);
+            $address = KolabSieve::getForwardAddress( $script );
+            $keep = KolabSieve::getKeepOnServer( $script );
+        } else {
+            $address = '';
+            $keep = true;
+        }
+        $active = ( $this->_sieve->getActive() === $scriptname );
+        return ($address, $keep, $active);
+    }
+
+    /**
+     * Get vacation information.
+     *
+     * @return array A list of information extracted from the vacation
+     * script.
+     */
+    function getVacationSieve()
+    {
+        $addresses = $days = $text = false;
+        $scripts = $this->_sieve->listScripts();
+        if( in_array( KOLAB_SIEVE_VACATION, $scripts ) ) {
+            $script = $this->_sieve->getScript(KOLAB_SIEVE_VACATION);
+            $maildomain = KolabSieve::getMailDomain( $script );
+            $reacttospam = KolabSieve::getReactToSpam( $script );
+            debug("reacttospam=".($reacttospam?"true":"false"));
+            $addresses = KolabSieve::getVacationAddresses( $script );
+            $days = KolabSieve::getVacationDays( $script );
+            $text = KolabSieve::getVacationText( $script );
+        } else $reacttospam = true;
+        if( $addresses === false ) {
+            $object = $ldap->read( $auth->dn() );
+            $addresses = array_merge( (array)$object['mail'], (array)$object['alias'] );
+        }
+        if( $days === false || $days < 1 ) $days = 7;
+        if( $text === false ) {
+            $date = strftime(_('%x'));
+            $text = sprintf(
+                _("I am out of office until %s.\r\n").
+                _("In urgent cases, please contact Mrs. <vacation replacement>\r\n\r\n").
+                _("email: <email address of vacation replacement>\r\n").
+                _("phone: +49 711 1111 11\r\n").
+                _("fax.:  +49 711 1111 12\r\n\r\n").
+                _("Yours sincerely,\r\n").
+                _("-- \r\n").
+                _("<enter your name and email address here>"),
+                $date);
+        }
+        $active = ( $this->_sieve->getActive() === KOLAB_SIEVE_VACATION);  
+        return ($addresses, $maildomain, $reacttospam, $text, $days, $active);
+    }
+
+    /**
+     * Get delivery information.
+     *
+     * @return array A list of information extracted from the deliver
+     * script.
+     */
+    function getDeliverSieve()
+    {
+        $scripts = $this->_sieve->listScripts();
+        $inbox = false;
+        if( in_array( $scriptname, $scripts ) ) {
+            // Fetch script data from server
+            $script = $this->_sieve->getScript($scriptname);
+            $inbox = KolabSieve::getDeliverFolder( $script );
+        }
+        if( $inbox === false ) $inbox = 'Inbox';
+        $active = ( $this->_sieve->getActive() === $scriptname );  
+    }
+}
 
-  function createScript( $scriptname ) {
-    // TODO
-  }
-};
 
 /*
   Local variables:
   mode: php
-  indent-tabs-mode: t
+  indent-tabs-mode: f
   tab-width: 4
   buffer-file-coding-system: utf-8
   End:

Index: webadmin.class.php
===================================================================
RCS file: /kolabrepository/server/php-kolab/Kolab_Webadmin/Webadmin/webadmin.class.php,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- webadmin.class.php	17 Aug 2007 18:23:48 -0000	1.2
+++ webadmin.class.php	20 Aug 2007 09:43:15 -0000	1.3
@@ -45,6 +45,12 @@
 /** Provides functionality for locale handling. */
 require_once('Kolab/Webadmin/locale.php');
 
+/** We need some sieve tools. */
+require_once 'Kolab/Webadmin/sieveutils.class.php';
+
+/** We need some form tools. */
+require_once 'Kolab/Webadmin/form.class.php';
+
 /**
  * This class combines methods common to most pages of the Kolab web
  * admin frontend.
@@ -73,6 +79,13 @@
     var $_auth;
 
     /**
+     * A sieve connection.
+     *
+     * @var KolabSieve
+     */
+    var $_sieve;
+
+    /**
      * An array of section accessible to the current user.
      *
      * @var array
@@ -90,8 +103,6 @@
      * Initialize the web admin class
      *
      * @param array $config An array of basic configuration variables.
-     *
-     * @return KolabWebadmin The initialized KolabWebadmin object
      */
     function KolabWebadmin($config) 
     {
@@ -297,6 +308,53 @@
     {
         return $this->_auth->group() == 'admin';
     }
+
+    /**
+     * Check if the current user is a maintainer.
+     *
+     * @return boolean true if the current user is a maintainer
+     */
+    function isMaintainer()
+    {
+        return $this->_auth->group() == 'maintainer';
+    }
+
+    /**
+     * Check if the current user is a domain maintainer.
+     *
+     * @return boolean true if the current user is a domain maintainer
+     */
+    function isDomainMaintainer()
+    {
+        return $this->_auth->group() == 'domain-maintainer';
+    }
+
+    /**
+     * Return the sieve connection.
+     */
+    function getSieve()
+    {
+        if (empty($this->_sieve)) {
+            $obj = $ldap->read( $auth->dn() );
+            $this->_sieve =& new KolabSieve( $auth->uid(), 
+                                             $auth->password(), 
+                                             $obj['kolabHomeServer'][0] );
+        }
+        return $this->_sieve
+    }
+
+    /**
+     * Run a text through htmlentities.
+     *
+     * @param string $text Text to transform
+     *
+     * @return string The transformed text.
+     */
+    function htmlentitites($text)
+    {
+        return KolabForm::htmlentities($text)
+    }
+    
 };
 
 /*





More information about the commits mailing list