1 package net.pterodactylus.sone.web.pages
3 import freenet.clients.http.ToadletContext
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.main.SonePlugin
6 import net.pterodactylus.sone.utils.emptyToNull
7 import net.pterodactylus.sone.web.SessionProvider
8 import net.pterodactylus.sone.web.WebInterface
9 import net.pterodactylus.sone.web.page.FreenetRequest
10 import net.pterodactylus.sone.web.page.FreenetTemplatePage
11 import net.pterodactylus.util.notify.Notification
12 import net.pterodactylus.util.template.Template
13 import net.pterodactylus.util.template.TemplateContext
14 import java.net.URLEncoder
17 * Base page for the Sone web interface.
19 open class SoneTemplatePage @JvmOverloads constructor(
21 protected val webInterface: WebInterface,
23 private val pageTitleKey: String? = null,
24 private val requiresLogin: Boolean = false,
25 private val pageTitle: (FreenetRequest) -> String = { pageTitleKey?.let(webInterface.l10n::getString) ?: "" }
26 ) : FreenetTemplatePage(path, webInterface.templateContextFactory, template, "noPermission.html") {
28 private val core = webInterface.core
29 protected val sessionProvider: SessionProvider = webInterface
31 protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) =
32 sessionProvider.getCurrentSone(toadletContext, createSession)
34 protected fun setCurrentSone(toadletContext: ToadletContext, sone: Sone?) =
35 sessionProvider.setCurrentSone(toadletContext, sone)
37 fun requiresLogin() = requiresLogin
39 override public fun getPageTitle(freenetRequest: FreenetRequest) = pageTitle(freenetRequest)
41 override public fun getStyleSheets() =
42 listOf("css/sone.css")
44 override public fun getShortcutIcon() = "images/icon.png"
46 override public fun getAdditionalLinkNodes(request: FreenetRequest) =
49 "type" to "application/opensearchdescription+xml",
51 "href" to "http://${request.httpRequest.getHeader("host")}/Sone/OpenSearch.xml"
54 final override public fun processTemplate(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
55 super.processTemplate(freenetRequest, templateContext)
56 templateContext["preferences"] = core.preferences
57 templateContext["currentSone"] = getCurrentSone(freenetRequest.toadletContext)
58 templateContext["localSones"] = core.localSones
59 templateContext["request"] = freenetRequest
60 templateContext["currentVersion"] = SonePlugin.getPluginVersion()
61 templateContext["hasLatestVersion"] = core.updateChecker.hasLatestVersion()
62 templateContext["latestEdition"] = core.updateChecker.latestEdition
63 templateContext["latestVersion"] = core.updateChecker.latestVersion
64 templateContext["latestVersionTime"] = core.updateChecker.latestVersionDate
65 webInterface.getNotifications(getCurrentSone(freenetRequest.toadletContext)).sortedBy(Notification::getCreatedTime).run {
66 templateContext["notifications"] = this
67 templateContext["notificationHash"] = this.hashCode()
69 handleRequest(freenetRequest, templateContext)
72 internal open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
75 override public fun getRedirectTarget(freenetRequest: FreenetRequest): String? {
76 if (requiresLogin && getCurrentSone(freenetRequest.toadletContext) == null) {
77 val parameters = freenetRequest.httpRequest.parameterNames
78 .flatMap { name -> freenetRequest.httpRequest.getMultipleParam(name).map { name to it } }
79 .joinToString("&") { "${it.first.urlEncode}=${it.second.urlEncode}" }
81 return "login.html?target=${freenetRequest.httpRequest.path}${parameters?.let { ("?" + it).urlEncode } ?: ""}"
86 private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8")
88 override fun isEnabled(toadletContext: ToadletContext) = when {
89 requiresLogin && getCurrentSone(toadletContext) == null -> false
90 core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false