Subs-Editor.php 69 KB

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