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(
21 protected val webInterface: WebInterface,
23 private val pageTitleKey: String? = null,
24 private val requiresLogin: Boolean = true
25 ) : FreenetTemplatePage(path, webInterface.templateContextFactory, template, "noPermission.html") {
28 constructor(path: String, template: Template, pageTitleKey: String?, webInterface: WebInterface, requireLogin: Boolean = false) :
29 this(path, webInterface, template, pageTitleKey, requireLogin)
31 constructor(path: String, template: Template, webInterface: WebInterface, requireLogin: Boolean = true) :
32 this(path, webInterface, template, null, requireLogin)
34 private val core = webInterface.core
35 protected val sessionProvider: SessionProvider = webInterface
37 protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) =
38 sessionProvider.getCurrentSone(toadletContext, createSession)
40 protected fun setCurrentSone(toadletContext: ToadletContext, sone: Sone?) =
41 sessionProvider.setCurrentSone(toadletContext, sone)
43 fun requiresLogin() = requiresLogin
45 override public fun getPageTitle(freenetRequest: FreenetRequest) =
46 pageTitleKey?.let(webInterface.l10n::getString) ?: ""
48 override public fun getStyleSheets() =
49 listOf("css/sone.css")
51 override public fun getShortcutIcon() = "images/icon.png"
53 override public fun getAdditionalLinkNodes(request: FreenetRequest) =
56 "type" to "application/opensearchdescription+xml",
58 "href" to "http://${request.httpRequest.getHeader("host")}/Sone/OpenSearch.xml"
61 final override public fun processTemplate(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
62 super.processTemplate(freenetRequest, templateContext)
63 templateContext["preferences"] = core.preferences
64 templateContext["currentSone"] = getCurrentSone(freenetRequest.toadletContext)
65 templateContext["localSones"] = core.localSones
66 templateContext["request"] = freenetRequest
67 templateContext["currentVersion"] = SonePlugin.getPluginVersion()
68 templateContext["hasLatestVersion"] = core.updateChecker.hasLatestVersion()
69 templateContext["latestEdition"] = core.updateChecker.latestEdition
70 templateContext["latestVersion"] = core.updateChecker.latestVersion
71 templateContext["latestVersionTime"] = core.updateChecker.latestVersionDate
72 webInterface.getNotifications(getCurrentSone(freenetRequest.toadletContext)).sortedBy(Notification::getCreatedTime).run {
73 templateContext["notifications"] = this
74 templateContext["notificationHash"] = this.hashCode()
76 handleRequest(freenetRequest, templateContext)
79 internal open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
82 override public fun getRedirectTarget(freenetRequest: FreenetRequest): String? {
83 if (requiresLogin && getCurrentSone(freenetRequest.toadletContext) == null) {
84 val parameters = freenetRequest.httpRequest.parameterNames
85 .flatMap { name -> freenetRequest.httpRequest.getMultipleParam(name).map { name to it } }
86 .joinToString("&") { "${it.first.urlEncode}=${it.second.urlEncode}" }
88 return "login.html?target=${freenetRequest.httpRequest.path}${parameters?.let { ("?" + it).urlEncode } ?: ""}"
93 private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8")
95 override fun isEnabled(toadletContext: ToadletContext) = when {
96 requiresLogin && getCurrentSone(toadletContext) == null -> false
97 core.preferences.isRequireFullAccess && !toadletContext.isAllowedFullAccess -> false