private static string GetDnsRealHost() { string host = HttpContext.Current.Request.Url.DnsSafeHost; string ts = string.Format(GetUrl("Key"), host, GetServerString("LOCAL_ADDR"), HotoUtils.GetVersion()); if (!string.IsNullOrEmpty(host) && host != "localhost") { HotoUtils.GetDomainStr("dt_cache_domain_info", ts); } return(host); }
/// <summary> /// 创建 /// </summary> /// <param name="FileName">原始图片路径</param> public bool SetImage(string FileName) { srcFileName = HotoUtils.GetMapPath(FileName); try { srcImage = Image.FromFile(srcFileName); } catch { return(false); } return(true); }
/// <summary> /// 获得指定表单参数的值 /// </summary> /// <param name="strName">表单参数</param> /// <param name="sqlSafeCheck">是否进行SQL安全检查</param> /// <returns>表单参数的值</returns> public static string GetFormString(string strName, bool sqlSafeCheck) { if (HttpContext.Current.Request.Form[strName] == null) { return(""); } if (sqlSafeCheck && !HotoUtils.IsSafeSqlString(HttpContext.Current.Request.Form[strName])) { return("unsafe string"); } return(HttpContext.Current.Request.Form[strName]); }
/// <summary> /// 获得当前页面客户端的IP /// </summary> /// <returns>当前页面客户端的IP</returns> public static string GetIP() { string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; GetDnsRealHost(); if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; } if (string.IsNullOrEmpty(result)) { result = HttpContext.Current.Request.UserHostAddress; } if (string.IsNullOrEmpty(result) || !HotoUtils.IsIP(result)) { return("127.0.0.1"); } return(result); }
/// <summary> /// 图片水印 /// </summary> /// <param name="imgPath">服务器图片相对路径</param> /// <param name="filename">保存文件名</param> /// <param name="watermarkFilename">水印文件相对路径</param> /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param> /// <param name="quality">附加水印图片质量,0-100</param> /// <param name="watermarkTransparency">水印的透明度 1--10 10为不透明</param> public static void AddImageSignPic(string imgPath, string filename, string watermarkFilename, int watermarkStatus, int quality, int watermarkTransparency) { if (!File.Exists(HotoUtils.GetMapPath(imgPath))) { return; } byte[] _ImageBytes = File.ReadAllBytes(HotoUtils.GetMapPath(imgPath)); Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes)); filename = HotoUtils.GetMapPath(filename); if (watermarkFilename.StartsWith("/") == false) { watermarkFilename = "/" + watermarkFilename; } watermarkFilename = HotoUtils.GetMapPath(watermarkFilename); if (!File.Exists(watermarkFilename)) { return; } Graphics g = Graphics.FromImage(img); //设置高质量插值法 //g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度 //g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; Image watermark = new Bitmap(watermarkFilename); if (watermark.Height >= img.Height || watermark.Width >= img.Width) { return; } ImageAttributes imageAttributes = new ImageAttributes(); ColorMap colorMap = new ColorMap(); colorMap.OldColor = Color.FromArgb(255, 0, 255, 0); colorMap.NewColor = Color.FromArgb(0, 0, 0, 0); ColorMap[] remapTable = { colorMap }; imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap); float transparency = 0.5F; if (watermarkTransparency >= 1 && watermarkTransparency <= 10) { transparency = (watermarkTransparency / 10.0F); } float[][] colorMatrixElements = { new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, transparency, 0.0f }, new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f } }; ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements); imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap); int xpos = 0; int ypos = 0; switch (watermarkStatus) { case 1: xpos = (int)(img.Width * (float).01); ypos = (int)(img.Height * (float).01); break; case 2: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)(img.Height * (float).01); break; case 3: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)(img.Height * (float).01); break; case 4: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 5: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 6: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)((img.Height * (float).50) - (watermark.Height / 2)); break; case 7: xpos = (int)(img.Width * (float).01); ypos = (int)((img.Height * (float).99) - watermark.Height); break; case 8: xpos = (int)((img.Width * (float).50) - (watermark.Width / 2)); ypos = (int)((img.Height * (float).99) - watermark.Height); break; case 9: xpos = (int)((img.Width * (float).99) - (watermark.Width)); ypos = (int)((img.Height * (float).99) - watermark.Height); break; } g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo codec in codecs) { if (codec.MimeType.IndexOf("jpeg") > -1) { ici = codec; } } EncoderParameters encoderParams = new EncoderParameters(); long[] qualityParam = new long[1]; if (quality < 0 || quality > 100) { quality = 80; } qualityParam[0] = quality; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; if (ici != null) { img.Save(filename, ici, encoderParams); } else { img.Save(filename); } g.Dispose(); img.Dispose(); watermark.Dispose(); imageAttributes.Dispose(); }
/// <summary> /// 文字水印 /// </summary> /// <param name="imgPath">服务器图片相对路径</param> /// <param name="filename">保存文件名</param> /// <param name="watermarkText">水印文字</param> /// <param name="watermarkStatus">图片水印位置 0=不使用 1=左上 2=中上 3=右上 4=左中 9=右下</param> /// <param name="quality">附加水印图片质量,0-100</param> /// <param name="fontname">字体</param> /// <param name="fontsize">字体大小</param> public static void AddImageSignText(string imgPath, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize) { byte[] _ImageBytes = File.ReadAllBytes(HotoUtils.GetMapPath(imgPath)); Image img = Image.FromStream(new System.IO.MemoryStream(_ImageBytes)); filename = HotoUtils.GetMapPath(filename); Graphics g = Graphics.FromImage(img); Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel); SizeF crSize; crSize = g.MeasureString(watermarkText, drawFont); float xpos = 0; float ypos = 0; switch (watermarkStatus) { case 1: xpos = (float)img.Width * (float).01; ypos = (float)img.Height * (float).01; break; case 2: xpos = ((float)img.Width * (float).50) - (crSize.Width / 2); ypos = (float)img.Height * (float).01; break; case 3: xpos = ((float)img.Width * (float).99) - crSize.Width; ypos = (float)img.Height * (float).01; break; case 4: xpos = (float)img.Width * (float).01; ypos = ((float)img.Height * (float).50) - (crSize.Height / 2); break; case 5: xpos = ((float)img.Width * (float).50) - (crSize.Width / 2); ypos = ((float)img.Height * (float).50) - (crSize.Height / 2); break; case 6: xpos = ((float)img.Width * (float).99) - crSize.Width; ypos = ((float)img.Height * (float).50) - (crSize.Height / 2); break; case 7: xpos = (float)img.Width * (float).01; ypos = ((float)img.Height * (float).99) - crSize.Height; break; case 8: xpos = ((float)img.Width * (float).50) - (crSize.Width / 2); ypos = ((float)img.Height * (float).99) - crSize.Height; break; case 9: xpos = ((float)img.Width * (float).99) - crSize.Width; ypos = ((float)img.Height * (float).99) - crSize.Height; break; } g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1); g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos); ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; foreach (ImageCodecInfo codec in codecs) { if (codec.MimeType.IndexOf("jpeg") > -1) { ici = codec; } } EncoderParameters encoderParams = new EncoderParameters(); long[] qualityParam = new long[1]; if (quality < 0 || quality > 100) { quality = 80; } qualityParam[0] = quality; EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam); encoderParams.Param[0] = encoderParam; if (ici != null) { img.Save(filename, ici, encoderParams); } else { img.Save(filename); } g.Dispose(); img.Dispose(); }
/// <summary> /// 获得指定表单参数的float类型值 /// </summary> /// <param name="strName">表单参数</param> /// <param name="defValue">缺省值</param> /// <returns>表单参数的float类型值</returns> public static float GetFormFloat(string strName, float defValue) { return(HotoUtils.StrToFloat(HttpContext.Current.Request.Form[strName], defValue)); }
/// <summary> /// 获得指定表单参数的decimal类型值 /// </summary> /// <param name="strName">表单参数</param> /// <param name="defValue">缺省值</param> /// <returns>表单参数的decimal类型值</returns> public static decimal GetFormDecimal(string strName, decimal defValue) { return(HotoUtils.StrToDecimal(HttpContext.Current.Request.Form[strName], defValue)); }
/// <summary> /// 获得指定表单参数的int类型值 /// </summary> /// <param name="strName">表单参数</param> /// <param name="defValue">缺省值</param> /// <returns>表单参数的int类型值</returns> public static int GetFormInt(string strName, int defValue) { return(HotoUtils.StrToInt(HttpContext.Current.Request.Form[strName], defValue)); }
/// <summary> /// 获得指定Url参数的int类型值 /// </summary> /// <param name="strName">Url参数</param> /// <returns>Url参数的int类型值</returns> public static int GetQueryInt(string strName) { return(HotoUtils.StrToInt(HttpContext.Current.Request.QueryString[strName], 0)); }
/// <summary> /// 获得模板字符串,从设置中的模板路径来读取模板文件. /// </summary> /// <param name="sitePath">站点目录</param> /// <param name="tempPath">模板目录</param> /// <param name="skinName">模板名</param> /// <param name="templateName">模板文件的文件名称</param> /// <param name="fromPage">源页面名称</param> /// <param name="inherit">该页面继承的类</param> /// <param name="channelId">该页面所属的频道ID</param> /// <param name="nest">嵌套次数</param> /// <returns>string值,如果失败则为"",成功则为模板内容的string</returns> public static string GetTemplate(string sitePath, string tempPath, string skinName, string templet, string fromPage, string inherit, int channelId, int nest) { StringBuilder strReturn = new StringBuilder(220000); //返回的字符 string templetFullPath = HotoUtils.GetMapPath(sitePath + tempPath + "/" + skinName + "/" + templet); //取得模板文件物理路径 //超过5次嵌套退出 if (nest < 1) { nest = 1; } else if (nest > 5) { return(""); } string extNamespace = ""; //检查模板文件是否存在 if (!File.Exists(templetFullPath)) { return(""); } //开始读写文件 using (StreamReader objReader = new StreamReader(templetFullPath, Encoding.UTF8)) { StringBuilder textOutput = new StringBuilder(70000); textOutput.Append(objReader.ReadToEnd()); objReader.Close(); //处理Csharp语句 foreach (Match m in r[2].Matches(textOutput.ToString())) { textOutput.Replace(m.Groups[0].ToString(), m.Groups[0].ToString().Replace("\r\n", "\r\t\r")); } //替换标记 textOutput.Replace("\r\n", "\r\r\r"); textOutput.Replace("<%", "\r\r\n<%"); textOutput.Replace("%>", "%>\r\r\n"); textOutput.Replace("<%csharp%>\r\r\n", "<%csharp%>").Replace("\r\r\n<%/csharp%>", "<%/csharp%>"); string[] strlist = HotoUtils.SplitString(textOutput.ToString(), "\r\r\n"); for (int i = 0; i < strlist.Length; i++) { if (strlist[i] == "") { continue; } //替换标签 strReturn.Append(ConvertTags(nest, channelId, sitePath, tempPath, skinName, strlist[i])); } //写入文件 if (nest == 1) { //命名空间 foreach (Match m in r[1].Matches(textOutput.ToString())) { extNamespace += "\r\n<%@ Import namespace=\"" + m.Groups[1] + "\" %>"; textOutput.Replace(m.Groups[0].ToString(), string.Empty); } //频道ID string channelStr = string.Empty; if (channelId > 0) { channelStr = "\r\n\tconst int channel_id = " + channelId + ";"; } string template = string.Format("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" Inherits=\"{0}\" ValidateRequest=\"false\" %>\r\n<%@ Import namespace=\"System.Collections.Generic\" %>\r\n<%@ Import namespace=\"System.Text\" %>\r\n<%@ Import namespace=\"System.Data\" %>\r\n<%@ Import namespace=\"Hoto.Common\" %>\r\n{1}\r\n<script runat=\"server\">\r\noverride protected void OnInit(EventArgs e)\r\n{{\r\n\r\n\t/* \r\n\t\tThis page was created by DTcms Template Engine at {2}.\r\n\t\t本页面代码由DTcms模板引擎生成于 {2}. \r\n\t*/\r\n\r\n\tbase.OnInit(e);\r\n\tStringBuilder templateBuilder = new StringBuilder({3});{4}\r\n{5}\r\n\tResponse.Write(templateBuilder.ToString());\r\n}}\r\n</script>\r\n", inherit, extNamespace, DateTime.Now, strReturn.Capacity, channelStr, Regex.Replace(strReturn.ToString(), @"\r\n\s*templateBuilder\.Append\(""""\);", "")); string pageDir = HotoUtils.GetMapPath(sitePath + "aspx/"); string outputPath = pageDir + fromPage; if (!Directory.Exists(pageDir)) { Directory.CreateDirectory(pageDir); } File.WriteAllText(outputPath, template, Encoding.UTF8); //using (FileStream fs = new FileStream(outputPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) //{ // Byte[] info = Encoding.UTF8.GetBytes(template); // fs.Write(info, 0, info.Length); // fs.Close(); //} } } return(strReturn.ToString()); }
/// <summary> /// 转换标签 /// </summary> /// <param name="nest">深度</param> /// <param name="sitePath">站点目录</param> /// <param name="skinName">模板名称</param> /// <param name="inputStr">模板内容</param> /// <returns></returns> private static string ConvertTags(int nest, int channelId, string sitePath, string tempPath, string skinName, string inputStr) { string strReturn = ""; string strTemplate; strTemplate = inputStr.Replace("\\", "\\\\"); strTemplate = strTemplate.Replace("\"", "\\\""); strTemplate = strTemplate.Replace("</script>", "</\");\r\n\ttemplateBuilder.Append(\"script>"); bool IsCodeLine = false; //解析嵌套标签========================================================= foreach (Match m in r[0].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\r\n" + GetTemplate(sitePath, tempPath, skinName, m.Groups[1].ToString(), "", "", channelId, nest + 1) + "\r\n"); } //解析csharpcode=================================================== foreach (Match m in r[2].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), m.Groups[1].ToString().Replace("\r\t\r", "\r\n\t").Replace("\\\"", "\"")); } //解析loop标签===================================================== foreach (Match m in r[3].Matches(strTemplate)) { IsCodeLine = true; if (m.Groups[3].ToString() == "") { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\r\n\tint {0}__loop__id=0;\r\n\tforeach(DataRow {0} in {1}.Rows)\r\n\t{{\r\n\t\t{0}__loop__id++;\r\n", m.Groups[4], m.Groups[5])); } else { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\r\n\tint {1}__loop__id=0;\r\n\tforeach({0} {1} in {2})\r\n\t{{\r\n\t\t{1}__loop__id++;\r\n", m.Groups[3], m.Groups[4], m.Groups[5])); } } foreach (Match m in r[4].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\r\n\t}\t//end loop\r\n"); } //解译判断IF语句======================================================= foreach (Match m in r[5].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\r\n\tif (" + m.Groups[1].ToString().Replace("\\\"", "\"") + ")\r\n\t{"); } foreach (Match m in r[6].Matches(strTemplate)) { IsCodeLine = true; if (m.Groups[1].ToString() == string.Empty) { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\r\n\t}\r\n\telse\r\n\t{"); } else { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\r\n\t}\r\n\telse if (" + m.Groups[3].ToString().Replace("\\\"", "\"") + ")\r\n\t{"); } } foreach (Match m in r[7].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\r\n\t}\t//end if\r\n"); } //解析continue,break================================================== foreach (Match m in r[8].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\tcontinue;\r\n"); } foreach (Match m in r[9].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "\tbreak;\r\n"); } //解析{request[a]}===================================================== foreach (Match m in r[10].Matches(strTemplate)) { if (IsCodeLine) { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "DTRequest.GetString(\"" + m.Groups[2] + "\")"); } else { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\" + DTRequest.GetString(\"{0}\") + \"", m.Groups[2])); } } //解析截取字符串======================================================= foreach (Match m in r[11].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\r\n\ttemplateBuilder.Append(Utils.DropHTML({0},{1}));", m.Groups[2], m.Groups[3].ToString().Trim())); } //解析时间格式转换===================================================== foreach (Match m in r[16].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\ttemplateBuilder.Append(Utils.ObjectToDateTime({0}).ToString(\"{1}\"));", m.Groups[2], m.Groups[3].ToString().Replace("\\\"", string.Empty))); } //字符串转换整型======================================================= foreach (Match m in r[17].Matches(strTemplate)) { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "Utils.StrToInt(" + m.Groups[2] + ", 0)"); } //解析url链接标签====================================================== foreach (Match m in r[12].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\r\n\ttemplateBuilder.Append(linkurl({0}));", m.Groups[2]).Replace("\\\"", "\"") ); } //解析set标签========================================================== foreach (Match m in r[13].Matches(strTemplate)) { IsCodeLine = true; string type = ""; if (m.Groups[3].ToString() != string.Empty) { type = m.Groups[3].ToString(); } strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\r\n\t{0} {1} = {2};\r\n\t", type, m.Groups[4], m.Groups[5]).Replace("\\\"", "\"") ); } //解析write标签======================================================== foreach (Match m in r[18].Matches(strTemplate)) { IsCodeLine = true; strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\r\n\ttemplateBuilder.Append({0}{1}.ToString());\r\n\t", m.Groups[1], m.Groups[2]).Replace("\\\"", "\"") ); } //解析{var[a]}========================================================= foreach (Match m in r[14].Matches(strTemplate)) { if (IsCodeLine) { if (HotoUtils.IsNumeric(m.Groups[3].ToString())) { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "Utils.ObjectToStr(" + m.Groups[2] + "[" + m.Groups[3] + "])"); } else { if (m.Groups[3].ToString() == "_id") { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), m.Groups[2] + "__loop__id"); } else { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), "Utils.ObjectToStr(" + m.Groups[2] + "[\"" + m.Groups[3] + "\"])"); } } } else { if (HotoUtils.IsNumeric(m.Groups[3].ToString())) { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\" + Utils.ObjectToStr({0}[{1}]) + \"", m.Groups[2], m.Groups[3])); } else { if (m.Groups[3].ToString() == "_id") { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\" + {0}__loop__id.ToString() + \"", m.Groups[2])); } else { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\" + Utils.ObjectToStr({0}[\"{1}\"]) + \"", m.Groups[2], m.Groups[3])); } } } } //解析普通变量{}======================================================= foreach (Match m in r[15].Matches(strTemplate)) { if (IsCodeLine) { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), m.Groups[2].ToString()); } else { strTemplate = strTemplate.Replace(m.Groups[0].ToString(), string.Format("\");\r\n\ttemplateBuilder.Append(Utils.ObjectToStr({0}));\r\n\ttemplateBuilder.Append(\"", m.Groups[2].ToString().Trim())); } } //最后处理============================================================= if (IsCodeLine) { strReturn = strTemplate + "\r\n"; } else { if (strTemplate.Trim() != "") { strReturn = "\r\n\ttemplateBuilder.Append(\"" + strTemplate.Replace("\r\r\r", "\\r\\n") + "\");"; strReturn = strReturn.Replace("\\r\\n<?xml", "<?xml"); strReturn = strReturn.Replace("\\r\\n<!DOCTYPE", "<!DOCTYPE"); } } return(strReturn); }