コード例 #1
0
ファイル: site.task.php プロジェクト: stonyyi/anahita
 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>");
     }
 }
コード例 #2
0
ファイル: model.php プロジェクト: kumarsivarajan/jaderp
 /**
  * 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;
 }