01-26-2008, 11:43 PM
Hello All,
I have been working on a MyBB multiforums script and I need some help. I have modified the stock MyBB installer so that I can install multiple forums with one set of files and one database. In my testing, however, in order to install a forum I need to enter in my database information before the install continues on. What I want to do is hard code the Database Username, Password and Database Name fields into the installer. I also want to hide the Database Username, Database Password, Database Name, Database Engine and Database Host form fields. (They should still autodetect though as they currently do).
I looked at the index.php in the install folder but I couldn't find any html code that resembled a form, which I would expect as the DB info is edited on a form. Is there another file I need to look in?
Below is an edited copy of my index.php for the install directory. It basically bypasses the check to see if config.php is writable, takes the table prefix that the user enters and writes this to the database and to the .htaccess file and then it finishes. I will also need to make more modifications to it, but I want to do things one step at a time.
I have been working on a MyBB multiforums script and I need some help. I have modified the stock MyBB installer so that I can install multiple forums with one set of files and one database. In my testing, however, in order to install a forum I need to enter in my database information before the install continues on. What I want to do is hard code the Database Username, Password and Database Name fields into the installer. I also want to hide the Database Username, Database Password, Database Name, Database Engine and Database Host form fields. (They should still autodetect though as they currently do).
I looked at the index.php in the install folder but I couldn't find any html code that resembled a form, which I would expect as the DB info is edited on a form. Is there another file I need to look in?
Below is an edited copy of my index.php for the install directory. It basically bypasses the check to see if config.php is writable, takes the table prefix that the user enters and writes this to the database and to the .htaccess file and then it finishes. I will also need to make more modifications to it, but I want to do things one step at a time.
PHP Code:
<?php
/**
* MyBB 1.2
* Copyright © 2006 MyBB Group, All Rights Reserved
*
* Website: http://www.mybboard.net
* License: http://www.mybboard.net/eula.html
*
* $Id: index.php 3256 2007-07-31 19:31:05Z Tikitiki $
*/
$one = "Ok";
$two = "Ok";
error_reporting(E_ALL & ~E_NOTICE);
define('MYBB_ROOT', dirname(dirname(__FILE__))."/");
define("INSTALL_ROOT", dirname(__FILE__));
require_once MYBB_ROOT.'inc/class_core.php';
$mybb = new MyBB;
$userPrefix;
// Include the files necessary for installation
require_once MYBB_ROOT.'inc/class_timers.php';
require_once MYBB_ROOT.'inc/functions.php';
require_once MYBB_ROOT.'admin/adminfunctions.php';
require_once MYBB_ROOT.'inc/class_xml.php';
require_once MYBB_ROOT.'inc/functions_user.php';
require_once MYBB_ROOT.'inc/class_language.php';
$lang = new MyLanguage();
$lang->set_path(MYBB_ROOT.'install/resources');
$lang->load('language');
// Include the necessary contants for installation
$grouppermignore = array('gid', 'type', 'title', 'description', 'namestyle', 'usertitle', 'stars', 'starimage', 'image');
$groupzerogreater = array('pmquota', 'maxreputationsday', 'attachquota');
$displaygroupfields = array('title', 'description', 'namestyle', 'usertitle', 'stars', 'starimage', 'image');
$fpermfields = array('canview', 'candlattachments', 'canpostthreads', 'canpostreplys', 'canpostattachments', 'canratethreads', 'caneditposts', 'candeleteposts', 'candeletethreads', 'caneditattachments', 'canpostpolls', 'canvotepolls', 'cansearch');
// Include the installation resources
require_once INSTALL_ROOT.'/resources/output.php';
$output = new installerOutput;
$dboptions = array();
if(function_exists('mysqli_connect'))
{
$dboptions['mysqli'] = array(
'title' => 'MySQL Improved',
'structure_file' => 'mysql_db_tables.php',
'population_file' => 'mysql_db_inserts.php'
);
}
if(function_exists('mysql_connect'))
{
$dboptions['mysql'] = array(
'title' => 'MySQL',
'structure_file' => 'mysql_db_tables.php',
'population_file' => 'mysql_db_inserts.php'
);
}
// Perform a check if MyBB is already installed or not
/*
$installed = false;
if(file_exists(MYBB_ROOT."/inc/config.php") && file_exists(MYBB_ROOT."/inc/settings.php"))
{
require MYBB_ROOT."/inc/config.php";
require MYBB_ROOT."/inc/settings.php";
if(is_array($config) && is_array($settings))
{
$installed = true;
}
}
*/
if(file_exists('lock'))
{
$output->print_error($lang->locked);
}
else if($installed == true && !$mybb->input['action'])
{
$output->print_header($lang->already_installed, "errormsg", 0);
echo sprintf($lang->mybb_already_installed, $mybb->version);
$output->print_footer();
}
else
{
$output->steps = array(
'intro' => $lang->welcome,
'license' => $lang->license_agreement,
'requirements_check' => $lang->req_check,
'database_info' => $lang->db_config,
'create_tables' => $lang->table_creation,
'populate_tables' => $lang->data_insertion,
'templates' => $lang->theme_install,
'configuration' => $lang->board_config,
'adminuser' => $lang->admin_user,
'final' => $lang->finish_setup,
);
if(!isset($mybb->input['action']))
{
$mybb->input['action'] = 'intro';
}
switch($mybb->input['action'])
{
case 'license':
license_agreement();
break;
case 'requirements_check':
requirements_check();
break;
case 'database_info':
database_info();
break;
case 'create_tables':
create_tables();
break;
case 'populate_tables':
populate_tables();
break;
case 'templates':
insert_templates();
break;
case 'configuration':
configure();
break;
case 'adminuser';
create_admin_user();
break;
case 'final':
install_done();
break;
default:
intro();
break;
}
}
function intro()
{
global $output, $mybb, $lang;
$output->print_header($lang->welcome, 'welcome');
echo sprintf($lang->welcome_step, $mybb->version);
$output->print_footer('license');
}
function license_agreement()
{
global $output, $lang;
$output->print_header($lang->license_agreement, 'license');
$license = '<h3>Important - Read Carefully</h3>';
//License goes here, only removed for thread readability
echo sprintf($lang->license_step, $license);
$output->print_footer('requirements_check');
}
function requirements_check()
{
global $output, $mybb, $dboptions, $lang;
$mybb->input['action'] = "requirements_check";
$output->print_header($lang->req_check, 'requirements');
echo $lang->req_step_top;
$errors = array();
$showerror = 0;
// Check PHP Version
$phpversion = @phpversion();
if($phpversion < '4.1.0')
{
$errors[] = sprintf($lang->req_step_error_box, sprintf($lang->req_step_error_phpversion, $phpversion));
$phpversion = sprintf($lang->req_step_span_fail, $phpversion);
$showerror = 1;
}
else
{
$phpversion = sprintf($lang->req_step_span_pass, $phpversion);
}
// Check database engines
if(count($dboptions) < 1)
{
$errors[] = sprintf($lang->req_step_error_box, $lang->req_step_error_dboptions);
$dbsupportlist = sprintf($lang->req_step_span_fail, $lang->none);
$showerror = 1;
}
else
{
foreach($dboptions as $dboption)
{
$dbsupportlist[] = $dboption['title'];
}
$dbsupportlist = implode(', ', $dbsupportlist);
}
// Check XML parser is installed
if(!function_exists('xml_parser_create'))
{
$errors[] = sprintf($lang->req_step_error_box, $lang->req_step_error_xmlsupport);
$xmlstatus = sprintf($lang->req_step_span_fail, $lang->not_installed);
$showerror = 1;
}
else
{
$xmlstatus = sprintf($lang->req_step_span_pass, $lang->installed);
}
// Check config file is writable
/*
$configwritable = @fopen(MYBB_ROOT.'inc/config.php', 'w');
if(!$configwritable)
{
$errors[] = sprintf($lang->req_step_error_box, $lang->req_step_error_configfile);
$configstatus = sprintf($lang->req_step_span_fail, $lang->not_writable);
$showerror = 1;
}
else
{
$configstatus = sprintf($lang->req_step_span_pass, $lang->writable);
}
@fclose($configwritable);
// Check settings file is writable
$settingswritable = @fopen(MYBB_ROOT.'inc/settings.php', 'w');
if(!$settingswritable)
{
$errors[] = sprintf($lang->req_step_error_box, $lang->req_step_error_settingsfile);
$settingsstatus = sprintf($lang->req_step_span_fail, $lang->not_writable);
$showerror = 1;
}
else
{
$settingsstatus = sprintf($lang->req_step_span_pass, $lang->writable);
}
@fclose($settingswritable);
*/
// Check upload directory is writable
$uploadswritable = @fopen(MYBB_ROOT.'uploads/test.write', 'w');
if(!$uploadswritable)
{
$errors[] = sprintf($lang->req_step_error_box, $lang->req_step_error_uploaddir);
$uploadsstatus = sprintf($lang->req_step_span_fail, $lang->not_writable);
$showerror = 1;
@fclose($uploadswritable);
}
else
{
$uploadsstatus = sprintf($lang->req_step_span_pass, $lang->writable);
@fclose($uploadswritable);
@chmod(MYBB_ROOT.'uploads', 0777);
@chmod(MYBB_ROOT.'uploads/test.write', 0777);
@unlink(MYBB_ROOT.'uploads/test.write');
}
// Check avatar directory is writable
$avatarswritable = @fopen(MYBB_ROOT.'uploads/avatars/test.write', 'w');
if(!$avatarswritable)
{
$errors[] = sprintf($lang->req_step_error_box, $lang->req_step_error_avatardir);
$avatarsstatus = sprintf($lang->req_step_span_fail, $lang->not_writable);
$showerror = 1;
@fclose($avatarswritable);
}
else
{
$avatarsstatus = sprintf($lang->req_step_span_pass, $lang->writable);
@fclose($avatarswritable);
@chmod(MYBB_ROOT.'uploads/avatars', 0777);
@chmod(MYBB_ROOT.'uploads/avatars/test.write', 0777);
@unlink(MYBB_ROOT.'uploads/avatars/test.write');
}
// Output requirements page
echo sprintf($lang->req_step_reqtable, $phpversion, $dbsupportlist, $xmlstatus, $uploadsstatus, $avatarsstatus);
if($showerror == 1)
{
$error_list = error_list($errors);
echo sprintf($lang->req_step_error_tablelist, $error_list);
$output->print_footer();
}
else
{
echo $lang->req_step_reqcomplete;
$output->print_footer('database_info');
}
}
function database_info()
{
global $output, $dbinfo, $errors, $mybb, $dboptions, $lang;
$mybb->input['action'] = 'database_info';
$output->print_header($lang->db_config, 'dbconfig');
// Check for errors from this stage
if(is_array($errors))
{
$error_list = error_list($errors);
echo sprintf($lang->db_step_error_config, $error_list);
$dbengine = $mybb->input['dbengine'];
$dbhost = $mybb->input['dbhost'];
$dbuser = $mybb->input['dbuser'];
$dbname = $mybb->input['dbname'];
$tableprefix = $mybb->input['tableprefix'];
$encoding = 'utf8';
}
else
{
echo $lang->db_step_config_db;
$dbhost = 'localhost';
$tableprefix = 'mybb_';
$dbuser = '';
$dbname = '';
$dbengine = 'mysql';
$encoding = 'utf8';
}
// Loop through database engines
foreach($dboptions as $dbfile => $dbtype)
{
if($dbengine != '' && $dbengine == $dbfile)
{
$dbengines .= "<option value=\"{$dbfile}\" selected=\"selected\">{$dbtype['title']}</option>";
}
else
{
$dbengines .= "<option value=\"{$dbfile}\">{$dbtype['title']}</option>";
}
}
$encodings_array = array(
'big5' => 'Big5 Traditional Chinese',
'dec8' => 'DEC West European',
'cp850' => 'DOS West European',
'hp8' => 'HP West European',
'koi8r' => 'KOI8-R Relcom Russian',
'latin1' => 'cp1252 West European',
'latin2' => 'ISO 8859-2 Central European',
'swe7' => '7bit Swedish',
'ascii' => 'US ASCII',
'ujis' => 'EUC-JP Japanese',
'sjis' => 'Shift-JIS Japanese',
'hebrew' => 'ISO 8859-8 Hebrew',
'tis620' => 'TIS620 Thai',
'euckr' => 'EUC-KR Korean',
'koi8u' => 'KOI8-U Ukrainian',
'gb2312' => 'GB2312 Simplified Chinese',
'greek' => 'ISO 8859-7 Greek',
'cp1250' => 'Windows Central European',
'gbk' => 'GBK Simplified Chinese',
'latin5' => 'ISO 8859-9 Turkish',
'armscii8' => 'ARMSCII-8 Armenian',
'utf8' => 'UTF-8 Unicode',
'ucs2' => 'UCS-2 Unicode',
'cp866' => 'DOS Russian',
'keybcs2' => 'DOS Kamenicky Czech-Slovak',
'macce' => 'Mac Central European',
'macroman' => 'Mac West European',
'cp852' => 'DOS Central European',
'latin7' => 'ISO 8859-13 Baltic',
'cp1251' => 'Windows Cyrillic',
'cp1256' => 'Windows Arabic',
'cp1257' => 'Windows Baltic',
'binary' => 'Binary pseudo charset',
'geostd8' => 'GEOSTD8 Georgian',
'cp932' => 'SJIS for Windows Japanese',
'eucjpms' => 'UJIS for Windows Japanese',
);
// Loop through database encodings
foreach($encodings_array as $key => $encodingtext)
{
if($key == $encoding)
{
$encodings .= "<option value=\"{$key}\" selected=\"selected\">{$encodingtext}</option>\n";
}
else
{
$encodings .= "utf8";
}
}
echo sprintf($lang->db_step_config_table, $dbengines, $dbhost, $dbuser, $dbname, $tableprefix, $encodings);
$output->print_footer('create_tables');
}
function create_tables()
{
global $output, $dbinfo, $errors, $mybb, $dboptions, $lang;
if(!$mybb->input['encoding'])
{
$errors[] = $lang->db_step_error_missingencoding;
}
if(!file_exists(MYBB_ROOT."inc/db_{$mybb->input['dbengine']}.php"))
{
$errors[] = $lang->db_step_error_invalidengine;
database_info();
}
// Attempt to connect to the db
require_once MYBB_ROOT."inc/db_{$mybb->input['dbengine']}.php";
$db = new databaseEngine;
$db->error_reporting = 0;
$connection = $db->connect($mybb->input['dbhost'], $mybb->input['dbuser'], $mybb->input['dbpass']);
if(!$connection)
{
$errors[] = sprintf($lang->db_step_error_noconnect, $mybb->input['dbhost']);
}
// Select the database
$dbselect = $db->select_db($mybb->input['dbname']);
if(!$dbselect)
{
$errors[] = sprintf($lang->db_step_error_nodbname, $mybb->input['dbname']);
}
if(is_array($errors))
{
database_info();
}
$collations = array(
'big5' => 'big5_chinese_ci',
'dec8' => 'dec8_swedish_ci',
'cp850' => 'cp850_general_ci',
'hp8' => 'hp8_english_ci',
'koi8r' => 'koi8r_general_ci',
'latin1' => 'latin1_swedish_ci',
'latin2' => 'latin2_general_ci',
'swe7' => 'swe7_swedish_ci',
'ascii' => 'ascii_general_ci',
'ujis' => 'ujis_japanese_ci',
'sjis' => 'sjis_japanese_ci',
'hebrew' => 'hebrew_general_ci',
'tis620' => 'tis620_thai_ci',
'euckr' => 'euckr_korean_ci',
'koi8u' => 'koi8u_general_ci',
'gb2312' => 'gb2312_chinese_ci',
'greek' => 'greek_general_ci',
'cp1250' => 'cp1250_general_ci',
'gbk' => 'gbk_chinese_ci',
'latin5' => 'latin5_turkish_ci',
'armscii8' => 'armscii8_general_ci',
'utf8' => 'utf8_general_ci',
'ucs2' => 'ucs2_general_ci',
'cp866' => 'cp866_general_ci',
'keybcs2' => 'keybcs2_general_ci',
'macce' => 'macce_general_ci',
'macroman' => 'macroman_general_ci',
'cp852' => 'cp852_general_ci',
'latin7' => 'latin7_general_ci',
'cp1251' => 'cp1251_general_ci',
'cp1256' => 'cp1256_general_ci',
'cp1257' => 'cp1257_general_ci',
'binary' => 'binary',
'geostd8' => 'geostd8_general_ci',
'cp932' => 'cp932_japanese_ci',
'eucjpms' => 'eucjpms_japanese_ci',
);
// Decide if we can use a database encoding or not
if(($db->title == "MySQLi" || $db->title == "MySQL") && $db->get_version() >= '4.1.0')
{
$db_encoding = "\$config['db_encoding'] = '{$mybb->input['encoding']}';";
$charset = " CHARACTER SET {$mybb->input['encoding']} COLLATE ".$collations[$mybb->input['encoding']];
}
else
{
$db_encoding = "// \$config['db_encoding'] = '{$mybb->input['encoding']}';";
$charset = "";
}
// Write the temp install multiforum configuration file
$configdata = "<?php
/**
* Database configuration
*/
\$config['dbtype'] = '{$mybb->input['dbengine']}';
\$config['hostname'] = '{$mybb->input['dbhost']}';
\$config['username'] = '{$mybb->input['dbuser']}';
\$config['password'] = '{$mybb->input['dbpass']}';
\$config['database'] = '{$mybb->input['dbname']}';
\$config['table_prefix'] = '{$mybb->input['tableprefix']}';
/**
* Admin CP directory
* For security reasons, it is recommended you
* rename your Admin CP directory. You then need
* to adjust the value below to point to the
* new directory.
*/
\$config['admin_dir'] = 'admin';
/**
* Hide all Admin CP links
* If you wish to hide all Admin CP links
* on the front end of the board after
* renaming your Admin CP directory, set this
* to 1.
*/
\$config['hide_admin_links'] = 0;
/**
* Data-cache configuration
* The data cache is a temporary cache
* of the most commonly accessed data in MyBB.
* By default, the database is used to store this data.
*
* If you wish to use the file system (inc/cache directory)
* you can change the value below to 'files' from 'db'.
*/
\$config['cache_store'] = 'db';
/**
* Super Administrators
* A comma separated list of user IDs who cannot
* be edited, deleted or banned in the Admin CP.
* The administrator permissions for these users
* cannot be altered either.
*/
\$config['super_admins'] = '1';
/**
* Database Encoding
* If you wish to set an encoding for MyBB uncomment
* the line below (if it isn't already) and change
* the current value to the mysql charset:
* http://dev.mysql.com/doc/refman/5.1/en/charset-mysql.html
*/
{$db_encoding}
?>";
$file = fopen('../inc/mconfig.php', 'w');
fwrite($file, $configdata);
fclose($file);
//echo '<br>The user prefix is: '.$mybb->input['tableprefix'].'<br>';
$userPrefix = $mybb->input['tableprefix'];
//echo '<br>The userPrefix is: '.$userPrefix.'<br>';
$output->print_header($lang->table_creation, 'createtables');
echo sprintf($lang->tablecreate_step_connected, $dboptions[$mybb->input['dbengine']]['title'], $db->get_version());
if($dboptions[$config['dbtype']]['structure_file'])
{
$structure_file = $dboptions[$config['dbtype']]['structure_file'];
}
else
{
$structure_file = 'mysql_db_tables.php';
}
//echo '<br>Writing HTACCESS<br>';
//echo '<br>Table Prefix for Write is: '.$mybb->input['tableprefix'];
//Write our htaccess code while we're golden
//Append data to a file
//Simulate a table name
$go = $mybb->input['tableprefix']; //Our table prefix from the installer
//Get rid of the underscore before writing to htaccess file
$go = preg_replace("/[^a-zA-Z0-9s]/", "", $go);
//Open file for writing
$ourFileName = '../../.htaccess';
$fh = fopen($ourFileName, 'a') or die("Can't open file to write forum alias");
//Start Writing
$write1 = "\nRewriteCond %{SCRIPT_FILENAME} !-f\n";
fwrite($fh, $write1);
$write2 = "RewriteCond %{SCRIPT_FILENAME} !-d\n";
fwrite($fh, $write2);
$write3 = 'RewriteRule ^mybb/'.$go.'/(.+)$ /mybb/$1 [QSA,L]';
fwrite($fh, $write3);
fclose($fh); //Close the file
//
require_once INSTALL_ROOT."/resources/{$structure_file}";
$userPrefix = $mybb->input['tableprefix'];
foreach($tables as $val)
{
$val = preg_replace('#mybb_(\S+?)([\s\.,]|$)#', $mybb->input['tableprefix'].'\\1\\2', $val);
preg_match('#CREATE TABLE (\S+) \(#i', $val, $match);
if($match[1])
{
$db->query('DROP TABLE IF EXISTS '.$match[1]);
echo sprintf($lang->tablecreate_step_created, $match[1]);
}
$db->query($val);
if($match[1])
{
echo $lang->done . "<br />\n";
}
}
echo $lang->tablecreate_step_done;
$output->print_footer('populate_tables');
}
function populate_tables()
{
global $output, $lang, $config;
require MYBB_ROOT.'inc/mconfig.php';
$db = db_connection($config);
//echo '<br>The user prefix is: '.$tableprefix.'<br>';
//echo '<br>The config table prefix is: '.$config['table_prefix'].'<br>';
$output->print_header($lang->table_population, 'tablepopulate');
echo sprintf($lang->populate_step_insert);
if($dboptions[$config['dbtype']]['population_file'])
{
$population_file = $dboptions[$config['dbtype']]['population_file'];
}
else
{
$population_file = 'mysql_db_inserts.php';
}
require_once INSTALL_ROOT."/resources/{$population_file}";
foreach($inserts as $val)
{
$val = preg_replace('#mybb_(\S+?)([\s\.,]|$)#', $config['table_prefix'].'\\1\\2', $val);
//$val = preg_replace('#mybb_(\S+?)([\s\.,]|$)#', $userPrefix.'\\1\\2', $val);
$db->query($val);
}
echo $lang->populate_step_inserted;
$output->print_footer('templates');
}
function insert_templates()
{
global $output, $cache, $db, $lang, $config;
require MYBB_ROOT.'inc/mconfig.php';
$db = db_connection($config);
require_once MYBB_ROOT.'inc/class_datacache.php';
$cache = new datacache;
$output->print_header($lang->theme_installation, 'theme');
echo $lang->theme_step_importing;
$db->query("DELETE FROM ".TABLE_PREFIX."themes");
$db->query("DELETE FROM ".TABLE_PREFIX."templates");
$db->query("INSERT INTO ".TABLE_PREFIX."themes (name,pid,css,cssbits,themebits,extracss,allowedgroups) VALUES ('MyBB Master Style','0','','','','','')");
$db->query("INSERT INTO ".TABLE_PREFIX."themes (name,pid,def,css,cssbits,themebits,extracss,allowedgroups) VALUES ('MyBB Default','1','1','','','','','')");
$db->query("INSERT INTO ".TABLE_PREFIX."templatesets (title) VALUES ('Default Templates');");
$templateset = $db->insert_id();
$contents = @file_get_contents(INSTALL_ROOT.'/resources/mybb_theme.xml');
$parser = new XMLParser($contents);
$tree = $parser->get_tree();
$theme = $tree['theme'];
$css = kill_tags($theme['cssbits']);
$themebits = kill_tags($theme['themebits']);
$templates = $theme['templates']['template'];
$themebits['templateset'] = $templateset;
$sid = -2;
foreach($templates as $template)
{
$templatename = $template['attributes']['name'];
$templatevalue = $db->escape_string($template['value']);
$templateversion = $template['attributes']['version'];
$time = time();
$db->query("INSERT INTO ".TABLE_PREFIX."templates (title,template,sid,version,status,dateline) VALUES ('{$templatename}','{$templatevalue}','{$sid}','{$templateversion}','','{$time}')");
}
update_theme(1, 0, $themebits, $css, 0);
echo $lang->theme_step_imported;
$output->print_footer('configuration');
}
function configure()
{
global $output, $mybb, $errors, $lang;
$output->print_header($lang->board_config, 'config');
// If board configuration errors
if(is_array($errors))
{
$error_list = error_list($errors);
echo sprintf($lang->config_step_error_config, $error_list);
$