[dev] update to cyrsql

Ilya mail at krel.org
Thu Feb 13 01:32:39 PST 2003


The attached diff introduces virtual domain support for cyrus (2.2a).
It is backwards compatible. Only if 'domain' is set the new code kicks in.
This is not finished yet, user rename is not supported now. User creation in
defaultdomain (without @fqdn.com part) is not supported.
If 'domain' is set users can only be created as user at virtualdomain.com.
There also seems to be a bug in deleting virtual users (subfolders are
left over).
If anyone is using this, and wants to give it a try or add/test any above
features please do so.

-------------- next part --------------
--- cyrsql.php.orig	Wed Feb 12 22:18:58 2003
+++ cyrsql.php	Thu Feb 13 01:23:21 2003
@@ -32,6 +32,9 @@
  *                    Doesn't create subfolders by default
  *   'quota'          The quota (in kilobytes) to grant on the mailbox.
  *                    Does not establish quota by default.
+ *   'domain_field'   If set to anything other than 'none' this is used as
+ *                    field name where domain is stored. Default is unset, so 
+ *                    domain is not used/shown
  *
  * Required by some database implementations:
  *   'options'       Additional options to pass to the database.
@@ -102,9 +105,24 @@
     {
         $this->_connect();
 
-        $result = parent::addUser($userID, $credentials);
-        if (DB::isError($result)) {
-            return $result;
+        if (array_key_exists('domain_field', $this->_params) &&
+                            ($this->_params['domain_field'] != 'none')){
+            list($name,$domain)=explode('@',$userID);
+        	/* Build the SQL query. */
+            $query = sprintf('INSERT INTO %s (%s, %s, %s) VALUES (%s, %s, %s)',
+                            $this->_params['table'],
+                            $this->_params['username_field'],
+                            $this->_params['domain_field'],
+                            $this->_params['password_field'],
+                            $this->_db->quote($name),
+                            $this->_db->quote($domain),
+                            $this->_db->quote(md5($credentials['password'])));
+            $dbresult = $this->_db->query($query);
+        } else {
+            $dbresult = parent::addUser($userID, $credentials);
+        }
+        if (DB::isError($dbresult)) {
+            return $dbresult;
         }
 
         $name = imap_utf7_encode($userID);
@@ -144,21 +162,36 @@
     {
         $this->_connect();
 
-        $dbresult = parent::removeUser($userID);
+        if (array_key_exists('domain_field', $this->_params) &&
+                            ($this->_params['domain_field'] != 'none')){
+            list($name,$domain)=explode('@',$userID);
+            /* Build the SQL query. */
+            $query = sprintf('DELETE FROM %s WHERE %s = %s and %s = %s',
+                         $this->_params['table'],
+                         $this->_params['username_field'],
+                         $this->_db->quote($name),
+                         $this->_params['domain_field'],
+                         $this->_db->quote($domain));
+            $dbresult = $this->_db->query($query);
+        } else {
+            $dbresult = parent::removeUser($userID);
+        }
+
+        if (DB::isError($dbresult)) {
+            return $dbresult;
+        }
 
         /* Set ACL for mailbox deletion. */
+        list($admin)=explode('@',$this->_params['cyradmin']);
         @imap_setacl($this->_imapStream, 
                      'user' . $this->_separator . $userID,
-                     $this->_params['cyradmin'], 'lrswipcda');
+                     $admin, 'lrswipcda');
 
         /* Delete IMAP mailbox. */
         $imapresult = @imap_deletemailbox($this->_imapStream, 
                                           $this->_params['imap_dsn'] . 
                                           'user' . $this->_separator . $userID);
 
-        if (DB::isError($dbresult)) {
-            return $dbresult;
-        }
         if (!$imapresult) {
             return PEAR::raiseError(sprintf(_("IMAP mailbox deletion failed: %s"), imap_last_error()));
         }
@@ -231,4 +264,99 @@
                                      $this->_separator . $value));
     }
 
+    /**
+     * List all users in the system.
+     *
+     * @access public
+     *
+     * @return mixed  The array of userIDs, or false on failure/unsupported.
+     */
+    function listUsers()
+    {
+        /* _connect() will die with Horde::fatal() upon failure. */
+        $this->_connect();
+
+        if (array_key_exists('domain_field', $this->_params) && 
+                            ($this->_params['domain_field'] != 'none')){
+            /* Build the SQL query with domain. */
+            $query = sprintf('SELECT %s , %s FROM %s ORDER BY %s',
+                         $this->_params['username_field'],
+                         $this->_params['domain_field'],
+                         $this->_params['table'],
+                         $this->_params['username_field']);
+        } else {
+            /* Build the SQL query without domain. */
+            $query = sprintf('SELECT %s FROM %s ORDER BY %s',
+                         $this->_params['username_field'],
+                         $this->_params['table'],
+                         $this->_params['username_field']);
+       }
+
+        $result = $this->_db->getAll($query, null, DB_FETCHMODE_ORDERED);
+        if (PEAR::isError($result)) {
+            return $result;
+        }
+
+        /* Loop through and build return array. */
+        $users = array();
+        if (array_key_exists('domain_field', $this->_params) &&
+                            ($this->_params['domain_field'] != 'none')){
+            foreach ($result as $ar) {
+           	   $users[] = $ar[0] . '@' . $ar[1];
+            }
+        } else {
+            foreach ($result as $ar) {
+               $users[] = $ar[0];
+            }
+        }
+
+        return $users;
+    }
+    
+    /**
+     * Update a set of authentication credentials.
+     *
+     * @access public
+     *
+     * @param string $oldID       The old userID.
+     * @param string $newID       The new userID.
+     * @param array $credentials  The new credentials
+     *
+     * @return mixed  True on success or a PEAR_Error object on failure.
+     */
+
+    function updateUser($oldID, $newID, $credentials)
+    {
+        /* _connect() will die with Horde::fatal() upon failure. */
+        $this->_connect();
+
+        if (array_key_exists('domain_field', $this->_params) &&
+                            ($this->_params['domain_field'] != 'none')){
+            list($name,$domain)=explode('@',$oldID);
+            /* Build the SQL query with domain. */
+            $query = sprintf('UPDATE %s SET %s = %s WHERE %s = %s and %s = %s',
+                         $this->_params['table'],
+                         $this->_params['password_field'],
+                         $this->_db->quote(md5($credentials['password'])),
+                         $this->_params['username_field'],
+                         $this->_db->quote($name),
+                         $this->_params['domain_field'],
+                         $this->_db->quote($domain));
+        } else {
+           /* Build the SQL query. */
+            $query = sprintf('UPDATE %s SET %s = %s WHERE %s = %s',
+                         $this->_params['table'],
+                         $this->_params['password_field'],
+                         $this->_db->quote(md5($credentials['password'])),
+                         $this->_params['username_field'],
+                         $this->_db->quote($oldID)); 
+        }
+
+        $dbresult = $this->_db->query($query);
+        if (DB::isError($dbresult)) {
+            return $dbresult;
+        }
+
+        return true;
+    }
 }


More information about the dev mailing list