<%@ Page Language="C#" AutoEventWireup="true" Description="火车采集器E动力发布接口" ValidateRequest="false"%>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>
<script runat=server>
private string pw = "123456";//这个是登陆的密码
protected override void OnInit(EventArgs e)
{
    string pw2 = this.Request["pw"];
    if (pw2 != pw)
    {
        Response.Write("验证密码不正确");
        Response.End();
    }
    try
    {
        yycar.ADO.Config.InitLoad();//初使化sql链接数据
        this.conn = new System.Data.OleDb.OleDbConnection("Provider=SQLOLEDB;" + yycar.ADO.Config.Sql);
        if (this.Request.HttpMethod == "GET")
        {
            string sql = "select id,itemname,pid from auto_news_class";
            //对查询的结果进行处理
            System.Data.DataTable dt = this.GetDataTable(sql);
            string catestr = this.makeTree(dt, "0", "　");
            Response.Write("<select name='list'>" + catestr + "</select>");
        }
        else
        {
            string userid = "1";
            string userip = "";
            string usertype = "Admin";//1是管理员

            yycar.Public.ClientXML txml = new yycar.Public.ClientXML();
            //<r details='0' ><m  pid='64' cid='0' sid='0' itemname='11111111111111111111111' itemname2='22222222222222222222222' picpath='' summary='' fontcolor=''
            //fontbold='0' keys='' author='' source='' url='' ifgood='0' memo='33333333333333333333333333333333333333' memohtml='33333333333333333333333333333333333333&lt;br /&gt;' htmlposition=',' brand=''  /><method>ManageAdd</method>

            if (!string.IsNullOrEmpty(this.Request["pid"]) && System.Text.RegularExpressions.Regex.IsMatch(this.Request["pid"], "^\\d+$"))
            {
                string sql = "select * from auto_news_class where id=" + this.Request["pid"];
                System.Data.DataTable dt = this.GetDataTable(sql);
                if (dt.Rows.Count == 0)
                {
                    this.Response.Write("不存在的栏目id");
                    return;
                }
                else
                {
                    if (dt.Rows[0]["type"].ToString() == "0")
                    {
                        this.Response.Write("该栏目不允许发表文章(" + this.Request["pid"] + ")");
                        return;
                    }
                }
            }
            if (string.IsNullOrEmpty(this.Request["memohtml"]))
            {
                this.Response.Write("内容不得为空");
                return;
            }
            if (string.IsNullOrEmpty(this.Request["itemname2"]))
            {
                this.Response.Write("标题不得为空");
                return;
            }
            
            try
            {
                txml.addMNode("pid", this.Request["pid"]);
                txml.addMNode("cid", this.Request["cid"] == null ? "0" : this.Request["cid"]);
                txml.addMNode("sid", this.Request["sid"] == null ? "0" : this.Request["sid"]);
                txml.addMNode("itemname", this.Request["itemname"]==null?"":this.Request["itemname"]);
                txml.addMNode("itemname2", this.Request["itemname2"]);
                txml.addMNode("picpath", this.Request["picpath"] == null ? "" : this.Request["picpath"]);
                txml.addMNode("summary", this.Request["summary"] == null ? "" : this.Request["summary"]);
                txml.addMNode("fontcolor", "");
                txml.addMNode("fontbold", "0");
                txml.addMNode("keys", "");
                txml.addMNode("author", this.Request["author"] == null ? "" : this.Request["author"]);
                txml.addMNode("source", this.Request["source"] == null ? "" : this.Request["source"]);
                txml.addMNode("url", "");
                txml.addMNode("ifgood", "0");
                txml.addMNode("memo", this.Request["memo"] == null ? "" : this.Request["memo"]);
                txml.addMNode("memohtml", this.Request["memohtml"]);
                txml.addMNode("htmlposition", ",");
                txml.addMNode("brand", "");
            }
            catch (Exception ex)
            {
                this.Response.Write("生成xml出错:" + ex);
                return;
            }

            yycar.UI.Base base2 = new yycar.UI.Base();
            System.Data.DataSet dataSet = base2.GetData("auto_news_add", txml.OuterXml, userid, userip, "201", usertype);
            if (base2.ErrorMsg != "")
            {
                Response.Write(base2.ErrorMsg);
                return;
            }

            Response.Write("内容发布成功");
 
            string str4 = dataSet.Tables[0].Rows[0][0].ToString();
            string str5 = dataSet.Tables[0].Rows[0][1].ToString();
            string str6 = "";
            try
            {
                DataSet ds = base2.GetData("html_news", "<r><m id='" + str4 + "' /></r>", userid, userip, "12", usertype);
                str6 = this.html_show(ds); 
                
                if (str6 != "1")
                {
                    this.Response.Write(",生成内容页时发生错误:" + str6);
                    return;
                }
                this.Response.Write(",生成静态页成功");
            }
            catch (Exception exception1)
            {
                Response.Write("生成静态时发生错误:" + exception1.Message + "\r\n\r\n提交的数据为:" + txml.OuterXml);
                return;
            }
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
    base.OnInit(e);
}
    
public string html_show(DataSet set)
{
    try
    {
        if (set == null)
        {
            return "更新新闻浏览页面时，连接数据库出错！";
        }
        if (set.Tables[0].Rows.Count == 0)
        {
            return "更新新闻浏览页面时，暂无数据！";
        }
        string fileContent = "";
        if (set.Tables[0].Rows[0]["url"].ToString() == "")
        {
            DateTime time;
            string str14;
            fileContent = new yycar.Lable.PageTemplate_s().ConvertTags("temp_news_shownews.html");
            if (fileContent == "")
            {
                return "更新新闻浏览页面时，模板读取出错！";
            }
            string newValue = "";
            string oldValue = "[page]***[/page]";
            DataRow row = set.Tables[0].Rows[0];
            string str4 = row["id"].ToString();
            string str5 = string.Format("{0:yyyyMM}", Convert.ToDateTime(row["createtime"].ToString()));
            fileContent = fileContent.Replace("@id@", str4).Replace("@curname@", row["itemname"].ToString()).Replace("@memo@", row["memo"].ToString().Replace(" ", "").Replace("　", "").Replace("?", "").Replace("'", "").Replace("\"", ""));
            fileContent = (row["keys"].ToString() == "") ? fileContent.Replace("@keys@", "") : fileContent.Replace("@keys@", "," + row["keys"].ToString().Replace(" ", "").Replace("　", "").Replace("?", "").Replace("'", "").Replace("\"", ""));
            newValue = "";
            if (row["brand"].ToString().Trim() != "")
            {
                string strString = row["brand"].ToString().Split(new char[] { ',' })[0].Split(new char[] { '|' })[0];
                if (yycar.Public.NumFuns.IsNumber(strString))
                {
                    DataSet dataSet = new yycar.ADO.ADO().GetDataSet("select top 1 a.id,a.itemname,a.bbs,b.itemname grade,a.dir from auto_car_brand a inner join auto_car_grade b on a.grade=b.id where a.id=" + strString);
                    if (dataSet != null)
                    {
                        if (dataSet.Tables[0].Rows.Count != 0)
                        {
                            fileContent = fileContent.Replace("@carid@", dataSet.Tables[0].Rows[0]["dir"].ToString());
                            if (dataSet.Tables[0].Rows[0]["bbs"].ToString().Trim() != "")
                            {
                                fileContent = fileContent.Replace("@bbs@", dataSet.Tables[0].Rows[0]["bbs"].ToString());
                            }
                            else
                            {
                                fileContent = fileContent.Replace("@bbs@", "#");
                            }
                            fileContent = fileContent.Replace("@carname@", dataSet.Tables[0].Rows[0]["itemname"].ToString()).Replace("@cargrade@", dataSet.Tables[0].Rows[0]["grade"].ToString()).Replace("@tag@", "").Replace("@/tag@", "");
                        }
                        else
                        {
                            fileContent = fileContent.Substring(0, fileContent.IndexOf("@tag@")) + fileContent.Substring(fileContent.IndexOf("@/tag@") + 6, (fileContent.Length - fileContent.IndexOf("@/tag@")) - 6);
                        }
                    }
                }
            }
            else
            {
                fileContent = fileContent.Substring(0, fileContent.IndexOf("@tag@")) + fileContent.Substring(fileContent.IndexOf("@/tag@") + 6, (fileContent.Length - fileContent.IndexOf("@/tag@")) - 6);
            }
            newValue = "";
            string str7 = "";
            for (int i = 0; i < set.Tables[1].Rows.Count; i++)
            {
                if (i == (set.Tables[1].Rows.Count - 1))
                {
                    str14 = newValue;
                    newValue = str14 + "<a href='/list/" + set.Tables[1].Rows[i]["dir"].ToString() + "/1.html' title='' target=''>" + set.Tables[1].Rows[i]["itemname"].ToString() + "</a>";
                }
                else
                {
                    str14 = newValue;
                    newValue = str14 + "<a href='/list/" + set.Tables[1].Rows[i]["dir"].ToString() + "/1.html' title='' target=''>" + set.Tables[1].Rows[i]["itemname"].ToString() + "</a> | ";
                }
            }
            fileContent = fileContent.Replace("@top@", newValue);
            newValue = "<a href='/list/" + row["dir"].ToString() + "/1.html' title='" + row["classname"].ToString() + "' target='_self'>" + row["classname"].ToString() + "</a>";
            fileContent = fileContent.Replace("@curpath@", newValue).Replace("@itemname@", row["itemname"].ToString()).Replace("@author@", row["author"].ToString()).Replace("@source@", row["source"].ToString()).Replace("@createtime@", Convert.ToDateTime(row["createtime"].ToString()).ToLongDateString().ToString()).Replace("@ups@", row["ups"].ToString()).Replace("@downs@", row["downs"].ToString()).Replace("@msgs@", row["msgs"].ToString()).Replace("@url@", yycar.Lable.Config.Url + "/news/" + str5 + "/" + str4 + ".html");
            newValue = "";
            foreach (string str8 in row["keys"].ToString().Split(new char[] { ',' }))
            {
                if (!(str8 == ""))
                {
                    str14 = newValue;
                    newValue = str14 + "<a href=\"/news/search.aspx?m=" + base.Server.UrlEncode(str8) + "\" target=\"_blank\" >" + str8 + "</a> ";
                }
            }
            if (newValue == "")
            {
                newValue = " 无";
            }
            fileContent = fileContent.Replace("@tags@", newValue);
            newValue = "";
            foreach (DataRow row2 in set.Tables[2].Rows)
            {
                time = Convert.ToDateTime(row2["createtime"].ToString());
                str14 = newValue;
                newValue = str14 + "<li><a href='../" + string.Format("{0:yyyyMM}", time) + "/" + row2["id"].ToString() + ".html' title='" + row2["itemname"].ToString() + "' target='_self'>" + row2["itemname"].ToString() + "</a><span class=note>" + time.ToLongDateString() + "</span></li>";
            }
            if (newValue == "")
            {
                foreach (DataRow row2 in set.Tables[3].Rows)
                {
                    time = Convert.ToDateTime(row2["createtime"].ToString());
                    str14 = newValue;
                    newValue = str14 + "<li><a href='../" + string.Format("{0:yyyyMM}", time) + "/" + row2["id"].ToString() + ".html' title='" + row2["itemname"].ToString() + "' target='_self'>" + row2["itemname"].ToString() + "</a><span class=note>" + time.ToLongDateString() + "</span></li>";
                }
            }
            fileContent = fileContent.Replace("@contentend@", newValue);
            string str9 = row["memohtml"].ToString();
            string str10 = "";
            int length = 0;
            int num3 = 0;
            int num4 = (str9.Replace(oldValue, oldValue + "1").Length - str9.Length) + 1;
            if (num4 == 1)
            {
                newValue = row["memohtml"].ToString();
                fileContent = fileContent.Replace("@content@", newValue).Replace("@btn@", "");
                yycar.Public.fileobject.WriteFile(base.Server.MapPath("~/news/" + str5 + "/" + str4 + ".html"), fileContent);
            }
            else
            {
                str9 = str9 + oldValue;
                while (str9.IndexOf(oldValue) > 0)
                {
                    int num5;
                    num3++;
                    length = str9.IndexOf(oldValue);
                    str10 = str9.Substring(0, length);
                    string str12 = fileContent;
                    str12 = str12.Replace("@content@", str10);
                    str9 = str9.Substring(length + oldValue.Length, (str9.Length - length) - oldValue.Length);
                    if (num3 == 1)
                    {
                        str7 = "<span class='Total'>共 " + num4.ToString() + " 条 1/" + num4.ToString() + "</span><span class=current >1</span>";
                        num5 = 2;
                        while (num5 <= num4)
                        {
                            str14 = str7;
                            str7 = str14 + "<a href='" + str4 + "_" + num5.ToString() + ".html'>" + num5.ToString() + "</a>";
                            num5++;
                        }
                        str14 = str7;
                        str7 = str14 + "<a href='" + str4 + "_2.html'>下一页</a><a href='" + str4 + "_" + num4.ToString() + ".html' >末页</a>";
                        str12 = str12.Replace("@btn@", str7);
                        yycar.Public.fileobject.WriteFile(base.Server.MapPath("~/news/" + str5 + "/" + str4 + ".html"), str12);
                    }
                    else
                    {
                        string[] strArray;
                        if (num3 == num4)
                        {
                            str7 = "<span class='Total'>共 " + num4.ToString() + " 条 " + num4.ToString() + "/" + num4.ToString() + "</span>";
                            if ((num4 - 1) == 1)
                            {
                                str14 = str7;
                                str7 = str14 + "<a href='" + str4 + ".html' >首页</a><a href='" + str4 + ".html' >上一页</a>";
                            }
                            else
                            {
                                str14 = str7;
                                strArray = new string[8];
                                strArray[0] = str14;
                                strArray[1] = "<a href='";
                                strArray[2] = str4;
                                strArray[3] = ".html' >首页</a><a href='";
                                strArray[4] = str4;
                                strArray[5] = "_";
                                int num7 = num4 - 1;
                                strArray[6] = num7.ToString();
                                strArray[7] = ".html' >上一页</a>";
                                str7 = string.Concat(strArray);
                            }
                            str7 = str7 + "<a href='" + str4 + ".html'>1</a>";
                            num5 = 2;
                            while (num5 < num4)
                            {
                                str14 = str7;
                                str7 = str14 + "<a href='" + str4 + "_" + num5.ToString() + ".html'>" + num5.ToString() + "</a>";
                                num5++;
                            }
                            str7 = str7 + "<span class=current >" + num4.ToString() + "</span>";
                            str12 = str12.Replace("@btn@", str7);
                            yycar.Public.fileobject.WriteFile(base.Server.MapPath("~/news/" + str5 + "/" + str4 + "_" + num3.ToString() + ".html"), str12);
                        }
                        else
                        {
                            str7 = "<span class='Total'>共 " + num4.ToString() + " 条 " + num3.ToString() + "/" + num4.ToString() + "</span>";
                            if ((num3 - 1) == 1)
                            {
                                str14 = str7;
                                str7 = str14 + "<a href='" + str4 + ".html' >首页</a><a href='" + str4 + ".html' >上一页</a>";
                            }
                            else
                            {
                                str14 = str7;
                                strArray = new string[] { str14, "<a href='", str4, ".html' >首页</a><a href='", str4, "_", (num3 - 1).ToString(), ".html' >上一页</a>" };
                                str7 = string.Concat(strArray);
                            }
                            str7 = str7 + "<a href='" + str4 + ".html'>1</a>";
                            num5 = 2;
                            while (num5 < num3)
                            {
                                str14 = str7;
                                str7 = str14 + "<a href='" + str4 + "_" + num5.ToString() + ".html'>" + num5.ToString() + "</a>";
                                num5++;
                            }
                            str7 = str7 + "<span class=current >" + num3.ToString() + "</span>";
                            for (num5 = num3 + 1; num5 <= num4; num5++)
                            {
                                str14 = str7;
                                str7 = str14 + "<a href='" + str4 + "_" + num5.ToString() + ".html'>" + num5.ToString() + "</a>";
                            }
                            str14 = str7;
                            strArray = new string[] { str14, "<a href='", str4, "_", (num3 + 1).ToString(), ".html'>下一页</a><a href='", str4, "_", num4.ToString(), ".html' >末页</a>" };
                            str7 = string.Concat(strArray);
                            str12 = str12.Replace("@btn@", str7);
                            yycar.Public.fileobject.WriteFile(base.Server.MapPath("~/news/" + str5 + "/" + str4 + "_" + num3.ToString() + ".html"), str12);
                        }
                    }
                }
            }
        }
        return "1";
    }
    catch (Exception exception)
    {
        return exception.Message;
    }
}
 
private System.Data.OleDb.OleDbConnection conn = null;

private string makeTree(System.Data.DataTable dt, string pid, string pre)
{
    StringBuilder sb = new StringBuilder();
    System.Data.DataRow[] drs = dt.Select("pid=" + pid);
    string pre2 = "└";
    for (int i = 0; i < drs.Length; i++)
    {
        string cname = drs[i]["itemname"].ToString();
        string id = drs[i]["id"].ToString();
        if (id == pid) continue;
        if (pid == "0") pre = pre2 = "";
        sb.Append("<option value='" + id + "'>" + pre + pre2 + cname + "</option>");
        string next = this.makeTree(dt, id, pre + "　");
        sb.Append(next);
    }
    return sb.ToString();
}

public System.Data.DataTable GetDataTable(string sql)
{
    if (this.conn.State != System.Data.ConnectionState.Open) this.conn.Open();
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = this.conn;
    System.Data.DataSet ds = new System.Data.DataSet();
    OleDbDataAdapter da = new OleDbDataAdapter();
    cmd.CommandText = sql;
    da.SelectCommand = cmd;
    try
    {
        da.Fill(ds);
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
    }
    this.conn.Close();
    if (ds.Tables.Count > 0) return ds.Tables[0];
    else return new System.Data.DataTable();
}
</script>