github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/routes/panel/common.go (about)

     1  package panel
     2  
     3  import (
     4  	"net/http"
     5  
     6  	c "github.com/Azareal/Gosora/common"
     7  	p "github.com/Azareal/Gosora/common/phrases"
     8  )
     9  
    10  // A blank list to fill out that parameter in Page for routes which don't use it
    11  var tList []interface{}
    12  var successJSONBytes = []byte(`{"success":1}`)
    13  
    14  // 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
    15  func successRedirect(dest string, w http.ResponseWriter, r *http.Request, js bool) c.RouteError {
    16  	if !js {
    17  		http.Redirect(w, r, dest, http.StatusSeeOther)
    18  	} else {
    19  		w.Write(successJSONBytes)
    20  	}
    21  	return nil
    22  }
    23  
    24  // TODO: Prerender needs to handle dyntmpl templates better...
    25  func renderTemplate(tmplName string, w http.ResponseWriter, r *http.Request, h *c.Header, pi interface{}) c.RouteError {
    26  	if !h.LooseCSP {
    27  		if c.Config.SslSchema {
    28  			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")
    29  		} else {
    30  			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'")
    31  		}
    32  	}
    33  
    34  	h.AddScript("global.js")
    35  	if c.RunPreRenderHook("pre_render_"+tmplName, w, r, h.CurrentUser, pi) {
    36  		return nil
    37  	}
    38  	// TODO: Prepend this with panel_?
    39  	err := h.Theme.RunTmpl(tmplName, pi, w)
    40  	if err != nil {
    41  		return c.InternalError(err, w, r)
    42  	}
    43  	return nil
    44  }
    45  
    46  func buildBasePage(w http.ResponseWriter, r *http.Request, u *c.User, titlePhrase, zone string) (*c.BasePanelPage, c.RouteError) {
    47  	h, stats, ferr := c.PanelUserCheck(w, r, u)
    48  	if ferr != nil {
    49  		return nil, ferr
    50  	}
    51  	h.Title = p.GetTitlePhrase("panel_" + titlePhrase)
    52  	debugAdmin := true
    53  
    54  	return &c.BasePanelPage{h, stats, zone, c.ReportForumID, debugAdmin}, nil
    55  }