using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using JuSNS.Config;
using JuSNS.Model;
using System.IO;
using JuSNS.Common;
using JuSNS.Home;

namespace JuSNS.UI.Page
{
    public class BasePage : System.Web.UI.Page
    {

        /// <summary>
        /// 得到虚拟目录
        /// </summary>
        protected string rooDir;

        /// <summary>
        /// 当前登录用户ID
        /// </summary>
        protected int UserID;

        /// <summary>
        /// 当前登录用户姓名
        /// </summary>
        protected string UserName;

        /// <summary>
        /// 当前用户电子邮件
        /// </summary>
        protected string UserEmail;

        protected const int PAGESZ = 20;
        /// <summary>
        /// 登录用户的令牌
        /// </summary>
        protected HttpCookie SNSToKenCookie;

        /// <summary>
        /// 皮肤Cookies
        /// </summary>
        protected HttpCookie skinStyle;

        /// <summary>
        /// 当前登录用户的头像地址
        /// </summary>
        protected string UserPortrait;
        /// <summary>
        /// 成功信息
        /// </summary>
        protected string SucMsg = string.Empty;
        /// <summary>
        /// 错误信息
        /// </summary>
        protected string ErrMsg = string.Empty;
        /// 构造函数
        /// </summary>
        public BasePage()
        {
            this.Load += new EventHandler(BasePage_Load);
            this.LoadComplete += new EventHandler(BasePage_LoadComplete);
        }


        /// <summary>
        /// LOAD事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BasePage_Load(object sender, EventArgs e)
        {
            string GetValidateIP = Public.GetXMLValue("ValidateIP");
            if (!Public.ValidateIP(GetValidateIP))
            {
                Response.Write("你所在的IP段(" + Public.GetClientIP() + ")已经被限制！");
                Response.End();
            }
            rooDir = Public.rootDir;
            AddRootToJs();
        }

        /// <summary>
        /// 为JS增加虚拟目录
        /// </summary>
        private void AddRootToJs()
        {
            StreamReader sr = new StreamReader(Server.MapPath("~/js/public.js"));
            string s = string.Empty;
            string ss = string.Empty;
            s = sr.ReadLine();
            string sss = sr.ReadToEnd(); ;
            ss = s + "\r\n" + sss;
            sr.Dispose();
            if (s.IndexOf("RootDir=") == -1)
            {
                StreamWriter sw = new StreamWriter(new FileStream(Server.MapPath("~/js/public.js"), FileMode.Open), Encoding.UTF8);
                sw.WriteLine("var RootDir='" + rooDir + "';");
                sw.Write(ss);
                sw.Close();
                sw.Dispose();
            }
            else
            {
                if (s.IndexOf("RootDir='" + rooDir + "';") == -1)
                {
                    s = Regex.Replace(s, @"RootDir=(\S*)$", "RootDir='" + rooDir + "';");
                    StreamWriter sw = new StreamWriter(new FileStream(Server.MapPath("~/js/public.js"), FileMode.Open), Encoding.UTF8);
                    sw.WriteLine(s);
                    sw.Write(sss);
                    sw.Close();
                    sw.Dispose();
                }
            }
        }
    
        /// <summary>
        /// LOAD事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BasePage_LoadComplete(object sender, EventArgs e)
        {
            HtmlGenericControl control = (HtmlGenericControl)this.FindControl("ptitle");
            if (control != null)
            {
                control.InnerText = control.InnerText + " - " + UiConfig.PageTitle;
            }
            #region 处理错误信息
            SucMsg = Request.QueryString["sucmsg"];
            ErrMsg = Request.QueryString["errmsg"];
            System.Web.UI.Control errid = this.FindControl("errmsg");
            if ((errid != null) && (!string.IsNullOrEmpty(ErrMsg)))
            {
                control = (System.Web.UI.HtmlControls.HtmlGenericControl)errid;
                if (!string.IsNullOrEmpty(control.InnerHtml))
                {
                    control.InnerHtml = ErrMsg;
                }
                control.Visible = true;
            }
            else
            {
                if (errid != null) errid.Visible = false;
            }
            System.Web.UI.Control sucid = this.FindControl("sucmsg");
            if ((sucid != null) && (!string.IsNullOrEmpty(SucMsg)))
            {
                control = (System.Web.UI.HtmlControls.HtmlGenericControl)sucid;
                if (!string.IsNullOrEmpty(control.InnerHtml))
                {
                    control.InnerHtml = SucMsg;
                }
                control.Visible = true;
            }
            else
            {
                if (sucid != null) sucid.Visible = false;
            }
            #endregion
        }

