2017-01-10 06:51:28 +00:00
/* Copyright Azareal 2017 - 2018 */
package main
2017-06-06 08:47:33 +00:00
import (
"fmt"
"os"
"bytes"
"bufio"
"strconv"
"io/ioutil"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)
2017-01-10 06:51:28 +00:00
var scanner * bufio . Scanner
2017-06-06 08:47:33 +00:00
var db_host , db_username , db_password , db_name string
2017-01-10 06:51:28 +00:00
//var db_collation string = "utf8mb4_general_ci"
var db_port string = "3306"
2017-06-06 08:47:33 +00:00
var site_name , site_url , server_port string
2017-01-10 06:51:28 +00:00
var default_host string = "localhost"
var default_username string = "root"
2017-01-10 08:24:46 +00:00
var default_dbname string = "gosora"
2017-01-10 06:51:28 +00:00
var default_site_name string = "Site Name"
var default_site_url string = "localhost"
var default_server_port string = "80" // 8080's a good one, if you're testing and don't want it to clash with port 80
func main ( ) {
scanner = bufio . NewScanner ( os . Stdin )
fmt . Println ( "Welcome to Gosora's Installer" )
fmt . Println ( "We're going to take you through a few steps to help you get started :)" )
if ! get_database_details ( ) {
err := scanner . Err ( )
if err != nil {
fmt . Println ( err )
} else {
fmt . Println ( "Something went wrong!" )
}
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
if ! get_site_details ( ) {
err := scanner . Err ( )
if err != nil {
fmt . Println ( err )
} else {
fmt . Println ( "Something went wrong!" )
}
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
_db_password := db_password
if ( _db_password != "" ) {
_db_password = ":" + _db_password
}
db , err := sql . Open ( "mysql" , db_username + _db_password + "@tcp(" + db_host + ":" + db_port + ")/" )
if err != nil {
fmt . Println ( err )
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
// Make sure that the connection is alive..
err = db . Ping ( )
if err != nil {
fmt . Println ( err )
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
fmt . Println ( "Successfully connected to the database" )
fmt . Println ( "Opening the database seed file" )
2017-06-06 08:47:33 +00:00
sqlContents , err := ioutil . ReadFile ( "./mysql.sql" )
2017-01-10 06:51:28 +00:00
if err != nil {
fmt . Println ( err )
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
var waste string
err = db . QueryRow ( "SHOW DATABASES LIKE '" + db_name + "'" ) . Scan ( & waste )
if err != nil && err != sql . ErrNoRows {
fmt . Println ( err )
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
if err == sql . ErrNoRows {
fmt . Println ( "Unable to find the database. Attempting to create it" )
_ , err = db . Exec ( "CREATE DATABASE IF NOT EXISTS " + db_name + "" )
if err != nil {
fmt . Println ( err )
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
fmt . Println ( "The database was successfully created" )
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "Switching to database " + db_name )
_ , err = db . Exec ( "USE " + db_name )
if err != nil {
fmt . Println ( err )
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "Preparing installation queries" )
sqlContents = bytes . TrimSpace ( sqlContents )
statements := bytes . Split ( sqlContents , [ ] byte ( ";" ) )
for key , statement := range statements {
if len ( statement ) == 0 {
continue
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "Executing query #" + strconv . Itoa ( key ) + " " + string ( statement ) )
_ , err = db . Exec ( string ( statement ) )
if err != nil {
fmt . Println ( err )
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
}
fmt . Println ( "Finished inserting the database data" )
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
configContents := [ ] byte ( ` package main
2017-05-07 08:31:41 +00:00
// Site Info
var site_name = "` + site_name + `" // Should be a setting in the database
var site_url = "` + site_url + `"
var server_port = "` + server_port + `"
var enable_ssl = false
var ssl_privkey = ""
var ssl_fullchain = ""
2017-01-10 06:51:28 +00:00
// Database details
var dbhost = "` + db_host + `"
var dbuser = "` + db_username + `"
var dbpassword = "` + db_password + `"
var dbname = "` + db_name + `"
var dbport = "` + db_port + `" // You probably won't need to change this
// Limiters
var max_request_size = 5 * megabyte
2017-05-07 08:31:41 +00:00
// Caching
2017-02-11 14:51:16 +00:00
var cache_topicuser = CACHE_STATIC
var user_cache_capacity = 100 // The max number of users held in memory
var topic_cache_capacity = 100 // The max number of topics held in memory
2017-03-15 08:34:14 +00:00
2017-05-07 08:31:41 +00:00
// Email
var site_email = "" // Should be a setting in the database
2017-01-10 06:51:28 +00:00
var smtp_server = ""
2017-03-15 08:34:14 +00:00
var smtp_username = ""
var smtp_password = ""
2017-03-16 05:06:58 +00:00
var smtp_port = "25"
2017-05-07 08:31:41 +00:00
var enable_emails = false
// Misc
var default_route = route_topics
var default_group = 3 // Should be a setting in the database
var activation_group = 5 // Should be a setting in the database
var staff_css = " background-color: #ffeaff;"
var uncategorised_forum_visible = true
var minify_templates = true
2017-06-14 09:55:47 +00:00
var multi_server = false // Experimental: Enable Cross-Server Synchronisation and several other features
2017-03-15 08:34:14 +00:00
2017-01-10 06:51:28 +00:00
//var noavatar = "https://api.adorable.io/avatars/{width}/{id}@{site_url}.png"
var noavatar = "https://api.adorable.io/avatars/285/{id}@" + site_url + ".png"
2017-01-26 13:37:50 +00:00
var items_per_page = 25
2017-01-10 06:51:28 +00:00
// Developer flag
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
var debug_mode = true
2017-05-07 08:31:41 +00:00
var super_debug = false
2017-01-26 13:37:50 +00:00
var profiling = false
2017-01-10 06:51:28 +00:00
` )
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "Opening the configuration file" )
configFile , err := os . Create ( "./config.go" )
if err != nil {
fmt . Println ( err )
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "Writing to the configuration file..." )
_ , err = configFile . Write ( configContents )
if err != nil {
fmt . Println ( err )
fmt . Println ( "Aborting installation..." )
press_any_key ( )
return
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
configFile . Sync ( )
configFile . Close ( )
fmt . Println ( "Finished writing to the configuration file" )
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "Yay, you have successfully installed Gosora!" )
fmt . Println ( "Your name is Admin and you can login with the password 'password'. Don't forget to change it! Seriously. It's really insecure." )
press_any_key ( )
}
func get_database_details ( ) bool {
fmt . Println ( "Database Host? Default: " + default_host )
if ! scanner . Scan ( ) {
return false
}
db_host = scanner . Text ( )
if db_host == "" {
db_host = default_host
}
fmt . Println ( "Set database host to " + db_host )
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "Database Username? Default: " + default_username )
if ! scanner . Scan ( ) {
return false
}
db_username = scanner . Text ( )
if db_username == "" {
db_username = default_username
}
fmt . Println ( "Set database username to " + db_username )
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "Database Password? Default: ''" )
if ! scanner . Scan ( ) {
return false
}
db_password = scanner . Text ( )
if len ( db_password ) == 0 {
fmt . Println ( "You didn't set a password for this user. This won't block the installation process, but it might create security issues in the future.\n" )
} else {
fmt . Println ( "Set password to " + obfuscate_password ( db_password ) )
}
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "Database Name? Pick a name you like or one provided to you. Default: " + default_dbname )
if ! scanner . Scan ( ) {
return false
}
db_name = scanner . Text ( )
if db_name == "" {
db_name = default_dbname
}
fmt . Println ( "Set database name to " + db_name )
return true
}
func get_site_details ( ) bool {
fmt . Println ( "Okay. We also need to know some actual information about your site!" )
fmt . Println ( "What's your site's name? Default: " + default_site_name )
if ! scanner . Scan ( ) {
return false
}
site_name = scanner . Text ( )
if site_name == "" {
site_name = default_site_name
}
fmt . Println ( "Set the site name to " + site_name )
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "What's your site's url? Default: " + default_site_url )
if ! scanner . Scan ( ) {
return false
}
site_url = scanner . Text ( )
if site_url == "" {
site_url = default_site_url
}
fmt . Println ( "Set the site url to " + site_url )
2017-06-06 08:47:33 +00:00
2017-01-10 06:51:28 +00:00
fmt . Println ( "What port do you want the server to listen on? If you don't know what this means, you should probably leave it on the default. Default: " + default_server_port )
if ! scanner . Scan ( ) {
return false
}
server_port = scanner . Text ( )
if server_port == "" {
server_port = default_server_port
}
2017-01-12 02:55:08 +00:00
_ , err := strconv . Atoi ( server_port )
if err != nil {
fmt . Println ( "That's not a valid number!" )
return false
}
2017-01-10 06:51:28 +00:00
fmt . Println ( "Set the server port to " + server_port )
return true
}
func obfuscate_password ( password string ) ( out string ) {
for i := 0 ; i < len ( password ) ; i ++ {
out += "*"
}
return out
}
func press_any_key ( ) {
//fmt.Println("Press any key to exit...")
fmt . Println ( "Please press enter to exit..." )
for scanner . Scan ( ) {
_ = scanner . Text ( )
return
}
2017-06-06 08:47:33 +00:00
}