1
0
Fork 0
forked from mirror/pixelfed
pixelfed/app/Util/Lexer/LooseAutolink.php

368 lines
11 KiB
PHP
Raw 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>
* @author Takashi Nojima
* @copyright Copyright 2014 Mike Cochrane, Nick Pope, Takashi Nojima
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
*/
namespace App\Util\Lexer;
/**
2018-08-28 03:07:36 +00:00
* Twitter LooseAutolink 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>
* @author Takashi Nojima
* @copyright Copyright 2014 Mike Cochrane, Nick Pope, Takashi Nojima
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
2018-08-28 03:07:36 +00:00
*
2018-06-09 03:31:42 +00:00
* @since 1.8.0
* @deprecated since version 1.9.0
*/
class LooseAutolink extends Autolink
{
/**
* 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
* @deprecated since version 1.9.0
*/
public function autoLink($tweet = null)
{
if (!is_null($tweet)) {
$this->tweet = $tweet;
}
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this->addLinks();
}
/**
* 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
*/
public function autoLinkUsernamesAndLists($tweet = null)
{
if (!is_null($tweet)) {
$this->tweet = $tweet;
}
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this->addLinksToUsernamesAndLists();
}
/**
* 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
*/
public function autoLinkHashtags($tweet = null)
{
if (!is_null($tweet)) {
$this->tweet = $tweet;
}
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this->addLinksToHashtags();
}
/**
* Auto-link URLs in the Tweet text provided.
* <p/>
* This only auto-links URLs with protocol.
*
* @return string that auto-link HTML added
*/
public function autoLinkURLs($tweet = null)
{
if (!is_null($tweet)) {
$this->tweet = $tweet;
}
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this->addLinksToURLs();
}
/**
* 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
*/
public function autoLinkCashtags($tweet = null)
{
if (!is_null($tweet)) {
$this->tweet = $tweet;
}
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $this->addLinksToCashtags();
}
/**
* Adds links to all elements in the tweet.
*
2018-08-28 03:07:36 +00:00
* @return string The modified tweet.
*
2018-06-09 03:31:42 +00:00
* @deprecated since version 1.9.0
*/
public function addLinks()
{
$original = $this->tweet;
$this->tweet = $this->addLinksToURLs();
$this->tweet = $this->addLinksToHashtags();
$this->tweet = $this->addLinksToCashtags();
$this->tweet = $this->addLinksToUsernamesAndLists();
$modified = $this->tweet;
$this->tweet = $original;
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $modified;
}
/**
* Adds links to hashtag elements in the tweet.
*
2018-08-28 03:07:36 +00:00
* @return string The modified tweet.
2018-06-09 03:31:42 +00:00
*/
public function addLinksToHashtags()
{
return preg_replace_callback(
self::$patterns['valid_hashtag'],
2018-08-28 03:07:36 +00:00
[$this, '_addLinksToHashtags'],
2018-06-09 03:31:42 +00:00
$this->tweet
);
}
/**
* Adds links to cashtag elements in the tweet.
*
2018-08-28 03:07:36 +00:00
* @return string The modified tweet.
2018-06-09 03:31:42 +00:00
*/
public function addLinksToCashtags()
{
return preg_replace_callback(
self::$patterns['valid_cashtag'],
2018-08-28 03:07:36 +00:00
[$this, '_addLinksToCashtags'],
2018-06-09 03:31:42 +00:00
$this->tweet
);
}
/**
* Adds links to URL elements in the tweet.
*
2018-08-28 03:07:36 +00:00
* @return string The modified tweet
2018-06-09 03:31:42 +00:00
*/
public function addLinksToURLs()
{
2018-08-28 03:07:36 +00:00
return preg_replace_callback(self::$patterns['valid_url'], [$this, '_addLinksToURLs'], $this->tweet);
2018-06-09 03:31:42 +00:00
}
/**
* Adds links to username/list elements in the tweet.
*
2018-08-28 03:07:36 +00:00
* @return string The modified tweet.
2018-06-09 03:31:42 +00:00
*/
public function addLinksToUsernamesAndLists()
{
return preg_replace_callback(
self::$patterns['valid_mentions_or_lists'],
2018-08-28 03:07:36 +00:00
[$this, '_addLinksToUsernamesAndLists'],
2018-06-09 03:31:42 +00:00
$this->tweet
);
}
/**
* Wraps a tweet element in an HTML anchor tag using the provided URL.
*
* This is a helper function to perform the generation of the link.
*
2018-08-28 03:07:36 +00:00
* @param string $url The URL to use as the href.
* @param string $class The CSS class(es) to apply (space separated).
* @param string $element The tweet element to wrap.
*
* @return string The tweet element with a link applied.
2018-06-09 03:31:42 +00:00
*
* @deprecated since version 1.1.0
*/
protected function wrap($url, $class, $element)
{
$link = '<a';
if ($class) {
2018-08-28 03:07:36 +00:00
$link .= ' class="'.$class.'"';
2018-06-09 03:31:42 +00:00
}
2018-08-28 03:07:36 +00:00
$link .= ' href="'.$url.'"';
$rel = [];
2018-06-09 03:31:42 +00:00
if ($this->external) {
$rel[] = 'external';
}
if ($this->nofollow) {
$rel[] = 'nofollow';
}
if (!empty($rel)) {
2018-08-28 03:07:36 +00:00
$link .= ' rel="'.implode(' ', $rel).'"';
2018-06-09 03:31:42 +00:00
}
if ($this->target) {
2018-08-28 03:07:36 +00:00
$link .= ' target="'.$this->target.'"';
2018-06-09 03:31:42 +00:00
}
2018-08-28 03:07:36 +00:00
$link .= '>'.$element.'</a>';
2018-06-09 03:31:42 +00:00
return $link;
}
/**
* Wraps a tweet element in an HTML anchor tag using the provided URL.
*
* This is a helper function to perform the generation of the hashtag link.
*
2018-08-28 03:07:36 +00:00
* @param string $url The URL to use as the href.
* @param string $class The CSS class(es) to apply (space separated).
* @param string $element The tweet element to wrap.
2018-06-09 03:31:42 +00:00
*
2018-08-28 03:07:36 +00:00
* @return string The tweet element with a link applied.
2018-06-09 03:31:42 +00:00
*/
protected function wrapHash($url, $class, $element)
{
$title = preg_replace('//u', '#', $element);
$link = '<a';
2018-08-28 03:07:36 +00:00
$link .= ' href="'.$url.'"';
$link .= ' title="'.$title.'"';
2018-06-09 03:31:42 +00:00
if ($class) {
2018-08-28 03:07:36 +00:00
$link .= ' class="'.$class.'"';
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 (!empty($rel)) {
2018-08-28 03:07:36 +00:00
$link .= ' rel="'.implode(' ', $rel).'"';
2018-06-09 03:31:42 +00:00
}
if ($this->target) {
2018-08-28 03:07:36 +00:00
$link .= ' target="'.$this->target.'"';
2018-06-09 03:31:42 +00:00
}
2018-08-28 03:07:36 +00:00
$link .= '>'.$element.'</a>';
2018-06-09 03:31:42 +00:00
return $link;
}
/**
* Callback used by the method that adds links to hashtags.
*
* @see addLinksToHashtags()
2018-08-28 03:07:36 +00:00
*
* @param array $matches The regular expression matches.
*
* @return string The link-wrapped hashtag.
2018-06-09 03:31:42 +00:00
*/
protected function _addLinksToHashtags($matches)
{
list($all, $before, $hash, $tag, $after) = array_pad($matches, 5, '');
if (preg_match(self::$patterns['end_hashtag_match'], $after)
|| (!preg_match('!\A["\']!', $before) && preg_match('!\A["\']!', $after)) || preg_match('!\A</!', $after)) {
return $all;
}
$replacement = $before;
2018-08-28 03:07:36 +00:00
$element = $hash.$tag;
$url = $this->url_base_hash.$tag;
2018-06-09 03:31:42 +00:00
$class_hash = $this->class_hash;
if (preg_match(self::$patterns['rtl_chars'], $element)) {
$class_hash .= ' rtl';
}
$replacement .= $this->wrapHash($url, $class_hash, $element);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $replacement;
}
/**
* Callback used by the method that adds links to cashtags.
*
* @see addLinksToCashtags()
2018-08-28 03:07:36 +00:00
*
* @param array $matches The regular expression matches.
*
* @return string The link-wrapped cashtag.
2018-06-09 03:31:42 +00:00
*/
protected function _addLinksToCashtags($matches)
{
list($all, $before, $cash, $tag, $after) = array_pad($matches, 5, '');
if (preg_match(self::$patterns['end_cashtag_match'], $after)
|| (!preg_match('!\A["\']!', $before) && preg_match('!\A["\']!', $after)) || preg_match('!\A</!', $after)) {
return $all;
}
$replacement = $before;
2018-08-28 03:07:36 +00:00
$element = $cash.$tag;
$url = $this->url_base_cash.$tag;
2018-06-09 03:31:42 +00:00
$replacement .= $this->wrapHash($url, $this->class_cash, $element);
2018-08-28 03:07:36 +00:00
2018-06-09 03:31:42 +00:00
return $replacement;
}
/**
* Callback used by the method that adds links to URLs.
*
* @see addLinksToURLs()
2018-08-28 03:07:36 +00:00
*
* @param array $matches The regular expression matches.
*
* @return string The link-wrapped URL.
2018-06-09 03:31:42 +00:00
*/
protected function _addLinksToURLs($matches)
{
list($all, $before, $url, $protocol, $domain, $path, $query) = array_pad($matches, 7, '');
$url = htmlspecialchars($url, ENT_QUOTES, 'UTF-8', false);
if (!$protocol) {
return $all;
}
2018-08-28 03:07:36 +00:00
return $before.$this->wrap($url, $this->class_url, $url);
2018-06-09 03:31:42 +00:00
}
/**
* Callback used by the method that adds links to username/list pairs.
*
* @see addLinksToUsernamesAndLists()
2018-08-28 03:07:36 +00:00
*
* @param array $matches The regular expression matches.
*
* @return string The link-wrapped username/list pair.
2018-06-09 03:31:42 +00:00
*/
protected function _addLinksToUsernamesAndLists($matches)
{
list($all, $before, $at, $username, $slash_listname, $after) = array_pad($matches, 6, '');
2018-08-28 03:07:36 +00:00
// If $after is not empty, there is an invalid character.
2018-06-09 03:31:42 +00:00
if (!empty($slash_listname)) {
2018-08-28 03:07:36 +00:00
// Replace the list and username
$element = $username.$slash_listname;
2018-06-09 03:31:42 +00:00
$class = $this->class_list;
2018-08-28 03:07:36 +00:00
$url = $this->url_base_list.$element;
2018-06-09 03:31:42 +00:00
} else {
if (preg_match(self::$patterns['end_mention_match'], $after)) {
return $all;
}
2018-08-28 03:07:36 +00:00
// Replace the username
2018-06-09 03:31:42 +00:00
$element = $username;
$class = $this->class_user;
2018-08-28 03:07:36 +00:00
$url = $this->url_base_user.$element;
2018-06-09 03:31:42 +00:00
}
2018-08-28 03:07:36 +00:00
// XXX: Due to use of preg_replace_callback() for multiple replacements in a
// single tweet and also as only the match is replaced and we have to
// use a look-ahead for $after because there is no equivalent for the
// $' (dollar apostrophe) global from Ruby, we MUST NOT append $after.
return $before.$at.$this->wrap($url, $class, $element);
2018-06-09 03:31:42 +00:00
}
}