2018-05-27 09:36:35 +00:00
package panel
import (
"net/http"
2019-04-19 06:36:26 +00:00
c "github.com/Azareal/Gosora/common"
2018-11-01 06:43:56 +00:00
"github.com/Azareal/Gosora/common/phrases"
2018-05-27 09:36:35 +00:00
)
// A blank list to fill out that parameter in Page for routes which don't use it
var tList [ ] interface { }
2019-11-04 07:46:34 +00:00
var successJSONBytes = [ ] byte ( ` { "success":1} ` )
2018-05-27 09:36:35 +00:00
// We're trying to reduce the amount of boilerplate in here, so I added these two functions, they might wind up circulating outside this file in the future
2019-09-29 05:10:05 +00:00
func successRedirect ( dest string , w http . ResponseWriter , r * http . Request , js bool ) c . RouteError {
if ! js {
2018-05-27 09:36:35 +00:00
http . Redirect ( w , r , dest , http . StatusSeeOther )
} else {
w . Write ( successJSONBytes )
}
return nil
}
2018-09-30 00:42:33 +00:00
2019-04-29 01:28:55 +00:00
// TODO: Prerender needs to handle dyntmpl templates better...
2020-03-18 09:21:34 +00:00
func renderTemplate ( tmplName string , w http . ResponseWriter , r * http . Request , h * c . Header , pi interface { } ) c . RouteError {
if ! h . LooseCSP {
2019-11-04 11:55:52 +00:00
if c . Config . SslSchema {
2019-11-04 10:11:07 +00:00
w . Header ( ) . Set ( "Content-Security-Policy" , "default-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-eval' 'unsafe-inline'; img-src * data: 'unsafe-eval' 'unsafe-inline'; connect-src * 'unsafe-eval' 'unsafe-inline'; frame-src 'self';upgrade-insecure-requests" )
} else {
w . Header ( ) . Set ( "Content-Security-Policy" , "default-src 'self' 'unsafe-eval'; style-src 'self' 'unsafe-eval' 'unsafe-inline'; img-src * data: 'unsafe-eval' 'unsafe-inline'; connect-src * 'unsafe-eval' 'unsafe-inline'; frame-src 'self'" )
}
2019-11-04 07:46:34 +00:00
}
2020-03-18 09:21:34 +00:00
h . AddScript ( "global.js" )
if c . RunPreRenderHook ( "pre_render_" + tmplName , w , r , h . CurrentUser , pi ) {
2018-05-27 09:36:35 +00:00
return nil
}
2018-10-02 07:20:55 +00:00
// TODO: Prepend this with panel_?
2020-03-18 09:21:34 +00:00
err := h . Theme . RunTmpl ( tmplName , pi , w )
2018-05-27 09:36:35 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-05-27 09:36:35 +00:00
}
return nil
}
2018-06-17 07:28:18 +00:00
2020-02-18 23:04:14 +00:00
func buildBasePage ( w http . ResponseWriter , r * http . Request , user * c . User , titlePhrase , zone string ) ( * c . BasePanelPage , c . RouteError ) {
2019-04-19 06:36:26 +00:00
header , stats , ferr := c . PanelUserCheck ( w , r , user )
2018-06-17 07:28:18 +00:00
if ferr != nil {
return nil , ferr
}
2018-11-01 06:43:56 +00:00
header . Title = phrases . GetTitlePhrase ( "panel_" + titlePhrase )
2018-06-17 07:28:18 +00:00
2019-04-19 06:36:26 +00:00
return & c . BasePanelPage { header , stats , zone , c . ReportForumID } , nil
2018-06-17 07:28:18 +00:00
}