/** * Uploads a sql script and executes it. Script can be text file or zip/gz packed * * @static * @param array The installation variables * @param boolean true if the script is a migration script * @return string Success or error messages * @since 1.5 */ function uploadSql(&$args, $migration = false, $preconverted = false) { global $mainframe; $archive = ''; $script = ''; /* * Check for iconv */ if ($migration && !$preconverted && !function_exists('iconv')) { return JText::_('WARNICONV'); } /* * Get the uploaded file information */ if ($migration) { $sqlFile = JRequest::getVar('migrationFile', '', 'files', 'array'); } else { $sqlFile = JRequest::getVar('sqlFile', '', 'files', 'array'); } /* * Make sure that file uploads are enabled in php */ if (!(bool) ini_get('file_uploads')) { return JText::_('WARNINSTALLFILE'); } /* * Make sure that zlib is loaded so that the package can be unpacked */ if (!extension_loaded('zlib')) { return JText::_('WARNINSTALLZLIB'); } /* * If there is no uploaded file, we have a problem... */ if (!is_array($sqlFile) || $sqlFile['size'] < 1) { return JText::_('WARNNOFILE'); } /* * Move uploaded file */ // Set permissions for tmp dir JInstallationHelper::_chmod(JPATH_SITE . DS . 'tmp', 0777); jimport('joomla.filesystem.file'); $uploaded = JFile::upload($sqlFile['tmp_name'], JPATH_SITE . DS . 'tmp' . DS . $sqlFile['name']); if (!$uploaded) { return JText::_('WARNUPLOADFAILURE'); } if (!eregi('.sql$', $sqlFile['name'])) { $archive = JPATH_SITE . DS . 'tmp' . DS . $sqlFile['name']; } else { $script = JPATH_SITE . DS . 'tmp' . DS . $sqlFile['name']; } // unpack archived sql files if ($archive) { $package = JInstallationHelper::unpack($archive, $args); if ($package === false) { return JText::_('WARNUNPACK'); } $script = $package['folder'] . DS . $package['script']; } $db =& JInstallationHelper::getDBO($args['DBtype'], $args['DBhostname'], $args['DBuserName'], $args['DBpassword'], $args['DBname'], $args['DBPrefix']); /* * If migration perform manipulations on script file before population */ if ($migration) { $script = JInstallationHelper::preMigrate($script, $args, $db); if ($script == false) { return JText::_('Script operations failed'); } } $errors = null; $msg = ''; $result = JInstallationHelper::populateDatabase($db, $script, $errors); /* * If migration, perform post population manipulations (menu table construction) */ $migErrors = null; if ($migration) { $migResult = JInstallationHelper::postMigrate($db, $migErrors, $args); if ($migResult != 0) { /* * Merge populate and migrate processing errors */ if ($result == 0) { $result = $migResult; $errors = $migErrors; } else { $result += $migResult; $errors = array_merge($errors, $migErrors); } } } /* * prepare sql error messages if returned from populate and migrate */ if (!is_null($errors)) { foreach ($errors as $error) { $msg .= stripslashes($error['msg']); $msg .= chr(13) . "-------------" . chr(13); $txt = '<textarea cols="40" rows="4" name="instDefault" readonly="readonly" >' . JText::_("Database Errors Reported") . chr(13) . $msg . '</textarea>'; } } else { // consider other possible errors from populate $msg = $result == 0 ? JText::_('SQL script installed successfully') : JText::_('Error installing SQL script'); $txt = '<input size="50" value="' . $msg . '" readonly="readonly" />'; } /* * Clean up */ if ($archive) { JFile::delete($archive); JFolder::delete($package['folder']); } else { JFile::delete($script); } return $txt; }
echo ("<p class=\"error\">". JText::_('STOPPEDATLINE') ." $linenumber. </p>"); echo ("<p>". JText::sprintf('TOOMANYLINES',MAX_QUERY_LINES)."</p>"); $error = true; break; } $vars = $this->getVars(); $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'); //$migration = JArrayHelper::getValue($vars, 'migration', '0'); $migration = JRequest::getVar( 'migration', 0, 'post', 'bool' ); $db = & JInstallationHelper::getDBO('mysqli', $DBhostname, $DBuserName, $DBpassword, $DBname, $DBPrefix); if(JError::isError($db)) jexit(JText::_('CONNECTION FAIL')); // echo 'Done.<br />'; // Execute query if end of query detected (; as last character) AND NOT in parents if (preg_match('/;$/', trim($dumpline)) && !$inparents) { if (!TESTMODE) { $db->setQuery(trim($query)); // echo $query . '<br />'; if (!$db->Query()) { echo ("<p class=\"error\">".JText::_('Error at the line') ." $linenumber: ". trim($dumpline) . "</p>\n"); echo ("<p>".JText::_('Query:') . trim(nl2br(htmlentities($query))) ."</p>\n"); echo ("<p>MySQL: " . mysql_error() . "</p>\n"); $error = true; break;
/** * Method to load and execute a sql script */ function sampledata($args) { jimport('joomla.database.database'); jimport('joomla.language.language'); jimport('joomla.registry.registry'); $errors = null; $msg = ''; $objResponse = new xajaxResponse(); $lang = new JAJAXLang($args['lang']); // $lang->setDebug(true); /* * execute the default sample data file */ $type = $args['DBtype']; if ($type == 'mysqli') { $type = 'mysql'; } $dbsample = '../sql' . DS . $type . DS . 'sample_data.sql'; $db =& JInstallationHelper::getDBO($args['DBtype'], $args['DBhostname'], $args['DBuserName'], $args['DBpassword'], $args['DBname'], $args['DBPrefix']); $result = JInstallationHelper::populateDatabase($db, $dbsample, $errors); /* * prepare sql error messages if returned from populate */ if (!is_null($errors)) { foreach ($errors as $error) { $msg .= stripslashes($error['msg']); $msg .= chr(13) . "-------------" . chr(13); $txt = '<textarea cols="35" rows="5" name="instDefault" readonly="readonly" >' . $lang->_('Database Errors Reported') . chr(13) . $msg . '</textarea>'; } } else { // consider other possible errors from populate $msg = $result == 0 ? $lang->_("Sample data installed successfully") : $lang->_("Error installing SQL script"); $txt = '<input size="35" name="instDefault" value="' . $msg . '" readonly="readonly" />'; } $objResponse->addAssign("theDefault", "innerHTML", $txt); return $objResponse; }
protected function _configure() { $dialog = $this->getHelperSet()->get('dialog'); $output = $this->_output; $input = $this->_input; $prompt = function ($key, $text, $default = null, $error = null) use($dialog, $output, $input) { $result = $input->getOption($key); if (empty($result) && !$input->getOption('no-interaction')) { if (!empty($default)) { $text .= '(default: ' . $default . ') '; } while (strlen($result = $dialog->ask($output, '<info>' . $text . '</info>', $default)) == 0) { } } elseif (empty($result)) { $result = $default; if (empty($result)) { $output->writeLn('<error>' . $error . '</error>'); exit(1); } } return $result; }; $config = new Config(WWW_ROOT); $info = $config->getDatabaseInfo(); $config->setDatabaseInfo(array('name' => $prompt('database-name', 'Enter the name of the database? ', @$info['name'], 'Please enter the database name'), 'user' => $prompt('database-user', 'Enter the database user? ', @$info['user'], 'Please enter the database user'), 'password' => $prompt('database-password', 'Enter the database password? ', @$info['password'], 'Please enter the database password'), 'host' => $prompt('database-host', 'Enter the database host address? ', '127.0.0.1', @$info['host']), 'port' => $prompt('database-port', 'Enter the database port? ', '3306', @$info['port']), 'prefix' => $prompt('database-prefix', 'Enter a prefix for the tables in the database? ', 'jos_', @$info['prefix']))); define('DS', DIRECTORY_SEPARATOR); define('_JEXEC', 1); define('JPATH_BASE', WWW_ROOT); define('JPATH_ROOT', JPATH_BASE); define('JPATH_SITE', JPATH_ROOT); define('JPATH_CONFIGURATION', JPATH_ROOT); define('JPATH_ADMINISTRATOR', JPATH_ROOT . '/administrator'); define('JPATH_XMLRPC', JPATH_ROOT . '/xmlrpc'); define('JPATH_LIBRARIES', JPATH_ROOT . '/libraries'); define('JPATH_PLUGINS', JPATH_ROOT . '/plugins'); define('JPATH_INSTALLATION', JPATH_ROOT . '/installation'); define('JPATH_THEMES', JPATH_BASE . '/templates'); define('JPATH_CACHE', JPATH_BASE . '/cache'); include_once JPATH_LIBRARIES . '/joomla/import.php'; require_once 'Console/Installer/Helper.php'; $output->writeLn('<info>connecting to database...</info>'); $errors = array(); $database = $config->getDatabaseInfo(); $db = \JInstallationHelper::getDBO('mysqli', $database['host'] . ':' . $database['port'], $database['user'], $database['password'], $database['name'], $database['prefix'], false); if ($db instanceof \JException) { $output->writeLn('<error>' . $db->toString() . '</error>'); exit(1); } $db_exists = \JInstallationHelper::databaseExists($db, $database['name']); $dump_file = null; if ($input->getOption('database-dump')) { $dump_file = realpath($input->getOption('database-dump')); } if ($db_exists && $input->getOption('drop-database')) { $output->writeLn('<fg=red>Dropping existing database...</fg=red>'); \JInstallationHelper::deleteDatabase($db, $database['name'], $database['prefix'], $errors); $db_exists = false; } if (!$db_exists) { $output->writeLn('<info>Creating new database...</info>'); \JInstallationHelper::createDatabase($db, $database['name'], true); $db->select($database['name']); $sql_files = $dump_file ? array($dump_file) : array_map(function ($file) { return $file = ANAHITA_ROOT . "/vendor/joomla/installation/sql/{$file}"; }, array("schema.sql", "data.sql")); $output->writeLn('<info>Populating database...</info>'); array_walk($sql_files, function ($file) use($db) { \JInstallationHelper::populateDatabase($db, $file, $errors); }); } jimport('joomla.user.helper'); $config->secret = \JUserHelper::genRandomPassword(32); //exec("rm -rf ".JPATH_ROOT."/installation"); $config->save(); $output->writeLn("<info>Congratulations you're done.</info>"); if (!$db_exists && !$dump_file) { $output->writeLn("<info>The first person who registers for an account becomes the Super Administrator. Point your browser to http://yoursite/people/signup and create a new account.</info>"); } }
function postMigrate() { $migErrors = null; $args =& $this->getVars(); $db =& JInstallationHelper::getDBO($args['DBtype'], $args['DBhostname'], $args['DBuserName'], $args['DBpassword'], $args['DBname'], $args['DBPrefix']); $migResult = JInstallationHelper::postMigrate($db, $migErrors, $args); if (!$migResult) { echo JText::_("Migration Successful"); } else { echo '<div id="installer">'; echo '<p>' . JText::_('Migration failed') . ':</p>'; foreach ($migErrors as $error) { echo '<p>' . $error['msg'] . '</p>'; } echo '</div>'; } return $migResult; }
$cmd .= " | sed -e 's/`{$config->prefix}/`#__/'"; } if ($file) { @mkdir(dirname($file), 0755, true); system("{$cmd} > {$file}"); } else { passthru($cmd); } }); } $console->register('db:load')->setDescription('Load data from a sql file into the database')->setDefinition(array(new InputArgument('file', InputArgument::REQUIRED, 'The output file')))->setCode(function (InputInterface $input, OutputInterface $output) use($console) { $file = realpath($input->getArgument('file')); if (!file_exists($file)) { throw new \Exception("File '{$file}' doesn't exists"); } require_once 'Console/Installer/Helper.php'; $console->loadFramework(); $config = new Config(WWW_ROOT); $database = $config->getDatabaseInfo(); $errors = array(); $db = \JInstallationHelper::getDBO('mysqli', $database['host'] . ':' . $database['port'], $database['user'], $database['password'], $database['name'], $database['prefix'], true); if ($db instanceof \JException) { $output->writeLn('<error>' . $db->toString() . '</error>'); exit(1); } if ($input->getOption('drop-tables')) { \JInstallationHelper::deleteDatabase($db, $database['name'], $database['prefix'], $errors); } $output->writeLn('<info>Loading data. This may take a while...</info>'); \JInstallationHelper::populateDatabase($db, $file, $errors); });
function postMigrate() { $migErrors = null; $args =& $this->getVars(); $db =& JInstallationHelper::getDBO($args['DBtype'], $args['DBhostname'], $args['DBuserName'], $args['DBpassword'], $args['DBname'], $args['DBPrefix']); $migResult = JInstallationHelper::postMigrate($db, $migErrors, $args); // Clean up the migration SQL file $migratePath = JPATH_BASE . DS . 'sql' . DS . 'migration' . DS . 'migrate.sql'; jimport('joomla.filesystem.file'); if (JFile::exists($migratePath)) { JFile::delete($migratePath); } if ($migResult) { echo '<div id="installer">'; echo '<p>' . JText::_('Migration failed') . ':</p>'; foreach ($migErrors as $error) { echo '<p>' . $error['msg'] . '</p>'; } echo '</div>'; } else { echo JText::_("Migration Successful"); } return $migResult; }