/data/core/classes/Database/DB.php
protected QueryRecorder $_query_recorder;
private function __construct(
string $host,
string $database,
string $username,
string $password,
int $port,
?string $force_charset,
?string $force_collation,
string $prefix
) {
$this->_force_charset = $force_charset;
$this->_force_collation = $force_collation;
$this->_prefix = $prefix;
$connection_string = 'mysql:host=' . $host . ';port=' . $port . ';dbname=' . $database;
if ($force_charset) {
$connection_string .= ';charset=' . $force_charset;
}
$this->_pdo = new PDO(
$connection_string,
$username,
$password,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]
);
$this->_query_recorder = QueryRecorder::getInstance();
}
public static function getCustomInstance(
string $host,
string $database,
string $username,
string $password,
int $port = 3306,
?string $force_charset = null,
?string $force_collation = null,
string $prefix = 'nl2_'
/data/core/classes/Database/DB.php
$username,
$password,
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]
);
$this->_query_recorder = QueryRecorder::getInstance();
}
public static function getCustomInstance(
string $host,
string $database,
string $username,
string $password,
int $port = 3306,
?string $force_charset = null,
?string $force_collation = null,
string $prefix = 'nl2_'
): DB {
return new DB(
$host,
$database,
$username,
$password,
$port,
$force_charset,
$force_collation,
$prefix
);
}
public static function getInstance(): DB
{
if (self::$_instance) {
return self::$_instance;
}
if (Config::get('mysql.initialise_charset')) {
$force_charset = Config::get('mysql.charset') ?: 'utf8mb4';
} else {
/data/core/classes/Database/DB.php
}
public static function getInstance(): DB
{
if (self::$_instance) {
return self::$_instance;
}
if (Config::get('mysql.initialise_charset')) {
$force_charset = Config::get('mysql.charset') ?: 'utf8mb4';
} else {
$force_charset = null;
}
if (Config::get('mysql.initialise_collation')) {
$force_collation = Config::get('mysql.collation') ?: 'utf8mb4_unicode_ci';
} else {
$force_collation = null;
}
return self::$_instance = self::getCustomInstance(
Config::get('mysql.host'),
Config::get('mysql.db'),
Config::get('mysql.username'),
Config::get('mysql.password'),
Config::get('mysql.port'),
$force_charset,
$force_collation
);
}
/**
* Get the underlying PDO instance.
*
* @return PDO The PDO instance.
*/
public function getPDO(): PDO
{
return $this->_pdo;
}
/data/core/classes/Database/PhinxAdapter.php
if (!$migrationDir) {
$migrationDir = __DIR__ . '/../../migrations';
}
$migration_files = array_map(
static function ($file_name) {
[$version, $migration_name] = explode('_', $file_name, 2);
$migration_name = str_replace(['.php', '_'], '', ucwords($migration_name, '_'));
return $version . '_' . $migration_name;
},
array_filter(scandir($migrationDir), static function ($file_name) {
// Pattern that matches Phinx migration file names (eg: 20230403000000_create_stroopwafel_table.php)
return preg_match('/^\d{14}_\w+\.php$/', $file_name);
}),
);
$migration_database_entries = array_map(static function ($row) {
return $row->version . '_' . $row->migration_name;
}, DB::getInstance()->query("SELECT version, migration_name FROM $table")->results());
$missing = array_diff($migration_files, $migration_database_entries);
$extra = array_diff($migration_database_entries, $migration_files);
if ($returnResults) {
return [
'missing' => count($missing),
'extra' => count($extra),
];
}
// Likely a pull from the repo dev branch or migrations
// weren't run during an upgrade script.
if (($missing_count = count($missing)) > 0) {
echo "There are $missing_count migrations files which have not been executed:" . '<br>';
foreach ($missing as $missing_migration) {
echo " - $missing_migration" . '<br>';
}
}
/data/core/init.php
if (Config::get('core.force_www')) {
define('FORCE_WWW', true);
}
$host = HttpUtils::getHeader('Host');
// Only check force HTTPS and force www. when Host header is set
// These options don't make sense when making requests to IP addresses anyway
if ($host !== null) {
if (defined('FORCE_SSL') && HttpUtils::getProtocol() === 'http') {
if (defined('FORCE_WWW') && !str_contains($host, 'www.')) {
Redirect::to('https://www.' . $host . $_SERVER['REQUEST_URI']);
} else {
Redirect::to('https://' . $host . $_SERVER['REQUEST_URI']);
}
} elseif (defined('FORCE_WWW') && !str_contains($host, 'www.')) {
Redirect::to(HttpUtils::getProtocol() . '://www.' . $host . $_SERVER['REQUEST_URI']);
}
}
// Ensure database is up-to-date
PhinxAdapter::ensureUpToDate('Core');
// Error reporting
if (!defined('DEBUGGING')) {
if (Settings::get('error_reporting') === '1') {
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
define('DEBUGGING', 1);
} else {
// Disable by default
error_reporting(0);
ini_set('display_errors', 0);
}
}
if ((defined('DEBUGGING') && DEBUGGING) && class_exists('DebugBar\DebugBar')) {
define('PHPDEBUGBAR', true);
DebugBarHelper::getInstance()->enable();
}
/data/index.php
if (!ini_get('upload_tmp_dir')) {
$tmp_dir = sys_get_temp_dir();
} else {
$tmp_dir = ini_get('upload_tmp_dir');
}
if (HttpUtils::getProtocol() === 'https') {
ini_set('session.cookie_secure', 'On');
}
ini_set('session.cookie_httponly', 1);
ini_set('open_basedir', ROOT_PATH . PATH_SEPARATOR . $tmp_dir . PATH_SEPARATOR . '/proc/stat');
// Get the directory the user is trying to access
$directory = $_SERVER['REQUEST_URI'];
$directories = explode('/', $directory);
$lim = count($directories);
// Start initialising the page
require(ROOT_PATH . '/core/init.php');
// Get page to load from URL
if (!isset($_GET['route']) || $_GET['route'] == '/') {
if ((!isset($_GET['route']) || ($_GET['route'] != '/')) && count($directories) > 1) {
require(ROOT_PATH . '/404.php');
} else {
// Homepage
$homepage = $pages->getPageByURL(Settings::get('home_type'));
if ($homepage != null) {
$pages->setActivePage($homepage);
require(implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'modules', $homepage['module'], $homepage['file']]));
} else {
$pages->setActivePage($pages->getPageByURL('/'));
require(ROOT_PATH . '/modules/Core/pages/index.php');
}
}
die;
}
$route = rtrim(strtok($_GET['route'], '?'), '/');