/**
  * Install step 3
  * Database configuration details
  */
 protected function install_step3($continue = TRUE)
 {
     Fabriq::title('Database configuration');
     // go back to site configuration step if the session isn't set
     if (!isset($_SESSION['FAB_INSTALL_site']) || $_SESSION['FAB_INSTALL_site'] == '') {
         PathMap::arg(2, 2);
         $this->install_step2();
     }
     if (isset($_POST['submit'])) {
         if (strlen(trim($_POST['db'])) == 0) {
             Messaging::message('You must enter a database name');
         }
         if (strlen(trim($_POST['user'])) == 0) {
             Messaging::message('You must enter a database user');
         }
         if (strlen(trim($_POST['pwd'])) == 0) {
             Messaging::message('You must enter a database user password');
         }
         if (strlen(trim($_POST['server'])) == 0) {
             Messaging::message('You must enter a database server');
         }
         // test database connectivity
         $mysqli = @mysqli_connect(trim($_POST['server']), trim($_POST['user']), trim($_POST['pwd']), trim($_POST['db']));
         if (!$mysqli) {
             Messaging::message('Error connecting to the database. Please check your database settings and try again.');
         } else {
             mysqli_close($mysqli);
         }
         if (!Messaging::has_messages()) {
             $dbConfig = array('db' => trim($_POST['db']), 'user' => trim($_POST['user']), 'pwd' => trim($_POST['pwd']), 'server' => trim($_POST['server']));
             $_SESSION['FAB_INSTALL_db'] = serialize($dbConfig);
             // write out configuration file
             $siteConfig = unserialize($_SESSION['FAB_INSTALL_site']);
             $confFile = 'sites/' . FabriqStack::site() . '/config/config.inc.php';
             $fh = fopen($confFile, 'w');
             fwrite($fh, "<?php\n");
             fwrite($fh, "/**\n");
             fwrite($fh, " * @file\n");
             fwrite($fh, " * Base config file for a Fabriq app.\n");
             fwrite($fh, " */\n\n");
             fwrite($fh, "\$_FAPP = array(\n");
             fwrite($fh, "\t'title' => \"{$siteConfig['title']}\",\n");
             fwrite($fh, "\t'title_pos' => '{$siteConfig['title_pos']}',\n");
             fwrite($fh, "\t'title_sep' => \"{$siteConfig['title_sep']}\",\n");
             fwrite($fh, "\t'cleanurls' => {$siteConfig['cleanurls']},\n");
             fwrite($fh, "\t'cdefault' => 'homepage',\n");
             fwrite($fh, "\t'adefault' => 'index',\n");
             fwrite($fh, "\t'url' => '{$siteConfig['url']}',\n");
             fwrite($fh, "\t'apppath' => '{$siteConfig['apppath']}',\n");
             fwrite($fh, "\t'templates' => array(\n");
             fwrite($fh, "\t\t'default' => 'application'\n");
             fwrite($fh, "\t)\n");
             fwrite($fh, ");\n\n");
             fwrite($fh, "\$_FDB['default'] = array(\n");
             fwrite($fh, "\t'user' => '{$_POST['user']}',\n");
             fwrite($fh, "\t'pwd' => '{$_POST['pwd']}',\n");
             fwrite($fh, "\t'db' => '{$_POST['db']}',\n");
             fwrite($fh, "\t'server' => '{$_POST['server']}'\n");
             fwrite($fh, ");\n");
             fclose($fh);
             // write default controller if the file isn't already there
             // file may exist from being created in a dev environment or this is
             // a distributed web app
             $contFile = 'sites/' . FabriqStack::site() . "/app/controllers/homepage.controller.php";
             if (!file_exists($contFile)) {
                 $fh = fopen($contFile, 'w');
                 fwrite($fh, "<?php\n");
                 fwrite($fh, "class homepage_controller extends Controller {\n");
                 fwrite($fh, "\tfunction index() {\n");
                 fwrite($fh, "\t\tFabriq::title('Welcome to {$siteConfig['title']}');\n");
                 fwrite($fh, "\t}\n");
                 fwrite($fh, "}\n");
                 fclose($fh);
             }
             // write default action if it doesn't already exist
             // may already exist from being created in a dev environmentor this is
             // a distributed web app
             if (!is_dir('sites/' . FabriqStack::site() . "/app/views/homepage")) {
                 mkdir('sites/' . FabriqStack::site() . "/app/views/homepage");
             }
             $actionFile = 'sites/' . FabriqStack::site() . "/app/views/homepage/index.view.php";
             if (!file_exists($actionFile)) {
                 $fh = fopen($actionFile, 'w');
                 fwrite($fh, "<h1>homepage#index</h1>\n");
                 fclose($fh);
             }
             // create the framework database tables
             global $db;
             $db_info = array('server' => trim($_POST['server']), 'user' => trim($_POST['user']), 'pwd' => trim($_POST['pwd']), 'db' => trim($_POST['db']));
             $db = new Database($db_info);
             // install config table
             $query = "CREATE TABLE IF NOT EXISTS  `fabriq_config` (\n\t\t\t\t\t\t`version` VARCHAR(10) NOT NULL,\n\t\t\t\t\t\t`installed` DATETIME NOT NULL,\n\t\t\t\t\t\tPRIMARY KEY (`version`)\n\t\t\t\t\t) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
             $db->query($query);
             $query = "INSERT INTO fabriq_config (version, installed) VALUES (?, ?)";
             $db->prepare_cud($query, array($this->installVersion, date('Y-m-d H:i:s')));
             // modules table
             $query = "CREATE TABLE IF NOT EXISTS `fabmods_modules` (\n\t\t\t\t\t\t`id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\t`module` varchar(100) NOT NULL,\n\t\t\t\t\t\t`enabled` tinyint(4) NOT NULL,\n\t\t\t\t\t\t`hasconfigs` tinyint(1) NOT NULL,\n\t\t\t\t\t\t`installed` tinyint(1) NOT NULL,\n\t\t\t\t\t\t`versioninstalled` varchar(20) NOT NULL,\n\t\t\t\t\t\t`description` text NOT NULL,\n\t\t\t\t\t\t`dependson` text,\n\t\t\t\t\t\t`created` datetime NOT NULL,\n\t\t\t\t\t\t`updated` datetime NOT NULL,\n\t\t\t\t\t\tPRIMARY KEY (`id`)\n\t\t\t\t\t) ENGINE=InnoDB  DEFAULT CHARSET=utf8;";
             $db->query($query);
             // module configs table
             $query = "CREATE TABLE IF NOT EXISTS `fabmods_module_configs` (\n\t\t\t\t\t\t`id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\t`module` int(11) NOT NULL,\n\t\t\t\t\t\t`var` varchar(100) NOT NULL,\n\t\t\t\t\t\t`val` text NOT NULL,\n\t\t\t\t\t\t`created` datetime NOT NULL,\n\t\t\t\t\t\t`updated` datetime NOT NULL,\n\t\t\t\t\t\tPRIMARY KEY (`id`)\n\t\t\t\t\t) ENGINE=InnoDB  DEFAULT CHARSET=utf8;";
             $db->query($query);
             // module perms table
             $query = "CREATE TABLE IF NOT EXISTS `fabmods_perms` (\n\t\t\t\t\t\t`id` int(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t\t`permission` varchar(100) NOT NULL,\n\t\t\t\t\t\t`module` int(11) NOT NULL,\n\t\t\t\t\t\t`created` datetime NOT NULL,\n\t\t\t\t\t\t`updated` datetime NOT NULL,\n\t\t\t\t\t\tPRIMARY KEY (`id`)\n\t\t\t\t\t) ENGINE=InnoDB  DEFAULT CHARSET=utf8;";
             $db->query($query);
             // install the module events table
             $query = "CREATE TABLE IF NOT EXISTS `fabmods_module_events` (\n\t\t\t\t\t`id` INT(11) NOT NULL AUTO_INCREMENT,\n\t\t\t\t\t`eventModule` VARCHAR(50) NOT NULL,\n\t\t\t\t\t`eventAction` VARCHAR(50) NOT NULL,\n\t\t\t\t\t`eventName` VARCHAR(100) NOT NULL,\n\t\t\t\t\t`handlerModule` VARCHAR(50) NOT NULL,\n\t\t\t\t\t`handlerAction` VARCHAR(50) NOT NULL,\n\t\t\t\t\tPRIMARY KEY (`id`)\n\t\t\t\t) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
             $db->query($query);
             if (!isset($_SESSION['FAB_INSTALL_mods_installed'])) {
                 Messaging::message('Configuration file has been written', 'success');
                 Messaging::message('Core database tables have been created', 'success');
                 FabriqModules::register_module('pathmap');
                 FabriqModules::register_module('roles');
                 FabriqModules::register_module('users');
                 FabriqModules::register_module('sitemenus');
                 FabriqModules::register_module('fabriqmodules');
                 FabriqModules::register_module('fabriqinstall');
                 FabriqModules::install('pathmap');
                 $module = new Modules();
                 $module->getModuleByName('pathmap');
                 $module->enabled = 1;
                 $module->update();
                 Messaging::message('Installed pathmap module', 'success');
                 FabriqModules::install('roles');
                 $module = new Modules();
                 $module->getModuleByName('roles');
                 $module->enabled = 1;
                 $module->update();
                 Messaging::message('Installed roles module', 'success');
                 FabriqModules::install('users');
                 $module = new Modules();
                 $module->getModuleByName('users');
                 $module->enabled = 1;
                 $module->update();
                 Messaging::message('Installed users module', 'success');
                 FabriqModules::register_module('sitemenus');
                 FabriqModules::install('sitemenus');
                 $module = new Modules();
                 $module->getModuleByName('sitemenus');
                 $module->enabled = 1;
                 $module->update();
                 Messaging::message('Installed sitemenus module', 'success');
                 FabriqModules::register_module('fabriqmodules');
                 FabriqModules::install('fabriqmodules');
                 $module = new Modules();
                 $module->getModuleByName('fabriqmodules');
                 $module->enabled = 1;
                 $module->update();
                 Messaging::message('Installed fabriqmodules module', 'success');
                 FabriqModules::register_module('fabriqinstall');
                 FabriqModules::install('fabriqinstall');
                 $module = new Modules();
                 $module->getModuleByName('fabriqinstall');
                 $module->enabled = 1;
                 $module->update();
                 Messaging::message('Installed fabriqinstall module', 'success');
                 // get admin role and give it all perms so that the admin can actually set
                 // things up
                 $role = FabriqModules::new_model('roles', 'Roles');
                 $role->getRole('administrator');
                 $perms = new Perms();
                 $perms->getAll();
                 foreach ($perms as $perm) {
                     $modPerm = FabriqModules::new_model('roles', 'ModulePerms');
                     $modPerm->permission = $perm->id;
                     $modPerm->role = $role->id;
                     $modPerm->create();
                 }
                 $_SESSION['FAB_INSTALL_mods_installed'] = true;
             }
             if ($continue) {
                 // go to next step
                 header("Location: " . PathMap::build_path('fabriqinstall', 'install', 4));
                 exit;
             }
         }
         FabriqModules::set_var('fabriqinstall', 'submitted', true);
     }
 }