$language){ if(strpos($language, ';') == false) { $tmp_languages[$language] = 1; } else { $tmp_languages[explode(";q=", $language)[0]] = (float) explode(";q=", $language)[1]; } } 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){ if(in_array($tmp_language, $loaded_languages) && $language == null) { $language = $tmp_language; } } if(isset($_COOKIE["forceLanguage"]) && in_array($_COOKIE["forceLanguage"], $loaded_languages)){ $language = $_COOKIE["forceLanguage"]; } if (file_exists("translations/".$language.".php")) { $loaded_translations = include "translations/".$language.".php"; } else { $loaded_translations = include "translations/en.php"; } function t($string, $echo=true) { global $loaded_translations; try { if (!array_key_exists($string, $loaded_translations)) { throw new Exception('string does not exist'); } $string = $loaded_translations[$string]; } catch (\Exception $e) { //nothing } if ($echo) { echo $string; } else { return $string; } } function is_cli() //https://www.binarytides.com/php-check-running-cli/ { 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; } if (file_exists('../vendor/autoload.php')) { try { include '../vendor/autoload.php'; } catch (Exception $e) { if(is_cli()) { echo($e); exit(1); } die("Please install composer and run composer install (".$e); } } else { if(is_cli()) { echo($e); exit(1); } die("Please install composer and run composer install"); } function checkConnection($host, $user, $password, $database, $return=false) { try{ $connection = new PDO("mysql:host=$host", $user, $password, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); $connectionOk = true; } catch (PDOException $e){ if($return) { return false; } else { if(is_cli()) { echo($e); exit(8); } $connectionOk = false; ?>

config.php", "$database"); ?>.

exec("CREATE DATABASE IF NOT EXISTS " . trim($database)); } catch(Exception $e) { //nothing } $connection->exec("use " . trim($database)); } catch (PDOException $e){ if($return) { return false; } else { if(is_cli()) { echo($e); exit(7); } ?>

.

config.php"); echo ".
"; t("It is however possible to edit it manually by following the instructions below:"); ?>

PDO::ERRMODE_EXCEPTION]); $prefix = DB_PREFIX; $connection->exec( " CREATE TABLE IF NOT EXISTS `".$prefix."_trainings` ( `id` int(11) NOT NULL AUTO_INCREMENT, `date` date NOT NULL, `name` varchar(999) NOT NULL, `beginning` time NOT NULL, `end` time NOT NULL, `crew` text NOT NULL, `chief` text NOT NULL, `place` text NOT NULL, `notes` text NOT NULL, `increment` varchar(999) NOT NULL DEFAULT 'test', `inserted_by` varchar(200) NOT NULL DEFAULT 'test', PRIMARY KEY (`id`) )ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE IF NOT EXISTS `".$prefix."_services` ( `id` int(11) NOT NULL AUTO_INCREMENT, `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, `increment` varchar(999) NOT NULL, `inserted_by` varchar(200) NOT NULL, PRIMARY KEY (`id`) )ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE IF NOT EXISTS `".$prefix."_intrusions` ( `id` int(11) NOT NULL AUTO_INCREMENT, `page` varchar(999) COLLATE utf8mb4_unicode_ci NOT NULL, `date` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, `hour` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, `ip` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL, `server_var` varchar(9999) COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (`id`) )ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE IF NOT EXISTS `".$prefix."_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `action` varchar(100) NOT NULL, `changed` varchar(100), `editor` varchar(100), `date` varchar(100) NOT NULL, `time` varchar(100) NOT NULL, PRIMARY KEY (`id`) )ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE IF NOT EXISTS `".$prefix."_minutes` ( `id` int(11) NOT NULL AUTO_INCREMENT, `month` int(2) NOT NULL, `year` int(2) NOT NULL, `list` mediumtext NOT NULL, PRIMARY KEY (`id`) )ENGINE=InnoDB DEFAULT CHARSET=latin1; CREATE TABLE IF NOT EXISTS `".$prefix."_type` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` text NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `type_name` (`name`(99)) )ENGINE=InnoDB DEFAULT CHARSET=latin1; 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, `hidden` BOOLEAN NOT NULL DEFAULT FALSE, `disabled` BOOLEAN NOT NULL DEFAULT FALSE, `name` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `available` tinyint(1) NOT NULL DEFAULT 0, `chief` tinyint(1) NOT NULL DEFAULT 0, `driver` tinyint(1) NOT NULL DEFAULT 0, `phone_number` varchar(25) DEFAULT NULL, `services` int(11) NOT NULL DEFAULT 0, `trainings` int(11) NOT NULL DEFAULT 0, `online_time` int(11) NOT NULL DEFAULT 0, `availability_minutes` int(11) NOT NULL DEFAULT 0, `image` varchar(1000) DEFAULT NULL, 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; 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, `timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, 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; 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; INSERT INTO `".$prefix."_dbversion` (`version`, `timestamp`) VALUES('1', current_timestamp()); INSERT INTO `".$prefix."_type` (`id`, `name`) VALUES (NULL, 'type1'), (NULL, 'type2');" ); } catch (Exception $e) { if(is_cli()) { echo($e); exit(10); } ?>

.

PDO::ERRMODE_EXCEPTION]); $prefix = DB_PREFIX; $auth = new \Delight\Auth\Auth($connection, $_SERVER['REMOTE_ADDR'], $prefix."_"); $userId = $auth->register($report_email, $password, $name); $auth->admin()->addRoleForUserById($userId, \Delight\Auth\Role::SUPER_ADMIN); if($developer) { $auth->admin()->addRoleForUserById($userId, \Delight\Auth\Role::DEVELOPER); } $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 ]; $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); $prep->bindValue(':check_cf_ip', (empty($_SERVER['HTTP_CF_CONNECTING_IP']) ? 0 : 1), PDO::PARAM_INT); $prep->bindValue(':hidden', ($visible ? 0 : 1), PDO::PARAM_INT); $prep->bindValue(':report_email', $report_email, PDO::PARAM_STR); $prep->bindValue(':owner', $owner, PDO::PARAM_STR); if(is_null($url)){ $url = str_replace("install/install.php", "", full_path()); } $prep->bindValue(':web_url', $url, PDO::PARAM_STR); $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); $prep->execute(); } catch (Exception $e) { if(is_cli()) { echo($e); exit(11); } ?>

.

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); $developer = array_key_exists("developer", $options); $password = validate_arg($options, "password", "password"); $report_email = validate_arg($options, "report_email", "postmaster@localhost.local"); $owner = validate_arg($options, "owner", "Owner"); $url = validate_arg($options, "url", "htp://localhost/"); initDB(); initOptions($name, $visible, $developer, $password, $report_email, $owner, $url); t("DB Populated successful"); finalInstallationHelperStep(); exit(0); } } function run_cli() { $_SERVER['REMOTE_ADDR'] = "127.0.0.1"; $getopt = new \GetOpt\GetOpt(); $getopt->addCommands( [ \GetOpt\Command::create( 'config', 'conf', [ \GetOpt\Option::create('n', 'db_name', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->setDescription(t("DB name", false)) ->setArgumentName(t("DB name", false)), \GetOpt\Option::create('u', 'db_username', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->setDescription(t("DB username", false)) ->setArgumentName(t("DB username", false)), \GetOpt\Option::create('a', 'db_password', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->setDescription(t("DB password", false)) ->setArgumentName(t("DB password", false)), \GetOpt\Option::create('o', 'db_host', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->setDescription(t("DB host", false)) ->setArgumentName(t("DB host", false)), \GetOpt\Option::create('r', 'db_prefix', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->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)), \GetOpt\Command::create( 'populate', 'Populate', [ \GetOpt\Option::create('m', 'name', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->setDescription(t("Admin name", false)) ->setArgumentName(t("Admin name", false)), \GetOpt\Option::create('b', 'visible', \GetOpt\GetOpt::NO_ARGUMENT) ->setDescription(t("Is admin visible?", false)) ->setArgumentName(t("Is admin visible?", false)), \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)), \GetOpt\Option::create('s', 'password', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->setDescription(t("Admin password", false)) ->setArgumentName(t("Admin password", false)), \GetOpt\Option::create('w', 'owner', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->setDescription(t("Owner", false)) ->setArgumentName(t("Owner", false)), \GetOpt\Option::create('e', 'report_email', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->setDescription(t("Report email", false)) ->setArgumentName(t("Report email", false)), \GetOpt\Option::create('u', 'url', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->setDescription(t("App url", false)) ->setArgumentName(t("App url", false)), ] )->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)) ] ); $getopt->addOptions( [ Option::create('v', 'version', \GetOpt\GetOpt::NO_ARGUMENT) ->setDescription(t("Show version information and quit", false)), Option::create('h', 'help', \GetOpt\GetOpt::NO_ARGUMENT) ->setDescription(t("Show this help and quit", false)), Option::create("p", 'path', \GetOpt\GetOpt::OPTIONAL_ARGUMENT) ->setDescription(t("Destination path", false)) ->setArgumentName('path') ->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); } } ) ] ); // process arguments and catch user errors try { try { $getopt->process(); } catch (Missing $exception) { // catch missing exceptions if help is requested if (!$getopt->getOption('help')) { throw $exception; } } } 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')) { echo sprintf('%s: %s' . PHP_EOL, "AllertaVVF", "0.0"); exit; } // show help and quit $command = $getopt->getCommand(); if (!$command || $getopt->getOption('help')) { echo $getopt->getHelpText(); exit; } if (isset($getopt->getOptions()["path"])) { chdir($getopt->getOption('path')); } $options = $getopt->getOptions(); switch ($command->name()) { case "config": cli_helper("config", $options); case "populate": cli_helper("populate", $options); } }