2020-06-22 07:56:26 +09:00
< ? php
2021-12-05 04:58:00 +09:00
declare ( strict_types = 1 );
2021-10-10 17:26:18 +09:00
2020-06-22 07:56:26 +09:00
// {{{ License
2020-08-15 07:37:45 +09:00
2020-06-22 07:56:26 +09:00
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
2020-08-15 07:37:45 +09:00
2020-06-22 07:56:26 +09:00
// }}}
/**
* Handle network public feed
*
* @ package GNUsocial
* @ category Controller
*
2021-02-20 08:29:43 +09:00
* @ author Hugo Sales < hugo @ hsal . es >
2020-07-01 01:35:38 +09:00
* @ author Eliseu Amaro < eliseu @ fc . up . pt >
2021-02-20 08:29:43 +09:00
* @ copyright 2020 - 2021 Free Software Foundation , Inc http :// www . fsf . org
2020-06-22 07:56:26 +09:00
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
*/
2022-02-11 01:02:51 +09:00
namespace Component\Person\Controller ;
2020-06-22 07:56:26 +09:00
2020-07-31 07:47:47 +09:00
// {{{ Imports
2021-11-15 22:34:34 +09:00
use App\Core\Controller ;
2020-07-31 07:47:47 +09:00
use App\Core\DB\DB ;
use App\Core\Event ;
2020-06-27 07:44:53 +09:00
use App\Core\Form ;
2021-12-05 04:58:00 +09:00
use function App\Core\I18n\_m ;
2021-10-21 21:44:30 +09:00
use App\Core\Log ;
2020-07-26 02:56:38 +09:00
use App\Util\Common ;
2021-08-05 05:11:01 +09:00
use App\Util\Exception\AuthenticationException ;
2022-02-11 01:02:51 +09:00
use App\Util\Exception\NicknameEmptyException ;
use App\Util\Exception\NicknameInvalidException ;
use App\Util\Exception\NicknameNotAllowedException ;
use App\Util\Exception\NicknameTakenException ;
use App\Util\Exception\NicknameTooLongException ;
2021-12-03 11:51:41 +09:00
use App\Util\Exception\NoLoggedInUser ;
2022-02-11 01:02:51 +09:00
use App\Util\Exception\RedirectException ;
2021-08-05 05:11:01 +09:00
use App\Util\Exception\ServerException ;
2021-09-07 03:49:03 +09:00
use App\Util\Form\ActorArrayTransformer ;
2021-12-24 02:10:56 +09:00
use App\Util\Form\ActorForms ;
2021-08-06 20:12:10 +09:00
use App\Util\Form\FormFields ;
use App\Util\Formatting ;
2021-12-22 04:04:38 +09:00
use Component\Language\Controller\Language as LanguageController ;
2021-11-27 13:11:35 +09:00
use Component\Notification\Entity\UserNotificationPrefs ;
2020-07-31 07:47:47 +09:00
use Doctrine\DBAL\Types\Types ;
use Exception ;
2020-07-22 06:02:39 +09:00
use Symfony\Component\Form\Extension\Core\Type\CheckboxType ;
2021-12-03 11:51:41 +09:00
use Symfony\Component\Form\Extension\Core\Type\PasswordType ;
2020-06-22 07:56:26 +09:00
use Symfony\Component\Form\Extension\Core\Type\SubmitType ;
use Symfony\Component\Form\Extension\Core\Type\TextType ;
2021-12-03 11:51:41 +09:00
use Symfony\Component\Form\FormInterface ;
2020-06-22 07:56:26 +09:00
use Symfony\Component\HttpFoundation\Request ;
2020-07-31 07:47:47 +09:00
// }}} Imports
2022-02-11 01:02:51 +09:00
class PersonSettings extends Controller
2020-06-22 07:56:26 +09:00
{
2021-07-30 01:29:25 +09:00
/**
* Return main settings page forms
*
2022-02-11 01:02:51 +09:00
* @ throws \App\Util\Exception\ClientException
* @ throws \App\Util\Exception\NicknameException
2022-01-13 04:05:29 +09:00
* @ throws \Doctrine\DBAL\Exception
* @ throws AuthenticationException
2022-02-11 01:02:51 +09:00
* @ throws NicknameEmptyException
* @ throws NicknameInvalidException
* @ throws NicknameNotAllowedException
* @ throws NicknameTakenException
* @ throws NicknameTooLongException
2022-01-13 04:05:29 +09:00
* @ throws NoLoggedInUser
2022-02-11 01:02:51 +09:00
* @ throws RedirectException
2022-01-13 04:05:29 +09:00
* @ throws ServerException
2021-07-30 01:29:25 +09:00
*/
2021-12-22 04:04:38 +09:00
public function allSettings ( Request $request , LanguageController $language ) : array
2021-07-30 01:29:25 +09:00
{
2021-12-24 02:10:56 +09:00
// Ensure the user is logged in and retrieve Actor object for given user
$user = Common :: ensureLoggedIn ();
$actor = $user -> getActor ();
2021-12-03 11:51:41 +09:00
2022-02-16 06:16:58 +09:00
$personal_form = ActorForms :: personalInfo ( request : $request , scope : $actor , target : $actor );
2021-12-24 02:10:56 +09:00
$email_form = self :: email ( $request );
$password_form = self :: password ( $request );
$notifications_form_array = self :: notifications ( $request );
$language_form = $language -> settings ( $request );
2021-07-30 01:29:25 +09:00
2021-08-05 05:11:01 +09:00
return [
2021-12-05 04:58:00 +09:00
'_template' => 'settings/base.html.twig' ,
2021-12-09 02:53:29 +09:00
'personal_info_form' => $personal_form -> createView (),
'email_form' => $email_form -> createView (),
'password_form' => $password_form -> createView (),
'language_form' => $language_form -> createView (),
2021-08-05 05:11:01 +09:00
'tabbed_forms_notify' => $notifications_form_array ,
2021-12-05 04:58:00 +09:00
'open_details_query' => $this -> string ( 'open' ),
2021-07-30 01:29:25 +09:00
];
}
2020-11-07 04:47:15 +09:00
/**
2021-12-03 11:51:41 +09:00
* Change email settings form
*
* @ throws NoLoggedInUser
* @ throws ServerException
2020-11-07 04:47:15 +09:00
*/
2021-12-24 02:10:56 +09:00
private static function email ( Request $request ) : FormInterface
2020-06-22 07:56:26 +09:00
{
2021-12-03 11:51:41 +09:00
$user = Common :: ensureLoggedIn ();
// TODO Add support missing settings
$form = Form :: create ([
2022-01-28 01:59:43 +09:00
[ 'outgoing_email_sanitized' , TextType :: class ,
[
'label' => _m ( 'Outgoing email' ),
'required' => false ,
'help' => _m ( 'Change the email we use to contact you' ),
'data' => $user -> getOutgoingEmail () ? : '' ,
],
],
[ 'incoming_email_sanitized' , TextType :: class ,
[
'label' => _m ( 'Incoming email' ),
'required' => false ,
'help' => _m ( 'Change the email you use to contact us (for posting, for instance)' ),
'data' => $user -> getIncomingEmail () ? : '' ,
],
],
2021-12-03 11:51:41 +09:00
[ 'save_email' , SubmitType :: class , [ 'label' => _m ( 'Save email info' )]],
]);
$form -> handleRequest ( $request );
if ( $form -> isSubmitted () && $form -> isValid ()) {
$data = $form -> getData ();
foreach ( $data as $key => $val ) {
$method = 'set' . ucfirst ( Formatting :: snakeCaseToCamelCase ( $key ));
if ( method_exists ( $user , $method )) {
$user -> { $method }( $val );
}
2021-12-03 06:26:17 +09:00
}
2021-12-03 11:51:41 +09:00
DB :: flush ();
}
return $form ;
2020-06-22 07:56:26 +09:00
}
2020-07-22 06:02:39 +09:00
2020-11-07 04:47:15 +09:00
/**
2021-12-03 11:51:41 +09:00
* Change password form
*
* @ throws AuthenticationException
* @ throws NoLoggedInUser
* @ throws ServerException
2020-11-07 04:47:15 +09:00
*/
2021-12-24 02:10:56 +09:00
private static function password ( Request $request ) : FormInterface
2020-07-22 06:02:39 +09:00
{
2021-11-16 02:00:58 +09:00
$user = Common :: ensureLoggedIn ();
2021-08-05 05:11:01 +09:00
// TODO Add support missing settings
2021-11-16 02:00:58 +09:00
2021-08-05 05:11:01 +09:00
$form = Form :: create ([
2021-12-03 11:51:41 +09:00
[ 'old_password' , PasswordType :: class , [ 'label' => _m ( 'Old password' ), 'required' => true , 'help' => _m ( 'Enter your old password for verification' ), 'attr' => [ 'placeholder' => '********' ]]],
FormFields :: repeated_password ([ 'required' => true ]),
[ 'save_password' , SubmitType :: class , [ 'label' => _m ( 'Save new password' )]],
2021-08-05 05:11:01 +09:00
]);
$form -> handleRequest ( $request );
if ( $form -> isSubmitted () && $form -> isValid ()) {
$data = $form -> getData ();
2021-12-05 04:58:00 +09:00
if ( ! \is_null ( $data [ 'old_password' ])) {
2021-08-05 05:11:01 +09:00
$data [ 'password' ] = $form -> get ( 'password' ) -> getData ();
if ( ! ( $user -> changePassword ( $data [ 'old_password' ], $data [ 'password' ]))) {
throw new AuthenticationException ( _m ( 'The provided password is incorrect' ));
}
}
unset ( $data [ 'old_password' ], $data [ 'password' ]);
2021-12-03 11:51:41 +09:00
foreach ( $data as $key => $val ) {
$method = 'set' . ucfirst ( Formatting :: snakeCaseToCamelCase ( $key ));
if ( method_exists ( $user , $method )) {
$user -> { $method }( $val );
}
}
DB :: flush ();
}
return $form ;
}
2020-11-07 04:47:15 +09:00
/**
* Local user notification settings tabbed panel
2022-01-13 04:05:29 +09:00
*
* @ throws \Doctrine\DBAL\Exception
* @ throws NoLoggedInUser
* @ throws ServerException
2020-11-07 04:47:15 +09:00
*/
2021-12-24 02:10:56 +09:00
private static function notifications ( Request $request ) : array
2020-07-23 05:58:23 +09:00
{
2021-12-05 04:58:00 +09:00
$user = Common :: ensureLoggedIn ();
$schema = DB :: getConnection () -> getSchemaManager ();
$platform = $schema -> getDatabasePlatform ();
$columns = Common :: arrayRemoveKeys ( $schema -> listTableColumns ( 'user_notification_prefs' ), [ 'user_id' , 'transport' , 'created' , 'modified' ]);
2020-07-31 07:47:47 +09:00
$form_defs = [ 'placeholder' => []];
foreach ( $columns as $name => $col ) {
2022-01-13 04:05:29 +09:00
$type = $col -> getType ();
// TODO: current value is never retrieved properly, form always gets defaults
2021-12-05 04:58:00 +09:00
$val = $type -> convertToPHPValue ( $col -> getDefault (), $platform );
2021-11-15 22:37:29 +09:00
$type_str = $type -> getName ();
2021-12-05 04:58:00 +09:00
$label = str_replace ( '_' , ' ' , ucfirst ( $name ));
2020-08-06 08:05:06 +09:00
$labels = [
2021-09-18 11:22:27 +09:00
'target_actor_id' => 'Target Actors' ,
2021-12-05 04:58:00 +09:00
'dm' => 'DM' ,
2020-08-06 08:05:06 +09:00
];
$help = [
2021-12-05 04:58:00 +09:00
'target_actor_id' => 'If specified, these settings apply only to these profiles (comma- or space-separated list)' ,
2021-11-08 22:44:35 +09:00
'activity_by_subscribed' => 'Notify me when someone I subscribed has new activity' ,
2021-12-05 04:58:00 +09:00
'mention' => 'Notify me when mentions me in a notice' ,
'reply' => 'Notify me when someone replies to a notice made by me' ,
'subscription' => 'Notify me when someone subscribes to me or asks for permission to do so' ,
'favorite' => 'Notify me when someone favorites one of my notices' ,
'nudge' => 'Notify me when someone nudges me' ,
'dm' => 'Notify me when someone sends me a direct message' ,
'post_on_status_change' => 'Post a notice when my status in this service changes' ,
'enable_posting' => 'Enable posting from this service' ,
2020-08-06 08:05:06 +09:00
];
2020-08-06 01:31:39 +09:00
switch ( $type_str ) {
2021-12-03 11:51:41 +09:00
case Types :: BOOLEAN :
$form_defs [ 'placeholder' ][ $name ] = [ $name , CheckboxType :: class , [ 'data' => $val , 'required' => false , 'label' => _m ( $labels [ $name ] ? ? $label ), 'help' => _m ( $help [ $name ])]];
break ;
case Types :: INTEGER :
2022-01-13 04:05:29 +09:00
if ( $name === 'target_actor_id' ) {
2021-12-03 11:51:41 +09:00
$form_defs [ 'placeholder' ][ $name ] = [ $name , TextType :: class , [ 'data' => $val , 'required' => false , 'label' => _m ( $labels [ $name ]), 'help' => _m ( $help [ $name ])], 'transformer' => ActorArrayTransformer :: class ];
}
break ;
default :
// @codeCoverageIgnoreStart
Log :: critical ( " Structure of table user_notification_prefs changed in a way not accounted to in notification settings ( { $name } ): " . $type_str );
throw new ServerException ( _m ( 'Internal server error' ));
2021-08-05 05:11:01 +09:00
// @codeCoverageIgnoreEnd
2020-07-31 07:47:47 +09:00
}
}
2021-12-05 04:58:00 +09:00
$form_defs [ 'placeholder' ][ 'save' ] = fn ( string $transport , string $form_name ) => [ $form_name , SubmitType :: class ,
[ 'label' => _m ( 'Save notification settings for {transport}' , [ 'transport' => $transport ])], ];
2021-08-08 03:24:11 +09:00
2020-10-20 03:22:59 +09:00
Event :: handle ( 'AddNotificationTransport' , [ & $form_defs ]);
2020-07-31 07:47:47 +09:00
unset ( $form_defs [ 'placeholder' ]);
$tabbed_forms = [];
2021-12-27 07:17:26 +09:00
foreach ( $form_defs as $transport_name => $f ) { // @phpstan-ignore-line
2021-08-08 03:24:11 +09:00
unset ( $f [ 'save' ]);
2021-12-09 02:53:29 +09:00
$form = Form :: create ( $f );
$tabbed_forms [ $transport_name ][ 'title' ] = $transport_name ;
$tabbed_forms [ $transport_name ][ 'desc' ] = _m ( '{transport} notification settings' , [ 'transport' => $transport_name ]);
$tabbed_forms [ $transport_name ][ 'id' ] = " settings-notifications- { $transport_name } " ;
$tabbed_forms [ $transport_name ][ 'form' ] = $form -> createView ();
2021-08-08 03:24:11 +09:00
$form -> handleRequest ( $request );
2022-01-13 04:05:29 +09:00
// TODO: on submit, form reports a nonce error. Therefore, user changes are not applied
// errors: array:1 [▼
// 0 => Symfony\Component\Form\FormError {#2956 ▼
// #messageTemplate: "Invalid nonce"
// #messageParameters: []
// #messagePluralization: null
// -message: "Invalid nonce"
// -cause: Symfony\Component\Security\Csrf\CsrfToken {#2955 ▶}
// -origin: Symfony\Component\Form\Form {#2868}
// }
// ]
if ( $form -> isSubmitted ()) {
2021-08-08 03:24:11 +09:00
$data = $form -> getData ();
2021-08-08 03:25:10 +09:00
unset ( $data [ 'translation_domain' ]);
2021-08-05 05:11:01 +09:00
try {
2022-01-13 04:05:29 +09:00
[ $entity , $is_update ] = UserNotificationPrefs :: createOrUpdate (
2021-08-05 05:11:01 +09:00
array_merge ([ 'user_id' => $user -> getId (), 'transport' => $transport_name ], $data ),
2021-10-10 17:26:18 +09:00
find_by_keys : [ 'user_id' , 'transport' ],
2021-08-05 05:11:01 +09:00
);
if ( ! $is_update ) {
2022-01-13 04:05:29 +09:00
DB :: persist ( $entity );
2021-08-05 05:11:01 +09:00
}
DB :: flush ();
// @codeCoverageIgnoreStart
2021-10-10 17:26:18 +09:00
} catch ( Exception $e ) {
2021-08-05 05:11:01 +09:00
// Somehow, the exception doesn't bubble up in phpunit
2021-10-21 21:44:30 +09:00
// dd($data, $e);
2021-08-05 05:11:01 +09:00
// @codeCoverageIgnoreEnd
2021-10-21 21:44:30 +09:00
Log :: critical ( 'Exception at ' . $e -> getFile () . ':' . $e -> getLine () . ': ' . $e -> getMessage ());
2021-08-08 03:25:10 +09:00
}
2021-08-08 03:24:11 +09:00
}
2020-07-31 07:47:47 +09:00
}
2021-07-30 01:29:25 +09:00
return $tabbed_forms ;
2020-07-23 05:58:23 +09:00
}
2020-07-23 23:08:31 +09:00
}