fix nullsafe warning

This commit is contained in:
Jean-Christian Denis 2023-04-22 10:59:59 +02:00
parent a3bb29bd64
commit 78b0c4d577
Signed by: JcDenis
GPG key ID: 1B5B8C5B90B6C951
4 changed files with 67 additions and 18 deletions

View file

@ -15,7 +15,6 @@ declare(strict_types=1);
namespace Dotclear\Plugin\filesAlias; namespace Dotclear\Plugin\filesAlias;
use dcAdmin; use dcAdmin;
use dcAuth;
use dcCore; use dcCore;
use dcFavorites; use dcFavorites;
use dcNsProcess; use dcNsProcess;
@ -36,23 +35,32 @@ class Backend extends dcNsProcess
return false; return false;
} }
// nullsafe
if (is_null(dcCore::app()->auth) || is_null(dcCore::app()->blog) || is_null(dcCore::app()->adminurl)) {
return false;
}
dcCore::app()->menu[dcAdmin::MENU_BLOG]->addItem( dcCore::app()->menu[dcAdmin::MENU_BLOG]->addItem(
My::name(), My::name(),
dcCore::app()->adminurl->get('admin.plugin.' . My::id()), dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
dcPage::getPF(My::id() . '/icon.svg'), dcPage::getPF(My::id() . '/icon.svg'),
preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']), preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.' . My::id())) . '(&.*)?$/', $_SERVER['REQUEST_URI']),
dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcAuth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id) dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcCore::app()->auth::PERMISSION_CONTENT_ADMIN]), dcCore::app()->blog->id)
); );
dcCore::app()->addBehavior('adminDashboardFavoritesV2', function (dcFavorites $favs): void { dcCore::app()->addBehavior('adminDashboardFavoritesV2', function (dcFavorites $favs): void {
// nullsafe
if (is_null(dcCore::app()->auth) || is_null(dcCore::app()->adminurl)) {
return;
}
$favs->register(My::id(), [ $favs->register(My::id(), [
'title' => My::name(), 'title' => My::name(),
'url' => dcCore::app()->adminurl->get('admin.plugin.' . My::id()), 'url' => dcCore::app()->adminurl->get('admin.plugin.' . My::id()),
'small-icon' => dcPage::getPF(My::id() . '/icon.svg'), 'small-icon' => dcPage::getPF(My::id() . '/icon.svg'),
'large-icon' => dcPage::getPF(My::id() . '/icon.svg'), 'large-icon' => dcPage::getPF(My::id() . '/icon.svg'),
'permissions' => dcCore::app()->auth->makePermissions([ 'permissions' => dcCore::app()->auth->makePermissions([
dcAuth::PERMISSION_USAGE, dcCore::app()->auth::PERMISSION_USAGE,
dcAuth::PERMISSION_CONTENT_ADMIN, dcCore::app()->auth::PERMISSION_CONTENT_ADMIN,
]), ]),
]); ]);
}); });

View file

