2016-12-11 16:06:17 +00:00
/* Copyright Azareal 2016 - 2017 */
2017-06-06 14:41:06 +00:00
// +build !pgsql !sqlite !mssql
2016-12-09 13:46:29 +00:00
package main
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
import "log"
2016-12-21 02:30:32 +00:00
import "fmt"
2017-01-21 18:16:27 +00:00
import "strconv"
2016-12-21 02:30:32 +00:00
import "encoding/json"
2016-12-09 13:46:29 +00:00
var db * sql . DB
2017-02-15 10:49:30 +00:00
var db_version string
2017-04-12 10:10:36 +00:00
var db_collation string = "utf8mb4_general_ci"
2017-02-15 10:49:30 +00:00
2017-06-06 14:41:06 +00:00
var get_topic_replies_offset_stmt * sql . Stmt // I'll need to rewrite this one to stop it hard-coding the per page setting before moving it to the query generator
2017-01-26 13:37:50 +00:00
var get_forum_topics_offset_stmt * sql . Stmt
2016-12-11 16:06:17 +00:00
var create_report_stmt * sql . Stmt
2016-12-09 13:46:29 +00:00
var create_reply_stmt * sql . Stmt
2017-04-02 13:00:40 +00:00
var create_action_reply_stmt * sql . Stmt
2017-01-21 18:16:27 +00:00
var add_replies_to_topic_stmt * sql . Stmt
var remove_replies_from_topic_stmt * sql . Stmt
2017-01-26 13:37:50 +00:00
var add_topics_to_forum_stmt * sql . Stmt
var remove_topics_from_forum_stmt * sql . Stmt
2016-12-09 13:46:29 +00:00
var update_forum_cache_stmt * sql . Stmt
2017-02-10 13:39:13 +00:00
var create_like_stmt * sql . Stmt
var add_likes_to_topic_stmt * sql . Stmt
var add_likes_to_reply_stmt * sql . Stmt
2017-03-03 16:28:49 +00:00
var add_activity_stmt * sql . Stmt
var notify_watchers_stmt * sql . Stmt
var notify_one_stmt * sql . Stmt
2017-03-05 07:53:41 +00:00
var add_subscription_stmt * sql . Stmt
2016-12-09 13:46:29 +00:00
var edit_topic_stmt * sql . Stmt
var edit_reply_stmt * sql . Stmt
var delete_reply_stmt * sql . Stmt
var delete_topic_stmt * sql . Stmt
var stick_topic_stmt * sql . Stmt
var unstick_topic_stmt * sql . Stmt
2017-02-28 09:27:28 +00:00
var get_activity_feed_by_watcher_stmt * sql . Stmt
2017-01-17 07:55:46 +00:00
var update_last_ip_stmt * sql . Stmt
2016-12-09 13:46:29 +00:00
var update_session_stmt * sql . Stmt
var logout_stmt * sql . Stmt
var set_password_stmt * sql . Stmt
var set_avatar_stmt * sql . Stmt
var set_username_stmt * sql . Stmt
2017-01-03 07:47:31 +00:00
var add_email_stmt * sql . Stmt
var update_email_stmt * sql . Stmt
var verify_email_stmt * sql . Stmt
2016-12-09 13:46:29 +00:00
var register_stmt * sql . Stmt
var change_group_stmt * sql . Stmt
2016-12-18 12:56:06 +00:00
var activate_user_stmt * sql . Stmt
2017-01-12 02:55:08 +00:00
var update_user_level_stmt * sql . Stmt
var increment_user_score_stmt * sql . Stmt
var increment_user_posts_stmt * sql . Stmt
var increment_user_bigposts_stmt * sql . Stmt
var increment_user_megaposts_stmt * sql . Stmt
var increment_user_topics_stmt * sql . Stmt
2016-12-09 13:46:29 +00:00
var create_profile_reply_stmt * sql . Stmt
var edit_profile_reply_stmt * sql . Stmt
var delete_profile_reply_stmt * sql . Stmt
var create_forum_stmt * sql . Stmt
var delete_forum_stmt * sql . Stmt
var update_forum_stmt * sql . Stmt
2017-01-26 13:37:50 +00:00
var forum_entry_exists_stmt * sql . Stmt
2017-03-27 07:09:28 +00:00
var group_entry_exists_stmt * sql . Stmt
2017-02-04 06:19:55 +00:00
var delete_forum_perms_by_forum_stmt * sql . Stmt
var add_forum_perms_to_forum_stmt * sql . Stmt
var add_forum_perms_to_forum_admins_stmt * sql . Stmt
var add_forum_perms_to_forum_staff_stmt * sql . Stmt
var add_forum_perms_to_forum_members_stmt * sql . Stmt
2017-03-27 07:09:28 +00:00
var add_forum_perms_to_group_stmt * sql . Stmt
2017-06-05 11:57:27 +00:00
//var forum_perm_exists_for_group_stmt *sql.Stmt
var update_forum_perms_for_group_stmt * sql . Stmt
2016-12-09 13:46:29 +00:00
var update_setting_stmt * sql . Stmt
2016-12-11 16:06:17 +00:00
var add_plugin_stmt * sql . Stmt
2016-12-13 02:14:14 +00:00
var update_plugin_stmt * sql . Stmt
2016-12-23 12:35:22 +00:00
var update_user_stmt * sql . Stmt
2017-03-23 12:13:50 +00:00
var update_group_perms_stmt * sql . Stmt
2017-03-18 07:23:02 +00:00
var update_group_rank_stmt * sql . Stmt
var update_group_stmt * sql . Stmt
2017-03-27 07:09:28 +00:00
var create_group_stmt * sql . Stmt
2017-01-01 15:45:43 +00:00
var add_theme_stmt * sql . Stmt
var update_theme_stmt * sql . Stmt
2017-04-05 14:05:37 +00:00
var add_modlog_entry_stmt * sql . Stmt
var add_adminlog_entry_stmt * sql . Stmt
2017-06-06 14:41:06 +00:00
var todays_post_count_stmt * sql . Stmt
var todays_topic_count_stmt * sql . Stmt
var todays_report_count_stmt * sql . Stmt
var todays_newuser_count_stmt * sql . Stmt
2016-12-09 13:46:29 +00:00
2017-04-12 10:10:36 +00:00
func init_database ( ) ( err error ) {
2016-12-09 13:46:29 +00:00
if ( dbpassword != "" ) {
dbpassword = ":" + dbpassword
}
2017-06-05 11:57:27 +00:00
2017-05-02 17:24:33 +00:00
// Open the database connection
2017-04-12 10:10:36 +00:00
db , err = sql . Open ( "mysql" , dbuser + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?collation=" + db_collation )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-05-02 17:24:33 +00:00
// Make sure that the connection is alive
2016-12-09 13:46:29 +00:00
err = db . Ping ( )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-05-02 17:24:33 +00:00
// Fetch the database version
2017-02-15 10:49:30 +00:00
db . QueryRow ( "SELECT VERSION()" ) . Scan ( & db_version )
2017-06-05 11:57:27 +00:00
2017-05-02 17:24:33 +00:00
// Set the number of max open connections
db . SetMaxOpenConns ( 64 )
2017-06-05 11:57:27 +00:00
// Build the generated prepared statements, we are going to slowly move the queries over to the query generator rather than writing them all by hand, this'll make it easier for us to implement database adapters for other databases like PostgreSQL, MSSQL, SQlite, etc.
err = gen_mysql ( )
2016-12-23 12:35:22 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-23 12:35:22 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-21 18:16:27 +00:00
log . Print ( "Preparing get_topic_replies_offset statement." )
2017-04-02 13:00:40 +00:00
get_topic_replies_offset_stmt , err = db . Prepare ( "select replies.rid, replies.content, replies.createdBy, replies.createdAt, replies.lastEdit, replies.lastEditBy, users.avatar, users.name, users.group, users.url_prefix, users.url_name, users.level, replies.ipaddress, replies.likeCount, replies.actionType from replies left join users on replies.createdBy = users.uid where tid = ? limit ?, " + strconv . Itoa ( items_per_page ) )
2017-01-17 07:55:46 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-17 07:55:46 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-26 13:37:50 +00:00
log . Print ( "Preparing get_forum_topics_offset statement." )
2017-06-05 11:57:27 +00:00
get_forum_topics_offset_stmt , err = db . Prepare ( "select topics.tid, topics.title, topics.content, topics.createdBy, topics.is_closed, topics.sticky, topics.createdAt, topics.lastReplyAt, topics.parentID, topics.postCount, topics.likeCount, users.name, users.avatar from topics left join users ON topics.createdBy = users.uid WHERE topics.parentID = ? order by topics.sticky DESC, topics.lastReplyAt DESC, topics.createdBy DESC limit ?, " + strconv . Itoa ( items_per_page ) )
2017-01-17 07:55:46 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-17 07:55:46 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-11 16:06:17 +00:00
log . Print ( "Preparing create_report statement." )
2017-06-05 11:57:27 +00:00
create_report_stmt , err = db . Prepare ( "INSERT INTO topics(title,content,parsed_content,createdAt,lastReplyAt,createdBy,data,parentID,css_class) VALUES(?,?,?,NOW(),NOW(),?,?,1,'report')" )
2016-12-11 16:06:17 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-11 16:06:17 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing create_reply statement." )
2017-02-10 13:39:13 +00:00
create_reply_stmt , err = db . Prepare ( "INSERT INTO replies(tid,content,parsed_content,createdAt,ipaddress,words,createdBy) VALUES(?,?,?,NOW(),?,?,?)" )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-04-02 13:00:40 +00:00
log . Print ( "Preparing create_action_reply statement." )
create_action_reply_stmt , err = db . Prepare ( "INSERT INTO replies(tid,actionType,ipaddress,createdBy) VALUES(?,?,?,?)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-04-02 13:00:40 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-21 18:16:27 +00:00
log . Print ( "Preparing add_replies_to_topic statement." )
2017-02-06 04:52:19 +00:00
add_replies_to_topic_stmt , err = db . Prepare ( "UPDATE topics SET postCount = postCount + ?, lastReplyAt = NOW() WHERE tid = ?" )
2017-01-21 18:16:27 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-21 18:16:27 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-21 18:16:27 +00:00
log . Print ( "Preparing remove_replies_from_topic statement." )
remove_replies_from_topic_stmt , err = db . Prepare ( "UPDATE topics SET postCount = postCount - ? WHERE tid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-21 18:16:27 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-26 13:37:50 +00:00
log . Print ( "Preparing add_topics_to_forum statement." )
add_topics_to_forum_stmt , err = db . Prepare ( "UPDATE forums SET topicCount = topicCount + ? WHERE fid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-26 13:37:50 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-26 13:37:50 +00:00
log . Print ( "Preparing remove_topics_from_forum statement." )
remove_topics_from_forum_stmt , err = db . Prepare ( "UPDATE forums SET topicCount = topicCount - ? WHERE fid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-26 13:37:50 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing update_forum_cache statement." )
update_forum_cache_stmt , err = db . Prepare ( "UPDATE forums SET lastTopic = ?, lastTopicID = ?, lastReplyer = ?, lastReplyerID = ?, lastTopicTime = NOW() WHERE fid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-10 13:39:13 +00:00
log . Print ( "Preparing create_like statement." )
create_like_stmt , err = db . Prepare ( "INSERT INTO likes(weight, targetItem, targetType, sentBy) VALUES(?,?,?,?)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-10 13:39:13 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-10 13:39:13 +00:00
log . Print ( "Preparing add_likes_to_topic statement." )
add_likes_to_topic_stmt , err = db . Prepare ( "UPDATE topics SET likeCount = likeCount + ? WHERE tid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-10 13:39:13 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-10 13:39:13 +00:00
log . Print ( "Preparing add_likes_to_reply statement." )
add_likes_to_reply_stmt , err = db . Prepare ( "UPDATE replies SET likeCount = likeCount + ? WHERE rid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-03 16:28:49 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-03 16:28:49 +00:00
log . Print ( "Preparing add_activity statement." )
add_activity_stmt , err = db . Prepare ( "INSERT INTO activity_stream(actor,targetUser,event,elementType,elementID) VALUES(?,?,?,?,?)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-03 16:28:49 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-03 16:28:49 +00:00
log . Print ( "Preparing notify_watchers statement." )
2017-03-05 07:53:41 +00:00
notify_watchers_stmt , err = db . Prepare ( "INSERT INTO activity_stream_matches(watcher, asid) SELECT activity_subscriptions.user, activity_stream.asid FROM activity_stream INNER JOIN activity_subscriptions ON activity_subscriptions.targetType = activity_stream.elementType and activity_subscriptions.targetID = activity_stream.elementID and activity_subscriptions.user != activity_stream.actor where asid = ?" )
2017-03-03 16:28:49 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-03 16:28:49 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-03 16:28:49 +00:00
log . Print ( "Preparing notify_one statement." )
notify_one_stmt , err = db . Prepare ( "INSERT INTO activity_stream_matches(watcher,asid) VALUES(?,?)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-10 13:39:13 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-05 07:53:41 +00:00
log . Print ( "Preparing add_subscription statement." )
add_subscription_stmt , err = db . Prepare ( "INSERT INTO activity_subscriptions(user,targetID,targetType,level) VALUES(?,?,?,2)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-05 07:53:41 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing edit_topic statement." )
edit_topic_stmt , err = db . Prepare ( "UPDATE topics SET title = ?, content = ?, parsed_content = ?, is_closed = ? WHERE tid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing edit_reply statement." )
edit_reply_stmt , err = db . Prepare ( "UPDATE replies SET content = ?, parsed_content = ? WHERE rid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing delete_reply statement." )
delete_reply_stmt , err = db . Prepare ( "DELETE FROM replies WHERE rid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing delete_topic statement." )
delete_topic_stmt , err = db . Prepare ( "DELETE FROM topics WHERE tid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing stick_topic statement." )
stick_topic_stmt , err = db . Prepare ( "UPDATE topics SET sticky = 1 WHERE tid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing unstick_topic statement." )
unstick_topic_stmt , err = db . Prepare ( "UPDATE topics SET sticky = 0 WHERE tid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-28 09:27:28 +00:00
log . Print ( "Preparing get_activity_feed_by_watcher statement." )
2017-03-05 07:53:41 +00:00
get_activity_feed_by_watcher_stmt , err = db . Prepare ( "SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID FROM `activity_stream_matches` INNER JOIN `activity_stream` ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE `watcher` = ?" )
2017-02-28 09:27:28 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-28 09:27:28 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-17 07:55:46 +00:00
log . Print ( "Preparing update_last_ip statement." )
update_last_ip_stmt , err = db . Prepare ( "UPDATE users SET last_ip = ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-17 07:55:46 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing update_session statement." )
update_session_stmt , err = db . Prepare ( "UPDATE users SET session = ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing logout statement." )
logout_stmt , err = db . Prepare ( "UPDATE users SET session = '' WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing set_password statement." )
set_password_stmt , err = db . Prepare ( "UPDATE users SET password = ?, salt = ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing set_avatar statement." )
set_avatar_stmt , err = db . Prepare ( "UPDATE users SET avatar = ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing set_username statement." )
set_username_stmt , err = db . Prepare ( "UPDATE users SET name = ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
// Add an admin version of register_stmt with more flexibility
2017-06-05 11:57:27 +00:00
// create_account_stmt, err = db.Prepare("INSERT INTO
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing register statement." )
2016-12-21 02:30:32 +00:00
register_stmt , err = db . Prepare ( "INSERT INTO users(`name`,`email`,`password`,`salt`,`group`,`is_super_admin`,`session`,`active`,`message`) VALUES(?,?,?,?,?,0,?,?,'')" )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing change_group statement." )
2017-01-26 13:37:50 +00:00
change_group_stmt , err = db . Prepare ( "update `users` set `group` = ? where `uid` = ?" )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-03 07:47:31 +00:00
log . Print ( "Preparing add_email statement." )
add_email_stmt , err = db . Prepare ( "INSERT INTO emails(`email`,`uid`,`validated`,`token`) VALUES(?,?,?,?)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-03 07:47:31 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-03 07:47:31 +00:00
log . Print ( "Preparing update_email statement." )
update_email_stmt , err = db . Prepare ( "UPDATE emails SET email = ?, uid = ?, validated = ?, token = ? WHERE email = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-03 07:47:31 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-03 07:47:31 +00:00
log . Print ( "Preparing verify_email statement." )
verify_email_stmt , err = db . Prepare ( "UPDATE emails SET validated = 1, token = '' WHERE email = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-03 07:47:31 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-18 12:56:06 +00:00
log . Print ( "Preparing activate_user statement." )
activate_user_stmt , err = db . Prepare ( "UPDATE users SET active = 1 WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-18 12:56:06 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-12 02:55:08 +00:00
log . Print ( "Preparing update_user_level statement." )
update_user_level_stmt , err = db . Prepare ( "UPDATE users SET level = ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-12 02:55:08 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-12 02:55:08 +00:00
log . Print ( "Preparing increment_user_score statement." )
increment_user_score_stmt , err = db . Prepare ( "UPDATE users SET score = score + ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-12 02:55:08 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-12 02:55:08 +00:00
log . Print ( "Preparing increment_user_posts statement." )
increment_user_posts_stmt , err = db . Prepare ( "UPDATE users SET posts = posts + ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-12 02:55:08 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-12 02:55:08 +00:00
log . Print ( "Preparing increment_user_bigposts statement." )
increment_user_bigposts_stmt , err = db . Prepare ( "UPDATE users SET posts = posts + ?, bigposts = bigposts + ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-12 02:55:08 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-12 02:55:08 +00:00
log . Print ( "Preparing increment_user_megaposts statement." )
increment_user_megaposts_stmt , err = db . Prepare ( "UPDATE users SET posts = posts + ?, bigposts = bigposts + ?, megaposts = megaposts + ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-12 02:55:08 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-12 02:55:08 +00:00
log . Print ( "Preparing increment_user_topics statement." )
increment_user_topics_stmt , err = db . Prepare ( "UPDATE users SET topics = topics + ? WHERE uid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-12 02:55:08 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing create_profile_reply statement." )
create_profile_reply_stmt , err = db . Prepare ( "INSERT INTO users_replies(uid,content,parsed_content,createdAt,createdBy) VALUES(?,?,?,NOW(),?)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing edit_profile_reply statement." )
edit_profile_reply_stmt , err = db . Prepare ( "UPDATE users_replies SET content = ?, parsed_content = ? WHERE rid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing delete_profile_reply statement." )
delete_profile_reply_stmt , err = db . Prepare ( "DELETE FROM users_replies WHERE rid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing create_forum statement." )
2017-06-05 11:57:27 +00:00
create_forum_stmt , err = db . Prepare ( "INSERT INTO forums(`name`,`desc`,`active`,`preset`) VALUES(?,?,?,?)" )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing delete_forum statement." )
2017-06-05 11:57:27 +00:00
//delete_forum_stmt, err = db.Prepare("delete from forums where fid = ?")
2017-01-26 13:37:50 +00:00
delete_forum_stmt , err = db . Prepare ( "update forums set name= '', active = 0 where fid = ?" )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing update_forum statement." )
2017-06-05 11:57:27 +00:00
update_forum_stmt , err = db . Prepare ( "update forums set `name` = ?, `desc` = ?, `active` = ?, `preset` = ? where fid = ?" )
2017-01-26 13:37:50 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-26 13:37:50 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-26 13:37:50 +00:00
log . Print ( "Preparing forum_entry_exists statement." )
forum_entry_exists_stmt , err = db . Prepare ( "SELECT `fid` FROM `forums` WHERE `name` = '' order by fid asc limit 1" )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-27 07:09:28 +00:00
log . Print ( "Preparing group_entry_exists statement." )
group_entry_exists_stmt , err = db . Prepare ( "SELECT `gid` FROM `users_groups` WHERE `name` = '' order by gid asc limit 1" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-27 07:09:28 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-04 06:19:55 +00:00
log . Print ( "Preparing delete_forum_perms_by_forum statement." )
delete_forum_perms_by_forum_stmt , err = db . Prepare ( "DELETE FROM forums_permissions WHERE fid = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-04 06:19:55 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-04 06:19:55 +00:00
log . Print ( "Preparing add_forum_perms_to_forum statement." )
add_forum_perms_to_forum_stmt , err = db . Prepare ( "INSERT INTO forums_permissions(gid,fid,preset,permissions) VALUES(?,?,?,?)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-04 06:19:55 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-04 06:19:55 +00:00
log . Print ( "Preparing add_forum_perms_to_forum_admins statement." )
2017-02-28 09:27:28 +00:00
add_forum_perms_to_forum_admins_stmt , err = db . Prepare ( "INSERT INTO forums_permissions(gid,fid,preset,permissions) SELECT `gid`,? AS fid,? AS preset,? AS permissions FROM users_groups WHERE is_admin = 1" )
2017-02-04 06:19:55 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-04 06:19:55 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-04 06:19:55 +00:00
log . Print ( "Preparing add_forum_perms_to_forum_staff statement." )
2017-02-28 09:27:28 +00:00
add_forum_perms_to_forum_staff_stmt , err = db . Prepare ( "INSERT INTO forums_permissions(gid,fid,preset,permissions) SELECT `gid`,? AS fid,? AS preset,? AS permissions FROM users_groups WHERE is_admin = 0 AND is_mod = 1" )
2017-02-04 06:19:55 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-04 06:19:55 +00:00
}
2017-06-05 11:57:27 +00:00
2017-02-04 06:19:55 +00:00
log . Print ( "Preparing add_forum_perms_to_forum_members statement." )
2017-02-28 09:27:28 +00:00
add_forum_perms_to_forum_members_stmt , err = db . Prepare ( "INSERT INTO forums_permissions(gid,fid,preset,permissions) SELECT `gid`,? AS fid,? AS preset,? AS permissions FROM users_groups WHERE is_admin = 0 AND is_mod = 0 AND is_banned = 0" )
2017-02-04 06:19:55 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-04 06:19:55 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-27 07:09:28 +00:00
log . Print ( "Preparing add_forum_perms_to_group statement." )
2017-06-05 11:57:27 +00:00
add_forum_perms_to_group_stmt , err = db . Prepare ( "REPLACE INTO forums_permissions(gid,fid,preset,permissions) VALUES(?,?,?,?)" )
2017-02-04 06:19:55 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-02-04 06:19:55 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Preparing update_setting statement." )
update_setting_stmt , err = db . Prepare ( "UPDATE settings SET content = ? WHERE name = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-11 16:06:17 +00:00
log . Print ( "Preparing add_plugin statement." )
add_plugin_stmt , err = db . Prepare ( "INSERT INTO plugins(uname,active) VALUES(?,?)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-11 16:06:17 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-13 02:14:14 +00:00
log . Print ( "Preparing update_plugin statement." )
update_plugin_stmt , err = db . Prepare ( "UPDATE plugins SET active = ? WHERE uname = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-13 02:14:14 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-01 15:45:43 +00:00
log . Print ( "Preparing add_theme statement." )
add_theme_stmt , err = db . Prepare ( "INSERT INTO `themes`(`uname`,`default`) VALUES(?,?)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-01 15:45:43 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-01 15:45:43 +00:00
log . Print ( "Preparing update_theme statement." )
2017-02-04 06:19:55 +00:00
update_theme_stmt , err = db . Prepare ( "update `themes` set `default` = ? where `uname` = ?" )
2017-01-01 15:45:43 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-01 15:45:43 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-23 12:35:22 +00:00
log . Print ( "Preparing update_user statement." )
2017-02-04 06:19:55 +00:00
update_user_stmt , err = db . Prepare ( "update `users` set `name` = ?,`email` = ?,`group` = ? where `uid` = ?" )
2016-12-23 12:35:22 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-23 12:35:22 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-23 12:13:50 +00:00
log . Print ( "Preparing update_group_rank statement." )
update_group_perms_stmt , err = db . Prepare ( "update `users_groups` set `permissions` = ? where `gid` = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-23 12:13:50 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-18 07:23:02 +00:00
log . Print ( "Preparing update_group_rank statement." )
update_group_rank_stmt , err = db . Prepare ( "update `users_groups` set `is_admin` = ?, `is_mod` = ?, `is_banned` = ? where `gid` = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-18 07:23:02 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-18 07:23:02 +00:00
log . Print ( "Preparing update_group statement." )
update_group_stmt , err = db . Prepare ( "update `users_groups` set `name` = ?, `tag` = ? where `gid` = ?" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-18 07:23:02 +00:00
}
2017-06-05 11:57:27 +00:00
2017-03-27 07:09:28 +00:00
log . Print ( "Preparing create_group statement." )
create_group_stmt , err = db . Prepare ( "INSERT INTO users_groups(name,tag,is_admin,is_mod,is_banned,permissions) VALUES(?,?,?,?,?,?)" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-03-27 07:09:28 +00:00
}
2017-06-05 11:57:27 +00:00
2017-04-05 14:05:37 +00:00
log . Print ( "Preparing add_modlog_entry statement." )
2017-04-06 17:37:32 +00:00
add_modlog_entry_stmt , err = db . Prepare ( "INSERT INTO moderation_logs(action,elementID,elementType,ipaddress,actorID,doneAt) VALUES(?,?,?,?,?,NOW())" )
2017-04-05 14:05:37 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-04-05 14:05:37 +00:00
}
2017-06-05 11:57:27 +00:00
2017-04-05 14:05:37 +00:00
log . Print ( "Preparing add_adminlog_entry statement." )
2017-04-06 17:37:32 +00:00
add_adminlog_entry_stmt , err = db . Prepare ( "INSERT INTO moderation_logs(action,elementID,elementType,ipaddress,actorID,doneAt) VALUES(?,?,?,?,?,NOW())" )
2017-04-05 14:05:37 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-04-05 14:05:37 +00:00
}
2017-06-05 11:57:27 +00:00
2017-06-06 14:41:06 +00:00
log . Print ( "Preparing todays_post_count statement." )
todays_post_count_stmt , err = db . Prepare ( "select count(*) from replies where createdAt BETWEEN (now() - interval 1 day) and now()" )
if err != nil {
return err
}
log . Print ( "Preparing todays_topic_count statement." )
todays_topic_count_stmt , err = db . Prepare ( "select count(*) from topics where createdAt BETWEEN (now() - interval 1 day) and now()" )
if err != nil {
return err
}
log . Print ( "Preparing todays_report_count statement." )
todays_report_count_stmt , err = db . Prepare ( "select count(*) from topics where createdAt BETWEEN (now() - interval 1 day) and now() and parentID = 1" )
if err != nil {
return err
}
log . Print ( "Preparing todays_newuser_count statement." )
todays_newuser_count_stmt , err = db . Prepare ( "select count(*) from users where createdAt BETWEEN (now() - interval 1 day) and now()" )
if err != nil {
return err
}
2016-12-09 13:46:29 +00:00
log . Print ( "Loading the usergroups." )
2017-01-31 05:13:38 +00:00
groups = append ( groups , Group { ID : 0 , Name : "System" } )
2017-06-05 11:57:27 +00:00
2017-01-31 05:13:38 +00:00
rows , err := db . Query ( "select gid,name,permissions,is_mod,is_admin,is_banned,tag from users_groups" )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
defer rows . Close ( )
2017-06-05 11:57:27 +00:00
2017-01-31 05:13:38 +00:00
i := 1
for ; rows . Next ( ) ; i ++ {
2016-12-21 02:30:32 +00:00
group := Group { ID : 0 , }
err := rows . Scan ( & group . ID , & group . Name , & group . PermissionsText , & group . Is_Mod , & group . Is_Admin , & group . Is_Banned , & group . Tag )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-31 05:13:38 +00:00
// Ugh, you really shouldn't physically delete these items, it makes a big mess of things
if group . ID != i {
2017-05-07 08:31:41 +00:00
log . Print ( "Stop physically deleting groups. You are messing up the IDs. Use the Group Manager or delete_group() instead x.x" )
2017-01-31 05:13:38 +00:00
fill_group_id_gap ( i , group . ID )
}
2017-06-05 11:57:27 +00:00
2016-12-21 02:30:32 +00:00
err = json . Unmarshal ( group . PermissionsText , & group . Perms )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-21 02:30:32 +00:00
}
2017-05-07 08:31:41 +00:00
if debug {
log . Print ( group . Name + ": " )
2016-12-23 12:35:22 +00:00
fmt . Printf ( "%+v\n" , group . Perms )
}
2017-06-05 11:57:27 +00:00
2016-12-21 02:30:32 +00:00
group . Perms . ExtData = make ( map [ string ] bool )
2017-01-31 05:13:38 +00:00
groups = append ( groups , group )
2016-12-09 13:46:29 +00:00
}
err = rows . Err ( )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-03-18 07:23:02 +00:00
groupCapCount = i
2017-06-05 11:57:27 +00:00
2017-01-31 05:13:38 +00:00
log . Print ( "Binding the Not Loggedin Group" )
GuestPerms = groups [ 6 ] . Perms
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Loading the forums." )
2017-01-26 13:37:50 +00:00
log . Print ( "Adding the uncategorised forum" )
2017-06-05 11:57:27 +00:00
forums = append ( forums , Forum { 0 , "Uncategorised" , "" , uncategorised_forum_visible , "all" , 0 , "" , 0 , "" , 0 , "" } )
2017-01-26 13:37:50 +00:00
//rows, err = db.Query("SELECT fid, name, active, lastTopic, lastTopicID, lastReplyer, lastReplyerID, lastTopicTime FROM forums")
2017-06-05 11:57:27 +00:00
rows , err = db . Query ( "select `fid`, `name`, `desc`, `active`, `preset`, `topicCount`, `lastTopic`, `lastTopicID`, `lastReplyer`, `lastReplyerID`, `lastTopicTime` from forums order by fid asc" )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
defer rows . Close ( )
2017-06-05 11:57:27 +00:00
2017-01-31 05:13:38 +00:00
i = 1
2017-01-26 13:37:50 +00:00
for ; rows . Next ( ) ; i ++ {
2017-02-04 15:49:24 +00:00
forum := Forum { ID : 0 , Name : "" , Active : true , Preset : "all" }
2017-06-05 11:57:27 +00:00
err := rows . Scan ( & forum . ID , & forum . Name , & forum . Desc , & forum . Active , & forum . Preset , & forum . TopicCount , & forum . LastTopic , & forum . LastTopicID , & forum . LastReplyer , & forum . LastReplyerID , & forum . LastTopicTime )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-26 13:37:50 +00:00
// Ugh, you really shouldn't physically delete these items, it makes a big mess of things
if forum . ID != i {
2017-05-07 08:31:41 +00:00
log . Print ( "Stop physically deleting forums. You are messing up the IDs. Use the Forum Manager or delete_forum() instead x.x" )
2017-01-26 13:37:50 +00:00
fill_forum_id_gap ( i , forum . ID )
2017-01-31 05:13:38 +00:00
}
2017-06-05 11:57:27 +00:00
2017-05-07 08:31:41 +00:00
if forum . Name == "" {
if debug {
log . Print ( "Adding a placeholder forum" )
}
} else {
log . Print ( "Adding the " + forum . Name + " forum" )
}
2017-01-26 13:37:50 +00:00
forums = append ( forums , forum )
2016-12-09 13:46:29 +00:00
}
err = rows . Err ( )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-01-26 13:37:50 +00:00
forumCapCount = i
2017-06-05 11:57:27 +00:00
2017-01-26 13:37:50 +00:00
//log.Print("Adding the reports forum")
//forums[-1] = Forum{-1,"Reports",false,0,"",0,"",0,""}
2017-06-05 11:57:27 +00:00
2017-01-31 05:13:38 +00:00
log . Print ( "Loading the forum permissions" )
rows , err = db . Query ( "select gid, fid, permissions from forums_permissions order by gid asc, fid asc" )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-31 05:13:38 +00:00
}
defer rows . Close ( )
2017-06-05 11:57:27 +00:00
2017-05-07 08:31:41 +00:00
if debug {
log . Print ( "Adding the forum permissions" )
}
2017-01-31 05:13:38 +00:00
// Temporarily store the forum perms in a map before transferring it to a much faster slice
2017-02-04 06:19:55 +00:00
forum_perms = make ( map [ int ] map [ int ] ForumPerms )
for rows . Next ( ) {
2017-05-07 08:31:41 +00:00
var gid , fid int
2017-01-31 05:13:38 +00:00
var perms [ ] byte
var pperms ForumPerms
err := rows . Scan ( & gid , & fid , & perms )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-31 05:13:38 +00:00
}
err = json . Unmarshal ( perms , & pperms )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-31 05:13:38 +00:00
}
pperms . ExtData = make ( map [ string ] bool )
pperms . Overrides = true
_ , ok := forum_perms [ gid ]
if ! ok {
forum_perms [ gid ] = make ( map [ int ] ForumPerms )
}
forum_perms [ gid ] [ fid ] = pperms
}
for gid , _ := range groups {
2017-05-07 08:31:41 +00:00
if debug {
log . Print ( "Adding the forum permissions for Group #" + strconv . Itoa ( gid ) + " - " + groups [ gid ] . Name )
}
2017-01-31 05:13:38 +00:00
//groups[gid].Forums = append(groups[gid].Forums,BlankForumPerms) // GID 0. I sometimes wish MySQL's AUTO_INCREMENT would start at zero
for fid , _ := range forums {
forum_perm , ok := forum_perms [ gid ] [ fid ]
if ok {
// Override group perms
//log.Print("Overriding permissions for forum #" + strconv.Itoa(fid))
groups [ gid ] . Forums = append ( groups [ gid ] . Forums , forum_perm )
} else {
// Inherit from Group
//log.Print("Inheriting from default for forum #" + strconv.Itoa(fid))
forum_perm = BlankForumPerms
groups [ gid ] . Forums = append ( groups [ gid ] . Forums , forum_perm )
}
2017-06-05 11:57:27 +00:00
2017-01-31 05:13:38 +00:00
if forum_perm . Overrides {
if forum_perm . ViewTopic {
groups [ gid ] . CanSee = append ( groups [ gid ] . CanSee , fid )
}
} else if groups [ gid ] . Perms . ViewTopic {
groups [ gid ] . CanSee = append ( groups [ gid ] . CanSee , fid )
}
}
//fmt.Printf("%+v\n", groups[gid].CanSee)
//fmt.Printf("%+v\n", groups[gid].Forums)
2017-02-04 06:19:55 +00:00
//fmt.Println(len(groups[gid].CanSee))
2017-01-31 05:13:38 +00:00
//fmt.Println(len(groups[gid].Forums))
}
2017-06-05 11:57:27 +00:00
2016-12-09 13:46:29 +00:00
log . Print ( "Loading the settings." )
2017-01-31 05:13:38 +00:00
rows , err = db . Query ( "select name, content, type, constraints from settings" )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
defer rows . Close ( )
2017-06-05 11:57:27 +00:00
var sname , scontent , stype , sconstraints string
2016-12-09 13:46:29 +00:00
for rows . Next ( ) {
2016-12-21 02:30:32 +00:00
err := rows . Scan ( & sname , & scontent , & stype , & sconstraints )
2016-12-09 13:46:29 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2016-12-21 02:30:32 +00:00
errmsg := parseSetting ( sname , scontent , stype , sconstraints )
2016-12-09 13:46:29 +00:00
if errmsg != "" {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
}
err = rows . Err ( )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-09 13:46:29 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-11 16:06:17 +00:00
log . Print ( "Loading the plugins." )
2017-01-31 05:13:38 +00:00
rows , err = db . Query ( "select uname, active from plugins" )
2016-12-11 16:06:17 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-11 16:06:17 +00:00
}
defer rows . Close ( )
2017-06-05 11:57:27 +00:00
2016-12-11 16:06:17 +00:00
var uname string
var active bool
for rows . Next ( ) {
err := rows . Scan ( & uname , & active )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-11 16:06:17 +00:00
}
2017-06-05 11:57:27 +00:00
2016-12-16 10:37:42 +00:00
// Was the plugin deleted at some point?
plugin , ok := plugins [ uname ]
if ! ok {
continue
}
2016-12-11 16:06:17 +00:00
plugin . Active = active
plugins [ uname ] = plugin
}
err = rows . Err ( )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2016-12-11 16:06:17 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-01 15:45:43 +00:00
log . Print ( "Loading the themes." )
2017-01-31 05:13:38 +00:00
rows , err = db . Query ( "select `uname`, `default` from `themes`" )
2017-01-01 15:45:43 +00:00
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-01 15:45:43 +00:00
}
defer rows . Close ( )
2017-06-05 11:57:27 +00:00
2017-01-01 15:45:43 +00:00
var defaultThemeSwitch bool
for rows . Next ( ) {
err := rows . Scan ( & uname , & defaultThemeSwitch )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-01 15:45:43 +00:00
}
2017-06-05 11:57:27 +00:00
2017-01-01 15:45:43 +00:00
// Was the theme deleted at some point?
theme , ok := themes [ uname ]
if ! ok {
continue
}
2017-06-05 11:57:27 +00:00
2017-01-01 15:45:43 +00:00
if defaultThemeSwitch {
log . Print ( "Loading the theme '" + theme . Name + "'" )
theme . Active = true
defaultTheme = uname
add_theme_static_files ( uname )
map_theme_templates ( theme )
} else {
theme . Active = false
}
themes [ uname ] = theme
}
err = rows . Err ( )
if err != nil {
2017-04-12 10:10:36 +00:00
return err
2017-01-01 15:45:43 +00:00
}
2017-04-12 10:10:36 +00:00
return nil
2016-12-09 13:46:29 +00:00
}