before_login_btn hook + system conf attributes (#7761)

* `before_login_btn` hook + system conf attributes

* phpstan fix

* Refactoring

---------

Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
This commit is contained in:
Inverle
2025-07-30 08:03:04 +02:00
committed by GitHub
parent e967b07589
commit e33ef74af9
8 changed files with 68 additions and 43 deletions

View File

@@ -54,8 +54,8 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController {
}
foreach ($userConfigUpdated as $configName => $configValue) {
if ($configValue !== null) {
$userConfig->_param($configName, $configValue);
if ($configName !== '' && $configValue !== null) {
$userConfig->_attribute($configName, $configValue);
}
}
@@ -624,7 +624,12 @@ class FreshRSS_user_Controller extends FreshRSS_ActionController {
return;
}
$userConfig->_param($field, $value);
if ($field === '') {
Minz_Error::error(400, 'Invalid field name');
return;
}
$userConfig->_attribute($field, $value);
$ok = $userConfig->save();
FreshRSS_UserDAO::touch($username);

View File

@@ -55,10 +55,12 @@ final class FreshRSS_Context {
public static bool $isCli = false;
/**
* @access private
* @deprecated Will be made `private`; use `FreshRSS_Context::systemConf()` instead.
*/
public static ?FreshRSS_SystemConfiguration $system_conf = null;
/**
* @access private
* @deprecated Will be made `private`; use `FreshRSS_Context::userConf()` instead.
*/
public static ?FreshRSS_UserConfiguration $user_conf = null;
@@ -128,10 +130,8 @@ final class FreshRSS_Context {
FreshRSS_Context::$search = new FreshRSS_BooleanSearch('');
//Legacy
$oldEntries = FreshRSS_Context::$user_conf->param('old_entries', 0);
$oldEntries = is_numeric($oldEntries) ? (int)$oldEntries : 0;
$keepMin = FreshRSS_Context::$user_conf->param('keep_history_default', -5);
$keepMin = is_numeric($keepMin) ? (int)$keepMin : -5;
$oldEntries = FreshRSS_Context::$user_conf->attributeInt('old_entries') ?? 0;
$keepMin = FreshRSS_Context::$user_conf->attributeInt('keep_history_default') ?? -5;
if ($oldEntries > 0 || $keepMin > -5) { //Freshrss < 1.15
$archiving = FreshRSS_Context::$user_conf->archiving;
$archiving['keep_max'] = false;

View File

@@ -105,39 +105,4 @@ final class FreshRSS_UserConfiguration extends Minz_Configuration {
}
return $default_user_conf;
}
/**
* @param non-empty-string $key
* @return array<int|string,mixed>|null
*/
public function attributeArray(string $key): ?array {
$a = parent::param($key, null);
return is_array($a) ? $a : null;
}
/** @param non-empty-string $key */
public function attributeBool(string $key): ?bool {
$a = parent::param($key, null);
return is_bool($a) ? $a : null;
}
/** @param non-empty-string $key */
public function attributeInt(string $key): ?int {
$a = parent::param($key, null);
return is_numeric($a) ? (int)$a : null;
}
/** @param non-empty-string $key */
public function attributeString(string $key): ?string {
$a = parent::param($key, null);
return is_string($a) ? $a : null;
}
/**
* @param non-empty-string $key
* @param array<string,mixed>|mixed|null $value Value, not HTML-encoded
*/
public function _attribute(string $key, $value = null): void {
parent::_param($key, $value);
}
}

View File

@@ -38,6 +38,8 @@
</label>
</div>
<?= Minz_ExtensionManager::callHookString('before_login_btn') ?>
<div class="form-group form-group-actions">
<button id="loginButton" type="submit" class="btn btn-important" disabled="disabled">
<?= _t('gen.auth.login') ?>

View File

@@ -66,6 +66,8 @@
</div>
<?php } ?>
<?= Minz_ExtensionManager::callHookString('before_login_btn') ?>
<div class="form-group form-group-actions">
<?php
$redirect_url = urlencode(Minz_Url::display(

View File

@@ -166,6 +166,7 @@ The following events are available:
* `api_misc` (`function(): void`): to allow extensions to have own API endpoint
on `/api/misc.php/Extension%20Name/` or `/api/misc.php?ext=Extension%20Name`.
* `before_login_btn` (`function(): string`): Allows to insert HTML before the login button. Applies to the create button on the register page as well. Example use case is inserting a captcha widget.
* `check_url_before_add` (`function($url) -> Url | null`): will be executed every time a URL is added. The URL itself will be passed as parameter. This way a website known to have feeds which doesnt advertise it in the header can still be automatically supported.
* `custom_favicon_btn_url` (`function(FreshRSS_Feed $feed): string | null`): Allows extensions to implement a button for setting a custom favicon for individual feeds by providing an URL. The URL will be sent a POST request with the `extAction` field set to either `query_icon_info` or `update_icon`, along with an `id` field which describes the feed's ID.
Example response for a `query_icon_info` request:

View File

@@ -155,6 +155,8 @@ class Minz_Configuration {
* @param string $key the name of the param.
* @param mixed $default default value to return if key does not exist.
* @return array|mixed value corresponding to the key.
* @access private
* @deprecated Use `attribute*()` methods instead.
*/
public function param(string $key, mixed $default = null): mixed {
if (isset($this->data[$key])) {
@@ -170,6 +172,8 @@ class Minz_Configuration {
/**
* A wrapper for param().
* @return array|mixed
* @access private
* @deprecated
*/
public function __get(string $key): mixed {
return $this->param($key);
@@ -180,6 +184,8 @@ class Minz_Configuration {
*
* @param string $key the param name to set.
* @param mixed $value the value to set. If null, the key is removed from the configuration.
* @access private
* @deprecated Use `_attribute()` instead.
*/
public function _param(string $key, mixed $value = null): void {
if ($this->configuration_setter !== null && $this->configuration_setter->support($key)) {
@@ -193,6 +199,8 @@ class Minz_Configuration {
/**
* A wrapper for _param().
* @access private
* @deprecated
*/
public function __set(string $key, mixed $value): void {
$this->_param($key, $value);
@@ -217,4 +225,39 @@ class Minz_Configuration {
return true;
}
/**
* @param non-empty-string $key
* @return array<int|string,mixed>|null
*/
public function attributeArray(string $key): ?array {
$a = self::param($key, null);
return is_array($a) ? $a : null;
}
/** @param non-empty-string $key */
public function attributeBool(string $key): ?bool {
$a = self::param($key, null);
return is_bool($a) ? $a : null;
}
/** @param non-empty-string $key */
public function attributeInt(string $key): ?int {
$a = self::param($key, null);
return is_numeric($a) ? (int)$a : null;
}
/** @param non-empty-string $key */
public function attributeString(string $key): ?string {
$a = self::param($key, null);
return is_string($a) ? $a : null;
}
/**
* @param non-empty-string $key
* @param array<string,mixed>|mixed|null $value Value, not HTML-encoded
*/
public function _attribute(string $key, $value = null): void {
self::_param($key, $value);
}
}

View File

@@ -26,6 +26,10 @@ final class Minz_ExtensionManager {
'list' => [],
'signature' => 'NoneToNone',
],
'before_login_btn' => [ // function(): string
'list' => [],
'signature' => 'NoneToString',
],
'check_url_before_add' => [ // function($url) -> Url | null
'list' => [],
'signature' => 'OneToOne',
@@ -155,7 +159,10 @@ final class Minz_ExtensionManager {
$list_potential_extensions = array_merge($list_core_extensions, $list_thirdparty_extensions);
$system_conf = Minz_Configuration::get('system');
self::$ext_auto_enabled = $system_conf->extensions_enabled;
self::$ext_auto_enabled = array_filter(
$system_conf->attributeArray('extensions_enabled') ?? [],
fn($value, $key): bool => is_string($key) && is_bool($value),
ARRAY_FILTER_USE_BOTH);
foreach ($list_potential_extensions as $ext_pathname) {
if (!is_dir($ext_pathname)) {