<commit_before><?php

namespace OpenCFP\Http\Controller;

use OpenCFP\Domain\Model\User;
use OpenCFP\Domain\Services\Authentication;
use OpenCFP\Http\Form\SignupForm;
use Silex\Application;
use Spot\Locator;
use Symfony\Component\HttpFoundation\Request;

class ProfileController extends BaseController
{
    use FlashableTrait;

    public function editAction(Request $req)
    {
        $user = $this->service(Authentication::class)->user();

        if ((string) $user->getId() !== $req->get('id')) {
            $this->service('session')->set('flash', [
                'type' => 'error',
                'short' => 'Error',
                'ext' => "You cannot edit someone else's profile",
            ]);

            return $this->redirectTo('dashboard');
        }

        /* @var Locator $spot */
        $spot = $this->service('spot');

        $mapper = $spot->mapper('\OpenCFP\Domain\Entity\User');
        $speaker_data = $mapper->get($user->getId())->toArray();

        $form_data = [
            'email' => $user->getLogin(),
            'first_name' => $speaker_data['first_name'],
            'last_name' => $speaker_data['last_name'],
            'company' => $speaker_data['company'],
            'twitter' => $speaker_data['twitter'],
            'url' => $speaker_data['url'],
            'speaker_info' => $speaker_data['info'],
            'speaker_bio' => $speaker_data['bio'],
            'speaker_photo' => $speaker_data['photo_path'],
            'preview_photo' => '/uploads/' . $speaker_data['photo_path'],
            'airport' => $speaker_data['airport'],
            'transportation' => $speaker_data['transportation'],
            'hotel' => $speaker_data['hotel'],
            'id' => $user->getId(),
            'formAction' => $this->url('user_update'),
            'buttonInfo' => 'Update Profile',
        ];

        return $this->render('user/edit.twig', $form_data) ;
    }

    public function processAction(Request $req)
    {
        $userId = $this->service(Authentication::class)->userId();

        if ((string) $userId !== $req->get('id')) {
            $this->service('session')->set('flash', [
                'type' => 'error',
                'short' => 'Error',
                'ext' => "You cannot edit someone else's profile",
            ]);

            return $this->redirectTo('dashboard');
        }

        $form_data = $this->getFormData($req);

        if ($req->files->get('speaker_photo') != null) {
            // Upload Image
            $form_data['speaker_photo'] = $req->files->get('speaker_photo');
        }

        $form = new SignupForm($form_data, $this->service('purifier'));
        $isValid = $form->validateAll('update');

        if ($isValid) {
            $sanitized_data = $this->transformSanitizedData($form->getCleanData());

            if (isset($form_data['speaker_photo'])) {
                /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
                $file = $form_data['speaker_photo'];
                /** @var \OpenCFP\Domain\Services\ProfileImageProcessor $processor */
                $processor = $this->service('profile_image_processor');
                /** @var PseudoRandomStringGenerator $generator */
                $generator = $this->service('security.random');

                /**
                 * The extension technically is not required. We guess the extension using a trusted method.
                 */
                $sanitized_data['speaker_photo'] = $generator->generate(40) . '.' . $file->guessExtension();

                $processor->process($file, $sanitized_data['speaker_photo']);
            }

            User::find($userId)->update($sanitized_data);
            return $this->redirectTo('dashboard');
        }
        $this->service('session')->set('flash', [
                'type' => 'error',
                'short' => 'Error',
                'ext' => implode('<br>', $form->getErrorMessages()),
            ]);

        $form_data['formAction'] = $this->url('user_update');
        $form_data['buttonInfo'] = 'Update Profile';
        $form_data['id'] = $userId;
        $form_data['flash'] = $this->getFlash($this->app);

        return $this->render('user/edit.twig', $form_data);
    }

    public function passwordAction()
    {
        return $this->render('user/change_password.twig');
    }

