2016-12-11 16:06:17 +00:00
|
|
|
/* Copyright Azareal 2016 - 2017 */
|
2016-12-02 07:38:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"log"
|
2016-12-05 07:21:17 +00:00
|
|
|
"mime"
|
2016-12-08 14:11:18 +00:00
|
|
|
"strings"
|
2016-12-02 07:38:54 +00:00
|
|
|
"path/filepath"
|
2016-12-17 03:39:53 +00:00
|
|
|
"io"
|
2016-12-05 07:21:17 +00:00
|
|
|
"io/ioutil"
|
2016-12-09 13:46:29 +00:00
|
|
|
"os"
|
2016-12-02 07:38:54 +00:00
|
|
|
"html/template"
|
|
|
|
)
|
|
|
|
|
|
|
|
const hour int = 60 * 60
|
|
|
|
const day int = hour * 24
|
|
|
|
const month int = day * 30
|
|
|
|
const year int = day * 365
|
2016-12-02 15:03:31 +00:00
|
|
|
const kilobyte int = 1024
|
|
|
|
const megabyte int = 1024 * 1024
|
2016-12-02 07:38:54 +00:00
|
|
|
const saltLength int = 32
|
|
|
|
const sessionLength int = 80
|
2016-12-23 12:35:22 +00:00
|
|
|
var nogrouplog bool = false // This is mainly for benchmarks, as we don't want a lot of information getting in the way of the results
|
2016-12-04 06:16:59 +00:00
|
|
|
|
2016-12-02 07:38:54 +00:00
|
|
|
var templates = template.Must(template.ParseGlob("templates/*"))
|
2016-12-04 06:16:59 +00:00
|
|
|
var no_css_tmpl = template.CSS("")
|
|
|
|
var staff_css_tmpl = template.CSS(staff_css)
|
2016-12-09 13:46:29 +00:00
|
|
|
var settings map[string]interface{} = make(map[string]interface{})
|
|
|
|
var external_sites map[string]string = make(map[string]string)
|
2016-12-04 06:16:59 +00:00
|
|
|
var groups map[int]Group = make(map[int]Group)
|
2016-12-06 10:26:48 +00:00
|
|
|
var forums map[int]Forum = make(map[int]Forum)
|
2016-12-05 07:21:17 +00:00
|
|
|
var static_files map[string]SFile = make(map[string]SFile)
|
2016-12-21 02:30:32 +00:00
|
|
|
var ctemplates []string
|
|
|
|
var template_topic_handle func(TopicPage,io.Writer) = nil
|
|
|
|
var template_topics_handle func(Page,io.Writer) = nil
|
|
|
|
var template_forum_handle func(Page,io.Writer) = nil
|
|
|
|
var template_forums_handle func(Page,io.Writer) = nil
|
|
|
|
var template_profile_handle func(Page,io.Writer) = nil
|
2016-12-16 10:37:42 +00:00
|
|
|
|
|
|
|
func compile_templates() {
|
|
|
|
var c CTemplateSet
|
2016-12-23 12:35:22 +00:00
|
|
|
user := User{0,"","compiler@localhost",0,false,false,false,false,false,false,GuestPerms,"",false,"","","","",""}
|
2016-12-16 10:37:42 +00:00
|
|
|
var noticeList map[int]string = make(map[int]string)
|
|
|
|
noticeList[0] = "test"
|
|
|
|
|
|
|
|
log.Print("Compiling the templates")
|
|
|
|
|
|
|
|
topic := TopicUser{0,"",template.HTML(""),0,false,false,"",0,"","","",no_css_tmpl,0,"","","",""}
|
2016-12-21 02:30:32 +00:00
|
|
|
var replyList []Reply
|
2016-12-18 12:56:06 +00:00
|
|
|
replyList = append(replyList, Reply{0,0,"",template.HTML(""),0,"","",0,0,"",no_css_tmpl,0,"","","",""})
|
2016-12-16 10:37:42 +00:00
|
|
|
|
2016-12-21 02:30:32 +00:00
|
|
|
var varList map[string]VarItem = make(map[string]VarItem)
|
|
|
|
tpage := TopicPage{"Title","name",user,noticeList,replyList,topic,false}
|
|
|
|
topic_id_tmpl := c.compile_template("topic.html","templates/","TopicPage", tpage, varList)
|
2016-12-16 10:37:42 +00:00
|
|
|
|
|
|
|
varList = make(map[string]VarItem)
|
|
|
|
varList["extra_data"] = VarItem{"extra_data","tmpl_profile_vars.Something.(User)","User"}
|
2016-12-21 02:30:32 +00:00
|
|
|
//pi := Page{"Title","name",user,noticeList,replyList,user}
|
|
|
|
//profile_tmpl := c.compile_template("profile.html","templates/","Page", pi, varList)
|
2016-12-18 12:56:06 +00:00
|
|
|
|
|
|
|
var forumList []interface{}
|
|
|
|
for _, forum := range forums {
|
|
|
|
if forum.Active {
|
|
|
|
forumList = append(forumList, forum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
varList = make(map[string]VarItem)
|
2016-12-21 02:30:32 +00:00
|
|
|
pi := Page{"Forum List","forums",user,noticeList,forumList,0}
|
2016-12-18 12:56:06 +00:00
|
|
|
forums_tmpl := c.compile_template("forums.html","templates/","Page", pi, varList)
|
|
|
|
|
|
|
|
var topicList []interface{}
|
|
|
|
topicList = append(topicList, TopicUser{1,"Topic Title","The topic content.",1,false,false,"",1,"open","Admin","","",0,"","","",""})
|
|
|
|
pi = Page{"Topic List","topics",user,noticeList,topicList,""}
|
|
|
|
topics_tmpl := c.compile_template("topics.html","templates/","Page", pi, varList)
|
|
|
|
|
|
|
|
pi = Page{"General Forum","forum",user,noticeList,topicList,"There aren't any topics in this forum yet."}
|
|
|
|
forum_tmpl := c.compile_template("forum.html","templates/","Page", pi, varList)
|
2016-12-16 10:37:42 +00:00
|
|
|
|
|
|
|
log.Print("Writing the templates")
|
2016-12-18 12:56:06 +00:00
|
|
|
write_template("topic", topic_id_tmpl)
|
2016-12-21 02:30:32 +00:00
|
|
|
//write_template("profile", profile_tmpl)
|
2016-12-18 12:56:06 +00:00
|
|
|
write_template("forums", forums_tmpl)
|
|
|
|
write_template("topics", topics_tmpl)
|
|
|
|
write_template("forum", forum_tmpl)
|
2016-12-16 10:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func write_template(name string, content string) {
|
|
|
|
f, err := os.Create("./template_" + name + ".go")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = f.WriteString(content)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
f.Sync()
|
|
|
|
f.Close()
|
|
|
|
}
|
2016-12-02 07:38:54 +00:00
|
|
|
|
|
|
|
func main(){
|
|
|
|
var err error
|
|
|
|
init_database(err);
|
2016-12-18 12:56:06 +00:00
|
|
|
compile_templates()
|
2016-12-02 07:38:54 +00:00
|
|
|
|
2016-12-05 07:21:17 +00:00
|
|
|
log.Print("Loading the static files.")
|
2016-12-08 14:11:18 +00:00
|
|
|
err = filepath.Walk("./public", func(path string, f os.FileInfo, err error) error {
|
2016-12-05 07:21:17 +00:00
|
|
|
if f.IsDir() {
|
2016-12-08 14:11:18 +00:00
|
|
|
return nil
|
2016-12-05 07:21:17 +00:00
|
|
|
}
|
2016-12-08 14:11:18 +00:00
|
|
|
|
|
|
|
path = strings.Replace(path,"\\","/",-1)
|
|
|
|
data, err := ioutil.ReadFile(path)
|
2016-12-05 07:21:17 +00:00
|
|
|
if err != nil {
|
2016-12-08 14:11:18 +00:00
|
|
|
return err
|
2016-12-05 07:21:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-08 14:11:18 +00:00
|
|
|
path = strings.TrimPrefix(path,"public/")
|
|
|
|
log.Print("Added the '" + path + "' static file.")
|
|
|
|
static_files["/static/" + path] = SFile{data,0,int64(len(data)),mime.TypeByExtension(filepath.Ext("/public/" + path)),f,f.ModTime().UTC().Format(http.TimeFormat)}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2016-12-05 07:21:17 +00:00
|
|
|
}
|
|
|
|
|
2016-12-09 13:46:29 +00:00
|
|
|
external_sites["YT"] = "https://www.youtube.com/"
|
2016-12-11 16:06:17 +00:00
|
|
|
hooks["trow_assign"] = nil
|
|
|
|
hooks["rrow_assign"] = nil
|
2016-12-16 10:37:42 +00:00
|
|
|
templates.ParseGlob("pages/*")
|
2016-12-11 16:06:17 +00:00
|
|
|
|
|
|
|
for name, body := range plugins {
|
|
|
|
log.Print("Added plugin " + name)
|
|
|
|
if body.Active {
|
|
|
|
log.Print("Initialised plugin " + name)
|
|
|
|
plugins[name].Init()
|
|
|
|
}
|
|
|
|
}
|
2016-12-08 14:11:18 +00:00
|
|
|
|
2016-12-02 07:38:54 +00:00
|
|
|
// In a directory to stop it clashing with the other paths
|
2016-12-05 07:21:17 +00:00
|
|
|
http.HandleFunc("/static/", route_static)
|
|
|
|
//http.HandleFunc("/static/", route_fstatic)
|
|
|
|
//fs_p := http.FileServer(http.Dir("./public"))
|
|
|
|
//http.Handle("/static/", http.StripPrefix("/static/",fs_p))
|
2016-12-02 15:03:31 +00:00
|
|
|
fs_u := http.FileServer(http.Dir("./uploads"))
|
|
|
|
http.Handle("/uploads/", http.StripPrefix("/uploads/",fs_u))
|
2016-12-02 07:38:54 +00:00
|
|
|
|
|
|
|
http.HandleFunc("/overview/", route_overview)
|
|
|
|
http.HandleFunc("/topics/create/", route_topic_create)
|
|
|
|
http.HandleFunc("/topics/", route_topics)
|
2016-12-03 13:45:08 +00:00
|
|
|
http.HandleFunc("/forums/", route_forums)
|
|
|
|
http.HandleFunc("/forum/", route_forum)
|
2016-12-09 13:46:29 +00:00
|
|
|
http.HandleFunc("/topic/create/submit/", route_create_topic)
|
2016-12-02 07:38:54 +00:00
|
|
|
http.HandleFunc("/topic/", route_topic_id)
|
2016-12-09 13:46:29 +00:00
|
|
|
http.HandleFunc("/reply/create/", route_create_reply)
|
|
|
|
//http.HandleFunc("/reply/edit/", route_reply_edit)
|
|
|
|
//http.HandleFunc("/reply/delete/", route_reply_delete)
|
|
|
|
http.HandleFunc("/reply/edit/submit/", route_reply_edit_submit)
|
|
|
|
http.HandleFunc("/reply/delete/submit/", route_reply_delete_submit)
|
2016-12-11 16:06:17 +00:00
|
|
|
http.HandleFunc("/report/submit/", route_report_submit)
|
2016-12-09 13:46:29 +00:00
|
|
|
http.HandleFunc("/topic/edit/submit/", route_edit_topic)
|
2016-12-03 10:25:39 +00:00
|
|
|
http.HandleFunc("/topic/delete/submit/", route_delete_topic)
|
|
|
|
http.HandleFunc("/topic/stick/submit/", route_stick_topic)
|
|
|
|
http.HandleFunc("/topic/unstick/submit/", route_unstick_topic)
|
2016-12-02 07:38:54 +00:00
|
|
|
|
|
|
|
// Custom Pages
|
2016-12-02 09:29:45 +00:00
|
|
|
http.HandleFunc("/pages/", route_custom_page)
|
2016-12-02 07:38:54 +00:00
|
|
|
|
|
|
|
// Accounts
|
|
|
|
http.HandleFunc("/accounts/login/", route_login)
|
|
|
|
http.HandleFunc("/accounts/create/", route_register)
|
|
|
|
http.HandleFunc("/accounts/logout/", route_logout)
|
2016-12-09 13:46:29 +00:00
|
|
|
http.HandleFunc("/accounts/login/submit/", route_login_submit)
|
|
|
|
http.HandleFunc("/accounts/create/submit/", route_register_submit)
|
2016-12-02 07:38:54 +00:00
|
|
|
|
|
|
|
//http.HandleFunc("/accounts/list/", route_login) // Redirect /accounts/ and /user/ to here..
|
|
|
|
//http.HandleFunc("/accounts/create/full/", route_logout)
|
|
|
|
//http.HandleFunc("/user/edit/", route_logout)
|
|
|
|
http.HandleFunc("/user/edit/critical/", route_account_own_edit_critical) // Password & Email
|
|
|
|
http.HandleFunc("/user/edit/critical/submit/", route_account_own_edit_critical_submit)
|
2016-12-03 04:50:35 +00:00
|
|
|
http.HandleFunc("/user/edit/avatar/", route_account_own_edit_avatar)
|
2016-12-02 15:03:31 +00:00
|
|
|
http.HandleFunc("/user/edit/avatar/submit/", route_account_own_edit_avatar_submit)
|
2016-12-03 08:09:40 +00:00
|
|
|
http.HandleFunc("/user/edit/username/", route_account_own_edit_username)
|
|
|
|
http.HandleFunc("/user/edit/username/submit/", route_account_own_edit_username_submit)
|
2016-12-07 09:34:09 +00:00
|
|
|
http.HandleFunc("/user/", route_profile)
|
|
|
|
http.HandleFunc("/profile/reply/create/", route_profile_reply_create)
|
|
|
|
http.HandleFunc("/profile/reply/edit/submit/", route_profile_reply_edit_submit)
|
|
|
|
http.HandleFunc("/profile/reply/delete/submit/", route_profile_reply_delete_submit)
|
2016-12-09 13:46:29 +00:00
|
|
|
//http.HandleFunc("/user/edit/submit/", route_logout)
|
2016-12-08 14:11:18 +00:00
|
|
|
http.HandleFunc("/users/ban/", route_ban)
|
|
|
|
http.HandleFunc("/users/ban/submit/", route_ban_submit)
|
|
|
|
http.HandleFunc("/users/unban/", route_unban)
|
2016-12-18 12:56:06 +00:00
|
|
|
http.HandleFunc("/users/activate/", route_activate)
|
2016-12-04 10:44:28 +00:00
|
|
|
|
|
|
|
// Admin
|
|
|
|
http.HandleFunc("/panel/forums/", route_panel_forums)
|
|
|
|
http.HandleFunc("/panel/forums/create/", route_panel_forums_create_submit)
|
2016-12-06 10:26:48 +00:00
|
|
|
http.HandleFunc("/panel/forums/delete/", route_panel_forums_delete)
|
|
|
|
http.HandleFunc("/panel/forums/delete/submit/", route_panel_forums_delete_submit)
|
|
|
|
http.HandleFunc("/panel/forums/edit/submit/", route_panel_forums_edit_submit)
|
2016-12-09 13:46:29 +00:00
|
|
|
http.HandleFunc("/panel/settings/", route_panel_settings)
|
|
|
|
http.HandleFunc("/panel/settings/edit/", route_panel_setting)
|
|
|
|
http.HandleFunc("/panel/settings/edit/submit/", route_panel_setting_edit)
|
2016-12-11 16:06:17 +00:00
|
|
|
http.HandleFunc("/panel/plugins/", route_panel_plugins)
|
|
|
|
http.HandleFunc("/panel/plugins/activate/", route_panel_plugins_activate)
|
2016-12-13 02:14:14 +00:00
|
|
|
http.HandleFunc("/panel/plugins/deactivate/", route_panel_plugins_deactivate)
|
|
|
|
http.HandleFunc("/panel/users/", route_panel_users)
|
2016-12-21 02:30:32 +00:00
|
|
|
http.HandleFunc("/panel/users/edit/", route_panel_users_edit)
|
2016-12-23 12:35:22 +00:00
|
|
|
http.HandleFunc("/panel/users/edit/submit/", route_panel_users_edit_submit)
|
2016-12-04 10:44:28 +00:00
|
|
|
|
2016-12-03 08:09:40 +00:00
|
|
|
http.HandleFunc("/", default_route)
|
2016-12-02 07:38:54 +00:00
|
|
|
|
|
|
|
defer db.Close()
|
|
|
|
http.ListenAndServe(":8080", nil)
|
|
|
|
}
|