diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..3c44241cc --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 4a0b5bd5f..6f5eeea43 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use Carbon\Carbon; use Auth, Cache, Redis; use App\{Notification, Profile, User}; @@ -15,10 +16,17 @@ class AccountController extends Controller public function notifications(Request $request) { + $this->validate($request, [ + 'page' => 'nullable|min:1|max:3' + ]); $profile = Auth::user()->profile; - //$notifications = $this->fetchNotifications($profile->id); + $timeago = Carbon::now()->subMonths(6); $notifications = Notification::whereProfileId($profile->id) - ->orderBy('id','desc')->take(30)->simplePaginate(); + ->whereDate('created_at', '>', $timeago) + ->orderBy('id','desc') + ->take(30) + ->simplePaginate(); + return view('account.activity', compact('profile', 'notifications')); } diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index 9c7fa9041..4e25bd236 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -32,6 +32,7 @@ class ProfileController extends Controller // TODO: refactor this mess $owner = Auth::check() && Auth::id() === $user->user_id; $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false; + $is_admin = is_null($user->domain) ? $user->user->is_admin : false; $timeline = $user->statuses() ->whereHas('media') ->whereNull('in_reply_to_id') @@ -39,7 +40,7 @@ class ProfileController extends Controller ->withCount(['comments', 'likes']) ->simplePaginate(21); - return view('profile.show', compact('user', 'owner', 'is_following', 'timeline')); + return view('profile.show', compact('user', 'owner', 'is_following', 'is_admin', 'timeline')); } public function showActivityPub(Request $request, $user) @@ -66,7 +67,8 @@ class ProfileController extends Controller $owner = Auth::check() && Auth::id() === $user->user_id; $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false; $followers = $profile->followers()->orderBy('created_at','desc')->simplePaginate(12); - return view('profile.followers', compact('user', 'profile', 'followers', 'owner', 'is_following')); + $is_admin = is_null($user->domain) ? $user->user->is_admin : false; + return view('profile.followers', compact('user', 'profile', 'followers', 'owner', 'is_following', 'is_admin')); } public function following(Request $request, $username) @@ -77,7 +79,8 @@ class ProfileController extends Controller $owner = Auth::check() && Auth::id() === $user->user_id; $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false; $following = $profile->following()->orderBy('created_at','desc')->simplePaginate(12); - return view('profile.following', compact('user', 'profile', 'following', 'owner', 'is_following')); + $is_admin = is_null($user->domain) ? $user->user->is_admin : false; + return view('profile.following', compact('user', 'profile', 'following', 'owner', 'is_following', 'is_admin')); } public function savedBookmarks(Request $request, $username) @@ -88,7 +91,9 @@ class ProfileController extends Controller $user = Auth::user()->profile; $owner = true; $following = false; - $timeline = $user->bookmarks()->orderBy('created_at','desc')->simplePaginate(10); - return view('profile.show', compact('user', 'owner', 'following', 'timeline')); + $timeline = $user->bookmarks()->withCount(['likes','comments'])->orderBy('created_at','desc')->simplePaginate(10); + $is_following = ($owner == false && Auth::check()) ? $user->followedBy(Auth::user()->profile) : false; + $is_admin = is_null($user->domain) ? $user->user->is_admin : false; + return view('profile.show', compact('user', 'owner', 'following', 'timeline', 'is_following', 'is_admin')); } } diff --git a/app/Http/Controllers/StatusController.php b/app/Http/Controllers/StatusController.php index 27cf768f8..4e5c2e8d4 100644 --- a/app/Http/Controllers/StatusController.php +++ b/app/Http/Controllers/StatusController.php @@ -33,9 +33,11 @@ class StatusController extends Controller $this->validate($request, [ 'photo' => 'required|mimes:jpeg,png,bmp,gif|max:' . config('pixelfed.max_photo_size'), - 'caption' => 'string|max:' . config('pixelfed.max_caption_length') + 'caption' => 'string|max:' . config('pixelfed.max_caption_length'), + 'cw' => 'nullable|string' ]); + $cw = $request->filled('cw') && $request->cw == 'on' ? true : false; $monthHash = hash('sha1', date('Y') . date('m')); $userHash = hash('sha1', $user->id . (string) $user->created_at); $storagePath = "public/m/{$monthHash}/{$userHash}"; @@ -45,6 +47,8 @@ class StatusController extends Controller $status = new Status; $status->profile_id = $profile->id; $status->caption = $request->caption; + $status->is_nsfw = $cw; + $status->save(); $media = new Media; diff --git a/app/Jobs/LikePipeline/LikePipeline.php b/app/Jobs/LikePipeline/LikePipeline.php index 28410bfc3..9d53fd8b1 100644 --- a/app/Jobs/LikePipeline/LikePipeline.php +++ b/app/Jobs/LikePipeline/LikePipeline.php @@ -37,7 +37,14 @@ class LikePipeline implements ShouldQueue $status = $this->like->status; $actor = $this->like->actor; - if($actor->id === $status->profile_id) { + $exists = Notification::whereProfileId($status->profile_id) + ->whereActorId($actor->id) + ->whereAction('like') + ->whereItemId($status->id) + ->whereItemType('App\Status') + ->count(); + + if($actor->id === $status->profile_id || $exists !== 0) { return true; } diff --git a/app/Jobs/MentionPipeline/MentionPipeline.php b/app/Jobs/MentionPipeline/MentionPipeline.php new file mode 100644 index 000000000..69b7ebbb2 --- /dev/null +++ b/app/Jobs/MentionPipeline/MentionPipeline.php @@ -0,0 +1,72 @@ +status = $status; + $this->mention = $mention; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + + $status = $this->status; + $mention = $this->mention; + $actor = $this->status->profile; + $target = $this->mention->profile_id; + + $exists = Notification::whereProfileId($target) + ->whereActorId($actor->id) + ->whereAction('mention') + ->whereItemId($status->id) + ->whereItemType('App\Status') + ->count(); + + if($actor->id === $target || $exists !== 0) { + return true; + } + + try { + + $notification = new Notification; + $notification->profile_id = $target; + $notification->actor_id = $actor->id; + $notification->action = 'mention'; + $notification->message = $mention->toText(); + $notification->rendered = $mention->toHtml(); + $notification->item_id = $status->id; + $notification->item_type = "App\Status"; + $notification->save(); + + } catch (Exception $e) { + + } + + } +} diff --git a/app/Mention.php b/app/Mention.php new file mode 100644 index 000000000..3473c2924 --- /dev/null +++ b/app/Mention.php @@ -0,0 +1,33 @@ +belongsTo(Profile::class, 'profile_id', 'id'); + } + + public function status() + { + return $this->belongsTo(Status::class, 'status_id', 'id'); + } + + public function toText() + { + $actorName = $this->status->profile->username; + return "{$actorName} " . __('notification.mentionedYou'); + } + + public function toHtml() + { + $actorName = $this->status->profile->username; + $actorUrl = $this->status->profile->url(); + return "{$actorName} " . + __('notification.mentionedYou'); + } +} diff --git a/app/Profile.php b/app/Profile.php index 3c4b27cea..b0bcd3da3 100644 --- a/app/Profile.php +++ b/app/Profile.php @@ -14,6 +14,11 @@ class Profile extends Model protected $visible = ['id', 'username', 'name']; + public function user() + { + return $this->belongsTo(User::class); + } + public function url($suffix = '') { return url($this->username . $suffix); diff --git a/app/Status.php b/app/Status.php index 68e65410a..dd9bf98c4 100644 --- a/app/Status.php +++ b/app/Status.php @@ -25,6 +25,9 @@ class Status extends Model public function thumb() { + if($this->media->count() == 0) { + return "data:image/gif;base64,R0lGODlhAQABAIAAAMLCwgAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=="; + } return url(Storage::url($this->firstMedia()->thumbnail_path)); } diff --git a/app/Util/Lexer/Autolink.php b/app/Util/Lexer/Autolink.php new file mode 100755 index 000000000..eb899dfd8 --- /dev/null +++ b/app/Util/Lexer/Autolink.php @@ -0,0 +1,771 @@ + + * @author Nick Pope + * @copyright Copyright © 2010, Mike Cochrane, Nick Pope + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 + * @package Twitter.Text + */ + +namespace App\Util\Lexer; + +use App\Util\Lexer\Regex; +use App\Util\Lexer\Extractor; +use App\Util\Lexer\StringUtils; + +/** + * Twitter Autolink 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 + * @author Nick Pope + * @copyright Copyright © 2010, Mike Cochrane, Nick Pope + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 + * @package Twitter.Text + */ +class Autolink extends Regex +{ + + /** + * CSS class for auto-linked URLs. + * + * @var string + */ + protected $class_url = ''; + + /** + * CSS class for auto-linked username URLs. + * + * @var string + */ + protected $class_user = 'u-url mention'; + + /** + * CSS class for auto-linked list URLs. + * + * @var string + */ + protected $class_list = 'u-url list-slug'; + + /** + * CSS class for auto-linked hashtag URLs. + * + * @var string + */ + protected $class_hash = 'u-url hashtag'; + + /** + * CSS class for auto-linked cashtag URLs. + * + * @var string + */ + protected $class_cash = 'u-url cashtag'; + + /** + * URL base for username links (the username without the @ will be appended). + * + * @var string + */ + protected $url_base_user = null; + + /** + * URL base for list links (the username/list without the @ will be appended). + * + * @var string + */ + protected $url_base_list = null; + + /** + * URL base for hashtag links (the hashtag without the # will be appended). + * + * @var string + */ + protected $url_base_hash = null; + + /** + * URL base for cashtag links (the hashtag without the $ will be appended). + * + * @var string + */ + protected $url_base_cash = null; + + /** + * Whether to include the value 'nofollow' in the 'rel' attribute. + * + * @var bool + */ + protected $nofollow = true; + + /** + * Whether to include the value 'noopener' in the 'rel' attribute. + * + * @var bool + */ + 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. + * + * @var bool + */ + protected $external = true; + + /** + * 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. + * + * @var string + */ + protected $target = '_blank'; + + /** + * attribute for invisible span tag + * + * @var string + */ + protected $invisibleTagAttrs = "style='position:absolute;left:-9999px;'"; + + /** + * + * @var Extractor + */ + protected $extractor = null; + + /** + * Provides fluent method chaining. + * + * @param string $tweet The tweet to be converted. + * @param bool $full_encode Whether to encode all special characters. + * + * @see __construct() + * + * @return Autolink + */ + 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() + * + * @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. + */ + 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(); + $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'; + } + + /** + * CSS class for auto-linked URLs. + * + * @return string CSS class for URL links. + */ + public function getURLClass() + { + return $this->class_url; + } + + /** + * CSS class for auto-linked URLs. + * + * @param string $v CSS class for URL links. + * + * @return Autolink Fluid method chaining. + */ + public function setURLClass($v) + { + $this->class_url = trim($v); + return $this; + } + + /** + * CSS class for auto-linked username URLs. + * + * @return string CSS class for username links. + */ + public function getUsernameClass() + { + return $this->class_user; + } + + /** + * CSS class for auto-linked username URLs. + * + * @param string $v CSS class for username links. + * + * @return Autolink Fluid method chaining. + */ + public function setUsernameClass($v) + { + $this->class_user = trim($v); + return $this; + } + + /** + * CSS class for auto-linked username/list URLs. + * + * @return string CSS class for username/list links. + */ + public function getListClass() + { + return $this->class_list; + } + + /** + * CSS class for auto-linked username/list URLs. + * + * @param string $v CSS class for username/list links. + * + * @return Autolink Fluid method chaining. + */ + public function setListClass($v) + { + $this->class_list = trim($v); + return $this; + } + + /** + * CSS class for auto-linked hashtag URLs. + * + * @return string CSS class for hashtag links. + */ + public function getHashtagClass() + { + return $this->class_hash; + } + + /** + * CSS class for auto-linked hashtag URLs. + * + * @param string $v CSS class for hashtag links. + * + * @return Autolink Fluid method chaining. + */ + public function setHashtagClass($v) + { + $this->class_hash = trim($v); + return $this; + } + + /** + * CSS class for auto-linked cashtag URLs. + * + * @return string CSS class for cashtag links. + */ + public function getCashtagClass() + { + return $this->class_cash; + } + + /** + * CSS class for auto-linked cashtag URLs. + * + * @param string $v CSS class for cashtag links. + * + * @return Autolink Fluid method chaining. + */ + public function setCashtagClass($v) + { + $this->class_cash = trim($v); + return $this; + } + + /** + * Whether to include the value 'nofollow' in the 'rel' attribute. + * + * @return bool Whether to add 'nofollow' to the 'rel' attribute. + */ + public function getNoFollow() + { + return $this->nofollow; + } + + /** + * Whether to include the value 'nofollow' in the 'rel' attribute. + * + * @param bool $v The value to add to the 'target' attribute. + * + * @return Autolink Fluid method chaining. + */ + public function setNoFollow($v) + { + $this->nofollow = $v; + 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. + * + * @return bool Whether to add 'external' to the 'rel' attribute. + */ + 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. + * + * @param bool $v The value to add to the 'target' attribute. + * + * @return Autolink Fluid method chaining. + */ + public function setExternal($v) + { + $this->external = $v; + 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. + * + * @return string The value to add to the 'target' attribute. + */ + 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. + * + * @param string $v The value to add to the 'target' attribute. + * + * @return Autolink Fluid method chaining. + */ + public function setTarget($v) + { + $this->target = trim($v); + return $this; + } + + /** + * Autolink with entities + * + * @param string $tweet + * @param array $entities + * @return string + * @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'])) { + $text .= StringUtils::substr($tweet, $beginIndex, $entity['indices'][0] - $beginIndex + 1); + } 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)); + return $text; + } + + /** + * Auto-link hashtags, URLs, usernames and lists, with JSON entities. + * + * @param string The tweet to be converted + * @param mixed The entities info + * @return string that auto-link HTML added + * @since 1.1.0 + */ + public function autoLinkWithJson($tweet = null, $json = null) + { + // concatenate entities + $entities = array(); + 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); + return $this->autoLinkEntities($tweet, $entities); + } + + /** + * convert Object to Array + * + * @param mixed $obj + * @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); + } + } + return $array; + } + + /** + * Auto-link hashtags, URLs, usernames and lists. + * + * @param string The tweet to be converted + * @return string that auto-link HTML added + * @since 1.1.0 + */ + public function autoLink($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + $entities = $this->extractor->extractURLWithoutProtocol(false)->extractEntitiesWithIndices($tweet); + 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 + * @since 1.1.0 + */ + public function autoLinkUsernamesAndLists($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + $entities = $this->extractor->extractMentionsOrListsWithIndices($tweet); + 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 + * @since 1.1.0 + */ + public function autoLinkHashtags($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + $entities = $this->extractor->extractHashtagsWithIndices($tweet); + return $this->autoLinkEntities($tweet, $entities); + } + + /** + * Auto-link URLs in the Tweet text provided. + *

+ * This only auto-links URLs with protocol. + * + * @return string that auto-link HTML added + * @since 1.1.0 + */ + public function autoLinkURLs($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + $entities = $this->extractor->extractURLWithoutProtocol(false)->extractURLsWithIndices($tweet); + 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 + * @since 1.1.0 + */ + public function autoLinkCashtags($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + $entities = $this->extractor->extractCashtagsWithIndices($tweet); + 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: + // + // … + // + // http://longdomai + // + // + // nname.com/foo + // + // + //   + // … + // + // + // 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 = "invisibleTagAttrs}>"; + + $linkText = "{$precedingEllipsis}{$invisibleSpan} "; + $linkText .= "{$invisibleSpan}{$this->escapeHTML($beforeDisplayURL)}"; + $linkText .= "{$this->escapeHTML($displayURLSansEllipses)}"; + $linkText .= "{$invisibleSpan}{$this->escapeHTML($afterDisplayURL)}"; + $linkText .= "{$invisibleSpan} {$followingEllipsis}"; + } 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 + * @return string + * @since 1.1.0 + */ + public function linkToHashtag($entity, $tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + $this->target = false; + $attributes = array(); + $class = array(); + $hash = StringUtils::substr($tweet, $entity['indices'][0], 1); + $linkText = $hash . $entity['hashtag']; + + $attributes['href'] = $this->url_base_hash . $entity['hashtag'] . '?src=hash'; + $attributes['title'] = '#' . $entity['hashtag']; + if (!empty($this->class_hash)) { + $class[] = $this->class_hash; + } + if (preg_match(self::$patterns['rtl_chars'], $linkText)) { + $class[] = 'rtl'; + } + if (!empty($class)) { + $attributes['class'] = join(' ', $class); + } + + return $this->linkToText($entity, $linkText, $attributes); + } + + /** + * + * @param array $entity + * @return string + * @since 1.1.0 + */ + public function linkToMentionAndList($entity) + { + $attributes = array(); + + if (!empty($entity['list_slug'])) { + # Replace the list and username + $linkText = $entity['screen_name'] . $entity['list_slug']; + $class = $this->class_list; + $url = $this->url_base_list . $linkText; + } else { + # Replace the username + $linkText = $entity['screen_name']; + $class = $this->class_user; + $url = $this->url_base_user . $linkText; + } + if (!empty($class)) { + $attributes['class'] = $class; + } + $attributes['href'] = $url; + + return $this->linkToText($entity, $linkText, $attributes); + } + + /** + * + * @param array $entity + * @param string $tweet + * @return string + * @since 1.1.0 + */ + public function linkToCashtag($entity, $tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + $attributes = array(); + $doller = StringUtils::substr($tweet, $entity['indices'][0], 1); + $linkText = $doller . $entity['cashtag']; + $attributes['href'] = $this->url_base_cash . $entity['cashtag']; + $attributes['title'] = $linkText; + if (!empty($this->class_cash)) { + $attributes['class'] = $this->class_cash; + } + + return $this->linkToText($entity, $linkText, $attributes); + } + + /** + * + * @param array $entity + * @param string $text + * @param array $attributes + * @return string + * @since 1.1.0 + */ + public function linkToText(array $entity, $text, $attributes = array()) + { + $rel = array(); + if ($this->external) { + $rel[] = 'external'; + } + if ($this->nofollow) { + $rel[] = 'nofollow'; + } + if ($this->noopener) { + $rel[] = 'noopener'; + } + if (!empty($rel)) { + $attributes['rel'] = join(' ', $rel); + } + if ($this->target) { + $attributes['target'] = $this->target; + } + $link = ' $val) { + $link .= ' ' . $key . '="' . $this->escapeHTML($val) . '"'; + } + $link .= '>' . $text . ''; + return $link; + } + + /** + * html escape + * + * @param string $text + * @return string + */ + protected function escapeHTML($text) + { + return htmlspecialchars($text, ENT_QUOTES, 'UTF-8', false); + } +} diff --git a/app/Util/Lexer/Extractor.php b/app/Util/Lexer/Extractor.php new file mode 100755 index 000000000..5a066985e --- /dev/null +++ b/app/Util/Lexer/Extractor.php @@ -0,0 +1,548 @@ + + * @author Nick Pope + * @copyright Copyright © 2010, Mike Cochrane, Nick Pope + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 + * @package Twitter.Text + */ + +namespace App\Util\Lexer; + +use App\Util\Lexer\Regex; +use App\Util\Lexer\StringUtils; + +/** + * Twitter Extractor Class + * + * Parses tweets and extracts 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 + * @author Nick Pope + * @copyright Copyright © 2010, Mike Cochrane, Nick Pope + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 + * @package Twitter.Text + */ +class Extractor extends Regex +{ + + /** + * @var boolean + */ + protected $extractURLWithoutProtocol = true; + + /** + * Provides fluent method chaining. + * + * @param string $tweet The tweet to be converted. + * + * @see __construct() + * + * @return Extractor + */ + public static function create($tweet = null) + { + return new self($tweet); + } + + /** + * Reads in a tweet to be parsed and extracts elements from it. + * + * Extracts various parts of a tweet including URLs, usernames, hashtags... + * + * @param string $tweet The tweet to extract. + */ + public function __construct($tweet = null) + { + parent::__construct($tweet); + } + + /** + * Extracts all parts of a tweet and returns an associative array containing + * the extracted elements. + * + * @param string $tweet The tweet to extract. + * @return array The elements in the tweet. + */ + public function extract($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + return array( + 'hashtags' => $this->extractHashtags($tweet), + 'urls' => $this->extractURLs($tweet), + 'mentions' => $this->extractMentionedUsernames($tweet), + 'replyto' => $this->extractRepliedUsernames($tweet), + 'hashtags_with_indices' => $this->extractHashtagsWithIndices($tweet), + 'urls_with_indices' => $this->extractURLsWithIndices($tweet), + 'mentions_with_indices' => $this->extractMentionedUsernamesWithIndices($tweet), + ); + } + + /** + * Extract URLs, @mentions, lists and #hashtag from a given text/tweet. + * + * @param string $tweet The tweet to extract. + * @return array list of extracted entities + */ + public function extractEntitiesWithIndices($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + $entities = array(); + $entities = array_merge($entities, $this->extractURLsWithIndices($tweet)); + $entities = array_merge($entities, $this->extractHashtagsWithIndices($tweet, false)); + $entities = array_merge($entities, $this->extractMentionsOrListsWithIndices($tweet)); + $entities = array_merge($entities, $this->extractCashtagsWithIndices($tweet)); + $entities = $this->removeOverlappingEntities($entities); + return $entities; + } + + /** + * Extracts all the hashtags from the tweet. + * + * @param string $tweet The tweet to extract. + * @return array The hashtag elements in the tweet. + */ + public function extractHashtags($tweet = null) + { + $hashtagsOnly = array(); + $hashtagsWithIndices = $this->extractHashtagsWithIndices($tweet); + + foreach ($hashtagsWithIndices as $hashtagWithIndex) { + $hashtagsOnly[] = $hashtagWithIndex['hashtag']; + } + return $hashtagsOnly; + } + + /** + * Extracts all the cashtags from the tweet. + * + * @param string $tweet The tweet to extract. + * @return array The cashtag elements in the tweet. + */ + public function extractCashtags($tweet = null) + { + $cashtagsOnly = array(); + $cashtagsWithIndices = $this->extractCashtagsWithIndices($tweet); + + foreach ($cashtagsWithIndices as $cashtagWithIndex) { + $cashtagsOnly[] = $cashtagWithIndex['cashtag']; + } + return $cashtagsOnly; + } + + /** + * Extracts all the URLs from the tweet. + * + * @param string $tweet The tweet to extract. + * @return array The URL elements in the tweet. + */ + public function extractURLs($tweet = null) + { + $urlsOnly = array(); + $urlsWithIndices = $this->extractURLsWithIndices($tweet); + + foreach ($urlsWithIndices as $urlWithIndex) { + $urlsOnly[] = $urlWithIndex['url']; + } + return $urlsOnly; + } + + /** + * Extract all the usernames from the tweet. + * + * A mention is an occurrence of a username anywhere in a tweet. + * + * @param string $tweet The tweet to extract. + * @return array The usernames elements in the tweet. + */ + public function extractMentionedScreennames($tweet = null) + { + $usernamesOnly = array(); + $mentionsWithIndices = $this->extractMentionsOrListsWithIndices($tweet); + + foreach ($mentionsWithIndices as $mentionWithIndex) { + $screen_name = mb_strtolower($mentionWithIndex['screen_name']); + if (empty($screen_name) OR in_array($screen_name, $usernamesOnly)) { + continue; + } + $usernamesOnly[] = $screen_name; + } + return $usernamesOnly; + } + + /** + * Extract all the usernames from the tweet. + * + * A mention is an occurrence of a username anywhere in a tweet. + * + * @return array The usernames elements in the tweet. + * @deprecated since version 1.1.0 + */ + public function extractMentionedUsernames($tweet) + { + $this->tweet = $tweet; + return $this->extractMentionedScreennames($tweet); + } + + /** + * Extract all the usernames replied to from the tweet. + * + * A reply is an occurrence of a username at the beginning of a tweet. + * + * @param string $tweet The tweet to extract. + * @return array The usernames replied to in a tweet. + */ + public function extractReplyScreenname($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + $matched = preg_match(self::$patterns['valid_reply'], $tweet, $matches); + # Check username ending in + if ($matched && preg_match(self::$patterns['end_mention_match'], $matches[2])) { + $matched = false; + } + return $matched ? $matches[1] : null; + } + + /** + * Extract all the usernames replied to from the tweet. + * + * A reply is an occurrence of a username at the beginning of a tweet. + * + * @return array The usernames replied to in a tweet. + * @deprecated since version 1.1.0 + */ + public function extractRepliedUsernames() + { + return $this->extractReplyScreenname(); + } + + /** + * Extracts all the hashtags and the indices they occur at from the tweet. + * + * @param string $tweet The tweet to extract. + * @param boolean $checkUrlOverlap if true, check if extracted hashtags overlap URLs and remove overlapping ones + * @return array The hashtag elements in the tweet. + */ + public function extractHashtagsWithIndices($tweet = null, $checkUrlOverlap = true) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + + if (!preg_match('/[##]/iu', $tweet)) { + return array(); + } + + preg_match_all(self::$patterns['valid_hashtag'], $tweet, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); + $tags = array(); + + foreach ($matches as $match) { + list($all, $before, $hash, $hashtag, $outer) = array_pad($match, 3, array('', 0)); + $start_position = $hash[1] > 0 ? StringUtils::strlen(substr($tweet, 0, $hash[1])) : $hash[1]; + $end_position = $start_position + StringUtils::strlen($hash[0] . $hashtag[0]); + + if (preg_match(self::$patterns['end_hashtag_match'], $outer[0])) { + continue; + } + + $tags[] = array( + 'hashtag' => $hashtag[0], + 'indices' => array($start_position, $end_position) + ); + } + + if (!$checkUrlOverlap) { + return $tags; + } + + # check url overlap + $urls = $this->extractURLsWithIndices($tweet); + $entities = $this->removeOverlappingEntities(array_merge($tags, $urls)); + + $validTags = array(); + foreach ($entities as $entity) { + if (empty($entity['hashtag'])) { + continue; + } + $validTags[] = $entity; + } + + return $validTags; + } + + /** + * Extracts all the cashtags and the indices they occur at from the tweet. + * + * @param string $tweet The tweet to extract. + * @return array The cashtag elements in the tweet. + */ + public function extractCashtagsWithIndices($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + + if (!preg_match('/\$/iu', $tweet)) { + return array(); + } + + preg_match_all(self::$patterns['valid_cashtag'], $tweet, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); + $tags = array(); + + foreach ($matches as $match) { + list($all, $before, $dollar, $cash_text, $outer) = array_pad($match, 3, array('', 0)); + $start_position = $dollar[1] > 0 ? StringUtils::strlen(substr($tweet, 0, $dollar[1])) : $dollar[1]; + $end_position = $start_position + StringUtils::strlen($dollar[0] . $cash_text[0]); + + if (preg_match(self::$patterns['end_hashtag_match'], $outer[0])) { + continue; + } + + $tags[] = array( + 'cashtag' => $cash_text[0], + 'indices' => array($start_position, $end_position) + ); + } + + return $tags; + } + + /** + * Extracts all the URLs and the indices they occur at from the tweet. + * + * @param string $tweet The tweet to extract. + * @return array The URLs elements in the tweet. + */ + public function extractURLsWithIndices($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + + $needle = $this->extractURLWithoutProtocol() ? '.' : ':'; + if (strpos($tweet, $needle) === false) { + return array(); + } + + $urls = array(); + preg_match_all(self::$patterns['valid_url'], $tweet, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); + + foreach ($matches as $match) { + list($all, $before, $url, $protocol, $domain, $port, $path, $query) = array_pad($match, 8, array('')); + $start_position = $url[1] > 0 ? StringUtils::strlen(substr($tweet, 0, $url[1])) : $url[1]; + $end_position = $start_position + StringUtils::strlen($url[0]); + + $all = $all[0]; + $before = $before[0]; + $url = $url[0]; + $protocol = $protocol[0]; + $domain = $domain[0]; + $port = $port[0]; + $path = $path[0]; + $query = $query[0]; + + // If protocol is missing and domain contains non-ASCII characters, + // extract ASCII-only domains. + if (empty($protocol)) { + if (!$this->extractURLWithoutProtocol || preg_match(self::$patterns['invalid_url_without_protocol_preceding_chars'], $before)) { + continue; + } + + $last_url = null; + $ascii_end_position = 0; + + if (preg_match(self::$patterns['valid_ascii_domain'], $domain, $asciiDomain)) { + $asciiDomain[0] = preg_replace('/' . preg_quote($domain, '/') . '/u', $asciiDomain[0], $url); + $ascii_start_position = StringUtils::strpos($domain, $asciiDomain[0], $ascii_end_position); + $ascii_end_position = $ascii_start_position + StringUtils::strlen($asciiDomain[0]); + $last_url = array( + 'url' => $asciiDomain[0], + 'indices' => array($start_position + $ascii_start_position, $start_position + $ascii_end_position), + ); + if (!empty($path) + || preg_match(self::$patterns['valid_special_short_domain'], $asciiDomain[0]) + || !preg_match(self::$patterns['invalid_short_domain'], $asciiDomain[0])) { + $urls[] = $last_url; + } + } + + // no ASCII-only domain found. Skip the entire URL + if (empty($last_url)) { + continue; + } + + // $last_url only contains domain. Need to add path and query if they exist. + if (!empty($path)) { + // last_url was not added. Add it to urls here. + $last_url['url'] = preg_replace('/' . preg_quote($domain, '/') . '/u', $last_url['url'], $url); + $last_url['indices'][1] = $end_position; + } + } else { + // In the case of t.co URLs, don't allow additional path characters + if (preg_match(self::$patterns['valid_tco_url'], $url, $tcoUrlMatches)) { + $url = $tcoUrlMatches[0]; + $end_position = $start_position + StringUtils::strlen($url); + } + $urls[] = array( + 'url' => $url, + 'indices' => array($start_position, $end_position), + ); + } + } + + return $urls; + } + + /** + * Extracts all the usernames and the indices they occur at from the tweet. + * + * @param string $tweet The tweet to extract. + * @return array The username elements in the tweet. + */ + public function extractMentionedScreennamesWithIndices($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + + $usernamesOnly = array(); + $mentions = $this->extractMentionsOrListsWithIndices($tweet); + foreach ($mentions as $mention) { + if (isset($mention['list_slug'])) { + unset($mention['list_slug']); + } + $usernamesOnly[] = $mention; + } + return $usernamesOnly; + } + + /** + * Extracts all the usernames and the indices they occur at from the tweet. + * + * @return array The username elements in the tweet. + * @deprecated since version 1.1.0 + */ + public function extractMentionedUsernamesWithIndices() + { + return $this->extractMentionedScreennamesWithIndices(); + } + + /** + * Extracts all the usernames and the indices they occur at from the tweet. + * + * @param string $tweet The tweet to extract. + * @return array The username elements in the tweet. + */ + public function extractMentionsOrListsWithIndices($tweet = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + + if (!preg_match('/[@@]/iu', $tweet)) { + return array(); + } + + preg_match_all(self::$patterns['valid_mentions_or_lists'], $tweet, $matches, PREG_SET_ORDER | PREG_OFFSET_CAPTURE); + $results = array(); + + foreach ($matches as $match) { + list($all, $before, $at, $username, $list_slug, $outer) = array_pad($match, 6, array('', 0)); + $start_position = $at[1] > 0 ? StringUtils::strlen(substr($tweet, 0, $at[1])) : $at[1]; + $end_position = $start_position + StringUtils::strlen($at[0]) + StringUtils::strlen($username[0]); + $entity = array( + 'screen_name' => $username[0], + 'list_slug' => $list_slug[0], + 'indices' => array($start_position, $end_position), + ); + + if (preg_match(self::$patterns['end_mention_match'], $outer[0])) { + continue; + } + + if (!empty($list_slug[0])) { + $entity['indices'][1] = $end_position + StringUtils::strlen($list_slug[0]); + } + + $results[] = $entity; + } + + return $results; + } + + /** + * Extracts all the usernames and the indices they occur at from the tweet. + * + * @return array The username elements in the tweet. + * @deprecated since version 1.1.0 + */ + public function extractMentionedUsernamesOrListsWithIndices() + { + return $this->extractMentionsOrListsWithIndices(); + } + + /** + * setter/getter for extractURLWithoutProtocol + * + * @param boolean $flag + * @return Extractor + */ + public function extractURLWithoutProtocol($flag = null) + { + if (is_null($flag)) { + return $this->extractURLWithoutProtocol; + } + $this->extractURLWithoutProtocol = (bool) $flag; + return $this; + } + + /** + * Remove overlapping entities. + * This returns a new array with no overlapping entities. + * + * @param array $entities + * @return array + */ + public function removeOverlappingEntities($entities) + { + $result = array(); + usort($entities, array($this, 'sortEntites')); + + $prev = null; + foreach ($entities as $entity) { + if (isset($prev) && $entity['indices'][0] < $prev['indices'][1]) { + continue; + } + $prev = $entity; + $result[] = $entity; + } + return $result; + } + + /** + * sort by entity start index + * + * @param array $a + * @param array $b + * @return int + */ + protected function sortEntites($a, $b) + { + if ($a['indices'][0] == $b['indices'][0]) { + return 0; + } + return ($a['indices'][0] < $b['indices'][0]) ? -1 : 1; + } +} diff --git a/app/Util/Lexer/HitHighlighter.php b/app/Util/Lexer/HitHighlighter.php new file mode 100755 index 000000000..77b56157a --- /dev/null +++ b/app/Util/Lexer/HitHighlighter.php @@ -0,0 +1,202 @@ + + * @copyright Copyright © 2010, Nick Pope + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 + * @package Twitter.Text + */ + +namespace App\Util\Lexer; + +use App\Util\Lexer\Regex; +use App\Util\Lexer\StringUtils; + +/** + * Twitter HitHighlighter Class + * + * Performs "hit highlighting" on tweets that have been auto-linked already. + * Useful with the results returned from the search API. + * + * 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 + * @copyright Copyright © 2010, Nick Pope + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 + * @package Twitter.Text + */ +class HitHighlighter extends Regex +{ + + /** + * The tag to surround hits with. + * + * @var string + */ + protected $tag = 'em'; + + /** + * Provides fluent method chaining. + * + * @param string $tweet The tweet to be hit highlighted. + * @param bool $full_encode Whether to encode all special characters. + * + * @see __construct() + * + * @return HitHighlighter + */ + public static function create($tweet = null, $full_encode = false) + { + return new self($tweet, $full_encode); + } + + /** + * Reads in a tweet to be parsed and hit highlighted. + * + * We take this opportunity to ensure that we escape user input. + * + * @see htmlspecialchars() + * + * @param string $tweet The tweet to be hit highlighted. + * @param bool $escape Whether to escape the tweet (default: true). + * @param bool $full_encode Whether to encode all special characters. + */ + public function __construct($tweet = null, $escape = true, $full_encode = false) + { + if (!empty($tweet) && $escape) { + 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); + } + } + + /** + * Set the highlighting tag to surround hits with. The default tag is 'em'. + * + * @return string The tag name. + */ + public function getTag() + { + return $this->tag; + } + + /** + * Set the highlighting tag to surround hits with. The default tag is 'em'. + * + * @param string $v The tag name. + * + * @return HitHighlighter Fluid method chaining. + */ + public function setTag($v) + { + $this->tag = $v; + return $this; + } + + /** + * Hit highlights the tweet. + * + * @param string $tweet The tweet to be hit highlighted. + * @param array $hits An array containing the start and end index pairs + * for the highlighting. + * @param bool $escape Whether to escape the tweet (default: true). + * @param bool $full_encode Whether to encode all special characters. + * + * @return string The hit highlighted tweet. + */ + public function highlight($tweet = null, array $hits = null) + { + if (is_null($tweet)) { + $tweet = $this->tweet; + } + if (empty($hits)) { + return $tweet; + } + $highlightTweet = ''; + $tags = array('<' . $this->tag . '>', 'tag . '>'); + # Check whether we can simply replace or whether we need to chunk... + if (strpos($tweet, '<') === false) { + $ti = 0; // tag increment (for added tags) + $highlightTweet = $tweet; + foreach ($hits as $hit) { + $highlightTweet = StringUtils::substrReplace($highlightTweet, $tags[0], $hit[0] + $ti, 0); + $ti += StringUtils::strlen($tags[0]); + $highlightTweet = StringUtils::substrReplace($highlightTweet, $tags[1], $hit[1] + $ti, 0); + $ti += StringUtils::strlen($tags[1]); + } + } else { + $chunks = preg_split('/[<>]/iu', $tweet); + $chunk = $chunks[0]; + $chunk_index = 0; + $chunk_cursor = 0; + $offset = 0; + $start_in_chunk = false; + # Flatten the multidimensional hits array: + $hits_flat = array(); + foreach ($hits as $hit) { + $hits_flat = array_merge($hits_flat, $hit); + } + # Loop over the hit indices: + for ($index = 0; $index < count($hits_flat); $index++) { + $hit = $hits_flat[$index]; + $tag = $tags[$index % 2]; + $placed = false; + while ($chunk !== null && $hit >= ($i = $offset + StringUtils::strlen($chunk))) { + $highlightTweet .= StringUtils::substr($chunk, $chunk_cursor); + if ($start_in_chunk && $hit === $i) { + $highlightTweet .= $tag; + $placed = true; + } + if (isset($chunks[$chunk_index + 1])) { + $highlightTweet .= '<' . $chunks[$chunk_index + 1] . '>'; + } + $offset += StringUtils::strlen($chunk); + $chunk_cursor = 0; + $chunk_index += 2; + $chunk = (isset($chunks[$chunk_index]) ? $chunks[$chunk_index] : null); + $start_in_chunk = false; + } + if (!$placed && $chunk !== null) { + $hit_spot = $hit - $offset; + $highlightTweet .= StringUtils::substr($chunk, $chunk_cursor, $hit_spot - $chunk_cursor) . $tag; + $chunk_cursor = $hit_spot; + $start_in_chunk = ($index % 2 === 0); + $placed = true; + } + # Ultimate fallback - hits that run off the end get a closing tag: + if (!$placed) { + $highlightTweet .= $tag; + } + } + if ($chunk !== null) { + if ($chunk_cursor < StringUtils::strlen($chunk)) { + $highlightTweet .= StringUtils::substr($chunk, $chunk_cursor); + } + for ($index = $chunk_index + 1; $index < count($chunks); $index++) { + $highlightTweet .= ($index % 2 === 0 ? $chunks[$index] : '<' . $chunks[$index] . '>'); + } + } + } + return $highlightTweet; + } + + /** + * Hit highlights the tweet. + * + * @param array $hits An array containing the start and end index pairs + * for the highlighting. + * + * @return string The hit highlighted tweet. + * @deprecated since version 1.1.0 + */ + public function addHitHighlighting(array $hits) + { + return $this->highlight($this->tweet, $hits); + } +} diff --git a/app/Util/Lexer/LooseAutolink.php b/app/Util/Lexer/LooseAutolink.php new file mode 100755 index 000000000..979b0d0b0 --- /dev/null +++ b/app/Util/Lexer/LooseAutolink.php @@ -0,0 +1,348 @@ + + * @author Nick Pope + * @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 + * @package Twitter.Text + */ + +namespace App\Util\Lexer; + +use App\Util\Lexer\Autolink; + +/** + * 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 + * @author Nick Pope + * @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 + * @package Twitter.Text + * @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. + *

+ * 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'], + array($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'], + array($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'], array($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'], + array($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 = 'external) { + $rel[] = 'external'; + } + if ($this->nofollow) { + $rel[] = 'nofollow'; + } + if (!empty($rel)) { + $link .= ' rel="' . implode(' ', $rel) . '"'; + } + if ($this->target) { + $link .= ' target="' . $this->target . '"'; + } + $link .= '>' . $element . ''; + 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 = 'external) { + $rel[] = 'external'; + } + if ($this->nofollow) { + $rel[] = 'nofollow'; + } + if (!empty($rel)) { + $link .= ' rel="' . implode(' ', $rel) . '"'; + } + if ($this->target) { + $link .= ' target="' . $this->target . '"'; + } + $link .= '>' . $element . ''; + 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('!\Aurl_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('!\Aurl_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); + } +} diff --git a/app/Util/Lexer/Regex.php b/app/Util/Lexer/Regex.php new file mode 100755 index 000000000..7c1f0627b --- /dev/null +++ b/app/Util/Lexer/Regex.php @@ -0,0 +1,337 @@ + + * @author Nick Pope + * @copyright Copyright © 2010, Mike Cochrane, Nick Pope + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 + * @package Twitter.Text + */ + +namespace App\Util\Lexer; + +/** + * Twitter Regex Abstract Class + * + * Used by subclasses that need to parse 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 Mike Cochrane + * @author Nick Pope + * @copyright Copyright © 2010, Mike Cochrane, Nick Pope + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 + * @package Twitter + */ +abstract class Regex +{ + + /** + * Contains all generated regular expressions. + * + * @var string The regex patterns. + */ + protected static $patterns = array(); + + /** + * The tweet to be used in parsing. This should be populated by the + * constructor of all subclasses. + * + * @var string + */ + protected $tweet = ''; + + /** + * This constructor is used to populate some variables. + * + * @param string $tweet The tweet to parse. + */ + protected function __construct($tweet = null) + { + $this->tweet = $tweet; + } + + /** + * Emulate a static initialiser while PHP doesn't have one. + */ + public static function __static() + { + # Check whether we have initialized the regular expressions: + static $initialized = false; + if ($initialized) { + return; + } + # Get a shorter reference to the regular expression array: + $re = & self::$patterns; + # Initialise local storage arrays: + $tmp = array(); + + # Expression to match whitespace characters. + # + # 0x0009-0x000D Cc # .. + # 0x0020 Zs # SPACE + # 0x0085 Cc # + # 0x00A0 Zs # NO-BREAK SPACE + # 0x1680 Zs # OGHAM SPACE MARK + # 0x180E Zs # MONGOLIAN VOWEL SEPARATOR + # 0x2000-0x200A Zs # EN QUAD..HAIR SPACE + # 0x2028 Zl # LINE SEPARATOR + # 0x2029 Zp # PARAGRAPH SEPARATOR + # 0x202F Zs # NARROW NO-BREAK SPACE + # 0x205F Zs # MEDIUM MATHEMATICAL SPACE + # 0x3000 Zs # IDEOGRAPHIC SPACE + $tmp['spaces'] = '\x{0009}-\x{000D}\x{0020}\x{0085}\x{00a0}\x{1680}\x{180E}\x{2000}-\x{200a}\x{2028}\x{2029}\x{202f}\x{205f}\x{3000}'; + + # Invalid Characters: + # 0xFFFE,0xFEFF # BOM + # 0xFFFF # Special + # 0x202A-0x202E # Directional change + $tmp['invalid_characters'] = '\x{202a}-\x{202e}\x{feff}\x{fffe}\x{ffff}'; + + # Expression to match at and hash sign characters: + $tmp['at_signs'] = '@@'; + $tmp['hash_signs'] = '##'; + + # Expression to match latin accented characters. + # + # 0x00C0-0x00D6 + # 0x00D8-0x00F6 + # 0x00F8-0x00FF + # 0x0100-0x024f + # 0x0253-0x0254 + # 0x0256-0x0257 + # 0x0259 + # 0x025b + # 0x0263 + # 0x0268 + # 0x026f + # 0x0272 + # 0x0289 + # 0x028b + # 0x02bb + # 0x0300-0x036f + # 0x1e00-0x1eff + # + # Excludes 0x00D7 - multiplication sign (confusable with 'x'). + # Excludes 0x00F7 - division sign. + $tmp['latin_accents'] = '\x{00c0}-\x{00d6}\x{00d8}-\x{00f6}\x{00f8}-\x{00ff}'; + $tmp['latin_accents'] .= '\x{0100}-\x{024f}\x{0253}-\x{0254}\x{0256}-\x{0257}'; + $tmp['latin_accents'] .= '\x{0259}\x{025b}\x{0263}\x{0268}\x{026f}\x{0272}\x{0289}\x{028b}\x{02bb}\x{0300}-\x{036f}\x{1e00}-\x{1eff}'; + + # Expression to match RTL characters. + # + # 0x0600-0x06FF Arabic + # 0x0750-0x077F Arabic Supplement + # 0x08A0-0x08FF Arabic Extended-A + # 0x0590-0x05FF Hebrew + # 0xFB50-0xFDFF Arabic Presentation Forms-A + # 0xFE70-0xFEFF Arabic Presentation Forms-B + $tmp['rtl_chars'] = '\x{0600}-\x{06ff}\x{0750}-\x{077f}\x{08a0}-\x{08ff}\x{0590}-\x{05ff}\x{fb50}-\x{fdff}\x{fe70}-\x{feff}'; + + $tmp['hashtag_letters'] = '\p{L}\p{M}'; + $tmp['hashtag_numerals'] = '\p{Nd}'; + # Hashtag special chars + # + # _ underscore + # 0x200c ZERO WIDTH NON-JOINER (ZWNJ) + # 0x200d ZERO WIDTH JOINER (ZWJ) + # 0xa67e CYRILLIC KAVYKA + # 0x05be HEBREW PUNCTUATION MAQAF + # 0x05f3 HEBREW PUNCTUATION GERESH + # 0x05f4 HEBREW PUNCTUATION GERSHAYIM + # 0xff5e FULLWIDTH TILDE + # 0x301c WAVE DASH + # 0x309b KATAKANA-HIRAGANA VOICED SOUND MARK + # 0x309c KATAKANA-HIRAGANA SEMI-VOICED SOUND MARK + # 0x30a0 KATAKANA-HIRAGANA DOUBLE HYPHEN + # 0x30fb KATAKANA MIDDLE DOT + # 0x3003 DITTO MARK + # 0x0f0b TIBETAN MARK INTERSYLLABIC TSHEG + # 0x0f0c TIBETAN MARK DELIMITER TSHEG BSTAR + # 0x00b7 MIDDLE DOT + $tmp['hashtag_special_chars'] = '_\x{200c}\x{200d}\x{a67e}\x{05be}\x{05f3}\x{05f4}\x{ff5e}\x{301c}\x{309b}\x{309c}\x{30a0}\x{30fb}\x{3003}\x{0f0b}\x{0f0c}\x{00b7}'; + $tmp['hashtag_letters_numerals_set'] = '[' . $tmp['hashtag_letters'] . $tmp['hashtag_numerals'] . $tmp['hashtag_special_chars'] . ']'; + $tmp['hashtag_letters_set'] = '[' . $tmp['hashtag_letters'] . ']'; + $tmp['hashtag_boundary'] = '(?:\A|\x{fe0e}|\x{fe0f}|[^&' . $tmp['hashtag_letters'] . $tmp['hashtag_numerals'] . $tmp['hashtag_special_chars'] . '])'; + $tmp['hashtag'] = '(' . $tmp['hashtag_boundary'] . ')(#|\x{ff03})(?!\x{fe0f}|\x{20e3})(' . $tmp['hashtag_letters_numerals_set'] . '*' . $tmp['hashtag_letters_set'] . $tmp['hashtag_letters_numerals_set'] . '*)'; + + $re['valid_hashtag'] = '/' . $tmp['hashtag'] . '(?=(.*|$))/iu'; + $re['end_hashtag_match'] = '/\A(?:[' . $tmp['hash_signs'] . ']|:\/\/)/u'; + + # XXX: PHP doesn't have Ruby's $' (dollar apostrophe) so we have to capture + # $after in the following regular expression. Note that we only use a + # look-ahead capture here and don't append $after when we return. + $tmp['valid_mention_preceding_chars'] = '([^a-zA-Z0-9_!#\$%&*@@\/]|^|(?:^|[^a-z0-9_+~.-])RT:?)'; + $re['valid_mentions_or_lists'] = '/' . $tmp['valid_mention_preceding_chars'] . '([' . $tmp['at_signs'] . '])([a-z0-9_]{1,20})(\/[a-z][a-z0-9_\-]{0,24})?(?=(.*|$))/iu'; + $re['valid_reply'] = '/^(?:[' . $tmp['spaces'] . '])*[' . $tmp['at_signs'] . ']([a-z0-9_]{1,20})(?=(.*|$))/iu'; + $re['end_mention_match'] = '/\A(?:[' . $tmp['at_signs'] . ']|[' . $tmp['latin_accents'] . ']|:\/\/)/iu'; + + # URL related hash regex collection + + $tmp['valid_url_preceding_chars'] = '(?:[^A-Z0-9_@@\$##\.' . $tmp['invalid_characters'] . ']|^)'; + + $tmp['domain_valid_chars'] = '0-9a-z' . $tmp['latin_accents']; + $tmp['valid_subdomain'] = '(?>(?:[' . $tmp['domain_valid_chars'] . '][' . $tmp['domain_valid_chars'] . '\-_]*)?[' . $tmp['domain_valid_chars'] . ']\.)'; + $tmp['valid_domain_name'] = '(?:(?:[' . $tmp['domain_valid_chars'] . '][' . $tmp['domain_valid_chars'] . '\-]*)?[' . $tmp['domain_valid_chars'] . ']\.)'; + $tmp['domain_valid_unicode_chars'] = '[^\p{P}\p{Z}\p{C}' . $tmp['invalid_characters'] . $tmp['spaces'] . ']'; + + $gTLD = 'abb|abbott|abogado|academy|accenture|accountant|accountants|aco|active|actor|ads|adult|aeg|aero|afl|agency|aig|airforce|airtel|allfinanz|alsace|amsterdam|android|apartments|app|aquarelle|archi|army|arpa|asia|associates|attorney|auction|audio|auto|autos|axa|azure|band|bank|bar|barcelona|barclaycard|barclays|bargains|bauhaus|bayern|bbc|bbva|bcn|beer|bentley|berlin|best|bet|bharti|bible|bid|bike|bing|bingo|bio|biz|black|blackfriday|bloomberg|blue|bmw|bnl|bnpparibas|boats|bond|boo|boots|boutique|bradesco|bridgestone|broker|brother|brussels|budapest|build|builders|business|buzz|bzh|cab|cafe|cal|camera|camp|cancerresearch|canon|capetown|capital|caravan|cards|care|career|careers|cars|cartier|casa|cash|casino|cat|catering|cba|cbn|ceb|center|ceo|cern|cfa|cfd|chanel|channel|chat|cheap|chloe|christmas|chrome|church|cisco|citic|city|claims|cleaning|click|clinic|clothing|cloud|club|coach|codes|coffee|college|cologne|com|commbank|community|company|computer|condos|construction|consulting|contractors|cooking|cool|coop|corsica|country|coupons|courses|credit|creditcard|cricket|crown|crs|cruises|cuisinella|cymru|cyou|dabur|dad|dance|date|dating|datsun|day|dclk|deals|degree|delivery|delta|democrat|dental|dentist|desi|design|dev|diamonds|diet|digital|direct|directory|discount|dnp|docs|dog|doha|domains|doosan|download|drive|durban|dvag|earth|eat|edu|education|email|emerck|energy|engineer|engineering|enterprises|epson|equipment|erni|esq|estate|eurovision|eus|events|everbank|exchange|expert|exposed|express|fage|fail|faith|family|fan|fans|farm|fashion|feedback|film|finance|financial|firmdale|fish|fishing|fit|fitness|flights|florist|flowers|flsmidth|fly|foo|football|forex|forsale|forum|foundation|frl|frogans|fund|furniture|futbol|fyi|gal|gallery|game|garden|gbiz|gdn|gent|genting|ggee|gift|gifts|gives|giving|glass|gle|global|globo|gmail|gmo|gmx|gold|goldpoint|golf|goo|goog|google|gop|gov|graphics|gratis|green|gripe|group|guge|guide|guitars|guru|hamburg|hangout|haus|healthcare|help|here|hermes|hiphop|hitachi|hiv|hockey|holdings|holiday|homedepot|homes|honda|horse|host|hosting|hoteles|hotmail|house|how|hsbc|ibm|icbc|ice|icu|ifm|iinet|immo|immobilien|industries|infiniti|info|ing|ink|institute|insure|int|international|investments|ipiranga|irish|ist|istanbul|itau|iwc|java|jcb|jetzt|jewelry|jlc|jll|jobs|joburg|jprs|juegos|kaufen|kddi|kim|kitchen|kiwi|koeln|komatsu|krd|kred|kyoto|lacaixa|lancaster|land|lasalle|lat|latrobe|law|lawyer|lds|lease|leclerc|legal|lexus|lgbt|liaison|lidl|life|lighting|limited|limo|link|live|lixil|loan|loans|lol|london|lotte|lotto|love|ltda|lupin|luxe|luxury|madrid|maif|maison|man|management|mango|market|marketing|markets|marriott|mba|media|meet|melbourne|meme|memorial|men|menu|miami|microsoft|mil|mini|mma|mobi|moda|moe|mom|monash|money|montblanc|mormon|mortgage|moscow|motorcycles|mov|movie|movistar|mtn|mtpc|museum|nadex|nagoya|name|navy|nec|net|netbank|network|neustar|new|news|nexus|ngo|nhk|nico|ninja|nissan|nokia|nra|nrw|ntt|nyc|office|okinawa|omega|one|ong|onl|online|ooo|oracle|orange|org|organic|osaka|otsuka|ovh|page|panerai|paris|partners|parts|party|pet|pharmacy|philips|photo|photography|photos|physio|piaget|pics|pictet|pictures|pink|pizza|place|play|plumbing|plus|pohl|poker|porn|post|praxi|press|pro|prod|productions|prof|properties|property|pub|qpon|quebec|racing|realtor|realty|recipes|red|redstone|rehab|reise|reisen|reit|ren|rent|rentals|repair|report|republican|rest|restaurant|review|reviews|rich|ricoh|rio|rip|rocks|rodeo|rsvp|ruhr|run|ryukyu|saarland|sakura|sale|samsung|sandvik|sandvikcoromant|sanofi|sap|sarl|saxo|sca|scb|schmidt|scholarships|school|schule|schwarz|science|scor|scot|seat|seek|sener|services|sew|sex|sexy|shiksha|shoes|show|shriram|singles|site|ski|sky|skype|sncf|soccer|social|software|sohu|solar|solutions|sony|soy|space|spiegel|spreadbetting|srl|starhub|statoil|studio|study|style|sucks|supplies|supply|support|surf|surgery|suzuki|swatch|swiss|sydney|systems|taipei|tatamotors|tatar|tattoo|tax|taxi|team|tech|technology|tel|telefonica|temasek|tennis|thd|theater|tickets|tienda|tips|tires|tirol|today|tokyo|tools|top|toray|toshiba|tours|town|toyota|toys|trade|trading|training|travel|trust|tui|ubs|university|uno|uol|vacations|vegas|ventures|vermögensberater|vermögensberatung|versicherung|vet|viajes|video|villas|vin|vision|vista|vistaprint|vlaanderen|vodka|vote|voting|voto|voyage|wales|walter|wang|watch|webcam|website|wed|wedding|weir|whoswho|wien|wiki|williamhill|win|windows|wine|wme|work|works|world|wtc|wtf|xbox|xerox|xin|xperia|xxx|xyz|yachts|yandex|yodobashi|yoga|yokohama|youtube|zip|zone|zuerich|дети|ком|москва|онлайн|орг|рус|сайт|קום|بازار|شبكة|كوم|موقع|कॉम|नेट|संगठन|คอม|みんな|グーグル|コム|世界|中信|中文网|企业|佛山|信息|健康|八卦|公司|公益|商城|商店|商标|在线|大拿|娱乐|工行|广东|慈善|我爱你|手机|政务|政府|新闻|时尚|机构|淡马锡|游戏|点看|移动|组织机构|网址|网店|网络|谷歌|集团|飞利浦|餐厅|닷넷|닷컴|삼성|onion'; + $ccTLD = '한국|香港|澳門|新加坡|台灣|台湾|中國|中国|გე|ไทย|ලංකා|ഭാരതം|ಭಾರತ|భారత్|சிங்கப்பூர்|இலங்கை|இந்தியா|ଭାରତ|ભારત|ਭਾਰਤ|ভাৰত|ভারত|বাংলা|भारोत|भारतम्|भारत|ڀارت|پاکستان|مليسيا|مصر|قطر|فلسطين|عمان|عراق|سورية|سودان|تونس|بھارت|بارت|ایران|امارات|المغرب|السعودية|الجزائر|الاردن|հայ|қаз|укр|срб|рф|мон|мкд|ею|бел|бг|ελ|zw|zm|za|yt|ye|ws|wf|vu|vn|vi|vg|ve|vc|va|uz|uy|us|um|uk|ug|ua|tz|tw|tv|tt|tr|tp|to|tn|tm|tl|tk|tj|th|tg|tf|td|tc|sz|sy|sx|sv|su|st|ss|sr|so|sn|sm|sl|sk|sj|si|sh|sg|se|sd|sc|sb|sa|rw|ru|rs|ro|re|qa|py|pw|pt|ps|pr|pn|pm|pl|pk|ph|pg|pf|pe|pa|om|nz|nu|nr|np|no|nl|ni|ng|nf|ne|nc|na|mz|my|mx|mw|mv|mu|mt|ms|mr|mq|mp|mo|mn|mm|ml|mk|mh|mg|mf|me|md|mc|ma|ly|lv|lu|lt|ls|lr|lk|li|lc|lb|la|kz|ky|kw|kr|kp|kn|km|ki|kh|kg|ke|jp|jo|jm|je|it|is|ir|iq|io|in|im|il|ie|id|hu|ht|hr|hn|hm|hk|gy|gw|gu|gt|gs|gr|gq|gp|gn|gm|gl|gi|gh|gg|gf|ge|gd|gb|ga|fr|fo|fm|fk|fj|fi|eu|et|es|er|eh|eg|ee|ec|dz|do|dm|dk|dj|de|cz|cy|cx|cw|cv|cu|cr|co|cn|cm|cl|ck|ci|ch|cg|cf|cd|cc|ca|bz|by|bw|bv|bt|bs|br|bq|bo|bn|bm|bl|bj|bi|bh|bg|bf|be|bd|bb|ba|az|ax|aw|au|at|as|ar|aq|ao|an|am|al|ai|ag|af|ae|ad|ac'; + + $tmp['valid_gTLD'] = '(?:(?:' . $gTLD . ')(?=[^0-9a-z@]|$))'; + $tmp['valid_ccTLD'] = '(?:(?:' . $ccTLD . ')(?=[^0-9a-z@]|$))'; + $tmp['valid_special_ccTLD'] = '(?:(?:' . 'co|tv' . ')(?=[^0-9a-z@]|$))'; + $tmp['valid_punycode'] = '(?:xn--[0-9a-z]+)'; + + $tmp['valid_domain'] = '(?:' // subdomains + domain + TLD + . $tmp['valid_subdomain'] . '+' . $tmp['valid_domain_name'] // e.g. www.twitter.com, foo.co.jp, bar.co.uk + . '(?:' . $tmp['valid_gTLD'] . '|' . $tmp['valid_ccTLD'] . '|' . $tmp['valid_punycode'] . '))' + . '|(?:' // domain + gTLD | some ccTLD + . $tmp['valid_domain_name'] // e.g. twitter.com + . '(?:' . $tmp['valid_gTLD'] . '|' . $tmp['valid_punycode'] . '|' . $tmp['valid_special_ccTLD'] . ')' + . ')' + . '|(?:(?:(?<=http:\/\/)|(?<=https:\/\/))' + . '(?:' + . '(?:' . $tmp['valid_domain_name'] . $tmp['valid_ccTLD'] . ')' // protocol + domain + ccTLD + . '|(?:' // protocol + unicode domain + TLD + . $tmp['domain_valid_unicode_chars'] . '+\.' + . '(?:' . $tmp['valid_gTLD'] . '|' . $tmp['valid_ccTLD'] . ')' + . ')' + . ')' + . ')' + . '|(?:' // domain + ccTLD + '/' + . $tmp['valid_domain_name'] . $tmp['valid_ccTLD'] . '(?=\/)' // e.g. t.co/ + . ')'; + # Used by the extractor: + $re['valid_ascii_domain'] = '/' . $tmp['valid_subdomain'] . '*' . $tmp['valid_domain_name'] . '(?:' . $tmp['valid_gTLD'] . '|' . $tmp['valid_ccTLD'] . '|' . $tmp['valid_punycode'] . ')/iu'; + + # Used by the extractor for stricter t.co URL extraction: + $re['valid_tco_url'] = '/^https?:\/\/t\.co\/[a-z0-9]+/iu'; + + # Used by the extractor to filter out unwanted URLs: + $re['invalid_short_domain'] = '/\A' . $tmp['valid_domain_name'] . $tmp['valid_ccTLD'] . '\Z/iu'; + $re['valid_special_short_domain'] = '/\A' . $tmp['valid_domain_name'] . $tmp['valid_special_ccTLD'] . '\Z/iu'; + $re['invalid_url_without_protocol_preceding_chars'] = '/[\-_.\/]\z/iu'; + + $tmp['valid_port_number'] = '[0-9]+'; + + $tmp['valid_general_url_path_chars'] = '[a-z\p{Cyrillic}0-9!\*;:=\+\,\.\$\/%#\[\]\-_~&|@' . $tmp['latin_accents'] . ']'; + # Allow URL paths to contain up to two nested levels of balanced parentheses: + # 1. Used in Wikipedia URLs, e.g. /Primer_(film) + # 2. Used in IIS sessions, e.g. /S(dfd346)/ + # 3. Used in Rdio URLs like /track/We_Up_(Album_Version_(Edited))/ + $tmp['valid_url_balanced_parens'] = '(?:\(' + . '(?:' . $tmp['valid_general_url_path_chars'] . '+' + . '|' + // allow one nested level of balanced parentheses + . '(?:' + . $tmp['valid_general_url_path_chars'] . '*' + . '\(' . $tmp['valid_general_url_path_chars'] . '+' . '\)' + . $tmp['valid_general_url_path_chars'] . '*' + . ')' + . ')' + . '\))'; + # Valid end-of-path characters (so /foo. does not gobble the period). + # 1. Allow =&# for empty URL parameters and other URL-join artifacts. + $tmp['valid_url_path_ending_chars'] = '[a-z\p{Cyrillic}0-9=_#\/\+\-' . $tmp['latin_accents'] . ']|(?:' . $tmp['valid_url_balanced_parens'] . ')'; + $tmp['valid_url_path'] = '(?:(?:' + . $tmp['valid_general_url_path_chars'] . '*(?:' + . $tmp['valid_url_balanced_parens'] . ' ' + . $tmp['valid_general_url_path_chars'] . '*)*' + . $tmp['valid_url_path_ending_chars'] . ')|(?:@' + . $tmp['valid_general_url_path_chars'] . '+\/))'; + + $tmp['valid_url_query_chars'] = '[a-z0-9!?\*\'\(\);:&=\+\$\/%#\[\]\-_\.,~|@]'; + $tmp['valid_url_query_ending_chars'] = '[a-z0-9_&=#\/\-]'; + + $re['valid_url'] = '/(?:' # $1 Complete match (preg_match() already matches everything.) + . '(' . $tmp['valid_url_preceding_chars'] . ')' # $2 Preceding characters + . '(' # $3 Complete URL + . '(https?:\/\/)?' # $4 Protocol (optional) + . '(' . $tmp['valid_domain'] . ')' # $5 Domain(s) + . '(?::(' . $tmp['valid_port_number'] . '))?' # $6 Port number (optional) + . '(\/' . $tmp['valid_url_path'] . '*)?' # $7 URL Path + . '(\?' . $tmp['valid_url_query_chars'] . '*' . $tmp['valid_url_query_ending_chars'] . ')?' # $8 Query String + . ')' + . ')/iux'; + + $tmp['cash_signs'] = '\$'; + $tmp['cashtag'] = '[a-z]{1,6}(?:[._][a-z]{1,2})?'; + $re['valid_cashtag'] = '/(^|[' . $tmp['spaces'] . '])([' . $tmp['cash_signs'] . '])(' . $tmp['cashtag'] . ')(?=($|\s|[[:punct:]]))/iu'; + $re['end_cashtag_match'] = '/\A(?:[' . $tmp['cash_signs'] . ']|:\/\/)/u'; + + # These URL validation pattern strings are based on the ABNF from RFC 3986 + $tmp['validate_url_unreserved'] = '[a-z\p{Cyrillic}0-9\-._~]'; + $tmp['validate_url_pct_encoded'] = '(?:%[0-9a-f]{2})'; + $tmp['validate_url_sub_delims'] = '[!$&\'()*+,;=]'; + $tmp['validate_url_pchar'] = '(?:' . $tmp['validate_url_unreserved'] . '|' . $tmp['validate_url_pct_encoded'] . '|' . $tmp['validate_url_sub_delims'] . '|[:\|@])'; #/iox + + $tmp['validate_url_userinfo'] = '(?:' . $tmp['validate_url_unreserved'] . '|' . $tmp['validate_url_pct_encoded'] . '|' . $tmp['validate_url_sub_delims'] . '|:)*'; #/iox + + $tmp['validate_url_dec_octet'] = '(?:[0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])'; #/i + $tmp['validate_url_ipv4'] = '(?:' . $tmp['validate_url_dec_octet'] . '(?:\.' . $tmp['validate_url_dec_octet'] . '){3})'; #/iox + # Punting on real IPv6 validation for now + $tmp['validate_url_ipv6'] = '(?:\[[a-f0-9:\.]+\])'; #/i + # Also punting on IPvFuture for now + $tmp['validate_url_ip'] = '(?:' . $tmp['validate_url_ipv4'] . '|' . $tmp['validate_url_ipv6'] . ')'; #/iox + # This is more strict than the rfc specifies + $tmp['validate_url_subdomain_segment'] = '(?:[a-z0-9](?:[a-z0-9_\-]*[a-z0-9])?)'; #/i + $tmp['validate_url_domain_segment'] = '(?:[a-z0-9](?:[a-z0-9\-]*[a-z0-9])?)'; #/i + $tmp['validate_url_domain_tld'] = '(?:[a-z](?:[a-z0-9\-]*[a-z0-9])?)'; #/i + $tmp['validate_url_domain'] = '(?:(?:' . $tmp['validate_url_subdomain_segment'] . '\.)*(?:' . $tmp['validate_url_domain_segment'] . '\.)' . $tmp['validate_url_domain_tld'] . ')'; #/iox + + $tmp['validate_url_host'] = '(?:' . $tmp['validate_url_ip'] . '|' . $tmp['validate_url_domain'] . ')'; #/iox + # Unencoded internationalized domains - this doesn't check for invalid UTF-8 sequences + $tmp['validate_url_unicode_subdomain_segment'] = '(?:(?:[a-z0-9]|[^\x00-\x7f])(?:(?:[a-z0-9_\-]|[^\x00-\x7f])*(?:[a-z0-9]|[^\x00-\x7f]))?)'; #/ix + $tmp['validate_url_unicode_domain_segment'] = '(?:(?:[a-z0-9]|[^\x00-\x7f])(?:(?:[a-z0-9\-]|[^\x00-\x7f])*(?:[a-z0-9]|[^\x00-\x7f]))?)'; #/ix + $tmp['validate_url_unicode_domain_tld'] = '(?:(?:[a-z]|[^\x00-\x7f])(?:(?:[a-z0-9\-]|[^\x00-\x7f])*(?:[a-z0-9]|[^\x00-\x7f]))?)'; #/ix + $tmp['validate_url_unicode_domain'] = '(?:(?:' . $tmp['validate_url_unicode_subdomain_segment'] . '\.)*(?:' . $tmp['validate_url_unicode_domain_segment'] . '\.)' . $tmp['validate_url_unicode_domain_tld'] . ')'; #/iox + + $tmp['validate_url_unicode_host'] = '(?:' . $tmp['validate_url_ip'] . '|' . $tmp['validate_url_unicode_domain'] . ')'; #/iox + + $tmp['validate_url_port'] = '[0-9]{1,5}'; + + $re['validate_url_unicode_authority'] = '/' + . '(?:(' . $tmp['validate_url_userinfo'] . ')@)?' # $1 userinfo + . '(' . $tmp['validate_url_unicode_host'] . ')' # $2 host + . '(?::(' . $tmp['validate_url_port'] . '))?' # $3 port + . '/iux'; + + $re['validate_url_authority'] = '/' + . '(?:(' . $tmp['validate_url_userinfo'] . ')@)?' # $1 userinfo + . '(' . $tmp['validate_url_host'] . ')' # $2 host + . '(?::(' . $tmp['validate_url_port'] . '))?' # $3 port + . '/ix'; + + $re['validate_url_scheme'] = '/(?:[a-z][a-z0-9+\-.]*)/i'; + $re['validate_url_path'] = '/(\/' . $tmp['validate_url_pchar'] . '*)*/iu'; + $re['validate_url_query'] = '/(' . $tmp['validate_url_pchar'] . '|\/|\?)*/iu'; + $re['validate_url_fragment'] = '/(' . $tmp['validate_url_pchar'] . '|\/|\?)*/iu'; + + # Modified version of RFC 3986 Appendix B + $re['validate_url_unencoded'] = '/^' # Full URL + . '(?:' + . '([^:\/?#]+):\/\/' # $1 Scheme + . ')?' + . '([^\/?#]*)' # $2 Authority + . '([^?#]*)' # $3 Path + . '(?:' + . '\?([^#]*)' # $4 Query + . ')?' + . '(?:' + . '\#(.*)' # $5 Fragment + . ')?$/iux'; + + $re['invalid_characters'] = '/[' . $tmp['invalid_characters'] . ']/u'; + + $re['rtl_chars'] = '/[' . $tmp['rtl_chars'] . ']/iu'; + + # Flag that initialization is complete: + $initialized = true; + } +} + +# Cause regular expressions to be initialized as soon as this file is loaded: +Regex::__static(); diff --git a/app/Util/Lexer/StringUtils.php b/app/Util/Lexer/StringUtils.php new file mode 100755 index 000000000..88722d158 --- /dev/null +++ b/app/Util/Lexer/StringUtils.php @@ -0,0 +1,104 @@ + $string_length) { + $start = $string_length; + } + if ($length < 0) { + $length = max(0, $string_length - $start + $length); + } elseif ((is_null($length) === true) || ($length > $string_length)) { + $length = $string_length; + } + if (($start + $length) > $string_length) { + $length = $string_length - $start; + } + + $suffixOffset = $start + $length; + $suffixLength = $string_length - $start - $length; + return static::substr($string, 0, $start, $encoding) . $replacement . static::substr($string, $suffixOffset, $suffixLength, $encoding); + } + return (is_null($length) === true) ? substr_replace($string, $replacement, $start) : substr_replace($string, $replacement, $start, $length); + } +} diff --git a/app/Util/Lexer/Validator.php b/app/Util/Lexer/Validator.php new file mode 100755 index 000000000..ddfb2c5f6 --- /dev/null +++ b/app/Util/Lexer/Validator.php @@ -0,0 +1,388 @@ + + * @copyright Copyright © 2010, Nick Pope + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 + * @package Twitter.Text + */ + +namespace App\Util\Lexer; + +use App\Util\Lexer\Regex; +use App\Util\Lexer\Extractor; +use App\Util\Lexer\StringUtils; + +/** + * 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 + * @copyright Copyright © 2010, Nick Pope + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License v2.0 + * @package Twitter.Text + */ +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 boolean 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 boolean 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 boolean 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 boolean 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 boolean 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 boolean 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 boolean 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 boolean 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 boolean $unicode_domains Consider the domain to be unicode. + * @param boolean $require_protocol Require a protocol for valid domain? + * + * @return boolean 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 boolean $unicode_domains Consider the domain to be unicode. + * @param boolean $require_protocol Require a protocol for valid domain? + * + * @return boolean 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 boolean $optional Whether a match is compulsory or not. + * + * @return boolean 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)); + } + } +} diff --git a/database/migrations/2018_06_08_003624_create_mentions_table.php b/database/migrations/2018_06_08_003624_create_mentions_table.php new file mode 100644 index 000000000..2f43d1bae --- /dev/null +++ b/database/migrations/2018_06_08_003624_create_mentions_table.php @@ -0,0 +1,34 @@ +bigIncrements('id'); + $table->bigInteger('status_id')->unsigned(); + $table->bigInteger('profile_id')->unsigned(); + $table->boolean('local')->default(true); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('mentions'); + } +} diff --git a/public/css/app.css b/public/css/app.css index b91bdbc25..4fc774d19 100644 Binary files a/public/css/app.css and b/public/css/app.css differ diff --git a/public/js/activity.js b/public/js/activity.js new file mode 100644 index 000000000..be031f9af Binary files /dev/null and b/public/js/activity.js differ diff --git a/public/js/timeline.js b/public/js/timeline.js index 1faa1ef12..4b14bcf4c 100644 Binary files a/public/js/timeline.js and b/public/js/timeline.js differ diff --git a/public/mix-manifest.json b/public/mix-manifest.json index 6b221b798..a0fc846da 100644 Binary files a/public/mix-manifest.json and b/public/mix-manifest.json differ diff --git a/resources/assets/js/activity.js b/resources/assets/js/activity.js new file mode 100644 index 000000000..b1b72d1b8 --- /dev/null +++ b/resources/assets/js/activity.js @@ -0,0 +1,10 @@ +$(document).ready(function() { + $('.pagination').hide(); + let elem = document.querySelector('.notification-page .list-group'); + let infScroll = new InfiniteScroll( elem, { + path: '.pagination__next', + append: '.notification-page .list-group', + status: '.page-load-status', + history: true, + }); +}); diff --git a/resources/assets/sass/_variables.scss b/resources/assets/sass/_variables.scss index f4a958a78..46639fe6b 100644 --- a/resources/assets/sass/_variables.scss +++ b/resources/assets/sass/_variables.scss @@ -6,3 +6,17 @@ $body-bg: #f5f8fa; $font-family-sans-serif:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif; $font-size-base: 0.9rem; $line-height-base: 1.6; + +$font-size-lg: ($font-size-base * 1.25); +$font-size-sm: ($font-size-base * .875); +$input-height: 2.375rem; +$input-height-sm: 1.9375rem; +$input-height-lg: 3rem; +$input-btn-focus-width: .2rem; +$custom-control-indicator-bg: #dee2e6; +$custom-control-indicator-disabled-bg: #e9ecef; +$custom-control-description-disabled-color: #868e96; +$white: white; +$theme-colors: ( + 'primary': #08d +); \ No newline at end of file diff --git a/resources/assets/sass/app.scss b/resources/assets/sass/app.scss index 235957bce..a9369498b 100644 --- a/resources/assets/sass/app.scss +++ b/resources/assets/sass/app.scss @@ -13,4 +13,6 @@ @import "components/typeahead"; -@import "components/notifications"; \ No newline at end of file +@import "components/notifications"; + +@import "components/switch"; \ No newline at end of file diff --git a/resources/assets/sass/components/switch.scss b/resources/assets/sass/components/switch.scss new file mode 100644 index 000000000..6ddb47241 --- /dev/null +++ b/resources/assets/sass/components/switch.scss @@ -0,0 +1,152 @@ +$switch-height: calc(#{$input-height} * .8) !default; +$switch-height-sm: calc(#{$input-height-sm} * .8) !default; +$switch-height-lg: calc(#{$input-height-lg} * .8) !default; +$switch-border-radius: $switch-height !default; +$switch-bg: $custom-control-indicator-bg !default; +$switch-checked-bg: map-get($theme-colors, 'danger') !default; +$switch-disabled-bg: $custom-control-indicator-disabled-bg !default; +$switch-disabled-color: $custom-control-description-disabled-color !default; +$switch-thumb-bg: $white !default; +$switch-thumb-border-radius: 50% !default; +$switch-thumb-padding: 2px !default; +$switch-focus-box-shadow: 0 0 0 $input-btn-focus-width rgba(map-get($theme-colors, 'primary'), .25); +$switch-transition: .2s all !default; + +.switch { + font-size: $font-size-base; + position: relative; + + input { + position: absolute; + height: 1px; + width: 1px; + background: none; + border: 0; + clip: rect(0 0 0 0); + clip-path: inset(50%); + overflow: hidden; + padding: 0; + + + label { + position: relative; + min-width: calc(#{$switch-height} * 2); + border-radius: $switch-border-radius; + height: $switch-height; + line-height: $switch-height; + display: inline-block; + cursor: pointer; + outline: none; + user-select: none; + vertical-align: middle; + text-indent: calc(calc(#{$switch-height} * 2) + .5rem); + } + + + label::before, + + label::after { + content: ''; + position: absolute; + top: 0; + left: 0; + width: calc(#{$switch-height} * 2); + bottom: 0; + display: block; + } + + + label::before { + right: 0; + background-color: $switch-bg; + border-radius: $switch-border-radius; + transition: $switch-transition; + } + + + label::after { + top: $switch-thumb-padding; + left: $switch-thumb-padding; + width: calc(#{$switch-height} - calc(#{$switch-thumb-padding} * 2)); + height: calc(#{$switch-height} - calc(#{$switch-thumb-padding} * 2)); + border-radius: $switch-thumb-border-radius; + background-color: $switch-thumb-bg; + transition: $switch-transition; + } + + &:checked + label::before { + background-color: $switch-checked-bg; + } + + &:checked + label::after { + margin-left: $switch-height; + } + + &:focus + label::before { + outline: none; + box-shadow: $switch-focus-box-shadow; + } + + &:disabled + label { + color: $switch-disabled-color; + cursor: not-allowed; + } + + &:disabled + label::before { + background-color: $switch-disabled-bg; + } + } + + // Small variation + &.switch-sm { + font-size: $font-size-sm; + + input { + + label { + min-width: calc(#{$switch-height-sm} * 2); + height: $switch-height-sm; + line-height: $switch-height-sm; + text-indent: calc(calc(#{$switch-height-sm} * 2) + .5rem); + } + + + label::before { + width: calc(#{$switch-height-sm} * 2); + } + + + label::after { + width: calc(#{$switch-height-sm} - calc(#{$switch-thumb-padding} * 2)); + height: calc(#{$switch-height-sm} - calc(#{$switch-thumb-padding} * 2)); + } + + &:checked + label::after { + margin-left: $switch-height-sm; + } + } + } + + // Large variation + &.switch-lg { + font-size: $font-size-lg; + + input { + + label { + min-width: calc(#{$switch-height-lg} * 2); + height: $switch-height-lg; + line-height: $switch-height-lg; + text-indent: calc(calc(#{$switch-height-lg} * 2) + .5rem); + } + + + label::before { + width: calc(#{$switch-height-lg} * 2); + } + + + label::after { + width: calc(#{$switch-height-lg} - calc(#{$switch-thumb-padding} * 2)); + height: calc(#{$switch-height-lg} - calc(#{$switch-thumb-padding} * 2)); + } + + &:checked + label::after { + margin-left: $switch-height-lg; + } + } + } + + + .switch { + margin-left: 1rem; + } +} diff --git a/resources/assets/sass/custom.scss b/resources/assets/sass/custom.scss index 33a15a998..551d2571f 100644 --- a/resources/assets/sass/custom.scss +++ b/resources/assets/sass/custom.scss @@ -10,6 +10,7 @@ body { #content { margin-bottom: auto !important; } + body, button, input, textarea { font-family: -apple-system,BlinkMacSystemFont,"Segoe UI", Roboto,Helvetica,Arial,sans-serif; @@ -203,3 +204,33 @@ body, button, input, textarea { height: .25rem; animation: loading-bar 3s linear infinite; } + +.max-hide-overflow { + max-height: 500px; + overflow-y: hidden; +} + +@media (min-width: map-get($grid-breakpoints, "xs")) { + .max-hide-overflow { + max-height: 600px!important; + } +} + +@media (min-width: map-get($grid-breakpoints, "md")) { + .max-hide-overflow { + max-height: 800px!important; + } +} + +@media (min-width: map-get($grid-breakpoints, "xl")) { + .max-hide-overflow { + max-height: 1000px!important; + } +} + +.notification-image { + background-size: cover; + width: 32px; + height: 32px; + background-position: 50%; +} diff --git a/resources/lang/en/navmenu.php b/resources/lang/en/navmenu.php new file mode 100644 index 000000000..427dc9dc8 --- /dev/null +++ b/resources/lang/en/navmenu.php @@ -0,0 +1,13 @@ + 'View my profile', + 'myTimeline' => 'My Timeline', + 'publicTimeline' => 'Public Timeline', + 'remoteFollow' => 'Remote Follow', + 'settings' => 'Settings', + 'admin' => 'Admin', + 'logout' => 'Logout', + +]; \ No newline at end of file diff --git a/resources/lang/en/notification.php b/resources/lang/en/notification.php index 2f85c4386..71d87a131 100644 --- a/resources/lang/en/notification.php +++ b/resources/lang/en/notification.php @@ -5,5 +5,6 @@ return [ 'likedPhoto' => 'liked your photo.', 'startedFollowingYou' => 'started following you.', 'commented' => 'commented on your post.', + 'mentionedYou' => 'mentioned you.' ]; \ No newline at end of file diff --git a/resources/views/account/activity.blade.php b/resources/views/account/activity.blade.php index 1b46263ae..dec6e16a8 100644 --- a/resources/views/account/activity.blade.php +++ b/resources/views/account/activity.blade.php @@ -19,7 +19,7 @@ {{$notification->created_at->diffForHumans(null, true, true, true)}} - @if($notification->item_id) + @if($notification->item_id && $notification->item_type == 'App\Status') @endif @@ -54,7 +54,32 @@ @if($notification->item_id) - + +

+ + @endif + + @break + + @case('mention') + + + + + {!! $notification->rendered !!} + {{$notification->created_at->diffForHumans(null, true, true, true)}} + + + @if($notification->item_id && $notification->item_type === 'App\Status') + @if(is_null($notification->status->in_reply_to_id)) + +
+
+ @else + +
+
+ @endif @endif
@break @@ -62,12 +87,20 @@ @endswitch @endforeach + + +
+ {{$notifications->links()}} +
@else
No unread notifications found.
@endif - @endsection + +@push('scripts') + +@endpush diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index 83a10462a..b07d4a3f3 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -20,11 +20,9 @@ + - - - @stack('styles') diff --git a/resources/views/layouts/partial/footer.blade.php b/resources/views/layouts/partial/footer.blade.php index b1c9d8944..90d9c07ce 100644 --- a/resources/views/layouts/partial/footer.blade.php +++ b/resources/views/layouts/partial/footer.blade.php @@ -1,17 +1,19 @@ diff --git a/resources/views/layouts/partial/nav.blade.php b/resources/views/layouts/partial/nav.blade.php index bcb4bfbd2..b2280fa57 100644 --- a/resources/views/layouts/partial/nav.blade.php +++ b/resources/views/layouts/partial/nav.blade.php @@ -1,6 +1,6 @@