Login
Username: Password:
Remember me

[Sign Up] / [Recover Password]

Theme / Language

Popular
New Torrents
new name
Size: 18.40 MB
Most active
new name
S: 74 L: 3
New Posters
Format Comment
Controls ·  Search New posts · Mark unread

You are in: Forum / Forum / Torrent Trader / v3 Support

2023-01-02 06:24:13
Here is a back-port function that takes care of special chars, formatting, bbcode and censor all in one. I post here, because I plan to gain feedback on new things coming to this function such as:

- Filter validation
- Improved bbcode array
- Whatever else fits here

Requires: mbstring extension

This function is critical for our descriptions, so its been with years of active attention.


function format_comment(string $text, bool $utf8 = false): string {
   $bbcode = strtr($text, array_combine(array('', '', '', '', '', '', '[img]', '[/img]', '[img=', '[/img]'), array('<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<img src=', ' />', '<img src=', ' />')));
   $string = str_replace('  ', '  ', nl2br(mb_convert_encoding($bbcode, 'UTF-8', 'ISO-8859-1')));
   $smilies = array(':)' => 'smile.png', ':(' => 'sad.png', ';)' => 'wink.png', ':P' => 'razz.png', ':D' => 'grin.png', ':|' => 'plain.png', ':O' => 'suprise.png', ':?' => 'confused.png', '8)' => 'glasses.png', '8o' => 'eek.png', 'B)' => 'cool.png', ':-)' => 'smile-big.png', ':-(' => 'crying.png', ':-*' => 'kiss.png', 'O:-D' => 'angel.png', ':-@' => 'devilish.png', ':o)' => 'monkey.png', ':help' => 'help.png', ':love' => 'love.png', ':warn' => 'warn.png', ':bomb' => 'bomb.png', ':idea' => 'idea.png', ':bad' => 'bad.png', ':!' => 'important.png', 'brb' => 'brb.png');
   $badword = false;
   foreach ($smilies as $code => &$url) {
       $string = str_replace($code, '<img style="width:25px;height:25px;vertical-align:middle" src="/images/smilies/' . $url . '" alt="' . $code . '" title="' . $code . '" />', $string);
   }
   $r = SQL_Query_exec('SELECT word, censor FROM censor');
   foreach (mysqli_fetch_assoc($r) as $rr) {
       $badword = true;
       $string = str_replace(trim($rr['word']), '<img src="/images/censored.png" alt="Censored" title="Censored">', $string);
   }
   $encoding = $utf8 ? 'UTF-8' : 'HTML-ENTITIES';
   return mb_convert_encoding($string, $encoding, 'ISO-8859-1');
}


Usage
format_comment($row['descr'], true);



-----

This one is much better. It uses ICONV for better character encoding/detection, enable/disable HTML, support bbcode, censor and UTF8 (optional). I'll be replacing the old function (above) with this function.

Requires: iconv + mbstring extensions

UPDATED Jan/15/23


function format_comment(string $text, bool $utf8 = false, $html = false): mixed {
   $encoding = mb_detect_encoding($text);
   if ($encoding !== 'UTF-8') {
       $string = iconv($encoding, 'UTF-8', $text);
   }
   $bbcode = strtr($text, array_combine(array('', '', '', '', '', '', '[img]', '[/img]', '[img=', '[/img]'), array('<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<img src=', ' />', '<img src=', ' />')));
   $string = preg_replace('/\r\n|\r|\n/', '<br>', $bbcode, -1, $count);
   if ($count === 0) {
       $string = strip_tags($string, '<br>');
   }
   $smilies = array(':)' => 'smile.png', ':(' => 'sad.png', ';)' => 'wink.png', ':P' => 'razz.png', ':D' => 'grin.png', ':|' => 'plain.png', ':O' => 'suprise.png', ':?' => 'confused.png', '8)' => 'glasses.png', '8o' => 'eek.png', 'B)' => 'cool.png', ':-)' => 'smile-big.png', ':-(' => 'crying.png', ':-*' => 'kiss.png', 'O:-D' => 'angel.png', ':-@' => 'devilish.png', ':o)' => 'monkey.png', ':help' => 'help.png', ':love' => 'love.png', ':warn' => 'warn.png', ':bomb' => 'bomb.png', ':idea' => 'idea.png', ':bad' => 'bad.png', ':!' => 'important.png', 'brb' => 'brb.png');
   foreach ($smilies as $code => &$url) {
       $string = str_replace($code, '<img style="width:25px;height:25px;vertical-align:middle" src="/images/smilies/' . $url . '" alt="' . $code . '" title="' . $code . '" />', $string);
   }
   $result = SQL_Query_exec('SELECT word, censor FROM censor');
   $censoredWords = mysqli_fetch_all($result, MYSQLI_ASSOC);
   foreach ($censoredWords as $rr) {
       $string = str_replace(trim($rr['word']), '<img src="/images/censored.png" alt="Censored" title="Censored">', $string);
   }
   if ($utf8) {
       $string = iconv('UTF-8', 'ISO-8859-1//IGNORE', $string);
   } else {
       $string = mb_convert_encoding($string, 'HTML-ENTITIES', 'ISO-8859-1');
   }
   if (strlen($string) > 0) {
       return $string;
   }
   return false;
}


Usage
format_comment($row['descr'], false, true);



if ($text = format_comment($descr, false, true)) {
   echo $text;
} else {
   echo 'Unable to convert charset.';
}


NOTE:
mysql_fetch_all caches the table, that way its not being hit with tons of requests for just a censor image. Eliminates unnecessary computations (just use PDO lol)
Last edited by STorrents2020 on 2023-01-16 00:59:48
2023-01-03 07:35:03
Nice function mate big improvement will test this out
More modern and replaces the slow preg
2023-01-03 07:59:36
Most welcome! Working great on the site. This old dinosaur was needing an update for a while lol

I made it only support BR tags (for line breaks), so it handles formatting quite well. All other HTML is removed (strip_tags), and parsed accordingly.

I know it had security issues previously, as HTML was never sanitized and was wide open (while I prevented with htmlspecialchars() after each call), which made no sense at all. This resolves everything.

Because I deal with so much Japanese & UTF8 encoding, this was very important.
Last edited by STorrents2020 on 2023-01-03 08:05:22
Users online

No users online

Search



Statistics

    Torrents

  • Tracking: 3
  • Todays torrents: 0
  • Seeders: 0
  • Leechers: 0
  • Peers: 0
  • Downloaded: 89.96 GB
  • Uploaded: 9.86 GB

  • Members

  • We have: 838
  • New today: 0
  • Guest today: 9

  • Online

  • Total online: 2
  • Members: 0
  • Guests online: 2
  • Comments: 3
  • Messages: 1,728