public function run($sArgument) { if (!isset($sArgument) || !isset($sArgument[0]) || !isset($sArgument[1]) || !isset($sArgument[2]) || !isset($sArgument[3])) { die('You have to set admin/password/full name and email address on the command line like this: php starter.php adminname mypassword fullname emailaddress'); } Yii::import('application.helpers.common_helper', true); try { $this->connection = App()->getDb(); $this->connection->active = true; } catch (CDbException $e) { $this->createDatabase(); } $this->connection->charset = 'utf8'; switch ($this->connection->driverName) { case 'mysql': case 'mysqli': $this->connection->createCommand("ALTER DATABASE " . $this->connection->quoteTableName($this->getDBConnectionStringProperty('dbname')) . " DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;")->execute(); $sql_file = 'mysql'; break; case 'pgsql': if (version_compare($this->connection->getServerVersion(), '9', '>=')) { $this->connection->createCommand("ALTER DATABASE " . $this->connection->quoteTableName($this->getDBConnectionStringProperty('dbname')) . " SET bytea_output='escape';")->execute(); } $sql_file = 'pgsql'; break; case 'dblib': case 'mssql': case 'sqlsrv': $sql_file = 'mssql'; break; default: throw new Exception(sprintf('Unknown database type "%s".', $this->connection->driverName)); } $this->_executeSQLFile(dirname(Yii::app()->basePath) . '/installer/sql/create-' . $sql_file . '.sql'); $this->connection->createCommand()->insert($this->connection->tablePrefix . 'users', array('users_name' => $sArgument[0], 'password' => hash('sha256', $sArgument[1]), 'full_name' => $sArgument[2], 'parent_id' => 0, 'lang' => 'auto', 'email' => $sArgument[3])); $this->connection->createCommand()->insert($this->connection->tablePrefix . 'permissions', array('entity' => 'global', 'entity_id' => 0, 'uid' => 1, 'permission' => 'superadmin', 'create_p' => 0, 'read_p' => 1, 'update_p' => 0, 'delete_p' => 0, 'import_p' => 0, 'export_p' => 0)); }
/** * Installer::_setup_tables() * Function that actually modify the database. Read $sqlfile and execute it. * @param string $sqlfile * @return Empty string if everything was okay - otherwise the error messages */ function _setup_tables($sFileName, $aDbConfig = array(), $sDatabasePrefix = '') { extract(empty($aDbConfig) ? self::_getDatabaseConfig() : $aDbConfig); switch ($sDatabaseType) { case 'mysql': case 'mysqli': $this->connection->createCommand("ALTER DATABASE " . $this->connection->quoteTableName($sDatabaseName) . " DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;")->execute(); break; case 'pgsql': if (version_compare($this->connection->getServerVersion(), '9', '>=')) { $this->connection->createCommand("ALTER DATABASE " . $this->connection->quoteTableName($sDatabaseName) . " SET bytea_output='escape';")->execute(); } break; } return $this->_executeSQLFile($sFileName, $sDatabasePrefix); }
/** * Configure database screen */ private function stepDatabaseConfiguration() { $this->loadHelper('surveytranslator'); $aData['clang'] = $clang = $this->lang; // usual data required by view $aData['title'] = $clang->gT('Database configuration'); $aData['descp'] = $clang->gT('Please enter the database settings you want to use for LimeSurvey:'); $aData['classesForStep'] = array('off', 'off', 'off', 'on', 'off', 'off'); $aData['progressValue'] = 40; $aData['model'] = $oModel = new InstallerConfigForm(); if (isset(Yii::app()->session['populateerror'])) { $oModel->addError('dblocation', Yii::app()->session['populateerror']); $oModel->addError('dbpwd', ''); $oModel->addError('dbuser', ''); unset(Yii::app()->session['populateerror']); } if (isset($_POST['InstallerConfigForm'])) { $oModel->attributes = $_POST['InstallerConfigForm']; //run validation, if it fails, load the view again else proceed to next step. if ($oModel->validate()) { $sDatabaseType = $oModel->dbtype; $sDatabaseName = $oModel->dbname; $sDatabaseUser = $oModel->dbuser; $sDatabasePwd = $oModel->dbpwd; $sDatabasePrefix = $oModel->dbprefix; $sDatabaseLocation = $oModel->dblocation; $sDatabasePort = ''; if (strpos($sDatabaseLocation, ':') !== false) { list($sDatabaseLocation, $sDatabasePort) = explode(':', $sDatabaseLocation, 2); } else { $sDatabasePort = self::_getDbPort($sDatabaseType, $sDatabasePort); } $bDBExists = false; $bDBConnectionWorks = false; $aDbConfig = compact('sDatabaseType', 'sDatabaseName', 'sDatabaseUser', 'sDatabasePwd', 'sDatabasePrefix', 'sDatabaseLocation', 'sDatabasePort'); if (self::_dbConnect($aDbConfig, array())) { $bDBExists = true; $bDBConnectionWorks = true; } else { $aDbConfig['sDatabaseName'] = ''; if (self::_dbConnect($aDbConfig, array())) { $bDBConnectionWorks = true; } else { $oModel->addError('dblocation', $clang->gT('Connection with database failed. Please check database location, user name and password and try again.')); $oModel->addError('dbpwd', ''); $oModel->addError('dbuser', ''); } } //if connection with database fail if ($bDBConnectionWorks) { //saving the form data foreach (array('dbname', 'dbtype', 'dbpwd', 'dbuser', 'dbprefix') as $sStatusKey) { Yii::app()->session[$sStatusKey] = $oModel->{$sStatusKey}; } Yii::app()->session['dbport'] = $sDatabasePort; Yii::app()->session['dblocation'] = $sDatabaseLocation; //check if table exists or not $bTablesDoNotExist = false; // Check if the surveys table exists or not if ($bDBExists == true) { try { if ($dataReader = $this->connection->createCommand()->select()->from('{{users}}')->query()->rowCount == 0) { // DBLIB does not throw an exception on a missing table $bTablesDoNotExist = true; } } catch (Exception $e) { $bTablesDoNotExist = true; } } $bDBExistsButEmpty = $bDBExists && $bTablesDoNotExist; //store them in session Yii::app()->session['databaseexist'] = $bDBExists; Yii::app()->session['tablesexist'] = !$bTablesDoNotExist; // If database is up to date, redirect to administration screen. if ($bDBExists && !$bTablesDoNotExist) { Yii::app()->session['optconfig_message'] = sprintf('<b>%s</b>', $clang->gT('The database you specified does already exist.')); Yii::app()->session['step3'] = true; //wrte config file! as we no longer redirect to optional view $this->_writeConfigFile(); //$this->redirect(array("installer/loadOptView")); header("refresh:5;url=" . $this->createUrl("/admin")); echo sprintf($clang->gT('The database does exists and contains LimeSurvey tables. You\'ll be redirected to the database update or (if your database is already up to date) to the administration login in 5 seconds. If not, please click <a href="%s">here</a>.', 'unescaped'), $this->createUrl("/admin")); exit; } if (in_array($oModel->dbtype, array('mysql', 'mysqli'))) { //for development - use mysql in the strictest mode //Checked) if (Yii::app()->getConfig('debug') > 1) { $this->connection->createCommand("SET SESSION SQL_MODE='STRICT_ALL_TABLES,ANSI'")->execute(); } $sMySQLVersion = $this->connection->getServerVersion(); if (version_compare($sMySQLVersion, '4.1', '<')) { die("<br />Error: You need at least MySQL version 4.1 to run LimeSurvey. Your version:" . $sMySQLVersion); } @$this->connection->createCommand("SET CHARACTER SET 'utf8'")->execute(); //Checked @$this->connection->createCommand("SET NAMES 'utf8'")->execute(); //Checked } // Setting dateformat for mssql driver. It seems if you don't do that the in- and output format could be different if (in_array($oModel->dbtype, array('mssql', 'sqlsrv', 'dblib'))) { @$this->connection->createCommand('SET DATEFORMAT ymd;')->execute(); //Checked @$this->connection->createCommand('SET QUOTED_IDENTIFIER ON;')->execute(); //Checked } //$aData array won't work here. changing the name $aValues['title'] = $clang->gT('Database settings'); $aValues['descp'] = $clang->gT('Database settings'); $aValues['classesForStep'] = array('off', 'off', 'off', 'off', 'on', 'off'); $aValues['progressValue'] = 60; //it store text content $aValues['adminoutputText'] = ''; //it store the form code to be displayed $aValues['adminoutputForm'] = ''; //if DB exist, check if its empty or up to date. if not, tell user LS can create it. if (!$bDBExists) { Yii::app()->session['databaseDontExist'] = true; $aValues['adminoutputText'] .= "\t<tr bgcolor='#efefef'><td align='center'>\n" . "<strong>" . $clang->gT("Database doesn't exist!") . "</strong><br /><br />\n" . $clang->gT("The database you specified does not exist:") . "<br /><br />\n<strong>" . $oModel->dbname . "</strong><br /><br />\n" . $clang->gT("LimeSurvey can attempt to create this database for you.") . "<br /><br />\n"; $aValues['next'] = array('action' => 'installer/createdb', 'label' => $clang->gT('Create database'), 'name' => ''); } elseif ($bDBExistsButEmpty) { Yii::app()->session['populatedatabase'] = true; //$this->connection->database = $model->dbname; // //$this->connection->createCommand("USE DATABASE `".$model->dbname."`")->execute(); $aValues['adminoutputText'] .= sprintf($clang->gT('A database named "%s" already exists.'), $oModel->dbname) . "<br /><br />\n" . $clang->gT("Do you want to populate that database now by creating the necessary tables?") . "<br /><br />"; $aValues['next'] = array('action' => 'installer/populatedb', 'label' => $clang->gT("Populate database"), 'name' => 'createdbstep2'); } elseif (!$bDBExistsButEmpty) { //DB EXISTS, CHECK FOR APPROPRIATE UPGRADES //$this->connection->database = $model->dbname; //$this->connection->createCommand("USE DATABASE `$databasename`")->execute(); /* @todo Implement Upgrade */ //$output=CheckForDBUpgrades(); if ($output == '') { $aValues['adminoutput'] .= '<br />' . $clang->gT('LimeSurvey database is up to date. No action needed'); } else { $aValues['adminoutput'] .= $output; } $aValues['adminoutput'] .= "<br />" . sprintf($clang->gT('Please <a href="%s">log in</a>.', 'unescaped'), $this->createUrl("/admin")); } $aValues['clang'] = $clang; $this->render('/installer/dbsettings_view', $aValues); } else { $this->render('/installer/dbconfig_view', $aData); } } else { $this->render('/installer/dbconfig_view', $aData); } } else { $this->render('/installer/dbconfig_view', $aData); } }