<commit_before><?php

namespace Bolt\Filesystem;

use Bolt\Library as Lib;
use Silex\Application;

/**
 * Use to check if an access to a file is allowed.
 *
 * @author <NAME> <<EMAIL>>
 */
class FilePermissions
{
    /**
     * @var \Bolt\Application
     */
    protected $app;

    /**
     * List of Filesystem prefixes that are editable.
     *
     * @var string[]
     */
    protected $allowedPrefixes = array();

    /**
     * Regex list represented editable resources.
     *
     * @var array
     */
    protected $allowed = array();

    /**
     * Regex list represented resources forbidden for edition.
     *
     * @var array
     */
    protected $blocked = array();

    /**
     * Maximum upload size allowed by PHP, in bytes
     *
     * @var int
     */
    protected $maxUploadSize;

    /**
     * Constructor, initialize filters rules.
     *
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;

        $this->allowedPrefixes = array(
            'config',
            'files',
            'theme',
        );

        $this->blocked = array(
            '#.php$#',
            '#\.htaccess#',
            '#\.htpasswd#'
        );
    }

    /**
     * Check if you can do something with the given file or directory.
     *
     * @param string $prefix
     * @param string $path
     *
     * @return bool
     */
    public function authorized($prefix, $path)
    {
        // Check blocked resources
        foreach ($this->blocked as $rule) {
            if (preg_match($rule, $path)) {
                return false;
            }
        }

        // Check allowed filesystems
        foreach ($this->allowedPrefixes as $allowedPrefix) {
            if ($allowedPrefix === $prefix) {
                return true;
            }
        }

        // Check allowed resources
        foreach ($this->allowed as $rule) {
            if (preg_match($rule, $path)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Checks if a given file is acceptable for upload.
     *
     * @param string $originalFilename
     *
     * @return bool
     */
    public function allowedUpload($originalFilename)
    {
        // no UNIX-hidden files
        if ($originalFilename[0] === '.') {
            return false;
        }
        // only whitelisted extensions
        $extension = strtolower(Lib::getExtension($originalFilename));
        $allowedExtensions = $this->getAllowedUploadExtensions();

        return (in_array($extension, $allowedExtensions));
    }

    public function getAllowedUploadExtensions()
    {
        return $this->app['config']->get('general/accept_file_types');
    }

    public function getMaxUploadSize()
    {
        if (!isset($this->maxUploadSize)) {
            $size = Lib::filesizeToBytes(ini_get('post_max_size'));

            $uploadMax = Lib::filesizeToBytes(ini_get('upload_max_filesize'));
            if (($uploadMax > 0) && ($uploadMax < $size)) {
                $size = $uploadMax;
            }

            $this->maxUploadSize = $size;
        }

        return $size;
    }
}<commit_msg>Allow a nice, formatted string for max upload size<commit_after><?php

namespace Bolt\Filesystem;

use Bolt\Library as Lib;
use Silex\Application;

/**
 * Use to check if an access to a file is allowed.
 *
 * @author <NAME> <<EMAIL>>
 */
class FilePermissions
{
    /**
     * @var \Bolt\Application
     */
    protected $app;

    /**
     * List of Filesystem prefixes that are editable.
     *
     * @var string[]
     */
    protected $allowedPrefixes = array();

    /**
     * Regex list represented editable resources.
     *
     * @var array
     */
    protected $allowed = array();

    /**
     * Regex list represented resources forbidden for edition.
     *
     * @var array
     */
    protected $blocked = array();

    /**
     * Maximum upload size allowed by PHP, in bytes
     *
     * @var int
     */
    protected $maxUploadSize;

    /**
     * Constructor, initialize filters rules.
     *
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        $this->app = $app;

        $this->allowedPrefixes = array(
            'config',
            'files',
            'theme',
        );

        $this->blocked = array(
            '#.php$#',
            '#\.htaccess#',
            '#\.htpasswd#'
        );
    }

    /**
     * Check if you can do something with the given file or directory.
     *
     * @param string $prefix
     * @param string $path
     *
     * @return bool
     */
    public function authorized($prefix, $path)
    {
        // Check blocked resources
        foreach ($this->blocked as $rule) {
            if (preg_match($rule, $path)) {
                return false;
            }
        }

        // Check allowed filesystems
        foreach ($this->allowedPrefixes as $allowedPrefix) {
            if ($allowedPrefix === $prefix) {
                return true;
            }
        }

        // Check allowed resources
        foreach ($this->allowed as $rule) {
            if (preg_match($rule, $path)) {
                return true;
            }
        }

        return false;
    }

    /**
     * Checks if a given file is acceptable for upload.
     *
     * @param string $originalFilename
     *
     * @return bool
     */
    public function allowedUpload($originalFilename)
    {
        // no UNIX-hidden files
        if ($originalFilename[0] === '.') {
            return false;
        }
        // only whitelisted extensions
        $extension = strtolower(Lib::getExtension($originalFilename));
        $allowedExtensions = $this->getAllowedUploadExtensions();

        return (in_array($extension, $allowedExtensions));
    }

    public function getAllowedUploadExtensions()
    {
        return $this->app['config']->get('general/accept_file_types');
    }

    public function getMaxUploadSize()
    {
        if (!isset($this->maxUploadSize)) {
            $size = Lib::filesizeToBytes(ini_get('post_max_size'));

            $uploadMax = Lib::filesizeToBytes(ini_get('upload_max_filesize'));
            if (($uploadMax > 0) && ($uploadMax < $size)) {
                $size = $uploadMax;
            }

            $this->maxUploadSize = $size;
        }

        return $this->maxUploadSize;
    }

    public function getMaxUploadSizeNice()
    {
        return Lib::formatFilesize($this->getMaxUploadSize());
    }
}
{'fraction_non_alphanumeric': 0.12379495026778883, 'fraction_numerical': 0.000612088752869166, 'mean_word_length': 1.724468528553564, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 35, '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': '5917866', 'n_tokens_mistral': 1847, 'n_tokens_neox': 1800, 'n_words': 451}