Skip to content

Commit

Permalink
[Core] Better link encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinDev committed Feb 9, 2022
1 parent 470e970 commit a1a71b9
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/contribute.xml
Expand Up @@ -19,7 +19,7 @@
<title><![CDATA[Roadmap]]></title>
<link>https://pushword.piedweb.com/roadmap</link>
<guid>https://pushword.piedweb.com/roadmap</guid>
<pubDate>Fri, 04 Feb 2022 09:55:17 +0100</pubDate>
<pubDate>Wed, 09 Feb 2022 09:36:24 +0100</pubDate>
</item>

</channel>
Expand Down
2 changes: 1 addition & 1 deletion docs/feed.xml
Expand Up @@ -13,7 +13,7 @@
<title><![CDATA[Roadmap]]></title>
<link>https://pushword.piedweb.com/roadmap</link>
<guid>https://pushword.piedweb.com/roadmap</guid>
<pubDate>Fri, 04 Feb 2022 09:55:17 +0100</pubDate>
<pubDate>Wed, 09 Feb 2022 09:36:24 +0100</pubDate>
</item>
<item>
<title><![CDATA[Upgrade Guide]]></title>
Expand Down
2 changes: 1 addition & 1 deletion docs/roadmap.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/search.json

Large diffs are not rendered by default.

Expand Up @@ -2,12 +2,21 @@

namespace Pushword\Core\Component\EntityFilter\Filter;

use Exception;

final class HtmlEncryptedLink extends EncryptedLink
{
/**
* @var string
* Attr Regex : (?<=href=)((?P<hrefQuote>'|")(?P<href>.*?)(?P=hrefQuote)|(?P<href>[^"'>][^> \r\n\t\f\v]*))
* Attr Regex : (?<=rel=)((?P<relQuote>'|")encrypt(?P=relQuote)|encrypt)
* Between Attr : \s+(?:[^>]*\s{0,1})
* or between Tag and attr
* End Attr : (?:[^>]*)?
* /(<a\s+(?:[^>]*\s{0,1})(HREF-ESPACE-ENCRYPT|ENCRYPT-ESPACE-HREF)(?:[^>]*)?>(?P<anchor>((?!<\/a>).)*)<\/a>)/iJ
* /(<a\s+(?:[^>]*\s{0,1})((?<=href=)((?P<hrefQuote>'|")(?P<href>.*?)(?P=hrefQuote)|(?P<href>[^"'>][^> \r\n\t\f\v]*))\s+(?:[^>]*\s{0,1})(?<=rel=)((?P<relQuote>'|")encrypt(?P=relQuote)|encrypt)|(?<=rel=)((?P<relQuote>'|")encrypt(?P=relQuote)|encrypt)\s+(?:[^>]*\s{0,1})(?<=href=)((?P<hrefQuote>'|")(?P<href>.*?)(?P=hrefQuote)|(?P<href>[^"'>][^> \r\n\t\f\v]*)))(?:[^>]*)?>(?P<anchor>((?!<\/a>).)*)<\/a>)/iJ
*/
public const HTML_REGEX = '/(<a\s+(?:[^>]*?\s+)?href=(["\'])(?P<href>((?!\2).)*)\2(?:[^>]*?\s+)?rel=(["\'])encrypt\5(?:[^>]*?\s+)?>(?P<anchor>((?!<\/a>).)*)<\/a>)/i';
public const HTML_REGEX = '/(<a\s+(?:[^>]*\s{0,1})((?<=href=)((?P<hrefQuote>\'|")(?P<href1>.*?)(?P=hrefQuote)|(?P<href2>[^"\'>][^> \r\n\t\f\v]*))\s+(?:[^>]*\s{0,1})(?<=rel=)((?P<relQuote>\'|")encrypt(?P=relQuote)|encrypt)|(?<=rel=)((?P<relQuote>\'|")encrypt(?P=relQuote)|encrypt)\s+(?:[^>]*\s{0,1})(?<=href=)((?P<hrefQuote>\'|")(?P<href3>.*?)(?P=hrefQuote)|(?P<href4>[^"\'>][^> \r\n\t\f\v]*)))(?:[^>]*)?>(?P<anchor>((?!<\/a>).)*)<\/a>)/iJ';

/**
* @var string
Expand All @@ -32,7 +41,7 @@ public function convertHtmlRelEncryptedLink(string $body): string
return $body;
}

return $this->replaceRelEncryptedLink($body, $matches, self::HTML_REGEX_HREF_KEY, self::HTML_REGEX_ANCHOR_KEY);
return $this->replaceRelEncryptedLink($body, $matches);
}

private function extractClass(string $openingTag): string
Expand All @@ -43,16 +52,34 @@ private function extractClass(string $openingTag): string
/**
* @param array<(string|int), array<int, string>> $matches
*/
private function replaceRelEncryptedLink(string $body, array $matches, string $hrefKey = '2', string $anchorKey = '1'): string
private function replaceRelEncryptedLink(string $body, array $matches): string
{
$nbrMatch = \count($matches[0]);
for ($k = 0; $k < $nbrMatch; ++$k) {
$attr = $this->extractClass($matches[1][$k]);
$attr = '' !== $attr ? ['class' => $attr] : [];
$link = $this->renderLink($matches[$anchorKey][$k], $matches[$hrefKey][$k], $attr);
$link = $this->renderLink(
$matches[self::HTML_REGEX_ANCHOR_KEY][$k],
$this->getHrefValue($matches, $k),
$attr
);
$body = str_replace($matches[0][$k], $link, $body);
}

return $body;
}

/**
* @param array<(string|int), array<int, string>> $matches
*/
private function getHrefValue(array $matches, int $k): string
{
for ($i = 1; $i < 5; ++$i) {
if ('' !== $matches[self::HTML_REGEX_HREF_KEY.$i][$k]) {
return $matches[self::HTML_REGEX_HREF_KEY.$i][$k];
}
}

throw new Exception();
}
}
2 changes: 1 addition & 1 deletion packages/docs/content/roadmap.md
Expand Up @@ -9,7 +9,7 @@ parent: contribute

- Implement ESlint

- [Core] Rewrite MediaTrait/MediaSlugTrait and MediaListener (tag reference before starting refactoring 0.0.859)
- [Core] Rewrite MediaTrait/MediaSlugTrait and MediaListener (tag reference before starting refactoring 0.0.859)n

- [AdminBlockEditor] Add Layout https://github.com/hata6502/editorjs-layout#readme

Expand Down

0 comments on commit a1a71b9

Please sign in to comment.