Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

23.5 KB
Raw Blame
<?php
require_once 'includes/connect.php';
require_once 'includes/checkuser.php';
ob_start();
require_once 'includes/run_migrations.php';
ob_end_clean();

require_once 'includes/i18n/languages.php';
require_once 'includes/i18n/getlang.php';
require_once 'includes/i18n/' . $lang . '.php';

require_once 'includes/version.php';
require_once 'includes/theme_helpers.php';
require_once 'includes/default_names.php';

function validate($value)
{
    $value = trim($value);
    $value = stripslashes($value);
    $value = htmlspecialchars($value);
    $value = htmlentities($value);
    return $value;
}

// If logo folder doesn't exist, create it
if (!file_exists('images/uploads/logos')) {
    mkdir('images/uploads/logos', 0777, true);
    mkdir('images/uploads/logos/avatars', 0777, true);
}

// If there's already a user on the database, redirect to login page if registrations are closed or maxn users is reached
$stmt = $db->prepare('SELECT COUNT(*) as userCount FROM user');
$result = $stmt->execute();
$userCountResult = $result->fetchArray(SQLITE3_ASSOC);
$userCount = $userCountResult['userCount'];

if ($userCount == 0) {
    $setupTokenFile = __DIR__ . '/db/setup_token.db';
    if (!file_exists($setupTokenFile)) {
        $setupToken = bin2hex(random_bytes(32));
        file_put_contents($setupTokenFile, $setupToken);
        error_log("Setup token for database restore: " . $setupToken);
    }
}

if ($userCount > 0) {
    $stmt = $db->prepare('SELECT * FROM admin');
    $result = $stmt->execute();
    $settings = $result->fetchArray(SQLITE3_ASSOC);

    if ($settings['registrations_open'] == 0) {
        header("Location: login.php");
        exit();
    }

    if ($settings['max_users'] != 0) {

        if ($userCount >= $settings['max_users']) {
            header("Location: login.php");
            exit();
        }
    }
}


$theme = "light";
$updateThemeSettings = false;
if (isset($_COOKIE['theme'])) {
    $theme = sanitize_theme_mode($_COOKIE['theme']);
} else {
    $updateThemeSettings = true;
}

$colorTheme = "blue";
if (isset($_COOKIE['colorTheme'])) {
    $colorTheme = sanitize_color_theme($_COOKIE['colorTheme']);
}

$currencies = [
    ['id' => 1, 'name' => 'Euro', 'symbol' => '€', 'code' => 'EUR'],
    ['id' => 2, 'name' => 'US Dollar', 'symbol' => '$', 'code' => 'USD'],
    ['id' => 3, 'name' => 'Japanese Yen', 'symbol' => '¥', 'code' => 'JPY'],
    ['id' => 4, 'name' => 'Bulgarian Lev', 'symbol' => 'лв', 'code' => 'BGN'],
    ['id' => 5, 'name' => 'Czech Republic Koruna', 'symbol' => 'Kč', 'code' => 'CZK'],
    ['id' => 6, 'name' => 'Danish Krone', 'symbol' => 'kr', 'code' => 'DKK'],
    ['id' => 7, 'name' => 'British Pound Sterling', 'symbol' => '£', 'code' => 'GBP'],
    ['id' => 8, 'name' => 'Hungarian Forint', 'symbol' => 'Ft', 'code' => 'HUF'],
    ['id' => 9, 'name' => 'Polish Zloty', 'symbol' => 'zł', 'code' => 'PLN'],
    ['id' => 10, 'name' => 'Romanian Leu', 'symbol' => 'lei', 'code' => 'RON'],
    ['id' => 11, 'name' => 'Swedish Krona', 'symbol' => 'kr', 'code' => 'SEK'],
    ['id' => 12, 'name' => 'Swiss Franc', 'symbol' => 'Fr', 'code' => 'CHF'],
    ['id' => 13, 'name' => 'Icelandic Króna', 'symbol' => 'kr', 'code' => 'ISK'],
    ['id' => 14, 'name' => 'Norwegian Krone', 'symbol' => 'kr', 'code' => 'NOK'],
    ['id' => 15, 'name' => 'Russian Ruble', 'symbol' => '₽', 'code' => 'RUB'],
    ['id' => 16, 'name' => 'Turkish Lira', 'symbol' => '₺', 'code' => 'TRY'],
    ['id' => 17, 'name' => 'Australian Dollar', 'symbol' => '$', 'code' => 'AUD'],
    ['id' => 18, 'name' => 'Brazilian Real', 'symbol' => 'R$', 'code' => 'BRL'],
    ['id' => 19, 'name' => 'Canadian Dollar', 'symbol' => '$', 'code' => 'CAD'],
    ['id' => 20, 'name' => 'Chinese Yuan', 'symbol' => '¥', 'code' => 'CNY'],
    ['id' => 21, 'name' => 'Hong Kong Dollar', 'symbol' => 'HK$', 'code' => 'HKD'],
    ['id' => 22, 'name' => 'Indonesian Rupiah', 'symbol' => 'Rp', 'code' => 'IDR'],
    ['id' => 23, 'name' => 'Israeli New Sheqel', 'symbol' => '₪', 'code' => 'ILS'],
    ['id' => 24, 'name' => 'Indian Rupee', 'symbol' => '₹', 'code' => 'INR'],
    ['id' => 25, 'name' => 'South Korean Won', 'symbol' => '₩', 'code' => 'KRW'],
    ['id' => 26, 'name' => 'Mexican Peso', 'symbol' => 'Mex$', 'code' => 'MXN'],
    ['id' => 27, 'name' => 'Malaysian Ringgit', 'symbol' => 'RM', 'code' => 'MYR'],
    ['id' => 28, 'name' => 'New Zealand Dollar', 'symbol' => 'NZ$', 'code' => 'NZD'],
    ['id' => 29, 'name' => 'Philippine Peso', 'symbol' => '₱', 'code' => 'PHP'],
    ['id' => 30, 'name' => 'Singapore Dollar', 'symbol' => 'S$', 'code' => 'SGD'],
    ['id' => 31, 'name' => 'Thai Baht', 'symbol' => '฿', 'code' => 'THB'],
    ['id' => 32, 'name' => 'South African Rand', 'symbol' => 'R', 'code' => 'ZAR'],
    ['id' => 33, 'name' => 'Ukrainian Hryvnia', 'symbol' => '₴', 'code' => 'UAH'],
    ['id' => 34, 'name' => 'New Taiwan Dollar', 'symbol' => 'NT$', 'code' => 'TWD'],
];

