Esempio n. 1
0
function xthreads_uninstall()
{
    global $db, $cache, $mybb, $plugins;
    if ($mybb->input['no']) {
        admin_redirect(xthreads_admin_url('config', 'plugins'));
        exit;
    }
    if (!$mybb->input['confirm_uninstall']) {
        $link = 'index.php?confirm_uninstall=1&' . htmlspecialchars($_SERVER['QUERY_STRING']);
        $GLOBALS['page']->output_confirm_action($link, $GLOBALS['lang']->xthreads_confirm_uninstall);
        exit;
    } else {
        unset($mybb->input['confirm_uninstall']);
    }
    $plugins->run_hooks('xthreads_uninstall_start');
    $query = $db->simple_select('adminoptions', 'uid,permissions');
    while ($adminopt = $db->fetch_array($query)) {
        $perms = @unserialize($adminopt['permissions']);
        if (empty($perms)) {
            continue;
        }
        // inherited or just messed up
        unset($perms['config']['threadfields']);
        $db->update_query('adminoptions', array('permissions' => $db->escape_string(serialize($perms))), 'uid=' . $adminopt['uid']);
    }
    $db->free_result($query);
    if ($db->table_exists('threadfields_data')) {
        $db->write_query('DROP TABLE ' . $db->table_prefix . 'threadfields_data');
    }
    if ($db->table_exists('threadfields')) {
        $db->write_query('DROP TABLE ' . $db->table_prefix . 'threadfields');
    }
    if ($db->table_exists('xtattachments')) {
        // remove attachments first
        require_once MYBB_ROOT . 'inc/xthreads/xt_updatehooks.php';
        $query = $db->simple_select('xtattachments', 'aid,indir,attachname');
        while ($xta = $db->fetch_array($query)) {
            xthreads_rm_attach_fs($xta);
        }
        $db->free_result($query);
        $db->write_query('DROP TABLE ' . $db->table_prefix . 'xtattachments');
    }
    // remove any indexes added on the threads table
    foreach (array('uid', 'lastposteruid', 'prefix', 'icon') as $afe) {
        if ($afe == 'uid') {
            continue;
        }
        // we won't remove this from the above array
        $db->write_query('ALTER TABLE `' . $db->table_prefix . 'threads` DROP KEY `xthreads_' . $afe . '`', true);
    }
    $fields = array('xthreads_grouping', 'xthreads_firstpostattop', 'xthreads_inlinesearch', 'xthreads_tplprefix', 'xthreads_langprefix', 'xthreads_allow_blankmsg', 'xthreads_nostatcount', 'xthreads_fdcolspan_offset', 'xthreads_settingoverrides', 'xthreads_postsperpage', 'xthreads_hideforum', 'xthreads_hidebreadcrumb', 'xthreads_defaultfilter', 'xthreads_addfiltenable', 'xthreads_wol_announcements', 'xthreads_wol_forumdisplay', 'xthreads_wol_newthread', 'xthreads_wol_attachment', 'xthreads_wol_newreply', 'xthreads_wol_showthread');
    foreach ($fields as $k => &$f) {
        if (!$db->field_exists($f, 'forums')) {
            unset($fields[$k]);
        }
    }
    if (!empty($fields)) {
        switch ($db->type) {
            case 'sqlite3':
            case 'sqlite2':
            case 'sqlite':
                $db->alter_table_parse($db->table_prefix . 'forums', 'DROP ' . implode(', DROP COLUMN ', $fields) . '');
                break;
            case 'pgsql':
                foreach ($fields as &$f) {
                    $db->write_query('ALTER TABLE ' . $db->table_prefix . 'forums
						DROP COLUMN ' . $f);
                }
                break;
            default:
                $db->write_query('ALTER TABLE ' . $db->table_prefix . 'forums
					DROP COLUMN ' . implode(', DROP COLUMN ', $fields));
        }
    }
    // remove any custom default sorts and reduce size of sorting column back to original
    $db->update_query('forums', array('defaultsortby' => ''), 'defaultsortby LIKE "tf_%" OR defaultsortby LIKE "tfa_%"');
    $db->write_query('ALTER TABLE `' . $db->table_prefix . 'forums` MODIFY `defaultsortby` varchar(10) NOT NULL default \'\'');
    $cache->update_forums();
    xthreads_delete_datacache('threadfields');
    @unlink(MYBB_ROOT . 'cache/xthreads.php');
    @unlink(MYBB_ROOT . 'cache/xthreads_evalcache.php');
    $db->delete_query('templates', 'title IN ("' . implode('","', array_keys(xthreads_new_templates())) . '") AND sid=-2');
    // revert QuickThread modification
    if (function_exists('quickthread_uninstall')) {
        $tpl = $db->fetch_array($db->simple_select('templates', 'tid,template', 'title="forumdisplay_quick_thread" AND sid=-1', array('limit' => 1)));
        if ($tpl && strpos($tpl['template'], '{$GLOBALS[\'extra_threadfields\']}') !== false) {
            $newtpl = preg_replace('~\\{\\$GLOBALS\\[\'extra_threadfields\'\\]\\}' . "\r?(\n\t{0,3})?" . '~is', '', $tpl['template'], 1);
            if ($newtpl != $tpl['template']) {
                $db->update_query('templates', array('template' => $db->escape_string($newtpl)), 'tid=' . $tpl['tid']);
            }
        }
    }
    // try to determine and remove stuff added to the custom moderation table
    $query = $db->simple_select('modtools', 'tid,threadoptions');
    while ($tool = $db->fetch_array($query)) {
        $opts = unserialize($tool['threadoptions']);
        if (isset($opts['edit_threadfields'])) {
            unset($opts['edit_threadfields']);
            $db->update_query('modtools', array('threadoptions' => $db->escape_string(serialize($opts))), 'tid=' . $tool['tid']);
        }
    }
    $plugins->run_hooks('xthreads_uninstall_end');
}
Esempio n. 2
0
function threadfields_add_edit_handler(&$tf, $update)
{
    global $mybb, $page, $lang, $db, $plugins, $sub_tabs;
    global $form;
    if ($update) {
        $title = $lang->edit_threadfield;
    } else {
        $title = $lang->add_threadfield;
    }
    $props = xthreads_threadfields_props();
    if ($mybb->request_method == 'post') {
        foreach ($props as $field => &$prop) {
            if ($field == 'field') {
                $field = 'newfield';
            }
            // cause you can't "continue" in a switch statement, lol...
            if ($field == 'forums' || $field == 'editable_gids' || $field == 'viewable_gids' || $field == 'filemaxsize' || $field == 'multival') {
                continue;
            }
            if ($prop['datatype'] == 'string') {
                $mybb->input[$field] = trim($mybb->input[$field]);
            } else {
                $mybb->input[$field] = (int) $mybb->input[$field];
            }
        }
        $mybb->input['textmask'] = str_replace("", '', $mybb->input['textmask']);
        $mybb->input['filemaxsize'] = xthreads_size_to_bytes($mybb->input['filemaxsize']);
        $mybb->input['fileimage_mindim'] = strtolower(trim($mybb->input['fileimage_mindim']));
        $mybb->input['fileimage_maxdim'] = strtolower(trim($mybb->input['fileimage_maxdim']));
        if (!xthreads_empty($mybb->input['formatmap'])) {
            $fm = array();
            $fms = str_replace("{\n}", "\r", str_replace("\r", '', $mybb->input['formatmap']));
            foreach (explode("\n", $fms) as $map) {
                $map = str_replace("\r", "\n", $map);
                $p = strpos($map, '{|}');
                if (!$p) {
                    continue;
                }
                // can't be zero index either - blank display format used for that
                $fmkey = substr($map, 0, $p);
                if (isset($fm[$fmkey])) {
                    $errors[] = $lang->sprintf($lang->error_dup_formatmap, htmlspecialchars_uni($fmkey));
                    unset($fm);
                    break;
                }
                $fm[$fmkey] = substr($map, $p + 3);
            }
            if (isset($fm)) {
                $mybb->input['formatmap'] = serialize($fm);
            }
        }
        if (is_array($mybb->input['forums'])) {
            $mybb->input['forums'] = implode(',', array_unique(array_map('intval', array_map('trim', $mybb->input['forums']))));
            if (empty($mybb->input['forums'])) {
                $mybb->input['forums'] = '';
            }
        } else {
            $mybb->input['forums'] = trim($mybb->input['forums']);
            if ($mybb->input['forums']) {
                $mybb->input['forums'] = implode(',', array_unique(array_map('intval', array_map('trim', explode(',', $mybb->input['forums'])))));
            }
            if (!$mybb->input['forums']) {
                $mybb->input['forums'] = '';
            }
        }
        if ($mybb->input['editable'] == '99') {
            if (is_array($mybb->input['editable_gids'])) {
                $mybb->input['editable_gids'] = implode(',', array_unique(array_map('intval', array_map('trim', $mybb->input['editable_gids']))));
                if (empty($mybb->input['editable_gids'])) {
                    $mybb->input['editable_gids'] = '';
                }
            } else {
                $mybb->input['editable_gids'] = trim($mybb->input['editable_gids']);
                if ($mybb->input['editable_gids']) {
                    $mybb->input['editable_gids'] = implode(',', array_unique(array_map('intval', array_map('trim', explode(',', $mybb->input['editable_gids'])))));
                }
                if (!$mybb->input['editable_gids']) {
                    $mybb->input['editable_gids'] = '';
                }
            }
            if ($mybb->input['editable_gids']) {
                $mybb->input['editable'] = 0;
            } else {
                $mybb->input['editable'] = XTHREADS_EDITABLE_NONE;
            }
            // no group ids selected
        } else {
            $mybb->input['editable'] = min_max((int) $mybb->input['editable'], XTHREADS_EDITABLE_ALL, XTHREADS_EDITABLE_NONE);
            $mybb->input['editable_gids'] = '';
        }
        $mybb->input['hidefield'] = 0;
        foreach (array('input' => XTHREADS_HIDE_INPUT, 'thread' => XTHREADS_HIDE_THREAD) as $k => $v) {
            if ($mybb->input['hidefield_' . $k]) {
                $mybb->input['hidefield'] |= $v;
            }
        }
        if (!xthreads_empty($mybb->input['editable_values'])) {
            $ev = array();
            $evs = str_replace("{\n}", "\r", str_replace("\r", '', $mybb->input['editable_values']));
            foreach (explode("\n", $evs) as $editable_value) {
                $editable_value = str_replace("\r", "\n", $editable_value);
                $p = strpos($editable_value, '{|}');
                if ($p === false) {
                    continue;
                }
                $evkey = substr($editable_value, 0, $p);
                if (isset($ev[$evkey])) {
                    $errors[] = $lang->sprintf($lang->error_dup_editable_value, htmlspecialchars_uni($evkey));
                    unset($ev);
                    break;
                }
                $ev[$evkey] = array_unique(array_map('intval', explode(',', substr($editable_value, $p + 3))));
                // remove '0' element
                if (($zerorm = array_search(0, $ev[$evkey])) !== false) {
                    unset($ev[$evkey][$zerorm]);
                }
            }
            if (isset($ev)) {
                $mybb->input['editable_values'] = serialize($ev);
            }
        }
        if (is_array($mybb->input['viewable_gids'])) {
            $mybb->input['viewable_gids'] = implode(',', array_unique(array_map('intval', array_map('trim', $mybb->input['viewable_gids']))));
            if (empty($mybb->input['viewable_gids'])) {
                $mybb->input['viewable_gids'] = '';
            }
        } else {
            $mybb->input['viewable_gids'] = trim($mybb->input['viewable_gids']);
            if ($mybb->input['viewable_gids']) {
                $mybb->input['viewable_gids'] = implode(',', array_unique(array_map('intval', array_map('trim', explode(',', $mybb->input['viewable_gids'])))));
            }
            if (!$mybb->input['viewable_gids']) {
                $mybb->input['viewable_gids'] = '';
            }
        }
        $mybb->input['sanitize'] = min_max((int) $mybb->input['sanitize'], XTHREADS_SANITIZE_HTML, XTHREADS_SANITIZE_NONE);
        //if($mybb->input['sanitize'] == XTHREADS_SANITIZE_PARSER) {
        $parser_opts = array('parser_nl2br' => XTHREADS_SANITIZE_PARSER_NL2BR, 'parser_nobadw' => XTHREADS_SANITIZE_PARSER_NOBADW, 'parser_html' => XTHREADS_SANITIZE_PARSER_HTML, 'parser_mycode' => XTHREADS_SANITIZE_PARSER_MYCODE, 'parser_mycodeimg' => XTHREADS_SANITIZE_PARSER_MYCODEIMG, 'parser_mycodevid' => XTHREADS_SANITIZE_PARSER_VIDEOCODE, 'parser_smilies' => XTHREADS_SANITIZE_PARSER_SMILIES);
        foreach ($parser_opts as $opt => $n) {
            if ($mybb->input[$opt]) {
                $mybb->input['sanitize'] |= $n;
            }
        }
        //}
        $mybb->input['inputtype'] = min_max((int) $mybb->input['inputtype'], XTHREADS_INPUT_TEXT, XTHREADS_INPUT_FILE_URL);
        if (xthreads_empty($mybb->input['title'])) {
            $errors[] = $lang->error_missing_title;
        }
        if (xthreads_empty($mybb->input['newfield'])) {
            $errors[] = $lang->error_missing_field;
        }
        if (!xthreads_empty($mybb->input['textmask'])) {
            // test for bad regex
            xthreads_catch_errorhandler();
            @preg_match('~' . str_replace('~', '\\~', $mybb->input['textmask']) . '~si', 'testvalue');
            restore_error_handler();
            if (!empty($GLOBALS['_previous_error'])) {
                $errmsg =& $GLOBALS['_previous_error'][1];
                if (substr($errmsg, 0, 12) == 'preg_match()') {
                    $p = strpos($errmsg, ':', 12);
                    if ($p) {
                        $errmsg = trim(substr($errmsg, $p + 1));
                    } else {
                        $errmsg = trim(substr($errmsg, 12));
                    }
                    $errors[] = $lang->sprintf($lang->error_bad_textmask, $errmsg);
                }
            }
        }
        switch ($mybb->input['inputtype']) {
            case XTHREADS_INPUT_SELECT:
            case XTHREADS_INPUT_RADIO:
            case XTHREADS_INPUT_CHECKBOX:
                $mybb->input['sanitize'] = $mybb->input['inputtype'] == XTHREADS_INPUT_SELECT ? XTHREADS_SANITIZE_HTML : XTHREADS_SANITIZE_NONE;
                $mybb->input['textmask'] = '';
                // must have value defined
                if (xthreads_empty($mybb->input['vallist'])) {
                    $errors[] = $lang->error_require_valllist;
                }
                break;
            case XTHREADS_INPUT_TEXTAREA:
            case XTHREADS_INPUT_FILE:
            case XTHREADS_INPUT_FILE_URL:
                $mybb->input['allowfilter'] = 0;
                $mybb->input['vallist'] = '';
                break;
            case XTHREADS_INPUT_TEXT:
                $mybb->input['vallist'] = '';
        }
        if ($mybb->input['multival_enable'] || $mybb->input['inputtype'] == XTHREADS_INPUT_CHECKBOX) {
            if (xthreads_empty($mybb->input['multival'])) {
                $errors[] = $lang->error_require_multival_delimiter;
            }
            // force textual datatype
            if ($mybb->input['datatype'] !== XTHREADS_DATATYPE_TEXT) {
                $mybb->input['datatype'] = XTHREADS_DATATYPE_TEXT;
            }
        } else {
            $mybb->input['multival'] = '';
        }
        if ($mybb->input['use_formhtml']) {
            if (xthreads_empty($mybb->input['formhtml'])) {
                $errors[] = $lang->error_require_formhtml;
            }
        } else {
            $mybb->input['formhtml'] = '';
        }
        if ($mybb->input['datatype'] !== XTHREADS_DATATYPE_TEXT) {
            // verify value list if applicable
            /* if($mybb->input['inputtype'] == XTHREADS_INPUT_SELECT || $mybb->input['inputtype'] == XTHREADS_INPUT_RADIO) {
            				// maybe we won't do this...
            			} */
            $mybb->input['datatype'] = min_max($mybb->input['datatype'], XTHREADS_DATATYPE_TEXT, XTHREADS_DATATYPE_FLOAT);
        }
        $mybb->input['fileimage'] = '';
        if ($mybb->input['filereqimg']) {
            if ($mybb->input['fileimage_mindim'] && !preg_match('~^[0-9]+x[0-9]+$~', $mybb->input['fileimage_mindim'])) {
                $errors[] = $lang->error_invalid_min_dims;
            }
            if ($mybb->input['fileimage_maxdim'] && !preg_match('~^[0-9]+x[0-9]+$~', $mybb->input['fileimage_maxdim'])) {
                $errors[] = $lang->error_invalid_max_dims;
            }
            if ($mybb->input['fileimage_mindim']) {
                $mybb->input['fileimage'] = $mybb->input['fileimage_mindim'];
            } else {
                $mybb->input['fileimage'] = '0x0';
            }
            if ($mybb->input['fileimage_maxdim']) {
                $mybb->input['fileimage'] .= '|' . $mybb->input['fileimage_maxdim'];
            }
        }
        //if($mybb->input['fileimgthumbs']) {
        // TODO: verify format
        //if(!preg_match('~^[0-9]+x[0-9]+(\\|[0-9]+x[0-9]+)*$~', $mybb->input['fileimgthumbs']))
        //	$errors[] = $lang->error_invalid_thumb_dims;
        //}
        if ($update) {
            // check that sent field name is valid
            // and whilst we're here, check for bad conversions (eg file -> textbox)
            $oldfield = $db->fetch_array($db->simple_select('threadfields', '*', 'field="' . $db->escape_string($mybb->input['field']) . '"'));
            if (empty($oldfield)) {
                $errors[] = $lang->error_bad_old_field;
            } else {
                switch ($oldfield['inputtype']) {
                    case XTHREADS_INPUT_FILE:
                    case XTHREADS_INPUT_FILE_URL:
                        if ($oldfield['inputtype'] != $mybb->input['inputtype']) {
                            $errors['error_invalid_inputtype'] = $lang->error_invalid_inputtype;
                        }
                        break;
                    default:
                        if ($mybb->input['inputtype'] == XTHREADS_INPUT_FILE || $mybb->input['inputtype'] == XTHREADS_INPUT_FILE_URL) {
                            $errors['error_invalid_inputtype'] = $lang->error_invalid_inputtype;
                        }
                }
            }
        }
        if (!xthreads_empty($mybb->input['newfield'])) {
            if ($mybb->input['newfield'] == 'tid') {
                $errors[] = $lang->error_field_name_tid;
            } elseif (strlen($mybb->input['newfield']) > 50) {
                $errors[] = $lang->error_field_name_too_long;
            } elseif (!preg_match('~^[a-zA-Z0-9_]+$~', $mybb->input['newfield'])) {
                $errors[] = $lang->error_field_name_invalid;
            } elseif (isset($mybb->input['newfield'][2]) && $mybb->input['newfield'][0] == '_' && $mybb->input['newfield'][1] == '_') {
                // don't allow fields starting with "__" (reserved for special use)
                // in hindsight, special uses (eg filters) really should've used something like '~' so we don't need to do this, but it's too late now
                $errors[] = $lang->error_field_name_reserved;
            } elseif (!$update || $mybb->input['field'] != $mybb->input['newfield']) {
                $ftest = $db->fetch_field($db->simple_select('threadfields', 'field', 'field="' . $db->escape_string($mybb->input['newfield']) . '"'), 'field');
                if (!xthreads_empty($ftest)) {
                    $errors[] = $lang->error_field_name_in_use;
                }
            }
        }
        // check for syntax errors in conditionals
        // this is a bit tricky because we need the cache function to build the conditional for checking
        if ($update) {
            $test_tf = array_merge($oldfield, $mybb->input);
        } else {
            $test_tf = $mybb->input;
        }
        xthreads_buildtfcache_parseitem($test_tf);
        // test for bad conditional syntax
        foreach (array('defaultval', 'blankval', 'inputformat', 'inputvalidate', 'dispformat', 'dispitemformat', 'unviewableval', 'formhtml', 'formhtml_item') as $condcheck) {
            if ($test_tf[$condcheck] && !xthreads_check_evalstr($test_tf[$condcheck])) {
                if ($condcheck == 'formhtml_item') {
                    $condcheck = 'formhtml';
                }
                $tflangkey = 'threadfields_' . $condcheck;
                $errors[] = $lang->sprintf($lang->error_bad_conditional, $lang->{$tflangkey});
            }
        }
        if (!xthreads_empty($test_tf['formatmap'])) {
            foreach ($test_tf['formatmap'] as &$fm) {
                if ($fm && !xthreads_check_evalstr($fm)) {
                    $errors[] = $lang->sprintf($lang->error_bad_conditional, $lang->threadfields_formatmap);
                    break;
                }
            }
        }
        if (!xthreads_empty($test_tf['fileimgthumbs'])) {
            foreach ($test_tf['fileimgthumbs'] as $thumb => $chain) {
                if ($chain) {
                    if (!xthreads_check_evalstr('".$img->' . $chain . '."')) {
                        $errors[] = $lang->sprintf($lang->error_bad_conditional, $lang->threadfields_fileimgthumbs);
                        break;
                    }
                }
            }
        }
        if (!$errors) {
            $new_tf = array();
            foreach (array_keys($props) as $field) {
                if ($field == 'field') {
                    $new_tf[$field] = $db->escape_string($mybb->input['newfield']);
                } else {
                    $new_tf[$field] = $db->escape_string($mybb->input[$field]);
                }
            }
            if ($mybb->input['inputtype'] == XTHREADS_INPUT_FILE) {
                if (xthreads_empty($mybb->input['multival'])) {
                    $fieldtype = xthreads_db_fielddef('int', null, true) . ' not null default 0';
                } else {
                    $fieldtype = 'varchar(255) not null default ""';
                }
                // we'll stick a hard limit of 25 files
            } elseif ($mybb->input['inputtype'] == XTHREADS_INPUT_FILE_URL) {
                $fieldtype = 'varchar(255) not null default ""';
            } else {
                switch ($new_tf['datatype']) {
                    case XTHREADS_DATATYPE_INT:
                    case XTHREADS_DATATYPE_UINT:
                        $fieldtype = xthreads_db_fielddef('int', null, $new_tf['datatype'] == XTHREADS_DATATYPE_UINT) . ' default null';
                        break;
                    case XTHREADS_DATATYPE_BIGINT:
                    case XTHREADS_DATATYPE_BIGUINT:
                        $fieldtype = xthreads_db_fielddef('bigint', null, $new_tf['datatype'] == XTHREADS_DATATYPE_BIGUINT) . ' default null';
                        break;
                    case XTHREADS_DATATYPE_FLOAT:
                        $fieldtype = 'double default null';
                        break;
                    default:
                        switch ($mybb->input['inputtype']) {
                            case XTHREADS_INPUT_TEXTAREA:
                                $fieldtype = 'text not null';
                                break;
                            case XTHREADS_INPUT_SELECT:
                            case XTHREADS_INPUT_RADIO:
                                if ($new_tf['multival'] === '' || $mybb->input['inputtype'] == XTHREADS_INPUT_RADIO) {
                                    $fieldtype = 'varchar(255) not null default ""';
                                    $using_long_varchar = false;
                                    break;
                                }
                            default:
                                if ($new_tf['allowfilter']) {
                                    // initially, try 1024 chars
                                    $fieldtype = 'varchar(1024) not null default ""';
                                    $using_long_varchar = true;
                                } else {
                                    $fieldtype = 'text not null';
                                }
                        }
                }
            }
            if ($update) {
                $plugins->run_hooks('admin_config_threadfields_edit_commit');
                $db->update_query('threadfields', $new_tf, 'field="' . $db->escape_string($mybb->input['field']) . '"');
                $alterations = array();
                // TODO: perhaps only run this query if necessary
                //if($mybb->input['field'] != $mybb->input['newfield'])
                $alterfield_base = 'CHANGE `' . $db->escape_string($mybb->input['field']) . '` `' . $new_tf['field'] . '` ';
                $alterations['field'] = $alterfield_base . $fieldtype;
                if ((bool) $new_tf['allowfilter'] != (bool) $oldfield['allowfilter']) {
                    if ($new_tf['allowfilter']) {
                        $alterations['addkey'] = 'ADD KEY `' . $new_tf['field'] . '` (`' . $new_tf['field'] . '`)';
                    } else {
                        $alterations['dropkey'] = 'DROP KEY `' . $db->escape_string($mybb->input['field']) . '`';
                    }
                } elseif ($new_tf['allowfilter'] && $mybb->input['field'] != $mybb->input['newfield']) {
                    // change key name - only way to do this in MySQL appears to be recreating the key...
                    $alterations['dropkey'] = 'DROP KEY `' . $db->escape_string($mybb->input['field']) . '`';
                    $alterations['addkey'] = 'ADD KEY `' . $new_tf['field'] . '` (`' . $new_tf['field'] . '`)';
                }
                if (!empty($alterations)) {
                    $qry_base = 'ALTER TABLE `' . $db->table_prefix . 'threadfields_data` ';
                    if ($using_long_varchar) {
                        if (!$db->write_query($qry_base . implode(', ', $alterations), true)) {
                            $alterations['field'] = $alterfield_base . str_replace('varchar(1024)', 'varchar(255)', $fieldtype);
                            $db->write_query($qry_base . implode(', ', $alterations));
                        }
                    } else {
                        $db->write_query($qry_base . implode(', ', $alterations));
                    }
                    if ($mybb->input['field'] != $mybb->input['newfield'] && ($new_tf['inputtype'] == XTHREADS_INPUT_FILE || $new_tf['inputtype'] == XTHREADS_INPUT_FILE_URL)) {
                        // need to update xtattachments table too!
                        $db->update_query('xtattachments', array('field' => $new_tf['field']), 'field="' . $db->escape_string($mybb->input['field']) . '"');
                    }
                }
            } else {
                $plugins->run_hooks('admin_config_threadfields_add_commit');
                $db->insert_query('threadfields', $new_tf);
                $addkey = '';
                if ($new_tf['allowfilter']) {
                    $addkey .= ', ADD KEY (`' . $new_tf['field'] . '`)';
                }
                $qry_base = 'ALTER TABLE `' . $db->table_prefix . 'threadfields_data` ADD COLUMN `' . $new_tf['field'] . '` ';
                if ($using_long_varchar) {
                    if (!$db->write_query($qry_base . $fieldtype . $addkey, true)) {
                        $db->write_query($qry_base . str_replace('varchar(1024)', 'varchar(255)', $fieldtype) . $addkey);
                    }
                } else {
                    $db->write_query($qry_base . $fieldtype . $addkey);
                }
            }
            // Log admin action
            log_admin_action($new_tf['field'], htmlspecialchars_uni($mybb->input['title']));
            xthreads_buildtfcache();
            if ($update) {
                flash_message($lang->success_updated_threadfield, 'success');
            } else {
                flash_message($lang->success_added_threadfield, 'success');
            }
            admin_redirect(xthreads_admin_url('config', 'threadfields'));
        }
    }
    $page->add_breadcrumb_item($title);
    $page->output_header($lang->custom_threadfields . ' - ' . $title);
    echo '<noscript>';
    $page->output_alert($lang->threadfields_enable_js);
    echo '</noscript>';
    if (!$update) {
        $page->output_nav_tabs($sub_tabs, 'threadfields_add');
    }
    if ($update) {
        $form = new Form(xthreads_admin_url('config', 'threadfields') . '&amp;action=edit&amp;field=' . urlencode($tf['field']), 'post');
    } else {
        $form = new Form(xthreads_admin_url('config', 'threadfields&amp;action=add'), 'post');
    }
    if ($errors) {
        $page->output_inline_error($errors);
        $GLOBALS['data'] =& $mybb->input;
    } else {
        $GLOBALS['data'] =& $tf;
    }
    global $data;
    global $form_container;
    $form_container = new FormContainer($title);
    $form_container->output_row($lang->threadfields_title . ' <em>*</em>', $lang->threadfields_title_desc, $form->generate_text_box('title', $data['title'], array('id' => 'title')), 'title');
    if (isset($data['newfield'])) {
        $key =& $data['newfield'];
    } else {
        $key =& $data['field'];
    }
    $form_container->output_row($lang->threadfields_name . ' <em>*</em>', $lang->threadfields_name_desc, $form->generate_text_box('newfield', $key, array('id' => 'newfield')), 'newfield');
    if ($data['forums'] && !is_array($data['forums'])) {
        $data['forums'] = array_map('intval', array_map('trim', explode(',', $data['forums'])));
    }
    $form_container->output_row($lang->threadfields_forums, $lang->threadfields_forums_desc, $form->generate_forum_select('forums[]', $data['forums'], array('multiple' => true, 'size' => 5)), 'forums');
    $hidefield_boxes = '';
    foreach (array('input' => XTHREADS_HIDE_INPUT, 'thread' => XTHREADS_HIDE_THREAD) as $k => $v) {
        $l = 'threadfields_hidefield_' . $k;
        $ld = 'threadfields_hidefield_' . $k . '_desc';
        $hidefield_boxes .= $form->generate_check_box('hidefield_' . $k, '1', $lang->{$l}, array('checked' => (bool) ($data['hidefield'] & $v))) . '<div style="margin-left: 2.25em;" class="description">' . $lang->{$ld} . '</div>';
    }
    $form_container->output_row($lang->threadfields_hidefield, $lang->threadfields_hidefield_desc, $hidefield_boxes, 'hidefield');
    $inputtypes = array(XTHREADS_INPUT_TEXT => $lang->threadfields_inputtype_text, XTHREADS_INPUT_TEXTAREA => $lang->threadfields_inputtype_textarea, XTHREADS_INPUT_SELECT => $lang->threadfields_inputtype_select, XTHREADS_INPUT_RADIO => $lang->threadfields_inputtype_radio, XTHREADS_INPUT_CHECKBOX => $lang->threadfields_inputtype_checkbox, XTHREADS_INPUT_FILE => $lang->threadfields_inputtype_file);
    if ($update) {
        // disable some conversions as they are not possible
        if (isset($errors['error_invalid_inputtype'])) {
            // but if invalid type is supplied, don't lock the user in either
            $inputtype = $oldfield['inputtype'];
        } else {
            $inputtype = $data['inputtype'];
        }
        if ($inputtype == XTHREADS_INPUT_FILE || $inputtype == XTHREADS_INPUT_FILE_URL) {
            foreach ($inputtypes as $k => &$v) {
                if ($k != $inputtype) {
                    unset($inputtypes[$k]);
                }
            }
        } else {
            unset($inputtypes[XTHREADS_INPUT_FILE], $inputtypes[XTHREADS_INPUT_FILE_URL]);
        }
    }
    // TODO: weird issue where inputtype isn't being set...
    if (!ini_get('file_uploads')) {
        $lang->threadfields_file_name_info .= '<div style="color: red; font-style: italic;">' . $lang->threadfields_file_upload_disabled_warning . '</div>';
    }
    make_form_row('inputtype', 'select_box', $inputtypes, '<div id="inputtype_file_explain" style="font-size: 0.95em; margin-top: 1em;">' . $lang->threadfields_file_name_info . '</div>');
    make_form_row('disporder', 'text_box');
    $form_container->end();
    unset($GLOBALS['form_container']);
    global $form_container;
    $form_container = new FormContainer($lang->threadfields_cat_input);
    if ($data['editable_gids'] && !is_array($data['editable_gids'])) {
        $data['editable_gids'] = array_map('intval', array_map('trim', explode(',', $data['editable_gids'])));
    }
    if (!empty($data['editable_gids'])) {
        $data['editable'] = 99;
    }
    make_form_row('editable', 'select_box', array(XTHREADS_EDITABLE_ALL => $lang->threadfields_editable_everyone, XTHREADS_EDITABLE_REQ => $lang->threadfields_editable_requied, XTHREADS_EDITABLE_MOD => $lang->threadfields_editable_mod, XTHREADS_EDITABLE_ADMIN => $lang->threadfields_editable_admin, XTHREADS_EDITABLE_NONE => $lang->threadfields_editable_none, 99 => $lang->threadfields_editable_bygroup));
    $form_container->output_row($lang->threadfields_editable_gids, $lang->threadfields_editable_gids_desc, xt_generate_group_select('editable_gids[]', $data['editable_gids'], array('multiple' => true, 'size' => 5)), 'editable_gids', array(), array('id' => 'row_editable_gids'));
    make_form_row('maxlen', 'text_box');
    make_form_row('vallist', 'text_area');
    make_form_row('fileexts', 'text_box');
    if (!is_int(2147483648)) {
        // detect 32-bit PHP
        $lang->threadfields_filemaxsize_desc .= $lang->threadfields_filemaxsize_desc_2gbwarn;
    }
    // PHP upload limits
    $upload_max_filesize = @ini_get('upload_max_filesize');
    $post_max_size = @ini_get('post_max_size');
    // TODO: maybe also pull in [ file_uploads, max_file_uploads, max_input_time ] ?
    if ($upload_max_filesize || $post_max_size) {
        $lang->threadfields_filemaxsize_desc .= '<br /><br />' . $lang->threadfields_filemaxsize_desc_phplimit;
        if (!$lang->limit_upload_max_filesize) {
            $lang->load('config_attachment_types');
        }
        if ($upload_max_filesize) {
            $lang->threadfields_filemaxsize_desc .= '<br />' . $lang->sprintf($lang->limit_upload_max_filesize, $upload_max_filesize);
        }
        if ($post_max_size) {
            $lang->threadfields_filemaxsize_desc .= '<br />' . $lang->sprintf($lang->limit_post_max_size, $post_max_size);
        }
    }
    make_form_row('filemaxsize', 'text_box');
    make_form_row('filemagic', 'text_box');
    $data['filereqimg'] = $data['fileimage'] ? 1 : 0;
    if (!function_exists('imagecreate')) {
        $lang->threadfields_filereqimg_desc .= $lang->threadfields_filereqimg_desc_nogd;
    }
    make_form_row('filereqimg', 'yes_no_radio');
    unset($data['filereqimg']);
    $data['fileimage_mindim'] = $data['fileimage_maxdim'] = '';
    if ($data['fileimage']) {
        list($min, $max) = explode('|', $data['fileimage']);
        if ($min === '0x0') {
            $min = '';
        }
        $data['fileimage_mindim'] = $min;
        $data['fileimage_maxdim'] = $max;
    }
    make_form_row('fileimage_mindim', 'text_box');
    make_form_row('fileimage_maxdim', 'text_box');
    unset($data['fileimage_mindim'], $data['fileimage_maxdim']);
    make_form_row('fileimgthumbs', 'text_box');
    $data['multival_enable'] = $data['multival'] !== '' ? 1 : 0;
    make_form_row('multival_enable', 'yes_no_radio');
    unset($data['multival_enable']);
    make_form_row('multival_limit', 'text_box');
    make_form_row('textmask', 'text_box');
    make_form_row('inputformat', 'text_area', array('style' => 'font-family: monospace'));
    make_form_row('inputvalidate', 'text_area', array('style' => 'font-family: monospace'));
    if (!is_array($data['editable_values'])) {
        $ev = @unserialize($data['editable_values']);
        if (is_array($ev)) {
            $data['editable_values'] =& $ev;
        }
    }
    if (is_array($data['editable_values'])) {
        $evtxt = '';
        foreach ($data['editable_values'] as $k => &$v) {
            // don't need to htmlspecialchar - it'll be done for us
            $evtxt .= str_replace("\n", "{\n}", $k) . '{|}' . implode(',', $v) . "\n";
        }
        $data['editable_values'] =& $evtxt;
    }
    make_form_row('editable_values', 'text_area', array('style' => 'font-family: monospace'));
    $form_container->end();
    unset($GLOBALS['form_container']);
    global $form_container;
    $form_container = new FormContainer($lang->threadfields_cat_inputfield);
    make_form_row('desc', 'text_box');
    make_form_row('defaultval', 'text_area', array('style' => 'font-family: monospace'));
    make_form_row('fieldwidth', 'text_box');
    make_form_row('fieldheight', 'text_box');
    make_form_row('tabstop', 'yes_no_radio');
    $data['use_formhtml'] = $data['formhtml'] !== '' ? 1 : 0;
    make_form_row('use_formhtml', 'yes_no_radio');
    unset($data['use_formhtml']);
    $lang->threadfields_formhtml .= ' <em>*</em>';
    make_form_row('formhtml', 'text_area', array('style' => 'font-family: monospace'));
    $form_container->end();
    unset($GLOBALS['form_container']);
    global $form_container;
    $form_container = new FormContainer($lang->threadfields_cat_output);
    $sanitize = $data['sanitize'];
    $data['sanitize'] &= XTHREADS_SANITIZE_MASK;
    make_form_row('sanitize', 'select_box', array(XTHREADS_SANITIZE_HTML => $lang->threadfields_sanitize_plain, XTHREADS_SANITIZE_HTML_NL => $lang->threadfields_sanitize_plain_nl, XTHREADS_SANITIZE_PARSER => $lang->threadfields_sanitize_mycode, XTHREADS_SANITIZE_NONE => $lang->threadfields_sanitize_none));
    $parser_opts = array('parser_nl2br' => $sanitize & XTHREADS_SANITIZE_PARSER_NL2BR, 'parser_nobadw' => $sanitize & XTHREADS_SANITIZE_PARSER_NOBADW, 'parser_html' => $sanitize & XTHREADS_SANITIZE_PARSER_HTML, 'parser_mycode' => $sanitize & XTHREADS_SANITIZE_PARSER_MYCODE, 'parser_mycodeimg' => $sanitize & XTHREADS_SANITIZE_PARSER_MYCODEIMG, 'parser_mycodevid' => $sanitize & XTHREADS_SANITIZE_PARSER_VIDEOCODE, 'parser_smilies' => $sanitize & XTHREADS_SANITIZE_PARSER_SMILIES);
    if ($mybb->version_code < 1600) {
        unset($parser_opts['parser_mycodevid']);
    }
    $parser_opts_str = '';
    foreach ($parser_opts as $opt => $checked) {
        $langstr = 'threadfields_sanitize_' . $opt;
        $parser_opts_str .= '<div style="display: block;">' . $form->generate_check_box($opt, 1, $lang->{$langstr}, array('checked' => $checked ? 1 : 0)) . '</div>';
    }
    $form_container->output_row($lang->threadfields_sanitize_parser, $lang->threadfields_sanitize_parser_desc, $parser_opts_str, 'sanitize_parser', array(), array('id' => 'parser_opts'));
    make_form_row('blankval', 'text_area', array('style' => 'font-family: monospace'));
    make_form_row('dispformat', 'text_area', array('style' => 'font-family: monospace'));
    $lang->threadfields_multival .= ' <em>*</em>';
    make_form_row('multival', 'text_box');
    $lang->threadfields_multival = substr($lang->threadfields_multival, 0, -11);
    make_form_row('dispitemformat', 'text_area', array('style' => 'font-family: monospace'));
    if (!is_array($data['formatmap'])) {
        $fm = @unserialize($data['formatmap']);
        if (is_array($fm)) {
            $data['formatmap'] =& $fm;
        }
    }
    if (is_array($data['formatmap'])) {
        $fmtxt = '';
        foreach ($data['formatmap'] as $k => &$v) {
            // don't need to htmlspecialchar - it'll be done for us
            $fmtxt .= str_replace("\n", "{\n}", $k . '{|}' . $v) . "\n";
        }
        $data['formatmap'] =& $fmtxt;
    }
    make_form_row('formatmap', 'text_area', array('style' => 'font-family: monospace'));
    if ($data['viewable_gids'] && !is_array($data['viewable_gids'])) {
        $data['viewable_gids'] = array_map('intval', array_map('trim', explode(',', $data['viewable_gids'])));
    }
    $form_container->output_row($lang->threadfields_viewable_gids, $lang->threadfields_viewable_gids_desc, xt_generate_group_select('viewable_gids[]', $data['viewable_gids'], array('multiple' => true, 'size' => 5, 'id' => 'viewable_gids')), 'viewable_gids', array(), array('id' => 'row_viewable_gids'));
    make_form_row('unviewableval', 'text_area', array('style' => 'font-family: monospace'));
    $form_container->end();
    unset($GLOBALS['form_container']);
    // this will currently be empty if a file input is chosen...
    global $form_container;
    $form_container = new FormContainer($lang->threadfields_cat_misc);
    make_form_row('allowfilter', 'select_box', array(XTHREADS_FILTER_NONE => $lang->threadfields_filter_none, XTHREADS_FILTER_EXACT => $lang->threadfields_filter_exact, XTHREADS_FILTER_PREFIX => $lang->threadfields_filter_prefix, XTHREADS_FILTER_ANYWHERE => $lang->threadfields_filter_anywhere, XTHREADS_FILTER_WILDCARD => $lang->threadfields_filter_wildcard));
    make_form_row('datatype', 'select_box', array(XTHREADS_DATATYPE_TEXT => $lang->threadfields_datatype_text, XTHREADS_DATATYPE_INT => $lang->threadfields_datatype_int, XTHREADS_DATATYPE_UINT => $lang->threadfields_datatype_uint, XTHREADS_DATATYPE_BIGINT => $lang->threadfields_datatype_bigint, XTHREADS_DATATYPE_BIGUINT => $lang->threadfields_datatype_biguint, XTHREADS_DATATYPE_FLOAT => $lang->threadfields_datatype_float));
    $form_container->end();
    unset($GLOBALS['form_container']);
    if ($update) {
        $buttons[] = $form->generate_submit_button($lang->update_threadfield);
    } else {
        $buttons[] = $form->generate_submit_button($lang->add_threadfield);
    }
    $form->output_submit_wrapper($buttons);
    $form->end();
    ?>
<script type="text/javascript">
<!--
	var xt_inited = false;
	function xt_visi(o,v) {
		document.getElementById(o).style.display = (v ? '':'none');
	}
	document.getElementById('sanitize').onchange = function() {
		xt_visi('parser_opts', this.options[this.selectedIndex].value == "<?php 
    echo XTHREADS_SANITIZE_PARSER;
    ?>
" && document.getElementById('row_sanitize').style.display != 'none');
	};
	
	function xt_multival_enable() {
		var si = parseInt(document.getElementById('inputtype').options[document.getElementById('inputtype').selectedIndex].value);
		var checkboxIn = (si == <?php 
    echo XTHREADS_INPUT_CHECKBOX;
    ?>
);
		var pureFileIn = (si == <?php 
    echo XTHREADS_INPUT_FILE;
    ?>
);
		var fileIn = (pureFileIn || si == <?php 
    echo XTHREADS_INPUT_FILE_URL;
    ?>
);
		e = checkboxIn; // forced
		
		var datatypeText = (document.getElementById('datatype').options[document.getElementById('datatype').selectedIndex].value == "<?php 
    echo XTHREADS_DATATYPE_TEXT;
    ?>
");
		xt_visi('row_multival_enable', checkboxIn || ((
			si != <?php 
    echo XTHREADS_INPUT_RADIO;
    ?>
 && (datatypeText || pureFileIn)
		)));
		
		if(!e) e = (document.getElementById('multival_enable_yes').checked && document.getElementById('row_multival_enable').style.display != 'none');
		xt_visi('row_multival', e);
		xt_visi('row_multival_limit', e);
		xt_visi('row_dispitemformat', e);
		datatypeVisible = (!e && !checkboxIn && !fileIn);
		xt_visi('row_datatype', datatypeVisible);
		
		// hide some sanitise options (if browser supports it)
		var sanitizeOptShow = ((datatypeVisible && !datatypeText) ? 'none' : '');
		for(i in document.getElementById('sanitize').options) {
			var optItem = document.getElementById('sanitize').options[i];
			if(!optItem) continue; // fix IE6 bug
			if(optItem.value == "<?php 
    echo XTHREADS_SANITIZE_HTML_NL;
    ?>
" || optItem.value == "<?php 
    echo XTHREADS_SANITIZE_NONE;
    ?>
") {
				// our target
				if(sanitizeOptShow == 'none' && document.getElementById('sanitize').selectedIndex == i)
					document.getElementById('sanitize').selectedIndex = 0;
				optItem.style.display = sanitizeOptShow;
			}
		}
		
		dispfmt_obj = document.getElementById('dispformat');
		fileVal = "<a href=\"{URL}\">{FILENAME}</a>";
		nonFileVal = "{VALUE}";
		if(pureFileIn) {
			if(e) {
				if(document.getElementById('dispitemformat').value == nonFileVal) {
					if(dispfmt_obj.value == nonFileVal)
						document.getElementById('dispitemformat').value = fileVal;
					else {
						// swap dispformat <-> dispitemformat
						document.getElementById('dispitemformat').value = dispfmt_obj.value;
						dispfmt_obj.value = nonFileVal;
					}
				}
				if(dispfmt_obj.value == fileVal)
					dispfmt_obj.value = nonFileVal;
			} else {
				if(dispfmt_obj.value == nonFileVal) {
					dispfmt_obj.value = fileVal;
					if(document.getElementById('dispitemformat').value != nonFileVal) {
						// maybe swap?
						var DIFval = document.getElementById('dispitemformat').value.toUpperCase();
						if((function(s){
							for(i in s)
								if(DIFval.indexOf("{"+s[i]+"}") > -1)
									return true;
							return false;
						})(
							["DOWNLOADS","DOWNLOADS_FRIENDLY","FILENAME","UPLOADMIME","URL","FILESIZE","FILESIZE_FRIENDLY","MD5HASH","UPLOADTIME","UPLOAD_TIME","UPLOAD_DATE","UPDATETIME","UPDATE_TIME","UPDATE_DATE","THUMBS","DIMS","MODIFIED"]
						)) {
							dispfmt_obj.value = document.getElementById('dispitemformat').value;
							document.getElementById('dispitemformat').value = nonFileVal;
						}
					}
				}
				if(document.getElementById('dispitemformat').value == fileVal)
					document.getElementById('dispitemformat').value = nonFileVal;
			}
		} else {
			if(document.getElementById('dispitemformat').value == fileVal)
				document.getElementById('dispitemformat').value = nonFileVal;
			if(dispfmt_obj.value == fileVal)
				dispfmt_obj.value = nonFileVal;
		}
	}
	document.getElementById('multival_enable_yes').onclick = xt_multival_enable;
	document.getElementById('multival_enable_no').onclick = xt_multival_enable;
	
	(document.getElementById('use_formhtml_yes').onclick = document.getElementById('use_formhtml_no').onclick = xt_use_formhtml = function() {
		xt_visi('row_formhtml', document.getElementById('use_formhtml_yes').checked);
		xt_visi('formhtml_desc_js', true);
	})();
	
	function xt_filereqimg() {
		var e = (document.getElementById('filereqimg_yes').checked && document.getElementById('row_filereqimg').style.display != 'none');
		xt_visi('row_fileimage_mindim', e);
		xt_visi('row_fileimage_maxdim', e);
		xt_visi('row_fileimgthumbs', e);
	}
	document.getElementById('filereqimg_yes').onclick = xt_filereqimg;
	document.getElementById('filereqimg_no').onclick = xt_filereqimg;
	
	
	(document.getElementById('inputtype').onchange = function() {
		var si = parseInt(this.options[this.selectedIndex].value);
		
		var pureFileIn = (si == <?php 
    echo XTHREADS_INPUT_FILE;
    ?>
);
		var fileIn = (pureFileIn || si == <?php 
    echo XTHREADS_INPUT_FILE_URL;
    ?>
);
		var radioIn = (si == <?php 
    echo XTHREADS_INPUT_RADIO;
    ?>
);
		var checkboxIn = (si == <?php 
    echo XTHREADS_INPUT_CHECKBOX;
    ?>
);
		var selectBoxIn = (si == <?php 
    echo XTHREADS_INPUT_SELECT;
    ?>
);
		var selectIn = (selectBoxIn || radioIn || checkboxIn);
		var textAreaIn = (si == <?php 
    echo XTHREADS_INPUT_TEXTAREA;
    ?>
);
		var textIn = (textAreaIn || si == <?php 
    echo XTHREADS_INPUT_TEXT;
    ?>
);
		xt_visi('row_sanitize', !fileIn && !selectIn);
		document.getElementById('sanitize').onchange();
		
		xt_visi('inputtype_file_explain', pureFileIn);
		
		xt_visi('row_allowfilter', !fileIn && !textAreaIn);
		xt_visi('row_formatmap', !fileIn);
		xt_visi('row_editable_values', !fileIn);
		xt_visi('row_defaultval', !pureFileIn);
		
		xt_visi('row_textmask', textIn);
		xt_visi('row_inputformat', !fileIn);
		xt_visi('row_maxlen', textIn);
		xt_visi('row_fieldwidth', textIn || fileIn || selectBoxIn);
		xt_visi('row_fieldheight', textAreaIn || selectBoxIn);
		
		xt_visi('row_vallist', selectIn);
		
		//xt_visi('row_datatype', !checkboxIn && !fileIn);
		//xt_visi('row_multival_enable', !checkboxIn && !radioIn && !fileIn);
		xt_multival_enable();
		
		xt_visi('row_filemagic', pureFileIn);
		xt_visi('row_fileexts', pureFileIn);
		xt_visi('row_filemaxsize', pureFileIn);
		xt_visi('row_filereqimg', pureFileIn);
		xt_filereqimg();
		
		if(textAreaIn) {
			if(document.getElementById('sanitize').options[document.getElementById('sanitize').selectedIndex].value == "<?php 
    echo XTHREADS_SANITIZE_HTML;
    ?>
")
				document.getElementById('sanitize').selectedIndex++;
		} else if(textIn) {
			if(document.getElementById('sanitize').options[document.getElementById('sanitize').selectedIndex].value == "<?php 
    echo XTHREADS_SANITIZE_HTML_NL;
    ?>
")
				document.getElementById('sanitize').selectedIndex--;
		}
		
		var setFormhtml = true;
		if(document.getElementById('use_formhtml_yes').checked) {
			if(!xt_inited)
				setFormhtml = (document.getElementById("formhtml").value == "");
			else
				setFormhtml = confirm("<?php 
    echo xt_js_str_escape($lang->threadfields_formhtml_js_reset_warning);
    ?>
");
			if(setFormhtml) {
				document.getElementById('use_formhtml_no').checked = true;
			}
			xt_use_formhtml();
		}
		switch(si) {
			<?php 
    foreach (array(XTHREADS_INPUT_TEXTAREA, XTHREADS_INPUT_SELECT, XTHREADS_INPUT_CHECKBOX, XTHREADS_INPUT_RADIO, XTHREADS_INPUT_FILE, XTHREADS_INPUT_TEXT) as $inputtype) {
        $formhtml_info = xthreads_default_threadfields_formhtml($inputtype);
        $formhtml_desc = '';
        foreach ($formhtml_info[1] as $fhvar) {
            $langvar = 'threadfields_formhtml_desc_' . strtolower($fhvar);
            $formhtml_desc .= '<li><code>{' . $fhvar . '}</code>: ' . $lang->{$langvar} . '</li>';
        }
        echo '
				case ' . $inputtype . ':
					if(setFormhtml) document.getElementById("formhtml").value = "' . xt_js_str_escape($formhtml_info[0]) . '";
					document.getElementById("formhtml_desc_ul_js").innerHTML = "' . xt_js_str_escape($formhtml_desc) . '";
					break;';
    }
    ?>
		}
	}).apply(document.getElementById('inputtype'));
	
	(document.getElementById('datatype').onchange = function() {
		//var isText = this.options[this.selectedIndex].value == "<?php 
    echo XTHREADS_DATATYPE_TEXT;
    ?>
";
		//xt_visi('row_multival_enable', isText);
		xt_multival_enable();
	}).apply(document.getElementById('datatype'));
	
	(document.getElementById('editable').onchange = function() {
		xt_visi('row_editable_gids', this.options[this.selectedIndex].value == "99");
	}).apply(document.getElementById('editable'));
	
	(document.getElementById('viewable_gids').onchange = function() {
		var e=false;
		var o=document.getElementById('viewable_gids').options;
		for(i=0; i<o.length; i++)
			if(e = o[i].selected) // no, I do mean =, not ==
				break;
		xt_visi('row_unviewableval', e);
	}).apply(document.getElementById('viewable_gids'));
	
	<?php 
    $textmask_types = array('anything' => '^.*$', 'digit' => '^\\d+$', 'alphadigit' => '^[a-z0-9]+$', 'number' => '^(-?)([0-9]*)(?:\\.(\\d*))?(?:e([+-]?\\d*))?$', 'date' => '^(0?[1-9]|[12]\\d|3[01])/(0?[1-9]|1[012])/((?:19|20)\\d\\d)$', 'date_us' => '^(0?[1-9]|1[012])/(0?[1-9]|[12]\\d|3[01])/((?:19|20)\\d\\d)$', 'uri' => '^([^:/?#]+)\\:((//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?)$', 'url' => '^([a-z0-9]+)\\://([^/?#]+)(/([^\\r\\n"<>#?]*)(\\?([^\\r\\n"<>#]*))?(#([^\\r\\n"<>]*))?)?$', 'httpurl' => '^(https?)\\://([^/?#]+)(/([^\\r\\n"<>#?]*)(\\?([^\\r\\n"<>#]*))?(#([^\\r\\n"<>]*))?)?$', 'email' => '^(.+)@([a-z0-9_.\\-]+)$', 'emailr' => '^([^ "(),:;<>@\\[\\\\\\]]+)@([a-z0-9_.\\-]+)$', 'css' => '^[a-z0-9_\\- ]+$', 'color' => '^[a-z\\-]+|#?[0-9a-f]{6}$');
    ?>
	document.getElementById('textmask').parentNode.innerHTML =
			'<select name="textmask_select" id="textmask_select">' +
<?php 
    foreach ($textmask_types as $type => &$mask) {
        $langvar = 'threadfields_textmask_' . $type;
        echo '			\'<option value="', $type, '">', $lang->{$langvar}, '</option>\' +
';
    }
    ?>
			'<option value="custom">'+<?php 
    echo "'", $lang->threadfields_textmask_custom, "'";
    ?>
+'</option>' +
			'</select> ' + document.getElementById('textmask').parentNode.innerHTML + '<div id="textmask_select_descriptions" style="font-size: smaller; padding-top: 0.5em;">' +
<?php 
    foreach ($textmask_types as $type => &$mask) {
        $langvar = 'threadfields_textmask_' . $type . '_desc';
        if (property_exists($lang, $langvar)) {
            echo '			\'<div id="textmask_selector_desc_', $type, '" style="display: none;">', xt_js_str_escape($lang->{$langvar}), '</div>\' +
';
        }
    }
    ?>
			'</div>';
	var textmaskMapping = {
<?php 
    $comma = '';
    foreach ($textmask_types as $type => &$mask) {
        echo $comma, '		', $type, ': "', xt_js_str_escape($mask), '"';
        if (!$comma) {
            $comma = ',
';
        }
    }
    ?>

	};
	// determine which option to be selected by default
	(function() {
		// we can only index by number, and as we're a little lazy, create a name -> index map
		var textmaskSelectOpts = document.getElementById('textmask_select').options;
		var textmaskSelectMap = {};
		for(i=0; i<textmaskSelectOpts.length; i++) {
			textmaskSelectMap[textmaskSelectOpts[i].value] = i;
		}
		
		var mask = document.getElementById('textmask').value;
		for(var maskName in textmaskMapping) {
			if(mask == textmaskMapping[maskName]) {
				document.getElementById('textmask_select').selectedIndex = textmaskSelectMap[maskName];
				textmaskSelectUpdated();
				return;
			}
		}
		document.getElementById('textmask_select').selectedIndex = textmaskSelectMap["custom"];
	})();
	document.getElementById('textmask_select').onchange = function() {
		var maskName = this.options[this.selectedIndex].value;
		if(textmaskMapping[maskName])
			document.getElementById('textmask').value = textmaskMapping[maskName];
		textmaskSelectUpdated();
	};
	document.getElementById('textmask_select').onkeypress = document.getElementById('textmask_select').onkeydown = document.getElementById('textmask_select').onkeyup = function(e) {
		document.getElementById('textmask_select').onchange();
		return true;
	};
	function textmaskSelectUpdated() {
		var maskName = document.getElementById('textmask_select').options[document.getElementById('textmask_select').selectedIndex].value;
		var d = (maskName != "custom");
		document.getElementById('textmask').readOnly = d;
		document.getElementById('textmask').tabIndex = (d?'-1':''); // note, this is non-standard
		document.getElementById('textmask').style.background = (d ? "#F0F0F0":"");
		document.getElementById('textmask').style.color = (d ? "#808080":"");
		
		var o = document.getElementById('textmask_select_descriptions').childNodes;
		for(i=0; i<o.length; i++) {
			if(o[i].id == "textmask_selector_desc_"+maskName)
				o[i].style.display = "";
			else
				o[i].style.display = "none";
		}
	}
	document.getElementById('textmask').onfocus = function() {
		if(this.readOnly)
			document.getElementById('textmask_select').focus();
	};
	xt_inited = true;
//-->
</script>
<script type="text/javascript" src="jscripts/xtofedit.js?xtver=<?php 
    echo XTHREADS_VERSION;
    ?>
"></script>
<script type="text/javascript">
<!--
xtOFEditorLang.confirmFormSubmit = "<?php 
    echo $lang->xthreads_js_confirm_form_submit;
    ?>
";
xtOFEditorLang.windowTitle = "<?php 
    echo $lang->xthreads_js_edit_value;
    ?>
";
xtOFEditorLang.saveButton = "<?php 
    echo $lang->xthreads_js_save_changes;
    ?>
";
xtOFEditorLang.closeSaveChanges = "<?php 
    echo $lang->xthreads_js_close_save_changes;
    ?>
";

var fmtMapEditor = new xtOFEditor();
fmtMapEditor.src = document.getElementById('formatmap');
fmtMapEditor.loadFunc = function(s) {
	var a = s.replace(/\r/g, "").replace(/\{\n\}/g, "\r").split("\n");
	var data = [];
	for(var i=0; i<a.length; i++) {
		a[i] = a[i].replace(/\r/g, "\n");
		var p = a[i].indexOf("{|}");
		if(p < 0) continue;
		data.push([ a[i].substring(0, p), a[i].substring(p+3) ]);
	}
	return data;
};
fmtMapEditor.saveFunc = function(a) {
	var ret = "";
	for(var i=0; i<a.length; i++) {
		ret += a[i].join("{|}").replace(/\n/g, "{\n}") + "\n";
	}
	return ret;
};
fmtMapEditor.fields = [
	{title: "<?php 
    echo $lang->xthreads_js_formatmap_from;
    ?>
", width: '45%', elemFunc: fmtMapEditor.textAreaFunc},
	{title: "<?php 
    echo $lang->xthreads_js_formatmap_to;
    ?>
", width: '55%', elemFunc: fmtMapEditor.textAreaFunc}
];

fmtMapEditor.copyStyles=true;
fmtMapEditor.init();

var editValEditor = new xtOFEditor();
editValEditor.src = document.getElementById('editable_values');
editValEditor.loadFunc = function(s) {
	var a = s.replace(/\r/g, "").replace(/\{\n\}/g, "\r").split("\n");
	var data = [];
	for(var i=0; i<a.length; i++) {
		a[i] = a[i].replace(/\r/g, "\n");
		var p = a[i].indexOf("{|}");
		if(p < 0) continue;
		data.push([ a[i].substring(0, p), a[i].substring(p+3).split(",") ]);
	}
	return data;
};
editValEditor.saveFunc = function(a) {
	var ret = "";
	for(var i=0; i<a.length; i++) {
		ret += a[i][0].replace(/\n/g, "{\n}") + "{|}" + a[i][1].join(",") + "\n";
	}
	return ret;
};
editValEditor.fields = [
	{title: "<?php 
    echo $lang->xthreads_js_formatmap_from;
    ?>
", width: '50%', elemFunc: editValEditor.textAreaFunc},
	{title: "<?php 
    echo $lang->xthreads_js_editable_values_groups;
    ?>
", width: '50%', elemFunc: function(c) {
		var o = appendNewChild(c, "select");
		o.multiple = true;
		o.size = 3;
		o.style.width = '100%';
		o.innerHTML = '<?php 
    foreach ($GLOBALS['cache']->read('usergroups') as $group) {
        echo '<option value="' . $group['gid'] . '">' . xt_js_str_escape(htmlspecialchars_uni(strip_tags($group['title']))) . '</option>';
    }
    ?>
';
		return o;
	}}
];

editValEditor.copyStyles=true;
editValEditor.init();

//-->
</script><?php 
    $page->output_footer();
}
Esempio n. 3
0
function xthreads_admin_rebuildthumbs()
{
    global $mybb, $db;
    if ($mybb->request_method == 'post') {
        if (isset($mybb->input['do_rebuildxtathumbs'])) {
            $page = (int) $mybb->input['page'];
            if ($page < 1) {
                $page = 1;
            }
            $perpage = (int) $mybb->input['xtathumbs'];
            if (!$perpage) {
                $perpage = 500;
            }
            global $lang;
            if (!$lang->xthreads_rebuildxtathumbs_nofields) {
                $lang->load('xthreads');
            }
            $thumbfields = xthreads_admin_getthumbfields();
            if (empty($thumbfields)) {
                flash_message($lang->xthreads_rebuildxtathumbs_nofields, 'error');
                admin_redirect(xthreads_admin_url('tools', 'recount_rebuild'));
                return;
            }
            $where = 'field IN ("' . implode('","', array_keys($thumbfields)) . '")';
            //  AND tid!=0
            $num_xta = $db->fetch_field($db->simple_select('xtattachments', 'count(*) as n', $where), 'n');
            @set_time_limit(1800);
            require_once MYBB_ROOT . 'inc/xthreads/xt_upload.php';
            require_once MYBB_ROOT . 'inc/xthreads/xt_updatehooks.php';
            include_once MYBB_ROOT . 'cache/xthreads_evalcache.php';
            // for thumbnail code
            $xtadir = $mybb->settings['uploadspath'] . '/xthreads_ul/';
            if ($xtadir[0] != '/') {
                $xtadir = '../' . $xtadir;
            }
            // TODO: perhaps check for absolute Windows paths as well?  but then, who uses Windows on a production server? :>
            $query = $db->simple_select('xtattachments', '*', $where, array('order_by' => 'aid', 'limit' => $perpage, 'limit_start' => ($page - 1) * $perpage));
            while ($xta = $db->fetch_array($query)) {
                // remove thumbs, then rebuild
                $name = xthreads_get_attach_path($xta);
                // unfortunately, we still need $xtadir
                if ($thumbs = @glob(substr($name, 0, -6) . '*.thumb')) {
                    foreach ($thumbs as &$thumb) {
                        @unlink($xtadir . $xta['indir'] . basename($thumb));
                    }
                }
                $thumb = xthreads_build_thumbnail($thumbfields[$xta['field']], $xta['aid'], $xta['field'], $name, $xtadir, $xta['indir']);
                // TODO: perhaps check for errors? but then, what to do?
            }
            $db->free_result($query);
            ++$page;
            check_proceed($num_xta, $page * $perpage, $page, $perpage, 'xtathumbs', 'do_rebuildxtathumbs', $lang->xthreads_rebuildxtathumbs_done);
        }
    } else {
        $GLOBALS['plugins']->add_hook('admin_formcontainer_end', 'xthreads_admin_rebuildthumbs_show');
    }
}
Esempio n. 4
0
$l['xthreads_allow_blankmsg'] = 'Allow Blank Post Message';
$l['xthreads_allow_blankmsg_desc'] = 'If yes, new threads in this forum will not require a message to be entered.';
$l['xthreads_nostatcount'] = 'Don\'t include this forum\'s threads/posts in global forum statistics';
$l['xthreads_nostatcount_desc'] = 'If yes, threads and posts made in this forum will not increase the forum\'s statistics on the number of threads and posts across all forums (eg at the bottom of the forum home, or stats.php).';
$l['xthreads_defaultfilter'] = 'Default Thread Filter';
$l['xthreads_defaultfilter_desc'] = 'This filter is applied to forumdisplay if no filter has been specified in the URL.
<br /><!-- more -->Separate entries with newlines; variables/conditionals supported (in filter value only).  Note that URI encoding is not supported.
<br />The default filter can also be disabled with no additional filter in use, by specifying <em>filterdisable</em> in the URL, eg <em>forumdisplay.php?fid=2&amp;filterdisable=1</em>
<br />Example value for this field:
<code style="display: block; margin-left: 2em;">myfield=something<br />__xt_uid=1<br />field2[]=value1<br />field2[]={$mybb-&gt;user[\'username\']}</code>';
/* $l['xthreads_addfiltenable'] = 'Enable Thread Filters';
$l['xthreads_addfiltenable_desc'] = 'Enable users to filter forumdisplay by certain thread attributes (eg thread starter).
<br /><!-- more -->This feature works similar to filtering forumdisplay threads by custom thread fields.  This does not affect templates, so you need to make appropriate changes to make this option useful.  If you tick any of the options below, users can filter threads displayed on forumdisplay by the relevant fields by appending the URL parameter <code>filterxt_<em>fieldname</em></code>, for example <em>forumdisplay.php?fid=2&amp;filterxt_uid=2</em> will only display threads created by the user with UID of 2.  Note, multiple filters are allowed, and you can also specify an array of values for a single field.'; */
$l['xthreads_cust_wolstr'] = 'Custom WOL Text';
$l['xthreads_cust_wolstr_desc'] = 'You can have custom text for this forum on the Who\'s Online List.
<br /><!-- more -->If you enter text in the following textboxes, it will replace the default WOL language text.  As this replaces language strings, it will accept variables in the same way.  Go to <a href="' . xthreads_admin_url('config', 'languages') . '">Languages section</a> -&gt; Edit Language Variables (under Options for your selected language) -&gt; edit <em>online.lang.php</em> to see the defaults.';
$l['xthreads_afe_uid'] = 'Thread starter\'s User ID';
$l['xthreads_afe_lastposteruid'] = 'Last poster\'s User ID';
$l['xthreads_afe_prefix'] = 'Thread Prefix ID; <em>check URL (look for <strong>pid</strong>) when editing thread prefix in ACP</em>';
$l['xthreads_afe_icon'] = 'Thread Icon ID; <em>check URL (look for <strong>iid</strong>) when editing thread icon in ACP</em>';
$l['xthreads_wol_announcements'] = 'Announcements';
$l['xthreads_wol_forumdisplay'] = 'Forum Display';
$l['xthreads_wol_newthread'] = 'New Thread';
$l['xthreads_wol_attachment'] = 'Attachment Download';
$l['xthreads_wol_newreply'] = 'New Reply';
$l['xthreads_wol_showthread'] = 'Show Thread';
$l['xthreads_sort_threadfield_prefix'] = 'Thread Field: ';
$l['xthreads_sort_filename'] = 'file name';
$l['xthreads_sort_filesize'] = 'file size';
$l['xthreads_sort_uploadtime'] = 'upload time';
$l['xthreads_sort_updatetime'] = 'update time';