Moved code from com.woltlab.wbb into com.woltlab.wcf
authorMarcel Werk <burntime@woltlab.com>
Mon, 3 Jun 2013 19:41:00 +0000 (21:41 +0200)
committerMarcel Werk <burntime@woltlab.com>
Mon, 3 Jun 2013 19:41:00 +0000 (21:41 +0200)
wcfsetup/install/files/lib/util/MessageUtil.class.php

index 834ab377eec429ddc03cdc768a2e7d234b2505b6..659cda9e205cd072f6c32eb88da3ebf86654e023 100644 (file)
@@ -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 <http://opensource.org/licenses/lgpl-license.php>
  * @package    com.woltlab.wcf.message
  * @subpackage util
@@ -34,4 +34,69 @@ class MessageUtil {
                
                return $text;
        }
+       
+       /**
+        * Gets mentioned users.
+        * 
+        * @param       string          $text
+        * @return      array<string>
+        */
+       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<string>
+        */
+       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;
+       }
 }