Exemple #1
0
func Admin(w http.ResponseWriter, r *http.Request) {
	current_user := utils.GetCurrentUser(r)
	if current_user == nil || !current_user.IsAdmin() {
		http.NotFound(w, r)
		return
	}

	var err error
	success := false
	stylesheet, _ := models.GetStringSetting("theme_stylesheet")
	favicon, _ := models.GetStringSetting("favicon_url")
	current_template, _ := models.GetStringSetting("template")

	if r.Method == "POST" {
		stylesheet = r.FormValue("theme_stylesheet")
		favicon = r.FormValue("favicon_url")
		current_template = r.FormValue("template")
		models.SetStringSetting("theme_stylesheet", stylesheet)
		models.SetStringSetting("favicon_url", favicon)
		models.SetStringSetting("template", current_template)
		success = true
	}

	utils.RenderTemplate(w, r, "admin.html", map[string]interface{}{
		"error":            err,
		"success":          success,
		"theme_stylesheet": stylesheet,
		"favicon_url":      favicon,
		"current_template": current_template,
		"templates":        utils.ListTemplates(),
	}, map[string]interface{}{
		"IsCurrentTemplate": func(name string) bool {
			return name == current_template
		},
	})
}
Exemple #2
0
func main() {
	// Get the config file
	var config_path string
	flag.StringVar(&config_path, "config", "gobb.conf", "Specifies the location of a config file")
	run_migrations := flag.Bool("migrate", false, "Runs database migrations")
	ign_migrations := flag.Bool("ignore-migrations", false, "Ignores an out of date database and runs the server anyways")
	serve := flag.Bool("serve", false, "run server")
	useradd := flag.Bool("add-user", false, "add a user")
	var name, password string
	flag.StringVar(&name, "name", "", "username to add")
	flag.StringVar(&password, "password", "", "password new user")
	group := flag.Int64("group", 0, "group of new user (<0 is special group, 0 is default, 1 is mod, 2 is admin)")
	flag.Parse()
	config.GetConfig(config_path)

	// Do we need to run migrations?
	latest_db_version, migrations, err := utils.GetMigrationInfo()
	if len(migrations) != 0 && *run_migrations {
		fmt.Println("[notice] Running database migrations:\n")
		err = utils.RunMigrations(latest_db_version)
		if err != nil {
			fmt.Printf("[error] Could not run migrations (%s)\n", err.Error())
			return
		}

		fmt.Println("\n[notice] Database migration successful!")
	} else if len(migrations) != 0 && !(*ign_migrations) {
		fmt.Println("Your database appears to be out of date. Please run migrations with --migrate or ignore this message with --ignore-migrations")
		return
	}
	db := models.GetDbSession()

	if *useradd {
		user := models.NewUser(name, password)
		user.GroupId = *group
		err = db.Insert(user)

	}

	if !*serve {
		return
	}
	// URL Routing!
	r := mux.NewRouter()
	r.StrictSlash(true)

	r.HandleFunc("/", controllers.Index)
	r.HandleFunc("/register", controllers.Register)
	r.HandleFunc("/login", controllers.Login)
	r.HandleFunc("/logout", controllers.Logout)
	r.HandleFunc("/admin", controllers.Admin)
	r.HandleFunc("/admin/boards", controllers.AdminBoards)
	r.HandleFunc("/admin/users/{id:[0-9]+}", controllers.AdminUser)
	r.HandleFunc("/admin/users", controllers.AdminUsers)
	r.HandleFunc("/action/stick", controllers.ActionStickThread)
	r.HandleFunc("/action/lock", controllers.ActionLockThread)
	r.HandleFunc("/action/delete", controllers.ActionDeleteThread)
	r.HandleFunc("/action/move", controllers.ActionMoveThread)
	r.HandleFunc("/action/mark_read", controllers.ActionMarkAllRead)
	r.HandleFunc("/action/edit", controllers.PostEditor)
	r.HandleFunc("/board/{id:[0-9]+}", controllers.Board)
	r.HandleFunc("/board/{board_id:[0-9]+}/new", controllers.PostEditor)
	r.HandleFunc("/board/{board_id:[0-9]+}/{post_id:[0-9]+}", controllers.Thread)
	r.HandleFunc("/user/{id:[0-9]+}", controllers.User)
	r.HandleFunc("/user/{id:[0-9]+}/settings", controllers.UserSettings)

	// Handle static files
	selected_template, _ := models.GetStringSetting("template")
	base_path, _ := config.Config.GetString("gobb", "base_path")
	if selected_template == "default" {
		pkg, _ := build.Import("github.com/stevenleeg/gobb/gobb", ".", build.FindOnly)
		static_path := filepath.Join(pkg.SrcRoot, pkg.ImportPath, "../templates")
		r.PathPrefix("/static/").Handler(http.FileServer(http.Dir(static_path)))
	} else {
		static_path := filepath.Join(base_path, "templates", selected_template)
		r.PathPrefix("/static/").Handler(http.FileServer(http.Dir(static_path)))
	}

	// User provided static files
	static_path, err := config.Config.GetString("gobb", "base_path")
	if err == nil {
		r.PathPrefix("/assets/").Handler(http.FileServer(http.Dir(static_path)))
	}

	http.Handle("/", r)

	port, err := config.Config.GetString("gobb", "port")
	fmt.Println("[notice] Starting server on port " + port)
	http.ListenAndServe(":"+port, nil)
}
Exemple #3
0
func RenderTemplate(
	out http.ResponseWriter,
	r *http.Request,
	tpl_file string,
	context map[string]interface{},
	funcs template.FuncMap) {

	current_user := GetCurrentUser(r)
	site_name, _ := config.Config.GetString("gobb", "site_name")
	ga_tracking_id, _ := config.Config.GetString("googleanalytics", "tracking_id")
	ga_account, _ := config.Config.GetString("googleanalytics", "account")

	stylesheet := ""
	if (current_user != nil) && current_user.StylesheetUrl.Valid && current_user.StylesheetUrl.String != "" {
		stylesheet = current_user.StylesheetUrl.String
	} else if current_user == nil || !current_user.StylesheetUrl.Valid || current_user.StylesheetUrl.String == "" {
		global_theme, _ := models.GetStringSetting("theme_stylesheet")
		if global_theme != "" {
			stylesheet = global_theme
		}
	}

	favicon_url, _ := models.GetStringSetting("favicon_url")

	send := map[string]interface{}{
		"current_user":   current_user,
		"request":        r,
		"site_name":      site_name,
		"ga_tracking_id": ga_tracking_id,
		"ga_account":     ga_account,
		"stylesheet":     stylesheet,
		"favicon_url":    favicon_url,
	}

	// Merge the global template variables with the local context
	for key, val := range context {
		send[key] = val
	}

	// Same with the function map
	func_map := default_funcmap
	func_map["GetCurrentUser"] = tplGetCurrentUser(r)
	for key, val := range funcs {
		func_map[key] = val
	}

	// Get the base template path
	selected_template, _ := models.GetStringSetting("template")
	var base_path string
	if selected_template == "default" {
		pkg, _ := build.Import("github.com/stevenleeg/gobb/gobb", ".", build.FindOnly)
		base_path = filepath.Join(pkg.SrcRoot, pkg.ImportPath, "../templates/")
	} else {
		base_path, _ = config.Config.GetString("gobb", "base_path")
		base_path = filepath.Join(base_path, "templates", selected_template)
	}

	base_tpl := filepath.Join(base_path, "base.html")
	rend_tpl := filepath.Join(base_path, tpl_file)

	tpl, err := template.New("tpl").Funcs(func_map).ParseFiles(base_tpl, rend_tpl)
	if err != nil {
		fmt.Printf("[error] Could not parse template (%s)\n", err.Error())
	}

	// Attempt to execute the template we're on
	err = tpl.ExecuteTemplate(out, tpl_file, send)
	if err != nil {
		fmt.Printf("[error] Could not parse template (%s)\n", err.Error())
	}

	// And now the base template
	err = tpl.ExecuteTemplate(out, "base.html", send)
	if err != nil {
		fmt.Printf("[error] Could not parse template (%s)\n", err.Error())
	}
}
Exemple #4
0
func tplGetStringSetting(key string) string {
	val, _ := models.GetStringSetting(key)
	return val
}