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...
2019-04-19 06:36:26 +00:00
func renderTemplate ( tmplName string , w http . ResponseWriter , r * http . Request , header * c . Header , pi interface { } ) c . RouteError {
2019-11-04 07:46:34 +00:00
// TODO: Expand this to non-HTTPS requests too
if ! header . LooseCSP && c . Site . EnableSsl {
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" )
}
2019-03-21 22:59:41 +00:00
header . AddScript ( "global.js" )
2019-04-19 06:36:26 +00:00
if c . RunPreRenderHook ( "pre_render_" + tmplName , w , r , & header . 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_?
2019-02-10 05:52:26 +00:00
err := header . 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
2019-04-19 06:36:26 +00:00
func buildBasePage ( w http . ResponseWriter , r * http . Request , user * c . User , titlePhrase string , zone string ) ( * c . BasePanelPage , c . RouteError ) {
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
}