X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FEditProfilePage.java;h=cffdeb9144f52ae58a7b7d1f98a3ef676a57f258;hp=181c4542dab7d90f0fd2b8d7119dbff6a028bdd7;hb=9acbc5bdec4ccb752e0856a501568b0bb6161579;hpb=08ea47f96c1e0a5afa203db78ea2afeaaaea5d49 diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java index 181c454..cffdeb9 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -1,5 +1,5 @@ /* - * Sone - EditProfilePage.java - Copyright © 2010 David Roden + * Sone - EditProfilePage.java - Copyright © 2010–2016 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,10 +17,19 @@ package net.pterodactylus.sone.web; +import static net.pterodactylus.sone.text.TextFilter.filter; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + +import java.util.List; + import net.pterodactylus.sone.data.Profile; +import net.pterodactylus.sone.data.Profile.DuplicateField; +import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Sone; -import net.pterodactylus.sone.web.page.Page.Request.Method; +import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.template.Template; +import net.pterodactylus.util.template.TemplateContext; +import net.pterodactylus.util.web.Method; import freenet.clients.http.ToadletContext; /** @@ -39,7 +48,7 @@ public class EditProfilePage extends SoneTemplatePage { * The Sone web interface */ public EditProfilePage(Template template, WebInterface webInterface) { - super("editProfile.html", template, "Page.EditProfile.Title", webInterface); + super("editProfile.html", template, "Page.EditProfile.Title", webInterface, true); } // @@ -50,41 +59,113 @@ public class EditProfilePage extends SoneTemplatePage { * {@inheritDoc} */ @Override - protected void processTemplate(Request request, Template template) throws RedirectException { - super.processTemplate(request, template); + protected void handleRequest(FreenetRequest request, TemplateContext templateContext) throws RedirectException { ToadletContext toadletContenxt = request.getToadletContext(); Sone currentSone = getCurrentSone(toadletContenxt); Profile profile = currentSone.getProfile(); String firstName = profile.getFirstName(); String middleName = profile.getMiddleName(); String lastName = profile.getLastName(); + Integer birthDay = profile.getBirthDay(); + Integer birthMonth = profile.getBirthMonth(); + Integer birthYear = profile.getBirthYear(); + String avatarId = profile.getAvatar(); + List fields = profile.getFields(); if (request.getMethod() == Method.POST) { - firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim(); - middleName = request.getHttpRequest().getPartAsStringFailsafe("middle-name", 256).trim(); - lastName = request.getHttpRequest().getPartAsStringFailsafe("last-name", 256).trim(); - profile.setFirstName(firstName.length() > 0 ? firstName : null); - profile.setMiddleName(middleName.length() > 0 ? middleName : null); - profile.setLastName(lastName.length() > 0 ? lastName : null); - if (profile.isModified()) { + if (request.getHttpRequest().getPartAsStringFailsafe("save-profile", 4).equals("true")) { + firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim(); + middleName = request.getHttpRequest().getPartAsStringFailsafe("middle-name", 256).trim(); + lastName = request.getHttpRequest().getPartAsStringFailsafe("last-name", 256).trim(); + birthDay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim(), null); + birthMonth = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim(), null); + birthYear = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim(), null); + avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36); + profile.setFirstName(firstName.length() > 0 ? firstName : null); + profile.setMiddleName(middleName.length() > 0 ? middleName : null); + profile.setLastName(lastName.length() > 0 ? lastName : null); + profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear); + profile.setAvatar(webInterface.getCore().getImage(avatarId, false)); + for (Field field : fields) { + String value = request.getHttpRequest().getPartAsStringFailsafe("field-" + field.getId(), 400); + String filteredValue = filter(request.getHttpRequest().getHeader("Host"), value); + field.setValue(filteredValue); + } currentSone.setProfile(profile); + webInterface.getCore().touchConfiguration(); + throw new RedirectException("editProfile.html"); + } else if (request.getHttpRequest().getPartAsStringFailsafe("add-field", 4).equals("true")) { + String fieldName = request.getHttpRequest().getPartAsStringFailsafe("field-name", 256).trim(); + try { + profile.addField(fieldName); + currentSone.setProfile(profile); + webInterface.getCore().touchConfiguration(); + throw new RedirectException("editProfile.html#profile-fields"); + } catch (DuplicateField df1) { + templateContext.set("fieldName", fieldName); + templateContext.set("duplicateFieldName", true); + } + } else { + String id = getFieldId(request, "delete-field-"); + if (id != null) { + throw new RedirectException("deleteProfileField.html?field=" + id); + } + id = getFieldId(request, "move-up-field-"); + if (id != null) { + Field field = profile.getFieldById(id); + if (field == null) { + throw new RedirectException("invalid.html"); + } + profile.moveFieldUp(field); + currentSone.setProfile(profile); + throw new RedirectException("editProfile.html#profile-fields"); + } + id = getFieldId(request, "move-down-field-"); + if (id != null) { + Field field = profile.getFieldById(id); + if (field == null) { + throw new RedirectException("invalid.html"); + } + profile.moveFieldDown(field); + currentSone.setProfile(profile); + throw new RedirectException("editProfile.html#profile-fields"); + } + id = getFieldId(request, "edit-field-"); + if (id != null) { + throw new RedirectException("editProfileField.html?field=" + id); + } } - throw new RedirectException("index.html"); } - template.set("firstName", firstName); - template.set("middleName", middleName); - template.set("lastName", lastName); + templateContext.set("firstName", firstName); + templateContext.set("middleName", middleName); + templateContext.set("lastName", lastName); + templateContext.set("birthDay", birthDay); + templateContext.set("birthMonth", birthMonth); + templateContext.set("birthYear", birthYear); + templateContext.set("avatarId", avatarId); + templateContext.set("fields", fields); } // - // SONETEMPLATEPAGE METHODS + // PRIVATE METHODS // /** - * {@inheritDoc} + * Searches for a part whose names starts with the given {@code String} and + * extracts the ID from the located name. + * + * @param request + * The request to get the parts from + * @param partNameStart + * The start of the name of the requested part + * @return The parsed ID, or {@code null} if there was no part matching the + * given string */ - @Override - protected boolean requiresLogin() { - return true; + private static String getFieldId(FreenetRequest request, String partNameStart) { + for (String partName : request.getHttpRequest().getParts()) { + if (partName.startsWith(partNameStart)) { + return partName.substring(partNameStart.length()); + } + } + return null; } - }