Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ROOT_PATH constant to a class #3246

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 403.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

const PAGE = 403;
$page_title = '403';
require_once(ROOT_PATH . '/core/templates/frontend_init.php');
require_once(Constants::ROOT_PATH . '/core/templates/frontend_init.php');

// Load modules + template
Module::loadPage($user, $pages, $cache, $smarty, [$navigation, $cc_nav, $staffcp_nav], $widgets, $template);

$template->onPageLoad();

require(ROOT_PATH . '/core/templates/navbar.php');
require(ROOT_PATH . '/core/templates/footer.php');
require(Constants::ROOT_PATH . '/core/templates/navbar.php');
require(Constants::ROOT_PATH . '/core/templates/footer.php');

// Assign Smarty variables
$smarty->assign(
Expand Down
6 changes: 3 additions & 3 deletions 404.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

const PAGE = 404;
$page_title = '404';
require_once(ROOT_PATH . '/core/templates/frontend_init.php');
require_once(Constants::ROOT_PATH . '/core/templates/frontend_init.php');

// Load modules + template
Module::loadPage($user, $pages, $cache, $smarty, [$navigation, $cc_nav, $staffcp_nav], $widgets, $template);

$template->onPageLoad();

require(ROOT_PATH . '/core/templates/navbar.php');
require(ROOT_PATH . '/core/templates/footer.php');
require(Constants::ROOT_PATH . '/core/templates/navbar.php');
require(Constants::ROOT_PATH . '/core/templates/footer.php');

// Assign Smarty variables
$smarty->assign(
Expand Down
8 changes: 4 additions & 4 deletions core/classes/Avatars/AvatarSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function getAvatarFromUserData(object $data, bool $allow_gifs = fa
}

foreach ($exts as $ext) {
if (file_exists(ROOT_PATH . '/uploads/avatars/' . $data->id . '.' . $ext)) {
if (file_exists(Constants::ROOT_PATH . '/uploads/avatars/' . $data->id . '.' . $ext)) {
// We don't check the validity here since we know the file exists for sure
return ($full ? rtrim(URL::getSelfURL(), '/') : '') . ((defined('CONFIG_PATH')) ? CONFIG_PATH . '/' : '/') . 'uploads/avatars/' . $data->id . '.' . $ext . '?v=' . urlencode($data->avatar_updated);
}
Expand All @@ -62,7 +62,7 @@ public static function getAvatarFromUserData(object $data, bool $allow_gifs = fa

// Fallback to default avatar image if it is set and the avatar type is custom
if (defined('DEFAULT_AVATAR_TYPE') && DEFAULT_AVATAR_TYPE == 'custom' && DEFAULT_AVATAR_IMAGE !== '') {
if (file_exists(ROOT_PATH . '/uploads/avatars/defaults/' . DEFAULT_AVATAR_IMAGE)) {
if (file_exists(Constants::ROOT_PATH . '/uploads/avatars/defaults/' . DEFAULT_AVATAR_IMAGE)) {
// We don't check the validity here since we know the file exists for sure
return ($full ? rtrim(URL::getSelfURL(), '/') : '') . ((defined('CONFIG_PATH')) ? CONFIG_PATH . '/' : '/') . 'uploads/avatars/defaults/' . DEFAULT_AVATAR_IMAGE;
}
Expand Down Expand Up @@ -99,7 +99,7 @@ public static function getAvatarFromUserData(object $data, bool $allow_gifs = fa
* @return bool Whether the URL is a valid image URL
*/
private static function validImageUrl(string $url): bool {
$cache = new Cache(['name' => 'nameless', 'extension' => '.cache', 'path' => ROOT_PATH . '/cache/']);
$cache = new Cache(['name' => 'nameless', 'extension' => '.cache', 'path' => Constants::ROOT_PATH . '/cache/']);
$cache->setCache('avatar_validity');

if ($cache->isCached($url)) {
Expand Down Expand Up @@ -189,7 +189,7 @@ public static function getAllSources(): iterable {
public static function getUrlToFormat(): string {
// Default to Cravatar
if (!isset(self::$_active_source)) {
require_once(ROOT_PATH . '/modules/Core/classes/Avatars/CravatarAvatarSource.php');
require_once(Constants::ROOT_PATH . '/modules/Core/classes/Avatars/CravatarAvatarSource.php');
return (new CravatarAvatarSource())->getUrlToFormat(self::getDefaultPerspective());
}

Expand Down
12 changes: 6 additions & 6 deletions core/classes/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ class Config {
public static function writeable(): bool {
clearstatcache();
if (self::exists()) {
return is_writable(ROOT_PATH . '/core/config.php');
return is_writable(Constants::ROOT_PATH . '/core/config.php');
}

return is_writable(ROOT_PATH . '/core');
return is_writable(Constants::ROOT_PATH . '/core');
}

/**
* @return bool Whether config file exists
*/
public static function exists(): bool {
return file_exists(ROOT_PATH . '/core/config.php');
return file_exists(Constants::ROOT_PATH . '/core/config.php');
}

/**
Expand All @@ -45,7 +45,7 @@ public static function all(): array {
throw new RuntimeException('Config file does not exist. If this happened during installation, please restart the installation in a new private/incognito browser window.');
}

return self::$_config_cache = require(ROOT_PATH . '/core/config.php');
return self::$_config_cache = require(Constants::ROOT_PATH . '/core/config.php');
}

/**
Expand All @@ -55,11 +55,11 @@ public static function all(): array {
*/
public static function write(array $config): void {
$contents = '<?php' . PHP_EOL . PHP_EOL . 'return ' . self::arrayToString($config) . ';';
if (file_put_contents(ROOT_PATH . '/core/config.php', $contents) === false) {
if (file_put_contents(Constants::ROOT_PATH . '/core/config.php', $contents) === false) {
throw new RuntimeException('Failed to write to config file');
}
if (function_exists('opcache_invalidate')) {
opcache_invalidate(ROOT_PATH . '/core/config.php', true);
opcache_invalidate(Constants::ROOT_PATH . '/core/config.php', true);
}
self::$_config_cache = $config;
}
Expand Down
2 changes: 1 addition & 1 deletion core/classes/Core/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public static function formatEmail(string $email, Language $viewing_language): s
return str_replace(
$placeholders,
$placeholder_values,
file_get_contents(implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'custom', 'templates', TEMPLATE, 'email', $email . '.html']))
file_get_contents(implode(DIRECTORY_SEPARATOR, [Constants::ROOT_PATH, 'custom', 'templates', TEMPLATE, 'email', $email . '.html']))
);
}
}
2 changes: 1 addition & 1 deletion core/classes/Core/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function __construct(string $module = 'core', string $active_language = n

// Require file
if ($module === 'core') {
$path = implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'custom', 'languages', '__lng__.json']);
$path = implode(DIRECTORY_SEPARATOR, [Constants::ROOT_PATH, 'custom', 'languages', '__lng__.json']);
} else {
$path = str_replace('/', DIRECTORY_SEPARATOR, $module) . DIRECTORY_SEPARATOR . '__lng__.json';
}
Expand Down
2 changes: 1 addition & 1 deletion core/classes/Core/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public static function isModuleEnabled(string $name): bool {
return true;
}

$cache = new Cache(['name' => 'nameless', 'extension' => '.cache', 'path' => ROOT_PATH . '/cache/']);
$cache = new Cache(['name' => 'nameless', 'extension' => '.cache', 'path' => Constants::ROOT_PATH . '/cache/']);
$cache->setCache('modulescache');

$enabled_modules = $cache->retrieve('enabled_modules');
Expand Down
15 changes: 15 additions & 0 deletions core/classes/Misc/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

tadhgboyle marked this conversation as resolved.
Show resolved Hide resolved
/**
* Constants utility class.
*
* @package NamelessMC\Misc
* @author Derkades
* @version 2.1.0
* @license MIT
*/
class Constants {

const ROOT_PATH = __DIR__ . '/../../..';

}
12 changes: 6 additions & 6 deletions core/classes/Misc/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static function catchException(?Throwable $exception, ?string $error_stri

$smarty = new Smarty();

$smarty->setCompileDir(ROOT_PATH . '/cache/templates_c');
$smarty->setCompileDir(Constants::ROOT_PATH . '/cache/templates_c');

$smarty->assign([
'LANG' => defined('HTML_LANG') ? HTML_LANG : 'en',
Expand Down Expand Up @@ -175,7 +175,7 @@ public static function catchException(?Throwable $exception, ?string $error_stri
'DEBUG_COPIED' => $language->get('general', 'debug_link_copied'),
]);

$smarty->display(ROOT_PATH . '/core/includes/error.tpl');
$smarty->display(Constants::ROOT_PATH . '/core/includes/error.tpl');
die();
}

Expand All @@ -199,17 +199,17 @@ private static function logError(string $type, string $contents): void {
$dir_exists = false;

try {
if (!is_dir(implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'cache', 'logs']))) {
if (is_writable(ROOT_PATH . DIRECTORY_SEPARATOR . 'cache')) {
mkdir(ROOT_PATH . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'logs');
if (!is_dir(implode(DIRECTORY_SEPARATOR, [Constants::ROOT_PATH, 'cache', 'logs']))) {
if (is_writable(Constants::ROOT_PATH . DIRECTORY_SEPARATOR . 'cache')) {
mkdir(Constants::ROOT_PATH . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'logs');
$dir_exists = true;
}
} else {
$dir_exists = true;
}

if ($dir_exists) {
file_put_contents(implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'cache', 'logs', $type . '-log.log']), '[' . date('Y-m-d, H:i:s') . '] ' . $contents . PHP_EOL, FILE_APPEND);
file_put_contents(implode(DIRECTORY_SEPARATOR, [Constants::ROOT_PATH, 'cache', 'logs', $type . '-log.log']), '[' . date('Y-m-d, H:i:s') . '] ' . $contents . PHP_EOL, FILE_APPEND);
}
} catch (Exception $ignored) {
}
Expand Down
6 changes: 3 additions & 3 deletions core/classes/Misc/UpgradeScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class UpgradeScript {

public function __construct() {
$this->_cache = new Cache(
['name' => 'nameless', 'extension' => '.cache', 'path' => ROOT_PATH . '/cache/']
['name' => 'nameless', 'extension' => '.cache', 'path' => Constants::ROOT_PATH . '/cache/']
);
}

Expand All @@ -24,7 +24,7 @@ public function __construct() {
* @return UpgradeScript|null Instance of UpgradeScript from file
*/
public static function get(string $current_version): ?UpgradeScript {
$path = ROOT_PATH . '/core/includes/updates/' . str_replace('.', '', $current_version) . '.php';
$path = Constants::ROOT_PATH . '/core/includes/updates/' . str_replace('.', '', $current_version) . '.php';

if (!file_exists($path)) {
return null;
Expand Down Expand Up @@ -122,7 +122,7 @@ protected function deleteFilesInPath(string $path, array $files, bool $recursive
*/
protected function deleteFiles($paths): void {
foreach ((array) $paths as $path) {
$path = ROOT_PATH . '/' . $path;
$path = Constants::ROOT_PATH . '/' . $path;
if (!file_exists($path)) {
$this->log("'$path' does not exist, cannot delete.");
continue;
Expand Down
2 changes: 1 addition & 1 deletion core/classes/Templates/AssetResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function buildPath(string $file, string $type): string {
: '')
. '/core/assets/' . $file;

if (!file_exists(ROOT_PATH . '/core/assets/' . $file)) {
if (!file_exists(Constants::ROOT_PATH . '/core/assets/' . $file)) {
throw new InvalidArgumentException('Asset file "' . $href . '" not found');
}

Expand Down
13 changes: 6 additions & 7 deletions core/includes/image_upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@

// Initialisation
$page = 'image_uploads';
const ROOT_PATH = '../..';

// Get the directory the user is trying to access
$directory = $_SERVER['REQUEST_URI'];
$directories = explode('/', $directory);

require(ROOT_PATH . '/vendor/autoload.php');
require(ROOT_PATH . '/core/init.php');
require(Constants::ROOT_PATH . '/vendor/autoload.php');
require(Constants::ROOT_PATH . '/core/init.php');

if (!$user->isLoggedIn()) {
die('Not logged in');
Expand Down Expand Up @@ -88,8 +87,8 @@
}

if (
!is_dir(join(DIRECTORY_SEPARATOR, [ROOT_PATH, 'uploads', 'profile_images', $user->data()->id]))
&& !mkdir(join(DIRECTORY_SEPARATOR, [ROOT_PATH, 'uploads', 'profile_images', $user->data()->id]))
!is_dir(join(DIRECTORY_SEPARATOR, [Constants::ROOT_PATH, 'uploads', 'profile_images', $user->data()->id]))
&& !mkdir(join(DIRECTORY_SEPARATOR, [Constants::ROOT_PATH, 'uploads', 'profile_images', $user->data()->id]))
) {
Session::flash('profile_banner_error', $language->get('admin', 'x_directory_not_writable', ['directory' => 'uploads/profile_images']));
Redirect::to(URL::build('/profile/' . urlencode($user->data()->username)));
Expand All @@ -109,7 +108,7 @@
break;
}

$image->setLocation(implode(DIRECTORY_SEPARATOR, [ROOT_PATH, 'uploads', $folder]));
$image->setLocation(implode(DIRECTORY_SEPARATOR, [Constants::ROOT_PATH, 'uploads', $folder]));

if ($image['file']) {
try {
Expand All @@ -130,7 +129,7 @@
$diff = array_diff(array_merge($image_extensions, ['gif', 'ico']), [strtolower($image->getMime())]);

foreach ($diff as $extension) {
$to_remove = glob(ROOT_PATH . '/uploads/avatars/' . $user->data()->id . '.' . $extension);
$to_remove = glob(Constants::ROOT_PATH . '/uploads/avatars/' . $user->data()->id . '.' . $extension);
foreach ($to_remove as $file) {
unlink($file);
}
Expand Down
2 changes: 1 addition & 1 deletion core/includes/maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

const PAGE = 'maintenance';
$page_title = $language->get('errors', 'maintenance_title');
require_once(ROOT_PATH . '/core/templates/frontend_init.php');
require_once(Constants::ROOT_PATH . '/core/templates/frontend_init.php');

if (!$user->isLoggedIn()) {
$smarty->assign(
Expand Down
4 changes: 2 additions & 2 deletions core/includes/tfa_signin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

$template->onPageLoad();

require(ROOT_PATH . '/core/templates/navbar.php');
require(ROOT_PATH . '/core/templates/footer.php');
require(Constants::ROOT_PATH . '/core/templates/navbar.php');
require(Constants::ROOT_PATH . '/core/templates/footer.php');

// Display template
$template->displayTemplate('tfa.tpl', $smarty);
2 changes: 1 addition & 1 deletion core/includes/updates/203.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public function run(): void {
$this->runMigrations();

// Move query interval from cache to settings table
$cache = new Cache(['name' => 'nameless', 'extension' => '.cache', 'path' => ROOT_PATH . '/cache/']);
$cache = new Cache(['name' => 'nameless', 'extension' => '.cache', 'path' => Constants::ROOT_PATH . '/cache/']);
$cache->setCache('server_query_cache');
if ($cache->isCached('query_interval')) {
$query_interval = $cache->retrieve('query_interval');
Expand Down
28 changes: 14 additions & 14 deletions core/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@
// All paths should be writable, but recursively checking everything would take too much time.
// Only check the most important paths.
$writable_check_paths = [
ROOT_PATH,
ROOT_PATH . '/cache',
ROOT_PATH . '/cache/logs',
ROOT_PATH . '/cache/sitemaps',
ROOT_PATH . '/cache/templates_c',
ROOT_PATH . '/uploads',
ROOT_PATH . '/core/config.php'
Constants::ROOT_PATH,
Constants::ROOT_PATH . '/cache',
Constants::ROOT_PATH . '/cache/logs',
Constants::ROOT_PATH . '/cache/sitemaps',
Constants::ROOT_PATH . '/cache/templates_c',
Constants::ROOT_PATH . '/uploads',
Constants::ROOT_PATH . '/core/config.php'
];

foreach ($writable_check_paths as $path) {
if (is_dir($path) && !is_writable($path)) {
die('<p>Your website directory or a subdirectory is not writable. Please ensure all files and directories are owned by
the correct user.</p><p><strong>Example</strong> command to change owner recursively: <code>sudo chown -R www-data: ' . Output::getClean(ROOT_PATH) . '</code></p>');
the correct user.</p><p><strong>Example</strong> command to change owner recursively: <code>sudo chown -R www-data: ' . Output::getClean(Constants::ROOT_PATH) . '</code></p>');
}
}

if (!file_exists(ROOT_PATH . '/cache/templates_c')) {
if (!file_exists(Constants::ROOT_PATH . '/cache/templates_c')) {
try {
mkdir(ROOT_PATH . '/cache/templates_c', 0777, true);
mkdir(Constants::ROOT_PATH . '/cache/templates_c', 0777, true);
} catch (Exception $e) {
die('Unable to create <strong>/cache</strong> directories, please check your file permissions.');
}
Expand Down Expand Up @@ -329,7 +329,7 @@
'file_exists',
'array_key_exists'
];
$securityPolicy->secure_dir = [ROOT_PATH . '/custom/templates', ROOT_PATH . '/custom/panel_templates'];
$securityPolicy->secure_dir = [Constants::ROOT_PATH . '/custom/templates', Constants::ROOT_PATH . '/custom/panel_templates'];
$smarty->enableSecurity($securityPolicy);

// Basic Smarty variables
Expand Down Expand Up @@ -455,8 +455,8 @@
});

foreach ($enabled_modules as $module) {
if (file_exists(ROOT_PATH . '/modules/' . $module['name'] . '/init.php')) {
require(ROOT_PATH . '/modules/' . $module['name'] . '/init.php');
if (file_exists(Constants::ROOT_PATH . '/modules/' . $module['name'] . '/init.php')) {
require(Constants::ROOT_PATH . '/modules/' . $module['name'] . '/init.php');
}
}

Expand All @@ -475,7 +475,7 @@
)) {
// Can continue as normal
} else {
require(ROOT_PATH . '/core/includes/maintenance.php');
require(Constants::ROOT_PATH . '/core/includes/maintenance.php');
die();
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/installation/includes/header.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
$readme = file(ROOT_PATH . '/README.md');
$readme = file(Constants::ROOT_PATH . '/README.md');
$subheader = str_replace('#', '', $readme[0]);

if (isset($_SESSION['installer_language']) && is_file('custom/languages/' . $_SESSION['installer_language'] . '.json')) {
Expand Down
Loading