1 package net.pterodactylus.sone.web.pages
3 import freenet.clients.http.*
4 import net.pterodactylus.sone.data.*
5 import net.pterodactylus.sone.freenet.*
6 import net.pterodactylus.sone.main.*
7 import net.pterodactylus.sone.utils.*
8 import net.pterodactylus.sone.web.*
9 import net.pterodactylus.sone.web.page.*
10 import net.pterodactylus.util.notify.*
11 import net.pterodactylus.util.template.*
12 import net.pterodactylus.util.web.*
16 * Base page for the Sone web interface.
18 open class SoneTemplatePage(
19 private val webInterface: WebInterface,
21 templateRenderer: TemplateRenderer,
22 private val pageTitleKey: String? = null,
23 private val requiresLogin: Boolean = false,
24 private val pageTitle: (FreenetRequest) -> String = { pageTitleKey?.let(webInterface.translation::translate) ?: "" }
25 ) : FreenetTemplatePage(templateRenderer, loaders, "noPermission.html") {
27 private val core = webInterface.core
28 private val sessionProvider: SessionProvider = webInterface
29 protected val translation: Translation = webInterface.translation
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 fun getPageTitle(request: FreenetRequest) = getPageTitle(request.toSoneRequest(core, webInterface))
41 open fun getPageTitle(soneRequest: SoneRequest) = pageTitle(soneRequest)
43 override val styleSheets = listOf("css/sone.css")
45 override val shortcutIcon = "images/icon.png"
47 override fun getAdditionalLinkNodes(request: FreenetRequest) =
50 "type" to "application/opensearchdescription+xml",
52 "href" to "http://${request.httpRequest.getHeader("host")}/Sone/OpenSearch.xml"
55 final override fun processTemplate(request: FreenetRequest, templateContext: TemplateContext) {
56 super.processTemplate(request, templateContext)
57 templateContext["preferences"] = core.preferences
58 templateContext["currentSone"] = getCurrentSone(request.toadletContext)
59 templateContext["localSones"] = core.localSones
60 templateContext["request"] = request
61 templateContext["currentVersion"] = SonePlugin.getPluginVersion()
62 templateContext["hasLatestVersion"] = core.updateChecker.hasLatestVersion()
63 templateContext["latestEdition"] = core.updateChecker.latestEdition
64 templateContext["latestVersion"] = core.updateChecker.latestVersion
65 templateContext["latestVersionTime"] = core.updateChecker.latestVersionDate
66 webInterface.getNotifications(getCurrentSone(request.toadletContext)).sortedBy(Notification::getCreatedTime).run {
67 templateContext["notifications"] = this
68 templateContext["notificationHash"] = this.hashCode()
70 handleRequest(request, templateContext)
73 open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
74 handleRequest(freenetRequest.toSoneRequest(core, webInterface), templateContext)
77 open fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
80 override fun getRedirectTarget(request: FreenetRequest): String? {
81 if (requiresLogin && getCurrentSone(request.toadletContext) == null) {
82 val parameters = request.httpRequest.parameterNames
83 .flatMap { name -> request.httpRequest.getMultipleParam(name).map { name to it } }
84 .joinToString("&") { "${it.first.urlEncode}=${it.second.urlEncode}" }
86 return "login.html?target=${request.httpRequest.path}${parameters?.let { ("?" + it).urlEncode } ?: ""}"
91 private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8")
93 override fun isEnabled(toadletContext: ToadletContext) =
94 isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.sessionManager, core, webInterface))
96 open fun isEnabled(soneRequest: SoneRequest) = when {
97 requiresLogin && getCurrentSone(soneRequest.toadletContext) == null -> false
98 core.preferences.requireFullAccess && !soneRequest.toadletContext.isAllowedFullAccess -> false