forked from aledeg/xExtension-WhiteList
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Related to FreshRSS/Extensions#185 fix FreshRSS/FreshRSS#5989 (comment)
- Loading branch information
Showing
3 changed files
with
22 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# xExtension-WhiteList | ||
|
||
A FreshRSS extension to validate that new feeds are allowed according to a white-list. | ||
The comparison with the white-list uses regex to find matches. If the format used has | ||
to be recognized by the PHP `preg_match` function. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,41 @@ | ||
<?php | ||
|
||
class WhiteListExtension extends Minz_Extension { | ||
public function init() { | ||
public function init(): void { | ||
$this->registerTranslates(); | ||
|
||
$this->registerHook('check_url_before_add', [$this, 'checkWhiteList']); | ||
} | ||
public function handleConfigureAction() { | ||
|
||
public function handleConfigureAction(): void { | ||
$this->registerTranslates(); | ||
|
||
if (Minz_Request::isPost()) { | ||
$configuration = [ | ||
'patterns' => Minz_Request::paramTextToArray('patterns', ''), | ||
'patterns' => Minz_Request::paramTextToArray('patterns', []), | ||
]; | ||
$this->setSystemConfiguration($configuration); | ||
} | ||
} | ||
|
||
public function checkWhiteList($url) { | ||
foreach ($this->getSystemConfigurationValue('patterns') as $pattern) { | ||
if (1 === preg_match("/{$pattern}/i", $url)) { | ||
return $url; | ||
public function checkWhiteList(string $url): string { | ||
$patterns = $this->getSystemConfigurationValue('patterns') ?? []; | ||
if (is_array($patterns)) { | ||
foreach ($patterns as $pattern) { | ||
if (1 === preg_match("/{$pattern}/i", $url)) { | ||
return $url; | ||
} | ||
} | ||
} | ||
Minz_Log::warning(_t('ext.white_list.warning.not_white_listed', $url)); | ||
throw new FreshRSS_FeedNotAdded_Exception($url); | ||
} | ||
|
||
public function getPatterns() { | ||
return implode(PHP_EOL, $this->getSystemConfigurationValue('patterns') ?? []); | ||
public function getPatterns(): string { | ||
$patterns = $this->getSystemConfigurationValue('patterns') ?? []; | ||
if (is_array($patterns)) { | ||
return implode(PHP_EOL, $patterns); | ||
} | ||
return ''; | ||
} | ||
} |