123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- <?php
- /**
- * @author Nick Pope <nick@nickpope.me.uk>
- * @copyright Copyright © 2010, Nick Pope
- * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
- */
- namespace App\Util\Lexer;
- /**
- * Twitter Validator Class.
- *
- * Performs "validation" on tweets.
- *
- * 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 Nick Pope <nick@nickpope.me.uk>
- * @copyright Copyright © 2010, Nick Pope
- * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0
- */
- class Validator extends Regex
- {
- /**
- * The maximum length of a tweet.
- *
- * @var int
- */
- const MAX_LENGTH = 140;
- /**
- * The length of a short URL beginning with http:.
- *
- * @var int
- */
- protected $short_url_length = 23;
- /**
- * The length of a short URL beginning with http:.
- *
- * @var int
- */
- protected $short_url_length_https = 23;
- /**
- * @var Extractor
- */
- protected $extractor = null;
- /**
- * Provides fluent method chaining.
- *
- * @param string $tweet The tweet to be validated.
- * @param mixed $config Setup short URL length from Twitter API /help/configuration response.
- *
- * @see __construct()
- *
- * @return Validator
- */
- public static function create($tweet = null, $config = null)
- {
- return new self($tweet, $config);
- }
- /**
- * Reads in a tweet to be parsed and validates it.
- *
- * @param string $tweet The tweet to validate.
- */
- public function __construct($tweet = null, $config = null)
- {
- parent::__construct($tweet);
- if (!empty($config)) {
- $this->setConfiguration($config);
- }
- $this->extractor = Extractor::create();
- }
- /**
- * Setup short URL length from Twitter API /help/configuration response.
- *
- * @param mixed $config
- *
- * @return Validator
- *
- * @link https://dev.twitter.com/docs/api/1/get/help/configuration
- */
- public function setConfiguration($config)
- {
- if (is_array($config)) {
- // setup from array
- if (isset($config['short_url_length'])) {
- $this->setShortUrlLength($config['short_url_length']);
- }
- if (isset($config['short_url_length_https'])) {
- $this->setShortUrlLengthHttps($config['short_url_length_https']);
- }
- } elseif (is_object($config)) {
- // setup from object
- if (isset($config->short_url_length)) {
- $this->setShortUrlLength($config->short_url_length);
- }
- if (isset($config->short_url_length_https)) {
- $this->setShortUrlLengthHttps($config->short_url_length_https);
- }
- }
- return $this;
- }
- /**
- * Set the length of a short URL beginning with http:.
- *
- * @param mixed $length
- *
- * @return Validator
- */
- public function setShortUrlLength($length)
- {
- $this->short_url_length = intval($length);
- return $this;
- }
- /**
- * Get the length of a short URL beginning with http:.
- *
- * @return int
- */
- public function getShortUrlLength()
- {
- return $this->short_url_length;
- }
- /**
- * Set the length of a short URL beginning with https:.
- *
- * @param mixed $length
- *
- * @return Validator
- */
- public function setShortUrlLengthHttps($length)
- {
- $this->short_url_length_https = intval($length);
- return $this;
- }
- /**
- * Get the length of a short URL beginning with https:.
- *
- * @return int
- */
- public function getShortUrlLengthHttps()
- {
- return $this->short_url_length_https;
- }
- /**
- * Check whether a tweet is valid.
- *
- * @param string $tweet The tweet to validate.
- *
- * @return bool Whether the tweet is valid.
- */
- public function isValidTweetText($tweet = null)
- {
- if (is_null($tweet)) {
- $tweet = $this->tweet;
- }
- $length = $this->getTweetLength($tweet);
- if (!$tweet || !$length) {
- return false;
- }
- if ($length > self::MAX_LENGTH) {
- return false;
- }
- if (preg_match(self::$patterns['invalid_characters'], $tweet)) {
- return false;
- }
- return true;
- }
- /**
- * Check whether a tweet is valid.
- *
- * @return bool Whether the tweet is valid.
- *
- * @deprecated since version 1.1.0
- */
- public function validateTweet()
- {
- return $this->isValidTweetText();
- }
- /**
- * Check whether a username is valid.
- *
- * @param string $username The username to validate.
- *
- * @return bool Whether the username is valid.
- */
- public function isValidUsername($username = null)
- {
- if (is_null($username)) {
- $username = $this->tweet;
- }
- $length = StringUtils::strlen($username);
- if (empty($username) || !$length) {
- return false;
- }
- $extracted = $this->extractor->extractMentionedScreennames($username);
- return count($extracted) === 1 && $extracted[0] === substr($username, 1);
- }
- /**
- * Check whether a username is valid.
- *
- * @return bool Whether the username is valid.
- *
- * @deprecated since version 1.1.0
- */
- public function validateUsername()
- {
- return $this->isValidUsername();
- }
- /**
- * Check whether a list is valid.
- *
- * @param string $list The list name to validate.
- *
- * @return bool Whether the list is valid.
- */
- public function isValidList($list = null)
- {
- if (is_null($list)) {
- $list = $this->tweet;
- }
- $length = StringUtils::strlen($list);
- if (empty($list) || !$length) {
- return false;
- }
- preg_match(self::$patterns['valid_mentions_or_lists'], $list, $matches);
- $matches = array_pad($matches, 5, '');
- return isset($matches) && $matches[1] === '' && $matches[4] && !empty($matches[4]) && $matches[5] === '';
- }
- /**
- * Check whether a list is valid.
- *
- * @return bool Whether the list is valid.
- *
- * @deprecated since version 1.1.0
- */
- public function validateList()
- {
- return $this->isValidList();
- }
- /**
- * Check whether a hashtag is valid.
- *
- * @param string $hashtag The hashtag to validate.
- *
- * @return bool Whether the hashtag is valid.
- */
- public function isValidHashtag($hashtag = null)
- {
- if (is_null($hashtag)) {
- $hashtag = $this->tweet;
- }
- $length = StringUtils::strlen($hashtag);
- if (empty($hashtag) || !$length) {
- return false;
- }
- $extracted = $this->extractor->extractHashtags($hashtag);
- return count($extracted) === 1 && $extracted[0] === substr($hashtag, 1);
- }
- /**
- * Check whether a hashtag is valid.
- *
- * @return bool Whether the hashtag is valid.
- *
- * @deprecated since version 1.1.0
- */
- public function validateHashtag()
- {
- return $this->isValidHashtag();
- }
- /**
- * Check whether a URL is valid.
- *
- * @param string $url The url to validate.
- * @param bool $unicode_domains Consider the domain to be unicode.
- * @param bool $require_protocol Require a protocol for valid domain?
- *
- * @return bool Whether the URL is valid.
- */
- public function isValidURL($url = null, $unicode_domains = true, $require_protocol = true)
- {
- if (is_null($url)) {
- $url = $this->tweet;
- }
- $length = StringUtils::strlen($url);
- if (empty($url) || !$length) {
- return false;
- }
- preg_match(self::$patterns['validate_url_unencoded'], $url, $matches);
- $match = array_shift($matches);
- if (!$matches || $match !== $url) {
- return false;
- }
- list($scheme, $authority, $path, $query, $fragment) = array_pad($matches, 5, '');
- // Check scheme, path, query, fragment:
- if (($require_protocol && !(
- self::isValidMatch($scheme, self::$patterns['validate_url_scheme']) && preg_match('/^https?$/i', $scheme))
- ) || !self::isValidMatch($path, self::$patterns['validate_url_path']) || !self::isValidMatch($query, self::$patterns['validate_url_query'], true)
- || !self::isValidMatch($fragment, self::$patterns['validate_url_fragment'], true)) {
- return false;
- }
- // Check authority:
- $authority_pattern = $unicode_domains ? 'validate_url_unicode_authority' : 'validate_url_authority';
- return self::isValidMatch($authority, self::$patterns[$authority_pattern]);
- }
- /**
- * Check whether a URL is valid.
- *
- * @param bool $unicode_domains Consider the domain to be unicode.
- * @param bool $require_protocol Require a protocol for valid domain?
- *
- * @return bool Whether the URL is valid.
- *
- * @deprecated since version 1.1.0
- */
- public function validateURL($unicode_domains = true, $require_protocol = true)
- {
- return $this->isValidURL(null, $unicode_domains, $require_protocol);
- }
- /**
- * Determines the length of a tweet. Takes shortening of URLs into account.
- *
- * @param string $tweet The tweet to validate.
- *
- * @return int the length of a tweet.
- */
- public function getTweetLength($tweet = null)
- {
- if (is_null($tweet)) {
- $tweet = $this->tweet;
- }
- $length = StringUtils::strlen($tweet);
- $urls_with_indices = $this->extractor->extractURLsWithIndices($tweet);
- foreach ($urls_with_indices as $x) {
- $length += $x['indices'][0] - $x['indices'][1];
- $length += stripos($x['url'], 'https://') === 0 ? $this->short_url_length_https : $this->short_url_length;
- }
- return $length;
- }
- /**
- * Determines the length of a tweet. Takes shortening of URLs into account.
- *
- * @return int the length of a tweet.
- *
- * @deprecated since version 1.1.0
- */
- public function getLength()
- {
- return $this->getTweetLength();
- }
- /**
- * A helper function to check for a valid match. Used in URL validation.
- *
- * @param string $string The subject string to test.
- * @param string $pattern The pattern to match against.
- * @param bool $optional Whether a match is compulsory or not.
- *
- * @return bool Whether an exact match was found.
- */
- protected static function isValidMatch($string, $pattern, $optional = false)
- {
- $found = preg_match($pattern, $string, $matches);
- if (!$optional) {
- return ($string || $string === '') && $found && $matches[0] === $string;
- } else {
- return !(($string || $string === '') && (!$found || $matches[0] !== $string));
- }
- }
- }
|