]>
git.wh0rd.org - tt-rss.git/blob - classes/pref/filters.php
2 class Pref_Filters
extends Handler_Protected
{
4 function csrf_ignore($method) {
5 $csrf_ignored = array("index", "getfiltertree", "edit", "newfilter", "newrule",
6 "newaction", "savefilterorder");
8 return array_search($method, $csrf_ignored) !== false;
11 function filtersortreset() {
12 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2
13 SET order_id = 0 WHERE owner_uid = ?");
14 $sth->execute([$_SESSION['uid']]);
18 function savefilterorder() {
19 $data = json_decode($_POST['payload'], true);
21 #file_put_contents("/tmp/saveorder.json", $_POST['payload']);
22 #$data = json_decode(file_get_contents("/tmp/saveorder.json"), true);
24 if (!is_array($data['items']))
25 $data['items'] = json_decode($data['items'], true);
29 if (is_array($data) && is_array($data['items'])) {
31 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2 SET
32 order_id = ? WHERE id = ? AND
35 foreach ($data['items'][0]['items'] as $item) {
36 $filter_id = (int) str_replace("FILTER:", "", $item['_reference']);
39 $sth->execute([$index, $filter_id, $_SESSION['uid']]);
48 function testFilterDo() {
49 $offset = (int) $_REQUEST["offset"];
50 $limit = (int) $_REQUEST["limit"];
54 $filter["enabled"] = true;
55 $filter["match_any_rule"] = sql_bool_to_bool(
56 checkbox_to_sql_bool($_REQUEST["match_any_rule"]));
57 $filter["inverse"] = sql_bool_to_bool(
58 checkbox_to_sql_bool($_REQUEST["inverse"]));
60 $filter["rules"] = array();
61 $filter["actions"] = array("dummy-action");
63 $res = $this->pdo
->query("SELECT id,name FROM ttrss_filter_types");
65 $filter_types = array();
66 while ($line = $res->fetch()) {
67 $filter_types[$line["id"]] = $line["name"];
70 $scope_qparts = array();
73 foreach ($_REQUEST["rule"] AS $r) {
74 $rule = json_decode($r, true);
76 if ($rule && $rctr < 5) {
77 $rule["type"] = $filter_types[$rule["filter_type"]];
78 unset($rule["filter_type"]);
80 $scope_inner_qparts = [];
81 foreach ($rule["feed_id"] as $feed_id) {
83 if (strpos($feed_id, "CAT:") === 0) {
84 $cat_id = (int) substr($feed_id, 4);
85 array_push($scope_inner_qparts, "cat_id = " . $this->pdo
->quote($cat_id));
86 } else if ($feed_id > 0) {
87 array_push($scope_inner_qparts, "feed_id = " . $this->pdo
->quote($feed_id));
91 if (count($scope_inner_qparts) > 0) {
92 array_push($scope_qparts, "(" . implode(" OR ", $scope_inner_qparts) . ")");
95 array_push($filter["rules"], $rule);
103 if (count($scope_qparts) == 0) $scope_qparts = ["true"];
105 $glue = $filter['match_any_rule'] ?
" OR " : " AND ";
106 $scope_qpart = join($glue, $scope_qparts);
108 if (!$scope_qpart) $scope_qpart = "true";
112 //while ($found < $limit && $offset < $limit * 1000 && time() - $started < ini_get("max_execution_time") * 0.7) {
114 $sth = $this->pdo
->prepare("SELECT ttrss_entries.id,
116 ttrss_feeds.id AS feed_id,
117 ttrss_feeds.title AS feed_title,
118 ttrss_feed_categories.id AS cat_id,
125 ttrss_entries, ttrss_user_entries
126 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
127 LEFT JOIN ttrss_feed_categories ON (ttrss_feeds.cat_id = ttrss_feed_categories.id)
129 ref_id = ttrss_entries.id AND
131 ttrss_user_entries.owner_uid = ?
132 ORDER BY date_entered DESC LIMIT ?OFFSET ?");
134 $sth->execute([$_SESSION['uid'], $limit, $offset]);;
136 while ($line = $sth->fetch()) {
138 $rc = RSSUtils
::get_article_filters(array($filter), $line['title'], $line['content'], $line['link'],
139 $line['author'], explode(",", $line['tag_cache']));
141 if (count($rc) > 0) {
143 $line["content_preview"] = truncate_string(strip_tags($line["content"]), 200, '…');
145 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_QUERY_HEADLINES
) as $p) {
146 $line = $p->hook_query_headlines($line, 100);
149 $content_preview = $line["content_preview"];
151 $tmp = "<tr style='margin-top : 5px'>";
153 #$tmp .= "<td width='5%' align='center'><input dojoType=\"dijit.form.CheckBox\"
154 # checked=\"1\" disabled=\"1\" type=\"checkbox\"></td>";
157 $tmp .= "<td width='5%' align='center'><img style='cursor : pointer' title='".__("Preview article")."'
158 src='images/information.png' onclick='openArticlePopup($id)'></td><td>";
160 /*foreach ($filter['rules'] as $rule) {
161 $reg_exp = str_replace('/', '\/', $rule["reg_exp"]);
163 $line["title"] = preg_replace("/($reg_exp)/i",
164 "<span class=\"highlight\">$1</span>", $line["title"]);
166 $content_preview = preg_replace("/($reg_exp)/i",
167 "<span class=\"highlight\">$1</span>", $content_preview);
170 $tmp .= "<strong>" . $line["title"] . "</strong><br/>";
171 $tmp .= $line['feed_title'] . ", " . mb_substr($line["date_entered"], 0, 16);
172 $tmp .= "<div class='insensitive'>" . $content_preview . "</div>";
173 $tmp .= "</td></tr>";
175 array_push($rv, $tmp);
177 /*array_push($rv, array("title" => $line["title"],
178 "content" => $content_preview,
179 "date" => $line["date_entered"],
180 "feed" => $line["feed_title"])); */
189 print "<tr><td align='center'>" .
190 __("No recent articles matching this filter have been found.");
193 print json_encode($rv);
196 function testFilter() {
198 if (isset($_REQUEST["offset"])) return $this->testFilterDo();
200 //print __("Articles matching this filter:");
202 print "<div><img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'> <span id='prefFilterProgressMsg'>Looking for articles...</span></div>";
204 print "<br/><div class=\"filterTestHolder\">";
205 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefFilterTestResultList\">";
206 print "</table></div>";
208 print "<div style='text-align : center'>";
209 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
210 __('Close this window')."</button>";
215 private function getfilterrules_concise($filter_id) {
216 $sth = $this->pdo
->prepare("SELECT reg_exp,
222 ttrss_filter_types.description AS field
224 ttrss_filters2_rules, ttrss_filter_types
226 filter_id = ? AND filter_type = ttrss_filter_types.id
228 $sth->execute([$filter_id]);
232 while ($line = $sth->fetch()) {
234 if ($line["match_on"]) {
235 $feeds = json_decode($line["match_on"], true);
238 foreach ($feeds as $feed_id) {
240 if (strpos($feed_id, "CAT:") === 0) {
241 $feed_id = (int)substr($feed_id, 4);
242 array_push($feeds_fmt, Feeds
::getCategoryTitle($feed_id));
245 array_push($feeds_fmt, Feeds
::getFeedTitle((int)$feed_id));
247 array_push($feeds_fmt, __("All feeds"));
251 $where = implode(", ", $feeds_fmt);
255 $where = sql_bool_to_bool($line["cat_filter"]) ?
256 Feeds
::getCategoryTitle($line["cat_id"]) :
258 Feeds
::getFeedTitle($line["feed_id"]) : __("All feeds"));
261 # $where = $line["cat_id"] . "/" . $line["feed_id"];
263 $inverse = sql_bool_to_bool($line["inverse"]) ?
"inverse" : "";
265 $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
266 htmlspecialchars($line["reg_exp"]),
269 sql_bool_to_bool($line["inverse"]) ?
__("(inverse)") : "") . "</span>";
275 function getfiltertree() {
277 $root['id'] = 'root';
278 $root['name'] = __('Filters');
279 $root['items'] = array();
281 $filter_search = $_SESSION["prefs_filter_search"];
283 $sth = $this->pdo
->prepare("SELECT *,
284 (SELECT action_param FROM ttrss_filters2_actions
285 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
286 (SELECT action_id FROM ttrss_filters2_actions
287 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
288 (SELECT description FROM ttrss_filter_actions
289 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
290 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
291 (SELECT reg_exp FROM ttrss_filters2_rules
292 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
293 FROM ttrss_filters2 WHERE
294 owner_uid = ? ORDER BY order_id, title");
295 $sth->execute([$_SESSION['uid']]);
298 $folder['items'] = array();
300 while ($line = $sth->fetch()) {
302 $name = $this->getFilterName($line["id"]);
305 if ($filter_search) {
306 $rules_sth = $this->pdo
->prepare("SELECT reg_exp
307 FROM ttrss_filters2_rules WHERE filter_id = ?");
308 $rules_sth->execute([$line['id']]);
310 while ($rule_line = $rules_sth->fetch()) {
311 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
318 if ($line['action_id'] == 7) {
319 $label_sth = $this->pdo
->prepare("SELECT fg_color, bg_color
320 FROM ttrss_labels2 WHERE caption = ? AND
322 $label_sth->execute([$line['action_param'], $_SESSION['uid']]);
324 if ($label_row = $label_sth->fetch()) {
325 $fg_color = $label_row["fg_color"];
326 $bg_color = $label_row["bg_color"];
328 $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>α</span>" . $name[1];
333 $filter['id'] = 'FILTER:' . $line['id'];
334 $filter['bare_id'] = $line['id'];
335 $filter['name'] = $name[0];
336 $filter['param'] = $name[1];
337 $filter['checkbox'] = false;
338 $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
339 $filter['rules'] = $this->getfilterrules_concise($line['id']);
341 if (!$filter_search ||
$match_ok) {
342 array_push($folder['items'], $filter);
346 $root['items'] = $folder['items'];
349 $fl['identifier'] = 'id';
350 $fl['label'] = 'name';
351 $fl['items'] = array($root);
353 print json_encode($fl);
359 $filter_id = $_REQUEST["id"];
361 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2
362 WHERE id = ? AND owner_uid = ?");
363 $sth->execute([$filter_id, $_SESSION['uid']]);
365 if ($row = $sth->fetch()) {
367 $enabled = sql_bool_to_bool($row["enabled"]);
368 $match_any_rule = sql_bool_to_bool($row["match_any_rule"]);
369 $inverse = sql_bool_to_bool($row["inverse"]);
370 $title = htmlspecialchars($row["title"]);
372 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
374 print_hidden("op", "pref-filters");
375 print_hidden("id", "$filter_id");
376 print_hidden("method", "editSave");
377 print_hidden("csrf_token", $_SESSION['csrf_token']);
379 print "<div class=\"dlgSec\">".__("Caption")."</div>";
381 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
385 print "<div class=\"dlgSec\">".__("Match")."</div>";
387 print "<div dojoType=\"dijit.Toolbar\">";
389 print "<div dojoType=\"dijit.form.DropDownButton\">".
390 "<span>" . __('Select')."</span>";
391 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
392 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
393 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
394 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
395 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
396 print "</div></div>";
398 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
399 __('Add')."</button> ";
401 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
402 __('Delete')."</button> ";
406 print "<ul id='filterDlg_Matches'>";
408 $rules_sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_rules
409 WHERE filter_id = ? ORDER BY reg_exp, id");
410 $rules_sth->execute([$filter_id]);
412 while ($line = $rules_sth->fetch()) {
413 if ($line["match_on"]) {
414 $line["feed_id"] = json_decode($line["match_on"], true);
416 if (sql_bool_to_bool($line["cat_filter"])) {
417 $feed_id = "CAT:" . (int)$line["cat_id"];
419 $feed_id = (int)$line["feed_id"];
422 $line["feed_id"] = ["" . $feed_id]; // set item type to string for in_array()
425 unset($line["cat_filter"]);
426 unset($line["cat_id"]);
427 unset($line["filter_id"]);
429 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
430 unset($line["match_on"]);
432 $data = htmlspecialchars(json_encode($line));
434 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
435 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
436 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
443 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
445 print "<div dojoType=\"dijit.Toolbar\">";
447 print "<div dojoType=\"dijit.form.DropDownButton\">".
448 "<span>" . __('Select')."</span>";
449 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
450 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
451 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
452 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
453 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
454 print "</div></div>";
456 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
457 __('Add')."</button> ";
459 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
460 __('Delete')."</button> ";
464 print "<ul id='filterDlg_Actions'>";
466 $actions_sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_actions
467 WHERE filter_id = ? ORDER BY id");
468 $actions_sth->execute([$filter_id]);
470 while ($line = $actions_sth->fetch()) {
471 $line["action_param_label"] = $line["action_param"];
473 unset($line["filter_id"]);
476 $data = htmlspecialchars(json_encode($line));
478 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
479 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
480 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
488 $checked = "checked=\"1\"";
493 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
494 <label for=\"enabled\">".__('Enabled')."</label>";
496 if ($match_any_rule) {
497 $checked = "checked=\"1\"";
502 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
503 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
506 $checked = "checked=\"1\"";
511 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
512 <label for=\"inverse\">".__('Inverse matching')."</label>";
516 print "<div class=\"dlgButtons\">";
518 print "<div style=\"float : left\">";
519 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
520 __('Remove')."</button>";
523 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
524 __('Test')."</button> ";
526 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
527 __('Save')."</button> ";
529 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
530 __('Cancel')."</button>";
537 private function getRuleName($rule) {
538 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
540 $feeds = $rule["feed_id"];
543 if (!is_array($feeds)) $feeds = [$feeds];
545 foreach ($feeds as $feed_id) {
547 if (strpos($feed_id, "CAT:") === 0) {
548 $feed_id = (int)substr($feed_id, 4);
549 array_push($feeds_fmt, Feeds
::getCategoryTitle($feed_id));
552 array_push($feeds_fmt, Feeds
::getFeedTitle((int)$feed_id));
554 array_push($feeds_fmt, __("All feeds"));
558 $feed = implode(", ", $feeds_fmt);
560 $sth = $this->pdo
->prepare("SELECT description FROM ttrss_filter_types
562 $sth->execute([(int)$rule["filter_type"]]);
564 if ($row = $sth->fetch()) {
565 $filter_type = $row["description"];
567 $filter_type = "?UNKNOWN?";
570 $inverse = isset($rule["inverse"]) ?
"inverse" : "";
572 return "<span class='filterRule $inverse'>" .
573 T_sprintf("%s on %s in %s %s", htmlspecialchars($rule["reg_exp"]),
574 $filter_type, $feed, isset($rule["inverse"]) ?
__("(inverse)") : "") . "</span>";
577 function printRuleName() {
578 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
581 private function getActionName($action) {
582 $sth = $this->pdo
->prepare("SELECT description FROM
583 ttrss_filter_actions WHERE id = ?");
584 $sth->execute([(int)$action["action_id"]]);
588 if ($row = $sth->fetch()) {
590 $title = __($row["description"]);
592 if ($action["action_id"] == 4 ||
$action["action_id"] == 6 ||
593 $action["action_id"] == 7)
594 $title .= ": " . $action["action_param"];
596 if ($action["action_id"] == 9) {
597 list ($pfclass, $pfaction) = explode(":", $action["action_param"]);
599 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
601 foreach ($filter_actions as $fclass => $factions) {
602 foreach ($factions as $faction) {
603 if ($pfaction == $faction["action"] && $pfclass == $fclass) {
604 $title .= ": " . $fclass . ": " . $faction["description"];
615 function printActionName() {
616 print $this->getActionName(json_decode($_REQUEST["action"], true));
619 function editSave() {
620 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
621 return $this->testFilter();
624 $filter_id = $_REQUEST["id"];
625 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
626 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
627 $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
628 $title = $_REQUEST["title"];
630 $this->pdo
->beginTransaction();
632 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2 SET enabled = ?,
636 WHERE id = ? AND owner_uid = ?");
638 $sth->execute([$enabled, $match_any_rule, $inverse, $title, $filter_id, $_SESSION['uid']]);
640 $this->saveRulesAndActions($filter_id);
642 $this->pdo
->commit();
647 $ids = explode(",", $_REQUEST["ids"]);
648 $ids_qmarks = arr_qmarks($ids);
650 $sth = $this->pdo
->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)
652 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
655 private function saveRulesAndActions($filter_id)
658 $sth = $this->pdo
->prepare("DELETE FROM ttrss_filters2_rules WHERE filter_id = ?");
659 $sth->execute([$filter_id]);
661 $sth = $this->pdo
->prepare("DELETE FROM ttrss_filters2_actions WHERE filter_id = ?");
662 $sth->execute([$filter_id]);
664 if (!is_array($_REQUEST["rule"])) $_REQUEST["rule"] = [];
665 if (!is_array($_REQUEST["action"])) $_REQUEST["action"] = [];
673 foreach ($_REQUEST["rule"] as $rule) {
674 $rule = json_decode($rule, true);
677 if (array_search($rule, $rules) === false) {
678 array_push($rules, $rule);
682 foreach ($_REQUEST["action"] as $action) {
683 $action = json_decode($action, true);
684 unset($action["id"]);
686 if (array_search($action, $actions) === false) {
687 array_push($actions, $action);
691 $rsth = $this->pdo
->prepare("INSERT INTO ttrss_filters2_rules
692 (filter_id, reg_exp,filter_type,feed_id,cat_id,match_on,inverse) VALUES
693 (?, ?, ?, NULL, NULL, ?, ?)");
695 foreach ($rules as $rule) {
698 $reg_exp = trim($rule["reg_exp"]);
699 $inverse = isset($rule["inverse"]) ?
"true" : "false";
701 $filter_type = (int)trim($rule["filter_type"]);
702 $match_on = json_encode($rule["feed_id"]);
704 $rsth->execute([$filter_id, $reg_exp, $filter_type, $match_on, $inverse]);
708 $asth = $this->pdo
->prepare("INSERT INTO ttrss_filters2_actions
709 (filter_id, action_id, action_param) VALUES
712 foreach ($actions as $action) {
715 $action_id = (int)$action["action_id"];
716 $action_param = $action["action_param"];
717 $action_param_label = $action["action_param_label"];
719 if ($action_id == 7) {
720 $action_param = $action_param_label;
723 if ($action_id == 6) {
724 $action_param = (int)str_replace("+", "", $action_param);
727 $asth->execute([$filter_id, $action_id, $action_param]);
734 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
735 return $this->testFilter();
738 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
739 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
740 $title = $_REQUEST["title"];
741 $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
743 $this->pdo
->beginTransaction();
745 /* create base filter */
747 $sth = $this->pdo
->prepare("INSERT INTO ttrss_filters2
748 (owner_uid, match_any_rule, enabled, title, inverse) VALUES
751 $sth->execute([$_SESSION['uid'], $match_any_rule, $enabled, $title, $inverse]);
753 $sth = $this->pdo
->prepare("SELECT MAX(id) AS id FROM ttrss_filters2
754 WHERE owner_uid = ?");
755 $sth->execute([$_SESSION['uid']]);
757 if ($row = $sth->fetch()) {
758 $filter_id = $row['id'];
759 $this->saveRulesAndActions($filter_id);
762 $this->pdo
->commit();
767 $sort = $_REQUEST["sort"];
769 if (!$sort ||
$sort == "undefined") {
773 $filter_search = $_REQUEST["search"];
775 if (array_key_exists("search", $_REQUEST)) {
776 $_SESSION["prefs_filter_search"] = $filter_search;
778 $filter_search = $_SESSION["prefs_filter_search"];
781 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
782 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
783 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
785 $filter_search = $_REQUEST["search"];
787 if (array_key_exists("search", $_REQUEST)) {
788 $_SESSION["prefs_filter_search"] = $filter_search;
790 $filter_search = $_SESSION["prefs_filter_search"];
793 print "<div style='float : right; padding-right : 4px;'>
794 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
795 value=\"$filter_search\">
796 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
797 __('Search')."</button>
800 print "<div dojoType=\"dijit.form.DropDownButton\">".
801 "<span>" . __('Select')."</span>";
802 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
803 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
804 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
805 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
806 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
807 print "</div></div>";
809 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
810 __('Create filter')."</button> ";
812 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
813 __('Combine')."</button> ";
815 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
816 __('Edit')."</button> ";
818 print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
819 __('Reset sort order')."</button> ";
822 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
823 __('Remove')."</button> ";
825 if (defined('_ENABLE_FEED_DEBUGGING')) {
826 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
827 __('Rescore articles')."</button> ";
830 print "</div>"; # toolbar
831 print "</div>"; # toolbar-frame
832 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
834 print "<div id=\"filterlistLoading\">
835 <img src='images/indicator_tiny.gif'>".
836 __("Loading, please wait...")."</div>";
838 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
839 url=\"backend.php?op=pref-filters&method=getfiltertree\">
841 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
842 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
843 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
845 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
846 dndController=\"dijit.tree.dndSource\"
847 betweenThreshold=\"5\"
848 model=\"filterModel\" openOnClick=\"true\">
849 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
850 Element.hide(\"filterlistLoading\");
852 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
853 var id = String(item.id);
854 var bare_id = id.substr(id.indexOf(':')+1);
856 if (id.match('FILTER:')) {
863 print "</div>"; #pane
865 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB
,
866 "hook_prefs_tab", "prefFilters");
868 print "</div>"; #container
872 function newfilter() {
874 print "<form name='filter_new_form' id='filter_new_form'>";
876 print_hidden("op", "pref-filters");
877 print_hidden("method", "add");
878 print_hidden("csrf_token", $_SESSION['csrf_token']);
880 print "<div class=\"dlgSec\">".__("Caption")."</div>";
882 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
884 print "<div class=\"dlgSec\">".__("Match")."</div>";
886 print "<div dojoType=\"dijit.Toolbar\">";
888 print "<div dojoType=\"dijit.form.DropDownButton\">".
889 "<span>" . __('Select')."</span>";
890 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
891 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
892 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
893 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
894 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
895 print "</div></div>";
897 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
898 __('Add')."</button> ";
900 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
901 __('Delete')."</button> ";
905 print "<ul id='filterDlg_Matches'>";
906 # print "<li>No rules</li>";
911 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
913 print "<div dojoType=\"dijit.Toolbar\">";
915 print "<div dojoType=\"dijit.form.DropDownButton\">".
916 "<span>" . __('Select')."</span>";
917 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
918 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
919 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
920 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
921 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
922 print "</div></div>";
924 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
925 __('Add')."</button> ";
927 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
928 __('Delete')."</button> ";
932 print "<ul id='filterDlg_Actions'>";
933 # print "<li>No actions</li>";
936 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
937 print "<div class=\"dlgSecCont\">"; */
939 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
940 <label for=\"enabled\">".__('Enabled')."</label>";
942 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
943 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
945 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
946 <label for=\"inverse\">".__('Inverse matching')."</label>";
950 print "<div class=\"dlgButtons\">";
952 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
953 __('Test')."</button> ";
955 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
956 __('Create')."</button> ";
958 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
959 __('Cancel')."</button>";
966 $rule = json_decode($_REQUEST["rule"], true);
969 $reg_exp = htmlspecialchars($rule["reg_exp"]);
970 $filter_type = $rule["filter_type"];
971 $feed_id = $rule["feed_id"];
972 $inverse_checked = isset($rule["inverse"]) ?
"checked" : "";
977 $inverse_checked = "";
980 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
982 $res = $this->pdo
->query("SELECT id,description
983 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
985 $filter_types = array();
987 while ($line = $res->fetch()) {
988 $filter_types[$line["id"]] = __($line["description"]);
991 print "<div class=\"dlgSec\">".__("Match")."</div>";
993 print "<div class=\"dlgSecCont\">";
995 print "<input dojoType=\"dijit.form.ValidationTextBox\"
996 required=\"true\" id=\"filterDlg_regExp\"
997 style=\"font-size : 16px; width : 20em;\"
998 name=\"reg_exp\" value=\"$reg_exp\"/>";
1001 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
1002 name=\"inverse\" $inverse_checked/>";
1003 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
1005 print "<hr/>" . __("on field") . " ";
1006 print_select_hash("filter_type", $filter_type, $filter_types,
1007 'dojoType="dijit.form.Select"');
1011 print __("in") . " ";
1013 print "<span id='filterDlg_feeds'>";
1014 print_feed_multi_select("feed_id",
1016 'dojoType="dijit.form.MultiSelect"');
1021 print "<div class=\"dlgButtons\">";
1023 print "<div style=\"float : left\">
1024 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
1028 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
1029 ($rule ?
__("Save rule") : __('Add rule'))."</button> ";
1031 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
1032 __('Cancel')."</button>";
1039 function newaction() {
1040 $action = json_decode($_REQUEST["action"], true);
1043 $action_param = $action["action_param"];
1044 $action_id = (int)$action["action_id"];
1050 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
1052 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
1054 print "<div class=\"dlgSecCont\">";
1056 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
1057 onchange=\"filterDlgCheckAction(this)\">";
1059 $res = $this->pdo
->query("SELECT id,description FROM ttrss_filter_actions
1062 while ($line = $res->fetch()) {
1063 $is_selected = ($line["id"] == $action_id) ?
"selected='1'" : "";
1064 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
1069 $param_box_hidden = ($action_id == 7 ||
$action_id == 4 ||
$action_id == 6 ||
$action_id == 9) ?
1070 "" : "display : none";
1072 $param_hidden = ($action_id == 4 ||
$action_id == 6) ?
1073 "" : "display : none";
1075 $label_param_hidden = ($action_id == 7) ?
"" : "display : none";
1076 $plugin_param_hidden = ($action_id == 9) ?
"" : "display : none";
1078 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
1080 //print " " . __("with parameters:") . " ";
1081 print "<input dojoType=\"dijit.form.TextBox\"
1082 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
1083 name=\"action_param\" value=\"$action_param\">";
1085 print_label_select("action_param_label", $action_param,
1086 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
1087 dojoType=\"dijit.form.Select\"");
1089 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
1090 $filter_action_hash = array();
1092 foreach ($filter_actions as $fclass => $factions) {
1093 foreach ($factions as $faction) {
1095 $filter_action_hash[$fclass . ":" . $faction["action"]] =
1096 $fclass . ": " . $faction["description"];
1100 if (count($filter_action_hash) == 0) {
1101 $filter_plugin_disabled = "disabled";
1103 $filter_action_hash["no-data"] = __("No actions available");
1106 $filter_plugin_disabled = "";
1109 print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash,
1110 "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled",
1111 "action_param_plugin");
1115 print " "; // tiny layout hack
1119 print "<div class=\"dlgButtons\">";
1121 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
1122 ($action ?
__("Save action") : __('Add action'))."</button> ";
1124 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
1125 __('Cancel')."</button>";
1132 private function getFilterName($id) {
1134 $sth = $this->pdo
->prepare(
1135 "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
1136 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
1137 ON (r.filter_id = f.id)
1138 LEFT JOIN ttrss_filters2_actions AS a
1139 ON (a.filter_id = f.id) WHERE f.id = ? GROUP BY f.title, f.match_any_rule");
1140 $sth->execute([$id]);
1142 if ($row = $sth->fetch()) {
1144 $title = $row["title"];
1145 $num_rules = $row["num_rules"];
1146 $num_actions = $row["num_actions"];
1147 $match_any_rule = sql_bool_to_bool($row["match_any_rule"]);
1149 if (!$title) $title = __("[No caption]");
1151 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", (int) $num_rules), $title, $num_rules);
1153 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_actions
1154 WHERE filter_id = ? ORDER BY id LIMIT 1");
1155 $sth->execute([$id]);
1159 if ($line = $sth->fetch()) {
1160 $actions = $this->getActionName($line);
1165 if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
1167 if ($num_actions > 0)
1168 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", (int) $num_actions), $actions, $num_actions);
1170 return [$title, $actions];
1177 $ids = explode(",", $_REQUEST["ids"]);
1179 if (count($ids) > 1) {
1180 $base_id = array_shift($ids);
1181 $ids_qmarks = arr_qmarks($ids);
1183 $this->pdo
->beginTransaction();
1185 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2_rules
1186 SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
1187 $sth->execute(array_merge([$base_id], $ids));
1189 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2_actions
1190 SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
1191 $sth->execute(array_merge([$base_id], $ids));
1193 $sth = $this->pdo
->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)");
1194 $sth->execute($ids);
1196 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = ?");
1197 $sth->execute([$base_id]);
1199 $this->pdo
->commit();
1201 $this->optimizeFilter($base_id);
1206 private function optimizeFilter($id) {
1208 $this->pdo
->beginTransaction();
1210 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_actions
1211 WHERE filter_id = ?");
1212 $sth->execute([$id]);
1215 $dupe_ids = array();
1217 while ($line = $sth->fetch()) {
1221 if (array_search($line, $tmp) === false) {
1222 array_push($tmp, $line);
1224 array_push($dupe_ids, $id);
1228 if (count($dupe_ids) > 0) {
1229 $ids_str = join(",", $dupe_ids);
1231 $this->pdo
->query("DELETE FROM ttrss_filters2_actions WHERE id IN ($ids_str)");
1234 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_rules
1235 WHERE filter_id = ?");
1236 $sth->execute([$id]);
1239 $dupe_ids = array();
1241 while ($line = $sth->fetch()) {
1245 if (array_search($line, $tmp) === false) {
1246 array_push($tmp, $line);
1248 array_push($dupe_ids, $id);
1252 if (count($dupe_ids) > 0) {
1253 $ids_str = join(",", $dupe_ids);
1255 $this->pdo
->query("DELETE FROM ttrss_filters2_rules WHERE id IN ($ids_str)");
1258 $this->pdo
->commit();