<commit_before><?php

namespace HtProfileImage\Controller;

use HtProfileImage\Form\ProfileImageForm;
use Zend\View\Model;
use Zend\Mvc\Controller\AbstractActionController;
use HtProfileImage\Service\ProfileImageServiceInterface;
use HtImgModule\View\Model\ImageModel;

class ProfileImageController extends AbstractActionController
{
    /**
     * @var ProfileImageServiceInterface
     */
    protected $profileImageService;

    /**
     * @var \HtProfileImage\Options\ModuleOptionsInterface
     */
    protected $options;

    /**
     * @var \ZfcUser\Mapper\UserInterface
     */
    protected $userMapper;

    /**
     *  Constructor
     *
     * @param ProfileImageServiceInterface $profileImageService
     */
    public function __construct(ProfileImageServiceInterface $profileImageService)
    {
        $this->profileImageService = $profileImageService;
    }

    /**
     * Uploads User Image
     */
    public function uploadAction()
    {
        $user = $this->getUser();
        if (!$user) {
            return $this->notFoundAction();
        }
        $options = $this->getOptions();
        $form = $this->getServiceLocator()->get('HtProfileImage\ProfileImageForm');
        $request = $this->getRequest();
        $imageUploaded = false;
        if ($request->isPost()) {
            if ($this->profileImageService->storeImage($user, $request->getFiles()->toArray())) {
                if ($request->isXmlHttpRequest()) {
                    return new Model\JsonModel([
                        'uploaded' => true
                    ]);
                } elseif ($options->getPostUploadRoute()) {
                        return call_user_func_array([$this->redirect(), 'toRoute'], (array) $options->getPostUploadRoute());
                }
                $imageUploaded = true;
            } else {
                if ($request->isXmlHttpRequest()) {
                    return new Model\JsonModel([
                        'error' => true,
                        'messages' => $form->getMessages()
                    ]);
                }

            }
        }

        return new Model\ViewModel([
            'form' => $form,
            'imageUploaded' => $imageUploaded,
            'user' => $user
        ]);
    }

    /**
     * Displays User Image
     */
    public function displayAction()
    {
        $id = $this->params()->fromRoute('id', null);
        $filter = $this->params()->fromRoute('filter', null);
        if (!$id) {
            return $this->notFoundAction();
        }
        $user = $this->getUserMapper()->findById($id);
        if (!$user) {
            return $this->notFoundAction();
        }
        $image = $this->profileImageService->getUserImage($user, $filter);

        return new ImageModel($image);
    }

    public function deleteAction()
    {
        $user = $this->getUser();
        if (!$user) {
            return $this->notFoundAction();
        }
        $this->profileImageService->deleteUserImage($user);

        return call_user_func_array([$this->redirect(), 'toRoute'], (array) $this->getOptions()->getPostImageDeleteRoute());
    }

    protected function getUser()
    {
        $authenticationService = $this->getServiceLocator()->get('zfcuser_auth_service');
        if (!$authenticationService->hasIdentity()) {
            return $this->redirect()->toRoute('zfcuser');
        }
        $user = $authenticationService->getIdentity();

        $userId = $this->params()->fromRoute('userId', null);
        if ($userId !== null) {
            $currentUser = $user;
            $user = $this->getUserMapper()->findById($userId);
            if (!$user) {
                return $this->notFoundAction();
            }
            if (!$this->getOptions()->getEnableInterUserImageUpload() && ($user->getId() !== $currentUser->getId())) {
                return $this->notFoundAction();
            }
        }
        
        return $user;         
    }

    /**
     * Gets options
     *
     * @return \HtProfileImage\Options\ModuleOptionsInterface
     */
    public function getOptions()
    {
        if (!$this->options) {
            $this->options = $this->getServiceLocator()->get('HtProfileImage\ModuleOptions');
        }

        return $this->options;
    }

    /**
     * Gets userMapper
     *
     * @return \ZfcUser\Mapper\UserInterface
     */
    public function getUserMapper()
    {
        if (!$this->userMapper) {
            $this->userMapper = $this->getServiceLocator()->get('zfcuser_user_mapper');
        }

        return $this->userMapper;
    }
}
<commit_msg>Disallow image delete if not enabled<commit_after><?php

namespace HtProfileImage\Controller;

