<commit_before><?php

/**
 * FileUpload.php - This class implements file upload with Ajax.
 *
 * @package jaxon-core
 * @author <NAME> <<EMAIL>>
 * @copyright 2017 <NAME> <<EMAIL>>
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
 * @link https://github.com/jaxon-php/jaxon-core
 */

namespace Jaxon\Request\Plugin;

use Jaxon\Jaxon;
use Jaxon\Plugin\Request as RequestPlugin;
use Jaxon\Request\Support\UploadedFile;
use Jaxon\Response\UploadResponse;

use Exception;
use Closure;

class FileUpload extends RequestPlugin
{
    use \Jaxon\Features\Config;
    use \Jaxon\Features\Validator;
    use \Jaxon\Features\Translator;

    /**
     * The uploaded files copied in the user dir
     *
     * @var array
     */
    protected $aUserFiles = [];

    /**
     * The name of file containing upload data
     *
     * @var string
     */
    protected $sTempFile = '';

    /**
     * The subdir where uploaded files are stored
     *
     * @var string
     */
    protected $sUploadSubdir = '';

    /**
     * A user defined function to transform uploaded file names
     *
     * @var Closure
     */
    protected $cFileFilter = null;

    /**
     * Is the current request an HTTP upload
     *
     * @var boolean
     */
    protected $bRequestIsHttpUpload = false;

    /**
     * Read uploaded files info from the $_FILES global var
     */
    public function __construct()
    {
        $this->sUploadSubdir = uniqid() . DIRECTORY_SEPARATOR;

        if(array_key_exists('jxnupl', $_POST))
        {
            $this->sTempFile = $_POST['jxnupl'];
        }
        elseif(array_key_exists('jxnupl', $_GET))
        {
            $this->sTempFile = $_GET['jxnupl'];
        }
    }

    /**
     * Filter uploaded file name
     *
     * @param Closure       $cFileFilter            The closure which filters filenames
     *
     * @return void
     */
    public function setFileFilter(Closure $cFileFilter)
    {
        $this->cFileFilter = $cFileFilter;
    }

    /**
     * Filter uploaded file name
     *
     * @param string        $sFilename              The filename
     * @param string        $sVarName               The associated variable name
     *
     * @return string
     */
    protected function filterFilename($sFilename, $sVarName)
    {
        if(($this->cFileFilter))
        {
            $cFileFilter = $this->cFileFilter;
            $sFilename = (string)$cFileFilter($sFilename, $sVarName);
        }
        return $sFilename;
    }

    /**
     * Get the path to the upload dir
     *
     * @param string        $sFieldId               The filename
     *
     * @return string
     */
    protected function getUploadDir($sFieldId)
    {
        // Default upload dir
        $sDefaultUploadDir = $this->getOption('upload.default.dir');
        $sUploadDir = $this->getOption('upload.files.' . $sFieldId . '.dir', $sDefaultUploadDir);
        $sUploadDir = rtrim(trim($sUploadDir), '/\\') . DIRECTORY_SEPARATOR;
        // Verify that the upload dir exists and is writable
        if(!is_writable($sUploadDir))
        {
            throw new \Jaxon\Exception\Error($this->trans('errors.upload.access'));
        }
        $sUploadDir .= $this->sUploadSubdir;
        if(!file_exists($sUploadDir) && !@mkdir($sUploadDir))
        {
            throw new \Jaxon\Exception\Error($this->trans('errors.upload.access'));
        }
        return $sUploadDir;
    }

    /**
     * Get the path to the upload temp dir
     *
     * @return string
     */
    protected function getUploadTempDir()
    {
        // Default upload dir
        $sUploadDir = $this->getOption('upload.default.dir');
        $sUploadDir = rtrim(trim($sUploadDir), '/\\') . DIRECTORY_SEPARATOR;
        // Verify that the upload dir exists and is writable
        if(!is_writable($sUploadDir))
        {
            throw new \Jaxon\Exception\Error($this->trans('errors.upload.access'));
        }
        $sUploadDir .= 'tmp' . DIRECTORY_SEPARATOR;
        if(!file_exists($sUploadDir) && !@mkdir($sUploadDir))
        {
            throw new \Jaxon\Exception\Error($this->trans('errors.upload.access'));
        }
        return $sUploadDir;
    }

    /**
     * Get the path to the upload temp file
     *
     * @return string
     */
    protected function getUploadTempFile()
    {
        $sUploadDir = $this->getOption('upload.default.dir');
        $sUploadDir = rtrim(trim($sUploadDir), '/\\') . DIRECTORY_SEPARATOR;
        $sUploadDir .= 'tmp' . DIRECTORY_SEPARATOR;
        $sUploadTempFile = $sUploadDir . $this->sTempFile . '.json';
        if(!is_readable($sUploadTempFile))
        {
            throw new \Jaxon\Exception\Error($this->trans('errors.upload.access'));
        }
        return $sUploadTempFile;
    }

