annotate forum/Sources/Subs-Editor.php @ 85:6d7b61434be7 website

Add a copy of this here, just in case!
author Chris Cannam
date Mon, 20 Jan 2014 11:02:36 +0000
parents e3e11437ecea
children
rev   line source
Chris@76 1 <?php
Chris@76 2
Chris@76 3 /**
Chris@76 4 * Simple Machines Forum (SMF)
Chris@76 5 *
Chris@76 6 * @package SMF
Chris@76 7 * @author Simple Machines http://www.simplemachines.org
Chris@76 8 * @copyright 2011 Simple Machines
Chris@76 9 * @license http://www.simplemachines.org/about/smf/license.php BSD
Chris@76 10 *
Chris@76 11 * @version 2.0
Chris@76 12 */
Chris@76 13
Chris@76 14 if (!defined('SMF'))
Chris@76 15 die('Hacking attempt...');
Chris@76 16
Chris@76 17 /* This file contains those functions specific to the editing box and is
Chris@76 18 generally used for WYSIWYG type functionality. Doing all this is the
Chris@76 19 following:
Chris@76 20
Chris@76 21 void EditorMain()
Chris@76 22 // !!
Chris@76 23
Chris@76 24 void bbc_to_html()
Chris@76 25 // !!
Chris@76 26
Chris@76 27 void html_to_bbc()
Chris@76 28 // !!
Chris@76 29
Chris@76 30 void theme_postbox(string message)
Chris@76 31 - for compatibility - passes right through to the template_control_richedit function.
Chris@76 32
Chris@76 33 void create_control_richedit(&array editorOptions)
Chris@76 34 // !!
Chris@76 35
Chris@76 36 void create_control_verification(&array suggestOptions)
Chris@76 37 // !!
Chris@76 38
Chris@76 39 void fetchTagAttributes()
Chris@76 40 // !!
Chris@76 41
Chris@76 42 array getMessageIcons(int board_id)
Chris@76 43 - retrieves a list of message icons.
Chris@76 44 - based on the settings, the array will either contain a list of default
Chris@76 45 message icons or a list of custom message icons retrieved from the
Chris@76 46 database.
Chris@76 47 - the board_id is needed for the custom message icons (which can be set for
Chris@76 48 each board individually).
Chris@76 49
Chris@76 50 void AutoSuggestHandler(string checkRegistered = null)
Chris@76 51 // !!!
Chris@76 52
Chris@76 53 void AutoSuggest_Search_Member()
Chris@76 54 // !!!
Chris@76 55 */
Chris@76 56
Chris@76 57 // At the moment this is only used for returning WYSIWYG data...
Chris@76 58 function EditorMain()
Chris@76 59 {
Chris@76 60 global $context, $smcFunc;
Chris@76 61
Chris@76 62 checkSession('get');
Chris@76 63
Chris@76 64 if (!isset($_REQUEST['view']) || !isset($_REQUEST['message']))
Chris@76 65 fatal_lang_error('no_access', false);
Chris@76 66
Chris@76 67 $context['sub_template'] = 'sendbody';
Chris@76 68
Chris@76 69 $context['view'] = (int) $_REQUEST['view'];
Chris@76 70
Chris@76 71 // Return the right thing for the mode.
Chris@76 72 if ($context['view'])
Chris@76 73 {
Chris@76 74 $_REQUEST['message'] = strtr($_REQUEST['message'], array('#smcol#' => ';', '#smlt#' => '&lt;', '#smgt#' => '&gt;', '#smamp#' => '&amp;'));
Chris@76 75 $context['message'] = bbc_to_html($_REQUEST['message']);
Chris@76 76 }
Chris@76 77 else
Chris@76 78 {
Chris@76 79 $_REQUEST['message'] = un_htmlspecialchars($_REQUEST['message']);
Chris@76 80 $_REQUEST['message'] = strtr($_REQUEST['message'], array('#smcol#' => ';', '#smlt#' => '&lt;', '#smgt#' => '&gt;', '#smamp#' => '&amp;'));
Chris@76 81
Chris@76 82 $context['message'] = html_to_bbc($_REQUEST['message']);
Chris@76 83 }
Chris@76 84
Chris@76 85 $context['message'] = $smcFunc['htmlspecialchars']($context['message']);
Chris@76 86 }
Chris@76 87
Chris@76 88 // Convert only the BBC that can be edited in HTML mode for the editor.
Chris@76 89 function bbc_to_html($text)
Chris@76 90 {
Chris@76 91 global $modSettings, $smcFunc;
Chris@76 92
Chris@76 93 // Turn line breaks back into br's.
Chris@76 94 $text = strtr($text, array("\r" => '', "\n" => '<br />'));
Chris@76 95
Chris@76 96 // Prevent conversion of all bbcode inside these bbcodes.
Chris@76 97 // !!! Tie in with bbc permissions ?
Chris@76 98 foreach (array('code', 'php', 'nobbc') as $code)
Chris@76 99 {
Chris@76 100 if (strpos($text, '['. $code) !== false)
Chris@76 101 {
Chris@76 102 $parts = preg_split('~(\[/' . $code . '\]|\[' . $code . '(?:=[^\]]+)?\])~i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
Chris@76 103
Chris@76 104 // Only mess with stuff inside tags.
Chris@76 105 for ($i = 0, $n = count($parts); $i < $n; $i++)
Chris@76 106 {
Chris@76 107 // Value of 2 means we're inside the tag.
Chris@76 108 if ($i % 4 == 2)
Chris@76 109 $parts[$i] = strtr($parts[$i], array('[' => '&#91;', ']' => '&#93;', "'" => "'"));
Chris@76 110 }
Chris@76 111 // Put our humpty dumpty message back together again.
Chris@76 112 $text = implode('', $parts);
Chris@76 113 }
Chris@76 114 }
Chris@76 115
Chris@76 116 // What tags do we allow?
Chris@76 117 $allowed_tags = array('b', 'u', 'i', 's', 'hr', 'list', 'li', 'font', 'size', 'color', 'img', 'left', 'center', 'right', 'url', 'email', 'ftp', 'sub', 'sup');
Chris@76 118
Chris@76 119 $text = parse_bbc($text, true, '', $allowed_tags);
Chris@76 120
Chris@76 121 // Fix for having a line break then a thingy.
Chris@76 122 $text = strtr($text, array('<br /><div' => '<div', "\n" => '', "\r" => ''));
Chris@76 123
Chris@76 124 // Note that IE doesn't understand spans really - make them something "legacy"
Chris@76 125 $working_html = array(
Chris@76 126 '~<del>(.+?)</del>~i' => '<strike>$1</strike>',
Chris@76 127 '~<span\sclass="bbc_u">(.+?)</span>~i' => '<u>$1</u>',
Chris@76 128 '~<span\sstyle="color:\s*([#\d\w]+);" class="bbc_color">(.+?)</span>~i' => '<font color="$1">$2</font>',
Chris@76 129 '~<span\sstyle="font-family:\s*([#\d\w\s]+);" class="bbc_font">(.+?)</span>~i' => '<font face="$1">$2</font>',
Chris@76 130 '~<div\sstyle="text-align:\s*(left|right);">(.+?)</div>~i' => '<p align="$1">$2</p>',
Chris@76 131 );
Chris@76 132 $text = preg_replace(array_keys($working_html), array_values($working_html), $text);
Chris@76 133
Chris@76 134 // Parse unique ID's and disable javascript into the smileys - using the double space.
Chris@76 135 $i = 1;
Chris@76 136 $text = preg_replace('~(?:\s|&nbsp;)?<(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 137
Chris@76 138 return $text;
Chris@76 139 }
Chris@76 140
Chris@76 141 // The harder one - wysiwyg to BBC!
Chris@76 142 function html_to_bbc($text)
Chris@76 143 {
Chris@76 144 global $modSettings, $smcFunc, $sourcedir, $scripturl, $context;
Chris@76 145
Chris@76 146 // Replace newlines with spaces, as that's how browsers usually interpret them.
Chris@76 147 $text = preg_replace("~\s*[\r\n]+\s*~", ' ', $text);
Chris@76 148
Chris@76 149 // Though some of us love paragraphs, the parser will do better with breaks.
Chris@76 150 $text = preg_replace('~</p>\s*?<p~i', '</p><br /><p', $text);
Chris@76 151 $text = preg_replace('~</p>\s*(?!<)~i', '</p><br />', $text);
Chris@76 152
Chris@76 153 // Safari/webkit wraps lines in Wysiwyg in <div>'s.
Chris@76 154 if ($context['browser']['is_webkit'])
Chris@76 155 $text = preg_replace(array('~<div(?:\s(?:[^<>]*?))?' . '>~i', '</div>'), array('<br />', ''), $text);
Chris@76 156
Chris@76 157 // If there's a trailing break get rid of it - Firefox tends to add one.
Chris@76 158 $text = preg_replace('~<br\s?/?' . '>$~i', '', $text);
Chris@76 159
Chris@76 160 // Remove any formatting within code tags.
Chris@76 161 if (strpos($text, '[code') !== false)
Chris@76 162 {
Chris@76 163 $text = preg_replace('~<br\s?/?' . '>~i', '#smf_br_spec_grudge_cool!#', $text);
Chris@76 164 $parts = preg_split('~(\[/code\]|\[code(?:=[^\]]+)?\])~i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
Chris@76 165
Chris@76 166 // Only mess with stuff outside [code] tags.
Chris@76 167 for ($i = 0, $n = count($parts); $i < $n; $i++)
Chris@76 168 {
Chris@76 169 // Value of 2 means we're inside the tag.
Chris@76 170 if ($i % 4 == 2)
Chris@76 171 $parts[$i] = strip_tags($parts[$i]);
Chris@76 172 }
Chris@76 173
Chris@76 174 $text = strtr(implode('', $parts), array('#smf_br_spec_grudge_cool!#' => '<br />'));
Chris@76 175 }
Chris@76 176
Chris@76 177 // Remove scripts, style and comment blocks.
Chris@76 178 $text = preg_replace('~<script[^>]*[^/]?' . '>.*?</script>~i', '', $text);
Chris@76 179 $text = preg_replace('~<style[^>]*[^/]?' . '>.*?</style>~i', '', $text);
Chris@76 180 $text = preg_replace('~\\<\\!--.*?-->~i', '', $text);
Chris@76 181 $text = preg_replace('~\\<\\!\\[CDATA\\[.*?\\]\\]\\>~i', '', $text);
Chris@76 182
Chris@76 183 // Do the smileys ultra first!
Chris@76 184 preg_match_all('~<img\s+[^<>]*?id="*smiley_\d+_([^<>]+?)[\s"/>]\s*[^<>]*?/*>(?:\s)?~i', $text, $matches);
Chris@76 185 if (!empty($matches[0]))
Chris@76 186 {
Chris@76 187 // Easy if it's not custom.
Chris@76 188 if (empty($modSettings['smiley_enable']))
Chris@76 189 {
Chris@76 190 $smileysfrom = array('>:D', ':D', '::)', '>:(', ':)', ';)', ';D', ':(', ':o', '8)', ':P', '???', ':-[', ':-X', ':-*', ':\'(', ':-\\', '^-^', 'O0', 'C:-)', '0:)');
Chris@76 191 $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 192
Chris@76 193 foreach ($matches[1] as $k => $file)
Chris@76 194 {
Chris@76 195 $found = array_search($file, $smileysto);
Chris@76 196 // Note the weirdness here is to stop double spaces between smileys.
Chris@76 197 if ($found)
Chris@76 198 $matches[1][$k] = '-[]-smf_smily_start#|#' . htmlspecialchars($smileysfrom[$found]) . '-[]-smf_smily_end#|#';
Chris@76 199 else
Chris@76 200 $matches[1][$k] = '';
Chris@76 201 }
Chris@76 202 }
Chris@76 203 else
Chris@76 204 {
Chris@76 205 // Load all the smileys.
Chris@76 206 $names = array();
Chris@76 207 foreach ($matches[1] as $file)
Chris@76 208 $names[] = $file;
Chris@76 209 $names = array_unique($names);
Chris@76 210
Chris@76 211 if (!empty($names))
Chris@76 212 {
Chris@76 213 $request = $smcFunc['db_query']('', '
Chris@76 214 SELECT code, filename
Chris@76 215 FROM {db_prefix}smileys
Chris@76 216 WHERE filename IN ({array_string:smiley_filenames})',
Chris@76 217 array(
Chris@76 218 'smiley_filenames' => $names,
Chris@76 219 )
Chris@76 220 );
Chris@76 221 $mappings = array();
Chris@76 222 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 223 $mappings[$row['filename']] = htmlspecialchars($row['code']);
Chris@76 224 $smcFunc['db_free_result']($request);
Chris@76 225
Chris@76 226 foreach ($matches[1] as $k => $file)
Chris@76 227 if (isset($mappings[$file]))
Chris@76 228 $matches[1][$k] = '-[]-smf_smily_start#|#' . $mappings[$file] . '-[]-smf_smily_end#|#';
Chris@76 229 }
Chris@76 230 }
Chris@76 231
Chris@76 232 // Replace the tags!
Chris@76 233 $text = str_replace($matches[0], $matches[1], $text);
Chris@76 234
Chris@76 235 // Now sort out spaces
Chris@76 236 $text = str_replace(array('-[]-smf_smily_end#|#-[]-smf_smily_start#|#', '-[]-smf_smily_end#|#', '-[]-smf_smily_start#|#'), ' ', $text);
Chris@76 237 }
Chris@76 238
Chris@76 239 // Only try to buy more time if the client didn't quit.
Chris@76 240 if (connection_aborted() && $context['server']['is_apache'])
Chris@76 241 @apache_reset_timeout();
Chris@76 242
Chris@76 243 $parts = preg_split('~(<[A-Za-z]+\s*[^<>]*?style="?[^<>"]+"?[^<>]*?(?:/?)>|</[A-Za-z]+>)~', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
Chris@76 244 $replacement = '';
Chris@76 245 $stack = array();
Chris@76 246
Chris@76 247 foreach ($parts as $part)
Chris@76 248 {
Chris@76 249 if (preg_match('~(<([A-Za-z]+)\s*[^<>]*?)style="?([^<>"]+)"?([^<>]*?(/?)>)~', $part, $matches) === 1)
Chris@76 250 {
Chris@76 251 // If it's being closed instantly, we can't deal with it...yet.
Chris@76 252 if ($matches[5] === '/')
Chris@76 253 continue;
Chris@76 254 else
Chris@76 255 {
Chris@76 256 // Get an array of styles that apply to this element. (The strtr is there to combat HTML generated by Word.)
Chris@76 257 $styles = explode(';', strtr($matches[3], array('&quot;' => '')));
Chris@76 258 $curElement = $matches[2];
Chris@76 259 $precedingStyle = $matches[1];
Chris@76 260 $afterStyle = $matches[4];
Chris@76 261 $curCloseTags = '';
Chris@76 262 $extra_attr = '';
Chris@76 263
Chris@76 264 foreach ($styles as $type_value_pair)
Chris@76 265 {
Chris@76 266 // Remove spaces and convert uppercase letters.
Chris@76 267 $clean_type_value_pair = strtolower(strtr(trim($type_value_pair), '=', ':'));
Chris@76 268
Chris@76 269 // Something like 'font-weight: bold' is expected here.
Chris@76 270 if (strpos($clean_type_value_pair, ':') === false)
Chris@76 271 continue;
Chris@76 272
Chris@76 273 // Capture the elements of a single style item (e.g. 'font-weight' and 'bold').
Chris@76 274 list ($style_type, $style_value) = explode(':', $type_value_pair);
Chris@76 275
Chris@76 276 $style_value = trim($style_value);
Chris@76 277
Chris@76 278 switch (trim($style_type))
Chris@76 279 {
Chris@76 280 case 'font-weight':
Chris@76 281 if ($style_value === 'bold')
Chris@76 282 {
Chris@76 283 $curCloseTags .= '[/b]';
Chris@76 284 $replacement .= '[b]';
Chris@76 285 }
Chris@76 286 break;
Chris@76 287
Chris@76 288 case 'text-decoration':
Chris@76 289 if ($style_value == 'underline')
Chris@76 290 {
Chris@76 291 $curCloseTags .= '[/u]';
Chris@76 292 $replacement .= '[u]';
Chris@76 293 }
Chris@76 294 elseif ($style_value == 'line-through')
Chris@76 295 {
Chris@76 296 $curCloseTags .= '[/s]';
Chris@76 297 $replacement .= '[s]';
Chris@76 298 }
Chris@76 299 break;
Chris@76 300
Chris@76 301 case 'text-align':
Chris@76 302 if ($style_value == 'left')
Chris@76 303 {
Chris@76 304 $curCloseTags .= '[/left]';
Chris@76 305 $replacement .= '[left]';
Chris@76 306 }
Chris@76 307 elseif ($style_value == 'center')
Chris@76 308 {
Chris@76 309 $curCloseTags .= '[/center]';
Chris@76 310 $replacement .= '[center]';
Chris@76 311 }
Chris@76 312 elseif ($style_value == 'right')
Chris@76 313 {
Chris@76 314 $curCloseTags .= '[/right]';
Chris@76 315 $replacement .= '[right]';
Chris@76 316 }
Chris@76 317 break;
Chris@76 318
Chris@76 319 case 'font-style':
Chris@76 320 if ($style_value == 'italic')
Chris@76 321 {
Chris@76 322 $curCloseTags .= '[/i]';
Chris@76 323 $replacement .= '[i]';
Chris@76 324 }
Chris@76 325 break;
Chris@76 326
Chris@76 327 case 'color':
Chris@76 328 $curCloseTags .= '[/color]';
Chris@76 329 $replacement .= '[color=' . $style_value . ']';
Chris@76 330 break;
Chris@76 331
Chris@76 332 case 'font-size':
Chris@76 333 // Sometimes people put decimals where decimals should not be.
Chris@76 334 if (preg_match('~(\d)+\.\d+(p[xt])~i', $style_value, $dec_matches) === 1)
Chris@76 335 $style_value = $dec_matches[1] . $dec_matches[2];
Chris@76 336
Chris@76 337 $curCloseTags .= '[/size]';
Chris@76 338 $replacement .= '[size=' . $style_value . ']';
Chris@76 339 break;
Chris@76 340
Chris@76 341 case 'font-family':
Chris@76 342 // Only get the first freaking font if there's a list!
Chris@76 343 if (strpos($style_value, ',') !== false)
Chris@76 344 $style_value = substr($style_value, 0, strpos($style_value, ','));
Chris@76 345
Chris@76 346 $curCloseTags .= '[/font]';
Chris@76 347 $replacement .= '[font=' . strtr($style_value, array("'" => '')) . ']';
Chris@76 348 break;
Chris@76 349
Chris@76 350 // This is a hack for images with dimensions embedded.
Chris@76 351 case 'width':
Chris@76 352 case 'height':
Chris@76 353 if (preg_match('~[1-9]\d*~i', $style_value, $dimension) === 1)
Chris@76 354 $extra_attr .= ' ' . $style_type . '="' . $dimension[0] . '"';
Chris@76 355 break;
Chris@76 356
Chris@76 357 case 'list-style-type':
Chris@76 358 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 359 $extra_attr .= ' listtype="' . $listType[0] . '"';
Chris@76 360 break;
Chris@76 361 }
Chris@76 362 }
Chris@76 363
Chris@76 364 // Preserve some tags stripping the styling.
Chris@76 365 if (in_array($matches[2], array('a', 'font')))
Chris@76 366 {
Chris@76 367 $replacement .= $precedingStyle . $afterStyle;
Chris@76 368 $curCloseTags = '</' . $matches[2] . '>' . $curCloseTags;
Chris@76 369 }
Chris@76 370
Chris@76 371 // If there's something that still needs closing, push it to the stack.
Chris@76 372 if (!empty($curCloseTags))
Chris@76 373 array_push($stack, array(
Chris@76 374 'element' => strtolower($curElement),
Chris@76 375 'closeTags' => $curCloseTags
Chris@76 376 )
Chris@76 377 );
Chris@76 378 elseif (!empty($extra_attr))
Chris@76 379 $replacement .= $precedingStyle . $extra_attr . $afterStyle;
Chris@76 380 }
Chris@76 381 }
Chris@76 382
Chris@76 383 elseif (preg_match('~</([A-Za-z]+)>~', $part, $matches) === 1)
Chris@76 384 {
Chris@76 385 // Is this the element that we've been waiting for to be closed?
Chris@76 386 if (!empty($stack) && strtolower($matches[1]) === $stack[count($stack) - 1]['element'])
Chris@76 387 {
Chris@76 388 $byebyeTag = array_pop($stack);
Chris@76 389 $replacement .= $byebyeTag['closeTags'];
Chris@76 390 }
Chris@76 391
Chris@76 392 // Must've been something else.
Chris@76 393 else
Chris@76 394 $replacement .= $part;
Chris@76 395 }
Chris@76 396 // In all other cases, just add the part to the replacement.
Chris@76 397 else
Chris@76 398 $replacement .= $part;
Chris@76 399 }
Chris@76 400
Chris@76 401 // Now put back the replacement in the text.
Chris@76 402 $text = $replacement;
Chris@76 403
Chris@76 404 // We are not finished yet, request more time.
Chris@76 405 if (connection_aborted() && $context['server']['is_apache'])
Chris@76 406 @apache_reset_timeout();
Chris@76 407
Chris@76 408 // Let's pull out any legacy alignments.
Chris@76 409 while (preg_match('~<([A-Za-z]+)\s+[^<>]*?(align="*(left|center|right)"*)[^<>]*?(/?)>~i', $text, $matches) === 1)
Chris@76 410 {
Chris@76 411 // Find the position in the text of this tag over again.
Chris@76 412 $start_pos = strpos($text, $matches[0]);
Chris@76 413 if ($start_pos === false)
Chris@76 414 break;
Chris@76 415
Chris@76 416 // End tag?
Chris@76 417 if ($matches[4] != '/' && strpos($text, '</' . $matches[1] . '>', $start_pos) !== false)
Chris@76 418 {
Chris@76 419 $end_length = strlen('</' . $matches[1] . '>');
Chris@76 420 $end_pos = strpos($text, '</' . $matches[1] . '>', $start_pos);
Chris@76 421
Chris@76 422 // Remove the align from that tag so it's never checked again.
Chris@76 423 $tag = substr($text, $start_pos, strlen($matches[0]));
Chris@76 424 $content = substr($text, $start_pos + strlen($matches[0]), $end_pos - $start_pos - strlen($matches[0]));
Chris@76 425 $tag = str_replace($matches[2], '', $tag);
Chris@76 426
Chris@76 427 // Put the tags back into the body.
Chris@76 428 $text = substr($text, 0, $start_pos) . $tag . '[' . $matches[3] . ']' . $content . '[/' . $matches[3] . ']' . substr($text, $end_pos);
Chris@76 429 }
Chris@76 430 else
Chris@76 431 {
Chris@76 432 // Just get rid of this evil tag.
Chris@76 433 $text = substr($text, 0, $start_pos) . substr($text, $start_pos + strlen($matches[0]));
Chris@76 434 }
Chris@76 435 }
Chris@76 436
Chris@76 437 // Let's do some special stuff for fonts - cause we all love fonts.
Chris@76 438 while (preg_match('~<font\s+([^<>]*)>~i', $text, $matches) === 1)
Chris@76 439 {
Chris@76 440 // Find the position of this again.
Chris@76 441 $start_pos = strpos($text, $matches[0]);
Chris@76 442 $end_pos = false;
Chris@76 443 if ($start_pos === false)
Chris@76 444 break;
Chris@76 445
Chris@76 446 // This must have an end tag - and we must find the right one.
Chris@76 447 $lower_text = strtolower($text);
Chris@76 448
Chris@76 449 $start_pos_test = $start_pos + 4;
Chris@76 450 // How many starting tags must we find closing ones for first?
Chris@76 451 $start_font_tag_stack = 0;
Chris@76 452 while ($start_pos_test < strlen($text))
Chris@76 453 {
Chris@76 454 // Where is the next starting font?
Chris@76 455 $next_start_pos = strpos($lower_text, '<font', $start_pos_test);
Chris@76 456 $next_end_pos = strpos($lower_text, '</font>', $start_pos_test);
Chris@76 457
Chris@76 458 // Did we past another starting tag before an end one?
Chris@76 459 if ($next_start_pos !== false && $next_start_pos < $next_end_pos)
Chris@76 460 {
Chris@76 461 $start_font_tag_stack++;
Chris@76 462 $start_pos_test = $next_start_pos + 4;
Chris@76 463 }
Chris@76 464 // Otherwise we have an end tag but not the right one?
Chris@76 465 elseif ($start_font_tag_stack)
Chris@76 466 {
Chris@76 467 $start_font_tag_stack--;
Chris@76 468 $start_pos_test = $next_end_pos + 4;
Chris@76 469 }
Chris@76 470 // Otherwise we're there!
Chris@76 471 else
Chris@76 472 {
Chris@76 473 $end_pos = $next_end_pos;
Chris@76 474 break;
Chris@76 475 }
Chris@76 476 }
Chris@76 477 if ($end_pos === false)
Chris@76 478 break;
Chris@76 479
Chris@76 480 // Now work out what the attributes are.
Chris@76 481 $attribs = fetchTagAttributes($matches[1]);
Chris@76 482 $tags = array();
Chris@76 483 foreach ($attribs as $s => $v)
Chris@76 484 {
Chris@76 485 if ($s == 'size')
Chris@76 486 $tags[] = array('[size=' . (int) trim($v) . ']', '[/size]');
Chris@76 487 elseif ($s == 'face')
Chris@76 488 $tags[] = array('[font=' . trim(strtolower($v)) . ']', '[/font]');
Chris@76 489 elseif ($s == 'color')
Chris@76 490 $tags[] = array('[color=' . trim(strtolower($v)) . ']', '[/color]');
Chris@76 491 }
Chris@76 492
Chris@76 493 // As before add in our tags.
Chris@76 494 $before = $after = '';
Chris@76 495 foreach ($tags as $tag)
Chris@76 496 {
Chris@76 497 $before .= $tag[0];
Chris@76 498 if (isset($tag[1]))
Chris@76 499 $after = $tag[1] . $after;
Chris@76 500 }
Chris@76 501
Chris@76 502 // Remove the tag so it's never checked again.
Chris@76 503 $content = substr($text, $start_pos + strlen($matches[0]), $end_pos - $start_pos - strlen($matches[0]));
Chris@76 504
Chris@76 505 // Put the tags back into the body.
Chris@76 506 $text = substr($text, 0, $start_pos) . $before . $content . $after . substr($text, $end_pos + 7);
Chris@76 507 }
Chris@76 508
Chris@76 509 // Almost there, just a little more time.
Chris@76 510 if (connection_aborted() && $context['server']['is_apache'])
Chris@76 511 @apache_reset_timeout();
Chris@76 512
Chris@76 513 if (count($parts = preg_split('~<(/?)(li|ol|ul)([^>]*)>~i', $text, null, PREG_SPLIT_DELIM_CAPTURE)) > 1)
Chris@76 514 {
Chris@76 515 // A toggle that dermines whether we're directly under a <ol> or <ul>.
Chris@76 516 $inList = false;
Chris@76 517
Chris@76 518 // Keep track of the number of nested list levels.
Chris@76 519 $listDepth = 0;
Chris@76 520
Chris@76 521 // Map what we can expect from the HTML to what is supported by SMF.
Chris@76 522 $listTypeMapping = array(
Chris@76 523 '1' => 'decimal',
Chris@76 524 'A' => 'upper-alpha',
Chris@76 525 'a' => 'lower-alpha',
Chris@76 526 'I' => 'upper-roman',
Chris@76 527 'i' => 'lower-roman',
Chris@76 528 'disc' => 'disc',
Chris@76 529 'square' => 'square',
Chris@76 530 'circle' => 'circle',
Chris@76 531 );
Chris@76 532
Chris@76 533 // $i: text, $i + 1: '/', $i + 2: tag, $i + 3: tail.
Chris@76 534 for ($i = 0, $numParts = count($parts) - 1; $i < $numParts; $i += 4)
Chris@76 535 {
Chris@76 536 $tag = strtolower($parts[$i + 2]);
Chris@76 537 $isOpeningTag = $parts[$i + 1] === '';
Chris@76 538
Chris@76 539 if ($isOpeningTag)
Chris@76 540 {
Chris@76 541 switch ($tag)
Chris@76 542 {
Chris@76 543 case 'ol':
Chris@76 544 case 'ul':
Chris@76 545
Chris@76 546 // We have a problem, we're already in a list.
Chris@76 547 if ($inList)
Chris@76 548 {
Chris@76 549 // Inject a list opener, we'll deal with the ol/ul next loop.
Chris@76 550 array_splice($parts, $i, 0, array(
Chris@76 551 '',
Chris@76 552 '',
Chris@76 553 str_repeat("\t", $listDepth) . '[li]',
Chris@76 554 '',
Chris@76 555 ));
Chris@76 556 $numParts = count($parts) - 1;
Chris@76 557
Chris@76 558 // The inlist status changes a bit.
Chris@76 559 $inList = false;
Chris@76 560 }
Chris@76 561
Chris@76 562 // Just starting a new list.
Chris@76 563 else
Chris@76 564 {
Chris@76 565 $inList = true;
Chris@76 566
Chris@76 567 if ($tag === 'ol')
Chris@76 568 $listType = 'decimal';
Chris@76 569 elseif (preg_match('~type="?(' . implode('|', array_keys($listTypeMapping)) . ')"?~', $parts[$i + 3], $match) === 1)
Chris@76 570 $listType = $listTypeMapping[$match[1]];
Chris@76 571 else
Chris@76 572 $listType = null;
Chris@76 573
Chris@76 574 $listDepth++;
Chris@76 575
Chris@76 576 $parts[$i + 2] = '[list' . ($listType === null ? '' : ' type=' . $listType) . ']' . "\n";
Chris@76 577 $parts[$i + 3] = '';
Chris@76 578 }
Chris@76 579 break;
Chris@76 580
Chris@76 581 case 'li':
Chris@76 582
Chris@76 583 // This is how it should be: a list item inside the list.
Chris@76 584 if ($inList)
Chris@76 585 {
Chris@76 586 $parts[$i + 2] = str_repeat("\t", $listDepth) . '[li]';
Chris@76 587 $parts[$i + 3] = '';
Chris@76 588
Chris@76 589 // Within a list item, it's almost as if you're outside.
Chris@76 590 $inList = false;
Chris@76 591 }
Chris@76 592
Chris@76 593 // The li is no direct child of a list.
Chris@76 594 else
Chris@76 595 {
Chris@76 596 // We are apparently in a list item.
Chris@76 597 if ($listDepth > 0)
Chris@76 598 {
Chris@76 599 $parts[$i + 2] = '[/li]' . "\n" . str_repeat("\t", $listDepth) . '[li]';
Chris@76 600 $parts[$i + 3] = '';
Chris@76 601 }
Chris@76 602
Chris@76 603 // We're not even near a list.
Chris@76 604 else
Chris@76 605 {
Chris@76 606 // Quickly create a list with an item.
Chris@76 607 $listDepth++;
Chris@76 608
Chris@76 609 $parts[$i + 2] = '[list]' . "\n\t" . '[li]';
Chris@76 610 $parts[$i + 3] = '';
Chris@76 611 }
Chris@76 612 }
Chris@76 613
Chris@76 614 break;
Chris@76 615 }
Chris@76 616 }
Chris@76 617
Chris@76 618 // Handle all the closing tags.
Chris@76 619 else
Chris@76 620 {
Chris@76 621 switch ($tag)
Chris@76 622 {
Chris@76 623 case 'ol':
Chris@76 624 case 'ul':
Chris@76 625
Chris@76 626 // As we expected it, closing the list while we're in it.
Chris@76 627 if ($inList)
Chris@76 628 {
Chris@76 629 $inList = false;
Chris@76 630
Chris@76 631 $listDepth--;
Chris@76 632
Chris@76 633 $parts[$i + 1] = '';
Chris@76 634 $parts[$i + 2] = str_repeat("\t", $listDepth) . '[/list]';
Chris@76 635 $parts[$i + 3] = '';
Chris@76 636 }
Chris@76 637
Chris@76 638 else
Chris@76 639 {
Chris@76 640 // We're in a list item.
Chris@76 641 if ($listDepth > 0)
Chris@76 642 {
Chris@76 643 // Inject closure for this list item first.
Chris@76 644 // The content of $parts[$i] is left as is!
Chris@76 645 array_splice($parts, $i + 1, 0, array(
Chris@76 646 '', // $i + 1
Chris@76 647 '[/li]' . "\n", // $i + 2
Chris@76 648 '', // $i + 3
Chris@76 649 '', // $i + 4
Chris@76 650 ));
Chris@76 651 $numParts = count($parts) - 1;
Chris@76 652
Chris@76 653 // Now that we've closed the li, we're in list space.
Chris@76 654 $inList = true;
Chris@76 655 }
Chris@76 656
Chris@76 657 // We're not even in a list, ignore
Chris@76 658 else
Chris@76 659 {
Chris@76 660 $parts[$i + 1] = '';
Chris@76 661 $parts[$i + 2] = '';
Chris@76 662 $parts[$i + 3] = '';
Chris@76 663 }
Chris@76 664 }
Chris@76 665 break;
Chris@76 666
Chris@76 667 case 'li':
Chris@76 668
Chris@76 669 if ($inList)
Chris@76 670 {
Chris@76 671 // There's no use for a </li> after <ol> or <ul>, ignore.
Chris@76 672 $parts[$i + 1] = '';
Chris@76 673 $parts[$i + 2] = '';
Chris@76 674 $parts[$i + 3] = '';
Chris@76 675 }
Chris@76 676
Chris@76 677 else
Chris@76 678 {
Chris@76 679 // Remove the trailing breaks from the list item.
Chris@76 680 $parts[$i] = preg_replace('~\s*<br\s*' . '/?' . '>\s*$~', '', $parts[$i]);
Chris@76 681 $parts[$i + 1] = '';
Chris@76 682 $parts[$i + 2] = '[/li]' . "\n";
Chris@76 683 $parts[$i + 3] = '';
Chris@76 684
Chris@76 685 // And we're back in the [list] space.
Chris@76 686 $inList = true;
Chris@76 687 }
Chris@76 688
Chris@76 689 break;
Chris@76 690 }
Chris@76 691 }
Chris@76 692
Chris@76 693 // If we're in the [list] space, no content is allowed.
Chris@76 694 if ($inList && trim(preg_replace('~\s*<br\s*' . '/?' . '>\s*~', '', $parts[$i + 4])) !== '')
Chris@76 695 {
Chris@76 696 // Fix it by injecting an extra list item.
Chris@76 697 array_splice($parts, $i + 4, 0, array(
Chris@76 698 '', // No content.
Chris@76 699 '', // Opening tag.
Chris@76 700 'li', // It's a <li>.
Chris@76 701 '', // No tail.
Chris@76 702 ));
Chris@76 703 $numParts = count($parts) - 1;
Chris@76 704 }
Chris@76 705 }
Chris@76 706
Chris@76 707 $text = implode('', $parts);
Chris@76 708
Chris@76 709 if ($inList)
Chris@76 710 {
Chris@76 711 $listDepth--;
Chris@76 712 $text .= str_repeat("\t", $listDepth) . '[/list]';
Chris@76 713 }
Chris@76 714
Chris@76 715 for ($i = $listDepth; $i > 0; $i--)
Chris@76 716 $text .= '[/li]' . "\n" . str_repeat("\t", $i - 1) . '[/list]';
Chris@76 717
Chris@76 718 }
Chris@76 719
Chris@76 720 // I love my own image...
Chris@76 721 while (preg_match('~<img\s+([^<>]*)/*>~i', $text, $matches) === 1)
Chris@76 722 {
Chris@76 723 // Find the position of the image.
Chris@76 724 $start_pos = strpos($text, $matches[0]);
Chris@76 725 if ($start_pos === false)
Chris@76 726 break;
Chris@76 727 $end_pos = $start_pos + strlen($matches[0]);
Chris@76 728
Chris@76 729 $params = '';
Chris@76 730 $had_params = array();
Chris@76 731 $src = '';
Chris@76 732
Chris@76 733 $attrs = fetchTagAttributes($matches[1]);
Chris@76 734 foreach ($attrs as $attrib => $value)
Chris@76 735 {
Chris@76 736 if (in_array($attrib, array('width', 'height')))
Chris@76 737 $params .= ' ' . $attrib . '=' . (int) $value;
Chris@76 738 elseif ($attrib == 'alt' && trim($value) != '')
Chris@76 739 $params .= ' alt=' . trim($value);
Chris@76 740 elseif ($attrib == 'src')
Chris@76 741 $src = trim($value);
Chris@76 742 }
Chris@76 743
Chris@76 744 $tag = '';
Chris@76 745 if (!empty($src))
Chris@76 746 {
Chris@76 747 // Attempt to fix the path in case it's not present.
Chris@76 748 if (preg_match('~^https?://~i', $src) === 0 && is_array($parsedURL = parse_url($scripturl)) && isset($parsedURL['host']))
Chris@76 749 {
Chris@76 750 $baseURL = (isset($parsedURL['scheme']) ? $parsedURL['scheme'] : 'http') . '://' . $parsedURL['host'] . (empty($parsedURL['port']) ? '' : ':' . $parsedURL['port']);
Chris@76 751
Chris@76 752 if (substr($src, 0, 1) === '/')
Chris@76 753 $src = $baseURL . $src;
Chris@76 754 else
Chris@76 755 $src = $baseURL . (empty($parsedURL['path']) ? '/' : preg_replace('~/(?:index\\.php)?$~', '', $parsedURL['path'])) . '/' . $src;
Chris@76 756 }
Chris@76 757
Chris@76 758 $tag = '[img' . $params . ']' . $src . '[/img]';
Chris@76 759 }
Chris@76 760
Chris@76 761 // Replace the tag
Chris@76 762 $text = substr($text, 0, $start_pos) . $tag . substr($text, $end_pos);
Chris@76 763 }
Chris@76 764
Chris@76 765 // The final bits are the easy ones - tags which map to tags which map to tags - etc etc.
Chris@76 766 $tags = array(
Chris@76 767 '~<b(\s(.)*?)*?' . '>~i' => '[b]',
Chris@76 768 '~</b>~i' => '[/b]',
Chris@76 769 '~<i(\s(.)*?)*?' . '>~i' => '[i]',
Chris@76 770 '~</i>~i' => '[/i]',
Chris@76 771 '~<u(\s(.)*?)*?' . '>~i' => '[u]',
Chris@76 772 '~</u>~i' => '[/u]',
Chris@76 773 '~<strong(\s(.)*?)*?' . '>~i' => '[b]',
Chris@76 774 '~</strong>~i' => '[/b]',
Chris@76 775 '~<em(\s(.)*?)*?' . '>~i' => '[i]',
Chris@76 776 '~</em>~i' => '[/i]',
Chris@76 777 '~<s(\s(.)*?)*?' . '>~i' => "[s]",
Chris@76 778 '~</s>~i' => "[/s]",
Chris@76 779 '~<strike(\s(.)*?)*?' . '>~i' => '[s]',
Chris@76 780 '~</strike>~i' => '[/s]',
Chris@76 781 '~<del(\s(.)*?)*?' . '>~i' => '[s]',
Chris@76 782 '~</del>~i' => '[/s]',
Chris@76 783 '~<center(\s(.)*?)*?' . '>~i' => '[center]',
Chris@76 784 '~</center>~i' => '[/center]',
Chris@76 785 '~<pre(\s(.)*?)*?' . '>~i' => '[pre]',
Chris@76 786 '~</pre>~i' => '[/pre]',
Chris@76 787 '~<sub(\s(.)*?)*?' . '>~i' => '[sub]',
Chris@76 788 '~</sub>~i' => '[/sub]',
Chris@76 789 '~<sup(\s(.)*?)*?' . '>~i' => '[sup]',
Chris@76 790 '~</sup>~i' => '[/sup]',
Chris@76 791 '~<tt(\s(.)*?)*?' . '>~i' => '[tt]',
Chris@76 792 '~</tt>~i' => '[/tt]',
Chris@76 793 '~<table(\s(.)*?)*?' . '>~i' => '[table]',
Chris@76 794 '~</table>~i' => '[/table]',
Chris@76 795 '~<tr(\s(.)*?)*?' . '>~i' => '[tr]',
Chris@76 796 '~</tr>~i' => '[/tr]',
Chris@76 797 '~<(td|th)\s[^<>]*?colspan="?(\d{1,2})"?.*?' . '>~ie' => 'str_repeat(\'[td][/td]\', $2 - 1) . \'[td]\'',
Chris@76 798 '~<(td|th)(\s(.)*?)*?' . '>~i' => '[td]',
Chris@76 799 '~</(td|th)>~i' => '[/td]',
Chris@76 800 '~<br(?:\s[^<>]*?)?' . '>~i' => "\n",
Chris@76 801 '~<hr[^<>]*>(\n)?~i' => "[hr]\n$1",
Chris@76 802 '~(\n)?\\[hr\\]~i' => "\n[hr]",
Chris@76 803 '~^\n\\[hr\\]~i' => "[hr]",
Chris@76 804 '~<blockquote(\s(.)*?)*?' . '>~i' => "&lt;blockquote&gt;",
Chris@76 805 '~</blockquote>~i' => "&lt;/blockquote&gt;",
Chris@76 806 '~<ins(\s(.)*?)*?' . '>~i' => "&lt;ins&gt;",
Chris@76 807 '~</ins>~i' => "&lt;/ins&gt;",
Chris@76 808 );
Chris@76 809 $text = preg_replace(array_keys($tags), array_values($tags), $text);
Chris@76 810
Chris@76 811 // Please give us just a little more time.
Chris@76 812 if (connection_aborted() && $context['server']['is_apache'])
Chris@76 813 @apache_reset_timeout();
Chris@76 814
Chris@76 815 // What about URL's - the pain in the ass of the tag world.
Chris@76 816 while (preg_match('~<a\s+([^<>]*)>([^<>]*)</a>~i', $text, $matches) === 1)
Chris@76 817 {
Chris@76 818 // Find the position of the URL.
Chris@76 819 $start_pos = strpos($text, $matches[0]);
Chris@76 820 if ($start_pos === false)
Chris@76 821 break;
Chris@76 822 $end_pos = $start_pos + strlen($matches[0]);
Chris@76 823
Chris@76 824 $tag_type = 'url';
Chris@76 825 $href = '';
Chris@76 826
Chris@76 827 $attrs = fetchTagAttributes($matches[1]);
Chris@76 828 foreach ($attrs as $attrib => $value)
Chris@76 829 {
Chris@76 830 if ($attrib == 'href')
Chris@76 831 {
Chris@76 832 $href = trim($value);
Chris@76 833
Chris@76 834 // Are we dealing with an FTP link?
Chris@76 835 if (preg_match('~^ftps?://~', $href) === 1)
Chris@76 836 $tag_type = 'ftp';
Chris@76 837
Chris@76 838 // Or is this a link to an email address?
Chris@76 839 elseif (substr($href, 0, 7) == 'mailto:')
Chris@76 840 {
Chris@76 841 $tag_type = 'email';
Chris@76 842 $href = substr($href, 7);
Chris@76 843 }
Chris@76 844
Chris@76 845 // No http(s), so attempt to fix this potential relative URL.
Chris@76 846 elseif (preg_match('~^https?://~i', $href) === 0 && is_array($parsedURL = parse_url($scripturl)) && isset($parsedURL['host']))
Chris@76 847 {
Chris@76 848 $baseURL = (isset($parsedURL['scheme']) ? $parsedURL['scheme'] : 'http') . '://' . $parsedURL['host'] . (empty($parsedURL['port']) ? '' : ':' . $parsedURL['port']);
Chris@76 849
Chris@76 850 if (substr($href, 0, 1) === '/')
Chris@76 851 $href = $baseURL . $href;
Chris@76 852 else
Chris@76 853 $href = $baseURL . (empty($parsedURL['path']) ? '/' : preg_replace('~/(?:index\\.php)?$~', '', $parsedURL['path'])) . '/' . $href;
Chris@76 854 }
Chris@76 855 }
Chris@76 856
Chris@76 857 // External URL?
Chris@76 858 if ($attrib == 'target' && $tag_type == 'url')
Chris@76 859 {
Chris@76 860 if (trim($value) == '_blank')
Chris@76 861 $tag_type == 'iurl';
Chris@76 862 }
Chris@76 863 }
Chris@76 864
Chris@76 865 $tag = '';
Chris@76 866 if ($href != '')
Chris@76 867 {
Chris@76 868 if ($matches[2] == $href)
Chris@76 869 $tag = '[' . $tag_type . ']' . $href . '[/' . $tag_type . ']';
Chris@76 870 else
Chris@76 871 $tag = '[' . $tag_type . '=' . $href . ']' . $matches[2] . '[/' . $tag_type . ']';
Chris@76 872 }
Chris@76 873
Chris@76 874 // Replace the tag
Chris@76 875 $text = substr($text, 0, $start_pos) . $tag . substr($text, $end_pos);
Chris@76 876 }
Chris@76 877
Chris@76 878 $text = strip_tags($text);
Chris@76 879
Chris@76 880 // Some tags often end up as just dummy tags - remove those.
Chris@76 881 $text = preg_replace('~\[[bisu]\]\s*\[/[bisu]\]~', '', $text);
Chris@76 882
Chris@76 883 // Fix up entities.
Chris@76 884 $text = preg_replace('~&#38;~i', '&#38;#38;', $text);
Chris@76 885
Chris@76 886 $text = legalise_bbc($text);
Chris@76 887
Chris@76 888 return $text;
Chris@76 889 }
Chris@76 890
Chris@76 891 // Returns an array of attributes associated with a tag.
Chris@76 892 function fetchTagAttributes($text)
Chris@76 893 {
Chris@76 894 $attribs = array();
Chris@76 895 $key = $value = '';
Chris@76 896 $strpos = 0;
Chris@76 897 $tag_state = 0; // 0 = key, 1 = attribute with no string, 2 = attribute with string
Chris@76 898 for ($i = 0; $i < strlen($text); $i++)
Chris@76 899 {
Chris@76 900 // We're either moving from the key to the attribute or we're in a string and this is fine.
Chris@76 901 if ($text{$i} == '=')
Chris@76 902 {
Chris@76 903 if ($tag_state == 0)
Chris@76 904 $tag_state = 1;
Chris@76 905 elseif ($tag_state == 2)
Chris@76 906 $value .= '=';
Chris@76 907 }
Chris@76 908 // A space is either moving from an attribute back to a potential key or in a string is fine.
Chris@76 909 elseif ($text{$i} == ' ')
Chris@76 910 {
Chris@76 911 if ($tag_state == 2)
Chris@76 912 $value .= ' ';
Chris@76 913 elseif ($tag_state == 1)
Chris@76 914 {
Chris@76 915 $attribs[$key] = $value;
Chris@76 916 $key = $value = '';
Chris@76 917 $tag_state = 0;
Chris@76 918 }
Chris@76 919 }
Chris@76 920 // A quote?
Chris@76 921 elseif ($text{$i} == '"')
Chris@76 922 {
Chris@76 923 // Must be either going into or out of a string.
Chris@76 924 if ($tag_state == 1)
Chris@76 925 $tag_state = 2;
Chris@76 926 else
Chris@76 927 $tag_state = 1;
Chris@76 928 }
Chris@76 929 // Otherwise it's fine.
Chris@76 930 else
Chris@76 931 {
Chris@76 932 if ($tag_state == 0)
Chris@76 933 $key .= $text{$i};
Chris@76 934 else
Chris@76 935 $value .= $text{$i};
Chris@76 936 }
Chris@76 937 }
Chris@76 938
Chris@76 939 // Anything left?
Chris@76 940 if ($key != '' && $value != '')
Chris@76 941 $attribs[$key] = $value;
Chris@76 942
Chris@76 943 return $attribs;
Chris@76 944 }
Chris@76 945
Chris@76 946 function getMessageIcons($board_id)
Chris@76 947 {
Chris@76 948 global $modSettings, $context, $txt, $settings, $smcFunc;
Chris@76 949
Chris@76 950 if (empty($modSettings['messageIcons_enable']))
Chris@76 951 {
Chris@76 952 loadLanguage('Post');
Chris@76 953
Chris@76 954 $icons = array(
Chris@76 955 array('value' => 'xx', 'name' => $txt['standard']),
Chris@76 956 array('value' => 'thumbup', 'name' => $txt['thumbs_up']),
Chris@76 957 array('value' => 'thumbdown', 'name' => $txt['thumbs_down']),
Chris@76 958 array('value' => 'exclamation', 'name' => $txt['excamation_point']),
Chris@76 959 array('value' => 'question', 'name' => $txt['question_mark']),
Chris@76 960 array('value' => 'lamp', 'name' => $txt['lamp']),
Chris@76 961 array('value' => 'smiley', 'name' => $txt['icon_smiley']),
Chris@76 962 array('value' => 'angry', 'name' => $txt['icon_angry']),
Chris@76 963 array('value' => 'cheesy', 'name' => $txt['icon_cheesy']),
Chris@76 964 array('value' => 'grin', 'name' => $txt['icon_grin']),
Chris@76 965 array('value' => 'sad', 'name' => $txt['icon_sad']),
Chris@76 966 array('value' => 'wink', 'name' => $txt['icon_wink'])
Chris@76 967 );
Chris@76 968
Chris@76 969 foreach ($icons as $k => $dummy)
Chris@76 970 {
Chris@76 971 $icons[$k]['url'] = $settings['images_url'] . '/post/' . $dummy['value'] . '.gif';
Chris@76 972 $icons[$k]['is_last'] = false;
Chris@76 973 }
Chris@76 974 }
Chris@76 975 // Otherwise load the icons, and check we give the right image too...
Chris@76 976 else
Chris@76 977 {
Chris@76 978 if (($temp = cache_get_data('posting_icons-' . $board_id, 480)) == null)
Chris@76 979 {
Chris@76 980 $request = $smcFunc['db_query']('select_message_icons', '
Chris@76 981 SELECT title, filename
Chris@76 982 FROM {db_prefix}message_icons
Chris@76 983 WHERE id_board IN (0, {int:board_id})',
Chris@76 984 array(
Chris@76 985 'board_id' => $board_id,
Chris@76 986 )
Chris@76 987 );
Chris@76 988 $icon_data = array();
Chris@76 989 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 990 $icon_data[] = $row;
Chris@76 991 $smcFunc['db_free_result']($request);
Chris@76 992
Chris@76 993 cache_put_data('posting_icons-' . $board_id, $icon_data, 480);
Chris@76 994 }
Chris@76 995 else
Chris@76 996 $icon_data = $temp;
Chris@76 997
Chris@76 998 $icons = array();
Chris@76 999 foreach ($icon_data as $icon)
Chris@76 1000 {
Chris@76 1001 $icons[$icon['filename']] = array(
Chris@76 1002 'value' => $icon['filename'],
Chris@76 1003 'name' => $icon['title'],
Chris@76 1004 'url' => $settings[file_exists($settings['theme_dir'] . '/images/post/' . $icon['filename'] . '.gif') ? 'images_url' : 'default_images_url'] . '/post/' . $icon['filename'] . '.gif',
Chris@76 1005 'is_last' => false,
Chris@76 1006 );
Chris@76 1007 }
Chris@76 1008 }
Chris@76 1009
Chris@76 1010 return array_values($icons);
Chris@76 1011 }
Chris@76 1012
Chris@76 1013 // 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 1014 function legalise_bbc($text)
Chris@76 1015 {
Chris@76 1016 global $modSettings;
Chris@76 1017
Chris@76 1018 // Don't care about the texts that are too short.
Chris@76 1019 if (strlen($text) < 3)
Chris@76 1020 return $text;
Chris@76 1021
Chris@76 1022 // 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 1023 // This will keep the order of tags that are open.
Chris@76 1024 $current_tags = array();
Chris@76 1025
Chris@76 1026 // This will quickly let us see if the tag is active.
Chris@76 1027 $active_tags = array();
Chris@76 1028
Chris@76 1029 // A list of tags that's disabled by the admin.
Chris@76 1030 $disabled = empty($modSettings['disabledBBC']) ? array() : array_flip(explode(',', strtolower($modSettings['disabledBBC'])));
Chris@76 1031
Chris@76 1032 // Add flash if it's disabled as embedded tag.
Chris@76 1033 if (empty($modSettings['enableEmbeddedFlash']))
Chris@76 1034 $disabled['flash'] = true;
Chris@76 1035
Chris@76 1036 // Get a list of all the tags that are not disabled.
Chris@76 1037 $all_tags = parse_bbc(false);
Chris@76 1038 $valid_tags = array();
Chris@76 1039 $self_closing_tags = array();
Chris@76 1040 foreach ($all_tags as $tag)
Chris@76 1041 {
Chris@76 1042 if (!isset($disabled[$tag['tag']]))
Chris@76 1043 $valid_tags[$tag['tag']] = !empty($tag['block_level']);
Chris@76 1044 if (isset($tag['type']) && $tag['type'] == 'closed')
Chris@76 1045 $self_closing_tags[] = $tag['tag'];
Chris@76 1046 }
Chris@76 1047
Chris@76 1048 // Don't worry if we're in a code/nobbc.
Chris@76 1049 $in_code_nobbc = false;
Chris@76 1050
Chris@76 1051 // 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 1052 $align_tags = array('left', 'center', 'right', 'pre');
Chris@76 1053
Chris@76 1054 // Remove those align tags that are not valid.
Chris@76 1055 $align_tags = array_intersect($align_tags, array_keys($valid_tags));
Chris@76 1056
Chris@76 1057 // These keep track of where we are!
Chris@76 1058 if (!empty($align_tags) && count($matches = preg_split('~(\\[/?(?:' . implode('|', $align_tags) . ')\\])~', $text, -1, PREG_SPLIT_DELIM_CAPTURE)) > 1)
Chris@76 1059 {
Chris@76 1060 // The first one is never a tag.
Chris@76 1061 $isTag = false;
Chris@76 1062
Chris@76 1063 // By default we're not inside a tag too.
Chris@76 1064 $insideTag = null;
Chris@76 1065
Chris@76 1066 foreach ($matches as $i => $match)
Chris@76 1067 {
Chris@76 1068 // We're only interested in tags, not text.
Chris@76 1069 if ($isTag)
Chris@76 1070 {
Chris@76 1071 $isClosingTag = substr($match, 1, 1) === '/';
Chris@76 1072 $tagName = substr($match, $isClosingTag ? 2 : 1, -1);
Chris@76 1073
Chris@76 1074 // We're closing the exact same tag that we opened.
Chris@76 1075 if ($isClosingTag && $insideTag === $tagName)
Chris@76 1076 $insideTag = null;
Chris@76 1077
Chris@76 1078 // We're opening a tag and we're not yet inside one either
Chris@76 1079 elseif (!$isClosingTag && $insideTag === null)
Chris@76 1080 $insideTag = $tagName;
Chris@76 1081
Chris@76 1082 // In all other cases, this tag must be invalid
Chris@76 1083 else
Chris@76 1084 unset($matches[$i]);
Chris@76 1085 }
Chris@76 1086
Chris@76 1087 // The next one is gonna be the other one.
Chris@76 1088 $isTag = !$isTag;
Chris@76 1089 }
Chris@76 1090
Chris@76 1091 // We're still inside a tag and had no chance for closure?
Chris@76 1092 if ($insideTag !== null)
Chris@76 1093 $matches[] = '[/' . $insideTag . ']';
Chris@76 1094
Chris@76 1095 // And a complete text string again.
Chris@76 1096 $text = implode('', $matches);
Chris@76 1097 }
Chris@76 1098
Chris@76 1099 // Quickly remove any tags which are back to back.
Chris@76 1100 $backToBackPattern = '~\\[(' . implode('|', array_diff(array_keys($valid_tags), array('td', 'anchor'))) . ')[^<>\\[\\]]*\\]\s*\\[/\\1\\]~';
Chris@76 1101 $lastlen = 0;
Chris@76 1102 while (strlen($text) !== $lastlen)
Chris@76 1103 $lastlen = strlen($text = preg_replace($backToBackPattern, '', $text));
Chris@76 1104
Chris@76 1105 // Need to sort the tags my name length.
Chris@76 1106 uksort($valid_tags, 'sort_array_length');
Chris@76 1107
Chris@76 1108 // These inline tags can compete with each other regarding style.
Chris@76 1109 $competing_tags = array(
Chris@76 1110 'color',
Chris@76 1111 'size',
Chris@76 1112 );
Chris@76 1113
Chris@76 1114 // In case things changed above set these back to normal.
Chris@76 1115 $in_code_nobbc = false;
Chris@76 1116 $new_text_offset = 0;
Chris@76 1117
Chris@76 1118 // These keep track of where we are!
Chris@76 1119 if (count($parts = preg_split(sprintf('~(\\[)(/?)(%1$s)((?:[\\s=][^\\]\\[]*)?\\])~', implode('|', array_keys($valid_tags))), $text, -1, PREG_SPLIT_DELIM_CAPTURE)) > 1)
Chris@76 1120 {
Chris@76 1121 // Start with just text.
Chris@76 1122 $isTag = false;
Chris@76 1123
Chris@76 1124 // Start outside [nobbc] or [code] blocks.
Chris@76 1125 $inCode = false;
Chris@76 1126 $inNoBbc = false;
Chris@76 1127
Chris@76 1128 // A buffer containing all opened inline elements.
Chris@76 1129 $inlineElements = array();
Chris@76 1130
Chris@76 1131 // A buffer containing all opened block elements.
Chris@76 1132 $blockElements = array();
Chris@76 1133
Chris@76 1134 // A buffer containing the opened inline elements that might compete.
Chris@76 1135 $competingElements = array();
Chris@76 1136
Chris@76 1137 // $i: text, $i + 1: '[', $i + 2: '/', $i + 3: tag, $i + 4: tag tail.
Chris@76 1138 for ($i = 0, $n = count($parts) - 1; $i < $n; $i += 5)
Chris@76 1139 {
Chris@76 1140 $tag = $parts[$i + 3];
Chris@76 1141 $isOpeningTag = $parts[$i + 2] === '';
Chris@76 1142 $isClosingTag = $parts[$i + 2] === '/';
Chris@76 1143 $isBlockLevelTag = isset($valid_tags[$tag]) && $valid_tags[$tag] && !in_array($tag, $self_closing_tags);
Chris@76 1144 $isCompetingTag = in_array($tag, $competing_tags);
Chris@76 1145
Chris@76 1146 // Check if this might be one of those cleaned out tags.
Chris@76 1147 if ($tag === '')
Chris@76 1148 continue;
Chris@76 1149
Chris@76 1150 // Special case: inside [code] blocks any code is left untouched.
Chris@76 1151 elseif ($tag === 'code')
Chris@76 1152 {
Chris@76 1153 // We're inside a code block and closing it.
Chris@76 1154 if ($inCode && $isClosingTag)
Chris@76 1155 {
Chris@76 1156 $inCode = false;
Chris@76 1157
Chris@76 1158 // Reopen tags that were closed before the code block.
Chris@76 1159 if (!empty($inlineElements))
Chris@76 1160 $parts[$i + 4] .= '[' . implode('][', array_keys($inlineElements)) . ']';
Chris@76 1161 }
Chris@76 1162
Chris@76 1163 // We're outside a coding and nobbc block and opening it.
Chris@76 1164 elseif (!$inCode && !$inNoBbc && $isOpeningTag)
Chris@76 1165 {
Chris@76 1166 // If there are still inline elements left open, close them now.
Chris@76 1167 if (!empty($inlineElements))
Chris@76 1168 {
Chris@76 1169 $parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']';
Chris@76 1170 //$inlineElements = array();
Chris@76 1171 }
Chris@76 1172
Chris@76 1173 $inCode = true;
Chris@76 1174 }
Chris@76 1175
Chris@76 1176 // Nothing further to do.
Chris@76 1177 continue;
Chris@76 1178 }
Chris@76 1179
Chris@76 1180 // Special case: inside [nobbc] blocks any BBC is left untouched.
Chris@76 1181 elseif ($tag === 'nobbc')
Chris@76 1182 {
Chris@76 1183 // We're inside a nobbc block and closing it.
Chris@76 1184 if ($inNoBbc && $isClosingTag)
Chris@76 1185 {
Chris@76 1186 $inNoBbc = false;
Chris@76 1187
Chris@76 1188 // Some inline elements might've been closed that need reopening.
Chris@76 1189 if (!empty($inlineElements))
Chris@76 1190 $parts[$i + 4] .= '[' . implode('][', array_keys($inlineElements)) . ']';
Chris@76 1191 }
Chris@76 1192
Chris@76 1193 // We're outside a nobbc and coding block and opening it.
Chris@76 1194 elseif (!$inNoBbc && !$inCode && $isOpeningTag)
Chris@76 1195 {
Chris@76 1196 // Can't have inline elements still opened.
Chris@76 1197 if (!empty($inlineElements))
Chris@76 1198 {
Chris@76 1199 $parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']';
Chris@76 1200 //$inlineElements = array();
Chris@76 1201 }
Chris@76 1202
Chris@76 1203 $inNoBbc = true;
Chris@76 1204 }
Chris@76 1205
Chris@76 1206 continue;
Chris@76 1207 }
Chris@76 1208
Chris@76 1209 // So, we're inside one of the special blocks: ignore any tag.
Chris@76 1210 elseif ($inCode || $inNoBbc)
Chris@76 1211 continue;
Chris@76 1212
Chris@76 1213 // We're dealing with an opening tag.
Chris@76 1214 if ($isOpeningTag)
Chris@76 1215 {
Chris@76 1216 // Everyting inside the square brackets of the opening tag.
Chris@76 1217 $elementContent = $parts[$i + 3] . substr($parts[$i + 4], 0, -1);
Chris@76 1218
Chris@76 1219 // A block level opening tag.
Chris@76 1220 if ($isBlockLevelTag)
Chris@76 1221 {
Chris@76 1222 // Are there inline elements still open?
Chris@76 1223 if (!empty($inlineElements))
Chris@76 1224 {
Chris@76 1225 // Close all the inline tags, a block tag is coming...
Chris@76 1226 $parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']';
Chris@76 1227
Chris@76 1228 // Now open them again, we're inside the block tag now.
Chris@76 1229 $parts[$i + 5] = '[' . implode('][', array_keys($inlineElements)) . ']' . $parts[$i + 5];
Chris@76 1230 }
Chris@76 1231
Chris@76 1232 $blockElements[] = $tag;
Chris@76 1233 }
Chris@76 1234
Chris@76 1235 // Inline opening tag.
Chris@76 1236 elseif (!in_array($tag, $self_closing_tags))
Chris@76 1237 {
Chris@76 1238 // Can't have two opening elements with the same contents!
Chris@76 1239 if (isset($inlineElements[$elementContent]))
Chris@76 1240 {
Chris@76 1241 // Get rid of this tag.
Chris@76 1242 $parts[$i + 1] = $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = '';
Chris@76 1243
Chris@76 1244 // Now try to find the corresponding closing tag.
Chris@76 1245 $curLevel = 1;
Chris@76 1246 for ($j = $i + 5, $m = count($parts) - 1; $j < $m; $j += 5)
Chris@76 1247 {
Chris@76 1248 // Find the tags with the same tagname
Chris@76 1249 if ($parts[$j + 3] === $tag)
Chris@76 1250 {
Chris@76 1251 // If it's an opening tag, increase the level.
Chris@76 1252 if ($parts[$j + 2] === '')
Chris@76 1253 $curLevel++;
Chris@76 1254
Chris@76 1255 // A closing tag, decrease the level.
Chris@76 1256 else
Chris@76 1257 {
Chris@76 1258 $curLevel--;
Chris@76 1259
Chris@76 1260 // Gotcha! Clean out this closing tag gone rogue.
Chris@76 1261 if ($curLevel === 0)
Chris@76 1262 {
Chris@76 1263 $parts[$j + 1] = $parts[$j + 2] = $parts[$j + 3] = $parts[$j + 4] = '';
Chris@76 1264 break;
Chris@76 1265 }
Chris@76 1266 }
Chris@76 1267 }
Chris@76 1268 }
Chris@76 1269 }
Chris@76 1270
Chris@76 1271 // Otherwise, add this one to the list.
Chris@76 1272 else
Chris@76 1273 {
Chris@76 1274 if ($isCompetingTag)
Chris@76 1275 {
Chris@76 1276 if (!isset($competingElements[$tag]))
Chris@76 1277 $competingElements[$tag] = array();
Chris@76 1278
Chris@76 1279 $competingElements[$tag][] = $parts[$i + 4];
Chris@76 1280
Chris@76 1281 if (count($competingElements[$tag]) > 1)
Chris@76 1282 $parts[$i] .= '[/' . $tag . ']';
Chris@76 1283 }
Chris@76 1284
Chris@76 1285 $inlineElements[$elementContent] = $tag;
Chris@76 1286 }
Chris@76 1287 }
Chris@76 1288
Chris@76 1289 }
Chris@76 1290
Chris@76 1291 // Closing tag.
Chris@76 1292 else
Chris@76 1293 {
Chris@76 1294 // Closing the block tag.
Chris@76 1295 if ($isBlockLevelTag)
Chris@76 1296 {
Chris@76 1297 // Close the elements that should've been closed by closing this tag.
Chris@76 1298 if (!empty($blockElements))
Chris@76 1299 {
Chris@76 1300 $addClosingTags = array();
Chris@76 1301 while ($element = array_pop($blockElements))
Chris@76 1302 {
Chris@76 1303 if ($element === $tag)
Chris@76 1304 break;
Chris@76 1305
Chris@76 1306 // Still a block tag was open not equal to this tag.
Chris@76 1307 $addClosingTags[] = $element['type'];
Chris@76 1308 }
Chris@76 1309
Chris@76 1310 if (!empty($addClosingTags))
Chris@76 1311 $parts[$i + 1] = '[/' . implode('][/', array_reverse($addClosingTags)) . ']' . $parts[$i + 1];
Chris@76 1312
Chris@76 1313 // Apparently the closing tag was not found on the stack.
Chris@76 1314 if (!is_string($element) || $element !== $tag)
Chris@76 1315 {
Chris@76 1316 // Get rid of this particular closing tag, it was never opened.
Chris@76 1317 $parts[$i + 1] = substr($parts[$i + 1], 0, -1);
Chris@76 1318 $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = '';
Chris@76 1319 continue;
Chris@76 1320 }
Chris@76 1321 }
Chris@76 1322 else
Chris@76 1323 {
Chris@76 1324 // Get rid of this closing tag!
Chris@76 1325 $parts[$i + 1] = $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = '';
Chris@76 1326 continue;
Chris@76 1327 }
Chris@76 1328
Chris@76 1329 // Inline elements are still left opened?
Chris@76 1330 if (!empty($inlineElements))
Chris@76 1331 {
Chris@76 1332 // Close them first..
Chris@76 1333 $parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']';
Chris@76 1334
Chris@76 1335 // Then reopen them.
Chris@76 1336 $parts[$i + 5] = '[' . implode('][', array_keys($inlineElements)) . ']' . $parts[$i + 5];
Chris@76 1337 }
Chris@76 1338 }
Chris@76 1339 // Inline tag.
Chris@76 1340 else
Chris@76 1341 {
Chris@76 1342 // Are we expecting this tag to end?
Chris@76 1343 if (in_array($tag, $inlineElements))
Chris@76 1344 {
Chris@76 1345 foreach (array_reverse($inlineElements, true) as $tagContentToBeClosed => $tagToBeClosed)
Chris@76 1346 {
Chris@76 1347 // Closing it one way or the other.
Chris@76 1348 unset($inlineElements[$tagContentToBeClosed]);
Chris@76 1349
Chris@76 1350 // Was this the tag we were looking for?
Chris@76 1351 if ($tagToBeClosed === $tag)
Chris@76 1352 break;
Chris@76 1353
Chris@76 1354 // Nope, close it and look further!
Chris@76 1355 else
Chris@76 1356 $parts[$i] .= '[/' . $tagToBeClosed . ']';
Chris@76 1357 }
Chris@76 1358
Chris@76 1359 if ($isCompetingTag && !empty($competingElements[$tag]))
Chris@76 1360 {
Chris@76 1361 array_pop($competingElements[$tag]);
Chris@76 1362
Chris@76 1363 if (count($competingElements[$tag]) > 0)
Chris@76 1364 $parts[$i + 5] = '[' . $tag . $competingElements[$tag][count($competingElements[$tag]) - 1] . $parts[$i + 5];
Chris@76 1365 }
Chris@76 1366 }
Chris@76 1367
Chris@76 1368 // Unexpected closing tag, ex-ter-mi-nate.
Chris@76 1369 else
Chris@76 1370 $parts[$i + 1] = $parts[$i + 2] = $parts[$i + 3] = $parts[$i + 4] = '';
Chris@76 1371 }
Chris@76 1372 }
Chris@76 1373 }
Chris@76 1374
Chris@76 1375 // Close the code tags.
Chris@76 1376 if ($inCode)
Chris@76 1377 $parts[$i] .= '[/code]';
Chris@76 1378
Chris@76 1379 // The same for nobbc tags.
Chris@76 1380 elseif ($inNoBbc)
Chris@76 1381 $parts[$i] .= '[/nobbc]';
Chris@76 1382
Chris@76 1383 // Still inline tags left unclosed? Close them now, better late than never.
Chris@76 1384 elseif (!empty($inlineElements))
Chris@76 1385 $parts[$i] .= '[/' . implode('][/', array_reverse($inlineElements)) . ']';
Chris@76 1386
Chris@76 1387 // Now close the block elements.
Chris@76 1388 if (!empty($blockElements))
Chris@76 1389 $parts[$i] .= '[/' . implode('][/', array_reverse($blockElements)) . ']';
Chris@76 1390
Chris@76 1391 $text = implode('', $parts);
Chris@76 1392 }
Chris@76 1393
Chris@76 1394 // Final clean up of back to back tags.
Chris@76 1395 $lastlen = 0;
Chris@76 1396 while (strlen($text) !== $lastlen)
Chris@76 1397 $lastlen = strlen($text = preg_replace($backToBackPattern, '', $text));
Chris@76 1398
Chris@76 1399 return $text;
Chris@76 1400 }
Chris@76 1401
Chris@76 1402 // A help function for legalise_bbc for sorting arrays based on length.
Chris@76 1403 function sort_array_length($a, $b)
Chris@76 1404 {
Chris@76 1405 return strlen($a) < strlen($b) ? 1 : -1;
Chris@76 1406 }
Chris@76 1407
Chris@76 1408 // Compatibility function - used in 1.1 for showing a post box.
Chris@76 1409 function theme_postbox($msg)
Chris@76 1410 {
Chris@76 1411 global $context;
Chris@76 1412
Chris@76 1413 return template_control_richedit($context['post_box_name']);
Chris@76 1414 }
Chris@76 1415
Chris@76 1416 // Creates a box that can be used for richedit stuff like BBC, Smileys etc.
Chris@76 1417 function create_control_richedit($editorOptions)
Chris@76 1418 {
Chris@76 1419 global $txt, $modSettings, $options, $smcFunc;
Chris@76 1420 global $context, $settings, $user_info, $sourcedir, $scripturl;
Chris@76 1421
Chris@76 1422 // Load the Post language file... for the moment at least.
Chris@76 1423 loadLanguage('Post');
Chris@76 1424
Chris@76 1425 // Every control must have a ID!
Chris@76 1426 assert(isset($editorOptions['id']));
Chris@76 1427 assert(isset($editorOptions['value']));
Chris@76 1428
Chris@76 1429 // Is this the first richedit - if so we need to ensure some template stuff is initialised.
Chris@76 1430 if (empty($context['controls']['richedit']))
Chris@76 1431 {
Chris@76 1432 // Some general stuff.
Chris@76 1433 $settings['smileys_url'] = $modSettings['smileys_url'] . '/' . $user_info['smiley_set'];
Chris@76 1434
Chris@76 1435 // This really has some WYSIWYG stuff.
Chris@76 1436 loadTemplate('GenericControls', $context['browser']['is_ie'] ? 'editor_ie' : 'editor');
Chris@76 1437 $context['html_headers'] .= '
Chris@76 1438 <script type="text/javascript"><!-- // --><![CDATA[
Chris@76 1439 var smf_smileys_url = \'' . $settings['smileys_url'] . '\';
Chris@76 1440 var oEditorStrings= {
Chris@76 1441 wont_work: \'' . addcslashes($txt['rich_edit_wont_work'], "'") . '\',
Chris@76 1442 func_disabled: \'' . addcslashes($txt['rich_edit_function_disabled'], "'") . '\',
Chris@76 1443 prompt_text_email: \'' . addcslashes($txt['prompt_text_email'], "'") . '\',
Chris@76 1444 prompt_text_ftp: \'' . addcslashes($txt['prompt_text_ftp'], "'") . '\',
Chris@76 1445 prompt_text_url: \'' . addcslashes($txt['prompt_text_url'], "'") . '\',
Chris@76 1446 prompt_text_img: \'' . addcslashes($txt['prompt_text_img'], "'") . '\'
Chris@76 1447 }
Chris@76 1448 // ]]></script>
Chris@76 1449 <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/editor.js?fin20"></script>';
Chris@76 1450
Chris@76 1451 $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && function_exists('pspell_new');
Chris@76 1452 if ($context['show_spellchecking'])
Chris@76 1453 {
Chris@76 1454 $context['html_headers'] .= '
Chris@76 1455 <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/spellcheck.js"></script>';
Chris@76 1456
Chris@76 1457 // Some hidden information is needed in order to make the spell checking work.
Chris@76 1458 if (!isset($_REQUEST['xml']))
Chris@76 1459 $context['insert_after_template'] .= '
Chris@76 1460 <form name="spell_form" id="spell_form" method="post" accept-charset="' . $context['character_set'] . '" target="spellWindow" action="' . $scripturl . '?action=spellcheck">
Chris@76 1461 <input type="hidden" name="spellstring" value="" />
Chris@76 1462 </form>';
Chris@76 1463
Chris@76 1464 // Also make sure that spell check works with rich edit.
Chris@76 1465 $context['html_headers'] .= '
Chris@76 1466 <script type="text/javascript"><!-- // --><![CDATA[
Chris@76 1467 function spellCheckDone()
Chris@76 1468 {
Chris@76 1469 for (i = 0; i < smf_editorArray.length; i++)
Chris@76 1470 setTimeout("smf_editorArray[" + i + "].spellCheckEnd()", 150);
Chris@76 1471 }
Chris@76 1472 // ]]></script>';
Chris@76 1473 }
Chris@76 1474 }
Chris@76 1475
Chris@76 1476 // Start off the editor...
Chris@76 1477 $context['controls']['richedit'][$editorOptions['id']] = array(
Chris@76 1478 'id' => $editorOptions['id'],
Chris@76 1479 'value' => $editorOptions['value'],
Chris@76 1480 'rich_value' => bbc_to_html($editorOptions['value']),
Chris@76 1481 'rich_active' => empty($modSettings['disable_wysiwyg']) && (!empty($options['wysiwyg_default']) || !empty($editorOptions['force_rich']) || !empty($_REQUEST[$editorOptions['id'] . '_mode'])),
Chris@76 1482 'disable_smiley_box' => !empty($editorOptions['disable_smiley_box']),
Chris@76 1483 'columns' => isset($editorOptions['columns']) ? $editorOptions['columns'] : 60,
Chris@76 1484 'rows' => isset($editorOptions['rows']) ? $editorOptions['rows'] : 12,
Chris@76 1485 'width' => isset($editorOptions['width']) ? $editorOptions['width'] : '70%',
Chris@76 1486 'height' => isset($editorOptions['height']) ? $editorOptions['height'] : '150px',
Chris@76 1487 'form' => isset($editorOptions['form']) ? $editorOptions['form'] : 'postmodify',
Chris@76 1488 'bbc_level' => !empty($editorOptions['bbc_level']) ? $editorOptions['bbc_level'] : 'full',
Chris@76 1489 'preview_type' => isset($editorOptions['preview_type']) ? (int) $editorOptions['preview_type'] : 1,
Chris@76 1490 'labels' => !empty($editorOptions['labels']) ? $editorOptions['labels'] : array(),
Chris@76 1491 );
Chris@76 1492
Chris@76 1493 // Switch between default images and back... mostly in case you don't have an PersonalMessage template, but do have a Post template.
Chris@76 1494 if (isset($settings['use_default_images']) && $settings['use_default_images'] == 'defaults' && isset($settings['default_template']))
Chris@76 1495 {
Chris@76 1496 $temp1 = $settings['theme_url'];
Chris@76 1497 $settings['theme_url'] = $settings['default_theme_url'];
Chris@76 1498
Chris@76 1499 $temp2 = $settings['images_url'];
Chris@76 1500 $settings['images_url'] = $settings['default_images_url'];
Chris@76 1501
Chris@76 1502 $temp3 = $settings['theme_dir'];
Chris@76 1503 $settings['theme_dir'] = $settings['default_theme_dir'];
Chris@76 1504 }
Chris@76 1505
Chris@76 1506 if (empty($context['bbc_tags']))
Chris@76 1507 {
Chris@76 1508 // 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 1509 $context['bbc_tags'] = array();
Chris@76 1510 $context['bbc_tags'][] = array(
Chris@76 1511 array(
Chris@76 1512 'image' => 'bold',
Chris@76 1513 'code' => 'b',
Chris@76 1514 'before' => '[b]',
Chris@76 1515 'after' => '[/b]',
Chris@76 1516 'description' => $txt['bold'],
Chris@76 1517 ),
Chris@76 1518 array(
Chris@76 1519 'image' => 'italicize',
Chris@76 1520 'code' => 'i',
Chris@76 1521 'before' => '[i]',
Chris@76 1522 'after' => '[/i]',
Chris@76 1523 'description' => $txt['italic'],
Chris@76 1524 ),
Chris@76 1525 array(
Chris@76 1526 'image' => 'underline',
Chris@76 1527 'code' => 'u',
Chris@76 1528 'before' => '[u]',
Chris@76 1529 'after' => '[/u]',
Chris@76 1530 'description' => $txt['underline']
Chris@76 1531 ),
Chris@76 1532 array(
Chris@76 1533 'image' => 'strike',
Chris@76 1534 'code' => 's',
Chris@76 1535 'before' => '[s]',
Chris@76 1536 'after' => '[/s]',
Chris@76 1537 'description' => $txt['strike']
Chris@76 1538 ),
Chris@76 1539 array(),
Chris@76 1540 array(
Chris@76 1541 'image' => 'pre',
Chris@76 1542 'code' => 'pre',
Chris@76 1543 'before' => '[pre]',
Chris@76 1544 'after' => '[/pre]',
Chris@76 1545 'description' => $txt['preformatted']
Chris@76 1546 ),
Chris@76 1547 array(
Chris@76 1548 'image' => 'left',
Chris@76 1549 'code' => 'left',
Chris@76 1550 'before' => '[left]',
Chris@76 1551 'after' => '[/left]',
Chris@76 1552 'description' => $txt['left_align']
Chris@76 1553 ),
Chris@76 1554 array(
Chris@76 1555 'image' => 'center',
Chris@76 1556 'code' => 'center',
Chris@76 1557 'before' => '[center]',
Chris@76 1558 'after' => '[/center]',
Chris@76 1559 'description' => $txt['center']
Chris@76 1560 ),
Chris@76 1561 array(
Chris@76 1562 'image' => 'right',
Chris@76 1563 'code' => 'right',
Chris@76 1564 'before' => '[right]',
Chris@76 1565 'after' => '[/right]',
Chris@76 1566 'description' => $txt['right_align']
Chris@76 1567 ),
Chris@76 1568 );
Chris@76 1569 $context['bbc_tags'][] = array(
Chris@76 1570 array(
Chris@76 1571 'image' => 'flash',
Chris@76 1572 'code' => 'flash',
Chris@76 1573 'before' => '[flash=200,200]',
Chris@76 1574 'after' => '[/flash]',
Chris@76 1575 'description' => $txt['flash']
Chris@76 1576 ),
Chris@76 1577 array(
Chris@76 1578 'image' => 'img',
Chris@76 1579 'code' => 'img',
Chris@76 1580 'before' => '[img]',
Chris@76 1581 'after' => '[/img]',
Chris@76 1582 'description' => $txt['image']
Chris@76 1583 ),
Chris@76 1584 array(
Chris@76 1585 'image' => 'url',
Chris@76 1586 'code' => 'url',
Chris@76 1587 'before' => '[url]',
Chris@76 1588 'after' => '[/url]',
Chris@76 1589 'description' => $txt['hyperlink']
Chris@76 1590 ),
Chris@76 1591 array(
Chris@76 1592 'image' => 'email',
Chris@76 1593 'code' => 'email',
Chris@76 1594 'before' => '[email]',
Chris@76 1595 'after' => '[/email]',
Chris@76 1596 'description' => $txt['insert_email']
Chris@76 1597 ),
Chris@76 1598 array(
Chris@76 1599 'image' => 'ftp',
Chris@76 1600 'code' => 'ftp',
Chris@76 1601 'before' => '[ftp]',
Chris@76 1602 'after' => '[/ftp]',
Chris@76 1603 'description' => $txt['ftp']
Chris@76 1604 ),
Chris@76 1605 array(),
Chris@76 1606 array(
Chris@76 1607 'image' => 'glow',
Chris@76 1608 'code' => 'glow',
Chris@76 1609 'before' => '[glow=red,2,300]',
Chris@76 1610 'after' => '[/glow]',
Chris@76 1611 'description' => $txt['glow']
Chris@76 1612 ),
Chris@76 1613 array(
Chris@76 1614 'image' => 'shadow',
Chris@76 1615 'code' => 'shadow',
Chris@76 1616 'before' => '[shadow=red,left]',
Chris@76 1617 'after' => '[/shadow]',
Chris@76 1618 'description' => $txt['shadow']
Chris@76 1619 ),
Chris@76 1620 array(
Chris@76 1621 'image' => 'move',
Chris@76 1622 'code' => 'move',
Chris@76 1623 'before' => '[move]',
Chris@76 1624 'after' => '[/move]',
Chris@76 1625 'description' => $txt['marquee']
Chris@76 1626 ),
Chris@76 1627 array(),
Chris@76 1628 array(
Chris@76 1629 'image' => 'sup',
Chris@76 1630 'code' => 'sup',
Chris@76 1631 'before' => '[sup]',
Chris@76 1632 'after' => '[/sup]',
Chris@76 1633 'description' => $txt['superscript']
Chris@76 1634 ),
Chris@76 1635 array(
Chris@76 1636 'image' => 'sub',
Chris@76 1637 'code' => 'sub',
Chris@76 1638 'before' => '[sub]',
Chris@76 1639 'after' => '[/sub]',
Chris@76 1640 'description' => $txt['subscript']
Chris@76 1641 ),
Chris@76 1642 array(
Chris@76 1643 'image' => 'tele',
Chris@76 1644 'code' => 'tt',
Chris@76 1645 'before' => '[tt]',
Chris@76 1646 'after' => '[/tt]',
Chris@76 1647 'description' => $txt['teletype']
Chris@76 1648 ),
Chris@76 1649 array(),
Chris@76 1650 array(
Chris@76 1651 'image' => 'table',
Chris@76 1652 'code' => 'table',
Chris@76 1653 'before' => '[table]\n[tr]\n[td]',
Chris@76 1654 'after' => '[/td]\n[/tr]\n[/table]',
Chris@76 1655 'description' => $txt['table']
Chris@76 1656 ),
Chris@76 1657 array(
Chris@76 1658 'image' => 'code',
Chris@76 1659 'code' => 'code',
Chris@76 1660 'before' => '[code]',
Chris@76 1661 'after' => '[/code]',
Chris@76 1662 'description' => $txt['bbc_code']
Chris@76 1663 ),
Chris@76 1664 array(
Chris@76 1665 'image' => 'quote',
Chris@76 1666 'code' => 'quote',
Chris@76 1667 'before' => '[quote]',
Chris@76 1668 'after' => '[/quote]',
Chris@76 1669 'description' => $txt['bbc_quote']
Chris@76 1670 ),
Chris@76 1671 array(),
Chris@76 1672 array(
Chris@76 1673 'image' => 'list',
Chris@76 1674 'code' => 'list',
Chris@76 1675 'before' => '[list]\n[li]',
Chris@76 1676 'after' => '[/li]\n[li][/li]\n[/list]',
Chris@76 1677 'description' => $txt['list_unordered']
Chris@76 1678 ),
Chris@76 1679 array(
Chris@76 1680 'image' => 'orderlist',
Chris@76 1681 'code' => 'orderlist',
Chris@76 1682 'before' => '[list type=decimal]\n[li]',
Chris@76 1683 'after' => '[/li]\n[li][/li]\n[/list]',
Chris@76 1684 'description' => $txt['list_ordered']
Chris@76 1685 ),
Chris@76 1686 array(
Chris@76 1687 'image' => 'hr',
Chris@76 1688 'code' => 'hr',
Chris@76 1689 'before' => '[hr]',
Chris@76 1690 'description' => $txt['horizontal_rule']
Chris@76 1691 ),
Chris@76 1692 );
Chris@76 1693
Chris@76 1694 // Allow mods to modify BBC buttons.
Chris@76 1695 call_integration_hook('integrate_bbc_buttons', array(&$context['bbc_tags']));
Chris@76 1696
Chris@76 1697 // Show the toggle?
Chris@76 1698 if (empty($modSettings['disable_wysiwyg']))
Chris@76 1699 {
Chris@76 1700 $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array();
Chris@76 1701 $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array(
Chris@76 1702 'image' => 'unformat',
Chris@76 1703 'code' => 'unformat',
Chris@76 1704 'before' => '',
Chris@76 1705 'description' => $txt['unformat_text'],
Chris@76 1706 );
Chris@76 1707 $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array(
Chris@76 1708 'image' => 'toggle',
Chris@76 1709 'code' => 'toggle',
Chris@76 1710 'before' => '',
Chris@76 1711 'description' => $txt['toggle_view'],
Chris@76 1712 );
Chris@76 1713 }
Chris@76 1714
Chris@76 1715 foreach ($context['bbc_tags'] as $row => $tagRow)
Chris@76 1716 $context['bbc_tags'][$row][count($tagRow) - 1]['isLast'] = true;
Chris@76 1717 }
Chris@76 1718
Chris@76 1719 // Initialize smiley array... if not loaded before.
Chris@76 1720 if (empty($context['smileys']) && empty($editorOptions['disable_smiley_box']))
Chris@76 1721 {
Chris@76 1722 $context['smileys'] = array(
Chris@76 1723 'postform' => array(),
Chris@76 1724 'popup' => array(),
Chris@76 1725 );
Chris@76 1726
Chris@76 1727 // Load smileys - don't bother to run a query if we're not using the database's ones anyhow.
Chris@76 1728 if (empty($modSettings['smiley_enable']) && $user_info['smiley_set'] != 'none')
Chris@76 1729 $context['smileys']['postform'][] = array(
Chris@76 1730 'smileys' => array(
Chris@76 1731 array(
Chris@76 1732 'code' => ':)',
Chris@76 1733 'filename' => 'smiley.gif',
Chris@76 1734 'description' => $txt['icon_smiley'],
Chris@76 1735 ),
Chris@76 1736 array(
Chris@76 1737 'code' => ';)',
Chris@76 1738 'filename' => 'wink.gif',
Chris@76 1739 'description' => $txt['icon_wink'],
Chris@76 1740 ),
Chris@76 1741 array(
Chris@76 1742 'code' => ':D',
Chris@76 1743 'filename' => 'cheesy.gif',
Chris@76 1744 'description' => $txt['icon_cheesy'],
Chris@76 1745 ),
Chris@76 1746 array(
Chris@76 1747 'code' => ';D',
Chris@76 1748 'filename' => 'grin.gif',
Chris@76 1749 'description' => $txt['icon_grin']
Chris@76 1750 ),
Chris@76 1751 array(
Chris@76 1752 'code' => '>:(',
Chris@76 1753 'filename' => 'angry.gif',
Chris@76 1754 'description' => $txt['icon_angry'],
Chris@76 1755 ),
Chris@76 1756 array(
Chris@76 1757 'code' => ':(',
Chris@76 1758 'filename' => 'sad.gif',
Chris@76 1759 'description' => $txt['icon_sad'],
Chris@76 1760 ),
Chris@76 1761 array(
Chris@76 1762 'code' => ':o',
Chris@76 1763 'filename' => 'shocked.gif',
Chris@76 1764 'description' => $txt['icon_shocked'],
Chris@76 1765 ),
Chris@76 1766 array(
Chris@76 1767 'code' => '8)',
Chris@76 1768 'filename' => 'cool.gif',
Chris@76 1769 'description' => $txt['icon_cool'],
Chris@76 1770 ),
Chris@76 1771 array(
Chris@76 1772 'code' => '???',
Chris@76 1773 'filename' => 'huh.gif',
Chris@76 1774 'description' => $txt['icon_huh'],
Chris@76 1775 ),
Chris@76 1776 array(
Chris@76 1777 'code' => '::)',
Chris@76 1778 'filename' => 'rolleyes.gif',
Chris@76 1779 'description' => $txt['icon_rolleyes'],
Chris@76 1780 ),
Chris@76 1781 array(
Chris@76 1782 'code' => ':P',
Chris@76 1783 'filename' => 'tongue.gif',
Chris@76 1784 'description' => $txt['icon_tongue'],
Chris@76 1785 ),
Chris@76 1786 array(
Chris@76 1787 'code' => ':-[',
Chris@76 1788 'filename' => 'embarrassed.gif',
Chris@76 1789 'description' => $txt['icon_embarrassed'],
Chris@76 1790 ),
Chris@76 1791 array(
Chris@76 1792 'code' => ':-X',
Chris@76 1793 'filename' => 'lipsrsealed.gif',
Chris@76 1794 'description' => $txt['icon_lips'],
Chris@76 1795 ),
Chris@76 1796 array(
Chris@76 1797 'code' => ':-\\',
Chris@76 1798 'filename' => 'undecided.gif',
Chris@76 1799 'description' => $txt['icon_undecided'],
Chris@76 1800 ),
Chris@76 1801 array(
Chris@76 1802 'code' => ':-*',
Chris@76 1803 'filename' => 'kiss.gif',
Chris@76 1804 'description' => $txt['icon_kiss'],
Chris@76 1805 ),
Chris@76 1806 array(
Chris@76 1807 'code' => ':\'(',
Chris@76 1808 'filename' => 'cry.gif',
Chris@76 1809 'description' => $txt['icon_cry'],
Chris@76 1810 'isLast' => true,
Chris@76 1811 ),
Chris@76 1812 ),
Chris@76 1813 'isLast' => true,
Chris@76 1814 );
Chris@76 1815 elseif ($user_info['smiley_set'] != 'none')
Chris@76 1816 {
Chris@76 1817 if (($temp = cache_get_data('posting_smileys', 480)) == null)
Chris@76 1818 {
Chris@76 1819 $request = $smcFunc['db_query']('', '
Chris@76 1820 SELECT code, filename, description, smiley_row, hidden
Chris@76 1821 FROM {db_prefix}smileys
Chris@76 1822 WHERE hidden IN (0, 2)
Chris@76 1823 ORDER BY smiley_row, smiley_order',
Chris@76 1824 array(
Chris@76 1825 )
Chris@76 1826 );
Chris@76 1827 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 1828 {
Chris@76 1829 $row['filename'] = htmlspecialchars($row['filename']);
Chris@76 1830 $row['description'] = htmlspecialchars($row['description']);
Chris@76 1831
Chris@76 1832 $context['smileys'][empty($row['hidden']) ? 'postform' : 'popup'][$row['smiley_row']]['smileys'][] = $row;
Chris@76 1833 }
Chris@76 1834 $smcFunc['db_free_result']($request);
Chris@76 1835
Chris@76 1836 foreach ($context['smileys'] as $section => $smileyRows)
Chris@76 1837 {
Chris@76 1838 foreach ($smileyRows as $rowIndex => $smileys)
Chris@76 1839 $context['smileys'][$section][$rowIndex]['smileys'][count($smileys['smileys']) - 1]['isLast'] = true;
Chris@76 1840
Chris@76 1841 if (!empty($smileyRows))
Chris@76 1842 $context['smileys'][$section][count($smileyRows) - 1]['isLast'] = true;
Chris@76 1843 }
Chris@76 1844
Chris@76 1845 cache_put_data('posting_smileys', $context['smileys'], 480);
Chris@76 1846 }
Chris@76 1847 else
Chris@76 1848 $context['smileys'] = $temp;
Chris@76 1849 }
Chris@76 1850 }
Chris@76 1851
Chris@76 1852 // Set a flag so the sub template knows what to do...
Chris@76 1853 $context['show_bbc'] = !empty($modSettings['enableBBC']) && !empty($settings['show_bbc']);
Chris@76 1854
Chris@76 1855 // Generate a list of buttons that shouldn't be shown - this should be the fastest way to do this.
Chris@76 1856 $disabled_tags = array();
Chris@76 1857 if (!empty($modSettings['disabledBBC']))
Chris@76 1858 $disabled_tags = explode(',', $modSettings['disabledBBC']);
Chris@76 1859 if (empty($modSettings['enableEmbeddedFlash']))
Chris@76 1860 $disabled_tags[] = 'flash';
Chris@76 1861
Chris@76 1862 foreach ($disabled_tags as $tag)
Chris@76 1863 {
Chris@76 1864 if ($tag == 'list')
Chris@76 1865 $context['disabled_tags']['orderlist'] = true;
Chris@76 1866
Chris@76 1867 $context['disabled_tags'][trim($tag)] = true;
Chris@76 1868 }
Chris@76 1869
Chris@76 1870 // Switch the URLs back... now we're back to whatever the main sub template is. (like folder in PersonalMessage.)
Chris@76 1871 if (isset($settings['use_default_images']) && $settings['use_default_images'] == 'defaults' && isset($settings['default_template']))
Chris@76 1872 {
Chris@76 1873 $settings['theme_url'] = $temp1;
Chris@76 1874 $settings['images_url'] = $temp2;
Chris@76 1875 $settings['theme_dir'] = $temp3;
Chris@76 1876 }
Chris@76 1877 }
Chris@76 1878
Chris@76 1879 // Create a anti-bot verification control?
Chris@76 1880 function create_control_verification(&$verificationOptions, $do_test = false)
Chris@76 1881 {
Chris@76 1882 global $txt, $modSettings, $options, $smcFunc;
Chris@76 1883 global $context, $settings, $user_info, $sourcedir, $scripturl;
Chris@76 1884
Chris@76 1885 // First verification means we need to set up some bits...
Chris@76 1886 if (empty($context['controls']['verification']))
Chris@76 1887 {
Chris@76 1888 // The template
Chris@76 1889 loadTemplate('GenericControls');
Chris@76 1890
Chris@76 1891 // Some javascript ma'am?
Chris@76 1892 if (!empty($verificationOptions['override_visual']) || (!empty($modSettings['visual_verification_type']) && !isset($verificationOptions['override_visual'])))
Chris@76 1893 $context['html_headers'] .= '
Chris@76 1894 <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/captcha.js"></script>';
Chris@76 1895
Chris@76 1896 $context['use_graphic_library'] = in_array('gd', get_loaded_extensions());
Chris@76 1897
Chris@76 1898 // Skip I, J, L, O, Q, S and Z.
Chris@76 1899 $context['standard_captcha_range'] = array_merge(range('A', 'H'), array('K', 'M', 'N', 'P', 'R'), range('T', 'Y'));
Chris@76 1900 }
Chris@76 1901
Chris@76 1902 // Always have an ID.
Chris@76 1903 assert(isset($verificationOptions['id']));
Chris@76 1904 $isNew = !isset($context['controls']['verification'][$verificationOptions['id']]);
Chris@76 1905
Chris@76 1906 // Log this into our collection.
Chris@76 1907 if ($isNew)
Chris@76 1908 $context['controls']['verification'][$verificationOptions['id']] = array(
Chris@76 1909 'id' => $verificationOptions['id'],
Chris@76 1910 'show_visual' => !empty($verificationOptions['override_visual']) || (!empty($modSettings['visual_verification_type']) && !isset($verificationOptions['override_visual'])),
Chris@76 1911 'number_questions' => isset($verificationOptions['override_qs']) ? $verificationOptions['override_qs'] : (!empty($modSettings['qa_verification_number']) ? $modSettings['qa_verification_number'] : 0),
Chris@76 1912 'max_errors' => isset($verificationOptions['max_errors']) ? $verificationOptions['max_errors'] : 3,
Chris@76 1913 'image_href' => $scripturl . '?action=verificationcode;vid=' . $verificationOptions['id'] . ';rand=' . md5(mt_rand()),
Chris@76 1914 'text_value' => '',
Chris@76 1915 'questions' => array(),
Chris@76 1916 );
Chris@76 1917 $thisVerification = &$context['controls']['verification'][$verificationOptions['id']];
Chris@76 1918
Chris@76 1919 // Add javascript for the object.
Chris@76 1920 if ($context['controls']['verification'][$verificationOptions['id']]['show_visual'] && !WIRELESS)
Chris@76 1921 $context['insert_after_template'] .= '
Chris@76 1922 <script type="text/javascript"><!-- // --><![CDATA[
Chris@76 1923 var verification' . $verificationOptions['id'] . 'Handle = new smfCaptcha("' . $thisVerification['image_href'] . '", "' . $verificationOptions['id'] . '", ' . ($context['use_graphic_library'] ? 1 : 0) . ');
Chris@76 1924 // ]]></script>';
Chris@76 1925
Chris@76 1926 // Is there actually going to be anything?
Chris@76 1927 if (empty($thisVerification['show_visual']) && empty($thisVerification['number_questions']))
Chris@76 1928 return false;
Chris@76 1929 elseif (!$isNew && !$do_test)
Chris@76 1930 return true;
Chris@76 1931
Chris@76 1932 // If we want questions do we have a cache of all the IDs?
Chris@76 1933 if (!empty($thisVerification['number_questions']) && empty($modSettings['question_id_cache']))
Chris@76 1934 {
Chris@76 1935 if (($modSettings['question_id_cache'] = cache_get_data('verificationQuestionIds', 300)) == null)
Chris@76 1936 {
Chris@76 1937 $request = $smcFunc['db_query']('', '
Chris@76 1938 SELECT id_comment
Chris@76 1939 FROM {db_prefix}log_comments
Chris@76 1940 WHERE comment_type = {string:ver_test}',
Chris@76 1941 array(
Chris@76 1942 'ver_test' => 'ver_test',
Chris@76 1943 )
Chris@76 1944 );
Chris@76 1945 $modSettings['question_id_cache'] = array();
Chris@76 1946 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 1947 $modSettings['question_id_cache'][] = $row['id_comment'];
Chris@76 1948 $smcFunc['db_free_result']($request);
Chris@76 1949
Chris@76 1950 if (!empty($modSettings['cache_enable']))
Chris@76 1951 cache_put_data('verificationQuestionIds', $modSettings['question_id_cache'], 300);
Chris@76 1952 }
Chris@76 1953 }
Chris@76 1954
Chris@76 1955 if (!isset($_SESSION[$verificationOptions['id'] . '_vv']))
Chris@76 1956 $_SESSION[$verificationOptions['id'] . '_vv'] = array();
Chris@76 1957
Chris@76 1958 // Do we need to refresh the verification?
Chris@76 1959 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 1960 $force_refresh = true;
Chris@76 1961 else
Chris@76 1962 $force_refresh = false;
Chris@76 1963
Chris@76 1964 // This can also force a fresh, although unlikely.
Chris@76 1965 if (($thisVerification['show_visual'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['code'])) || ($thisVerification['number_questions'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['q'])))
Chris@76 1966 $force_refresh = true;
Chris@76 1967
Chris@76 1968 $verification_errors = array();
Chris@76 1969
Chris@76 1970 // Start with any testing.
Chris@76 1971 if ($do_test)
Chris@76 1972 {
Chris@76 1973 // This cannot happen!
Chris@76 1974 if (!isset($_SESSION[$verificationOptions['id'] . '_vv']['count']))
Chris@76 1975 fatal_lang_error('no_access', false);
Chris@76 1976 // ... nor this!
Chris@76 1977 if ($thisVerification['number_questions'] && (!isset($_SESSION[$verificationOptions['id'] . '_vv']['q']) || !isset($_REQUEST[$verificationOptions['id'] . '_vv']['q'])))
Chris@76 1978 fatal_lang_error('no_access', false);
Chris@76 1979
Chris@76 1980 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 1981 $verification_errors[] = 'wrong_verification_code';
Chris@76 1982 if ($thisVerification['number_questions'])
Chris@76 1983 {
Chris@76 1984 // Get the answers and see if they are all right!
Chris@76 1985 $request = $smcFunc['db_query']('', '
Chris@76 1986 SELECT id_comment, recipient_name AS answer
Chris@76 1987 FROM {db_prefix}log_comments
Chris@76 1988 WHERE comment_type = {string:ver_test}
Chris@76 1989 AND id_comment IN ({array_int:comment_ids})',
Chris@76 1990 array(
Chris@76 1991 'ver_test' => 'ver_test',
Chris@76 1992 'comment_ids' => $_SESSION[$verificationOptions['id'] . '_vv']['q'],
Chris@76 1993 )
Chris@76 1994 );
Chris@76 1995 $incorrectQuestions = array();
Chris@76 1996 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 1997 {
Chris@76 1998 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 1999 $incorrectQuestions[] = $row['id_comment'];
Chris@76 2000 }
Chris@76 2001 $smcFunc['db_free_result']($request);
Chris@76 2002
Chris@76 2003 if (!empty($incorrectQuestions))
Chris@76 2004 $verification_errors[] = 'wrong_verification_answer';
Chris@76 2005 }
Chris@76 2006 }
Chris@76 2007
Chris@76 2008 // Any errors means we refresh potentially.
Chris@76 2009 if (!empty($verification_errors))
Chris@76 2010 {
Chris@76 2011 if (empty($_SESSION[$verificationOptions['id'] . '_vv']['errors']))
Chris@76 2012 $_SESSION[$verificationOptions['id'] . '_vv']['errors'] = 0;
Chris@76 2013 // Too many errors?
Chris@76 2014 elseif ($_SESSION[$verificationOptions['id'] . '_vv']['errors'] > $thisVerification['max_errors'])
Chris@76 2015 $force_refresh = true;
Chris@76 2016
Chris@76 2017 // Keep a track of these.
Chris@76 2018 $_SESSION[$verificationOptions['id'] . '_vv']['errors']++;
Chris@76 2019 }
Chris@76 2020
Chris@76 2021 // Are we refreshing then?
Chris@76 2022 if ($force_refresh)
Chris@76 2023 {
Chris@76 2024 // Assume nothing went before.
Chris@76 2025 $_SESSION[$verificationOptions['id'] . '_vv']['count'] = 0;
Chris@76 2026 $_SESSION[$verificationOptions['id'] . '_vv']['errors'] = 0;
Chris@76 2027 $_SESSION[$verificationOptions['id'] . '_vv']['did_pass'] = false;
Chris@76 2028 $_SESSION[$verificationOptions['id'] . '_vv']['q'] = array();
Chris@76 2029 $_SESSION[$verificationOptions['id'] . '_vv']['code'] = '';
Chris@76 2030
Chris@76 2031 // Generating a new image.
Chris@76 2032 if ($thisVerification['show_visual'])
Chris@76 2033 {
Chris@76 2034 // Are we overriding the range?
Chris@76 2035 $character_range = !empty($verificationOptions['override_range']) ? $verificationOptions['override_range'] : $context['standard_captcha_range'];
Chris@76 2036
Chris@76 2037 for ($i = 0; $i < 6; $i++)
Chris@76 2038 $_SESSION[$verificationOptions['id'] . '_vv']['code'] .= $character_range[array_rand($character_range)];
Chris@76 2039 }
Chris@76 2040
Chris@76 2041 // Getting some new questions?
Chris@76 2042 if ($thisVerification['number_questions'])
Chris@76 2043 {
Chris@76 2044 // Pick some random IDs
Chris@76 2045 $questionIDs = array();
Chris@76 2046 if ($thisVerification['number_questions'] == 1)
Chris@76 2047 $questionIDs[] = $modSettings['question_id_cache'][array_rand($modSettings['question_id_cache'], $thisVerification['number_questions'])];
Chris@76 2048 else
Chris@76 2049 foreach (array_rand($modSettings['question_id_cache'], $thisVerification['number_questions']) as $index)
Chris@76 2050 $questionIDs[] = $modSettings['question_id_cache'][$index];
Chris@76 2051 }
Chris@76 2052 }
Chris@76 2053 else
Chris@76 2054 {
Chris@76 2055 // Same questions as before.
Chris@76 2056 $questionIDs = !empty($_SESSION[$verificationOptions['id'] . '_vv']['q']) ? $_SESSION[$verificationOptions['id'] . '_vv']['q'] : array();
Chris@76 2057 $thisVerification['text_value'] = !empty($_REQUEST[$verificationOptions['id'] . '_vv']['code']) ? $smcFunc['htmlspecialchars']($_REQUEST[$verificationOptions['id'] . '_vv']['code']) : '';
Chris@76 2058 }
Chris@76 2059
Chris@76 2060 // Have we got some questions to load?
Chris@76 2061 if (!empty($questionIDs))
Chris@76 2062 {
Chris@76 2063 $request = $smcFunc['db_query']('', '
Chris@76 2064 SELECT id_comment, body AS question
Chris@76 2065 FROM {db_prefix}log_comments
Chris@76 2066 WHERE comment_type = {string:ver_test}
Chris@76 2067 AND id_comment IN ({array_int:comment_ids})',
Chris@76 2068 array(
Chris@76 2069 'ver_test' => 'ver_test',
Chris@76 2070 'comment_ids' => $questionIDs,
Chris@76 2071 )
Chris@76 2072 );
Chris@76 2073 $_SESSION[$verificationOptions['id'] . '_vv']['q'] = array();
Chris@76 2074 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 2075 {
Chris@76 2076 $thisVerification['questions'][] = array(
Chris@76 2077 'id' => $row['id_comment'],
Chris@76 2078 'q' => parse_bbc($row['question']),
Chris@76 2079 'is_error' => !empty($incorrectQuestions) && in_array($row['id_comment'], $incorrectQuestions),
Chris@76 2080 // Remember a previous submission?
Chris@76 2081 '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 2082 );
Chris@76 2083 $_SESSION[$verificationOptions['id'] . '_vv']['q'][] = $row['id_comment'];
Chris@76 2084 }
Chris@76 2085 $smcFunc['db_free_result']($request);
Chris@76 2086 }
Chris@76 2087
Chris@76 2088 $_SESSION[$verificationOptions['id'] . '_vv']['count'] = empty($_SESSION[$verificationOptions['id'] . '_vv']['count']) ? 1 : $_SESSION[$verificationOptions['id'] . '_vv']['count'] + 1;
Chris@76 2089
Chris@76 2090 // Return errors if we have them.
Chris@76 2091 if (!empty($verification_errors))
Chris@76 2092 return $verification_errors;
Chris@76 2093 // If we had a test that one, make a note.
Chris@76 2094 elseif ($do_test)
Chris@76 2095 $_SESSION[$verificationOptions['id'] . '_vv']['did_pass'] = true;
Chris@76 2096
Chris@76 2097 // Say that everything went well chaps.
Chris@76 2098 return true;
Chris@76 2099 }
Chris@76 2100
Chris@76 2101 // This keeps track of all registered handling functions for auto suggest functionality and passes execution to them.
Chris@76 2102 function AutoSuggestHandler($checkRegistered = null)
Chris@76 2103 {
Chris@76 2104 global $context;
Chris@76 2105
Chris@76 2106 // These are all registered types.
Chris@76 2107 $searchTypes = array(
Chris@76 2108 'member' => 'Member',
Chris@76 2109 );
Chris@76 2110
Chris@76 2111 // If we're just checking the callback function is registered return true or false.
Chris@76 2112 if ($checkRegistered != null)
Chris@76 2113 return isset($searchTypes[$checkRegistered]) && function_exists('AutoSuggest_Search_' . $checkRegistered);
Chris@76 2114
Chris@76 2115 checkSession('get');
Chris@76 2116 loadTemplate('Xml');
Chris@76 2117
Chris@76 2118 // Any parameters?
Chris@76 2119 $context['search_param'] = isset($_REQUEST['search_param']) ? unserialize(base64_decode($_REQUEST['search_param'])) : array();
Chris@76 2120
Chris@76 2121 if (isset($_REQUEST['suggest_type'], $_REQUEST['search']) && isset($searchTypes[$_REQUEST['suggest_type']]))
Chris@76 2122 {
Chris@76 2123 $function = 'AutoSuggest_Search_' . $searchTypes[$_REQUEST['suggest_type']];
Chris@76 2124 $context['sub_template'] = 'generic_xml';
Chris@76 2125 $context['xml_data'] = $function();
Chris@76 2126 }
Chris@76 2127 }
Chris@76 2128
Chris@76 2129 // Search for a member - by real_name or member_name by default.
Chris@76 2130 function AutoSuggest_Search_Member()
Chris@76 2131 {
Chris@76 2132 global $user_info, $txt, $smcFunc, $context;
Chris@76 2133
Chris@76 2134 $_REQUEST['search'] = trim($smcFunc['strtolower']($_REQUEST['search'])) . '*';
Chris@76 2135 $_REQUEST['search'] = strtr($_REQUEST['search'], array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_', '&#038;' => '&amp;'));
Chris@76 2136
Chris@76 2137 // Find the member.
Chris@76 2138 $request = $smcFunc['db_query']('', '
Chris@76 2139 SELECT id_member, real_name
Chris@76 2140 FROM {db_prefix}members
Chris@76 2141 WHERE real_name LIKE {string:search}' . (!empty($context['search_param']['buddies']) ? '
Chris@76 2142 AND id_member IN ({array_int:buddy_list})' : '') . '
Chris@76 2143 AND is_activated IN (1, 11)
Chris@76 2144 LIMIT ' . ($smcFunc['strlen']($_REQUEST['search']) <= 2 ? '100' : '800'),
Chris@76 2145 array(
Chris@76 2146 'buddy_list' => $user_info['buddies'],
Chris@76 2147 'search' => $_REQUEST['search'],
Chris@76 2148 )
Chris@76 2149 );
Chris@76 2150 $xml_data = array(
Chris@76 2151 'items' => array(
Chris@76 2152 'identifier' => 'item',
Chris@76 2153 'children' => array(),
Chris@76 2154 ),
Chris@76 2155 );
Chris@76 2156 while ($row = $smcFunc['db_fetch_assoc']($request))
Chris@76 2157 {
Chris@76 2158 $row['real_name'] = strtr($row['real_name'], array('&amp;' => '&#038;', '&lt;' => '&#060;', '&gt;' => '&#062;', '&quot;' => '&#034;'));
Chris@76 2159
Chris@76 2160 $xml_data['items']['children'][] = array(
Chris@76 2161 'attributes' => array(
Chris@76 2162 'id' => $row['id_member'],
Chris@76 2163 ),
Chris@76 2164 'value' => $row['real_name'],
Chris@76 2165 );
Chris@76 2166 }
Chris@76 2167 $smcFunc['db_free_result']($request);
Chris@76 2168
Chris@76 2169 return $xml_data;
Chris@76 2170 }
Chris@76 2171
Chris@76 2172 ?>