        /// <summary>
        /// 取得当前登陆的用户ID(basepage状态)
        /// </summary>
        /// <returns></returns>
        protected int GetUserID()
        {
            return GetCUserID();
        }

        protected void PageError(string ErrMsg, string Url)
        {
            PageError(ErrMsg, Url, false);
        }

        protected void PageError(string ErrMsg, string Url, bool RetrunUrl)
        {
            string tmStr = "Error";
            string s = ParseTemplate.replaceTemplate(tmStr);
            s = s.Replace("[@MSGStr]", ErrMsg);
            s = s.Replace("[@GetHtmlHead]", this.GetHtmlHead());
            s = s.Replace("[@GetHtmlFoot]", this.GetHtmlFoot());
            s = s.Replace("[@GetHtmlShort]", this.GetHtmlShort());
            s = s.Replace("[@GetHtmlLeft]", this.GetHtmlLeft());
            if (RetrunUrl)
            {
                s = s.Replace("[@Refresh]", "<meta http-equiv=\"Refresh\" content=\"8;URL=" + Url + "\" />\r\n");
                s = s.Replace("[@IsReturn]", "<div>8秒后自动转到：<a href=\"" + Url + "\">" + Url.Replace("-----", "&") + "</a></div>\r\n");
            }
            else
            {
                s = s.Replace("[@Refresh]", string.Empty);
                s = s.Replace("[@IsReturn]", string.Empty);
            }
            HttpContext.Current.Response.Write(s);
            HttpContext.Current.Response.End();
        }

        protected void PageRight(string MSG, string URL)
        {
            string tmStr = "Right";
            string s = ParseTemplate.replaceTemplate(tmStr);
            s = s.Replace("[@MSGStr]", MSG);
            s = s.Replace("[@GetHtmlHead]", this.GetHtmlHead());
            s = s.Replace("[@GetHtmlFoot]", this.GetHtmlFoot());
            s = s.Replace("[@GetHtmlShort]", this.GetHtmlShort());
            s = s.Replace("[@GetHtmlLeft]", this.GetHtmlLeft());
            if (!string.IsNullOrEmpty(URL))
            {
                s = s.Replace("[@Refresh]", "<meta http-equiv=\"Refresh\" content=\"8;URL=" + URL + "\" />\r\n");
                s = s.Replace("[@IsReturn]", "<div>8秒后自动转到：<a href=\"" + URL + "\">" + URL.Replace("-----", "&") + "</a></div>\r\n");
            }
            else
            {
                s = s.Replace("[@Refresh]", string.Empty);
                s = s.Replace("[@IsReturn]", string.Empty);
            }
            HttpContext.Current.Response.Write(s);
            HttpContext.Current.Response.End();
        }

        /// <summary>
        /// 退出
        /// </summary>
        protected virtual void Logout()
        {
            Context.Session.Remove("SITEINFO");
        }

        /// <summary>
        /// 执行一个JS语句
        /// </summary>
        /// <param name="sentence">要执行的语句</param>
        protected void ExecuteJs(string sentence)
        {
            Context.Response.Write("<script language=\"javascript\" type=\"text/javascript\">");
            Context.Response.Write(sentence);
            Context.Response.Write("</script>");
        }

        /// <summary>
        /// 检查是否登录
        /// </summary>
        protected virtual void CheckLogin()
        { }

