<commit_before><?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Zend\Mvc\Controller\Plugin;

use Zend\Filter\FilterChain;
use Zend\Form\Form;
use Zend\Http\Response;
use Zend\InputFilter\FileInput;
use Zend\InputFilter\InputFilterInterface;
use Zend\Mvc\Exception\RuntimeException;
use Zend\Session\Container;
use Zend\Validator\ValidatorChain;

/**
 * Plugin to help facilitate Post/Redirect/Get for file upload forms
 * (http://en.wikipedia.org/wiki/Post/Redirect/Get)
 *
 * Requires that the Form's File inputs contain a 'fileRenameUpload' filter
 * with the target option set: 'target' => /valid/target/path'.
 * This is so the files are moved to a new location between requests.
 * If this filter is not added, the temporary upload files will disappear
 * between requests.
 */
class FilePostRedirectGet extends AbstractPlugin
{
    /**
     * @var Container
     */
    protected $sessionContainer;

    /**
     * @param  Form    $form
     * @param  string  $redirect      Route or URL string (default: current route)
     * @param  boolean $redirectToUrl Use $redirect as a URL string (default: false)
     * @return boolean|array|Response
     */
    public function __invoke(Form $form, $redirect = null, $redirectToUrl = false)
    {
        $request = $this->getController()->getRequest();
        if ($request->isPost()) {
            return $this->handlePostRequest($form, $redirect, $redirectToUrl);
        } else {
            return $this->handleGetRequest($form);
        }
    }

    /**
     * @param  Form    $form
     * @param  string  $redirect      Route or URL string (default: current route)
     * @param  boolean $redirectToUrl Use $redirect as a URL string (default: false)
     * @return Response
     */
    protected function handlePostRequest(Form $form, $redirect, $redirectToUrl)
    {
        $container = $this->getSessionContainer();
        $request   = $this->getController()->getRequest();

        // Change required flag to false for any previously uploaded files
        $inputFilter   = $form->getInputFilter();
        $previousFiles = ($container->files) ?: array();
        $this->traverseInputs(
            $inputFilter,
            $previousFiles,
            function($input, $value) {
                if ($input instanceof FileInput) {
                    $input->setRequired(false);
                }
                return $value;
            }
        );

        // Run the form validations/filters and retrieve any errors
        $postFiles = $request->getFiles()->toArray();
        $postOther = $request->getPost()->toArray();
        $post      = array_merge($postOther, $postFiles);

        $form->setData($post);
        $isValid = $form->isValid();
        $data    = $form->getData(Form::VALUES_AS_ARRAY);
        $errors  = (!$isValid) ? $form->getMessages() : null;

        // Loop through data and merge previous files with new valid files
        $postFiles = array_merge(
            $previousFiles,
            $this->filterInvalidFileInputPostData($inputFilter, $data)
        );
        $post = array_merge($post, $postFiles);

        // Save form data in session
        $container->setExpirationHops(1, array('post', 'errors', 'isValid'));
        $container->post    = $post;
        $container->errors  = $errors;
        $container->isValid = $isValid;
        $container->files   = $postFiles;

        return $this->redirect($redirect, $redirectToUrl);
    }

    /**
     * @param  Form $form
     * @return boolean|array
     */
    protected function handleGetRequest(Form $form)
    {
        $container = $this->getSessionContainer();
        if (null === $container->post) {
            // No previous post, bail early<commit_msg>replace array_merge with ArrayUtils::merge<commit_after><?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Zend\Mvc\Controller\Plugin;

use Zend\Filter\FilterChain;
use Zend\Form\Form;
use Zend\Http\Response;
use Zend\InputFilter\FileInput;
use Zend\InputFilter\InputFilterInterface;
use Zend\Mvc\Exception\RuntimeException;
use Zend\Session\Container;
use Zend\Stdlib\ArrayUtils;
use Zend\Validator\ValidatorChain;

/**
 * Plugin to help facilitate Post/Redirect/Get for file upload forms
 * (http://en.wikipedia.org/wiki/Post/Redirect/Get)
 *
 * Requires that the Form's File inputs contain a 'fileRenameUpload' filter
 * with the target option set: 'target' => /valid/target/path'.
 * This is so the files are moved to a new location between requests.
 * If this filter is not added, the temporary upload files will disappear
 * between requests.
 */
class FilePostRedirectGet extends AbstractPlugin
{
    /**
     * @var Container
     */
    protected $sessionContainer;

    /**
     * @param  Form    $form
     * @param  string  $redirect      Route or URL string (default: current route)
     * @param  boolean $redirectToUrl Use $redirect as a URL string (default: false)
     * @return boolean|array|Response
     */
    public function __invoke(Form $form, $redirect = null, $redirectToUrl = false)
    {
        $request = $this->getController()->getRequest();
        if ($request->isPost()) {
            return $this->handlePostRequest($form, $redirect, $redirectToUrl);
        } else {
            return $this->handleGetRequest($form);
        }
    }

    /**
     * @param  Form    $form
     * @param  string  $redirect      Route or URL string (default: current route)
     * @param  boolean $redirectToUrl Use $redirect as a URL string (default: false)
     * @return Response
     */
    protected function handlePostRequest(Form $form, $redirect, $redirectToUrl)
    {
        $container = $this->getSessionContainer();
        $request   = $this->getController()->getRequest();

        // Change required flag to false for any previously uploaded files
        $inputFilter   = $form->getInputFilter();
        $previousFiles = ($container->files) ?: array();
        $this->traverseInputs(
            $inputFilter,
            $previousFiles,
            function($input, $value) {
                if ($input instanceof FileInput) {
                    $input->setRequired(false);
                }
                return $value;
            }
        );

        // Run the form validations/filters and retrieve any errors
        $postFiles = $request->getFiles()->toArray();
        $postOther = $request->getPost()->toArray();
        $post      = ArrayUtils::merge($postOther, $postFiles);

        $form->setData($post);
        $isValid = $form->isValid();
        $data    = $form->getData(Form::VALUES_AS_ARRAY);
        $errors  = (!$isValid) ? $form->getMessages() : null;

        // Loop through data and merge previous files with new valid files
        $postFiles = ArrayUtils::merge(
            $previousFiles,
            $this->filterInvalidFileInputPostData($inputFilter, $data)
        );
        $post = ArrayUtils::merge($post, $postFiles);

        // Save form data in session
        $container->setExpirationHops(1, array('post', 'errors', 'isValid'));
        $container->post    = $post;
        $container->errors  = $errors;
        $container->isValid = $isValid;
        $container->files   = $postFiles;

        return $this->redirect($redirect, $redirectToUrl);
    }

    /**
     * @param  Form $form
     * @return boolean|array
     */
    protected function handleGetRequest(Form $form)
    {
        $container = $this->getSessionContainer();
        if (null === $container->post) {
            // No previous post, bail early
{'fraction_non_alphanumeric': 0.1251395262309314, 'fraction_numerical': 0.0024804663276696017, 'mean_word_length': 2.568141592920354, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5626320', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2183, 'n_words': 594}