2017-07-16 17:14:21 +02:00
package settingsController
2017-06-28 13:42:38 +02:00
import (
"net/http"
"net/url"
"github.com/NyaaPantsu/nyaa/config"
2017-07-16 17:14:21 +02:00
"github.com/NyaaPantsu/nyaa/controllers/router"
2017-07-16 15:35:24 +02:00
"github.com/NyaaPantsu/nyaa/templates"
2017-07-02 16:54:55 +02:00
msg "github.com/NyaaPantsu/nyaa/utils/messages"
"github.com/NyaaPantsu/nyaa/utils/publicSettings"
"github.com/NyaaPantsu/nyaa/utils/timeHelper"
2017-06-28 13:42:38 +02:00
"github.com/gin-gonic/gin"
)
// SeePublicSettingsHandler : Controller to view the languages and themes
func SeePublicSettingsHandler ( c * gin . Context ) {
_ , Tlang := publicSettings . GetTfuncAndLanguageFromRequest ( c )
availableLanguages := publicSettings . GetAvailableLanguages ( )
2017-07-23 07:46:29 +02:00
languagesJSON := templates . LanguagesJSONResponse { Tlang . Tag , availableLanguages }
2017-06-28 13:42:38 +02:00
contentType := c . Request . Header . Get ( "Content-Type" )
if contentType == "application/json" {
c . Header ( "Content-Type" , "application/json" )
2017-07-23 07:46:29 +02:00
c . JSON ( http . StatusOK , languagesJSON )
2017-06-28 13:42:38 +02:00
} else {
2017-07-23 07:46:29 +02:00
templates . Form ( c , "site/user/public/settings.jet.html" , languagesJSON )
2017-06-28 13:42:38 +02:00
}
}
// ChangePublicSettingsHandler : Controller for changing the current language and theme
func ChangePublicSettingsHandler ( c * gin . Context ) {
theme := c . PostForm ( "theme" )
lang := c . PostForm ( "language" )
mascot := c . PostForm ( "mascot" )
mascotURL := c . PostForm ( "mascot_url" )
messages := msg . GetMessages ( c )
availableLanguages := publicSettings . GetAvailableLanguages ( )
2017-07-06 21:53:13 +02:00
if ! availableLanguages . Exist ( lang ) {
2017-06-28 13:42:38 +02:00
messages . AddErrorT ( "errors" , "language_not_available" )
}
// FIXME Are the settings actually sanitized?
// Limit the mascot URL, so base64-encoded images aren't valid
if len ( mascotURL ) > 256 {
messages . AddErrorT ( "errors" , "mascot_url_too_long" )
}
_ , err := url . Parse ( mascotURL )
if err != nil {
messages . AddErrorTf ( "errors" , "mascor_url_parse_error" , err . Error ( ) )
}
// If logged in, update user settings.
2017-07-16 17:14:21 +02:00
user := router . GetUser ( c )
2017-06-28 13:42:38 +02:00
if user . ID > 0 {
user . Language = lang
user . Theme = theme
user . Mascot = mascot
user . MascotURL = mascotURL
2017-07-02 23:53:23 +02:00
user . UpdateRaw ( )
2017-06-28 13:42:38 +02:00
}
// Set cookie with http and not gin for expires (maxage not supported in <IE8)
http . SetCookie ( c . Writer , & http . Cookie { Name : "lang" , Value : lang , Domain : getDomainName ( ) , Path : "/" , Expires : timeHelper . FewDaysLater ( 365 ) } )
http . SetCookie ( c . Writer , & http . Cookie { Name : "theme" , Value : theme , Domain : getDomainName ( ) , Path : "/" , Expires : timeHelper . FewDaysLater ( 365 ) } )
http . SetCookie ( c . Writer , & http . Cookie { Name : "mascot" , Value : mascot , Domain : getDomainName ( ) , Path : "/" , Expires : timeHelper . FewDaysLater ( 365 ) } )
http . SetCookie ( c . Writer , & http . Cookie { Name : "mascot_url" , Value : mascotURL , Domain : getDomainName ( ) , Path : "/" , Expires : timeHelper . FewDaysLater ( 365 ) } )
c . Redirect ( http . StatusSeeOther , "/" )
}
func getDomainName ( ) string {
2017-07-10 14:11:05 +02:00
domain := config . Get ( ) . Cookies . DomainName
if config . Get ( ) . Environment == "DEVELOPMENT" {
2017-06-28 13:42:38 +02:00
domain = ""
}
return domain
}