        /// <summary>
        /// 验证COOKIE是否过期
        /// </summary>
        /// <returns></returns>
        public bool isTimeOut()
        {
            if (this.GetUserID() == 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 取得当前登陆的用户ID
        /// </summary>
        /// <returns></returns>
        /// static public int GetCUserID()
        static public int GetCUserID()
        {
            int UserID = 0;
            HttpCookie cookieToken = HttpContext.Current.Request.Cookies["SNSUserPassPort"];
            if (cookieToken == null || cookieToken["token"] == null || cookieToken["token"] == "")
                return UserID;
            string userCookie = cookieToken["token"];
            string desstr = DES.Decrypt(userCookie, SecretConfig.UserKey, SecretConfig.UserIV);
            string[] userInfo = desstr.Split(SecretConfig.UserSeparator);
            EnumLoginState state = EnumLoginState.Err_TimeOut;
            if (userInfo.Length == 3)
            {
                UserID = int.Parse(userInfo[0]);
                string pass = userInfo[1];
                User us = new User();
                state = us.CheckLogin(UserID, pass);
            }
            if (state == EnumLoginState.Succeed)
            {
                return UserID;
            }
            else
            {
                return 0;
            }
        }

        protected string ReturnIsNotEmail()
        {
            if (JuSNS.Common.Public.GetXMLValue("isNotEmail") == "1")
            {
                User us = new User();
                UserInfo mdl = new UserInfo();
                mdl = us.GetUserInfo(this.UserID);
                if (mdl.State != EnumUserState.Normal)
                {
                    return "NotEmail";
                }
            }
            return string.Empty;
        }

        protected void GetIsNotEmail()
        {
            if (JuSNS.Common.Public.GetXMLValue("isNotEmail") == "1")
            {
                User us = new User();
                UserInfo mdl = new UserInfo();
                mdl = us.GetUserInfo(this.UserID);
                if (mdl.State != EnumUserState.Normal)
                {
                    PageError("<span class=\"font14\">您需要激活电子邮件后才能进行此操作。</span><a href=\"" + rooDir + "/EmailActive.aspx?r=" + this.UserID.ToString() + "\">点这里激活邮箱</a>", rooDir + "/home");
                }
            }
        }

        /// <summary>
        /// 登录状态处理
        /// </summary>
        /// <param name="state"></param>
        protected void CheckResultShow(EnumLoginState loginState, string backUrl)
        {
            string ErrorMsg = string.Empty;
            int isAdminReturn = 0;
            switch (loginState)
            {
                case EnumLoginState.Err_IPLimited:
                    ErrorMsg = "您的IP[" + JuSNS.Common.Public.GetClientIP() + "]被限制，不能登陆！";
                    isAdminReturn = 2;
                    break;
                case EnumLoginState.Err_Locked:
                    ErrorMsg = "您已经被锁定！";
                    isAdminReturn = 0;
                    break;
                case EnumLoginState.Err_AdminLocked:
                    ErrorMsg = "您已经被锁定！";
                    isAdminReturn = 2;
                    break;
                case EnumLoginState.Err_UnActivation:
                    ErrorMsg = "您还没通过电子邮件激活，不能登陆本系统！";
                    isAdminReturn = 0;
                    break;
                case EnumLoginState.Err_TimeOut:
                    ErrorMsg = "你访问的页面需要登录后才能查看";//登录过期
                    break;
                case EnumLoginState.Err_AdminTimeOut:
                    ErrorMsg = "你访问的页面需要登录后才能查看";//登录过期
                    isAdminReturn = 1;
                    break;
                case EnumLoginState.Err_NoAuthority:
                    ErrorMsg = "您没有此项的操作权限！";
                    isAdminReturn = 1;
                    break;
                case EnumLoginState.Err_DbException:
                    ErrorMsg = "系统错误。出错原因可能是：<br />1.与数据库服务器的通信失败；<br />2.数据库连接字符串不正确；<br />3.数据库发生异常。";
                    isAdminReturn = 2;
                    break;
                case EnumLoginState.Err_DurativeLogError:
                    ErrorMsg = "连续错误登陆，您已经被锁定！";
                    isAdminReturn = 0;
                    break;
                case EnumLoginState.Err_NameOrPwdError:
                    ErrorMsg = "用户名不存在或者密码错误";
                    isAdminReturn = 2;
                    break;
                case EnumLoginState.Err_GroupExpire:
                    ErrorMsg = "您的帐号已过期";
                    isAdminReturn = 0;
                    break;
                case EnumLoginState.Err_NotAdmin:
                    ErrorMsg = "抱歉，您不是管理员。您的操作已经记录！<br />您的IP：[" + JuSNS.Common.Public.GetClientIP() + "]已被记录";
                    isAdminReturn = 1;
                    break;
                case EnumLoginState.Succeed:
                    return;
                default:
                    ErrorMsg = "异常错误：" + loginState.ToString();
                    isAdminReturn = 2;
                    break;
            }
            if (isAdminReturn == 1)
            {
                Context.Response.Write("<script language=\"javascript\">window.parent.location.href=\"" + rooDir + "/system/login.aspx\";</script>");
            }
            else
            {
                PageError(ErrorMsg, rooDir + "/?urls=" + Request.Url.ToString().Replace("&", "-----"), true);
            }
            Context.Response.End();
        }

        /// <summary>
        /// 显示错误信息,并中止执行
        /// </summary>
        /// <param name="ErrInfo">错误提示信息</param>
        /// <param name="DivID">容器编号</param>
        protected void ShowErr(string ErrInfo, HtmlContainerControl DivID)
        {
            DivID.Style.Value = "display:";
            DivID.InnerHtml = ErrInfo;
        }

        /// <summary>
        /// 显示操作成功信息
        /// </summary>
        /// <param name="SccInfo">成功提示信息</param>
        /// <param name="DivID">容器编号</param>
        protected void ShowSuc(string SucInfo, HtmlContainerControl DivID)
        {
            DivID.Style.Value = "display:";
            DivID.InnerHtml = SucInfo;
        }

        /// <summary>
        /// 取得原始照片路径
        /// </summary>
        /// <param name="filename">文件名称</param>
        /// <returns></returns>
        protected string GetOrgPic(string filename)
        {
            return Common.Public.GetOrgPic(filename);
        }
        /// <summary>
        /// 取得原始头像路径
        /// </summary>
        /// <param name="filename">文件名称</param>
        /// <returns></returns>
        protected string GetOrgHeadPic(string filename)
        {
            return JuSNS.Common.Public.GetOrgHeadPic(filename);
        }
        /// <summary>
        /// 转换照片路径为缩略图路径
        /// </summary>
        /// <param name="orgPicPath">原始路径</param>
        /// <param name="name">缩小级别</param>
        /// <returns></returns>
        protected string GetSmallPic(string orgPicPath, int size)
        {
            return JuSNS.Common.Public.GetSmallPic(orgPicPath, size);
        }

        /// <summary>
        /// 取得头象
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <param name="size">缩略图</param>
        /// <returns>头象路径</returns>
        protected string GetHeadImage(int userid, int size)
        {
            JuSNS.Home.User u = new JuSNS.Home.User();
            string filepath = u.GetUserHeadPic(userid);
            string tmStr = JuSNS.Common.Public.GetSmallHeadPic(filepath, size);
            int userSex = u.GetUserSex(userid);
            if (userSex == 0 && tmStr.IndexOf("default") != -1)
            {
                tmStr = tmStr.Replace("default", "default_w");
            }
            return tmStr;
        }

        /// <summary>
        /// 取得头象
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <param name="size">缩略图</param>
        /// <returns>头象路径</returns>
        protected string GetHeadImage(object userid, object size)
        {
            return GetHeadImage((int)userid, (int)size);
        }
        /// <summary>
        /// 取得头象
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <returns>头象路径</returns>
        protected string GetHeadImage(int userid)
        {
            return GetHeadImage(userid, 1);
        }
        /// <summary>
        /// 取得头象
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <returns>头象路径</returns>
        protected string GetHeadImage(object userid)
        {
            return GetHeadImage((int)userid);
        }
        /// <summary>
        /// 取得自己的最小的确头像
        /// </summary>
        /// <returns>头象路径</returns>
        protected string GetHeadImage()
        {
            return GetHeadImage(this.UserID, 1);
        }

        /// <summary>
        /// 取得头象路径
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <returns>头象路径</returns>
        protected string GetHeadPath(int userid)
        {
            JuSNS.Home.User u = new JuSNS.Home.User();
            UserInfo ui = u.GetUserInfo(userid);
            JuSNS.Home.Photo p = new JuSNS.Home.Photo();
            PhotoInfo pi = p.Info(ui.Portrait);
            return pi.FilePath;
        }
        /// <summary>
        /// 取得群组头像
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <param name="n">大小级别</param>
        /// <returns></returns>
        protected string GetGroupHead(object filename, int n)
        {
            return JuSNS.Common.Public.GetGroupHead(filename, n);
        }
        /// <summary>
        /// 默认群组头像
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        protected string GetGroupHead(object filename)
        {
            return GetGroupHead(filename, 4);
        }
        /// <summary>
        /// 取得用户名
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        protected string GetUserName(object UserID)
        {
            return GetUserName((int)UserID);
        }
        /// <summary>
        /// 取得用户名
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        protected string GetUserName(int UserID)
        {
            return new User().GetUserName(UserID);
        }
        /// <summary>
        /// 取得应用图标
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        protected string GetAppIcon(string filename)
        {
            return Public.GetAppIcon(filename);
        }
        /// <summary>
        /// 取得应用图路径
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        protected string GetAppIcon(object filename)
        {
            return Public.GetAppIcon(Convert.ToString(filename));
        }
        /// <summary>
        /// 取得空间连接
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
        protected string GetSpaceLink(object UserID)
        {
            return Public.GetSpaceLink((int)UserID);
        }

        /// <summary>
        /// 取得顶部导航
        /// </summary>
        /// <returns></returns>
        protected string GetHtmlShort()
        {
            return GetHtmlShort(0, string.Empty);
        }

        protected string GetHtmlShort(int Num, string URL)
        {
            string list = string.Empty;
            if (JuSNS.Config.UiConfig.isShortmenu == "1" || Num == 1)
            {
                string sStr = string.Empty;
                string v = Request.QueryString["namekey"];
                if (string.IsNullOrEmpty(v))
                {
                    v = "姓名、邮件...";
                }
                sStr += "<input type=\"text\" name=\"s_keyt\"  id=\"s_keyt\" maxlength=\"20\" class=\"f-search c999\" value=\"" + v + "\" onclick=\"this.value=''\" />\r\n";
                sStr += "<a href=\"javascript:Tolsearch();\"><img class=\"cnViSearchSubmit\" src=\"" + rooDir + "/template/" + UiConfig.SkinStyle + "/images/site/zoom.gif\" align=\"absmiddle\" /></a>\r\n";
                sStr += "<a href=\"" + rooDir + "/home/network?s=ram&f=0\"  target=\"_top\">同地区</a>&nbsp; \r\n";
                sStr += "<a href=\"" + rooDir + "/home/ram\"  target=\"_top\">同城</a>&nbsp; \r\n";
                if (Request.Url.ToString().IndexOf("/home/utop.aspx?url=") > -1)
                {
                    sStr += "<a href=\""+rooDir+"/home/search\"  target=\"_top\" title=\"通过高级搜索找朋友\">高级查找</a>\r\n";
                }
                else
                {
                    sStr += "<a href=\"javascript:void(0);\" onclick=\"showSearch();\"  target=\"_top\" title=\"通过高级搜索找朋友\">高级查找</a>\r\n";
                }
                string GetShare = string.Empty;
                string rspace = string.Empty;
                string rIndex = string.Empty;
                if (Num == 1)
                {
                    GetShare += "<a href=\""+rooDir+"/home/share/postshare.aspx?action=newurl&url=" + URL + "&desc=\" target=\"_top\"><img src=\"" + rooDir + "/template/" + UiConfig.SkinStyle + "/images/share.gif\" align=\"absmiddle\" />分享此网页</a>";
                    GetShare += "&nbsp; <img src=\"" + rooDir + "/template/" + UiConfig.SkinStyle + "/images/network.gif\" align=\"absmiddle\" /> <a href=\"" + URL + "\" target=\"_blank\">" + Input.GetSubString(URL,30) + "</a>";
                    rspace = "返回";
                    rIndex = "<a href=\"javascript:history.back();\"  target=\"_top\">返回上一页</a><span class=\"pike\">|</span><a href=\"" + rooDir + "/home\"  target=\"_top\">返回首页</a><span class=\"pike\">|</span>";
                }
                list += "<div id=\"divshort\">";
                list += "<div class=\"nl\">" + rIndex + "<a  target=\"_top\" href=\"" + rooDir + "/user/" + Public.URLWrite("", "r", GetUserID().ToString()) + "\"><a  target=\"_top\"  title=\"访问我的空间\" href=\"" + rooDir + "/user/" + Public.URLWrite("", "r", GetUserID().ToString()) + "\">我的空间</a> " + GetShare + " <span class=\"pike\">|</span> <a href=\"" + rooDir + "/home/friend/invite.aspx\"  target=\"_top\">邀请好友</a> <span class=\"pike\">|</span> 找朋友：" + sStr + "</div> <div class=\"nr\"><span id=\"getsysMessage\" class=\"sysmessage\">loading...</span><script>SysMessage();</script><span id=\"onlinediv\"></span></div>";
                list += "</div>";
            }
            string topAdSTR = "<div class=\"adscss\"><script type=\"text/javascript\" src=\"" + rooDir + "/js/ads/top.js\"></script></div>";
            return list + topAdSTR;
        }


        /// <summary>
        /// 得到公共头部
        /// </summary>
        /// <returns></returns>
        protected string GetHtmlHead()
        {
            string tmStr = "HtmlHead";
            if (isTimeOut())
                tmStr = "NoLoginHtmlHead";
            string s = ParseTemplate.replaceTemplate(tmStr);
            return s;
        }

        /// <summary>
        /// 得到公共左边
        /// </summary>
        /// <returns></returns>
        protected string GetHtmlLeft()
        {
            string tmStr = "menu";
            if (isTimeOut())
            {
                tmStr = "NoLoginMenu";
            }
            string s = ParseTemplate.replaceTemplate(tmStr);
            if (!string.IsNullOrEmpty(Request.QueryString["u"]))
            {
                s = s.Replace("[@URLs]", Request.QueryString["u"]);
            }
            else if (!string.IsNullOrEmpty(Request.QueryString["urls"]))
            {
                s = s.Replace("[@URLs]", Request.QueryString["urls"]);
            }
            else
            {
                s = s.Replace("[@URLs]", Request.Url.ToString());
            }
            s = s.Replace("[@Email]", Request.QueryString["email"]);
            return s;
        }

        /// <summary>
        /// 得到公共左边
        /// </summary>
        /// <returns></returns>
        protected string GetHtmlAdvs()
        {
            string tmStr = "advs";
            string s = ParseTemplate.replaceTemplate(tmStr);
            return s;
        }
        /// <summary>
        /// 得到底部
        /// </summary>
        /// <returns></returns>
        protected string GetHtmlFoot()
        {
            string tmStr = "foot";
            string s = ParseTemplate.replaceTemplate(tmStr);
            return s;
        }

        protected string app()
        {
            string s = string.Empty;
            StringBuilder sb = new StringBuilder();
            List<JuSNS.Model.ApiInterface.AppInfo> infolist = JuSNS.ApiInterface.UtilUser.GetUserApp(UserID);
            foreach (JuSNS.Model.ApiInterface.AppInfo info in infolist)
            {
                string url = HttpUtility.UrlEncode(info.Url + "?u=" + JuSNS.ApiInterface.SecretUser.EncryptUserID(UserID.ToString()));
                sb.Append("<li><span><img src=\"" + JuSNS.Common.Public.GetAppIcon(info.Icon) + "\" style=\"width:16px;height:16px;\" align=\"absmiddle\" /> <a href=\"");
                sb.Append(Public.rootDir + "/app/frame?id=" + info.AppID);
                sb.Append("\"> ");
                sb.Append(info.AppName);
                sb.Append("</a></span></li>");
            }
            s = sb.ToString();
            return s;
        }
        /// <summary>
        /// 替换空格
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        protected string ReplaceSpace(object str)
        {
            if (str == null || str == DBNull.Value)
            {
                return "";
            }
            return Public.ReplaceSpace(str.ToString());
        }
        protected void DeleteAppIcon(object filename)
        {
            string fname = Convert.ToString(filename);
        }

        /// <summary>
        /// 是否是管理员
        /// </summary>
        /// <returns></returns>
        protected bool IsAdmin()
        {

            return false;
        }
        /// <summary>
        /// 检查隐私是否被允许查看
        /// </summary>
        /// <param name="ept">隐私类型</param>
        /// <param name="userid">用户ID</param>
        /// <param name="visitUserid">被访问的用户ID</param>
        /// <returns>true|false</returns>
        protected bool PrivacyAvailable(EnumPrivacyType ept, int userid, int visitUserid)
        {
            if (IsAdmin())
                return true;
            if (userid == visitUserid)
                return true;
            Home.User u = new Home.User();
            Home.Friend fr = new JuSNS.Home.Friend();
            PrivacyInfo pri = u.GetPrivacy(visitUserid);
            switch (ept)
            {
                case EnumPrivacyType.Educate:
                    switch (pri.PrivacyEducate)
                    {
                        case EnumPrivacy.ForFriends:
                            if (fr.IsFriends(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForNetWorkAndFriends:
                            if (fr.IsInSameNetwork(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForOwner:
                            if (userid == visitUserid)
                                return true;
                            break;
                        case EnumPrivacy.ForWholeSite:
                            return true;
                    }
                    break;
                case EnumPrivacyType.Favourite:
                    switch (pri.PrivacyFavourite)
                    {
                        case EnumPrivacy.ForFriends:
                            if (fr.IsFriends(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForNetWorkAndFriends:
                            if (fr.IsInSameNetwork(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForOwner:
                            if (userid == visitUserid)
                                return true;
                            break;
                        case EnumPrivacy.ForWholeSite:
                            return true;
                    }
                    break;
                case EnumPrivacyType.Friends:
                    switch (pri.PrivacyFriends)
                    {
                        case EnumPrivacy.ForFriends:
                            if (fr.IsFriends(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForNetWorkAndFriends:
                            if (fr.IsInSameNetwork(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForOwner:
                            if (userid == visitUserid)
                                return true;
                            break;
                        case EnumPrivacy.ForWholeSite:
                            return true;
                    }
                    break;
                case EnumPrivacyType.Group:
                    switch (pri.PrivacyGroup)
                    {
                        case EnumPrivacy.ForFriends:
                            if (fr.IsFriends(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForNetWorkAndFriends:
                            if (fr.IsInSameNetwork(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForOwner:
                            if (userid == visitUserid)
                                return true;
                            break;
                        case EnumPrivacy.ForWholeSite:
                            return true;
                    }
                    break;
                case EnumPrivacyType.Lasso:
                    switch (pri.PrivacyLasso)
                    {
                        case EnumPrivacy.ForFriends:
                            if (fr.IsFriends(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForNetWorkAndFriends:
                            if (fr.IsInSameNetwork(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForOwner:
                            if (userid == visitUserid)
                                return true;
                            break;
                        case EnumPrivacy.ForWholeSite:
                            return true;
                    }
                    break;
                case EnumPrivacyType.LeaveWord:
                    switch (pri.PrivacyLeaveWord)
                    {
                        case EnumPrivacy.ForFriends:
                            if (fr.IsFriends(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForNetWorkAndFriends:
                            if (fr.IsInSameNetwork(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForOwner:
                            if (userid == visitUserid)
                                return true;
                            break;
                        case EnumPrivacy.ForWholeSite:
                            return true;
                    }
                    break;
                case EnumPrivacyType.MiniBlog:
                    switch (pri.PrivacyMiniBlog)
                    {
                        case EnumPrivacy.ForFriends:
                            if (fr.IsFriends(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForNetWorkAndFriends:
                            if (fr.IsInSameNetwork(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForOwner:
                            if (userid == visitUserid)
                                return true;
                            break;
                        case EnumPrivacy.ForWholeSite:
                            return true;
                    }
                    break;
                case EnumPrivacyType.Movies:
                    switch (pri.PrivacyMovies)
                    {
                        case EnumPrivacy.ForFriends:
                            if (fr.IsFriends(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForNetWorkAndFriends:
                            if (fr.IsInSameNetwork(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForOwner:
                            if (userid == visitUserid)
                                return true;
                            break;
                        case EnumPrivacy.ForWholeSite:
                            return true;
                    }
                    break;
                case EnumPrivacyType.Share:
                    switch (pri.PrivacyShare)
                    {
                        case EnumPrivacy.ForFriends:
                            if (fr.IsFriends(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForNetWorkAndFriends:
                            if (fr.IsInSameNetwork(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForOwner:
                            if (userid == visitUserid)
                                return true;
                            break;
                        case EnumPrivacy.ForWholeSite:
                            return true;
                    }
                    break;
                case EnumPrivacyType.Space:
                    switch (pri.PrivacySpace)
                    {
                        case EnumPrivacy.ForFriends:
                            if (fr.IsFriends(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForNetWorkAndFriends:
                            if (fr.IsInSameNetwork(userid, visitUserid))
                                return true;
                            break;
                        case EnumPrivacy.ForOwner:
                            if (userid == visitUserid)
                                return true;
                            break;
                        case EnumPrivacy.ForWholeSite:
                            return true;
                    }
                    break;
            }
            return false;
        }
        /// <summary>
        /// 隐私是否允许
        /// </summary>
        /// <param name="ept">隐私</param>
        /// <param name="userid">用户ID</param>
        /// <param name="visitUserid">被访者用户ID</param>
        /// <returns></returns>
        protected bool PrivacyAvailable(EnumPrivacy ep, int userid, int visitUserid)
        {
            if (IsAdmin())
                return true;
            if (userid == visitUserid)
                return true;
            Home.User u = new Home.User();
            Home.Friend fr = new JuSNS.Home.Friend();
            switch (ep)
            {
                case EnumPrivacy.ForFriends:
                    if (fr.IsFriends(userid, visitUserid))
                        return true;
                    break;
                case EnumPrivacy.ForNetWorkAndFriends:
                    if (fr.IsInSameNetwork(userid, visitUserid))
                        return true;
                    break;
                case EnumPrivacy.ForOwner:
                    if (userid == visitUserid)
                        return true;
                    break;
                case EnumPrivacy.ForWholeSite:
                    return true;
            }
            return false;
        }
        /// <summary>
        /// 隐私是否允许
        /// </summary>
        /// <param name="ep">隐私</param>
        /// <param name="visitUserid">被访者用户</param>
        /// <returns></returns>
        protected bool PrivacyAvailable(EnumPrivacy ep, int visitUserid)
        {
            if (IsAdmin())
                return true;
            return PrivacyAvailable(ep, this.UserID, visitUserid);
        }
    }
}
