<commit_before><?php

/**
 * Minds Media Proxy
 *
 * @author emi
 */

namespace Minds\Controllers\api\v2\media;

use Minds\Core\Di\Di;
use Minds\Core\Media\Proxy\Download;
use Minds\Core\Media\Proxy\Resize;
use Minds\Interfaces;

class proxy implements Interfaces\Api, Interfaces\ApiIgnorePam
{
    const MAX_TIME = 5;

    /**
     * Equivalent to HTTP GET method
     * @param  array $pages
     * @return mixed|null
     */
    public function get($pages)
    {
        $src = isset($_GET['src']) ? $_GET['src'] : null;

        // thumbProxy polyfill
        $width = isset($_GET['size']) ? (int) $_GET['size'] : null;

        if ($width && is_numeric($width)) {
            $size = $width;
        } else {
            $size = isset($_GET['size']) ? (int) $_GET['size'] : 1024;
        }

        if ($src && strpos($src, '//') === 0) {
            $src = 'https:' . $src;
        }

        if ($size < 0) {
            exit;
        }

        try {
            set_time_limit(static::MAX_TIME + 1);
            ini_set('max_execution_time', static::MAX_TIME + 1);

            /** @var Download $downloader */
            $downloader = Di::_()->get('Media\Proxy\Download');

            /** @var Resize $resizer */
            $resizer = Di::_()->get('Media\Proxy\Resize');

            $original = $downloader
                ->setSrc($src)
                ->setTimeout(static::MAX_TIME)
                ->download();
<commit_msg>Fix infinite looping media proxy<commit_after><?php

/**
 * Minds Media Proxy
 *
 * @author emi
 */

namespace Minds\Controllers\api\v2\media;

use Minds\Core\Di\Di;
use Minds\Core\Media\Proxy\Download;
use Minds\Core\Media\Proxy\Resize;
use Minds\Interfaces;

class proxy implements Interfaces\Api, Interfaces\ApiIgnorePam
{
    const MAX_TIME = 5;

    /**
     * Equivalent to HTTP GET method
     * @param  array $pages
     * @return mixed|null
     */
    public function get($pages)
    {
        if (!isset($_GET['src'])) {
            exit;
        }

        $src = $_GET['src'];
        if (strpos($src, '//') === 0) {
            $src = 'https:' . $src;
        }

        $size = isset($_GET['size']) ? (int) $_GET['size'] : 1024;
        if ($size < 0) {
            exit;
        }

        // exit if src was a media proxy url
        $siteUrl = Di::_()->get('Config')->get('site_url');
        $cdnUrl = Di::_()->get('Config')->get('cdn_url');
        $mediaUrlRoute = "api/v2/media/proxy";

        if ($siteUrl && strpos($src, $siteUrl . $mediaUrlRoute) === 0) {
            exit;
        } elseif ($cdnUrl && strpos($src, $cdnUrl . $mediaUrlRoute) === 0) {
            exit;
        }

        try {
            set_time_limit(static::MAX_TIME + 1);
            ini_set('max_execution_time', static::MAX_TIME + 1);

            /** @var Download $downloader */
            $downloader = Di::_()->get('Media\Proxy\Download');

            /** @var Resize $resizer */
            $resizer = Di::_()->get('Media\Proxy\Resize');

            $original = $downloader
                ->setSrc($src)
                ->setTimeout(static::MAX_TIME)
                ->download();

{'fraction_non_alphanumeric': 0.16996805111821087, 'fraction_numerical': 0.007348242811501597, 'mean_word_length': 1.8777573529411764, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4103247', 'n_tokens_mistral': 1017, 'n_tokens_neox': 983, 'n_words': 201}