    /**
     * Read uploaded files info from HTTP request data
     *
     * @return void
     */
    protected function readFromHttpData()
    {
        // Check validity of the uploaded files
        $aTempFiles = [];<commit_msg>Fix in file ipload.<commit_after><?php

/**
 * FileUpload.php - This class implements file upload with Ajax.
 *
 * @package jaxon-core
 * @author <NAME> <<EMAIL>>
 * @copyright 2017 <NAME> <<EMAIL>>
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
 * @link https://github.com/jaxon-php/jaxon-core
 */

namespace Jaxon\Request\Plugin;

use Jaxon\Jaxon;
use Jaxon\Plugin\Request as RequestPlugin;
use Jaxon\Request\Support\UploadedFile;
use Jaxon\Response\UploadResponse;

use Exception;
use Closure;

class FileUpload extends RequestPlugin
{
    use \Jaxon\Features\Config;
    use \Jaxon\Features\Validator;
    use \Jaxon\Features\Translator;

    /**
     * The uploaded files copied in the user dir
     *
     * @var array
     */
    protected $aUserFiles = [];

    /**
     * The name of file containing upload data
     *
     * @var string
     */
    protected $sTempFile = '';

    /**
     * The subdir where uploaded files are stored
     *
     * @var string
     */
    protected $sUploadSubdir = '';

    /**
     * A user defined function to transform uploaded file names
     *
     * @var Closure
     */
    protected $cFileFilter = null;

    /**
     * Is the current request an HTTP upload
     *
     * @var boolean
     */
    protected $bRequestIsHttpUpload = false;

    /**
     * Read uploaded files info from the $_FILES global var
     */
    public function __construct()
    {
        $this->sUploadSubdir = uniqid() . DIRECTORY_SEPARATOR;

        if(array_key_exists('jxnupl', $_POST))
        {
            $this->sTempFile = $_POST['jxnupl'];
        }
        elseif(array_key_exists('jxnupl', $_GET))
        {
            $this->sTempFile = $_GET['jxnupl'];
        }
    }

    /**
     * Filter uploaded file name
     *
     * @param Closure       $cFileFilter            The closure which filters filenames
     *
     * @return void
     */
    public function setFileFilter(Closure $cFileFilter)
    {
        $this->cFileFilter = $cFileFilter;
    }

    /**
     * Filter uploaded file name
     *
     * @param string        $sFilename              The filename
     * @param string        $sVarName               The associated variable name
     *
     * @return string
     */
    protected function filterFilename($sFilename, $sVarName)
    {
        if(($this->cFileFilter))
        {
            $cFileFilter = $this->cFileFilter;
            $sFilename = (string)$cFileFilter($sFilename, $sVarName);
        }
        return $sFilename;
    }

    /**
     * Verify that the upload dir exists and is writable
     *
     * @param string        $sUploadDir             The filename
     * @param string        $sUploadSubDir          The filename
     *
     * @return string
     */
    private function _makeUploadDir($sUploadDir, $sUploadSubDir)
    {
        $sUploadDir = rtrim(trim($sUploadDir), '/\\') . DIRECTORY_SEPARATOR;
        // Verify that the upload dir exists and is writable
        if(!is_writable($sUploadDir))
        {
            throw new \Jaxon\Exception\Error($this->trans('errors.upload.access'));
        }
        $sUploadDir .= $sUploadSubDir;
        if(!file_exists($sUploadDir) && !@mkdir($sUploadDir))
        {
            throw new \Jaxon\Exception\Error($this->trans('errors.upload.access'));
        }
        return $sUploadDir;
    }

    /**
     * Get the path to the upload dir
     *
     * @param string        $sFieldId               The filename
     *
     * @return string
     */
    protected function getUploadDir($sFieldId)
    {
        // Default upload dir
        $sDefaultUploadDir = $this->getOption('upload.default.dir');
        $sUploadDir = $this->getOption('upload.files.' . $sFieldId . '.dir', $sDefaultUploadDir);

        return $this->_makeUploadDir($sUploadDir, $this->sUploadSubdir);
    }

    /**
     * Get the path to the upload temp dir
     *
     * @return string
     */
    protected function getUploadTempDir()
    {
        // Default upload dir
        $sUploadDir = $this->getOption('upload.default.dir');

        return $this->_makeUploadDir($sUploadDir, 'tmp' . DIRECTORY_SEPARATOR);
    }

    /**
     * Get the path to the upload temp file
     *
     * @return string
     */
    protected function getUploadTempFile()
    {
        $sUploadDir = $this->getOption('upload.default.dir');
        $sUploadDir = rtrim(trim($sUploadDir), '/\\') . DIRECTORY_SEPARATOR;
        $sUploadDir .= 'tmp' . DIRECTORY_SEPARATOR;
        $sUploadTempFile = $sUploadDir . $this->sTempFile . '.json';
        if(!is_readable($sUploadTempFile))
        {
            throw new \Jaxon\Exception\Error($this->trans('errors.upload.access'));
        }
        return $sUploadTempFile;
    }

    /**
     * Read uploaded files info from HTTP request data
     *
     * @return void
     */
    protected function readFromHttpData()
    {
        // Check validity of the uploaded files
        $aTempFiles = [];
{'fraction_non_alphanumeric': 0.12169471153846154, 'fraction_numerical': 0.001201923076923077, 'mean_word_length': 2.0395738203957383, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 49, 'https://': 4, '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': '3810488', 'n_tokens_mistral': 3032, 'n_tokens_neox': 2928, 'n_words': 671}