@ -14,7 +14,6 @@ declare(strict_types=1);
namespace Dotclear\Plugin\filesAlias; namespace Dotclear\Plugin\filesAlias;
use dcAuth;
use dcCore; use dcCore;
use dcMedia; use dcMedia;
use dcNsProcess; use dcNsProcess;
@ -37,12 +36,14 @@ class Manage extends dcNsProcess
{ {
public static function init(): bool public static function init(): bool
{ {
static::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->auth->check( static::$init = defined('DC_CONTEXT_ADMIN')
dcCore::app()->auth->makePermissions([ && !is_null(dcCore::app()->auth) && !is_null(dcCore::app()->blog) // nullsafe
dcAuth::PERMISSION_ADMIN, && dcCore::app()->auth->check(
]), dcCore::app()->auth->makePermissions([
dcCore::app()->blog->id dcCore::app()->auth::PERMISSION_ADMIN,
); ]),
dcCore::app()->blog->id
);
return static::$init; return static::$init;
} }
@ -53,11 +54,16 @@ class Manage extends dcNsProcess
return false; return false;
} }
// nullsafe
if (is_null(dcCore::app()->blog) || is_null(dcCore::app()->adminurl)) {
return false;
}
if (!(dcCore::app()->media instanceof dcMedia)) { if (!(dcCore::app()->media instanceof dcMedia)) {
dcCore::app()->media = new dcMedia(); dcCore::app()->media = new dcMedia();
} }
# Update aliases // Update aliases
if (isset($_POST['a']) && is_array($_POST['a'])) { if (isset($_POST['a']) && is_array($_POST['a'])) {
try { try {
Utils::updateAliases($_POST['a']); Utils::updateAliases($_POST['a']);
@ -68,7 +74,7 @@ class Manage extends dcNsProcess
} }
} }
# New alias // New alias
if (isset($_POST['filesalias_url'])) { if (isset($_POST['filesalias_url'])) {
$url = empty($_POST['filesalias_url']) ? PallazzoTools::rand_uniqid() : $_POST['filesalias_url']; $url = empty($_POST['filesalias_url']) ? PallazzoTools::rand_uniqid() : $_POST['filesalias_url'];
@ -132,6 +138,11 @@ class Manage extends dcNsProcess
private static function displayAliasForm(): void private static function displayAliasForm(): void
{ {
// nullsafe
if (is_null(dcCore::app()->blog) || is_null(dcCore::app()->adminurl) || is_null(dcCore::app()->media)) {
return;
}
echo echo
dcPage::breadcrumb([ dcPage::breadcrumb([
Html::escapeHTML(dcCore::app()->blog->name) => '', Html::escapeHTML(dcCore::app()->blog->name) => '',
@ -175,6 +186,11 @@ class Manage extends dcNsProcess
private static function displayAliasList(): void private static function displayAliasList(): void
{ {
// nullsafe
if (is_null(dcCore::app()->blog) || is_null(dcCore::app()->adminurl) || is_null(dcCore::app()->media)) {
return;
}
$aliases = Utils::getAliases(); $aliases = Utils::getAliases();
echo echo

View file

@ -22,6 +22,11 @@ class UrlHandler extends dcUrlHandlers
{ {
public static function alias(string $args): void public static function alias(string $args): void
{ {
// nullsafe
if (is_null(dcCore::app()->ctx)) {
return;
}
$delete = false; $delete = false;
dcCore::app()->ctx->__set('filealias', Utils::getAlias($args)); dcCore::app()->ctx->__set('filealias', Utils::getAlias($args));

View file

@ -23,20 +23,26 @@ class Utils
{ {
public static function getAliases(): dcRecord public static function getAliases(): dcRecord
{ {
// nullsafe
$blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id;
return new dcRecord(dcCore::app()->con->select( return new dcRecord(dcCore::app()->con->select(
'SELECT filesalias_url, filesalias_destination, filesalias_password, filesalias_disposable ' . 'SELECT filesalias_url, filesalias_destination, filesalias_password, filesalias_disposable ' .
'FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' . 'FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' .
"WHERE blog_id = '" . dcCore::app()->con->escapeStr(dcCore::app()->blog->id) . "' " . "WHERE blog_id = '" . dcCore::app()->con->escapeStr($blog_id) . "' " .
'ORDER BY filesalias_url ASC ' 'ORDER BY filesalias_url ASC '
)); ));
} }
public static function getAlias(string $url): dcRecord public static function getAlias(string $url): dcRecord
{ {
// nullsafe
$blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id;
return new dcRecord(dcCore::app()->con->select( return new dcRecord(dcCore::app()->con->select(
'SELECT filesalias_url, filesalias_destination, filesalias_password, filesalias_disposable ' . 'SELECT filesalias_url, filesalias_destination, filesalias_password, filesalias_disposable ' .
'FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' . 'FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' .
"WHERE blog_id = '" . dcCore::app()->con->escapeStr(dcCore::app()->blog->id) . "' " . "WHERE blog_id = '" . dcCore::app()->con->escapeStr($blog_id) . "' " .
"AND filesalias_url = '" . dcCore::app()->con->escapeStr($url) . "' " . "AND filesalias_url = '" . dcCore::app()->con->escapeStr($url) . "' " .
'ORDER BY filesalias_url ASC ' 'ORDER BY filesalias_url ASC '
)); ));
@ -73,8 +79,11 @@ class Utils
throw new Exception(__('File destination is empty.')); throw new Exception(__('File destination is empty.'));
} }
// nullsafe
$blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id;
$cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . My::ALIAS_TABLE_NAME); $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . My::ALIAS_TABLE_NAME);
$cur->setField('blog_id', (string) dcCore::app()->blog->id); $cur->setField('blog_id', $blog_id);
$cur->setField('filesalias_url', (string) $url); $cur->setField('filesalias_url', (string) $url);
$cur->setField('filesalias_destination', (string) $destination); $cur->setField('filesalias_destination', (string) $destination);
$cur->setField('filesalias_password', $password); $cur->setField('filesalias_password', $password);
@ -84,23 +93,34 @@ class Utils
public static function deleteAliases(): void public static function deleteAliases(): void
{ {
// nullsafe
$blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id;
dcCore::app()->con->execute( dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' . 'DELETE FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' .
"WHERE blog_id = '" . dcCore::app()->con->escapeStr(dcCore::app()->blog->id) . "' " "WHERE blog_id = '" . dcCore::app()->con->escapeStr($blog_id) . "' "
); );
} }
public static function deleteAlias(string $url): void public static function deleteAlias(string $url): void
{ {
// nullsafe
$blog_id = is_null(dcCore::app()->blog) ? '' : dcCore::app()->blog->id;
dcCore::app()->con->execute( dcCore::app()->con->execute(
'DELETE FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' . 'DELETE FROM ' . dcCore::app()->prefix . My::ALIAS_TABLE_NAME . ' ' .
"WHERE blog_id = '" . dcCore::app()->con->escapeStr(dcCore::app()->blog->id) . "' " . "WHERE blog_id = '" . dcCore::app()->con->escapeStr($blog_id) . "' " .
"AND filesalias_url = '" . dcCore::app()->con->escapeStr($url) . "' " "AND filesalias_url = '" . dcCore::app()->con->escapeStr($url) . "' "
); );
} }
public static function getMediaId(string $target): int public static function getMediaId(string $target): int
{ {
// nullsafe
if (is_null(dcCore::app()->blog)) {
return 0;
}
$strReq = 'SELECT media_id ' . $strReq = 'SELECT media_id ' .
'FROM ' . dcCore::app()->prefix . dcMedia::MEDIA_TABLE_NAME . ' ' . 'FROM ' . dcCore::app()->prefix . dcMedia::MEDIA_TABLE_NAME . ' ' .
"WHERE media_path = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->settings->get('system')->get('public_path')) . "' " . "WHERE media_path = '" . dcCore::app()->con->escapeStr((string) dcCore::app()->blog->settings->get('system')->get('public_path')) . "' " .