2015-10-29 09:19:53 -04:00
<? php
class MailActions extends Actions
{
2018-05-21 11:49:15 -04:00
public static function executeSubscribe ()
2015-10-29 09:19:53 -04:00
{
2018-05-21 11:49:15 -04:00
if ( ! Request :: isPost ()) {
return [ 'mail/subscribe' ];
}
$nextUrl = Request :: getPostParam ( 'returnUrl' , '/' );
if ( ! $nextUrl || $nextUrl [ 0 ] != '/' || ! filter_var ( $nextUrl , FILTER_VALIDATE_URL )) {
$nextUrl = '/' ;
}
$email = Request :: getPostParam ( 'email' );
if ( ! $email || ! filter_var ( $email , FILTER_VALIDATE_EMAIL )) {
Session :: set (
2021-05-14 20:10:54 +08:00
Session :: KEY_LIST_SUB_ERROR ,
$email ? __ ( 'Please provide a valid email address.' ) : __ ( 'Please provide an email address.' )
);
2018-05-21 11:49:15 -04:00
return Controller :: redirect ( Request :: getRelativeUri ());
}
$tag = Request :: getPostParam ( 'tag' );
$response = LBRY :: subscribe ( $email , $tag );
if ( $response [ 'error' ]) {
return [ 'mail/subscribe' , [ 'error' => $response [ 'error' ]]];
}
return [ 'mail/subscribe' , [ 'subscribeSuccess' => true , 'nextUrl' => $nextUrl ]];
2015-10-29 09:19:53 -04:00
}
2016-03-02 21:14:26 -05:00
2018-05-21 11:49:15 -04:00
public static function executeSubscribed ()
2016-09-22 10:06:24 -04:00
{
2018-05-21 11:49:15 -04:00
return [ 'mail/subscribe' , [ 'confirmSuccess' => true , 'learnFooter' => true ]];
2016-09-22 10:06:24 -04:00
}
2016-09-15 11:24:14 -04:00
2018-05-21 11:49:15 -04:00
public static function prepareSubscribeFormPartial ( array $vars )
{
$vars += [ 'btnClass' => 'btn-primary' , 'returnUrl' => Request :: getRelativeUri ()];
$vars [ 'error' ] = Session :: get ( Session :: KEY_LIST_SUB_ERROR );
Session :: unsetKey ( Session :: KEY_LIST_SUB_ERROR );
return $vars ;
2015-10-29 09:19:53 -04:00
}
2018-05-21 11:49:15 -04:00
public static function executeUnsubscribe ( string $email )
2016-09-15 11:24:14 -04:00
{
2019-09-22 10:06:46 -04:00
Response :: disableHttpCache ();
2018-05-21 11:49:15 -04:00
$decodedEmail = Encoding :: base64DecodeUrlsafe ( urldecode ( $email ));
if ( ! $decodedEmail ) {
return [ 'mail/unsubscribe' , [ 'error' => 'Invalid unsubscribe link' ]];
}
$response = LBRY :: unsubscribe ( $decodedEmail );
return [ 'mail/unsubscribe' , [ 'error' => $response [ 'error' ]]];
2016-09-15 11:24:14 -04:00
}
2018-08-13 22:28:31 -04:00
public static function editEmailSettings ( string $token )
{
2019-09-22 10:06:46 -04:00
Response :: disableHttpCache ();
2018-09-15 20:55:03 -04:00
list ( $status , $headers , $response ) = LBRY :: emailStatus ( $token );
2018-10-23 17:27:07 +05:30
if ( $status == 403 ) {
2018-09-15 20:55:03 -04:00
//Catch 403 to return elegant error message.
2019-03-19 16:59:22 -05:00
$response [ 'error' ] = "This email link is invalid. If you clicked this from an older email it may have been expired for security purposes. Please email help@lbry.com for a valid one." ;
2018-09-15 20:55:03 -04:00
}
2018-08-21 19:08:15 -04:00
$responseData = $response [ 'data' ] ?? [];
2018-08-17 17:01:16 -04:00
return [ 'mail/settings' , [
2018-08-21 19:08:15 -04:00
'emails' => $responseData [ 'emails' ] ?? [],
'tags' => $responseData [ 'tags' ] ?? [],
2018-08-17 17:01:16 -04:00
'token' => $token ,
'error' => $response [ 'error' ] ?? false
]];
2018-08-13 22:28:31 -04:00
}
2018-08-21 19:08:15 -04:00
public static function prepareSettingsFormPartial ( array $vars )
{
2021-05-14 20:10:54 +08:00
$tags = LBRY :: listTags ( $vars [ 'token' ]);
$tagMetadata = [];
2019-10-24 10:27:48 -04:00
2021-05-14 20:10:54 +08:00
foreach ( $tags as $tag ) {
if ( $tag [ 'is_user_addable' ]) {
$tagMetadata [ $tag [ 'name' ]] = [
2019-10-24 10:27:48 -04:00
'label' => $tag [ 'display_name' ],
2019-09-19 12:14:15 -04:00
'description' => $tag [ 'description' ]
];
2021-05-14 20:10:54 +08:00
}
2019-09-19 12:14:15 -04:00
}
2021-05-14 20:10:54 +08:00
return $vars + [ 'tagMetadata' => $tagMetadata ];
2018-08-21 19:08:15 -04:00
}
2018-05-21 11:49:15 -04:00
}