module_index.go 24 KB

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