2
0
Files
gitcaddy-server/routers/web/admin/config.go
logikonline 5f2420d353
Some checks failed
Build and Release / Create Release (push) Successful in 0s
Build and Release / Integration Tests (PostgreSQL) (push) Successful in 2m47s
Build and Release / Unit Tests (push) Successful in 6m10s
Build and Release / Lint (push) Successful in 6m17s
Build and Release / Build Binaries (amd64, linux, linux-latest) (push) Failing after 37s
Build and Release / Build Binaries (amd64, windows, windows-latest) (push) Failing after 8h59m46s
Build and Release / Build Binaries (amd64, darwin, macos) (push) Failing after 1m12s
Build and Release / Build Binary (linux/arm64) (push) Failing after 1m19s
Build and Release / Build Binaries (arm64, darwin, macos) (push) Has been cancelled
feat(admin): add configurable footer powered by message
Move ShowFooterPoweredBy from static setting to dynamic theme configuration, allowing admins to toggle the "Powered by GitCaddy Server" footer message through the admin panel without restarting the server.
2026-03-06 16:40:41 -05:00

456 lines
14 KiB
Go

// Copyright 2014 The Gogs Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package admin
import (
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
system_model "code.gitcaddy.com/server/v3/models/system"
"code.gitcaddy.com/server/v3/modules/cache"
"code.gitcaddy.com/server/v3/modules/git"
"code.gitcaddy.com/server/v3/modules/json"
"code.gitcaddy.com/server/v3/modules/log"
"code.gitcaddy.com/server/v3/modules/setting"
"code.gitcaddy.com/server/v3/modules/setting/config"
"code.gitcaddy.com/server/v3/modules/templates"
"code.gitcaddy.com/server/v3/modules/util"
"code.gitcaddy.com/server/v3/services/context"
"code.gitcaddy.com/server/v3/services/mailer"
"gitea.com/go-chi/session"
)
const (
tplConfig templates.TplName = "admin/config"
tplConfigSettings templates.TplName = "admin/config_settings/config_settings"
)
// SendTestMail send test mail to confirm mail service is OK
func SendTestMail(ctx *context.Context) {
email := ctx.FormString("email")
// Send a test email to the user's email address and redirect back to Config
if err := mailer.SendTestMail(email); err != nil {
ctx.Flash.Error(ctx.Tr("admin.config.test_mail_failed", email, err))
} else {
ctx.Flash.Info(ctx.Tr("admin.config.test_mail_sent", email))
}
ctx.Redirect(setting.AppSubURL + "/-/admin/config")
}
// TestCache test the cache settings
func TestCache(ctx *context.Context) {
elapsed, err := cache.Test()
if err != nil {
ctx.Flash.Error(ctx.Tr("admin.config.cache_test_failed", err))
} else {
if elapsed > cache.SlowCacheThreshold {
ctx.Flash.Warning(ctx.Tr("admin.config.cache_test_slow", elapsed))
} else {
ctx.Flash.Info(ctx.Tr("admin.config.cache_test_succeeded", elapsed))
}
}
ctx.Redirect(setting.AppSubURL + "/-/admin/config")
}
func shadowPasswordKV(cfgItem, splitter string) string {
fields := strings.Split(cfgItem, splitter)
for i := range fields {
if strings.HasPrefix(fields[i], "password=") {
fields[i] = "password=******"
break
}
}
return strings.Join(fields, splitter)
}
func shadowURL(provider, cfgItem string) string {
u, err := url.Parse(cfgItem)
if err != nil {
log.Error("Shadowing Password for %v failed: %v", provider, err)
return cfgItem
}
if u.User != nil {
atIdx := strings.Index(cfgItem, "@")
if atIdx > 0 {
colonIdx := strings.LastIndex(cfgItem[:atIdx], ":")
if colonIdx > 0 {
return cfgItem[:colonIdx+1] + "******" + cfgItem[atIdx:]
}
}
}
return cfgItem
}
func shadowPassword(provider, cfgItem string) string {
switch provider {
case "redis":
return shadowPasswordKV(cfgItem, ",")
case "mysql":
// root:@tcp(localhost:3306)/macaron?charset=utf8
atIdx := strings.Index(cfgItem, "@")
if atIdx > 0 {
colonIdx := strings.Index(cfgItem[:atIdx], ":")
if colonIdx > 0 {
return cfgItem[:colonIdx+1] + "******" + cfgItem[atIdx:]
}
}
return cfgItem
case "postgres":
// user=jiahuachen dbname=macaron port=5432 sslmode=disable
if !strings.HasPrefix(cfgItem, "postgres://") {
return shadowPasswordKV(cfgItem, " ")
}
fallthrough
case "couchbase":
return shadowURL(provider, cfgItem)
// postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full
// Notice: use shadowURL
}
return cfgItem
}
// Config show admin config page
func Config(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("admin.config_summary")
ctx.Data["PageIsAdminConfig"] = true
ctx.Data["PageIsAdminConfigSummary"] = true
ctx.Data["CustomConf"] = setting.CustomConf
ctx.Data["AppUrl"] = setting.AppURL
ctx.Data["AppBuiltWith"] = setting.AppBuiltWith
ctx.Data["Domain"] = setting.Domain
ctx.Data["OfflineMode"] = setting.OfflineMode
ctx.Data["RunUser"] = setting.RunUser
ctx.Data["RunMode"] = util.ToTitleCase(setting.RunMode)
ctx.Data["GitVersion"] = git.DefaultFeatures().VersionInfo()
ctx.Data["AppDataPath"] = setting.AppDataPath
ctx.Data["RepoRootPath"] = setting.RepoRootPath
ctx.Data["CustomRootPath"] = setting.CustomPath
ctx.Data["LogRootPath"] = setting.Log.RootPath
ctx.Data["ScriptType"] = setting.ScriptType
ctx.Data["ReverseProxyAuthUser"] = setting.ReverseProxyAuthUser
ctx.Data["ReverseProxyAuthEmail"] = setting.ReverseProxyAuthEmail
ctx.Data["SSH"] = setting.SSH
ctx.Data["LFS"] = setting.LFS
ctx.Data["Service"] = setting.Service
ctx.Data["DbCfg"] = setting.Database
ctx.Data["Webhook"] = setting.Webhook
ctx.Data["MailerEnabled"] = false
if setting.MailService != nil {
ctx.Data["MailerEnabled"] = true
ctx.Data["Mailer"] = setting.MailService
}
ctx.Data["CacheAdapter"] = setting.CacheService.Adapter
ctx.Data["CacheInterval"] = setting.CacheService.Interval
ctx.Data["CacheConn"] = shadowPassword(setting.CacheService.Adapter, setting.CacheService.Conn)
ctx.Data["CacheItemTTL"] = setting.CacheService.TTL
sessionCfg := setting.SessionConfig
if sessionCfg.Provider == "VirtualSession" {
var realSession session.Options
if err := json.Unmarshal([]byte(sessionCfg.ProviderConfig), &realSession); err != nil {
log.Error("Unable to unmarshall session config for virtual provider config: %s\nError: %v", sessionCfg.ProviderConfig, err)
}
sessionCfg.Provider = realSession.Provider
sessionCfg.ProviderConfig = realSession.ProviderConfig
sessionCfg.CookieName = realSession.CookieName
sessionCfg.CookiePath = realSession.CookiePath
sessionCfg.Gclifetime = realSession.Gclifetime
sessionCfg.Maxlifetime = realSession.Maxlifetime
sessionCfg.Secure = realSession.Secure
sessionCfg.Domain = realSession.Domain
}
sessionCfg.ProviderConfig = shadowPassword(sessionCfg.Provider, sessionCfg.ProviderConfig)
ctx.Data["SessionConfig"] = sessionCfg
ctx.Data["Git"] = setting.Git
ctx.Data["AccessLogTemplate"] = setting.Log.AccessLogTemplate
ctx.Data["LogSQL"] = setting.Database.LogSQL
ctx.Data["Loggers"] = log.GetManager().DumpLoggers()
config.GetDynGetter().InvalidateCache()
prepareStartupProblemsAlert(ctx)
ctx.HTML(http.StatusOK, tplConfig)
}
func ConfigSettings(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("admin.config_settings")
ctx.Data["PageIsAdminConfig"] = true
ctx.Data["PageIsAdminConfigSettings"] = true
ctx.Data["DefaultOpenWithEditorAppsString"] = setting.DefaultOpenWithEditorApps().ToTextareaString()
ctx.HTML(http.StatusOK, tplConfigSettings)
}
func ChangeConfig(ctx *context.Context) {
cfg := setting.Config()
marshalBool := func(v string) ([]byte, error) {
b, _ := strconv.ParseBool(v)
return json.Marshal(b)
}
marshalString := func(emptyDefault string) func(v string) ([]byte, error) {
return func(v string) ([]byte, error) {
return json.Marshal(util.IfZero(v, emptyDefault))
}
}
marshalOpenWithApps := func(value string) ([]byte, error) {
// TODO: move the block alongside OpenWithEditorAppsType.ToTextareaString
lines := strings.Split(value, "\n")
var openWithEditorApps setting.OpenWithEditorAppsType
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
displayName, openURL, ok := strings.Cut(line, "=")
displayName, openURL = strings.TrimSpace(displayName), strings.TrimSpace(openURL)
if !ok || displayName == "" || openURL == "" {
continue
}
openWithEditorApps = append(openWithEditorApps, setting.OpenWithEditorApp{
DisplayName: strings.TrimSpace(displayName),
OpenURL: strings.TrimSpace(openURL),
})
}
return json.Marshal(openWithEditorApps)
}
marshallers := map[string]func(string) ([]byte, error){
cfg.Picture.DisableGravatar.DynKey(): marshalBool,
cfg.Picture.EnableFederatedAvatar.DynKey(): marshalBool,
cfg.Repository.OpenWithEditorApps.DynKey(): marshalOpenWithApps,
cfg.Repository.GitGuideRemoteName.DynKey(): marshalString(cfg.Repository.GitGuideRemoteName.DefaultValue()),
cfg.Theme.DisableRegistration.DynKey(): marshalBool,
cfg.Theme.CustomHomeHTML.DynKey(): marshalString(""),
cfg.Theme.APIHeaderURL.DynKey(): marshalString(""),
cfg.Theme.HelpURL.DynKey(): marshalString(""),
cfg.Theme.HideExploreUsers.DynKey(): marshalBool,
cfg.Theme.HideExploreButton.DynKey(): marshalBool,
cfg.Theme.EnableExplorePackages.DynKey(): marshalBool,
cfg.Theme.CustomHomeTitle.DynKey(): marshalString(""),
cfg.Theme.CustomHomeTagline.DynKey(): marshalString(""),
cfg.Theme.PinnedOrgDisplayFormat.DynKey(): marshalString("condensed"),
cfg.Theme.ExploreOrgDisplayFormat.DynKey(): marshalString("list"),
cfg.Theme.EnableBlogs.DynKey(): marshalBool,
cfg.Theme.BlogsInTopNav.DynKey(): marshalBool,
cfg.Theme.ShowFooterPoweredBy.DynKey(): marshalBool,
}
_ = ctx.Req.ParseForm()
configKeys := ctx.Req.Form["key"]
configValues := ctx.Req.Form["value"]
configSettings := map[string]string{}
loop:
for i, key := range configKeys {
if i >= len(configValues) {
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
break loop
}
value := configValues[i]
marshaller, hasMarshaller := marshallers[key]
if !hasMarshaller {
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
break loop
}
marshaledValue, err := marshaller(value)
if err != nil {
ctx.JSONError(ctx.Tr("admin.config.set_setting_failed", key))
break loop
}
configSettings[key] = string(marshaledValue)
}
if ctx.Written() {
return
}
if err := system_model.SetSettings(ctx, configSettings); err != nil {
ctx.ServerError("SetSettings", err)
return
}
config.GetDynGetter().InvalidateCache()
ctx.JSONOK()
}
// ChangeThemeLogo handles homepage logo upload and custom URL
func ChangeThemeLogo(ctx *context.Context) {
cfg := setting.Config()
action := ctx.FormString("action")
if action == "reset" {
configSettings := map[string]string{
cfg.Theme.CustomHomeLogoURL.DynKey(): "\"\"",
}
if err := system_model.SetSettings(ctx, configSettings); err != nil {
ctx.ServerError("SetSettings", err)
return
}
config.GetDynGetter().InvalidateCache()
ctx.Flash.Success(ctx.Tr("admin.config.logo_reset_success"))
ctx.Redirect(setting.AppSubURL + "/-/admin/config/settings")
return
}
// Check for file upload first
file, header, err := ctx.Req.FormFile("logo_file")
if err == nil && header != nil {
defer file.Close()
ext := strings.ToLower(filepath.Ext(header.Filename))
allowedExts := map[string]bool{".svg": true, ".png": true, ".jpg": true, ".jpeg": true, ".gif": true}
if !allowedExts[ext] {
ctx.Flash.Error(ctx.Tr("admin.config.logo_invalid_type"))
ctx.Redirect(setting.AppSubURL + "/-/admin/config/settings")
return
}
customDir := filepath.Join(setting.CustomPath, "public", "assets", "img")
if err := os.MkdirAll(customDir, 0o755); err != nil {
ctx.ServerError("MkdirAll", err)
return
}
fileName := "custom-home-logo" + ext
filePath := filepath.Join(customDir, fileName)
destFile, err := os.Create(filePath)
if err != nil {
ctx.ServerError("Create", err)
return
}
defer destFile.Close()
if _, err := io.Copy(destFile, file); err != nil {
ctx.ServerError("Copy", err)
return
}
fileURL := setting.AppSubURL + "/assets/img/" + fileName
marshaledValue, _ := json.Marshal(fileURL)
configSettings := map[string]string{
cfg.Theme.CustomHomeLogoURL.DynKey(): string(marshaledValue),
}
if err := system_model.SetSettings(ctx, configSettings); err != nil {
ctx.ServerError("SetSettings", err)
return
}
config.GetDynGetter().InvalidateCache()
ctx.Flash.Success(ctx.Tr("admin.config.logo_upload_success"))
ctx.Redirect(setting.AppSubURL + "/-/admin/config/settings")
return
}
// Check for custom URL
customURL := ctx.FormString("custom_logo_url")
marshaledValue, _ := json.Marshal(customURL)
configSettings := map[string]string{
cfg.Theme.CustomHomeLogoURL.DynKey(): string(marshaledValue),
}
if err := system_model.SetSettings(ctx, configSettings); err != nil {
ctx.ServerError("SetSettings", err)
return
}
config.GetDynGetter().InvalidateCache()
ctx.Flash.Success(ctx.Tr("admin.config.logo_url_success"))
ctx.Redirect(setting.AppSubURL + "/-/admin/config/settings")
}
// ChangeThemeIcon handles site icon (favicon + navbar) upload and custom URL
func ChangeThemeIcon(ctx *context.Context) {
cfg := setting.Config()
action := ctx.FormString("action")
if action == "reset" {
configSettings := map[string]string{
cfg.Theme.CustomSiteIconURL.DynKey(): "\"\"",
}
if err := system_model.SetSettings(ctx, configSettings); err != nil {
ctx.ServerError("SetSettings", err)
return
}
config.GetDynGetter().InvalidateCache()
ctx.Flash.Success(ctx.Tr("admin.config.icon_reset_success"))
ctx.Redirect(setting.AppSubURL + "/-/admin/config/settings")
return
}
// Check for file upload first
file, header, err := ctx.Req.FormFile("icon_file")
if err == nil && header != nil {
defer file.Close()
ext := strings.ToLower(filepath.Ext(header.Filename))
allowedExts := map[string]bool{".svg": true, ".png": true, ".ico": true}
if !allowedExts[ext] {
ctx.Flash.Error(ctx.Tr("admin.config.icon_invalid_type"))
ctx.Redirect(setting.AppSubURL + "/-/admin/config/settings")
return
}
customDir := filepath.Join(setting.CustomPath, "public", "assets", "img")
if err := os.MkdirAll(customDir, 0o755); err != nil {
ctx.ServerError("MkdirAll", err)
return
}
fileName := "custom-site-icon" + ext
filePath := filepath.Join(customDir, fileName)
destFile, err := os.Create(filePath)
if err != nil {
ctx.ServerError("Create", err)
return
}
defer destFile.Close()
if _, err := io.Copy(destFile, file); err != nil {
ctx.ServerError("Copy", err)
return
}
fileURL := setting.AppSubURL + "/assets/img/" + fileName
marshaledValue, _ := json.Marshal(fileURL)
configSettings := map[string]string{
cfg.Theme.CustomSiteIconURL.DynKey(): string(marshaledValue),
}
if err := system_model.SetSettings(ctx, configSettings); err != nil {
ctx.ServerError("SetSettings", err)
return
}
config.GetDynGetter().InvalidateCache()
ctx.Flash.Success(ctx.Tr("admin.config.icon_upload_success"))
ctx.Redirect(setting.AppSubURL + "/-/admin/config/settings")
return
}
// Check for custom URL
customURL := ctx.FormString("custom_icon_url")
marshaledValue, _ := json.Marshal(customURL)
configSettings := map[string]string{
cfg.Theme.CustomSiteIconURL.DynKey(): string(marshaledValue),
}
if err := system_model.SetSettings(ctx, configSettings); err != nil {
ctx.ServerError("SetSettings", err)
return
}
config.GetDynGetter().InvalidateCache()
ctx.Flash.Success(ctx.Tr("admin.config.icon_url_success"))
ctx.Redirect(setting.AppSubURL + "/-/admin/config/settings")
}