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 db_ping(&$dbobj) { if ($dbobj->type == 'mysqli') { $func = 'mysqli_ping'; } else { $func = xthreads_db_type($dbobj->type) . '_ping'; } if (!function_exists($func)) { return true; } // fallback if (is_object(@$dbobj->db)) { return true; } // sqlite $ret = @$func($dbobj->read_link); if ($dbobj->write_link !== $dbobj->read_link) { $ret = @$func($dbobj->write_link) && $ret; } return $ret; }
function xthreads_db_concat_sql($a) { switch (xthreads_db_type()) { case 'sqlite': case 'pgsql': return implode('||', $a); default: return 'CONCAT(' . implode(',', $a) . ')'; } }
function xthreads_db_fielddef($type, $size = null, $unsigned = null) { // defaults if (!isset($unsigned)) { $unsigned = $type != 'tinyint'; } $text_type = false; switch ($type) { case 'text': case 'blob': $size = 0; // fall through // fall through case 'varchar': case 'varbinary': case 'char': case 'binary': $unsigned = false; $text_type = true; } if (!isset($size)) { switch ($type) { case 'tinyint': $size = 3; break; case 'smallint': $size = 5; break; case 'int': $size = 10; break; case 'bigint': $size = 20; break; case 'varchar': case 'varbinary': $size = 255; break; default: $size = 0; } if ($size && $unsigned) { ++$size; } } if ($size !== 0) { $size = '(' . $size . ')'; } elseif ($type == 'varchar' || $type == 'varbinary') { // force length for varchar if one not defined $size = '(255)'; } else { $size = ''; } $unsigned = $unsigned ? ' unsigned' : ''; if ($text_type) { if (xthreads_db_type() != 'pg' || $type != 'varbinary') { $type .= $size; } $size = ''; //$unsigned = ''; } switch (xthreads_db_type()) { case 'sqlite': if ($type == 'tinyint') { $type = 'smallint'; } return $type . $unsigned; case 'pg': if ($type == 'tinyint') { $type = 'smallint'; } if ($type == 'binary') { $type = 'bytea'; } return $type; default: // mysql return $type . $size . $unsigned; } }