2017-05-07 00:10:40 +02:00
package form
2017-05-06 21:21:39 +02:00
import (
"regexp"
2017-05-07 10:25:09 +02:00
2017-05-17 07:58:40 +02:00
"github.com/NyaaPantsu/nyaa/util/log"
2017-05-21 18:13:28 +02:00
msg "github.com/NyaaPantsu/nyaa/util/messages"
2017-05-06 21:21:39 +02:00
)
2017-05-29 00:22:42 +02:00
// Regex by: Philippe Verdy (in a comment somewhere on a website) - Valid every email RFC valid
const emailRegex = ` ^((?:[-!#$%&'*+/=?^ ` + "`" + ` { |}~\w]|\\.)+(?:\.(?:[-!#$%&'*+/=?^ ` + "`" + ` { |}~\w]|\\.)+)*|"(?:[^\\"]|\\.)+")@(?:\[(?:((?:(?:[01][\d] { 0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)\.) { 3}(?:[01][\d] { 0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?))|IPv6:((?:[0-9A-F] { 1,4}:) { 7}[0-9A-F] { 1,4}|(?:[0-9A-F] { 1,4}:) { 6}:[0-9A-F] { 1,4}|(?:[0-9A-F] { 1,4}:) { 5}:(?:[0-9A-F] { 1,4}:)?[0-9A-F] { 1,4}|(?:[0-9A-F] { 1,4}:) { 4}:(?:[0-9A-F] { 1,4}:) { 0,2}[0-9A-F] { 1,4}|(?:[0-9A-F] { 1,4}:) { 3}:(?:[0-9A-F] { 1,4}:) { 0,3}[0-9A-F] { 1,4}|(?:[0-9A-F] { 1,4}:) { 2}:(?:[0-9A-F] { 1,4}:) { 0,4}[0-9A-F] { 1,4}|[0-9A-F] { 1,4}::(?:[0-9A-F] { 1,4}:) { 0,5}[0-9A-F] { 1,4}|::(?:[0-9A-F] { 1,4}:) { 0,6}[0-9A-F] { 1,4}|(?:[0-9A-F] { 1,4}:) { 1,7}:|(?:[0-9A-F] { 1,4}:) { 6}(?:(?:[01][\d] { 0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)\.) { 3}(?:[01][\d] { 0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)|(?:[0-9A-F] { 1,4}:) { 0,5}:(?:(?:[01][\d] { 0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)\.) { 3}(?:[01][\d] { 0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)|::(?:[0-9A-F] { 1,4}:) { 0,5}(?:(?:[01][\d] { 0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?)\.) { 3}(?:[01][\d] { 0,2}|2(?:[0-4]\d?|5[0-5]?|[6-9])?|[3-9]\d?))|([-a-z\d] { 0,62}[a-z\d]:[^\[\\\]]+))\]|([a-z\d](?:[-a-z\d] { 0,62}[a-z\d])?(?:\.[a-z\d](?:[-a-z\d] { 0,62}[a-z\d])?)+))$ `
2017-05-26 12:12:52 +02:00
const usernameRegex = ` (\W) `
2017-05-06 21:21:39 +02:00
2017-05-26 12:12:52 +02:00
// EmailValidation : Check if an email is valid
2017-05-21 18:13:28 +02:00
func EmailValidation ( email string , mes * msg . Messages ) bool {
2017-05-26 12:12:52 +02:00
exp , errorRegex := regexp . Compile ( emailRegex )
2017-05-07 19:59:38 +02:00
if regexpCompiled := log . CheckError ( errorRegex ) ; regexpCompiled {
2017-05-06 21:21:39 +02:00
if exp . MatchString ( email ) {
2017-05-21 18:13:28 +02:00
return true
2017-05-06 21:21:39 +02:00
}
2017-06-10 00:58:28 +02:00
mes . AddErrorT ( "email" , "email_not_valid" )
return false
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
}
2017-06-10 00:58:28 +02:00
mes . AddError ( "errors" , "Regexp couldn't be parsed!" )
2017-05-21 18:13:28 +02:00
return false
2017-05-06 21:21:39 +02:00
}
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
2017-05-26 12:12:52 +02:00
// ValidateUsername : Check if a username is valid
2017-05-21 18:13:28 +02:00
func ValidateUsername ( username string , mes * msg . Messages ) bool {
2017-05-26 12:12:52 +02:00
exp , errorRegex := regexp . Compile ( usernameRegex )
2017-05-07 19:59:38 +02:00
if regexpCompiled := log . CheckError ( errorRegex ) ; regexpCompiled {
2017-05-07 05:04:55 +02:00
if exp . MatchString ( username ) {
2017-06-10 00:58:28 +02:00
mes . AddErrorT ( "username" , "username_illegal" )
2017-05-21 18:13:28 +02:00
return false
2017-05-07 05:04:55 +02:00
}
2017-06-10 00:58:28 +02:00
return true
2017-05-07 05:04:55 +02:00
}
2017-06-10 00:58:28 +02:00
mes . AddError ( "errors" , "Regexp couldn't be parsed!" )
return false
2017-05-07 19:59:38 +02:00
}
2017-05-26 12:12:52 +02:00
// IsAgreed : Check if terms and conditions are valid
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
func IsAgreed ( termsAndConditions string ) bool { // TODO: Inline function
return termsAndConditions == "1"
2017-05-07 16:08:45 +02:00
}
2017-05-06 21:21:39 +02:00
// RegistrationForm is used when creating a user.
type RegistrationForm struct {
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
Username string ` form:"username" needed:"true" len_min:"3" len_max:"20" `
2017-05-10 15:40:23 +02:00
Email string ` form:"email" `
2017-05-13 18:20:21 +02:00
Password string ` form:"password" needed:"true" len_min:"6" len_max:"72" equalInput:"ConfirmPassword" `
2017-06-20 02:06:07 +02:00
ConfirmPassword string ` form:"password_confirmation" hum_name:"Password Confirmation" omit:"true" needed:"true" `
Consistency, formatting, error checking, cleanup, and a couple bug fixes (#245)
* Checkpoint: it builds
The config, db, model, network, os, and public packages have had some
fixes to glaringly obvious flaws, dead code removed, and stylistic
changes.
* Style changes and old code removal in router
Router needs a lot of work done to its (lack of) error handling.
* Dead code removal and style changes
Now up to util/email/email.go. After I'm finished with the initial sweep
I'll go back and fix error handling and security issues. Then I'll fix
the broken API. Then I'll go through to add documentation and fix code
visibility.
* Finish dead code removal and style changes
Vendored libraries not touched. Everything still needs security fixes
and documentation. There's also one case of broken functionality.
* Fix accidental find-and-replace
* Style, error checking, saftey, bug fix changes
* Redo error checking erased during merge
* Re-add merge-erased fix. Make Safe safe.
2017-05-10 04:34:40 +02:00
CaptchaID string ` form:"captchaID" omit:"true" needed:"true" `
TermsAndConditions bool ` form:"t_and_c" omit:"true" needed:"true" equal:"true" hum_name:"Terms and Conditions" `
2017-05-06 21:21:39 +02:00
}
2017-05-08 22:12:57 +02:00
// LoginForm is used when a user logs in.
2017-05-06 21:21:39 +02:00
type LoginForm struct {
2017-06-13 08:01:57 +02:00
Username string ` form:"username" needed:"true" json:"username" `
Password string ` form:"password" needed:"true" json:"password" `
2017-05-06 21:21:39 +02:00
}
// UserForm is used when updating a user.
type UserForm struct {
2017-05-26 12:12:52 +02:00
Username string ` form:"username" needed:"true" len_min:"3" len_max:"20" `
Email string ` form:"email" `
Language string ` form:"language" default:"en-us" `
CurrentPassword string ` form:"current_password" len_min:"6" len_max:"72" omit:"true" `
Password string ` form:"password" len_min:"6" len_max:"72" equalInput:"ConfirmPassword" `
2017-06-20 02:06:07 +02:00
ConfirmPassword string ` form:"password_confirmation" hum_name:"Password Confirmation" omit:"true" `
2017-05-26 12:12:52 +02:00
Status int ` form:"status" default:"0" `
2017-05-29 15:15:04 +02:00
Theme string ` form:"theme" `
2017-05-06 21:21:39 +02:00
}
2017-05-21 18:13:28 +02:00
// UserSettingsForm is used when updating a user.
type UserSettingsForm struct {
2017-05-24 09:11:13 +02:00
NewTorrent bool ` form:"new_torrent" default:"true" `
NewTorrentEmail bool ` form:"new_torrent_email" default:"true" `
NewComment bool ` form:"new_comment" default:"true" `
NewCommentEmail bool ` form:"new_comment_email" default:"false" `
NewResponses bool ` form:"new_responses" default:"true" `
NewResponsesEmail bool ` form:"new_responses_email" default:"false" `
NewFollower bool ` form:"new_follower" default:"true" `
NewFollowerEmail bool ` form:"new_follower_email" default:"true" `
Followed bool ` form:"followed" default:"false" `
FollowedEmail bool ` form:"followed_email" default:"false" `
2017-05-21 18:13:28 +02:00
}
2017-05-06 21:21:39 +02:00
// PasswordForm is used when updating a user password.
type PasswordForm struct {
2017-05-07 14:53:01 +02:00
CurrentPassword string ` form:"currentPassword" `
Password string ` form:"newPassword" `
2017-05-06 21:21:39 +02:00
}
// SendPasswordResetForm is used when sending a password reset token.
type SendPasswordResetForm struct {
2017-05-07 14:53:01 +02:00
Email string ` form:"email" `
2017-05-06 21:21:39 +02:00
}
// PasswordResetForm is used when reseting a password.
type PasswordResetForm struct {
2017-05-07 14:53:01 +02:00
PasswordResetToken string ` form:"token" `
Password string ` form:"newPassword" `
2017-05-06 21:21:39 +02:00
}