summaryrefslogtreecommitdiff
path: root/classes/pref/filters.php
diff options
context:
space:
mode:
Diffstat (limited to 'classes/pref/filters.php')
-rwxr-xr-xclasses/pref/filters.php132
1 files changed, 82 insertions, 50 deletions
diff --git a/classes/pref/filters.php b/classes/pref/filters.php
index 29d309dbb..04178f1a6 100755
--- a/classes/pref/filters.php
+++ b/classes/pref/filters.php
@@ -1,20 +1,28 @@
<?php
class Pref_Filters extends Handler_Protected {
- function csrf_ignore($method) {
+ const ACTION_TAG = 4;
+ const ACTION_SCORE = 6;
+ const ACTION_LABEL = 7;
+ const ACTION_PLUGIN = 9;
+ const ACTION_REMOVE_TAG = 10;
+
+ const PARAM_ACTIONS = [self::ACTION_TAG, self::ACTION_SCORE,
+ self::ACTION_LABEL, self::ACTION_PLUGIN, self::ACTION_REMOVE_TAG];
+
+ function csrf_ignore(string $method): bool {
$csrf_ignored = array("index", "getfiltertree", "savefilterorder");
return array_search($method, $csrf_ignored) !== false;
}
- function filtersortreset() {
+ function filtersortreset(): void {
$sth = $this->pdo->prepare("UPDATE ttrss_filters2
SET order_id = 0 WHERE owner_uid = ?");
$sth->execute([$_SESSION['uid']]);
- return;
}
- function savefilterorder() {
+ function savefilterorder(): void {
$data = json_decode($_POST['payload'], true);
#file_put_contents("/tmp/saveorder.json", clean($_POST['payload']));
@@ -40,11 +48,9 @@ class Pref_Filters extends Handler_Protected {
}
}
}
-
- return;
}
- function testFilterDo() {
+ function testFilterDo(): void {
$offset = (int) clean($_REQUEST["offset"]);
$limit = (int) clean($_REQUEST["limit"]);
@@ -59,7 +65,9 @@ class Pref_Filters extends Handler_Protected {
$res = $this->pdo->query("SELECT id,name FROM ttrss_filter_types");
- $filter_types = array();
+ /** @var array<int, string> */
+ $filter_types = [];
+
while ($line = $res->fetch()) {
$filter_types[$line["id"]] = $line["name"];
}
@@ -67,7 +75,10 @@ class Pref_Filters extends Handler_Protected {
$scope_qparts = array();
$rctr = 0;
+
+ /** @var string $r */
foreach (clean($_REQUEST["rule"]) AS $r) {
+ /** @var array{'reg_exp': string, 'filter_type': int, 'feed_id': array<int, int|string>, 'name': string}|null */
$rule = json_decode($r, true);
if ($rule && $rctr < 5) {
@@ -75,19 +86,21 @@ class Pref_Filters extends Handler_Protected {
unset($rule["filter_type"]);
$scope_inner_qparts = [];
+
+ /** @var int|string $feed_id may be a category string (e.g. 'CAT:7') or feed ID int */
foreach ($rule["feed_id"] as $feed_id) {
- if (strpos($feed_id, "CAT:") === 0) {
- $cat_id = (int) substr($feed_id, 4);
- array_push($scope_inner_qparts, "cat_id = " . $this->pdo->quote($cat_id));
- } else if ($feed_id > 0) {
- array_push($scope_inner_qparts, "feed_id = " . $this->pdo->quote($feed_id));
- }
- }
+ if (strpos("$feed_id", "CAT:") === 0) {
+ $cat_id = (int) substr("$feed_id", 4);
+ array_push($scope_inner_qparts, "cat_id = " . $cat_id);
+ } else if (is_numeric($feed_id) && $feed_id > 0) {
+ array_push($scope_inner_qparts, "feed_id = " . (int)$feed_id);
+ }
+ }
- if (count($scope_inner_qparts) > 0) {
- array_push($scope_qparts, "(" . implode(" OR ", $scope_inner_qparts) . ")");
- }
+ if (count($scope_inner_qparts) > 0) {
+ array_push($scope_qparts, "(" . implode(" OR ", $scope_inner_qparts) . ")");
+ }
array_push($filter["rules"], $rule);
@@ -162,7 +175,7 @@ class Pref_Filters extends Handler_Protected {
print json_encode($rv);
}
- private function _get_rules_list($filter_id) {
+ private function _get_rules_list(int $filter_id): string {
$sth = $this->pdo->prepare("SELECT reg_exp,
inverse,
match_on,
@@ -203,7 +216,7 @@ class Pref_Filters extends Handler_Protected {
} else {
$where = $line["cat_filter"] ?
- Feeds::_get_cat_title($line["cat_id"]) :
+ Feeds::_get_cat_title($line["cat_id"] ?? 0) :
($line["feed_id"] ?
Feeds::_get_title($line["feed_id"]) : __("All feeds"));
}
@@ -222,7 +235,7 @@ class Pref_Filters extends Handler_Protected {
return $rv;
}
- function getfiltertree() {
+ function getfiltertree(): void {
$root = array();
$root['id'] = 'root';
$root['name'] = __('Filters');
@@ -270,7 +283,7 @@ class Pref_Filters extends Handler_Protected {
}
}
- if ($line['action_id'] == 7) {
+ if ($line['action_id'] == self::ACTION_LABEL) {
$label_sth = $this->pdo->prepare("SELECT fg_color, bg_color
FROM ttrss_labels2 WHERE caption = ? AND
owner_uid = ?");
@@ -307,10 +320,9 @@ class Pref_Filters extends Handler_Protected {
$fl['items'] = array($root);
print json_encode($fl);
- return;
}
- function edit() {
+ function edit(): void {
$filter_id = (int) clean($_REQUEST["id"] ?? 0);
@@ -406,7 +418,10 @@ class Pref_Filters extends Handler_Protected {
}
}
- private function _get_rule_name($rule) {
+ /**
+ * @param array<string, mixed>|null $rule
+ */
+ private function _get_rule_name(?array $rule = null): string {
if (!$rule) $rule = json_decode(clean($_REQUEST["rule"]), true);
$feeds = $rule["feed_id"];
@@ -446,11 +461,18 @@ class Pref_Filters extends Handler_Protected {
"<span class='field'>$filter_type</span>", "<span class='feed'>$feed</span>", isset($rule["inverse"]) ? __("(inverse)") : "") . "</span>";
}
- function printRuleName() {
+ function printRuleName(): void {
print $this->_get_rule_name(json_decode(clean($_REQUEST["rule"]), true));
}
- private function _get_action_name($action) {
+ /**
+ * @param array<string, mixed>|null $action
+ */
+ private function _get_action_name(?array $action = null): string {
+ if (!$action) {
+ return "";
+ }
+
$sth = $this->pdo->prepare("SELECT description FROM
ttrss_filter_actions WHERE id = ?");
$sth->execute([(int)$action["action_id"]]);
@@ -461,11 +483,7 @@ class Pref_Filters extends Handler_Protected {
$title = __($row["description"]);
- if ($action["action_id"] == 4 || $action["action_id"] == 6 ||
- $action["action_id"] == 7)
- $title .= ": " . $action["action_param"];
-
- if ($action["action_id"] == 9) {
+ if ($action["action_id"] == self::ACTION_PLUGIN) {
list ($pfclass, $pfaction) = explode(":", $action["action_param"]);
$filter_actions = PluginHost::getInstance()->get_filter_actions();
@@ -478,18 +496,20 @@ class Pref_Filters extends Handler_Protected {
}
}
}
+ } else if (in_array($action["action_id"], self::PARAM_ACTIONS)) {
+ $title .= ": " . $action["action_param"];
}
}
return $title;
}
- function printActionName() {
- print $this->_get_action_name(json_decode(clean($_REQUEST["action"]), true));
+ function printActionName(): void {
+ print $this->_get_action_name(json_decode(clean($_REQUEST["action"] ?? ""), true));
}
- function editSave() {
- $filter_id = clean($_REQUEST["id"]);
+ function editSave(): void {
+ $filter_id = (int) clean($_REQUEST["id"]);
$enabled = checkbox_to_sql_bool(clean($_REQUEST["enabled"] ?? false));
$match_any_rule = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"] ?? false));
$inverse = checkbox_to_sql_bool(clean($_REQUEST["inverse"] ?? false));
@@ -510,7 +530,7 @@ class Pref_Filters extends Handler_Protected {
$this->pdo->commit();
}
- function remove() {
+ function remove(): void {
$ids = explode(",", clean($_REQUEST["ids"]));
$ids_qmarks = arr_qmarks($ids);
@@ -520,7 +540,7 @@ class Pref_Filters extends Handler_Protected {
$sth->execute(array_merge($ids, [$_SESSION['uid']]));
}
- private function _save_rules_and_actions($filter_id) {
+ private function _save_rules_and_actions(int $filter_id): void {
$sth = $this->pdo->prepare("DELETE FROM ttrss_filters2_rules WHERE filter_id = ?");
$sth->execute([$filter_id]);
@@ -583,21 +603,26 @@ class Pref_Filters extends Handler_Protected {
$action_param = $action["action_param"];
$action_param_label = $action["action_param_label"];
- if ($action_id == 7) {
+ if ($action_id == self::ACTION_LABEL) {
$action_param = $action_param_label;
}
- if ($action_id == 6) {
+ if ($action_id == self::ACTION_SCORE) {
$action_param = (int)str_replace("+", "", $action_param);
}
+ if (in_array($action_id, [self::ACTION_TAG, self::ACTION_REMOVE_TAG])) {
+ $action_param = implode(", ", FeedItem_Common::normalize_categories(
+ explode(",", $action_param)));
+ }
+
$asth->execute([$filter_id, $action_id, $action_param]);
}
}
}
}
- function add () {
+ function add(): void {
$enabled = checkbox_to_sql_bool(clean($_REQUEST["enabled"] ?? false));
$match_any_rule = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"] ?? false));
$title = clean($_REQUEST["title"]);
@@ -625,7 +650,7 @@ class Pref_Filters extends Handler_Protected {
$this->pdo->commit();
}
- function index() {
+ function index(): void {
if (array_key_exists("search", $_REQUEST)) {
$filter_search = clean($_REQUEST["search"]);
$_SESSION["prefs_filter_search"] = $filter_search;
@@ -691,7 +716,8 @@ class Pref_Filters extends Handler_Protected {
<?php
}
- function editrule() {
+ function editrule(): void {
+ /** @var array<int, int|string> */
$feed_ids = explode(",", clean($_REQUEST["ids"]));
print json_encode([
@@ -699,7 +725,10 @@ class Pref_Filters extends Handler_Protected {
]);
}
- private function _get_name($id) {
+ /**
+ * @return array<int, string>
+ */
+ private function _get_name(int $id): array {
$sth = $this->pdo->prepare(
"SELECT title,match_any_rule,f.inverse AS inverse,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
@@ -745,8 +774,9 @@ class Pref_Filters extends Handler_Protected {
return [];
}
- function join() {
- $ids = explode(",", clean($_REQUEST["ids"]));
+ function join(): void {
+ /** @var array<int, int> */
+ $ids = array_map("intval", explode(",", clean($_REQUEST["ids"])));
if (count($ids) > 1) {
$base_id = array_shift($ids);
@@ -775,7 +805,7 @@ class Pref_Filters extends Handler_Protected {
}
}
- private function _optimize($id) {
+ private function _optimize(int $id): void {
$this->pdo->beginTransaction();
@@ -830,9 +860,11 @@ class Pref_Filters extends Handler_Protected {
$this->pdo->commit();
}
- private function _feed_multi_select($id, $default_ids = [],
- $attributes = "", $include_all_feeds = true,
- $root_id = null, $nest_level = 0) {
+ /**
+ * @param array<int, int|string> $default_ids
+ */
+ private function _feed_multi_select(string $id, array $default_ids = [], string $attributes = "",
+ bool $include_all_feeds = true, ?int $root_id = null, int $nest_level = 0): string {
$pdo = Db::pdo();