Subdomain Posts
None | 8 min ago
PHP | 12 hours ago
None | 14 hours ago
None | 15 hours ago
PHP | 17 hours ago
PHP | 18 hours ago
None | 19 hours ago
None | 22 hours ago
PHP | 1 day ago
None | 1 day ago
Recent Posts
None | 17 sec ago
Bash | 23 sec ago
None | 42 sec ago
None | 45 sec ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 2 min ago
None | 2 min ago
None | 2 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 9th of Feb 2010 06:46:50 AM Download | Raw | Embed | Report
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3
  5. * @version $Id: functions_content.php 10039 2009-08-21 09:44:55Z bantu $
  6. * @copyright (c) 2005 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10.  
  11. /**
  12. * @ignore
  13. */
  14. if (!defined('IN_PHPBB'))
  15. {
  16.         exit;
  17. }
  18.  
  19. /**
  20. * gen_sort_selects()
  21. * make_jumpbox()
  22. * bump_topic_allowed()
  23. * get_context()
  24. * decode_message()
  25. * strip_bbcode()
  26. * generate_text_for_display()
  27. * generate_text_for_storage()
  28. * generate_text_for_edit()
  29. * make_clickable_callback()
  30. * make_clickable()
  31. * censor_text()
  32. * bbcode_nl2br()
  33. * smiley_text()
  34. * parse_attachments()
  35. * extension_allowed()
  36. * truncate_string()
  37. * get_username_string()
  38. * class bitfield
  39. */
  40.  
  41. /**
  42. * Generate sort selection fields
  43. */
  44. function gen_sort_selects(&$limit_days, &$sort_by_text, &$sort_days, &$sort_key, &$sort_dir, &$s_limit_days, &$s_sort_key, &$s_sort_dir, &$u_sort_param, $def_st = false, $def_sk = false, $def_sd = false)
  45. {
  46.         global $user;
  47.  
  48.         $sort_dir_text = array('a' => $user->lang['ASCENDING'], 'd' => $user->lang['DESCENDING']);
  49.  
  50.         $sorts = array(
  51.                 'st'    => array(
  52.                         'key'           => 'sort_days',
  53.                         'default'       => $def_st,
  54.                         'options'       => $limit_days,
  55.                         'output'        => &$s_limit_days,
  56.                 ),
  57.  
  58.                 'sk'    => array(
  59.                         'key'           => 'sort_key',
  60.                         'default'       => $def_sk,
  61.                         'options'       => $sort_by_text,
  62.                         'output'        => &$s_sort_key,
  63.                 ),
  64.  
  65.                 'sd'    => array(
  66.                         'key'           => 'sort_dir',
  67.                         'default'       => $def_sd,
  68.                         'options'       => $sort_dir_text,
  69.                         'output'        => &$s_sort_dir,
  70.                 ),
  71.         );
  72.         $u_sort_param  = '';
  73.  
  74.         foreach ($sorts as $name => $sort_ary)
  75.         {
  76.                 $key = $sort_ary['key'];
  77.                 $selected = $$sort_ary['key'];
  78.  
  79.                 // Check if the key is selectable. If not, we reset to the default or first key found.
  80.                 // This ensures the values are always valid. We also set $sort_dir/sort_key/etc. to the
  81.                 // correct value, else the protection is void. ;)
  82.                 if (!isset($sort_ary['options'][$selected]))
  83.                 {
  84.                         if ($sort_ary['default'] !== false)
  85.                         {
  86.                                 $selected = $$key = $sort_ary['default'];
  87.                         }
  88.                         else
  89.                         {
  90.                                 @reset($sort_ary['options']);
  91.                                 $selected = $$key = key($sort_ary['options']);
  92.                         }
  93.                 }
  94.  
  95.                 $sort_ary['output'] = '<select name="' . $name . '" id="' . $name . '">';
  96.                 foreach ($sort_ary['options'] as $option => $text)
  97.                 {
  98.                         $sort_ary['output'] .= '<option value="' . $option . '"' . (($selected == $option) ? ' selected="selected"' : '') . '>' . $text . '</option>';
  99.                 }
  100.                 $sort_ary['output'] .= '</select>';
  101.  
  102.                 $u_sort_param .= ($selected !== $sort_ary['default']) ? ((strlen($u_sort_param)) ? '&amp;' : '') . "{$name}={$selected}" : '';
  103.         }
  104.  
  105.         return;
  106. }
  107.  
  108. /**
  109. * Generate Jumpbox
  110. */
  111. function make_jumpbox($action, $forum_id = false, $select_all = false, $acl_list = false, $force_display = false)
  112. {
  113.         global $config, $auth, $template, $user, $db;
  114.  
  115.         // We only return if the jumpbox is not forced to be displayed (in case it is needed for functionality)
  116.         if (!$config['load_jumpbox'] && $force_display === false)
  117.         {
  118.                 return;
  119.         }
  120.  
  121.         $sql = 'SELECT forum_id, forum_name, parent_id, forum_type, left_id, right_id
  122.                 FROM ' . FORUMS_TABLE . '
  123.                 ORDER BY left_id ASC';
  124.         $result = $db->sql_query($sql, 600);
  125.  
  126.         $right = $padding = 0;
  127.         $padding_store = array('0' => 0);
  128.         $display_jumpbox = false;
  129.         $iteration = 0;
  130.  
  131.         // Sometimes it could happen that forums will be displayed here not be displayed within the index page
  132.         // This is the result of forums not displayed at index, having list permissions and a parent of a forum with no permissions.
  133.         // If this happens, the padding could be "broken"
  134.  
  135.         while ($row = $db->sql_fetchrow($result))
  136.         {
  137.                 if ($row['left_id'] < $right)
  138.                 {
  139.                         $padding++;
  140.                         $padding_store[$row['parent_id']] = $padding;
  141.                 }
  142.                 else if ($row['left_id'] > $right + 1)
  143.                 {
  144.                         // Ok, if the $padding_store for this parent is empty there is something wrong. For now we will skip over it.
  145.                         // @todo digging deep to find out "how" this can happen.
  146.                         $padding = (isset($padding_store[$row['parent_id']])) ? $padding_store[$row['parent_id']] : $padding;
  147.                 }
  148.  
  149.                 $right = $row['right_id'];
  150.  
  151.                 if ($row['forum_type'] == FORUM_CAT && ($row['left_id'] + 1 == $row['right_id']))
  152.                 {
  153.                         // Non-postable forum with no subforums, don't display
  154.                         continue;
  155.                 }
  156.  
  157.                 if (!$auth->acl_get('f_list', $row['forum_id']))
  158.                 {
  159.                         // if the user does not have permissions to list this forum skip
  160.                         continue;
  161.                 }
  162.  
  163.                 if ($acl_list && !$auth->acl_gets($acl_list, $row['forum_id']))
  164.                 {
  165.                         continue;
  166.                 }
  167.  
  168.                 if (!$display_jumpbox)
  169.                 {
  170.                         $template->assign_block_vars('jumpbox_forums', array(
  171.                                 'FORUM_ID'              => ($select_all) ? 0 : -1,
  172.                                 'FORUM_NAME'    => ($select_all) ? $user->lang['ALL_FORUMS'] : $user->lang['SELECT_FORUM'],
  173.                                 'S_FORUM_COUNT' => $iteration)
  174.                         );
  175.  
  176.                         $iteration++;
  177.                         $display_jumpbox = true;
  178.                 }
  179.  
  180.                 $template->assign_block_vars('jumpbox_forums', array(
  181.                         'FORUM_ID'              => $row['forum_id'],
  182.                         'FORUM_NAME'    => $row['forum_name'],
  183.                         'SELECTED'              => ($row['forum_id'] == $forum_id) ? ' selected="selected"' : '',
  184.                         'S_FORUM_COUNT' => $iteration,
  185.                         'S_IS_CAT'              => ($row['forum_type'] == FORUM_CAT) ? true : false,
  186.                         'S_IS_LINK'             => ($row['forum_type'] == FORUM_LINK) ? true : false,
  187.                         'S_IS_POST'             => ($row['forum_type'] == FORUM_POST) ? true : false)
  188.                 );
  189.  
  190.                 for ($i = 0; $i < $padding; $i++)
  191.                 {
  192.                         $template->assign_block_vars('jumpbox_forums.level', array());
  193.                 }
  194.                 $iteration++;
  195.         }
  196.         $db->sql_freeresult($result);
  197.         unset($padding_store);
  198.  
  199.         $template->assign_vars(array(
  200.                 'S_DISPLAY_JUMPBOX'     => $display_jumpbox,
  201.                 'S_JUMPBOX_ACTION'      => $action)
  202.         );
  203.  
  204.         return;
  205. }
  206.  
  207. /**
  208. * Bump Topic Check - used by posting and viewtopic
  209. */
  210. function bump_topic_allowed($forum_id, $topic_bumped, $last_post_time, $topic_poster, $last_topic_poster)
  211. {
  212.         global $config, $auth, $user;
  213.  
  214.         // Check permission and make sure the last post was not already bumped
  215.         if (!$auth->acl_get('f_bump', $forum_id) || $topic_bumped)
  216.         {
  217.                 return false;
  218.         }
  219.  
  220.         // Check bump time range, is the user really allowed to bump the topic at this time?
  221.         $bump_time = ($config['bump_type'] == 'm') ? $config['bump_interval'] * 60 : (($config['bump_type'] == 'h') ? $config['bump_interval'] * 3600 : $config['bump_interval'] * 86400);
  222.  
  223.         // Check bump time
  224.         if ($last_post_time + $bump_time > time())
  225.         {
  226.                 return false;
  227.         }
  228.  
  229.         // Check bumper, only topic poster and last poster are allowed to bump
  230.         if ($topic_poster != $user->data['user_id'] && $last_topic_poster != $user->data['user_id'])
  231.         {
  232.                 return false;
  233.         }
  234.  
  235.         // A bump time of 0 will completely disable the bump feature... not intended but might be useful.
  236.         return $bump_time;
  237. }
  238.  
  239. /**
  240. * Generates a text with approx. the specified length which contains the specified words and their context
  241. *
  242. * @param        string  $text   The full text from which context shall be extracted
  243. * @param        string  $words  An array of words which should be contained in the result, has to be a valid part of a PCRE pattern (escape with preg_quote!)
  244. * @param        int             $length The desired length of the resulting text, however the result might be shorter or longer than this value
  245. *
  246. * @return       string                  Context of the specified words separated by "..."
  247. */
  248. function get_context($text, $words, $length = 400)
  249. {
  250.         // first replace all whitespaces with single spaces
  251.         $text = preg_replace('/ +/', ' ', strtr($text, "\t\n\r\x0C ", '     '));
  252.  
  253.         // we need to turn the entities back into their original form, to not cut the message in between them
  254.         $entities = array('&lt;', '&gt;', '&#91;', '&#93;', '&#46;', '&#58;', '&#058;');
  255.         $characters = array('<', '>', '[', ']', '.', ':', ':');
  256.         $text = str_replace($entities, $characters, $text);
  257.  
  258.         $word_indizes = array();
  259.         if (sizeof($words))
  260.         {
  261.                 $match = '';
  262.                 // find the starting indizes of all words
  263.                 foreach ($words as $word)
  264.                 {
  265.                         if ($word)
  266.                         {
  267.                                 if (preg_match('#(?:[^\w]|^)(' . $word . ')(?:[^\w]|$)#i', $text, $match))
  268.                                 {
  269.                                         if (empty($match[1]))
  270.                                         {
  271.                                                 continue;
  272.                                         }
  273.  
  274.                                         $pos = utf8_strpos($text, $match[1]);
  275.                                         if ($pos !== false)
  276.                                         {
  277.                                                 $word_indizes[] = $pos;
  278.                                         }
  279.                                 }
  280.                         }
  281.                 }
  282.                 unset($match);
  283.  
  284.                 if (sizeof($word_indizes))
  285.                 {
  286.                         $word_indizes = array_unique($word_indizes);
  287.                         sort($word_indizes);
  288.  
  289.                         $wordnum = sizeof($word_indizes);
  290.                         // number of characters on the right and left side of each word
  291.                         $sequence_length = (int) ($length / (2 * $wordnum)) - 2;
  292.                         $final_text = '';
  293.                         $word = $j = 0;
  294.                         $final_text_index = -1;
  295.  
  296.                         // cycle through every character in the original text
  297.                         for ($i = $word_indizes[$word], $n = utf8_strlen($text); $i < $n; $i++)
  298.                         {
  299.                                 // if the current position is the start of one of the words then append $sequence_length characters to the final text
  300.                                 if (isset($word_indizes[$word]) && ($i == $word_indizes[$word]))
  301.                                 {
  302.                                         if ($final_text_index < $i - $sequence_length - 1)
  303.                                         {
  304.                                                 $final_text .= '... ' . preg_replace('#^([^ ]*)#', '', utf8_substr($text, $i - $sequence_length, $sequence_length));
  305.                                         }
  306.                                         else
  307.                                         {
  308.                                                 // if the final text is already nearer to the current word than $sequence_length we only append the text
  309.                                                 // from its current index on and distribute the unused length to all other sequenes
  310.                                                 $sequence_length += (int) (($final_text_index - $i + $sequence_length + 1) / (2 * $wordnum));
  311.                                                 $final_text .= utf8_substr($text, $final_text_index + 1, $i - $final_text_index - 1);
  312.                                         }
  313.                                         $final_text_index = $i - 1;
  314.  
  315.                                         // add the following characters to the final text (see below)
  316.                                         $word++;
  317.                                         $j = 1;
  318.                                 }
  319.  
  320.                                 if ($j > 0)
  321.                                 {
  322.                                         // add the character to the final text and increment the sequence counter
  323.                                         $final_text .= utf8_substr($text, $i, 1);
  324.                                         $final_text_index++;
  325.                                         $j++;
  326.  
  327.                                         // if this is a whitespace then check whether we are done with this sequence
  328.                                         if (utf8_substr($text, $i, 1) == ' ')
  329.                                         {
  330.                                                 // only check whether we have to exit the context generation completely if we haven't already reached the end anyway
  331.                                                 if ($i + 4 < $n)
  332.                                                 {
  333.                                                         if (($j > $sequence_length && $word >= $wordnum) || utf8_strlen($final_text) > $length)
  334.                                                         {
  335.                                                                 $final_text .= ' ...';
  336.                                                                 break;
  337.                                                         }
  338.                                                 }
  339.                                                 else
  340.                                                 {
  341.                                                         // make sure the text really reaches the end
  342.                                                         $j -= 4;
  343.                                                 }
  344.  
  345.                                                 // stop context generation and wait for the next word
  346.                                                 if ($j > $sequence_length)
  347.                                                 {
  348.                                                         $j = 0;
  349.                                                 }
  350.                                         }
  351.                                 }
  352.                         }
  353.                         return str_replace($characters, $entities, $final_text);
  354.                 }
  355.         }
  356.  
  357.         if (!sizeof($words) || !sizeof($word_indizes))
  358.         {
  359.                 return str_replace($characters, $entities, ((utf8_strlen($text) >= $length + 3) ? utf8_substr($text, 0, $length) . '...' : $text));
  360.         }
  361. }
  362.  
  363. /**
  364. * Decode text whereby text is coming from the db and expected to be pre-parsed content
  365. * We are placing this outside of the message parser because we are often in need of it...
  366. */
  367. function decode_message(&$message, $bbcode_uid = '')
  368. {
  369.         global $config;
  370.  
  371.         if ($bbcode_uid)
  372.         {
  373.                 $match = array('<br />', "[/*:m:$bbcode_uid]", ":u:$bbcode_uid", ":o:$bbcode_uid", ":$bbcode_uid");
  374.                 $replace = array("\n", '', '', '', '');
  375.         }
  376.         else
  377.         {
  378.                 $match = array('<br />');
  379.                 $replace = array("\n");
  380.         }
  381.  
  382.         $message = str_replace($match, $replace, $message);
  383.  
  384.         $match = get_preg_expression('bbcode_htm');
  385.         $replace = array('\1', '\1', '\2', '\1', '', '');
  386.  
  387.         $message = preg_replace($match, $replace, $message);
  388. }
  389.  
  390. /**
  391. * Strips all bbcode from a text and returns the plain content
  392. */
  393. function strip_bbcode(&$text, $uid = '')
  394. {
  395.         if (!$uid)
  396.         {
  397.                 $uid = '[0-9a-z]{5,}';
  398.         }
  399.  
  400.         $text = preg_replace("#\[\/?[a-z0-9\*\+\-]+(?:=(?:&quot;.*&quot;|[^\]]*))?(?::[a-z])?(\:$uid)\]#", ' ', $text);
  401.  
  402.         $match = get_preg_expression('bbcode_htm');
  403.         $replace = array('\1', '\1', '\2', '\1', '', '');
  404.  
  405.         $text = preg_replace($match, $replace, $text);
  406. }
  407.  
  408. /**
  409. * For display of custom parsed text on user-facing pages
  410. * Expects $text to be the value directly from the database (stored value)
  411. */
  412. function generate_text_for_display($text, $uid, $bitfield, $flags)
  413. {
  414.         static $bbcode;
  415.  
  416.         if (!$text)
  417.         {
  418.                 return '';
  419.         }
  420.  
  421.         $text = censor_text($text);
  422.  
  423.         // Parse bbcode if bbcode uid stored and bbcode enabled
  424.         if ($uid && ($flags & OPTION_FLAG_BBCODE))
  425.         {
  426.                 if (!class_exists('bbcode'))
  427.                 {
  428.                         global $phpbb_root_path, $phpEx;
  429.                         include($phpbb_root_path . 'includes/bbcode.' . $phpEx);
  430.                 }
  431.  
  432.                 if (empty($bbcode))
  433.                 {
  434.                         $bbcode = new bbcode($bitfield);
  435.                 }
  436.                 else
  437.                 {
  438.                         $bbcode->bbcode($bitfield);
  439.                 }
  440.  
  441.                 $bbcode->bbcode_second_pass($text, $uid);
  442.         }
  443.  
  444.         $text = bbcode_nl2br($text);
  445.         $text = smiley_text($text, !($flags & OPTION_FLAG_SMILIES));
  446.  
  447.         return $text;
  448. }
  449.  
  450. /**
  451. * For parsing custom parsed text to be stored within the database.
  452. * This function additionally returns the uid and bitfield that needs to be stored.
  453. * Expects $text to be the value directly from request_var() and in it's non-parsed form
  454. */
  455. function generate_text_for_storage(&$text, &$uid, &$bitfield, &$flags, $allow_bbcode = false, $allow_urls = false, $allow_smilies = false)
  456. {
  457.         global $phpbb_root_path, $phpEx;
  458.  
  459.         $uid = $bitfield = '';
  460.         $flags = (($allow_bbcode) ? OPTION_FLAG_BBCODE : 0) + (($allow_smilies) ? OPTION_FLAG_SMILIES : 0) + (($allow_urls) ? OPTION_FLAG_LINKS : 0);
  461.  
  462.         if (!$text)
  463.         {
  464.                 return;
  465.         }
  466.  
  467.         if (!class_exists('parse_message'))
  468.         {
  469.                 include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
  470.         }
  471.  
  472.         $message_parser = new parse_message($text);
  473.         $message_parser->parse($allow_bbcode, $allow_urls, $allow_smilies);
  474.  
  475.         $text = $message_parser->message;
  476.         $uid = $message_parser->bbcode_uid;
  477.  
  478.         // If the bbcode_bitfield is empty, there is no need for the uid to be stored.
  479.         if (!$message_parser->bbcode_bitfield)
  480.         {
  481.                 $uid = '';
  482.         }
  483.  
  484.         $bitfield = $message_parser->bbcode_bitfield;
  485.  
  486.         return;
  487. }
  488.  
  489. /**
  490. * For decoding custom parsed text for edits as well as extracting the flags
  491. * Expects $text to be the value directly from the database (pre-parsed content)
  492. */
  493. function generate_text_for_edit($text, $uid, $flags)
  494. {
  495.         global $phpbb_root_path, $phpEx;
  496.  
  497.         decode_message($text, $uid);
  498.  
  499.         return array(
  500.                 'allow_bbcode'  => ($flags & OPTION_FLAG_BBCODE) ? 1 : 0,
  501.                 'allow_smilies' => ($flags & OPTION_FLAG_SMILIES) ? 1 : 0,
  502.                 'allow_urls'    => ($flags & OPTION_FLAG_LINKS) ? 1 : 0,
  503.                 'text'                  => $text
  504.         );
  505. }
  506.  
  507. /**
  508. * A subroutine of make_clickable used with preg_replace
  509. * It places correct HTML around an url, shortens the displayed text
  510. * and makes sure no entities are inside URLs
  511. */
  512. function make_clickable_callback($type, $whitespace, $url, $relative_url, $class)
  513. {
  514.         $orig_url               = $url;
  515.         $orig_relative  = $relative_url;
  516.         $append                 = '';
  517.         $url                    = htmlspecialchars_decode($url);
  518.         $relative_url   = htmlspecialchars_decode($relative_url);
  519.  
  520.         // make sure no HTML entities were matched
  521.         $chars = array('<', '>', '"');
  522.         $split = false;
  523.  
  524.         foreach ($chars as $char)
  525.         {
  526.                 $next_split = strpos($url, $char);
  527.                 if ($next_split !== false)
  528.                 {
  529.                         $split = ($split !== false) ? min($split, $next_split) : $next_split;
  530.                 }
  531.         }
  532.  
  533.         if ($split !== false)
  534.         {
  535.                 // an HTML entity was found, so the URL has to end before it
  536.                 $append                 = substr($url, $split) . $relative_url;
  537.                 $url                    = substr($url, 0, $split);
  538.                 $relative_url   = '';
  539.         }
  540.         else if ($relative_url)
  541.         {
  542.                 // same for $relative_url
  543.                 $split = false;
  544.                 foreach ($chars as $char)
  545.                 {
  546.                         $next_split = strpos($relative_url, $char);
  547.                         if ($next_split !== false)
  548.                         {
  549.                                 $split = ($split !== false) ? min($split, $next_split) : $next_split;
  550.                         }
  551.                 }
  552.  
  553.                 if ($split !== false)
  554.                 {
  555.                         $append                 = substr($relative_url, $split);
  556.                         $relative_url   = substr($relative_url, 0, $split);
  557.                 }
  558.         }
  559.  
  560.         // if the last character of the url is a punctuation mark, exclude it from the url
  561.         $last_char = ($relative_url) ? $relative_url[strlen($relative_url) - 1] : $url[strlen($url) - 1];
  562.  
  563.         switch ($last_char)
  564.         {
  565.                 case '.':
  566.                 case '?':
  567.                 case '!':
  568.                 case ':':
  569.                 case ',':
  570.                         $append = $last_char;
  571.                         if ($relative_url)
  572.                         {
  573.                                 $relative_url = substr($relative_url, 0, -1);
  574.                         }
  575.                         else
  576.                         {
  577.                                 $url = substr($url, 0, -1);
  578.                         }
  579.                 break;
  580.  
  581.                 // set last_char to empty here, so the variable can be used later to
  582.                 // check whether a character was removed
  583.                 default:
  584.                         $last_char = '';
  585.                 break;
  586.         }
  587.  
  588.         $short_url = (strlen($url) > 55) ? substr($url, 0, 39) . ' ... ' . substr($url, -10) : $url;
  589.  
  590.         switch ($type)
  591.         {
  592.                 case MAGIC_URL_LOCAL:
  593.                         $tag                    = 'l';
  594.                         $relative_url   = preg_replace('/[&?]sid=[0-9a-f]{32}$/', '', preg_replace('/([&?])sid=[0-9a-f]{32}&/', '$1', $relative_url));
  595.                         $url                    = $url . '/' . $relative_url;
  596.                         $text                   = $relative_url;
  597.  
  598.                         // this url goes to http://domain.tld/path/to/board/ which
  599.                         // would result in an empty link if treated as local so
  600.                         // don't touch it and let MAGIC_URL_FULL take care of it.
  601.                         if (!$relative_url)
  602.                         {
  603.                                 return $whitespace . $orig_url . '/' . $orig_relative; // slash is taken away by relative url pattern
  604.                         }
  605.                 break;
  606.  
  607.                 case MAGIC_URL_FULL:
  608.                         $tag    = 'm';
  609.                         $text   = $short_url;
  610.                 break;
  611.  
  612.                 case MAGIC_URL_WWW:
  613.                         $tag    = 'w';
  614.                         $url    = 'http://' . $url;
  615.                         $text   = $short_url;
  616.                 break;
  617.  
  618.                 case MAGIC_URL_EMAIL:
  619.                         $tag    = 'e';
  620.                         $text   = $short_url;
  621.                         $url    = 'mailto:' . $url;
  622.                 break;
  623.         }
  624.  
  625.         $url    = htmlspecialchars($url);
  626.         $text   = htmlspecialchars($text);
  627.         $append = htmlspecialchars($append);
  628.  
  629.         $html   = "$whitespace<!-- $tag --><a$class href=\"$url\">$text</a><!-- $tag -->$append";
  630.  
  631.         return $html;
  632. }
  633.  
  634. /**
  635. * make_clickable function
  636. *
  637. * Replace magic urls of form http://xxx.xxx., www.xxx. and xxx@xxx.xxx.
  638. * Cuts down displayed size of link if over 50 chars, turns absolute links
  639. * into relative versions when the server/script path matches the link
  640. */
  641. if (!function_exists('make_clickable'))
  642. {
  643.  
  644. function make_clickable($text, $server_url = false, $class = 'postlink')
  645. {
  646.         global $IN_WORDPRESS;
  647.         if ($IN_WORDPRESS)
  648.         {
  649.                 return wp_make_clickable($text); //WP version
  650.         } else {//phpBB version
  651.  
  652.         if ($server_url === false)
  653.         {
  654.                 $server_url = generate_board_url();
  655.         }
  656.  
  657.         static $magic_url_match;
  658.         static $magic_url_replace;
  659.         static $static_class;
  660.  
  661.         if (!is_array($magic_url_match) || $static_class != $class)
  662.         {
  663.                 $static_class = $class;
  664.                 $class = ($static_class) ? ' class="' . $static_class . '"' : '';
  665.                 $local_class = ($static_class) ? ' class="' . $static_class . '-local"' : '';
  666.  
  667.                 $magic_url_match = $magic_url_replace = array();
  668.                 // Be sure to not let the matches cross over. ;)
  669.  
  670.                 // relative urls for this board
  671.                 $magic_url_match[] = '#(^|[\n\t (>.])(' . preg_quote($server_url, '#') . ')/(' . get_preg_expression('relative_url_inline') . ')#ie';
  672.                 $magic_url_replace[] = "make_clickable_callback(MAGIC_URL_LOCAL, '\$1', '\$2', '\$3', '$local_class')";
  673.  
  674.                 // matches a xxxx://aaaaa.bbb.cccc. ...
  675.                 $magic_url_match[] = '#(^|[\n\t (>.])(' . get_preg_expression('url_inline') . ')#ie';
  676.                 $magic_url_replace[] = "make_clickable_callback(MAGIC_URL_FULL, '\$1', '\$2', '', '$class')";
  677.  
  678.                 // matches a "www.xxxx.yyyy[/zzzz]" kinda lazy URL thing
  679.                 $magic_url_match[] = '#(^|[\n\t (>])(' . get_preg_expression('www_url_inline') . ')#ie';
  680.                 $magic_url_replace[] = "make_clickable_callback(MAGIC_URL_WWW, '\$1', '\$2', '', '$class')";
  681.  
  682.                 // matches an email@domain type address at the start of a line, or after a space or after what might be a BBCode.
  683.                 $magic_url_match[] = '/(^|[\n\t (>])(' . get_preg_expression('email') . ')/ie';
  684.                 $magic_url_replace[] = "make_clickable_callback(MAGIC_URL_EMAIL, '\$1', '\$2', '', '')";
  685.         }
  686.  
  687.         }
  688. }
  689.  
  690.         return preg_replace($magic_url_match, $magic_url_replace, $text);
  691. }
  692.  
  693. /**
  694. * Censoring
  695. */
  696. function censor_text($text)
  697. {
  698.         static $censors;
  699.  
  700.         // Nothing to do?
  701.         if ($text === '')
  702.         {
  703.                 return '';
  704.         }
  705.  
  706.         // We moved the word censor checks in here because we call this function quite often - and then only need to do the check once
  707.         if (!isset($censors) || !is_array($censors))
  708.         {
  709.                 global $config, $user, $auth, $cache;
  710.  
  711.                 // We check here if the user is having viewing censors disabled (and also allowed to do so).
  712.                 if (!$user->optionget('viewcensors') && $config['allow_nocensors'] && $auth->acl_get('u_chgcensors'))
  713.                 {
  714.                         $censors = array();
  715.                 }
  716.                 else
  717.                 {
  718.                         $censors = $cache->obtain_word_list();
  719.                 }
  720.         }
  721.  
  722.         if (sizeof($censors))
  723.         {
  724.                 return preg_replace($censors['match'], $censors['replace'], $text);
  725.         }
  726.  
  727.         return $text;
  728. }
  729.  
  730. /**
  731. * custom version of nl2br which takes custom BBCodes into account
  732. */
  733. function bbcode_nl2br($text)
  734. {
  735.         // custom BBCodes might contain carriage returns so they
  736.         // are not converted into <br /> so now revert that
  737.         $text = str_replace(array("\n", "\r"), array('<br />', "\n"), $text);
  738.         return $text;
  739. }
  740.  
  741. /**
  742. * Smiley processing
  743. */
  744. function smiley_text($text, $force_option = false)
  745. {
  746.         global $config, $user, $phpbb_root_path;
  747.  
  748.         if ($force_option || !$config['allow_smilies'] || !$user->optionget('viewsmilies'))
  749.         {
  750.                 return preg_replace('#<!\-\- s(.*?) \-\-><img src="\{SMILIES_PATH\}\/.*? \/><!\-\- s\1 \-\->#', '\1', $text);
  751.         }
  752.         else
  753.         {
  754.                 $root_path = (defined('PHPBB_USE_BOARD_URL_PATH') && PHPBB_USE_BOARD_URL_PATH) ? generate_board_url() . '/' : $phpbb_root_path;
  755.                 return preg_replace('#<!\-\- s(.*?) \-\-><img src="\{SMILIES_PATH\}\/(.*?) \/><!\-\- s\1 \-\->#', '<img src="' . $root_path . $config['smilies_path'] . '/\2 />', $text);
  756.         }
  757. }
  758.  
  759. /**
  760. * General attachment parsing
  761. *
  762. * @param mixed $forum_id The forum id the attachments are displayed in (false if in private message)
  763. * @param string &$message The post/private message
  764. * @param array &$attachments The attachments to parse for (inline) display. The attachments array will hold templated data after parsing.
  765. * @param array &$update_count The attachment counts to be updated - will be filled
  766. * @param bool $preview If set to true the attachments are parsed for preview. Within preview mode the comments are fetched from the given $attachments array and not fetched from the database.
  767. */
  768. function parse_attachments($forum_id, &$message, &$attachments, &$update_count, $preview = false)
  769. {
  770.         if (!sizeof($attachments))
  771.         {
  772.                 return;
  773.         }
  774.  
  775.         global $template, $cache, $user;
  776.         global $extensions, $config, $phpbb_root_path, $phpEx;
  777.  
  778.         //
  779.         $compiled_attachments = array();
  780.  
  781.         if (!isset($template->filename['attachment_tpl']))
  782.         {
  783.                 $template->set_filenames(array(
  784.                         'attachment_tpl'        => 'attachment.html')
  785.                 );
  786.         }
  787.  
  788.         if (empty($extensions) || !is_array($extensions))
  789.         {
  790.                 $extensions = $cache->obtain_attach_extensions($forum_id);
  791.         }
  792.  
  793.         // Look for missing attachment information...
  794.         $attach_ids = array();
  795.         foreach ($attachments as $pos => $attachment)
  796.         {
  797.                 // If is_orphan is set, we need to retrieve the attachments again...
  798.                 if (!isset($attachment['extension']) && !isset($attachment['physical_filename']))
  799.                 {
  800.                         $attach_ids[(int) $attachment['attach_id']] = $pos;
  801.                 }
  802.         }
  803.  
  804.         // Grab attachments (security precaution)
  805.         if (sizeof($attach_ids))
  806.         {
  807.                 global $db;
  808.  
  809.                 $new_attachment_data = array();
  810.  
  811.                 $sql = 'SELECT *
  812.                         FROM ' . ATTACHMENTS_TABLE . '
  813.                         WHERE ' . $db->sql_in_set('attach_id', array_keys($attach_ids));
  814.                 $result = $db->sql_query($sql);
  815.  
  816.                 while ($row = $db->sql_fetchrow($result))
  817.                 {
  818.                         if (!isset($attach_ids[$row['attach_id']]))
  819.                         {
  820.                                 continue;
  821.                         }
  822.  
  823.                         // If we preview attachments we will set some retrieved values here
  824.                         if ($preview)
  825.                         {
  826.                                 $row['attach_comment'] = $attachments[$attach_ids[$row['attach_id']]]['attach_comment'];
  827.                         }
  828.  
  829.                         $new_attachment_data[$attach_ids[$row['attach_id']]] = $row;
  830.                 }
  831.                 $db->sql_freeresult($result);
  832.  
  833.                 $attachments = $new_attachment_data;
  834.                 unset($new_attachment_data);
  835.         }
  836.  
  837.         // Sort correctly
  838.         if ($config['display_order'])
  839.         {
  840.                 // Ascending sort
  841.                 krsort($attachments);
  842.         }
  843.         else
  844.         {
  845.                 // Descending sort
  846.                 ksort($attachments);
  847.         }
  848.  
  849.         foreach ($attachments as $attachment)
  850.         {
  851.                 if (!sizeof($attachment))
  852.                 {
  853.                         continue;
  854.                 }
  855.  
  856.                 // We need to reset/empty the _file block var, because this function might be called more than once
  857.                 $template->destroy_block_vars('_file');
  858.  
  859.                 $block_array = array();
  860.  
  861.                 // Some basics...
  862.                 $attachment['extension'] = strtolower(trim($attachment['extension']));
  863.                 $filename = $phpbb_root_path . $config['upload_path'] . '/' . utf8_basename($attachment['physical_filename']);
  864.                 $thumbnail_filename = $phpbb_root_path . $config['upload_path'] . '/thumb_' . utf8_basename($attachment['physical_filename']);
  865.  
  866.                 $upload_icon = '';
  867.  
  868.                 if (isset($extensions[$attachment['extension']]))
  869.                 {
  870.                         if ($user->img('icon_topic_attach', '') && !$extensions[$attachment['extension']]['upload_icon'])
  871.                         {
  872.                                 $upload_icon = $user->img('icon_topic_attach', '');
  873.                         }
  874.                         else if ($extensions[$attachment['extension']]['upload_icon'])
  875.                         {
  876.                                 $upload_icon = '<img src="' . $phpbb_root_path . $config['upload_icons_path'] . '/' . trim($extensions[$attachment['extension']]['upload_icon']) . '" alt="" />';
  877.                         }
  878.                 }
  879.  
  880.                 $filesize = get_formatted_filesize($attachment['filesize'], false);
  881.  
  882.                 $comment = bbcode_nl2br(censor_text($attachment['attach_comment']));
  883.  
  884.                 $block_array += array(
  885.                         'UPLOAD_ICON'           => $upload_icon,
  886.                         'FILESIZE'                      => $filesize['value'],
  887.                         'SIZE_LANG'                     => $filesize['unit'],
  888.                         'DOWNLOAD_NAME'         => utf8_basename($attachment['real_filename']),
  889.                         'COMMENT'                       => $comment,
  890.                 );
  891.  
  892.                 $denied = false;
  893.  
  894.                 if (!extension_allowed($forum_id, $attachment['extension'], $extensions))
  895.                 {
  896.                         $denied = true;
  897.  
  898.                         $block_array += array(
  899.                                 'S_DENIED'                      => true,
  900.                                 'DENIED_MESSAGE'        => sprintf($user->lang['EXTENSION_DISABLED_AFTER_POSTING'], $attachment['extension'])
  901.                         );
  902.                 }
  903.  
  904.                 if (!$denied)
  905.                 {
  906.                         $l_downloaded_viewed = $download_link = '';
  907.                         $display_cat = $extensions[$attachment['extension']]['display_cat'];
  908.  
  909.                         if ($display_cat == ATTACHMENT_CATEGORY_IMAGE)
  910.                         {
  911.                                 if ($attachment['thumbnail'])
  912.                                 {
  913.                                         $display_cat = ATTACHMENT_CATEGORY_THUMB;
  914.                                 }
  915.                                 else
  916.                                 {
  917.                                         if ($config['img_display_inlined'])
  918.                                         {
  919.                                                 if ($config['img_link_width'] || $config['img_link_height'])
  920.                                                 {
  921.                                                         $dimension = @getimagesize($filename);
  922.  
  923.                                                         // If the dimensions could not be determined or the image being 0x0 we display it as a link for safety purposes
  924.                                                         if ($dimension === false || empty($dimension[0]) || empty($dimension[1]))
  925.                                                         {
  926.                                                                 $display_cat = ATTACHMENT_CATEGORY_NONE;
  927.                                                         }
  928.                                                         else
  929.                                                         {
  930.                                                                 $display_cat = ($dimension[0] <= $config['img_link_width'] && $dimension[1] <= $config['img_link_height']) ? ATTACHMENT_CATEGORY_IMAGE : ATTACHMENT_CATEGORY_NONE;
  931.                                                         }
  932.                                                 }
  933.                                         }
  934.                                         else
  935.                                         {
  936.                                                 $display_cat = ATTACHMENT_CATEGORY_NONE;
  937.                                         }
  938.                                 }
  939.                         }
  940.  
  941.                         // Make some descisions based on user options being set.
  942.                         if (($display_cat == ATTACHMENT_CATEGORY_IMAGE || $display_cat == ATTACHMENT_CATEGORY_THUMB) && !$user->optionget('viewimg'))
  943.                         {
  944.                                 $display_cat = ATTACHMENT_CATEGORY_NONE;
  945.                         }
  946.  
  947.                         if ($display_cat == ATTACHMENT_CATEGORY_FLASH && !$user->optionget('viewflash'))
  948.                         {
  949.                                 $display_cat = ATTACHMENT_CATEGORY_NONE;
  950.                         }
  951.  
  952.                         $download_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
  953.  
  954.                         switch ($display_cat)
  955.                         {
  956.                                 // Images
  957.                                 case ATTACHMENT_CATEGORY_IMAGE:
  958.                                         $l_downloaded_viewed = 'VIEWED_COUNT';
  959.                                         $inline_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id']);
  960.                                         $download_link .= '&amp;mode=view';
  961.  
  962.                                         $block_array += array(
  963.                                                 'S_IMAGE'               => true,
  964.                                                 'U_INLINE_LINK'         => $inline_link,
  965.                                         );
  966.  
  967.                                         $update_count[] = $attachment['attach_id'];
  968.                                 break;
  969.  
  970.                                 // Images, but display Thumbnail
  971.                                 case ATTACHMENT_CATEGORY_THUMB:
  972.                                         $l_downloaded_viewed = 'VIEWED_COUNT';
  973.                                         $thumbnail_link = append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . $attachment['attach_id'] . '&amp;t=1');
  974.                                         $download_link .= '&amp;mode=view';
  975.  
  976.                                         $block_array += array(
  977.                                                 'S_THUMBNAIL'           => true,
  978.                                                 'THUMB_IMAGE'           => $thumbnail_link,
  979.                                         );
  980.  
  981.                                         $update_count[] = $attachment['attach_id'];
  982.                                 break;
  983.  
  984.                                 // Windows Media Streams
  985.                                 case ATTACHMENT_CATEGORY_WM:
  986.                                         $l_downloaded_viewed = 'VIEWED_COUNT';
  987.  
  988.                                         // Giving the filename directly because within the wm object all variables are in local context making it impossible
  989.                                         // to validate against a valid session (all params can differ)
  990.                                         // $download_link = $filename;
  991.  
  992.                                         $block_array += array(
  993.                                                 'U_FORUM'               => generate_board_url(),
  994.                                                 'ATTACH_ID'             => $attachment['attach_id'],
  995.                                                 'S_WM_FILE'             => true,
  996.                                         );
  997.  
  998.                                         // Viewed/Heared File ... update the download count
  999.                                         $update_count[] = $attachment['attach_id'];
  1000.                                 break;
  1001.  
  1002.                                 // Real Media Streams
  1003.                                 case ATTACHMENT_CATEGORY_RM:
  1004.                                 case ATTACHMENT_CATEGORY_QUICKTIME:
  1005.                                         $l_downloaded_viewed = 'VIEWED_COUNT';
  1006.  
  1007.                                         $block_array += array(
  1008.                                                 'S_RM_FILE'                     => ($display_cat == ATTACHMENT_CATEGORY_RM) ? true : false,
  1009.                                                 'S_QUICKTIME_FILE'      => ($display_cat == ATTACHMENT_CATEGORY_QUICKTIME) ? true : false,
  1010.                                                 'U_FORUM'                       => generate_board_url(),
  1011.                                                 'ATTACH_ID'                     => $attachment['attach_id'],
  1012.                                         );
  1013.  
  1014.                                         // Viewed/Heared File ... update the download count
  1015.                                         $update_count[] = $attachment['attach_id'];
  1016.                                 break;
  1017.  
  1018.                                 // Macromedia Flash Files
  1019.                                 case ATTACHMENT_CATEGORY_FLASH:
  1020.                                         list($width, $height) = @getimagesize($filename);
  1021.  
  1022.                                         $l_downloaded_viewed = 'VIEWED_COUNT';
  1023.  
  1024.                                         $block_array += array(
  1025.                                                 'S_FLASH_FILE'  => true,
  1026.                                                 'WIDTH'                 => $width,
  1027.                                                 'HEIGHT'                => $height,
  1028.                                                 'U_VIEW_LINK'   => $download_link . '&amp;view=1',
  1029.                                         );
  1030.  
  1031.                                         // Viewed/Heared File ... update the download count
  1032.                                         $update_count[] = $attachment['attach_id'];
  1033.                                 break;
  1034.  
  1035.                                 default:
  1036.                                         $l_downloaded_viewed = 'DOWNLOAD_COUNT';
  1037.  
  1038.                                         $block_array += array(
  1039.                                                 'S_FILE'                => true,
  1040.                                         );
  1041.                                 break;
  1042.                         }
  1043.  
  1044.                         $l_download_count = (!isset($attachment['download_count']) || $attachment['download_count'] == 0) ? $user->lang[$l_downloaded_viewed . '_NONE'] : (($attachment['download_count'] == 1) ? sprintf($user->lang[$l_downloaded_viewed], $attachment['download_count']) : sprintf($user->lang[$l_downloaded_viewed . 'S'], $attachment['download_count']));
  1045.  
  1046.                         $block_array += array(
  1047.                                 'U_DOWNLOAD_LINK'               => $download_link,
  1048.                                 'L_DOWNLOAD_COUNT'              => $l_download_count
  1049.                         );
  1050.                 }
  1051.  
  1052.                 $template->assign_block_vars('_file', $block_array);
  1053.  
  1054.                 $compiled_attachments[] = $template->assign_display('attachment_tpl');
  1055.         }
  1056.  
  1057.         $attachments = $compiled_attachments;
  1058.         unset($compiled_attachments);
  1059.  
  1060.         $tpl_size = sizeof($attachments);
  1061.  
  1062.         $unset_tpl = array();
  1063.  
  1064.         preg_match_all('#<!\-\- ia([0-9]+) \-\->(.*?)<!\-\- ia\1 \-\->#', $message, $matches, PREG_PATTERN_ORDER);
  1065.  
  1066.         $replace = array();
  1067.         foreach ($matches[0] as $num => $capture)
  1068.         {
  1069.                 // Flip index if we are displaying the reverse way
  1070.                 $index = ($config['display_order']) ? ($tpl_size-($matches[1][$num] + 1)) : $matches[1][$num];
  1071.  
  1072.                 $replace['from'][] = $matches[0][$num];
  1073.                 $replace['to'][] = (isset($attachments[$index])) ? $attachments[$index] : sprintf($user->lang['MISSING_INLINE_ATTACHMENT'], $matches[2][array_search($index, $matches[1])]);
  1074.  
  1075.                 $unset_tpl[] = $index;
  1076.         }
  1077.  
  1078.         if (isset($replace['from']))
  1079.         {
  1080.                 $message = str_replace($replace['from'], $replace['to'], $message);
  1081.         }
  1082.  
  1083.         $unset_tpl = array_unique($unset_tpl);
  1084.  
  1085.         // Needed to let not display the inlined attachments at the end of the post again
  1086.         foreach ($unset_tpl as $index)
  1087.         {
  1088.                 unset($attachments[$index]);
  1089.         }
  1090. }
  1091.  
  1092. /**
  1093. * Check if extension is allowed to be posted.
  1094. *
  1095. * @param mixed $forum_id The forum id to check or false if private message
  1096. * @param string $extension The extension to check, for example zip.
  1097. * @param array &$extensions The extension array holding the information from the cache (will be obtained if empty)
  1098. *
  1099. * @return bool False if the extension is not allowed to be posted, else true.
  1100. */
  1101. function extension_allowed($forum_id, $extension, &$extensions)
  1102. {
  1103.         if (empty($extensions))
  1104.         {
  1105.                 global $cache;
  1106.                 $extensions = $cache->obtain_attach_extensions($forum_id);
  1107.         }
  1108.  
  1109.         return (!isset($extensions['_allowed_'][$extension])) ? false : true;
  1110. }
  1111.  
  1112. /**
  1113. * Truncates string while retaining special characters if going over the max length
  1114. * The default max length is 60 at the moment
  1115. * The maximum storage length is there to fit the string within the given length. The string may be further truncated due to html entities.
  1116. * For example: string given is 'a "quote"' (length: 9), would be a stored as 'a &quot;quote&quot;' (length: 19)
  1117. *
  1118. * @param string $string The text to truncate to the given length. String is specialchared.
  1119. * @param int $max_length Maximum length of string (multibyte character count as 1 char / Html entity count as 1 char)
  1120. * @param int $max_store_length Maximum character length of string (multibyte character count as 1 char / Html entity count as entity chars).
  1121. * @param bool $allow_reply Allow Re: in front of string
  1122. * @param string $append String to be appended
  1123. */
  1124. function truncate_string($string, $max_length = 60, $max_store_length = 255, $allow_reply = true, $append = '')
  1125. {
  1126.         $chars = array();
  1127.  
  1128.         $strip_reply = false;
  1129.         $stripped = false;
  1130.         if ($allow_reply && strpos($string, 'Re: ') === 0)
  1131.         {
  1132.                 $strip_reply = true;
  1133.                 $string = substr($string, 4);
  1134.         }
  1135.  
  1136.         $_chars = utf8_str_split(htmlspecialchars_decode($string));
  1137.         $chars = array_map('utf8_htmlspecialchars', $_chars);
  1138.  
  1139.         // Now check the length ;)
  1140.         if (sizeof($chars) > $max_length)
  1141.         {
  1142.                 // Cut off the last elements from the array
  1143.                 $string = implode('', array_slice($chars, 0, $max_length - utf8_strlen($append)));
  1144.                 $stripped = true;
  1145.         }
  1146.  
  1147.         // Due to specialchars, we may not be able to store the string...
  1148.         if (utf8_strlen($string) > $max_store_length)
  1149.         {
  1150.                 // let's split again, we do not want half-baked strings where entities are split
  1151.                 $_chars = utf8_str_split(htmlspecialchars_decode($string));
  1152.                 $chars = array_map('utf8_htmlspecialchars', $_chars);
  1153.  
  1154.                 do
  1155.                 {
  1156.                         array_pop($chars);
  1157.                         $string = implode('', $chars);
  1158.                 }
  1159.                 while (!empty($chars) && utf8_strlen($string) > $max_store_length);
  1160.         }
  1161.  
  1162.         if ($strip_reply)
  1163.         {
  1164.                 $string = 'Re: ' . $string;
  1165.         }
  1166.  
  1167.         if ($append != '' && $stripped)
  1168.         {
  1169.                 $string = $string . $append;
  1170.         }
  1171.  
  1172.         return $string;
  1173. }
  1174.  
  1175. /**
  1176. * Get username details for placing into templates.
  1177. * This function caches all modes on first call, except for no_profile and anonymous user - determined by $user_id.
  1178. *
  1179. * @param string $mode Can be profile (for getting an url to the profile), username (for obtaining the username), colour (for obtaining the user colour), full (for obtaining a html string representing a coloured link to the users profile) or no_profile (the same as full but forcing no profile link)
  1180. * @param int $user_id The users id
  1181. * @param string $username The users name
  1182. * @param string $username_colour The users colour
  1183. * @param string $guest_username optional parameter to specify the guest username. It will be used in favor of the GUEST language variable then.
  1184. * @param string $custom_profile_url optional parameter to specify a profile url. The user id get appended to this url as &amp;u={user_id}
  1185. *
  1186. * @return string A string consisting of what is wanted based on $mode.
  1187. * @author BartVB, Acyd Burn
  1188. */
  1189. function get_username_string($mode, $user_id, $username, $username_colour = '', $guest_username = false, $custom_profile_url = false)
  1190. {
  1191.         static $_profile_cache;
  1192.  
  1193.         // We cache some common variables we need within this function
  1194.         if (empty($_profile_cache))
  1195.         {
  1196.                 global $phpbb_root_path, $phpEx;
  1197.  
  1198.                 $_profile_cache['base_url'] = append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=viewprofile&amp;u={USER_ID}');
  1199.                 $_profile_cache['tpl_noprofile'] = '{USERNAME}';
  1200.                 $_profile_cache['tpl_noprofile_colour'] = '<span style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</span>';
  1201.                 $_profile_cache['tpl_profile'] = '<a href="{PROFILE_URL}">{USERNAME}</a>';
  1202.                 $_profile_cache['tpl_profile_colour'] = '<a href="{PROFILE_URL}" style="color: {USERNAME_COLOUR};" class="username-coloured">{USERNAME}</a>';
  1203.         }
  1204.  
  1205.         global $user, $auth;
  1206.  
  1207.         // This switch makes sure we only run code required for the mode
  1208.         switch ($mode)
  1209.         {
  1210.                 case 'full':
  1211.                 case 'no_profile':
  1212.                 case 'colour':
  1213.  
  1214.                         // Build correct username colour
  1215.                         $username_colour = ($username_colour) ? '#' . $username_colour : '';
  1216.  
  1217.                         // Return colour
  1218.                         if ($mode == 'colour')
  1219.                         {
  1220.                                 return $username_colour;
  1221.                         }
  1222.  
  1223.                 // no break;
  1224.  
  1225.                 case 'username':
  1226.  
  1227.                         // Build correct username
  1228.                         if ($guest_username === false)
  1229.                         {
  1230.                                 $username = ($username) ? $username : $user->lang['GUEST'];
  1231.                         }
  1232.                         else
  1233.                         {
  1234.                                 $username = ($user_id && $user_id != ANONYMOUS) ? $username : ((!empty($guest_username)) ? $guest_username : $user->lang['GUEST']);
  1235.                         }
  1236.  
  1237.                         // Return username
  1238.                         if ($mode == 'username')
  1239.                         {
  1240.                                 return $username;
  1241.                         }
  1242.  
  1243.                 // no break;
  1244.  
  1245.                 case 'profile':
  1246.  
  1247.                         // Build correct profile url - only show if not anonymous and permission to view profile if registered user
  1248.                         // For anonymous the link leads to a login page.
  1249.                         if ($user_id && $user_id != ANONYMOUS && ($user->data['user_id'] == ANONYMOUS || $auth->acl_get('u_viewprofile')))
  1250.                         {
  1251.                                 $profile_url = ($custom_profile_url !== false) ? $custom_profile_url . '&amp;u=' . (int) $user_id : str_replace(array('={USER_ID}', '=%7BUSER_ID%7D'), '=' . (int) $user_id, $_profile_cache['base_url']);
  1252.                         }
  1253.                         else
  1254.                         {
  1255.                                 $profile_url = '';
  1256.                         }
  1257.  
  1258.                         // Return profile
  1259.                         if ($mode == 'profile')
  1260.                         {
  1261.                                 return $profile_url;
  1262.                         }
  1263.  
  1264.                 // no break;
  1265.         }
  1266.  
  1267.         if (($mode == 'full' && !$profile_url) || $mode == 'no_profile')
  1268.         {
  1269.                 return str_replace(array('{USERNAME_COLOUR}', '{USERNAME}'), array($username_colour, $username), (!$username_colour) ? $_profile_cache['tpl_noprofile'] : $_profile_cache['tpl_noprofile_colour']);
  1270.         }
  1271.  
  1272.         return str_replace(array('{PROFILE_URL}', '{USERNAME_COLOUR}', '{USERNAME}'), array($profile_url, $username_colour, $username), (!$username_colour) ? $_profile_cache['tpl_profile'] : $_profile_cache['tpl_profile_colour']);
  1273. }
  1274.  
  1275. /**
  1276. * @package phpBB3
  1277. */
  1278. class bitfield
  1279. {
  1280.         var $data;
  1281.  
  1282.         function bitfield($bitfield = '')
  1283.         {
  1284.                 $this->data = base64_decode($bitfield);
  1285.         }
  1286.  
  1287.         /**
  1288.         */
  1289.         function get($n)
  1290.         {
  1291.                 // Get the ($n / 8)th char
  1292.                 $byte = $n >> 3;
  1293.  
  1294.                 if (strlen($this->data) >= $byte + 1)
  1295.                 {
  1296.                         $c = $this->data[$byte];
  1297.  
  1298.                         // Lookup the ($n % 8)th bit of the byte
  1299.                         $bit = 7 - ($n & 7);
  1300.                         return (bool) (ord($c) & (1 << $bit));
  1301.                 }
  1302.                 else
  1303.                 {
  1304.                         return false;
  1305.                 }
  1306.         }
  1307.  
  1308.         function set($n)
  1309.         {
  1310.                 $byte = $n >> 3;
  1311.                 $bit = 7 - ($n & 7);
  1312.  
  1313.                 if (strlen($this->data) >= $byte + 1)
  1314.                 {
  1315.                         $this->data[$byte] = $this->data[$byte] | chr(1 << $bit);
  1316.                 }
  1317.                 else
  1318.                 {
  1319.                         $this->data .= str_repeat("\0", $byte - strlen($this->data));
  1320.                         $this->data .= chr(1 << $bit);
  1321.                 }
  1322.         }
  1323.  
  1324.         function clear($n)
  1325.         {
  1326.                 $byte = $n >> 3;
  1327.  
  1328.                 if (strlen($this->data) >= $byte + 1)
  1329.                 {
  1330.                         $bit = 7 - ($n & 7);
  1331.                         $this->data[$byte] = $this->data[$byte] &~ chr(1 << $bit);
  1332.                 }
  1333.         }
  1334.  
  1335.         function get_blob()
  1336.         {
  1337.                 return $this->data;
  1338.         }
  1339.  
  1340.         function get_base64()
  1341.         {
  1342.                 return base64_encode($this->data);
  1343.         }
  1344.  
  1345.         function get_bin()
  1346.         {
  1347.                 $bin = '';
  1348.                 $len = strlen($this->data);
  1349.  
  1350.                 for ($i = 0; $i < $len; ++$i)
  1351.                 {
  1352.                         $bin .= str_pad(decbin(ord($this->data[$i])), 8, '0', STR_PAD_LEFT);
  1353.                 }
  1354.  
  1355.                 return $bin;
  1356.         }
  1357.  
  1358.         function get_all_set()
  1359.         {
  1360.                 return array_keys(array_filter(str_split($this->get_bin())));
  1361.         }
  1362.  
  1363.         function merge($bitfield)
  1364.         {
  1365.                 $this->data = $this->data | $bitfield->get_blob();
  1366.         }
  1367. }
  1368.  
  1369. ?>
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: