2018-10-27 03:21:02 +00:00
/ *
*
* Gosora Common Resources
2019-01-21 12:27:59 +00:00
* Copyright Azareal 2018 - 2020
2018-10-27 03:21:02 +00:00
*
* /
package common // import "github.com/Azareal/Gosora/common"
2017-11-10 03:33:11 +00:00
2017-11-11 23:34:27 +00:00
import (
"database/sql"
2019-02-23 06:29:19 +00:00
"io"
2017-11-13 05:22:37 +00:00
"log"
2019-02-23 06:29:19 +00:00
"os"
2018-05-31 06:51:31 +00:00
"sync/atomic"
2018-05-27 09:36:35 +00:00
"time"
2018-12-27 05:42:41 +00:00
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
meta "github.com/Azareal/Gosora/common/meta"
2020-03-07 02:59:06 +00:00
qgen "github.com/Azareal/Gosora/query_gen"
2017-11-11 23:34:27 +00:00
)
2017-11-10 03:33:11 +00:00
2019-04-08 07:50:10 +00:00
var SoftwareVersion = Version { Major : 0 , Minor : 3 , Patch : 0 , Tag : "dev" }
2018-10-02 05:03:20 +00:00
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
var Meta meta . MetaStore
2017-11-11 04:06:16 +00:00
// nolint I don't want to write comments for each of these o.o
2018-05-16 10:46:14 +00:00
const Hour int = 60 * 60
2020-04-30 21:32:34 +00:00
const Day = Hour * 24
const Week = Day * 7
const Month = Day * 30
const Year = Day * 365
2018-05-16 10:46:14 +00:00
const Kilobyte int = 1024
2020-04-30 21:32:34 +00:00
const Megabyte = Kilobyte * 1024
const Gigabyte = Megabyte * 1024
const Terabyte = Gigabyte * 1024
const Petabyte = Terabyte * 1024
2017-11-11 04:06:16 +00:00
2018-05-27 09:36:35 +00:00
var StartTime time . Time
2020-03-07 02:59:06 +00:00
var GzipStartEtag string
var StartEtag string
2017-11-11 04:06:16 +00:00
var TmplPtrMap = make ( map [ string ] interface { } )
2018-05-31 06:51:31 +00:00
// Anti-spam token with rotated key
2018-06-17 07:28:18 +00:00
var JSTokenBox atomic . Value // TODO: Move this and some of these other globals somewhere else
2018-12-27 05:42:41 +00:00
var SessionSigningKeyBox atomic . Value // For MFA to avoid hitting the database unneccessarily
2018-06-17 07:28:18 +00:00
var OldSessionSigningKeyBox atomic . Value // Just in case we've signed with a key that's about to go stale so we don't annoy the user too much
2018-07-15 10:29:31 +00:00
var IsDBDown int32 = 0 // 0 = false, 1 = true. this is value which should be manipulated with package atomic for representing whether the database is down so we don't spam the log with lots of redundant errors
2018-05-31 06:51:31 +00:00
2017-11-10 03:33:11 +00:00
// ErrNoRows is an alias of sql.ErrNoRows, just in case we end up with non-database/sql datastores
var ErrNoRows = sql . ErrNoRows
2017-11-11 04:06:16 +00:00
// ? - Make this more customisable?
2020-03-08 04:15:27 +00:00
/ * var ExternalSites = map [ string ] string {
2017-11-11 04:06:16 +00:00
"YT" : "https://www.youtube.com/" ,
2020-03-08 04:15:27 +00:00
} * /
2017-11-11 04:06:16 +00:00
2019-05-18 01:18:19 +00:00
// TODO: Make this more customisable
2020-03-08 04:15:27 +00:00
var SpammyDomainBits = [ ] string { "porn" , "sex" , "lesbian" , "acup" , "nude" , "milf" , "tits" , "vape" , "busty" , "kink" , "lingerie" , "strapon" , "problog" , "fet" , "xblog" , "blogin" , "blognetwork" , "relayblog" }
2020-03-22 04:37:45 +00:00
var Chrome , Firefox int // ! Temporary Hack for http push
var SimpleBots [ ] int // ! Temporary hack to stop semrush, ahrefs, python bots and other from wasting resources
2019-05-18 01:18:19 +00:00
2017-11-11 04:06:16 +00:00
type StringList [ ] string
// ? - Should we allow users to upload .php or .go files? It could cause security issues. We could store them with a mangled extension to render them inert
// TODO: Let admins manage this from the Control Panel
2018-08-07 04:14:28 +00:00
// apng is commented out for now, as we have no way of re-encoding it into a smaller file
2017-11-11 04:06:16 +00:00
var AllowedFileExts = StringList {
2020-04-30 21:32:34 +00:00
"png" , "jpg" , "jpe" , "jpeg" , "jif" , "jfi" , "jfif" , "svg" , "bmp" , "gif" , "tiff" , "tif" , "webp" , "apng" , /*"bpg", "heif",*/ // images (encodable) + apng (browser support) + bpg
2017-11-11 04:06:16 +00:00
2018-08-07 04:14:28 +00:00
"txt" , "xml" , "json" , "yaml" , "toml" , "ini" , "md" , "html" , "rtf" , "js" , "py" , "rb" , "css" , "scss" , "less" , "eqcss" , "pcss" , "java" , "ts" , "cs" , "c" , "cc" , "cpp" , "cxx" , "C" , "c++" , "h" , "hh" , "hpp" , "hxx" , "h++" , "rs" , "rlib" , "htaccess" , "gitignore" , /*"go","php",*/ // text
2017-11-11 04:06:16 +00:00
2020-05-05 08:10:19 +00:00
"wav" , "mp3" , "oga" , // audio
2020-04-30 21:32:34 +00:00
2020-05-05 08:10:19 +00:00
"mp4" , "avi" , "ogg" , "ogv" , "ogx" , "wmv" , "webm" , // video
2017-11-11 04:06:16 +00:00
"otf" , "woff2" , "woff" , "ttf" , "eot" , // fonts
2019-10-31 23:17:26 +00:00
2019-10-31 23:34:50 +00:00
"bz2" , "zip" , "zipx" , "gz" , "7z" , "tar" , "cab" , "rar" , "kgb" , "pea" , "xz" , "zz" , // archives
2020-04-30 21:32:34 +00:00
//"docx", "pdf", // documents
2017-11-11 04:06:16 +00:00
}
var ImageFileExts = StringList {
2020-04-30 21:32:34 +00:00
"png" , "jpg" , "jpe" , "jpeg" , "jif" , "jfi" , "jfif" , "svg" , "bmp" , "gif" , "tiff" , "tif" , "webp" , /* "apng", "bpg", */
}
var VideoFileExts = StringList {
2020-05-05 08:10:19 +00:00
"mp4" , "avi" , "ogg" , "ogv" , "ogx" , "wmv" , "webm" ,
2020-04-30 21:32:34 +00:00
}
var WebVideoFileExts = StringList {
2020-05-05 08:10:19 +00:00
"mp4" , "avi" , "ogg" , "ogv" , "webm" ,
}
var WebAudioFileExts = StringList {
"wav" , "mp3" , "oga" ,
2017-11-11 04:06:16 +00:00
}
var ArchiveFileExts = StringList {
2019-10-31 23:34:50 +00:00
"bz2" , "zip" , "zipx" , "gz" , "7z" , "tar" , "cab" , "rar" , "kgb" , "pea" , "xz" , "zz" ,
2017-11-11 04:06:16 +00:00
}
var ExecutableFileExts = StringList {
2019-10-31 23:34:50 +00:00
"exe" , "jar" , "phar" , "shar" , "iso" , "apk" , "deb" ,
2017-11-11 04:06:16 +00:00
}
2018-05-31 06:51:31 +00:00
func init ( ) {
JSTokenBox . Store ( "" )
2018-06-17 07:28:18 +00:00
SessionSigningKeyBox . Store ( "" )
OldSessionSigningKeyBox . Store ( "" )
2018-05-31 06:51:31 +00:00
}
2017-11-11 04:06:16 +00:00
// TODO: Write a test for this
2020-04-30 21:32:34 +00:00
func ( sl StringList ) Contains ( needle string ) bool {
for _ , item := range sl {
2017-11-11 04:06:16 +00:00
if item == needle {
return true
}
}
return false
}
2020-04-30 21:32:34 +00:00
/ * var DbTables [ ] string
var TableToID = make ( map [ string ] int )
var IDToTable = make ( map [ int ] string )
func InitTables ( acc * qgen . Accumulator ) error {
stmt := acc . Select ( "tables" ) . Columns ( "id,name" ) . Prepare ( )
if err := acc . FirstError ( ) ; err != nil {
return err
}
return eachall ( stmt , func ( r * sql . Rows ) error {
var id int
var name string
if err := r . Scan ( & id , & name ) ; err != nil {
return err
}
TableToID [ name ] = id
IDToTable [ id ] = name
return nil
} )
} * /
2017-11-12 03:29:05 +00:00
type dbInits [ ] func ( acc * qgen . Accumulator ) error
2017-11-11 04:06:16 +00:00
2017-11-11 23:34:27 +00:00
var DbInits dbInits
2017-11-11 04:06:16 +00:00
2017-11-11 23:34:27 +00:00
func ( inits dbInits ) Run ( ) error {
2020-04-30 21:32:34 +00:00
for _ , i := range inits {
err := i ( qgen . NewAcc ( ) )
2017-11-11 04:06:16 +00:00
if err != nil {
return err
}
}
return nil
}
2017-11-12 03:29:05 +00:00
func ( inits dbInits ) Add ( init ... func ( acc * qgen . Accumulator ) error ) {
2017-11-11 23:34:27 +00:00
DbInits = dbInits ( append ( DbInits , init ... ) )
2017-11-11 04:06:16 +00:00
}
2017-11-13 05:22:37 +00:00
2018-08-22 01:32:07 +00:00
// TODO: Add a graceful shutdown function
func StoppedServer ( msg ... interface { } ) {
//log.Print("stopped server")
StopServerChan <- msg
}
var StopServerChan = make ( chan [ ] interface { } )
2019-02-23 06:29:19 +00:00
var LogWriter = io . MultiWriter ( os . Stderr )
2018-02-19 04:26:01 +00:00
func DebugDetail ( args ... interface { } ) {
2017-11-13 05:22:37 +00:00
if Dev . SuperDebug {
log . Print ( args ... )
}
}
2018-02-19 04:26:01 +00:00
func DebugDetailf ( str string , args ... interface { } ) {
2017-11-13 05:22:37 +00:00
if Dev . SuperDebug {
log . Printf ( str , args ... )
}
}
2018-02-19 04:26:01 +00:00
func DebugLog ( args ... interface { } ) {
2017-11-13 05:22:37 +00:00
if Dev . DebugMode {
log . Print ( args ... )
}
}
2018-02-19 04:26:01 +00:00
func DebugLogf ( str string , args ... interface { } ) {
2017-11-13 05:22:37 +00:00
if Dev . DebugMode {
log . Printf ( str , args ... )
}
}
2019-04-08 07:44:41 +00:00
func Log ( args ... interface { } ) {
log . Print ( args ... )
}
func Logf ( str string , args ... interface { } ) {
log . Printf ( str , args ... )
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
func Countf ( stmt * sql . Stmt , args ... interface { } ) ( count int ) {
err := stmt . QueryRow ( args ... ) . Scan ( & count )
if err != nil {
LogError ( err )
}
return count
}
func eachall ( stmt * sql . Stmt , f func ( r * sql . Rows ) error ) error {
rows , err := stmt . Query ( )
if err != nil {
return err
}
defer rows . Close ( )
for rows . Next ( ) {
if err := f ( rows ) ; err != nil {
return err
}
}
return rows . Err ( )
2020-03-07 02:59:06 +00:00
}