diff --git a/ren4.PNG b/ren4.PNG new file mode 100644 index 00000000..015dee17 Binary files /dev/null and b/ren4.PNG differ diff --git a/src/config.go b/src/config.go index 73962c8c..93415042 100644 --- a/src/config.go +++ b/src/config.go @@ -8,4 +8,7 @@ var dbname = "grosolo" var dbport = "3306" // You probably won't need to change this // Limiters -var max_request_size = 5 * megabyte \ No newline at end of file +var max_request_size = 5 * megabyte + +// Misc +var default_route = route_topics \ No newline at end of file diff --git a/src/main.go b/src/main.go index 4311d2e7..ce0630fc 100644 --- a/src/main.go +++ b/src/main.go @@ -30,6 +30,7 @@ var logout_stmt *sql.Stmt var set_password_stmt *sql.Stmt var get_password_stmt *sql.Stmt var set_avatar_stmt *sql.Stmt +var set_username_stmt *sql.Stmt var register_stmt *sql.Stmt var username_exists_stmt *sql.Stmt var custom_pages map[string]string = make(map[string]string) @@ -122,6 +123,12 @@ func init_database(err error) { log.Fatal(err) } + log.Print("Preparing set_username statement.") + set_username_stmt, err = db.Prepare("UPDATE users SET name = ? WHERE uid = ?") + if err != nil { + log.Fatal(err) + } + // Add an admin version of register_stmt with more flexibility // create_account_stmt, err = db.Prepare("INSERT INTO @@ -183,9 +190,11 @@ func main(){ http.HandleFunc("/user/edit/critical/submit/", route_account_own_edit_critical_submit) http.HandleFunc("/user/edit/avatar/", route_account_own_edit_avatar) http.HandleFunc("/user/edit/avatar/submit/", route_account_own_edit_avatar_submit) + http.HandleFunc("/user/edit/username/", route_account_own_edit_username) + 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) - http.HandleFunc("/", route_topics) + http.HandleFunc("/", default_route) defer db.Close() http.ListenAndServe(":8080", nil) diff --git a/src/pages.go b/src/pages.go index a60fc68a..7b2b6af3 100644 --- a/src/pages.go +++ b/src/pages.go @@ -37,4 +37,8 @@ func add_custom_page(path string, f os.FileInfo, err error) error { name := strings.TrimSuffix(path, filepath.Ext(path)) custom_pages[name] = string(custom_page) return nil +} + +func parse_message(msg string) string { + return strings.Replace(msg,"\n","
",-1) } \ No newline at end of file diff --git a/src/routes.go b/src/routes.go index 5064630a..ddb075d8 100644 --- a/src/routes.go +++ b/src/routes.go @@ -62,11 +62,13 @@ func route_topics(w http.ResponseWriter, r *http.Request){ createdAt string parentID int status string + name string + avatar string ) topicList = make(map[int]interface{}) currentID = 0 - rows, err := db.Query("select tid, title, content, createdBy, is_closed, sticky, createdAt, parentID from topics") + rows, err := db.Query("select topics.tid, topics.title, topics.content, topics.createdBy, topics.is_closed, topics.sticky, topics.createdAt, topics.parentID, users.name, users.avatar from topics left join users ON topics.createdBy = users.uid") if err != nil { InternalError(err,w,r,user) return @@ -74,7 +76,7 @@ func route_topics(w http.ResponseWriter, r *http.Request){ defer rows.Close() for rows.Next() { - err := rows.Scan(&tid, &title, &content, &createdBy, &is_closed, &sticky, &createdAt, &parentID) + err := rows.Scan(&tid, &title, &content, &createdBy, &is_closed, &sticky, &createdAt, &parentID, &name, &avatar) if err != nil { InternalError(err,w,r,user) return @@ -85,7 +87,11 @@ func route_topics(w http.ResponseWriter, r *http.Request){ } else { status = "open" } - topicList[currentID] = Topic{tid, title, content, createdBy, is_closed, sticky, createdAt,parentID, status} + if avatar != "" && avatar[0] == '.' { + avatar = "/uploads/avatar_" + strconv.Itoa(createdBy) + avatar + } + + topicList[currentID] = TopicUser{tid, title, content, createdBy, is_closed, sticky, createdAt,parentID, status, name, avatar} currentID++ } err = rows.Err() @@ -174,7 +180,7 @@ func route_topic_id(w http.ResponseWriter, r *http.Request){ replyAvatar = "/uploads/avatar_" + strconv.Itoa(user.ID) + replyAvatar } - replyList[currentID] = Reply{rid,topic.ID,replyContent,template.HTML(strings.Replace(replyContent,"\n","
",-1)),replyCreatedBy,replyCreatedByName,replyCreatedAt,replyLastEdit,replyLastEditBy,replyAvatar} + replyList[currentID] = Reply{rid,topic.ID,replyContent,template.HTML(parse_message(replyContent)),replyCreatedBy,replyCreatedByName,replyCreatedAt,replyLastEdit,replyLastEditBy,replyAvatar} currentID++ } err = rows.Err() @@ -211,7 +217,7 @@ func route_create_topic(w http.ResponseWriter, r *http.Request) { } success := 1 - res, err := create_topic_stmt.Exec(html.EscapeString(r.PostFormValue("topic-name")),html.EscapeString(r.PostFormValue("topic-content")),strings.Replace(html.EscapeString(r.PostFormValue("topic-content")),"\n","
",-1),user.ID) + res, err := create_topic_stmt.Exec(html.EscapeString(r.PostFormValue("topic-name")),html.EscapeString(r.PostFormValue("topic-content")),parse_message(html.EscapeString(r.PostFormValue("topic-content"))),user.ID) if err != nil { log.Print(err) success = 0 @@ -266,7 +272,7 @@ func route_create_reply(w http.ResponseWriter, r *http.Request) { return } - _, err = create_reply_stmt.Exec(tid,html.EscapeString(r.PostFormValue("reply-content")),strings.Replace(html.EscapeString(r.PostFormValue("reply-content")),"\n","
",-1),user.ID) + _, err = create_reply_stmt.Exec(tid,html.EscapeString(r.PostFormValue("reply-content")),parse_message(html.EscapeString(r.PostFormValue("reply-content"))),user.ID) if err != nil { log.Print(err) success = 0 @@ -311,14 +317,17 @@ func route_edit_topic(w http.ResponseWriter, r *http.Request) { topic_name := r.PostFormValue("topic_name") topic_status := r.PostFormValue("topic_status") + //log.Print(topic_name) + //log.Print(topic_status) var is_closed bool if topic_status == "closed" { is_closed = true } else { is_closed = false } - topic_content := html.EscapeString(r.PostFormValue("topic-content")) - _, err = edit_topic_stmt.Exec(topic_name, topic_content, strings.Replace(topic_content,"\n","
",-1), is_closed, tid) + topic_content := html.EscapeString(r.PostFormValue("topic_content")) + //log.Print(topic_content) + _, err = edit_topic_stmt.Exec(topic_name, topic_content, parse_message(topic_content), is_closed, tid) if err != nil { InternalErrorJSQ(err,w,r,user,is_js) return @@ -356,7 +365,7 @@ func route_reply_edit_submit(w http.ResponseWriter, r *http.Request) { } content := html.EscapeString(r.PostFormValue("edit_item")) - _, err = edit_reply_stmt.Exec(content, strings.Replace(content,"\n","
",-1), rid) + _, err = edit_reply_stmt.Exec(content, parse_message(content), rid) if err != nil { InternalError(err,w,r,user) return @@ -624,6 +633,55 @@ func route_account_own_edit_avatar_submit(w http.ResponseWriter, r *http.Request pi := Page{"Edit Avatar","account-own-edit-avatar-success",user,tList,0} templates.ExecuteTemplate(w,"account-own-edit-avatar-success.html", pi) } + +func route_account_own_edit_username(w http.ResponseWriter, r *http.Request) { + user := SessionCheck(w,r) + if !user.Loggedin { + errmsg := "You need to login to edit your own account." + pi := Page{"Error","error",user,tList,errmsg} + + var b bytes.Buffer + templates.ExecuteTemplate(&b,"error.html", pi) + errpage := b.String() + http.Error(w,errpage,500) + return + } + + pi := Page{"Edit Username","account-own-edit-username",user,tList,user.Name} + templates.ExecuteTemplate(w,"account-own-edit-username.html", pi) +} + +func route_account_own_edit_username_submit(w http.ResponseWriter, r *http.Request) { + user := SessionCheck(w,r) + if !user.Loggedin { + errmsg := "You need to login to edit your own account." + pi := Page{"Error","error",user,tList,errmsg} + + var b bytes.Buffer + templates.ExecuteTemplate(&b,"error.html", pi) + errpage := b.String() + http.Error(w,errpage,500) + return + } + + err := r.ParseForm() + if err != nil { + LocalError("Bad Form", w, r, user) + return + } + + new_username := html.EscapeString(r.PostFormValue("account-new-username")) + _, err = set_username_stmt.Exec(new_username, strconv.Itoa(user.ID)) + if err != nil { + InternalError(err,w,r,user) + return + } + user.Name = new_username + + pi := Page{"Edit Username","account-own-edit-username",user,tList,user.Name} + templates.ExecuteTemplate(w,"account-own-edit-username.html", pi) +} + func route_logout(w http.ResponseWriter, r *http.Request) { user := SessionCheck(w,r) if !user.Loggedin { diff --git a/src/templates/account-own-edit-avatar-success.html b/src/templates/account-own-edit-avatar-success.html index c6d333cc..d914082a 100644 --- a/src/templates/account-own-edit-avatar-success.html +++ b/src/templates/account-own-edit-avatar-success.html @@ -3,6 +3,7 @@
My Account
Edit Avatar
+
Edit Username
Edit Password
Coming Soon
Coming Soon
diff --git a/src/templates/account-own-edit-avatar.html b/src/templates/account-own-edit-avatar.html index ffdf7ac9..c45d8936 100644 --- a/src/templates/account-own-edit-avatar.html +++ b/src/templates/account-own-edit-avatar.html @@ -2,6 +2,7 @@
+ diff --git a/src/templates/account-own-edit-success.html b/src/templates/account-own-edit-success.html index 607e22d4..8a08a300 100644 --- a/src/templates/account-own-edit-success.html +++ b/src/templates/account-own-edit-success.html @@ -3,6 +3,7 @@
+ diff --git a/src/templates/account-own-edit-username.html b/src/templates/account-own-edit-username.html new file mode 100644 index 00000000..ab825145 --- /dev/null +++ b/src/templates/account-own-edit-username.html @@ -0,0 +1,29 @@ +{{template "header.html" . }} + + +
+
+
+ +
{{.Something}}
+
+
+ +
+
+
+
+
+ +
+{{template "footer.html" . }} \ No newline at end of file diff --git a/src/templates/account-own-edit.html b/src/templates/account-own-edit.html index 6c87a01f..68ae2b6d 100644 --- a/src/templates/account-own-edit.html +++ b/src/templates/account-own-edit.html @@ -2,6 +2,7 @@
+ diff --git a/src/templates/topics.html b/src/templates/topics.html index 53d8564c..0de8b8d2 100644 --- a/src/templates/topics.html +++ b/src/templates/topics.html @@ -3,8 +3,8 @@
- {{range .ItemList}}
- {{.Title}} + {{range .ItemList}}
+ {{.Title}} {{if .Is_Closed}}closed{{else}}open{{end}}
{{end}}
{{template "footer.html" . }} \ No newline at end of file