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
|
|
|
|
|
*
|
2016-05-04 01:27:51 +02:00
|
|
|
|
* Authors of STUdS (initial project): Guilhem BORGHESI (borghesi@unistra.fr) and Rapha<EFBFBD>l DROZ
|
2016-08-04 22:26:37 +02:00
|
|
|
|
* Authors of Framadate/OpenSondage: Framasoft (https://github.com/framasoft)
|
2015-08-31 08:56:26 +02:00
|
|
|
|
*
|
|
|
|
|
* =============================
|
|
|
|
|
*
|
2016-05-04 01:27:51 +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
|
|
|
|
|
*
|
2016-05-04 01:27:51 +02:00
|
|
|
|
* 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;
|
2021-12-20 17:46:50 +01:00
|
|
|
|
use function __f;
|
|
|
|
|
use Exception;
|
2015-08-31 08:56:26 +02:00
|
|
|
|
use Framadate\Utils;
|
2021-12-20 17:46:50 +01:00
|
|
|
|
use PDO;
|
2015-08-31 08:56:26 +02:00
|
|
|
|
use Smarty;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This class helps to clean all inputs from the users or external services.
|
|
|
|
|
*/
|
|
|
|
|
class InstallService {
|
2018-02-19 00:18:43 +01:00
|
|
|
|
private $fields = [
|
2015-09-07 16:34:24 +02:00
|
|
|
|
// General
|
|
|
|
|
'appName' => 'Framadate',
|
|
|
|
|
'appMail' => '',
|
|
|
|
|
'responseMail' => '',
|
|
|
|
|
'defaultLanguage' => 'fr',
|
|
|
|
|
'cleanUrl' => true,
|
|
|
|
|
|
|
|
|
|
// Database configuration
|
|
|
|
|
'dbConnectionString' => 'mysql:host=<HOST>;dbname=<SCHEMA>;port=3306',
|
|
|
|
|
'dbUser' => 'root',
|
|
|
|
|
'dbPassword' => '',
|
|
|
|
|
'dbPrefix' => 'fd_',
|
|
|
|
|
'migrationTable' => 'framadate_migration'
|
2018-02-19 00:18:43 +01:00
|
|
|
|
];
|
2015-08-31 08:56:26 +02:00
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
|
public function __construct() {}
|
2015-08-31 08:56:26 +02:00
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
|
public function updateFields($data): void
|
|
|
|
|
{
|
2015-09-07 16:34:24 +02:00
|
|
|
|
foreach ($data as $field => $value) {
|
|
|
|
|
$this->fields[$field] = $value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
|
public function install(Smarty &$smarty): array
|
|
|
|
|
{
|
2015-08-31 08:56:26 +02:00
|
|
|
|
// Check values are present
|
2015-09-07 16:34:24 +02:00
|
|
|
|
if (empty($this->fields['appName']) || empty($this->fields['appMail']) || empty($this->fields['defaultLanguage']) || empty($this->fields['dbConnectionString']) || empty($this->fields['dbUser'])) {
|
2015-08-31 08:56:26 +02:00
|
|
|
|
return $this->error('MISSING_VALUES');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Connect to database
|
2018-08-03 14:06:23 +02:00
|
|
|
|
try {
|
|
|
|
|
$connect = $this->connectTo($this->fields['dbConnectionString'], $this->fields['dbUser'], $this->fields['dbPassword']);
|
2021-12-20 17:46:50 +01:00
|
|
|
|
} catch(Exception $e) {
|
2018-08-03 14:06:23 +02:00
|
|
|
|
return $this->error('CANT_CONNECT_TO_DATABASE', $e->getMessage());
|
2015-08-31 08:56:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write configuration to conf.php file
|
2016-05-04 01:27:51 +02:00
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 14:06:23 +02:00
|
|
|
|
/**
|
|
|
|
|
* Connect to PDO compatible source
|
|
|
|
|
*
|
|
|
|
|
* @param string $connectionString
|
|
|
|
|
* @param string $user
|
|
|
|
|
* @param string $password
|
2021-12-20 17:46:50 +01:00
|
|
|
|
* @return PDO
|
2018-08-03 14:06:23 +02:00
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
|
public function connectTo(string $connectionString, string $user, string $password): PDO
|
|
|
|
|
{
|
|
|
|
|
$pdo = @new PDO($connectionString, $user, $password);
|
|
|
|
|
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
|
|
|
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
2018-08-03 14:06:23 +02:00
|
|
|
|
return $pdo;
|
2015-08-31 08:56:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return false|int
|
|
|
|
|
*/
|
|
|
|
|
public function writeConfiguration(Smarty &$smarty) {
|
2015-09-07 16:34:24 +02:00
|
|
|
|
foreach($this->fields as $field=>$value) {
|
|
|
|
|
$smarty->assign($field, $value);
|
2015-08-31 08:56:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$content = $smarty->fetch('admin/config.tpl');
|
|
|
|
|
|
2016-05-04 01:27:51 +02:00
|
|
|
|
return $this->writeToFile($content);
|
2015-08-31 08:56:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $content
|
2021-12-20 17:46:50 +01:00
|
|
|
|
* @return false|int
|
2015-08-31 08:56:26 +02:00
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
|
public function writeToFile(string $content) {
|
2016-05-04 01:27:51 +02:00
|
|
|
|
return @file_put_contents(CONF_FILENAME, $content);
|
2015-08-31 08:56:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
|
public function ok(): array
|
|
|
|
|
{
|
2018-02-19 00:18:43 +01:00
|
|
|
|
return [
|
2015-08-31 08:56:26 +02:00
|
|
|
|
'status' => 'OK',
|
|
|
|
|
'msg' => __f('Installation', 'Ended', Utils::get_server_name())
|
2018-02-19 00:18:43 +01:00
|
|
|
|
];
|
2015-08-31 08:56:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-12-20 17:46:50 +01:00
|
|
|
|
* @param string $msg
|
|
|
|
|
* @param string $details
|
2015-08-31 08:56:26 +02:00
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-12-20 17:46:50 +01:00
|
|
|
|
public function error(string $msg, string $details = ''): array
|
|
|
|
|
{
|
2018-02-19 00:18:43 +01:00
|
|
|
|
return [
|
2015-08-31 08:56:26 +02:00
|
|
|
|
'status' => 'ERROR',
|
2018-08-03 14:06:23 +02:00
|
|
|
|
'code' => $msg,
|
|
|
|
|
'details' => $details,
|
2018-02-19 00:18:43 +01:00
|
|
|
|
];
|
2015-08-31 08:56:26 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 17:46:50 +01:00
|
|
|
|
public function getFields(): array
|
|
|
|
|
{
|
2015-09-07 12:24:55 +02:00
|
|
|
|
return $this->fields;
|
|
|
|
|
}
|
2015-08-31 08:56:26 +02:00
|
|
|
|
}
|