Subs-Editor.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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 2011 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. * Creates the javascript code for localization of the editor (SCEditor)
  19. */
  20. function loadLocale()
  21. {
  22. global $context, $txt, $txteditor, $modSettings;
  23. loadLanguage('Editor');
  24. $context['template_layers'] = array();
  25. // Lets make sure we aren't going to output anything nasty.
  26. @ob_end_clean();
  27. if (!empty($modSettings['enableCompressedOutput']))
  28. @ob_start('ob_gzhandler');
  29. else
  30. @ob_start();
  31. // If we don't have any locale better avoit broken js
  32. if (empty($txt['lang_locale']))
  33. die();
  34. $file_data = '(function ($) {
  35. \'use strict\';
  36. $.sceditor.locale[' . javaScriptEscape($txt['lang_locale']) . '] = {';
  37. foreach ($txteditor as $key => $val)
  38. $file_data .= '
  39. ' . javaScriptEscape($key) . ': ' . javaScriptEscape($val) . ',';
  40. $file_data .= '
  41. dateFormat: "day.month.year"
  42. }
  43. })(jQuery);';
  44. // Make sure they know what type of file we are.
  45. header('Content-Type: text/javascript');
  46. echo $file_data;
  47. obExit(false);
  48. }
  49. /**
  50. * Retrieves a list of message icons.
  51. * - Based on the settings, the array will either contain a list of default
  52. * message icons or a list of custom message icons retrieved from the database.
  53. * - The board_id is needed for the custom message icons (which can be set for
  54. * each board individually).
  55. *
  56. * @param int $board_id
  57. * @return array
  58. */
  59. function getMessageIcons($board_id)
  60. {
  61. global $modSettings, $context, $txt, $settings, $smcFunc;
  62. if (empty($modSettings['messageIcons_enable']))
  63. {
  64. loadLanguage('Post');
  65. $icons = array(
  66. array('value' => 'xx', 'name' => $txt['standard']),
  67. array('value' => 'thumbup', 'name' => $txt['thumbs_up']),
  68. array('value' => 'thumbdown', 'name' => $txt['thumbs_down']),
  69. array('value' => 'exclamation', 'name' => $txt['excamation_point']),
  70. array('value' => 'question', 'name' => $txt['question_mark']),
  71. array('value' => 'lamp', 'name' => $txt['lamp']),
  72. array('value' => 'smiley', 'name' => $txt['icon_smiley']),
  73. array('value' => 'angry', 'name' => $txt['icon_angry']),
  74. array('value' => 'cheesy', 'name' => $txt['icon_cheesy']),
  75. array('value' => 'grin', 'name' => $txt['icon_grin']),
  76. array('value' => 'sad', 'name' => $txt['icon_sad']),
  77. array('value' => 'wink', 'name' => $txt['icon_wink']),
  78. array('value' => 'poll', 'name' => $txt['icon_poll']),
  79. );
  80. foreach ($icons as $k => $dummy)
  81. {
  82. $icons[$k]['url'] = $settings['images_url'] . '/post/' . $dummy['value'] . '.png';
  83. $icons[$k]['is_last'] = false;
  84. }
  85. }
  86. // Otherwise load the icons, and check we give the right image too...
  87. else
  88. {
  89. if (($temp = cache_get_data('posting_icons-' . $board_id, 480)) == null)
  90. {
  91. $request = $smcFunc['db_query']('select_message_icons', '
  92. SELECT title, filename
  93. FROM {db_prefix}message_icons
  94. WHERE id_board IN (0, {int:board_id})',
  95. array(
  96. 'board_id' => $board_id,
  97. )
  98. );
  99. $icon_data = array();
  100. while ($row = $smcFunc['db_fetch_assoc']($request))
  101. $icon_data[] = $row;
  102. $smcFunc['db_free_result']($request);
  103. $icons = array();
  104. foreach ($icon_data as $icon)
  105. {
  106. $icons[$icon['filename']] = array(
  107. 'value' => $icon['filename'],
  108. 'name' => $icon['title'],
  109. 'url' => $settings[file_exists($settings['theme_dir'] . '/images/post/' . $icon['filename'] . '.png') ? 'images_url' : 'default_images_url'] . '/post/' . $icon['filename'] . '.png',
  110. 'is_last' => false,
  111. );
  112. }
  113. cache_put_data('posting_icons-' . $board_id, $icons, 480);
  114. }
  115. else
  116. $icons = $temp;
  117. }
  118. return array_values($icons);
  119. }
  120. /**
  121. * A help function for legalise_bbc for sorting arrays based on length.
  122. * @param string $a
  123. * @param string $b
  124. * @return int 1 or -1
  125. */
  126. function sort_array_length($a, $b)
  127. {
  128. return strlen($a) < strlen($b) ? 1 : -1;
  129. }
  130. /**
  131. * Compatibility function - used in 1.1 for showing a post box.
  132. *
  133. * @param string $msg
  134. * @return string
  135. */
  136. function theme_postbox($msg)
  137. {
  138. global $context;
  139. return template_control_richedit($context['post_box_name']);
  140. }
  141. /**
  142. * Creates a box that can be used for richedit stuff like BBC, Smileys etc.
  143. * @param array $editorOptions
  144. */
  145. function create_control_richedit($editorOptions)
  146. {
  147. global $txt, $modSettings, $options, $smcFunc;
  148. global $context, $settings, $user_info, $sourcedir, $scripturl;
  149. // Load the Post language file... for the moment at least.
  150. loadLanguage('Post');
  151. // Every control must have a ID!
  152. assert(isset($editorOptions['id']));
  153. assert(isset($editorOptions['value']));
  154. // Is this the first richedit - if so we need to ensure some template stuff is initialised.
  155. if (empty($context['controls']['richedit']))
  156. {
  157. // Some general stuff.
  158. $settings['smileys_url'] = $modSettings['smileys_url'] . '/' . $user_info['smiley_set'];
  159. // This really has some WYSIWYG stuff.
  160. loadTemplate('GenericControls', isBrowser('ie') ? 'editor_ie' : 'editor');
  161. $context['html_headers'] .= '
  162. <script type="text/javascript"><!-- // --><![CDATA[
  163. var smf_smileys_url = \'' . $settings['smileys_url'] . '\';
  164. var bbc_quote_from = \'' . addcslashes($txt['quote_from'], "'") . '\';
  165. var bbc_quote = \'' . addcslashes($txt['quote'], "'") . '\';
  166. var bbc_search_on = \'' . addcslashes($txt['search_on'], "'") . '\';
  167. // ]]></script>
  168. <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/editor.js?alp21"></script>
  169. <link rel="stylesheet" href="' . $settings['default_theme_url'] . '/css/jquery.sceditor.css" type="text/css" media="all" />
  170. <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/jquery.sceditor.js"></script>
  171. <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/jquery.sceditor.bbcode.js"></script>';
  172. if (!empty($txt['lang_locale']) && $txt['lang_locale'] != 'en_US')
  173. $context['html_headers'] .= '
  174. <script type="text/javascript" src="' . $scripturl . '?action=loadeditorlocale"></script>';
  175. $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && function_exists('pspell_new');
  176. if ($context['show_spellchecking'])
  177. {
  178. $context['html_headers'] .= '
  179. <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/spellcheck.js?alp21"></script>';
  180. // Some hidden information is needed in order to make the spell checking work.
  181. if (!isset($_REQUEST['xml']))
  182. $context['insert_after_template'] .= '
  183. <form name="spell_form" id="spell_form" method="post" accept-charset="' . $context['character_set'] . '" target="spellWindow" action="' . $scripturl . '?action=spellcheck">
  184. <input type="hidden" name="spellstring" value="" />
  185. </form>';
  186. }
  187. }
  188. // Start off the editor...
  189. $context['controls']['richedit'][$editorOptions['id']] = array(
  190. 'id' => $editorOptions['id'],
  191. 'value' => $editorOptions['value'],
  192. 'rich_value' => $editorOptions['value'], // 2.0 editor compatibility
  193. 'rich_active' => empty($modSettings['disable_wysiwyg']) && (!empty($options['wysiwyg_default']) || !empty($editorOptions['force_rich']) || !empty($_REQUEST[$editorOptions['id'] . '_mode'])),
  194. 'disable_smiley_box' => !empty($editorOptions['disable_smiley_box']),
  195. 'columns' => isset($editorOptions['columns']) ? $editorOptions['columns'] : 60,
  196. 'rows' => isset($editorOptions['rows']) ? $editorOptions['rows'] : 18,
  197. 'width' => isset($editorOptions['width']) ? $editorOptions['width'] : '70%',
  198. 'height' => isset($editorOptions['height']) ? $editorOptions['height'] : '250px',
  199. 'form' => isset($editorOptions['form']) ? $editorOptions['form'] : 'postmodify',
  200. 'bbc_level' => !empty($editorOptions['bbc_level']) ? $editorOptions['bbc_level'] : 'full',
  201. 'preview_type' => isset($editorOptions['preview_type']) ? (int) $editorOptions['preview_type'] : 1,
  202. 'labels' => !empty($editorOptions['labels']) ? $editorOptions['labels'] : array(),
  203. 'locale' => !empty($txt['lang_locale']) && substr($txt['lang_locale'], 0, 5) != 'en_US' ? $txt['lang_locale'] : '',
  204. );
  205. // Switch between default images and back... mostly in case you don't have an PersonalMessage template, but do have a Post template.
  206. if (isset($settings['use_default_images']) && $settings['use_default_images'] == 'defaults' && isset($settings['default_template']))
  207. {
  208. $temp1 = $settings['theme_url'];
  209. $settings['theme_url'] = $settings['default_theme_url'];
  210. $temp2 = $settings['images_url'];
  211. $settings['images_url'] = $settings['default_images_url'];
  212. $temp3 = $settings['theme_dir'];
  213. $settings['theme_dir'] = $settings['default_theme_dir'];
  214. }
  215. if (empty($context['bbc_tags']))
  216. {
  217. // 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!
  218. /*
  219. array(
  220. 'image' => 'bold',
  221. 'code' => 'b',
  222. 'before' => '[b]',
  223. 'after' => '[/b]',
  224. 'description' => $txt['bold'],
  225. ),
  226. */
  227. $context['bbc_tags'] = array();
  228. $context['bbc_tags'][] = array(
  229. array(
  230. 'code' => 'bold',
  231. 'description' => $txt['bold'],
  232. ),
  233. array(
  234. 'code' => 'italic',
  235. 'description' => $txt['italic'],
  236. ),
  237. array(
  238. 'code' => 'underline',
  239. 'description' => $txt['underline']
  240. ),
  241. array(
  242. 'code' => 'strike',
  243. 'description' => $txt['strike']
  244. ),
  245. array(),
  246. array(
  247. 'code' => 'pre',
  248. 'description' => $txt['preformatted']
  249. ),
  250. array(
  251. 'code' => 'left',
  252. 'description' => $txt['left_align']
  253. ),
  254. array(
  255. 'code' => 'center',
  256. 'description' => $txt['center']
  257. ),
  258. array(
  259. 'code' => 'right',
  260. 'description' => $txt['right_align']
  261. ),
  262. );
  263. $context['bbc_tags'][] = array(
  264. array(
  265. 'code' => 'flash',
  266. 'description' => $txt['flash']
  267. ),
  268. array(
  269. 'code' => 'image',
  270. 'description' => $txt['image']
  271. ),
  272. array(
  273. 'code' => 'link',
  274. 'description' => $txt['hyperlink']
  275. ),
  276. array(
  277. 'code' => 'email',
  278. 'description' => $txt['insert_email']
  279. ),
  280. array(
  281. 'code' => 'ftp',
  282. 'description' => $txt['ftp']
  283. ),
  284. array(),
  285. array(
  286. 'code' => 'glow',
  287. 'description' => $txt['glow']
  288. ),
  289. array(
  290. 'code' => 'shadow',
  291. 'description' => $txt['shadow']
  292. ),
  293. array(
  294. 'code' => 'move',
  295. 'description' => $txt['marquee']
  296. ),
  297. array(),
  298. array(
  299. 'code' => 'superscript',
  300. 'description' => $txt['superscript']
  301. ),
  302. array(
  303. 'code' => 'subscript',
  304. 'description' => $txt['subscript']
  305. ),
  306. array(
  307. 'code' => 'tt',
  308. 'description' => $txt['teletype']
  309. ),
  310. array(),
  311. array(
  312. 'code' => 'table',
  313. 'description' => $txt['table']
  314. ),
  315. array(
  316. 'code' => 'code',
  317. 'description' => $txt['bbc_code']
  318. ),
  319. array(
  320. 'code' => 'quote',
  321. 'description' => $txt['bbc_quote']
  322. ),
  323. array(),
  324. array(
  325. 'code' => 'bulletlist',
  326. 'description' => $txt['list_unordered']
  327. ),
  328. array(
  329. 'code' => 'orderedlist',
  330. 'description' => $txt['list_ordered']
  331. ),
  332. array(
  333. 'code' => 'horizontalrule',
  334. 'description' => $txt['horizontal_rule']
  335. ),
  336. );
  337. // Allow mods to modify BBC buttons.
  338. call_integration_hook('integrate_bbc_buttons');
  339. // Show the toggle?
  340. if (empty($modSettings['disable_wysiwyg']))
  341. {
  342. $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array();
  343. $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array(
  344. 'code' => 'unformat',
  345. 'description' => $txt['unformat_text'],
  346. );
  347. $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array(
  348. 'code' => 'toggle',
  349. 'description' => $txt['toggle_view'],
  350. );
  351. }
  352. // Generate a list of buttons that shouldn't be shown - this should be the fastest way to do this.
  353. $disabled_tags = array();
  354. if (!empty($modSettings['disabledBBC']))
  355. $disabled_tags = explode(',', $modSettings['disabledBBC']);
  356. if (empty($modSettings['enableEmbeddedFlash']))
  357. $disabled_tags[] = 'flash';
  358. foreach ($disabled_tags as $tag)
  359. {
  360. if ($tag == 'list')
  361. {
  362. $context['disabled_tags']['bulletlist'] = true;
  363. $context['disabled_tags']['orderedlist'] = true;
  364. }
  365. elseif ($tag == 'b')
  366. $context['disabled_tags']['bold'] = true;
  367. elseif ($tag == 'i')
  368. $context['disabled_tags']['italic'] = true;
  369. elseif ($tag == 'i')
  370. $context['disabled_tags']['underline'] = true;
  371. elseif ($tag == 'i')
  372. $context['disabled_tags']['strike'] = true;
  373. elseif ($tag == 'img')
  374. $context['disabled_tags']['image'] = true;
  375. elseif ($tag == 'url')
  376. $context['disabled_tags']['link'] = true;
  377. elseif ($tag == 'sup')
  378. $context['disabled_tags']['superscript'] = true;
  379. elseif ($tag == 'sub')
  380. $context['disabled_tags']['subscript'] = true;
  381. elseif ($tag == 'hr')
  382. $context['disabled_tags']['horizontalrule'] = true;
  383. $context['disabled_tags'][trim($tag)] = true;
  384. }
  385. $bbcodes_styles = '';
  386. $context['bbcodes_hanlders'] = '';
  387. $context['bbc_toolbar'] = array();
  388. foreach ($context['bbc_tags'] as $row => $tagRow)
  389. {
  390. if (!isset($context['bbc_toolbar'][$row]))
  391. $context['bbc_toolbar'][$row] = array();
  392. $tagsRow = array();
  393. foreach ($tagRow as $tag)
  394. {
  395. if (!empty($tag))
  396. {
  397. if (empty($context['disabled_tags'][$tag['code']]))
  398. {
  399. $tagsRow[] = $tag['code'];
  400. if (isset($tag['image']))
  401. $bbcodes_styles .= '
  402. .sceditor-button-' . $tag['code'] . ' div {
  403. background: url(\'' . $settings['default_theme_url'] . '/images/bbc/' . $tag['image'] . '.png\');
  404. }';
  405. if (isset($tag['before']))
  406. {
  407. $context['bbcodes_hanlders'] = '
  408. $.sceditor.setCommand(
  409. ' . javaScriptEscape($tag['code']) . ',
  410. function () {
  411. this.wysiwygEditorInsertHtml(' . javaScriptEscape($tag['before']) . (isset($tag['after']) ? ', ' . javaScriptEscape($tag['after']) : '') . ');
  412. },
  413. ' . javaScriptEscape($tag['description']) . ',
  414. null,
  415. [' . javaScriptEscape($tag['before']) . (isset($tag['after']) ? ', ' . javaScriptEscape($tag['after']) : '') . ']
  416. );';
  417. }
  418. }
  419. }
  420. else
  421. {
  422. $context['bbc_toolbar'][$row][] = implode(',', $tagsRow);
  423. $tagsRow = array();
  424. }
  425. }
  426. if ($row == 0)
  427. {
  428. $context['bbc_toolbar'][$row][] = implode(',', $tagsRow);
  429. $tagsRow = array();
  430. if (!isset($context['disabled_tags']['font']))
  431. $tagsRow[] = 'font';
  432. if (!isset($context['disabled_tags']['size']))
  433. $tagsRow[] = 'size';
  434. if (!isset($context['disabled_tags']['color']))
  435. $tagsRow[] = 'color';
  436. }
  437. elseif ($row == 1 && empty($modSettings['disable_wysiwyg']))
  438. {
  439. $tmp = array();
  440. $tagsRow[] = 'removeformat';
  441. $tagsRow[] = 'source';
  442. if (!empty($tmp))
  443. {
  444. $tagsRow[] = '|' . implode(',', $tmp);
  445. }
  446. }
  447. if (!empty($tagsRow))
  448. $context['bbc_toolbar'][$row][] = implode(',', $tagsRow);
  449. }
  450. if (!empty($bbcodes_styles))
  451. $context['html_headers'] .= '
  452. <style type="text/css">' . $bbcodes_styles . '
  453. </style>';
  454. }
  455. // Initialize smiley array... if not loaded before.
  456. if (empty($context['smileys']) && empty($editorOptions['disable_smiley_box']))
  457. {
  458. $context['smileys'] = array(
  459. 'postform' => array(),
  460. 'popup' => array(),
  461. );
  462. // Load smileys - don't bother to run a query if we're not using the database's ones anyhow.
  463. if (empty($modSettings['smiley_enable']) && $user_info['smiley_set'] != 'none')
  464. $context['smileys']['postform'][] = array(
  465. 'smileys' => array(
  466. array(
  467. 'code' => ':)',
  468. 'filename' => 'smiley.gif',
  469. 'description' => $txt['icon_smiley'],
  470. ),
  471. array(
  472. 'code' => ';)',
  473. 'filename' => 'wink.gif',
  474. 'description' => $txt['icon_wink'],
  475. ),
  476. array(
  477. 'code' => ':D',
  478. 'filename' => 'cheesy.gif',
  479. 'description' => $txt['icon_cheesy'],
  480. ),
  481. array(
  482. 'code' => ';D',
  483. 'filename' => 'grin.gif',
  484. 'description' => $txt['icon_grin']
  485. ),
  486. array(
  487. 'code' => '>:(',
  488. 'filename' => 'angry.gif',
  489. 'description' => $txt['icon_angry'],
  490. ),
  491. array(
  492. 'code' => ':(',
  493. 'filename' => 'sad.gif',
  494. 'description' => $txt['icon_sad'],
  495. ),
  496. array(
  497. 'code' => ':o',
  498. 'filename' => 'shocked.gif',
  499. 'description' => $txt['icon_shocked'],
  500. ),
  501. array(
  502. 'code' => '8)',
  503. 'filename' => 'cool.gif',
  504. 'description' => $txt['icon_cool'],
  505. ),
  506. array(
  507. 'code' => '???',
  508. 'filename' => 'huh.gif',
  509. 'description' => $txt['icon_huh'],
  510. ),
  511. array(
  512. 'code' => '::)',
  513. 'filename' => 'rolleyes.gif',
  514. 'description' => $txt['icon_rolleyes'],
  515. ),
  516. array(
  517. 'code' => ':P',
  518. 'filename' => 'tongue.gif',
  519. 'description' => $txt['icon_tongue'],
  520. ),
  521. array(
  522. 'code' => ':-[',
  523. 'filename' => 'embarrassed.gif',
  524. 'description' => $txt['icon_embarrassed'],
  525. ),
  526. array(
  527. 'code' => ':-X',
  528. 'filename' => 'lipsrsealed.gif',
  529. 'description' => $txt['icon_lips'],
  530. ),
  531. array(
  532. 'code' => ':-\\',
  533. 'filename' => 'undecided.gif',
  534. 'description' => $txt['icon_undecided'],
  535. ),
  536. array(
  537. 'code' => ':-*',
  538. 'filename' => 'kiss.gif',
  539. 'description' => $txt['icon_kiss'],
  540. ),
  541. array(
  542. 'code' => ':\'(',
  543. 'filename' => 'cry.gif',
  544. 'description' => $txt['icon_cry'],
  545. 'isLast' => true,
  546. ),
  547. ),
  548. 'isLast' => true,
  549. );
  550. elseif ($user_info['smiley_set'] != 'none')
  551. {
  552. if (($temp = cache_get_data('posting_smileys', 480)) == null)
  553. {
  554. $request = $smcFunc['db_query']('', '
  555. SELECT code, filename, description, smiley_row, hidden
  556. FROM {db_prefix}smileys
  557. WHERE hidden IN (0, 2)
  558. ORDER BY smiley_row, smiley_order',
  559. array(
  560. )
  561. );
  562. while ($row = $smcFunc['db_fetch_assoc']($request))
  563. {
  564. $row['filename'] = htmlspecialchars($row['filename']);
  565. $row['description'] = htmlspecialchars($row['description']);
  566. $context['smileys'][empty($row['hidden']) ? 'postform' : 'popup'][$row['smiley_row']]['smileys'][] = $row;
  567. }
  568. $smcFunc['db_free_result']($request);
  569. foreach ($context['smileys'] as $section => $smileyRows)
  570. {
  571. foreach ($smileyRows as $rowIndex => $smileys)
  572. $context['smileys'][$section][$rowIndex]['smileys'][count($smileys['smileys']) - 1]['isLast'] = true;
  573. if (!empty($smileyRows))
  574. $context['smileys'][$section][count($smileyRows) - 1]['isLast'] = true;
  575. }
  576. cache_put_data('posting_smileys', $context['smileys'], 480);
  577. }
  578. else
  579. $context['smileys'] = $temp;
  580. }
  581. }
  582. // Set a flag so the sub template knows what to do...
  583. $context['show_bbc'] = !empty($modSettings['enableBBC']) && !empty($settings['show_bbc']);
  584. // Switch the URLs back... now we're back to whatever the main sub template is. (like folder in PersonalMessage.)
  585. if (isset($settings['use_default_images']) && $settings['use_default_images'] == 'defaults' && isset($settings['default_template']))
  586. {
  587. $settings['theme_url'] = $temp1;
  588. $settings['images_url'] = $temp2;
  589. $settings['theme_dir'] = $temp3;
  590. }
  591. }
  592. /**
  593. * Create a anti-bot verification control?
  594. * @param array &$verificationOptions
  595. * @param bool $do_test = false
  596. */
  597. function create_control_verification(&$verificationOptions, $do_test = false)
  598. {
  599. global $txt, $modSettings, $options, $smcFunc;
  600. global $context, $settings, $user_info, $sourcedir, $scripturl;
  601. // First verification means we need to set up some bits...
  602. if (empty($context['controls']['verification']))
  603. {
  604. // The template
  605. loadTemplate('GenericControls');
  606. // Some javascript ma'am?
  607. if (!empty($verificationOptions['override_visual']) || (!empty($modSettings['visual_verification_type']) && !isset($verificationOptions['override_visual'])))
  608. $context['html_headers'] .= '
  609. <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/captcha.js"></script>';
  610. $context['use_graphic_library'] = in_array('gd', get_loaded_extensions());
  611. // Skip I, J, L, O, Q, S and Z.
  612. $context['standard_captcha_range'] = array_merge(range('A', 'H'), array('K', 'M', 'N', 'P', 'R'), range('T', 'Y'));
  613. }
  614. // Always have an ID.
  615. assert(isset($verificationOptions['id']));
  616. $isNew = !isset($context['controls']['verification'][$verificationOptions['id']]);
  617. // Log this into our collection.
  618. if ($isNew)
  619. $context['controls']['verification'][$verificationOptions['id']] = array(
  620. 'id' => $verificationOptions['id'],
  621. 'show_visual' => !empty($verificationOptions['override_visual']) || (!empty($modSettings['visual_verification_type']) && !isset($verificationOptions['override_visual'])),
  622. 'number_questions' => isset($verificationOptions['override_qs']) ? $verificationOptions['override_qs'] : (!empty($modSettings['qa_verification_number']) ? $modSettings['qa_verification_number'] : 0),
  623. 'max_errors' => isset($verificationOptions['max_errors']) ? $verificationOptions['max_errors'] : 3,
  624. 'image_href' => $scripturl . '?action=verificationcode;vid=' . $verificationOptions['id'] . ';rand=' . md5(mt_rand()),
  625. 'text_value' => '',
  626. 'questions' => array(),
  627. );
  628. $thisVerification = &$context['controls']['verification'][$verificationOptions['id']];
  629. // Add javascript for the object.
  630. if ($context['controls']['verification'][$verificationOptions['id']]['show_visual'] && !WIRELESS)
  631. $context['insert_after_template'] .= '
  632. <script type="text/javascript"><!-- // --><![CDATA[
  633. var verification' . $verificationOptions['id'] . 'Handle = new smfCaptcha("' . $thisVerification['image_href'] . '", "' . $verificationOptions['id'] . '", ' . ($context['use_graphic_library'] ? 1 : 0) . ');
  634. // ]]></script>';
  635. // Is there actually going to be anything?
  636. if (empty($thisVerification['show_visual']) && empty($thisVerification['number_questions']))
  637. return false;
  638. elseif (!$isNew && !$do_test)
  639. return true;
  640. // If we want questions do we have a cache of all the IDs?
  641. if (!empty($thisVerification['number_questions']) && empty($modSettings['question_id_cache']))
  642. {
  643. if (($modSettings['question_id_cache'] = cache_get_data('verificationQuestionIds', 300)) == null)
  644. {
  645. $request = $smcFunc['db_query']('', '
  646. SELECT id_comment
  647. FROM {db_prefix}log_comments
  648. WHERE comment_type = {string:ver_test}',
  649. array(
  650. 'ver_test' => 'ver_test',
  651. )
  652. );
  653. $modSettings['question_id_cache'] = array();
  654. while ($row = $smcFunc['db_fetch_assoc']($request))
  655. $modSettings['question_id_cache'][] = $row['id_comment'];
  656. $smcFunc['db_free_result']($request);
  657. if (!empty($modSettings['cache_enable']))
  658. cache_put_data('verificationQuestionIds', $modSettings['question_id_cache'], 300);
  659. }
  660. }
  661. if (!isset($_SESSION[$verificationOptions['id'] . '_vv']))
  662. $_SESSION[$verificationOptions['id'] . '_vv'] = array();
  663. // Do we need to refresh the verification?
  664. 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']))
  665. $force_refresh = true;
  666. else
  667. $force_refresh = false;
  668. // This can also force a fresh, although unlikely.
  669. if (($thisVerification['show_visual'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['code'])) || ($thisVerification['number_questions'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['q'])))
  670. $force_refresh = true;
  671. $verification_errors = array();
  672. // Start with any testing.
  673. if ($do_test)
  674. {
  675. // This cannot happen!
  676. if (!isset($_SESSION[$verificationOptions['id'] . '_vv']['count']))
  677. fatal_lang_error('no_access', false);
  678. // ... nor this!
  679. if ($thisVerification['number_questions'] && (!isset($_SESSION[$verificationOptions['id'] . '_vv']['q']) || !isset($_REQUEST[$verificationOptions['id'] . '_vv']['q'])))
  680. fatal_lang_error('no_access', false);
  681. 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']))
  682. $verification_errors[] = 'wrong_verification_code';
  683. if ($thisVerification['number_questions'])
  684. {
  685. // Get the answers and see if they are all right!
  686. $request = $smcFunc['db_query']('', '
  687. SELECT id_comment, recipient_name AS answer
  688. FROM {db_prefix}log_comments
  689. WHERE comment_type = {string:ver_test}
  690. AND id_comment IN ({array_int:comment_ids})',
  691. array(
  692. 'ver_test' => 'ver_test',
  693. 'comment_ids' => $_SESSION[$verificationOptions['id'] . '_vv']['q'],
  694. )
  695. );
  696. $incorrectQuestions = array();
  697. while ($row = $smcFunc['db_fetch_assoc']($request))
  698. {
  699. 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']))
  700. $incorrectQuestions[] = $row['id_comment'];
  701. }
  702. $smcFunc['db_free_result']($request);
  703. if (!empty($incorrectQuestions))
  704. $verification_errors[] = 'wrong_verification_answer';
  705. }
  706. }
  707. // Any errors means we refresh potentially.
  708. if (!empty($verification_errors))
  709. {
  710. if (empty($_SESSION[$verificationOptions['id'] . '_vv']['errors']))
  711. $_SESSION[$verificationOptions['id'] . '_vv']['errors'] = 0;
  712. // Too many errors?
  713. elseif ($_SESSION[$verificationOptions['id'] . '_vv']['errors'] > $thisVerification['max_errors'])
  714. $force_refresh = true;
  715. // Keep a track of these.
  716. $_SESSION[$verificationOptions['id'] . '_vv']['errors']++;
  717. }
  718. // Are we refreshing then?
  719. if ($force_refresh)
  720. {
  721. // Assume nothing went before.
  722. $_SESSION[$verificationOptions['id'] . '_vv']['count'] = 0;
  723. $_SESSION[$verificationOptions['id'] . '_vv']['errors'] = 0;
  724. $_SESSION[$verificationOptions['id'] . '_vv']['did_pass'] = false;
  725. $_SESSION[$verificationOptions['id'] . '_vv']['q'] = array();
  726. $_SESSION[$verificationOptions['id'] . '_vv']['code'] = '';
  727. // Generating a new image.
  728. if ($thisVerification['show_visual'])
  729. {
  730. // Are we overriding the range?
  731. $character_range = !empty($verificationOptions['override_range']) ? $verificationOptions['override_range'] : $context['standard_captcha_range'];
  732. for ($i = 0; $i < 6; $i++)
  733. $_SESSION[$verificationOptions['id'] . '_vv']['code'] .= $character_range[array_rand($character_range)];
  734. }
  735. // Getting some new questions?
  736. if ($thisVerification['number_questions'])
  737. {
  738. // Pick some random IDs
  739. $questionIDs = array();
  740. if ($thisVerification['number_questions'] == 1)
  741. $questionIDs[] = $modSettings['question_id_cache'][array_rand($modSettings['question_id_cache'], $thisVerification['number_questions'])];
  742. else
  743. foreach (array_rand($modSettings['question_id_cache'], $thisVerification['number_questions']) as $index)
  744. $questionIDs[] = $modSettings['question_id_cache'][$index];
  745. }
  746. }
  747. else
  748. {
  749. // Same questions as before.
  750. $questionIDs = !empty($_SESSION[$verificationOptions['id'] . '_vv']['q']) ? $_SESSION[$verificationOptions['id'] . '_vv']['q'] : array();
  751. $thisVerification['text_value'] = !empty($_REQUEST[$verificationOptions['id'] . '_vv']['code']) ? $smcFunc['htmlspecialchars']($_REQUEST[$verificationOptions['id'] . '_vv']['code']) : '';
  752. }
  753. // Have we got some questions to load?
  754. if (!empty($questionIDs))
  755. {
  756. $request = $smcFunc['db_query']('', '
  757. SELECT id_comment, body AS question
  758. FROM {db_prefix}log_comments
  759. WHERE comment_type = {string:ver_test}
  760. AND id_comment IN ({array_int:comment_ids})',
  761. array(
  762. 'ver_test' => 'ver_test',
  763. 'comment_ids' => $questionIDs,
  764. )
  765. );
  766. $_SESSION[$verificationOptions['id'] . '_vv']['q'] = array();
  767. while ($row = $smcFunc['db_fetch_assoc']($request))
  768. {
  769. $thisVerification['questions'][] = array(
  770. 'id' => $row['id_comment'],
  771. 'q' => parse_bbc($row['question']),
  772. 'is_error' => !empty($incorrectQuestions) && in_array($row['id_comment'], $incorrectQuestions),
  773. // Remember a previous submission?
  774. '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']]) : '',
  775. );
  776. $_SESSION[$verificationOptions['id'] . '_vv']['q'][] = $row['id_comment'];
  777. }
  778. $smcFunc['db_free_result']($request);
  779. }
  780. $_SESSION[$verificationOptions['id'] . '_vv']['count'] = empty($_SESSION[$verificationOptions['id'] . '_vv']['count']) ? 1 : $_SESSION[$verificationOptions['id'] . '_vv']['count'] + 1;
  781. // Return errors if we have them.
  782. if (!empty($verification_errors))
  783. return $verification_errors;
  784. // If we had a test that one, make a note.
  785. elseif ($do_test)
  786. $_SESSION[$verificationOptions['id'] . '_vv']['did_pass'] = true;
  787. // Say that everything went well chaps.
  788. return true;
  789. }
  790. /**
  791. * This keeps track of all registered handling functions for auto suggest functionality and passes execution to them.
  792. * @param bool $checkRegistered = null
  793. */
  794. function AutoSuggestHandler($checkRegistered = null)
  795. {
  796. global $context;
  797. // These are all registered types.
  798. $searchTypes = array(
  799. 'member' => 'Member',
  800. 'versions' => 'SMFVersions',
  801. );
  802. // If we're just checking the callback function is registered return true or false.
  803. if ($checkRegistered != null)
  804. return isset($searchTypes[$checkRegistered]) && function_exists('AutoSuggest_Search_' . $checkRegistered);
  805. checkSession('get');
  806. loadTemplate('Xml');
  807. // Any parameters?
  808. $context['search_param'] = isset($_REQUEST['search_param']) ? unserialize(base64_decode($_REQUEST['search_param'])) : array();
  809. if (isset($_REQUEST['suggest_type'], $_REQUEST['search']) && isset($searchTypes[$_REQUEST['suggest_type']]))
  810. {
  811. $function = 'AutoSuggest_Search_' . $searchTypes[$_REQUEST['suggest_type']];
  812. $context['sub_template'] = 'generic_xml';
  813. $context['xml_data'] = $function();
  814. }
  815. }
  816. /**
  817. * Search for a member - by real_name or member_name by default.
  818. *
  819. * @return string
  820. */
  821. function AutoSuggest_Search_Member()
  822. {
  823. global $user_info, $txt, $smcFunc, $context;
  824. $_REQUEST['search'] = trim($smcFunc['strtolower']($_REQUEST['search'])) . '*';
  825. $_REQUEST['search'] = strtr($_REQUEST['search'], array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_', '&#038;' => '&amp;'));
  826. // Find the member.
  827. $request = $smcFunc['db_query']('', '
  828. SELECT id_member, real_name
  829. FROM {db_prefix}members
  830. WHERE real_name LIKE {string:search}' . (!empty($context['search_param']['buddies']) ? '
  831. AND id_member IN ({array_int:buddy_list})' : '') . '
  832. AND is_activated IN (1, 11)
  833. LIMIT ' . ($smcFunc['strlen']($_REQUEST['search']) <= 2 ? '100' : '800'),
  834. array(
  835. 'buddy_list' => $user_info['buddies'],
  836. 'search' => $_REQUEST['search'],
  837. )
  838. );
  839. $xml_data = array(
  840. 'items' => array(
  841. 'identifier' => 'item',
  842. 'children' => array(),
  843. ),
  844. );
  845. while ($row = $smcFunc['db_fetch_assoc']($request))
  846. {
  847. $row['real_name'] = strtr($row['real_name'], array('&amp;' => '&#038;', '&lt;' => '&#060;', '&gt;' => '&#062;', '&quot;' => '&#034;'));
  848. $xml_data['items']['children'][] = array(
  849. 'attributes' => array(
  850. 'id' => $row['id_member'],
  851. ),
  852. 'value' => $row['real_name'],
  853. );
  854. }
  855. $smcFunc['db_free_result']($request);
  856. return $xml_data;
  857. }
  858. function AutoSuggest_Search_SMFVersions()
  859. {
  860. $xml_data = array(
  861. 'items' => array(
  862. 'identifier' => 'item',
  863. 'children' => array(),
  864. ),
  865. );
  866. $versions = array(
  867. 'SMF 1.1',
  868. 'SMF 1.1.1',
  869. 'SMF 1.1.2',
  870. 'SMF 1.1.3',
  871. 'SMF 1.1.4',
  872. 'SMF 1.1.5',
  873. 'SMF 1.1.6',
  874. 'SMF 1.1.7',
  875. 'SMF 1.1.8',
  876. 'SMF 1.1.9',
  877. 'SMF 1.1.10',
  878. 'SMF 1.1.11',
  879. 'SMF 1.1.12',
  880. 'SMF 1.1.13',
  881. 'SMF 1.1.14',
  882. 'SMF 1.1.15',
  883. 'SMF 1.1.16',
  884. 'SMF 2.0 beta 1',
  885. 'SMF 2.0 beta 1.2',
  886. 'SMF 2.0 beta 2',
  887. 'SMF 2.0 beta 3',
  888. 'SMF 2.0 RC 1',
  889. 'SMF 2.0 RC 1.2',
  890. 'SMF 2.0 RC 2',
  891. 'SMF 2.0 RC 3',
  892. 'SMF 2.0',
  893. 'SMF 2.0.1',
  894. 'SMF 2.0.2',
  895. );
  896. foreach ($versions as $id => $version)
  897. if (strpos($version, strtoupper($_REQUEST['search'])) !== false)
  898. $xml_data['items']['children'][] = array(
  899. 'attributes' => array(
  900. 'id' => $id,
  901. ),
  902. 'value' => $version,
  903. );
  904. return $xml_data;
  905. }
  906. ?>