Browse Source

Make settings.php an array, not a bunch of defines

master
Skylar Ittner 8 months ago
parent
commit
c179ed7ebb
6 changed files with 65 additions and 72 deletions
  1. 5
    5
      app.php
  2. 8
    8
      index.php
  3. 4
    2
      lib/AccountHubApi.lib.php
  4. 6
    2
      lib/User.lib.php
  5. 11
    11
      required.php
  6. 31
    44
      settings.template.php

+ 5
- 5
app.php View File

@@ -39,7 +39,7 @@ header("Link: <static/js/bootstrap.bundle.min.js>; rel=preload; as=script", fals
39 39
         <meta http-equiv="X-UA-Compatible" content="IE=edge">
40 40
         <meta name="viewport" content="width=device-width, initial-scale=1">
41 41
 
42
-        <title><?php echo SITE_TITLE; ?></title>
42
+        <title><?php echo $SETTINGS['site_title']; ?></title>
43 43
 
44 44
         <link rel="icon" href="static/img/logo.svg">
45 45
 
@@ -127,7 +127,7 @@ END;
127 127
             </button>
128 128
             <a class="navbar-brand py-0 mr-auto" href="app.php">
129 129
                 <img src="static/img/logo.svg" alt="" class="d-none d-<?php echo $navbar_breakpoint; ?>-inline brand-img py-0" />
130
-                <?php echo SITE_TITLE; ?>
130
+                <?php echo $SETTINGS['site_title']; ?>
131 131
             </a>
132 132
 
133 133
             <div class="collapse navbar-collapse py-0" id="navbar-collapse">
@@ -163,7 +163,7 @@ END;
163 163
                 </div>
164 164
                 <div class="navbar-nav ml-auto py-0" id="navbar-right">
165 165
                     <span class="nav-item py-<?php echo $navbar_breakpoint; ?>-0">
166
-                        <a class="nav-link py-<?php echo $navbar_breakpoint; ?>-0" href="<?php echo PORTAL_URL; ?>">
166
+                        <a class="nav-link py-<?php echo $navbar_breakpoint; ?>-0" href="<?php echo $SETTINGS['accounthub']['home']; ?>">
167 167
                             <i class="fas fa-user fa-fw"></i><span>&nbsp;<?php echo $_SESSION['realname'] ?></span>
168 168
                         </a>
169 169
                     </span>
@@ -183,8 +183,8 @@ END;
183 183
                 ?>
184 184
             </div>
185 185
             <div class="footer">
186
-                <?php echo FOOTER_TEXT; ?><br />
187
-                Copyright &copy; <?php echo date('Y'); ?> <?php echo COPYRIGHT_NAME; ?>
186
+                <?php echo $SETTINGS['footer_text']; ?><br />
187
+                Copyright &copy; <?php echo date('Y'); ?> <?php echo $SETTINGS['copyright']; ?>
188 188
             </div>
189 189
         </div>
190 190
         <script src="static/js/jquery-3.3.1.min.js"></script>

+ 8
- 8
index.php View File

@@ -21,7 +21,7 @@ if (Login::checkLoginServer()) {
21 21
     if (empty($VARS['progress'])) {
22 22
         // Easy way to remove "undefined" warnings.
23 23
     } else if ($VARS['progress'] == "1") {
24
-        if (!CAPTCHA_ENABLED || (CAPTCHA_ENABLED && Login::verifyCaptcha($VARS['captcheck_session_code'], $VARS['captcheck_selected_answer'], CAPTCHA_SERVER . "/api.php"))) {
24
+        if (!$SETTINGS['captcha']['enabled'] || ($SETTINGS['captcha']['enabled'] && Login::verifyCaptcha($VARS['captcheck_session_code'], $VARS['captcheck_selected_answer'], $SETTINGS['captcha']['server'] . "/api.php"))) {
25 25
             $autherror = "";
26 26
             $user = User::byUsername($VARS['username']);
27 27
             if ($user->exists()) {
@@ -41,7 +41,7 @@ if (Login::checkLoginServer()) {
41 41
                         break;
42 42
                     case "ALERT_ON_ACCESS":
43 43
                         $mail_resp = $user->sendAlertEmail();
44
-                        if (DEBUG) {
44
+                        if ($SETTINGS['debug']) {
45 45
                             var_dump($mail_resp);
46 46
                         }
47 47
                         $username_ok = true;
@@ -105,15 +105,15 @@ header("Link: <static/js/bootstrap.bundle.min.js>; rel=preload; as=script", fals
105 105
         <meta http-equiv="X-UA-Compatible" content="IE=edge">
106 106
         <meta name="viewport" content="width=device-width, initial-scale=1">
107 107
 
108
-        <title><?php echo SITE_TITLE; ?></title>
108
+        <title><?php echo $SETTINGS['site_title']; ?></title>
109 109
 
110 110
         <link rel="icon" href="static/img/logo.svg">
111 111
 
112 112
         <link href="static/css/bootstrap.min.css" rel="stylesheet">
113 113
         <link href="static/css/material-color/material-color.min.css" rel="stylesheet">
114 114
         <link href="static/css/index.css" rel="stylesheet">
115
-        <?php if (CAPTCHA_ENABLED) { ?>
116
-            <script src="<?php echo CAPTCHA_SERVER ?>/captcheck.dist.js"></script>
115
+        <?php if ($SETTINGS['captcha']['enabled']) { ?>
116
+            <script src="<?php echo $SETTINGS['captcha']['server'] ?>/captcheck.dist.js"></script>
117 117
         <?php } ?>
118 118
     </head>
119 119
     <body>
@@ -140,7 +140,7 @@ header("Link: <static/js/bootstrap.bundle.min.js>; rel=preload; as=script", fals
140 140
                             ?>
141 141
                             <input type="text" class="form-control" name="username" placeholder="<?php $Strings->get("username"); ?>" required="required" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" autofocus /><br />
142 142
                             <input type="password" class="form-control" name="password" placeholder="<?php $Strings->get("password"); ?>" required="required" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" /><br />
143
-                            <?php if (CAPTCHA_ENABLED) { ?>
143
+                            <?php if ($SETTINGS['captcha']['enabled']) { ?>
144 144
                                 <div class="captcheck_container" data-stylenonce="<?php echo $SECURE_NONCE; ?>"></div>
145 145
                                 <br />
146 146
                             <?php } ?>
@@ -165,8 +165,8 @@ header("Link: <static/js/bootstrap.bundle.min.js>; rel=preload; as=script", fals
165 165
             </div>
166 166
         </div>
167 167
         <div class="footer">
168
-            <?php echo FOOTER_TEXT; ?><br />
169
-            Copyright &copy; <?php echo date('Y'); ?> <?php echo COPYRIGHT_NAME; ?>
168
+            <?php echo $SETTINGS['footer_text']; ?><br />
169
+            Copyright &copy; <?php echo date('Y'); ?> <?php echo $SETTINGS['copyright']; ?>
170 170
         </div>
171 171
     </div>
172 172
     <script src="static/js/jquery-3.3.1.min.js"></script>

+ 4
- 2
lib/AccountHubApi.lib.php View File

@@ -9,9 +9,11 @@
9 9
 class AccountHubApi {
10 10
 
11 11
     public static function get(string $action, array $data = null, bool $throwex = false) {
12
+        global $SETTINGS;
13
+
12 14
         $content = [
13 15
             "action" => $action,
14
-            "key" => PORTAL_KEY
16
+            "key" => $SETTINGS['accounthub']['key']
15 17
         ];
16 18
         if (!is_null($data)) {
17 19
             $content = array_merge($content, $data);
@@ -27,7 +29,7 @@ class AccountHubApi {
27 29
         ];
28 30
 
29 31
         $context = stream_context_create($options);
30
-        $result = file_get_contents(PORTAL_API, false, $context);
32
+        $result = file_get_contents($SETTINGS['accounthub']['api'], false, $context);
31 33
         $response = json_decode($result, true);
32 34
         if ($result === false || !AccountHubApi::checkHttpRespCode($http_response_header) || json_last_error() != JSON_ERROR_NONE) {
33 35
             if ($throwex) {

+ 6
- 2
lib/User.lib.php View File

@@ -140,8 +140,12 @@ class User {
140 140
         }
141 141
     }
142 142
 
143
-    function sendAlertEmail(string $appname = SITE_TITLE) {
144
-        $resp = AccountHubApi::get("alertemail", ['username' => $this->username, 'appname' => SITE_TITLE]);
143
+    function sendAlertEmail(string $appname = null) {
144
+        global $SETTINGS;
145
+        if (is_null($appname)) {
146
+            $appname = $SETTINGS['site_title'];
147
+        }
148
+        $resp = AccountHubApi::get("alertemail", ['username' => $this->username, 'appname' => $SETTINGS['site_title']]);
145 149
 
146 150
         if ($resp['status'] == "OK") {
147 151
             return true;

+ 11
- 11
required.php View File

@@ -32,7 +32,7 @@ session_start(); // stick some cookies in it
32 32
 // renew session cookie
33 33
 setcookie(session_name(), session_id(), time() + $session_length, "/", false, false);
34 34
 
35
-$captcha_server = (CAPTCHA_ENABLED === true ? preg_replace("/http(s)?:\/\//", "", CAPTCHA_SERVER) : "");
35
+$captcha_server = ($SETTINGS['captcha']['enabled'] === true ? preg_replace("/http(s)?:\/\//", "", $SETTINGS['captcha']['server']) : "");
36 36
 if ($_SESSION['mobile'] === TRUE) {
37 37
     header("Content-Security-Policy: "
38 38
             . "default-src 'self';"
@@ -69,7 +69,7 @@ foreach ($libs as $lib) {
69 69
     require_once $lib;
70 70
 }
71 71
 
72
-$Strings = new Strings(LANGUAGE);
72
+$Strings = new Strings($SETTINGS['language']);
73 73
 
74 74
 /**
75 75
  * Kill off the running process and spit out an error message
@@ -93,7 +93,7 @@ function sendError($error) {
93 93
             . "<p>" . htmlspecialchars($error) . "</p>");
94 94
 }
95 95
 
96
-date_default_timezone_set(TIMEZONE);
96
+date_default_timezone_set($SETTINGS['timezone']);
97 97
 
98 98
 // Database settings
99 99
 // Also inits database and stuff
@@ -102,12 +102,12 @@ use Medoo\Medoo;
102 102
 $database;
103 103
 try {
104 104
     $database = new Medoo([
105
-        'database_type' => DB_TYPE,
106
-        'database_name' => DB_NAME,
107
-        'server' => DB_SERVER,
108
-        'username' => DB_USER,
109
-        'password' => DB_PASS,
110
-        'charset' => DB_CHARSET
105
+        'database_type' => $SETTINGS['database']['type'],
106
+        'database_name' => $SETTINGS['database']['name'],
107
+        'server' => $SETTINGS['database']['server'],
108
+        'username' => $SETTINGS['database']['user'],
109
+        'password' => $SETTINGS['database']['password'],
110
+        'charset' => $SETTINGS['database']['charset']
111 111
     ]);
112 112
 } catch (Exception $ex) {
113 113
     //header('HTTP/1.1 500 Internal Server Error');
@@ -115,7 +115,7 @@ try {
115 115
 }
116 116
 
117 117
 
118
-if (!DEBUG) {
118
+if (!$SETTINGS['debug']) {
119 119
     error_reporting(0);
120 120
 } else {
121 121
     error_reporting(E_ALL);
@@ -158,7 +158,7 @@ function checkDBError($specials = []) {
158 158
 
159 159
 function redirectIfNotLoggedIn() {
160 160
     if ($_SESSION['loggedin'] !== TRUE) {
161
-        header('Location: ' . URL . '/index.php');
161
+        header('Location: ' . $SETTINGS['url'] . '/index.php');
162 162
         die();
163 163
     }
164 164
 }

+ 31
- 44
settings.template.php View File

@@ -1,47 +1,34 @@
1 1
 <?php
2 2
 
3
-/* This Source Code Form is subject to the terms of the Mozilla Public
3
+/*
4
+ * This Source Code Form is subject to the terms of the Mozilla Public
4 5
  * License, v. 2.0. If a copy of the MPL was not distributed with this
5
- * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
-
7
-// Whether to show debugging data in output.
8
-// DO NOT SET TO TRUE IN PRODUCTION!!!
9
-define("DEBUG", false);
10
-
11
-// Database connection settings
12
-// See http://medoo.in/api/new for info
13
-define("DB_TYPE", "mysql");
14
-define("DB_NAME", "app");
15
-define("DB_SERVER", "localhost");
16
-define("DB_USER", "app");
17
-define("DB_PASS", "");
18
-define("DB_CHARSET", "utf8");
19
-
20
-// Name of the app.
21
-define("SITE_TITLE", "Web App Template");
22
-
23
-
24
-// URL of the AccountHub API endpoint
25
-define("PORTAL_API", "http://localhost/accounthub/api/");
26
-// URL of the AccountHub home page
27
-define("PORTAL_URL", "http://localhost/accounthub/home.php");
28
-// AccountHub API Key
29
-define("PORTAL_KEY", "123");
30
-
31
-// For supported values, see http://php.net/manual/en/timezones.php
32
-define("TIMEZONE", "America/Denver");
33
-
34
-// Base URL for site links.
35
-define('URL', '.');
36
-
37
-// Use Captcheck on login screen
38
-// https://captcheck.netsyms.com
39
-define("CAPTCHA_ENABLED", FALSE);
40
-define('CAPTCHA_SERVER', 'https://captcheck.netsyms.com');
41
-
42
-// See lang folder for language options
43
-define('LANGUAGE', "en_us");
44
-
45
-
46
-define("FOOTER_TEXT", "");
47
-define("COPYRIGHT_NAME", "Netsyms Technologies");
6
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
+ */
8
+
9
+$SETTINGS = [
10
+    "debug" => false,
11
+    "database" => [
12
+        "type" => "mysql",
13
+        "name" => "app",
14
+        "server" => "localhost",
15
+        "user" => "app",
16
+        "password" => "",
17
+        "charset" => "utf8"
18
+    ],
19
+    "site_title" => "Web App Template",
20
+    "accounthub" => [
21
+        "api" => "http://localhost/accounthub/api/",
22
+        "home" => "http://localhost/accounthub/home.php",
23
+        "key" => "123"
24
+    ],
25
+    "timezone" => "America/Denver",
26
+    "captcha" => [
27
+        "enabled" => false,
28
+        "server" => "https://captcheck.netsyms.com"
29
+    ],
30
+    "language" => "en",
31
+    "footer_text" => "",
32
+    "copyright" => "Netsyms Technologies",
33
+    "url" => "."
34
+];

Loading…
Cancel
Save