allerta-vvf/server/install/installHelper.php

711 lines
29 KiB
PHP
Raw Normal View History

2020-05-19 23:09:11 +02:00
<?php
2020-05-28 16:35:26 +02:00
use GetOpt\GetOpt as Getopt;
use GetOpt\Option;
2020-12-31 11:43:50 +01:00
function finalInstallationHelperStep(){
$logopngPath = "../resources/images/";
unlink("runInstall.php");
if(file_exists("../options.txt")){
unlink("../options.txt");
}
if(file_exists($logopngPath."logo_sample.png") && !file_exists($logopngPath."logo.png")){
copy($logopngPath."logo_sample.png", $logopngPath."logo.png");
}
if(file_exists($logopngPath."owner_sample.png") && !file_exists($logopngPath."owner.png")){
copy($logopngPath."owner_sample.png", $logopngPath."owner.png");
}
}
2020-11-13 18:57:47 +01:00
function client_languages()
{
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
2020-09-04 22:50:08 +02:00
$client_languages = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
} else {
$client_languages = "en-US;q=0.5,en;q=0.3";
}
2020-11-13 18:57:47 +01:00
if(strpos($client_languages, ';') == false) {
if(strpos($client_languages, '-') !== false) {
2020-09-04 22:50:08 +02:00
return [substr($client_languages, 0, 5)];
} else {
return [substr($client_languages, 0, 2)];
}
} else {
$client_languages = explode(",", $client_languages);
$tmp_languages = [];
foreach($client_languages as $key=>$language){
2020-11-13 18:57:47 +01:00
if(strpos($language, ';') == false) {
2020-09-04 22:50:08 +02:00
$tmp_languages[$language] = 1;
} else {
2020-11-13 18:57:47 +01:00
$tmp_languages[explode(";q=", $language)[0]] = (float) explode(";q=", $language)[1];
2020-09-04 22:50:08 +02:00
}
}
arsort($tmp_languages);
return array_keys($tmp_languages);
}
}
$client_languages = client_languages();
$loaded_languages = ["en", "it"];
$default_language = "en";
$language = null;
foreach($client_languages as $tmp_language){
2020-11-13 18:57:47 +01:00
if(in_array($tmp_language, $loaded_languages) && $language == null) {
2020-09-04 22:50:08 +02:00
$language = $tmp_language;
}
}
2020-12-16 17:59:47 +01:00
if(isset($_COOKIE["forceLanguage"]) && in_array($_COOKIE["forceLanguage"], $loaded_languages)){
$language = $_COOKIE["forceLanguage"];
}
2020-11-13 18:57:47 +01:00
if (file_exists("translations/".$language.".php")) {
$loaded_translations = include "translations/".$language.".php";
2020-09-04 22:50:08 +02:00
} else {
2020-11-13 18:57:47 +01:00
$loaded_translations = include "translations/en.php";
2020-09-04 22:50:08 +02:00
}
2020-11-13 18:57:47 +01:00
function t($string, $echo=true)
{
2020-09-04 22:50:08 +02:00
global $loaded_translations;
try {
2020-11-13 18:57:47 +01:00
if (!array_key_exists($string, $loaded_translations)) {
throw new Exception('string does not exist');
}
2020-09-04 22:50:08 +02:00
$string = $loaded_translations[$string];
} catch (\Exception $e) {
2020-09-05 12:08:43 +02:00
//nothing
2020-09-04 22:50:08 +02:00
}
2020-11-13 18:57:47 +01:00
if ($echo) {
2020-09-05 12:08:43 +02:00
echo $string;
2020-09-04 22:50:08 +02:00
} else {
2020-09-05 12:08:43 +02:00
return $string;
2020-09-04 22:50:08 +02:00
}
}
2020-05-28 16:35:26 +02:00
function is_cli() //https://www.binarytides.com/php-check-running-cli/
{
2020-11-13 18:57:47 +01:00
if(defined('STDIN') ) {
return true;
}
if(empty($_SERVER['REMOTE_ADDR']) and !isset($_SERVER['HTTP_USER_AGENT']) and count($_SERVER['argv']) > 0) {
return true;
}
return false;
2020-05-28 16:35:26 +02:00
}
2020-05-19 23:09:11 +02:00
if (file_exists('../vendor/autoload.php')) {
try {
2020-11-13 18:57:47 +01:00
include '../vendor/autoload.php';
2020-05-19 23:09:11 +02:00
} catch (Exception $e) {
2020-11-13 18:57:47 +01:00
if(is_cli()) {
2020-05-28 16:35:26 +02:00
echo($e);
exit(1);
}
2020-05-19 23:09:11 +02:00
die("Please install composer and run composer install (".$e);
}
} else {
2020-11-13 18:57:47 +01:00
if(is_cli()) {
2020-05-28 16:35:26 +02:00
echo($e);
exit(1);
}
2020-05-19 23:09:11 +02:00
die("Please install composer and run composer install");
}
2020-11-13 18:57:47 +01:00
function checkConnection($host, $user, $password, $database, $return=false)
{
2020-05-19 23:09:11 +02:00
try{
2020-11-13 18:57:47 +01:00
$connection = new PDO("mysql:host=$host", $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
2020-05-19 23:09:11 +02:00
$connectionOk = true;
} catch (PDOException $e){
2020-11-13 18:57:47 +01:00
if($return) {
2020-06-05 17:56:22 +02:00
return false;
} else {
2020-11-13 18:57:47 +01:00
if(is_cli()) {
2020-06-05 17:56:22 +02:00
echo($e);
exit(8);
}
$connectionOk = false;
2020-11-13 18:57:47 +01:00
?>
2020-09-04 22:50:08 +02:00
<div class="wp-die-message"><h1><?php t("Error establishing a database connection"); ?></h1>
2020-11-13 18:57:47 +01:00
<p><?php printf(t("This could mean that %s and %s in file %s are wrong or that we cannot contact database %s. It could mean that your database is unreachable", false), t("DB username", false), t("DB password", false), "<code>config.php</code>", "<code>$database</code>"); ?>.</p>
2020-05-19 23:09:11 +02:00
<ul>
2020-11-13 18:57:47 +01:00
<li><?php printf(t("Are you sure that %s and %s correct?", false), t("DB username", false), t("DB password", false)); ?></li>
2020-09-04 22:50:08 +02:00
<li><?php t("Are you sure you have entered the correct hostname?"); ?></li>
<li><?php t("Are you sure the database server is up and running?"); ?></li>
2020-05-19 23:09:11 +02:00
</ul>
2020-09-04 22:50:08 +02:00
<p><?php t("If you're not sure what these terms mean, try contacting your hosting provider. Try providing the following information:"); ?></p>
2020-09-24 21:38:33 +02:00
<details open>
2020-09-04 22:50:08 +02:00
<summary><?php t("Advanced informations"); ?></summary>
2020-05-19 23:09:11 +02:00
<pre><?php echo($e); ?></pre>
</details>
2020-09-04 22:50:08 +02:00
<p class="step"><a href="#" onclick="javascript:history.go(-2);return false;" class="button button-large"><?php t("Try again"); ?></a></p>
2020-05-19 23:09:11 +02:00
</div>
2020-11-13 18:57:47 +01:00
<?php
2020-06-05 17:56:22 +02:00
exit();
}
2020-05-19 23:09:11 +02:00
}
2020-11-13 18:57:47 +01:00
if($connectionOk) {
2020-05-19 23:09:11 +02:00
try{
try{
2020-09-02 17:31:41 +02:00
$connection->exec("CREATE DATABASE IF NOT EXISTS " . trim($database));
2020-05-19 23:09:11 +02:00
} catch(Exception $e) {
//nothing
}
2020-11-13 18:57:47 +01:00
$connection->exec("use " . trim($database));
2020-05-19 23:09:11 +02:00
} catch (PDOException $e){
2020-11-13 18:57:47 +01:00
if($return) {
2020-06-05 17:56:22 +02:00
return false;
} else {
2020-11-13 18:57:47 +01:00
if(is_cli()) {
2020-06-05 17:56:22 +02:00
echo($e);
exit(7);
}
2020-11-13 18:57:47 +01:00
?>
2020-09-04 22:50:08 +02:00
<div class="wp-die-message"><h1><?php t("Cannot select database"); ?></h1>
<p><?php t("We were able to connect to the database server (which means your username and password are ok)"); echo(", "); t("but we could not select the database"); ?> <code><?php echo $database; ?></code>.</p>
2020-05-19 23:09:11 +02:00
<ul>
2020-09-04 22:50:08 +02:00
<li><?php t("Are you sure that it exists?"); ?></li>
<li><?php printf(t("Does user %s have permissions to use database %s?", false), "<code>$user</code>", "<code>$database</code>"); ?></li>
<li><?php printf(t("In some systems your database name has your username as a prefix, which is %s. Could this be the problem?", false), "<code>".$user."_".$database."</code>"); ?> </li>
2020-05-19 23:09:11 +02:00
</ul>
2020-09-04 22:50:08 +02:00
<p><?php t("If you're not sure what these terms mean, try contacting your hosting provider. Try providing the following information:"); ?></p>
2020-09-24 21:38:33 +02:00
<details open>
2020-09-04 22:50:08 +02:00
<summary><?php t("Advanced informations"); ?></summary>
2020-05-19 23:09:11 +02:00
<pre><?php echo($e); ?></pre>
</details>
2020-09-04 22:50:08 +02:00
<p class="step"><a href="#" onclick="javascript:history.go(-2);return false;" class="button button-large"><?php t("Try again"); ?></a></p>
2020-05-19 23:09:11 +02:00
</div>
2020-11-13 18:57:47 +01:00
<?php
2020-06-05 17:56:22 +02:00
exit();
}
2020-05-19 23:09:11 +02:00
}
2020-06-05 17:56:22 +02:00
return true;
2020-05-19 23:09:11 +02:00
}
}
2020-11-13 18:57:47 +01:00
function replaceInFile($edits,$file)
{
2020-05-19 23:09:11 +02:00
$content = file_get_contents($file);
foreach($edits as $edit){
2020-11-13 18:57:47 +01:00
$content = str_replace($edit[0], $edit[1], $content);
2020-05-19 23:09:11 +02:00
}
2020-11-13 18:57:47 +01:00
file_put_contents($file, $content);
2020-05-19 23:09:11 +02:00
}
2020-11-13 18:57:47 +01:00
function generateConfig($host,$user,$password,$db,$prefix,$path="..")
{
2020-05-19 23:09:11 +02:00
try{
2020-05-29 16:34:20 +02:00
if (file_exists($path.DIRECTORY_SEPARATOR.'config.php')) {
rename($path.DIRECTORY_SEPARATOR."config.php", $path.DIRECTORY_SEPARATOR."config.old.php");
2020-05-19 23:09:11 +02:00
}
2020-05-29 16:34:20 +02:00
copy($path.DIRECTORY_SEPARATOR."config-sample.php", $path.DIRECTORY_SEPARATOR."config.php");
2020-11-13 18:57:47 +01:00
replaceInFile([["@@db@@", $db],["@@user@@",$user],["@@password@@",$password],["@@host@@",$host],["@@prefix@@",$prefix]], $path.DIRECTORY_SEPARATOR."config.php");
2020-05-19 23:09:11 +02:00
} catch (Exception $e) {
2020-11-13 18:57:47 +01:00
if(is_cli()) {
2020-05-28 16:35:26 +02:00
echo($e);
exit(6);
}
2020-05-19 23:09:11 +02:00
?>
2020-09-04 22:50:08 +02:00
<div class="wp-die-message"><h1></h1>
<p><?php printf(t("We were unable to write the configuration file %s, which is required for the program to work", false), "<code>config.php</code>"); echo ".<br>"; t("It is however possible to edit it manually by following the instructions below:"); ?></p>
2020-05-19 23:09:11 +02:00
<ul>
2020-09-04 22:50:08 +02:00
<li><?php t("Access the Allerta installation folder (connect via FTP in case of cloud server)"); ?>.</li>
<li><?php printf(t("Rename the file %s to %s", false), "<code>config-sample.php</code>", "<code>config.php</code>"); ?>.</li>
<li><?php t("Replace the first 16 lines of the file with the following text:"); ?></li>
2020-05-19 23:09:11 +02:00
<code>
&lt;?php<br>
// ** Database settings ** //<br>
/* The name of the database for Allerta-vvf */<br>
define( 'DB_NAME', '<?php echo $db; ?>' );<br>
<br>
/* Database username */<br>
define( 'DB_USER', '<?php echo $user; ?>' );<br>
<br>
/* Database password */<br>
define( 'DB_PASSWORD', '<?php echo $password; ?>' );<br>
<br>
/* Database hostname */<br>
define( 'DB_HOST', '<?php echo $host; ?>' );<br>
<br>
/* Database hostname */<br>
define( 'DB_PREFIX', '<?php echo $prefix; ?>' );<br>
2021-02-20 21:05:03 +01:00
<br>
/* Sentry options */<br>
define('SENTRY_CSP_REPORT_URI', '');<br>
2021-03-01 16:35:56 +01:00
define('SENTRY_ENABLED', false);<br>
define('SENTRY_DSN', '');<br>
define('SENTRY_ENV', 'prod');<br>
2020-05-19 23:09:11 +02:00
</code>
</ul>
2020-09-04 22:50:08 +02:00
<p><?php t("If you're not sure what these terms mean, try contacting your hosting provider. Try providing the following information:"); ?></p>
2020-09-24 21:38:33 +02:00
<details open>
2020-09-04 22:50:08 +02:00
<summary><?php t("Advanced informations"); ?></summary>
2020-05-19 23:09:11 +02:00
<pre><?php echo($e); ?></pre>
</details>
2020-09-04 22:50:08 +02:00
<p class="step"><a href="#" onclick="javascript:history.go(-1);return false;" class="button button-large"><?php t("Try again"); ?></a></p>
2020-05-19 23:09:11 +02:00
</div>
<?php
exit();
}
}
2020-11-13 18:57:47 +01:00
function initDB()
{
2020-05-19 23:09:11 +02:00
try{
2020-11-13 18:57:47 +01:00
$connection = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
2020-05-19 23:09:11 +02:00
$prefix = DB_PREFIX;
2020-11-13 18:57:47 +01:00
$connection->exec(
"
2020-07-03 12:10:41 +02:00
CREATE TABLE IF NOT EXISTS `".$prefix."_trainings` (
2020-05-19 23:09:11 +02:00
`id` int(11) NOT NULL AUTO_INCREMENT,
2020-11-25 11:29:19 +01:00
`date` date NOT NULL,
`name` varchar(999) NOT NULL,
2020-11-25 11:29:19 +01:00
`beginning` time NOT NULL,
`end` time NOT NULL,
`crew` text NOT NULL,
`chief` text NOT NULL,
`place` text NOT NULL,
`notes` text NOT NULL,
2020-09-27 00:57:37 +02:00
`increment` varchar(999) NOT NULL DEFAULT 'test',
`inserted_by` varchar(200) NOT NULL DEFAULT 'test',
2020-05-19 23:09:11 +02:00
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
2020-07-03 12:10:41 +02:00
CREATE TABLE IF NOT EXISTS `".$prefix."_services` (
2020-05-19 23:09:11 +02:00
`id` int(11) NOT NULL AUTO_INCREMENT,
2020-11-25 11:29:19 +01:00
`date` date NOT NULL,
`code` text NOT NULL,
`beginning` time NOT NULL,
`end` time NOT NULL,
`chief` varchar(999) NOT NULL DEFAULT 'test',
`drivers` varchar(999) NOT NULL DEFAULT 'test',
`crew` varchar(999) NOT NULL DEFAULT 'test',
`place` varchar(999) NOT NULL DEFAULT 'test',
`notes` varchar(999) NOT NULL DEFAULT 'test',
`type` text NOT NULL,
2020-09-27 00:57:37 +02:00
`increment` varchar(999) NOT NULL,
`inserted_by` varchar(200) NOT NULL,
2020-05-19 23:09:11 +02:00
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
2020-05-29 12:13:33 +02:00
CREATE TABLE IF NOT EXISTS `".$prefix."_intrusions` (
2020-05-19 23:09:11 +02:00
`id` int(11) NOT NULL AUTO_INCREMENT,
2020-09-27 00:57:37 +02:00
`page` varchar(999) COLLATE utf8mb4_unicode_ci NOT NULL,
2020-11-25 11:29:19 +01:00
`date` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`hour` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
2020-05-19 23:09:11 +02:00
`ip` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
2020-11-25 11:29:19 +01:00
`server_var` varchar(9999) COLLATE utf8mb4_unicode_ci NOT NULL,
2020-05-19 23:09:11 +02:00
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `".$prefix."_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
2020-05-29 14:57:13 +02:00
`action` varchar(100) NOT NULL,
`changed` varchar(100),
`editor` varchar(100),
2020-05-29 14:57:13 +02:00
`date` varchar(100) NOT NULL,
`time` varchar(100) NOT NULL,
2020-05-19 23:09:11 +02:00
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
2020-09-02 17:31:41 +02:00
CREATE TABLE IF NOT EXISTS `".$prefix."_minutes` (
2020-05-19 23:09:11 +02:00
`id` int(11) NOT NULL AUTO_INCREMENT,
2020-09-02 17:31:41 +02:00
`month` int(2) NOT NULL,
`year` int(2) NOT NULL,
2020-05-19 23:09:11 +02:00
`list` mediumtext NOT NULL,
PRIMARY KEY (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
2020-11-25 11:29:19 +01:00
CREATE TABLE IF NOT EXISTS `".$prefix."_type` (
2020-05-19 23:09:11 +02:00
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
2020-05-19 23:09:11 +02:00
PRIMARY KEY (`id`),
2020-11-25 11:29:19 +01:00
UNIQUE KEY `type_name` (`name`(99))
2020-05-19 23:09:11 +02:00
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
2020-05-20 19:56:39 +02:00
CREATE TABLE IF NOT EXISTS `".$prefix."_users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`email` varchar(249) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`username` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` tinyint(2) unsigned NOT NULL DEFAULT '0',
`verified` tinyint(1) unsigned NOT NULL DEFAULT '0',
`resettable` tinyint(1) unsigned NOT NULL DEFAULT '1',
`roles_mask` int(10) unsigned NOT NULL DEFAULT '0',
`registered` int(10) unsigned NOT NULL,
`last_login` int(10) unsigned DEFAULT NULL,
`force_logout` mediumint(7) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `Id` (`id`),
UNIQUE KEY `email` (`email`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `".$prefix."_profiles` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
2020-05-19 23:09:11 +02:00
`hidden` BOOLEAN NOT NULL DEFAULT FALSE,
`disabled` BOOLEAN NOT NULL DEFAULT FALSE,
`name` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
2020-07-03 11:38:41 +02:00
`available` tinyint(1) NOT NULL DEFAULT 0,
2020-09-27 13:23:37 +02:00
`chief` tinyint(1) NOT NULL DEFAULT 0,
2020-11-25 11:29:19 +01:00
`driver` tinyint(1) NOT NULL DEFAULT 0,
`phone_number` varchar(25) DEFAULT NULL,
2020-07-03 12:10:41 +02:00
`services` int(11) NOT NULL DEFAULT 0,
`trainings` int(11) NOT NULL DEFAULT 0,
`online_time` int(11) NOT NULL DEFAULT 0,
2020-09-02 17:31:41 +02:00
`availability_minutes` int(11) NOT NULL DEFAULT 0,
2020-11-25 11:29:19 +01:00
`image` varchar(1000) DEFAULT NULL,
2020-05-19 23:09:11 +02:00
PRIMARY KEY (`id`),
KEY `Id` (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS `".$prefix."_users_confirmations` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(10) unsigned NOT NULL,
`email` varchar(249) COLLATE utf8mb4_unicode_ci NOT NULL,
`selector` varchar(16) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`token` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`expires` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `selector` (`selector`),
KEY `email_expires` (`email`,`expires`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `".$prefix."_users_remembered` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user` int(10) unsigned NOT NULL,
`selector` varchar(24) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`token` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`expires` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `selector` (`selector`),
KEY `user` (`user`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `".$prefix."_users_resets` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`user` int(10) unsigned NOT NULL,
`selector` varchar(20) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`token` varchar(255) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`expires` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `selector` (`selector`),
KEY `user_expires` (`user`,`expires`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `".$prefix."_users_throttling` (
`bucket` varchar(44) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,
`tokens` float unsigned NOT NULL,
`replenished_at` int(10) unsigned NOT NULL,
`expires_at` int(10) unsigned NOT NULL,
PRIMARY KEY (`bucket`),
KEY `expires_at` (`expires_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
2020-05-19 23:09:11 +02:00
CREATE TABLE IF NOT EXISTS `".$prefix."_options` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` TEXT NOT NULL, `value` MEDIUMTEXT NOT NULL,
`enabled` BOOLEAN NOT NULL DEFAULT TRUE,
`created_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`last_edit` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_id` INT NOT NULL,
PRIMARY KEY (`id`),
KEY `Id` (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `".$prefix."_dbversion` (
`id` INT NOT NULL AUTO_INCREMENT,
`version` INT NOT NULL,
2020-11-13 18:57:47 +01:00
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
2020-05-19 23:09:11 +02:00
PRIMARY KEY (`id`),
KEY `Id` (`id`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `".$prefix."_api_keys` (
`id` INT NOT NULL AUTO_INCREMENT,
`apikey` VARCHAR(128) NOT NULL,
`user` INT NOT NULL,
`permissions` VARCHAR(128) NOT NULL DEFAULT 'ALL',
PRIMARY KEY (`id`),
KEY `Id` (`id`)
) ENGINE = InnoDB DEFAULT CHARSET=latin1;
2020-07-12 10:57:02 +02:00
CREATE TABLE `".$prefix."_bot_telegram` (
`id` INT NOT NULL AUTO_INCREMENT,
`chat_id` VARCHAR(128) NOT NULL,
`user` INT NOT NULL,
PRIMARY KEY (`id`),
KEY `Id` (`id`)
) ENGINE = InnoDB DEFAULT CHARSET=latin1;
2020-06-09 22:44:56 +02:00
INSERT INTO `".$prefix."_dbversion` (`version`, `timestamp`) VALUES('1', current_timestamp());
2020-11-25 11:29:19 +01:00
INSERT INTO `".$prefix."_type` (`id`, `name`) VALUES (NULL, 'type1'), (NULL, 'type2');"
2020-11-13 18:57:47 +01:00
);
2020-05-19 23:09:11 +02:00
} catch (Exception $e) {
2020-11-13 18:57:47 +01:00
if(is_cli()) {
2020-05-28 16:35:26 +02:00
echo($e);
exit(10);
}
2020-05-19 23:09:11 +02:00
?>
2020-09-04 22:50:08 +02:00
<div class="wp-die-message"><h1><?php t("Unable to create tables"); ?></h1>
<p><?php t("We were able to connect to the database server (which means your username and password are ok)"); echo(", "); t("but we were unable to create the tables"); ?>.</p>
<p><?php t("If you're not sure what these terms mean, try contacting your hosting provider. Try providing the following information:"); ?></p>
2020-09-24 21:38:33 +02:00
<details open>
2020-09-04 22:50:08 +02:00
<summary><?php t("Advanced informations"); ?></summary>
2020-05-19 23:09:11 +02:00
<pre><?php echo($e); ?></pre>
</details>
2020-09-04 22:50:08 +02:00
<p class="step"><a href="#" onclick="javascript:history.go(-1);return false;" class="button button-large"><?php t("Try again"); ?></a></p>
2020-05-19 23:09:11 +02:00
</div>
<?php
exit();
}
}
2020-06-06 18:58:37 +02:00
function full_path()
{
$s = &$_SERVER;
$ssl = (!empty($s['HTTPS']) && $s['HTTPS'] == 'on') ? true:false;
$sp = strtolower($s['SERVER_PROTOCOL']);
$protocol = substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : '');
$port = $s['SERVER_PORT'];
$port = ((!$ssl && $port=='80') || ($ssl && $port=='443')) ? '' : ':'.$port;
$host = isset($s['HTTP_X_FORWARDED_HOST']) ? $s['HTTP_X_FORWARDED_HOST'] : (isset($s['HTTP_HOST']) ? $s['HTTP_HOST'] : null);
$host = isset($host) ? $host : $s['SERVER_NAME'] . $port;
$uri = $protocol . '://' . $host . $s['REQUEST_URI'];
$segments = explode('?', $uri, 2);
$url = $segments[0];
return $url;
}
2021-03-16 12:10:38 +01:00
function initOptions($name, $visible, $developer, $password, $report_email, $owner, $url=null)
2020-11-13 18:57:47 +01:00
{
2020-05-19 23:09:11 +02:00
try{
2020-11-13 18:57:47 +01:00
include_once "../config.php";
$connection = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASSWORD, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
2020-05-19 23:09:11 +02:00
$prefix = DB_PREFIX;
$auth = new \Delight\Auth\Auth($connection, $_SERVER['REMOTE_ADDR'], $prefix."_");
$userId = $auth->register($report_email, $password, $name);
2020-09-03 22:57:04 +02:00
$auth->admin()->addRoleForUserById($userId, \Delight\Auth\Role::SUPER_ADMIN);
2020-11-13 18:57:47 +01:00
if($developer) {
2020-09-03 22:57:04 +02:00
$auth->admin()->addRoleForUserById($userId, \Delight\Auth\Role::DEVELOPER);
}
2020-12-03 23:47:43 +01:00
$options = [
'check_cf_ip' => ':check_cf_ip',
'report_email' => ':report_email',
'owner' => ':owner',
'web_url' => ':web_url',
'use_custom_error_sound' => 0,
'use_custom_error_image' => 0,
'intrusion_save' => 1,
'intrusion_save_info' => 1,
'enable_technical_support' => 0,
'technical_support_key' => 0,
'cron_job_code' => ':cron_job_code',
'cron_job_enabled' => 1,
'cron_job_time' => ':cron_job_time',
'service_edit' => 1,
'service_remove' => 1,
'training_edit' => 1,
'training_remove' => 1,
'use_location_picker' => 1,
'force_language' => 0,
'force_remember_cookie' => 0
2020-12-03 23:47:43 +01:00
];
$query = "";
foreach ($options as $key => $value) {
$query .= "
INSERT INTO `".$prefix."_options` (`id`, `name`, `value`, `enabled`, `created_time`, `last_edit`, `user_id`) VALUES (NULL, '".$key."', $value, 1, current_timestamp(), current_timestamp(), '1');";
}
$query = "
INSERT INTO `".$prefix."_profiles` (`id`, `hidden`) VALUES (NULL, :hidden);".$query;
$prep = $connection->prepare($query);
mt_srand(10);
2020-12-03 23:47:43 +01:00
$prep->bindValue(':check_cf_ip', (empty($_SERVER['HTTP_CF_CONNECTING_IP']) ? 0 : 1), PDO::PARAM_INT);
2020-11-13 18:57:47 +01:00
$prep->bindValue(':hidden', ($visible ? 0 : 1), PDO::PARAM_INT);
2020-05-29 11:35:41 +02:00
$prep->bindValue(':report_email', $report_email, PDO::PARAM_STR);
$prep->bindValue(':owner', $owner, PDO::PARAM_STR);
2021-03-16 12:10:38 +01:00
if(is_null($url)){
$url = str_replace("install/install.php", "", full_path());
}
$prep->bindValue(':web_url', $url, PDO::PARAM_STR);
2020-09-02 16:57:56 +02:00
$prep->bindValue(':cron_job_code', str_replace(".", "", bin2hex(random_bytes(10)).base64_encode(openssl_random_pseudo_bytes(30))), PDO::PARAM_STR);
$prep->bindValue(':cron_job_time', "01;00:00", PDO::PARAM_STR);
2020-05-19 23:09:11 +02:00
$prep->execute();
} catch (Exception $e) {
2020-11-13 18:57:47 +01:00
if(is_cli()) {
2020-05-28 16:35:26 +02:00
echo($e);
exit(11);
}
2020-05-19 23:09:11 +02:00
?>
2020-09-04 22:50:08 +02:00
<div class="wp-die-message"><h1><?php t("Unable to fill in the tables"); ?></h1>
<p><?php t("We were able to connect to the database server (which means your username and password are ok)"); echo(", "); t("but we were unable to fill in the tables"); ?>.</p>
<p><?php t("If you're not sure what these terms mean, try contacting your hosting provider. Try providing the following information:"); ?></p>
2020-09-24 21:38:33 +02:00
<details open>
2020-09-04 22:50:08 +02:00
<summary><?php t("Advanced informations"); ?></summary>
2020-05-19 23:09:11 +02:00
<pre><?php echo($e); ?></pre>
</details>
2020-09-04 22:50:08 +02:00
<p class="step"><a href="#" onclick="javascript:history.go(-1);return false;" class="button button-large"><?php t("Try again"); ?></a></p>
2020-05-19 23:09:11 +02:00
</div>
<?php
exit();
}
2020-05-28 16:35:26 +02:00
}
2020-11-13 18:57:47 +01:00
function validate_arg($options, $name, $default)
{
2020-05-31 23:56:38 +02:00
return array_key_exists($name, $options) ? $options[$name] : (getenv($name)!==false ? getenv($name) : (getenv(strtoupper($name))!==false ? getenv(strtoupper($name)) : $default));
2020-05-28 16:35:26 +02:00
}
2020-11-13 18:57:47 +01:00
function change_dir($directory)
{
2020-05-28 16:35:26 +02:00
try{
chdir($directory);
} catch(Exception $e){
2020-11-13 18:57:47 +01:00
if(is_cli()) {
2020-05-28 16:35:26 +02:00
exit(4);
}
}
}
2020-11-13 18:57:47 +01:00
function cli_helper($action, $options)
{
2020-05-29 16:34:20 +02:00
switch ($action) {
2020-11-13 18:57:47 +01:00
case "config":
$db_name = validate_arg($options, "db_name", "allerta");
$db_username = validate_arg($options, "db_username", "root");
$db_password = validate_arg($options, "db_password", "");
$db_host = validate_arg($options, "db_host", "127.0.0.1");
$db_prefix = validate_arg($options, "db_prefix", "allerta");
$path = isset($options->getOptions["path"]) ? "." : "..";
checkConnection($db_host, $db_username, $db_password, $db_name);
generateConfig($db_host, $db_username, $db_password, $db_name, $db_prefix, $path);
t("Config created successful");
exit(0);
case "populate":
$name = validate_arg($options, "name", "admin");
$visible = array_key_exists("visible", $options);
2021-03-16 12:10:38 +01:00
$developer = array_key_exists("developer", $options);
2020-11-13 18:57:47 +01:00
$password = validate_arg($options, "password", "password");
$report_email = validate_arg($options, "report_email", "postmaster@localhost.local");
$owner = validate_arg($options, "owner", "Owner");
2021-03-16 12:10:38 +01:00
$url = validate_arg($options, "url", "htp://localhost/");
2020-11-13 18:57:47 +01:00
initDB();
2021-03-16 12:10:38 +01:00
initOptions($name, $visible, $developer, $password, $report_email, $owner, $url);
2020-11-13 18:57:47 +01:00
t("DB Populated successful");
2020-12-31 11:43:50 +01:00
finalInstallationHelperStep();
2020-11-13 18:57:47 +01:00
exit(0);
2020-05-29 16:34:20 +02:00
}
}
2020-11-13 18:57:47 +01:00
function run_cli()
{
2020-05-28 16:35:26 +02:00
$_SERVER['REMOTE_ADDR'] = "127.0.0.1";
$getopt = new \GetOpt\GetOpt();
2020-11-13 18:57:47 +01:00
$getopt->addCommands(
[
\GetOpt\Command::create(
'config', 'conf', [
2020-05-28 16:35:26 +02:00
\GetOpt\Option::create('n', 'db_name', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("DB name", false))
->setArgumentName(t("DB name", false)),
2020-05-28 16:35:26 +02:00
\GetOpt\Option::create('u', 'db_username', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("DB username", false))
->setArgumentName(t("DB username", false)),
2020-05-28 16:35:26 +02:00
\GetOpt\Option::create('a', 'db_password', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("DB password", false))
->setArgumentName(t("DB password", false)),
2020-05-28 16:35:26 +02:00
\GetOpt\Option::create('o', 'db_host', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("DB host", false))
->setArgumentName(t("DB host", false)),
2020-05-28 16:35:26 +02:00
\GetOpt\Option::create('r', 'db_prefix', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("DB prefix", false))
->setArgumentName(t("DB prefix", false))
]
)->setDescription(
t("Create the config file", false).' "config.php".' . PHP_EOL .
PHP_EOL .
sprintf(t("This file is required for running %s", false), '"populate"') . "."
)->setShortDescription(t("Create a new config file", false)),
2020-05-28 16:35:26 +02:00
2020-11-13 18:57:47 +01:00
\GetOpt\Command::create(
'populate', 'Populate', [
2020-05-28 16:35:26 +02:00
\GetOpt\Option::create('m', 'name', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("Admin name", false))
->setArgumentName(t("Admin name", false)),
2020-05-28 16:35:26 +02:00
\GetOpt\Option::create('b', 'visible', \GetOpt\GetOpt::NO_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("Is admin visible?", false))
->setArgumentName(t("Is admin visible?", false)),
2021-03-16 12:10:38 +01:00
\GetOpt\Option::create('d', 'developer', \GetOpt\GetOpt::NO_ARGUMENT)
->setDescription(t("Enable devmode per the user", false))
->setArgumentName(t("Enable devmode per the user", false)),
2020-05-28 16:35:26 +02:00
\GetOpt\Option::create('s', 'password', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("Admin password", false))
->setArgumentName(t("Admin password", false)),
2020-05-28 16:35:26 +02:00
\GetOpt\Option::create('w', 'owner', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("Owner", false))
->setArgumentName(t("Owner", false)),
2020-05-28 16:35:26 +02:00
\GetOpt\Option::create('e', 'report_email', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("Report email", false))
2021-03-16 12:10:38 +01:00
->setArgumentName(t("Report email", false)),
\GetOpt\Option::create('u', 'url', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
->setDescription(t("App url", false))
->setArgumentName(t("App url", false)),
2020-11-13 18:57:47 +01:00
]
)->setDescription(
t("Populate Allerta database", false) . "." . PHP_EOL .
PHP_EOL .
sprintf(t("This require a working %s file", false), "config.php") . "."
)->setShortDescription(t("Populate DB", false))
]
);
2020-05-28 16:35:26 +02:00
2020-11-13 18:57:47 +01:00
$getopt->addOptions(
[
2020-05-28 16:35:26 +02:00
Option::create('v', 'version', \GetOpt\GetOpt::NO_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("Show version information and quit", false)),
2020-05-28 16:35:26 +02:00
Option::create('h', 'help', \GetOpt\GetOpt::NO_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("Show this help and quit", false)),
2020-05-28 16:35:26 +02:00
Option::create("p", 'path', \GetOpt\GetOpt::OPTIONAL_ARGUMENT)
2020-11-13 18:57:47 +01:00
->setDescription(t("Destination path", false))
2020-05-28 16:35:26 +02:00
->setArgumentName('path')
2020-11-13 18:57:47 +01:00
->setValidation(
'is_writable', function ($operand, $value) {
if(file_exists($value)) {
printf(t("%s is not writable. Directory permissions: %s"), $value, @fileperms($value));
exit(4);
} else {
printf(t("%s not exists"), $value);
echo(".");
exit(3);
}
2020-05-28 16:35:26 +02:00
}
2020-11-13 18:57:47 +01:00
)
]
);
2020-05-28 16:35:26 +02:00
// process arguments and catch user errors
try {
try {
$getopt->process();
} catch (Missing $exception) {
// catch missing exceptions if help is requested
2020-11-13 18:57:47 +01:00
if (!$getopt->getOption('help')) {
throw $exception;
}
2020-05-28 16:35:26 +02:00
}
} catch (ArgumentException $exception) {
file_put_contents('php://stderr', $exception->getMessage() . PHP_EOL);
echo PHP_EOL . $getopt->getHelpText();
exit;
}
// show version and quit
if ($getopt->getOption('version')) {
2021-02-20 21:05:03 +01:00
echo sprintf('%s: %s' . PHP_EOL, "AllertaVVF", "0.0");
2020-05-28 16:35:26 +02:00
exit;
}
// show help and quit
$command = $getopt->getCommand();
if (!$command || $getopt->getOption('help')) {
echo $getopt->getHelpText();
exit;
}
2020-05-29 16:34:20 +02:00
if (isset($getopt->getOptions()["path"])) {
chdir($getopt->getOption('path'));
2020-05-28 16:35:26 +02:00
}
$options = $getopt->getOptions();
switch ($command->name()) {
2020-11-13 18:57:47 +01:00
case "config":
cli_helper("config", $options);
case "populate":
cli_helper("populate", $options);
2020-05-29 16:34:20 +02:00
}
2020-11-13 18:57:47 +01:00
}