diff --git a/pkg/context/context.go b/pkg/context/context.go index 85f49cc6b..33ae8b792 100644 --- a/pkg/context/context.go +++ b/pkg/context/context.go @@ -327,6 +327,8 @@ func Contexter() macaron.Handler { c.Data["ShowFooterBranding"] = setting.ShowFooterBranding c.Data["ShowFooterVersion"] = setting.ShowFooterVersion + c.Data["ServerNotice"] = readServerNotice() + ctx.Map(c) } } diff --git a/pkg/context/notice.go b/pkg/context/notice.go new file mode 100644 index 000000000..e1bce474b --- /dev/null +++ b/pkg/context/notice.go @@ -0,0 +1,62 @@ +// Copyright 2019 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package context + +import ( + "os" + "path" + + "github.com/Unknwon/com" + log "gopkg.in/clog.v1" + + "github.com/gogs/gogs/pkg/markup" + "github.com/gogs/gogs/pkg/setting" + "github.com/gogs/gogs/pkg/tool" +) + +// readServerNotice checks if a notice file exists and loads the message to display +// on all pages. +func readServerNotice() string { + fpath := path.Join(setting.CustomPath, "notice.md") + if !com.IsExist(fpath) { + return "" + } + + f, err := os.Open(fpath) + if err != nil { + log.Error(2, "Failed to open notice file %s: %v", fpath, err) + return "" + } + defer f.Close() + + fi, err := f.Stat() + if err != nil { + log.Error(2, "Failed to stat notice file %s: %v", fpath, err) + return "" + } + + // Limit size to prevent very large messages from breaking pages + var maxSize int64 = 1024 + + if fi.Size() > maxSize { // Refuse to print very long messages + log.Error(2, "Notice file %s size too large [%d > %d]: refusing to render", fpath, fi.Size(), maxSize) + return "" + } + + buf := make([]byte, maxSize) + n, err := f.Read(buf) + if err != nil { + log.Error(2, "Failed to read notice file: %v", err) + return "" + } + buf = buf[:n] + + if !tool.IsTextFile(buf) { + log.Error(2, "Notice file %s does not appear to be a text file: aborting", fpath) + return "" + } + + return string(markup.RawMarkdown(buf, "")) +} diff --git a/templates/base/head.tmpl b/templates/base/head.tmpl index 27b2f4c72..dd144f706 100644 --- a/templates/base/head.tmpl +++ b/templates/base/head.tmpl @@ -192,6 +192,14 @@ {{end}} + + {{if .ServerNotice}} +
+ {{end}} {{/*