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 16 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
56 changes: 52 additions & 4 deletions .github/workflows/ci.yml
Expand Up @@ -21,7 +21,7 @@ jobs:

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

- name: Install PHP with latest composer
uses: shivammathur/setup-php@v2
Expand All @@ -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,12 +46,60 @@ jobs:
- name: Run unit tests
run: composer test

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

strategy:
fail-fast: false
matrix:
php: ['7.2', '8.2']
with: ['idna-convert-v051', 'idna-convert-v2', 'idna-convert-v3']
exclude:
# no PHP 8.2 support in idna-convert-v2
- php: '8.2'
with: 'idna-convert-v2'

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

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

- 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 with idna_convert 0.5.1
if: ${{ matrix.with == 'idna-convert-v051' }}
run: bin/phpunit --bootstrap=tests/bootstrap-with-idna-convert-051.php
Art4 marked this conversation as resolved.
Show resolved Hide resolved

- name: Run unit tests
if: ${{ matrix.with != 'idna-convert-v051' }}
run: composer test

test-compiled:
runs-on: ubuntu-latest

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

- name: Install PHP
uses: shivammathur/setup-php@v2
Expand All @@ -62,7 +110,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
1 change: 1 addition & 0 deletions idn/idna_convert.class.php
Expand Up @@ -89,6 +89,7 @@ class idna_convert
var $_api_encoding = 'utf8'; // Default input charset is UTF-8
var $_allow_overlong = false; // Overlong UTF-8 encodings are forbidden
var $_strict_mode = false; // Behave strict or not
var $slast = 0;

// The constructor
function __construct($options = false)
Expand Down
10 changes: 2 additions & 8 deletions src/Enclosure.php
Expand Up @@ -220,8 +220,6 @@ class Enclosure
*
* For documentation on all the parameters, see the corresponding
* properties and their accessors
*
* @uses idna_convert If available, this will convert an IDN
*/
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 @@ -251,12 +249,8 @@ 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
}
$this->handler = $this->get_handler(); // Needs to load last
// Needs to load last
$this->handler = $this->get_handler();
}

/**
Expand Down
16 changes: 11 additions & 5 deletions src/File.php
Expand Up @@ -68,15 +68,21 @@ class File

public function __construct($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false, $curl_options = [])
{
if (class_exists('idna_convert')) {
$idn = new \idna_convert();
$parsed = \SimplePie\Misc::parse_url($url);
$url = \SimplePie\Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], null);
}
$this->url = $url;
$this->permanent_url = $url;
$this->useragent = $useragent;

if (preg_match('/^http(s)?:\/\//i', $url)) {
// Convert only urls, not paths
if (class_exists('idna_convert')) {
$idn = new \idna_convert();
$parsed = \SimplePie\Misc::parse_url($url);
$url = \SimplePie\Misc::compress_parse_url($parsed['scheme'], $idn->encode($parsed['authority']), $parsed['path'], $parsed['query'], null);

$this->url = $url;
$this->permanent_url = $url;
}

if ($useragent === null) {
$useragent = ini_get('user_agent');
$this->useragent = $useragent;
Expand Down
87 changes: 87 additions & 0 deletions src/Idna/IdnaConverter.php
@@ -0,0 +1,87 @@
<?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.
*
* @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;
use idna_convert;

/**
* Convert internationalized domain names using idna-convert
*
* @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 filterDomain(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 if (class_exists(idna_convert::class)) {
// Support for idna_convert:0.5.1
$idnaConvert = new idna_convert();

return $idnaConvert->decode($encoded);
Art4 marked this conversation as resolved.
Show resolved Hide resolved
} else {
// No idna-convert library is available
return $encoded;
}
}
}
60 changes: 60 additions & 0 deletions src/Idna/IdnaDomainFilter.php
@@ -0,0 +1,60 @@
<?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.
*
* @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
*/
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 filterDomain(string $encoded): string;
Copy link
Contributor

Choose a reason for hiding this comment

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

filterDomain does not really say anything about what is being done.

Copy link
Contributor Author

@Art4 Art4 Feb 1, 2023

Choose a reason for hiding this comment

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

Naming things is hard. 🙂 Any suggestions?

}