pixelfed/app/Util/Lexer/Autolink.php

849 lines
25 KiB
PHP
Raw Permalink Normal View History

2018-06-09 03:31:42 +00:00
<?php
/**
* @author Mike Cochrane <mikec@mikenz.geek.nz>
* @author Nick Pope <nick@nickpope.me.uk>
* @copyright Copyright © 2010, Mike Cochrane, Nick Pope
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
*/
namespace App\Util\Lexer;
2019-05-21 01:43:12 +00:00
use Illuminate\Support\Str;
2021-11-16 02:35:10 +00:00
use App\Services\AutolinkService;
2019-05-21 01:43:12 +00:00
2018-06-09 03:31:42 +00:00
/**
2018-08-28 03:07:36 +00:00
* Twitter Autolink Class.
2018-06-09 03:31:42 +00:00
*
* Parses tweets and generates HTML anchor tags around URLs, usernames,
* username/list pairs and hashtags.
*
* Originally written by {@link http://github.com/mikenz Mike Cochrane}, this
* is based on code by {@link http://github.com/mzsanford Matt Sanford} and
* heavily modified by {@link http://github.com/ngnpope Nick Pope}.
*
* @author Mike Cochrane <mikec@mikenz.geek.nz>
* @author Nick Pope <nick@nickpope.me.uk>
* @copyright Copyright © 2010, Mike Cochrane, Nick Pope
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
*/
class Autolink extends Regex
{
/**
* CSS class for auto-linked URLs.
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $class_url = '';
/**
* CSS class for auto-linked username URLs.
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $class_user = 'u-url mention';
/**
* CSS class for auto-linked list URLs.
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $class_list = 'u-url list-slug';
/**
* CSS class for auto-linked hashtag URLs.
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $class_hash = 'u-url hashtag';
/**
* CSS class for auto-linked cashtag URLs.
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $class_cash = 'u-url cashtag';
/**
* URL base for username links (the username without the @ will be appended).
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $url_base_user = null;
/**
* URL base for list links (the username/list without the @ will be appended).
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $url_base_list = null;
/**
* URL base for hashtag links (the hashtag without the # will be appended).
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $url_base_hash = null;
/**
* URL base for cashtag links (the hashtag without the $ will be appended).
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $url_base_cash = null;
/**
* Whether to include the value 'nofollow' in the 'rel' attribute.
*
2018-08-28 03:07:36 +00:00
* @var bool
2018-06-09 03:31:42 +00:00
*/
protected $nofollow = true;
/**
* Whether to include the value 'noopener' in the 'rel' attribute.
*
2018-08-28 03:07:36 +00:00
* @var bool
2018-06-09 03:31:42 +00:00
*/
protected $noopener = true;
/**
* Whether to include the value 'external' in the 'rel' attribute.
*
* Often this is used to be matched on in JavaScript for dynamically adding
* the 'target' attribute which is deprecated in HTML 4.01. In HTML 5 it has
* been undeprecated and thus the 'target' attribute can be used. If this is
* set to false then the 'target' attribute will be output.
*
2018-08-28 03:07:36 +00:00
* @var bool
2018-06-09 03:31:42 +00:00
*/
2018-08-28 03:07:36 +00:00
protected $external = true;
2018-06-09 03:31:42 +00:00
/**
* The scope to open the link in.
*
* Support for the 'target' attribute was deprecated in HTML 4.01 but has
* since been reinstated in HTML 5. To output the 'target' attribute you
* must disable the adding of the string 'external' to the 'rel' attribute.
*
2018-08-28 03:07:36 +00:00
* @var string
2018-06-09 03:31:42 +00:00
*/
protected $target = '_blank';
/**
2018-08-28 03:07:36 +00:00
* attribute for invisible span tag.
2018-06-09 03:31:42 +00:00
*
* @var string
*/
protected $invisibleTagAttrs = "style='position:absolute;left:-9999px;'";
/**
* @var Extractor
*/
protected $extractor = null;
2021-11-16 02:35:10 +00:00
/**
* @var autolinkActiveUsersOnly
*/
protected $autolinkActiveUsersOnly = false;
2018-06-09 03:31:42 +00:00
/**
* Provides fluent method chaining.
*
2018-08-28 03:07:36 +00:00
* @param string $tweet The tweet to be converted.
* @param bool $full_encode Whether to encode all special characters.
2018-06-09 03:31:42 +00:00
*
* @see __construct()
*
2018-08-28 03:07:36 +00:00
* @return Autolink
2018-06-09 03:31:42 +00:00
*/
public static function create($tweet = null, $full_encode = false)
{
return new static($tweet, $full_encode);
}
/**
* Reads in a tweet to be parsed and converted to contain links.
*
* As the intent is to produce links and output the modified tweet to the
* user, we take this opportunity to ensure that we escape user input.
*
* @see htmlspecialchars()
*
2018-08-28 03:07:36 +00:00
* @param string $tweet The tweet to be converted.
* @param bool $escape Whether to escape the tweet (default: true).
* @param bool $full_encode Whether to encode all special characters.
2018-06-09 03:31:42 +00:00
*/
public function __construct($tweet = null, $escape = true, $full_encode = false)
{
if ($escape && !empty($tweet)) {
if ($full_encode) {
parent::__construct(htmlentities($tweet, ENT_QUOTES, 'UTF-8', false));
} else {
parent::__construct(htmlspecialchars($tweet, ENT_QUOTES, 'UTF-8', false));
}
} else {
parent::__construct($tweet);
}
$this->extractor = Extractor::create();
2018-08-28 03:07:36 +00:00
$this->url_base_user = config('app.url').'/';
$this->url_base_list = config('app.url').'/';
$this->url_base_hash = config('app.url').'/discover/tags/';
$this->url_base_cash = config('app.url').'/search?q=%24';
2018-06-09 03:31:42 +00:00
}
2021-11-16 02:35:10 +00:00
public function setBaseUserPath($path = '/')
{
$this->url_base_user = config('app.url') . $path;
$this->target = null;
$this->external = null;
return $this;
}
public function setBaseHashPath($path = '/discover/tags/')
{
$this->url_base_hash = config('app.url') . $path;
$this->target = null;
$this->external = null;
return $this;
}
public function setAutolinkActiveUsersOnly($active)
{
$this->autolinkActiveUsersOnly = $active;
$this->target = null;
$this->external = null;
return $this;
}
2018-06-09 03:31:42 +00:00
/**
* CSS class for auto-linked URLs.
*
2018-08-28 03:07:36 +00:00
* @return string CSS class for URL links.
2018-06-09 03:31:42 +00:00
*/
public function getURLClass()
{
return $this->class_url;
}
/**
* CSS class for auto-linked URLs.
*
2018-08-28 03:07:36 +00:00
* @param string $v CSS class for URL links.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return Autolink Fluid method chaining.
2018-06-09 03:31:42 +00:00
*/
public function setURLClass($v)
{
$this->class_url = trim($v);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this;
}
/**
* CSS class for auto-linked username URLs.
*
2018-08-28 03:07:36 +00:00
* @return string CSS class for username links.
2018-06-09 03:31:42 +00:00
*/
public function getUsernameClass()
{
return $this->class_user;
}
/**
* CSS class for auto-linked username URLs.
*
2018-08-28 03:07:36 +00:00
* @param string $v CSS class for username links.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return Autolink Fluid method chaining.
2018-06-09 03:31:42 +00:00
*/
public function setUsernameClass($v)
{
$this->class_user = trim($v);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this;
}
/**
* CSS class for auto-linked username/list URLs.
*
2018-08-28 03:07:36 +00:00
* @return string CSS class for username/list links.
2018-06-09 03:31:42 +00:00
*/
public function getListClass()
{
return $this->class_list;
}
/**
* CSS class for auto-linked username/list URLs.
*
2018-08-28 03:07:36 +00:00
* @param string $v CSS class for username/list links.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return Autolink Fluid method chaining.
2018-06-09 03:31:42 +00:00
*/
public function setListClass($v)
{
$this->class_list = trim($v);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this;
}
/**
* CSS class for auto-linked hashtag URLs.
*
2018-08-28 03:07:36 +00:00
* @return string CSS class for hashtag links.
2018-06-09 03:31:42 +00:00
*/
public function getHashtagClass()
{
return $this->class_hash;
}
/**
* CSS class for auto-linked hashtag URLs.
*
2018-08-28 03:07:36 +00:00
* @param string $v CSS class for hashtag links.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return Autolink Fluid method chaining.
2018-06-09 03:31:42 +00:00
*/
public function setHashtagClass($v)
{
$this->class_hash = trim($v);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this;
}
/**
* CSS class for auto-linked cashtag URLs.
*
2018-08-28 03:07:36 +00:00
* @return string CSS class for cashtag links.
2018-06-09 03:31:42 +00:00
*/
public function getCashtagClass()
{
return $this->class_cash;
}
/**
* CSS class for auto-linked cashtag URLs.
*
2018-08-28 03:07:36 +00:00
* @param string $v CSS class for cashtag links.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return Autolink Fluid method chaining.
2018-06-09 03:31:42 +00:00
*/
public function setCashtagClass($v)
{
$this->class_cash = trim($v);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this;
}
/**
* Whether to include the value 'nofollow' in the 'rel' attribute.
*
2018-08-28 03:07:36 +00:00
* @return bool Whether to add 'nofollow' to the 'rel' attribute.
2018-06-09 03:31:42 +00:00
*/
public function getNoFollow()
{
return $this->nofollow;
}
/**
* Whether to include the value 'nofollow' in the 'rel' attribute.
*
2018-08-28 03:07:36 +00:00
* @param bool $v The value to add to the 'target' attribute.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return Autolink Fluid method chaining.
2018-06-09 03:31:42 +00:00
*/
public function setNoFollow($v)
{
$this->nofollow = $v;
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this;
}
/**
* Whether to include the value 'external' in the 'rel' attribute.
*
* Often this is used to be matched on in JavaScript for dynamically adding
* the 'target' attribute which is deprecated in HTML 4.01. In HTML 5 it has
* been undeprecated and thus the 'target' attribute can be used. If this is
* set to false then the 'target' attribute will be output.
*
2018-08-28 03:07:36 +00:00
* @return bool Whether to add 'external' to the 'rel' attribute.
2018-06-09 03:31:42 +00:00
*/
public function getExternal()
{
return $this->external;
}
/**
* Whether to include the value 'external' in the 'rel' attribute.
*
* Often this is used to be matched on in JavaScript for dynamically adding
* the 'target' attribute which is deprecated in HTML 4.01. In HTML 5 it has
* been undeprecated and thus the 'target' attribute can be used. If this is
* set to false then the 'target' attribute will be output.
*
2018-08-28 03:07:36 +00:00
* @param bool $v The value to add to the 'target' attribute.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return Autolink Fluid method chaining.
2018-06-09 03:31:42 +00:00
*/
public function setExternal($v)
{
$this->external = $v;
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this;
}
/**
* The scope to open the link in.
*
* Support for the 'target' attribute was deprecated in HTML 4.01 but has
* since been reinstated in HTML 5. To output the 'target' attribute you
* must disable the adding of the string 'external' to the 'rel' attribute.
*
2018-08-28 03:07:36 +00:00
* @return string The value to add to the 'target' attribute.
2018-06-09 03:31:42 +00:00
*/
public function getTarget()
{
return $this->target;
}
/**
* The scope to open the link in.
*
* Support for the 'target' attribute was deprecated in HTML 4.01 but has
* since been reinstated in HTML 5. To output the 'target' attribute you
* must disable the adding of the string 'external' to the 'rel' attribute.
*
2018-08-28 03:07:36 +00:00
* @param string $v The value to add to the 'target' attribute.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return Autolink Fluid method chaining.
2018-06-09 03:31:42 +00:00
*/
public function setTarget($v)
{
$this->target = trim($v);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this;
}
/**
2018-08-28 03:07:36 +00:00
* Autolink with entities.
2018-06-09 03:31:42 +00:00
*
* @param string $tweet
2018-08-28 03:07:36 +00:00
* @param array $entities
*
2018-06-09 03:31:42 +00:00
* @return string
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
public function autoLinkEntities($tweet = null, $entities = null)
{
if (is_null($tweet)) {
$tweet = $this->tweet;
}
$text = '';
$beginIndex = 0;
foreach ($entities as $entity) {
if (isset($entity['screen_name'])) {
2019-05-21 01:43:12 +00:00
if(Str::startsWith($entity['screen_name'], '@')) {
$text .= StringUtils::substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex);
} else {
2019-05-28 02:56:44 +00:00
$text .= StringUtils::substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex);
2019-05-21 01:43:12 +00:00
}
2018-06-09 03:31:42 +00:00
} else {
$text .= StringUtils::substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex);
}
if (isset($entity['url'])) {
$text .= $this->linkToUrl($entity);
} elseif (isset($entity['hashtag'])) {
$text .= $this->linkToHashtag($entity, $tweet);
} elseif (isset($entity['screen_name'])) {
$text .= $this->linkToMentionAndList($entity);
} elseif (isset($entity['cashtag'])) {
$text .= $this->linkToCashtag($entity, $tweet);
}
$beginIndex = $entity['indices'][1];
}
$text .= StringUtils::substr($tweet, $beginIndex, StringUtils::strlen($tweet));
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $text;
}
/**
* Auto-link hashtags, URLs, usernames and lists, with JSON entities.
*
* @param string The tweet to be converted
* @param mixed The entities info
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @return string that auto-link HTML added
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
public function autoLinkWithJson($tweet = null, $json = null)
{
// concatenate entities
2018-08-28 03:07:36 +00:00
$entities = [];
2018-06-09 03:31:42 +00:00
if (is_object($json)) {
$json = $this->object2array($json);
}
if (is_array($json)) {
foreach ($json as $key => $vals) {
$entities = array_merge($entities, $json[$key]);
}
}
// map JSON entity to twitter-text entity
foreach ($entities as $idx => $entity) {
if (!empty($entity['text'])) {
$entities[$idx]['hashtag'] = $entity['text'];
}
}
$entities = $this->extractor->removeOverlappingEntities($entities);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this->autoLinkEntities($tweet, $entities);
}
/**
2018-08-28 03:07:36 +00:00
* convert Object to Array.
2018-06-09 03:31:42 +00:00
*
* @param mixed $obj
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @return array
*/
protected function object2array($obj)
{
$array = (array) $obj;
foreach ($array as $key => $var) {
if (is_object($var) || is_array($var)) {
$array[$key] = $this->object2array($var);
}
}
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $array;
}
/**
* Auto-link hashtags, URLs, usernames and lists.
*
* @param string The tweet to be converted
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @return string that auto-link HTML added
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
public function autoLink($tweet = null)
{
if (is_null($tweet)) {
$tweet = $this->tweet;
}
$entities = $this->extractor->extractURLWithoutProtocol(false)->extractEntitiesWithIndices($tweet);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this->autoLinkEntities($tweet, $entities);
}
/**
* Auto-link the @username and @username/list references in the provided text. Links to @username references will
* have the usernameClass CSS classes added. Links to @username/list references will have the listClass CSS class
* added.
*
* @return string that auto-link HTML added
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
public function autoLinkUsernamesAndLists($tweet = null)
{
if (is_null($tweet)) {
$tweet = $this->tweet;
}
$entities = $this->extractor->extractMentionsOrListsWithIndices($tweet);
2018-08-28 03:07:36 +00:00
2021-11-16 02:35:10 +00:00
if($this->autolinkActiveUsersOnly == true) {
$entities = collect($entities)
->filter(function($entity) {
return AutolinkService::mentionedUsernameExists($entity['screen_name']);
})
->toArray();
}
2018-06-09 03:31:42 +00:00
return $this->autoLinkEntities($tweet, $entities);
}
/**
* Auto-link #hashtag references in the provided Tweet text. The #hashtag links will have the hashtagClass CSS class
* added.
*
* @return string that auto-link HTML added
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
public function autoLinkHashtags($tweet = null)
{
if (is_null($tweet)) {
$tweet = $this->tweet;
}
$entities = $this->extractor->extractHashtagsWithIndices($tweet);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this->autoLinkEntities($tweet, $entities);
}
/**
* Auto-link URLs in the Tweet text provided.
* <p/>
* This only auto-links URLs with protocol.
*
* @return string that auto-link HTML added
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
public function autoLinkURLs($tweet = null)
{
if (is_null($tweet)) {
$tweet = $this->tweet;
}
$entities = $this->extractor->extractURLWithoutProtocol(false)->extractURLsWithIndices($tweet);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this->autoLinkEntities($tweet, $entities);
}
/**
* Auto-link $cashtag references in the provided Tweet text. The $cashtag links will have the cashtagClass CSS class
* added.
*
* @return string that auto-link HTML added
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
public function autoLinkCashtags($tweet = null)
{
if (is_null($tweet)) {
$tweet = $this->tweet;
}
$entities = $this->extractor->extractCashtagsWithIndices($tweet);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this->autoLinkEntities($tweet, $entities);
}
public function linkToUrl($entity)
{
if (!empty($this->class_url)) {
$attributes['class'] = $this->class_url;
}
$attributes['href'] = $entity['url'];
$linkText = $this->escapeHTML($entity['url']);
if (!empty($entity['display_url']) && !empty($entity['expanded_url'])) {
// Goal: If a user copies and pastes a tweet containing t.co'ed link, the resulting paste
// should contain the full original URL (expanded_url), not the display URL.
//
// Method: Whenever possible, we actually emit HTML that contains expanded_url, and use
// font-size:0 to hide those parts that should not be displayed (because they are not part of display_url).
// Elements with font-size:0 get copied even though they are not visible.
// Note that display:none doesn't work here. Elements with display:none don't get copied.
//
// Additionally, we want to *display* ellipses, but we don't want them copied. To make this happen we
// wrap the ellipses in a tco-ellipsis class and provide an onCopy handler that sets display:none on
// everything with the tco-ellipsis class.
//
// As an example: The user tweets "hi http://longdomainname.com/foo"
// This gets shortened to "hi http://t.co/xyzabc", with display_url = "…nname.com/foo"
// This will get rendered as:
// <span class='tco-ellipsis'> <!-- This stuff should get displayed but not copied -->
// …
// <!-- There's a chance the onCopy event handler might not fire. In case that happens,
// we include an &nbsp; here so that the … doesn't bump up against the URL and ruin it.
// The &nbsp; is inside the tco-ellipsis span so that when the onCopy handler *does*
// fire, it doesn't get copied. Otherwise the copied text would have two spaces in a row,
// e.g. "hi http://longdomainname.com/foo".
// <span style='font-size:0'>&nbsp;</span>
// </span>
// <span style='font-size:0'> <!-- This stuff should get copied but not displayed -->
// http://longdomai
// </span>
// <span class='js-display-url'> <!-- This stuff should get displayed *and* copied -->
// nname.com/foo
// </span>
// <span class='tco-ellipsis'> <!-- This stuff should get displayed but not copied -->
// <span style='font-size:0'>&nbsp;</span>
// …
// </span>
//
// Exception: pic.socialhub.dev images, for which expandedUrl = "https://socialhub.dev/#!/username/status/1234/photo/1
// For those URLs, display_url is not a substring of expanded_url, so we don't do anything special to render the elided parts.
// For a pic.socialhub.dev URL, the only elided part will be the "https://", so this is fine.
$displayURL = $entity['display_url'];
$expandedURL = $entity['expanded_url'];
$displayURLSansEllipses = preg_replace('/…/u', '', $displayURL);
$diplayURLIndexInExpandedURL = mb_strpos($expandedURL, $displayURLSansEllipses);
if ($diplayURLIndexInExpandedURL !== false) {
$beforeDisplayURL = mb_substr($expandedURL, 0, $diplayURLIndexInExpandedURL);
$afterDisplayURL = mb_substr($expandedURL, $diplayURLIndexInExpandedURL + mb_strlen($displayURLSansEllipses));
$precedingEllipsis = (preg_match('/\A…/u', $displayURL)) ? '…' : '';
$followingEllipsis = (preg_match('/…\z/u', $displayURL)) ? '…' : '';
$invisibleSpan = "<span {$this->invisibleTagAttrs}>";
$linkText = "<span class='tco-ellipsis'>{$precedingEllipsis}{$invisibleSpan}&nbsp;</span></span>";
$linkText .= "{$invisibleSpan}{$this->escapeHTML($beforeDisplayURL)}</span>";
$linkText .= "<span class='js-display-url'>{$this->escapeHTML($displayURLSansEllipses)}</span>";
$linkText .= "{$invisibleSpan}{$this->escapeHTML($afterDisplayURL)}</span>";
$linkText .= "<span class='tco-ellipsis'>{$invisibleSpan}&nbsp;</span>{$followingEllipsis}</span>";
} else {
$linkText = $entity['display_url'];
}
$attributes['title'] = $entity['expanded_url'];
} elseif (!empty($entity['display_url'])) {
$linkText = $entity['display_url'];
}
return $this->linkToText($entity, $linkText, $attributes);
}
/**
* @param array $entity
* @param string $tweet
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @return string
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
public function linkToHashtag($entity, $tweet = null)
{
if (is_null($tweet)) {
$tweet = $this->tweet;
}
$this->target = false;
2018-08-28 03:07:36 +00:00
$attributes = [];
$class = [];
2018-06-09 03:31:42 +00:00
$hash = StringUtils::substr($tweet, $entity['indices'][0], 1);
2018-08-28 03:07:36 +00:00
$linkText = $hash.$entity['hashtag'];
2018-06-09 03:31:42 +00:00
2018-08-28 03:07:36 +00:00
$attributes['href'] = $this->url_base_hash.$entity['hashtag'].'?src=hash';
$attributes['title'] = '#'.$entity['hashtag'];
2018-06-09 03:31:42 +00:00
if (!empty($this->class_hash)) {
$class[] = $this->class_hash;
}
if (preg_match(self::$patterns['rtl_chars'], $linkText)) {
$class[] = 'rtl';
}
if (!empty($class)) {
2018-08-28 03:07:36 +00:00
$attributes['class'] = implode(' ', $class);
2018-06-09 03:31:42 +00:00
}
return $this->linkToText($entity, $linkText, $attributes);
}
/**
2018-08-28 03:07:36 +00:00
* @param array $entity
2018-06-09 03:31:42 +00:00
*
* @return string
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
public function linkToMentionAndList($entity)
{
2018-08-28 03:07:36 +00:00
$attributes = [];
2019-05-28 02:56:44 +00:00
$screen_name = $entity['screen_name'];
2021-11-16 02:35:10 +00:00
if($this->autolinkActiveUsersOnly == true) {
if(!AutolinkService::mentionedUsernameExists($screen_name)) {
return Str::of($screen_name)->startsWith('@') ? $screen_name : "@{$screen_name}";
}
}
2018-06-09 03:31:42 +00:00
if (!empty($entity['list_slug'])) {
2018-08-28 03:07:36 +00:00
// Replace the list and username
2019-05-28 02:56:44 +00:00
$linkText = Str::startsWith($screen_name, '@') ? $screen_name : '@'.$screen_name;
2018-06-09 03:31:42 +00:00
$class = $this->class_list;
2019-05-28 02:56:44 +00:00
$url = $this->url_base_list.$screen_name;
2018-06-09 03:31:42 +00:00
} else {
2018-08-28 03:07:36 +00:00
// Replace the username
2019-05-28 02:56:44 +00:00
$linkText = Str::startsWith($screen_name, '@') ? $screen_name : '@'.$screen_name;
2018-06-09 03:31:42 +00:00
$class = $this->class_user;
2019-06-27 06:02:44 +00:00
$url = $this->url_base_user . $screen_name;
2018-06-09 03:31:42 +00:00
}
if (!empty($class)) {
$attributes['class'] = $class;
}
$attributes['href'] = $url;
return $this->linkToText($entity, $linkText, $attributes);
}
/**
* @param array $entity
* @param string $tweet
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @return string
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
public function linkToCashtag($entity, $tweet = null)
{
if (is_null($tweet)) {
$tweet = $this->tweet;
}
2018-08-28 03:07:36 +00:00
$attributes = [];
2018-06-09 03:31:42 +00:00
$doller = StringUtils::substr($tweet, $entity['indices'][0], 1);
2018-08-28 03:07:36 +00:00
$linkText = $doller.$entity['cashtag'];
$attributes['href'] = $this->url_base_cash.$entity['cashtag'];
2018-06-09 03:31:42 +00:00
$attributes['title'] = $linkText;
if (!empty($this->class_cash)) {
$attributes['class'] = $this->class_cash;
}
return $this->linkToText($entity, $linkText, $attributes);
}
/**
2018-08-28 03:07:36 +00:00
* @param array $entity
2018-06-09 03:31:42 +00:00
* @param string $text
2018-08-28 03:07:36 +00:00
* @param array $attributes
*
2018-06-09 03:31:42 +00:00
* @return string
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.1.0
*/
2018-08-28 03:07:36 +00:00
public function linkToText(array $entity, $text, $attributes = [])
2018-06-09 03:31:42 +00:00
{
2018-08-28 03:07:36 +00:00
$rel = [];
2018-06-09 03:31:42 +00:00
if ($this->external) {
$rel[] = 'external';
}
if ($this->nofollow) {
$rel[] = 'nofollow';
}
if ($this->noopener) {
$rel[] = 'noopener';
}
if (!empty($rel)) {
2018-08-28 03:07:36 +00:00
$attributes['rel'] = implode(' ', $rel);
2018-06-09 03:31:42 +00:00
}
if ($this->target) {
$attributes['target'] = $this->target;
}
$link = '<a';
foreach ($attributes as $key => $val) {
2018-08-28 03:07:36 +00:00
$link .= ' '.$key.'="'.$this->escapeHTML($val).'"';
2018-06-09 03:31:42 +00:00
}
2018-08-28 03:07:36 +00:00
$link .= '>'.$text.'</a>';
2018-06-09 03:31:42 +00:00
return $link;
}
/**
2018-08-28 03:07:36 +00:00
* html escape.
2018-06-09 03:31:42 +00:00
*
* @param string $text
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @return string
*/
protected function escapeHTML($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8', false);
}
}