$passwordMismatch = false;
$usernameExists = false;
$emailExists = false;
$registrationFailed = false;
$hasErrors = false;
if (isset($_POST['username'])) {
    $username = validate($_POST['username']);
    $firstname = validate($_POST['firstname']);
    $lastname = validate($_POST['lastname']);
    $email = validate($_POST['email']);
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];
    $main_currency = $_POST['main_currency'];
    $main_currency_index = array_search($main_currency, array_column($currencies, 'code'));
    $main_currency_id = $currencies[$main_currency_index]['id'];
    $language = $_POST['language'];
    $avatar = "images/avatars/0.svg";

    if ($password != $confirm_password) {
        $passwordMismatch = true;
        $hasErrors = true;
    }

    $emailQuery = "SELECT * FROM user WHERE email = :email";
    $stmtEmail = $db->prepare($emailQuery);
    $stmtEmail->bindValue(':email', $email, SQLITE3_TEXT);
    $resultEmail = $stmtEmail->execute();

    if ($resultEmail->fetchArray()) {
        $emailExists = true;
        $hasErrors = true;
    }

    $usernameQuery = "SELECT * FROM user WHERE username = :username";
    $stmtUsername = $db->prepare($usernameQuery);
    $stmtUsername->bindValue(':username', $username, SQLITE3_TEXT);
    $resultUsername = $stmtUsername->execute();

    if ($resultUsername->fetchArray()) {
        $usernameExists = true;
        $hasErrors = true;
    }

    $requireValidation = false;

    if ($hasErrors == false) {
        $query = "INSERT INTO user (username, firstname, lastname, email, password, main_currency, avatar, language, budget, api_key) VALUES (:username, :firstname, :lastname, :email, :password, :main_currency, :avatar, :language, :budget, :api_key)";
        $stmt = $db->prepare($query);
        $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
        $stmt->bindValue(':username', $username, SQLITE3_TEXT);
        $stmt->bindValue(':firstname', $firstname, SQLITE3_TEXT);
        $stmt->bindValue(':lastname', $lastname, SQLITE3_TEXT);
        $stmt->bindValue(':email', $email, SQLITE3_TEXT);
        $stmt->bindValue(':password', $hashedPassword, SQLITE3_TEXT);
        $stmt->bindValue(':main_currency', $main_currency_id, SQLITE3_TEXT);
        $stmt->bindValue(':avatar', $avatar, SQLITE3_TEXT);
        $stmt->bindValue(':language', $language, SQLITE3_TEXT);
        $stmt->bindValue(':budget', 0, SQLITE3_INTEGER);
        $stmt->bindValue(':api_key', bin2hex(random_bytes(32)), SQLITE3_TEXT);
        $result = $stmt->execute();

        if ($result) {

            // Get id of the newly created user
            $userId = $db->lastInsertRowID();

            // Add username as household member for that user
            $query = "INSERT INTO household (name, user_id) VALUES (:name, :user_id)";
            $stmt = $db->prepare($query);
            $stmt->bindValue(':name', $username, SQLITE3_TEXT);
            $stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
            $stmt->execute();

            if ($userId > 1) {

                // Add categories for that user
                $query = 'INSERT INTO categories (name, "order", user_id) VALUES (:name, :order, :user_id)';
                $stmt = $db->prepare($query);
                foreach (default_categories($language) as $index => $name) {
                    $stmt->bindValue(':name', $name, SQLITE3_TEXT);
                    $stmt->bindValue(':order', $index + 1, SQLITE3_INTEGER);
                    $stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
                    $stmt->execute();
                }

                // Add payment methods for that user
                $query = 'INSERT INTO payment_methods (name, icon, "order", user_id) VALUES (:name, :icon, :order, :user_id)';
                $stmt = $db->prepare($query);
                foreach (default_payment_methods($language) as $index => $payment_method) {
                    $stmt->bindValue(':name', $payment_method['name'], SQLITE3_TEXT);
                    $stmt->bindValue(':icon', $payment_method['icon'], SQLITE3_TEXT);
                    $stmt->bindValue(':order', $index + 1, SQLITE3_INTEGER);
                    $stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
                    $stmt->execute();
                }

                // Add currencies for that user
                $query = "INSERT INTO currencies (name, symbol, code, rate, user_id) VALUES (:name, :symbol, :code, :rate, :user_id)";
                $stmt = $db->prepare($query);
                foreach ($currencies as $currency) {
                    $stmt->bindValue(':name', $currency['name'], SQLITE3_TEXT);
                    $stmt->bindValue(':symbol', $currency['symbol'], SQLITE3_TEXT);
                    $stmt->bindValue(':code', $currency['code'], SQLITE3_TEXT);
                    $stmt->bindValue(':rate', 1, SQLITE3_FLOAT);
                    $stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
                    $stmt->execute();
                }

                // Retrieve main currency id
                $query = "SELECT id FROM currencies WHERE code = :code AND user_id = :user_id";
                $stmt = $db->prepare($query);
                $stmt->bindValue(':code', $main_currency, SQLITE3_TEXT);
                $stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
                $result = $stmt->execute();
                $currency = $result->fetchArray(SQLITE3_ASSOC);

                // Update user main currency
                $query = "UPDATE user SET main_currency = :main_currency WHERE id = :user_id";
                $stmt = $db->prepare($query);
                $stmt->bindValue(':main_currency', $currency['id'], SQLITE3_INTEGER);
                $stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
                $stmt->execute();

                // Add settings for that user
                $query = "INSERT INTO settings (dark_theme, monthly_price, convert_currency, remove_background, color_theme, hide_disabled, user_id, disabled_to_bottom, show_original_price, mobile_nav, week_starts_sunday) 
                          VALUES (2, 0, 0, 0, 'blue', 0, :user_id, 0, 0, 0, 0)";
                $stmt = $db->prepare($query);
                $stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
                $stmt->execute();

                // If email verification is required add the user to the email_verification table
                $query = "SELECT * FROM admin";
                $stmt = $db->prepare($query);
                $result = $stmt->execute();
                $settings = $result->fetchArray(SQLITE3_ASSOC);

                if ($settings['require_email_verification'] == 1) {
                    $query = "INSERT INTO email_verification (user_id, email, token, email_sent) VALUES (:user_id, :email, :token, 0)";
                    $stmt = $db->prepare($query);
                    $token = bin2hex(random_bytes(32));
                    $stmt->bindValue(':user_id', $userId, SQLITE3_INTEGER);
                    $stmt->bindValue(':token', $token, SQLITE3_TEXT);
                    $stmt->bindValue(':email', $email, SQLITE3_TEXT);
                    $stmt->execute();

                    $requireValidation = true;
                }
            } else {
                // The first account of an installation adopts the rows
                // createdatabase.php seeded before anybody had stated a
                // language, which is why it is not seeded above. This is the
                // first moment a language is known, so the rows that are
                // still the English default are renamed into it.
                localize_default_names($db, $userId, $language);
            }

            $db->close();
            header("Location: login.php?registered=true&requireValidation=$requireValidation");
            exit();
        } else {
            $registrationFailed = true;
        }
    }
}
?>
<!DOCTYPE html>
<html dir="<?= $languages[$lang]['dir'] ?>">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="theme-color" content="<?= $theme == "light" ? "#FFFFFF" : "#12151C" ?>" id="theme-color" />
    <meta name="apple-mobile-web-app-title" content="Wallos">
    <title>Wallos - Subscription Tracker</title>
    <link rel="icon" type="image/png" href="images/icon/favicon.ico" sizes="16x16">
    <link rel="apple-touch-icon" href="images/icon/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="152x152" href="images/icon/apple-touch-icon-152.png">
    <link rel="apple-touch-icon" sizes="180x180" href="images/icon/apple-touch-icon-180.png">
    <link rel="manifest" href="manifest.php">
    <link rel="stylesheet" href="styles/theme.css?<?= $version ?>">
    <link rel="stylesheet" href="styles/login.css?<?= $version ?>">
    <link rel="stylesheet" href="styles/themes/red.css?<?= $version ?>" id="red-theme" <?= $colorTheme != "red" ? "disabled" : "" ?>>
    <link rel="stylesheet" href="styles/themes/green.css?<?= $version ?>" id="green-theme" <?= $colorTheme != "green" ? "disabled" : "" ?>>
    <link rel="stylesheet" href="styles/themes/yellow.css?<?= $version ?>" id="yellow-theme" <?= $colorTheme != "yellow" ? "disabled" : "" ?>>
    <link rel="stylesheet" href="styles/themes/purple.css?<?= $version ?>" id="purple-theme" <?= $colorTheme != "purple" ? "disabled" : "" ?>>
    <link rel="stylesheet" href="styles/login-dark-theme.css?<?= $version ?>" id="dark-theme" <?= $theme == "light" ? "disabled" : "" ?>>
    <link rel="stylesheet" href="styles/font-awesome.min.css">
    <link rel="stylesheet" href="styles/barlow.css">
    <script type="text/javascript">
        window.update_theme_settings = "<?= $updateThemeSettings ?>";
        window.colorTheme = <?= json_encode($colorTheme, JSON_HEX_TAG | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_HEX_APOS) ?>;
    </script>
    <script type="text/javascript" src="scripts/registration.js?<?= $version ?>"></script>
    <script type="text/javascript" src="scripts/auth-theme.js?<?= $version ?>"></script>
    <script type="text/javascript" src="scripts/password-toggle.js?<?= $version ?>"></script>
