2017-11-10 03:33:11 +00:00
package common
import (
"html/template"
2017-11-29 02:34:02 +00:00
"net/http"
2018-08-16 05:27:26 +00:00
"runtime"
2017-11-10 03:33:11 +00:00
"sync"
"time"
2018-11-01 06:43:56 +00:00
"github.com/Azareal/Gosora/common/phrases"
2017-11-10 03:33:11 +00:00
)
2019-04-02 07:43:11 +00:00
/ * type HResource struct {
2019-03-21 22:59:41 +00:00
Name string
Hash string
2019-04-02 07:43:11 +00:00
} * /
2019-03-21 22:59:41 +00:00
2018-04-03 04:34:07 +00:00
// TODO: Allow resources in spots other than /static/ and possibly even external domains (e.g. CDNs)
2018-04-04 04:49:13 +00:00
// TODO: Preload Trumboyg on Cosora on the forum list
2018-04-22 12:33:56 +00:00
type Header struct {
2018-11-29 07:27:17 +00:00
Title string
//Title []byte // Experimenting with []byte for increased efficiency, let's avoid converting too many things to []byte, as it involves a lot of extra boilerplate
2019-02-28 07:28:17 +00:00
NoticeList [ ] string
2019-04-02 07:43:11 +00:00
Scripts [ ] string
PreScriptsAsync [ ] string
ScriptsAsync [ ] string
2018-06-24 13:49:29 +00:00
//Preload []string
2019-04-02 07:43:11 +00:00
Stylesheets [ ] string
2017-11-10 03:33:11 +00:00
Widgets PageWidgets
2017-11-11 04:06:16 +00:00
Site * site
2017-11-13 09:23:43 +00:00
Settings SettingMap
2017-12-01 02:04:29 +00:00
Themes map [ string ] * Theme // TODO: Use a slice containing every theme instead of the main map for speed?
Theme * Theme
2017-11-10 03:33:11 +00:00
//TemplateName string // TODO: Use this to move template calls to the router rather than duplicating them over and over and over?
2018-06-06 00:21:22 +00:00
CurrentUser User // TODO: Deprecate CurrentUser on the page structs and use a pointer here
2018-10-21 13:54:32 +00:00
Hooks * HookTable
2018-04-22 12:33:56 +00:00
Zone string
2019-01-21 12:27:59 +00:00
ZoneID int
ZoneData interface { }
2018-10-14 05:08:44 +00:00
Path string
2018-04-22 12:33:56 +00:00
MetaDesc string
2019-02-23 06:29:19 +00:00
//OGImage string
2019-02-28 07:28:17 +00:00
OGDesc string
GoogSiteVerify string
2019-04-09 07:21:00 +00:00
IsoCode string
2019-02-28 07:28:17 +00:00
LooseCSP bool
StartedAt time . Time
Elapsed1 string
Writer http . ResponseWriter
ExtData ExtData
2017-11-10 03:33:11 +00:00
}
2018-04-22 12:33:56 +00:00
func ( header * Header ) AddScript ( name string ) {
2019-03-21 22:59:41 +00:00
fname := "/static/" + name
2019-04-02 07:43:11 +00:00
var oname string
2019-03-21 22:59:41 +00:00
if fname [ 0 ] == '/' && fname [ 1 ] != '/' {
file , ok := StaticFiles . Get ( fname )
if ok {
2019-04-02 07:43:11 +00:00
oname = name + "?h=" + file . Sha256
2019-03-21 22:59:41 +00:00
}
}
2019-04-02 07:43:11 +00:00
if oname == "" {
oname = name
}
//log.Print("oname:", oname)
header . Scripts = append ( header . Scripts , oname )
2018-04-04 04:49:13 +00:00
}
2019-02-28 07:28:17 +00:00
func ( header * Header ) AddPreScriptAsync ( name string ) {
2019-03-21 22:59:41 +00:00
fname := "/static/" + name
2019-04-02 07:43:11 +00:00
var oname string
2019-03-21 22:59:41 +00:00
if fname [ 0 ] == '/' && fname [ 1 ] != '/' {
file , ok := StaticFiles . Get ( fname )
if ok {
2019-04-02 07:43:11 +00:00
oname = name + "?h=" + file . Sha256
2019-03-21 22:59:41 +00:00
}
}
2019-04-02 07:43:11 +00:00
if oname == "" {
oname = name
}
header . PreScriptsAsync = append ( header . PreScriptsAsync , oname )
2019-03-21 22:59:41 +00:00
}
func ( header * Header ) AddScriptAsync ( name string ) {
fname := "/static/" + name
2019-04-02 07:43:11 +00:00
var oname string
2019-03-21 22:59:41 +00:00
if fname [ 0 ] == '/' && fname [ 1 ] != '/' {
file , ok := StaticFiles . Get ( fname )
if ok {
2019-04-02 07:43:11 +00:00
oname = name + "?h=" + file . Sha256
2019-03-21 22:59:41 +00:00
}
}
2019-04-02 07:43:11 +00:00
if oname == "" {
oname = name
}
header . ScriptsAsync = append ( header . ScriptsAsync , oname )
2019-02-28 07:28:17 +00:00
}
2018-06-24 13:49:29 +00:00
/ * func ( header * Header ) Preload ( name string ) {
header . Preload = append ( header . Preload , name )
2018-04-04 04:49:13 +00:00
} * /
2018-04-22 12:33:56 +00:00
func ( header * Header ) AddSheet ( name string ) {
2019-03-24 07:23:38 +00:00
fname := "/static/" + name
2019-04-02 07:43:11 +00:00
var oname string
2019-03-24 07:23:38 +00:00
if fname [ 0 ] == '/' && fname [ 1 ] != '/' {
file , ok := StaticFiles . Get ( fname )
if ok {
2019-04-02 07:43:11 +00:00
oname = name + "?h=" + file . Sha256
2019-03-24 07:23:38 +00:00
}
}
2019-04-02 07:43:11 +00:00
if oname == "" {
oname = name
}
header . Stylesheets = append ( header . Stylesheets , oname )
2018-04-04 04:49:13 +00:00
}
2018-06-06 00:21:22 +00:00
func ( header * Header ) AddNotice ( name string ) {
2018-11-01 06:43:56 +00:00
header . NoticeList = append ( header . NoticeList , phrases . GetNoticePhrase ( name ) )
2018-06-06 00:21:22 +00:00
}
2017-11-10 03:33:11 +00:00
// TODO: Add this to routes which don't use templates. E.g. Json APIs.
type HeaderLite struct {
2017-11-11 04:06:16 +00:00
Site * site
2017-11-10 03:33:11 +00:00
Settings SettingMap
2018-10-21 13:54:32 +00:00
Hooks * HookTable
2017-11-10 03:33:11 +00:00
ExtData ExtData
}
type PageWidgets struct {
LeftSidebar template . HTML
RightSidebar template . HTML
}
// TODO: Add a ExtDataHolder interface with methods for manipulating the contents?
// ? - Could we use a sync.Map instead?
type ExtData struct {
2017-11-11 04:06:16 +00:00
Items map [ string ] interface { } // Key: pluginname
2017-11-10 03:33:11 +00:00
sync . RWMutex
}
type Page struct {
2018-06-06 00:21:22 +00:00
* Header
ItemList [ ] interface { }
Something interface { }
}
type SimplePage struct {
* Header
}
type ErrorPage struct {
* Header
Message string
2017-11-10 03:33:11 +00:00
}
2018-04-22 12:33:56 +00:00
type Paginator struct {
PageList [ ] int
Page int
LastPage int
}
2018-06-06 00:21:22 +00:00
type CustomPagePage struct {
* Header
Page * CustomPage
}
2017-11-10 03:33:11 +00:00
type TopicPage struct {
2018-05-27 09:36:35 +00:00
* Header
ItemList [ ] ReplyUser
Topic TopicUser
2018-08-30 10:31:21 +00:00
Forum * Forum
2018-05-27 09:36:35 +00:00
Poll Poll
2018-12-17 04:58:55 +00:00
Paginator
2017-11-10 03:33:11 +00:00
}
2018-09-20 08:56:50 +00:00
type TopicListSort struct {
SortBy string // lastupdate, mostviewed, mostviewedtoday, mostviewedthisweek, mostviewedthismonth
Ascending bool
}
2018-04-22 12:33:56 +00:00
type TopicListPage struct {
* Header
2017-11-10 03:33:11 +00:00
TopicList [ ] * TopicsRow
ForumList [ ] Forum
DefaultForum int
2018-09-20 08:56:50 +00:00
Sort TopicListSort
2018-04-22 12:33:56 +00:00
Paginator
2017-11-10 03:33:11 +00:00
}
type ForumPage struct {
2018-05-27 09:36:35 +00:00
* Header
ItemList [ ] * TopicsRow
Forum * Forum
2018-04-22 12:33:56 +00:00
Paginator
2017-11-10 03:33:11 +00:00
}
type ForumsPage struct {
2018-06-01 05:02:29 +00:00
* Header
ItemList [ ] Forum
2017-11-10 03:33:11 +00:00
}
type ProfilePage struct {
2018-06-01 05:02:29 +00:00
* Header
2017-11-10 03:33:11 +00:00
ItemList [ ] ReplyUser
ProfileOwner User
2018-10-08 05:34:25 +00:00
CurrentScore int
NextScore int
2017-11-10 03:33:11 +00:00
}
type CreateTopicPage struct {
2018-06-01 05:02:29 +00:00
* Header
ItemList [ ] Forum
FID int
2017-11-10 03:33:11 +00:00
}
type IPSearchPage struct {
2018-06-01 05:02:29 +00:00
* Header
ItemList map [ int ] * User
IP string
2017-11-10 03:33:11 +00:00
}
2018-12-17 04:58:55 +00:00
type Account struct {
* Header
HTMLID string
TmplName string
Inner nobreak
}
2018-05-27 09:36:35 +00:00
type EmailListPage struct {
2018-06-01 05:02:29 +00:00
* Header
2018-12-17 04:58:55 +00:00
ItemList [ ] Email
}
type AccountLoginsPage struct {
* Header
ItemList [ ] LoginLogItem
Paginator
2018-05-27 09:36:35 +00:00
}
2018-06-17 07:28:18 +00:00
type AccountDashPage struct {
* Header
2018-10-08 05:34:25 +00:00
MFASetup bool
CurrentScore int
NextScore int
NextLevel int
2018-10-14 05:08:44 +00:00
Percentage int
2018-06-17 07:28:18 +00:00
}
2018-10-10 07:33:51 +00:00
type LevelListItem struct {
Level int
Score int
Status string
Percentage int // 0 to 200 to fit with the CSS logic
}
type LevelListPage struct {
* Header
Levels [ ] LevelListItem
}
2019-03-11 08:47:45 +00:00
type ResetPage struct {
* Header
UID int
Token string
MFA bool
}
2017-11-10 03:33:11 +00:00
type PanelStats struct {
Users int
Groups int
Forums int
2018-06-06 00:21:22 +00:00
Pages int
2017-11-10 03:33:11 +00:00
Settings int
WordFilters int
Themes int
Reports int
}
2018-06-06 00:21:22 +00:00
type BasePanelPage struct {
* Header
Stats PanelStats
Zone string
ReportForumID int
}
2017-11-10 03:33:11 +00:00
type PanelPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
ItemList [ ] interface { }
Something interface { }
2017-11-10 03:33:11 +00:00
}
type GridElement struct {
ID string
Body string
Order int // For future use
Class string
Background string
TextColour string
Note string
}
type PanelDashboardPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2018-05-11 05:41:51 +00:00
GridItems [ ] GridElement
2017-11-10 03:33:11 +00:00
}
2018-05-27 09:36:35 +00:00
type PanelSetting struct {
* Setting
FriendlyName string
}
type PanelSettingPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2018-05-27 09:36:35 +00:00
ItemList [ ] OptionLabel
Setting * PanelSetting
}
2018-06-06 00:21:22 +00:00
type PanelCustomPagesPage struct {
* BasePanelPage
ItemList [ ] * CustomPage
Paginator
}
type PanelCustomPageEditPage struct {
* BasePanelPage
Page * CustomPage
}
2019-02-23 06:29:19 +00:00
/ * type PanelTimeGraph struct {
2018-01-03 07:46:18 +00:00
Series [ ] int64 // The counts on the left
Labels [ ] int64 // unixtimes for the bottom, gets converted into 1:00, 2:00, etc. with JS
2019-02-23 06:29:19 +00:00
} * /
type PanelTimeGraph struct {
Series [ ] [ ] int64 // The counts on the left
Labels [ ] int64 // unixtimes for the bottom, gets converted into 1:00, 2:00, etc. with JS
Legends [ ] string
2018-01-03 07:46:18 +00:00
}
2018-01-08 08:53:51 +00:00
type PanelAnalyticsItem struct {
Time int64
Count int64
}
2018-01-03 07:46:18 +00:00
type PanelAnalyticsPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2019-02-23 06:29:19 +00:00
Graph PanelTimeGraph
ViewItems [ ] PanelAnalyticsItem
TimeRange string
2019-02-24 01:29:06 +00:00
Unit string
TimeType string
2018-01-03 07:46:18 +00:00
}
2018-01-04 10:23:33 +00:00
type PanelAnalyticsRoutesItem struct {
Route string
Count int
}
type PanelAnalyticsRoutesPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
ItemList [ ] PanelAnalyticsRoutesItem
2019-02-24 01:29:06 +00:00
Graph PanelTimeGraph
2018-06-06 00:21:22 +00:00
TimeRange string
2018-01-05 22:47:13 +00:00
}
2018-01-09 07:39:29 +00:00
2019-02-24 01:29:06 +00:00
// TODO: Rename the fields as this structure is being used in a generic way now
2018-01-09 07:39:29 +00:00
type PanelAnalyticsAgentsItem struct {
2018-02-04 08:15:20 +00:00
Agent string
FriendlyAgent string
Count int
2018-01-09 07:39:29 +00:00
}
type PanelAnalyticsAgentsPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
ItemList [ ] PanelAnalyticsAgentsItem
TimeRange string
2018-01-09 07:39:29 +00:00
}
2018-01-05 22:47:13 +00:00
type PanelAnalyticsRoutePage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2019-02-23 06:29:19 +00:00
Route string
Graph PanelTimeGraph
ViewItems [ ] PanelAnalyticsItem
TimeRange string
2018-01-04 10:23:33 +00:00
}
2018-01-10 03:32:48 +00:00
type PanelAnalyticsAgentPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2018-02-15 13:15:27 +00:00
Agent string
2018-02-04 08:15:20 +00:00
FriendlyAgent string
2019-02-23 06:29:19 +00:00
Graph PanelTimeGraph
2018-02-15 13:15:27 +00:00
TimeRange string
2018-01-10 03:32:48 +00:00
}
2019-02-23 06:29:19 +00:00
type PanelAnalyticsDuoPage struct {
* BasePanelPage
ItemList [ ] PanelAnalyticsAgentsItem
Graph PanelTimeGraph
TimeRange string
}
2017-11-10 03:33:11 +00:00
type PanelThemesPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2017-12-01 02:04:29 +00:00
PrimaryThemes [ ] * Theme
VariantThemes [ ] * Theme
2017-11-10 03:33:11 +00:00
}
2018-05-11 05:41:51 +00:00
type PanelMenuListItem struct {
2018-05-15 05:59:52 +00:00
Name string
2018-05-11 05:41:51 +00:00
ID int
ItemCount int
}
type PanelMenuListPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2018-05-11 05:41:51 +00:00
ItemList [ ] PanelMenuListItem
}
2019-01-21 12:27:59 +00:00
type PanelWidgetListPage struct {
* BasePanelPage
Docks map [ string ] [ ] WidgetEdit
BlankWidget WidgetEdit
}
2018-05-11 05:41:51 +00:00
type PanelMenuPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2018-05-13 06:56:59 +00:00
MenuID int
2018-05-11 05:41:51 +00:00
ItemList [ ] MenuItem
}
type PanelMenuItemPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
Item MenuItem
2018-05-11 05:41:51 +00:00
}
2017-11-10 03:33:11 +00:00
type PanelUserPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2018-05-27 09:36:35 +00:00
ItemList [ ] * User
2018-04-22 12:33:56 +00:00
Paginator
2017-11-10 03:33:11 +00:00
}
type PanelGroupPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
ItemList [ ] GroupAdmin
2018-04-22 12:33:56 +00:00
Paginator
2017-11-10 03:33:11 +00:00
}
type PanelEditGroupPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2017-11-10 03:33:11 +00:00
ID int
Name string
Tag string
Rank string
DisableRank bool
}
type GroupForumPermPreset struct {
2018-11-29 07:27:17 +00:00
Group * Group
Preset string
DefaultPreset bool
2017-11-10 03:33:11 +00:00
}
type PanelEditForumPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
ID int
Name string
Desc string
Active bool
Preset string
Groups [ ] GroupForumPermPreset
2017-11-10 03:33:11 +00:00
}
type NameLangToggle struct {
Name string
LangStr string
Toggle bool
}
2018-01-10 03:32:48 +00:00
type PanelEditForumGroupPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
ForumID int
GroupID int
Name string
Desc string
Active bool
Preset string
Perms [ ] NameLangToggle
2018-01-10 03:32:48 +00:00
}
2017-11-10 03:33:11 +00:00
type PanelEditGroupPermsPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
2017-11-10 03:33:11 +00:00
ID int
Name string
LocalPerms [ ] NameLangToggle
GlobalPerms [ ] NameLangToggle
}
2017-11-11 04:06:16 +00:00
type BackupItem struct {
2017-11-10 03:33:11 +00:00
SQLURL string
// TODO: Add an easier to parse format here for Gosora to be able to more easily reimport portions of the dump and to strip unnecessary data (e.g. table defs and parsed post data)
Timestamp time . Time
}
type PanelBackupPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
Backups [ ] BackupItem
2017-11-10 03:33:11 +00:00
}
2018-05-16 10:46:14 +00:00
type PageLogItem struct {
2017-11-10 03:33:11 +00:00
Action template . HTML
IPAddress string
DoneAt string
}
type PanelLogsPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
Logs [ ] PageLogItem
2018-05-16 10:46:14 +00:00
Paginator
}
type PageRegLogItem struct {
RegLogItem
ParsedReason string
}
type PanelRegLogsPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
Logs [ ] PageRegLogItem
2018-04-22 12:33:56 +00:00
Paginator
2017-11-10 03:33:11 +00:00
}
type PanelDebugPage struct {
2018-06-06 00:21:22 +00:00
* BasePanelPage
GoVersion string
DBVersion string
Uptime string
2018-08-16 05:27:26 +00:00
2018-06-06 00:21:22 +00:00
OpenConns int
DBAdapter string
2018-08-16 05:27:26 +00:00
Goroutines int
CPUs int
MemStats runtime . MemStats
2017-11-10 03:33:11 +00:00
}
type PageSimple struct {
Title string
Something interface { }
}
type AreYouSure struct {
URL string
Message string
}
// TODO: Write a test for this
2018-06-06 00:21:22 +00:00
func DefaultHeader ( w http . ResponseWriter , user User ) * Header {
return & Header { Site : Site , Theme : Themes [ fallbackTheme ] , CurrentUser : user , Writer : w }
2017-11-10 03:33:11 +00:00
}
2019-01-21 12:27:59 +00:00
func SimpleDefaultHeader ( w http . ResponseWriter ) * Header {
return & Header { Site : Site , Theme : Themes [ fallbackTheme ] , CurrentUser : GuestUser , Writer : w }
}