📄 addressbook.cs
字号:
// -----------------------------------------------------------------------
//
// Copyright (C) 2003-2005 Angel Marin
//
// This file is part of SharpWebMail.
//
// SharpWebMail 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.
//
// SharpWebMail 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 SharpWebMail; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// -----------------------------------------------------------------------
using System;
namespace anmar.SharpWebMail.UI
{
public class AddressBook : System.Web.UI.Page {
protected static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected anmar.SharpWebMail.UI.globalUI SharpUI;
protected System.Web.UI.WebControls.DataGrid AddressBookDataGrid;
protected System.Web.UI.HtmlControls.HtmlSelect addressbookselect;
protected System.Web.UI.WebControls.Label addressbooklabel;
private System.Collections.Specialized.StringCollection _delete_items = null;
private System.String _sort_expression = "[NameColumn] ASC";
public static System.Collections.Specialized.ListDictionary GetAddressbook (System.String name, System.Object books) {
if ( name==null || name.Length==0 || books==null || !(books is System.Collections.SortedList) )
return null;
System.Collections.SortedList addressbooks = (System.Collections.SortedList)books;
if ( addressbooks.ContainsKey(name) ) {
return (System.Collections.Specialized.ListDictionary)addressbooks[name];
}
return null;
}
private static System.Data.Common.DbDataAdapter GetDataAdapter (System.String type, System.String connectstring, System.String connectusername, System.String connectpassword, System.String searchfilter) {
if ( type.Equals("odbc") )
return new System.Data.Odbc.OdbcDataAdapter(searchfilter, connectstring);
else if ( type.Equals("oledb") )
return new System.Data.OleDb.OleDbDataAdapter(searchfilter, connectstring);
else
return null;
}
public static System.Data.DataTable GetDataSource (System.Collections.Specialized.ListDictionary addressbook, bool specific, anmar.SharpWebMail.IEmailClient client ) {
if ( !addressbook.Contains("connectionstring")
|| !addressbook.Contains("searchstring") )
return null;
System.String connectstring = addressbook["connectionstring"].ToString();
System.String connectusername = null, connectpassword = null;
if ( addressbook.Contains("connectionusername") && addressbook.Contains("connectionpassword") ) {
connectusername = addressbook["connectionusername"].ToString();
connectpassword = addressbook["connectionpassword"].ToString();
} else if ( client!=null ) {
connectusername = client.UserName;
connectpassword = client.Password;
}
System.String searchfilter;
if ( specific )
searchfilter = addressbook["searchstringrealname"].ToString();
else
searchfilter = addressbook["searchstring"].ToString();
if ( client!=null )
searchfilter=searchfilter.Replace("$USERNAME$", client.UserName);
else
searchfilter=searchfilter.Replace("$USERNAME$", System.String.Empty);
System.String namecolumn = addressbook["namecolumn"].ToString();
System.String mailcolumn = addressbook["emailcolumn"].ToString();
System.String ownercolumn = "owner";
if ( addressbook.Contains("usernamecolumn") )
ownercolumn = addressbook["usernamecolumn"].ToString();
if ( addressbook["type"].Equals("ldap") )
return GetDataSourceLDAP(addressbook["name"].ToString(), connectstring, connectusername, connectpassword, searchfilter, namecolumn, mailcolumn, ownercolumn);
else if ( addressbook["type"].Equals("odbc") )
return GetDataSourceODBC(addressbook["name"].ToString(), connectstring, connectusername, connectpassword, searchfilter, namecolumn, mailcolumn, ownercolumn);
else if ( addressbook["type"].Equals("oledb") )
return GetDataSourceOLEDB(addressbook["name"].ToString(), connectstring, connectusername, connectpassword, searchfilter, namecolumn, mailcolumn, ownercolumn);
else
return null;
}
private static System.Data.DataTable GetDataSourceData (System.Data.Common.DbDataAdapter adapter, System.String namecolumn, System.String mailcolumn, System.String ownercolumn, System.String book) {
if ( adapter==null )
return null;
System.Data.DataTable data = GetDataSourceDataTable (namecolumn, mailcolumn, ownercolumn, book);
try {
adapter.Fill(data);
} catch ( System.Exception e ) {
if ( log.IsErrorEnabled )
log.Error("Error while doing query", e);
return null;
}
return data;
}
private static System.Data.DataTable GetDataSourceDataTable (System.String namecolumn, System.String mailcolumn, System.String ownercolumn, System.String book) {
System.Data.DataTable table = new System.Data.DataTable("addressbook");
table.Columns.Add(new System.Data.DataColumn(namecolumn, typeof(System.String)));
table.Columns.Add(new System.Data.DataColumn(mailcolumn, typeof(System.String)));
table.Columns.Add(new System.Data.DataColumn("addressbook", typeof(System.String)));
table.Columns.Add(new System.Data.DataColumn(ownercolumn, typeof(System.String)));
table.Columns[1].Unique = true;
table.Columns[2].DefaultValue = book;
return table;
}
private static System.Data.DataTable GetDataSourceLDAP (System.String book, System.String connectstring, System.String connectusername, System.String connectpassword, System.String searchfilter, System.String namecolumn, System.String mailcolumn, System.String ownercolumn) {
System.Data.DataTable datasource = GetDataSourceDataTable(namecolumn, mailcolumn, ownercolumn, book);
System.DirectoryServices.DirectoryEntry direntry = new System.DirectoryServices.DirectoryEntry(connectstring);
direntry.Username = connectusername;
direntry.Password = connectpassword;
System.DirectoryServices.DirectorySearcher dirsearcher = new System.DirectoryServices.DirectorySearcher(direntry);
dirsearcher.Filter = searchfilter;
dirsearcher.SearchScope = System.DirectoryServices.SearchScope.OneLevel;
dirsearcher.PropertiesToLoad.Add(namecolumn);
dirsearcher.PropertiesToLoad.Add(mailcolumn);
System.DirectoryServices.SearchResultCollection results = null;
try {
results = dirsearcher.FindAll();
} catch ( System.Exception e) {
if (log.IsErrorEnabled)
log.Error("Error while doing LDAP query", e);
return null;
}
System.String name, value;
foreach ( System.DirectoryServices.SearchResult result in results ) {
name = null;
value = null;
if ( result.Properties.Contains(namecolumn) && result.Properties.Contains(mailcolumn) && result.Properties[namecolumn].Count>0 && result.Properties[mailcolumn].Count>0 ) {
name = result.Properties[namecolumn][0].ToString();
value = result.Properties[mailcolumn][0].ToString();
}
if ( name!=null && value!=null ) {
try {
datasource.Rows.Add(new object[]{name, value});
} catch ( System.Exception ){}
}
}
return datasource;
}
private static System.Data.DataTable GetDataSourceODBC (System.String book, System.String connectstring, System.String connectusername, System.String connectpassword, System.String searchfilter, System.String namecolumn, System.String mailcolumn, System.String ownercolumn) {
return GetDataSourceData(GetDataAdapter("odbc", connectstring, connectusername, connectpassword, searchfilter), namecolumn, mailcolumn, ownercolumn, book);
}
private static System.Data.DataTable GetDataSourceOLEDB (System.String book, System.String connectstring, System.String connectusername, System.String connectpassword, System.String searchfilter, System.String namecolumn, System.String mailcolumn, System.String ownercolumn) {
return GetDataSourceData(GetDataAdapter("oledb", connectstring, connectusername, connectpassword, searchfilter), namecolumn, mailcolumn, ownercolumn, book);
}
public static bool UpdateDataSource (System.Data.DataTable data, System.Collections.Specialized.ListDictionary addressbook, anmar.SharpWebMail.IEmailClient client) {
bool error = false;
if ( data==null || addressbook==null || !addressbook.Contains("connectionstring")
|| !addressbook.Contains("searchstring") || !addressbook.Contains("allowupdate") || !((bool)addressbook["allowupdate"]) )
return false;
System.String connectstring = addressbook["connectionstring"].ToString();
System.String connectusername = null, connectpassword = null;
if ( addressbook.Contains("connectionusername") && addressbook.Contains("connectionpassword") ) {
connectusername = addressbook["connectionusername"].ToString();
connectpassword = addressbook["connectionpassword"].ToString();
} else if ( client!=null ) {
connectusername = client.UserName;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -