Пример #1
0
 public static function parseBBCode($text)
 {
     $config = Komento::getConfig();
     $nofollow = $config->get('links_nofollow') ? ' rel="nofollow"' : '';
     $maxdimension = '';
     if ($config->get('max_image_width') || $config->get('max_image_height')) {
         $maxdimension = ' style="';
         if ($config->get('max_image_width')) {
             $maxdimension .= 'max-width:' . $config->get('max_image_width') . 'px;';
         }
         if ($config->get('max_image_height')) {
             $maxdimension .= 'max-height:' . $config->get('max_image_width') . 'px;';
         }
         $maxdimension .= '"';
     }
     // Converts all html entities properly
     $text = htmlspecialchars($text, ENT_NOQUOTES);
     $text = trim($text);
     $text = preg_replace_callback('/\\[code( type="(.*?)")?\\](.*?)\\[\\/code\\]/ms', array('KomentoCommentHelper', 'escape'), $text);
     // avoid smileys in pre tag gets replaced
     $text = KomentoCommentHelper::encodePre($text);
     // change new line to br (without affecting pre)
     $text = nl2br($text);
     // BBCode to find...
     $in = array('/\\[b\\](.*?)\\[\\/b\\]/ms', '/\\[i\\](.*?)\\[\\/i\\]/ms', '/\\[u\\](.*?)\\[\\/u\\]/ms', '/\\[img\\](.*?)\\[\\/img\\]/ms', '/\\[email\\](.*?)\\[\\/email\\]/ms', '/\\[size\\="?(.*?)"?\\](.*?)\\[\\/size\\]/ms', '/\\[color\\="?(.*?)"?\\](.*?)\\[\\/color\\]/ms', '/\\[quote\\](.*?)\\[\\/quote\\]/ms');
     // And replace them by...
     $out = array('<strong>\\1</strong>', '<em>\\1</em>', '<u>\\1</u>', '<img src="\\1" alt="\\1"' . $maxdimension . ' />', '<a target="_blank" href="mailto:\\1"' . $nofollow . '>\\1</a>', '<span style="font-size:\\1%">\\2</span>', '<span style="color:\\1">\\2</span>', '<blockquote>\\1</blockquote>');
     // strip out bbcode data first
     $tmp = preg_replace($in, '', $text);
     // strip out bbcode url data
     $urlin = '/\\[url\\="?(.*?)"?\\](.*?)\\[\\/url\\]/ms';
     $tmp = preg_replace($urlin, '', $tmp);
     // strip out video links too
     $tmp = Komento::getHelper('videos')->strip($tmp);
     // replace url
     if ($config->get('auto_hyperlink')) {
         $text = self::replaceURL($tmp, $text);
     }
     // replace video links
     if ($config->get('allow_video')) {
         $text = Komento::getHelper('Videos')->replace($text);
     } else {
         $text = Komento::getHelper('Videos')->strip($text);
     }
     // replace bbcode with html
     $text = preg_replace($in, $out, $text);
     // Replace url bbcode with html
     $text = self::replaceBBUrl($text);
     // manual fix for unwrapped li issue
     $text = self::replaceBBList($text);
     $smileyin = array();
     $smileyout = array();
     if ($config->get('bbcode_smile')) {
         $smileyin[] = ':)';
         $smileyout[] = '<img alt=":)" style="width:16px; height:16px;" class="kmt-emoticon" src="' . KOMENTO_EMOTICONS_DIR . 'emoticon-smile.png" />';
     }
     if ($config->get('bbcode_happy')) {
         $smileyin[] = ':D';
         $smileyout[] = '<img alt=":D" style="width:16px; height:16px;" class="kmt-emoticon" src="' . KOMENTO_EMOTICONS_DIR . 'emoticon-happy.png" />';
     }
     if ($config->get('bbcode_surprised')) {
         $smileyin[] = ':o';
         $smileyout[] = '<img alt=":o" style="width:16px; height:16px;" class="kmt-emoticon" src="' . KOMENTO_EMOTICONS_DIR . 'emoticon-surprised.png" />';
     }
     if ($config->get('bbcode_tongue')) {
         $smileyin[] = ':p';
         $smileyout[] = '<img alt=":p" style="width:16px; height:16px;" class="kmt-emoticon" src="' . KOMENTO_EMOTICONS_DIR . 'emoticon-tongue.png" />';
     }
     if ($config->get('bbcode_unhappy')) {
         $smileyin[] = ':(';
         $smileyout[] = '<img alt=":(" style="width:16px; height:16px;" class="kmt-emoticon" src="' . KOMENTO_EMOTICONS_DIR . 'emoticon-unhappy.png" />';
     }
     if ($config->get('bbcode_wink')) {
         $smileyin[] = ';)';
         $smileyout[] = '<img alt=";)" style="width:16px; height:16px;" class="kmt-emoticon" src="' . KOMENTO_EMOTICONS_DIR . 'emoticon-wink.png" />';
     }
     // add in custom smileys
     $smileycode = $config->get('smileycode');
     $smileypath = $config->get('smileypath');
     if (is_array($smileycode) && is_array($smileypath)) {
         foreach ($smileycode as $index => $code) {
             if (!empty($code) && !empty($smileypath[$index])) {
                 $smileyin[] = $code;
                 $smileyout[] = '<img alt="' . $code . '" class="kmt-emoticon" src="' . $smileypath[$index] . '" />';
             }
         }
     }
     $text = str_replace($smileyin, $smileyout, $text);
     // done parsing emoticons and bbcode, decode pre text back
     $text = KomentoCommentHelper::decodePre($text);
     // paragraphs
     $text = str_replace("\r", "", $text);
     $text = "<p>" . preg_replace("/(\n){2,}/", "</p><p>", $text) . "</p>";
     // $text = preg_replace_callback('/<pre>(.*?)<\/pre>/ms', "removeBr", $text);
     // $text = preg_replace('/<p><pre(.*)>(.*?)<\/pre><\/p>/ms', "<pre\\1>\\2</pre>", $text);
     $text = preg_replace_callback('/<ul>(.*?)<\\/ul>/ms', array('KomentoCommentHelper', 'removeBr'), $text);
     // fix [list] within [*] causing dom errors
     $text = preg_replace('/<li>(.*?)<ul>(.*?)<\\/ul>(.*?)<\\/li>/ms', "\\1<ul>\\2</ul>\\3", $text);
     $text = preg_replace('/<p><ul>(.*?)<\\/ul><\\/p>/ms', "<ul>\\1</ul>", $text);
     return $text;
 }