3df343a6c30c0ac3bd624d2f9156443bf9ef1ec5
[Sone.git] / src / main / kotlin / net / pterodactylus / sone / web / pages / SoneTemplatePage.kt
1 package net.pterodactylus.sone.web.pages
2
3 import freenet.clients.http.*
4 import net.pterodactylus.sone.data.Sone
5 import net.pterodactylus.sone.main.*
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.*
10 import net.pterodactylus.util.notify.Notification
11 import net.pterodactylus.util.template.TemplateContext
12 import net.pterodactylus.util.web.*
13 import java.net.URLEncoder
14
15 /**
16  * Base page for the Sone web interface.
17  */
18 open class SoneTemplatePage @JvmOverloads constructor(
19                 path: String,
20                 private val webInterface: WebInterface,
21                 loaders: Loaders,
22                 templateRenderer: TemplateRenderer,
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, templateRenderer, loaders, "noPermission.html") {
27
28         private val core = webInterface.core
29         private val sessionProvider: SessionProvider = webInterface
30
31         protected fun getCurrentSone(toadletContext: ToadletContext, createSession: Boolean = true) =
32                         sessionProvider.getCurrentSone(toadletContext, createSession)
33
34         protected fun setCurrentSone(toadletContext: ToadletContext, sone: Sone?) =
35                         sessionProvider.setCurrentSone(toadletContext, sone)
36
37         fun requiresLogin() = requiresLogin
38
39         override public fun getPageTitle(freenetRequest: FreenetRequest) = getPageTitle(freenetRequest.toSoneRequest(core, webInterface))
40
41         open fun getPageTitle(soneRequest: SoneRequest) = pageTitle(soneRequest)
42
43         override val styleSheets = listOf("css/sone.css")
44
45         override val shortcutIcon = "images/icon.png"
46
47         override public fun getAdditionalLinkNodes(request: FreenetRequest) =
48                         listOf(mapOf(
49                                         "rel" to "search",
50                                         "type" to "application/opensearchdescription+xml",
51                                         "title" to "Sone",
52                                         "href" to "http://${request.httpRequest.getHeader("host")}/Sone/OpenSearch.xml"
53                         ))
54
55         final override public fun processTemplate(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
56                 super.processTemplate(freenetRequest, templateContext)
57                 templateContext["preferences"] = core.preferences
58                 templateContext["currentSone"] = getCurrentSone(freenetRequest.toadletContext)
59                 templateContext["localSones"] = core.localSones
60                 templateContext["request"] = freenetRequest
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(freenetRequest.toadletContext)).sortedBy(Notification::getCreatedTime).run {
67                         templateContext["notifications"] = this
68                         templateContext["notificationHash"] = this.hashCode()
69                 }
70                 handleRequest(freenetRequest, templateContext)
71         }
72
73         open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
74                 handleRequest(freenetRequest.toSoneRequest(core, webInterface), templateContext)
75         }
76
77         open fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
78         }
79
80         override public fun getRedirectTarget(freenetRequest: FreenetRequest): String? {
81                 if (requiresLogin && getCurrentSone(freenetRequest.toadletContext) == null) {
82                         val parameters = freenetRequest.httpRequest.parameterNames
83                                         .flatMap { name -> freenetRequest.httpRequest.getMultipleParam(name).map { name to it } }
84                                         .joinToString("&") { "${it.first.urlEncode}=${it.second.urlEncode}" }
85                                         .emptyToNull
86                         return "login.html?target=${freenetRequest.httpRequest.path}${parameters?.let { ("?" + it).urlEncode } ?: ""}"
87                 }
88                 return null
89         }
90
91         private val String.urlEncode: String get() = URLEncoder.encode(this, "UTF-8")
92
93         override fun isEnabled(toadletContext: ToadletContext) =
94                         isEnabled(SoneRequest(toadletContext.uri, Method.GET, HTTPRequestImpl(toadletContext.uri, "GET"), toadletContext, webInterface.l10n, webInterface.sessionManager, core, webInterface))
95
96         open fun isEnabled(soneRequest: SoneRequest) = when {
97                 requiresLogin && getCurrentSone(soneRequest.toadletContext) == null -> false
98                 core.preferences.requireFullAccess && !soneRequest.toadletContext.isAllowedFullAccess -> false
99                 else -> true
100         }
101
102 }