indvdum@0: bbcodes) indvdum@0: { indvdum@0: $this->bbcode_init(); indvdum@0: } indvdum@0: indvdum@0: global $user; indvdum@0: indvdum@0: $this->bbcode_bitfield = ''; indvdum@0: $bitfield = new bitfield(); indvdum@0: indvdum@0: foreach ($this->bbcodes as $bbcode_name => $bbcode_data) indvdum@0: { indvdum@0: if (isset($bbcode_data['disabled']) && $bbcode_data['disabled']) indvdum@0: { indvdum@0: foreach ($bbcode_data['regexp'] as $regexp => $replacement) indvdum@0: { indvdum@0: if (preg_match($regexp, $this->message)) indvdum@0: { indvdum@0: $this->warn_msg[] = sprintf($user->lang['UNAUTHORISED_BBCODE'] , '[' . $bbcode_name . ']'); indvdum@0: continue; indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: foreach ($bbcode_data['regexp'] as $regexp => $replacement) indvdum@0: { indvdum@0: // The pattern gets compiled and cached by the PCRE extension, indvdum@0: // it should not demand recompilation indvdum@0: if (preg_match($regexp, $this->message)) indvdum@0: { indvdum@0: $this->message = preg_replace($regexp, $replacement, $this->message); indvdum@0: $bitfield->set($bbcode_data['bbcode_id']); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $this->bbcode_bitfield = $bitfield->get_base64(); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Prepare some bbcodes for better parsing indvdum@0: */ indvdum@0: function prepare_bbcodes() indvdum@0: { indvdum@0: // Ok, seems like users instead want the no-parsing of urls, smilies, etc. after and before and within quote tags being tagged as "not a bug". indvdum@0: // Fine by me ;) Will ease our live... but do not come back and cry at us, we won't hear you. indvdum@0: indvdum@0: /* Add newline at the end and in front of each quote block to prevent parsing errors (urls, smilies, etc.) indvdum@0: if (strpos($this->message, '[quote') !== false && strpos($this->message, '[/quote]') !== false) indvdum@0: { indvdum@0: $this->message = str_replace("\r\n", "\n", $this->message); indvdum@0: indvdum@0: // We strip newlines and spaces after and before quotes in quotes (trimming) and then add exactly one newline indvdum@0: $this->message = preg_replace('#\[quote(=".*?")?\]\s*(.*?)\s*\[/quote\]#siu', '[quote\1]' . "\n" . '\2' ."\n[/quote]", $this->message); indvdum@0: } indvdum@0: */ indvdum@0: indvdum@0: // Add other checks which needs to be placed before actually parsing anything (be it bbcodes, smilies, urls...) indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Init bbcode data for later parsing indvdum@0: */ indvdum@0: function bbcode_init($allow_custom_bbcode = true) indvdum@0: { indvdum@0: static $rowset; indvdum@0: indvdum@0: // This array holds all bbcode data. BBCodes will be processed in this indvdum@0: // order, so it is important to keep [code] in first position and indvdum@0: // [quote] in second position. indvdum@0: // To parse multiline URL we enable dotall option setting only for URL text indvdum@0: // but not for link itself, thus [url][/url] is not affected. indvdum@0: $this->bbcodes = array( indvdum@0: 'code' => array('bbcode_id' => 8, 'regexp' => array('#\[code(?:=([a-z]+))?\](.+\[/code\])#uise' => "\$this->bbcode_code('\$1', '\$2')")), indvdum@0: 'quote' => array('bbcode_id' => 0, 'regexp' => array('#\[quote(?:="(.*?)")?\](.+)\[/quote\]#uise' => "\$this->bbcode_quote('\$0')")), indvdum@0: 'attachment' => array('bbcode_id' => 12, 'regexp' => array('#\[attachment=([0-9]+)\](.*?)\[/attachment\]#uise' => "\$this->bbcode_attachment('\$1', '\$2')")), indvdum@0: 'b' => array('bbcode_id' => 1, 'regexp' => array('#\[b\](.*?)\[/b\]#uise' => "\$this->bbcode_strong('\$1')")), indvdum@0: 'i' => array('bbcode_id' => 2, 'regexp' => array('#\[i\](.*?)\[/i\]#uise' => "\$this->bbcode_italic('\$1')")), indvdum@0: 'url' => array('bbcode_id' => 3, 'regexp' => array('#\[url(=(.*))?\](?(1)((?s).*(?-s))|(.*))\[/url\]#uiUe' => "\$this->validate_url('\$2', ('\$3') ? '\$3' : '\$4')")), indvdum@0: 'img' => array('bbcode_id' => 4, 'regexp' => array('#\[img\](.*)\[/img\]#uiUe' => "\$this->bbcode_img('\$1')")), indvdum@0: 'size' => array('bbcode_id' => 5, 'regexp' => array('#\[size=([\-\+]?\d+)\](.*?)\[/size\]#uise' => "\$this->bbcode_size('\$1', '\$2')")), indvdum@0: 'color' => array('bbcode_id' => 6, 'regexp' => array('!\[color=(#[0-9a-f]{3}|#[0-9a-f]{6}|[a-z\-]+)\](.*?)\[/color\]!uise' => "\$this->bbcode_color('\$1', '\$2')")), indvdum@0: 'u' => array('bbcode_id' => 7, 'regexp' => array('#\[u\](.*?)\[/u\]#uise' => "\$this->bbcode_underline('\$1')")), indvdum@0: 'list' => array('bbcode_id' => 9, 'regexp' => array('#\[list(?:=(?:[a-z0-9]|disc|circle|square))?].*\[/list]#uise' => "\$this->bbcode_parse_list('\$0')")), indvdum@0: 'email' => array('bbcode_id' => 10, 'regexp' => array('#\[email=?(.*?)?\](.*?)\[/email\]#uise' => "\$this->validate_email('\$1', '\$2')")), indvdum@0: 'flash' => array('bbcode_id' => 11, 'regexp' => array('#\[flash=([0-9]+),([0-9]+)\](.*?)\[/flash\]#uie' => "\$this->bbcode_flash('\$1', '\$2', '\$3')")) indvdum@0: ); indvdum@0: indvdum@0: // Zero the parsed items array indvdum@0: $this->parsed_items = array(); indvdum@0: indvdum@0: foreach ($this->bbcodes as $tag => $bbcode_data) indvdum@0: { indvdum@0: $this->parsed_items[$tag] = 0; indvdum@0: } indvdum@0: indvdum@0: if (!$allow_custom_bbcode) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: if (!is_array($rowset)) indvdum@0: { indvdum@0: global $db; indvdum@0: $rowset = array(); indvdum@0: indvdum@0: $sql = 'SELECT * indvdum@0: FROM ' . BBCODES_TABLE; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $rowset[] = $row; indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: foreach ($rowset as $row) indvdum@0: { indvdum@0: $this->bbcodes[$row['bbcode_tag']] = array( indvdum@0: 'bbcode_id' => (int) $row['bbcode_id'], indvdum@0: 'regexp' => array($row['first_pass_match'] => str_replace('$uid', $this->bbcode_uid, $row['first_pass_replace'])) indvdum@0: ); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Making some pre-checks for bbcodes as well as increasing the number of parsed items indvdum@0: */ indvdum@0: function check_bbcode($bbcode, &$in) indvdum@0: { indvdum@0: // when using the /e modifier, preg_replace slashes double-quotes but does not indvdum@0: // seem to slash anything else indvdum@0: $in = str_replace("\r\n", "\n", str_replace('\"', '"', $in)); indvdum@0: indvdum@0: // Trimming here to make sure no empty bbcodes are parsed accidently indvdum@0: if (trim($in) == '') indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: $this->parsed_items[$bbcode]++; indvdum@0: indvdum@0: return true; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Transform some characters in valid bbcodes indvdum@0: */ indvdum@0: function bbcode_specialchars($text) indvdum@0: { indvdum@0: $str_from = array('<', '>', '[', ']', '.', ':'); indvdum@0: $str_to = array('<', '>', '[', ']', '.', ':'); indvdum@0: indvdum@0: return str_replace($str_from, $str_to, $text); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse size tag indvdum@0: */ indvdum@0: function bbcode_size($stx, $in) indvdum@0: { indvdum@0: global $user, $config; indvdum@0: indvdum@0: if (!$this->check_bbcode('size', $in)) indvdum@0: { indvdum@0: return $in; indvdum@0: } indvdum@0: indvdum@0: if ($config['max_' . $this->mode . '_font_size'] && $config['max_' . $this->mode . '_font_size'] < $stx) indvdum@0: { indvdum@0: $this->warn_msg[] = sprintf($user->lang['MAX_FONT_SIZE_EXCEEDED'], $config['max_' . $this->mode . '_font_size']); indvdum@0: indvdum@0: return '[size=' . $stx . ']' . $in . '[/size]'; indvdum@0: } indvdum@0: indvdum@0: // Do not allow size=0 indvdum@0: if ($stx <= 0) indvdum@0: { indvdum@0: return '[size=' . $stx . ']' . $in . '[/size]'; indvdum@0: } indvdum@0: indvdum@0: return '[size=' . $stx . ':' . $this->bbcode_uid . ']' . $in . '[/size:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse color tag indvdum@0: */ indvdum@0: function bbcode_color($stx, $in) indvdum@0: { indvdum@0: if (!$this->check_bbcode('color', $in)) indvdum@0: { indvdum@0: return $in; indvdum@0: } indvdum@0: indvdum@0: return '[color=' . $stx . ':' . $this->bbcode_uid . ']' . $in . '[/color:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse u tag indvdum@0: */ indvdum@0: function bbcode_underline($in) indvdum@0: { indvdum@0: if (!$this->check_bbcode('u', $in)) indvdum@0: { indvdum@0: return $in; indvdum@0: } indvdum@0: indvdum@0: return '[u:' . $this->bbcode_uid . ']' . $in . '[/u:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse b tag indvdum@0: */ indvdum@0: function bbcode_strong($in) indvdum@0: { indvdum@0: if (!$this->check_bbcode('b', $in)) indvdum@0: { indvdum@0: return $in; indvdum@0: } indvdum@0: indvdum@0: return '[b:' . $this->bbcode_uid . ']' . $in . '[/b:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse i tag indvdum@0: */ indvdum@0: function bbcode_italic($in) indvdum@0: { indvdum@0: if (!$this->check_bbcode('i', $in)) indvdum@0: { indvdum@0: return $in; indvdum@0: } indvdum@0: indvdum@0: return '[i:' . $this->bbcode_uid . ']' . $in . '[/i:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse img tag indvdum@0: */ indvdum@0: function bbcode_img($in) indvdum@0: { indvdum@0: global $user, $config; indvdum@0: indvdum@0: if (!$this->check_bbcode('img', $in)) indvdum@0: { indvdum@0: return $in; indvdum@0: } indvdum@0: indvdum@0: $in = trim($in); indvdum@0: $error = false; indvdum@0: indvdum@0: $in = str_replace(' ', '%20', $in); indvdum@0: indvdum@0: // Checking urls indvdum@0: if (!preg_match('#^' . get_preg_expression('url') . '$#i', $in) && !preg_match('#^' . get_preg_expression('www_url') . '$#i', $in)) indvdum@0: { indvdum@0: return '[img]' . $in . '[/img]'; indvdum@0: } indvdum@0: indvdum@0: // Try to cope with a common user error... not specifying a protocol but only a subdomain indvdum@0: if (!preg_match('#^[a-z0-9]+://#i', $in)) indvdum@0: { indvdum@0: $in = 'http://' . $in; indvdum@0: } indvdum@0: indvdum@0: if ($config['max_' . $this->mode . '_img_height'] || $config['max_' . $this->mode . '_img_width']) indvdum@0: { indvdum@0: $stats = @getimagesize(htmlspecialchars_decode($in)); indvdum@0: indvdum@0: if ($stats === false) indvdum@0: { indvdum@0: $error = true; indvdum@0: $this->warn_msg[] = $user->lang['UNABLE_GET_IMAGE_SIZE']; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: if ($config['max_' . $this->mode . '_img_height'] && $config['max_' . $this->mode . '_img_height'] < $stats[1]) indvdum@0: { indvdum@0: $error = true; indvdum@0: $this->warn_msg[] = sprintf($user->lang['MAX_IMG_HEIGHT_EXCEEDED'], $config['max_' . $this->mode . '_img_height']); indvdum@0: } indvdum@0: indvdum@0: if ($config['max_' . $this->mode . '_img_width'] && $config['max_' . $this->mode . '_img_width'] < $stats[0]) indvdum@0: { indvdum@0: $error = true; indvdum@0: $this->warn_msg[] = sprintf($user->lang['MAX_IMG_WIDTH_EXCEEDED'], $config['max_' . $this->mode . '_img_width']); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if ($error || $this->path_in_domain($in)) indvdum@0: { indvdum@0: return '[img]' . $in . '[/img]'; indvdum@0: } indvdum@0: indvdum@0: return '[img:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($in) . '[/img:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse flash tag indvdum@0: */ indvdum@0: function bbcode_flash($width, $height, $in) indvdum@0: { indvdum@0: global $user, $config; indvdum@0: indvdum@0: if (!$this->check_bbcode('flash', $in)) indvdum@0: { indvdum@0: return $in; indvdum@0: } indvdum@0: indvdum@0: $in = trim($in); indvdum@0: $error = false; indvdum@0: indvdum@0: // Do not allow 0-sizes generally being entered indvdum@0: if ($width <= 0 || $height <= 0) indvdum@0: { indvdum@0: return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]'; indvdum@0: } indvdum@0: indvdum@0: $in = str_replace(' ', '%20', $in); indvdum@0: indvdum@0: // Make sure $in is a URL. indvdum@0: if (!preg_match('#^' . get_preg_expression('url') . '$#i', $in) && indvdum@0: !preg_match('#^' . get_preg_expression('www_url') . '$#i', $in)) indvdum@0: { indvdum@0: return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]'; indvdum@0: } indvdum@0: indvdum@0: // Apply the same size checks on flash files as on images indvdum@0: if ($config['max_' . $this->mode . '_img_height'] || $config['max_' . $this->mode . '_img_width']) indvdum@0: { indvdum@0: if ($config['max_' . $this->mode . '_img_height'] && $config['max_' . $this->mode . '_img_height'] < $height) indvdum@0: { indvdum@0: $error = true; indvdum@0: $this->warn_msg[] = sprintf($user->lang['MAX_FLASH_HEIGHT_EXCEEDED'], $config['max_' . $this->mode . '_img_height']); indvdum@0: } indvdum@0: indvdum@0: if ($config['max_' . $this->mode . '_img_width'] && $config['max_' . $this->mode . '_img_width'] < $width) indvdum@0: { indvdum@0: $error = true; indvdum@0: $this->warn_msg[] = sprintf($user->lang['MAX_FLASH_WIDTH_EXCEEDED'], $config['max_' . $this->mode . '_img_width']); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if ($error || $this->path_in_domain($in)) indvdum@0: { indvdum@0: return '[flash=' . $width . ',' . $height . ']' . $in . '[/flash]'; indvdum@0: } indvdum@0: indvdum@0: return '[flash=' . $width . ',' . $height . ':' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($in) . '[/flash:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse inline attachments [ia] indvdum@0: */ indvdum@0: function bbcode_attachment($stx, $in) indvdum@0: { indvdum@0: if (!$this->check_bbcode('attachment', $in)) indvdum@0: { indvdum@0: return $in; indvdum@0: } indvdum@0: indvdum@0: return '[attachment=' . $stx . ':' . $this->bbcode_uid . ']' . trim($in) . '[/attachment:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse code text from code tag indvdum@0: * @access private indvdum@0: */ indvdum@0: function bbcode_parse_code($stx, &$code) indvdum@0: { indvdum@0: switch (strtolower($stx)) indvdum@0: { indvdum@0: case 'php': indvdum@0: indvdum@0: $remove_tags = false; indvdum@0: indvdum@0: $str_from = array('<', '>', '[', ']', '.', ':', ':'); indvdum@0: $str_to = array('<', '>', '[', ']', '.', ':', ':'); indvdum@0: $code = str_replace($str_from, $str_to, $code); indvdum@0: indvdum@0: if (!preg_match('/\<\?.*?\?\>/is', $code)) indvdum@0: { indvdum@0: $remove_tags = true; indvdum@0: $code = ""; indvdum@0: } indvdum@0: indvdum@0: $conf = array('highlight.bg', 'highlight.comment', 'highlight.default', 'highlight.html', 'highlight.keyword', 'highlight.string'); indvdum@0: foreach ($conf as $ini_var) indvdum@0: { indvdum@0: @ini_set($ini_var, str_replace('highlight.', 'syntax', $ini_var)); indvdum@0: } indvdum@0: indvdum@0: // Because highlight_string is specialcharing the text (but we already did this before), we have to reverse this in order to get correct results indvdum@0: $code = htmlspecialchars_decode($code); indvdum@0: $code = highlight_string($code, true); indvdum@0: indvdum@0: $str_from = array('', '', '','[', ']', '.', ':'); indvdum@0: $str_to = array('', '', '', '[', ']', '.', ':'); indvdum@0: indvdum@0: if ($remove_tags) indvdum@0: { indvdum@0: $str_from[] = '<?php '; indvdum@0: $str_to[] = ''; indvdum@0: $str_from[] = '<?php '; indvdum@0: $str_to[] = ''; indvdum@0: } indvdum@0: indvdum@0: $code = str_replace($str_from, $str_to, $code); indvdum@0: $code = preg_replace('#^()\n?(.*?)\n?()$#is', '$1$2$3', $code); indvdum@0: indvdum@0: if ($remove_tags) indvdum@0: { indvdum@0: $code = preg_replace('#()?\?>()#', '$1 $2', $code); indvdum@0: } indvdum@0: indvdum@0: $code = preg_replace('#^(.*)#s', '$2', $code); indvdum@0: $code = preg_replace('#(?:\s++| )*+$#u', '', $code); indvdum@0: indvdum@0: // remove newline at the end indvdum@0: if (!empty($code) && substr($code, -1) == "\n") indvdum@0: { indvdum@0: $code = substr($code, 0, -1); indvdum@0: } indvdum@0: indvdum@0: return "[code=$stx:" . $this->bbcode_uid . ']' . $code . '[/code:' . $this->bbcode_uid . ']'; indvdum@0: break; indvdum@0: indvdum@0: default: indvdum@0: return '[code:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($code) . '[/code:' . $this->bbcode_uid . ']'; indvdum@0: break; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse code tag indvdum@0: * Expects the argument to start right after the opening [code] tag and to end with [/code] indvdum@0: */ indvdum@0: function bbcode_code($stx, $in) indvdum@0: { indvdum@0: if (!$this->check_bbcode('code', $in)) indvdum@0: { indvdum@0: return $in; indvdum@0: } indvdum@0: indvdum@0: // We remove the hardcoded elements from the code block here because it is not used in code blocks indvdum@0: // Having it here saves us one preg_replace per message containing [code] blocks indvdum@0: // Additionally, magic url parsing should go after parsing bbcodes, but for safety those are stripped out too... indvdum@0: $htm_match = get_preg_expression('bbcode_htm'); indvdum@0: unset($htm_match[4], $htm_match[5]); indvdum@0: $htm_replace = array('\1', '\1', '\2', '\1'); indvdum@0: indvdum@0: $out = $code_block = ''; indvdum@0: $open = 1; indvdum@0: indvdum@0: while ($in) indvdum@0: { indvdum@0: // Determine position and tag length of next code block indvdum@0: preg_match('#(.*?)(\[code(?:=([a-z]+))?\])(.+)#is', $in, $buffer); indvdum@0: $pos = (isset($buffer[1])) ? strlen($buffer[1]) : false; indvdum@0: $tag_length = (isset($buffer[2])) ? strlen($buffer[2]) : false; indvdum@0: indvdum@0: // Determine position of ending code tag indvdum@0: $pos2 = stripos($in, '[/code]'); indvdum@0: indvdum@0: // Which is the next block, ending code or code block indvdum@0: if ($pos !== false && $pos < $pos2) indvdum@0: { indvdum@0: // Open new block indvdum@0: if (!$open) indvdum@0: { indvdum@0: $out .= substr($in, 0, $pos); indvdum@0: $in = substr($in, $pos); indvdum@0: $stx = (isset($buffer[3])) ? $buffer[3] : ''; indvdum@0: $code_block = ''; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Already opened block, just append to the current block indvdum@0: $code_block .= substr($in, 0, $pos) . ((isset($buffer[2])) ? $buffer[2] : ''); indvdum@0: $in = substr($in, $pos); indvdum@0: } indvdum@0: indvdum@0: $in = substr($in, $tag_length); indvdum@0: $open++; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Close the block indvdum@0: if ($open == 1) indvdum@0: { indvdum@0: $code_block .= substr($in, 0, $pos2); indvdum@0: $code_block = preg_replace($htm_match, $htm_replace, $code_block); indvdum@0: indvdum@0: // Parse this code block indvdum@0: $out .= $this->bbcode_parse_code($stx, $code_block); indvdum@0: $code_block = ''; indvdum@0: $open--; indvdum@0: } indvdum@0: else if ($open) indvdum@0: { indvdum@0: // Close one open tag... add to the current code block indvdum@0: $code_block .= substr($in, 0, $pos2 + 7); indvdum@0: $open--; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // end code without opening code... will be always outside code block indvdum@0: $out .= substr($in, 0, $pos2 + 7); indvdum@0: } indvdum@0: indvdum@0: $in = substr($in, $pos2 + 7); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // if now $code_block has contents we need to parse the remaining code while removing the last closing tag to match up. indvdum@0: if ($code_block) indvdum@0: { indvdum@0: $code_block = substr($code_block, 0, -7); indvdum@0: $code_block = preg_replace($htm_match, $htm_replace, $code_block); indvdum@0: indvdum@0: $out .= $this->bbcode_parse_code($stx, $code_block); indvdum@0: } indvdum@0: indvdum@0: return $out; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse list bbcode indvdum@0: * Expects the argument to start with a tag indvdum@0: */ indvdum@0: function bbcode_parse_list($in) indvdum@0: { indvdum@0: if (!$this->check_bbcode('list', $in)) indvdum@0: { indvdum@0: return $in; indvdum@0: } indvdum@0: indvdum@0: // $tok holds characters to stop at. Since the string starts with a '[' we'll get everything up to the first ']' which should be the opening [list] tag indvdum@0: $tok = ']'; indvdum@0: $out = '['; indvdum@0: indvdum@0: // First character is [ indvdum@0: $in = substr($in, 1); indvdum@0: $list_end_tags = $item_end_tags = array(); indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: $pos = strlen($in); indvdum@0: indvdum@0: for ($i = 0, $tok_len = strlen($tok); $i < $tok_len; ++$i) indvdum@0: { indvdum@0: $tmp_pos = strpos($in, $tok[$i]); indvdum@0: indvdum@0: if ($tmp_pos !== false && $tmp_pos < $pos) indvdum@0: { indvdum@0: $pos = $tmp_pos; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $buffer = substr($in, 0, $pos); indvdum@0: $tok = $in[$pos]; indvdum@0: indvdum@0: $in = substr($in, $pos + 1); indvdum@0: indvdum@0: if ($tok == ']') indvdum@0: { indvdum@0: // if $tok is ']' the buffer holds a tag indvdum@0: if (strtolower($buffer) == '/list' && sizeof($list_end_tags)) indvdum@0: { indvdum@0: // valid [/list] tag, check nesting so that we don't hit false positives indvdum@0: if (sizeof($item_end_tags) && sizeof($item_end_tags) >= sizeof($list_end_tags)) indvdum@0: { indvdum@0: // current li tag has not been closed indvdum@0: $out = preg_replace('/\n?\[$/', '[', $out) . array_pop($item_end_tags) . ']['; indvdum@0: } indvdum@0: indvdum@0: $out .= array_pop($list_end_tags) . ']'; indvdum@0: $tok = '['; indvdum@0: } indvdum@0: else if (preg_match('#^list(=[0-9a-z]+)?$#i', $buffer, $m)) indvdum@0: { indvdum@0: // sub-list, add a closing tag indvdum@0: if (empty($m[1]) || preg_match('/^=(?:disc|square|circle)$/i', $m[1])) indvdum@0: { indvdum@0: array_push($list_end_tags, '/list:u:' . $this->bbcode_uid); indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: array_push($list_end_tags, '/list:o:' . $this->bbcode_uid); indvdum@0: } indvdum@0: $out .= 'list' . substr($buffer, 4) . ':' . $this->bbcode_uid . ']'; indvdum@0: $tok = '['; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: if (($buffer == '*' || substr($buffer, -2) == '[*') && sizeof($list_end_tags)) indvdum@0: { indvdum@0: // the buffer holds a bullet tag and we have a [list] tag open indvdum@0: if (sizeof($item_end_tags) >= sizeof($list_end_tags)) indvdum@0: { indvdum@0: if (substr($buffer, -2) == '[*') indvdum@0: { indvdum@0: $out .= substr($buffer, 0, -2) . '['; indvdum@0: } indvdum@0: // current li tag has not been closed indvdum@0: if (preg_match('/\n\[$/', $out, $m)) indvdum@0: { indvdum@0: $out = preg_replace('/\n\[$/', '[', $out); indvdum@0: $buffer = array_pop($item_end_tags) . "]\n[*:" . $this->bbcode_uid; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $buffer = array_pop($item_end_tags) . '][*:' . $this->bbcode_uid; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $buffer = '*:' . $this->bbcode_uid; indvdum@0: } indvdum@0: indvdum@0: $item_end_tags[] = '/*:m:' . $this->bbcode_uid; indvdum@0: } indvdum@0: else if ($buffer == '/*') indvdum@0: { indvdum@0: array_pop($item_end_tags); indvdum@0: $buffer = '/*:' . $this->bbcode_uid; indvdum@0: } indvdum@0: indvdum@0: $out .= $buffer . $tok; indvdum@0: $tok = '[]'; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: // Not within a tag, just add buffer to the return string indvdum@0: $out .= $buffer . $tok; indvdum@0: $tok = ($tok == '[') ? ']' : '[]'; indvdum@0: } indvdum@0: } indvdum@0: while ($in); indvdum@0: indvdum@0: // do we have some tags open? close them now indvdum@0: if (sizeof($item_end_tags)) indvdum@0: { indvdum@0: $out .= '[' . implode('][', $item_end_tags) . ']'; indvdum@0: } indvdum@0: if (sizeof($list_end_tags)) indvdum@0: { indvdum@0: $out .= '[' . implode('][', $list_end_tags) . ']'; indvdum@0: } indvdum@0: indvdum@0: return $out; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse quote bbcode indvdum@0: * Expects the argument to start with a tag indvdum@0: */ indvdum@0: function bbcode_quote($in) indvdum@0: { indvdum@0: global $config, $user; indvdum@0: indvdum@0: /** indvdum@0: * If you change this code, make sure the cases described within the following reports are still working: indvdum@0: * #3572 - [quote="[test]test"]test [ test[/quote] - (correct: parsed) indvdum@0: * #14667 - [quote]test[/quote] test ] and [ test [quote]test[/quote] (correct: parsed) indvdum@0: * #14770 - [quote="["]test[/quote] (correct: parsed) indvdum@0: * [quote="[i]test[/i]"]test[/quote] (correct: parsed) indvdum@0: * [quote="[quote]test[/quote]"]test[/quote] (correct: parsed - Username displayed as [quote]test[/quote]) indvdum@0: * #20735 - [quote]test[/[/b]quote] test [/quote][/quote] test - (correct: quoted: "test[/[/b]quote] test" / non-quoted: "[/quote] test" - also failed if layout distorted) indvdum@0: * #40565 - [quote="a"]a[/quote][quote="a]a[/quote] (correct: first quote tag parsed, second quote tag unparsed) indvdum@0: */ indvdum@0: indvdum@0: $in = str_replace("\r\n", "\n", str_replace('\"', '"', trim($in))); indvdum@0: indvdum@0: if (!$in) indvdum@0: { indvdum@0: return ''; indvdum@0: } indvdum@0: indvdum@0: // To let the parser not catch tokens within quote_username quotes we encode them before we start this... indvdum@0: $in = preg_replace('#quote="(.*?)"\]#ie', "'quote="' . str_replace(array('[', ']', '\\\"'), array('[', ']', '\"'), '\$1') . '"]'", $in); indvdum@0: indvdum@0: $tok = ']'; indvdum@0: $out = '['; indvdum@0: indvdum@0: $in = substr($in, 1); indvdum@0: $close_tags = $error_ary = array(); indvdum@0: $buffer = ''; indvdum@0: indvdum@0: do indvdum@0: { indvdum@0: $pos = strlen($in); indvdum@0: for ($i = 0, $tok_len = strlen($tok); $i < $tok_len; ++$i) indvdum@0: { indvdum@0: $tmp_pos = strpos($in, $tok[$i]); indvdum@0: if ($tmp_pos !== false && $tmp_pos < $pos) indvdum@0: { indvdum@0: $pos = $tmp_pos; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $buffer .= substr($in, 0, $pos); indvdum@0: $tok = $in[$pos]; indvdum@0: $in = substr($in, $pos + 1); indvdum@0: indvdum@0: if ($tok == ']') indvdum@0: { indvdum@0: if (strtolower($buffer) == '/quote' && sizeof($close_tags) && substr($out, -1, 1) == '[') indvdum@0: { indvdum@0: // we have found a closing tag indvdum@0: $out .= array_pop($close_tags) . ']'; indvdum@0: $tok = '['; indvdum@0: $buffer = ''; indvdum@0: indvdum@0: /* Add space at the end of the closing tag if not happened before to allow following urls/smilies to be parsed correctly indvdum@0: * Do not try to think for the user. :/ Do not parse urls/smilies if there is no space - is the same as with other bbcodes too. indvdum@0: * Also, we won't have any spaces within $in anyway, only adding up spaces -> #10982 indvdum@0: if (!$in || $in[0] !== ' ') indvdum@0: { indvdum@0: $out .= ' '; indvdum@0: }*/ indvdum@0: } indvdum@0: else if (preg_match('#^quote(?:="(.*?)")?$#is', $buffer, $m) && substr($out, -1, 1) == '[') indvdum@0: { indvdum@0: $this->parsed_items['quote']++; indvdum@0: indvdum@0: // the buffer holds a valid opening tag indvdum@0: if ($config['max_quote_depth'] && sizeof($close_tags) >= $config['max_quote_depth']) indvdum@0: { indvdum@0: // there are too many nested quotes indvdum@0: $error_ary['quote_depth'] = sprintf($user->lang['QUOTE_DEPTH_EXCEEDED'], $config['max_quote_depth']); indvdum@0: indvdum@0: $out .= $buffer . $tok; indvdum@0: $tok = '[]'; indvdum@0: $buffer = ''; indvdum@0: indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: array_push($close_tags, '/quote:' . $this->bbcode_uid); indvdum@0: indvdum@0: if (isset($m[1]) && $m[1]) indvdum@0: { indvdum@0: $username = str_replace(array('[', ']'), array('[', ']'), $m[1]); indvdum@0: $username = preg_replace('#\[(?!b|i|u|color|url|email|/b|/i|/u|/color|/url|/email)#iU', '[$1', $username); indvdum@0: indvdum@0: $end_tags = array(); indvdum@0: $error = false; indvdum@0: indvdum@0: preg_match_all('#\[((?:/)?(?:[a-z]+))#i', $username, $tags); indvdum@0: foreach ($tags[1] as $tag) indvdum@0: { indvdum@0: if ($tag[0] != '/') indvdum@0: { indvdum@0: $end_tags[] = '/' . $tag; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $end_tag = array_pop($end_tags); indvdum@0: $error = ($end_tag != $tag) ? true : false; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if ($error) indvdum@0: { indvdum@0: $username = $m[1]; indvdum@0: } indvdum@0: indvdum@0: $out .= 'quote="' . $username . '":' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $out .= 'quote:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: $tok = '['; indvdum@0: $buffer = ''; indvdum@0: } indvdum@0: else if (preg_match('#^quote="(.*?)#is', $buffer, $m)) indvdum@0: { indvdum@0: // the buffer holds an invalid opening tag indvdum@0: $buffer .= ']'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $out .= $buffer . $tok; indvdum@0: $tok = '[]'; indvdum@0: $buffer = ''; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: /** indvdum@0: * Old quote code working fine, but having errors listed in bug #3572 indvdum@0: * indvdum@0: * $out .= $buffer . $tok; indvdum@0: * $tok = ($tok == '[') ? ']' : '[]'; indvdum@0: * $buffer = ''; indvdum@0: */ indvdum@0: indvdum@0: $out .= $buffer . $tok; indvdum@0: indvdum@0: if ($tok == '[') indvdum@0: { indvdum@0: // Search the text for the next tok... if an ending quote comes first, then change tok to [] indvdum@0: $pos1 = stripos($in, '[/quote'); indvdum@0: // If the token ] comes first, we change it to ] indvdum@0: $pos2 = strpos($in, ']'); indvdum@0: // If the token [ comes first, we change it to [ indvdum@0: $pos3 = strpos($in, '['); indvdum@0: indvdum@0: if ($pos1 !== false && ($pos2 === false || $pos1 < $pos2) && ($pos3 === false || $pos1 < $pos3)) indvdum@0: { indvdum@0: $tok = '[]'; indvdum@0: } indvdum@0: else if ($pos3 !== false && ($pos2 === false || $pos3 < $pos2)) indvdum@0: { indvdum@0: $tok = '['; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $tok = ']'; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $tok = '[]'; indvdum@0: } indvdum@0: $buffer = ''; indvdum@0: } indvdum@0: } indvdum@0: while ($in); indvdum@0: indvdum@0: $out .= $buffer; indvdum@0: indvdum@0: if (sizeof($close_tags)) indvdum@0: { indvdum@0: $out .= '[' . implode('][', $close_tags) . ']'; indvdum@0: } indvdum@0: indvdum@0: foreach ($error_ary as $error_msg) indvdum@0: { indvdum@0: $this->warn_msg[] = $error_msg; indvdum@0: } indvdum@0: indvdum@0: return $out; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Validate email indvdum@0: */ indvdum@0: function validate_email($var1, $var2) indvdum@0: { indvdum@0: $var1 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var1))); indvdum@0: $var2 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var2))); indvdum@0: indvdum@0: $txt = $var2; indvdum@0: $email = ($var1) ? $var1 : $var2; indvdum@0: indvdum@0: $validated = true; indvdum@0: indvdum@0: if (!preg_match('/^' . get_preg_expression('email') . '$/i', $email)) indvdum@0: { indvdum@0: $validated = false; indvdum@0: } indvdum@0: indvdum@0: if (!$validated) indvdum@0: { indvdum@0: return '[email' . (($var1) ? "=$var1" : '') . ']' . $var2 . '[/email]'; indvdum@0: } indvdum@0: indvdum@0: $this->parsed_items['email']++; indvdum@0: indvdum@0: if ($var1) indvdum@0: { indvdum@0: $retval = '[email=' . $this->bbcode_specialchars($email) . ':' . $this->bbcode_uid . ']' . $txt . '[/email:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $retval = '[email:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($email) . '[/email:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: return $retval; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Validate url indvdum@0: * indvdum@0: * @param string $var1 optional url parameter for url bbcode: [url(=$var1)]$var2[/url] indvdum@0: * @param string $var2 url bbcode content: [url(=$var1)]$var2[/url] indvdum@0: */ indvdum@0: function validate_url($var1, $var2) indvdum@0: { indvdum@0: global $config; indvdum@0: indvdum@0: $var1 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var1))); indvdum@0: $var2 = str_replace("\r\n", "\n", str_replace('\"', '"', trim($var2))); indvdum@0: indvdum@0: $url = ($var1) ? $var1 : $var2; indvdum@0: indvdum@0: if ($var1 && !$var2) indvdum@0: { indvdum@0: $var2 = $var1; indvdum@0: } indvdum@0: indvdum@0: if (!$url) indvdum@0: { indvdum@0: return '[url' . (($var1) ? '=' . $var1 : '') . ']' . $var2 . '[/url]'; indvdum@0: } indvdum@0: indvdum@0: $valid = false; indvdum@0: indvdum@0: $url = str_replace(' ', '%20', $url); indvdum@0: indvdum@0: // Checking urls indvdum@0: if (preg_match('#^' . get_preg_expression('url') . '$#i', $url) || indvdum@0: preg_match('#^' . get_preg_expression('www_url') . '$#i', $url) || indvdum@0: preg_match('#^' . preg_quote(generate_board_url(), '#') . get_preg_expression('relative_url') . '$#i', $url)) indvdum@0: { indvdum@0: $valid = true; indvdum@0: } indvdum@0: indvdum@0: if ($valid) indvdum@0: { indvdum@0: $this->parsed_items['url']++; indvdum@0: indvdum@0: // if there is no scheme, then add http schema indvdum@0: if (!preg_match('#^[a-z][a-z\d+\-.]*:/{2}#i', $url)) indvdum@0: { indvdum@0: $url = 'http://' . $url; indvdum@0: } indvdum@0: indvdum@0: // Is this a link to somewhere inside this board? If so then remove the session id from the url indvdum@0: if (strpos($url, generate_board_url()) !== false && strpos($url, 'sid=') !== false) indvdum@0: { indvdum@0: $url = preg_replace('/(&|\?)sid=[0-9a-f]{32}&/', '\1', $url); indvdum@0: $url = preg_replace('/(&|\?)sid=[0-9a-f]{32}$/', '', $url); indvdum@0: $url = append_sid($url); indvdum@0: } indvdum@0: indvdum@0: return ($var1) ? '[url=' . $this->bbcode_specialchars($url) . ':' . $this->bbcode_uid . ']' . $var2 . '[/url:' . $this->bbcode_uid . ']' : '[url:' . $this->bbcode_uid . ']' . $this->bbcode_specialchars($url) . '[/url:' . $this->bbcode_uid . ']'; indvdum@0: } indvdum@0: indvdum@0: return '[url' . (($var1) ? '=' . $var1 : '') . ']' . $var2 . '[/url]'; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Check if url is pointing to this domain/script_path/php-file indvdum@0: * indvdum@0: * @param string $url the url to check indvdum@0: * @return true if the url is pointing to this domain/script_path/php-file, false if not indvdum@0: * indvdum@0: * @access private indvdum@0: */ indvdum@0: function path_in_domain($url) indvdum@0: { indvdum@0: global $config, $phpEx, $user; indvdum@0: indvdum@0: if ($config['force_server_vars']) indvdum@0: { indvdum@0: $check_path = $config['script_path']; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $check_path = ($user->page['root_script_path'] != '/') ? substr($user->page['root_script_path'], 0, -1) : '/'; indvdum@0: } indvdum@0: indvdum@0: // Is the user trying to link to a php file in this domain and script path? indvdum@0: if (strpos($url, ".{$phpEx}") !== false && strpos($url, $check_path) !== false) indvdum@0: { indvdum@0: $server_name = $user->host; indvdum@0: indvdum@0: // Forcing server vars is the only way to specify/override the protocol indvdum@0: if ($config['force_server_vars'] || !$server_name) indvdum@0: { indvdum@0: $server_name = $config['server_name']; indvdum@0: } indvdum@0: indvdum@0: // Check again in correct order... indvdum@0: $pos_ext = strpos($url, ".{$phpEx}"); indvdum@0: $pos_path = strpos($url, $check_path); indvdum@0: $pos_domain = strpos($url, $server_name); indvdum@0: indvdum@0: if ($pos_domain !== false && $pos_path >= $pos_domain && $pos_ext >= $pos_path) indvdum@0: { indvdum@0: // Ok, actually we allow linking to some files (this may be able to be extended in some way later...) indvdum@0: if (strpos($url, '/' . $check_path . '/download/file.' . $phpEx) !== 0) indvdum@0: { indvdum@0: return false; indvdum@0: } indvdum@0: indvdum@0: return true; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: return false; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Main message parser for posting, pm, etc. takes raw message indvdum@0: * and parses it for attachments, bbcode and smilies indvdum@0: * @package phpBB3 indvdum@0: */ indvdum@0: class parse_message extends bbcode_firstpass indvdum@0: { indvdum@0: var $attachment_data = array(); indvdum@0: var $filename_data = array(); indvdum@0: indvdum@0: // Helps ironing out user error indvdum@0: var $message_status = ''; indvdum@0: indvdum@0: var $allow_img_bbcode = true; indvdum@0: var $allow_flash_bbcode = true; indvdum@0: var $allow_quote_bbcode = true; indvdum@0: var $allow_url_bbcode = true; indvdum@0: indvdum@0: var $mode; indvdum@0: indvdum@0: /** indvdum@0: * Init - give message here or manually indvdum@0: */ indvdum@0: function parse_message($message = '') indvdum@0: { indvdum@0: // Init BBCode UID indvdum@0: $this->bbcode_uid = substr(base_convert(unique_id(), 16, 36), 0, BBCODE_UID_LEN); indvdum@0: $this->message = $message; indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse Message indvdum@0: */ indvdum@0: function parse($allow_bbcode, $allow_magic_url, $allow_smilies, $allow_img_bbcode = true, $allow_flash_bbcode = true, $allow_quote_bbcode = true, $allow_url_bbcode = true, $update_this_message = true, $mode = 'post') indvdum@0: { indvdum@0: global $config, $db, $user; indvdum@0: indvdum@0: $this->mode = $mode; indvdum@0: indvdum@0: foreach (array('chars', 'smilies', 'urls', 'font_size', 'img_height', 'img_width') as $key) indvdum@0: { indvdum@0: if (!isset($config['max_' . $mode . '_' . $key])) indvdum@0: { indvdum@0: $config['max_' . $mode . '_' . $key] = 0; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $this->allow_img_bbcode = $allow_img_bbcode; indvdum@0: $this->allow_flash_bbcode = $allow_flash_bbcode; indvdum@0: $this->allow_quote_bbcode = $allow_quote_bbcode; indvdum@0: $this->allow_url_bbcode = $allow_url_bbcode; indvdum@0: indvdum@0: // If false, then $this->message won't be altered, the text will be returned instead. indvdum@0: if (!$update_this_message) indvdum@0: { indvdum@0: $tmp_message = $this->message; indvdum@0: $return_message = &$this->message; indvdum@0: } indvdum@0: indvdum@0: if ($this->message_status == 'display') indvdum@0: { indvdum@0: $this->decode_message(); indvdum@0: } indvdum@0: indvdum@0: // Do some general 'cleanup' first before processing message, indvdum@0: // e.g. remove excessive newlines(?), smilies(?) indvdum@0: $match = array('#(script|about|applet|activex|chrome):#i'); indvdum@0: $replace = array("\\1:"); indvdum@0: $this->message = preg_replace($match, $replace, trim($this->message)); indvdum@0: indvdum@0: // Store message length... indvdum@0: $message_length = ($mode == 'post') ? utf8_strlen($this->message) : utf8_strlen(preg_replace('#\[\/?[a-z\*\+\-]+(=[\S]+)?\]#ius', ' ', $this->message)); indvdum@0: indvdum@0: // Maximum message length check. 0 disables this check completely. indvdum@0: if ((int) $config['max_' . $mode . '_chars'] > 0 && $message_length > (int) $config['max_' . $mode . '_chars']) indvdum@0: { indvdum@0: $this->warn_msg[] = sprintf($user->lang['TOO_MANY_CHARS_' . strtoupper($mode)], $message_length, (int) $config['max_' . $mode . '_chars']); indvdum@0: return (!$update_this_message) ? $return_message : $this->warn_msg; indvdum@0: } indvdum@0: indvdum@0: // Minimum message length check for post only indvdum@0: if ($mode === 'post') indvdum@0: { indvdum@0: if (!$message_length || $message_length < (int) $config['min_post_chars']) indvdum@0: { indvdum@0: $this->warn_msg[] = (!$message_length) ? $user->lang['TOO_FEW_CHARS'] : sprintf($user->lang['TOO_FEW_CHARS_LIMIT'], $message_length, (int) $config['min_post_chars']); indvdum@0: return (!$update_this_message) ? $return_message : $this->warn_msg; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Prepare BBcode (just prepares some tags for better parsing) indvdum@0: if ($allow_bbcode && strpos($this->message, '[') !== false) indvdum@0: { indvdum@0: $this->bbcode_init(); indvdum@0: $disallow = array('img', 'flash', 'quote', 'url'); indvdum@0: foreach ($disallow as $bool) indvdum@0: { indvdum@0: if (!${'allow_' . $bool . '_bbcode'}) indvdum@0: { indvdum@0: $this->bbcodes[$bool]['disabled'] = true; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $this->prepare_bbcodes(); indvdum@0: } indvdum@0: indvdum@0: // Parse smilies indvdum@0: if ($allow_smilies) indvdum@0: { indvdum@0: $this->smilies($config['max_' . $mode . '_smilies']); indvdum@0: } indvdum@0: indvdum@0: $num_urls = 0; indvdum@0: indvdum@0: // Parse BBCode indvdum@0: if ($allow_bbcode && strpos($this->message, '[') !== false) indvdum@0: { indvdum@0: $this->parse_bbcode(); indvdum@0: $num_urls += $this->parsed_items['url']; indvdum@0: } indvdum@0: indvdum@0: // Parse URL's indvdum@0: if ($allow_magic_url) indvdum@0: { indvdum@0: $this->magic_url(generate_board_url()); indvdum@0: indvdum@0: if ($config['max_' . $mode . '_urls']) indvdum@0: { indvdum@0: $num_urls += preg_match_all('#\' . $row['code'] . ''; indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: if (sizeof($match)) indvdum@0: { indvdum@0: if ($max_smilies) indvdum@0: { indvdum@0: // 'u' modifier has been added to correctly parse smilies within unicode strings indvdum@0: // For details: http://tracker.phpbb.com/browse/PHPBB3-10117 indvdum@0: $num_matches = preg_match_all('#(?<=^|[\n .])(?:' . implode('|', $match) . ')(?![^<>]*>)#u', $this->message, $matches); indvdum@0: unset($matches); indvdum@0: indvdum@0: if ($num_matches !== false && $num_matches > $max_smilies) indvdum@0: { indvdum@0: $this->warn_msg[] = sprintf($user->lang['TOO_MANY_SMILIES'], $max_smilies); indvdum@0: return; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Make sure the delimiter # is added in front and at the end of every element within $match indvdum@0: // 'u' modifier has been added to correctly parse smilies within unicode strings indvdum@0: // For details: http://tracker.phpbb.com/browse/PHPBB3-10117 indvdum@0: indvdum@0: $this->message = trim(preg_replace(explode(chr(0), '#(?<=^|[\n .])' . implode('(?![^<>]*>)#u' . chr(0) . '#(?<=^|[\n .])', $match) . '(?![^<>]*>)#u'), $replace, $this->message)); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse Attachments indvdum@0: */ indvdum@0: function parse_attachments($form_name, $mode, $forum_id, $submit, $preview, $refresh, $is_message = false) indvdum@0: { indvdum@0: global $config, $auth, $user, $phpbb_root_path, $phpEx, $db; indvdum@0: indvdum@0: $error = array(); indvdum@0: indvdum@0: $num_attachments = sizeof($this->attachment_data); indvdum@0: $this->filename_data['filecomment'] = utf8_normalize_nfc(request_var('filecomment', '', true)); indvdum@0: $upload_file = (isset($_FILES[$form_name]) && $_FILES[$form_name]['name'] != 'none' && trim($_FILES[$form_name]['name'])) ? true : false; indvdum@0: indvdum@0: $add_file = (isset($_POST['add_file'])) ? true : false; indvdum@0: $delete_file = (isset($_POST['delete_file'])) ? true : false; indvdum@0: indvdum@0: // First of all adjust comments if changed indvdum@0: $actual_comment_list = utf8_normalize_nfc(request_var('comment_list', array(''), true)); indvdum@0: indvdum@0: foreach ($actual_comment_list as $comment_key => $comment) indvdum@0: { indvdum@0: if (!isset($this->attachment_data[$comment_key])) indvdum@0: { indvdum@0: continue; indvdum@0: } indvdum@0: indvdum@0: if ($this->attachment_data[$comment_key]['attach_comment'] != $actual_comment_list[$comment_key]) indvdum@0: { indvdum@0: $this->attachment_data[$comment_key]['attach_comment'] = $actual_comment_list[$comment_key]; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $cfg = array(); indvdum@0: $cfg['max_attachments'] = ($is_message) ? $config['max_attachments_pm'] : $config['max_attachments']; indvdum@0: $forum_id = ($is_message) ? 0 : $forum_id; indvdum@0: indvdum@0: if ($submit && in_array($mode, array('post', 'reply', 'quote', 'edit')) && $upload_file) indvdum@0: { indvdum@0: if ($num_attachments < $cfg['max_attachments'] || $auth->acl_get('a_') || $auth->acl_get('m_', $forum_id)) indvdum@0: { indvdum@0: $filedata = upload_attachment($form_name, $forum_id, false, '', $is_message); indvdum@0: $error = $filedata['error']; indvdum@0: indvdum@0: if ($filedata['post_attach'] && !sizeof($error)) indvdum@0: { indvdum@0: $sql_ary = array( indvdum@0: 'physical_filename' => $filedata['physical_filename'], indvdum@0: 'attach_comment' => $this->filename_data['filecomment'], indvdum@0: 'real_filename' => $filedata['real_filename'], indvdum@0: 'extension' => $filedata['extension'], indvdum@0: 'mimetype' => $filedata['mimetype'], indvdum@0: 'filesize' => $filedata['filesize'], indvdum@0: 'filetime' => $filedata['filetime'], indvdum@0: 'thumbnail' => $filedata['thumbnail'], indvdum@0: 'is_orphan' => 1, indvdum@0: 'in_message' => ($is_message) ? 1 : 0, indvdum@0: 'poster_id' => $user->data['user_id'], indvdum@0: ); indvdum@0: indvdum@0: $db->sql_query('INSERT INTO ' . ATTACHMENTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary)); indvdum@0: indvdum@0: $new_entry = array( indvdum@0: 'attach_id' => $db->sql_nextid(), indvdum@0: 'is_orphan' => 1, indvdum@0: 'real_filename' => $filedata['real_filename'], indvdum@0: 'attach_comment'=> $this->filename_data['filecomment'], indvdum@0: ); indvdum@0: indvdum@0: $this->attachment_data = array_merge(array(0 => $new_entry), $this->attachment_data); indvdum@0: $this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "'[attachment='.(\\1 + 1).']\\2[/attachment]'", $this->message); indvdum@0: indvdum@0: $this->filename_data['filecomment'] = ''; indvdum@0: indvdum@0: // This Variable is set to false here, because Attachments are entered into the indvdum@0: // Database in two modes, one if the id_list is 0 and the second one if post_attach is true indvdum@0: // Since post_attach is automatically switched to true if an Attachment got added to the filesystem, indvdum@0: // but we are assigning an id of 0 here, we have to reset the post_attach variable to false. indvdum@0: // indvdum@0: // This is very relevant, because it could happen that the post got not submitted, but we do not indvdum@0: // know this circumstance here. We could be at the posting page or we could be redirected to the entered indvdum@0: // post. :) indvdum@0: $filedata['post_attach'] = false; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $error[] = sprintf($user->lang['TOO_MANY_ATTACHMENTS'], $cfg['max_attachments']); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: if ($preview || $refresh || sizeof($error)) indvdum@0: { indvdum@0: // Perform actions on temporary attachments indvdum@0: if ($delete_file) indvdum@0: { indvdum@0: include_once($phpbb_root_path . 'includes/functions_admin.' . $phpEx); indvdum@0: indvdum@0: $index = array_keys(request_var('delete_file', array(0 => 0))); indvdum@0: $index = (!empty($index)) ? $index[0] : false; indvdum@0: indvdum@0: if ($index !== false && !empty($this->attachment_data[$index])) indvdum@0: { indvdum@0: // delete selected attachment indvdum@0: if ($this->attachment_data[$index]['is_orphan']) indvdum@0: { indvdum@0: $sql = 'SELECT attach_id, physical_filename, thumbnail indvdum@0: FROM ' . ATTACHMENTS_TABLE . ' indvdum@0: WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id'] . ' indvdum@0: AND is_orphan = 1 indvdum@0: AND poster_id = ' . $user->data['user_id']; indvdum@0: $result = $db->sql_query($sql); indvdum@0: $row = $db->sql_fetchrow($result); indvdum@0: $db->sql_freeresult($result); indvdum@0: indvdum@0: if ($row) indvdum@0: { indvdum@0: phpbb_unlink($row['physical_filename'], 'file'); indvdum@0: indvdum@0: if ($row['thumbnail']) indvdum@0: { indvdum@0: phpbb_unlink($row['physical_filename'], 'thumbnail'); indvdum@0: } indvdum@0: indvdum@0: $db->sql_query('DELETE FROM ' . ATTACHMENTS_TABLE . ' WHERE attach_id = ' . (int) $this->attachment_data[$index]['attach_id']); indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: delete_attachments('attach', array(intval($this->attachment_data[$index]['attach_id']))); indvdum@0: } indvdum@0: indvdum@0: unset($this->attachment_data[$index]); indvdum@0: $this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "(\\1 == \$index) ? '' : ((\\1 > \$index) ? '[attachment=' . (\\1 - 1) . ']\\2[/attachment]' : '\\0')", $this->message); indvdum@0: indvdum@0: // Reindex Array indvdum@0: $this->attachment_data = array_values($this->attachment_data); indvdum@0: } indvdum@0: } indvdum@0: else if (($add_file || $preview) && $upload_file) indvdum@0: { indvdum@0: if ($num_attachments < $cfg['max_attachments'] || $auth->acl_gets('m_', 'a_', $forum_id)) indvdum@0: { indvdum@0: $filedata = upload_attachment($form_name, $forum_id, false, '', $is_message); indvdum@0: $error = array_merge($error, $filedata['error']); indvdum@0: indvdum@0: if (!sizeof($error)) indvdum@0: { indvdum@0: $sql_ary = array( indvdum@0: 'physical_filename' => $filedata['physical_filename'], indvdum@0: 'attach_comment' => $this->filename_data['filecomment'], indvdum@0: 'real_filename' => $filedata['real_filename'], indvdum@0: 'extension' => $filedata['extension'], indvdum@0: 'mimetype' => $filedata['mimetype'], indvdum@0: 'filesize' => $filedata['filesize'], indvdum@0: 'filetime' => $filedata['filetime'], indvdum@0: 'thumbnail' => $filedata['thumbnail'], indvdum@0: 'is_orphan' => 1, indvdum@0: 'in_message' => ($is_message) ? 1 : 0, indvdum@0: 'poster_id' => $user->data['user_id'], indvdum@0: ); indvdum@0: indvdum@0: $db->sql_query('INSERT INTO ' . ATTACHMENTS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary)); indvdum@0: indvdum@0: $new_entry = array( indvdum@0: 'attach_id' => $db->sql_nextid(), indvdum@0: 'is_orphan' => 1, indvdum@0: 'real_filename' => $filedata['real_filename'], indvdum@0: 'attach_comment'=> $this->filename_data['filecomment'], indvdum@0: ); indvdum@0: indvdum@0: $this->attachment_data = array_merge(array(0 => $new_entry), $this->attachment_data); indvdum@0: $this->message = preg_replace('#\[attachment=([0-9]+)\](.*?)\[\/attachment\]#e', "'[attachment='.(\\1 + 1).']\\2[/attachment]'", $this->message); indvdum@0: $this->filename_data['filecomment'] = ''; indvdum@0: } indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $error[] = sprintf($user->lang['TOO_MANY_ATTACHMENTS'], $cfg['max_attachments']); indvdum@0: } indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: foreach ($error as $error_msg) indvdum@0: { indvdum@0: $this->warn_msg[] = $error_msg; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Get Attachment Data indvdum@0: */ indvdum@0: function get_submitted_attachment_data($check_user_id = false) indvdum@0: { indvdum@0: global $user, $db, $phpbb_root_path, $phpEx, $config; indvdum@0: indvdum@0: $this->filename_data['filecomment'] = utf8_normalize_nfc(request_var('filecomment', '', true)); indvdum@0: $attachment_data = (isset($_POST['attachment_data'])) ? $_POST['attachment_data'] : array(); indvdum@0: $this->attachment_data = array(); indvdum@0: indvdum@0: $check_user_id = ($check_user_id === false) ? $user->data['user_id'] : $check_user_id; indvdum@0: indvdum@0: if (!sizeof($attachment_data)) indvdum@0: { indvdum@0: return; indvdum@0: } indvdum@0: indvdum@0: $not_orphan = $orphan = array(); indvdum@0: indvdum@0: foreach ($attachment_data as $pos => $var_ary) indvdum@0: { indvdum@0: if ($var_ary['is_orphan']) indvdum@0: { indvdum@0: $orphan[(int) $var_ary['attach_id']] = $pos; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: $not_orphan[(int) $var_ary['attach_id']] = $pos; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: // Regenerate already posted attachments indvdum@0: if (sizeof($not_orphan)) indvdum@0: { indvdum@0: // Get the attachment data, based on the poster id... indvdum@0: $sql = 'SELECT attach_id, is_orphan, real_filename, attach_comment indvdum@0: FROM ' . ATTACHMENTS_TABLE . ' indvdum@0: WHERE ' . $db->sql_in_set('attach_id', array_keys($not_orphan)) . ' indvdum@0: AND poster_id = ' . $check_user_id; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $pos = $not_orphan[$row['attach_id']]; indvdum@0: $this->attachment_data[$pos] = $row; indvdum@0: set_var($this->attachment_data[$pos]['attach_comment'], $_POST['attachment_data'][$pos]['attach_comment'], 'string', true); indvdum@0: indvdum@0: unset($not_orphan[$row['attach_id']]); indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: if (sizeof($not_orphan)) indvdum@0: { indvdum@0: trigger_error('NO_ACCESS_ATTACHMENT', E_USER_ERROR); indvdum@0: } indvdum@0: indvdum@0: // Regenerate newly uploaded attachments indvdum@0: if (sizeof($orphan)) indvdum@0: { indvdum@0: $sql = 'SELECT attach_id, is_orphan, real_filename, attach_comment indvdum@0: FROM ' . ATTACHMENTS_TABLE . ' indvdum@0: WHERE ' . $db->sql_in_set('attach_id', array_keys($orphan)) . ' indvdum@0: AND poster_id = ' . $user->data['user_id'] . ' indvdum@0: AND is_orphan = 1'; indvdum@0: $result = $db->sql_query($sql); indvdum@0: indvdum@0: while ($row = $db->sql_fetchrow($result)) indvdum@0: { indvdum@0: $pos = $orphan[$row['attach_id']]; indvdum@0: $this->attachment_data[$pos] = $row; indvdum@0: set_var($this->attachment_data[$pos]['attach_comment'], $_POST['attachment_data'][$pos]['attach_comment'], 'string', true); indvdum@0: indvdum@0: unset($orphan[$row['attach_id']]); indvdum@0: } indvdum@0: $db->sql_freeresult($result); indvdum@0: } indvdum@0: indvdum@0: if (sizeof($orphan)) indvdum@0: { indvdum@0: trigger_error('NO_ACCESS_ATTACHMENT', E_USER_ERROR); indvdum@0: } indvdum@0: indvdum@0: ksort($this->attachment_data); indvdum@0: } indvdum@0: indvdum@0: /** indvdum@0: * Parse Poll indvdum@0: */ indvdum@0: function parse_poll(&$poll) indvdum@0: { indvdum@0: global $auth, $user, $config; indvdum@0: indvdum@0: $poll_max_options = $poll['poll_max_options']; indvdum@0: indvdum@0: // Parse Poll Option text ;) indvdum@0: $tmp_message = $this->message; indvdum@0: $this->message = $poll['poll_option_text']; indvdum@0: $bbcode_bitfield = $this->bbcode_bitfield; indvdum@0: indvdum@0: $poll['poll_option_text'] = $this->parse($poll['enable_bbcode'], ($config['allow_post_links']) ? $poll['enable_urls'] : false, $poll['enable_smilies'], $poll['img_status'], false, false, $config['allow_post_links'], false, 'poll'); indvdum@0: indvdum@0: $bbcode_bitfield = base64_encode(base64_decode($bbcode_bitfield) | base64_decode($this->bbcode_bitfield)); indvdum@0: $this->message = $tmp_message; indvdum@0: indvdum@0: // Parse Poll Title indvdum@0: $tmp_message = $this->message; indvdum@0: $this->message = $poll['poll_title']; indvdum@0: $this->bbcode_bitfield = $bbcode_bitfield; indvdum@0: indvdum@0: $poll['poll_options'] = explode("\n", trim($poll['poll_option_text'])); indvdum@0: $poll['poll_options_size'] = sizeof($poll['poll_options']); indvdum@0: indvdum@0: if (!$poll['poll_title'] && $poll['poll_options_size']) indvdum@0: { indvdum@0: $this->warn_msg[] = $user->lang['NO_POLL_TITLE']; indvdum@0: } indvdum@0: else indvdum@0: { indvdum@0: if (utf8_strlen(preg_replace('#\[\/?[a-z\*\+\-]+(=[\S]+)?\]#ius', ' ', $this->message)) > 100) indvdum@0: { indvdum@0: $this->warn_msg[] = $user->lang['POLL_TITLE_TOO_LONG']; indvdum@0: } indvdum@0: $poll['poll_title'] = $this->parse($poll['enable_bbcode'], ($config['allow_post_links']) ? $poll['enable_urls'] : false, $poll['enable_smilies'], $poll['img_status'], false, false, $config['allow_post_links'], false, 'poll'); indvdum@0: if (strlen($poll['poll_title']) > 255) indvdum@0: { indvdum@0: $this->warn_msg[] = $user->lang['POLL_TITLE_COMP_TOO_LONG']; indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: $this->bbcode_bitfield = base64_encode(base64_decode($bbcode_bitfield) | base64_decode($this->bbcode_bitfield)); indvdum@0: $this->message = $tmp_message; indvdum@0: unset($tmp_message); indvdum@0: indvdum@0: if (sizeof($poll['poll_options']) == 1) indvdum@0: { indvdum@0: $this->warn_msg[] = $user->lang['TOO_FEW_POLL_OPTIONS']; indvdum@0: } indvdum@0: else if ($poll['poll_options_size'] > (int) $config['max_poll_options']) indvdum@0: { indvdum@0: $this->warn_msg[] = $user->lang['TOO_MANY_POLL_OPTIONS']; indvdum@0: } indvdum@0: else if ($poll_max_options > $poll['poll_options_size']) indvdum@0: { indvdum@0: $this->warn_msg[] = $user->lang['TOO_MANY_USER_OPTIONS']; indvdum@0: } indvdum@0: indvdum@0: $poll['poll_max_options'] = ($poll['poll_max_options'] < 1) ? 1 : (($poll['poll_max_options'] > $config['max_poll_options']) ? $config['max_poll_options'] : $poll['poll_max_options']); indvdum@0: } indvdum@0: } indvdum@0: indvdum@0: ?>