| 1 | <?php |
| 2 | namespace wcf\system\bbcode; |
| 3 | use wcf\data\smiley\Smiley; |
| 4 | use wcf\data\smiley\SmileyCache; |
| 5 | use wcf\system\event\EventHandler; |
| 6 | use wcf\system\SingletonFactory; |
| 7 | use wcf\util\StringUtil; |
| 8 | |
| 9 | /** |
| 10 | * Parses urls and smilies in simple messages. |
| 11 | * |
| 12 | * @author Marcel Werk |
| 13 | * @copyright 2001-2016 WoltLab GmbH |
| 14 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 15 | * @package WoltLabSuite\Core\System\Bbcode |
| 16 | */ |
| 17 | class SimpleMessageParser extends SingletonFactory { |
| 18 | /** |
| 19 | * forbidden characters |
| 20 | * @var string |
| 21 | */ |
| 22 | protected static $illegalChars = '[^\x0-\x2C\x2E\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+'; |
| 23 | |
| 24 | /** |
| 25 | * list of smilies |
| 26 | * @var Smiley[] |
| 27 | */ |
| 28 | protected $smilies = []; |
| 29 | |
| 30 | /** |
| 31 | * cached URLs |
| 32 | * @var string[] |
| 33 | */ |
| 34 | protected $cachedURLs = []; |
| 35 | |
| 36 | /** |
| 37 | * cached e-mails |
| 38 | * @var string[] |
| 39 | */ |
| 40 | protected $cachedEmails = []; |
| 41 | |
| 42 | /** |
| 43 | * currently parsed message |
| 44 | * @var string |
| 45 | */ |
| 46 | public $message = ''; |
| 47 | |
| 48 | /** |
| 49 | * @inheritDoc |
| 50 | */ |
| 51 | protected function init() { |
| 52 | parent::init(); |
| 53 | |
| 54 | if (MODULE_SMILEY == 1) { |
| 55 | // get smilies |
| 56 | $smilies = SmileyCache::getInstance()->getSmilies(); |
| 57 | $categories = SmileyCache::getInstance()->getCategories(); |
| 58 | foreach ($smilies as $categoryID => $categorySmilies) { |
| 59 | if ($categories[$categoryID ?: null]->isDisabled) continue; |
| 60 | |
| 61 | /** @var Smiley $smiley */ |
| 62 | foreach ($categorySmilies as $smiley) { |
| 63 | foreach ($smiley->smileyCodes as $smileyCode) { |
| 64 | $this->smilies[$smileyCode] = '<img src="'.$smiley->getURL().'" alt="'.StringUtil::encodeHTML($smiley->smileyCode).'">'; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | krsort($this->smilies); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Parses the given message and returns the parsed message. |
| 74 | * |
| 75 | * @param string $message |
| 76 | * @param boolean $parseURLs |
| 77 | * @param boolean $parseSmilies |
| 78 | * @return string |
| 79 | */ |
| 80 | public function parse($message, $parseURLs = true, $parseSmilies = true) { |
| 81 | $this->message = $message; |
| 82 | $this->cachedURLs = $this->cachedEmails = []; |
| 83 | |
| 84 | // call event |
| 85 | EventHandler::getInstance()->fireAction($this, 'beforeParsing'); |
| 86 | |
| 87 | // parse urls |
| 88 | if ($parseURLs) { |
| 89 | $this->message = $this->parseURLs($this->message); |
| 90 | } |
| 91 | |
| 92 | // encode html |
| 93 | $this->message = StringUtil::encodeHTML($this->message); |
| 94 | |
| 95 | // converts newlines to <br>'s |
| 96 | $this->message = nl2br($this->message, false); |
| 97 | |
| 98 | // parse urls |
| 99 | if ($parseURLs) { |
| 100 | $this->message = $this->insertCachedURLs($this->message); |
| 101 | } |
| 102 | |
| 103 | // parse smilies |
| 104 | if ($parseSmilies) { |
| 105 | $this->message = $this->parseSmilies($this->message); |
| 106 | } |
| 107 | |
| 108 | // replace bad html tags (script etc.) |
| 109 | $badSearch = ['/(javascript):/i', '/(about):/i', '/(vbscript):/i']; |
| 110 | $badReplace = ['$1<b></b>:', '$1<b></b>:', '$1<b></b>:']; |
| 111 | $this->message = preg_replace($badSearch, $badReplace, $this->message); |
| 112 | |
| 113 | // call event |
| 114 | EventHandler::getInstance()->fireAction($this, 'afterParsing'); |
| 115 | |
| 116 | return $this->message; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Parses urls. |
| 121 | * |
| 122 | * @param string $text |
| 123 | * @return string text |
| 124 | */ |
| 125 | public function parseURLs($text) { |
| 126 | // define pattern |
| 127 | $urlPattern = '~(?<!\B|"|\'|=|/|\]|,|\?) |
| 128 | (?: # hostname |
| 129 | (?:ftp|https?)://'.static::$illegalChars.'(?:\.'.static::$illegalChars.')* |
| 130 | | |
| 131 | www\.(?:'.static::$illegalChars.'\.)+ |
| 132 | (?:[a-z]{2,63}(?=\b)) # tld |
| 133 | ) |
| 134 | |
| 135 | (?::\d+)? # port |
| 136 | |
| 137 | (?: |
| 138 | / |
| 139 | [^!.,?;"\'<>()\[\]{}\s]* |
| 140 | (?: |
| 141 | [!.,?;(){}]+ [^!.,?;"\'<>()\[\]{}\s]+ |
| 142 | )* |
| 143 | )? |
| 144 | ~ix'; |
| 145 | $emailPattern = '~(?<!\B|"|\'|=|/|\]|,|:) |
| 146 | (?:) |
| 147 | \w+(?:[\.\-]\w+)* |
| 148 | @ |
| 149 | (?:'.static::$illegalChars.'\.)+ # hostname |
| 150 | (?:[a-z]{2,4}(?=\b)) |
| 151 | (?!"|\'|\[|\-) |
| 152 | ~ix'; |
| 153 | |
| 154 | // parse urls |
| 155 | $text = preg_replace_callback($urlPattern, [$this, 'cacheURLsCallback'], $text); |
| 156 | |
| 157 | // parse emails |
| 158 | if (mb_strpos($text, '@') !== false) { |
| 159 | $text = preg_replace_callback($emailPattern, [$this, 'cacheEmailsCallback'], $text); |
| 160 | } |
| 161 | |
| 162 | return $text; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Returns the hash for an matched URL in the message. |
| 167 | * |
| 168 | * @param array $matches |
| 169 | * @return string |
| 170 | */ |
| 171 | protected function cacheURLsCallback($matches) { |
| 172 | $hash = '@@'.StringUtil::getHash(uniqid(microtime()).$matches[0]).'@@'; |
| 173 | $this->cachedURLs[$hash] = $matches[0]; |
| 174 | |
| 175 | return $hash; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Returns the hash for an matched e-mail in the message. |
| 180 | * |
| 181 | * @param array $matches |
| 182 | * @return string |
| 183 | */ |
| 184 | protected function cacheEmailsCallback($matches) { |
| 185 | $hash = '@@'.StringUtil::getHash(uniqid(microtime()).$matches[0]).'@@'; |
| 186 | $this->cachedEmails[$hash] = $matches[0]; |
| 187 | |
| 188 | return $hash; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Reinserts cached URLs and e-mails. |
| 193 | * |
| 194 | * @param string $text |
| 195 | * @return string |
| 196 | */ |
| 197 | protected function insertCachedURLs($text) { |
| 198 | foreach ($this->cachedURLs as $hash => $url) { |
| 199 | // add protocol if necessary |
| 200 | if (!preg_match("/[a-z]:\/\//si", $url)) { |
| 201 | $url = 'http://'.$url; |
| 202 | } |
| 203 | |
| 204 | $text = str_replace($hash, StringUtil::getAnchorTag($url), $text); |
| 205 | } |
| 206 | |
| 207 | foreach ($this->cachedEmails as $hash => $email) { |
| 208 | $email = StringUtil::encodeHTML($email); |
| 209 | |
| 210 | $text = str_replace($hash, '<a href="mailto:'.$email.'">'.$email.'</a>', $text); |
| 211 | } |
| 212 | |
| 213 | return $text; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Parses smiley codes. |
| 218 | * |
| 219 | * @param string $text |
| 220 | * @return string text |
| 221 | */ |
| 222 | public function parseSmilies($text) { |
| 223 | foreach ($this->smilies as $code => $html) { |
| 224 | //$text = preg_replace('~(?<!&\w{2}|&\w{3}|&\w{4}|&\w{5}|&\w{6}|&#\d{2}|&#\d{3}|&#\d{4}|&#\d{5})'.preg_quote(StringUtil::encodeHTML($code), '~').'(?![^<]*>)~', $html, $text); |
| 225 | $text = preg_replace('~(?<=^|\s)'.preg_quote(StringUtil::encodeHTML($code), '~').'(?=$|\s|<br />|<br>)~', $html, $text); |
| 226 | } |
| 227 | |
| 228 | return $text; |
| 229 | } |
| 230 | } |