2195 lines
78 KiB
C#
2195 lines
78 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.IO;
|
||
using System.IO.Compression;
|
||
using System.Net.Mail;
|
||
using System.Configuration;
|
||
using System.Drawing;
|
||
using System.Drawing.Imaging;
|
||
using System.Web;
|
||
using System.Web.UI;
|
||
using System.Web.UI.WebControls;
|
||
using System.Data.OleDb;
|
||
using Microsoft.VisualBasic;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Web.Security;
|
||
using System.Security.Cryptography;
|
||
|
||
namespace MyWeb
|
||
{
|
||
|
||
#region 全域設定
|
||
|
||
public class global : System.Web.UI.Page
|
||
{
|
||
//圖片品質
|
||
public int ImageCompressionQuality = 80;
|
||
public System.Drawing.Drawing2D.InterpolationMode InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Default;
|
||
public System.Drawing.Drawing2D.SmoothingMode SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
|
||
public System.Drawing.Drawing2D.CompositingQuality CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
|
||
}
|
||
|
||
public interface Nav
|
||
{
|
||
void Set(List<ListItem> nav);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 語系
|
||
|
||
public class language : System.Web.UI.Page
|
||
{
|
||
|
||
public struct langOption
|
||
{
|
||
public string Text;
|
||
public string Value;
|
||
public string Dir;
|
||
}
|
||
|
||
//語系設定
|
||
public ArrayList languageSet()
|
||
{
|
||
|
||
langOption langOption = new langOption();
|
||
ArrayList language = new ArrayList();
|
||
|
||
//如果不需要語系,只需保留一個預設語系即可
|
||
langOption.Text = "繁體中文";
|
||
langOption.Value = "TW";
|
||
langOption.Dir = "tw";
|
||
language.Add(langOption);
|
||
|
||
|
||
return language;
|
||
|
||
}
|
||
|
||
//建立語系下拉
|
||
|
||
public void buildDropDownList(DropDownList obj, string value, object container)
|
||
{
|
||
bool nationShow = isMultiLanguage();
|
||
|
||
ArrayList language = languageSet();
|
||
for (int i = 0; i <= language.Count - 1; i++)
|
||
{
|
||
obj.Items.Add(new ListItem(((langOption)language[i]).Text, ((langOption)language[i]).Value));
|
||
}
|
||
if (language.Count > 0 & value != "" & value != null)
|
||
{
|
||
obj.SelectedValue = value;
|
||
}
|
||
if (!nationShow)
|
||
{
|
||
if (obj.Items[0].Value == null || obj.Items[0].Value == "")
|
||
{
|
||
obj.Items.Remove(obj.Items[0]);
|
||
}
|
||
obj.Visible = false;
|
||
//如果只有一個語系就不顯示
|
||
}
|
||
|
||
if ((container != null))
|
||
{
|
||
((Control)container).Visible = nationShow;
|
||
//將語系下拉的容器也隱藏或顯示
|
||
}
|
||
|
||
}
|
||
|
||
//傳回目前的語系
|
||
public string nation()
|
||
{
|
||
ArrayList language = languageSet();
|
||
string nationValue = defaultNation();
|
||
//預設為第一個語系
|
||
string tmp = HttpContext.Current.Request.Url.AbsolutePath.ToLower();
|
||
string[] uri = tmp.Split('/');
|
||
for (int i = 0; i <= uri.Length - 1; i++)
|
||
{
|
||
for (int j = 0; j <= language.Count - 1; j++)
|
||
{
|
||
if (uri[i].ToLower() == ((langOption)language[j]).Dir.ToLower())
|
||
{
|
||
nationValue = ((langOption)language[j]).Value;
|
||
break; // TODO: might not be correct. Was : Exit For
|
||
}
|
||
}
|
||
}
|
||
return nationValue;
|
||
}
|
||
|
||
//傳回預設語系
|
||
public string defaultNation()
|
||
{
|
||
ArrayList language = languageSet();
|
||
return ((langOption)language[0]).Value;
|
||
}
|
||
|
||
//是否為多語系
|
||
public bool isMultiLanguage()
|
||
{
|
||
if (languageSet().Count > 1)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
//傳回對應的語系
|
||
public string Text(string Value)
|
||
{
|
||
ArrayList language = languageSet();
|
||
string nationTxt = "";
|
||
for (int i = 0; i <= language.Count - 1; i++)
|
||
{
|
||
if (((langOption)language[i]).Value == Value)
|
||
{
|
||
nationTxt = ((langOption)language[i]).Text;
|
||
break; // TODO: might not be correct. Was : Exit For
|
||
}
|
||
}
|
||
return nationTxt;
|
||
}
|
||
|
||
//傳回對應的語系資料夾
|
||
public string Dir(string Value)
|
||
{
|
||
ArrayList language = languageSet();
|
||
string nationDir = ((langOption)language[0]).Dir;
|
||
for (int i = 0; i <= language.Count - 1; i++)
|
||
{
|
||
if (((langOption)language[i]).Value == Value)
|
||
{
|
||
nationDir = ((langOption)language[i]).Dir;
|
||
break; // TODO: might not be correct. Was : Exit For
|
||
}
|
||
}
|
||
return nationDir;
|
||
}
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 資料庫
|
||
|
||
public class sql : System.Web.UI.Page
|
||
{
|
||
|
||
public OleDbConnection conn(string db, string p_name)
|
||
{
|
||
OleDbConnection con = new OleDbConnection(db);
|
||
return con;
|
||
}
|
||
|
||
public bool isSQL
|
||
{
|
||
get { return ConfigurationManager.ConnectionStrings["shopConn"].ProviderName == "System.Data.SqlClient" ? true : false; }
|
||
set { }
|
||
}
|
||
|
||
public DataTable dataTable(OleDbCommand sqlCmd)
|
||
{
|
||
System.Data.DataTable sqlTable = new System.Data.DataTable();
|
||
OleDbDataAdapter sqlAdapter = new OleDbDataAdapter(sqlCmd);
|
||
sqlAdapter.Fill(sqlTable);
|
||
return sqlTable;
|
||
}
|
||
|
||
public bool dbIsSql()
|
||
{
|
||
return isSQL;
|
||
}
|
||
|
||
public string mark(string column)
|
||
{
|
||
column = column.Replace("[", "");
|
||
column = column.Replace("]", "");
|
||
string value = "";
|
||
string[] c = column.Split(',');
|
||
for (int i = 0; i < c.Length; i++)
|
||
{
|
||
value += (value != "" ? "," : "") + "?";
|
||
}
|
||
return value;
|
||
}
|
||
|
||
public string mark2(string column)
|
||
{
|
||
column = column.Replace("[", "");
|
||
column = column.Replace("]", "");
|
||
string value = "";
|
||
string[] c = column.Split(',');
|
||
for (int i = 0; i < c.Length; i++)
|
||
{
|
||
value += (value != "" ? "," : "") + "[" + c[i] + "]=?";
|
||
}
|
||
return value;
|
||
}
|
||
|
||
public DataTable selectTable(string db, string p_name, string sqlCommand)
|
||
{
|
||
|
||
System.Data.DataTable sqlTable = new System.Data.DataTable();
|
||
OleDbConnection sqlConn = conn(db, p_name);
|
||
int sqlTableRow = 0;
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = sqlCommand;
|
||
OleDbDataAdapter sqlAdapter = new OleDbDataAdapter(sqlCmd);
|
||
sqlAdapter.Fill(sqlTable);
|
||
sqlTableRow = sqlTable.Rows.Count;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
HttpContext.Current.Response.Write("config.selectTable:" + ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
return sqlTable;
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region 網站設定
|
||
|
||
|
||
public class company : function
|
||
{
|
||
public string scc = ConfigurationManager.AppSettings["shopCarCode"].ToString();
|
||
|
||
public struct WebInfo
|
||
{
|
||
public string name;
|
||
public string email;
|
||
public string url;
|
||
public ArrayList bcc;
|
||
}
|
||
|
||
WebInfo info = new WebInfo();
|
||
|
||
public string name
|
||
{
|
||
get { return info.name; }
|
||
set { }
|
||
}
|
||
public string email
|
||
{
|
||
get { return info.email; }
|
||
set { }
|
||
}
|
||
public string url
|
||
{
|
||
get { return info.url; }
|
||
set { }
|
||
}
|
||
public ArrayList bcc
|
||
{
|
||
get { return info.bcc; }
|
||
set { }
|
||
}
|
||
|
||
|
||
public company()
|
||
{
|
||
if (isStrNull(HttpContext.Current.Application[scc + "WebInfo"]) || scc == "eztrust")
|
||
{
|
||
info.bcc = new ArrayList();
|
||
config config = new config();
|
||
sql sql = new sql();
|
||
OleDbConnection sqlConn = sql.conn(config.db, config.p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "Select top 1 * from [company] order by num";
|
||
DataTable dt = sql.dataTable(sqlCmd);
|
||
if (dt.Rows.Count > 0)
|
||
{
|
||
info.name = dt.Rows[0]["com_name"].ToString();
|
||
info.email = dt.Rows[0]["com_mail"].ToString();
|
||
info.url = dt.Rows[0]["pic_url"].ToString();
|
||
if (dt.Rows[0]["bcc_mail"] != null)
|
||
{
|
||
string[] bccTmp = dt.Rows[0]["bcc_mail"].ToString().Replace(Convert.ToString((char)13), "").Split(Convert.ToChar((char)10));
|
||
for (int i = 0; i <= bccTmp.Length - 1; i++)
|
||
{
|
||
if (bccTmp[i].ToString().Trim() != null & bccTmp[i].ToString().Trim() != "")
|
||
{
|
||
info.bcc.Add(bccTmp[i].ToString().Trim());
|
||
}
|
||
}
|
||
}
|
||
if (scc != "eztrust")
|
||
HttpContext.Current.Application[scc + "WebInfo"] = info;
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
info = (WebInfo)HttpContext.Current.Application[scc + "WebInfo"];
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region 後台所有頁面套用
|
||
|
||
public class config : function
|
||
{
|
||
public string db = ConfigurationManager.ConnectionStrings["shopConn"].ConnectionString;
|
||
public string p_name = ConfigurationManager.ConnectionStrings["shopConn"].ProviderName;
|
||
public string scc = ConfigurationManager.AppSettings["shopCarCode"].ToString();
|
||
DataTable menuDt = new DataTable();
|
||
public admin admin = new admin();
|
||
|
||
protected override void OnPreInit(System.EventArgs e)
|
||
{
|
||
base.OnPreInit(e);
|
||
|
||
if (admin.chkAdmIP && (admin.chkTwIP || admin.chkAdmIP_Enable))
|
||
{
|
||
sql sql = new sql();
|
||
|
||
//判斷是否登入
|
||
if (!admin.isLoign())
|
||
{
|
||
HttpContext.Current.Response.Redirect("~/admin/index.aspx?msg=A1");
|
||
}
|
||
else
|
||
{
|
||
if (admin.info.login_ip != admin.MyIP)
|
||
{
|
||
HttpContext.Current.Response.Redirect("~/admin/index.aspx?msg=E");
|
||
}
|
||
|
||
foreach (string key in HttpContext.Current.Request.Form)
|
||
{
|
||
if (HttpContext.Current.Request.Form[key].ToLower().IndexOf("<img") > -1 && HttpContext.Current.Request.Form[key].ToLower().IndexOf("base64") > -1)
|
||
{
|
||
HttpContext.Current.Response.Clear();
|
||
HttpContext.Current.Response.Write("請勿使用base64編碼的圖片置於內容中");
|
||
HttpContext.Current.Response.End();
|
||
}
|
||
}
|
||
string item_reg_key = null;
|
||
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
if (admin.info.group == "EZ")
|
||
{
|
||
item_reg_key = "y";
|
||
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "SELECT * FROM item Where root<>0 order by root, range";
|
||
menuDt = sql.dataTable(sqlCmd);
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
}
|
||
else if (!isStrNull(admin.info.power))
|
||
{
|
||
string v = admin.info.power;
|
||
if (Right(v, 1) == ",") { v = Left(v, v.Length - 1); }
|
||
if (Left(v, 1) == ",") { v = Right(v, v.Length - 1); }
|
||
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "SELECT * FROM item Where num in (" + (!isStrNull(v) && IsNumeric(v) ? v : "-1") + ") AND root<>0 order by root, range";
|
||
menuDt = sql.dataTable(sqlCmd);
|
||
|
||
//判斷網址權限
|
||
foreach (DataRow row in menuDt.Rows)
|
||
{
|
||
|
||
if (HttpContext.Current.Request.ServerVariables["URL"].IndexOf(row["url"].ToString().Replace("~/", ""), 0) > -1)
|
||
{
|
||
item_reg_key = "y";
|
||
break;
|
||
}
|
||
else
|
||
{
|
||
//判斷相關檔案中,是否有符合條件的網址
|
||
if (row["other_url"].ToString() != "")
|
||
{
|
||
string[] vv = row["other_url"].ToString().Split(',');
|
||
for (int j = 0; j < vv.Length; j++)
|
||
{
|
||
if (HttpContext.Current.Request.ServerVariables["URL"].IndexOf(vv[j].Replace("~/", ""), 0) > -1)
|
||
{
|
||
item_reg_key = "y";
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
}
|
||
|
||
|
||
//若連結的網址沒權限,則導至後台首頁
|
||
if (item_reg_key == "y")
|
||
{
|
||
this.Page.MasterPageFile = "~/admin/Templates/TBS5ADM001/MasterPage.master";
|
||
//this.Theme = "Theme1";
|
||
}
|
||
else
|
||
{
|
||
HttpContext.Current.Response.Redirect("~/admin/index2.aspx");
|
||
}
|
||
|
||
|
||
try
|
||
{
|
||
IMenu menu = (IMenu)Master.FindControl("menu");
|
||
if (menu != null) {
|
||
menu.Data(menuDt);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
|
||
}
|
||
|
||
|
||
|
||
}
|
||
}
|
||
else
|
||
{
|
||
HttpContext.Current.Response.Clear();
|
||
HttpContext.Current.Response.StatusCode = 404;
|
||
HttpContext.Current.Response.End();
|
||
}
|
||
|
||
}
|
||
|
||
public interface IMenu
|
||
{
|
||
void Data(DataTable dt);
|
||
}
|
||
|
||
public bool dbIsSql()
|
||
{
|
||
//判斷SQL或Access資料庫
|
||
if (p_name == "System.Data.SqlClient")
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public DataTable KindDt(string sqlTable, string nation, bool hasStatus)
|
||
{
|
||
DataTable dt = new DataTable();
|
||
MyWeb.sql sql = new MyWeb.sql();
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "SELECT num,kind,root FROM [" + sqlTable + "] ";
|
||
if(hasStatus)
|
||
sqlCmd.CommandText += " where status='Y'";
|
||
sqlCmd.CommandText += " order by kind,root, range";
|
||
dt = sql.dataTable(sqlCmd);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Response.Write(ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close();
|
||
sqlConn.Dispose();
|
||
}
|
||
return dt;
|
||
}
|
||
|
||
|
||
public void buildMultiKind(DropDownList kind, string sqlTable, int root, string nation, int level, int levelMax, DataTable dt = null,bool hasStatus=false )
|
||
{
|
||
if (dt == null)
|
||
{
|
||
dt = KindDt(sqlTable, nation, hasStatus);
|
||
}
|
||
|
||
string d = "";
|
||
for (int i = 1; i < level; i++)
|
||
{
|
||
d += "…";
|
||
}
|
||
|
||
foreach (DataRow row in dt.Rows)
|
||
{
|
||
if (row["root"].ToString() == root.ToString())
|
||
{
|
||
kind.Items.Add(new ListItem(d + row["kind"].ToString(), row["num"].ToString()));
|
||
if (level + 1 <= levelMax)
|
||
{
|
||
buildMultiKind(kind, sqlTable, (int)row["num"], nation, level + 1, levelMax, dt);
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
public string getNationTxt(string nation)
|
||
{
|
||
MyWeb.language language = new MyWeb.language();
|
||
return language.Text(nation).ToString();
|
||
}
|
||
|
||
public string sqlColumns(string column, string table, int num)
|
||
{
|
||
|
||
string data = "";
|
||
MyWeb.sql sql = new MyWeb.sql();
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "select [" + column + "] from [" + table + "] where [num]=?";
|
||
sqlCmd.Parameters.Add(new OleDbParameter("num", num));
|
||
System.Data.DataTable dt = sql.dataTable(sqlCmd);
|
||
if (dt.Rows.Count > 0)
|
||
{
|
||
data = dt.Rows[0][column].ToString();
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
return data;
|
||
|
||
}
|
||
|
||
//針對某資料表的所有排序+1
|
||
|
||
public void sort_add_one(string tb, string query)
|
||
{
|
||
MyWeb.sql sql = new MyWeb.sql();
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "update " + tb + " set range=range+1";
|
||
if (!isStrNull(query))
|
||
{
|
||
sqlCmd.CommandText += " where " + query;
|
||
}
|
||
sqlCmd.ExecuteNonQuery();
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
|
||
}
|
||
|
||
|
||
public string NoNationUrl()
|
||
{
|
||
string functionReturnValue = "";
|
||
functionReturnValue = HttpContext.Current.Request.Url.AbsoluteUri.Split('?')[0];
|
||
if (HttpContext.Current.Request.Url.AbsoluteUri.Split('?').Length > 1)
|
||
{
|
||
string[] query = HttpContext.Current.Request.Url.AbsoluteUri.Split('?')[1].Split('&');
|
||
int i = 0;
|
||
int j = 0;
|
||
for (i = 0; i <= query.Length - 1; i++)
|
||
{
|
||
if (query[i].Split('=')[0] != "nation")
|
||
{
|
||
j += 1;
|
||
functionReturnValue += (j == 1 ? "?" : "&") + query[i];
|
||
}
|
||
}
|
||
}
|
||
return functionReturnValue;
|
||
}
|
||
|
||
public object selectDate(TextBox obj)
|
||
{
|
||
if (obj.Text == "" | obj.Text == null)
|
||
{
|
||
return DBNull.Value;
|
||
}
|
||
else
|
||
{
|
||
return ValDate(obj.Text);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
public string get_navi()
|
||
{
|
||
//取得導覽列
|
||
|
||
string[] url = HttpContext.Current.Request.Url.AbsolutePath.ToLower().Split('/');
|
||
string u = "~";
|
||
bool r = false;
|
||
for (int i = 0; i < url.Length; i++)
|
||
{
|
||
if (url[i] == "admin") { r = true; }
|
||
if (r) { u += "/" + url[i]; }
|
||
}
|
||
|
||
string navText = "";
|
||
|
||
MyWeb.sql sql = new MyWeb.sql();
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "select top 1 [title],[root] from [item] where [url]=?";
|
||
sqlCmd.Parameters.Add(new OleDbParameter("url", u));
|
||
System.Data.DataTable dt = sql.dataTable(sqlCmd);
|
||
if (dt.Rows.Count > 0)
|
||
{
|
||
navText = rtn_navi(u, dt);
|
||
}
|
||
else
|
||
{
|
||
sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "select top 1 [title],[root] from [item] where ";
|
||
//判斷SQL或Access資料庫
|
||
if (dbIsSql())
|
||
{
|
||
sqlCmd.CommandText += " other_url like N'%" + u + "%'";
|
||
}
|
||
else
|
||
{
|
||
sqlCmd.CommandText += " InStr(1,LCase(other_url),LCase('" + u + "'),0)<>0";
|
||
}
|
||
dt = sql.dataTable(sqlCmd);
|
||
if (dt.Rows.Count > 0)
|
||
{
|
||
navText = rtn_navi(u, dt);
|
||
}
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
HttpContext.Current.Response.Write("config.get_navi:" + ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
|
||
return navText;
|
||
|
||
}
|
||
|
||
public string rtn_navi(string u, System.Data.DataTable dt)
|
||
{
|
||
|
||
string tmp = dt.Rows[0]["title"].ToString();
|
||
|
||
MyWeb.sql sql = new MyWeb.sql();
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "select [title] from [item] where num=?";
|
||
sqlCmd.Parameters.Add(new OleDbParameter("num", dt.Rows[0]["root"]));
|
||
System.Data.DataTable dt2 = sql.dataTable(sqlCmd);
|
||
if (dt2.Rows.Count > 0)
|
||
{
|
||
tmp = tmp + ",<span style=\"cursor:pointer\" onclick=\"window.location='../index2.aspx'\">首頁</span>>" + dt2.Rows[0]["title"].ToString() + ">" + tmp;
|
||
}
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
HttpContext.Current.Response.Write("config.rtn_navi:" + ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
|
||
if (u.ToLower().IndexOf("reg.aspx", 0) > -1 & !isStrNull(HttpContext.Current.Request["num"]))
|
||
{
|
||
tmp = tmp.Replace("登錄", "修改");
|
||
tmp = tmp.Replace("查詢", "修改");
|
||
}
|
||
|
||
return tmp;
|
||
|
||
}
|
||
|
||
public interface MenuNav
|
||
{
|
||
void InitNav(List<string> nItem);
|
||
}
|
||
|
||
#region "壓縮ViewState"
|
||
|
||
/// 壓縮
|
||
private byte[] Compress(byte[] data)
|
||
{
|
||
MemoryStream ms = new MemoryStream();
|
||
GZipStream stream = new GZipStream(ms, CompressionMode.Compress);
|
||
stream.Write(data, 0, data.Length);
|
||
stream.Close();
|
||
return ms.ToArray();
|
||
}
|
||
|
||
/// 解壓縮
|
||
public byte[] Decompress(byte[] data)
|
||
{
|
||
MemoryStream ms = new MemoryStream();
|
||
ms.Write(data, 0, data.Length);
|
||
ms.Position = 0;
|
||
GZipStream stream = new GZipStream(ms, CompressionMode.Decompress);
|
||
MemoryStream temp = new MemoryStream();
|
||
byte[] buffer = new byte[1025];
|
||
while (true)
|
||
{
|
||
int read = stream.Read(buffer, 0, buffer.Length);
|
||
if (read <= 0)
|
||
{
|
||
break; // TODO: might not be correct. Was : Exit While
|
||
}
|
||
else
|
||
{
|
||
temp.Write(buffer, 0, read);
|
||
}
|
||
}
|
||
stream.Close();
|
||
return temp.ToArray();
|
||
}
|
||
|
||
protected override void SavePageStateToPersistenceMedium(object state)
|
||
{
|
||
Pair pair = default(Pair);
|
||
PageStatePersister persister = this.PageStatePersister;
|
||
object ViewState = null;
|
||
if (state is Pair)
|
||
{
|
||
pair = (Pair)state;
|
||
persister.ControlState = pair.First;
|
||
ViewState = pair.Second;
|
||
}
|
||
else
|
||
{
|
||
ViewState = state;
|
||
}
|
||
LosFormatter formatter = new LosFormatter();
|
||
StringWriter writer = new StringWriter();
|
||
formatter.Serialize(writer, ViewState);
|
||
string viewStateStr = writer.ToString();
|
||
byte[] data = Convert.FromBase64String(viewStateStr);
|
||
byte[] compressedData = this.Compress(data);
|
||
string str = Convert.ToBase64String(compressedData);
|
||
persister.ViewState = str;
|
||
persister.Save();
|
||
}
|
||
|
||
protected override object LoadPageStateFromPersistenceMedium()
|
||
{
|
||
PageStatePersister persister = this.PageStatePersister;
|
||
persister.Load();
|
||
|
||
string viewState = persister.ViewState.ToString();
|
||
byte[] data = Convert.FromBase64String(viewState);
|
||
byte[] uncompressedData = this.Decompress(data);
|
||
string str = Convert.ToBase64String(uncompressedData);
|
||
LosFormatter formatter = new LosFormatter();
|
||
return new Pair(persister.ControlState, formatter.Deserialize(str));
|
||
}
|
||
|
||
|
||
|
||
#endregion
|
||
|
||
#region "驗證欄位"
|
||
public string chk_data(string obj_type, object obj_name)
|
||
{
|
||
string err_data = null;
|
||
ControlCollection controlCollection = this.Form.Controls;
|
||
|
||
switch (obj_type)
|
||
{
|
||
case "V":
|
||
View control_name = (View)obj_name;
|
||
controlCollection = control_name.Controls;
|
||
break;
|
||
case "P":
|
||
Panel control_name2 = (Panel)obj_name;
|
||
controlCollection = control_name2.Controls;
|
||
break;
|
||
case "F":
|
||
controlCollection = this.Form.Controls;
|
||
break;
|
||
}
|
||
int x = 0;
|
||
foreach (Control control in controlCollection)
|
||
{
|
||
x += 1;
|
||
//抓取為textbox的控制項
|
||
if (control is TextBox)
|
||
{
|
||
if (chk_data2(((TextBox)control).Text) != null)
|
||
{
|
||
err_data = control.ID;
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return (err_data);
|
||
|
||
}
|
||
|
||
public void create_js(string str)
|
||
{
|
||
StringBuilder str2 = new StringBuilder();
|
||
str2.Append("<script>");
|
||
str2.Append("alert(\"" + str + "\");");
|
||
str2.Append("</script>");
|
||
HttpContext.Current.Response.Write(str2.ToString());
|
||
}
|
||
|
||
public string chk_data2(string chk_str)
|
||
{
|
||
string functionReturnValue = null;
|
||
functionReturnValue = null;
|
||
string err_chars = "';*+#{}%|^/";
|
||
string c = null;
|
||
for (int i = 0; i < chk_str.Length; i++)
|
||
{
|
||
c = chk_str.Substring(0, 1);
|
||
if ((err_chars.IndexOf(c, 0) > -1))
|
||
{
|
||
create_js("資料輸入錯誤!\n\n您所輸入的資料不得使用特殊字元「" + c + "」");
|
||
functionReturnValue = c;
|
||
return functionReturnValue;
|
||
}
|
||
}
|
||
return functionReturnValue;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 資料表複製
|
||
|
||
//dataCopy("product","num,reg_time","num=1") '呼叫範例
|
||
|
||
|
||
public void dataCopy(string tb, string no_copy, string where_query)
|
||
{
|
||
string[] nocopy = no_copy.Split(',');
|
||
|
||
SqlDataSource ads = new SqlDataSource(db, "");
|
||
ads.ProviderName = p_name;
|
||
ads.SelectCommand = "select * from [" + tb + "]";
|
||
if (where_query != null)
|
||
{
|
||
ads.SelectCommand += " where " + where_query;
|
||
}
|
||
|
||
DataView dv = (DataView)ads.Select(DataSourceSelectArguments.Empty);
|
||
|
||
for (int j = 0; j <= dv.Count - 1; j++)
|
||
{
|
||
ads = new SqlDataSource(db, "");
|
||
ads.ProviderName = p_name;
|
||
string str1 = null;
|
||
string str2 = null;
|
||
for (int i = 0; i < dv.Table.Columns.Count; i++)
|
||
{
|
||
string cu = dv.Table.Columns[i].ColumnName;
|
||
|
||
bool chkcopy = true;
|
||
for (int c = 0; c <= nocopy.Length - 1; c++)
|
||
{
|
||
if (cu == nocopy[c])
|
||
{
|
||
chkcopy = false;
|
||
}
|
||
}
|
||
|
||
if (chkcopy)
|
||
{
|
||
str1 += (str1 != null ? "," : "") + "[" + cu + "]";
|
||
str2 += (str2 != null ? "," : "") + "@" + cu;
|
||
string value = null;
|
||
if (dv[j][cu] != null)
|
||
{
|
||
value = dv[j][cu].ToString();
|
||
}
|
||
ads.InsertParameters.Add(dv.Table.Columns[i].ColumnName, value);
|
||
}
|
||
}
|
||
ads.InsertCommand = "insert into [" + tb + "] (" + str1 + ") values (" + str2 + ")";
|
||
ads.Insert();
|
||
|
||
}
|
||
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region 後台index2、templatezte等頁面套用(不判斷選項的權限)
|
||
public class config2 : System.Web.UI.Page
|
||
{
|
||
//定義版型樣式(不驗證系統編號)
|
||
MyWeb.admin admin = new admin();
|
||
|
||
protected override void OnPreInit(System.EventArgs e)
|
||
{
|
||
base.OnPreInit(e);
|
||
if (!admin.isLoign())
|
||
{
|
||
HttpContext.Current.Response.Redirect("~/admin/index.aspx?msg=A2");
|
||
}
|
||
else
|
||
{
|
||
if (admin.info.login_ip != admin.MyIP)
|
||
{
|
||
HttpContext.Current.Response.Redirect("~/admin/index.aspx?msg=E");
|
||
}
|
||
this.Page.MasterPageFile = "~/admin/Templates/TBS5ADM001/MasterPage.master";
|
||
//this.Theme = "Theme1";
|
||
}
|
||
}
|
||
|
||
}
|
||
#endregion
|
||
|
||
#region 安全性過瀘
|
||
public class webSafeCheck
|
||
{
|
||
public void SetDetectXSS()
|
||
{
|
||
//跨站隱碼攻擊偵測
|
||
string[] chkData = {
|
||
"'", "\"", "onmouseover","onclick","onmouseout","script","alert","iframe","prompt"," and "," or "
|
||
};
|
||
foreach (string key in HttpContext.Current.Request.QueryString)
|
||
{
|
||
for (int i = 0; i < chkData.Length; i++)
|
||
{
|
||
if (HttpUtility.UrlDecode(HttpContext.Current.Request.QueryString[key]).ToLower().IndexOf(chkData[i].ToLower()) > -1)
|
||
{
|
||
throw new HttpException(404, "查無資料");
|
||
}
|
||
}
|
||
}
|
||
foreach (string key in HttpContext.Current.Request.Form)
|
||
{
|
||
for (int i = 0; i < chkData.Length; i++)
|
||
{
|
||
if (HttpUtility.UrlDecode(HttpContext.Current.Request.Form[key]).ToLower().IndexOf(chkData[i].ToLower()) > -1)
|
||
{
|
||
throw new HttpException(404, "查無資料");
|
||
}
|
||
}
|
||
if (HttpContext.Current.Request.Form[key].ToLower().IndexOf("<img") > -1 && HttpContext.Current.Request.Form[key].ToLower().IndexOf("base64") > -1)
|
||
{
|
||
HttpContext.Current.Response.Clear();
|
||
HttpContext.Current.Response.Write("請勿使用base64編碼的圖片置於內容中");
|
||
HttpContext.Current.Response.End();
|
||
}
|
||
}
|
||
|
||
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region 網站前台
|
||
|
||
public class web_config : function
|
||
{
|
||
public string db = ConfigurationManager.ConnectionStrings["shopConn"].ConnectionString;
|
||
public string p_name = ConfigurationManager.ConnectionStrings["shopConn"].ProviderName;
|
||
public string scc = ConfigurationManager.AppSettings["shopCarCode"].ToString();
|
||
|
||
protected override void OnPreInit(System.EventArgs e)
|
||
{
|
||
base.OnPreInit(e);
|
||
|
||
//安全性過瀘
|
||
MyWeb.webSafeCheck webSafeCheck = new MyWeb.webSafeCheck();
|
||
webSafeCheck.SetDetectXSS();
|
||
|
||
}
|
||
|
||
public string nation()
|
||
{
|
||
MyWeb.language language = new MyWeb.language();
|
||
return language.nation();
|
||
}
|
||
|
||
public bool dbIsSql()
|
||
{
|
||
//判斷SQL或Access資料庫
|
||
if (p_name == "System.Data.SqlClient")
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
#region 壓縮ViewState
|
||
|
||
/// 壓縮
|
||
private byte[] Compress(byte[] data)
|
||
{
|
||
MemoryStream ms = new MemoryStream();
|
||
GZipStream stream = new GZipStream(ms, CompressionMode.Compress);
|
||
stream.Write(data, 0, data.Length);
|
||
stream.Close();
|
||
return ms.ToArray();
|
||
}
|
||
|
||
/// 解壓縮
|
||
public byte[] Decompress(byte[] data)
|
||
{
|
||
MemoryStream ms = new MemoryStream();
|
||
ms.Write(data, 0, data.Length);
|
||
ms.Position = 0;
|
||
GZipStream stream = new GZipStream(ms, CompressionMode.Decompress);
|
||
MemoryStream temp = new MemoryStream();
|
||
byte[] buffer = new byte[1025];
|
||
while (true)
|
||
{
|
||
int read = stream.Read(buffer, 0, buffer.Length);
|
||
if (read <= 0)
|
||
{
|
||
break;
|
||
}
|
||
else
|
||
{
|
||
temp.Write(buffer, 0, read);
|
||
}
|
||
}
|
||
stream.Close();
|
||
return temp.ToArray();
|
||
}
|
||
|
||
protected override void SavePageStateToPersistenceMedium(object state)
|
||
{
|
||
Pair pair = default(Pair);
|
||
PageStatePersister persister = this.PageStatePersister;
|
||
object ViewState = null;
|
||
if (state is Pair)
|
||
{
|
||
pair = (Pair)state;
|
||
persister.ControlState = pair.First;
|
||
ViewState = pair.Second;
|
||
}
|
||
else
|
||
{
|
||
ViewState = state;
|
||
}
|
||
LosFormatter formatter = new LosFormatter();
|
||
StringWriter writer = new StringWriter();
|
||
formatter.Serialize(writer, ViewState);
|
||
string viewStateStr = writer.ToString();
|
||
byte[] data = Convert.FromBase64String(viewStateStr);
|
||
byte[] compressedData = this.Compress(data);
|
||
string str = Convert.ToBase64String(compressedData);
|
||
persister.ViewState = str;
|
||
persister.Save();
|
||
}
|
||
|
||
protected override object LoadPageStateFromPersistenceMedium()
|
||
{
|
||
PageStatePersister persister = this.PageStatePersister;
|
||
persister.Load();
|
||
|
||
string viewState = persister.ViewState.ToString();
|
||
byte[] data = Convert.FromBase64String(viewState);
|
||
byte[] uncompressedData = this.Decompress(data);
|
||
string str = Convert.ToBase64String(uncompressedData);
|
||
LosFormatter formatter = new LosFormatter();
|
||
return new Pair(persister.ControlState, formatter.Deserialize(str));
|
||
}
|
||
|
||
|
||
|
||
#endregion
|
||
|
||
#region 過濾字串(轉換成空白)
|
||
|
||
public string replace_str(string str)
|
||
{
|
||
string err_chars = "'/;*+#{}^|\\<>";
|
||
string c = null;
|
||
string n_str = str;
|
||
for (int i = 0; i < str.Length; i++)
|
||
{
|
||
c = str.Substring(i, 1);
|
||
if ((err_chars.IndexOf(c, 0) > -1))
|
||
{
|
||
n_str = n_str.Replace(c, "");
|
||
//HttpContext.Current.Response.Write("test=" & n_str & "<br>")
|
||
}
|
||
}
|
||
return n_str;
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 驗證欄位
|
||
public string chk_data(string obj_type, object obj_name, string kind)
|
||
{
|
||
string err_data = null;
|
||
ControlCollection controlCollection = this.Form.Controls;
|
||
ViewState["err_msg"] = null;
|
||
|
||
switch (obj_type)
|
||
{
|
||
case "V":
|
||
View control_name = (View)obj_name;
|
||
controlCollection = control_name.Controls;
|
||
break;
|
||
case "P":
|
||
Panel control_name2 = (Panel)obj_name;
|
||
controlCollection = control_name2.Controls;
|
||
break;
|
||
case "F":
|
||
controlCollection = this.Form.Controls;
|
||
break;
|
||
}
|
||
int x = 0;
|
||
foreach (Control control in controlCollection)
|
||
{
|
||
x += 1;
|
||
//抓取為textbox的控制項
|
||
if (control is TextBox)
|
||
{
|
||
if (chk_data2(((TextBox)control).Text, kind) != null)
|
||
{
|
||
err_data = ViewState["err_msg"].ToString();
|
||
//err_data = control.ID
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
return (err_data);
|
||
|
||
}
|
||
|
||
public void create_js(string str)
|
||
{
|
||
System.Text.StringBuilder str2 = new System.Text.StringBuilder();
|
||
str2.Append("<script>");
|
||
str2.Append("alert(\"" + str + "\");");
|
||
str2.Append("</script>");
|
||
HttpContext.Current.Response.Write(str2.ToString());
|
||
}
|
||
|
||
public string chk_data2(string chk_str, string kind)
|
||
{
|
||
string functionReturnValue = null;
|
||
functionReturnValue = null;
|
||
string err_chars = "';*+#{}%|^/";
|
||
string c = null;
|
||
for (int i = 0; i < chk_str.Length; i++)
|
||
{
|
||
c = chk_str.Substring(i, 1);
|
||
if ((err_chars.IndexOf(c, 0) > -1))
|
||
{
|
||
ViewState["err_msg"] = "資料輸入錯誤!您所輸入的資料不得使用特殊字元「" + c + "」";
|
||
if (kind != "B")
|
||
{
|
||
create_js("資料輸入錯誤!\n\n您所輸入的資料不得使用特殊字元「" + c + "」");
|
||
}
|
||
functionReturnValue = c;
|
||
return functionReturnValue;
|
||
}
|
||
}
|
||
return functionReturnValue;
|
||
}
|
||
|
||
#endregion
|
||
|
||
}
|
||
|
||
public class web_uc : System.Web.UI.UserControl
|
||
{
|
||
public string db = ConfigurationManager.ConnectionStrings["shopConn"].ConnectionString;
|
||
public string p_name = ConfigurationManager.ConnectionStrings["shopConn"].ProviderName;
|
||
public string scc = ConfigurationManager.AppSettings["shopCarCode"].ToString();
|
||
|
||
function f = new function();
|
||
|
||
public string nation()
|
||
{
|
||
MyWeb.language language = new MyWeb.language();
|
||
return language.nation();
|
||
}
|
||
|
||
public bool IsNumeric(object Expression)
|
||
{
|
||
return f.IsNumeric(Expression);
|
||
}
|
||
|
||
public bool isDate(object chkString)
|
||
{
|
||
return f.isDate(chkString);
|
||
}
|
||
|
||
public int Val(object Expression)
|
||
{
|
||
return f.Val(Expression);
|
||
}
|
||
|
||
public string ValString(object Expression)
|
||
{
|
||
return f.ValString(Expression);
|
||
}
|
||
|
||
public string Left(object Expression, int Length)
|
||
{
|
||
return f.Left(Expression, Length);
|
||
}
|
||
|
||
public string Right(object Expression, int Length)
|
||
{
|
||
return f.Right(Expression, Length);
|
||
}
|
||
|
||
public bool isStrNull(object value)
|
||
{
|
||
return f.isStrNull(value);
|
||
}
|
||
|
||
public bool dbIsSql()
|
||
{
|
||
//判斷SQL或Access資料庫
|
||
if (p_name == "System.Data.SqlClient")
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
public void ScriptMsg(string txt, string url = "", function.msgIcon icon = function.msgIcon.none)
|
||
{
|
||
f.ScriptMsg(txt, url, icon);
|
||
}
|
||
|
||
public void ScriptMsgTop(string txt, function.msgIcon icon)
|
||
{
|
||
f.ScriptMsgTop(txt, icon);
|
||
}
|
||
|
||
public void ScriptJS(string script)
|
||
{
|
||
f.ScriptJS(script);
|
||
}
|
||
|
||
public void ScriptMsg2(string txt, string url = "", function.msgIcon icon = function.msgIcon.none)
|
||
{
|
||
f.ScriptMsg2(txt, url, icon);
|
||
}
|
||
|
||
public void ScriptMsgTop2(string txt, function.msgIcon icon)
|
||
{
|
||
f.ScriptMsgTop(txt, icon);
|
||
}
|
||
|
||
public void ScriptJS2(string script)
|
||
{
|
||
f.ScriptJS2(script);
|
||
}
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 流量統計
|
||
|
||
public class web_counter : function
|
||
{
|
||
|
||
public string db = ConfigurationManager.ConnectionStrings["shopConn"].ConnectionString;
|
||
public string p_name = ConfigurationManager.ConnectionStrings["shopConn"].ProviderName;
|
||
|
||
public void chk_counter()
|
||
{
|
||
MyWeb.sql sql = new MyWeb.sql();
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "SELECT * FROM web_counter order by id desc";
|
||
DataTable dt = sql.dataTable(sqlCmd);
|
||
if (dt.Rows.Count > 0)
|
||
{
|
||
DateTime d = ValDate(dt.Rows[0]["Date"]);
|
||
if (d.Date == DateTime.Now.Date)
|
||
{
|
||
add_counter(Convert.ToInt32(dt.Rows[0]["Total"]));
|
||
}
|
||
else
|
||
{
|
||
edit_counter(Convert.ToInt32(dt.Rows[0]["Total"]), Convert.ToInt32(dt.Rows[0]["today"]), Convert.ToInt32(dt.Rows[0]["HR" + DateTime.Now.Hour]), Convert.ToInt32(dt.Rows[0]["id"]));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
add_counter(0);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
HttpContext.Current.Response.Write("config.web_counter:" + ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
}
|
||
|
||
public void add_counter(int total)
|
||
{
|
||
MyWeb.sql sql = new MyWeb.sql();
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "INSERT INTO web_counter (Total, Today, HR" + DateTime.Now.Hour + ", LastIP) VALUES (?, ?, ?, ?)";
|
||
sqlCmd.Parameters.Add(new OleDbParameter("Total", total + 1));
|
||
sqlCmd.Parameters.Add(new OleDbParameter("Today", 1));
|
||
sqlCmd.Parameters.Add(new OleDbParameter("HR", 1));
|
||
sqlCmd.Parameters.Add(new OleDbParameter("LastIP", Request.ServerVariables["REMOTE_ADDR"].ToString()));
|
||
sqlCmd.ExecuteNonQuery();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
HttpContext.Current.Response.Write("config.add_counter:" + ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
}
|
||
|
||
public void edit_counter(int total, int n_today, int hr, int id)
|
||
{
|
||
MyWeb.sql sql = new MyWeb.sql();
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "UPDATE web_counter SET Total=?, Today=?, HR" + DateTime.Now.Hour + "=?, LastIP=? WHERE id=?";
|
||
sqlCmd.Parameters.Add(new OleDbParameter("Total", total + 1));
|
||
sqlCmd.Parameters.Add(new OleDbParameter("Today", n_today + 1));
|
||
sqlCmd.Parameters.Add(new OleDbParameter("HR", hr + 1));
|
||
sqlCmd.Parameters.Add(new OleDbParameter("LastIP", Request.ServerVariables["REMOTE_ADDR"].ToString()));
|
||
sqlCmd.Parameters.Add(new OleDbParameter("id", id));
|
||
sqlCmd.ExecuteNonQuery();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
HttpContext.Current.Response.Write("config.edit_counter:" + ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close(); sqlConn.Dispose();
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 檔案處理
|
||
|
||
public class fileSystem : System.Web.UI.Page
|
||
{
|
||
public string WatermarkPath = ConfigurationManager.AppSettings["WatermarkPath"].ToString();
|
||
public string WatermarkMode = ConfigurationManager.AppSettings["WatermarkMode"].ToString();
|
||
public string WatermarkPct = ConfigurationManager.AppSettings["WatermarkPct"].ToString();
|
||
|
||
public int Count()
|
||
{
|
||
HttpFileCollection objFileCollection = HttpContext.Current.Request.Files;
|
||
return objFileCollection.Count;
|
||
}
|
||
|
||
public string[] Upload(string filePath)
|
||
{
|
||
|
||
string up_path = Server.MapPath(filePath);
|
||
if (up_path.Substring(up_path.Length - 1, 1).ToString() != "/")
|
||
{
|
||
up_path = up_path + "/";
|
||
}
|
||
|
||
DirectoryInfo Dir = new DirectoryInfo(up_path);
|
||
if (!Dir.Exists) { Dir.Create(); }
|
||
|
||
HttpFileCollection objFileCollection = HttpContext.Current.Request.Files;
|
||
if (objFileCollection.Count > 0)
|
||
{
|
||
HttpPostedFile file;
|
||
int ii = 0;
|
||
string[] pic_name = new string[objFileCollection.Count];
|
||
for (ii = 0; ii < objFileCollection.Count; ii++)
|
||
{
|
||
file = objFileCollection[ii];
|
||
if (file.ContentLength > 0)
|
||
{
|
||
string[] n = Path.GetFileName(file.FileName).Split('.');
|
||
pic_name[ii] = DateTime.Now.ToString("yyyyMMddHHmmss") + ii.ToString() + "." + n[n.Length - 1];
|
||
if (isAllowed(pic_name[ii])) { file.SaveAs(up_path + pic_name[ii]); } else { pic_name[ii] = ""; }
|
||
}
|
||
else
|
||
{
|
||
pic_name[ii] = "";
|
||
}
|
||
}
|
||
return pic_name;
|
||
}
|
||
else
|
||
{
|
||
int defMax = 99;
|
||
string[] pic_name = new string[defMax];
|
||
for (int i = 0; i < defMax; i++)
|
||
{
|
||
pic_name[i] = "";
|
||
}
|
||
return pic_name;
|
||
}
|
||
|
||
}
|
||
|
||
public void Delete(string filePath)
|
||
{
|
||
string path = Server.MapPath(filePath);
|
||
FileInfo FileInfo = new FileInfo(path);
|
||
if (FileInfo.Exists)
|
||
{
|
||
FileInfo.Delete();
|
||
}
|
||
}
|
||
|
||
public bool isPhoto(string fileName)
|
||
{
|
||
string[] n = fileName.ToLower().Split('.');
|
||
string[] type = {
|
||
"jpg",
|
||
"jpeg",
|
||
"gif",
|
||
"png",
|
||
"bmp"
|
||
};
|
||
for (int i = 0; i <= type.Length - 1; i++)
|
||
{
|
||
if (n[n.Length - 1] == type[i])
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
public bool isAllowed(string fileName)
|
||
{
|
||
string[] n = fileName.ToLower().Split('.');
|
||
string[] type = {
|
||
"jpg",
|
||
"jpeg",
|
||
"png",
|
||
"tif",
|
||
"bmp",
|
||
"gif",
|
||
"pdf",
|
||
"doc",
|
||
"docx",
|
||
"xls",
|
||
"xlsx",
|
||
"zip",
|
||
"rar",
|
||
"7z",
|
||
"ppt",
|
||
"pptx",
|
||
"xml"
|
||
};
|
||
for (int i = 0; i <= type.Length - 1; i++)
|
||
{
|
||
if (n[n.Length - 1] == type[i])
|
||
{
|
||
return true;
|
||
}
|
||
}
|
||
|
||
return false;
|
||
}
|
||
|
||
|
||
public string[] UploadPhoto(string filePath, double limitSizePx , int num=0)
|
||
{
|
||
|
||
string up_path = Server.MapPath(filePath);
|
||
if (up_path.Substring(up_path.Length - 1, 1).ToString() != "/")
|
||
{
|
||
up_path = up_path + "/";
|
||
}
|
||
|
||
DirectoryInfo Dir = new DirectoryInfo(up_path);
|
||
if (!Dir.Exists) { Dir.Create(); }
|
||
|
||
|
||
HttpFileCollection objFileCollection = HttpContext.Current.Request.Files;
|
||
if (objFileCollection.Count > 0)
|
||
{
|
||
HttpPostedFile file = default(HttpPostedFile);
|
||
int ii = 0;
|
||
string[] pic_name = new string[objFileCollection.Count];
|
||
for (ii = 0; ii < objFileCollection.Count; ii++)
|
||
{
|
||
file = objFileCollection[ii];
|
||
if (file.ContentLength > 0)
|
||
{
|
||
string[] n = Path.GetFileName(file.FileName).Split('.');
|
||
pic_name[ii] = DateTime.Now.ToString("yyyyMMddHHmmss") + ii.ToString() + "." + n[n.Length - 1];
|
||
if (num > 0) { pic_name[ii] = num + "_" + pic_name[ii]; }
|
||
if (isPhoto(pic_name[ii]))
|
||
{
|
||
System.Drawing.Image Bm = new System.Drawing.Bitmap(file.InputStream);
|
||
|
||
foreach (PropertyItem pi in Bm.PropertyItems)
|
||
{
|
||
// orientation tag id is 274
|
||
if (pi.Id == 274)
|
||
{
|
||
switch (pi.Value[0])
|
||
{
|
||
case 2:
|
||
Bm.RotateFlip(RotateFlipType.RotateNoneFlipX);
|
||
break;
|
||
case 3:
|
||
Bm.RotateFlip(RotateFlipType.Rotate180FlipNone);
|
||
break;
|
||
case 4:
|
||
Bm.RotateFlip(RotateFlipType.RotateNoneFlipY);
|
||
break;
|
||
case 5:
|
||
Bm.RotateFlip(RotateFlipType.Rotate90FlipX);
|
||
break;
|
||
case 6:
|
||
Bm.RotateFlip(RotateFlipType.Rotate90FlipNone);
|
||
break;
|
||
case 7:
|
||
Bm.RotateFlip(RotateFlipType.Rotate270FlipX);
|
||
break;
|
||
case 8:
|
||
Bm.RotateFlip(RotateFlipType.Rotate270FlipNone);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
double maxSize = limitSizePx;
|
||
//限制最尺寸不得超過象素
|
||
double newWidth = Bm.Width;
|
||
double newHeight = Bm.Height;
|
||
if (Bm.Width > Bm.Height)
|
||
{
|
||
if (Bm.Width > maxSize)
|
||
{
|
||
newWidth = maxSize;
|
||
newHeight = maxSize / Bm.Width;
|
||
newHeight = newHeight * Bm.Height;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (Bm.Height > maxSize)
|
||
{
|
||
newHeight = maxSize;
|
||
newWidth = maxSize / Bm.Height;
|
||
newWidth = newWidth * Bm.Width;
|
||
}
|
||
}
|
||
if (newWidth != 0 & newHeight != 0)
|
||
{
|
||
System.Drawing.Image New_Image = new Bitmap((int)newWidth, (int)newHeight);
|
||
Graphics ObjGraphics = Graphics.FromImage(New_Image);
|
||
|
||
|
||
MyWeb.global global = new MyWeb.global();
|
||
|
||
ObjGraphics.InterpolationMode = global.InterpolationMode;
|
||
ObjGraphics.SmoothingMode = global.SmoothingMode;
|
||
ObjGraphics.CompositingQuality = global.CompositingQuality;
|
||
|
||
string picType = n[n.Length - 1].ToLower();
|
||
if (picType == "jpg")
|
||
picType = "jpeg";
|
||
|
||
if (picType == "png")
|
||
{
|
||
ObjGraphics.Clear(Color.Transparent);
|
||
//清空Graphics, 以透明色填充
|
||
}
|
||
else
|
||
{
|
||
ObjGraphics.Clear(Color.White);
|
||
//清空Graphics, 以白色填充
|
||
}
|
||
|
||
//在指定位置按指定大小繪制原圖片的片段
|
||
ObjGraphics.DrawImage(Bm, new Rectangle(0, 0, (int)newWidth, (int)newHeight), new Rectangle(0, 0, Bm.Width, Bm.Height), GraphicsUnit.Pixel);
|
||
|
||
if (WatermarkMode == "2")
|
||
{
|
||
string FileNameAndPath = HttpContext.Current.Server.MapPath(WatermarkPath);
|
||
System.Drawing.Image Watermark_Image = System.Drawing.Image.FromFile(FileNameAndPath);
|
||
int w_Width = Convert.ToInt32(Math.Round(Convert.ToDouble(newWidth) * 0.8));
|
||
int w_Height = Convert.ToInt32(Watermark_Image.Height * (Convert.ToDouble(w_Width) / Convert.ToDouble(Watermark_Image.Width)));
|
||
int w_myX = ((int)newWidth - w_Width) / 2; //浮水印水平置中
|
||
int w_myY = ((int)newHeight - w_Height) / 2 + w_Height; //浮水印水平垂直置中向下偏移一個高度
|
||
var colorMatrix = new ColorMatrix();
|
||
colorMatrix.Matrix33 = (float)Convert.ToSingle(WatermarkPct);
|
||
var imageAttributes = new ImageAttributes();
|
||
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
|
||
ObjGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
||
ObjGraphics.DrawImage(
|
||
Watermark_Image,
|
||
new Rectangle(w_myX, w_myY, w_Width, w_Height),
|
||
0,
|
||
0,
|
||
Watermark_Image.Width,
|
||
Watermark_Image.Height,
|
||
GraphicsUnit.Pixel,
|
||
imageAttributes);
|
||
}
|
||
|
||
//下方設定使JPG質量
|
||
EncoderParameters EPS = new EncoderParameters();
|
||
EncoderParameter EP = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Convert.ToInt64(global.ImageCompressionQuality));
|
||
EPS.Param[0] = EP;
|
||
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
|
||
ImageCodecInfo ICI = null;
|
||
foreach (ImageCodecInfo codec in codecs)
|
||
{
|
||
if (codec.MimeType == "image/" + picType)
|
||
{
|
||
ICI = codec;
|
||
break; // TODO: might not be correct. Was : Exit For
|
||
}
|
||
}
|
||
New_Image.Save(up_path + pic_name[ii], ICI, EPS);
|
||
New_Image.Dispose();
|
||
ObjGraphics.Dispose();
|
||
}
|
||
else
|
||
{
|
||
file.SaveAs(up_path + pic_name[ii]);
|
||
//不需縮圖直接儲存
|
||
}
|
||
Bm.Dispose();
|
||
}
|
||
else if (isAllowed(pic_name[ii]))
|
||
{
|
||
file.SaveAs(up_path + pic_name[ii]);
|
||
//若不是圖片直接儲存
|
||
}
|
||
else
|
||
{
|
||
pic_name[ii] = "";
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
pic_name[ii] = "";
|
||
}
|
||
}
|
||
return pic_name;
|
||
}
|
||
else
|
||
{
|
||
int defMax = 99;
|
||
string[] pic_name = new string[defMax];
|
||
for (int i = 0; i <= defMax - 1; i++)
|
||
{
|
||
pic_name[i] = "";
|
||
}
|
||
return pic_name;
|
||
}
|
||
|
||
}
|
||
|
||
public string[] UploadNewsFile(string filePath, double limitSizePx, int num , ref string[] pic_name2, ref string[] pic_type)
|
||
{
|
||
|
||
string up_path = Server.MapPath(filePath);
|
||
if (up_path.Substring(up_path.Length - 1, 1).ToString() != "/")
|
||
{
|
||
up_path = up_path + "/";
|
||
}
|
||
|
||
DirectoryInfo Dir = new DirectoryInfo(up_path);
|
||
if (!Dir.Exists) { Dir.Create(); }
|
||
|
||
|
||
HttpFileCollection objFileCollection = HttpContext.Current.Request.Files;
|
||
if (objFileCollection.Count > 0)
|
||
{
|
||
HttpPostedFile file = default(HttpPostedFile);
|
||
int ii = 0;
|
||
string[] pic_name = new string[objFileCollection.Count];
|
||
pic_name2 = new string[objFileCollection.Count];
|
||
pic_type = new string[objFileCollection.Count];
|
||
for (ii = 0; ii < objFileCollection.Count; ii++)
|
||
{
|
||
file = objFileCollection[ii];
|
||
if (file.ContentLength > 0)
|
||
{
|
||
string[] n = Path.GetFileName(file.FileName).Split('.');
|
||
pic_name[ii] = DateTime.Now.ToString("yyyyMMddHHmmss") + ii.ToString() + "." + n[n.Length - 1];
|
||
pic_name[ii] = num + "_" + pic_name[ii];
|
||
|
||
pic_name2[ii] = System.IO.Path.GetFileName(file.FileName); //紀錄原先檔名
|
||
pic_type[ii] = n[n.Length - 1];//紀錄副檔名
|
||
|
||
|
||
if (isPhoto(pic_name[ii]))
|
||
{
|
||
System.Drawing.Image Bm = new System.Drawing.Bitmap(file.InputStream);
|
||
|
||
foreach (PropertyItem pi in Bm.PropertyItems)
|
||
{
|
||
// orientation tag id is 274
|
||
if (pi.Id == 274)
|
||
{
|
||
switch (pi.Value[0])
|
||
{
|
||
case 2:
|
||
Bm.RotateFlip(RotateFlipType.RotateNoneFlipX);
|
||
break;
|
||
case 3:
|
||
Bm.RotateFlip(RotateFlipType.Rotate180FlipNone);
|
||
break;
|
||
case 4:
|
||
Bm.RotateFlip(RotateFlipType.RotateNoneFlipY);
|
||
break;
|
||
case 5:
|
||
Bm.RotateFlip(RotateFlipType.Rotate90FlipX);
|
||
break;
|
||
case 6:
|
||
Bm.RotateFlip(RotateFlipType.Rotate90FlipNone);
|
||
break;
|
||
case 7:
|
||
Bm.RotateFlip(RotateFlipType.Rotate270FlipX);
|
||
break;
|
||
case 8:
|
||
Bm.RotateFlip(RotateFlipType.Rotate270FlipNone);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
double maxSize = limitSizePx;
|
||
//限制最尺寸不得超過象素
|
||
double newWidth = Bm.Width;
|
||
double newHeight = Bm.Height;
|
||
if (Bm.Width > Bm.Height)
|
||
{
|
||
if (Bm.Width > maxSize)
|
||
{
|
||
newWidth = maxSize;
|
||
newHeight = maxSize / Bm.Width;
|
||
newHeight = newHeight * Bm.Height;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (Bm.Height > maxSize)
|
||
{
|
||
newHeight = maxSize;
|
||
newWidth = maxSize / Bm.Height;
|
||
newWidth = newWidth * Bm.Width;
|
||
}
|
||
}
|
||
if (newWidth != 0 & newHeight != 0)
|
||
{
|
||
System.Drawing.Image New_Image = new Bitmap((int)newWidth, (int)newHeight);
|
||
Graphics ObjGraphics = Graphics.FromImage(New_Image);
|
||
|
||
|
||
MyWeb.global global = new MyWeb.global();
|
||
|
||
ObjGraphics.InterpolationMode = global.InterpolationMode;
|
||
ObjGraphics.SmoothingMode = global.SmoothingMode;
|
||
ObjGraphics.CompositingQuality = global.CompositingQuality;
|
||
|
||
string picType = n[n.Length - 1].ToLower();
|
||
if (picType == "jpg")
|
||
picType = "jpeg";
|
||
|
||
if (picType == "png")
|
||
{
|
||
ObjGraphics.Clear(Color.Transparent);
|
||
//清空Graphics, 以透明色填充
|
||
}
|
||
else
|
||
{
|
||
ObjGraphics.Clear(Color.White);
|
||
//清空Graphics, 以白色填充
|
||
}
|
||
|
||
//在指定位置按指定大小繪制原圖片的片段
|
||
ObjGraphics.DrawImage(Bm, new Rectangle(0, 0, (int)newWidth, (int)newHeight), new Rectangle(0, 0, Bm.Width, Bm.Height), GraphicsUnit.Pixel);
|
||
|
||
if (WatermarkMode == "2")
|
||
{
|
||
string FileNameAndPath = HttpContext.Current.Server.MapPath(WatermarkPath);
|
||
System.Drawing.Image Watermark_Image = System.Drawing.Image.FromFile(FileNameAndPath);
|
||
int w_Width = Convert.ToInt32(Math.Round(Convert.ToDouble(newWidth) * 0.8));
|
||
int w_Height = Convert.ToInt32(Watermark_Image.Height * (Convert.ToDouble(w_Width) / Convert.ToDouble(Watermark_Image.Width)));
|
||
int w_myX = ((int)newWidth - w_Width) / 2; //浮水印水平置中
|
||
int w_myY = ((int)newHeight - w_Height) / 2 + w_Height; //浮水印水平垂直置中向下偏移一個高度
|
||
var colorMatrix = new ColorMatrix();
|
||
colorMatrix.Matrix33 = (float)Convert.ToSingle(WatermarkPct);
|
||
var imageAttributes = new ImageAttributes();
|
||
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
|
||
ObjGraphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
|
||
ObjGraphics.DrawImage(
|
||
Watermark_Image,
|
||
new Rectangle(w_myX, w_myY, w_Width, w_Height),
|
||
0,
|
||
0,
|
||
Watermark_Image.Width,
|
||
Watermark_Image.Height,
|
||
GraphicsUnit.Pixel,
|
||
imageAttributes);
|
||
}
|
||
|
||
//下方設定使JPG質量
|
||
EncoderParameters EPS = new EncoderParameters();
|
||
EncoderParameter EP = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, Convert.ToInt64(global.ImageCompressionQuality));
|
||
EPS.Param[0] = EP;
|
||
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
|
||
ImageCodecInfo ICI = null;
|
||
foreach (ImageCodecInfo codec in codecs)
|
||
{
|
||
if (codec.MimeType == "image/" + picType)
|
||
{
|
||
ICI = codec;
|
||
break; // TODO: might not be correct. Was : Exit For
|
||
}
|
||
}
|
||
New_Image.Save(up_path + pic_name[ii], ICI, EPS);
|
||
New_Image.Dispose();
|
||
ObjGraphics.Dispose();
|
||
}
|
||
else
|
||
{
|
||
file.SaveAs(up_path + pic_name[ii]);
|
||
//不需縮圖直接儲存
|
||
}
|
||
Bm.Dispose();
|
||
}
|
||
else if (isAllowed(pic_name[ii]))
|
||
{
|
||
file.SaveAs(up_path + pic_name[ii]);
|
||
//若不是圖片直接儲存
|
||
}
|
||
else
|
||
{
|
||
pic_name[ii] = "";
|
||
pic_name2[ii] = "";
|
||
pic_type[ii] = "";
|
||
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
pic_name[ii] = "";
|
||
pic_name2[ii] = "";
|
||
pic_type[ii] = "";
|
||
}
|
||
}
|
||
return pic_name;
|
||
}
|
||
else
|
||
{
|
||
int defMax = 99;
|
||
string[] pic_name = new string[defMax];
|
||
for (int i = 0; i <= defMax - 1; i++)
|
||
{
|
||
pic_name[i] = "";
|
||
pic_name2[i] = "";
|
||
pic_type[i] = "";
|
||
}
|
||
return pic_name;
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
|
||
#endregion
|
||
|
||
#region 寄信
|
||
|
||
public class emailSystem : web_config
|
||
{
|
||
|
||
public string log = "";
|
||
encrypt encrypt = new encrypt();
|
||
|
||
public bool send(string toMail, ArrayList bccMail, string formMail, string subject, string content)
|
||
{
|
||
log = "";
|
||
|
||
MailMessage mail = new MailMessage();
|
||
mail.To.Add(new MailAddress(toMail)); //收件者
|
||
mail.From = new MailAddress(formMail); //寄件者
|
||
|
||
if (bccMail != null)
|
||
{
|
||
foreach (string bcc in bccMail)
|
||
{
|
||
mail.Bcc.Add(new MailAddress(bcc));
|
||
}
|
||
}
|
||
|
||
mail.IsBodyHtml = true;
|
||
mail.Subject = subject;
|
||
mail.Body = content;
|
||
|
||
SmtpClient smtp = new SmtpClient();
|
||
|
||
sql sql = new sql();
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "Select smtp_url,smtp_port,smtp_ssl,smtp_user,smtp_password,smtp_def,use_sender from [company] where [num]=1";
|
||
DataTable dt = sql.dataTable(sqlCmd);
|
||
if (dt.Rows.Count > 0)
|
||
{
|
||
DataRow row = dt.Rows[0];
|
||
if (!isStrNull(row["smtp_url"]))
|
||
{
|
||
smtp.Host = ValString(row["smtp_url"]);
|
||
|
||
if (!isStrNull(row["smtp_port"])) { smtp.Port = Val(row["smtp_port"]); }
|
||
smtp.EnableSsl = (ValString(row["smtp_ssl"]) == "Y" ? true : false);
|
||
|
||
if (!isStrNull(row["smtp_def"]))
|
||
{
|
||
smtp.UseDefaultCredentials = (ValString(row["smtp_def"]) == "Y" ? true : false);
|
||
}
|
||
|
||
if (!isStrNull(row["smtp_user"]) && !isStrNull(row["smtp_password"]))
|
||
{
|
||
if (ValString(row["use_sender"]) == "Y")
|
||
{
|
||
mail.From = new MailAddress(ValString(row["smtp_user"])); //寄件者
|
||
}
|
||
smtp.Credentials = new System.Net.NetworkCredential(ValString(row["smtp_user"]), encrypt.DecryptAutoKey(ValString(row["smtp_password"])));
|
||
}
|
||
|
||
smtp.Send(mail);
|
||
|
||
}
|
||
else
|
||
{
|
||
log = "網站未設定SMTP";
|
||
}
|
||
|
||
}
|
||
else
|
||
{
|
||
log = "網站設定錯誤";
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
log = ex.Message;
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close();
|
||
sqlConn.Dispose();
|
||
}
|
||
|
||
return (isStrNull(log) ? true : false);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 金額轉中文/千分位
|
||
|
||
public class price : System.Web.UI.Page
|
||
{
|
||
public string ValMoneyCh(object Expression)
|
||
{
|
||
string price = Expression.ToString().Split('.')[0]; //去除小數點
|
||
string i = price.Replace(",", ""); //去除千分位
|
||
|
||
string[] numc_arr = ("零,壹,貳,參,肆,伍,陸,柒,捌,玖").Split(',');
|
||
string[] unic_arr = (",拾,佰,仟").Split(',');
|
||
string[] unic1_arr = ("元整,萬,億,兆,京").Split(',');
|
||
|
||
int c0 = 0;
|
||
List<string> str = new List<string>();
|
||
do
|
||
{
|
||
int aa = 0;
|
||
int c1 = 0;
|
||
string s = "";
|
||
//取最右邊四位數跑迴圈,不足四位就全取
|
||
int lan = (i.Length >= 4 ? 4 : i.Length);
|
||
int j = Convert.ToInt32(i.Substring(i.Length - lan, lan));
|
||
while (j > 0)
|
||
{
|
||
int k = j % 10; //餘數
|
||
if (k > 0) { aa = 1; s = numc_arr[k] + unic_arr[c1] + s; }
|
||
else if (k == 0 && aa == 1) { s = "0" + s; }
|
||
j = j / 10; //商
|
||
c1++;
|
||
}
|
||
//轉成中文後丟入陣列,全部為零不加單位
|
||
str.Add((s == "" ? "" : s + unic1_arr[c0]));
|
||
//計算剩餘字串長度
|
||
int count_len = i.Length - 4;
|
||
i = (count_len > 0 ? i.Substring(0, count_len) : "");
|
||
c0++;
|
||
} while (!string.IsNullOrEmpty(i));
|
||
|
||
string chstring = "";
|
||
while (str.Count > 0) { chstring += str[str.Count - 1]; str.Remove(str[str.Count - 1]); }
|
||
|
||
string pattern = "0+";
|
||
string replacement = "零";
|
||
Regex rgx = new Regex(pattern);
|
||
string result = rgx.Replace(chstring, replacement);
|
||
|
||
return result;
|
||
|
||
}
|
||
|
||
public string ValMoney(object Expression, int DecimalLength = 2) //千分位,預設最多到小數點第2位
|
||
{
|
||
string format = "N0";
|
||
string[] m = Expression.ToString().Replace(",", "").Split('.');
|
||
if (Expression.ToString().IndexOf(".") > -1)
|
||
{
|
||
int ML = (m[m.Length - 1].Length > DecimalLength ? DecimalLength : m[m.Length - 1].Length);
|
||
if (!string.IsNullOrEmpty(m[m.Length - 1])) { format = "N" + ML.ToString(); }
|
||
}
|
||
return Convert.ToSingle(Expression).ToString(format);
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
|