summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2025-12-09 01:43:12 +0500
committerilotterytea <iltsu@alright.party>2025-12-09 01:43:12 +0500
commit29477e67ede12347b153e2255325327374e1b073 (patch)
treebe9170a20d332eab1fcc8b380f70f231921aac45 /lib
parent28bb4f3618e2e947d14a05a24e99d826c26c0ce3 (diff)
feat: new configuration
Diffstat (limited to 'lib')
-rw-r--r--lib/accounts.php2
-rw-r--r--lib/config.php113
-rw-r--r--lib/config.sample.php74
-rw-r--r--lib/partials.php16
4 files changed, 122 insertions, 83 deletions
diff --git a/lib/accounts.php b/lib/accounts.php
index 51cb3f6..2ddb796 100644
--- a/lib/accounts.php
+++ b/lib/accounts.php
@@ -28,7 +28,7 @@ function authorize_user(bool $required = false): bool
include_once "config.php";
- $db = new PDO(DB_URL, DB_USER, DB_PASS);
+ $db = new PDO(CONFIG['database']['url'], CONFIG['database']['user'], CONFIG['database']['pass']);
$key = $_SERVER["HTTP_AUTHORIZATION"] ?? $_COOKIE["secret_key"];
diff --git a/lib/config.php b/lib/config.php
new file mode 100644
index 0000000..1c6797d
--- /dev/null
+++ b/lib/config.php
@@ -0,0 +1,113 @@
+<?php
+$file_path = "{$_SERVER['DOCUMENT_ROOT']}/config.json";
+define('CFG_PATH', $file_path);
+
+$cfg = [
+ 'instance' => [
+ 'name' => $_SERVER['HTTP_HOST']
+ ],
+ 'database' => [
+ 'name' => '',
+ 'user' => '',
+ 'pass' => '',
+ 'host' => 'localhost',
+ 'url' => ''
+ ],
+ 'anonymous' => [
+ 'upload' => false,
+ 'defaultname' => 'Anonymous'
+ ],
+ 'emote' => [
+ 'upload' => true,
+ 'nameregex' => "/^[A-Za-z0-9_]+$/",
+ 'defaultvisibility' => 2,
+ 'maxnamelength' => 100,
+ 'maxcommentlength' => 100,
+ 'maxsizex' => 128,
+ 'maxsizey' => 128,
+ 'storeoriginal' => true
+ ],
+ 'rating' => [
+ 'enable' => true,
+ 'names' => "-1=COAL\n1=GEM",
+ 'minvotes' => 10
+ ],
+ 'tags' => [
+ 'enable' => true,
+ 'regex' => "/^[A-Za-z0-9_]+$/",
+ 'maxcount' => 10
+ ],
+ 'emoteset' => [
+ 'public' => true
+ ],
+ 'mod' => [
+ 'dashboard' => true,
+ 'approve' => true
+ ],
+ 'reports' => [
+ 'enable' => true
+ ],
+ 'account' => [
+ 'registration' => true,
+ 'maxcookielifetime' => 86400 * 30,
+ 'regex' => "/^[A-Za-z0-9_]+$/",
+ 'minusernamelength' => 2,
+ 'maxusernamelength' => 20,
+ 'minpasswordlength' => 10,
+ 'secretkeylength' => 32,
+ 'pfpsizex' => 128,
+ 'pfpsizey' => 128,
+ 'bannersizex' => 1920,
+ 'bannersizey' => 1080,
+ 'badgesizex' => 72,
+ 'badgesizey' => 72,
+ 'publiclist' => true,
+ 'log' => true
+ ],
+ 'twitch' => [
+ 'registration' => false,
+ 'clientid' => '',
+ 'clientsecret' => '',
+ 'redirecturi' => ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http") . "://$_SERVER[HTTP_HOST]/account/login/twitch.php"
+ ],
+ 'captcha' => [
+ 'enable' => false,
+ 'x' => 580,
+ 'y' => 220,
+ 'force' => false
+ ]
+];
+
+if (file_exists(CFG_PATH)) {
+ $c = json_decode(file_get_contents(CFG_PATH), true);
+ foreach ($cfg as $sk => $sv) {
+ if (!is_array($sv) || !array_key_exists($sk, $c)) {
+ continue;
+ }
+
+ foreach ($sv as $k => $v) {
+ if (array_key_exists($k, $c[$sk])) {
+ $cfg[$sk][$k] = $c[$sk][$k];
+ }
+ }
+ }
+}
+
+if (!empty($cfg['database']['host'])) {
+ $cfg['database']['url'] = "mysql:host={$cfg['database']['host']};dbname={$cfg['database']['name']};port=3306";
+}
+
+$cfg['rating']['names_string'] = $cfg['rating']['names'];
+$n = [];
+foreach (explode("\n", $cfg['rating']['names']) as $_ => $v) {
+ [$k, $v] = explode('=', $v, 2);
+ $n[intval($k)] = $v;
+}
+$cfg['rating']['names'] = $n;
+
+define('CONFIG', $cfg);
+
+define("INSTANCE_STATIC_FOLDER", "static"); // Static folder. Used only in /404.php.
+
+// FOR DEVELOPERS
+define("CLIENT_REQUIRES_JSON", isset($_SERVER["HTTP_ACCEPT"]) && $_SERVER["HTTP_ACCEPT"] == "application/json"); \ No newline at end of file
diff --git a/lib/config.sample.php b/lib/config.sample.php
deleted file mode 100644
index 3d30044..0000000
--- a/lib/config.sample.php
+++ /dev/null
@@ -1,74 +0,0 @@
-<?php
-// INSTANCE
-define("INSTANCE_NAME", "TinyEmotes");
-define("INSTANCE_STATIC_FOLDER", "static"); // Static folder. Used only in /404.php.
-
-// DATABASE
-define("DB_USER", "ENTER_DATABASE_USER"); // Database user. MANDATORY!
-define("DB_PASS", "ENTER_DATABASE_PASSWORD"); // Database password. MANDATORY!
-define("DB_HOST", "ENTER_DATABASE_HOST"); // Database host. Can be 'localhost' if it's on the same machine as Tinyemotes.
-define("DB_NAME", "ENTER_DATABASE_NAME"); // Database name.
-define("DB_URL", 'mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';port=3306'); // Database URL. Change it if you don't use MySQL/MariaDB.
-
-// RATINGS
-define("RATING_ENABLE", true); // Enable ratings for emotes.
-define("RATING_NAMES", [
- "-1" => "COAL",
- "1" => "GEM",
-]); // Rating names. The schema is [ "id/rating_point" => "name" ].
-define("RATING_EMOTE_MIN_VOTES", 10); // Minimal amount of votes to display emote rating.
-
-// UPLOADS
-define("ANONYMOUS_UPLOAD", false); // Allow anonymous upload for emotes.
-define("ANONYMOUS_DEFAULT_NAME", "Anonymous"); // Default uploader name for anonymous emotes. It's also used when original uploader has been deleted.
-
-// EMOTES
-define("EMOTE_UPLOAD", true); // Enable emote upload.
-define("EMOTE_NAME_MAX_LENGTH", 100); // Max length for emote name.
-define("EMOTE_COMMENT_MAX_LENGTH", 100); // Max length for emote comment.
-define("EMOTE_VISIBILITY_DEFAULT", 2); // Default visibility for emotes. 0 - unlisted, 1 - public, 2 - pending approval (same as unlisted).
-define("EMOTE_MAX_SIZE", [128, 128]); // Max size of emote.
-define("EMOTE_NAME_REGEX", "/^[A-Za-z0-9_]+$/"); // RegEx filter for emote names.
-define("EMOTE_STORE_ORIGINAL", true); // Store original uploads of emotes.
-
-// TAGS
-define("TAGS_ENABLE", true); // Allow emote tagging.
-define("TAGS_CODE_REGEX", "/^[A-Za-z0-9_]+$/");
-define("TAGS_MAX_COUNT", 10); // Maximum tags per emote. Set -1 for unlimited amount.
-
-// EMOTESETS
-define("EMOTESET_PUBLIC_LIST", true); // Show emotesets public.
-
-// MODERATION
-define("MOD_SYSTEM_DASHBOARD", true); // Enable system dashboard for moderators (/system).
-define("MOD_EMOTES_APPROVE", true); // Enable manual emote approval (/system/emotes).
-
-// REPORTS
-define("REPORTS_ENABLE", true); // Enable emote, user reports.
-
-// ACCOUNTS
-define("ACCOUNT_REGISTRATION_ENABLE", true); // Enable account registration.
-define("ACCOUNT_COOKIE_MAX_LIFETIME", 86400 * 30); // Remember user for a month.
-define("ACCOUNT_USERNAME_REGEX", "/^[A-Za-z0-9_]+$/"); // RegEx filter for account usernames.
-define("ACCOUNT_USERNAME_LENGTH", [2, 20]); // [Min, Max] length for account usernames.
-define("ACCOUNT_PASSWORD_MIN_LENGTH", 10); // Minimal length for passwords.
-define("ACCOUNT_SECRET_KEY_LENGTH", 32); // The length for secret keys.
-define("ACCOUNT_PFP_MAX_SIZE", [128, 128]); // Max dimensions for account pictures.
-define("ACCOUNT_BANNER_MAX_SIZE", [1920, 1080]); // Max dimensions for account banners.
-define("ACCOUNT_BADGE_MAX_SIZE", [72, 72]); // Max dimensions for account badges.
-define("ACCOUNT_PUBLIC_LIST", true); // The public list of accounts.
-define("ACCOUNT_LOG_ACTIONS", true); // Log user's actions (emote addition, etc.).
-
-// TWITCH
-define("TWITCH_REGISTRATION_ENABLE", false); // Enable account registration via Twitch.
-define("TWITCH_CLIENT_ID", "AAAAAAAAA"); // Client ID of your Twitch application.
-define("TWITCH_SECRET_KEY", "BBBBBBBBB"); // Secret key of your Twitch application.
-define("TWITCH_REDIRECT_URI", ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? "https" : "http") . "://$_SERVER[HTTP_HOST]/account/login/twitch.php"); // Redirect URI of your Twitch application.
-
-// CAPTCHA
-define("CAPTCHA_ENABLE", true); // Enable built-in captcha.
-define("CAPTCHA_SIZE", [580, 220]); // Captcha size.
-define("CAPTCHA_FORCE_USERS", false); // Force authorized users to solve captcha.
-
-// FOR DEVELOPERS
-define("CLIENT_REQUIRES_JSON", isset($_SERVER["HTTP_ACCEPT"]) && $_SERVER["HTTP_ACCEPT"] == "application/json"); \ No newline at end of file
diff --git a/lib/partials.php b/lib/partials.php
index 760923a..979621d 100644
--- a/lib/partials.php
+++ b/lib/partials.php
@@ -7,26 +7,26 @@ function html_navigation_bar()
<section class="navbar">
<a href="/" class="brand" style="color:black;text-decoration:none;">
<img src="/static/img/brand/mini.webp" alt="">
- <h2 style="margin-left:8px;font-size:24px;"><b><?php echo INSTANCE_NAME ?></b></h2>
+ <h2 style="margin-left:8px;font-size:24px;"><b><?= CONFIG['instance']['name'] ?></b></h2>
</a>
<div class="links">
<a href="/emotes" class="button">Emotes</a>
- <?php if (EMOTESET_PUBLIC_LIST): ?>
+ <?php if (CONFIG['emoteset']['public']): ?>
<a href="/emotesets.php" class="button">Emotesets</a>
<?php endif; ?>
- <?php if (ACCOUNT_PUBLIC_LIST): ?>
+ <?php if (CONFIG['account']['publiclist']): ?>
<a href="/users.php" class="button">Users</a>
<?php endif; ?>
- <?php if (EMOTE_UPLOAD && (ANONYMOUS_UPLOAD || (isset($_SESSION["user_role"]) && $_SESSION["user_role"]["permission_upload"]))) {
+ <?php if (CONFIG['emote']['upload'] && (CONFIG['anonymous']['upload'] || (isset($_SESSION["user_role"]) && $_SESSION["user_role"]["permission_upload"]))) {
echo '<a href="/emotes/upload.php" class="button">Upload</a>';
} ?>
<a href="/account" class="button">Account</a>
<?php
if (isset($_SESSION["user_id"])) {
- $db = new PDO(DB_URL, DB_USER, DB_PASS);
+ $db = new PDO(CONFIG['database']['url'], CONFIG['database']['user'], CONFIG['database']['pass']);
// getting inbox
$stmt = $db->prepare("SELECT COUNT(*) FROM inbox_messages WHERE recipient_id = ? AND has_read = false");
@@ -40,7 +40,7 @@ function html_navigation_bar()
$stmt = null;
if (isset($_SESSION["user_role"])) {
- if (REPORTS_ENABLE && $_SESSION["user_role"]["permission_report"]) {
+ if (CONFIG['reports']['enable'] && $_SESSION["user_role"]["permission_report"]) {
// getting reports
$stmt = $db->prepare("SELECT COUNT(*) FROM reports WHERE sender_id = ? AND resolved_by IS NULL");
$stmt->execute([$_SESSION["user_id"]]);
@@ -53,10 +53,10 @@ function html_navigation_bar()
<?php ;
}
- if (MOD_SYSTEM_DASHBOARD && ($_SESSION["user_role"]["permission_approve_emotes"] || $_SESSION["user_role"]["permission_report_review"])) {
+ if (CONFIG['mod']['dashboard'] && ($_SESSION["user_role"]["permission_approve_emotes"] || $_SESSION["user_role"]["permission_report_review"])) {
$system_count = 0;
- if ($_SESSION["user_role"]["permission_approve_emotes"] && MOD_EMOTES_APPROVE) {
+ if ($_SESSION["user_role"]["permission_approve_emotes"] && CONFIG['mod']['approve']) {
$system_count += intval($db->query("SELECT COUNT(*) FROM emotes WHERE visibility = 2")->fetch()[0]);
}