protected void ChangeAvatars_Click(object sender, EventArgs e)
 {
     DataTable avatars = DbHelper.ExecuteDataset(string.Format("SELECT {0}users.uid,avatar FROM {0}users JOIN {0}userfields ON {0}users.uid={0}userfields.uid WHERE avatar LIKE '/%'",
         BaseConfigs.GetTablePrefix)).Tables[0];
     int count = 0;
     foreach (DataRow dr in avatars.Rows)
     {
         string sourceAvatarPath = Utils.GetMapPath(dr["avatar"].ToString());
         string uid = Avatars.FormatUid(dr["uid"].ToString());
         if (File.Exists(sourceAvatarPath))
         {
             string destDir = string.Format("{0}avatars/upload/{1}/{2}/{3}", BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2));
             if(!Directory.Exists(Utils.GetMapPath(destDir)))
                 Directory.CreateDirectory(Utils.GetMapPath(destDir));
             string destAvatarPath = Utils.GetMapPath(string.Format("{0}avatars/upload/{1}/{2}/{3}/{4}_avatar_{5}.jpg",
                 BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2), uid.Substring(7, 2), "large"));
             File.Copy(sourceAvatarPath, destAvatarPath, true);
             destAvatarPath = Utils.GetMapPath(string.Format("{0}avatars/upload/{1}/{2}/{3}/{4}_avatar_{5}.jpg",
                 BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2), uid.Substring(7, 2), "medium"));
             File.Copy(sourceAvatarPath, destAvatarPath, true);
             destAvatarPath = Utils.GetMapPath(string.Format("{0}avatars/upload/{1}/{2}/{3}/{4}_avatar_{5}.jpg",
                 BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2), uid.Substring(7, 2), "small"));
             string destPath = string.Format("{0}avatars/upload/{1}/{2}/{3}/{4}_avatar_{5}.jpg",
                 BaseConfigs.GetForumPath, uid.Substring(0, 3), uid.Substring(3, 2), uid.Substring(5, 2), uid.Substring(7, 2), "small");
             File.Copy(sourceAvatarPath, destAvatarPath, true);
             Thumbnail thumb = new Thumbnail();
             thumb.SetImage(destPath);
             thumb.SaveThumbnailImage(48, 48);
             count++;
         }
     }
     info.Text = "<span style='color:blue'>提示:</span>头像转换程序已经成功转换了 <span style='color:red'>" + count + "</span> 个头像.";
     change.Text = "进入首页";
     change.Attributes.Add("onclick", "window.location.href='../index.aspx';return false;");
 }
Beispiel #2
0
        protected override void ShowPage()
        {
            pagetitle = "用户控制面板";
            if (userid == -1)
            {
                AddErrLine("你尚未登录");
                return;
            }
            user = Discuz.Forum.Users.GetUserInfo(userid);            

            avatarwidth = 100;
            avatarheight = 100;

            if (DNTRequest.IsPost())
            {
                int avatartype = DNTRequest.GetInt("avatartype", -1);
                if (avatartype != -1)
                {
                    switch (avatartype)
                    {
                        case 0: //从系统选择
                            avatar = DNTRequest.GetString("usingavatar");
                            avatar = Utils.UrlDecode(avatar.Substring(avatar.IndexOf("avatar")));
                            avatarwidth = 0;
                            avatarheight = 0;
                            if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + avatar))
                            {
                                AddErrLine("不存在的头像文件");
                                return;
                            }

                            break;

                        case 1: //上传头像

                            if (usergroupinfo.Allowavatar < 3)
                            {
                                AddErrLine("您当前的身份 \"" + usergroupinfo.Grouptitle + "\" 没有上传头像的权限");
                                return;
                            }
                            avatar = ForumUtils.SaveRequestAvatarFile(userid, config.Maxavatarsize);
                            if (avatar.Equals(""))
                            {
                                AddErrLine(
                                    string.Format("头像图片不合法, 系统要求必须为gif jpg png图片, 且宽高不得超过 {0}x{1}, 大小不得超过 {2} 字节",
                                                  config.Maxavatarwidth, config.Maxavatarheight, config.Maxavatarsize));
                                return;
                            }

                            Thumbnail thumb = new Thumbnail();
                            if (!thumb.SetImage(avatar))
                            {
                                AddErrLine("非法的图片格式");
                                return;
                            }
                            thumb.SaveThumbnailImage(config.Maxavatarwidth, config.Maxavatarheight);
                            avatarwidth = 0;
                            avatarheight = 0;
                            break;

                        case 2: //自定义头像Url

                            if (usergroupinfo.Allowavatar < 2)
                            {
                                AddErrLine(string.Format("您当前的身份 \"{0}\" 没有使用自定义头像的权限", usergroupinfo.Grouptitle));
                                return;
                            }
                            avatar = DNTRequest.GetString("avatarurl").Trim();
                            if (avatar.Length < 10)
                            {
                                AddErrLine("头像路径不合法");

                                return;
                            }
                            if (!avatar.Substring(0, 7).ToLower().Equals("http://"))
                            {
                                AddErrLine("头像路径必须以http://开始");
                                return;
                            }
                            string fileextname = Path.GetExtension(avatar).ToLower();
                            // 判断 文件扩展名/文件大小/文件类型 是否符合要求
                            if (
                                !(fileextname.Equals(".jpg") || fileextname.Equals(".gif") || fileextname.Equals(".png")))
                            {
                                AddErrLine("头像路径必须是.jpg .gif或.png结尾");
                                return;
                            }

                            avatarwidth = DNTRequest.GetInt("avatarwidth", 120);
                            avatarheight = DNTRequest.GetInt("avatarheight", 120);
                            if (avatarwidth <= 0 || avatarwidth > 120|| avatarheight <= 0 ||avatarheight > 120)
                            {
                                AddErrLine("自定义URL地址头像尺寸必须大于0, 且必须小于系统当前设置的最大尺寸 120x120");

                                return;
                            }
                            break;
                    }                    
                }
                else
                { 
                    //当允许使用头像时
                    if (usergroupinfo.Allowavatar > 0)
                    {
                        AddErrLine("请指定新头像的信息<br />");
                        return;
                    }
                }

                //当不允许使用头像时
                if (usergroupinfo.Allowavatar == 0)
                {
                    avatar = user.Avatar;
                    avatarwidth = user.Avatarwidth;
                    avatarheight = user.Avatarheight;
                }
                Discuz.Forum.Users.UpdateUserPreference(userid, avatar, avatarwidth, avatarheight,
                                               DNTRequest.GetInt("templateid", 0));
                SetUrl("usercppreference.aspx");
                SetMetaRefresh();
                SetShowBackLink(true);
                AddMsgLine("修改个性设置完毕");
            }
            else
            {
                templatelist = Templates.GetValidTemplateList();
                avatarfilelist = Caches.GetAvatarList();

                UserInfo __userinfo = Discuz.Forum.Users.GetUserInfo(userid);
                avatar = __userinfo.Avatar;
                avatarurl = "";
                avatartype = 1;
                avatarwidth = 0;
                avatarheight = 0;
                if (Utils.CutString(avatar, 0, 15).ToLower().Equals(@"avatars\common\"))
                {
                    avatartype = 0;
                }
                else if (Utils.CutString(avatar, 0, 7).ToLower().Equals("http://"))
                {
                    avatarurl = avatar;
                    avatartype = 2;
                    avatarwidth = __userinfo.Avatarwidth;
                    avatarheight = __userinfo.Avatarheight;
                }
            }
        }