Chris@76: ';', '#smlt#' => '<', '#smgt#' => '>', '#smamp#' => '&')); Chris@76: $context['message'] = bbc_to_html($_REQUEST['message']); Chris@76: } Chris@76: else Chris@76: { Chris@76: $_REQUEST['message'] = un_htmlspecialchars($_REQUEST['message']); Chris@76: $_REQUEST['message'] = strtr($_REQUEST['message'], array('#smcol#' => ';', '#smlt#' => '<', '#smgt#' => '>', '#smamp#' => '&')); Chris@76: Chris@76: $context['message'] = html_to_bbc($_REQUEST['message']); Chris@76: } Chris@76: Chris@76: $context['message'] = $smcFunc['htmlspecialchars']($context['message']); Chris@76: } Chris@76: Chris@76: // Convert only the BBC that can be edited in HTML mode for the editor. Chris@76: function bbc_to_html($text) Chris@76: { Chris@76: global $modSettings, $smcFunc; Chris@76: Chris@76: // Turn line breaks back into br's. Chris@76: $text = strtr($text, array("\r" => '', "\n" => '
')); Chris@76: Chris@76: // Prevent conversion of all bbcode inside these bbcodes. Chris@76: // !!! Tie in with bbc permissions ? Chris@76: foreach (array('code', 'php', 'nobbc') as $code) Chris@76: { Chris@76: if (strpos($text, '['. $code) !== false) Chris@76: { Chris@76: $parts = preg_split('~(\[/' . $code . '\]|\[' . $code . '(?:=[^\]]+)?\])~i', $text, -1, PREG_SPLIT_DELIM_CAPTURE); Chris@76: Chris@76: // Only mess with stuff inside tags. Chris@76: for ($i = 0, $n = count($parts); $i < $n; $i++) Chris@76: { Chris@76: // Value of 2 means we're inside the tag. Chris@76: if ($i % 4 == 2) Chris@76: $parts[$i] = strtr($parts[$i], array('[' => '[', ']' => ']', "'" => "'")); Chris@76: } Chris@76: // Put our humpty dumpty message back together again. Chris@76: $text = implode('', $parts); Chris@76: } Chris@76: } Chris@76: Chris@76: // What tags do we allow? Chris@76: $allowed_tags = array('b', 'u', 'i', 's', 'hr', 'list', 'li', 'font', 'size', 'color', 'img', 'left', 'center', 'right', 'url', 'email', 'ftp', 'sub', 'sup'); Chris@76: Chris@76: $text = parse_bbc($text, true, '', $allowed_tags); Chris@76: Chris@76: // Fix for having a line break then a thingy. Chris@76: $text = strtr($text, array('
' '', "\r" => '')); Chris@76: Chris@76: // Note that IE doesn't understand spans really - make them something "legacy" Chris@76: $working_html = array( Chris@76: '~(.+?)~i' => '$1', Chris@76: '~(.+?)~i' => '$1', Chris@76: '~(.+?)~i' => '$2', Chris@76: '~(.+?)~i' => '$2', Chris@76: '~(.+?)~i' => '

$2

', Chris@76: ); Chris@76: $text = preg_replace(array_keys($working_html), array_values($working_html), $text); Chris@76: Chris@76: // Parse unique ID's and disable javascript into the smileys - using the double space. Chris@76: $i = 1; Chris@76: $text = preg_replace('~(?:\s| )?<(img\ssrc="' . preg_quote($modSettings['smileys_url'], '~') . '/[^<>]+?/([^<>]+?)"\s*)[^<>]*?class="smiley" />~e', '\'<\' . ' . 'stripslashes(\'$1\') . \'alt="" title="" onresizestart="return false;" id="smiley_\' . ' . "\$" . 'i++ . \'_$2" style="padding: 0 3px 0 3px;" />\'', $text); Chris@76: Chris@76: return $text; Chris@76: } Chris@76: Chris@76: // The harder one - wysiwyg to BBC! Chris@76: function html_to_bbc($text) Chris@76: { Chris@76: global $modSettings, $smcFunc, $sourcedir, $scripturl, $context; Chris@76: Chris@76: // Replace newlines with spaces, as that's how browsers usually interpret them. Chris@76: $text = preg_replace("~\s*[\r\n]+\s*~", ' ', $text); Chris@76: Chris@76: // Though some of us love paragraphs, the parser will do better with breaks. Chris@76: $text = preg_replace('~

\s*?
\s*(?!<)~i', '