</head>

<body class="<?= $languages[$lang]['dir'] ?>">
    <button type="button" class="theme-toggle" id="theme-toggle" title="<?= translate('theme', $i18n) ?>"
        aria-label="<?= translate('theme', $i18n) ?>">
        <i class="fa-solid <?= $theme == "dark" ? "fa-sun" : "fa-moon" ?>"></i>
    </button>
    <div class="content auth-split">
        <aside class="auth-brand" aria-hidden="true">
            <div class="auth-brand-logo">
                <?php include "images/siteicons/svg/logo.php"; ?>
            </div>
            <div class="auth-brand-text">
                <h1><?= translate('auth_tagline', $i18n) ?></h1>
                <p><?= translate('auth_tagline_sub', $i18n) ?></p>
            </div>
            <div class="auth-brand-footer">Wallos &mdash; Subscription Tracker</div>
        </aside>
        <section class="container wide">
            <header>
                <div class="logo-image" title="Wallos - Subscription Tracker">
                    <?php include "images/siteicons/svg/logo.php"; ?>
                </div>
                <p>
                    <?= translate('create_account', $i18n) ?>
                </p>
            </header>
            <form action="registration.php" method="post" class="registration-form">
                <div class="form-group">
                    <label for="username"><?= translate('username', $i18n) ?>:</label>
                    <input type="text" id="username" name="username" autocomplete="username" required>
                </div>
                <div class="form-group">
                    <label for="firstname"><?= translate('firstname', $i18n) ?>:</label>
                    <input type="text" id="firstname" name="firstname" autocomplete="given-name">
                </div>
                <div class="form-group">
                    <label for="lastname"><?= translate('lastname', $i18n) ?>:</label>
                    <input type="text" id="lastname" name="lastname" autocomplete="family-name">
                </div>
                <div class="form-group">
                    <label for="email"><?= translate('email', $i18n) ?>:</label>
                    <input type="email" id="email" name="email" autocomplete="email" required>
                </div>
                <div class="form-group">
                    <label for="password"><?= translate('password', $i18n) ?>:</label>
                    <input type="password" id="password" name="password" autocomplete="new-password" required>
                </div>
                <div class="form-group">
                    <label for="confirm_password"><?= translate('confirm_password', $i18n) ?>:</label>
                    <input type="password" id="confirm_password" name="confirm_password" autocomplete="new-password" required>
                </div>
                <div class="form-group">
                    <label for="currency"><?= translate('main_currency', $i18n) ?>:</label>
                    <select id="currency" name="main_currency" placeholder="Currency">
                        <?php
                        foreach ($currencies as $currency) {
                            ?>
                            <option value="<?= $currency['code'] ?>"><?= $currency['name'] ?></option>
                            <?php
                        }
                        ?>
                    </select>
                </div>
                <div class="form-group">
                    <label for="language"><?= translate('language', $i18n) ?>:</label>
                    <select id="language" name="language" placeholder="Language" onchange="changeLanguage(this.value)">
                        <?php
                        foreach ($languages as $code => $language) {
                            $selected = ($code === $lang) ? 'selected' : '';
                            ?>
                            <option value="<?= $code ?>" <?= $selected ?>><?= $language['name'] ?></option>
                            <?php
                        }
                        ?>
                    </select>
                </div>

                <?php
                if ($hasErrors) {
                    ?>
                    <ul class="error-box">
                        <?php
                        if ($passwordMismatch) {
                            ?>
                            <li><i class="fa-solid fa-triangle-exclamation"></i><?= translate('passwords_dont_match', $i18n) ?>
                            </li>
                            <?php
                        }
                        ?>
                        <?php
                        if ($usernameExists) {
                            ?>
                            <li><i class="fa-solid fa-triangle-exclamation"></i><?= translate('username_exists', $i18n) ?></li>
                            <?php
                        }
                        ?>
                        <?php
                        if ($emailExists) {
                            ?>
                            <li><i class="fa-solid fa-triangle-exclamation"></i><?= translate('email_exists', $i18n) ?></li>
                            <?php
                        }
                        ?>
                        <?php
                        if ($registrationFailed) {
                            ?>
                            <li><i class="fa-solid fa-triangle-exclamation"></i><?= translate('registration_failed', $i18n) ?>
                            </li>
                            <?php
                        }
                        ?>
                    </ul>
                    <?php
                }
                ?>


                <div class="form-group">
                    <input type="submit" value="<?= translate('register', $i18n) ?>">
                </div>
            </form>
            <?php
            if ($userCount == 0) {
                ?>
                <div class="separator">
                    <input type="button" class="secondary-button" value="<?= translate('restore_database', $i18n) ?>"
                        onClick="openRestoreModal()" />
                </div>
                <?php
            } else {
                ?>
                <div class="login-form-link account-switch">
                    <span><?= translate('already_have_account', $i18n) ?></span>
                    <a href="login.php"><?= translate('login', $i18n) ?></a>
                </div>
                <?php
            }
            ?>
        </section>
    </div>
    <?php if ($userCount == 0) { ?>
    <div id="restoreModalBackdrop" class="modal-backdrop" onclick="closeRestoreModal()">
        <div id="restoreModal" class="subscription-modal" onclick="event.stopPropagation()">
            <div class="modal-header">
                <h3><?= translate('restore_database', $i18n) ?></h3>
                <span class="fa-solid fa-xmark close-modal" onclick="closeRestoreModal()"></span>
            </div>
            <div class="modal-body">
                <p><?= translate('restore_database_info', $i18n) ?></p>
                <ul>
                    <li><?= translate('setup_token_docker', $i18n) ?></li>
                    <li><?= translate('setup_token_file', $i18n) ?></li>
                </ul>
                <div class="form-group">
                    <input type="text" id="setupToken" placeholder="<?= translate('setup_token', $i18n) ?>" autocomplete="off">
                </div>
                <div class="form-group">
                    <input type="button" class="secondary-button" value="<?= translate('select_backup_file', $i18n) ?>"
                        onClick="openRestoreDBFileSelect()" />
                    <input type="file" name="restoreDBFile" id="restoreDBFile" style="display: none;" onChange="onRestoreFileSelected()" accept=".zip">
                    <span id="restoreFileName" style="font-size: 14px; margin-left: 8px;"></span>
                </div>
                <div class="form-group">
                    <input type="button" value="<?= translate('restore_database', $i18n) ?>" onClick="restoreDB()" />
                </div>
            </div>
        </div>
    </div>
    <?php } ?>
    <?php
    require_once 'includes/footer.php';
    ?>
</body>

</html>