<commit_before><?php
namespace ADmad\Glide\Middleware;

use ADmad\Glide\Responses\PsrResponseFactory;
use Cake\Core\InstanceConfigTrait;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Http\Response;
use Cake\Utility\Security;
use Exception;
use League\Glide\Server;
use League\Glide\ServerFactory;
use League\Glide\Signatures\SignatureFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class GlideMiddleware implements EventDispatcherInterface
{
    use EventDispatcherTrait;
    use InstanceConfigTrait;

    const EXCEPTION_EVENT = 'Glide.exception';

    /**
     * Default config.
     *
     * @var array
     */
    protected $_defaultConfig = [
        'when' => null,
        'scope' => null,
        'cacheTime' => '+1 days',
        'server' => [
            'base_url' => '/images/',
            'response' => null,
        ],
        'security' => [
            'secureUrls' => false,
            'signKey' => null,
        ],
        'headers' => null,
    ];

    /**
     * Image path.
     *
     * @var string
     */
    protected $_path = '';

    /**
     * Glide params for generating thumbnails.
     *
     * @var array
     */
    protected $_params = [];

    /**
     * Constructor.
     *
     * @param array $config Array of config.
     */
    public function __construct($config = [])
    {
        $this->config($config);

        if ($this->config('scope') === null) {
            $server = $this->config('server');
            if (is_array($server)) {
                $this->config('scope', $server['base_url']);
            } else {
                $this->config('scope', $server->getBaseUrl());
            }
        }
    }

    /**
     * Return response with image data.
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request The request.
     * @param \Psr\Http\Message\ResponseInterface $response The response.
     * @param callable $next Callback to invoke the next middleware.
     *
     * @return \Psr\Http\Message\ResponseInterface A response
     */
    public function __invoke($request, $response, $next)
    {
        $uri = $request->getUri();
        $this->_path = urldecode($uri->getPath());
        parse_str($uri->getQuery(), $this->_params);

        $config = $this->config();

        if (strpos($this->_path, $config['scope']) !== 0) {
            return $next($request, $response);
        }

        $return = $this->_checkSignature();
        if ($return !== null) {
            return $response;
        }

        $server = $config['server'];
        if (is_array($server)) {
            $server = ServerFactory::create($server);
        }

        $modifiedTime = null;
        if ($config['cacheTime']) {
            try {
                $modifiedTime = $server->getSource()
                    ->getTimestamp($server->getSourcePath($this->_path));
            } catch (Exception $exception) {
                return $this->_handleException($request, $response, $exception);
            }
<commit_msg>Remove "scope" option.<commit_after><?php
namespace ADmad\Glide\Middleware;

use ADmad\Glide\Responses\PsrResponseFactory;
use Cake\Core\InstanceConfigTrait;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Http\Response;
use Cake\Utility\Security;
use Exception;
use League\Glide\Server;
use League\Glide\ServerFactory;
use League\Glide\Signatures\SignatureFactory;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class GlideMiddleware implements EventDispatcherInterface
{
    use EventDispatcherTrait;
    use InstanceConfigTrait;

    const EXCEPTION_EVENT = 'Glide.exception';

    /**
     * Default config.
     *
     * @var array
     */
    protected $_defaultConfig = [
        'cacheTime' => '+1 days',
        'server' => [
            'base_url' => '/images/',
            'response' => null,
        ],
        'security' => [
            'secureUrls' => false,
            'signKey' => null,
        ],
        'headers' => null,
    ];

    /**
     * Image path.
     *
     * @var string
     */
    protected $_path = '';

    /**
     * Glide params for generating thumbnails.
     *
     * @var array
     */
    protected $_params = [];

    /**
     * Constructor.
     *
     * @param array $config Array of config.
     */
    public function __construct($config = [])
    {
        $this->config($config);
    }

    /**
     * Return response with image data.
     *
     * @param \Psr\Http\Message\ServerRequestInterface $request The request.
     * @param \Psr\Http\Message\ResponseInterface $response The response.
     * @param callable $next Callback to invoke the next middleware.
     *
     * @return \Psr\Http\Message\ResponseInterface A response
     */
    public function __invoke($request, $response, $next)
    {
        $uri = $request->getUri();
        $this->_path = urldecode($uri->getPath());
        parse_str($uri->getQuery(), $this->_params);

        $config = $this->config();

        $return = $this->_checkSignature();
        if ($return !== null) {
            return $response;
        }

        $server = $config['server'];
        if (is_array($server)) {
            $server = ServerFactory::create($server);
        }

        $modifiedTime = null;
        if ($config['cacheTime']) {
            try {
                $modifiedTime = $server->getSource()
                    ->getTimestamp($server->getSourcePath($this->_path));
            } catch (Exception $exception) {
                return $this->_handleException($request, $response, $exception);
            }

{'fraction_non_alphanumeric': 0.140693784116922, 'fraction_numerical': 0.0005282620179609086, 'mean_word_length': 2.1277533039647576, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '6182316', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1609, 'n_words': 333}