', $text); Chris@76: Chris@76: // Safari/webkit wraps lines in Wysiwyg in
's. Chris@76: if ($context['browser']['is_webkit']) Chris@76: $text = preg_replace(array('~]*?))?' . '>~i', '
'), array('
', ''), $text); Chris@76: Chris@76: // If there's a trailing break get rid of it - Firefox tends to add one. Chris@76: $text = preg_replace('~$~i', '', $text); Chris@76: Chris@76: // Remove any formatting within code tags. Chris@76: if (strpos($text, '[code') !== false) Chris@76: { Chris@76: $text = preg_replace('~~i', '#smf_br_spec_grudge_cool!#', $text); Chris@76: $parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $text, -1, PREG_SPLIT_DELIM_CAPTURE); Chris@76: Chris@76: // Only mess with stuff outside [code] tags. Chris@76: for ($i = 0, $n = count($parts); $i < $n; $i++) Chris@76: { Chris@76: // Value of 2 means we're inside the tag. Chris@76: if ($i % 4 == 2) Chris@76: $parts[$i] = strip_tags($parts[$i]); Chris@76: } Chris@76: Chris@76: $text = strtr(implode('', $parts), array('#smf_br_spec_grudge_cool!#' => '
')); Chris@76: } Chris@76: Chris@76: // Remove scripts, style and comment blocks. Chris@76: $text = preg_replace('~]*[^/]?' . '>.*?~i', '', $text); Chris@76: $text = preg_replace('~]*[^/]?' . '>.*?~i', '', $text); Chris@76: $text = preg_replace('~\\<\\!--.*?-->~i', '', $text); Chris@76: $text = preg_replace('~\\<\\!\\[CDATA\\[.*?\\]\\]\\>~i', '', $text); Chris@76: Chris@76: // Do the smileys ultra first! Chris@76: preg_match_all('~]*?id="*smiley_\d+_([^<>]+?)[\s"/>]\s*[^<>]*?/*>(?:\s)?~i', $text, $matches); Chris@76: if (!empty($matches[0])) Chris@76: { Chris@76: // Easy if it's not custom. Chris@76: if (empty($modSettings['smiley_enable'])) Chris@76: { Chris@76: $smileysfrom = array('>:D', ':D', '::)', '>:(', ':)', ';)', ';D', ':(', ':o', '8)', ':P', '???', ':-[', ':-X', ':-*', ':\'(', ':-\\', '^-^', 'O0', 'C:-)', '0:)'); Chris@76: $smileysto = array('evil.gif', 'cheesy.gif', 'rolleyes.gif', 'angry.gif', 'smiley.gif', 'wink.gif', 'grin.gif', 'sad.gif', 'shocked.gif', 'cool.gif', 'tongue.gif', 'huh.gif', 'embarrassed.gif', 'lipsrsealed.gif', 'kiss.gif', 'cry.gif', 'undecided.gif', 'azn.gif', 'afro.gif', 'police.gif', 'angel.gif'); Chris@76: Chris@76: foreach ($matches[1] as $k => $file) Chris@76: { Chris@76: $found = array_search($file, $smileysto); Chris@76: // Note the weirdness here is to stop double spaces between smileys. Chris@76: if ($found) Chris@76: $matches[1][$k] = '-[]-smf_smily_start#|#' . htmlspecialchars($smileysfrom[$found]) . '-[]-smf_smily_end#|#'; Chris@76: else Chris@76: $matches[1][$k] = ''; Chris@76: } Chris@76: } Chris@76: else Chris@76: { Chris@76: // Load all the smileys. Chris@76: $names = array(); Chris@76: foreach ($matches[1] as $file) Chris@76: $names[] = $file; Chris@76: $names = array_unique($names); Chris@76: Chris@76: if (!empty($names)) Chris@76: { Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT code, filename Chris@76: FROM {db_prefix}smileys Chris@76: WHERE filename IN ({array_string:smiley_filenames})', Chris@76: array( Chris@76: 'smiley_filenames' => $names, Chris@76: ) Chris@76: ); Chris@76: $mappings = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: $mappings[$row['filename']] = htmlspecialchars($row['code']); Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: foreach ($matches[1] as $k => $file) Chris@76: if (isset($mappings[$file])) Chris@76: $matches[1][$k] = '-[]-smf_smily_start#|#' . $mappings[$file] . '-[]-smf_smily_end#|#'; Chris@76: } Chris@76: } Chris@76: Chris@76: // Replace the tags! Chris@76: $text = str_replace($matches[0], $matches[1], $text); Chris@76: Chris@76: // Now sort out spaces Chris@76: $text = str_replace(array('-[]-smf_smily_end#|#-[]-smf_smily_start#|#', '-[]-smf_smily_end#|#', '-[]-smf_smily_start#|#'), ' ', $text); Chris@76: } Chris@76: Chris@76: // Only try to buy more time if the client didn't quit. Chris@76: if (connection_aborted() && $context['server']['is_apache']) Chris@76: @apache_reset_timeout(); Chris@76: Chris@76: $parts = preg_split('~(<[A-Za-z]+\s*[^<>]*?style="?[^<>"]+"?[^<>]*?(?:/?)>|)~', $text, -1, PREG_SPLIT_DELIM_CAPTURE); Chris@76: $replacement = ''; Chris@76: $stack = array(); Chris@76: Chris@76: foreach ($parts as $part) Chris@76: { Chris@76: if (preg_match('~(<([A-Za-z]+)\s*[^<>]*?)style="?([^<>"]+)"?([^<>]*?(/?)>)~', $part, $matches) === 1) Chris@76: { Chris@76: // If it's being closed instantly, we can't deal with it...yet. Chris@76: if ($matches[5] === '/') Chris@76: continue; Chris@76: else Chris@76: { Chris@76: // Get an array of styles that apply to this element. (The strtr is there to combat HTML generated by Word.) Chris@76: $styles = explode(';', strtr($matches[3], array('"' => ''))); Chris@76: $curElement = $matches[2]; Chris@76: $precedingStyle = $matches[1]; Chris@76: $afterStyle = $matches[4]; Chris@76: $curCloseTags = ''; Chris@76: $extra_attr = ''; Chris@76: Chris@76: foreach ($styles as $type_value_pair) Chris@76: { Chris@76: // Remove spaces and convert uppercase letters. Chris@76: $clean_type_value_pair = strtolower(strtr(trim($type_value_pair), '=', ':')); Chris@76: Chris@76: // Something like 'font-weight: bold' is expected here. Chris@76: if (strpos($clean_type_value_pair, ':') === false) Chris@76: continue; Chris@76: Chris@76: // Capture the elements of a single style item (e.g. 'font-weight' and 'bold'). Chris@76: list ($style_type, $style_value) = explode(':', $type_value_pair); Chris@76: Chris@76: $style_value = trim($style_value); Chris@76: Chris@76: switch (trim($style_type)) Chris@76: { Chris@76: case 'font-weight': Chris@76: if ($style_value === 'bold') Chris@76: { Chris@76: $curCloseTags .= '[/b]'; Chris@76: $replacement .= '[b]'; Chris@76: } Chris@76: break; Chris@76: Chris@76: case 'text-decoration': Chris@76: if ($style_value == 'underline') Chris@76: { Chris@76: $curCloseTags .= '[/u]'; Chris@76: $replacement .= '[u]'; Chris@76: } Chris@76: elseif ($style_value == 'line-through') Chris@76: { Chris@76: $curCloseTags .= '[/s]'; Chris@76: $replacement .= '[s]'; Chris@76: } Chris@76: break; Chris@76: Chris@76: case 'text-align': Chris@76: if ($style_value == 'left') Chris@76: { Chris@76: $curCloseTags .= '[/left]'; Chris@76: $replacement .= '[left]'; Chris@76: } Chris@76: elseif ($style_value == 'center') Chris@76: { Chris@76: $curCloseTags .= '[/center]'; Chris@76: $replacement .= '[center]'; Chris@76: } Chris@76: elseif ($style_value == 'right') Chris@76: { Chris@76: $curCloseTags .= '[/right]'; Chris@76: $replacement .= '[right]'; Chris@76: } Chris@76: break; Chris@76: Chris@76: case 'font-style': Chris@76: if ($style_value == 'italic') Chris@76: { Chris@76: $curCloseTags .= '[/i]'; Chris@76: $replacement .= '[i]'; Chris@76: } Chris@76: break; Chris@76: Chris@76: case 'color': Chris@76: $curCloseTags .= '[/color]'; Chris@76: $replacement .= '[color=' . $style_value . ']'; Chris@76: break; Chris@76: Chris@76: case 'font-size': Chris@76: // Sometimes people put decimals where decimals should not be. Chris@76: if (preg_match('~(\d)+\.\d+(p[xt])~i', $style_value, $dec_matches) === 1) Chris@76: $style_value = $dec_matches[1] . $dec_matches[2]; Chris@76: Chris@76: $curCloseTags .= '[/size]'; Chris@76: $replacement .= '[size=' . $style_value . ']'; Chris@76: break; Chris@76: Chris@76: case 'font-family': Chris@76: // Only get the first freaking font if there's a list! Chris@76: if (strpos($style_value, ',') !== false) Chris@76: $style_value = substr($style_value, 0, strpos($style_value, ',')); Chris@76: Chris@76: $curCloseTags .= '[/font]'; Chris@76: $replacement .= '[font=' . strtr($style_value, array("'" => '')) . ']'; Chris@76: break; Chris@76: Chris@76: // This is a hack for images with dimensions embedded. Chris@76: case 'width': Chris@76: case 'height': Chris@76: if (preg_match('~[1-9]\d*~i', $style_value, $dimension) === 1) Chris@76: $extra_attr .= ' ' . $style_type . '="' . $dimension[0] . '"'; Chris@76: break; Chris@76: Chris@76: case 'list-style-type': Chris@76: if (preg_match('~none|disc|circle|square|decimal|decimal-leading-zero|lower-roman|upper-roman|lower-alpha|upper-alpha|lower-greek|lower-latin|upper-latin|hebrew|armenian|georgian|cjk-ideographic|hiragana|katakana|hiragana-iroha|katakana-iroha~i', $style_value, $listType) === 1) Chris@76: $extra_attr .= ' listtype="' . $listType[0] . '"'; Chris@76: break; Chris@76: } Chris@76: } Chris@76: Chris@76: // Preserve some tags stripping the styling. Chris@76: if (in_array($matches[2], array('a', 'font'))) Chris@76: { Chris@76: $replacement .= $precedingStyle . $afterStyle; Chris@76: $curCloseTags = '' . $curCloseTags; Chris@76: } Chris@76: Chris@76: // If there's something that still needs closing, push it to the stack. Chris@76: if (!empty($curCloseTags)) Chris@76: array_push($stack, array( Chris@76: 'element' => strtolower($curElement), Chris@76: 'closeTags' => $curCloseTags Chris@76: ) Chris@76: ); Chris@76: elseif (!empty($extra_attr)) Chris@76: $replacement .= $precedingStyle . $extra_attr . $afterStyle; Chris@76: } Chris@76: } Chris@76: Chris@76: elseif (preg_match('~~', $part, $matches) === 1) Chris@76: { Chris@76: // Is this the element that we've been waiting for to be closed? Chris@76: if (!empty($stack) && strtolower($matches[1]) === $stack[count($stack) - 1]['element']) Chris@76: { Chris@76: $byebyeTag = array_pop($stack); Chris@76: $replacement .= $byebyeTag['closeTags']; Chris@76: } Chris@76: Chris@76: // Must've been something else. Chris@76: else Chris@76: $replacement .= $part; Chris@76: } Chris@76: // In all other cases, just add the part to the replacement. Chris@76: else Chris@76: $replacement .= $part; Chris@76: } Chris@76: Chris@76: // Now put back the replacement in the text. Chris@76: $text = $replacement; Chris@76: Chris@76: // We are not finished yet, request more time. Chris@76: if (connection_aborted() && $context['server']['is_apache']) Chris@76: @apache_reset_timeout(); Chris@76: Chris@76: // Let's pull out any legacy alignments. Chris@76: while (preg_match('~<([A-Za-z]+)\s+[^<>]*?(align="*(left|center|right)"*)[^<>]*?(/?)>~i', $text, $matches) === 1) Chris@76: { Chris@76: // Find the position in the text of this tag over again. Chris@76: $start_pos = strpos($text, $matches[0]); Chris@76: if ($start_pos === false) Chris@76: break; Chris@76: Chris@76: // End tag? Chris@76: if ($matches[4] != '/' && strpos($text, '', $start_pos) !== false) Chris@76: { Chris@76: $end_length = strlen(''); Chris@76: $end_pos = strpos($text, '', $start_pos); Chris@76: Chris@76: // Remove the align from that tag so it's never checked again. Chris@76: $tag = substr($text, $start_pos, strlen($matches[0])); Chris@76: $content = substr($text, $start_pos + strlen($matches[0]), $end_pos - $start_pos - strlen($matches[0])); Chris@76: $tag = str_replace($matches[2], '', $tag); Chris@76: Chris@76: // Put the tags back into the body. Chris@76: $text = substr($text, 0, $start_pos) . $tag . '[' . $matches[3] . ']' . $content . '[/' . $matches[3] . ']' . substr($text, $end_pos); Chris@76: } Chris@76: else Chris@76: { Chris@76: // Just get rid of this evil tag. Chris@76: $text = substr($text, 0, $start_pos) . substr($text, $start_pos + strlen($matches[0])); Chris@76: } Chris@76: } Chris@76: Chris@76: // Let's do some special stuff for fonts - cause we all love fonts. Chris@76: while (preg_match('~]*)>~i', $text, $matches) === 1) Chris@76: { Chris@76: // Find the position of this again. Chris@76: $start_pos = strpos($text, $matches[0]); Chris@76: $end_pos = false; Chris@76: if ($start_pos === false) Chris@76: break; Chris@76: Chris@76: // This must have an end tag - and we must find the right one. Chris@76: $lower_text = strtolower($text); Chris@76: Chris@76: $start_pos_test = $start_pos + 4; Chris@76: // How many starting tags must we find closing ones for first? Chris@76: $start_font_tag_stack = 0; Chris@76: while ($start_pos_test < strlen($text)) Chris@76: { Chris@76: // Where is the next starting font? Chris@76: $next_start_pos = strpos($lower_text, '', $start_pos_test); Chris@76: Chris@76: // Did we past another starting tag before an end one? Chris@76: if ($next_start_pos !== false && $next_start_pos < $next_end_pos) Chris@76: { Chris@76: $start_font_tag_stack++; Chris@76: $start_pos_test = $next_start_pos + 4; Chris@76: } Chris@76: // Otherwise we have an end tag but not the right one? Chris@76: elseif ($start_font_tag_stack) Chris@76: { Chris@76: $start_font_tag_stack--; Chris@76: $start_pos_test = $next_end_pos + 4; Chris@76: } Chris@76: // Otherwise we're there! Chris@76: else Chris@76: { Chris@76: $end_pos = $next_end_pos; Chris@76: break; Chris@76: } Chris@76: } Chris@76: if ($end_pos === false) Chris@76: break; Chris@76: Chris@76: // Now work out what the attributes are. Chris@76: $attribs = fetchTagAttributes($matches[1]); Chris@76: $tags = array(); Chris@76: foreach ($attribs as $s => $v) Chris@76: { Chris@76: if ($s == 'size') Chris@76: $tags[] = array('[size=' . (int) trim($v) . ']', '[/size]'); Chris@76: elseif ($s == 'face') Chris@76: $tags[] = array('[font=' . trim(strtolower($v)) . ']', '[/font]'); Chris@76: elseif ($s == 'color') Chris@76: $tags[] = array('[color=' . trim(strtolower($v)) . ']', '[/color]'); Chris@76: } Chris@76: Chris@76: // As before add in our tags. Chris@76: $before = $after = ''; Chris@76: foreach ($tags as $tag) Chris@76: { Chris@76: $before .= $tag[0]; Chris@76: if (isset($tag[1])) Chris@76: $after = $tag[1] . $after; Chris@76: } Chris@76: Chris@76: // Remove the tag so it's never checked again. Chris@76: $content = substr($text, $start_pos + strlen($matches[0]), $end_pos - $start_pos - strlen($matches[0])); Chris@76: Chris@76: // Put the tags back into the body. Chris@76: $text = substr($text, 0, $start_pos) . $before . $content . $after . substr($text, $end_pos + 7); Chris@76: } Chris@76: Chris@76: // Almost there, just a little more time. Chris@76: if (connection_aborted() && $context['server']['is_apache']) Chris@76: @apache_reset_timeout(); Chris@76: Chris@76: if (count($parts = preg_split('~<(/?)(li|ol|ul)([^>]*)>~i', $text, null, PREG_SPLIT_DELIM_CAPTURE)) > 1) Chris@76: { Chris@76: // A toggle that dermines whether we're directly under a
    or
      . Chris@76: $inList = false; Chris@76: Chris@76: // Keep track of the number of nested list levels. Chris@76: $listDepth = 0; Chris@76: Chris@76: // Map what we can expect from the HTML to what is supported by SMF. Chris@76: $listTypeMapping = array( Chris@76: '1' => 'decimal', Chris@76: 'A' => 'upper-alpha', Chris@76: 'a' => 'lower-alpha', Chris@76: 'I' => 'upper-roman', Chris@76: 'i' => 'lower-roman', Chris@76: 'disc' => 'disc', Chris@76: 'square' => 'square', Chris@76: 'circle' => 'circle', Chris@76: ); Chris@76: Chris@76: // $i: text, $i + 1: '/', $i + 2: tag, $i + 3: tail. Chris@76: for ($i = 0, $numParts = count($parts) - 1; $i < $numParts; $i += 4) Chris@76: { Chris@76: $tag = strtolower($parts[$i + 2]); Chris@76: $isOpeningTag = $parts[$i + 1] === ''; Chris@76: Chris@76: if ($isOpeningTag) Chris@76: { Chris@76: switch ($tag) Chris@76: { Chris@76: case 'ol': Chris@76: case 'ul': Chris@76: Chris@76: // We have a problem, we're already in a list. Chris@76: if ($inList) Chris@76: { Chris@76: // Inject a list opener, we'll deal with the ol/ul next loop. Chris@76: array_splice($parts, $i, 0, array( Chris@76: '', Chris@76: '', Chris@76: str_repeat("\t", $listDepth) . '[li]', Chris@76: '', Chris@76: )); Chris@76: $numParts = count($parts) - 1; Chris@76: Chris@76: // The inlist status changes a bit. Chris@76: $inList = false; Chris@76: } Chris@76: Chris@76: // Just starting a new list. Chris@76: else Chris@76: { Chris@76: $inList = true; Chris@76: Chris@76: if ($tag === 'ol') Chris@76: $listType = 'decimal'; Chris@76: elseif (preg_match('~type="?(' . implode('|', array_keys($listTypeMapping)) . ')"?~', $parts[$i + 3], $match) === 1) Chris@76: $listType = $listTypeMapping[$match[1]]; Chris@76: else Chris@76: $listType = null; Chris@76: Chris@76: $listDepth++; Chris@76: Chris@76: $parts[$i + 2] = '[list' . ($listType === null ? '' : ' type=' . $listType) . ']' . "\n"; Chris@76: $parts[$i + 3] = ''; Chris@76: } Chris@76: break; Chris@76: Chris@76: case 'li': Chris@76: Chris@76: // This is how it should be: a list item inside the list. Chris@76: if ($inList) Chris@76: { Chris@76: $parts[$i + 2] = str_repeat("\t", $listDepth) . '[li]'; Chris@76: $parts[$i + 3] = ''; Chris@76: Chris@76: // Within a list item, it's almost as if you're outside. Chris@76: $inList = false; Chris@76: } Chris@76: Chris@76: // The li is no direct child of a list. Chris@76: else Chris@76: { Chris@76: // We are apparently in a list item. Chris@76: if ($listDepth > 0) Chris@76: { Chris@76: $parts[$i + 2] = '[/li]' . "\n" . str_repeat("\t", $listDepth) . '[li]'; Chris@76: $parts[$i + 3] = ''; Chris@76: } Chris@76: Chris@76: // We're not even near a list. Chris@76: else Chris@76: { Chris@76: // Quickly create a list with an item. Chris@76: $listDepth++; Chris@76: Chris@76: $parts[$i + 2] = '[list]' . "\n\t" . '[li]'; Chris@76: $parts[$i + 3] = ''; Chris@76: } Chris@76: } Chris@76: Chris@76: break; Chris@76: } Chris@76: } Chris@76: Chris@76: // Handle all the closing tags. Chris@76: else Chris@76: { Chris@76: switch ($tag) Chris@76: { Chris@76: case 'ol': Chris@76: case 'ul': Chris@76: Chris@76: // As we expected it, closing the list while we're in it. Chris@76: if ($inList) Chris@76: { Chris@76: $inList = false; Chris@76: Chris@76: $listDepth--; Chris@76: Chris@76: $parts[$i + 1] = ''; Chris@76: $parts[$i + 2] = str_repeat("\t", $listDepth) . '[/list]'; Chris@76: $parts[$i + 3] = ''; Chris@76: } Chris@76: Chris@76: else Chris@76: { Chris@76: // We're in a list item. Chris@76: if ($listDepth > 0) Chris@76: { Chris@76: // Inject closure for this list item first. Chris@76: // The content of $parts[$i] is left as is! Chris@76: array_splice($parts, $i + 1, 0, array( Chris@76: '', // $i + 1 Chris@76: '[/li]' . "\n", // $i + 2 Chris@76: '', // $i + 3 Chris@76: '', // $i + 4 Chris@76: )); Chris@76: $numParts = count($parts) - 1; Chris@76: Chris@76: // Now that we've closed the li, we're in list space. Chris@76: $inList = true; Chris@76: } Chris@76: Chris@76: // We're not even in a list, ignore Chris@76: else Chris@76: { Chris@76: $parts[$i + 1] = ''; Chris@76: $parts[$i + 2] = ''; Chris@76: $parts[$i + 3] = ''; Chris@76: } Chris@76: } Chris@76: break; Chris@76: Chris@76: case 'li': Chris@76: Chris@76: if ($inList) Chris@76: { Chris@76: // There's no use for a after
        or
          , ignore. Chris@76: $parts[$i + 1] = ''; Chris@76: $parts[$i + 2] = ''; Chris@76: $parts[$i + 3] = ''; Chris@76: } Chris@76: Chris@76: else Chris@76: { Chris@76: // Remove the trailing breaks from the list item. Chris@76: $parts[$i] = preg_replace('~\s*\s*$~', '', $parts[$i]); Chris@76: $parts[$i + 1] = ''; Chris@76: $parts[$i + 2] = '[/li]' . "\n"; Chris@76: $parts[$i + 3] = ''; Chris@76: Chris@76: // And we're back in the [list] space. Chris@76: $inList = true; Chris@76: } Chris@76: Chris@76: break; Chris@76: } Chris@76: } Chris@76: Chris@76: // If we're in the [list] space, no content is allowed. Chris@76: if ($inList && trim(preg_replace('~\s*\s*~', '', $parts[$i + 4])) !== '') Chris@76: { Chris@76: // Fix it by injecting an extra list item. Chris@76: array_splice($parts, $i + 4, 0, array( Chris@76: '', // No content. Chris@76: '', // Opening tag. Chris@76: 'li', // It's a
        • . Chris@76: '', // No tail. Chris@76: )); Chris@76: $numParts = count($parts) - 1; Chris@76: } Chris@76: } Chris@76: Chris@76: $text = implode('', $parts); Chris@76: Chris@76: if ($inList) Chris@76: { Chris@76: $listDepth--; Chris@76: $text .= str_repeat("\t", $listDepth) . '[/list]'; Chris@76: } Chris@76: Chris@76: for ($i = $listDepth; $i > 0; $i--) Chris@76: $text .= '[/li]' . "\n" . str_repeat("\t", $i - 1) . '[/list]'; Chris@76: Chris@76: } Chris@76: Chris@76: // I love my own image... Chris@76: while (preg_match('~]*)/*>~i', $text, $matches) === 1) Chris@76: { Chris@76: // Find the position of the image. Chris@76: $start_pos = strpos($text, $matches[0]); Chris@76: if ($start_pos === false) Chris@76: break; Chris@76: $end_pos = $start_pos + strlen($matches[0]); Chris@76: Chris@76: $params = ''; Chris@76: $had_params = array(); Chris@76: $src = ''; Chris@76: Chris@76: $attrs = fetchTagAttributes($matches[1]); Chris@76: foreach ($attrs as $attrib => $value) Chris@76: { Chris@76: if (in_array($attrib, array('width', 'height'))) Chris@76: $params .= ' ' . $attrib . '=' . (int) $value; Chris@76: elseif ($attrib == 'alt' && trim($value) != '') Chris@76: $params .= ' alt=' . trim($value); Chris@76: elseif ($attrib == 'src') Chris@76: $src = trim($value); Chris@76: } Chris@76: Chris@76: $tag = ''; Chris@76: if (!empty($src)) Chris@76: { Chris@76: // Attempt to fix the path in case it's not present. Chris@76: if (preg_match('~^https?://~i', $src) === 0 && is_array($parsedURL = parse_url($scripturl)) && isset($parsedURL['host'])) Chris@76: { Chris@76: $baseURL = (isset($parsedURL['scheme']) ? $parsedURL['scheme'] : 'http') . '://' . $parsedURL['host'] . (empty($parsedURL['port']) ? '' : ':' . $parsedURL['port']); Chris@76: Chris@76: if (substr($src, 0, 1) === '/') Chris@76: $src = $baseURL . $src; Chris@76: else Chris@76: $src = $baseURL . (empty($parsedURL['path']) ? '/' : preg_replace('~/(?:index\\.php)?$~', '', $parsedURL['path'])) . '/' . $src; Chris@76: } Chris@76: Chris@76: $tag = '[img' . $params . ']' . $src . '[/img]'; Chris@76: } Chris@76: Chris@76: // Replace the tag Chris@76: $text = substr($text, 0, $start_pos) . $tag . substr($text, $end_pos); Chris@76: } Chris@76: Chris@76: // The final bits are the easy ones - tags which map to tags which map to tags - etc etc. Chris@76: $tags = array( Chris@76: '~~i' => '[b]', Chris@76: '~~i' => '[/b]', Chris@76: '~~i' => '[i]', Chris@76: '~~i' => '[/i]', Chris@76: '~~i' => '[u]', Chris@76: '~~i' => '[/u]', Chris@76: '~~i' => '[b]', Chris@76: '~~i' => '[/b]', Chris@76: '~~i' => '[i]', Chris@76: '~~i' => '[/i]', Chris@76: '~~i' => "[s]", Chris@76: '~~i' => "[/s]", Chris@76: '~~i' => '[s]', Chris@76: '~~i' => '[/s]', Chris@76: '~~i' => '[s]', Chris@76: '~~i' => '[/s]', Chris@76: '~~i' => '[center]', Chris@76: '~~i' => '[/center]', Chris@76: '~~i' => '[pre]', Chris@76: '~~i' => '[/pre]', Chris@76: '~~i' => '[sub]', Chris@76: '~~i' => '[/sub]', Chris@76: '~~i' => '[sup]', Chris@76: '~~i' => '[/sup]', Chris@76: '~~i' => '[tt]', Chris@76: '~~i' => '[/tt]', Chris@76: '~~i' => '[table]', Chris@76: '~~i' => '[/table]', Chris@76: '~~i' => '[tr]', Chris@76: '~~i' => '[/tr]', Chris@76: '~<(td|th)\s[^<>]*?colspan="?(\d{1,2})"?.*?' . '>~ie' => 'str_repeat(\'[td][/td]\', $2 - 1) . \'[td]\'', Chris@76: '~<(td|th)(\s(.)*?)*?' . '>~i' => '[td]', Chris@76: '~~i' => '[/td]', Chris@76: '~]*?)?' . '>~i' => "\n", Chris@76: '~]*>(\n)?~i' => "[hr]\n$1", Chris@76: '~(\n)?\\[hr\\]~i' => "\n[hr]", Chris@76: '~^\n\\[hr\\]~i' => "[hr]", Chris@76: '~~i' => "<blockquote>", Chris@76: '~~i' => "</blockquote>", Chris@76: '~~i' => "<ins>", Chris@76: '~~i' => "</ins>", Chris@76: ); Chris@76: $text = preg_replace(array_keys($tags), array_values($tags), $text); Chris@76: Chris@76: // Please give us just a little more time. Chris@76: if (connection_aborted() && $context['server']['is_apache']) Chris@76: @apache_reset_timeout(); Chris@76: Chris@76: // What about URL's - the pain in the ass of the tag world. Chris@76: while (preg_match('~]*)>([^<>]*)~i', $text, $matches) === 1) Chris@76: { Chris@76: // Find the position of the URL. Chris@76: $start_pos = strpos($text, $matches[0]); Chris@76: if ($start_pos === false) Chris@76: break; Chris@76: $end_pos = $start_pos + strlen($matches[0]); Chris@76: Chris@76: $tag_type = 'url'; Chris@76: $href = ''; Chris@76: Chris@76: $attrs = fetchTagAttributes($matches[1]); Chris@76: foreach ($attrs as $attrib => $value) Chris@76: { Chris@76: if ($attrib == 'href') Chris@76: { Chris@76: $href = trim($value); Chris@76: Chris@76: // Are we dealing with an FTP link? Chris@76: if (preg_match('~^ftps?://~', $href) === 1) Chris@76: $tag_type = 'ftp'; Chris@76: Chris@76: // Or is this a link to an email address? Chris@76: elseif (substr($href, 0, 7) == 'mailto:') Chris@76: { Chris@76: $tag_type = 'email'; Chris@76: $href = substr($href, 7); Chris@76: } Chris@76: Chris@76: // No http(s), so attempt to fix this potential relative URL. Chris@76: elseif (preg_match('~^https?://~i', $href) === 0 && is_array($parsedURL = parse_url($scripturl)) && isset($parsedURL['host'])) Chris@76: { Chris@76: $baseURL = (isset($parsedURL['scheme']) ? $parsedURL['scheme'] : 'http') . '://' . $parsedURL['host'] . (empty($parsedURL['port']) ? '' : ':' . $parsedURL['port']); Chris@76: Chris@76: if (substr($href, 0, 1) === '/') Chris@76: $href = $baseURL . $href; Chris@76: else Chris@76: $href = $baseURL . (empty($parsedURL['path']) ? '/' : preg_replace('~/(?:index\\.php)?$~', '', $parsedURL['path'])) . '/' . $href; Chris@76: } Chris@76: } Chris@76: Chris@76: // External URL? Chris@76: if ($attrib == 'target' && $tag_type == 'url') Chris@76: { Chris@76: if (trim($value) == '_blank') Chris@76: $tag_type == 'iurl'; Chris@76: } Chris@76: } Chris@76: Chris@76: $tag = ''; Chris@76: if ($href != '') Chris@76: { Chris@76: if ($matches[2] == $href) Chris@76: $tag = '[' . $tag_type . ']' . $href . '[/' . $tag_type . ']'; Chris@76: else Chris@76: $tag = '[' . $tag_type . '=' . $href . ']' . $matches[2] . '[/' . $tag_type . ']'; Chris@76: } Chris@76: Chris@76: // Replace the tag Chris@76: $text = substr($text, 0, $start_pos) . $tag . substr($text, $end_pos); Chris@76: } Chris@76: Chris@76: $text = strip_tags($text); Chris@76: Chris@76: // Some tags often end up as just dummy tags - remove those. Chris@76: $text = preg_replace('~\[[bisu]\]\s*\[/[bisu]\]~', '', $text); Chris@76: Chris@76: // Fix up entities. Chris@76: $text = preg_replace('~&~i', '&#38;', $text); Chris@76: Chris@76: $text = legalise_bbc($text); Chris@76: Chris@76: return $text; Chris@76: } Chris@76: Chris@76: // Returns an array of attributes associated with a tag. Chris@76: function fetchTagAttributes($text) Chris@76: { Chris@76: $attribs = array(); Chris@76: $key = $value = ''; Chris@76: $strpos = 0; Chris@76: $tag_state = 0; // 0 = key, 1 = attribute with no string, 2 = attribute with string Chris@76: for ($i = 0; $i < strlen($text); $i++) Chris@76: { Chris@76: // We're either moving from the key to the attribute or we're in a string and this is fine. Chris@76: if ($text{$i} == '=') Chris@76: { Chris@76: if ($tag_state == 0) Chris@76: $tag_state = 1; Chris@76: elseif ($tag_state == 2) Chris@76: $value .= '='; Chris@76: } Chris@76: // A space is either moving from an attribute back to a potential key or in a string is fine. Chris@76: elseif ($text{$i} == ' ') Chris@76: { Chris@76: if ($tag_state == 2) Chris@76: $value .= ' '; Chris@76: elseif ($tag_state == 1) Chris@76: { Chris@76: $attribs[$key] = $value; Chris@76: $key = $value = ''; Chris@76: $tag_state = 0; Chris@76: } Chris@76: } Chris@76: // A quote? Chris@76: elseif ($text{$i} == '"') Chris@76: { Chris@76: // Must be either going into or out of a string. Chris@76: if ($tag_state == 1) Chris@76: $tag_state = 2; Chris@76: else Chris@76: $tag_state = 1; Chris@76: } Chris@76: // Otherwise it's fine. Chris@76: else Chris@76: { Chris@76: if ($tag_state == 0) Chris@76: $key .= $text{$i}; Chris@76: else Chris@76: $value .= $text{$i}; Chris@76: } Chris@76: } Chris@76: Chris@76: // Anything left? Chris@76: if ($key != '' && $value != '') Chris@76: $attribs[$key] = $value; Chris@76: Chris@76: return $attribs; Chris@76: } Chris@76: Chris@76: function getMessageIcons($board_id) Chris@76: { Chris@76: global $modSettings, $context, $txt, $settings, $smcFunc; Chris@76: Chris@76: if (empty($modSettings['messageIcons_enable'])) Chris@76: { Chris@76: loadLanguage('Post'); Chris@76: Chris@76: $icons = array( Chris@76: array('value' => 'xx', 'name' => $txt['standard']), Chris@76: array('value' => 'thumbup', 'name' => $txt['thumbs_up']), Chris@76: array('value' => 'thumbdown', 'name' => $txt['thumbs_down']), Chris@76: array('value' => 'exclamation', 'name' => $txt['excamation_point']), Chris@76: array('value' => 'question', 'name' => $txt['question_mark']), Chris@76: array('value' => 'lamp', 'name' => $txt['lamp']), Chris@76: array('value' => 'smiley', 'name' => $txt['icon_smiley']), Chris@76: array('value' => 'angry', 'name' => $txt['icon_angry']), Chris@76: array('value' => 'cheesy', 'name' => $txt['icon_cheesy']), Chris@76: array('value' => 'grin', 'name' => $txt['icon_grin']), Chris@76: array('value' => 'sad', 'name' => $txt['icon_sad']), Chris@76: array('value' => 'wink', 'name' => $txt['icon_wink']) Chris@76: ); Chris@76: Chris@76: foreach ($icons as $k => $dummy) Chris@76: { Chris@76: $icons[$k]['url'] = $settings['images_url'] . '/post/' . $dummy['value'] . '.gif'; Chris@76: $icons[$k]['is_last'] = false; Chris@76: } Chris@76: } Chris@76: // Otherwise load the icons, and check we give the right image too... Chris@76: else Chris@76: { Chris@76: if (($temp = cache_get_data('posting_icons-' . $board_id, 480)) == null) Chris@76: { Chris@76: $request = $smcFunc['db_query']('select_message_icons', ' Chris@76: SELECT title, filename Chris@76: FROM {db_prefix}message_icons Chris@76: WHERE id_board IN (0, {int:board_id})', Chris@76: array( Chris@76: 'board_id' => $board_id, Chris@76: ) Chris@76: ); Chris@76: $icon_data = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: $icon_data[] = $row; Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: cache_put_data('posting_icons-' . $board_id, $icon_data, 480); Chris@76: } Chris@76: else Chris@76: $icon_data = $temp; Chris@76: Chris@76: $icons = array(); Chris@76: foreach ($icon_data as $icon) Chris@76: { Chris@76: $icons[$icon['filename']] = array( Chris@76: 'value' => $icon['filename'], Chris@76: 'name' => $icon['title'], Chris@76: 'url' => $settings[file_exists($settings['theme_dir'] . '/images/post/' . $icon['filename'] . '.gif') ? 'images_url' : 'default_images_url'] . '/post/' . $icon['filename'] . '.gif', Chris@76: 'is_last' => false, Chris@76: ); Chris@76: } Chris@76: } Chris@76: Chris@76: return array_values($icons); Chris@76: } Chris@76: Chris@76: // This is an important yet frustrating function - it attempts to clean up illegal BBC caused by browsers like Opera which don't obey the rules!!! Chris@76: function legalise_bbc($text) Chris@76: { Chris@76: global $modSettings; Chris@76: Chris@76: // Don't care about the texts that are too short. Chris@76: if (strlen($text) < 3) Chris@76: return $text; Chris@76: Chris@76: // We are going to cycle through the BBC and keep track of tags as they arise - in order. If get to a block level tag we're going to make sure it's not in a non-block level tag! Chris@76: // This will keep the order of tags that are open. Chris@76: $current_tags = array(); Chris@76: Chris@76: // This will quickly let us see if the tag is active. Chris@76: $active_tags = array(); Chris@76: Chris@76: // A list of tags that's disabled by the admin. Chris@76: $disabled = empty($modSettings['disabledBBC']) ? array() : array_flip(explode(',', strtolower($modSettings['disabledBBC']))); Chris@76: Chris@76: // Add flash if it's disabled as embedded tag. Chris@76: if (empty($modSettings['enableEmbeddedFlash'])) Chris@76: $disabled['flash'] = true; Chris@76: Chris@76: // Get a list of all the tags that are not disabled. Chris@76: $all_tags = parse_bbc(false); Chris@76: $valid_tags = array(); Chris@76: $self_closing_tags = array(); Chris@76: foreach ($all_tags as $tag) Chris@76: { Chris@76: if (!isset($disabled[$tag['tag']])) Chris@76: $valid_tags[$tag['tag']] = !empty($tag['block_level']); Chris@76: if (isset($tag['type']) && $tag['type'] == 'closed') Chris@76: $self_closing_tags[] = $tag['tag']; Chris@76: } Chris@76: Chris@76: // Don't worry if we're in a code/nobbc. Chris@76: $in_code_nobbc = false; Chris@76: Chris@76: // Right - we're going to start by going through the whole lot to make sure we don't have align stuff crossed as this happens load and is stupid! Chris@76: $align_tags = array('left', 'center', 'right', 'pre'); Chris@76: Chris@76: // Remove those align tags that are not valid. Chris@76: $align_tags = array_intersect($align_tags, array_keys($valid_tags)); Chris@76: Chris@76: // These keep track of where we are! Chris@76: if (!empty($align_tags) && count($matches = preg_split('~(\\[/?(?:' . implode('|', $align_tags) . ')\\])~', $text, -1, PREG_SPLIT_DELIM_CAPTURE)) > 1) Chris@76: { Chris@76: // The first one is never a tag. Chris@76: $isTag = false; Chris@76: Chris@76: // By default we're not inside a tag too. Chris@76: $insideTag = null; Chris@76: Chris@76: foreach ($matches as $i => $match) Chris@76: { Chris@76: // We're only interested in tags, not text. Chris@76: if ($isTag) Chris@76: { Chris@76: $isClosingTag = substr($match, 1, 1) === '/'; Chris@76: $tagName = substr($match, $isClosingTag ? 2 : 1, -1); Chris@76: Chris@76: // We're closing the exact same tag that we opened. Chris@76: if ($isClosingTag && $insideTag === $tagName) Chris@76: $insideTag = null; Chris@76: Chris@76: // We're opening a tag and we're not yet inside one either Chris@76: elseif (!$isClosingTag && $insideTag === null) Chris@76: $insideTag = $tagName; Chris@76: Chris@76: // In all other cases, this tag must be invalid Chris@76: else Chris@76: unset($matches[$i]); Chris@76: } Chris@76: Chris@76: // The next one is gonna be the other one. Chris@76: $isTag = !$isTag; Chris@76: } Chris@76: Chris@76: // We're still inside a tag and had no chance for closure? Chris@76: if ($insideTag !== null) Chris@76: $matches[] = '[/' . $insideTag . ']'; Chris@76: Chris@76: // And a complete text string again. Chris@76: $text = implode('', $matches); Chris@76: } Chris@76: Chris@76: // Quickly remove any tags which are back to back. Chris@76: $backToBackPattern = '~\\[(' . implode('|', array_diff(array_keys($valid_tags), array('td', 'anchor'))) . ')[^<>\\[\\]]*\\]\s*\\[/\\1\\]~'; Chris@76: $lastlen = 0; Chris@76: while (strlen($text) !== $lastlen) Chris@76: $lastlen = strlen($text = preg_replace($backToBackPattern, '', $text)); Chris@76: Chris@76: // Need to sort the tags my name length. Chris@76: uksort($valid_tags, 'sort_array_length'); Chris@76: Chris@76: // These inline tags can compete with each other regarding style. Chris@76: $competing_tags = array( Chris@76: 'color', Chris@76: 'size', Chris@76: ); Chris@76: Chris@76: // In case things changed above set these back to normal. Chris@76: $in_code_nobbc = false; Chris@76: $new_text_offset = 0; Chris@76: Chris@76: // These keep track of where we are! Chris@76: if (count($parts = preg_split(sprintf('~(\\[)(/?)(%1$s)((?:[\\s=][^\\]\\[]*)?\\])~', implode('|', array_keys($valid_tags))), $text, -1, PREG_SPLIT_DELIM_CAPTURE)) > 1) Chris@76: { Chris@76: // Start with just text. Chris@76: $isTag = false; Chris@76: Chris@76: // Start outside [nobbc] or [code] blocks. Chris@76: $inCode = false; Chris@76: $inNoBbc = false; Chris@76: Chris@76: // A buffer containing all opened inline elements. Chris@76: $inlineElements = array(); Chris@76: Chris@76: // A buffer containing all opened block elements. Chris@76: $blockElements = array(); Chris@76: Chris@76: // A buffer containing the opened inline elements that might compete. Chris@76: $competingElements = array(); Chris@76: Chris@76: // $i: text, $i + 1: '[', $i + 2: '/', $i + 3: tag, $i + 4: tag tail. Chris@76: for ($i = 0, $n = count($parts) - 1; $i < $n; $i += 5) Chris@76: { Chris@76: $tag = $parts[$i + 3]; Chris@76: $isOpeningTag = $parts[$i + 2] === ''; Chris@76: $isClosingTag = $parts[$i + 2] === '/'; Chris@76: $isBlockLevelTag = isset($valid_tags[$tag]) && $valid_tags[$tag] && !in_array($tag, $self_closing_tags); Chris@76: $isCompetingTag = in_array($tag, $competing_tags); Chris@76: Chris@76: // Check if this might be one of those cleaned out tags. Chris@76: if ($tag === '') Chris@76: continue; Chris@76: Chris@76: // Special case: inside [code] blocks any code is left untouched. Chris@76: elseif ($tag === 'code') Chris@76: { Chris@76: // We're inside a code block and closing it. Chris@76: if ($inCode && $isClosingTag) Chris@76: { Chris@76: $inCode = false; Chris@76: Chris@76: // Reopen tags that were closed before the code block. Chris@76: if (!empty($inlineElements)) Chris@76: $parts[$i + 4] .= '[' . implode('][', array_keys($inlineElements)) . ']'; Chris@76: } Chris@76: Chris@76: // We're outside a coding and nobbc block and opening it. Chris@76: elseif (!$inCode && !$inNoBbc && $isOpeningTag) Chris@76: { Chris@76: // If there are still inline elements left open, close them now. Chris@76: if (!empty($inlineElements)) Chris@76: { Chris@76: $parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']'; Chris@76: //$inlineElements = array(); Chris@76: } Chris@76: Chris@76: $inCode = true; Chris@76: } Chris@76: Chris@76: // Nothing further to do. Chris@76: continue; Chris@76: } Chris@76: Chris@76: // Special case: inside [nobbc] blocks any BBC is left untouched. Chris@76: elseif ($tag === 'nobbc') Chris@76: { Chris@76: // We're inside a nobbc block and closing it. Chris@76: if ($inNoBbc && $isClosingTag) Chris@76: { Chris@76: $inNoBbc = false; Chris@76: Chris@76: // Some inline elements might've been closed that need reopening. Chris@76: if (!empty($inlineElements)) Chris@76: $parts[$i + 4] .= '[' . implode('][', array_keys($inlineElements)) . ']'; Chris@76: } Chris@76: Chris@76: // We're outside a nobbc and coding block and opening it. Chris@76: elseif (!$inNoBbc && !$inCode && $isOpeningTag) Chris@76: { Chris@76: // Can't have inline elements still opened. Chris@76: if (!empty($inlineElements)) Chris@76: { Chris@76: $parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']'; Chris@76: //$inlineElements = array(); Chris@76: } Chris@76: Chris@76: $inNoBbc = true; Chris@76: } Chris@76: Chris@76: continue; Chris@76: } Chris@76: Chris@76: // So, we're inside one of the special blocks: ignore any tag. Chris@76: elseif ($inCode || $inNoBbc) Chris@76: continue; Chris@76: Chris@76: // We're dealing with an opening tag. Chris@76: if ($isOpeningTag) Chris@76: { Chris@76: // Everyting inside the square brackets of the opening tag. Chris@76: $elementContent = $parts[$i + 3] . substr($parts[$i + 4], 0, -1); Chris@76: Chris@76: // A block level opening tag. Chris@76: if ($isBlockLevelTag) Chris@76: { Chris@76: // Are there inline elements still open? Chris@76: if (!empty($inlineElements)) Chris@76: { Chris@76: // Close all the inline tags, a block tag is coming... Chris@76: $parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']'; Chris@76: Chris@76: // Now open them again, we're inside the block tag now. Chris@76: $parts[$i + 5] = '[' . implode('][', array_keys($inlineElements)) . ']' . $parts[$i + 5]; Chris@76: } Chris@76: Chris@76: $blockElements[] = $tag; Chris@76: } Chris@76: Chris@76: // Inline opening tag. Chris@76: elseif (!in_array($tag, $self_closing_tags)) Chris@76: { Chris@76: // Can't have two opening elements with the same contents! Chris@76: if (isset($inlineElements[$elementContent])) Chris@76: { Chris@76: // Get rid of this tag. Chris@76: $parts[$i + 1] = $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = ''; Chris@76: Chris@76: // Now try to find the corresponding closing tag. Chris@76: $curLevel = 1; Chris@76: for ($j = $i + 5, $m = count($parts) - 1; $j < $m; $j += 5) Chris@76: { Chris@76: // Find the tags with the same tagname Chris@76: if ($parts[$j + 3] === $tag) Chris@76: { Chris@76: // If it's an opening tag, increase the level. Chris@76: if ($parts[$j + 2] === '') Chris@76: $curLevel++; Chris@76: Chris@76: // A closing tag, decrease the level. Chris@76: else Chris@76: { Chris@76: $curLevel--; Chris@76: Chris@76: // Gotcha! Clean out this closing tag gone rogue. Chris@76: if ($curLevel === 0) Chris@76: { Chris@76: $parts[$j + 1] = $parts[$j + 2] = $parts[$j + 3] = $parts[$j + 4] = ''; Chris@76: break; Chris@76: } Chris@76: } Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@76: // Otherwise, add this one to the list. Chris@76: else Chris@76: { Chris@76: if ($isCompetingTag) Chris@76: { Chris@76: if (!isset($competingElements[$tag])) Chris@76: $competingElements[$tag] = array(); Chris@76: Chris@76: $competingElements[$tag][] = $parts[$i + 4]; Chris@76: Chris@76: if (count($competingElements[$tag]) > 1) Chris@76: $parts[$i] .= '[/' . $tag . ']'; Chris@76: } Chris@76: Chris@76: $inlineElements[$elementContent] = $tag; Chris@76: } Chris@76: } Chris@76: Chris@76: } Chris@76: Chris@76: // Closing tag. Chris@76: else Chris@76: { Chris@76: // Closing the block tag. Chris@76: if ($isBlockLevelTag) Chris@76: { Chris@76: // Close the elements that should've been closed by closing this tag. Chris@76: if (!empty($blockElements)) Chris@76: { Chris@76: $addClosingTags = array(); Chris@76: while ($element = array_pop($blockElements)) Chris@76: { Chris@76: if ($element === $tag) Chris@76: break; Chris@76: Chris@76: // Still a block tag was open not equal to this tag. Chris@76: $addClosingTags[] = $element['type']; Chris@76: } Chris@76: Chris@76: if (!empty($addClosingTags)) Chris@76: $parts[$i + 1] = '[/' . implode('][/', array_reverse($addClosingTags)) . ']' . $parts[$i + 1]; Chris@76: Chris@76: // Apparently the closing tag was not found on the stack. Chris@76: if (!is_string($element) || $element !== $tag) Chris@76: { Chris@76: // Get rid of this particular closing tag, it was never opened. Chris@76: $parts[$i + 1] = substr($parts[$i + 1], 0, -1); Chris@76: $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = ''; Chris@76: continue; Chris@76: } Chris@76: } Chris@76: else Chris@76: { Chris@76: // Get rid of this closing tag! Chris@76: $parts[$i + 1] = $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = ''; Chris@76: continue; Chris@76: } Chris@76: Chris@76: // Inline elements are still left opened? Chris@76: if (!empty($inlineElements)) Chris@76: { Chris@76: // Close them first.. Chris@76: $parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']'; Chris@76: Chris@76: // Then reopen them. Chris@76: $parts[$i + 5] = '[' . implode('][', array_keys($inlineElements)) . ']' . $parts[$i + 5]; Chris@76: } Chris@76: } Chris@76: // Inline tag. Chris@76: else Chris@76: { Chris@76: // Are we expecting this tag to end? Chris@76: if (in_array($tag, $inlineElements)) Chris@76: { Chris@76: foreach (array_reverse($inlineElements, true) as $tagContentToBeClosed => $tagToBeClosed) Chris@76: { Chris@76: // Closing it one way or the other. Chris@76: unset($inlineElements[$tagContentToBeClosed]); Chris@76: Chris@76: // Was this the tag we were looking for? Chris@76: if ($tagToBeClosed === $tag) Chris@76: break; Chris@76: Chris@76: // Nope, close it and look further! Chris@76: else Chris@76: $parts[$i] .= '[/' . $tagToBeClosed . ']'; Chris@76: } Chris@76: Chris@76: if ($isCompetingTag && !empty($competingElements[$tag])) Chris@76: { Chris@76: array_pop($competingElements[$tag]); Chris@76: Chris@76: if (count($competingElements[$tag]) > 0) Chris@76: $parts[$i + 5] = '[' . $tag . $competingElements[$tag][count($competingElements[$tag]) - 1] . $parts[$i + 5]; Chris@76: } Chris@76: } Chris@76: Chris@76: // Unexpected closing tag, ex-ter-mi-nate. Chris@76: else Chris@76: $parts[$i + 1] = $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = ''; Chris@76: } Chris@76: } Chris@76: } Chris@76: Chris@76: // Close the code tags. Chris@76: if ($inCode) Chris@76: $parts[$i] .= '[/code]'; Chris@76: Chris@76: // The same for nobbc tags. Chris@76: elseif ($inNoBbc) Chris@76: $parts[$i] .= '[/nobbc]'; Chris@76: Chris@76: // Still inline tags left unclosed? Close them now, better late than never. Chris@76: elseif (!empty($inlineElements)) Chris@76: $parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']'; Chris@76: Chris@76: // Now close the block elements. Chris@76: if (!empty($blockElements)) Chris@76: $parts[$i] .= '[/' . implode('][/', array_reverse($blockElements)) . ']'; Chris@76: Chris@76: $text = implode('', $parts); Chris@76: } Chris@76: Chris@76: // Final clean up of back to back tags. Chris@76: $lastlen = 0; Chris@76: while (strlen($text) !== $lastlen) Chris@76: $lastlen = strlen($text = preg_replace($backToBackPattern, '', $text)); Chris@76: Chris@76: return $text; Chris@76: } Chris@76: Chris@76: // A help function for legalise_bbc for sorting arrays based on length. Chris@76: function sort_array_length($a, $b) Chris@76: { Chris@76: return strlen($a) < strlen($b) ? 1 : -1; Chris@76: } Chris@76: Chris@76: // Compatibility function - used in 1.1 for showing a post box. Chris@76: function theme_postbox($msg) Chris@76: { Chris@76: global $context; Chris@76: Chris@76: return template_control_richedit($context['post_box_name']); Chris@76: } Chris@76: Chris@76: // Creates a box that can be used for richedit stuff like BBC, Smileys etc. Chris@76: function create_control_richedit($editorOptions) Chris@76: { Chris@76: global $txt, $modSettings, $options, $smcFunc; Chris@76: global $context, $settings, $user_info, $sourcedir, $scripturl; Chris@76: Chris@76: // Load the Post language file... for the moment at least. Chris@76: loadLanguage('Post'); Chris@76: Chris@76: // Every control must have a ID! Chris@76: assert(isset($editorOptions['id'])); Chris@76: assert(isset($editorOptions['value'])); Chris@76: Chris@76: // Is this the first richedit - if so we need to ensure some template stuff is initialised. Chris@76: if (empty($context['controls']['richedit'])) Chris@76: { Chris@76: // Some general stuff. Chris@76: $settings['smileys_url'] = $modSettings['smileys_url'] . '/' . $user_info['smiley_set']; Chris@76: Chris@76: // This really has some WYSIWYG stuff. Chris@76: loadTemplate('GenericControls', $context['browser']['is_ie'] ? 'editor_ie' : 'editor'); Chris@76: $context['html_headers'] .= ' Chris@76: Chris@76: '; Chris@76: Chris@76: $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && function_exists('pspell_new'); Chris@76: if ($context['show_spellchecking']) Chris@76: { Chris@76: $context['html_headers'] .= ' Chris@76: '; Chris@76: Chris@76: // Some hidden information is needed in order to make the spell checking work. Chris@76: if (!isset($_REQUEST['xml'])) Chris@76: $context['insert_after_template'] .= ' Chris@76:
          Chris@76: Chris@76:
          '; Chris@76: Chris@76: // Also make sure that spell check works with rich edit. Chris@76: $context['html_headers'] .= ' Chris@76: '; Chris@76: } Chris@76: } Chris@76: Chris@76: // Start off the editor... Chris@76: $context['controls']['richedit'][$editorOptions['id']] = array( Chris@76: 'id' => $editorOptions['id'], Chris@76: 'value' => $editorOptions['value'], Chris@76: 'rich_value' => bbc_to_html($editorOptions['value']), Chris@76: 'rich_active' => empty($modSettings['disable_wysiwyg']) && (!empty($options['wysiwyg_default']) || !empty($editorOptions['force_rich']) || !empty($_REQUEST[$editorOptions['id'] . '_mode'])), Chris@76: 'disable_smiley_box' => !empty($editorOptions['disable_smiley_box']), Chris@76: 'columns' => isset($editorOptions['columns']) ? $editorOptions['columns'] : 60, Chris@76: 'rows' => isset($editorOptions['rows']) ? $editorOptions['rows'] : 12, Chris@76: 'width' => isset($editorOptions['width']) ? $editorOptions['width'] : '70%', Chris@76: 'height' => isset($editorOptions['height']) ? $editorOptions['height'] : '150px', Chris@76: 'form' => isset($editorOptions['form']) ? $editorOptions['form'] : 'postmodify', Chris@76: 'bbc_level' => !empty($editorOptions['bbc_level']) ? $editorOptions['bbc_level'] : 'full', Chris@76: 'preview_type' => isset($editorOptions['preview_type']) ? (int) $editorOptions['preview_type'] : 1, Chris@76: 'labels' => !empty($editorOptions['labels']) ? $editorOptions['labels'] : array(), Chris@76: ); Chris@76: Chris@76: // Switch between default images and back... mostly in case you don't have an PersonalMessage template, but do have a Post template. Chris@76: if (isset($settings['use_default_images']) && $settings['use_default_images'] == 'defaults' && isset($settings['default_template'])) Chris@76: { Chris@76: $temp1 = $settings['theme_url']; Chris@76: $settings['theme_url'] = $settings['default_theme_url']; Chris@76: Chris@76: $temp2 = $settings['images_url']; Chris@76: $settings['images_url'] = $settings['default_images_url']; Chris@76: Chris@76: $temp3 = $settings['theme_dir']; Chris@76: $settings['theme_dir'] = $settings['default_theme_dir']; Chris@76: } Chris@76: Chris@76: if (empty($context['bbc_tags'])) Chris@76: { Chris@76: // The below array makes it dead easy to add images to this control. Add it to the array and everything else is done for you! Chris@76: $context['bbc_tags'] = array(); Chris@76: $context['bbc_tags'][] = array( Chris@76: array( Chris@76: 'image' => 'bold', Chris@76: 'code' => 'b', Chris@76: 'before' => '[b]', Chris@76: 'after' => '[/b]', Chris@76: 'description' => $txt['bold'], Chris@76: ), Chris@76: array( Chris@76: 'image' => 'italicize', Chris@76: 'code' => 'i', Chris@76: 'before' => '[i]', Chris@76: 'after' => '[/i]', Chris@76: 'description' => $txt['italic'], Chris@76: ), Chris@76: array( Chris@76: 'image' => 'underline', Chris@76: 'code' => 'u', Chris@76: 'before' => '[u]', Chris@76: 'after' => '[/u]', Chris@76: 'description' => $txt['underline'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'strike', Chris@76: 'code' => 's', Chris@76: 'before' => '[s]', Chris@76: 'after' => '[/s]', Chris@76: 'description' => $txt['strike'] Chris@76: ), Chris@76: array(), Chris@76: array( Chris@76: 'image' => 'pre', Chris@76: 'code' => 'pre', Chris@76: 'before' => '[pre]', Chris@76: 'after' => '[/pre]', Chris@76: 'description' => $txt['preformatted'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'left', Chris@76: 'code' => 'left', Chris@76: 'before' => '[left]', Chris@76: 'after' => '[/left]', Chris@76: 'description' => $txt['left_align'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'center', Chris@76: 'code' => 'center', Chris@76: 'before' => '[center]', Chris@76: 'after' => '[/center]', Chris@76: 'description' => $txt['center'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'right', Chris@76: 'code' => 'right', Chris@76: 'before' => '[right]', Chris@76: 'after' => '[/right]', Chris@76: 'description' => $txt['right_align'] Chris@76: ), Chris@76: ); Chris@76: $context['bbc_tags'][] = array( Chris@76: array( Chris@76: 'image' => 'flash', Chris@76: 'code' => 'flash', Chris@76: 'before' => '[flash=200,200]', Chris@76: 'after' => '[/flash]', Chris@76: 'description' => $txt['flash'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'img', Chris@76: 'code' => 'img', Chris@76: 'before' => '[img]', Chris@76: 'after' => '[/img]', Chris@76: 'description' => $txt['image'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'url', Chris@76: 'code' => 'url', Chris@76: 'before' => '[url]', Chris@76: 'after' => '[/url]', Chris@76: 'description' => $txt['hyperlink'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'email', Chris@76: 'code' => 'email', Chris@76: 'before' => '[email]', Chris@76: 'after' => '[/email]', Chris@76: 'description' => $txt['insert_email'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'ftp', Chris@76: 'code' => 'ftp', Chris@76: 'before' => '[ftp]', Chris@76: 'after' => '[/ftp]', Chris@76: 'description' => $txt['ftp'] Chris@76: ), Chris@76: array(), Chris@76: array( Chris@76: 'image' => 'glow', Chris@76: 'code' => 'glow', Chris@76: 'before' => '[glow=red,2,300]', Chris@76: 'after' => '[/glow]', Chris@76: 'description' => $txt['glow'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'shadow', Chris@76: 'code' => 'shadow', Chris@76: 'before' => '[shadow=red,left]', Chris@76: 'after' => '[/shadow]', Chris@76: 'description' => $txt['shadow'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'move', Chris@76: 'code' => 'move', Chris@76: 'before' => '[move]', Chris@76: 'after' => '[/move]', Chris@76: 'description' => $txt['marquee'] Chris@76: ), Chris@76: array(), Chris@76: array( Chris@76: 'image' => 'sup', Chris@76: 'code' => 'sup', Chris@76: 'before' => '[sup]', Chris@76: 'after' => '[/sup]', Chris@76: 'description' => $txt['superscript'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'sub', Chris@76: 'code' => 'sub', Chris@76: 'before' => '[sub]', Chris@76: 'after' => '[/sub]', Chris@76: 'description' => $txt['subscript'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'tele', Chris@76: 'code' => 'tt', Chris@76: 'before' => '[tt]', Chris@76: 'after' => '[/tt]', Chris@76: 'description' => $txt['teletype'] Chris@76: ), Chris@76: array(), Chris@76: array( Chris@76: 'image' => 'table', Chris@76: 'code' => 'table', Chris@76: 'before' => '[table]\n[tr]\n[td]', Chris@76: 'after' => '[/td]\n[/tr]\n[/table]', Chris@76: 'description' => $txt['table'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'code', Chris@76: 'code' => 'code', Chris@76: 'before' => '[code]', Chris@76: 'after' => '[/code]', Chris@76: 'description' => $txt['bbc_code'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'quote', Chris@76: 'code' => 'quote', Chris@76: 'before' => '[quote]', Chris@76: 'after' => '[/quote]', Chris@76: 'description' => $txt['bbc_quote'] Chris@76: ), Chris@76: array(), Chris@76: array( Chris@76: 'image' => 'list', Chris@76: 'code' => 'list', Chris@76: 'before' => '[list]\n[li]', Chris@76: 'after' => '[/li]\n[li][/li]\n[/list]', Chris@76: 'description' => $txt['list_unordered'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'orderlist', Chris@76: 'code' => 'orderlist', Chris@76: 'before' => '[list type=decimal]\n[li]', Chris@76: 'after' => '[/li]\n[li][/li]\n[/list]', Chris@76: 'description' => $txt['list_ordered'] Chris@76: ), Chris@76: array( Chris@76: 'image' => 'hr', Chris@76: 'code' => 'hr', Chris@76: 'before' => '[hr]', Chris@76: 'description' => $txt['horizontal_rule'] Chris@76: ), Chris@76: ); Chris@76: Chris@76: // Allow mods to modify BBC buttons. Chris@76: call_integration_hook('integrate_bbc_buttons', array(&$context['bbc_tags'])); Chris@76: Chris@76: // Show the toggle? Chris@76: if (empty($modSettings['disable_wysiwyg'])) Chris@76: { Chris@76: $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array(); Chris@76: $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array( Chris@76: 'image' => 'unformat', Chris@76: 'code' => 'unformat', Chris@76: 'before' => '', Chris@76: 'description' => $txt['unformat_text'], Chris@76: ); Chris@76: $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array( Chris@76: 'image' => 'toggle', Chris@76: 'code' => 'toggle', Chris@76: 'before' => '', Chris@76: 'description' => $txt['toggle_view'], Chris@76: ); Chris@76: } Chris@76: Chris@76: foreach ($context['bbc_tags'] as $row => $tagRow) Chris@76: $context['bbc_tags'][$row][count($tagRow) - 1]['isLast'] = true; Chris@76: } Chris@76: Chris@76: // Initialize smiley array... if not loaded before. Chris@76: if (empty($context['smileys']) && empty($editorOptions['disable_smiley_box'])) Chris@76: { Chris@76: $context['smileys'] = array( Chris@76: 'postform' => array(), Chris@76: 'popup' => array(), Chris@76: ); Chris@76: Chris@76: // Load smileys - don't bother to run a query if we're not using the database's ones anyhow. Chris@76: if (empty($modSettings['smiley_enable']) && $user_info['smiley_set'] != 'none') Chris@76: $context['smileys']['postform'][] = array( Chris@76: 'smileys' => array( Chris@76: array( Chris@76: 'code' => ':)', Chris@76: 'filename' => 'smiley.gif', Chris@76: 'description' => $txt['icon_smiley'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ';)', Chris@76: 'filename' => 'wink.gif', Chris@76: 'description' => $txt['icon_wink'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ':D', Chris@76: 'filename' => 'cheesy.gif', Chris@76: 'description' => $txt['icon_cheesy'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ';D', Chris@76: 'filename' => 'grin.gif', Chris@76: 'description' => $txt['icon_grin'] Chris@76: ), Chris@76: array( Chris@76: 'code' => '>:(', Chris@76: 'filename' => 'angry.gif', Chris@76: 'description' => $txt['icon_angry'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ':(', Chris@76: 'filename' => 'sad.gif', Chris@76: 'description' => $txt['icon_sad'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ':o', Chris@76: 'filename' => 'shocked.gif', Chris@76: 'description' => $txt['icon_shocked'], Chris@76: ), Chris@76: array( Chris@76: 'code' => '8)', Chris@76: 'filename' => 'cool.gif', Chris@76: 'description' => $txt['icon_cool'], Chris@76: ), Chris@76: array( Chris@76: 'code' => '???', Chris@76: 'filename' => 'huh.gif', Chris@76: 'description' => $txt['icon_huh'], Chris@76: ), Chris@76: array( Chris@76: 'code' => '::)', Chris@76: 'filename' => 'rolleyes.gif', Chris@76: 'description' => $txt['icon_rolleyes'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ':P', Chris@76: 'filename' => 'tongue.gif', Chris@76: 'description' => $txt['icon_tongue'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ':-[', Chris@76: 'filename' => 'embarrassed.gif', Chris@76: 'description' => $txt['icon_embarrassed'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ':-X', Chris@76: 'filename' => 'lipsrsealed.gif', Chris@76: 'description' => $txt['icon_lips'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ':-\\', Chris@76: 'filename' => 'undecided.gif', Chris@76: 'description' => $txt['icon_undecided'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ':-*', Chris@76: 'filename' => 'kiss.gif', Chris@76: 'description' => $txt['icon_kiss'], Chris@76: ), Chris@76: array( Chris@76: 'code' => ':\'(', Chris@76: 'filename' => 'cry.gif', Chris@76: 'description' => $txt['icon_cry'], Chris@76: 'isLast' => true, Chris@76: ), Chris@76: ), Chris@76: 'isLast' => true, Chris@76: ); Chris@76: elseif ($user_info['smiley_set'] != 'none') Chris@76: { Chris@76: if (($temp = cache_get_data('posting_smileys', 480)) == null) Chris@76: { Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT code, filename, description, smiley_row, hidden Chris@76: FROM {db_prefix}smileys Chris@76: WHERE hidden IN (0, 2) Chris@76: ORDER BY smiley_row, smiley_order', Chris@76: array( Chris@76: ) Chris@76: ); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: $row['filename'] = htmlspecialchars($row['filename']); Chris@76: $row['description'] = htmlspecialchars($row['description']); Chris@76: Chris@76: $context['smileys'][empty($row['hidden']) ? 'postform' : 'popup'][$row['smiley_row']]['smileys'][] = $row; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: foreach ($context['smileys'] as $section => $smileyRows) Chris@76: { Chris@76: foreach ($smileyRows as $rowIndex => $smileys) Chris@76: $context['smileys'][$section][$rowIndex]['smileys'][count($smileys['smileys']) - 1]['isLast'] = true; Chris@76: Chris@76: if (!empty($smileyRows)) Chris@76: $context['smileys'][$section][count($smileyRows) - 1]['isLast'] = true; Chris@76: } Chris@76: Chris@76: cache_put_data('posting_smileys', $context['smileys'], 480); Chris@76: } Chris@76: else Chris@76: $context['smileys'] = $temp; Chris@76: } Chris@76: } Chris@76: Chris@76: // Set a flag so the sub template knows what to do... Chris@76: $context['show_bbc'] = !empty($modSettings['enableBBC']) && !empty($settings['show_bbc']); Chris@76: Chris@76: // Generate a list of buttons that shouldn't be shown - this should be the fastest way to do this. Chris@76: $disabled_tags = array(); Chris@76: if (!empty($modSettings['disabledBBC'])) Chris@76: $disabled_tags = explode(',', $modSettings['disabledBBC']); Chris@76: if (empty($modSettings['enableEmbeddedFlash'])) Chris@76: $disabled_tags[] = 'flash'; Chris@76: Chris@76: foreach ($disabled_tags as $tag) Chris@76: { Chris@76: if ($tag == 'list') Chris@76: $context['disabled_tags']['orderlist'] = true; Chris@76: Chris@76: $context['disabled_tags'][trim($tag)] = true; Chris@76: } Chris@76: Chris@76: // Switch the URLs back... now we're back to whatever the main sub template is. (like folder in PersonalMessage.) Chris@76: if (isset($settings['use_default_images']) && $settings['use_default_images'] == 'defaults' && isset($settings['default_template'])) Chris@76: { Chris@76: $settings['theme_url'] = $temp1; Chris@76: $settings['images_url'] = $temp2; Chris@76: $settings['theme_dir'] = $temp3; Chris@76: } Chris@76: } Chris@76: Chris@76: // Create a anti-bot verification control? Chris@76: function create_control_verification(&$verificationOptions, $do_test = false) Chris@76: { Chris@76: global $txt, $modSettings, $options, $smcFunc; Chris@76: global $context, $settings, $user_info, $sourcedir, $scripturl; Chris@76: Chris@76: // First verification means we need to set up some bits... Chris@76: if (empty($context['controls']['verification'])) Chris@76: { Chris@76: // The template Chris@76: loadTemplate('GenericControls'); Chris@76: Chris@76: // Some javascript ma'am? Chris@76: if (!empty($verificationOptions['override_visual']) || (!empty($modSettings['visual_verification_type']) && !isset($verificationOptions['override_visual']))) Chris@76: $context['html_headers'] .= ' Chris@76: '; Chris@76: Chris@76: $context['use_graphic_library'] = in_array('gd', get_loaded_extensions()); Chris@76: Chris@76: // Skip I, J, L, O, Q, S and Z. Chris@76: $context['standard_captcha_range'] = array_merge(range('A', 'H'), array('K', 'M', 'N', 'P', 'R'), range('T', 'Y')); Chris@76: } Chris@76: Chris@76: // Always have an ID. Chris@76: assert(isset($verificationOptions['id'])); Chris@76: $isNew = !isset($context['controls']['verification'][$verificationOptions['id']]); Chris@76: Chris@76: // Log this into our collection. Chris@76: if ($isNew) Chris@76: $context['controls']['verification'][$verificationOptions['id']] = array( Chris@76: 'id' => $verificationOptions['id'], Chris@76: 'show_visual' => !empty($verificationOptions['override_visual']) || (!empty($modSettings['visual_verification_type']) && !isset($verificationOptions['override_visual'])), Chris@76: 'number_questions' => isset($verificationOptions['override_qs']) ? $verificationOptions['override_qs'] : (!empty($modSettings['qa_verification_number']) ? $modSettings['qa_verification_number'] : 0), Chris@76: 'max_errors' => isset($verificationOptions['max_errors']) ? $verificationOptions['max_errors'] : 3, Chris@76: 'image_href' => $scripturl . '?action=verificationcode;vid=' . $verificationOptions['id'] . ';rand=' . md5(mt_rand()), Chris@76: 'text_value' => '', Chris@76: 'questions' => array(), Chris@76: ); Chris@76: $thisVerification = &$context['controls']['verification'][$verificationOptions['id']]; Chris@76: Chris@76: // Add javascript for the object. Chris@76: if ($context['controls']['verification'][$verificationOptions['id']]['show_visual'] && !WIRELESS) Chris@76: $context['insert_after_template'] .= ' Chris@76: '; Chris@76: Chris@76: // Is there actually going to be anything? Chris@76: if (empty($thisVerification['show_visual']) && empty($thisVerification['number_questions'])) Chris@76: return false; Chris@76: elseif (!$isNew && !$do_test) Chris@76: return true; Chris@76: Chris@76: // If we want questions do we have a cache of all the IDs? Chris@76: if (!empty($thisVerification['number_questions']) && empty($modSettings['question_id_cache'])) Chris@76: { Chris@76: if (($modSettings['question_id_cache'] = cache_get_data('verificationQuestionIds', 300)) == null) Chris@76: { Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_comment Chris@76: FROM {db_prefix}log_comments Chris@76: WHERE comment_type = {string:ver_test}', Chris@76: array( Chris@76: 'ver_test' => 'ver_test', Chris@76: ) Chris@76: ); Chris@76: $modSettings['question_id_cache'] = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: $modSettings['question_id_cache'][] = $row['id_comment']; Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: if (!empty($modSettings['cache_enable'])) Chris@76: cache_put_data('verificationQuestionIds', $modSettings['question_id_cache'], 300); Chris@76: } Chris@76: } Chris@76: Chris@76: if (!isset($_SESSION[$verificationOptions['id'] . '_vv'])) Chris@76: $_SESSION[$verificationOptions['id'] . '_vv'] = array(); Chris@76: Chris@76: // Do we need to refresh the verification? Chris@76: if (!$do_test && (!empty($_SESSION[$verificationOptions['id'] . '_vv']['did_pass']) || empty($_SESSION[$verificationOptions['id'] . '_vv']['count']) || $_SESSION[$verificationOptions['id'] . '_vv']['count'] > 3) && empty($verificationOptions['dont_refresh'])) Chris@76: $force_refresh = true; Chris@76: else Chris@76: $force_refresh = false; Chris@76: Chris@76: // This can also force a fresh, although unlikely. Chris@76: if (($thisVerification['show_visual'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['code'])) || ($thisVerification['number_questions'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['q']))) Chris@76: $force_refresh = true; Chris@76: Chris@76: $verification_errors = array(); Chris@76: Chris@76: // Start with any testing. Chris@76: if ($do_test) Chris@76: { Chris@76: // This cannot happen! Chris@76: if (!isset($_SESSION[$verificationOptions['id'] . '_vv']['count'])) Chris@76: fatal_lang_error('no_access', false); Chris@76: // ... nor this! Chris@76: if ($thisVerification['number_questions'] && (!isset($_SESSION[$verificationOptions['id'] . '_vv']['q']) || !isset($_REQUEST[$verificationOptions['id'] . '_vv']['q']))) Chris@76: fatal_lang_error('no_access', false); Chris@76: Chris@76: if ($thisVerification['show_visual'] && (empty($_REQUEST[$verificationOptions['id'] . '_vv']['code']) || empty($_SESSION[$verificationOptions['id'] . '_vv']['code']) || strtoupper($_REQUEST[$verificationOptions['id'] . '_vv']['code']) !== $_SESSION[$verificationOptions['id'] . '_vv']['code'])) Chris@76: $verification_errors[] = 'wrong_verification_code'; Chris@76: if ($thisVerification['number_questions']) Chris@76: { Chris@76: // Get the answers and see if they are all right! Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_comment, recipient_name AS answer Chris@76: FROM {db_prefix}log_comments Chris@76: WHERE comment_type = {string:ver_test} Chris@76: AND id_comment IN ({array_int:comment_ids})', Chris@76: array( Chris@76: 'ver_test' => 'ver_test', Chris@76: 'comment_ids' => $_SESSION[$verificationOptions['id'] . '_vv']['q'], Chris@76: ) Chris@76: ); Chris@76: $incorrectQuestions = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: if (empty($_REQUEST[$verificationOptions['id'] . '_vv']['q'][$row['id_comment']]) || trim($smcFunc['htmlspecialchars'](strtolower($_REQUEST[$verificationOptions['id'] . '_vv']['q'][$row['id_comment']]))) != strtolower($row['answer'])) Chris@76: $incorrectQuestions[] = $row['id_comment']; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: if (!empty($incorrectQuestions)) Chris@76: $verification_errors[] = 'wrong_verification_answer'; Chris@76: } Chris@76: } Chris@76: Chris@76: // Any errors means we refresh potentially. Chris@76: if (!empty($verification_errors)) Chris@76: { Chris@76: if (empty($_SESSION[$verificationOptions['id'] . '_vv']['errors'])) Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['errors'] = 0; Chris@76: // Too many errors? Chris@76: elseif ($_SESSION[$verificationOptions['id'] . '_vv']['errors'] > $thisVerification['max_errors']) Chris@76: $force_refresh = true; Chris@76: Chris@76: // Keep a track of these. Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['errors']++; Chris@76: } Chris@76: Chris@76: // Are we refreshing then? Chris@76: if ($force_refresh) Chris@76: { Chris@76: // Assume nothing went before. Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['count'] = 0; Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['errors'] = 0; Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['did_pass'] = false; Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['q'] = array(); Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['code'] = ''; Chris@76: Chris@76: // Generating a new image. Chris@76: if ($thisVerification['show_visual']) Chris@76: { Chris@76: // Are we overriding the range? Chris@76: $character_range = !empty($verificationOptions['override_range']) ? $verificationOptions['override_range'] : $context['standard_captcha_range']; Chris@76: Chris@76: for ($i = 0; $i < 6; $i++) Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['code'] .= $character_range[array_rand($character_range)]; Chris@76: } Chris@76: Chris@76: // Getting some new questions? Chris@76: if ($thisVerification['number_questions']) Chris@76: { Chris@76: // Pick some random IDs Chris@76: $questionIDs = array(); Chris@76: if ($thisVerification['number_questions'] == 1) Chris@76: $questionIDs[] = $modSettings['question_id_cache'][array_rand($modSettings['question_id_cache'], $thisVerification['number_questions'])]; Chris@76: else Chris@76: foreach (array_rand($modSettings['question_id_cache'], $thisVerification['number_questions']) as $index) Chris@76: $questionIDs[] = $modSettings['question_id_cache'][$index]; Chris@76: } Chris@76: } Chris@76: else Chris@76: { Chris@76: // Same questions as before. Chris@76: $questionIDs = !empty($_SESSION[$verificationOptions['id'] . '_vv']['q']) ? $_SESSION[$verificationOptions['id'] . '_vv']['q'] : array(); Chris@76: $thisVerification['text_value'] = !empty($_REQUEST[$verificationOptions['id'] . '_vv']['code']) ? $smcFunc['htmlspecialchars']($_REQUEST[$verificationOptions['id'] . '_vv']['code']) : ''; Chris@76: } Chris@76: Chris@76: // Have we got some questions to load? Chris@76: if (!empty($questionIDs)) Chris@76: { Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_comment, body AS question Chris@76: FROM {db_prefix}log_comments Chris@76: WHERE comment_type = {string:ver_test} Chris@76: AND id_comment IN ({array_int:comment_ids})', Chris@76: array( Chris@76: 'ver_test' => 'ver_test', Chris@76: 'comment_ids' => $questionIDs, Chris@76: ) Chris@76: ); Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['q'] = array(); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: $thisVerification['questions'][] = array( Chris@76: 'id' => $row['id_comment'], Chris@76: 'q' => parse_bbc($row['question']), Chris@76: 'is_error' => !empty($incorrectQuestions) && in_array($row['id_comment'], $incorrectQuestions), Chris@76: // Remember a previous submission? Chris@76: 'a' => isset($_REQUEST[$verificationOptions['id'] . '_vv'], $_REQUEST[$verificationOptions['id'] . '_vv']['q'], $_REQUEST[$verificationOptions['id'] . '_vv']['q'][$row['id_comment']]) ? $smcFunc['htmlspecialchars']($_REQUEST[$verificationOptions['id'] . '_vv']['q'][$row['id_comment']]) : '', Chris@76: ); Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['q'][] = $row['id_comment']; Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: } Chris@76: Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['count'] = empty($_SESSION[$verificationOptions['id'] . '_vv']['count']) ? 1 : $_SESSION[$verificationOptions['id'] . '_vv']['count'] + 1; Chris@76: Chris@76: // Return errors if we have them. Chris@76: if (!empty($verification_errors)) Chris@76: return $verification_errors; Chris@76: // If we had a test that one, make a note. Chris@76: elseif ($do_test) Chris@76: $_SESSION[$verificationOptions['id'] . '_vv']['did_pass'] = true; Chris@76: Chris@76: // Say that everything went well chaps. Chris@76: return true; Chris@76: } Chris@76: Chris@76: // This keeps track of all registered handling functions for auto suggest functionality and passes execution to them. Chris@76: function AutoSuggestHandler($checkRegistered = null) Chris@76: { Chris@76: global $context; Chris@76: Chris@76: // These are all registered types. Chris@76: $searchTypes = array( Chris@76: 'member' => 'Member', Chris@76: ); Chris@76: Chris@76: // If we're just checking the callback function is registered return true or false. Chris@76: if ($checkRegistered != null) Chris@76: return isset($searchTypes[$checkRegistered]) && function_exists('AutoSuggest_Search_' . $checkRegistered); Chris@76: Chris@76: checkSession('get'); Chris@76: loadTemplate('Xml'); Chris@76: Chris@76: // Any parameters? Chris@76: $context['search_param'] = isset($_REQUEST['search_param']) ? unserialize(base64_decode($_REQUEST['search_param'])) : array(); Chris@76: Chris@76: if (isset($_REQUEST['suggest_type'], $_REQUEST['search']) && isset($searchTypes[$_REQUEST['suggest_type']])) Chris@76: { Chris@76: $function = 'AutoSuggest_Search_' . $searchTypes[$_REQUEST['suggest_type']]; Chris@76: $context['sub_template'] = 'generic_xml'; Chris@76: $context['xml_data'] = $function(); Chris@76: } Chris@76: } Chris@76: Chris@76: // Search for a member - by real_name or member_name by default. Chris@76: function AutoSuggest_Search_Member() Chris@76: { Chris@76: global $user_info, $txt, $smcFunc, $context; Chris@76: Chris@76: $_REQUEST['search'] = trim($smcFunc['strtolower']($_REQUEST['search'])) . '*'; Chris@76: $_REQUEST['search'] = strtr($_REQUEST['search'], array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_', '&' => '&')); Chris@76: Chris@76: // Find the member. Chris@76: $request = $smcFunc['db_query']('', ' Chris@76: SELECT id_member, real_name Chris@76: FROM {db_prefix}members Chris@76: WHERE real_name LIKE {string:search}' . (!empty($context['search_param']['buddies']) ? ' Chris@76: AND id_member IN ({array_int:buddy_list})' : '') . ' Chris@76: AND is_activated IN (1, 11) Chris@76: LIMIT ' . ($smcFunc['strlen']($_REQUEST['search']) <= 2 ? '100' : '800'), Chris@76: array( Chris@76: 'buddy_list' => $user_info['buddies'], Chris@76: 'search' => $_REQUEST['search'], Chris@76: ) Chris@76: ); Chris@76: $xml_data = array( Chris@76: 'items' => array( Chris@76: 'identifier' => 'item', Chris@76: 'children' => array(), Chris@76: ), Chris@76: ); Chris@76: while ($row = $smcFunc['db_fetch_assoc']($request)) Chris@76: { Chris@76: $row['real_name'] = strtr($row['real_name'], array('&' => '&', '<' => '<', '>' => '>', '"' => '"')); Chris@76: Chris@76: $xml_data['items']['children'][] = array( Chris@76: 'attributes' => array( Chris@76: 'id' => $row['id_member'], Chris@76: ), Chris@76: 'value' => $row['real_name'], Chris@76: ); Chris@76: } Chris@76: $smcFunc['db_free_result']($request); Chris@76: Chris@76: return $xml_data; Chris@76: } Chris@76: Chris@76: ?>