[Svn] r3705 - in trunk: plugins/squirrelmail_usercopy roundcubemail

trac at roundcube.net trac at roundcube.net
Thu Jun 3 11:12:44 CEST 2010


Author: thomasb
Date: 2010-06-03 04:12:44 -0500 (Thu, 03 Jun 2010)
New Revision: 3705

Modified:
   trunk/plugins/squirrelmail_usercopy/config.inc.php.dist
   trunk/plugins/squirrelmail_usercopy/squirrelmail_usercopy.php
   trunk/roundcubemail/CHANGELOG
Log:
Add multiple identity and database support to squirrelmail_usercopy plugin (#1486517)

Modified: trunk/plugins/squirrelmail_usercopy/config.inc.php.dist
===================================================================
--- trunk/plugins/squirrelmail_usercopy/config.inc.php.dist	2010-06-03 08:21:43 UTC (rev 3704)
+++ trunk/plugins/squirrelmail_usercopy/config.inc.php.dist	2010-06-03 09:12:44 UTC (rev 3705)
@@ -1,5 +1,16 @@
 <?php
 
+// Driver - 'file' or 'sql'
+$rcmail_config['squirrelmail_driver'] = 'sql';
+
 // full path to the squirrelmail data directory
 $rcmail_config['squirrelmail_data_dir'] = '';
+$rcmail_config['squirrelmail_data_dir_hash_level'] = 0;
 
+// 'mysql://dbuser:dbpass@localhost/database'
+$rcmail_config['squirrelmail_dsn'] = 'mysql://user:password@localhost/webmail';
+$rcmail_config['squirrelmail_db_charset'] = 'iso-8859-1';
+
+$rcmail_config['squirrelmail_address_table'] = 'address';
+$rcmail_config['squirrelmail_userprefs_table'] = 'userprefs';
+

Modified: trunk/plugins/squirrelmail_usercopy/squirrelmail_usercopy.php
===================================================================
--- trunk/plugins/squirrelmail_usercopy/squirrelmail_usercopy.php	2010-06-03 08:21:43 UTC (rev 3704)
+++ trunk/plugins/squirrelmail_usercopy/squirrelmail_usercopy.php	2010-06-03 09:12:44 UTC (rev 3705)
@@ -3,20 +3,22 @@
 /**
  * Copy a new users identity and settings from a nearby Squirrelmail installation
  *
- * Currently only file-based data storage of Squirrelmail is supported.
- *
- * @version 1.1
- * @author Thomas Bruederli
+ * @version 1.2
+ * @author Thomas Bruederli, Johannes Hessellund, pommi, Thomas Lueder
  */
 class squirrelmail_usercopy extends rcube_plugin
 {
 	public $task = 'login|settings';
 
 	private $prefs = null;
+	private $identities_level = 0;
 	private $abook = array();
 
 	public function init()
 	{
+		$rcmail = rcmail::get_instance();
+		$this->identities_level = intval($rcmail->config->get('identities_level', 0));
+
 		$this->add_hook('create_user', array($this, 'create_user'));
 		$this->add_hook('create_identity', array($this, 'create_identity'));
 	}
@@ -25,9 +27,8 @@
 	{
 		// read prefs and add email address
 		$this->read_squirrel_prefs($p['user']);
-		if ($this->prefs['email_address'])
+		if (($this->identities_level == 0 || $identities_level == 2) && $this->prefs['email_address'])
 			$p['user_email'] = $this->prefs['email_address'];
-
 		return $p;
 	}
 
@@ -39,12 +40,30 @@
 		if ($rcmail->task == 'login' && $this->prefs) {
 			if ($this->prefs['full_name'])
 				$p['record']['name'] = $this->prefs['full_name'];
-			if ($this->prefs['email_address'])
+			if (($this->identities_level == 0 || $this->identities_level == 2) && $this->prefs['email_address'])
 				$p['record']['email'] = $this->prefs['email_address'];
-			if ($this->prefs['signature'])
-				$p['record']['signature'] = $this->prefs['signature'];
-                        if ($this->prefs['reply-to']) 
-                                $p['record']['reply-to'] = $this->prefs['reply-to']; 		
+			if ($this->prefs['___signature___'])
+				$p['record']['signature'] = $this->prefs['___signature___'];
+			if ($this->prefs['reply-to']) 
+				$p['record']['reply-to'] = $this->prefs['reply-to']; 
+			if (($this->identities_level == 0 || $this->identities_level == 1) && isset($this->prefs['identities']) && $this->prefs['identities'] > 1) {
+				for ($i=1; $i < $this->prefs['identities']; $i++) {
+					unset($ident_data);
+					$ident_data = array('name' => '', 'email' => ''); // required data
+					if ($this->prefs['full_name'.$i])
+						$ident_data['name'] = $this->prefs['full_name'.$i];
+					if ($this->identities_level == 0 && $this->prefs['email_address'.$i])
+						$ident_data['email'] = $this->prefs['email_address'.$i];
+					else
+						$ident_data['email'] = $p['record']['email'];
+					if ($this->prefs['reply_to'.$i])
+						$ident_data['reply-to'] = $this->prefs['reply_to'.$i];
+					if ($this->prefs['___sig'.$i.'___'])
+						$ident_data['signature'] = $this->prefs['___sig'.$i.'___'];
+					// insert identity
+					$identid = $rcmail->user->insert_identity($ident_data);
+				}
+			}
 
 			// copy address book
 			$contacts = $rcmail->get_address_book(null, true);
@@ -65,11 +84,15 @@
 		$this->load_config();
 		$rcmail = rcmail::get_instance();
 
-		if ($srcdir = $rcmail->config->get('squirrelmail_data_dir')) {
+		/**** File based backend ****/
+		if ($rcmail->config->get('squirrelmail_driver') == 'file' && ($srcdir = $rcmail->config->get('squirrelmail_data_dir'))) {
+			if (($hash_level = $rcmail->config->get('squirrelmail_data_dir_hash_level')) > 0) 
+				$srcdir = slashify($srcdir).chunk_split(substr(base_convert(crc32($uname), 10, 16), 0, $hash_level), 1, '/');
 			$prefsfile = slashify($srcdir) . $uname . '.pref';
 			$abookfile = slashify($srcdir) . $uname . '.abook';
 			$sigfile = slashify($srcdir) . $uname . '.sig';
-
+			$sigbase = slashify($srcdir) . $uname . '.si';
+      
 			if (is_readable($prefsfile)) {
 				$this->prefs = array();
 				foreach (file($prefsfile) as $line) {
@@ -79,9 +102,18 @@
 
 				// also read signature file if exists
 				if (is_readable($sigfile)) {
-					$this->prefs['signature'] = utf8_encode(file_get_contents($sigfile));
+					$this->prefs['___signature___'] = utf8_encode(file_get_contents($sigfile));
 				}
 
+				if (isset($this->prefs['identities']) && $this->prefs['identities'] > 1) {
+					for ($i=1; $i < $this->prefs['identities']; $i++) {
+						// read signature file if exists  
+						if (is_readable($sigbase.$i)) {
+							$this->prefs['___sig'.$i.'___'] = utf8_encode(file_get_contents($sigbase.$i));
+						}
+					}
+				}
+
 				// parse addres book file
 				if (filesize($abookfile)) {
 					foreach(file($abookfile) as $line) {
@@ -91,7 +123,46 @@
 					}
 				}
 			}
-		}
+		} 
+		/**** Database backend ****/
+		else if ($rcmail->config->get('squirrelmail_driver') == 'sql') { 
+			$this->prefs = array();
+
+			/* connect to squirrelmail database */
+			$db = new rcube_mdb2($rcmail->config->get('squirrelmail_dsn'));
+			$db->db_connect('r'); // connect in read mode
+
+			// $db->set_debug(true);
+			
+			/* retrieve prefs */
+			$userprefs_table = $rcmail->config->get('squirrelmail_userprefs_table');
+			$address_table = $rcmail->config->get('squirrelmail_address_table');
+			$db_charset = $rcmail->config->get('squirrelmail_db_charset');
+
+			$db->query('SET CHARACTER SET '.$db_charset);
+			$db->query('SET NAMES '.$db_encoding);
+
+			$sql_result = $db->query('SELECT * FROM '.$userprefs_table.' WHERE user=?', $uname); // ? is replaced with emailaddress
+
+			while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
+				$this->prefs[$sql_array['prefkey']] = rcube_charset_convert(rtrim($sql_array['prefval']), $db_charset);
+			}
+
+			/* retrieve address table data */
+			$sql_result = $db->query('SELECT * FROM '.$address_table.' WHERE owner=?', $uname); // ? is replaced with emailaddress
+			
+			// parse addres book
+			while ($sql_array = $db->fetch_assoc($sql_result) ) { // fetch one row from result
+				$rec['name']      = rcube_charset_convert(rtrim($sql_array['nickname']), $db_charset);
+				$rec['firstname'] = rcube_charset_convert(rtrim($sql_array['firstname']), $db_charset);
+				$rec['surname']   = rcube_charset_convert(rtrim($sql_array['lastname']), $db_charset);
+				$rec['email']     = rcube_charset_convert(rtrim($sql_array['email']), $db_charset);
+				$rec['note']      = rcube_charset_convert(rtrim($sql_array['label']), $db_charset);
+				
+				if ($rec['name'] && $rec['email'])
+					$this->abook[] = $rec;
+			}
+		} // end if 'sql'-driver
 	}
 
 }

Modified: trunk/roundcubemail/CHANGELOG
===================================================================
--- trunk/roundcubemail/CHANGELOG	2010-06-03 08:21:43 UTC (rev 3704)
+++ trunk/roundcubemail/CHANGELOG	2010-06-03 09:12:44 UTC (rev 3705)
@@ -1,6 +1,7 @@
 CHANGELOG RoundCube Webmail
 ===========================
 
+- Multiple identity and database support for squirrelmail_usercopy plugin (#1486517)
 - Support dynamic hostname (%d/%n) variables in configuration options (#1485438)
 - Add 'messages_list' hook (#1486266)
 - Add request* event triggers in http_post/http_request (#1486054)

_______________________________________________
http://lists.roundcube.net/mailman/listinfo/svn



More information about the Svn mailing list