require_once ("../lib/user.inc.php");
require_once ("../lib/i18n.inc.php");
require_once ("../lib/backup.inc.php");
require_once ("../edit/lib/tar.inc.php");
class Install
{
private $messages = Array (); ///< Anzuzeigene Meldungen
private $warnings = Array (); ///< Anzuzeigene Fehlermeldungen
private $errors = Array (); ///< Anzuzeigene Fehlermeldungen
private $config = Array (); ///< Konfiguration
private $tr = Array (); ///< Übersetzungen
private $can_configure = true; ///< Zeigt an ob die Installation configuriert werden kann
private $is_disabled = false; ///< Zeigt an ob die Installationsscript deaktiviert ist
private $compleated = false; ///< Zeigt an ob die Installation abgeschlossen ist
private $default_data = false; ///< Zeigt an ob ein default Datensatz verfügbar ist
public function __construct ()
{
@session_start ();
$tr = array ();
@include ("install.tr.php");
$this->tr = $tr ? $tr : $this->tr;
if (isset ($_REQUEST['save']) && !Setting::get ("config", "disable_install")) {
// Änderungen speichern
$this->save ();
}
$_SESSION['cms']['install']['accessfrom'] = !empty ($_SESSION['cms']['install']['accessfrom']) ? $_SESSION['cms']['install']['accessfrom'] : "localhost";
if (phpversion () < "5.1") {
// PHP Version to low
$this->errors[] = I18n::replace ($this->tr ("used php version '[version]' is to low. minimum needed is '5.1'"), array ('version' => phpversion ()));
$this->is_disabled = true;
} else if (!function_exists ("mysqli_connect")) {
// MySQL not supported
$this->errors[] = $this->tr ("MySQL not supported by php");
$this->is_disabled = true;
} else if (Setting::get ("config", "disable_install")) {
$this->errors[] = $this->tr ("Installation script is disabled in config file.");
$this->is_disabled = true;
$this->compleated = true;
} else {
if (ini_get ('magic_quotes_gpc')) {
$this->warnings[] = $this->tr ("'magic_quotes_gpc' is enabled. Disable this for a better performance.");
}
if (ini_get ('magic_quotes_runtime')) {
$this->warnings[] = $this->tr ("'magic_quotes_runtime' is enabled. Disable this for a better performance.");
}
if (!get_extension_funcs("gd")) {
$this->warnings[] = $this->tr ("'php-gd' is not installed. Install this to enable rescale of images.");
}
if (!file_exists (Setting::get ("config", "configFile"))) {
$file = str_replace (Setting::get ("config", "documentRoot"), "", Setting::get ("config", "configFile"));
$this->errors[] = I18n::replace ($this->tr ("please copy example config file '[file].example'
to '[file]' and make it writeable"), array ('file' => $file));
$this->can_configure = false;
} else if (!is_writeable (Setting::get ("config", "configFile"))) {
// Config File not writeable
$file = str_replace (Setting::get ("config", "documentRoot"), "", Setting::get ("config", "configFile"));
$this->warnings[] = I18n::replace ($this->tr ("config file '[file]' is not writeable"), array ('file' => $file));
$this->can_configure = false;
}
if (isset ($_REQUEST['save']) || isset ($_REQUEST['update'])) {
if (!is_writeable (Setting::get ("config", "filePath"))) {
// File path not writeable
$this->errors[] = I18n::replace ($this->tr ("file save path '[path]' is not writeable"), array ('path' => Setting::get ("config", "filePathRel")));
}
$db = Setting::get ("config", "db");
$file = isset ($db['logfile']) ? $db['logfile'] : "";
$documentRoot = Setting::get ("config", "documentRoot");
if ($file && !is_writeable ($documentRoot.$file) && !@touch ($documentRoot.$file)) {
// DB Logfile not writeable
$this->warnings[] = I18n::replace ($this->tr ("database log file '[file]' is not writeable"), array ('file' => $file));
}
if (!$this->errors) {
$conf = Setting::get ("config", "db");
$conf['verbose'] = false;
$db = new Database ("test", $conf, true);
if ($db->err) {
$this->errors[] = $this->tr ("Database connection error:") . "
" . $db->errmsg;
} else if (Setting::get ("db", "version")) {
if (Setting::get ("db", "version") != Setting::VERSION_DB) {
// Datenbank Updaten
ob_start ();
@include ("update_db.php");
$this->messages[] = ob_get_clean ();
} else {
// Installation compleated
$this->messages[] = $this->tr ("Installation is compleated");
$this->compleated = true;
}
} else {
$this->errors[] = $this->tr ("Can't found database tables.");
}
}
}
}
if (file_exists ("../default.sql") || file_exists ("../default.tar")) {
$this->default_data = true;
}
}
public function __get ($name)
{
switch ($name) {
case "version":
return I18n::replace ($this->tr ("version [version]"), array ('version' => Setting::VERSION.".".Setting::REVISION));
case "manual":
$lc = I18n::lc ();
$lc = (file_exists ("docu/manual_$lc.pdf")) ? $lc : "en";
return "docu/manual_$lc.pdf";
default:
return $this->$name;
}
}
public function get ($section, $name)
{
if ($section == "install") {
return isset ($_SESSION['cms']['install'][$name]) ? $_SESSION['cms']['install'][$name] : "";
} else if ($section) {
$values = Setting::get ("config", $section);
return isset ($values[$name]) ? $values[$name] : "";
} else {
return Setting::get ("config", $name);
}
}
private function save ()
{
unset ($_SESSION['cms']['install']);
$_REQUEST['db']['verbose'] = empty ($_REQUEST['db']['verbose']) ? "" : "1";
foreach ($_REQUEST as $key => $value) {
switch ($key) {
case "db":
foreach ($value as $name => $val) {
$this->set ($key, $name, $val);
}
break;
case "rootuser":
case "rootpass":
$_SESSION['cms']['install'][$key] = $value;
break;
case "disable_install":
case "document_root":
case "cms_path":
case "file_path":
$this->set ("", $key, $value);
break;
case "createdb":
if (!$this->createDb ()) {
$_SESSION['cms']['install'][$key] = $value;
}
break;
case "createuser":
if (!$this->createUser ()) {
$_SESSION['cms']['install'][$key] = $value;
}
break;
case "createtables":
if (!$this->createTables ()) {
$_SESSION['cms']['install'][$key] = $value;
}
break;
case "insertbackup":
if (!$this->insertBackup ()) {
$_SESSION['cms']['install'][$key] = $value;
$_SESSION['cms']['install']['use_default_data'] = !empty ($_REQUEST['use_default_data']);
}
break;
}
}
}
private function set ($section, $name, $value)
{
$configName = Setting::get ("config", "configName");
$key = "\$config['$configName']" . ($section ? "['$section']" : "") . "['$name']";
$rkey = preg_quote ($key, '/');
$value = addslashes ($value);
$config = file (Setting::get ("config", "configFile"));
$fp = @fopen (Setting::get ("config", "configFile"), "w");
if ($fp) {
$match = false;
fputs ($fp, "\n");
foreach ($config as $row) {
if (preg_match ("/<\?|\?>/", $row)) {
continue;
}
if (preg_match ("/$rkey/", $row)) {
$match = true;
}
$row = preg_replace ("/($rkey).*/", "\\1 = \"$value\";", $row);
fputs ($fp, $row);
}
if (!$match) {
fputs ($fp, "$key = \"$value\";\n");
}
fputs ($fp, "?>\n");
fclose ($fp);
}
$values = Setting::get ("config", $section);
$values[$name] = $value;
Setting::set ("config", $section, $values);
}
private function createDb ()
{
$dbconf = $_REQUEST['can_configure'] ? $_REQUEST['db'] : Setting::get ("config", "db");
$conf = $dbconf;
$conf['username'] = isset ($_REQUEST['rootuser']) ? $_REQUEST['rootuser'] : "";
$conf['password'] = isset ($_REQUEST['rootpass']) ? $_REQUEST['rootpass'] : "";
$conf['verbose'] = false;
$conf['database'] = "-";
// Datenbank erstellen
$db = new Database ("create", $conf);
if ($db->query ("CREATE DATABASE IF NOT EXISTS `{$dbconf['database']}`")) {
$this->messages[] = $this->tr ("Created database");
return true;
} else {
$this->errors[] = $this->tr ("Can't create database:") . "
" . $db->errmsg;
return false;
}
}
private function createUser ()
{
$dbconf = $_REQUEST['can_configure'] ? $_REQUEST['db'] : Setting::get ("config", "db");
$conf = $dbconf;
$conf['username'] = isset ($_REQUEST['rootuser']) ? $_REQUEST['rootuser'] : "";
$conf['password'] = isset ($_REQUEST['rootpass']) ? $_REQUEST['rootpass'] : "";
$conf['verbose'] = false;
$conf['database'] = "-";
if (!$dbconf['username']) {
$this->errors[] = $this->tr ("Can't create user:") . "
" . $this->tr ("No username is given");
return false;
}
// User anlegen
$db = new Database ("user", $conf);
$accessfrom = !empty ($_REQUEST['accessfrom']) ? $_REQUEST['accessfrom'] : "%";
if ($db->query ("GRANT USAGE ON * . * TO '{$dbconf['username']}'@'$accessfrom' IDENTIFIED BY '{$dbconf['password']}'") &&
$db->query ("GRANT ALL PRIVILEGES ON `{$dbconf['database']}` . * TO '{$dbconf['username']}'@'$accessfrom'"))
{
$db->query ("FLUSH PRIVILEGES");
$this->messages[] = $this->tr ("Created user");
return true;
} else {
$this->errors[] = $this->tr ("Can't create user:") . "
" . $db->errmsg;
return false;
}
}
private function createTables ()
{
if ($err = Backup::createTables (true)) {
$this->errors[] = $this->tr ("Can't create tables in database") . "
" . $err;
return false;
} else {
$this->messages[] = $this->tr ("Created tables in database");
return true;
}
}
private function insertBackup ()
{
if (!Setting::get ("db", "version")) {
$this->errors[] = $this->tr ("Can't insert backup because database tables not found.");
} else {
if (!empty ($_REQUEST['use_default_data'])) {
if (file_exists ("../default.sql")) {
$file = "../default.sql";
$filename = "default.sql";
} else {
$file = "../default.tar";
$filename = "default.tar";
}
} else {
if (empty ($_FILES['backupfile']) || $_FILES['backupfile']['error']) {
$this->errors[] = $this->tr ("file upload error");
return false;
}
$file = $_FILES['backupfile']['tmp_name'];
$filename = $_FILES['backupfile']['name'];
}
if (preg_match ("/.tar$/", $filename)) {
$path = Setting::get ("config", "filePath");
if (!is_writeable ($path)) {
return false;
}
$this->emptyFilepath ($path);
$tar = new Tar ($file);
if ($err = $tar->extract ($path)) {
$this->messages[] = $this->tr ("Can't insert files into cms files folder") . "
" . $err;
return false;
}
$this->messages[] = $this->tr ("Inserted backup of files into cms files folder");
$file = $path."data.sql";
if (!file_exists ($file)) {
return true;
}
}
if ($err = Backup::insertData ($file, $filename)) {
$this->errors[] = $this->tr ("Can't insert data in database") . "
" . $err;
return false;
} else {
$this->messages[] = $this->tr ("Inserted backup into database");
return true;
}
}
}
private function emptyFilepath ($dir)
{
$handle = opendir ($dir);
while (false !== ($readdir = readdir ($handle))){
if ($readdir != '.' && $readdir != '..'){
$path = $dir.'/'.$readdir;
if (is_file ($path)) {
unlink ($path);
} elseif (is_dir ($path)) {
$this->emptyFilepath ($path);
}
}
}
closedir ($handle);
}
public function tr ($name, $defaultvalue=false)
{
return isset ($this->tr[$name]) ? $this->tr[$name] : ($defaultvalue!==false ? $defaultvalue : $name);
}
}
?>