Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
carlbennett committed Dec 13, 2024
1 parent a2ebdaf commit 5bc9870
Show file tree
Hide file tree
Showing 78 changed files with 585 additions and 556 deletions.
2 changes: 1 addition & 1 deletion bin/deploy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
PROJECT_NAME="bnetdocs-web"
VERSION="$(git symbolic-ref HEAD | sed -e 's/^refs\/heads\/*//g')"
SOURCE_DIRECTORY="$(git rev-parse --show-toplevel)"
TARGET_DIRECTORY="/opt/carlbennett/nginx-www/www.bnetdocs.org"
TARGET_DIRECTORY="/var/www/github-deploy.bnetdocs.org"
TARGET_FILE="${SOURCE_DIRECTORY}/etc/deploy-targets.csv"
TARGET_ENV=""
SKIP_COMPOSER=0
Expand Down
19 changes: 7 additions & 12 deletions etc/config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"asset_versioning": true,
"blizzard_greeting_html": "<div class=\"alert alert-secondary p-2\">Hey there, Blizzard! We hope you find our info useful with your work, come ask us in the Discord any questions. <a href=\"https://discord.gg/u87WVeu\">discord.gg/u87WVeu</a> 😊</div>",
"campaigns": {
"battleforthenet": "",
"vultr": ""
},
"donations": {
Expand All @@ -15,10 +14,10 @@
"paypal_url": null,
"user_id": null
},
"maintenance": [
false,
"BNETDocs has to take a brief moment to do some system maintenance. We'll be back shortly."
],
"maintenance": {
"enabled": false,
"message": "BNETDocs has to take a brief moment to do some system maintenance. We'll be back shortly."
},
"navigation": {
"front_page": "/welcome"
},
Expand Down Expand Up @@ -98,15 +97,11 @@
"enabled": true
},
"mysql": {
"character_set": "utf8",
"character_set": "utf8mb4",
"database": "bnetdocs_phoenix",
"hostname": "localhost",
"password": "",
"servers": [
{
"hostname": "localhost",
"port": 3306
}
],
"port": 3306,
"timeout": 3,
"timezone": "+00:00",
"username": "bnetdocs"
Expand Down
18 changes: 17 additions & 1 deletion src/Controllers/Community/Donate.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace BNETDocs\Controllers\Community;

use \BNETDocs\Libraries\Core\Config;

class Donate extends \BNETDocs\Controllers\Base
{
/**
Expand All @@ -20,7 +22,21 @@ public function __construct()
*/
public function invoke(?array $args): bool
{
$this->model->donations = \BNETDocs\Libraries\Core\Config::get('bnetdocs.donations');
$this->model->donations_btc_address = Config::get('bnetdocs.donations.btc_address');
$this->model->donations_email_address = Config::get('bnetdocs.donations.email_address');
$this->model->donations_paypal_url = Config::get('bnetdocs.donations.paypal_url');
$this->model->donations_user_id = Config::get('bnetdocs.donations.user_id');
if (is_int($this->model->donations_user_id))
{
try
{
$this->model->donations_user = new \BNETDocs\Libraries\User\User($this->model->donations_user_id);
}
catch (\BNETDocs\Exceptions\UserNotFoundException)
{
$this->model->donations_user = null;
}
}
$this->model->_responseCode = \BNETDocs\Libraries\Core\HttpCode::HTTP_OK;
return true;
}
Expand Down
1 change: 0 additions & 1 deletion src/Controllers/Core/Legacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Libraries\Core\Router;
use \CarlBennett\MVC\Libraries\Common;

class Legacy extends \BNETDocs\Controllers\Base
{
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Core/RedirectSoft.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct()
*/
public function invoke(?array $args): bool
{
$this->model->location = \CarlBennett\MVC\Libraries\Common::relativeUrlToAbsolute(\array_shift($args));
$this->model->location = \BNETDocs\Libraries\Core\UrlFormatter::format(\array_shift($args));
$this->model->_responseCode = \BNETDocs\Libraries\Core\HttpCode::HTTP_FOUND;
$this->model->_responseHeaders['Location'] = $this->model->location;
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/Core/Robotstxt.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct()
*/
public function invoke(?array $args): bool
{
$this->model->rules = \CarlBennett\MVC\Libraries\Common::$config->bnetdocs->robotstxt;
$this->model->rules = \BNETDocs\Libraries\Core\Config::get('bnetdocs.robotstxt');
$this->model->_responseCode = \BNETDocs\Libraries\Core\HttpCode::HTTP_OK;
return true;
}
Expand Down
9 changes: 5 additions & 4 deletions src/Controllers/Core/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace BNETDocs\Controllers\Core;

use \BNETDocs\Libraries\Core\Config;
use \BNETDocs\Libraries\Core\HttpCode;
use \BNETDocs\Models\Core\Status as StatusModel;
use \CarlBennett\MVC\Libraries\Common;

class Status extends \BNETDocs\Controllers\Base
{
Expand Down Expand Up @@ -42,15 +42,16 @@ protected static function getStatus(StatusModel $model) : bool
$ua = substr(getenv('HTTP_USER_AGENT') ?? '', 0, self::MAX_USER_AGENT);
$utc = new \DateTimeZone('Etc/UTC');

$geoip_enabled = Config::get('geoip.enabled');
$model->status = [
'healthcheck' => [
'database' => (!is_null(\BNETDocs\Libraries\Db\MariaDb::instance())),
'geoip' => Common::$config->geoip->enabled && file_exists(Common::$config->geoip->database_file),
'geoip' => $geoip_enabled && file_exists(Config::get('geoip.database_file')),
],
'remote_address' => $remote_address,
'remote_geoinfo' => Common::$config->geoip->enabled ? \BNETDocs\Libraries\Core\GeoIP::getRecord($remote_address) : null,
'remote_geoinfo' => $geoip_enabled ? \BNETDocs\Libraries\Core\GeoIP::getRecord($remote_address) : null,
'remote_is_blizzard' => \BNETDocs\Libraries\Core\BlizzardCheck::is_blizzard(),
'remote_is_browser' => Common::isBrowser($ua),
'remote_is_browser' => \BNETDocs\Libraries\Core\StringProcessor::isBrowser($ua),
'remote_is_slack' => \BNETDocs\Libraries\Core\SlackCheck::is_slack(),
'remote_user_agent' => $ua,
'timestamp' => new \BNETDocs\Libraries\Core\DateTimeImmutable('now', $utc),
Expand Down
3 changes: 1 addition & 2 deletions src/Controllers/Server/UpdateJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ public function invoke(?array $args): bool
return true;
}

$c = &\CarlBennett\MVC\Libraries\Common::$config;
$q = Router::query();
$server_id = isset($q['id']) ? (int) $q['id'] : null;
$job_token = isset($q['job_token']) ? $q['job_token'] : null;
$status = isset($q['status']) ? (int) $q['status'] : null;

if ($job_token !== $c->bnetdocs->server_update_job_token)
if ($job_token !== \BNETDocs\Libraries\Core\Config::get('bnetdocs.server_update_job_token'))
{
$this->model->_responseCode = HttpCode::HTTP_FORBIDDEN;
return true;
Expand Down
38 changes: 21 additions & 17 deletions src/Controllers/User/ChangePassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

namespace BNETDocs\Controllers\User;

use \BNETDocs\Libraries\Core\Config;
use \BNETDocs\Libraries\Core\Router;
use \BNETDocs\Libraries\EventLog\Logger;
use \CarlBennett\MVC\Libraries\Common;
use \BNETDocs\Libraries\User\User;
use \BNETDocs\Models\User\ChangePassword as ChangePasswordModel;

class ChangePassword extends \BNETDocs\Controllers\Base
{
public function __construct()
{
$this->model = new \BNETDocs\Models\User\ChangePassword();
$this->model = new ChangePasswordModel();
}

public function invoke(?array $args): bool
Expand All @@ -33,53 +35,55 @@ protected function tryChangePassword(): void

if ($pw2 !== $pw3)
{
$this->model->error = 'NONMATCHING_PASSWORD';
$this->model->error = ChangePasswordModel::ERROR_NONMATCHING_PASSWORD;
return;
}

if (!$this->model->active_user->checkPassword($pw1))
{
$this->model->error = 'PASSWORD_INCORRECT';
$this->model->error = ChangePasswordModel::ERROR_PASSWORD_INCORRECT;
return;
}

$pwlen = strlen($pw2);
$req = &Common::$config->bnetdocs->user_register_requirements;
$req = Config::get('bnetdocs.user_register_requirements') ?? [];
$email = $this->model->active_user->getEmail();
$username = $this->model->active_user->getUsername();

if (!$req->password_allow_email && stripos($pw2, $email))
if (!($req['password_allow_email'] ?? false) && stripos($pw2, $email))
{
$this->model->error = 'PASSWORD_CONTAINS_EMAIL';
$this->model->error = ChangePasswordModel::ERROR_PASSWORD_CONTAINS_EMAIL;
return;
}

if (!$req->password_allow_username && stripos($pw2, $username))
if (!($req['password_allow_username'] ?? false) && stripos($pw2, $username))
{
$this->model->error = 'PASSWORD_CONTAINS_USERNAME';
$this->model->error = ChangePasswordModel::ERROR_PASSWORD_CONTAINS_USERNAME;
return;
}

if (is_numeric($req->password_length_max) && $pwlen > $req->password_length_max)
if (is_numeric($req['password_length_max'] ?? User::MAX_PASSWORD_HASH)
&& $pwlen > ($req['password_length_max'] ?? User::MAX_PASSWORD_HASH))
{
$this->model->error = 'PASSWORD_TOO_LONG';
$this->model->error = ChangePasswordModel::ERROR_PASSWORD_TOO_LONG;
return;
}

if (is_numeric($req->password_length_min) && $pwlen < $req->password_length_min)
if (is_numeric($req['password_length_min'] ?? 4)
&& $pwlen < ($req['password_length_min'] ?? 4))
{
$this->model->error = 'PASSWORD_TOO_SHORT';
$this->model->error = ChangePasswordModel::ERROR_PASSWORD_TOO_SHORT;
return;
}

$denylist = Common::$config->bnetdocs->user_password_denylist_map;
$denylist = Config::get('bnetdocs.user_password_denylist_map') ?? '../etc/password_denylist.json';
$denylist = json_decode(file_get_contents('./' . $denylist));
foreach ($denylist as $denylist_pw)
{
if (strtolower($denylist_pw->password) == strtolower($pw2))
{
$this->model->error = 'PASSWORD_BLACKLIST';
$this->model->error_extra = $denylist_pw->reason;
$this->model->error = ChangePasswordModel::ERROR_PASSWORD_DENYLIST;
$this->model->denylist_reason = $denylist_pw->reason;
return;
}
}
Expand All @@ -88,7 +92,7 @@ protected function tryChangePassword(): void
$old_password_salt = $this->model->active_user->getPasswordSalt();

$this->model->active_user->setPassword($pw2);
$this->model->error = $this->model->active_user->commit() ? false : 'INTERNAL_ERROR';
$this->model->error = $this->model->active_user->commit() ? false : ChangePasswordModel::ERROR_INTERNAL;

$event = Logger::initEvent(
\BNETDocs\Libraries\EventLog\EventTypes::USER_PASSWORD_CHANGE,
Expand Down
3 changes: 1 addition & 2 deletions src/Controllers/User/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use \BNETDocs\Libraries\EventLog\Logger;
use \BNETDocs\Libraries\User\User;
use \BNETDocs\Models\User\Login as LoginModel;
use \CarlBennett\MVC\Libraries\Common;

class Login extends \BNETDocs\Controllers\Base
{
Expand Down Expand Up @@ -38,7 +37,7 @@ public function invoke(?array $args): bool
$this->model->error = LoginModel::ERROR_EMPTY_EMAIL;
else if (empty($this->model->password))
$this->model->error = LoginModel::ERROR_EMPTY_PASSWORD;
else if (Common::$config->bnetdocs->user_login_disabled)
else if (\BNETDocs\Libraries\Core\Config::get('bnetdocs.user_login_disabled'))
$this->model->error = LoginModel::ERROR_SYSTEM_DISABLED;

if ($this->model->error) return true;
Expand Down
Loading

0 comments on commit 5bc9870

Please sign in to comment.