From 022c1ceb61ae768e020a1ddd95a0ec0369c28290 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Mon, 3 Jun 2013 21:41:00 +0200 Subject: [PATCH] Moved code from com.woltlab.wbb into com.woltlab.wcf --- .../files/lib/util/MessageUtil.class.php | 67 ++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/wcfsetup/install/files/lib/util/MessageUtil.class.php b/wcfsetup/install/files/lib/util/MessageUtil.class.php index 834ab377ee..659cda9e20 100644 --- a/wcfsetup/install/files/lib/util/MessageUtil.class.php +++ b/wcfsetup/install/files/lib/util/MessageUtil.class.php @@ -7,7 +7,7 @@ use wcf\system\Regex; * Contains message-related functions. * * @author Marcel Werk - * @copyright 2001-2012 WoltLab GmbH + * @copyright 2001-2013 WoltLab GmbH * @license GNU Lesser General Public License * @package com.woltlab.wcf.message * @subpackage util @@ -34,4 +34,69 @@ class MessageUtil { return $text; } + + /** + * Gets mentioned users. + * + * @param string $text + * @return array + */ + public static function getMentionedUsers($text) { + // remove quotes + $newText = $text; + if (preg_match_all("~(?:\[quote|\[/quote\])~i", $text, $matches)) { + $newText = ''; + $substrings = preg_split("~(?:\[quote|\[/quote\])~i", $text); + + $inQuote = 0; + foreach ($matches[0] as $i => $tag) { + if (!$inQuote) { + $newText .= $substrings[$i]; + } + + if ($tag == '[quote') { + $inQuote++; + } + else { + $inQuote--; + } + } + + if (!$inQuote) $newText .= $substrings[count($substrings) - 1]; + } + + // check for mentions + if (preg_match_all("~\[url='[^']+'\]@(.+?)\[/url\]~", $newText, $matches)) { + return $matches[1]; + } + + return array(); + } + + /** + * Gets quoted users. + * + * @param string $text + * @return array + */ + public static function getQuotedUsers($text) { + $usernames = array(); + if (preg_match_all("~(?:\[(quote)=(?:')?(.+?)(?:')?(?:,[^\]]*)?\]|\[/quote\])~i", $text, $matches)) { + $level = 0; + + foreach ($matches[1] as $i => $tag) { + if ($tag == 'quote') { + if (!$level) { + $usernames[] = $matches[2][$i]; + } + $level++; + } + else { + $level--; + } + } + } + + return $usernames; + } } -- 2.20.1