Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace idna_convert.class.php with Algo26\IdnaConvert, allow custom idna library #776

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Expand Up @@ -72,6 +72,11 @@ jobs:
coverage: none
tools: none

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
- name: "Install Composer dependencies"
uses: "ramsey/composer-install@v2"

- name: "Install idna-convert v3"
if: ${{ matrix.with == 'idna-convert-v3' }}
run: "composer require --dev algo26-matthias/idna-convert:^3"
Expand All @@ -80,11 +85,6 @@ jobs:
if: ${{ matrix.with == 'idna-convert-v2' }}
run: "composer require --dev algo26-matthias/idna-convert:^2"

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
- name: "Install Composer dependencies"
uses: "ramsey/composer-install@v2"

- name: Run unit tests
run: composer test

Expand Down
35 changes: 29 additions & 6 deletions src/Enclosure.php
Expand Up @@ -45,6 +45,8 @@

namespace SimplePie;

use SimplePie\Idna\IdnaDomainFilter;

/**
* Handles everything related to enclosures (including Media RSS and iTunes RSS)
*
Expand All @@ -55,7 +57,7 @@
* @package SimplePie
* @subpackage API
*/
class Enclosure
class Enclosure implements RegistryAware
{
/**
* @var string
Expand Down Expand Up @@ -226,6 +228,7 @@ class Enclosure
* properties and their accessors
*
* @uses idna_convert If available, this will convert an IDN
* @see https://github.com/algo26-matthias/idna-convert
*/
public function __construct($link = null, $type = null, $length = null, $javascript = null, $bitrate = null, $captions = null, $categories = null, $channels = null, $copyright = null, $credits = null, $description = null, $duration = null, $expression = null, $framerate = null, $hashes = null, $height = null, $keywords = null, $lang = null, $medium = null, $player = null, $ratings = null, $restrictions = null, $samplingrate = null, $thumbnails = null, $title = null, $width = null)
{
Expand Down Expand Up @@ -255,12 +258,32 @@ public function __construct($link = null, $type = null, $length = null, $javascr
$this->type = $type;
$this->width = $width;

if (class_exists('idna_convert')) {
$idn = new \idna_convert();
$parsed = \SimplePie\Misc::parse_url($link);
$this->link = \SimplePie\Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], $parsed['fragment']);
Art4 marked this conversation as resolved.
Show resolved Hide resolved
// Needs to load last
$this->handler = $this->get_handler();
}

/**
* Set the Registry into the class
*
* @param Registry $registry
*
* @return void
*/
public function set_registry(Registry $registry): void
{
$idnaConverter = $registry->create(IdnaDomainFilter::class);
$parsed = $registry->call(Misc::class, 'parse_url', [$this->link]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why replace a simple pure function like this with indirection via registry? It is unlikely that we would want to swap it and it just makes the code more complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are right, but this is the only possibility to mock the Misc class for tests.

We should discuss the whole concept of the Registry in a separate issue and how we should refactore it.

$authority = $idnaConverter->filter($parsed['authority']);

if ($authority !== $parsed['authority']) {
$this->link = $registry->call(Misc::class, 'compress_parse_url', [
$parsed['scheme'],
$authority,
$parsed['path'],
$parsed['query'],
$parsed['fragment'],
]);
}
$this->handler = $this->get_handler(); // Needs to load last
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Registry.php
Expand Up @@ -46,6 +46,8 @@
namespace SimplePie;

use SimplePie\Content\Type\Sniffer;
use SimplePie\Idna\IdnaConverter;
use SimplePie\Idna\IdnaDomainFilter;
use SimplePie\Parse\Date;
use SimplePie\XML\Declaration\Parser as DeclarationParser;

Expand Down Expand Up @@ -85,6 +87,7 @@ class Registry
Misc::class => Misc::class,
DeclarationParser::class => DeclarationParser::class,
Date::class => Date::class,
IdnaDomainFilter::class => IdnaConverter::class,
];

/**
Expand Down