package tool
import (
"database/sql"
"fmt"
"html"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/flosch/pongo2/v6"
)
func Get_skin_route(skin_name string, route string) string {
return filepath.Join("..", "views", skin_name, route)
}
func Get_template_set(skin_name string) map[string]string {
set_file_path := Get_skin_route(skin_name, "set.json")
if _, err := os.Stat(set_file_path); err == nil {
data, err := os.ReadFile(set_file_path)
if err != nil {
panic(err)
}
set_json := map[string]string{}
json.Unmarshal([]byte(data), &set_json)
return set_json
}
return map[string]string{}
}
func Get_use_skin_name(db *sql.DB, ip string) string {
skin_list := Get_skin_list("ringo", true)
skin := skin_list[0]
user_skin_name := ""
if !IP_or_user(ip) {
QueryRow_DB(
db,
"select data from user_set where name = 'skin' and id = ?",
[]any{ &user_skin_name },
ip,
)
}
if user_skin_name == "default" {
user_skin_name = ""
}
if user_skin_name == "" {
QueryRow_DB(
db,
"select data from other where name = 'skin'",
[]any{ &user_skin_name },
)
}
if user_skin_name != "" && Arr_in_str(skin_list, user_skin_name) {
skin = user_skin_name
}
return skin
}
func Get_template(db *sql.DB, config Config, name string, data string, other []any, menu [][]any, option map[string]string) string {
skin_name := Get_use_skin_name(db, config.IP)
template_set := Get_template_set(skin_name)
for k, v := range template_set {
data = strings.ReplaceAll(data, k, v)
}
menu_func := func(menu [][]any) any {
if len(menu) == 0 {
return 0
} else {
return menu
}
}
menu_func_result := menu_func(menu)
if len(other) < 1 {
other = append(other, 0)
}
if len(other) < 2 {
other = append(other, 0)
}
for k := range other {
switch v := other[k].(type) {
case nil:
other[k] = 0
case float64:
other[k] = int(v)
case int64:
other[k] = int(v)
case int:
other[k] = v
case string:
if v == "" {
other[k] = 0
} else {
other[k] = v
}
default:
other[k] = 0
}
}
imp_1 := Get_wiki_set(db, config.IP, config.Cookies)
imp_2 := Get_wiki_custom(db, config.IP, config.Session, config.Cookies)
imp_3 := Get_wiki_css(other, config.Cookies)
if len(imp_3) < 8 {
imp_3 = append(imp_3, 0)
}
added_menu := []string{}
switch imp_1[7].(type) {
case []string:
added_menu = imp_1[7].([]string)
default:
added_menu = []string{"", "", ""}
}
if len(added_menu) < 3 {
for i := len(added_menu); i < 3; i++ {
added_menu = append(added_menu, "")
}
}
imp_1[7] = added_menu
path := ""
if option["path"] != "" {
path = option["path"]
}
doc_length := ""
if _, ok := option["length_doc"]; ok {
doc_length = option["length_doc"]
}
context := pongo2.Context{
"imp" : []any{
name,
imp_1,
imp_2,
imp_3,
},
"data" : `
` + data + `
`,
"menu" : menu_func_result,
"title" : name,
"wiki_name" : imp_1[0],
"license" : imp_1[1],
"wiki_logo" : imp_1[4],
"global_head" : imp_1[5],
"add_menu" : imp_1[6],
"template_var_1" : added_menu[0],
"template_var_2" : added_menu[1],
"template_var_3" : added_menu[2],
"user_login" : imp_2[2],
"user_head" : imp_2[3],
"user_email" : imp_2[4],
"user_name" : imp_2[5],
"user_is_admin" : imp_2[6],
"user_is_ban" : imp_2[7],
"user_alarm_count" : imp_2[8],
"user_auth" : imp_2[9],
"user_ip" : imp_2[10],
"user_discuss" : imp_2[11],
"user_path" : path,
"user_level" : imp_2[13],
"sub_title" : imp_3[0],
"last_edit" : imp_3[1],
"main_head" : imp_3[3],
"star_doc" : imp_3[4],
"main_head_dark" : imp_3[5],
"description_doc" : imp_3[6],
"view_count" : imp_3[7],
"length_doc" : doc_length,
}
tpl, err := pongo2.FromFile(Get_skin_route(skin_name, "index.html"))
if err != nil {
panic(err)
}
out, err := tpl.Execute(context)
if err != nil {
panic(err)
}
return out
}
func Get_redirect(target string) string {
attrURL := html.EscapeString(target)
jsURL := strconv.Quote(target)
return fmt.Sprintf(`
Redirecting…
Redirecting… continue
`, jsURL, attrURL, attrURL)
}
func Cache_v() string {
return ".cache_v288"
}
func Get_wiki_css(data []any, cookies string) []any {
for len(data) < 4 {
data = append(data, "")
}
data_css := ""
data_css_dark := ""
data_css_ver := Cache_v()
// Cache Control
data_css += ` `
// External JS
data_css += ``
data_css += ``
data_css += ``
data_css += ``
data_css += ``
data_css += ``
// Func JS
data_css += ``
data_css += ``
data_css += ``
data_css += ``
data_css += ``
data_css += ``
data_css += ``
data_css += ``
data_css += ``
// Main CSS
data_css += ` `
// External CSS
data_css += ` `
data_css += ` `
data_css += ` `
cookie_map := Get_cookie_header(cookies)
if cookie_map["main_css_darkmode"] == "1" {
// Main CSS
data_css_dark += ` `
// External CSS
data_css_dark += ` `
}
end := 2
if end > len(data) {
end = len(data)
}
new_data := append([]any{}, data[:end]...)
new_data = append(new_data, "", data_css)
if len(data) >= 3 {
new_data = append(new_data, data[2])
}
new_data = append(new_data, data_css_dark)
if len(data) >= 3 {
new_data = append(new_data, data[3:]...)
}
return new_data
}
func Get_list_ui(left string, right string, bottom string, class_name string) string {
data_html := ""
data_html += ``
data_html += ``
data_html += left
data_html += `
`
data_html += right
data_html += `
`
data_html += `
`
if bottom != "" {
data_html += "
"
data_html += bottom
}
data_html += "
"
data_html += ` `
data_html += " "
return data_html
}
// Get_error_page : auth, slow edit limit, edit filter (content), edit filter (send), send require, checkbox check require, overflow max length
func Get_error_page(db *sql.DB, config Config, error_name string) string {
data := ""
switch error_name {
case "auth":
data = Get_language(db, "authority_error", true)
case "slow edit limit":
data = Get_language(db, "fast_edit_error", true)
case "edit filter (content)":
data = Get_language(db, "edit_filter_error", true) + " (content)"
case "edit filter (send)":
data = Get_language(db, "edit_filter_error", true) + " (send)"
case "send require":
data = Get_language(db, "error_edit_send_request", true)
case "checkbox check require":
data = Get_language(db, "copyright_disagreed", true)
case "overflow max length":
data = Get_language(db, "document_content_max_length_error", true)
default:
data = Get_language(db, "inter_error", true)
}
return Get_template(
db,
config,
Get_language(db, "error", true),
`` + Get_language(db, "error", true) + ` ` +
``,
[]any{},
[][]any{},
map[string]string{},
)
}
func Get_page_control(db *sql.DB, page int, count int, max_count int, url string) string {
data_html := " "
if page > 1 {
prev_page := page - 1
before_url := strings.ReplaceAll(url, "{}", strconv.Itoa(prev_page))
data_html += `(` + Get_language(db, "previous", true) + `) `
}
if count == max_count {
prev_page := page + 1
after_url := strings.ReplaceAll(url, "{}", strconv.Itoa(prev_page))
data_html += `(` + Get_language(db, "next", true) + `) `
}
return data_html
}
func Get_editor_ui(db *sql.DB, config Config, data string, do_type string, add_on string, doc_name string) string {
monaco_editor_top := ""
help_text := ""
document_top := ""
switch do_type {
case "edit":
QueryRow_DB(
db,
`select data from other where name = "edit_help"`,
[]any{ &help_text },
)
QueryRow_DB(
db,
`select set_data from data_set where doc_name = ? and set_name = 'document_top'`,
[]any{ &document_top },
doc_name,
)
case "bbs":
QueryRow_DB(
db,
`select data from other where name = "bbs_help"`,
[]any{ &help_text },
)
case "bbs_comment":
QueryRow_DB(
db,
`select data from other where name = "bbs_comment_help"`,
[]any{ &help_text },
)
default:
QueryRow_DB(
db,
`select data from other where name = "topic_text"`,
[]any{ &help_text },
)
}
if help_text == "" {
help_text = Get_language(db, "default_edit_help", true)
} else {
help_text = HTML_escape(help_text)
}
editor_type := "edit"
if do_type == "bbs_comment" || do_type == "thread" {
editor_type = "thread"
}
monaco_editor_top += `
(` + Get_language(db, "load_temp_save", true) + `) (` + Get_language(db, "load_temp_save_load", true) + `)
`
dark_mode := false
cookie_map := Get_cookie_header(config.Cookies)
if cookie_map["main_css_darkmode"] == "1" {
dark_mode = true
}
monaco_theme := ""
if dark_mode {
monaco_theme = "vs-dark"
}
monaco_on := false
if Get_main_skin_set(db, config, "main_css_monaco") == "use" {
monaco_on = true
}
editor_display := []string{}
for for_a := 0; for_a < 3; for_a++ {
editor_display = append(editor_display, `style="display: none;"`)
}
select_A := ""
select_B := ""
if monaco_on {
editor_display[1] = ""
select_B = "selected"
} else {
editor_display[0] = ""
select_A = "selected"
}
monaco_editor_top += `
` + Get_language(db, "default", true) + `
` + Get_language(db, "monaco_editor", true) + `
`
if editor_type == "edit" {
monaco_editor_top += Get_markup_select_ui(db, config, doc_name, "", `id="opennamu_editor_markup" onclick="opennamu_do_sync_monaco_markup();"`, "")
} else {
monaco_editor_top += Get_markup_select_ui(db, config, doc_name, "", `id="opennamu_editor_markup" onclick="opennamu_do_sync_monaco_markup();"`, "disabled")
}
textarea_size := "opennamu_textarea_500"
if editor_type != "edit" {
textarea_size = "opennamu_textarea_100"
}
out_field := Get_captcha_ui(db, config) + Get_IP_warning_ui(db, config) + add_on
return `
` + monaco_editor_top + `
` + Get_editor_button_ui(db) + `
` + document_top + `
` + out_field + `
` + Get_language(db, "send", true) + `
` + Get_language(db, "preview", true) + `
`
}
func Get_markup_select_ui(db *sql.DB, config Config, doc_name string, markup string, add_on string, disable string) string {
default_markup := ""
QueryRow_DB(
db,
`select data from other where name = "markup"`,
[]any{ &default_markup },
)
markup_load := markup
if markup == "" {
QueryRow_DB(
db,
`select set_data from data_set where doc_name = ? and set_name = 'document_markup'`,
[]any{ &markup_load },
doc_name,
)
}
markup_list := []string{ "normal" }
markup_list = append(markup_list, Get_init_set_list("markup")["markup"]["list"].([]string)...)
markup_html := ""
for _, v := range markup_list {
selected := ""
if markup_load == v {
selected = "selected"
}
value := v
if v == "normal" {
value = default_markup
}
markup_html += `` + v + ` `
}
markup_html = `
` + markup_html + `
`
return markup_html
}
func Get_captcha_ui(db *sql.DB, config Config) string {
data := ""
if !Check_acl(db, "", "", "recaptcha", config.IP) {
pub_key := ""
QueryRow_DB(
db,
`select data from other where name = "recaptcha"`,
[]any{ &pub_key },
)
sec_key := ""
QueryRow_DB(
db,
`select data from other where name = "sec_re"`,
[]any{ &sec_key },
)
if pub_key != "" && sec_key != "" {
rec_ver := ""
QueryRow_DB(
db,
`select data from other where name = "recaptcha_ver"`,
[]any{ &rec_ver },
)
switch rec_ver {
case "":
data += `
`
case "v3":
data += `
`
case "cf":
data += `
`
default:
data += `
`
}
}
}
return data
}
func Get_IP_warning_ui(db *sql.DB, config Config) string {
text_data := ""
if IP_or_user(config.IP) {
text_db := ""
QueryRow_DB(
db,
`select data from other where name = "no_login_warning"`,
[]any{ &text_db },
)
if text_db == "" {
text_db = Get_language(db, "no_login_warning", true)
}
text_data = `` + text_db + ` `
}
return text_data
}
func Get_editor_button_ui(db *sql.DB) string {
data_html := ""
rows := Query_DB(
db,
`select html, plus from html_filter where kind = 'edit_top'`,
)
defer rows.Close()
for rows.Next() {
var html string
var plus string
err := rows.Scan(&html, &plus)
if err != nil {
panic(err)
}
data_html += `` + HTML_escape(html) + ` `
}
if data_html != "" {
data_html += " "
}
data_html += `(` + Get_language(db, "add", true) + `) `
return data_html
}
func Get_thread_ui(user_name string, date string, data string, code string, color string, blind string, add_style string, topic_num string) string {
color_b := ""
class_b := ""
if blind == "O" {
if data == "" {
color_b = "opennamu_comment_blind"
} else {
color_b = "opennamu_comment_blind_admin"
}
class_b = "opennamu_comment_blind_js opennamu_list_hidden"
} else {
color_b = "opennamu_comment_blind_not"
}
admin_check_box := ""
if topic_num != "" {
admin_check_box = ` `
}
return `
`
}
func Get_edit_check_box_ui(db *sql.DB) string {
cccb_text := ""
QueryRow_DB(
db,
`select data from other where name = "copyright_checkbox_text"`,
[]any{ &cccb_text },
)
result := ""
if cccb_text != "" {
result = `
` + cccb_text + `
`
}
return result
}
func Get_edit_bottom_text_ui(db *sql.DB, do_type string) string {
b_text := ""
QueryRow_DB(
db,
`select data from other where name = "edit_bottom_text"`,
[]any{ &b_text },
)
db_data := ""
switch do_type {
case "edit":
QueryRow_DB(
db,
`select data from other where name = "edit_only_bottom_text"`,
[]any{ &db_data },
)
case "move":
QueryRow_DB(
db,
`select data from other where name = "move_bottom_text"`,
[]any{ &db_data },
)
case "delete":
QueryRow_DB(
db,
`select data from other where name = "delete_bottom_text"`,
[]any{ &db_data },
)
default:
QueryRow_DB(
db,
`select data from other where name = "revert_bottom_text"`,
[]any{ &db_data },
)
}
result := ""
if db_data != "" {
result = db_data + ` `
} else if b_text != "" {
result = b_text + ` `
}
return result
}