date.chapril.org-framadate/app/classes/Framadate/Services/InstallService.php

138 lines
4.1 KiB
PHP
Raw Normal View History

2015-08-31 08:56:26 +02:00
<?php
/**
* This software is governed by the CeCILL-B license. If a copy of this license
* is not distributed with this file, you can obtain one at
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.txt
*
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Rapha<EFBFBD>l DROZ
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
2015-08-31 08:56:26 +02:00
*
* =============================
*
* Ce logiciel est r<EFBFBD>gi par la licence CeCILL-B. Si une copie de cette licence
2015-08-31 08:56:26 +02:00
* ne se trouve pas avec ce fichier vous pouvez l'obtenir sur
* http://www.cecill.info/licences/Licence_CeCILL-B_V1-fr.txt
*
* Auteurs de STUdS (projet initial) : Guilhem BORGHESI (borghesi@unistra.fr) et Rapha<EFBFBD>l DROZ
2015-08-31 08:56:26 +02:00
* Auteurs de Framadate/OpenSondage : Framasoft (https://github.com/framasoft)
*/
namespace Framadate\Services;
Make Framadate compatible with PostgreSQL and great again ! * Move the database handling to Doctrine DBAL * Move Migrations to Doctrine Migrations * Rename migrations for Doctrine Migrations Uses * Fix Migrations * Change config parameters, introduce db name, host and port parameters and get rid of database url * Change install form for this * Add a CLI command to make migrations * Add config.test.php to be used with APP_ENV=test for testing Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add sqlite to CI and execute migration in test env Signed-off-by: Thomas Citharel <tcit@tcit.fr> Typo Signed-off-by: Thomas Citharel <tcit@tcit.fr> SQLite is already inside the image... Signed-off-by: Thomas Citharel <tcit@tcit.fr> Rebase two new migrations Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move from trait to abstract class and remove legacy migration table after checks Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move doctrine command path inside CI Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move abstract migration class to correct namespace and remove unused command Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Check for legacy migration table existence Signed-off-by: Thomas Citharel <tcit@tcit.fr> Check if legacy migration table exists before deleting it Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add messages for skipped migrations and fix an issue with MySQL ERR_NO_DATE Migration Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2018-04-18 16:16:22 +02:00
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
2015-08-31 08:56:26 +02:00
use Framadate\Utils;
use Smarty;
/**
* This class helps to clean all inputs from the users or external services.
*/
class InstallService {
private $fields = [
// General
'appName' => 'Framadate',
'appMail' => '',
'responseMail' => '',
'defaultLanguage' => 'fr',
'cleanUrl' => true,
// Database configuration
Make Framadate compatible with PostgreSQL and great again ! * Move the database handling to Doctrine DBAL * Move Migrations to Doctrine Migrations * Rename migrations for Doctrine Migrations Uses * Fix Migrations * Change config parameters, introduce db name, host and port parameters and get rid of database url * Change install form for this * Add a CLI command to make migrations * Add config.test.php to be used with APP_ENV=test for testing Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add sqlite to CI and execute migration in test env Signed-off-by: Thomas Citharel <tcit@tcit.fr> Typo Signed-off-by: Thomas Citharel <tcit@tcit.fr> SQLite is already inside the image... Signed-off-by: Thomas Citharel <tcit@tcit.fr> Rebase two new migrations Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move from trait to abstract class and remove legacy migration table after checks Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move doctrine command path inside CI Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move abstract migration class to correct namespace and remove unused command Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Check for legacy migration table existence Signed-off-by: Thomas Citharel <tcit@tcit.fr> Check if legacy migration table exists before deleting it Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add messages for skipped migrations and fix an issue with MySQL ERR_NO_DATE Migration Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2018-04-18 16:16:22 +02:00
'dbName' => 'framadate',
'dbPort' => 3306,
'dbHost' => 'localhost',
'dbUser' => 'root',
'dbPassword' => '',
'dbPrefix' => 'fd_',
'migrationTable' => 'framadate_migration'
];
2015-08-31 08:56:26 +02:00
function __construct() {}
public function updateFields($data) {
foreach ($data as $field => $value) {
$this->fields[$field] = $value;
}
}
public function install(Smarty &$smarty) {
2015-08-31 08:56:26 +02:00
// Check values are present
Make Framadate compatible with PostgreSQL and great again ! * Move the database handling to Doctrine DBAL * Move Migrations to Doctrine Migrations * Rename migrations for Doctrine Migrations Uses * Fix Migrations * Change config parameters, introduce db name, host and port parameters and get rid of database url * Change install form for this * Add a CLI command to make migrations * Add config.test.php to be used with APP_ENV=test for testing Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add sqlite to CI and execute migration in test env Signed-off-by: Thomas Citharel <tcit@tcit.fr> Typo Signed-off-by: Thomas Citharel <tcit@tcit.fr> SQLite is already inside the image... Signed-off-by: Thomas Citharel <tcit@tcit.fr> Rebase two new migrations Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move from trait to abstract class and remove legacy migration table after checks Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move doctrine command path inside CI Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move abstract migration class to correct namespace and remove unused command Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Check for legacy migration table existence Signed-off-by: Thomas Citharel <tcit@tcit.fr> Check if legacy migration table exists before deleting it Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add messages for skipped migrations and fix an issue with MySQL ERR_NO_DATE Migration Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2018-04-18 16:16:22 +02:00
if (empty($this->fields['appName']) || empty($this->fields['appMail']) || empty($this->fields['defaultLanguage']) || empty($this->fields['dbName']) || empty($this->fields['dbHost']) || empty($this->fields['dbPort']) || empty($this->fields['dbUser'])) {
2015-08-31 08:56:26 +02:00
return $this->error('MISSING_VALUES');
}
// Connect to database
try {
2018-08-14 14:26:15 +02:00
$connect = $this->connectTo($this->fields);
} catch(\Doctrine\DBAL\DBALException $e) {
return $this->error('CANT_CONNECT_TO_DATABASE', $e->getMessage());
2015-08-31 08:56:26 +02:00
}
// Write configuration to conf.php file
if ($this->writeConfiguration($smarty) === false) {
return $this->error(__f('Error', "Can't create the config.php file in '%s'.", CONF_FILENAME));
}
2015-08-31 08:56:26 +02:00
return $this->ok();
}
Make Framadate compatible with PostgreSQL and great again ! * Move the database handling to Doctrine DBAL * Move Migrations to Doctrine Migrations * Rename migrations for Doctrine Migrations Uses * Fix Migrations * Change config parameters, introduce db name, host and port parameters and get rid of database url * Change install form for this * Add a CLI command to make migrations * Add config.test.php to be used with APP_ENV=test for testing Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add sqlite to CI and execute migration in test env Signed-off-by: Thomas Citharel <tcit@tcit.fr> Typo Signed-off-by: Thomas Citharel <tcit@tcit.fr> SQLite is already inside the image... Signed-off-by: Thomas Citharel <tcit@tcit.fr> Rebase two new migrations Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move from trait to abstract class and remove legacy migration table after checks Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move doctrine command path inside CI Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move abstract migration class to correct namespace and remove unused command Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Check for legacy migration table existence Signed-off-by: Thomas Citharel <tcit@tcit.fr> Check if legacy migration table exists before deleting it Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add messages for skipped migrations and fix an issue with MySQL ERR_NO_DATE Migration Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2018-04-18 16:16:22 +02:00
/**
* @param $fields
* @return \Doctrine\DBAL\Connection|null
*/
function connectTo($fields) {
$doctrineConfig = new Configuration();
$connectionParams = [
'dbname' => $fields['dbName'],
'user' => $fields['dbUser'],
'password' => $fields['dbPassword'],
'host' => $fields['dbHost'],
'driver' => $fields['dbDriver'],
'charset' => $fields['dbDriver'] === 'pdo_mysql' ? 'utf8mb4' : 'utf8',
];
2018-08-14 14:26:15 +02:00
return DriverManager::getConnection($connectionParams, $doctrineConfig);
2015-08-31 08:56:26 +02:00
}
function writeConfiguration(Smarty &$smarty) {
foreach($this->fields as $field=>$value) {
$smarty->assign($field, $value);
2015-08-31 08:56:26 +02:00
}
$content = $smarty->fetch('admin/config.tpl');
return $this->writeToFile($content);
2015-08-31 08:56:26 +02:00
}
/**
* @param $content
Make Framadate compatible with PostgreSQL and great again ! * Move the database handling to Doctrine DBAL * Move Migrations to Doctrine Migrations * Rename migrations for Doctrine Migrations Uses * Fix Migrations * Change config parameters, introduce db name, host and port parameters and get rid of database url * Change install form for this * Add a CLI command to make migrations * Add config.test.php to be used with APP_ENV=test for testing Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add sqlite to CI and execute migration in test env Signed-off-by: Thomas Citharel <tcit@tcit.fr> Typo Signed-off-by: Thomas Citharel <tcit@tcit.fr> SQLite is already inside the image... Signed-off-by: Thomas Citharel <tcit@tcit.fr> Rebase two new migrations Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move from trait to abstract class and remove legacy migration table after checks Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move doctrine command path inside CI Signed-off-by: Thomas Citharel <tcit@tcit.fr> Move abstract migration class to correct namespace and remove unused command Signed-off-by: Thomas Citharel <tcit@tcit.fr> CS Signed-off-by: Thomas Citharel <tcit@tcit.fr> Check for legacy migration table existence Signed-off-by: Thomas Citharel <tcit@tcit.fr> Check if legacy migration table exists before deleting it Signed-off-by: Thomas Citharel <tcit@tcit.fr> Add messages for skipped migrations and fix an issue with MySQL ERR_NO_DATE Migration Signed-off-by: Thomas Citharel <tcit@tcit.fr>
2018-04-18 16:16:22 +02:00
* @return bool|int
2015-08-31 08:56:26 +02:00
*/
function writeToFile($content) {
return @file_put_contents(CONF_FILENAME, $content);
2015-08-31 08:56:26 +02:00
}
/**
* @return array
*/
function ok() {
return [
2015-08-31 08:56:26 +02:00
'status' => 'OK',
'msg' => __f('Installation', 'Ended', Utils::get_server_name())
];
2015-08-31 08:56:26 +02:00
}
/**
* @param $msg
* @return array
*/
function error($msg, $details = '') {
return [
2015-08-31 08:56:26 +02:00
'status' => 'ERROR',
'code' => $msg,
'details' => $details,
];
2015-08-31 08:56:26 +02:00
}
public function getFields() {
return $this->fields;
}
2015-08-31 08:56:26 +02:00
}