]>
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", clean($_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) clean($_REQUEST["offset"]);
50 $limit = (int) clean($_REQUEST["limit"]);
54 $filter["enabled"] = true;
55 $filter["match_any_rule"] = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"]));
56 $filter["inverse"] = checkbox_to_sql_bool(clean($_REQUEST["inverse"]));
58 $filter["rules"] = array();
59 $filter["actions"] = array("dummy-action");
61 $res = $this->pdo
->query("SELECT id,name FROM ttrss_filter_types");
63 $filter_types = array();
64 while ($line = $res->fetch()) {
65 $filter_types[$line["id"]] = $line["name"];
68 $scope_qparts = array();
71 foreach (clean($_REQUEST["rule"]) AS $r) {
72 $rule = json_decode($r, true);
74 if ($rule && $rctr < 5) {
75 $rule["type"] = $filter_types[$rule["filter_type"]];
76 unset($rule["filter_type"]);
78 $scope_inner_qparts = [];
79 foreach ($rule["feed_id"] as $feed_id) {
81 if (strpos($feed_id, "CAT:") === 0) {
82 $cat_id = (int) substr($feed_id, 4);
83 array_push($scope_inner_qparts, "cat_id = " . $this->pdo
->quote($cat_id));
84 } else if ($feed_id > 0) {
85 array_push($scope_inner_qparts, "feed_id = " . $this->pdo
->quote($feed_id));
89 if (count($scope_inner_qparts) > 0) {
90 array_push($scope_qparts, "(" . implode(" OR ", $scope_inner_qparts) . ")");
93 array_push($filter["rules"], $rule);
101 if (count($scope_qparts) == 0) $scope_qparts = ["true"];
103 $glue = $filter['match_any_rule'] ?
" OR " : " AND ";
104 $scope_qpart = join($glue, $scope_qparts);
106 if (!$scope_qpart) $scope_qpart = "true";
110 //while ($found < $limit && $offset < $limit * 1000 && time() - $started < ini_get("max_execution_time") * 0.7) {
112 $sth = $this->pdo
->prepare("SELECT ttrss_entries.id,
114 ttrss_feeds.id AS feed_id,
115 ttrss_feeds.title AS feed_title,
116 ttrss_feed_categories.id AS cat_id,
123 ttrss_entries, ttrss_user_entries
124 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
125 LEFT JOIN ttrss_feed_categories ON (ttrss_feeds.cat_id = ttrss_feed_categories.id)
127 ref_id = ttrss_entries.id AND
129 ttrss_user_entries.owner_uid = ?
130 ORDER BY date_entered DESC LIMIT $limit OFFSET $offset");
132 $sth->execute([$_SESSION['uid']]);
134 while ($line = $sth->fetch()) {
136 $rc = RSSUtils
::get_article_filters(array($filter), $line['title'], $line['content'], $line['link'],
137 $line['author'], explode(",", $line['tag_cache']));
139 if (count($rc) > 0) {
141 $line["content_preview"] = truncate_string(strip_tags($line["content"]), 200, '…');
143 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_QUERY_HEADLINES
) as $p) {
144 $line = $p->hook_query_headlines($line, 100);
147 $content_preview = $line["content_preview"];
149 $tmp = "<tr style='margin-top : 5px'>";
151 #$tmp .= "<td width='5%' align='center'><input dojoType=\"dijit.form.CheckBox\"
152 # checked=\"1\" disabled=\"1\" type=\"checkbox\"></td>";
155 $tmp .= "<td width='5%' align='center'><img style='cursor : pointer' title='".__("Preview article")."'
156 src='images/information.png' onclick='openArticlePopup($id)'></td><td>";
158 /*foreach ($filter['rules'] as $rule) {
159 $reg_exp = str_replace('/', '\/', $rule["reg_exp"]);
161 $line["title"] = preg_replace("/($reg_exp)/i",
162 "<span class=\"highlight\">$1</span>", $line["title"]);
164 $content_preview = preg_replace("/($reg_exp)/i",
165 "<span class=\"highlight\">$1</span>", $content_preview);
168 $tmp .= "<strong>" . $line["title"] . "</strong><br/>";
169 $tmp .= $line['feed_title'] . ", " . mb_substr($line["date_entered"], 0, 16);
170 $tmp .= "<div class='insensitive'>" . $content_preview . "</div>";
171 $tmp .= "</td></tr>";
173 array_push($rv, $tmp);
175 /*array_push($rv, array("title" => $line["title"],
176 "content" => $content_preview,
177 "date" => $line["date_entered"],
178 "feed" => $line["feed_title"])); */
187 print "<tr><td align='center'>" .
188 __("No recent articles matching this filter have been found.");
191 print json_encode($rv);
194 function testFilter() {
196 if (isset($_REQUEST["offset"])) return $this->testFilterDo();
198 //print __("Articles matching this filter:");
200 print "<div><img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'> <span id='prefFilterProgressMsg'>Looking for articles...</span></div>";
202 print "<br/><div class=\"filterTestHolder\">";
203 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefFilterTestResultList\">";
204 print "</table></div>";
206 print "<div style='text-align : center'>";
207 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
208 __('Close this window')."</button>";
213 private function getfilterrules_concise($filter_id) {
214 $sth = $this->pdo
->prepare("SELECT reg_exp,
220 ttrss_filter_types.description AS field
222 ttrss_filters2_rules, ttrss_filter_types
224 filter_id = ? AND filter_type = ttrss_filter_types.id
226 $sth->execute([$filter_id]);
230 while ($line = $sth->fetch()) {
232 if ($line["match_on"]) {
233 $feeds = json_decode($line["match_on"], true);
236 foreach ($feeds as $feed_id) {
238 if (strpos($feed_id, "CAT:") === 0) {
239 $feed_id = (int)substr($feed_id, 4);
240 array_push($feeds_fmt, Feeds
::getCategoryTitle($feed_id));
243 array_push($feeds_fmt, Feeds
::getFeedTitle((int)$feed_id));
245 array_push($feeds_fmt, __("All feeds"));
249 $where = implode(", ", $feeds_fmt);
253 $where = $line["cat_filter"] ?
254 Feeds
::getCategoryTitle($line["cat_id"]) :
256 Feeds
::getFeedTitle($line["feed_id"]) : __("All feeds"));
259 # $where = $line["cat_id"] . "/" . $line["feed_id"];
261 $inverse = $line["inverse"] ?
"inverse" : "";
263 $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
264 htmlspecialchars($line["reg_exp"]),
267 $line["inverse"] ?
__("(inverse)") : "") . "</span>";
273 function getfiltertree() {
275 $root['id'] = 'root';
276 $root['name'] = __('Filters');
277 $root['items'] = array();
279 $filter_search = $_SESSION["prefs_filter_search"];
281 $sth = $this->pdo
->prepare("SELECT *,
282 (SELECT action_param FROM ttrss_filters2_actions
283 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
284 (SELECT action_id FROM ttrss_filters2_actions
285 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
286 (SELECT description FROM ttrss_filter_actions
287 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
288 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
289 (SELECT reg_exp FROM ttrss_filters2_rules
290 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
291 FROM ttrss_filters2 WHERE
292 owner_uid = ? ORDER BY order_id, title");
293 $sth->execute([$_SESSION['uid']]);
296 $folder['items'] = array();
298 while ($line = $sth->fetch()) {
300 $name = $this->getFilterName($line["id"]);
303 if ($filter_search) {
304 $rules_sth = $this->pdo
->prepare("SELECT reg_exp
305 FROM ttrss_filters2_rules WHERE filter_id = ?");
306 $rules_sth->execute([$line['id']]);
308 while ($rule_line = $rules_sth->fetch()) {
309 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
316 if ($line['action_id'] == 7) {
317 $label_sth = $this->pdo
->prepare("SELECT fg_color, bg_color
318 FROM ttrss_labels2 WHERE caption = ? AND
320 $label_sth->execute([$line['action_param'], $_SESSION['uid']]);
322 if ($label_row = $label_sth->fetch()) {
323 $fg_color = $label_row["fg_color"];
324 $bg_color = $label_row["bg_color"];
326 $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>α</span>" . $name[1];
331 $filter['id'] = 'FILTER:' . $line['id'];
332 $filter['bare_id'] = $line['id'];
333 $filter['name'] = $name[0];
334 $filter['param'] = $name[1];
335 $filter['checkbox'] = false;
336 $filter['enabled'] = $line["enabled"];
337 $filter['rules'] = $this->getfilterrules_concise($line['id']);
339 if (!$filter_search ||
$match_ok) {
340 array_push($folder['items'], $filter);
344 $root['items'] = $folder['items'];
347 $fl['identifier'] = 'id';
348 $fl['label'] = 'name';
349 $fl['items'] = array($root);
351 print json_encode($fl);
357 $filter_id = clean($_REQUEST["id"]);
359 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2
360 WHERE id = ? AND owner_uid = ?");
361 $sth->execute([$filter_id, $_SESSION['uid']]);
363 if ($row = $sth->fetch()) {
365 $enabled = $row["enabled"];
366 $match_any_rule = $row["match_any_rule"];
367 $inverse = $row["inverse"];
368 $title = htmlspecialchars($row["title"]);
370 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
372 print_hidden("op", "pref-filters");
373 print_hidden("id", "$filter_id");
374 print_hidden("method", "editSave");
375 print_hidden("csrf_token", $_SESSION['csrf_token']);
377 print "<div class=\"dlgSec\">".__("Caption")."</div>";
379 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
383 print "<div class=\"dlgSec\">".__("Match")."</div>";
385 print "<div dojoType=\"dijit.Toolbar\">";
387 print "<div dojoType=\"dijit.form.DropDownButton\">".
388 "<span>" . __('Select')."</span>";
389 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
390 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
391 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
392 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
393 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
394 print "</div></div>";
396 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
397 __('Add')."</button> ";
399 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
400 __('Delete')."</button> ";
404 print "<ul id='filterDlg_Matches'>";
406 $rules_sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_rules
407 WHERE filter_id = ? ORDER BY reg_exp, id");
408 $rules_sth->execute([$filter_id]);
410 while ($line = $rules_sth->fetch()) {
411 if ($line["match_on"]) {
412 $line["feed_id"] = json_decode($line["match_on"], true);
414 if ($line["cat_filter"]) {
415 $feed_id = "CAT:" . (int)$line["cat_id"];
417 $feed_id = (int)$line["feed_id"];
420 $line["feed_id"] = ["" . $feed_id]; // set item type to string for in_array()
423 unset($line["cat_filter"]);
424 unset($line["cat_id"]);
425 unset($line["filter_id"]);
427 if (!$line["inverse"]) unset($line["inverse"]);
428 unset($line["match_on"]);
430 $data = htmlspecialchars(json_encode($line));
432 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
433 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
434 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
441 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
443 print "<div dojoType=\"dijit.Toolbar\">";
445 print "<div dojoType=\"dijit.form.DropDownButton\">".
446 "<span>" . __('Select')."</span>";
447 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
448 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
449 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
450 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
451 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
452 print "</div></div>";
454 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
455 __('Add')."</button> ";
457 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
458 __('Delete')."</button> ";
462 print "<ul id='filterDlg_Actions'>";
464 $actions_sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_actions
465 WHERE filter_id = ? ORDER BY id");
466 $actions_sth->execute([$filter_id]);
468 while ($line = $actions_sth->fetch()) {
469 $line["action_param_label"] = $line["action_param"];
471 unset($line["filter_id"]);
474 $data = htmlspecialchars(json_encode($line));
476 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
477 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
478 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
486 $checked = "checked=\"1\"";
491 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
492 <label for=\"enabled\">".__('Enabled')."</label>";
494 if ($match_any_rule) {
495 $checked = "checked=\"1\"";
500 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
501 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
504 $checked = "checked=\"1\"";
509 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
510 <label for=\"inverse\">".__('Inverse matching')."</label>";
514 print "<div class=\"dlgButtons\">";
516 print "<div style=\"float : left\">";
517 print "<button dojoType=\"dijit.form.Button\" class=\"btn-danger\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
518 __('Remove')."</button>";
521 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
522 __('Test')."</button> ";
524 print "<button dojoType=\"dijit.form.Button\" type=\"submit\" class=\"btn-primary\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
525 __('Save')."</button> ";
527 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
528 __('Cancel')."</button>";
536 private function getRuleName($rule) {
537 if (!$rule) $rule = json_decode(clean($_REQUEST["rule"]), true);
539 $feeds = $rule["feed_id"];
542 if (!is_array($feeds)) $feeds = [$feeds];
544 foreach ($feeds as $feed_id) {
546 if (strpos($feed_id, "CAT:") === 0) {
547 $feed_id = (int)substr($feed_id, 4);
548 array_push($feeds_fmt, Feeds
::getCategoryTitle($feed_id));
551 array_push($feeds_fmt, Feeds
::getFeedTitle((int)$feed_id));
553 array_push($feeds_fmt, __("All feeds"));
557 $feed = implode(", ", $feeds_fmt);
559 $sth = $this->pdo
->prepare("SELECT description FROM ttrss_filter_types
561 $sth->execute([(int)$rule["filter_type"]]);
563 if ($row = $sth->fetch()) {
564 $filter_type = $row["description"];
566 $filter_type = "?UNKNOWN?";
569 $inverse = isset($rule["inverse"]) ?
"inverse" : "";
571 return "<span class='filterRule $inverse'>" .
572 T_sprintf("%s on %s in %s %s", htmlspecialchars($rule["reg_exp"]),
573 $filter_type, $feed, isset($rule["inverse"]) ?
__("(inverse)") : "") . "</span>";
576 function printRuleName() {
577 print $this->getRuleName(json_decode(clean($_REQUEST["rule"]), true));
580 private function getActionName($action) {
581 $sth = $this->pdo
->prepare("SELECT description FROM
582 ttrss_filter_actions WHERE id = ?");
583 $sth->execute([(int)$action["action_id"]]);
587 if ($row = $sth->fetch()) {
589 $title = __($row["description"]);
591 if ($action["action_id"] == 4 ||
$action["action_id"] == 6 ||
592 $action["action_id"] == 7)
593 $title .= ": " . $action["action_param"];
595 if ($action["action_id"] == 9) {
596 list ($pfclass, $pfaction) = explode(":", $action["action_param"]);
598 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
600 foreach ($filter_actions as $fclass => $factions) {
601 foreach ($factions as $faction) {
602 if ($pfaction == $faction["action"] && $pfclass == $fclass) {
603 $title .= ": " . $fclass . ": " . $faction["description"];
614 function printActionName() {
615 print $this->getActionName(json_decode(clean($_REQUEST["action"]), true));
618 function editSave() {
619 if (clean($_REQUEST["savemode"] && $_REQUEST["savemode"]) == "test") {
620 return $this->testFilter();
623 $filter_id = clean($_REQUEST["id"]);
624 $enabled = checkbox_to_sql_bool(clean($_REQUEST["enabled"]));
625 $match_any_rule = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"]));
626 $inverse = checkbox_to_sql_bool(clean($_REQUEST["inverse"]));
627 $title = clean($_REQUEST["title"]);
629 $this->pdo
->beginTransaction();
631 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2 SET enabled = ?,
635 WHERE id = ? AND owner_uid = ?");
637 $sth->execute([$enabled, $match_any_rule, $inverse, $title, $filter_id, $_SESSION['uid']]);
639 $this->saveRulesAndActions($filter_id);
641 $this->pdo
->commit();
646 $ids = explode(",", clean($_REQUEST["ids"]));
647 $ids_qmarks = arr_qmarks($ids);
649 $sth = $this->pdo
->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)
651 $sth->execute(array_merge($ids, [$_SESSION['uid']]));
654 private function saveRulesAndActions($filter_id)
657 $sth = $this->pdo
->prepare("DELETE FROM ttrss_filters2_rules WHERE filter_id = ?");
658 $sth->execute([$filter_id]);
660 $sth = $this->pdo
->prepare("DELETE FROM ttrss_filters2_actions WHERE filter_id = ?");
661 $sth->execute([$filter_id]);
663 if (!is_array(clean($_REQUEST["rule"]))) $_REQUEST["rule"] = [];
664 if (!is_array(clean($_REQUEST["action"]))) $_REQUEST["action"] = [];
672 foreach (clean($_REQUEST["rule"]) as $rule) {
673 $rule = json_decode($rule, true);
676 if (array_search($rule, $rules) === false) {
677 array_push($rules, $rule);
681 foreach (clean($_REQUEST["action"]) as $action) {
682 $action = json_decode($action, true);
683 unset($action["id"]);
685 if (array_search($action, $actions) === false) {
686 array_push($actions, $action);
690 $rsth = $this->pdo
->prepare("INSERT INTO ttrss_filters2_rules
691 (filter_id, reg_exp,filter_type,feed_id,cat_id,match_on,inverse) VALUES
692 (?, ?, ?, NULL, NULL, ?, ?)");
694 foreach ($rules as $rule) {
697 $reg_exp = trim($rule["reg_exp"]);
698 $inverse = isset($rule["inverse"]) ?
1 : 0;
700 $filter_type = (int)trim($rule["filter_type"]);
701 $match_on = json_encode($rule["feed_id"]);
703 $rsth->execute([$filter_id, $reg_exp, $filter_type, $match_on, $inverse]);
707 $asth = $this->pdo
->prepare("INSERT INTO ttrss_filters2_actions
708 (filter_id, action_id, action_param) VALUES
711 foreach ($actions as $action) {
714 $action_id = (int)$action["action_id"];
715 $action_param = $action["action_param"];
716 $action_param_label = $action["action_param_label"];
718 if ($action_id == 7) {
719 $action_param = $action_param_label;
722 if ($action_id == 6) {
723 $action_param = (int)str_replace("+", "", $action_param);
726 $asth->execute([$filter_id, $action_id, $action_param]);
733 if (clean($_REQUEST["savemode"] && $_REQUEST["savemode"]) == "test") {
734 return $this->testFilter();
737 $enabled = checkbox_to_sql_bool(clean($_REQUEST["enabled"]));
738 $match_any_rule = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"]));
739 $title = clean($_REQUEST["title"]);
740 $inverse = checkbox_to_sql_bool(clean($_REQUEST["inverse"]));
742 $this->pdo
->beginTransaction();
744 /* create base filter */
746 $sth = $this->pdo
->prepare("INSERT INTO ttrss_filters2
747 (owner_uid, match_any_rule, enabled, title, inverse) VALUES
750 $sth->execute([$_SESSION['uid'], $match_any_rule, $enabled, $title, $inverse]);
752 $sth = $this->pdo
->prepare("SELECT MAX(id) AS id FROM ttrss_filters2
753 WHERE owner_uid = ?");
754 $sth->execute([$_SESSION['uid']]);
756 if ($row = $sth->fetch()) {
757 $filter_id = $row['id'];
758 $this->saveRulesAndActions($filter_id);
761 $this->pdo
->commit();
766 $filter_search = clean($_REQUEST["search"]);
768 if (array_key_exists("search", $_REQUEST)) {
769 $_SESSION["prefs_filter_search"] = $filter_search;
771 $filter_search = $_SESSION["prefs_filter_search"];
774 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
775 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
776 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
778 if (array_key_exists("search", $_REQUEST)) {
779 $_SESSION["prefs_filter_search"] = $filter_search;
781 $filter_search = $_SESSION["prefs_filter_search"];
784 print "<div style='float : right; padding-right : 4px;'>
785 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
786 value=\"$filter_search\">
787 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
788 __('Search')."</button>
791 print "<div dojoType=\"dijit.form.DropDownButton\">".
792 "<span>" . __('Select')."</span>";
793 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
794 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
795 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
796 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
797 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
798 print "</div></div>";
800 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
801 __('Create filter')."</button> ";
803 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
804 __('Combine')."</button> ";
806 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
807 __('Edit')."</button> ";
809 print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
810 __('Reset sort order')."</button> ";
813 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
814 __('Remove')."</button> ";
816 print "</div>"; # toolbar
817 print "</div>"; # toolbar-frame
818 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
820 print "<div id=\"filterlistLoading\">
821 <img src='images/indicator_tiny.gif'>".
822 __("Loading, please wait...")."</div>";
824 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
825 url=\"backend.php?op=pref-filters&method=getfiltertree\">
827 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
828 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
829 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
831 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
832 dndController=\"dijit.tree.dndSource\"
833 betweenThreshold=\"5\"
834 model=\"filterModel\" openOnClick=\"true\">
835 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
836 Element.hide(\"filterlistLoading\");
838 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
839 var id = String(item.id);
840 var bare_id = id.substr(id.indexOf(':')+1);
842 if (id.match('FILTER:')) {
849 print "</div>"; #pane
851 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB
,
852 "hook_prefs_tab", "prefFilters");
854 print "</div>"; #container
858 function newfilter() {
860 print "<form name='filter_new_form' id='filter_new_form'>";
862 print_hidden("op", "pref-filters");
863 print_hidden("method", "add");
864 print_hidden("csrf_token", $_SESSION['csrf_token']);
866 print "<div class=\"dlgSec\">".__("Caption")."</div>";
868 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
870 print "<div class=\"dlgSec\">".__("Match")."</div>";
872 print "<div dojoType=\"dijit.Toolbar\">";
874 print "<div dojoType=\"dijit.form.DropDownButton\">".
875 "<span>" . __('Select')."</span>";
876 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
877 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
878 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
879 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
880 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
881 print "</div></div>";
883 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
884 __('Add')."</button> ";
886 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
887 __('Delete')."</button> ";
891 print "<ul id='filterDlg_Matches'>";
892 # print "<li>No rules</li>";
897 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
899 print "<div dojoType=\"dijit.Toolbar\">";
901 print "<div dojoType=\"dijit.form.DropDownButton\">".
902 "<span>" . __('Select')."</span>";
903 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
904 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
905 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
906 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
907 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
908 print "</div></div>";
910 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
911 __('Add')."</button> ";
913 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
914 __('Delete')."</button> ";
918 print "<ul id='filterDlg_Actions'>";
919 # print "<li>No actions</li>";
922 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
923 print "<div class=\"dlgSecCont\">"; */
925 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
926 <label for=\"enabled\">".__('Enabled')."</label>";
928 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
929 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
931 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
932 <label for=\"inverse\">".__('Inverse matching')."</label>";
936 print "<div class=\"dlgButtons\">";
938 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
939 __('Test')."</button> ";
941 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
942 __('Create')."</button> ";
944 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
945 __('Cancel')."</button>";
952 $rule = json_decode(clean($_REQUEST["rule"]), true);
955 $reg_exp = htmlspecialchars($rule["reg_exp"]);
956 $filter_type = $rule["filter_type"];
957 $feed_id = $rule["feed_id"];
958 $inverse_checked = isset($rule["inverse"]) ?
"checked" : "";
963 $inverse_checked = "";
966 print "<form name='filter_new_rule_form' id='filter_new_rule_form' onsubmit='return false;'>";
968 $res = $this->pdo
->query("SELECT id,description
969 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
971 $filter_types = array();
973 while ($line = $res->fetch()) {
974 $filter_types[$line["id"]] = __($line["description"]);
977 print "<div class=\"dlgSec\">".__("Match")."</div>";
979 print "<div class=\"dlgSecCont\">";
981 print "<input dojoType=\"dijit.form.ValidationTextBox\"
982 required=\"true\" id=\"filterDlg_regExp\"
983 style=\"font-size : 16px; width : 20em;\"
984 name=\"reg_exp\" value=\"$reg_exp\"/>";
987 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
988 name=\"inverse\" $inverse_checked/>";
989 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
991 print "<hr/>" . __("on field") . " ";
992 print_select_hash("filter_type", $filter_type, $filter_types,
993 'dojoType="dijit.form.Select"');
997 print __("in") . " ";
999 print "<span id='filterDlg_feeds'>";
1000 print_feed_multi_select("feed_id",
1002 'dojoType="dijit.form.MultiSelect"');
1007 print "<div class=\"dlgButtons\">";
1009 print "<div style=\"float : left\">
1010 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
1014 print "<button dojoType=\"dijit.form.Button\" class=\"btn-primary \" type=\"submit\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
1015 ($rule ?
__("Save rule") : __('Add rule'))."</button> ";
1017 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
1018 __('Cancel')."</button>";
1025 function newaction() {
1026 $action = json_decode(clean($_REQUEST["action"]), true);
1029 $action_param = $action["action_param"];
1030 $action_id = (int)$action["action_id"];
1036 print "<form name='filter_new_action_form' id='filter_new_action_form' onsubmit='return false;'>";
1038 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
1040 print "<div class=\"dlgSecCont\">";
1042 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
1043 onchange=\"filterDlgCheckAction(this)\">";
1045 $res = $this->pdo
->query("SELECT id,description FROM ttrss_filter_actions
1048 while ($line = $res->fetch()) {
1049 $is_selected = ($line["id"] == $action_id) ?
"selected='1'" : "";
1050 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
1055 $param_box_hidden = ($action_id == 7 ||
$action_id == 4 ||
$action_id == 6 ||
$action_id == 9) ?
1056 "" : "display : none";
1058 $param_hidden = ($action_id == 4 ||
$action_id == 6) ?
1059 "" : "display : none";
1061 $label_param_hidden = ($action_id == 7) ?
"" : "display : none";
1062 $plugin_param_hidden = ($action_id == 9) ?
"" : "display : none";
1064 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
1066 //print " " . __("with parameters:") . " ";
1067 print "<input dojoType=\"dijit.form.TextBox\"
1068 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
1069 name=\"action_param\" value=\"$action_param\">";
1071 print_label_select("action_param_label", $action_param,
1072 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
1073 dojoType=\"dijit.form.Select\"");
1075 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
1076 $filter_action_hash = array();
1078 foreach ($filter_actions as $fclass => $factions) {
1079 foreach ($factions as $faction) {
1081 $filter_action_hash[$fclass . ":" . $faction["action"]] =
1082 $fclass . ": " . $faction["description"];
1086 if (count($filter_action_hash) == 0) {
1087 $filter_plugin_disabled = "disabled";
1089 $filter_action_hash["no-data"] = __("No actions available");
1092 $filter_plugin_disabled = "";
1095 print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash,
1096 "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled",
1097 "action_param_plugin");
1101 print " "; // tiny layout hack
1105 print "<div class=\"dlgButtons\">";
1107 print "<button dojoType=\"dijit.form.Button\" class=\"btn-primary\" type=\"submit\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
1108 ($action ?
__("Save action") : __('Add action'))."</button> ";
1110 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
1111 __('Cancel')."</button>";
1118 private function getFilterName($id) {
1120 $sth = $this->pdo
->prepare(
1121 "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
1122 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
1123 ON (r.filter_id = f.id)
1124 LEFT JOIN ttrss_filters2_actions AS a
1125 ON (a.filter_id = f.id) WHERE f.id = ? GROUP BY f.title, f.match_any_rule");
1126 $sth->execute([$id]);
1128 if ($row = $sth->fetch()) {
1130 $title = $row["title"];
1131 $num_rules = $row["num_rules"];
1132 $num_actions = $row["num_actions"];
1133 $match_any_rule = $row["match_any_rule"];
1135 if (!$title) $title = __("[No caption]");
1137 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", (int) $num_rules), $title, $num_rules);
1139 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_actions
1140 WHERE filter_id = ? ORDER BY id LIMIT 1");
1141 $sth->execute([$id]);
1145 if ($line = $sth->fetch()) {
1146 $actions = $this->getActionName($line);
1151 if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
1153 if ($num_actions > 0)
1154 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", (int) $num_actions), $actions, $num_actions);
1156 return [$title, $actions];
1163 $ids = explode(",", clean($_REQUEST["ids"]));
1165 if (count($ids) > 1) {
1166 $base_id = array_shift($ids);
1167 $ids_qmarks = arr_qmarks($ids);
1169 $this->pdo
->beginTransaction();
1171 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2_rules
1172 SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
1173 $sth->execute(array_merge([$base_id], $ids));
1175 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2_actions
1176 SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
1177 $sth->execute(array_merge([$base_id], $ids));
1179 $sth = $this->pdo
->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)");
1180 $sth->execute($ids);
1182 $sth = $this->pdo
->prepare("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = ?");
1183 $sth->execute([$base_id]);
1185 $this->pdo
->commit();
1187 $this->optimizeFilter($base_id);
1192 private function optimizeFilter($id) {
1194 $this->pdo
->beginTransaction();
1196 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_actions
1197 WHERE filter_id = ?");
1198 $sth->execute([$id]);
1201 $dupe_ids = array();
1203 while ($line = $sth->fetch()) {
1207 if (array_search($line, $tmp) === false) {
1208 array_push($tmp, $line);
1210 array_push($dupe_ids, $id);
1214 if (count($dupe_ids) > 0) {
1215 $ids_str = join(",", $dupe_ids);
1217 $this->pdo
->query("DELETE FROM ttrss_filters2_actions WHERE id IN ($ids_str)");
1220 $sth = $this->pdo
->prepare("SELECT * FROM ttrss_filters2_rules
1221 WHERE filter_id = ?");
1222 $sth->execute([$id]);
1225 $dupe_ids = array();
1227 while ($line = $sth->fetch()) {
1231 if (array_search($line, $tmp) === false) {
1232 array_push($tmp, $line);
1234 array_push($dupe_ids, $id);
1238 if (count($dupe_ids) > 0) {
1239 $ids_str = join(",", $dupe_ids);
1241 $this->pdo
->query("DELETE FROM ttrss_filters2_rules WHERE id IN ($ids_str)");
1244 $this->pdo
->commit();