Esempio n. 1
0
func ProcessRanksUpdate(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while unescaping guild name", 500)
		return
	}
	captcha_response, err := captcha.Verify(req.FormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error while checking captcha code", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	if !models.CheckGuildName(name) {
		http.Error(res, "Unknown guild", 500)
		return
	}
	characters, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting account characters", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	level3 := req.PostFormValue("level3")
	level2 := req.PostFormValue("level2")
	level1 := req.PostFormValue("level1")
	if len(level1) > 15 || len(level2) > 15 || len(level3) > 15 {
		base.Session.SetFlash("Ranks cant have more than 15 characters", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	rgxp, err := regexp.Compile(`^[A-Z a-z-0-9]+$`)
	if err != nil {
		http.Error(res, "Error compiling RegExp at guild ranks", 500)
		return
	}
	if !rgxp.MatchString(level3) || !rgxp.MatchString(level2) || !rgxp.MatchString(level1) {
		base.Session.SetFlash("Ranks cant contain invalid characters only A-z and 0-9 are allowed", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	err = models.UpdateGuildRanks(guild_info.Id, level3, level2, level1)
	if err != nil {
		http.Error(res, "Error while updating guild ranks", 500)
		return
	}
	base.Session.SetFlash("Guild ranks updated", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Esempio n. 2
0
func ProcessMotdUpdate(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while unescaping guild name", 500)
		return
	}
	captcha_response, err := captcha.Verify(req.FormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error while checking captcha code", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	if !models.CheckGuildName(name) {
		http.Error(res, "Unknown guild", 500)
		return
	}
	characters, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting account characters", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	motd := req.PostFormValue("motd")
	if len(motd) > 255 {
		base.Session.SetFlash("The MOTD cant be longer than 255 characters", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	rgxp, err := regexp.Compile(`^[A-Z a-z 0-9]+$`)
	if err != nil {
		http.Error(res, "Error compiling regexp", 500)
		return
	}
	if !rgxp.MatchString(motd) {
		base.Session.SetFlash("The MOTD contains invalid characters you can only use A-Z or 0-9", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	err = models.UpdateGuildMotd(guild_info.Id, motd)
	if err != nil {
		http.Error(res, "Error while updating guild MOTD", 500)
		return
	}
	base.Session.SetFlash("Guild MOTD updated", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}
Esempio n. 3
0
func ProcessCharacterCreate(res http.ResponseWriter, req *http.Request, base *BaseController) {
	if models.GetAccountCharactersCount(base.Account.Id) >= config.Parser.Character.Max {
		base.Session.SetFlash("You cant have more than "+strconv.Itoa(config.Parser.Character.Max)+" characters!", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	captcha_response, err := captcha.Verify(req.PostFormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error checking captcha response", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	name := req.PostFormValue("name")
	sex := SexToInt(req.PostFormValue("sex"))
	vocation := VocationToInt(req.PostFormValue("vocation"))
	town_id := models.CheckTownExists(req.PostFormValue("town"))
	if town_id == -1 {
		base.Session.SetFlash("Unkown town name", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	if len(name) <= 5 {
		base.Session.SetFlash("This name is too short. Please choose another name!", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	rgxp, err := regexp.Compile(`^[A-Z a-z]+$`)
	if err != nil {
		http.Error(res, "Error compiling RegExp at register", 500)
		return
	}
	if !rgxp.MatchString(name) {
		base.Session.SetFlash("This name cannot be used because it contains a forbidden word or combination of letters. Please choose another name!", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	if models.CheckCharacterName(name) {
		base.Session.SetFlash("This name is already used. Please choose another name", "error")
		http.Redirect(res, req, "/account/manage/create", 301)
		return
	}
	err = models.CreateCharacter(name, sex, vocation, base.Account.Id, town_id)
	if err != nil {
		http.Error(res, "Error while creating your character: "+err.Error(), 500)
		return
	}
	views.Parser.ExecuteTemplate(res, "create_character_success.html", &CreateCharacterSuccessResponse{"account-manage", name, config.Parser.Spawn.Name})
}
Esempio n. 4
0
func ProcessGuildInvite(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while unescaping guild name", 500)
		return
	}
	captcha_response, err := captcha.Verify(req.FormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error while checking captcha code", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	if !models.CheckGuildName(name) {
		http.Error(res, "Unknown guild", 500)
		return
	}
	characters, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting account characters", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	player_name := req.PostFormValue("player")
	if !models.CheckCharacterName(player_name) {
		base.Session.SetFlash("Player not found", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	player_info := models.GetCharacterByName(player_name)
	if models.CheckCharacterInGuild(player_info.Id) {
		base.Session.SetFlash("Player is already in a guild", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	err = models.CreateGuildInvite(guild_info.Id, player_info.Id)
	if err != nil {
		log.Println(err)
		http.Error(res, "Error while inviting player", 301)
		return
	}
	base.Session.SetFlash("Player invited to the guild", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
	return
}
Esempio n. 5
0
func ProcessAccountLogin(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name := req.PostFormValue("loginname")
	password := req.PostFormValue("loginpassword")
	captcha_response, err := captcha.Verify(req.PostFormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error while checking captcha code", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, "/account/login", 301)
		return
	}
	if !models.CheckAccountName(name) {
		base.Session.SetFlash("Account name or password is not correct!", "error")
		http.Redirect(res, req, "/account/login", 301)
		return
	}
	account_id, success := models.CheckLogin(name, fmt.Sprintf("%x", sha1.Sum([]byte(password))))
	if !success {
		base.Session.SetFlash("Account name or password is not correct!", "error")
		http.Redirect(res, req, "/account/login", 301)
		return
	}
	token, err := GenerateLoginToken(15)
	if err != nil {
		http.Error(res, "Error while creating login token", 500)
		return
	}
	base.Session.SetValue("token", token)
	base.Session.SetValue("logged", "true")
	err = models.SetAccountTokenByName(account_id, token)
	if err != nil {
		http.Error(res, "Error while changing your account token", 500)
		return
	}
	intended_route, allow := base.Session.GetValue("intended").(string)
	if allow && intended_route != "" {
		base.Session.SetValue("intended", "")
		http.Redirect(res, req, intended_route, 301)
		return
	}
	http.Redirect(res, req, "/account/manage", 301)
}
Esempio n. 6
0
func ProcessPlayerEdit(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error processing name url query", 500)
		return
	}
	captcha_response, err := captcha.Verify(req.PostFormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error checking captcha response", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, "/account/manage/edit/"+name, 301)
		return
	}
	if !models.CheckCharacterNameOfAccount(name, base.Account.Id) || !models.CheckCharacterAlive(name) {
		http.Redirect(res, req, "/account/manage", 301)
		return
	}
	comment := req.PostFormValue("comment")
	if len(comment) > 100 {
		base.Session.SetFlash("Comment is bigger than 100 characters", "error")
		http.Redirect(res, req, "/account/manage/edit/"+name, 301)
		return
	}
	comment_lines := strings.Split(comment, "\n")
	if len(comment_lines) > 10 {
		base.Session.SetFlash("You cant have more than 10 comment lines", "error")
		http.Redirect(res, req, "/account/manage/edit/"+name, 301)
		return
	}
	signature := req.PostFormValue("signature")
	hide := req.PostFormValue("accountvisible")
	err = models.EditCharacter(name, comment, signature, hide)
	if err != nil {
		http.Error(res, "Error editing character", 500)
		return
	}
	views.Parser.ExecuteTemplate(res, "edit_character_success.html", &EditCharacterSuccessResponse{"account-manage"})
}
Esempio n. 7
0
func ProcessLogoUpload(res http.ResponseWriter, req *http.Request, base *BaseController) {
	name, err := url.QueryUnescape(mux.Vars(req)["name"])
	if err != nil {
		http.Error(res, "Error while unescaping guild name", 500)
		return
	}
	captcha_response, err := captcha.Verify(req.FormValue("g-recaptcha-response"))
	if err != nil {
		http.Error(res, "Error while checking captcha code", 500)
		return
	}
	if !captcha_response {
		base.Session.SetFlash("Wrong captcha response", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", mux.Vars(req)["name"]), 301)
		return
	}
	if !models.CheckGuildName(name) {
		http.Error(res, "Unknown guild", 500)
		return
	}
	characters, err := models.GetAccountCharacters(base.Account.Id)
	if err != nil {
		http.Error(res, "Error while getting account characters", 500)
		return
	}
	guild_info := models.GetGuildIdByName(name)
	if !checkAccountOwner(characters, guild_info) {
		http.Error(res, "You arent the owner of this guild", 500)
		return
	}
	file_logo, _, err := req.FormFile("logo")
	if err != nil {
		http.Error(res, "Error while reading logo image", 500)
		return
	}
	defer file_logo.Close()
	logo_image, format, err := image.Decode(file_logo)
	if !checkValidImageFormat(format) {
		base.Session.SetFlash("Guild logo image format is not allowed", "error")
		http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
		return
	}
	if err != nil {
		http.Error(res, "Error while decoding logo image", 500)
		return
	}
	logo_out, err := os.Create(config.Parser.Style.Template + "/public/guilds/" + guild_info.Name + ".gif")
	if err != nil {
		http.Error(res, "Error while creating logo stdout", 500)
		return
	}
	defer logo_out.Close()
	resized_logo := resize.Resize(80, 80, logo_image, resize.Lanczos3)
	err = png.Encode(logo_out, resized_logo)
	if err != nil {
		http.Error(res, "Error while encoding logo image", 500)
		return
	}
	base.Session.SetFlash("Guild logo updated", "success")
	http.Redirect(res, req, fmt.Sprintf("/guilds/view/%v", url.QueryEscape(name)), 301)
}