123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- <?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;
- /**
- * Twitter LooseAutolink Class.
- *
- * 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
- *
- * @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
- *
- * @return string that auto-link HTML added
- *
- * @deprecated since version 1.9.0
- */
- public function autoLink($tweet = null)
- {
- if (!is_null($tweet)) {
- $this->tweet = $tweet;
- }
- 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;
- }
- 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;
- }
- 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;
- }
- 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;
- }
- return $this->addLinksToCashtags();
- }
- /**
- * Adds links to all elements in the tweet.
- *
- * @return string The modified tweet.
- *
- * @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;
- return $modified;
- }
- /**
- * Adds links to hashtag elements in the tweet.
- *
- * @return string The modified tweet.
- */
- public function addLinksToHashtags()
- {
- return preg_replace_callback(
- self::$patterns['valid_hashtag'],
- [$this, '_addLinksToHashtags'],
- $this->tweet
- );
- }
- /**
- * Adds links to cashtag elements in the tweet.
- *
- * @return string The modified tweet.
- */
- public function addLinksToCashtags()
- {
- return preg_replace_callback(
- self::$patterns['valid_cashtag'],
- [$this, '_addLinksToCashtags'],
- $this->tweet
- );
- }
- /**
- * Adds links to URL elements in the tweet.
- *
- * @return string The modified tweet
- */
- public function addLinksToURLs()
- {
- return preg_replace_callback(self::$patterns['valid_url'], [$this, '_addLinksToURLs'], $this->tweet);
- }
- /**
- * Adds links to username/list elements in the tweet.
- *
- * @return string The modified tweet.
- */
- public function addLinksToUsernamesAndLists()
- {
- return preg_replace_callback(
- self::$patterns['valid_mentions_or_lists'],
- [$this, '_addLinksToUsernamesAndLists'],
- $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.
- *
- * @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.
- *
- * @deprecated since version 1.1.0
- */
- protected function wrap($url, $class, $element)
- {
- $link = '<a';
- if ($class) {
- $link .= ' class="'.$class.'"';
- }
- $link .= ' href="'.$url.'"';
- $rel = [];
- if ($this->external) {
- $rel[] = 'external';
- }
- if ($this->nofollow) {
- $rel[] = 'nofollow';
- }
- if (!empty($rel)) {
- $link .= ' rel="'.implode(' ', $rel).'"';
- }
- if ($this->target) {
- $link .= ' target="'.$this->target.'"';
- }
- $link .= '>'.$element.'</a>';
- 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.
- *
- * @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.
- */
- protected function wrapHash($url, $class, $element)
- {
- $title = preg_replace('/#/u', '#', $element);
- $link = '<a';
- $link .= ' href="'.$url.'"';
- $link .= ' title="'.$title.'"';
- if ($class) {
- $link .= ' class="'.$class.'"';
- }
- $rel = [];
- if ($this->external) {
- $rel[] = 'external';
- }
- if ($this->nofollow) {
- $rel[] = 'nofollow';
- }
- if (!empty($rel)) {
- $link .= ' rel="'.implode(' ', $rel).'"';
- }
- if ($this->target) {
- $link .= ' target="'.$this->target.'"';
- }
- $link .= '>'.$element.'</a>';
- return $link;
- }
- /**
- * Callback used by the method that adds links to hashtags.
- *
- * @see addLinksToHashtags()
- *
- * @param array $matches The regular expression matches.
- *
- * @return string The link-wrapped hashtag.
- */
- 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;
- $element = $hash.$tag;
- $url = $this->url_base_hash.$tag;
- $class_hash = $this->class_hash;
- if (preg_match(self::$patterns['rtl_chars'], $element)) {
- $class_hash .= ' rtl';
- }
- $replacement .= $this->wrapHash($url, $class_hash, $element);
- return $replacement;
- }
- /**
- * Callback used by the method that adds links to cashtags.
- *
- * @see addLinksToCashtags()
- *
- * @param array $matches The regular expression matches.
- *
- * @return string The link-wrapped cashtag.
- */
- 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;
- $element = $cash.$tag;
- $url = $this->url_base_cash.$tag;
- $replacement .= $this->wrapHash($url, $this->class_cash, $element);
- return $replacement;
- }
- /**
- * Callback used by the method that adds links to URLs.
- *
- * @see addLinksToURLs()
- *
- * @param array $matches The regular expression matches.
- *
- * @return string The link-wrapped URL.
- */
- 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;
- }
- return $before.$this->wrap($url, $this->class_url, $url);
- }
- /**
- * Callback used by the method that adds links to username/list pairs.
- *
- * @see addLinksToUsernamesAndLists()
- *
- * @param array $matches The regular expression matches.
- *
- * @return string The link-wrapped username/list pair.
- */
- protected function _addLinksToUsernamesAndLists($matches)
- {
- list($all, $before, $at, $username, $slash_listname, $after) = array_pad($matches, 6, '');
- // If $after is not empty, there is an invalid character.
- if (!empty($slash_listname)) {
- // Replace the list and username
- $element = $username.$slash_listname;
- $class = $this->class_list;
- $url = $this->url_base_list.$element;
- } else {
- if (preg_match(self::$patterns['end_mention_match'], $after)) {
- return $all;
- }
- // Replace the username
- $element = $username;
- $class = $this->class_user;
- $url = $this->url_base_user.$element;
- }
- // 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);
- }
- }
|