X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FSoneTemplatePage.kt;fp=src%2Fmain%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fpages%2FSoneTemplatePage.kt;h=4d0fa3f833c3173abec9c562bf948d054d5d60de;hp=0000000000000000000000000000000000000000;hb=8572a749a619643392c776208027b3d0209e8f4c;hpb=93a86ab418b9fda1a7c2cde90e0503bd3ca7b1ea diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt new file mode 100644 index 0000000..4d0fa3f --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt @@ -0,0 +1,101 @@ +package net.pterodactylus.sone.web.pages + +import freenet.clients.http.ToadletContext +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.main.SonePlugin +import net.pterodactylus.sone.utils.emptyToNull +import net.pterodactylus.sone.web.SessionProvider +import net.pterodactylus.sone.web.WebInterface +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.sone.web.page.FreenetTemplatePage +import net.pterodactylus.util.notify.Notification +import net.pterodactylus.util.template.Template +import net.pterodactylus.util.template.TemplateContext +import java.net.URLEncoder + +/** + * Base page for the Sone web interface. + */ +open class SoneTemplatePage( + path: String, + protected val webInterface: WebInterface, + template: Template, + private val pageTitleKey: String? = null, + private val requiresLogin: Boolean = true +) : FreenetTemplatePage(path, webInterface.templateContextFactory, template, "noPermission.html") { + + @JvmOverloads + constructor(path: String, template: Template, pageTitleKey: String?, webInterface: WebInterface, requireLogin: Boolean = false) : + this(path, webInterface, template, pageTitleKey, requireLogin) + + constructor(path: String, template: Template, webInterface: WebInterface, requireLogin: Boolean = true) : + this(path, webInterface, template, null, requireLogin) + + private val core = webInterface.core + protected val sessionProvider: SessionProvider = webInterface + + protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) = + sessionProvider.getCurrentSone(toadletContext, createSession) + + protected fun setCurrentSone(toadletContext: ToadletContext, sone: Sone?) = + sessionProvider.setCurrentSone(toadletContext, sone) + + fun requiresLogin() = requiresLogin + + override public fun getPageTitle(request: FreenetRequest) = + pageTitleKey?.let(webInterface.l10n::getString) ?: "" + + override public fun getStyleSheets() = + listOf("css/sone.css") + + override public fun getShortcutIcon() = "images/icon.png" + + override public fun getAdditionalLinkNodes(request: FreenetRequest) = + listOf(mapOf( + "rel" to "search", + "type" to "application/opensearchdescription+xml", + "title" to "Sone", + "href" to "http://${request.httpRequest.getHeader("host")}/Sone/OpenSearch.xml" + )) + + override public fun processTemplate(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + super.processTemplate(freenetRequest, templateContext) + templateContext["preferences"] = core.preferences + templateContext["currentSone"] = getCurrentSone(freenetRequest.toadletContext) + templateContext["localSones"] = core.localSones + templateContext["request"] = freenetRequest + templateContext["currentVersion"] = SonePlugin.getPluginVersion() + templateContext["hasLatestVersion"] = core.updateChecker.hasLatestVersion() + templateContext["latestEdition"] = core.updateChecker.latestEdition + templateContext["latestVersion"] = core.updateChecker.latestVersion + templateContext["latestVersionTime"] = core.updateChecker.latestVersionDate + webInterface.getNotifications(getCurrentSone(freenetRequest.toadletContext)).sortedBy(Notification::getCreatedTime).run { + templateContext["notifications"] = this + templateContext["notificationHash"] = this.hashCode() + } + handleRequest(freenetRequest, templateContext) + } + + internal open fun handleRequest(request: FreenetRequest, templateContext: TemplateContext) { + } + + override public fun getRedirectTarget(request: FreenetRequest): String? { + if (requiresLogin && getCurrentSone(request.toadletContext) == null) { + val parameters = request.httpRequest.parameterNames + .flatMap { name -> request.httpRequest.getMultipleParam(name).map { name to it } } + .joinToString("&") { "${it.first.urlEncode}=${it.second.urlEncode}" } + .emptyToNull + return "login.html?target=${request.httpRequest.path}${parameters?.let { ("?" + it).urlEncode } ?: ""}" + } + return null + } + + private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8") + + override fun isEnabled(toadletContext: ToadletContext) = when { + requiresLogin && getCurrentSone(toadletContext) == null -> false + core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false + else -> true + } + +}