use HtProfileImage\Form\ProfileImageForm;
use Zend\View\Model;
use Zend\Mvc\Controller\AbstractActionController;
use HtProfileImage\Service\ProfileImageServiceInterface;
use HtImgModule\View\Model\ImageModel;

class ProfileImageController extends AbstractActionController
{
    /**
     * @var ProfileImageServiceInterface
     */
    protected $profileImageService;

    /**
     * @var \HtProfileImage\Options\ModuleOptionsInterface
     */
    protected $options;

    /**
     * @var \ZfcUser\Mapper\UserInterface
     */
    protected $userMapper;

    /**
     *  Constructor
     *
     * @param ProfileImageServiceInterface $profileImageService
     */
    public function __construct(ProfileImageServiceInterface $profileImageService)
    {
        $this->profileImageService = $profileImageService;
    }

    /**
     * Uploads User Image
     */
    public function uploadAction()
    {
        $user = $this->getUser();
        if (!$user) {
            return $this->notFoundAction();
        }
        $options = $this->getOptions();
        $form = $this->getServiceLocator()->get('HtProfileImage\ProfileImageForm');
        $request = $this->getRequest();
        $imageUploaded = false;
        if ($request->isPost()) {
            if ($this->profileImageService->storeImage($user, $request->getFiles()->toArray())) {
                if ($request->isXmlHttpRequest()) {
                    return new Model\JsonModel([
                        'uploaded' => true
                    ]);
                } elseif ($options->getPostUploadRoute()) {
                        return call_user_func_array([$this->redirect(), 'toRoute'], (array) $options->getPostUploadRoute());
                }
                $imageUploaded = true;
            } else {
                if ($request->isXmlHttpRequest()) {
                    return new Model\JsonModel([
                        'error' => true,
                        'messages' => $form->getMessages()
                    ]);
                }

            }
        }

        return new Model\ViewModel([
            'form' => $form,
            'imageUploaded' => $imageUploaded,
            'user' => $user
        ]);
    }

    /**
     * Displays User Image
     */
    public function displayAction()
    {
        $id = $this->params()->fromRoute('id', null);
        $filter = $this->params()->fromRoute('filter', null);
        if (!$id) {
            return $this->notFoundAction();
        }
        $user = $this->getUserMapper()->findById($id);
        if (!$user) {
            return $this->notFoundAction();
        }
        $image = $this->profileImageService->getUserImage($user, $filter);

        return new ImageModel($image);
    }

    public function deleteAction()
    {
        if (!$this->getOptions()->getEnableImageDelete()) {
            return $this->notFoundAction();
        }
        $user = $this->getUser();
        if (!$user) {
            return $this->notFoundAction();
        }
        $this->profileImageService->deleteUserImage($user);

        return call_user_func_array([$this->redirect(), 'toRoute'], (array) $this->getOptions()->getPostImageDeleteRoute());
    }

    protected function getUser()
    {
        $authenticationService = $this->getServiceLocator()->get('zfcuser_auth_service');
        if (!$authenticationService->hasIdentity()) {
            return $this->redirect()->toRoute('zfcuser');
        }
        $user = $authenticationService->getIdentity();

        $userId = $this->params()->fromRoute('userId', null);
        if ($userId !== null) {
            $currentUser = $user;
            $user = $this->getUserMapper()->findById($userId);
            if (!$user) {
                return $this->notFoundAction();
            }
            if (!$this->getOptions()->getEnableInterUserImageUpload() && ($user->getId() !== $currentUser->getId())) {
                return $this->notFoundAction();
            }
        }
        
        return $user;         
    }

    /**
     * Gets options
     *
     * @return \HtProfileImage\Options\ModuleOptionsInterface
     */
    public function getOptions()
    {
        if (!$this->options) {
            $this->options = $this->getServiceLocator()->get('HtProfileImage\ModuleOptions');
        }

        return $this->options;
    }

    /**
     * Gets userMapper
     *
     * @return \ZfcUser\Mapper\UserInterface
     */
    public function getUserMapper()
    {
        if (!$this->userMapper) {
            $this->userMapper = $this->getServiceLocator()->get('zfcuser_user_mapper');
        }

        return $this->userMapper;
    }
}

{'fraction_non_alphanumeric': 0.14400686768966628, 'fraction_numerical': 0.0, 'mean_word_length': 1.9747845515480371, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 140, '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': '4657365', 'n_tokens_mistral': 2511, 'n_tokens_neox': 2473, 'n_words': 411}