module_index.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. package modules
  2. import (
  3. "database/sql"
  4. _ "github.com/go-sql-driver/mysql"
  5. "fmt"
  6. "html"
  7. "net/http"
  8. "os"
  9. "strconv"
  10. "golang-fave/assets"
  11. "golang-fave/consts"
  12. "golang-fave/engine/builder"
  13. "golang-fave/engine/fetdata"
  14. "golang-fave/engine/wrapper"
  15. "golang-fave/utils"
  16. )
  17. func (this *Modules) RegisterModule_Index() *Module {
  18. return this.newModule(MInfo{
  19. WantDB: true,
  20. Mount: "index",
  21. Name: "Pages",
  22. Order: 0,
  23. Icon: assets.SysSvgIconPage,
  24. Sub: &[]MISub{
  25. {Mount: "default", Name: "List of pages", Show: true, Icon: assets.SysSvgIconList},
  26. {Mount: "add", Name: "Add new page", Show: true, Icon: assets.SysSvgIconPlus},
  27. {Mount: "modify", Name: "Modify page", Show: false},
  28. },
  29. }, func(wrap *wrapper.Wrapper) {
  30. // Front-end
  31. row := &utils.MySql_page{}
  32. err := wrap.DB.QueryRow(`
  33. SELECT
  34. id,
  35. user,
  36. name,
  37. alias,
  38. content,
  39. meta_title,
  40. meta_keywords,
  41. meta_description,
  42. UNIX_TIMESTAMP(datetime) as datetime,
  43. active
  44. FROM
  45. pages
  46. WHERE
  47. active = 1 and
  48. alias = ?
  49. LIMIT 1;`,
  50. wrap.R.URL.Path,
  51. ).Scan(
  52. &row.A_id,
  53. &row.A_user,
  54. &row.A_name,
  55. &row.A_alias,
  56. &row.A_content,
  57. &row.A_meta_title,
  58. &row.A_meta_keywords,
  59. &row.A_meta_description,
  60. &row.A_datetime,
  61. &row.A_active,
  62. )
  63. if err != nil && err != sql.ErrNoRows {
  64. // System error 500
  65. utils.SystemErrorPageEngine(wrap.W, err)
  66. return
  67. } else if err == sql.ErrNoRows {
  68. // User error 404 page
  69. wrap.W.WriteHeader(http.StatusNotFound)
  70. wrap.RenderFrontEnd("404", fetdata.New(wrap, nil, true))
  71. return
  72. }
  73. // Replace title with page name
  74. if row.A_meta_title == "" {
  75. row.A_meta_title = row.A_name
  76. }
  77. // Which template
  78. tmpl_name := "index"
  79. if wrap.R.URL.Path != "/" {
  80. tmpl_name = "page"
  81. }
  82. // Render template
  83. wrap.RenderFrontEnd(tmpl_name, fetdata.New(wrap, row, false))
  84. }, func(wrap *wrapper.Wrapper) (string, string, string) {
  85. content := ""
  86. sidebar := ""
  87. if wrap.CurrSubModule == "" || wrap.CurrSubModule == "default" {
  88. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  89. {Name: "List of pages"},
  90. })
  91. content += builder.DataTable(wrap, "pages", "id", "DESC", &[]builder.DataTableRow{
  92. {
  93. DBField: "id",
  94. },
  95. {
  96. DBField: "name",
  97. NameInTable: "Page / Alias",
  98. CallBack: func(values *[]string) string {
  99. name := `<a href="/cp/` + wrap.CurrModule + `/modify/` + (*values)[0] + `/">` + html.EscapeString((*values)[1]) + `</a>`
  100. alias := html.EscapeString((*values)[2])
  101. return `<div>` + name + `</div><div><small>` + alias + `</small></div>`
  102. },
  103. },
  104. {
  105. DBField: "alias",
  106. },
  107. {
  108. DBField: "datetime",
  109. DBExp: "UNIX_TIMESTAMP(`datetime`)",
  110. NameInTable: "Date / Time",
  111. Classes: "d-none d-md-table-cell",
  112. CallBack: func(values *[]string) string {
  113. t := int64(utils.StrToInt((*values)[3]))
  114. return `<div>` + utils.UnixTimestampToFormat(t, "02.01.2006") + `</div>` +
  115. `<div><small>` + utils.UnixTimestampToFormat(t, "15:04:05") + `</small></div>`
  116. },
  117. },
  118. {
  119. DBField: "active",
  120. NameInTable: "Active",
  121. Classes: "d-none d-sm-table-cell",
  122. CallBack: func(values *[]string) string {
  123. return builder.CheckBox(utils.StrToInt((*values)[4]))
  124. },
  125. },
  126. }, func(values *[]string) string {
  127. return builder.DataTableAction(&[]builder.DataTableActionRow{
  128. {
  129. Icon: assets.SysSvgIconView,
  130. Href: (*values)[2],
  131. Hint: "View",
  132. Target: "_blank",
  133. },
  134. {
  135. Icon: assets.SysSvgIconEdit,
  136. Href: "/cp/" + wrap.CurrModule + "/modify/" + (*values)[0] + "/",
  137. Hint: "Edit",
  138. },
  139. {
  140. Icon: assets.SysSvgIconRemove,
  141. Href: "javascript:fave.ActionDataTableDelete(this,'index-delete','" +
  142. (*values)[0] + "','Are you sure want to delete page?');",
  143. Hint: "Delete",
  144. },
  145. })
  146. }, "/cp/"+wrap.CurrModule+"/")
  147. } else if wrap.CurrSubModule == "add" || wrap.CurrSubModule == "modify" {
  148. if wrap.CurrSubModule == "add" {
  149. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  150. {Name: "Add new page"},
  151. })
  152. } else {
  153. content += this.getBreadCrumbs(wrap, &[]consts.BreadCrumb{
  154. {Name: "Modify page"},
  155. })
  156. }
  157. data := utils.MySql_page{
  158. A_id: 0,
  159. A_user: 0,
  160. A_name: "",
  161. A_alias: "",
  162. A_content: "",
  163. A_meta_title: "",
  164. A_meta_keywords: "",
  165. A_meta_description: "",
  166. A_datetime: 0,
  167. A_active: 0,
  168. }
  169. if wrap.CurrSubModule == "modify" {
  170. if len(wrap.UrlArgs) != 3 {
  171. return "", "", ""
  172. }
  173. if !utils.IsNumeric(wrap.UrlArgs[2]) {
  174. return "", "", ""
  175. }
  176. err := wrap.DB.QueryRow(`
  177. SELECT
  178. id,
  179. user,
  180. name,
  181. alias,
  182. content,
  183. meta_title,
  184. meta_keywords,
  185. meta_description,
  186. active
  187. FROM
  188. pages
  189. WHERE
  190. id = ?
  191. LIMIT 1;`,
  192. utils.StrToInt(wrap.UrlArgs[2]),
  193. ).Scan(
  194. &data.A_id,
  195. &data.A_user,
  196. &data.A_name,
  197. &data.A_alias,
  198. &data.A_content,
  199. &data.A_meta_title,
  200. &data.A_meta_keywords,
  201. &data.A_meta_description,
  202. &data.A_active,
  203. )
  204. if err != nil {
  205. return "", "", ""
  206. }
  207. }
  208. btn_caption := "Add"
  209. if wrap.CurrSubModule == "modify" {
  210. btn_caption = "Save"
  211. }
  212. content += builder.DataForm(wrap, []builder.DataFormField{
  213. {
  214. Kind: builder.DFKHidden,
  215. Name: "action",
  216. Value: "index-modify",
  217. },
  218. {
  219. Kind: builder.DFKHidden,
  220. Name: "id",
  221. Value: utils.IntToStr(data.A_id),
  222. },
  223. {
  224. Kind: builder.DFKText,
  225. Caption: "Page name",
  226. Name: "name",
  227. Value: data.A_name,
  228. },
  229. {
  230. Kind: builder.DFKText,
  231. Caption: "Page alias",
  232. Name: "alias",
  233. Value: data.A_alias,
  234. Hint: "Example: /about-us/ or /about-us.html",
  235. },
  236. {
  237. Kind: builder.DFKTextArea,
  238. Caption: "Page content",
  239. Name: "content",
  240. Value: data.A_content,
  241. },
  242. {
  243. Kind: builder.DFKText,
  244. Caption: "Meta title",
  245. Name: "meta_title",
  246. Value: data.A_meta_title,
  247. },
  248. {
  249. Kind: builder.DFKText,
  250. Caption: "Meta keywords",
  251. Name: "meta_keywords",
  252. Value: data.A_meta_keywords,
  253. },
  254. {
  255. Kind: builder.DFKTextArea,
  256. Caption: "Meta description",
  257. Name: "meta_description",
  258. Value: data.A_meta_description,
  259. },
  260. {
  261. Kind: builder.DFKCheckBox,
  262. Caption: "Active",
  263. Name: "active",
  264. Value: utils.IntToStr(data.A_active),
  265. },
  266. {
  267. Kind: builder.DFKMessage,
  268. },
  269. {
  270. Kind: builder.DFKSubmit,
  271. Value: btn_caption,
  272. Target: "add-edit-button",
  273. },
  274. })
  275. if wrap.CurrSubModule == "add" {
  276. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  277. } else {
  278. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  279. }
  280. }
  281. return this.getSidebarModules(wrap), content, sidebar
  282. })
  283. }
  284. func (this *Modules) RegisterAction_IndexModify() *Action {
  285. return this.newAction(AInfo{
  286. WantDB: true,
  287. Mount: "index-modify",
  288. WantAdmin: true,
  289. }, func(wrap *wrapper.Wrapper) {
  290. pf_id := wrap.R.FormValue("id")
  291. pf_name := wrap.R.FormValue("name")
  292. pf_alias := wrap.R.FormValue("alias")
  293. pf_content := wrap.R.FormValue("content")
  294. pf_meta_title := wrap.R.FormValue("meta_title")
  295. pf_meta_keywords := wrap.R.FormValue("meta_keywords")
  296. pf_meta_description := wrap.R.FormValue("meta_description")
  297. pf_active := wrap.R.FormValue("active")
  298. if pf_active == "" {
  299. pf_active = "0"
  300. }
  301. if !utils.IsNumeric(pf_id) {
  302. wrap.MsgError(`Inner system error`)
  303. return
  304. }
  305. if pf_name == "" {
  306. wrap.MsgError(`Please specify page name`)
  307. return
  308. }
  309. if pf_alias == "" {
  310. pf_alias = utils.GenerateAlias(pf_name)
  311. }
  312. if !utils.IsValidAlias(pf_alias) {
  313. wrap.MsgError(`Please specify correct page alias`)
  314. return
  315. }
  316. if pf_id == "0" {
  317. // Add new page
  318. _, err := wrap.DB.Query(
  319. `INSERT INTO pages SET
  320. user = ?,
  321. name = ?,
  322. alias = ?,
  323. content = ?,
  324. meta_title = ?,
  325. meta_keywords = ?,
  326. meta_description = ?,
  327. datetime = ?,
  328. active = ?
  329. ;`,
  330. wrap.User.A_id,
  331. pf_name,
  332. pf_alias,
  333. pf_content,
  334. pf_meta_title,
  335. pf_meta_keywords,
  336. pf_meta_description,
  337. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  338. pf_active,
  339. )
  340. if err != nil {
  341. wrap.MsgError(err.Error())
  342. return
  343. }
  344. wrap.Write(`window.location='/cp/';`)
  345. } else {
  346. // Update page
  347. _, err := wrap.DB.Query(
  348. `UPDATE pages SET
  349. name = ?,
  350. alias = ?,
  351. content = ?,
  352. meta_title = ?,
  353. meta_keywords = ?,
  354. meta_description = ?,
  355. active = ?
  356. WHERE
  357. id = ?
  358. ;`,
  359. pf_name,
  360. pf_alias,
  361. pf_content,
  362. pf_meta_title,
  363. pf_meta_keywords,
  364. pf_meta_description,
  365. pf_active,
  366. utils.StrToInt(pf_id),
  367. )
  368. if err != nil {
  369. wrap.MsgError(err.Error())
  370. return
  371. }
  372. wrap.Write(`window.location='/cp/index/modify/` + pf_id + `/';`)
  373. }
  374. })
  375. }
  376. func (this *Modules) RegisterAction_IndexDelete() *Action {
  377. return this.newAction(AInfo{
  378. WantDB: true,
  379. Mount: "index-delete",
  380. WantAdmin: true,
  381. }, func(wrap *wrapper.Wrapper) {
  382. pf_id := wrap.R.FormValue("id")
  383. if !utils.IsNumeric(pf_id) {
  384. wrap.MsgError(`Inner system error`)
  385. return
  386. }
  387. // Delete page
  388. _, err := wrap.DB.Query(
  389. `DELETE FROM pages WHERE id = ?;`,
  390. utils.StrToInt(pf_id),
  391. )
  392. if err != nil {
  393. wrap.MsgError(err.Error())
  394. return
  395. }
  396. // Reload current page
  397. wrap.Write(`window.location.reload(false);`)
  398. })
  399. }
  400. func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
  401. return this.newAction(AInfo{
  402. WantDB: false,
  403. Mount: "index-mysql-setup",
  404. }, func(wrap *wrapper.Wrapper) {
  405. pf_host := wrap.R.FormValue("host")
  406. pf_port := wrap.R.FormValue("port")
  407. pf_name := wrap.R.FormValue("name")
  408. pf_user := wrap.R.FormValue("user")
  409. pf_password := wrap.R.FormValue("password")
  410. if pf_host == "" {
  411. wrap.MsgError(`Please specify host for MySQL connection`)
  412. return
  413. }
  414. if pf_port == "" {
  415. wrap.MsgError(`Please specify host port for MySQL connection`)
  416. return
  417. }
  418. if _, err := strconv.Atoi(pf_port); err != nil {
  419. wrap.MsgError(`MySQL host port must be integer number`)
  420. return
  421. }
  422. if pf_name == "" {
  423. wrap.MsgError(`Please specify MySQL database name`)
  424. return
  425. }
  426. if pf_user == "" {
  427. wrap.MsgError(`Please specify MySQL user`)
  428. return
  429. }
  430. // Try connect to mysql
  431. db, err := sql.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":"+pf_port+")/"+pf_name)
  432. if err != nil {
  433. wrap.MsgError(err.Error())
  434. return
  435. }
  436. defer db.Close()
  437. err = db.Ping()
  438. if err != nil {
  439. wrap.MsgError(err.Error())
  440. return
  441. }
  442. // Try to install all tables
  443. _, err = db.Query(fmt.Sprintf(
  444. `CREATE TABLE %s.users (
  445. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  446. first_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User first name',
  447. last_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User last name',
  448. email VARCHAR(64) NOT NULL COMMENT 'User email',
  449. password VARCHAR(32) NOT NULL COMMENT 'User password (MD5)',
  450. admin int(1) NOT NULL COMMENT 'Is admin user or not',
  451. active int(1) NOT NULL COMMENT 'Is active user or not',
  452. PRIMARY KEY (id)
  453. ) ENGINE = InnoDB;`,
  454. pf_name))
  455. if err != nil {
  456. wrap.MsgError(err.Error())
  457. return
  458. }
  459. _, err = db.Query(fmt.Sprintf(
  460. `ALTER TABLE %s.users ADD UNIQUE KEY email (email);`,
  461. pf_name))
  462. if err != nil {
  463. wrap.MsgError(err.Error())
  464. return
  465. }
  466. _, err = db.Query(fmt.Sprintf(
  467. `CREATE TABLE %s.pages (
  468. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  469. user int(11) NOT NULL COMMENT 'User id',
  470. name varchar(255) NOT NULL COMMENT 'Page name',
  471. alias varchar(255) NOT NULL COMMENT 'Page url part',
  472. content text NOT NULL COMMENT 'Page content',
  473. meta_title varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta title',
  474. meta_keywords varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords',
  475. meta_description varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description',
  476. datetime datetime NOT NULL COMMENT 'Creation date/time',
  477. active int(1) NOT NULL COMMENT 'Is active page or not',
  478. PRIMARY KEY (id)
  479. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  480. pf_name))
  481. if err != nil {
  482. wrap.MsgError(err.Error())
  483. return
  484. }
  485. _, err = db.Query(fmt.Sprintf(
  486. `ALTER TABLE %s.pages ADD UNIQUE KEY alias (alias);`,
  487. pf_name))
  488. if err != nil {
  489. wrap.MsgError(err.Error())
  490. return
  491. }
  492. // Save mysql config file
  493. err = utils.MySqlConfigWrite(wrap.DConfig+string(os.PathSeparator)+"mysql.json", pf_host, pf_port, pf_name, pf_user, pf_password)
  494. if err != nil {
  495. wrap.MsgError(err.Error())
  496. return
  497. }
  498. // Reload current page
  499. wrap.Write(`window.location.reload(false);`)
  500. })
  501. }
  502. func (this *Modules) RegisterAction_IndexFirstUser() *Action {
  503. return this.newAction(AInfo{
  504. WantDB: true,
  505. Mount: "index-first-user",
  506. }, func(wrap *wrapper.Wrapper) {
  507. pf_first_name := wrap.R.FormValue("first_name")
  508. pf_last_name := wrap.R.FormValue("last_name")
  509. pf_email := wrap.R.FormValue("email")
  510. pf_password := wrap.R.FormValue("password")
  511. if pf_email == "" {
  512. wrap.MsgError(`Please specify user email`)
  513. return
  514. }
  515. if !utils.IsValidEmail(pf_email) {
  516. wrap.MsgError(`Please specify correct user email`)
  517. return
  518. }
  519. if pf_password == "" {
  520. wrap.MsgError(`Please specify user password`)
  521. return
  522. }
  523. _, err := wrap.DB.Query(
  524. `INSERT INTO users SET
  525. first_name = ?,
  526. last_name = ?,
  527. email = ?,
  528. password = MD5(?),
  529. admin = 1,
  530. active = 1
  531. ;`,
  532. pf_first_name,
  533. pf_last_name,
  534. pf_email,
  535. pf_password,
  536. )
  537. if err != nil {
  538. wrap.MsgError(err.Error())
  539. return
  540. }
  541. // Add home page
  542. _, err = wrap.DB.Query(
  543. `INSERT INTO pages SET
  544. user = ?,
  545. name = ?,
  546. alias = ?,
  547. content = ?,
  548. datetime = ?,
  549. active = ?
  550. ;`,
  551. 1,
  552. "Home",
  553. "/",
  554. "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Feugiat in ante metus dictum at tempor commodo ullamcorper a. Et malesuada fames ac turpis egestas sed tempus urna et. Euismod elementum nisi quis eleifend. Nisi porta lorem mollis aliquam ut porttitor. Ac turpis egestas maecenas pharetra convallis posuere. Nunc non blandit massa enim nec dui. Commodo elit at imperdiet dui accumsan sit amet nulla. Viverra accumsan in nisl nisi scelerisque. Dui nunc mattis enim ut tellus. Molestie ac feugiat sed lectus vestibulum mattis ullamcorper. Faucibus ornare suspendisse sed nisi lacus. Nulla facilisi morbi tempus iaculis. Ut eu sem integer vitae justo eget magna fermentum iaculis. Ullamcorper sit amet risus nullam eget felis eget nunc. Volutpat sed cras ornare arcu dui vivamus. Eget magna fermentum iaculis eu non diam.</p><p>Arcu ac tortor dignissim convallis aenean et tortor. Vitae auctor eu augue ut lectus arcu. Ac turpis egestas integer eget aliquet nibh praesent. Interdum velit euismod in pellentesque massa placerat duis. Vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras tincidunt. Nisl rhoncus mattis rhoncus urna neque viverra justo. Odio ut enim blandit volutpat. Ac auctor augue mauris augue neque gravida. Ut lectus arcu bibendum at varius vel. Porttitor leo a diam sollicitudin tempor id eu nisl nunc. Dolor sit amet consectetur adipiscing elit duis tristique. Semper quis lectus nulla at volutpat diam ut. Sapien eget mi proin sed.</p><p>Ante metus dictum at tempor commodo ullamcorper a. Facilisis mauris sit amet massa vitae. Enim neque volutpat ac tincidunt vitae. Tempus quam pellentesque nec nam aliquam sem. Mollis aliquam ut porttitor leo a diam sollicitudin. Nunc pulvinar sapien et ligula ullamcorper. Dignissim suspendisse in est ante in nibh mauris. Eget egestas purus viverra accumsan in. Vitae tempus quam pellentesque nec nam aliquam sem et. Sodales ut etiam sit amet nisl. Aliquet risus feugiat in ante. Rhoncus urna neque viverra justo nec ultrices dui sapien. Sit amet aliquam id diam maecenas ultricies. Sed odio morbi quis commodo odio aenean sed adipiscing diam.</p>",
  555. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  556. 1,
  557. )
  558. if err != nil {
  559. wrap.MsgError(err.Error())
  560. return
  561. }
  562. // Add another page
  563. _, err = wrap.DB.Query(
  564. `INSERT INTO pages SET
  565. user = ?,
  566. name = ?,
  567. alias = ?,
  568. content = ?,
  569. datetime = ?,
  570. active = ?
  571. ;`,
  572. 1,
  573. "Another",
  574. "/another/",
  575. "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Feugiat in ante metus dictum at tempor commodo ullamcorper a. Et malesuada fames ac turpis egestas sed tempus urna et. Euismod elementum nisi quis eleifend. Nisi porta lorem mollis aliquam ut porttitor. Ac turpis egestas maecenas pharetra convallis posuere. Nunc non blandit massa enim nec dui. Commodo elit at imperdiet dui accumsan sit amet nulla. Viverra accumsan in nisl nisi scelerisque. Dui nunc mattis enim ut tellus. Molestie ac feugiat sed lectus vestibulum mattis ullamcorper. Faucibus ornare suspendisse sed nisi lacus. Nulla facilisi morbi tempus iaculis. Ut eu sem integer vitae justo eget magna fermentum iaculis. Ullamcorper sit amet risus nullam eget felis eget nunc. Volutpat sed cras ornare arcu dui vivamus. Eget magna fermentum iaculis eu non diam.</p><p>Arcu ac tortor dignissim convallis aenean et tortor. Vitae auctor eu augue ut lectus arcu. Ac turpis egestas integer eget aliquet nibh praesent. Interdum velit euismod in pellentesque massa placerat duis. Vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras tincidunt. Nisl rhoncus mattis rhoncus urna neque viverra justo. Odio ut enim blandit volutpat. Ac auctor augue mauris augue neque gravida. Ut lectus arcu bibendum at varius vel. Porttitor leo a diam sollicitudin tempor id eu nisl nunc. Dolor sit amet consectetur adipiscing elit duis tristique. Semper quis lectus nulla at volutpat diam ut. Sapien eget mi proin sed.</p><p>Ante metus dictum at tempor commodo ullamcorper a. Facilisis mauris sit amet massa vitae. Enim neque volutpat ac tincidunt vitae. Tempus quam pellentesque nec nam aliquam sem. Mollis aliquam ut porttitor leo a diam sollicitudin. Nunc pulvinar sapien et ligula ullamcorper. Dignissim suspendisse in est ante in nibh mauris. Eget egestas purus viverra accumsan in. Vitae tempus quam pellentesque nec nam aliquam sem et. Sodales ut etiam sit amet nisl. Aliquet risus feugiat in ante. Rhoncus urna neque viverra justo nec ultrices dui sapien. Sit amet aliquam id diam maecenas ultricies. Sed odio morbi quis commodo odio aenean sed adipiscing diam.</p>",
  576. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  577. 1,
  578. )
  579. if err != nil {
  580. wrap.MsgError(err.Error())
  581. return
  582. }
  583. // Add about page
  584. _, err = wrap.DB.Query(
  585. `INSERT INTO pages SET
  586. user = ?,
  587. name = ?,
  588. alias = ?,
  589. content = ?,
  590. datetime = ?,
  591. active = ?
  592. ;`,
  593. 1,
  594. "About",
  595. "/about/",
  596. "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Feugiat in ante metus dictum at tempor commodo ullamcorper a. Et malesuada fames ac turpis egestas sed tempus urna et. Euismod elementum nisi quis eleifend. Nisi porta lorem mollis aliquam ut porttitor. Ac turpis egestas maecenas pharetra convallis posuere. Nunc non blandit massa enim nec dui. Commodo elit at imperdiet dui accumsan sit amet nulla. Viverra accumsan in nisl nisi scelerisque. Dui nunc mattis enim ut tellus. Molestie ac feugiat sed lectus vestibulum mattis ullamcorper. Faucibus ornare suspendisse sed nisi lacus. Nulla facilisi morbi tempus iaculis. Ut eu sem integer vitae justo eget magna fermentum iaculis. Ullamcorper sit amet risus nullam eget felis eget nunc. Volutpat sed cras ornare arcu dui vivamus. Eget magna fermentum iaculis eu non diam.</p><p>Arcu ac tortor dignissim convallis aenean et tortor. Vitae auctor eu augue ut lectus arcu. Ac turpis egestas integer eget aliquet nibh praesent. Interdum velit euismod in pellentesque massa placerat duis. Vestibulum rhoncus est pellentesque elit ullamcorper dignissim cras tincidunt. Nisl rhoncus mattis rhoncus urna neque viverra justo. Odio ut enim blandit volutpat. Ac auctor augue mauris augue neque gravida. Ut lectus arcu bibendum at varius vel. Porttitor leo a diam sollicitudin tempor id eu nisl nunc. Dolor sit amet consectetur adipiscing elit duis tristique. Semper quis lectus nulla at volutpat diam ut. Sapien eget mi proin sed.</p><p>Ante metus dictum at tempor commodo ullamcorper a. Facilisis mauris sit amet massa vitae. Enim neque volutpat ac tincidunt vitae. Tempus quam pellentesque nec nam aliquam sem. Mollis aliquam ut porttitor leo a diam sollicitudin. Nunc pulvinar sapien et ligula ullamcorper. Dignissim suspendisse in est ante in nibh mauris. Eget egestas purus viverra accumsan in. Vitae tempus quam pellentesque nec nam aliquam sem et. Sodales ut etiam sit amet nisl. Aliquet risus feugiat in ante. Rhoncus urna neque viverra justo nec ultrices dui sapien. Sit amet aliquam id diam maecenas ultricies. Sed odio morbi quis commodo odio aenean sed adipiscing diam.</p>",
  597. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  598. 1,
  599. )
  600. if err != nil {
  601. wrap.MsgError(err.Error())
  602. return
  603. }
  604. // Reload current page
  605. wrap.Write(`window.location.reload(false);`)
  606. })
  607. }
  608. func (this *Modules) RegisterAction_IndexUserSignIn() *Action {
  609. return this.newAction(AInfo{
  610. WantDB: true,
  611. Mount: "index-user-sign-in",
  612. }, func(wrap *wrapper.Wrapper) {
  613. pf_email := wrap.R.FormValue("email")
  614. pf_password := wrap.R.FormValue("password")
  615. if pf_email == "" {
  616. wrap.MsgError(`Please specify user email`)
  617. return
  618. }
  619. if !utils.IsValidEmail(pf_email) {
  620. wrap.MsgError(`Please specify correct user email`)
  621. return
  622. }
  623. if pf_password == "" {
  624. wrap.MsgError(`Please specify user password`)
  625. return
  626. }
  627. if wrap.S.GetInt("UserId", 0) > 0 {
  628. wrap.MsgError(`You already logined`)
  629. return
  630. }
  631. var user_id int
  632. err := wrap.DB.QueryRow(
  633. `SELECT
  634. id
  635. FROM
  636. users
  637. WHERE
  638. email = ? and
  639. password = MD5(?) and
  640. admin = 1 and
  641. active = 1
  642. LIMIT 1;`,
  643. pf_email,
  644. pf_password,
  645. ).Scan(
  646. &user_id,
  647. )
  648. if err != nil && err != sql.ErrNoRows {
  649. wrap.MsgError(err.Error())
  650. return
  651. }
  652. if err == sql.ErrNoRows {
  653. wrap.MsgError(`Incorrect email or password`)
  654. return
  655. }
  656. // Save to current session
  657. wrap.S.SetInt("UserId", user_id)
  658. // Reload current page
  659. wrap.Write(`window.location.reload(false);`)
  660. })
  661. }
  662. func (this *Modules) RegisterAction_IndexUserLogout() *Action {
  663. return this.newAction(AInfo{
  664. WantDB: true,
  665. Mount: "index-user-logout",
  666. WantUser: true,
  667. }, func(wrap *wrapper.Wrapper) {
  668. // Reset session var
  669. wrap.S.SetInt("UserId", 0)
  670. // Reload current page
  671. wrap.Write(`window.location.reload(false);`)
  672. })
  673. }
  674. func (this *Modules) RegisterAction_IndexUserUpdateProfile() *Action {
  675. return this.newAction(AInfo{
  676. WantDB: true,
  677. Mount: "index-user-update-profile",
  678. WantUser: true,
  679. }, func(wrap *wrapper.Wrapper) {
  680. pf_first_name := wrap.R.FormValue("first_name")
  681. pf_last_name := wrap.R.FormValue("last_name")
  682. pf_email := wrap.R.FormValue("email")
  683. pf_password := wrap.R.FormValue("password")
  684. if pf_email == "" {
  685. wrap.MsgError(`Please specify user email`)
  686. return
  687. }
  688. if !utils.IsValidEmail(pf_email) {
  689. wrap.MsgError(`Please specify correct user email`)
  690. return
  691. }
  692. if pf_password != "" {
  693. // Update with password if set
  694. _, err := wrap.DB.Query(
  695. `UPDATE users SET
  696. first_name = ?,
  697. last_name = ?,
  698. email = ?,
  699. password = MD5(?)
  700. WHERE
  701. id = ?
  702. ;`,
  703. pf_first_name,
  704. pf_last_name,
  705. pf_email,
  706. pf_password,
  707. wrap.User.A_id,
  708. )
  709. if err != nil {
  710. wrap.MsgError(err.Error())
  711. return
  712. }
  713. } else {
  714. // Update without password if not set
  715. _, err := wrap.DB.Query(
  716. `UPDATE users SET
  717. first_name = ?,
  718. last_name = ?,
  719. email = ?
  720. WHERE
  721. id = ?
  722. ;`,
  723. pf_first_name,
  724. pf_last_name,
  725. pf_email,
  726. wrap.User.A_id,
  727. )
  728. if err != nil {
  729. wrap.MsgError(err.Error())
  730. return
  731. }
  732. }
  733. // Reload current page
  734. wrap.Write(`window.location.reload(false);`)
  735. })
  736. }