module_index.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. Classes: "autosize",
  242. },
  243. {
  244. Kind: builder.DFKText,
  245. Caption: "Meta title",
  246. Name: "meta_title",
  247. Value: data.A_meta_title,
  248. },
  249. {
  250. Kind: builder.DFKText,
  251. Caption: "Meta keywords",
  252. Name: "meta_keywords",
  253. Value: data.A_meta_keywords,
  254. },
  255. {
  256. Kind: builder.DFKTextArea,
  257. Caption: "Meta description",
  258. Name: "meta_description",
  259. Value: data.A_meta_description,
  260. },
  261. {
  262. Kind: builder.DFKCheckBox,
  263. Caption: "Active",
  264. Name: "active",
  265. Value: utils.IntToStr(data.A_active),
  266. },
  267. {
  268. Kind: builder.DFKMessage,
  269. },
  270. {
  271. Kind: builder.DFKSubmit,
  272. Value: btn_caption,
  273. Target: "add-edit-button",
  274. },
  275. })
  276. if wrap.CurrSubModule == "add" {
  277. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Add</button>`
  278. } else {
  279. sidebar += `<button class="btn btn-primary btn-sidebar" id="add-edit-button">Save</button>`
  280. }
  281. }
  282. return this.getSidebarModules(wrap), content, sidebar
  283. })
  284. }
  285. func (this *Modules) RegisterAction_IndexModify() *Action {
  286. return this.newAction(AInfo{
  287. WantDB: true,
  288. Mount: "index-modify",
  289. WantAdmin: true,
  290. }, func(wrap *wrapper.Wrapper) {
  291. pf_id := wrap.R.FormValue("id")
  292. pf_name := wrap.R.FormValue("name")
  293. pf_alias := wrap.R.FormValue("alias")
  294. pf_content := wrap.R.FormValue("content")
  295. pf_meta_title := wrap.R.FormValue("meta_title")
  296. pf_meta_keywords := wrap.R.FormValue("meta_keywords")
  297. pf_meta_description := wrap.R.FormValue("meta_description")
  298. pf_active := wrap.R.FormValue("active")
  299. if pf_active == "" {
  300. pf_active = "0"
  301. }
  302. if !utils.IsNumeric(pf_id) {
  303. wrap.MsgError(`Inner system error`)
  304. return
  305. }
  306. if pf_name == "" {
  307. wrap.MsgError(`Please specify page name`)
  308. return
  309. }
  310. if pf_alias == "" {
  311. pf_alias = utils.GenerateAlias(pf_name)
  312. }
  313. if !utils.IsValidAlias(pf_alias) {
  314. wrap.MsgError(`Please specify correct page alias`)
  315. return
  316. }
  317. if pf_id == "0" {
  318. // Add new page
  319. _, err := wrap.DB.Query(
  320. `INSERT INTO pages SET
  321. user = ?,
  322. name = ?,
  323. alias = ?,
  324. content = ?,
  325. meta_title = ?,
  326. meta_keywords = ?,
  327. meta_description = ?,
  328. datetime = ?,
  329. active = ?
  330. ;`,
  331. wrap.User.A_id,
  332. pf_name,
  333. pf_alias,
  334. pf_content,
  335. pf_meta_title,
  336. pf_meta_keywords,
  337. pf_meta_description,
  338. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  339. pf_active,
  340. )
  341. if err != nil {
  342. wrap.MsgError(err.Error())
  343. return
  344. }
  345. wrap.Write(`window.location='/cp/';`)
  346. } else {
  347. // Update page
  348. _, err := wrap.DB.Query(
  349. `UPDATE pages SET
  350. name = ?,
  351. alias = ?,
  352. content = ?,
  353. meta_title = ?,
  354. meta_keywords = ?,
  355. meta_description = ?,
  356. active = ?
  357. WHERE
  358. id = ?
  359. ;`,
  360. pf_name,
  361. pf_alias,
  362. pf_content,
  363. pf_meta_title,
  364. pf_meta_keywords,
  365. pf_meta_description,
  366. pf_active,
  367. utils.StrToInt(pf_id),
  368. )
  369. if err != nil {
  370. wrap.MsgError(err.Error())
  371. return
  372. }
  373. wrap.Write(`window.location='/cp/index/modify/` + pf_id + `/';`)
  374. }
  375. })
  376. }
  377. func (this *Modules) RegisterAction_IndexDelete() *Action {
  378. return this.newAction(AInfo{
  379. WantDB: true,
  380. Mount: "index-delete",
  381. WantAdmin: true,
  382. }, func(wrap *wrapper.Wrapper) {
  383. pf_id := wrap.R.FormValue("id")
  384. if !utils.IsNumeric(pf_id) {
  385. wrap.MsgError(`Inner system error`)
  386. return
  387. }
  388. // Delete page
  389. _, err := wrap.DB.Query(
  390. `DELETE FROM pages WHERE id = ?;`,
  391. utils.StrToInt(pf_id),
  392. )
  393. if err != nil {
  394. wrap.MsgError(err.Error())
  395. return
  396. }
  397. // Reload current page
  398. wrap.Write(`window.location.reload(false);`)
  399. })
  400. }
  401. func (this *Modules) RegisterAction_IndexMysqlSetup() *Action {
  402. return this.newAction(AInfo{
  403. WantDB: false,
  404. Mount: "index-mysql-setup",
  405. }, func(wrap *wrapper.Wrapper) {
  406. pf_host := wrap.R.FormValue("host")
  407. pf_port := wrap.R.FormValue("port")
  408. pf_name := wrap.R.FormValue("name")
  409. pf_user := wrap.R.FormValue("user")
  410. pf_password := wrap.R.FormValue("password")
  411. if pf_host == "" {
  412. wrap.MsgError(`Please specify host for MySQL connection`)
  413. return
  414. }
  415. if pf_port == "" {
  416. wrap.MsgError(`Please specify host port for MySQL connection`)
  417. return
  418. }
  419. if _, err := strconv.Atoi(pf_port); err != nil {
  420. wrap.MsgError(`MySQL host port must be integer number`)
  421. return
  422. }
  423. if pf_name == "" {
  424. wrap.MsgError(`Please specify MySQL database name`)
  425. return
  426. }
  427. if pf_user == "" {
  428. wrap.MsgError(`Please specify MySQL user`)
  429. return
  430. }
  431. // Try connect to mysql
  432. db, err := sql.Open("mysql", pf_user+":"+pf_password+"@tcp("+pf_host+":"+pf_port+")/"+pf_name)
  433. if err != nil {
  434. wrap.MsgError(err.Error())
  435. return
  436. }
  437. defer db.Close()
  438. err = db.Ping()
  439. if err != nil {
  440. wrap.MsgError(err.Error())
  441. return
  442. }
  443. // Try to install all tables
  444. _, err = db.Query(fmt.Sprintf(
  445. `CREATE TABLE %s.users (
  446. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  447. first_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User first name',
  448. last_name VARCHAR(64) NOT NULL DEFAULT '' COMMENT 'User last name',
  449. email VARCHAR(64) NOT NULL COMMENT 'User email',
  450. password VARCHAR(32) NOT NULL COMMENT 'User password (MD5)',
  451. admin int(1) NOT NULL COMMENT 'Is admin user or not',
  452. active int(1) NOT NULL COMMENT 'Is active user or not',
  453. PRIMARY KEY (id)
  454. ) ENGINE = InnoDB;`,
  455. pf_name))
  456. if err != nil {
  457. wrap.MsgError(err.Error())
  458. return
  459. }
  460. _, err = db.Query(fmt.Sprintf(
  461. `ALTER TABLE %s.users ADD UNIQUE KEY email (email);`,
  462. pf_name))
  463. if err != nil {
  464. wrap.MsgError(err.Error())
  465. return
  466. }
  467. _, err = db.Query(fmt.Sprintf(
  468. `CREATE TABLE %s.pages (
  469. id int(11) NOT NULL AUTO_INCREMENT COMMENT 'AI',
  470. user int(11) NOT NULL COMMENT 'User id',
  471. name varchar(255) NOT NULL COMMENT 'Page name',
  472. alias varchar(255) NOT NULL COMMENT 'Page url part',
  473. content text NOT NULL COMMENT 'Page content',
  474. meta_title varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta title',
  475. meta_keywords varchar(255) NOT NULL DEFAULT '' COMMENT 'Page meta keywords',
  476. meta_description varchar(510) NOT NULL DEFAULT '' COMMENT 'Page meta description',
  477. datetime datetime NOT NULL COMMENT 'Creation date/time',
  478. active int(1) NOT NULL COMMENT 'Is active page or not',
  479. PRIMARY KEY (id)
  480. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;`,
  481. pf_name))
  482. if err != nil {
  483. wrap.MsgError(err.Error())
  484. return
  485. }
  486. _, err = db.Query(fmt.Sprintf(
  487. `ALTER TABLE %s.pages ADD UNIQUE KEY alias (alias);`,
  488. pf_name))
  489. if err != nil {
  490. wrap.MsgError(err.Error())
  491. return
  492. }
  493. // Save mysql config file
  494. err = utils.MySqlConfigWrite(wrap.DConfig+string(os.PathSeparator)+"mysql.json", pf_host, pf_port, pf_name, pf_user, pf_password)
  495. if err != nil {
  496. wrap.MsgError(err.Error())
  497. return
  498. }
  499. // Reload current page
  500. wrap.Write(`window.location.reload(false);`)
  501. })
  502. }
  503. func (this *Modules) RegisterAction_IndexFirstUser() *Action {
  504. return this.newAction(AInfo{
  505. WantDB: true,
  506. Mount: "index-first-user",
  507. }, func(wrap *wrapper.Wrapper) {
  508. pf_first_name := wrap.R.FormValue("first_name")
  509. pf_last_name := wrap.R.FormValue("last_name")
  510. pf_email := wrap.R.FormValue("email")
  511. pf_password := wrap.R.FormValue("password")
  512. if pf_email == "" {
  513. wrap.MsgError(`Please specify user email`)
  514. return
  515. }
  516. if !utils.IsValidEmail(pf_email) {
  517. wrap.MsgError(`Please specify correct user email`)
  518. return
  519. }
  520. if pf_password == "" {
  521. wrap.MsgError(`Please specify user password`)
  522. return
  523. }
  524. _, err := wrap.DB.Query(
  525. `INSERT INTO users SET
  526. first_name = ?,
  527. last_name = ?,
  528. email = ?,
  529. password = MD5(?),
  530. admin = 1,
  531. active = 1
  532. ;`,
  533. pf_first_name,
  534. pf_last_name,
  535. pf_email,
  536. pf_password,
  537. )
  538. if err != nil {
  539. wrap.MsgError(err.Error())
  540. return
  541. }
  542. // Add home page
  543. _, err = wrap.DB.Query(
  544. `INSERT INTO pages SET
  545. user = ?,
  546. name = ?,
  547. alias = ?,
  548. content = ?,
  549. datetime = ?,
  550. active = ?
  551. ;`,
  552. 1,
  553. "Home",
  554. "/",
  555. "<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>",
  556. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  557. 1,
  558. )
  559. if err != nil {
  560. wrap.MsgError(err.Error())
  561. return
  562. }
  563. // Add another page
  564. _, err = wrap.DB.Query(
  565. `INSERT INTO pages SET
  566. user = ?,
  567. name = ?,
  568. alias = ?,
  569. content = ?,
  570. datetime = ?,
  571. active = ?
  572. ;`,
  573. 1,
  574. "Another",
  575. "/another/",
  576. "<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>",
  577. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  578. 1,
  579. )
  580. if err != nil {
  581. wrap.MsgError(err.Error())
  582. return
  583. }
  584. // Add about page
  585. _, err = wrap.DB.Query(
  586. `INSERT INTO pages SET
  587. user = ?,
  588. name = ?,
  589. alias = ?,
  590. content = ?,
  591. datetime = ?,
  592. active = ?
  593. ;`,
  594. 1,
  595. "About",
  596. "/about/",
  597. "<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>",
  598. utils.UnixTimestampToMySqlDateTime(utils.GetCurrentUnixTimestamp()),
  599. 1,
  600. )
  601. if err != nil {
  602. wrap.MsgError(err.Error())
  603. return
  604. }
  605. // Reload current page
  606. wrap.Write(`window.location.reload(false);`)
  607. })
  608. }
  609. func (this *Modules) RegisterAction_IndexUserSignIn() *Action {
  610. return this.newAction(AInfo{
  611. WantDB: true,
  612. Mount: "index-user-sign-in",
  613. }, func(wrap *wrapper.Wrapper) {
  614. pf_email := wrap.R.FormValue("email")
  615. pf_password := wrap.R.FormValue("password")
  616. if pf_email == "" {
  617. wrap.MsgError(`Please specify user email`)
  618. return
  619. }
  620. if !utils.IsValidEmail(pf_email) {
  621. wrap.MsgError(`Please specify correct user email`)
  622. return
  623. }
  624. if pf_password == "" {
  625. wrap.MsgError(`Please specify user password`)
  626. return
  627. }
  628. if wrap.S.GetInt("UserId", 0) > 0 {
  629. wrap.MsgError(`You already logined`)
  630. return
  631. }
  632. var user_id int
  633. err := wrap.DB.QueryRow(
  634. `SELECT
  635. id
  636. FROM
  637. users
  638. WHERE
  639. email = ? and
  640. password = MD5(?) and
  641. admin = 1 and
  642. active = 1
  643. LIMIT 1;`,
  644. pf_email,
  645. pf_password,
  646. ).Scan(
  647. &user_id,
  648. )
  649. if err != nil && err != sql.ErrNoRows {
  650. wrap.MsgError(err.Error())
  651. return
  652. }
  653. if err == sql.ErrNoRows {
  654. wrap.MsgError(`Incorrect email or password`)
  655. return
  656. }
  657. // Save to current session
  658. wrap.S.SetInt("UserId", user_id)
  659. // Reload current page
  660. wrap.Write(`window.location.reload(false);`)
  661. })
  662. }
  663. func (this *Modules) RegisterAction_IndexUserLogout() *Action {
  664. return this.newAction(AInfo{
  665. WantDB: true,
  666. Mount: "index-user-logout",
  667. WantUser: true,
  668. }, func(wrap *wrapper.Wrapper) {
  669. // Reset session var
  670. wrap.S.SetInt("UserId", 0)
  671. // Reload current page
  672. wrap.Write(`window.location.reload(false);`)
  673. })
  674. }
  675. func (this *Modules) RegisterAction_IndexUserUpdateProfile() *Action {
  676. return this.newAction(AInfo{
  677. WantDB: true,
  678. Mount: "index-user-update-profile",
  679. WantUser: true,
  680. }, func(wrap *wrapper.Wrapper) {
  681. pf_first_name := wrap.R.FormValue("first_name")
  682. pf_last_name := wrap.R.FormValue("last_name")
  683. pf_email := wrap.R.FormValue("email")
  684. pf_password := wrap.R.FormValue("password")
  685. if pf_email == "" {
  686. wrap.MsgError(`Please specify user email`)
  687. return
  688. }
  689. if !utils.IsValidEmail(pf_email) {
  690. wrap.MsgError(`Please specify correct user email`)
  691. return
  692. }
  693. if pf_password != "" {
  694. // Update with password if set
  695. _, err := wrap.DB.Query(
  696. `UPDATE users SET
  697. first_name = ?,
  698. last_name = ?,
  699. email = ?,
  700. password = MD5(?)
  701. WHERE
  702. id = ?
  703. ;`,
  704. pf_first_name,
  705. pf_last_name,
  706. pf_email,
  707. pf_password,
  708. wrap.User.A_id,
  709. )
  710. if err != nil {
  711. wrap.MsgError(err.Error())
  712. return
  713. }
  714. } else {
  715. // Update without password if not set
  716. _, err := wrap.DB.Query(
  717. `UPDATE users SET
  718. first_name = ?,
  719. last_name = ?,
  720. email = ?
  721. WHERE
  722. id = ?
  723. ;`,
  724. pf_first_name,
  725. pf_last_name,
  726. pf_email,
  727. wrap.User.A_id,
  728. )
  729. if err != nil {
  730. wrap.MsgError(err.Error())
  731. return
  732. }
  733. }
  734. // Reload current page
  735. wrap.Write(`window.location.reload(false);`)
  736. })
  737. }