2017-11-10 03:33:11 +00:00
package common
2017-01-01 15:45:43 +00:00
2017-05-29 14:52:37 +00:00
import (
2017-11-11 23:34:27 +00:00
"database/sql"
2017-09-03 04:50:31 +00:00
"encoding/json"
2017-08-13 11:22:34 +00:00
"errors"
2019-02-10 05:52:26 +00:00
"html/template"
2018-04-22 12:33:56 +00:00
"io"
2017-05-29 14:52:37 +00:00
"io/ioutil"
2017-09-03 04:50:31 +00:00
"log"
2017-05-29 14:52:37 +00:00
"net/http"
2017-09-03 04:50:31 +00:00
"os"
2019-02-10 05:52:26 +00:00
"path/filepath"
"strings"
2017-09-10 16:57:22 +00:00
"sync"
"sync/atomic"
2017-11-11 04:06:16 +00:00
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/query_gen"
2017-05-29 14:52:37 +00:00
)
2017-01-01 15:45:43 +00:00
2018-10-02 04:09:17 +00:00
// TODO: Something more thread-safe
2017-12-01 02:04:29 +00:00
type ThemeList map [ string ] * Theme
2017-11-06 04:02:35 +00:00
2018-05-27 09:36:35 +00:00
var Themes ThemeList = make ( map [ string ] * Theme ) // ? Refactor this into a store?
2017-11-10 03:33:11 +00:00
var DefaultThemeBox atomic . Value
2017-11-11 04:06:16 +00:00
var ChangeDefaultThemeMutex sync . Mutex
2017-01-01 15:45:43 +00:00
2018-12-08 00:45:27 +00:00
// TODO: Fallback to a random theme if this doesn't exist, so admins can remove themes they don't use
2017-09-22 02:21:17 +00:00
// TODO: Use this when the default theme doesn't exist
2018-03-17 08:16:43 +00:00
var fallbackTheme = "cosora"
2018-05-27 09:36:35 +00:00
var overridenTemplates = make ( map [ string ] bool ) // ? What is this used for?
2017-06-19 08:06:54 +00:00
2017-11-11 23:34:27 +00:00
type ThemeStmts struct {
2018-12-14 04:08:53 +00:00
getAll * sql . Stmt
isDefault * sql . Stmt
update * sql . Stmt
add * sql . Stmt
2017-11-11 23:34:27 +00:00
}
var themeStmts ThemeStmts
2017-09-23 19:57:13 +00:00
func init ( ) {
2017-11-12 03:29:05 +00:00
DbInits . Add ( func ( acc * qgen . Accumulator ) error {
2017-11-11 23:34:27 +00:00
themeStmts = ThemeStmts {
2018-12-14 04:08:53 +00:00
getAll : acc . Select ( "themes" ) . Columns ( "uname, default" ) . Prepare ( ) ,
isDefault : acc . Select ( "themes" ) . Columns ( "default" ) . Where ( "uname = ?" ) . Prepare ( ) ,
update : acc . Update ( "themes" ) . Set ( "default = ?" ) . Where ( "uname = ?" ) . Prepare ( ) ,
add : acc . Insert ( "themes" ) . Columns ( "uname, default" ) . Fields ( "?,?" ) . Prepare ( ) ,
2017-11-11 23:34:27 +00:00
}
return acc . FirstError ( )
} )
2017-09-23 19:57:13 +00:00
}
2018-05-27 09:36:35 +00:00
func NewThemeList ( ) ( themes ThemeList , err error ) {
themes = make ( map [ string ] * Theme )
2017-01-01 15:45:43 +00:00
themeFiles , err := ioutil . ReadDir ( "./themes" )
if err != nil {
2018-05-27 09:36:35 +00:00
return themes , err
2017-01-01 15:45:43 +00:00
}
2018-12-14 04:08:53 +00:00
if len ( themeFiles ) == 0 {
return themes , errors . New ( "You don't have any themes" )
}
2017-05-29 14:52:37 +00:00
2018-12-14 04:08:53 +00:00
var lastTheme , defaultTheme string
2017-01-01 15:45:43 +00:00
for _ , themeFile := range themeFiles {
if ! themeFile . IsDir ( ) {
continue
}
2017-05-29 14:52:37 +00:00
2017-01-01 15:45:43 +00:00
themeName := themeFile . Name ( )
2017-11-29 02:34:02 +00:00
log . Printf ( "Adding theme '%s'" , themeName )
2018-05-27 09:36:35 +00:00
themePath := "./themes/" + themeName
themeFile , err := ioutil . ReadFile ( themePath + "/theme.json" )
2017-01-01 15:45:43 +00:00
if err != nil {
2018-05-27 09:36:35 +00:00
return themes , err
2017-01-01 15:45:43 +00:00
}
2017-05-29 14:52:37 +00:00
2019-10-01 21:06:22 +00:00
theme := & Theme { Name : "" }
2017-12-01 02:04:29 +00:00
err = json . Unmarshal ( themeFile , theme )
2017-01-07 06:31:04 +00:00
if err != nil {
2018-05-27 09:36:35 +00:00
return themes , err
}
2018-12-14 04:08:53 +00:00
if theme . Name == "" {
return themes , errors . New ( "Theme " + themePath + " doesn't have a name set in theme.json" )
}
if theme . Name == fallbackTheme {
defaultTheme = fallbackTheme
}
lastTheme = theme . Name
2018-05-27 09:36:35 +00:00
// TODO: Implement the static file part of this and fsnotify
if theme . Path != "" {
log . Print ( "Resolving redirect to " + theme . Path )
themeFile , err := ioutil . ReadFile ( theme . Path + "/theme.json" )
if err != nil {
return themes , err
}
theme = & Theme { Name : "" , Path : theme . Path }
err = json . Unmarshal ( themeFile , theme )
if err != nil {
return themes , err
}
} else {
theme . Path = themePath
2017-01-07 06:31:04 +00:00
}
2017-05-29 14:52:37 +00:00
2017-01-01 15:45:43 +00:00
theme . Active = false // Set this to false, just in case someone explicitly overrode this value in the JSON file
2017-05-29 14:52:37 +00:00
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
// TODO: Let the theme specify where it's resources are via the JSON file?
// TODO: Let the theme inherit CSS from another theme?
// ? - This might not be too helpful, as it only searches for /public/ and not if /public/ is empty. Still, it might help some people with a slightly less cryptic error
2018-05-27 09:36:35 +00:00
log . Print ( theme . Path + "/public/" )
_ , err = os . Stat ( theme . Path + "/public/" )
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
if err != nil {
if os . IsNotExist ( err ) {
2018-05-27 09:36:35 +00:00
return themes , errors . New ( "We couldn't find this theme's resources. E.g. the /public/ folder." )
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
} else {
log . Print ( "We weren't able to access this theme's resources due to a permissions issue or some other problem" )
2018-05-27 09:36:35 +00:00
return themes , err
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
}
}
2017-01-07 06:31:04 +00:00
if theme . FullImage != "" {
2018-02-19 04:26:01 +00:00
DebugLog ( "Adding theme image" )
2018-05-27 09:36:35 +00:00
err = StaticFiles . Add ( theme . Path + "/" + theme . FullImage , themePath )
2017-01-07 06:31:04 +00:00
if err != nil {
2018-05-27 09:36:35 +00:00
return themes , err
2017-01-07 06:31:04 +00:00
}
}
2017-05-29 14:52:37 +00:00
2017-09-23 19:57:13 +00:00
theme . TemplatesMap = make ( map [ string ] string )
theme . TmplPtr = make ( map [ string ] interface { } )
if theme . Templates != nil {
for _ , themeTmpl := range theme . Templates {
theme . TemplatesMap [ themeTmpl . Name ] = themeTmpl . Source
2017-11-11 04:06:16 +00:00
theme . TmplPtr [ themeTmpl . Name ] = TmplPtrMap [ "o_" + themeTmpl . Source ]
2017-09-23 19:57:13 +00:00
}
}
2019-02-10 05:52:26 +00:00
theme . IntTmplHandle = DefaultTemplates
overrides , err := ioutil . ReadDir ( theme . Path + "/overrides/" )
if err != nil && ! os . IsNotExist ( err ) {
return themes , err
}
if len ( overrides ) > 0 {
var overCount = 0
2019-02-28 07:28:17 +00:00
theme . OverridenMap = make ( map [ string ] bool )
2019-02-10 05:52:26 +00:00
for _ , override := range overrides {
if override . IsDir ( ) {
continue
}
2019-10-01 21:06:22 +00:00
ext := filepath . Ext ( themePath + "/overrides/" + override . Name ( ) )
2019-02-10 05:52:26 +00:00
log . Print ( "attempting to add " + themePath + "/overrides/" + override . Name ( ) )
if ext != ".html" {
log . Print ( "not a html file" )
continue
}
overCount ++
2019-02-28 07:28:17 +00:00
nosuf := strings . TrimSuffix ( override . Name ( ) , ext )
theme . OverridenTemplates = append ( theme . OverridenTemplates , nosuf )
theme . OverridenMap [ nosuf ] = true
//theme.TmplPtr[nosuf] = TmplPtrMap["o_"+nosuf]
2019-02-10 05:52:26 +00:00
log . Print ( "succeeded" )
}
localTmpls := template . New ( "" )
err = loadTemplates ( localTmpls , theme . Name )
if err != nil {
return themes , err
}
theme . IntTmplHandle = localTmpls
log . Printf ( "theme.OverridenTemplates: %+v\n" , theme . OverridenTemplates )
log . Printf ( "theme.IntTmplHandle: %+v\n" , theme . IntTmplHandle )
} else {
log . Print ( "no overrides for " + theme . Name )
}
2018-05-27 09:36:35 +00:00
// TODO: Bind the built template, or an interpreted one for any dock overrides this theme has
themes [ theme . Name ] = theme
2017-01-01 15:45:43 +00:00
}
2018-12-14 04:08:53 +00:00
if defaultTheme == "" {
defaultTheme = lastTheme
}
DefaultThemeBox . Store ( defaultTheme )
2018-05-27 09:36:35 +00:00
return themes , nil
2017-01-01 15:45:43 +00:00
}
2018-05-27 09:36:35 +00:00
// TODO: Make the initThemes and LoadThemes functions less confusing
// ? - Delete themes which no longer exist in the themes folder from the database?
2019-10-01 21:06:22 +00:00
func ( t ThemeList ) LoadActiveStatus ( ) error {
2018-05-27 09:36:35 +00:00
ChangeDefaultThemeMutex . Lock ( )
2018-10-02 04:09:17 +00:00
defer ChangeDefaultThemeMutex . Unlock ( )
2018-12-14 04:08:53 +00:00
rows , err := themeStmts . getAll . Query ( )
2018-05-27 09:36:35 +00:00
if err != nil {
return err
}
defer rows . Close ( )
2017-12-01 02:04:29 +00:00
2018-05-27 09:36:35 +00:00
var uname string
var defaultThemeSwitch bool
for rows . Next ( ) {
err = rows . Scan ( & uname , & defaultThemeSwitch )
2017-01-01 15:45:43 +00:00
if err != nil {
return err
}
2017-05-29 14:52:37 +00:00
2018-05-27 09:36:35 +00:00
// Was the theme deleted at some point?
2019-10-01 21:06:22 +00:00
theme , ok := t [ uname ]
2018-05-27 09:36:35 +00:00
if ! ok {
continue
2017-01-01 15:45:43 +00:00
}
2017-05-29 14:52:37 +00:00
2018-05-27 09:36:35 +00:00
if defaultThemeSwitch {
2018-10-02 04:09:17 +00:00
DebugLogf ( "Loading the default theme '%s'" , theme . Name )
2018-05-27 09:36:35 +00:00
theme . Active = true
DefaultThemeBox . Store ( theme . Name )
theme . MapTemplates ( )
} else {
2018-10-02 04:09:17 +00:00
DebugLogf ( "Loading the theme '%s'" , theme . Name )
2018-05-27 09:36:35 +00:00
theme . Active = false
2017-05-07 08:31:41 +00:00
}
2017-06-25 09:56:39 +00:00
2019-10-01 21:06:22 +00:00
t [ uname ] = theme
2018-05-27 09:36:35 +00:00
}
return rows . Err ( )
2017-01-01 15:45:43 +00:00
}
2019-10-01 21:06:22 +00:00
func ( t ThemeList ) LoadStaticFiles ( ) error {
for _ , theme := range t {
2018-05-27 09:36:35 +00:00
err := theme . LoadStaticFiles ( )
if err != nil {
return err
2017-01-01 15:45:43 +00:00
}
}
2018-05-27 09:36:35 +00:00
return nil
2017-01-01 15:45:43 +00:00
}
2017-11-11 04:06:16 +00:00
func ResetTemplateOverrides ( ) {
2017-01-01 15:45:43 +00:00
log . Print ( "Resetting the template overrides" )
2017-09-03 04:50:31 +00:00
for name := range overridenTemplates {
2017-01-01 15:45:43 +00:00
log . Print ( "Resetting '" + name + "' template override" )
2017-05-29 14:52:37 +00:00
2017-11-11 04:06:16 +00:00
originPointer , ok := TmplPtrMap [ "o_" + name ]
2017-01-01 15:45:43 +00:00
if ! ok {
log . Print ( "The origin template doesn't exist!" )
return
}
2017-11-11 04:06:16 +00:00
destTmplPtr , ok := TmplPtrMap [ name ]
2017-01-01 15:45:43 +00:00
if ! ok {
log . Print ( "The destination template doesn't exist!" )
return
}
2017-05-29 14:52:37 +00:00
2017-01-01 16:14:38 +00:00
// Not really a pointer, more of a function handle, an artifact from one of the earlier versions of themes.go
2019-02-10 05:52:26 +00:00
oPtr , ok := originPointer . ( func ( interface { } , io . Writer ) error )
if ! ok {
2018-03-17 08:16:43 +00:00
log . Print ( "name: " , name )
2018-03-11 09:33:49 +00:00
LogError ( errors . New ( "Unknown destination template type!" ) )
2019-02-10 05:52:26 +00:00
return
2017-01-01 15:45:43 +00:00
}
2019-02-10 05:52:26 +00:00
dPtr , ok := destTmplPtr . ( * func ( interface { } , io . Writer ) error )
if ! ok {
LogError ( errors . New ( "The source and destination templates are incompatible" ) )
return
}
* dPtr = oPtr
2017-01-01 15:45:43 +00:00
log . Print ( "The template override was reset" )
}
2017-09-03 04:50:31 +00:00
overridenTemplates = make ( map [ string ] bool )
2017-01-01 15:45:43 +00:00
log . Print ( "All of the template overrides have been reset" )
}
2017-08-13 11:22:34 +00:00
2017-09-03 04:50:31 +00:00
// CreateThemeTemplate creates a theme template on the current default theme
2017-08-13 11:22:34 +00:00
func CreateThemeTemplate ( theme string , name string ) {
2017-11-11 04:06:16 +00:00
Themes [ theme ] . TmplPtr [ name ] = func ( pi Page , w http . ResponseWriter ) error {
mapping , ok := Themes [ DefaultThemeBox . Load ( ) . ( string ) ] . TemplatesMap [ name ]
2017-08-13 11:22:34 +00:00
if ! ok {
mapping = name
}
2019-02-10 05:52:26 +00:00
return DefaultTemplates . ExecuteTemplate ( w , mapping + ".html" , pi )
2017-08-13 11:22:34 +00:00
}
}
2017-09-10 16:57:22 +00:00
func GetDefaultThemeName ( ) string {
2017-11-11 04:06:16 +00:00
return DefaultThemeBox . Load ( ) . ( string )
2017-09-10 16:57:22 +00:00
}
func SetDefaultThemeName ( name string ) {
2017-11-11 04:06:16 +00:00
DefaultThemeBox . Store ( name )
2017-09-10 16:57:22 +00:00
}