}
        if ($fieldtype) {
            $fixsql .= ($fixsql ? ', ' : '') . 'MODIFY `' . $db->escape_string($tf['field']) . '` ' . $fieldtype;
        }
    }
    $db->free_result($query);
    if ($fixsql) {
        $db->write_query('ALTER TABLE `' . $db->table_prefix . 'threadfields_data` ' . $fixsql);
    }
}
if (XTHREADS_INSTALLED_VERSION < 1.6) {
    $db->write_query('ALTER TABLE `' . $db->table_prefix . 'threadfields` ADD COLUMN (
		`multival_limit` ' . xthreads_db_fielddef('int', null, true) . ' not null default 0,
		`inputformat` text not null,
		`inputvalidate` text not null,
		`hidefield` ' . xthreads_db_fielddef('int', null, false) . ' not null default 0
	)');
    $db->update_query('threadfields', array('inputformat' => '{VALUE}'));
    // hideedit -> hidefield transition
    $db->update_query('threadfields', array('hidefield' => XTHREADS_HIDE_THREAD));
    $db->update_query('threadfields', array('hidefield' => XTHREADS_HIDE_INPUT | XTHREADS_HIDE_THREAD), 'hideedit != 0');
    $db->write_query('ALTER TABLE `' . $db->table_prefix . 'threadfields` DROP COLUMN `hideedit`');
    // fix email masks
    $db->update_query('threadfields', array('textmask' => $db->escape_string('^([^ "(),:;<>@\\[\\\\\\]]+)@([a-z0-9_.\\-]+)$')), 'textmask="' . $db->escape_string('^([a-z0-9_.\\-]+)@([a-z0-9_.\\-]+)$') . '"');
    // we never used this, so may as well get rid of it
    $db->write_query('ALTER TABLE `' . $db->table_prefix . 'forums` DROP COLUMN `xthreads_wol_xtattachment`');
    if (XTHREADS_MODIFY_TEMPLATES) {
        find_replace_templatesets('showthread', '#\\{\\$classic_header\\}#', '{$threadfields_display}{$classic_header}');
    }
    require_once MYBB_ROOT . 'inc/xthreads/xt_install.php';
    // migrate templates - surely no-one else is ending their template names with "threadfields_inputrow", right?
Exemple #2
0
function xthreads_install()
{
    global $db, $cache, $plugins;
    $plugins->run_hooks('xthreads_install_start');
    $create_table_suffix = $db->build_create_table_collation();
    $dbtype = xthreads_db_type();
    switch ($dbtype) {
        case 'mysql':
            $engine = 'MyISAM';
            // try to see if a custom table engine is being used
            $query = $db->query('SHOW TABLE STATUS LIKE "' . $db->table_prefix . 'threads"', true);
            if ($query) {
                $eng = $db->fetch_field($query, 'Engine');
                if (in_array(strtolower($eng), array('innodb', 'aria', 'xtradb'))) {
                    // only stick to common possibilities to avoid issues with exquisite setups
                    $engine = $eng;
                }
            }
            $create_table_suffix = ' ENGINE=' . $engine . $create_table_suffix;
            $auto_increment = ' auto_increment';
            break;
        case 'sqlite':
            $auto_increment = ' PRIMARY KEY';
            break;
        case 'pgsql':
            $auto_increment = '';
    }
    if ($dbtype != 'mysql') {
        die('XThreads currently does not support database systems other than MySQL/i.');
    }
    if (!$db->table_exists('threadfields_data')) {
        $db->write_query('CREATE TABLE ' . $db->table_prefix . 'threadfields_data (
			tid ' . xthreads_db_fielddef('int') . ' not null
			' . ($dbtype != 'sqlite' ? ', PRIMARY KEY (tid)' : '') . '
		)' . $create_table_suffix);
    }
    if (!$db->table_exists('xtattachments')) {
        $db->write_query('CREATE TABLE ' . $db->table_prefix . 'xtattachments (
			aid ' . xthreads_db_fielddef('int') . ' not null' . $auto_increment . ',
			downloads ' . xthreads_db_fielddef('bigint') . ' not null default 0,
			
			tid ' . xthreads_db_fielddef('int') . ' not null,
			uid ' . xthreads_db_fielddef('int') . ' not null default 0,
			field varchar(50) not null default \'\',
			posthash varchar(50) not null default \'\',
			filename varchar(255) not null default \'\',
			uploadmime varchar(120) not null default \'\',
			filesize ' . xthreads_db_fielddef('bigint') . ' not null default 0,
			attachname varchar(120) not null default \'\',
			indir varchar(40) not null default \'\',
			md5hash ' . xthreads_db_fielddef('binary', 16) . ' default null,
			uploadtime ' . xthreads_db_fielddef('bigint') . ' not null default 0,
			updatetime ' . xthreads_db_fielddef('bigint') . ' not null default 0,
			
			thumbs text not null
			
			' . ($dbtype != 'sqlite' ? ',
				PRIMARY KEY (aid)
				' . ($dbtype != 'pg' ? ',
					KEY (tid),
					KEY (tid,uid),
					KEY (posthash),
					KEY (field)
				' : '') . '
			' : '') . '
		)' . $create_table_suffix);
    }
    if (!$db->table_exists('threadfields')) {
        $fieldprops = xthreads_threadfields_props();
        $query = '';
        foreach ($fieldprops as $field => &$prop) {
            $query .= ($query ? ',' : '') . '`' . $field . '` ' . xthreads_db_fielddef($prop['db_type'], $prop['db_size'], $prop['db_unsigned']) . ' not null';
            if (isset($prop['default']) && $prop['db_type'] != 'text') {
                if ($prop['datatype'] == 'string') {
                    $query .= ' default \'' . $db->escape_string($prop['default']) . '\'';
                } elseif ($prop['datatype'] == 'double') {
                    $query .= ' default ' . (double) $prop['default'];
                } else {
                    $query .= ' default ' . (int) $prop['default'];
                }
            }
            if ($field == 'field' && $dbtype == 'sqlite') {
                $query .= ' PRIMARY KEY';
            }
        }
        $db->write_query('CREATE TABLE ' . $db->table_prefix . 'threadfields (
			' . $query . '
			' . ($dbtype != 'sqlite' ? ',
				PRIMARY KEY (field)
				' . ($dbtype != 'pg' ? ',
					KEY (disporder)
				' : '') . '
			' : '') . '
		)' . $create_table_suffix);
        // `allowsort` '.xthreads_db_numdef('tinyint').' not null default 0,
    }
    foreach (array('grouping' => xthreads_db_fielddef('int') . ' not null default 0', 'firstpostattop' => xthreads_db_fielddef('tinyint') . ' not null default 0', 'inlinesearch' => xthreads_db_fielddef('tinyint') . ' not null default 0', 'tplprefix' => 'text not null', 'langprefix' => 'text not null', 'allow_blankmsg' => xthreads_db_fielddef('tinyint') . ' not null default 0', 'nostatcount' => xthreads_db_fielddef('tinyint') . ' not null default 0', 'fdcolspan_offset' => xthreads_db_fielddef('smallint', null, false) . ' not null default 0', 'settingoverrides' => 'text not null', 'postsperpage' => xthreads_db_fielddef('smallint') . ' not null default 0', 'hideforum' => xthreads_db_fielddef('tinyint') . ' not null default 0', 'hidebreadcrumb' => xthreads_db_fielddef('tinyint') . ' not null default 0', 'defaultfilter' => 'text not null', 'wol_announcements' => 'varchar(255) not null default \'\'', 'wol_forumdisplay' => 'varchar(255) not null default \'\'', 'wol_newthread' => 'varchar(255) not null default \'\'', 'wol_attachment' => 'varchar(255) not null default \'\'', 'wol_newreply' => 'varchar(255) not null default \'\'', 'wol_showthread' => 'varchar(255) not null default \'\'') as $field => $fdef) {
        if (!$db->field_exists($field, 'forums')) {
            $db->write_query('ALTER TABLE ' . $db->table_prefix . 'forums ADD COLUMN xthreads_' . $field . ' ' . $fdef);
        }
    }
    // add indexes
    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` ADD KEY `xthreads_' . $afe . '` (`' . $afe . '`)', true);
    }
    // increase size of sorting column
    $db->write_query('ALTER TABLE `' . $db->table_prefix . 'forums` MODIFY `defaultsortby` varchar(255) NOT NULL default \'\'');
    $cache->update_forums();
    // check for xthreads_attachment.php supported URL type
    if (file_exists(MYBB_ROOT . 'xthreads_attach.php')) {
        // if not, our admin is a dufus
        $rand = 'aA0._|' . mt_rand();
        $rand_md5 = md5($rand);
        $baseurl = $GLOBALS['mybb']->settings['bburl'] . '/xthreads_attach.php';
        if (fetch_remote_file($baseurl . '/test/' . $rand) == $rand_md5) {
            define('XTHREADS_ATTACH_USE_QUERY', -1);
        } elseif (fetch_remote_file($baseurl . '?file=test/' . $rand) == $rand_md5) {
            define('XTHREADS_ATTACH_USE_QUERY', 1);
        } elseif (fetch_remote_file($baseurl . '?file=test|' . $rand) == $rand_md5) {
            define('XTHREADS_ATTACH_USE_QUERY', 2);
        }
        // else, well, sucks for the user...
    }
    xthreads_buildtfcache();
    xthreads_write_xtcachefile();
    xthreads_insert_templates(xthreads_new_templates(), -2);
    xthreads_plugins_quickthread_tplmod();
    // admin permissions - default to all allow
    $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
        $perms['config']['threadfields'] = 1;
        $db->update_query('adminoptions', array('permissions' => $db->escape_string(serialize($perms))), 'uid=' . $adminopt['uid']);
    }
    $db->free_result($query);
    $plugins->run_hooks('xthreads_install_end');
}
Exemple #3
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();
}