123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998 |
- <?php
- /**
- * This file contains those functions specific to the editing box and is
- * generally used for WYSIWYG type functionality.
- *
- * Simple Machines Forum (SMF)
- *
- * @package SMF
- * @author Simple Machines http://www.simplemachines.org
- * @copyright 2011 Simple Machines
- * @license http://www.simplemachines.org/about/smf/license.php BSD
- *
- * @version 2.1 Alpha 1
- */
- if (!defined('SMF'))
- die('Hacking attempt...');
- /**
- * Creates the javascript code for localization of the editor (SCEditor)
- */
- function loadLocale()
- {
- global $context, $txt, $txteditor, $modSettings;
- loadLanguage('Editor');
- $context['template_layers'] = array();
- // Lets make sure we aren't going to output anything nasty.
- @ob_end_clean();
- if (!empty($modSettings['enableCompressedOutput']))
- @ob_start('ob_gzhandler');
- else
- @ob_start();
- // If we don't have any locale better avoit broken js
- if (empty($txt['lang_locale']))
- die();
- $file_data = '(function ($) {
- \'use strict\';
- $.sceditor.locale[' . javaScriptEscape($txt['lang_locale']) . '] = {';
- foreach ($txteditor as $key => $val)
- $file_data .= '
- ' . javaScriptEscape($key) . ': ' . javaScriptEscape($val) . ',';
- $file_data .= '
- dateFormat: "day.month.year"
- }
- })(jQuery);';
- // Make sure they know what type of file we are.
- header('Content-Type: text/javascript');
- echo $file_data;
- obExit(false);
- }
- /**
- * Retrieves a list of message icons.
- * - Based on the settings, the array will either contain a list of default
- * message icons or a list of custom message icons retrieved from the database.
- * - The board_id is needed for the custom message icons (which can be set for
- * each board individually).
- *
- * @param int $board_id
- * @return array
- */
- function getMessageIcons($board_id)
- {
- global $modSettings, $context, $txt, $settings, $smcFunc;
- if (empty($modSettings['messageIcons_enable']))
- {
- loadLanguage('Post');
- $icons = array(
- array('value' => 'xx', 'name' => $txt['standard']),
- array('value' => 'thumbup', 'name' => $txt['thumbs_up']),
- array('value' => 'thumbdown', 'name' => $txt['thumbs_down']),
- array('value' => 'exclamation', 'name' => $txt['excamation_point']),
- array('value' => 'question', 'name' => $txt['question_mark']),
- array('value' => 'lamp', 'name' => $txt['lamp']),
- array('value' => 'smiley', 'name' => $txt['icon_smiley']),
- array('value' => 'angry', 'name' => $txt['icon_angry']),
- array('value' => 'cheesy', 'name' => $txt['icon_cheesy']),
- array('value' => 'grin', 'name' => $txt['icon_grin']),
- array('value' => 'sad', 'name' => $txt['icon_sad']),
- array('value' => 'wink', 'name' => $txt['icon_wink']),
- array('value' => 'poll', 'name' => $txt['icon_poll']),
- );
- foreach ($icons as $k => $dummy)
- {
- $icons[$k]['url'] = $settings['images_url'] . '/post/' . $dummy['value'] . '.png';
- $icons[$k]['is_last'] = false;
- }
- }
- // Otherwise load the icons, and check we give the right image too...
- else
- {
- if (($temp = cache_get_data('posting_icons-' . $board_id, 480)) == null)
- {
- $request = $smcFunc['db_query']('select_message_icons', '
- SELECT title, filename
- FROM {db_prefix}message_icons
- WHERE id_board IN (0, {int:board_id})',
- array(
- 'board_id' => $board_id,
- )
- );
- $icon_data = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $icon_data[] = $row;
- $smcFunc['db_free_result']($request);
- $icons = array();
- foreach ($icon_data as $icon)
- {
- $icons[$icon['filename']] = array(
- 'value' => $icon['filename'],
- 'name' => $icon['title'],
- 'url' => $settings[file_exists($settings['theme_dir'] . '/images/post/' . $icon['filename'] . '.png') ? 'images_url' : 'default_images_url'] . '/post/' . $icon['filename'] . '.png',
- 'is_last' => false,
- );
- }
- cache_put_data('posting_icons-' . $board_id, $icons, 480);
- }
- else
- $icons = $temp;
- }
- return array_values($icons);
- }
- /**
- * A help function for legalise_bbc for sorting arrays based on length.
- * @param string $a
- * @param string $b
- * @return int 1 or -1
- */
- function sort_array_length($a, $b)
- {
- return strlen($a) < strlen($b) ? 1 : -1;
- }
- /**
- * Compatibility function - used in 1.1 for showing a post box.
- *
- * @param string $msg
- * @return string
- */
- function theme_postbox($msg)
- {
- global $context;
- return template_control_richedit($context['post_box_name']);
- }
- /**
- * Creates a box that can be used for richedit stuff like BBC, Smileys etc.
- * @param array $editorOptions
- */
- function create_control_richedit($editorOptions)
- {
- global $txt, $modSettings, $options, $smcFunc;
- global $context, $settings, $user_info, $sourcedir, $scripturl;
- // Load the Post language file... for the moment at least.
- loadLanguage('Post');
- // Every control must have a ID!
- assert(isset($editorOptions['id']));
- assert(isset($editorOptions['value']));
- // Is this the first richedit - if so we need to ensure some template stuff is initialised.
- if (empty($context['controls']['richedit']))
- {
- // Some general stuff.
- $settings['smileys_url'] = $modSettings['smileys_url'] . '/' . $user_info['smiley_set'];
- // This really has some WYSIWYG stuff.
- loadTemplate('GenericControls', isBrowser('ie') ? 'editor_ie' : 'editor');
- $context['html_headers'] .= '
- <script type="text/javascript"><!-- // --><![CDATA[
- var smf_smileys_url = \'' . $settings['smileys_url'] . '\';
- var bbc_quote_from = \'' . addcslashes($txt['quote_from'], "'") . '\';
- var bbc_quote = \'' . addcslashes($txt['quote'], "'") . '\';
- var bbc_search_on = \'' . addcslashes($txt['search_on'], "'") . '\';
- // ]]></script>
- <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/editor.js?alp21"></script>
- <link rel="stylesheet" href="' . $settings['default_theme_url'] . '/css/jquery.sceditor.css" type="text/css" media="all" />
- <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/jquery.sceditor.js"></script>
- <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/jquery.sceditor.bbcode.js"></script>';
- if (!empty($txt['lang_locale']) && $txt['lang_locale'] != 'en_US')
- $context['html_headers'] .= '
- <script type="text/javascript" src="' . $scripturl . '?action=loadeditorlocale"></script>';
- $context['show_spellchecking'] = !empty($modSettings['enableSpellChecking']) && function_exists('pspell_new');
- if ($context['show_spellchecking'])
- {
- $context['html_headers'] .= '
- <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/spellcheck.js?alp21"></script>';
- // Some hidden information is needed in order to make the spell checking work.
- if (!isset($_REQUEST['xml']))
- $context['insert_after_template'] .= '
- <form name="spell_form" id="spell_form" method="post" accept-charset="' . $context['character_set'] . '" target="spellWindow" action="' . $scripturl . '?action=spellcheck">
- <input type="hidden" name="spellstring" value="" />
- </form>';
- }
- }
- // Start off the editor...
- $context['controls']['richedit'][$editorOptions['id']] = array(
- 'id' => $editorOptions['id'],
- 'value' => $editorOptions['value'],
- 'rich_value' => $editorOptions['value'], // 2.0 editor compatibility
- 'rich_active' => empty($modSettings['disable_wysiwyg']) && (!empty($options['wysiwyg_default']) || !empty($editorOptions['force_rich']) || !empty($_REQUEST[$editorOptions['id'] . '_mode'])),
- 'disable_smiley_box' => !empty($editorOptions['disable_smiley_box']),
- 'columns' => isset($editorOptions['columns']) ? $editorOptions['columns'] : 60,
- 'rows' => isset($editorOptions['rows']) ? $editorOptions['rows'] : 18,
- 'width' => isset($editorOptions['width']) ? $editorOptions['width'] : '70%',
- 'height' => isset($editorOptions['height']) ? $editorOptions['height'] : '250px',
- 'form' => isset($editorOptions['form']) ? $editorOptions['form'] : 'postmodify',
- 'bbc_level' => !empty($editorOptions['bbc_level']) ? $editorOptions['bbc_level'] : 'full',
- 'preview_type' => isset($editorOptions['preview_type']) ? (int) $editorOptions['preview_type'] : 1,
- 'labels' => !empty($editorOptions['labels']) ? $editorOptions['labels'] : array(),
- 'locale' => !empty($txt['lang_locale']) && substr($txt['lang_locale'], 0, 5) != 'en_US' ? $txt['lang_locale'] : '',
- );
- // Switch between default images and back... mostly in case you don't have an PersonalMessage template, but do have a Post template.
- if (isset($settings['use_default_images']) && $settings['use_default_images'] == 'defaults' && isset($settings['default_template']))
- {
- $temp1 = $settings['theme_url'];
- $settings['theme_url'] = $settings['default_theme_url'];
- $temp2 = $settings['images_url'];
- $settings['images_url'] = $settings['default_images_url'];
- $temp3 = $settings['theme_dir'];
- $settings['theme_dir'] = $settings['default_theme_dir'];
- }
- if (empty($context['bbc_tags']))
- {
- // 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!
- /*
- array(
- 'image' => 'bold',
- 'code' => 'b',
- 'before' => '[b]',
- 'after' => '[/b]',
- 'description' => $txt['bold'],
- ),
- */
- $context['bbc_tags'] = array();
- $context['bbc_tags'][] = array(
- array(
- 'code' => 'bold',
- 'description' => $txt['bold'],
- ),
- array(
- 'code' => 'italic',
- 'description' => $txt['italic'],
- ),
- array(
- 'code' => 'underline',
- 'description' => $txt['underline']
- ),
- array(
- 'code' => 'strike',
- 'description' => $txt['strike']
- ),
- array(),
- array(
- 'code' => 'pre',
- 'description' => $txt['preformatted']
- ),
- array(
- 'code' => 'left',
- 'description' => $txt['left_align']
- ),
- array(
- 'code' => 'center',
- 'description' => $txt['center']
- ),
- array(
- 'code' => 'right',
- 'description' => $txt['right_align']
- ),
- );
- $context['bbc_tags'][] = array(
- array(
- 'code' => 'flash',
- 'description' => $txt['flash']
- ),
- array(
- 'code' => 'image',
- 'description' => $txt['image']
- ),
- array(
- 'code' => 'link',
- 'description' => $txt['hyperlink']
- ),
- array(
- 'code' => 'email',
- 'description' => $txt['insert_email']
- ),
- array(
- 'code' => 'ftp',
- 'description' => $txt['ftp']
- ),
- array(),
- array(
- 'code' => 'glow',
- 'description' => $txt['glow']
- ),
- array(
- 'code' => 'shadow',
- 'description' => $txt['shadow']
- ),
- array(
- 'code' => 'move',
- 'description' => $txt['marquee']
- ),
- array(),
- array(
- 'code' => 'superscript',
- 'description' => $txt['superscript']
- ),
- array(
- 'code' => 'subscript',
- 'description' => $txt['subscript']
- ),
- array(
- 'code' => 'tt',
- 'description' => $txt['teletype']
- ),
- array(),
- array(
- 'code' => 'table',
- 'description' => $txt['table']
- ),
- array(
- 'code' => 'code',
- 'description' => $txt['bbc_code']
- ),
- array(
- 'code' => 'quote',
- 'description' => $txt['bbc_quote']
- ),
- array(),
- array(
- 'code' => 'bulletlist',
- 'description' => $txt['list_unordered']
- ),
- array(
- 'code' => 'orderedlist',
- 'description' => $txt['list_ordered']
- ),
- array(
- 'code' => 'horizontalrule',
- 'description' => $txt['horizontal_rule']
- ),
- );
- // Allow mods to modify BBC buttons.
- call_integration_hook('integrate_bbc_buttons');
- // Show the toggle?
- if (empty($modSettings['disable_wysiwyg']))
- {
- $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array();
- $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array(
- 'code' => 'unformat',
- 'description' => $txt['unformat_text'],
- );
- $context['bbc_tags'][count($context['bbc_tags']) - 1][] = array(
- 'code' => 'toggle',
- 'description' => $txt['toggle_view'],
- );
- }
- // Generate a list of buttons that shouldn't be shown - this should be the fastest way to do this.
- $disabled_tags = array();
- if (!empty($modSettings['disabledBBC']))
- $disabled_tags = explode(',', $modSettings['disabledBBC']);
- if (empty($modSettings['enableEmbeddedFlash']))
- $disabled_tags[] = 'flash';
- foreach ($disabled_tags as $tag)
- {
- if ($tag == 'list')
- {
- $context['disabled_tags']['bulletlist'] = true;
- $context['disabled_tags']['orderedlist'] = true;
- }
- elseif ($tag == 'b')
- $context['disabled_tags']['bold'] = true;
- elseif ($tag == 'i')
- $context['disabled_tags']['italic'] = true;
- elseif ($tag == 'i')
- $context['disabled_tags']['underline'] = true;
- elseif ($tag == 'i')
- $context['disabled_tags']['strike'] = true;
- elseif ($tag == 'img')
- $context['disabled_tags']['image'] = true;
- elseif ($tag == 'url')
- $context['disabled_tags']['link'] = true;
- elseif ($tag == 'sup')
- $context['disabled_tags']['superscript'] = true;
- elseif ($tag == 'sub')
- $context['disabled_tags']['subscript'] = true;
- elseif ($tag == 'hr')
- $context['disabled_tags']['horizontalrule'] = true;
- $context['disabled_tags'][trim($tag)] = true;
- }
- $bbcodes_styles = '';
- $context['bbcodes_hanlders'] = '';
- $context['bbc_toolbar'] = array();
- foreach ($context['bbc_tags'] as $row => $tagRow)
- {
- if (!isset($context['bbc_toolbar'][$row]))
- $context['bbc_toolbar'][$row] = array();
- $tagsRow = array();
- foreach ($tagRow as $tag)
- {
- if (!empty($tag))
- {
- if (empty($context['disabled_tags'][$tag['code']]))
- {
- $tagsRow[] = $tag['code'];
- if (isset($tag['image']))
- $bbcodes_styles .= '
- .sceditor-button-' . $tag['code'] . ' div {
- background: url(\'' . $settings['default_theme_url'] . '/images/bbc/' . $tag['image'] . '.png\');
- }';
- if (isset($tag['before']))
- {
- $context['bbcodes_hanlders'] = '
- $.sceditor.setCommand(
- ' . javaScriptEscape($tag['code']) . ',
- function () {
- this.wysiwygEditorInsertHtml(' . javaScriptEscape($tag['before']) . (isset($tag['after']) ? ', ' . javaScriptEscape($tag['after']) : '') . ');
- },
- ' . javaScriptEscape($tag['description']) . ',
- null,
- [' . javaScriptEscape($tag['before']) . (isset($tag['after']) ? ', ' . javaScriptEscape($tag['after']) : '') . ']
- );';
- }
- }
- }
- else
- {
- $context['bbc_toolbar'][$row][] = implode(',', $tagsRow);
- $tagsRow = array();
- }
- }
- if ($row == 0)
- {
- $context['bbc_toolbar'][$row][] = implode(',', $tagsRow);
- $tagsRow = array();
- if (!isset($context['disabled_tags']['font']))
- $tagsRow[] = 'font';
- if (!isset($context['disabled_tags']['size']))
- $tagsRow[] = 'size';
- if (!isset($context['disabled_tags']['color']))
- $tagsRow[] = 'color';
- }
- elseif ($row == 1 && empty($modSettings['disable_wysiwyg']))
- {
- $tmp = array();
- $tagsRow[] = 'removeformat';
- $tagsRow[] = 'source';
- if (!empty($tmp))
- {
- $tagsRow[] = '|' . implode(',', $tmp);
- }
- }
- if (!empty($tagsRow))
- $context['bbc_toolbar'][$row][] = implode(',', $tagsRow);
- }
- if (!empty($bbcodes_styles))
- $context['html_headers'] .= '
- <style type="text/css">' . $bbcodes_styles . '
- </style>';
- }
- // Initialize smiley array... if not loaded before.
- if (empty($context['smileys']) && empty($editorOptions['disable_smiley_box']))
- {
- $context['smileys'] = array(
- 'postform' => array(),
- 'popup' => array(),
- );
- // Load smileys - don't bother to run a query if we're not using the database's ones anyhow.
- if (empty($modSettings['smiley_enable']) && $user_info['smiley_set'] != 'none')
- $context['smileys']['postform'][] = array(
- 'smileys' => array(
- array(
- 'code' => ':)',
- 'filename' => 'smiley.gif',
- 'description' => $txt['icon_smiley'],
- ),
- array(
- 'code' => ';)',
- 'filename' => 'wink.gif',
- 'description' => $txt['icon_wink'],
- ),
- array(
- 'code' => ':D',
- 'filename' => 'cheesy.gif',
- 'description' => $txt['icon_cheesy'],
- ),
- array(
- 'code' => ';D',
- 'filename' => 'grin.gif',
- 'description' => $txt['icon_grin']
- ),
- array(
- 'code' => '>:(',
- 'filename' => 'angry.gif',
- 'description' => $txt['icon_angry'],
- ),
- array(
- 'code' => ':(',
- 'filename' => 'sad.gif',
- 'description' => $txt['icon_sad'],
- ),
- array(
- 'code' => ':o',
- 'filename' => 'shocked.gif',
- 'description' => $txt['icon_shocked'],
- ),
- array(
- 'code' => '8)',
- 'filename' => 'cool.gif',
- 'description' => $txt['icon_cool'],
- ),
- array(
- 'code' => '???',
- 'filename' => 'huh.gif',
- 'description' => $txt['icon_huh'],
- ),
- array(
- 'code' => '::)',
- 'filename' => 'rolleyes.gif',
- 'description' => $txt['icon_rolleyes'],
- ),
- array(
- 'code' => ':P',
- 'filename' => 'tongue.gif',
- 'description' => $txt['icon_tongue'],
- ),
- array(
- 'code' => ':-[',
- 'filename' => 'embarrassed.gif',
- 'description' => $txt['icon_embarrassed'],
- ),
- array(
- 'code' => ':-X',
- 'filename' => 'lipsrsealed.gif',
- 'description' => $txt['icon_lips'],
- ),
- array(
- 'code' => ':-\\',
- 'filename' => 'undecided.gif',
- 'description' => $txt['icon_undecided'],
- ),
- array(
- 'code' => ':-*',
- 'filename' => 'kiss.gif',
- 'description' => $txt['icon_kiss'],
- ),
- array(
- 'code' => ':\'(',
- 'filename' => 'cry.gif',
- 'description' => $txt['icon_cry'],
- 'isLast' => true,
- ),
- ),
- 'isLast' => true,
- );
- elseif ($user_info['smiley_set'] != 'none')
- {
- if (($temp = cache_get_data('posting_smileys', 480)) == null)
- {
- $request = $smcFunc['db_query']('', '
- SELECT code, filename, description, smiley_row, hidden
- FROM {db_prefix}smileys
- WHERE hidden IN (0, 2)
- ORDER BY smiley_row, smiley_order',
- array(
- )
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- $row['filename'] = htmlspecialchars($row['filename']);
- $row['description'] = htmlspecialchars($row['description']);
- $context['smileys'][empty($row['hidden']) ? 'postform' : 'popup'][$row['smiley_row']]['smileys'][] = $row;
- }
- $smcFunc['db_free_result']($request);
- foreach ($context['smileys'] as $section => $smileyRows)
- {
- foreach ($smileyRows as $rowIndex => $smileys)
- $context['smileys'][$section][$rowIndex]['smileys'][count($smileys['smileys']) - 1]['isLast'] = true;
- if (!empty($smileyRows))
- $context['smileys'][$section][count($smileyRows) - 1]['isLast'] = true;
- }
- cache_put_data('posting_smileys', $context['smileys'], 480);
- }
- else
- $context['smileys'] = $temp;
- }
- }
- // Set a flag so the sub template knows what to do...
- $context['show_bbc'] = !empty($modSettings['enableBBC']) && !empty($settings['show_bbc']);
- // Switch the URLs back... now we're back to whatever the main sub template is. (like folder in PersonalMessage.)
- if (isset($settings['use_default_images']) && $settings['use_default_images'] == 'defaults' && isset($settings['default_template']))
- {
- $settings['theme_url'] = $temp1;
- $settings['images_url'] = $temp2;
- $settings['theme_dir'] = $temp3;
- }
- }
- /**
- * Create a anti-bot verification control?
- * @param array &$verificationOptions
- * @param bool $do_test = false
- */
- function create_control_verification(&$verificationOptions, $do_test = false)
- {
- global $txt, $modSettings, $options, $smcFunc;
- global $context, $settings, $user_info, $sourcedir, $scripturl;
- // First verification means we need to set up some bits...
- if (empty($context['controls']['verification']))
- {
- // The template
- loadTemplate('GenericControls');
- // Some javascript ma'am?
- if (!empty($verificationOptions['override_visual']) || (!empty($modSettings['visual_verification_type']) && !isset($verificationOptions['override_visual'])))
- $context['html_headers'] .= '
- <script type="text/javascript" src="' . $settings['default_theme_url'] . '/scripts/captcha.js"></script>';
- $context['use_graphic_library'] = in_array('gd', get_loaded_extensions());
- // Skip I, J, L, O, Q, S and Z.
- $context['standard_captcha_range'] = array_merge(range('A', 'H'), array('K', 'M', 'N', 'P', 'R'), range('T', 'Y'));
- }
- // Always have an ID.
- assert(isset($verificationOptions['id']));
- $isNew = !isset($context['controls']['verification'][$verificationOptions['id']]);
- // Log this into our collection.
- if ($isNew)
- $context['controls']['verification'][$verificationOptions['id']] = array(
- 'id' => $verificationOptions['id'],
- 'show_visual' => !empty($verificationOptions['override_visual']) || (!empty($modSettings['visual_verification_type']) && !isset($verificationOptions['override_visual'])),
- 'number_questions' => isset($verificationOptions['override_qs']) ? $verificationOptions['override_qs'] : (!empty($modSettings['qa_verification_number']) ? $modSettings['qa_verification_number'] : 0),
- 'max_errors' => isset($verificationOptions['max_errors']) ? $verificationOptions['max_errors'] : 3,
- 'image_href' => $scripturl . '?action=verificationcode;vid=' . $verificationOptions['id'] . ';rand=' . md5(mt_rand()),
- 'text_value' => '',
- 'questions' => array(),
- );
- $thisVerification = &$context['controls']['verification'][$verificationOptions['id']];
- // Add javascript for the object.
- if ($context['controls']['verification'][$verificationOptions['id']]['show_visual'] && !WIRELESS)
- $context['insert_after_template'] .= '
- <script type="text/javascript"><!-- // --><![CDATA[
- var verification' . $verificationOptions['id'] . 'Handle = new smfCaptcha("' . $thisVerification['image_href'] . '", "' . $verificationOptions['id'] . '", ' . ($context['use_graphic_library'] ? 1 : 0) . ');
- // ]]></script>';
- // Is there actually going to be anything?
- if (empty($thisVerification['show_visual']) && empty($thisVerification['number_questions']))
- return false;
- elseif (!$isNew && !$do_test)
- return true;
- // If we want questions do we have a cache of all the IDs?
- if (!empty($thisVerification['number_questions']) && empty($modSettings['question_id_cache']))
- {
- if (($modSettings['question_id_cache'] = cache_get_data('verificationQuestionIds', 300)) == null)
- {
- $request = $smcFunc['db_query']('', '
- SELECT id_comment
- FROM {db_prefix}log_comments
- WHERE comment_type = {string:ver_test}',
- array(
- 'ver_test' => 'ver_test',
- )
- );
- $modSettings['question_id_cache'] = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- $modSettings['question_id_cache'][] = $row['id_comment'];
- $smcFunc['db_free_result']($request);
- if (!empty($modSettings['cache_enable']))
- cache_put_data('verificationQuestionIds', $modSettings['question_id_cache'], 300);
- }
- }
- if (!isset($_SESSION[$verificationOptions['id'] . '_vv']))
- $_SESSION[$verificationOptions['id'] . '_vv'] = array();
- // Do we need to refresh the verification?
- 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']))
- $force_refresh = true;
- else
- $force_refresh = false;
- // This can also force a fresh, although unlikely.
- if (($thisVerification['show_visual'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['code'])) || ($thisVerification['number_questions'] && empty($_SESSION[$verificationOptions['id'] . '_vv']['q'])))
- $force_refresh = true;
- $verification_errors = array();
- // Start with any testing.
- if ($do_test)
- {
- // This cannot happen!
- if (!isset($_SESSION[$verificationOptions['id'] . '_vv']['count']))
- fatal_lang_error('no_access', false);
- // ... nor this!
- if ($thisVerification['number_questions'] && (!isset($_SESSION[$verificationOptions['id'] . '_vv']['q']) || !isset($_REQUEST[$verificationOptions['id'] . '_vv']['q'])))
- fatal_lang_error('no_access', false);
- 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']))
- $verification_errors[] = 'wrong_verification_code';
- if ($thisVerification['number_questions'])
- {
- // Get the answers and see if they are all right!
- $request = $smcFunc['db_query']('', '
- SELECT id_comment, recipient_name AS answer
- FROM {db_prefix}log_comments
- WHERE comment_type = {string:ver_test}
- AND id_comment IN ({array_int:comment_ids})',
- array(
- 'ver_test' => 'ver_test',
- 'comment_ids' => $_SESSION[$verificationOptions['id'] . '_vv']['q'],
- )
- );
- $incorrectQuestions = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- 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']))
- $incorrectQuestions[] = $row['id_comment'];
- }
- $smcFunc['db_free_result']($request);
- if (!empty($incorrectQuestions))
- $verification_errors[] = 'wrong_verification_answer';
- }
- }
- // Any errors means we refresh potentially.
- if (!empty($verification_errors))
- {
- if (empty($_SESSION[$verificationOptions['id'] . '_vv']['errors']))
- $_SESSION[$verificationOptions['id'] . '_vv']['errors'] = 0;
- // Too many errors?
- elseif ($_SESSION[$verificationOptions['id'] . '_vv']['errors'] > $thisVerification['max_errors'])
- $force_refresh = true;
- // Keep a track of these.
- $_SESSION[$verificationOptions['id'] . '_vv']['errors']++;
- }
- // Are we refreshing then?
- if ($force_refresh)
- {
- // Assume nothing went before.
- $_SESSION[$verificationOptions['id'] . '_vv']['count'] = 0;
- $_SESSION[$verificationOptions['id'] . '_vv']['errors'] = 0;
- $_SESSION[$verificationOptions['id'] . '_vv']['did_pass'] = false;
- $_SESSION[$verificationOptions['id'] . '_vv']['q'] = array();
- $_SESSION[$verificationOptions['id'] . '_vv']['code'] = '';
- // Generating a new image.
- if ($thisVerification['show_visual'])
- {
- // Are we overriding the range?
- $character_range = !empty($verificationOptions['override_range']) ? $verificationOptions['override_range'] : $context['standard_captcha_range'];
- for ($i = 0; $i < 6; $i++)
- $_SESSION[$verificationOptions['id'] . '_vv']['code'] .= $character_range[array_rand($character_range)];
- }
- // Getting some new questions?
- if ($thisVerification['number_questions'])
- {
- // Pick some random IDs
- $questionIDs = array();
- if ($thisVerification['number_questions'] == 1)
- $questionIDs[] = $modSettings['question_id_cache'][array_rand($modSettings['question_id_cache'], $thisVerification['number_questions'])];
- else
- foreach (array_rand($modSettings['question_id_cache'], $thisVerification['number_questions']) as $index)
- $questionIDs[] = $modSettings['question_id_cache'][$index];
- }
- }
- else
- {
- // Same questions as before.
- $questionIDs = !empty($_SESSION[$verificationOptions['id'] . '_vv']['q']) ? $_SESSION[$verificationOptions['id'] . '_vv']['q'] : array();
- $thisVerification['text_value'] = !empty($_REQUEST[$verificationOptions['id'] . '_vv']['code']) ? $smcFunc['htmlspecialchars']($_REQUEST[$verificationOptions['id'] . '_vv']['code']) : '';
- }
- // Have we got some questions to load?
- if (!empty($questionIDs))
- {
- $request = $smcFunc['db_query']('', '
- SELECT id_comment, body AS question
- FROM {db_prefix}log_comments
- WHERE comment_type = {string:ver_test}
- AND id_comment IN ({array_int:comment_ids})',
- array(
- 'ver_test' => 'ver_test',
- 'comment_ids' => $questionIDs,
- )
- );
- $_SESSION[$verificationOptions['id'] . '_vv']['q'] = array();
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- $thisVerification['questions'][] = array(
- 'id' => $row['id_comment'],
- 'q' => parse_bbc($row['question']),
- 'is_error' => !empty($incorrectQuestions) && in_array($row['id_comment'], $incorrectQuestions),
- // Remember a previous submission?
- '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']]) : '',
- );
- $_SESSION[$verificationOptions['id'] . '_vv']['q'][] = $row['id_comment'];
- }
- $smcFunc['db_free_result']($request);
- }
- $_SESSION[$verificationOptions['id'] . '_vv']['count'] = empty($_SESSION[$verificationOptions['id'] . '_vv']['count']) ? 1 : $_SESSION[$verificationOptions['id'] . '_vv']['count'] + 1;
- // Return errors if we have them.
- if (!empty($verification_errors))
- return $verification_errors;
- // If we had a test that one, make a note.
- elseif ($do_test)
- $_SESSION[$verificationOptions['id'] . '_vv']['did_pass'] = true;
- // Say that everything went well chaps.
- return true;
- }
- /**
- * This keeps track of all registered handling functions for auto suggest functionality and passes execution to them.
- * @param bool $checkRegistered = null
- */
- function AutoSuggestHandler($checkRegistered = null)
- {
- global $context;
- // These are all registered types.
- $searchTypes = array(
- 'member' => 'Member',
- 'versions' => 'SMFVersions',
- );
- // If we're just checking the callback function is registered return true or false.
- if ($checkRegistered != null)
- return isset($searchTypes[$checkRegistered]) && function_exists('AutoSuggest_Search_' . $checkRegistered);
- checkSession('get');
- loadTemplate('Xml');
- // Any parameters?
- $context['search_param'] = isset($_REQUEST['search_param']) ? unserialize(base64_decode($_REQUEST['search_param'])) : array();
- if (isset($_REQUEST['suggest_type'], $_REQUEST['search']) && isset($searchTypes[$_REQUEST['suggest_type']]))
- {
- $function = 'AutoSuggest_Search_' . $searchTypes[$_REQUEST['suggest_type']];
- $context['sub_template'] = 'generic_xml';
- $context['xml_data'] = $function();
- }
- }
- /**
- * Search for a member - by real_name or member_name by default.
- *
- * @return string
- */
- function AutoSuggest_Search_Member()
- {
- global $user_info, $txt, $smcFunc, $context;
- $_REQUEST['search'] = trim($smcFunc['strtolower']($_REQUEST['search'])) . '*';
- $_REQUEST['search'] = strtr($_REQUEST['search'], array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_', '&' => '&'));
- // Find the member.
- $request = $smcFunc['db_query']('', '
- SELECT id_member, real_name
- FROM {db_prefix}members
- WHERE real_name LIKE {string:search}' . (!empty($context['search_param']['buddies']) ? '
- AND id_member IN ({array_int:buddy_list})' : '') . '
- AND is_activated IN (1, 11)
- LIMIT ' . ($smcFunc['strlen']($_REQUEST['search']) <= 2 ? '100' : '800'),
- array(
- 'buddy_list' => $user_info['buddies'],
- 'search' => $_REQUEST['search'],
- )
- );
- $xml_data = array(
- 'items' => array(
- 'identifier' => 'item',
- 'children' => array(),
- ),
- );
- while ($row = $smcFunc['db_fetch_assoc']($request))
- {
- $row['real_name'] = strtr($row['real_name'], array('&' => '&', '<' => '<', '>' => '>', '"' => '"'));
- $xml_data['items']['children'][] = array(
- 'attributes' => array(
- 'id' => $row['id_member'],
- ),
- 'value' => $row['real_name'],
- );
- }
- $smcFunc['db_free_result']($request);
- return $xml_data;
- }
- function AutoSuggest_Search_SMFVersions()
- {
- $xml_data = array(
- 'items' => array(
- 'identifier' => 'item',
- 'children' => array(),
- ),
- );
- $versions = array(
- 'SMF 1.1',
- 'SMF 1.1.1',
- 'SMF 1.1.2',
- 'SMF 1.1.3',
- 'SMF 1.1.4',
- 'SMF 1.1.5',
- 'SMF 1.1.6',
- 'SMF 1.1.7',
- 'SMF 1.1.8',
- 'SMF 1.1.9',
- 'SMF 1.1.10',
- 'SMF 1.1.11',
- 'SMF 1.1.12',
- 'SMF 1.1.13',
- 'SMF 1.1.14',
- 'SMF 1.1.15',
- 'SMF 1.1.16',
- 'SMF 2.0 beta 1',
- 'SMF 2.0 beta 1.2',
- 'SMF 2.0 beta 2',
- 'SMF 2.0 beta 3',
- 'SMF 2.0 RC 1',
- 'SMF 2.0 RC 1.2',
- 'SMF 2.0 RC 2',
- 'SMF 2.0 RC 3',
- 'SMF 2.0',
- 'SMF 2.0.1',
- 'SMF 2.0.2',
- );
- foreach ($versions as $id => $version)
- if (strpos($version, strtoupper($_REQUEST['search'])) !== false)
- $xml_data['items']['children'][] = array(
- 'attributes' => array(
- 'id' => $id,
- ),
- 'value' => $version,
- );
- return $xml_data;
- }
- ?>
|