]>
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 $limit OFFSET $offset");
134 $sth->execute([$_SESSION['uid']]);
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"]) ?
1 : 0;
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 print "</div>"; # toolbar
826 print "</div>"; # toolbar-frame
827 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
829 print "<div id=\"filterlistLoading\">
830 <img src='images/indicator_tiny.gif'>".
831 __("Loading, please wait...")."</div>";
833 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
834 url=\"backend.php?op=pref-filters&method=getfiltertree\">
836 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
837 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
838 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
840 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
841 dndController=\"dijit.tree.dndSource\"
842 betweenThreshold=\"5\"
843 model=\"filterModel\" openOnClick=\"true\">
844 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
845 Element.hide(\"filterlistLoading\");
847 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
848 var id = String(item.id);
849 var bare_id = id.substr(id.indexOf(':')+1);
851 if (id.match('FILTER:')) {
858 print "</div>"; #pane
860 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB
,
861 "hook_prefs_tab", "prefFilters");
863 print "</div>"; #container
867 function newfilter() {
869 print "<form name='filter_new_form' id='filter_new_form'>";
871 print_hidden("op", "pref-filters");
872 print_hidden("method", "add");
873 print_hidden("csrf_token", $_SESSION['csrf_token']);
875 print "<div class=\"dlgSec\">".__("Caption")."</div>";
877 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
879 print "<div class=\"dlgSec\">".__("Match")."</div>";
881 print "<div dojoType=\"dijit.Toolbar\">";
883 print "<div dojoType=\"dijit.form.DropDownButton\">".
884 "<span>" . __('Select')."</span>";
885 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
886 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
887 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
888 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
889 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
890 print "</div></div>";
892 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
893 __('Add')."</button> ";
895 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
896 __('Delete')."</button> ";
900 print "<ul id='filterDlg_Matches'>";
901 # print "<li>No rules</li>";
906 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
908 print "<div dojoType=\"dijit.Toolbar\">";
910 print "<div dojoType=\"dijit.form.DropDownButton\">".
911 "<span>" . __('Select')."</span>";
912 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
913 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
914 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
915 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
916 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
917 print "</div></div>";
919 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
920 __('Add')."</button> ";
922 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
923 __('Delete')."</button> ";
927 print "<ul id='filterDlg_Actions'>";
928 # print "<li>No actions</li>";
931 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
932 print "<div class=\"dlgSecCont\">"; */
934 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
935 <label for=\"enabled\">".__('Enabled')."</label>";
937 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
938 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
940 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
941 <label for=\"inverse\">".__('Inverse matching')."</label>";
945 print "<div class=\"dlgButtons\">";
947 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
948 __('Test')."</button> ";
950 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
951 __('Create')."</button> ";
953 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
954 __('Cancel')."</button>";
961 $rule = json_decode($_REQUEST["rule"], true);
964 $reg_exp = htmlspecialchars($rule["reg_exp"]);
965 $filter_type = $rule["filter_type"];
966 $feed_id = $rule["feed_id"];
967 $inverse_checked = isset($rule["inverse"]) ?
"checked" : "";
972 $inverse_checked = "";
975 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
977 $res = $this->pdo
->query("SELECT id,description
978 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
980 $filter_types = array();
982 while ($line = $res->fetch()) {
983 $filter_types[$line["id"]] = __($line["description"]);
986 print "<div class=\"dlgSec\">".__("Match")."</div>";
988 print "<div class=\"dlgSecCont\">";
990 print "<input dojoType=\"dijit.form.ValidationTextBox\"
991 required=\"true\" id=\"filterDlg_regExp\"
992 style=\"font-size : 16px; width : 20em;\"
993 name=\"reg_exp\" value=\"$reg_exp\"/>";
996 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
997 name=\"inverse\" $inverse_checked/>";
998 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
1000 print "<hr/>" . __("on field") . " ";
1001 print_select_hash("filter_type", $filter_type, $filter_types,
1002 'dojoType="dijit.form.Select"');
1006 print __("in") . " ";
1008 print "<span id='filterDlg_feeds'>";
1009 print_feed_multi_select("feed_id",
1011 'dojoType="dijit.form.MultiSelect"');
1016 print "<div class=\"dlgButtons\">";
1018 print "<div style=\"float : left\">
1019 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
1023 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
1024 ($rule ?
__("Save rule") : __('Add rule'))."</button> ";
1026 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
1027 __('Cancel')."</button>";
1034 function newaction() {
1035 $action = json_decode($_REQUEST["action"], true);
1038 $action_param = $action["action_param"];
1039 $action_id = (int)$action["action_id"];
1045 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
1047 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
1049 print "<div class=\"dlgSecCont\">";
1051 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
1052 onchange=\"filterDlgCheckAction(this)\">";
1054 $res = $this->pdo
->query("SELECT id,description FROM ttrss_filter_actions
1057 while ($line = $res->fetch()) {
1058 $is_selected = ($line["id"] == $action_id) ?
"selected='1'" : "";
1059 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
1064 $param_box_hidden = ($action_id == 7 ||
$action_id == 4 ||
$action_id == 6 ||
$action_id == 9) ?
1065 "" : "display : none";
1067 $param_hidden = ($action_id == 4 ||
$action_id == 6) ?
1068 "" : "display : none";
1070 $label_param_hidden = ($action_id == 7) ?
"" : "display : none";
1071 $plugin_param_hidden = ($action_id == 9) ?
"" : "display : none";
1073 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
1075 //print " " . __("with parameters:") . " ";
1076 print "<input dojoType=\"dijit.form.TextBox\"
1077 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
1078 name=\"action_param\" value=\"$action_param\">";
1080 print_label_select("action_param_label", $action_param,
1081 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
1082 dojoType=\"dijit.form.Select\"");
1084 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
1085 $filter_action_hash = array();
1087 foreach ($filter_actions as $fclass => $factions) {
1088 foreach ($factions as $faction) {
1090 $filter_action_hash[$fclass . ":" . $faction["action"]] =
1091 $fclass . ": " . $faction["description"];
1095 if (count($filter_action_hash) == 0) {
1096 $filter_plugin_disabled = "disabled";
1098 $filter_action_hash["no-data"] = __("No actions available");
1101 $filter_plugin_disabled = "";
1104 print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash,
1105 "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled",
1106 "action_param_plugin");
1110 print " "; // tiny layout hack
1114 print "<div class=\"dlgButtons\">";
1116 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
1117 ($action ?
__("Save action") : __('Add action'))."</button> ";
1119 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
1120 __('Cancel')."</button>";
1127 private function getFilterName($id) {
1129 $sth = $this->pdo
->prepare(
1130 "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
1131 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
1132 ON (r.filter_id = f.id)
1133 LEFT JOIN ttrss_filters2_actions AS a
1134 ON (a.filter_id = f.id) WHERE f.id = ? GROUP BY f.title, f.match_any_rule");
1135 $sth->execute([$id]);
1137 if ($row = $sth->fetch()) {
1139 $title = $row["title"];
1140 $num_rules = $row["num_rules"];
1141 $num_actions = $row["num_actions"];
1142 $match_any_rule = sql_bool_to_bool($row["match_any_rule"]);
1144 if (!$title) $title = __("[No caption]");
1146 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", (int) $num_rules), $title, $num_rules);
1148 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_actions
1149 WHERE filter_id = ? ORDER BY id LIMIT 1");
1150 $sth->execute([$id]);
1154 if ($line = $sth->fetch()) {
1155 $actions = $this->getActionName($line);
1160 if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
1162 if ($num_actions > 0)
1163 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", (int) $num_actions), $actions, $num_actions);
1165 return [$title, $actions];
1172 $ids = explode(",", $_REQUEST["ids"]);
1174 if (count($ids) > 1) {
1175 $base_id = array_shift($ids);
1176 $ids_qmarks = arr_qmarks($ids);
1178 $this->pdo
->beginTransaction();
1180 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2_rules
1181 SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
1182 $sth->execute(array_merge([$base_id], $ids));
1184 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2_actions
1185 SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
1186 $sth->execute(array_merge([$base_id], $ids));
1188 $sth = $this->pdo
->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)");
1189 $sth->execute($ids);
1191 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = ?");
1192 $sth->execute([$base_id]);
1194 $this->pdo
->commit();
1196 $this->optimizeFilter($base_id);
1201 private function optimizeFilter($id) {
1203 $this->pdo
->beginTransaction();
1205 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_actions
1206 WHERE filter_id = ?");
1207 $sth->execute([$id]);
1210 $dupe_ids = array();
1212 while ($line = $sth->fetch()) {
1216 if (array_search($line, $tmp) === false) {
1217 array_push($tmp, $line);
1219 array_push($dupe_ids, $id);
1223 if (count($dupe_ids) > 0) {
1224 $ids_str = join(",", $dupe_ids);
1226 $this->pdo
->query("DELETE FROM ttrss_filters2_actions WHERE id IN ($ids_str)");
1229 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_rules
1230 WHERE filter_id = ?");
1231 $sth->execute([$id]);
1234 $dupe_ids = array();
1236 while ($line = $sth->fetch()) {
1240 if (array_search($line, $tmp) === false) {
1241 array_push($tmp, $line);
1243 array_push($dupe_ids, $id);
1247 if (count($dupe_ids) > 0) {
1248 $ids_str = join(",", $dupe_ids);
1250 $this->pdo
->query("DELETE FROM ttrss_filters2_rules WHERE id IN ($ids_str)");
1253 $this->pdo
->commit();