-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.php
More file actions
executable file
·237 lines (204 loc) · 9.37 KB
/
init.php
File metadata and controls
executable file
·237 lines (204 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace PHPizza;
use PHPizza\Rendering\ErrorScreen;
global $isInstaller;
global $settingsDB;
global $dbServer, $dbUser, $dbPassword, $dbName, $dbType;
// Initialize $isInstaller to false by default
$isInstaller = false;
# Load dependencies using composer autoloading
if (file_exists("vendor/autoload.php")) {
@include "vendor/autoload.php";
} elseif (file_exists("../vendor/autoload.php")) {
@include "../vendor/autoload.php";
} else {
error_log("Composer autoload script not found!");
// First, sync git submodules
@system("git submodule update --recursive");
// This is needed for the first run, as the submodules are not initialized by Composer.
// It is also needed for git-clone-and-run.
// Do NOT run composer automatically during web requests — it can block Apache/PHP.
// Only attempt to run composer when invoked from CLI (developer convenience).
if (php_sapi_name() === 'cli') {
@system("composer install");
if (file_exists("vendor/autoload.php")) {
include 'vendor/autoload.php';
} else {
die('Missing dependencies: composer install did not produce vendor/autoload.php. Please run "composer install" and try again.');
}
} else {
// Running under webserver — fail fast with an actionable message instead of blocking the request
http_response_code(500);
die('Missing dependencies. Please run "composer install" in the project root (web server cannot run composer automatically).');
}
}
if (!dir(__DIR__ . '/node_modules')) {
@system("npm i");
}
include 'includes/SpecialPages/specialPageClassMap.php';
$embedTypeClassMapping = [
"youtube" => PizzadownEmbedHandlerYouTube::class,
"mastodon" => PizzadownEmbedHandlerMastodon::class,
'NoradSantaTracker' => PizzadownEmbedHandlerNoradSantaTracker::class,
"facebook" => PizzadownEmbedHandlerFacebook::class,
"webpage" => PizzadownEmbedHandlerWebPage::class,
];
function _load_config(){
global $isInstaller, $dbServer, $dbUser, $dbPassword, $dbName, $dbType, $sitename, $siteLanguage, $useSkin, $skinName, $guestUsername, $guestPasswordB64, $siteLogoPath;
// Check if config.php exists FIRST - if not, we're in installer mode
// This must be checked before including default-config.php to prevent MariaDB connection attempts
$configExists = file_exists(__DIR__ . '/config.php');
// Insert config defaults (this sets $dbType = "mariadb" which we'll override if in installer mode)
@include __DIR__ . '/default-config.php';
// If config.php doesn't exist, we're in installer mode - override defaults immediately
if (!$configExists) {
$isInstaller = true;
// Immediately override database settings to SQLite to prevent any MariaDB connection attempts
$dbServer = "localhost";
$dbUser = "phpizza";
$dbPassword = "";
$dbName = "includes/Installer/phpizza_installer.sqlite3";
$dbType = "sqlite";
} else {
// Config file exists, try to load it
try {
@include __DIR__ . '/config.php';
// After loading config.php, check if database variables are set
// If they're not set or invalid, we might still need installer mode
if (!isset($dbType) || empty(trim($dbType ?? ''))) {
// Database type not set, might need installer
// But don't force installer mode if config exists - let it try to use the config
}
} catch (\Exception $e) {
$isInstaller = true;
}
}
// Only load additional configs if we're not in installer mode
if (!$isInstaller) {
// Load site-specific configuration file if it exists
global $siteDomain;
$siteDomain = preg_replace("/^www\./","",$_SERVER["HTTP_HOST"] ?? "phpizza.localhost");
global $isApi;
$isApi=false;
if (preg_match('/^api\./', $siteDomain, $matches)) {
$siteDomain=ltrim($siteDomain, "api.");
$isApi=true;
}
@include __DIR__ . "/config.$siteDomain.php";
// Reduce error verbosity for web requests to avoid exposing deprecation notices to visitors
if (php_sapi_name() !== 'cli') {
error_reporting(E_ALL & ~E_DEPRECATED & ~E_NOTICE);
ini_set('display_errors', '0');
}
// Load everything for the config
$configdir=dir(__DIR__ . "/config.d");
while (($file = $configdir->read()) !== false) {
try {
@include $file;
} catch (\Exception $e) {
error_log('file not found in config.d');
}
}
$configdir->close();
}
if ($isInstaller) {
include 'includes/Installer/installerEnvConfig.php';
}
// CRITICAL: After all config files are loaded, force installer mode to use SQLite
// This must be done INSIDE the function so it has access to the global variables
// and AFTER all config.d/ files are loaded to override any values they might set
if (isset($isInstaller) && $isInstaller) {
// Always use SQLite for installer mode, overriding any values from default-config.php or config.d/
$dbServer = "localhost";
$dbUser = "phpizza";
$dbPassword = "";
$dbName = "includes/Installer/phpizza_installer.sqlite3";
$dbType = "sqlite";
}
}
_load_config();
// Ensure we're working with global variables
global $dbServer, $dbUser, $dbPassword, $dbName, $dbType, $isInstaller;
// Final override: If in installer mode, ensure database variables are set to installer defaults
// This is a safety net in case something overrode them after _load_config()
if (isset($isInstaller) && $isInstaller) {
// Always use SQLite for installer mode, regardless of what was set
$dbServer = "localhost";
$dbUser = "phpizza";
$dbPassword = "";
$dbName = "includes/Installer/phpizza_installer.sqlite3";
$dbType = "sqlite";
// Force update global scope to ensure all code sees the correct values
$GLOBALS['dbServer'] = $dbServer;
$GLOBALS['dbUser'] = $dbUser;
$GLOBALS['dbPassword'] = $dbPassword;
$GLOBALS['dbName'] = $dbName;
$GLOBALS['dbType'] = $dbType;
}
// If $dbPassword wasn't provided in config.php, try loading passwd.b64
if (empty($dbPassword ?? null)) {
$candidates = [
__DIR__ . '/passwd.b64',
__DIR__ . '/../passwd.b64',
getcwd() . '/passwd.b64',
];
foreach ($candidates as $file) {
if (file_exists($file)) {
$raw = file_get_contents($file);
if ($raw !== false) {
$decoded = base64_decode(trim($raw));
if ($decoded !== false) {
$dbPassword = $decoded;
break;
}
}
}
}
}
// If still not set and we're in installer mode with SQLite, set empty password (SQLite doesn't use passwords)
if (!isset($dbPassword) && isset($isInstaller) && $isInstaller && isset($dbType) && strtolower(trim($dbType)) === 'sqlite') {
$dbPassword = '';
}
// Only load settings from database if all required database variables are defined and valid
// Check that all variables are set, are strings, and are non-empty (password can be empty string for SQLite)
// isset() returns false for NULL, so we don't need to check !== null separately
// IMPORTANT: Skip database loading in installer mode - the installer will set up the database
$dbVarsValid = (
!(isset($isInstaller) && $isInstaller) && // Don't load database config in installer mode
isset($dbServer) && is_string($dbServer) && trim($dbServer) !== '' &&
isset($dbUser) && is_string($dbUser) && trim($dbUser) !== '' &&
isset($dbPassword) && is_string($dbPassword) && // Password can be empty string, so just check it's a string
isset($dbName) && is_string($dbName) && trim($dbName) !== '' &&
isset($dbType) && is_string($dbType) && trim($dbType) !== ''
);
if ($dbVarsValid) {
// Double-check variables are still valid right before creating Database (defensive programming)
// Re-fetch from global scope to ensure we have the latest values
global $dbServer, $dbUser, $dbPassword, $dbName, $dbType, $isInstaller;
// CRITICAL: If in installer mode, force SQLite regardless of what $dbType says
if (isset($isInstaller) && $isInstaller) {
$dbType = "sqlite";
$dbServer = "localhost";
$dbUser = "phpizza";
$dbPassword = "";
$dbName = "includes/Installer/phpizza_installer.sqlite3";
}
if (isset($dbType) && is_string($dbType) && trim($dbType) !== '') {
// Load settings from the site_settings table
try {
$settingsdb=new ConfigurationDatabase($dbServer, $dbUser, $dbPassword, $dbName, $dbType);
$settingsdb->load_config();
} catch (\Exception $e) {
// If database connection fails, log error but don't crash
error_log("Failed to load configuration from database: " . $e->getMessage());
}
} else {
error_log("Skipping database configuration load: dbType is invalid (" . var_export($dbType, true) . ")");
}
}
// Optional debug logging (only emit when $debug is enabled)
if (!empty($debug)) {
error_log("PHPizza init: dbServer=" . ($dbServer ?? 'undefined') . ", dbUser=" . ($dbUser ?? 'undefined') . ", dbName=" . ($dbName ?? 'undefined'));
}
// Activate extensions
runExtensions(); // global function