Replaces links to twitter.com an "x.com" to a nitter server #1461

Closed
loma-one wants to merge 3 commits from (deleted):develop into develop
2 changed files with 13 additions and 7 deletions

View file

@ -21,6 +21,8 @@ Excerp from nitters about page.
Changelog Changelog
--------- ---------
* **Version 2.1**
* Extended by the domain x.com
* **Version 2.0** * **Version 2.0**
* Changes the used hook by the addon, so that attached previews of postings get replaced as well. * Changes the used hook by the addon, so that attached previews of postings get replaced as well.
This means the admins need to reload the addon This means the admins need to reload the addon

View file

@ -1,8 +1,8 @@
<?php <?php
/* /*
* Name: nitter * Name: nitter
* Description: Replaces links to twitter.com to a nitter server in all displays of postings on a node. * Description: Replaces links to twitter.com and "x.com" to a nitter server in all displays of postings on a node.

*and

*and
* Version: 2.0 * Version: 2.1
* Author: Tobias Diekershoff <tobias@social.diekershoff.de> * Author: Tobias Diekershoff <tobias@social.diekershoff.de>
* *
* Copyright (c) 2020 Tobias Diekershoff * Copyright (c) 2020 Tobias Diekershoff
@ -55,19 +55,23 @@ function nitter_addon_admin(string &$o)
} }
/* /*
* replace "twitter.com" with "nitter.net" * replace "twitter.com" and "x.com" with "nitter.net"

*and

*and
*/ */
function nitter_render(array &$b) function nitter_render(array &$b)
{ {
// this needs to be a system setting // this needs to be a system setting
$replaced = false; $replaced = false;
$nitter = DI::config()->get('nitter', 'server', 'https://nitter.net'); $nitter = DI::config()->get('nitter', 'server', 'https://nitter.net');
if (strstr($b['html'], 'https://mobile.twitter.com')) { if (strstr($b['html'], 'https://mobile.twitter.com/')) {
Review

It seems as if the nitter server url is stored without a trailing /. So replacing the url including a trailing / with the nitter url without a trailing / will not work.

It seems as if the nitter server url is stored without a trailing `/`. So replacing the url including a trailing `/` with the nitter url without a trailing `/` will not work.
$b['html'] = str_replace('https://mobile.twitter.com', $nitter, $b['html']); $b['html'] = str_replace('https://mobile.twitter.com/', $nitter, $b['html']);
$replaced = true; $replaced = true;
} }
if (strstr($b['html'], 'https://twitter.com')) { if (strstr($b['html'], 'https://twitter.com/')) {
$b['html'] = str_replace('https://twitter.com', $nitter, $b['html']); $b['html'] = str_replace('https://twitter.com/', $nitter, $b['html']);
$replaced = true;
}
if (strstr($b['html'], 'https://x.com/')) {

Wrong indentation, should be one fewer tabulation.

Also matching on just x.com exposes partial matches on different domains like x.commercial.biz. We'd be better off matching on https://x.com/ with the trailing slash.

Same goes for the https://twitter.com/ match.

Wrong indentation, should be one fewer tabulation. Also matching on just `x.com` exposes partial matches on different domains like `x.commercial.biz`. We'd be better off matching on `https://x.com/` with the trailing slash. Same goes for the `https://twitter.com/` match.
$b['html'] = str_replace('https://x.com/', $nitter, $b['html']);
$replaced = true; $replaced = true;
} }
if ($replaced) { if ($replaced) {