2021-12-23 17:10:56 +00:00
< ? php
declare ( strict_types = 1 );
// {{{ License
// 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/>.
// }}}
/**
* Transform between string and list of typed profiles
*
* @ package GNUsocial
* @ category Form
*
* @ author Hugo Sales < hugo @ hsal . es >
* @ copyright 2020 - 2021 Free Software Foundation , Inc http :// www . fsf . org
* @ license https :// www . gnu . org / licenses / agpl . html GNU AGPL v3 or later
*/
namespace App\Util\Form ;
use App\Core\Cache ;
2022-03-31 03:47:14 +01:00
use App\Core\Event ;
2021-12-23 17:10:56 +00:00
use App\Core\Form ;
use function App\Core\I18n\_m ;
use App\Entity\Actor ;
2022-01-27 17:53:02 +00:00
use App\Util\Exception\ClientException ;
2021-12-23 17:10:56 +00: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 ;
use App\Util\Exception\ServerException ;
2022-01-27 17:53:02 +00:00
use App\Util\Nickname ;
2021-12-23 17:10:56 +00:00
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType ;
use Symfony\Component\Form\Extension\Core\Type\SubmitType ;
use Symfony\Component\Form\Extension\Core\Type\TextareaType ;
use Symfony\Component\Form\Extension\Core\Type\TextType ;
use Symfony\Component\HttpFoundation\Request ;
class ActorForms
{
/**
* Actor personal information panel
*
2022-02-20 04:34:56 +00:00
* @ param Actor $scope The perpetrator of the change
2022-02-15 21:16:58 +00:00
* @ param Actor $target The victim of changes
2022-02-20 04:34:56 +00:00
*
* @ throws \App\Util\Exception\NicknameException
2022-01-27 17:53:02 +00:00
* @ throws ClientException
2021-12-23 17:10:56 +00:00
* @ throws NicknameEmptyException
* @ throws NicknameInvalidException
* @ throws NicknameNotAllowedException
* @ throws NicknameTakenException
* @ throws NicknameTooLongException
* @ throws ServerException
*/
2022-02-15 21:16:58 +00:00
public static function personalInfo ( Request $request , Actor $scope , Actor $target ) : mixed
2021-12-23 17:10:56 +00:00
{
2022-02-15 21:16:58 +00:00
// Is $target in $scope's sight?
if ( ! $scope -> canModerate ( $target )) {
throw new ClientException ( _m ( 'You do not have permissions to change :nickname\'s settings' , [ ':nickname' => $target -> getNickname ()]));
}
2021-12-23 17:10:56 +00:00
// Defining the various form fields
$form_definition = [
[ 'nickname' , TextType :: class , [ 'label' => _m ( 'Nickname' ), 'required' => true , 'help' => _m ( '1-64 lowercase letters or numbers, no punctuation or spaces.' )]],
[ 'full_name' , TextType :: class , [ 'label' => _m ( 'Full Name' ), 'required' => false , 'help' => _m ( 'A full name is required, if empty it will be set to your nickname.' )]],
[ 'homepage' , TextType :: class , [ 'label' => _m ( 'Homepage' ), 'required' => false , 'help' => _m ( 'URL of your homepage, blog, or profile on another site.' )]],
[ 'bio' , TextareaType :: class , [ 'label' => _m ( 'Bio' ), 'required' => false , 'help' => _m ( 'Describe yourself and your interests.' )]],
[ 'phone_number' , PhoneNumberType :: class , [ 'label' => _m ( 'Phone number' ), 'required' => false , 'help' => _m ( 'Your phone number' ), 'data_class' => null ]],
[ 'location' , TextType :: class , [ 'label' => _m ( 'Location' ), 'required' => false , 'help' => _m ( 'Where you are, like "City, State (or Region), Country".' )]],
[ 'save_personal_info' , SubmitType :: class , [ 'label' => _m ( 'Save personal info' )]],
];
// Setting nickname normalised and setting actor cache
2022-02-20 04:34:56 +00:00
$before_step = static function ( & $data , $extra_args ) use ( $target ) {
// Validate nickname
2022-02-15 21:16:58 +00:00
if ( $target -> getNickname () !== $data [ 'nickname' ]) {
// We must only check if is both already used and allowed if the actor is local
$check_is_allowed = $target -> getIsLocal ();
if ( $target -> isGroup ()) {
$data [ 'nickname' ] = Nickname :: normalize ( $data [ 'nickname' ], check_already_used : $check_is_allowed , which : Nickname :: CHECK_LOCAL_GROUP , check_is_allowed : $check_is_allowed );
} else {
$data [ 'nickname' ] = Nickname :: normalize ( $data [ 'nickname' ], check_already_used : $check_is_allowed , which : Nickname :: CHECK_LOCAL_USER , check_is_allowed : $check_is_allowed );
}
// We will set $target actor's nickname in the form::handle,
// but if it is local, we must update the local reference as well
2022-02-20 04:34:56 +00:00
if ( ! \is_null ( $local = $target -> getLocal ())) {
2022-02-15 21:16:58 +00:00
$local -> setNickname ( $data [ 'nickname' ]);
}
2022-01-27 17:53:02 +00:00
}
2022-02-20 04:34:56 +00:00
// Validate full name
2022-02-15 21:16:58 +00:00
if ( $target -> getFullname () !== $data [ 'full_name' ]) {
2022-02-26 14:45:38 +00:00
if ( ! \is_null ( $data [ 'full_name' ])) {
2022-02-20 04:34:56 +00:00
if ( mb_strlen ( $data [ 'full_name' ]) > 64 ) {
throw new ClientException ( _m ( 'Full name cannot be more than 64 character long.' ));
}
2022-01-27 17:53:02 +00:00
}
2022-01-12 17:46:13 +00:00
}
2021-12-23 17:10:56 +00:00
2022-02-20 04:34:56 +00:00
// Delete related cache
2022-10-19 22:39:17 +01:00
$cache_keys = array_intersect_key ( Actor :: cacheKeys ( $target -> getId ()), array_flip ([ 'id' , 'nickname' , 'fullname' ]));
2022-03-31 03:47:14 +01:00
foreach ( $cache_keys as $key ) {
Cache :: delete ( $key );
2021-12-23 17:10:56 +00:00
}
2022-10-19 22:38:49 +01:00
Event :: handle ( 'ActorFormInvalidateRelated' , [ $target , $local ? ? null ]);
2021-12-23 17:10:56 +00:00
};
return Form :: handle (
$form_definition ,
$request ,
target : $target ,
extra_args : [],
2022-02-15 21:16:58 +00:00
before_step : $before_step ,
2021-12-23 17:10:56 +00:00
);
}
}