Projekt

Obecné

Profil

<?php defined('SYSPATH') or die('No direct script access.');
/*
* This file is part of open source system FreeNetIS
* and it is released under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*
*/

/**
* Setup config controller.
* Configure database connection and .htaccess file before installation.
*
* @author Michal Kliment
* @package Controller
*/
class Setup_config_Controller extends Controller
{
/**
* Introductory page of setup config, only some information about setup config
*
* @author Michal Kliment
*/
public function index()
{
// check if the database is empty
if ($this->settings->get('db_schema_version'))
{
url::redirect('members/show/'.$this->session->get('member_id'));
}
// new form
$form = new Forge('setup_config/setup');
$form->submit(__('Next step') . ' >>>');

$view = new View('setup_config');
$view->content = new View('setup_config_index');
$view->content->form = $form->html();
$view->render(TRUE);
}

/**
* This method create config file (if root directory is writable)
* or generate code to create it by user
*
* @author Michal Kliment
*/
public function setup()
{
// check if the database is empty
if ($this->settings->get('db_schema_version'))
{
url::redirect(
url_lang::base().'members/show/'.
$this->session->get('member_id')
);
}
// new form
$form = new Forge('setup_config/setup');
$form->group('Database information');
$form->input('db_name')
->label(__('Database name'))
->value('freenetis')
->help(__('The name of the database you want to run FreeNetIS in.'));
$form->input('db_user')
->label(__('User name'))
->value('freenetis')
->help(__('Your MySQL username'));
$form->input('db_password')
->label(__('Password'))
->value('password')
->help(__('Your MySQL password'));
$form->input('db_host')
->label(__('Database host'))
->value('localhost')
->help(__('99% chance you won\'t need to change this value.'));
$form->submit('Submit');

// form is valid
if ($form->validate())
{
// convert object to array
$form_data = $form->as_array();

// test connection to database
$con = @mysql_connect(
$form_data['db_host'],
$form_data['db_user'],
$form_data['db_password']
);
$db = @mysql_select_db($form_data['db_name'], $con);

$view = new View('setup_config');
$view->content = new View('setup_config_setup');

// cannot connect to database => form data are bad
if (!$con OR !$db)
{
$view->content->error = TRUE;
}
// successfully connect to database, we can create config file
else
{
// load config-sample
$configFile = file('config-sample' . EXT);
foreach ($configFile as $line_num => $line)
{
// find only config lines (no comments or blank lines)
if (preg_match("/^\\\$config\['(.+)'\]/", $line, $matches))
{
// this config line is one from database config
if (isset($form_data[$matches[1]]))
{
// set value from form
$value = $form_data[$matches[1]];
$configFile[$line_num] = preg_replace(
"/^(\\\$config\[')(.+)('\] = ')(.+)(';)/",
'${1}${2}${3}' . $value . '${5}', $line
);
}
}
}


// root directory is not writable, only generate code and
// howto to create config file by user
if (!is_writable('.'))
{
$textarea = '';
foreach ($configFile as $line)
$textarea .= htmlentities($line);

$view->content->textarea =
'<textarea cols="90" rows="15">' . $textarea . '</textarea>';
}
// root directory is writable, create config
else
{
$handle = fopen('config.php', 'w');
foreach ($configFile as $line)
{
fwrite($handle, $line);
}
fclose($handle);
chmod('config.php', 0666);
}
}
$view->render(TRUE);
}
else
{
$view = new View('setup_config');
$view->content = $form->html();
$view->render(TRUE);
}
}

}
(55-55/75)