/** * Gets the parameters for database creation * * @return boolean True if successful * @access public * @since 1.5 */ function makeDB($vars = false) { global $mainframe; // Initialize variables if ($vars === false) { $vars = $this->getVars(); } $errors = null; $lang = JArrayHelper::getValue($vars, 'lang', 'en-GB'); $DBcreated = JArrayHelper::getValue($vars, 'DBcreated', '0'); $DBtype = JArrayHelper::getValue($vars, 'DBtype', 'mysql'); $DBhostname = JArrayHelper::getValue($vars, 'DBhostname', ''); $DBuserName = JArrayHelper::getValue($vars, 'DBuserName', ''); $DBpassword = JArrayHelper::getValue($vars, 'DBpassword', ''); $DBname = JArrayHelper::getValue($vars, 'DBname', ''); $DBPrefix = JArrayHelper::getValue($vars, 'DBPrefix', 'jos_'); $DBOld = JArrayHelper::getValue($vars, 'DBOld', 'bu'); $DBversion = JArrayHelper::getValue($vars, 'DBversion', ''); // these 3 errors should be caught by the javascript in dbConfig if ($DBtype == '') { $this->setError(JText::_('validType')); $this->setData('back', 'dbconfig'); $this->setData('errors', $errors); return false; //return JInstallationView::error($vars, JText::_('validType'), 'dbconfig'); } if (!$DBhostname || !$DBuserName || !$DBname) { $this->setError(JText::_('validDBDetails')); $this->setData('back', 'dbconfig'); $this->setData('errors', $errors); return false; //return JInstallationView::error($vars, JText::_('validDBDetails'), 'dbconfig'); } if ($DBname == '') { $this->setError(JText::_('emptyDBName')); $this->setData('back', 'dbconfig'); $this->setData('errors', $errors); return false; //return JInstallationView::error($vars, JText::_('emptyDBName'), 'dbconfig'); } if (!preg_match('#^[a-zA-Z]+[a-zA-Z0-9_]*$#', $DBPrefix)) { $this->setError(JText::_('MYSQLPREFIXINVALIDCHARS')); $this->setData('back', 'dbconfig'); $this->setData('errors', $errors); return false; } if (strlen($DBPrefix) > 15) { $this->setError(JText::_('MYSQLPREFIXTOOLONG')); $this->setData('back', 'dbconfig'); $this->setData('errors', $errors); return false; } if (strlen($DBname) > 64) { $this->setError(JText::_('MYSQLDBNAMETOOLONG')); $this->setData('back', 'dbconfig'); $this->setData('errors', $errors); return false; } if (!$DBcreated) { $DBselect = false; $db =& JInstallationHelper::getDBO($DBtype, $DBhostname, $DBuserName, $DBpassword, null, $DBPrefix, $DBselect); if (JError::isError($db)) { // connection failed $this->setError(JText::sprintf('WARNNOTCONNECTDB', $db->toString())); $this->setData('back', 'dbconfig'); $this->setData('errors', $db->toString()); return false; } if ($err = $db->getErrorNum()) { // connection failed $this->setError(JText::sprintf('WARNNOTCONNECTDB', $db->getErrorNum())); $this->setData('back', 'dbconfig'); $this->setData('errors', $db->getErrorMsg()); return false; } //Check utf8 support of database $DButfSupport = $db->hasUTF(); // Try to select the database if (!$db->select($DBname)) { if (JInstallationHelper::createDatabase($db, $DBname, $DButfSupport)) { $db->select($DBname); /* // make the new connection to the new database $db = NULL; $db = & JInstallationHelper::getDBO($DBtype, $DBhostname, $DBuserName, $DBpassword, $DBname, $DBPrefix); */ } else { $this->setError(JText::sprintf('WARNCREATEDB', $DBname)); $this->setData('back', 'dbconfig'); $this->setData('errors', $db->getErrorMsg()); return false; //return JInstallationView::error($vars, array (JText::sprintf('WARNCREATEDB', $DBname)), 'dbconfig', $error); } } else { // pre-existing database - need to set character set to utf8 // will only affect MySQL 4.1.2 and up JInstallationHelper::setDBCharset($db, $DBname); } $db =& JInstallationHelper::getDBO($DBtype, $DBhostname, $DBuserName, $DBpassword, $DBname, $DBPrefix); if ($DBOld == 'rm') { if (JInstallationHelper::deleteDatabase($db, $DBname, $DBPrefix, $errors)) { $this->setError(JText::_('WARNDELETEDB')); $this->setData('back', 'dbconfig'); $this->setData('errors', $errors); return false; //return JInstallationView::error($vars, , 'dbconfig', JInstallationHelper::errors2string($errors)); } } else { /* * We assume since we aren't deleting the database that we need * to back it up :) */ if (JInstallationHelper::backupDatabase($db, $DBname, $DBPrefix, $errors)) { $this->setError(JText::_('WARNBACKINGUPDB')); $this->setData('back', 'dbconfig'); $this->setData('errors', JInstallationHelper::errors2string($errors)); return false; //return JInstallationView::error($vars, JText::_('WARNBACKINGUPDB'), 'dbconfig', JInstallationHelper::errors2string($errors)); } } $type = $DBtype; if ($type == 'mysqli') { $type = 'mysql'; } // set collation and use utf-8 compatibile script if appropriate if ($DButfSupport) { $dbscheme = 'sql' . DS . $type . DS . 'joomla.sql'; } else { $dbscheme = 'sql' . DS . $type . DS . 'joomla_backward.sql'; } if (JInstallationHelper::populateDatabase($db, $dbscheme, $errors) > 0) { $this->setError(JText::_('WARNPOPULATINGDB')); $this->setData('back', 'dbconfig'); $this->setData('errors', JInstallationHelper::errors2string($errors)); return false; //return JInstallationView::error($vars, JText::_('WARNPOPULATINGDB'), 'dbconfig', JInstallationHelper::errors2string($errors)); } // Load the localise.sql for translating the data in joomla.sql/joomla_backwards.sql // This feature is available for localized version of Joomla! 1.5 jimport('joomla.filesystem.file'); $dblocalise = 'sql' . DS . $type . DS . 'localise.sql'; if (JFile::exists($dblocalise)) { if (JInstallationHelper::populateDatabase($db, $dblocalise, $errors) > 0) { $this->setError(JText::_('WARNPOPULATINGDB')); $this->setData('back', 'dbconfig'); $this->setData('errors', JInstallationHelper::errors2string($errors)); return false; } } // Handle default backend language setting. This feature is available for // localized versions of Joomla! 1.5. $langfiles = $mainframe->getLocaliseAdmin(); if (in_array($lang, $langfiles['admin']) || in_array($lang, $langfiles['site'])) { // Determine the language settings $param[] = array(); if (in_array($lang, $langfiles['admin'])) { $langparam[] = "administrator={$lang}"; } if (in_array($lang, $langfiles['site'])) { $langparam[] = "site={$lang}"; } $langparams = implode("\n", $langparam); // Because database config has not yet been set we just // do the trick by a plain update of the proper record. $where[] = "`option`='com_languages'"; $where = count($where) ? ' WHERE ' . implode(' AND ', $where) : ''; $query = "UPDATE #__components " . "SET params='{$langparams}'" . $where; $db->setQuery($query); if (!$db->query()) { return false; } } } return true; }