Browse Source

Config update and webhook fix

Owen Diffey 4 năm trước cách đây
mục cha
commit
621d94fe05
2 tập tin đã thay đổi với 46 bổ sung40 xóa
  1. 15 9
      config.php.example
  2. 31 31
      index.php

+ 15 - 9
config.php.example

@@ -1,13 +1,19 @@
 <?php
 
-define(siteUrl, "https://example.com");
-define(mysqlHost, "127.0.0.1");
-define(mysqlUser, "");
-define(mysqlPass, "");
-define(mysqlDatabase, "");
-define(allowedUsers, array());
-define(discordClientId, );
-define(discordClientSecret, "");
-define(discordWebhook, "");
+$config = array(
+    "siteUrl" => "",
+    "allowedUsers" => array(),
+    "mysql" => array(
+        "host" => "127.0.0.1",
+        "user" => "",
+        "password" => "",
+        "database" => ""
+    ),
+    "discord" => array(
+        "clientId" => "",
+        "clientSecret" => "",
+        "webhook" => ""
+    )
+);
 
 ?>

+ 31 - 31
index.php

@@ -16,8 +16,8 @@ class db {
   protected $query_closed = TRUE;
   public $query_count = 0;
 
-  public function __construct() {
-    $this->connection = new mysqli(mysqlHost, mysqlUser, mysqlPass, mysqlDatabase);
+  public function __construct($mysqlHost, $mysqlUser, $mysqlPass, $mysqlDatabase) {
+    $this->connection = new mysqli($mysqlHost, $mysqlUser, $mysqlPass, $mysqlDatabase);
     if ($this->connection->connect_error) {
       $this->error("Failed to connect to MySQL - " . $this->connection->connect_error);
     }
@@ -191,7 +191,7 @@ function getDiscordAuthInfoFromCode($code)
         'client_secret' => discordClientSecret,
         'grant_type' => 'authorization_code',
         'code' => $code,
-        'redirect_uri' => siteUrl."/login/redirect",
+        'redirect_uri' => $config["siteUrl"]."/login/redirect",
         'scope' => 'identify%20guilds'
     );
 
@@ -274,7 +274,7 @@ function getDiscordUserGuilds($access_token)
     }
 }
 
-function sendDcMessage($webook, $message)
+function sendDcMessage($webhook, $message)
 {
     $ch = curl_init();
     curl_setopt_array($ch, [
@@ -296,7 +296,7 @@ function sendDcMessage($webook, $message)
 if ($_SERVER["REQUEST_METHOD"] == "POST") {
   if (isset($_POST["delete"])) {
     print_r($_POST["delete"]);
-    $conn = new db();
+    $conn = new db($config["mysql"]["host"], $config["mysql"]["user"], $config["mysql"]["password"], $config["mysql"]["database"]);
     if ($conn->query("DELETE FROM empyreanrealm WHERE id = ?", $conn->escapeString(cleanInput($_POST["delete"])))->affectedRows() === 1) {
       $success = true;
     } else {
@@ -337,7 +337,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
   }
 
   if (empty($errors)) {
-    $conn = new db();
+    $conn = new db($config["mysql"]["host"], $config["mysql"]["user"], $config["mysql"]["password"], $config["mysql"]["database"]);
     $nickname = $conn->escapeString($nickname);
     $discordUsername = $conn->escapeString($discordUsername);
     $username = $conn->escapeString($username);
@@ -346,42 +346,42 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
     if ($conn->query("INSERT INTO empyreanrealm (nickname, discord, username, birthdate, submittime) VALUES (?,?,?,?,?)", $nickname, $discordUsername, $username, $birthDate, $submitTime)->affectedRows() === 1) {
       $message = json_encode([
           "username" => "Bedrock Applications",
-          "avatar_url" => siteUrl."/source/logo.png",
+          "avatar_url" => $config["siteUrl"]."/source/logo.png",
           "embeds" => [
           [
               "title" => "New Bedrock Application",
               "type" => "rich",
               "description" => "$nickname has applied to the bedrock server.",
               "timestamp" => date("Y-m-d\TH:i:s"),
-              "url" => siteUrl."/applications",
+              "url" => $config["siteUrl"]."/applications",
               "color" => hexdec("55C9F6"),
               "fields" => [
-                  [
+                [
                   "name" => "Nickname",
                   "value" => "$nickname",
-                  "inline" => true
-                  ],
-                  [
-                    "name" => "Bedrock Username",
-                    "value" => "$username",
-                    "inline" => true
-                  ],
-                  [
-                    "name" => "Discord Username",
-                    "value" => "$discordUsername",
-                    "inline" => true
-                  ],
-                  [
-                    "name" => "Birth Date",
-                    "value" => "$birthDate",
-                    "inline" => true
-                  ]
+                  "inline" => false
+                ],
+                [
+                  "name" => "Bedrock Username",
+                  "value" => "$username",
+                  "inline" => false
+                ],
+                [
+                  "name" => "Discord Username",
+                  "value" => "$discordUsername",
+                  "inline" => false
+                ],
+                [
+                  "name" => "Birth Date",
+                  "value" => "$birthDate",
+                  "inline" => false
+                ]
               ]
           ]
       ]
       
       ], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
-      sendDcMessage(discordWebhook, $message);
+      sendDcMessage($config["discord"]["webhook"], $message);
       $success = true;
     } else {
       $errors[] = "Error submitting form.";
@@ -389,7 +389,7 @@ if ($_SERVER["REQUEST_METHOD"] == "POST") {
   }
 }
 
-$url = parse_url(siteUrl.$_SERVER["REQUEST_URI"]);
+$url = parse_url($config["siteUrl"].$_SERVER["REQUEST_URI"]);
 $page = $url["path"];
 $page = strtolower($page);
 $page = str_replace(" ", "-", $page);
@@ -429,11 +429,11 @@ $page = preg_replace("/\?.+/", "", $page);
       session_destroy();
       header("Location: /");
     } else if (!isset($_SESSION["loggedIn"]) && stristr($page, "/login")) {
-        $redirectUri = urlencode(siteUrl."/login/redirect");
+        $redirectUri = urlencode($config["siteUrl"]."/login/redirect");
         header("Location: ". "https://discordapp.com/api/oauth2/authorize?client_id=" . discordClientId . "&redirect_uri=" . $redirectUri . "&response_type=code&scope=identify%20guilds");
     } else if (isset($_SESSION["loggedIn"]) && stristr($page, "/login")) {
         header("Location: /applications");
-    } else if (isset($_SESSION["loggedIn"]) && in_array($_SESSION["discordId"], allowedUsers) && stristr($page, "/applications")) { ?>
+    } else if (isset($_SESSION["loggedIn"]) && in_array($_SESSION["discordId"], $config["allowedUsers"]) && stristr($page, "/applications")) { ?>
     <div>
       <h1>Bedrock Server Applications</h1>
       <div class="tableContainer">
@@ -450,7 +450,7 @@ $page = preg_replace("/\?.+/", "", $page);
           </thead>
           <tbody>
             <?php 
-            $conn = new db();
+            $conn = new db($config["mysql"]["host"], $config["mysql"]["user"], $config["mysql"]["password"], $config["mysql"]["database"]);
             $conn->query('SELECT * FROM empyreanrealm')->fetchAll(function($player) { ?>
             <tr>
               <td><?= $player["nickname"] ?></td>