modules.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. package modules
  2. import (
  3. "html"
  4. "html/template"
  5. "net/http"
  6. "reflect"
  7. "sort"
  8. "strings"
  9. "golang-fave/assets"
  10. "golang-fave/consts"
  11. "golang-fave/engine/wrapper"
  12. "golang-fave/utils"
  13. )
  14. type MISub struct {
  15. Mount string
  16. Name string
  17. Icon string
  18. Show bool
  19. Sep bool
  20. }
  21. type MInfo struct {
  22. Id string
  23. WantDB bool
  24. Mount string
  25. Name string
  26. Order int
  27. System bool
  28. Icon string
  29. Sub *[]MISub
  30. }
  31. type Module struct {
  32. Info MInfo
  33. Front func(wrap *wrapper.Wrapper)
  34. Back func(wrap *wrapper.Wrapper) (string, string, string)
  35. }
  36. type AInfo struct {
  37. Id string
  38. WantDB bool
  39. Mount string
  40. WantUser bool
  41. WantAdmin bool
  42. }
  43. type Action struct {
  44. Info AInfo
  45. Act func(wrap *wrapper.Wrapper)
  46. }
  47. type Modules struct {
  48. mods map[string]*Module
  49. acts map[string]*Action
  50. }
  51. func (this *Modules) load() {
  52. t := reflect.TypeOf(this)
  53. for i := 0; i < t.NumMethod(); i++ {
  54. m := t.Method(i)
  55. if strings.HasPrefix(m.Name, "XXX") {
  56. continue
  57. }
  58. if strings.HasPrefix(m.Name, "RegisterModule_") {
  59. id := m.Name[15:]
  60. if _, ok := reflect.TypeOf(this).MethodByName("RegisterModule_" + id); ok {
  61. result := reflect.ValueOf(this).MethodByName("RegisterModule_" + id).Call([]reflect.Value{})
  62. if len(result) >= 1 {
  63. mod := result[0].Interface().(*Module)
  64. mod.Info.Id = id
  65. this.mods[mod.Info.Mount] = mod
  66. }
  67. }
  68. }
  69. if strings.HasPrefix(m.Name, "RegisterAction_") {
  70. id := m.Name[15:]
  71. if _, ok := reflect.TypeOf(this).MethodByName("RegisterAction_" + id); ok {
  72. result := reflect.ValueOf(this).MethodByName("RegisterAction_" + id).Call([]reflect.Value{})
  73. if len(result) >= 1 {
  74. act := result[0].Interface().(*Action)
  75. act.Info.Id = id
  76. this.acts[act.Info.Mount] = act
  77. }
  78. }
  79. }
  80. }
  81. }
  82. func (this *Modules) newModule(info MInfo, ff func(wrap *wrapper.Wrapper), bf func(wrap *wrapper.Wrapper) (string, string, string)) *Module {
  83. return &Module{Info: info, Front: ff, Back: bf}
  84. }
  85. func (this *Modules) newAction(info AInfo, af func(wrap *wrapper.Wrapper)) *Action {
  86. return &Action{Info: info, Act: af}
  87. }
  88. func (this *Modules) getCurrentModule(wrap *wrapper.Wrapper, backend bool) (*Module, string) {
  89. var mod *Module = nil
  90. var modCurr string = ""
  91. // Some module
  92. if len(wrap.UrlArgs) >= 1 {
  93. if m, ok := this.mods[wrap.UrlArgs[0]]; ok {
  94. if (!backend && m.Front != nil) || (backend && m.Back != nil) {
  95. mod = m
  96. modCurr = wrap.UrlArgs[0]
  97. }
  98. }
  99. }
  100. // Default module
  101. if !backend || (backend && len(wrap.UrlArgs) <= 0) {
  102. if mod == nil {
  103. if m, ok := this.mods["index"]; ok {
  104. mod = m
  105. modCurr = "index"
  106. }
  107. if (*wrap.Config).Engine.MainModule > 0 {
  108. if (*wrap.Config).Engine.MainModule == 1 {
  109. if m, ok := this.mods["blog"]; ok {
  110. mod = m
  111. modCurr = "blog"
  112. }
  113. } else if (*wrap.Config).Engine.MainModule == 2 {
  114. if m, ok := this.mods["shop"]; ok {
  115. mod = m
  116. modCurr = "shop"
  117. }
  118. }
  119. }
  120. }
  121. }
  122. return mod, modCurr
  123. }
  124. func (this *Modules) getModulesList(wrap *wrapper.Wrapper, sys bool, all bool) []*MInfo {
  125. list := make([]*MInfo, 0)
  126. for _, mod := range this.mods {
  127. if mod.Back != nil {
  128. if mod.Info.System == sys || all {
  129. list = append(list, &mod.Info)
  130. }
  131. }
  132. }
  133. sort.Slice(list, func(i, j int) bool {
  134. return list[i].Order < list[j].Order
  135. })
  136. return list
  137. }
  138. func (this *Modules) getSidebarModuleSubMenu(wrap *wrapper.Wrapper, mod *MInfo) string {
  139. html := ``
  140. if mod.Sub != nil {
  141. for _, item := range *mod.Sub {
  142. if item.Show {
  143. if !item.Sep {
  144. class := ""
  145. if (item.Mount == "default" && len(wrap.UrlArgs) <= 1) || (len(wrap.UrlArgs) >= 2 && item.Mount == wrap.UrlArgs[1]) || (len(wrap.UrlArgs) >= 2 && item.Mount == "default" && wrap.UrlArgs[1] == "modify") || (len(wrap.UrlArgs) >= 2 && len(strings.Split(item.Mount, "-")) <= 1 && len(strings.Split(wrap.UrlArgs[1], "-")) >= 2 && strings.Split(wrap.UrlArgs[1], "-")[1] == "modify" && strings.Split(item.Mount, "-")[0] == strings.Split(wrap.UrlArgs[1], "-")[0]) {
  146. class = " active"
  147. }
  148. icon := item.Icon
  149. if icon == "" {
  150. icon = assets.SysSvgIconGear
  151. }
  152. href := "/cp/" + mod.Mount + "/" + item.Mount + "/"
  153. if mod.Mount == "index" && item.Mount == "default" {
  154. href = "/cp/"
  155. } else if item.Mount == "default" {
  156. href = "/cp/" + mod.Mount + "/"
  157. }
  158. html += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + item.Name + `</a></li>`
  159. } else {
  160. html += `<li class="nav-separator"></li>`
  161. }
  162. }
  163. }
  164. if html != "" {
  165. html = `<ul class="nav flex-column">` + html + `</ul>`
  166. }
  167. }
  168. return html
  169. }
  170. func (this *Modules) getNavMenuModules(wrap *wrapper.Wrapper, sys bool) string {
  171. html := ``
  172. list := this.getModulesList(wrap, sys, false)
  173. for _, mod := range list {
  174. class := ""
  175. if mod.Mount == wrap.CurrModule {
  176. class = " active"
  177. }
  178. href := `/cp/` + mod.Mount + `/`
  179. if mod.Mount == "index" {
  180. href = `/cp/`
  181. }
  182. if !(sys && mod.Mount == "api") {
  183. html += `<a class="dropdown-item` + class + `" href="` + href + `">` + mod.Name + `</a>`
  184. }
  185. }
  186. return html
  187. }
  188. func (this *Modules) getSidebarModules(wrap *wrapper.Wrapper) string {
  189. html_def := ""
  190. html_sys := ""
  191. list := this.getModulesList(wrap, false, true)
  192. for _, mod := range list {
  193. class := ""
  194. submenu := ""
  195. if mod.Mount == wrap.CurrModule {
  196. class = " active"
  197. submenu = this.getSidebarModuleSubMenu(wrap, mod)
  198. }
  199. icon := mod.Icon
  200. if icon == "" {
  201. icon = assets.SysSvgIconGear
  202. }
  203. href := "/cp/" + mod.Mount + "/"
  204. if mod.Mount == "index" {
  205. href = "/cp/"
  206. }
  207. if !mod.System {
  208. html_def += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
  209. } else {
  210. if mod.Mount != "api" {
  211. html_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
  212. }
  213. }
  214. }
  215. if html_def != "" {
  216. html_def = `<ul class="nav flex-column">` + html_def + `</ul>`
  217. }
  218. if html_sys != "" {
  219. html_sys = `<ul class="nav flex-column">` + html_sys + `</ul>`
  220. }
  221. if html_def != "" && html_sys != "" {
  222. html_sys = `<div class="dropdown-divider"></div>` + html_sys
  223. }
  224. return html_def + html_sys
  225. }
  226. func (this *Modules) getBreadCrumbs(wrap *wrapper.Wrapper, data *[]consts.BreadCrumb) string {
  227. res := `<nav aria-label="breadcrumb">`
  228. res += `<ol class="breadcrumb">`
  229. if this.mods[wrap.CurrModule].Info.Mount == "index" {
  230. res += `<li class="breadcrumb-item"><a href="/cp/">` + html.EscapeString(this.mods[wrap.CurrModule].Info.Name) + `</a></li>`
  231. } else {
  232. res += `<li class="breadcrumb-item"><a href="/cp/` + this.mods[wrap.CurrModule].Info.Mount + `/">` + html.EscapeString(this.mods[wrap.CurrModule].Info.Name) + `</a></li>`
  233. }
  234. for _, item := range *data {
  235. if item.Link == "" {
  236. res += `<li class="breadcrumb-item active" aria-current="page">` + html.EscapeString(item.Name) + `</li>`
  237. } else {
  238. res += `<li class="breadcrumb-item"><a href="` + item.Link + `">` + html.EscapeString(item.Name) + `</a></li>`
  239. }
  240. }
  241. res += `</ol>`
  242. res += `</nav>`
  243. return res
  244. }
  245. func New() *Modules {
  246. m := Modules{
  247. mods: map[string]*Module{},
  248. acts: map[string]*Action{},
  249. }
  250. m.load()
  251. return &m
  252. }
  253. func (this *Modules) XXXActionHeaders(wrap *wrapper.Wrapper, status int) {
  254. wrap.W.WriteHeader(status)
  255. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  256. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  257. }
  258. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  259. if wrap.R.Method == "POST" {
  260. if err := wrap.R.ParseForm(); err == nil {
  261. name := wrap.R.FormValue("action")
  262. if name == "" {
  263. wrap.R.ParseMultipartForm(32 << 20)
  264. name = wrap.R.FormValue("action")
  265. }
  266. if name != "" {
  267. if act, ok := this.acts[name]; ok {
  268. if act.Info.WantDB {
  269. err := wrap.UseDatabase()
  270. if err != nil {
  271. this.XXXActionHeaders(wrap, http.StatusNotFound)
  272. wrap.MsgError(err.Error())
  273. return true
  274. }
  275. }
  276. if act.Info.WantUser || act.Info.WantAdmin {
  277. if !wrap.LoadSessionUser() {
  278. this.XXXActionHeaders(wrap, http.StatusNotFound)
  279. wrap.MsgError(`You must be loginned to run this action`)
  280. return true
  281. }
  282. if wrap.User.A_active <= 0 {
  283. if !wrap.LoadSessionUser() {
  284. this.XXXActionHeaders(wrap, http.StatusNotFound)
  285. wrap.MsgError(`You do not have rights to run this action`)
  286. return true
  287. }
  288. }
  289. }
  290. if act.Info.WantAdmin && wrap.User.A_admin <= 0 {
  291. if !wrap.LoadSessionUser() {
  292. this.XXXActionHeaders(wrap, http.StatusNotFound)
  293. wrap.MsgError(`You do not have rights to run this action`)
  294. return true
  295. }
  296. }
  297. this.XXXActionHeaders(wrap, http.StatusOK)
  298. act.Act(wrap)
  299. return true
  300. } else {
  301. this.XXXActionHeaders(wrap, http.StatusNotFound)
  302. wrap.MsgError(`This action is not implemented`)
  303. return true
  304. }
  305. }
  306. }
  307. }
  308. return false
  309. }
  310. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  311. mod, cm := this.getCurrentModule(wrap, false)
  312. if mod != nil {
  313. wrap.CurrModule = cm
  314. if mod.Front != nil {
  315. if mod.Info.WantDB {
  316. err := wrap.UseDatabase()
  317. if err != nil {
  318. utils.SystemErrorPageEngine(wrap.W, err)
  319. return true
  320. }
  321. }
  322. mod.Front(wrap)
  323. return true
  324. }
  325. }
  326. return false
  327. }
  328. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  329. mod, cm := this.getCurrentModule(wrap, true)
  330. if mod != nil {
  331. wrap.CurrModule = cm
  332. if len(wrap.UrlArgs) >= 2 && wrap.UrlArgs[1] != "" {
  333. wrap.CurrSubModule = wrap.UrlArgs[1]
  334. }
  335. // Search for sub module mount
  336. found := false
  337. submount := "default"
  338. if wrap.CurrSubModule != "" {
  339. submount = wrap.CurrSubModule
  340. }
  341. for _, item := range *mod.Info.Sub {
  342. if item.Mount == submount {
  343. found = true
  344. break
  345. }
  346. }
  347. // Display standart 404 error page
  348. if !found {
  349. return found
  350. }
  351. // Call module function
  352. if mod.Back != nil {
  353. sidebar_left, content, sidebar_right := mod.Back(wrap)
  354. // Display standart 404 error page
  355. if sidebar_left == "" && content == "" && sidebar_right == "" {
  356. return false
  357. }
  358. // Prepare CP page
  359. body_class := "cp"
  360. if sidebar_left != "" {
  361. body_class = body_class + " cp-sidebar-left"
  362. }
  363. if content == "" {
  364. body_class = body_class + " cp-404"
  365. }
  366. if sidebar_right != "" {
  367. body_class = body_class + " cp-sidebar-right"
  368. }
  369. wrap.RenderBackEnd(assets.TmplCpBase, consts.TmplDataCpBase{
  370. Title: wrap.CurrHost + " - Fave " + consts.ServerVersion,
  371. Caption: "Fave " + consts.ServerVersion,
  372. BodyClasses: body_class,
  373. UserId: wrap.User.A_id,
  374. UserFirstName: utils.JavaScriptVarValue(wrap.User.A_first_name),
  375. UserLastName: utils.JavaScriptVarValue(wrap.User.A_last_name),
  376. UserEmail: utils.JavaScriptVarValue(wrap.User.A_email),
  377. UserAvatarLink: "https://s.gravatar.com/avatar/" + utils.GetMd5(wrap.User.A_email) + "?s=80&r=g",
  378. NavBarModules: template.HTML(this.getNavMenuModules(wrap, false)),
  379. NavBarModulesSys: template.HTML(this.getNavMenuModules(wrap, true)),
  380. ModuleCurrentAlias: wrap.CurrModule,
  381. SidebarLeft: template.HTML(sidebar_left),
  382. Content: template.HTML(content),
  383. SidebarRight: template.HTML(sidebar_right),
  384. })
  385. return true
  386. }
  387. }
  388. return false
  389. }