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 5 commits
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
46 changes: 44 additions & 2 deletions .github/workflows/ci.yml
Expand Up @@ -32,7 +32,7 @@ jobs:
tools: none

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
- name: "Install Composer dependencies (PHP < 8.3)"
if: ${{ matrix.php < '8.3' }}
uses: "ramsey/composer-install@v2"
Expand All @@ -46,6 +46,48 @@ jobs:
- name: Run unit tests
run: composer test

test-with-libraries:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
php: ['8.2']
with: ['idna-convert-v3']
include:
- php: '7.2'
with: 'idna-convert-v2'

name: "PHP: ${{ matrix.php }} with ${{ matrix.with }}"

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Install PHP with latest composer
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
ini-values: error_reporting=-1, display_errors=On, log_errors_max_len=0
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"

- name: "Install idna-convert v2"
if: ${{ matrix.with == 'idna-convert-v2' }}
run: "composer require --dev algo26-matthias/idna-convert:^2"

- name: Run unit tests
run: composer test

test-compiled:
runs-on: ubuntu-latest

Expand All @@ -62,7 +104,7 @@ jobs:
tools: none

# Install dependencies and handle caching in one go.
# @link https://github.com/marketplace/actions/install-composer-dependencies
# @link https://github.com/marketplace/actions/install-php-dependencies-with-composer
- name: "Install Composer dependencies"
uses: "ramsey/composer-install@v2"
with:
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -39,6 +39,7 @@
"ext-iconv": "",
"ext-intl": "",
"ext-mbstring": "",
"algo26-matthias/idna-convert": "IdnaConvert allows you to convert internationalized domain names",
"mf2/mf2": "Microformat module that allows for parsing HTML for microformats"
},
"autoload": {
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
84 changes: 84 additions & 0 deletions src/Idna/IdnaConverter.php
@@ -0,0 +1,84 @@
<?php
/**
* SimplePie
*
* A PHP-Based RSS and Atom Feed Framework.
* Takes the hard work out of managing a complete RSS/Atom solution.
*
* Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* * Neither the name of the SimplePie Team nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
* AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package SimplePie
* @copyright 2004-2022 Ryan Parman, Sam Sneddon, Ryan McCue
* @author Ryan Parman
* @author Sam Sneddon
* @author Ryan McCue
* @link http://simplepie.org/ SimplePie
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/

namespace SimplePie\Idna;

use Algo26\IdnaConvert\IdnaConvert;
use Algo26\IdnaConvert\ToUnicode;

/**
* Convert internationalized domain names using idna-convert
*
* @package SimplePie
* @subpackage Idna
* @internal
*/
final class IdnaConverter implements IdnaDomainFilter
{
/**
* Convert an internationalized domain name.
*
* e.g. from `xn--mller-kva` to `müller`
*
* @param string $encoded The encoded domain name
*
* @return string the decoded domain name
*/
public function filter(string $encoded): string
{
if (class_exists(ToUnicode::class)) {
// Support for algo26-matthias/idna-convert:^3
$idnaConvert = new ToUnicode();

return $idnaConvert->convert($encoded);
} else if (class_exists(IdnaConvert::class)) {
// Support for algo26-matthias/idna-convert:^2
$idnaConvert = new IdnaConvert();

return $idnaConvert->decode($encoded);
} else {
// No idna-convert library is available
return $encoded;
}
}
}
64 changes: 64 additions & 0 deletions src/Idna/IdnaDomainFilter.php
@@ -0,0 +1,64 @@
<?php
/**
* SimplePie
*
* A PHP-Based RSS and Atom Feed Framework.
* Takes the hard work out of managing a complete RSS/Atom solution.
*
* Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* * Neither the name of the SimplePie Team nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
* AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package SimplePie
* @copyright 2004-2022 Ryan Parman, Sam Sneddon, Ryan McCue
* @author Ryan Parman
* @author Sam Sneddon
* @author Ryan McCue
* @link http://simplepie.org/ SimplePie
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/

namespace SimplePie\Idna;

/**
* Interface for convert internationalized domain names
*
* @package SimplePie
* @subpackage Idna
*/
interface IdnaDomainFilter
{
/**
* Convert an internationalized domain name.
*
* e.g. from `xn--mller-kva` to `müller`
*
* @param string $encoded The encoded domain name
*
* @return string the decoded domain name
*/
public function filter(string $encoded): string;
}
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
82 changes: 82 additions & 0 deletions tests/Integration/Idna/IdnaConverterTest.php
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);
/**
* SimplePie
*
* A PHP-Based RSS and Atom Feed Framework.
* Takes the hard work out of managing a complete RSS/Atom solution.
*
* Copyright (c) 2004-2022, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* * Neither the name of the SimplePie Team nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
* AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @package SimplePie
* @copyright 2004-2022 Ryan Parman, Sam Sneddon, Ryan McCue
* @author Ryan Parman
* @author Sam Sneddon
* @author Ryan McCue
* @link http://simplepie.org/ SimplePie
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/

namespace SimplePie\Tests\Integration\Idna;

use Algo26\IdnaConvert\IdnaConvert;
use Algo26\IdnaConvert\ToUnicode;
use PHPUnit\Framework\TestCase;
use SimplePie\Idna\IdnaConverter;

class IdnaConverterTest extends TestCase
{
/**
* @dataProvider getIdnaData
*/
public function testIdnaConverterReturnsCorrectData(string $encoded, string $expected): void
{
$idnaConverter = new IdnaConverter();

$this->assertSame($expected, $idnaConverter->filter($encoded));
}

public function getIdnaData(): array
{
if (!class_exists(ToUnicode::class) && !class_exists(IdnaConvert::class)) {
// No idna-convert library is available
return [
['', ''],
];
}

return [
['', ''],
['xn--mller-kva', 'müller'],
['xn--weienbach-i1a', 'weißenbach'],
['xn----9mcj9fole', 'يوم-جيد'],
['xn----2hckbod3a', 'יום-טוב'],
];
}
}