diff --git a/src/data.sql b/src/data.sql index 81d4ca3f..daa44bc8 100644 --- a/src/data.sql +++ b/src/data.sql @@ -61,6 +61,14 @@ CREATE TABLE `replies`( primary key(`rid`) ); +CREATE TABLE `replies_reports` ( + `rid` int not null AUTO_INCREMENT, + `reportedBy` int not null, + `reportedContent` text not null, + `resolved` tinyint DEFAULT 0 not null, + primary key(`rid`) +); + INSERT INTO users(`name`,`group`,`is_super_admin`,`createdAt`,`lastActiveAt`) VALUES ('Admin',1,1,NOW(),NOW()); INSERT INTO users_groups(`name`,`permissions`,`is_admin`) VALUES ('Administrator','{}',1); diff --git a/src/errors.go b/src/errors.go index 63c468eb..c0d12ad7 100644 --- a/src/errors.go +++ b/src/errors.go @@ -86,6 +86,30 @@ func NoPermissionsJSQ(w http.ResponseWriter, r *http.Request, user User, is_js s } } +func Banned(w http.ResponseWriter, r *http.Request, user User) { + errmsg := "You have been banned, thus you do not permission to do that." + pi := Page{"Local Error","error",user,tList,errmsg} + var b bytes.Buffer + templates.ExecuteTemplate(&b,"error.html", pi) + errpage := b.String() + w.WriteHeader(403) + fmt.Fprintln(w,errpage) +} + +func BannedJSQ(w http.ResponseWriter, r *http.Request, user User, is_js string) { + errmsg := "You have been banned from this site." + if is_js == "0" { + pi := Page{"Local Error","error",user,tList,errmsg} + var b bytes.Buffer + templates.ExecuteTemplate(&b,"error.html", pi) + errpage := b.String() + w.WriteHeader(403) + fmt.Fprintln(w,errpage) + } else { + http.Error(w,"{'errmsg': '" + errmsg + "'}",403) + } +} + func LoginRequiredJSQ(w http.ResponseWriter, r *http.Request, user User, is_js string) { errmsg := "You need to login to do that." if is_js == "0" { diff --git a/src/forum.go b/src/forum.go index 00ca2bae..b08cf501 100644 --- a/src/forum.go +++ b/src/forum.go @@ -10,3 +10,9 @@ type Forum struct LastReplyerID int LastTopicTime string } + +type ForumSimple struct +{ + ID int + Name string +} \ No newline at end of file diff --git a/src/main.go b/src/main.go index 4b7d52b1..b89576a8 100644 --- a/src/main.go +++ b/src/main.go @@ -39,6 +39,8 @@ var set_username_stmt *sql.Stmt var register_stmt *sql.Stmt var username_exists_stmt *sql.Stmt +var create_forum_stmt *sql.Stmt + var custom_pages map[string]string = make(map[string]string) var templates = template.Must(template.ParseGlob("templates/*")) var no_css_tmpl = template.CSS("") @@ -177,6 +179,12 @@ func init_database(err error) { log.Fatal(err) } + log.Print("Preparing create_forum statement.") + create_forum_stmt, err = db.Prepare("INSERT INTO forums(name) VALUES(?)") + if err != nil { + log.Fatal(err) + } + log.Print("Loading the usergroups.") rows, err := db.Query("select gid,name,permissions,is_admin,is_banned from users_groups") if err != nil { @@ -252,6 +260,11 @@ func main(){ http.HandleFunc("/user/edit/username/submit/", route_account_own_edit_username_submit) //http.HandleFunc("/user/:id/edit/", route_logout) //http.HandleFunc("/user/:id/ban/", route_logout) + + // Admin + http.HandleFunc("/panel/forums/", route_panel_forums) + http.HandleFunc("/panel/forums/create/", route_panel_forums_create_submit) + http.HandleFunc("/", default_route) defer db.Close() diff --git a/src/routes.go b/src/routes.go index 6dba20c5..12b9101d 100644 --- a/src/routes.go +++ b/src/routes.go @@ -209,10 +209,15 @@ func route_forums(w http.ResponseWriter, r *http.Request){ return } - forum.LastTopicTime, err = relative_time(forum.LastTopicTime) - if err != nil { - InternalError(err,w,r,user) - return + if forum.LastTopicID != 0 { + forum.LastTopicTime, err = relative_time(forum.LastTopicTime) + if err != nil { + InternalError(err,w,r,user) + return + } + } else { + forum.LastTopic = "None" + forum.LastTopicTime = "" } forumList[currentID] = forum @@ -334,6 +339,11 @@ func route_topic_id(w http.ResponseWriter, r *http.Request){ func route_topic_create(w http.ResponseWriter, r *http.Request){ user := SessionCheck(w,r) + if user.Is_Banned { + Banned(w,r,user) + return + } + pi := Page{"Create Topic","create-topic",user,tList,0} templates.ExecuteTemplate(w,"create-topic.html", pi) } @@ -345,6 +355,10 @@ func route_create_topic(w http.ResponseWriter, r *http.Request) { LoginRequired(w,r,user) return } + if user.Is_Banned { + Banned(w,r,user) + return + } err := r.ParseForm() if err != nil { @@ -392,6 +406,10 @@ func route_create_reply(w http.ResponseWriter, r *http.Request) { LoginRequired(w,r,user) return } + if user.Is_Banned { + Banned(w,r,user) + return + } err := r.ParseForm() if err != nil { @@ -468,6 +486,10 @@ func route_edit_topic(w http.ResponseWriter, r *http.Request) { NoPermissionsJSQ(w,r,user,is_js) return } + if user.Is_Banned { + BannedJSQ(w,r,user,is_js) + return + } var tid int tid, err = strconv.Atoi(r.URL.Path[len("/topic/edit/submit/"):]) @@ -1176,3 +1198,63 @@ func route_register_submit(w http.ResponseWriter, r *http.Request) { http.SetCookie(w,&cookie) http.Redirect(w,r, "/", http.StatusSeeOther) } + +func route_panel_forums(w http.ResponseWriter, r *http.Request){ + user := SessionCheck(w,r) + if !user.Is_Admin { + NoPermissions(w,r,user) + return + } + + var forumList map[int]interface{} + forumList = make(map[int]interface{}) + currentID := 0 + + rows, err := db.Query("select fid, name from forums") + if err != nil { + InternalError(err,w,r,user) + return + } + defer rows.Close() + + for rows.Next() { + forum := ForumSimple{0,""} + err := rows.Scan(&forum.ID, &forum.Name) + if err != nil { + InternalError(err,w,r,user) + return + } + + forumList[currentID] = forum + currentID++ + } + err = rows.Err() + if err != nil { + InternalError(err,w,r,user) + return + } + + pi := Page{"Forum Manager","panel-forums",user,forumList,0} + templates.ExecuteTemplate(w,"panel-forums.html", pi) +} + +func route_panel_forums_create_submit(w http.ResponseWriter, r *http.Request){ + user := SessionCheck(w,r) + if !user.Is_Admin { + NoPermissions(w,r,user) + return + } + err := r.ParseForm() + if err != nil { + LocalError("Bad Form", w, r, user) + return + } + + _, err = create_forum_stmt.Exec(r.PostFormValue("forum-name")) + if err != nil { + InternalError(err,w,r,user) + return + } + + http.Redirect(w,r, "/panel/forums/", http.StatusSeeOther) +} \ No newline at end of file diff --git a/src/templates/menu.html b/src/templates/menu.html index 4c8bb58e..5364340a 100644 --- a/src/templates/menu.html +++ b/src/templates/menu.html @@ -6,6 +6,7 @@