2021-11-30 08:50:53 -08:00
|
|
|
|
<?php
|
2025-03-29 10:50:03 +00:00
|
|
|
|
|
2021-11-30 08:50:53 -08:00
|
|
|
|
/*
|
|
|
|
|
|
* there is NO SUPPORT offered for this script.
|
|
|
|
|
|
* this script is provided AS IS and without any warranty.
|
|
|
|
|
|
* this script was made with the primary goal of making the install process automatic for hosting providers + our API test suite.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2025-03-29 10:50:03 +00:00
|
|
|
|
function getEnvVar(string $name, ?string $fallback = null, ?array $valid_values = null)
|
2024-03-09 03:19:55 -08:00
|
|
|
|
{
|
2021-12-22 00:17:50 -08:00
|
|
|
|
$value = getenv($name);
|
2022-04-08 12:46:07 -07:00
|
|
|
|
$required = $fallback === null;
|
2021-12-22 00:17:50 -08:00
|
|
|
|
|
|
|
|
|
|
if ($value === false && $required) {
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo "⚠️ Required environment variable '$name' is not set!" . PHP_EOL;
|
2021-12-22 00:17:50 -08:00
|
|
|
|
exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-08 12:46:07 -07:00
|
|
|
|
if (!$value && $fallback !== null) {
|
2022-03-24 10:51:22 -07:00
|
|
|
|
$value = $fallback;
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo "ℹ️ Environment variable '$name' is not set, using fallback '$fallback'" . PHP_EOL;
|
2022-03-24 10:51:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-06-02 18:28:03 +02:00
|
|
|
|
if ($valid_values != null && !in_array($value, $valid_values)) {
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo "⚠️ Environment variable '$name' has invalid value";
|
2022-06-02 18:28:03 +02:00
|
|
|
|
exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-24 10:51:22 -07:00
|
|
|
|
return $value;
|
2021-12-22 00:17:50 -08:00
|
|
|
|
}
|
2021-12-20 17:12:20 +01:00
|
|
|
|
|
2022-03-24 10:51:22 -07:00
|
|
|
|
if (PHP_SAPI !== 'cli') {
|
2021-11-30 08:50:53 -08:00
|
|
|
|
die('This script must be run from the command line.');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-22 00:17:50 -08:00
|
|
|
|
if (!isset($argv[1]) || $argv[1] !== '--iSwearIKnowWhatImDoing') {
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo "🚫 You don't know what you're doing." . PHP_EOL;
|
2021-12-05 22:13:49 -08:00
|
|
|
|
exit(1);
|
2021-11-30 08:50:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo PHP_EOL;
|
2021-11-30 08:50:53 -08:00
|
|
|
|
|
|
|
|
|
|
$reinstall = false;
|
|
|
|
|
|
if (isset($argv[2]) && $argv[2] == '--reinstall') {
|
|
|
|
|
|
$reinstall = true;
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '🧨 Reinstall mode enabled! ' . PHP_EOL . PHP_EOL;
|
2021-11-30 08:50:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-20 14:32:10 -08:00
|
|
|
|
if (!file_exists('./vendor/autoload.php')) {
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '⚠️ You need to run "composer install" first!' . PHP_EOL;
|
2021-12-20 14:32:10 -08:00
|
|
|
|
exit(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-11-30 08:50:53 -08:00
|
|
|
|
if (!$reinstall && file_exists('./core/config.php')) {
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '⚠️ NamelessMC is already installed! ' . PHP_EOL;
|
|
|
|
|
|
echo "🧨 If you want to reinstall, run this script with the '--reinstall' flag." . PHP_EOL;
|
2021-12-05 22:13:49 -08:00
|
|
|
|
exit(1);
|
2021-11-30 08:50:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$start = microtime(true);
|
|
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '🗑 Deleting cache directories...' . PHP_EOL;
|
2021-11-30 08:50:53 -08:00
|
|
|
|
// clear the cache directories
|
|
|
|
|
|
$folders = [
|
|
|
|
|
|
'./cache',
|
2024-03-09 03:19:55 -08:00
|
|
|
|
'./cache/templates_c',
|
2021-11-30 08:50:53 -08:00
|
|
|
|
];
|
2023-06-21 10:19:46 -07:00
|
|
|
|
$whitelist = [
|
|
|
|
|
|
'0_DO_NOT_DELETE.txt',
|
|
|
|
|
|
'.htaccess',
|
|
|
|
|
|
];
|
2021-11-30 08:50:53 -08:00
|
|
|
|
foreach ($folders as $folder) {
|
|
|
|
|
|
if (is_dir($folder)) {
|
|
|
|
|
|
$files = glob($folder . '/*');
|
|
|
|
|
|
foreach ($files as $file) {
|
2023-06-21 10:19:46 -07:00
|
|
|
|
if (!in_array(basename($file), $whitelist)) {
|
|
|
|
|
|
if (is_file($file)) {
|
|
|
|
|
|
unlink($file);
|
2024-03-09 03:19:55 -08:00
|
|
|
|
} elseif (is_dir($file)) {
|
2023-06-21 10:19:46 -07:00
|
|
|
|
rmdir($file);
|
|
|
|
|
|
}
|
2021-11-30 08:50:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-20 17:12:20 +01:00
|
|
|
|
if ($reinstall) {
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '🗑 Deleting old config.php file...' . PHP_EOL;
|
2021-12-20 17:12:20 +01:00
|
|
|
|
// delete the core/config.php file
|
|
|
|
|
|
if (is_file('./core/config.php')) {
|
|
|
|
|
|
unlink('./core/config.php');
|
|
|
|
|
|
}
|
2021-11-30 08:50:53 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-12 21:40:51 +01:00
|
|
|
|
const ROOT_PATH = __DIR__ . '/../..';
|
2022-06-13 02:21:21 +00:00
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '♻️ Registering autoloader...' . PHP_EOL;
|
2022-06-13 02:21:21 +00:00
|
|
|
|
require './vendor/autoload.php';
|
2021-11-30 08:50:53 -08:00
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '✍️ Creating new config.php file...' . PHP_EOL;
|
2021-11-30 08:50:53 -08:00
|
|
|
|
$conf = [
|
|
|
|
|
|
'mysql' => [
|
2022-04-08 12:46:07 -07:00
|
|
|
|
'host' => getEnvVar('NAMELESS_DATABASE_ADDRESS', '127.0.0.1'),
|
|
|
|
|
|
'port' => getEnvVar('NAMELESS_DATABASE_PORT', '3306'),
|
|
|
|
|
|
'username' => getEnvVar('NAMELESS_DATABASE_USERNAME', 'root'),
|
|
|
|
|
|
'password' => getEnvVar('NAMELESS_DATABASE_PASSWORD', ''),
|
|
|
|
|
|
'db' => getEnvVar('NAMELESS_DATABASE_NAME', 'nameless'),
|
2023-04-22 09:36:26 +01:00
|
|
|
|
'charset' => getEnvVar('NAMELESS_DATABASE_CHARSET', 'utf8mb4'),
|
|
|
|
|
|
'collation' => getEnvVar('NAMELESS_DATABASE_COLLATION', 'utf8mb4_unicode_ci'),
|
2021-11-30 08:50:53 -08:00
|
|
|
|
'initialise_charset' => true,
|
2023-04-22 09:36:26 +01:00
|
|
|
|
'initialise_collation' => true,
|
2021-11-30 08:50:53 -08:00
|
|
|
|
],
|
|
|
|
|
|
'remember' => [
|
|
|
|
|
|
'cookie_name' => 'nl2',
|
2024-10-17 23:21:27 -07:00
|
|
|
|
'cookie_expiry' => 2629800,
|
2021-11-30 08:50:53 -08:00
|
|
|
|
],
|
|
|
|
|
|
'session' => [
|
|
|
|
|
|
'session_name' => '2user',
|
|
|
|
|
|
'admin_name' => '2admin',
|
|
|
|
|
|
'token_name' => '2token',
|
|
|
|
|
|
],
|
|
|
|
|
|
'core' => [
|
2022-04-08 12:46:07 -07:00
|
|
|
|
'hostname' => getEnvVar('NAMELESS_HOSTNAME', 'localhost'),
|
|
|
|
|
|
'path' => getEnvVar('NAMELESS_PATH', ''),
|
2022-06-13 02:21:21 +00:00
|
|
|
|
'friendly' => getEnvVar('NAMELESS_FRIENDLY_URLS', 'false') === 'true',
|
2021-11-30 08:50:53 -08:00
|
|
|
|
'force_https' => false,
|
|
|
|
|
|
'force_www' => false,
|
|
|
|
|
|
'captcha' => false,
|
2022-05-22 00:46:28 +02:00
|
|
|
|
'date_format' => 'd M Y, H:i',
|
Bring back trusted proxies, somewhat securly
Ideal situation: Completely deny ALL requests with a proxy header if no trusted proxies are configured. Force proper configuration.
After Samerton's change just before pr13: If not configured (there is no warning), anyone can bypass IP checks, like for IP bans. Just to be clear: this affects EVERYONE, not just people with or without a proxy.
Situation now: If not configured, only the actual client address is trusted, not any header. This means users are not able to bypass IP bans, but if an admin bans a user, if that site uses a proxy, and if trusted proxies is not configured, they wil ban the proxy, meaning all users will be ip-banned. A warning is displayed in StaffCP if trusted proxies is not configured.
2022-06-12 23:12:21 +02:00
|
|
|
|
'trustedProxies' => null,
|
2021-11-30 08:50:53 -08:00
|
|
|
|
],
|
|
|
|
|
|
];
|
|
|
|
|
|
|
2022-06-13 02:21:21 +00:00
|
|
|
|
Config::write($conf);
|
2021-11-30 08:50:53 -08:00
|
|
|
|
|
2021-12-20 17:12:20 +01:00
|
|
|
|
if ($reinstall) {
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '🗑️ Deleting old database...' . PHP_EOL;
|
2022-05-11 12:14:37 +02:00
|
|
|
|
$instance = DB::getCustomInstance(
|
|
|
|
|
|
$conf['mysql']['host'],
|
|
|
|
|
|
$conf['mysql']['db'],
|
|
|
|
|
|
$conf['mysql']['username'],
|
|
|
|
|
|
$conf['mysql']['password'],
|
2022-05-15 14:42:42 +02:00
|
|
|
|
$conf['mysql']['port']
|
|
|
|
|
|
);
|
2022-05-01 23:16:22 -07:00
|
|
|
|
$instance->query('DROP DATABASE IF EXISTS `' . $conf['mysql']['db'] . '`');
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '✍️ Creating new database...' . PHP_EOL;
|
2022-05-01 23:16:22 -07:00
|
|
|
|
$instance->query('CREATE DATABASE `' . $conf['mysql']['db'] . '`');
|
2021-12-20 17:12:20 +01:00
|
|
|
|
}
|
2021-11-30 08:50:53 -08:00
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '✍️ Creating tables...' . PHP_EOL;
|
2022-06-13 02:21:21 +00:00
|
|
|
|
|
2023-06-10 17:38:20 +01:00
|
|
|
|
$message = PhinxAdapter::migrate('Core');
|
2022-06-13 02:21:21 +00:00
|
|
|
|
|
|
|
|
|
|
if (!str_contains($message, 'All Done')) {
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo $message;
|
2022-06-13 02:21:21 +00:00
|
|
|
|
exit(1);
|
|
|
|
|
|
}
|
2021-11-30 08:50:53 -08:00
|
|
|
|
|
2022-04-23 06:27:10 -07:00
|
|
|
|
Session::put('default_language', getEnvVar('NAMELESS_DEFAULT_LANGUAGE', 'en_UK'));
|
2021-11-30 08:50:53 -08:00
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '✍️ Inserting default data to database...' . PHP_EOL;
|
2022-05-31 11:29:47 -06:00
|
|
|
|
|
2023-01-20 08:30:34 -08:00
|
|
|
|
$_SESSION['install_timezone'] = in_array($timezone = getEnvVar('NAMELESS_TIMEZONE', 'Europe/London'), DateTimeZone::listIdentifiers())
|
|
|
|
|
|
? $timezone
|
|
|
|
|
|
: 'Europe/London';
|
|
|
|
|
|
|
2022-06-13 02:21:21 +00:00
|
|
|
|
DatabaseInitialiser::runPreUser();
|
2022-06-01 00:20:38 +02:00
|
|
|
|
|
2025-05-04 14:14:29 -07:00
|
|
|
|
Settings::set('sitename', getEnvVar('NAMELESS_SITE_NAME', 'NamelessMC'));
|
|
|
|
|
|
Settings::set('incoming_email', getEnvVar('NAMELESS_SITE_CONTACT_EMAIL', 'contact@example.com'));
|
|
|
|
|
|
Settings::set('outgoing_email', getEnvVar('NAMELESS_SITE_OUTGOING_EMAIL', 'no-reply@example.com'));
|
2023-06-12 10:30:49 -06:00
|
|
|
|
Settings::set('email_verification', getEnvVar('NAMELESS_EMAIL_VERIFICATION', '1', ['0', '1']));
|
2021-11-30 08:50:53 -08:00
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo '👮 Creating admin account...' . PHP_EOL;
|
2022-03-24 10:51:22 -07:00
|
|
|
|
|
2022-04-08 12:46:07 -07:00
|
|
|
|
$username = getEnvVar('NAMELESS_ADMIN_USERNAME', 'admin');
|
|
|
|
|
|
$password = getEnvVar('NAMELESS_ADMIN_PASSWORD', 'password');
|
2025-05-04 14:14:29 -07:00
|
|
|
|
$email = getEnvVar('NAMELESS_ADMIN_EMAIL', 'admin@example.com');
|
2022-03-24 10:51:22 -07:00
|
|
|
|
|
2021-11-30 08:50:53 -08:00
|
|
|
|
$user = new User();
|
|
|
|
|
|
$user->create([
|
2022-03-24 10:51:22 -07:00
|
|
|
|
'username' => $username,
|
|
|
|
|
|
'nickname' => $username,
|
|
|
|
|
|
'password' => password_hash($password, PASSWORD_BCRYPT, ['cost' => 13]),
|
2021-11-30 08:50:53 -08:00
|
|
|
|
'pass_method' => 'default',
|
|
|
|
|
|
'joined' => date('U'),
|
2022-03-24 10:51:22 -07:00
|
|
|
|
'email' => $email,
|
2021-11-30 08:50:53 -08:00
|
|
|
|
'lastip' => '127.0.0.1',
|
2022-05-31 11:29:47 -06:00
|
|
|
|
'active' => true,
|
2021-11-30 08:50:53 -08:00
|
|
|
|
'last_online' => date('U'),
|
2022-05-28 14:10:01 +02:00
|
|
|
|
'language_id' => DB::getInstance()->get('languages', ['is_default', 1])->results()[0]->id,
|
2023-01-20 08:30:34 -08:00
|
|
|
|
'timezone' => $_SESSION['install_timezone'],
|
2023-01-26 12:26:58 -08:00
|
|
|
|
'register_method' => 'nameless',
|
2021-11-30 08:50:53 -08:00
|
|
|
|
]);
|
2022-05-01 23:16:22 -07:00
|
|
|
|
DB::getInstance()->query('INSERT INTO `nl2_users_groups` (`user_id`, `group_id`, `received`, `expire`) VALUES (?, ?, ?, ?)', [
|
2021-11-30 08:50:53 -08:00
|
|
|
|
1,
|
|
|
|
|
|
2,
|
|
|
|
|
|
date('U'),
|
|
|
|
|
|
0,
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
2022-04-16 18:42:24 +02:00
|
|
|
|
$profile = ProfileUtils::getProfile($username);
|
|
|
|
|
|
if ($profile !== null) {
|
2022-04-17 20:10:53 -07:00
|
|
|
|
$result = $profile->getProfileAsArray();
|
2022-04-16 18:42:24 +02:00
|
|
|
|
if (isset($result['uuid']) && !empty($result['uuid'])) {
|
|
|
|
|
|
$uuid = $result['uuid'];
|
2022-04-17 20:10:53 -07:00
|
|
|
|
|
2022-05-31 21:11:21 +02:00
|
|
|
|
DB::getInstance()->insert('users_integrations', [
|
2022-04-16 18:42:24 +02:00
|
|
|
|
'integration_id' => 1,
|
|
|
|
|
|
'user_id' => 1,
|
|
|
|
|
|
'identifier' => $uuid,
|
|
|
|
|
|
'username' => $username,
|
2022-05-31 11:29:47 -06:00
|
|
|
|
'verified' => true,
|
2022-04-16 18:42:24 +02:00
|
|
|
|
'date' => date('U'),
|
|
|
|
|
|
]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-05-31 11:29:47 -06:00
|
|
|
|
DatabaseInitialiser::runPostUser();
|
|
|
|
|
|
|
2023-04-10 13:34:29 -07:00
|
|
|
|
Config::set('core.installed', true);
|
|
|
|
|
|
|
2024-03-09 03:19:55 -08:00
|
|
|
|
echo PHP_EOL . '✅ Installation complete! (Took ' . round(microtime(true) - $start, 2) . ' seconds)' . PHP_EOL;
|
|
|
|
|
|
echo PHP_EOL . '🖥 URL: http://' . $conf['core']['hostname'] . $conf['core']['path'];
|
|
|
|
|
|
echo PHP_EOL . '🔑 Admin username: ' . $username;
|
|
|
|
|
|
echo PHP_EOL . '🔑 Admin email: ' . $email;
|
|
|
|
|
|
echo PHP_EOL . '🔑 Admin password: ' . $password;
|
|
|
|
|
|
echo PHP_EOL;
|
2021-12-20 17:12:20 +01:00
|
|
|
|
exit(0);
|