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'); }
function xtu_normalize_template($s) { return str_replace(' />', '/>', strtr(preg_replace('~\\s+~', ' ', trim($s)), array('> ' => '>', ' <' => '<'))); } $query = $db->simple_select('templates', 'title,template', 'title IN ("editpost_first","forumdisplay_group_sep","forumdisplay_thread_null","showthread_noreplies","forumdisplay_searchforum_inline","post_threadfields_inputrow") AND sid=-1'); $rmtpl = array(); while ($tpl = $db->fetch_array($query)) { if (xtu_normalize_template($tpl['template']) == xtu_normalize_template($newtpl[$tpl['title']])) { // templates seem to be the same, remove $rmtpl[] = $tpl['title']; } } if (!empty($rmtpl)) { $db->delete_query('templates', 'title IN ("' . implode('","', $rmtpl) . '") AND sid=-1'); } xthreads_insert_templates($newtpl, -2); } if (XTHREADS_INSTALLED_VERSION < 1.62) { xthreads_buildtfcache(); // will also update XThreads forum cache } if (XTHREADS_INSTALLED_VERSION < 1.64 && XTHREADS_INSTALLED_VERSION > 1.32) { // we'll fix up broken regexes in these versions if they exist // note that this does assume people haven't deliberately used bad regexes $db->update_query('threadfields', array('textmask' => $db->escape_string('^(https?)\\://([^/?#]+)(/([^\\r\\n"<>#?]*)(\\?([^\\r\\n"<>#]*))?(#([^\\r\\n"<>]*))?)?$')), 'textmask="' . $db->escape_string('^(https?)\\://([a-z0-9.\\-_]+)(/[^\\r\\n"<>&]*)?$') . '"'); $db->update_query('threadfields', array('textmask' => $db->escape_string('^([a-z0-9]+)\\://([^/?#]+)(/([^\\r\\n"<>#?]*)(\\?([^\\r\\n"<>#]*))?(#([^\\r\\n"<>]*))?)?$')), 'textmask="' . $db->escape_string('^([a-z0-9]+)\\://([a-z0-9.\\-_]+)(/[^\\r\\n"<>&]*)?$') . '"'); $db->update_query('threadfields', array('textmask' => $db->escape_string('^([^:/?#]+)\\:((//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?)$')), 'textmask="' . $db->escape_string('^([a-z0-9]+)\\:(.+)$') . '"'); // if anyone's using custom file input HTML, try to fix the scriptaculous reference $db->write_query('UPDATE ' . $db->table_prefix . 'threadfields SET formhtml = REPLACE(formhtml, "' . $db->escape_string('src="{$mybb->settings[\'bburl\']}/jscripts/scriptaculous.js?load=effects,dragdrop"') . '", "' . $db->escape_string('src="{$mybb->settings[\'bburl\']}/jscripts/<if $mybb->version_code>=1700 then>xthreads_jquery-ui.min.js<else>scriptaculous.js?load=effects,dragdrop</if>"') . '") WHERE inputtype = ' . XTHREADS_INPUT_FILE . ' AND formhtml != ""