    public function passwordProcessAction(Request $req)
    {
        $user = $this->service(Authentication::class)->user();

        /**
         * Okay, the logic is kind of weird but we can use the SignupForm<commit_msg>Rename speaker_photo to photo_path<commit_after><?php

namespace OpenCFP\Http\Controller;

use OpenCFP\Domain\Model\User;
use OpenCFP\Domain\Services\Authentication;
use OpenCFP\Http\Form\SignupForm;
use Silex\Application;
use Spot\Locator;
use Symfony\Component\HttpFoundation\Request;

class ProfileController extends BaseController
{
    use FlashableTrait;

    public function editAction(Request $req)
    {
        $user = $this->service(Authentication::class)->user();

        if ((string) $user->getId() !== $req->get('id')) {
            $this->service('session')->set('flash', [
                'type' => 'error',
                'short' => 'Error',
                'ext' => "You cannot edit someone else's profile",
            ]);

            return $this->redirectTo('dashboard');
        }

        /* @var Locator $spot */
        $spot = $this->service('spot');

        $mapper = $spot->mapper('\OpenCFP\Domain\Entity\User');
        $speaker_data = $mapper->get($user->getId())->toArray();

        $form_data = [
            'email' => $user->getLogin(),
            'first_name' => $speaker_data['first_name'],
            'last_name' => $speaker_data['last_name'],
            'company' => $speaker_data['company'],
            'twitter' => $speaker_data['twitter'],
            'url' => $speaker_data['url'],
            'speaker_info' => $speaker_data['info'],
            'speaker_bio' => $speaker_data['bio'],
            'speaker_photo' => $speaker_data['photo_path'],
            'preview_photo' => '/uploads/' . $speaker_data['photo_path'],
            'airport' => $speaker_data['airport'],
            'transportation' => $speaker_data['transportation'],
            'hotel' => $speaker_data['hotel'],
            'id' => $user->getId(),
            'formAction' => $this->url('user_update'),
            'buttonInfo' => 'Update Profile',
        ];

        return $this->render('user/edit.twig', $form_data) ;
    }

    public function processAction(Request $req)
    {
        $userId = $this->service(Authentication::class)->userId();

        if ((string) $userId !== $req->get('id')) {
            $this->service('session')->set('flash', [
                'type' => 'error',
                'short' => 'Error',
                'ext' => "You cannot edit someone else's profile",
            ]);

            return $this->redirectTo('dashboard');
        }

        $form_data = $this->getFormData($req);

        if ($req->files->get('speaker_photo') != null) {
            // Upload Image
            $form_data['speaker_photo'] = $req->files->get('speaker_photo');
        }

        $form = new SignupForm($form_data, $this->service('purifier'));
        $isValid = $form->validateAll('update');

        if ($isValid) {
            $sanitized_data = $this->transformSanitizedData($form->getCleanData());

            if (isset($form_data['speaker_photo'])) {
                /** @var \Symfony\Component\HttpFoundation\File\UploadedFile $file */
                $file = $form_data['speaker_photo'];
                /** @var \OpenCFP\Domain\Services\ProfileImageProcessor $processor */
                $processor = $this->service('profile_image_processor');
                /** @var PseudoRandomStringGenerator $generator */
                $generator = $this->service('security.random');

                /**
                 * The extension technically is not required. We guess the extension using a trusted method.
                 */
                $sanitized_data['photo_path'] = $generator->generate(40) . '.' . $file->guessExtension();

                $processor->process($file, $sanitized_data['photo_path']);
            }

            User::find($userId)->update($sanitized_data);
            return $this->redirectTo('dashboard');
        }
        $this->service('session')->set('flash', [
                'type' => 'error',
                'short' => 'Error',
                'ext' => implode('<br>', $form->getErrorMessages()),
            ]);

        $form_data['formAction'] = $this->url('user_update');
        $form_data['buttonInfo'] = 'Update Profile';
        $form_data['id'] = $userId;
        $form_data['flash'] = $this->getFlash($this->app);

        return $this->render('user/edit.twig', $form_data);
    }

    public function passwordAction()
    {
        return $this->render('user/change_password.twig');
    }

    public function passwordProcessAction(Request $req)
    {
        $user = $this->service(Authentication::class)->user();

        /**
         * Okay, the logic is kind of weird but we can use the SignupForm
{'fraction_non_alphanumeric': 0.171329051921193, 'fraction_numerical': 0.00043539784478066834, 'mean_word_length': 2.248939179632249, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 151, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6531402', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2588, 'n_words': 442}