modules.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. package modules
  2. import (
  3. "fmt"
  4. "html"
  5. "html/template"
  6. "net/http"
  7. "reflect"
  8. "sort"
  9. "strings"
  10. "time"
  11. "golang-fave/engine/assets"
  12. "golang-fave/engine/consts"
  13. "golang-fave/engine/utils"
  14. "golang-fave/engine/wrapper"
  15. )
  16. type MISub struct {
  17. Mount string
  18. Name string
  19. Icon string
  20. Show bool
  21. Sep bool
  22. }
  23. type MInfo struct {
  24. Id string
  25. Mount string
  26. Name string
  27. Order int
  28. System bool
  29. Icon string
  30. Sub *[]MISub
  31. }
  32. type Module struct {
  33. Info MInfo
  34. Front func(wrap *wrapper.Wrapper)
  35. Back func(wrap *wrapper.Wrapper) (string, string, string)
  36. }
  37. type AInfo struct {
  38. Id string
  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. }
  108. }
  109. // Selected module
  110. if !backend {
  111. if len(wrap.UrlArgs) <= 0 {
  112. if (*wrap.Config).Engine.MainModule > 0 {
  113. if (*wrap.Config).Engine.MainModule == 1 {
  114. if m, ok := this.mods["blog"]; ok {
  115. mod = m
  116. modCurr = "blog"
  117. }
  118. } else if (*wrap.Config).Engine.MainModule == 2 {
  119. if m, ok := this.mods["shop"]; ok {
  120. mod = m
  121. modCurr = "shop"
  122. }
  123. }
  124. }
  125. }
  126. }
  127. return mod, modCurr
  128. }
  129. func (this *Modules) getModulesList(wrap *wrapper.Wrapper, sys bool, all bool) []*MInfo {
  130. list := make([]*MInfo, 0)
  131. for _, mod := range this.mods {
  132. if mod.Back != nil {
  133. if mod.Info.System == sys || all {
  134. list = append(list, &mod.Info)
  135. }
  136. }
  137. }
  138. sort.Slice(list, func(i, j int) bool {
  139. return list[i].Order < list[j].Order
  140. })
  141. return list
  142. }
  143. func (this *Modules) getSidebarModuleSubMenu(wrap *wrapper.Wrapper, mod *MInfo) string {
  144. html := ``
  145. if mod.Sub != nil {
  146. for _, item := range *mod.Sub {
  147. if item.Show {
  148. if !item.Sep {
  149. class := ""
  150. 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]) {
  151. class = " active"
  152. }
  153. icon := item.Icon
  154. if icon == "" {
  155. icon = assets.SysSvgIconGear
  156. }
  157. href := "/cp/" + mod.Mount + "/" + item.Mount + "/"
  158. if mod.Mount == "index" && item.Mount == "default" {
  159. href = "/cp/"
  160. } else if item.Mount == "default" {
  161. href = "/cp/" + mod.Mount + "/"
  162. }
  163. html += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + item.Name + `</a></li>`
  164. } else {
  165. html += `<li class="nav-separator"></li>`
  166. }
  167. }
  168. }
  169. if html != "" {
  170. html = `<ul class="nav flex-column">` + html + `</ul>`
  171. }
  172. }
  173. return html
  174. }
  175. func (this *Modules) getNavMenuModules(wrap *wrapper.Wrapper, sys bool) string {
  176. html := ``
  177. list := this.getModulesList(wrap, sys, false)
  178. for _, mod := range list {
  179. class := ""
  180. if mod.Mount == wrap.CurrModule {
  181. class += " active"
  182. }
  183. if mod.Mount == "blog" && (*wrap.Config).Modules.Enabled.Blog == 0 {
  184. class += " disabled"
  185. } else if mod.Mount == "shop" && (*wrap.Config).Modules.Enabled.Shop == 0 {
  186. class += " disabled"
  187. }
  188. href := `/cp/` + mod.Mount + `/`
  189. if mod.Mount == "index" {
  190. href = `/cp/`
  191. }
  192. if !(sys && (mod.Mount == "api")) {
  193. html += `<a class="dropdown-item` + class + `" href="` + href + `">` + mod.Name + `</a>`
  194. }
  195. }
  196. return html
  197. }
  198. func (this *Modules) getSidebarModules(wrap *wrapper.Wrapper) string {
  199. html_def := ""
  200. html_sys := ""
  201. list := this.getModulesList(wrap, false, true)
  202. for _, mod := range list {
  203. class := ""
  204. submenu := ""
  205. if mod.Mount == wrap.CurrModule {
  206. class += " active"
  207. submenu = this.getSidebarModuleSubMenu(wrap, mod)
  208. }
  209. if mod.Mount == "blog" && (*wrap.Config).Modules.Enabled.Blog == 0 {
  210. class += " disabled"
  211. } else if mod.Mount == "shop" && (*wrap.Config).Modules.Enabled.Shop == 0 {
  212. class += " disabled"
  213. }
  214. icon := mod.Icon
  215. if icon == "" {
  216. icon = assets.SysSvgIconGear
  217. }
  218. href := "/cp/" + mod.Mount + "/"
  219. if mod.Mount == "index" {
  220. href = "/cp/"
  221. }
  222. if !mod.System {
  223. html_def += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
  224. } else {
  225. if !(mod.Mount == "api") {
  226. html_sys += `<li class="nav-item` + class + `"><a class="nav-link" href="` + href + `">` + icon + mod.Name + `</a>` + submenu + `</li>`
  227. }
  228. }
  229. }
  230. if html_def != "" {
  231. html_def = `<ul class="nav flex-column">` + html_def + `</ul>`
  232. }
  233. if html_sys != "" {
  234. html_sys = `<ul class="nav flex-column">` + html_sys + `</ul>`
  235. }
  236. if html_def != "" && html_sys != "" {
  237. html_sys = `<div class="dropdown-divider"></div>` + html_sys
  238. }
  239. return html_def + html_sys
  240. }
  241. func (this *Modules) getBreadCrumbs(wrap *wrapper.Wrapper, data *[]consts.BreadCrumb) string {
  242. res := `<nav aria-label="breadcrumb">`
  243. res += `<ol class="breadcrumb">`
  244. if this.mods[wrap.CurrModule].Info.Mount == "index" {
  245. res += `<li class="breadcrumb-item"><a href="/cp/">` + html.EscapeString(this.mods[wrap.CurrModule].Info.Name) + `</a></li>`
  246. } else {
  247. res += `<li class="breadcrumb-item"><a href="/cp/` + this.mods[wrap.CurrModule].Info.Mount + `/">` + html.EscapeString(this.mods[wrap.CurrModule].Info.Name) + `</a></li>`
  248. }
  249. for _, item := range *data {
  250. if item.Link == "" {
  251. res += `<li class="breadcrumb-item active" aria-current="page">` + html.EscapeString(item.Name) + `</li>`
  252. } else {
  253. res += `<li class="breadcrumb-item"><a href="` + item.Link + `">` + html.EscapeString(item.Name) + `</a></li>`
  254. }
  255. }
  256. res += `</ol>`
  257. res += `</nav>`
  258. return res
  259. }
  260. func New() *Modules {
  261. m := Modules{
  262. mods: map[string]*Module{},
  263. acts: map[string]*Action{},
  264. }
  265. m.load()
  266. return &m
  267. }
  268. func (this *Modules) XXXActionHeaders(wrap *wrapper.Wrapper, status int) {
  269. wrap.W.WriteHeader(status)
  270. wrap.W.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
  271. wrap.W.Header().Set("Content-Type", "text/html; charset=utf-8")
  272. }
  273. func (this *Modules) XXXActionFire(wrap *wrapper.Wrapper) bool {
  274. if wrap.R.Method == "POST" {
  275. if err := wrap.R.ParseForm(); err == nil {
  276. name := wrap.R.FormValue("action")
  277. if name == "" {
  278. wrap.R.ParseMultipartForm(32 << 20)
  279. name = wrap.R.FormValue("action")
  280. }
  281. if name != "" {
  282. if act, ok := this.acts[name]; ok {
  283. // Check for MySQL connection
  284. if name != "index-mysql-setup" {
  285. if err := wrap.UseDatabase(); err != nil {
  286. this.XXXActionHeaders(wrap, http.StatusNotFound)
  287. wrap.MsgError(err.Error())
  288. return true
  289. }
  290. if act.Info.WantUser || act.Info.WantAdmin {
  291. if !wrap.LoadSessionUser() {
  292. this.XXXActionHeaders(wrap, http.StatusNotFound)
  293. wrap.MsgError(`You must be loginned to run this action`)
  294. return true
  295. }
  296. if wrap.User.A_active <= 0 {
  297. if !wrap.LoadSessionUser() {
  298. this.XXXActionHeaders(wrap, http.StatusNotFound)
  299. wrap.MsgError(`You do not have rights to run this action`)
  300. return true
  301. }
  302. }
  303. }
  304. if act.Info.WantAdmin && wrap.User.A_admin <= 0 {
  305. if !wrap.LoadSessionUser() {
  306. this.XXXActionHeaders(wrap, http.StatusNotFound)
  307. wrap.MsgError(`You do not have rights to run this action`)
  308. return true
  309. }
  310. }
  311. }
  312. this.XXXActionHeaders(wrap, http.StatusOK)
  313. act.Act(wrap)
  314. return true
  315. } else {
  316. this.XXXActionHeaders(wrap, http.StatusNotFound)
  317. wrap.MsgError(`This action is not implemented`)
  318. return true
  319. }
  320. }
  321. }
  322. }
  323. return false
  324. }
  325. func (this *Modules) XXXFrontEnd(wrap *wrapper.Wrapper) bool {
  326. mod, cm := this.getCurrentModule(wrap, false)
  327. if mod == nil {
  328. return false
  329. }
  330. // Index module, if module disabled
  331. if mod.Info.Mount == "blog" || mod.Info.Mount == "shop" {
  332. if (*wrap.Config).Modules.Enabled.Blog == 0 || (*wrap.Config).Modules.Enabled.Shop == 0 {
  333. if m, ok := this.mods["index"]; ok {
  334. if mod.Info.Mount == "blog" && (*wrap.Config).Modules.Enabled.Blog == 0 {
  335. mod = m
  336. cm = "index"
  337. } else if mod.Info.Mount == "shop" && (*wrap.Config).Modules.Enabled.Shop == 0 {
  338. mod = m
  339. cm = "index"
  340. }
  341. }
  342. }
  343. }
  344. wrap.CurrModule = cm
  345. if mod.Front != nil {
  346. start := time.Now()
  347. mod.Front(wrap)
  348. if !(mod.Info.Mount == "api" || (mod.Info.Mount == "shop" && len(wrap.UrlArgs) >= 3 && wrap.UrlArgs[1] == "basket")) {
  349. wrap.W.Write([]byte(fmt.Sprintf("<!-- %.3f ms -->", time.Now().Sub(start).Seconds())))
  350. }
  351. return true
  352. }
  353. return false
  354. }
  355. func (this *Modules) XXXBackEnd(wrap *wrapper.Wrapper) bool {
  356. mod, cm := this.getCurrentModule(wrap, true)
  357. if mod != nil {
  358. wrap.CurrModule = cm
  359. if len(wrap.UrlArgs) >= 2 && wrap.UrlArgs[1] != "" {
  360. wrap.CurrSubModule = wrap.UrlArgs[1]
  361. }
  362. // Search for sub module mount
  363. found := false
  364. submount := "default"
  365. if wrap.CurrSubModule != "" {
  366. submount = wrap.CurrSubModule
  367. }
  368. for _, item := range *mod.Info.Sub {
  369. if item.Mount == submount {
  370. found = true
  371. break
  372. }
  373. }
  374. // Display standart 404 error page
  375. if !found {
  376. return found
  377. }
  378. // Call module function
  379. if mod.Back != nil {
  380. sidebar_left, content, sidebar_right := mod.Back(wrap)
  381. // Display standart 404 error page
  382. if sidebar_left == "" && content == "" && sidebar_right == "" {
  383. return false
  384. }
  385. // Prepare CP page
  386. body_class := "cp"
  387. if sidebar_left != "" {
  388. body_class = body_class + " cp-sidebar-left"
  389. }
  390. if content == "" {
  391. body_class = body_class + " cp-404"
  392. }
  393. if sidebar_right != "" {
  394. body_class = body_class + " cp-sidebar-right"
  395. }
  396. wrap.RenderBackEnd(assets.TmplCpBase, consts.TmplDataCpBase{
  397. Title: wrap.CurrHost + " - Fave " + consts.ServerVersion,
  398. Caption: "Fave " + consts.ServerVersion,
  399. BodyClasses: body_class,
  400. UserId: wrap.User.A_id,
  401. UserFirstName: utils.JavaScriptVarValue(wrap.User.A_first_name),
  402. UserLastName: utils.JavaScriptVarValue(wrap.User.A_last_name),
  403. UserEmail: utils.JavaScriptVarValue(wrap.User.A_email),
  404. UserAvatarLink: "https://s.gravatar.com/avatar/" + utils.GetMd5(wrap.User.A_email) + "?s=80&r=g",
  405. NavBarModules: template.HTML(this.getNavMenuModules(wrap, false)),
  406. NavBarModulesSys: template.HTML(this.getNavMenuModules(wrap, true)),
  407. ModuleCurrentAlias: wrap.CurrModule,
  408. SidebarLeft: template.HTML(sidebar_left),
  409. Content: template.HTML(content),
  410. SidebarRight: template.HTML(sidebar_right),
  411. })
  412. return true
  413. }
  414. }
  415. return false
  416. }