]>
git.wh0rd.org - tt-rss.git/blob - classes/pref/filters.php
6ea233cb6c22933f6643af54e6f9971e1323e9aa
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 $this->dbh
->query("UPDATE ttrss_filters2
13 SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
17 function savefilterorder() {
18 $data = json_decode($_POST['payload'], true);
20 #file_put_contents("/tmp/saveorder.json", $_POST['payload']);
21 #$data = json_decode(file_get_contents("/tmp/saveorder.json"), true);
23 if (!is_array($data['items']))
24 $data['items'] = json_decode($data['items'], true);
28 if (is_array($data) && is_array($data['items'])) {
29 foreach ($data['items'][0]['items'] as $item) {
30 $filter_id = (int) str_replace("FILTER:", "", $item['_reference']);
34 $this->dbh
->query("UPDATE ttrss_filters2 SET
35 order_id = $index WHERE id = '$filter_id' AND
36 owner_uid = " .$_SESSION["uid"]);
46 function testFilterDo() {
47 $offset = (int) db_escape_string($_REQUEST["offset"]);
48 $limit = (int) db_escape_string($_REQUEST["limit"]);
52 $filter["enabled"] = true;
53 $filter["match_any_rule"] = sql_bool_to_bool(
54 checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["match_any_rule"])));
55 $filter["inverse"] = sql_bool_to_bool(
56 checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["inverse"])));
58 $filter["rules"] = array();
59 $filter["actions"] = array("dummy-action");
61 $result = $this->dbh
->query("SELECT id,name FROM ttrss_filter_types");
63 $filter_types = array();
64 while ($line = $this->dbh
->fetch_assoc($result)) {
65 $filter_types[$line["id"]] = $line["name"];
68 $scope_qparts = array();
71 foreach ($_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 = " . $cat_id);
84 } else if ($feed_id > 0) {
85 array_push($scope_inner_qparts, "feed_id = " . $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 $result = db_query("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 = " . $_SESSION["uid"] . "
130 ORDER BY date_entered DESC LIMIT $limit OFFSET $offset");
132 while ($line = db_fetch_assoc($result)) {
134 $rc = RSSUtils
::get_article_filters(array($filter), $line['title'], $line['content'], $line['link'],
135 $line['author'], explode(",", $line['tag_cache']));
137 if (count($rc) > 0) {
139 $line["content_preview"] = truncate_string(strip_tags($line["content"]), 200, '…');
141 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_QUERY_HEADLINES
) as $p) {
142 $line = $p->hook_query_headlines($line, 100);
145 $content_preview = $line["content_preview"];
147 $tmp = "<tr style='margin-top : 5px'>";
149 #$tmp .= "<td width='5%' align='center'><input dojoType=\"dijit.form.CheckBox\"
150 # checked=\"1\" disabled=\"1\" type=\"checkbox\"></td>";
153 $tmp .= "<td width='5%' align='center'><img style='cursor : pointer' title='".__("Preview article")."'
154 src='images/information.png' onclick='openArticlePopup($id)'></td><td>";
156 /*foreach ($filter['rules'] as $rule) {
157 $reg_exp = str_replace('/', '\/', $rule["reg_exp"]);
159 $line["title"] = preg_replace("/($reg_exp)/i",
160 "<span class=\"highlight\">$1</span>", $line["title"]);
162 $content_preview = preg_replace("/($reg_exp)/i",
163 "<span class=\"highlight\">$1</span>", $content_preview);
166 $tmp .= "<strong>" . $line["title"] . "</strong><br/>";
167 $tmp .= $line['feed_title'] . ", " . mb_substr($line["date_entered"], 0, 16);
168 $tmp .= "<div class='insensitive'>" . $content_preview . "</div>";
169 $tmp .= "</td></tr>";
171 array_push($rv, $tmp);
173 /*array_push($rv, array("title" => $line["title"],
174 "content" => $content_preview,
175 "date" => $line["date_entered"],
176 "feed" => $line["feed_title"])); */
185 print "<tr><td align='center'>" .
186 __("No recent articles matching this filter have been found.");
189 print json_encode($rv);
192 function testFilter() {
194 if (isset($_REQUEST["offset"])) return $this->testFilterDo();
196 //print __("Articles matching this filter:");
198 print "<div><img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'> <span id='prefFilterProgressMsg'>Looking for articles...</span></div>";
200 print "<br/><div class=\"filterTestHolder\">";
201 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefFilterTestResultList\">";
202 print "</table></div>";
204 print "<div style='text-align : center'>";
205 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
206 __('Close this window')."</button>";
211 private function getfilterrules_concise($filter_id) {
212 $result = $this->dbh
->query("SELECT reg_exp,
218 ttrss_filter_types.description AS field
220 ttrss_filters2_rules, ttrss_filter_types
222 filter_id = '$filter_id' AND filter_type = ttrss_filter_types.id
227 while ($line = $this->dbh
->fetch_assoc($result)) {
229 if ($line["match_on"]) {
230 $feeds = json_decode($line["match_on"], true);
233 foreach ($feeds as $feed_id) {
235 if (strpos($feed_id, "CAT:") === 0) {
236 $feed_id = (int)substr($feed_id, 4);
237 array_push($feeds_fmt, Feeds
::getCategoryTitle($feed_id));
240 array_push($feeds_fmt, Feeds
::getFeedTitle((int)$feed_id));
242 array_push($feeds_fmt, __("All feeds"));
246 $where = implode(", ", $feeds_fmt);
250 $where = sql_bool_to_bool($line["cat_filter"]) ?
251 Feeds
::getCategoryTitle($line["cat_id"]) :
253 Feeds
::getFeedTitle($line["feed_id"]) : __("All feeds"));
256 # $where = $line["cat_id"] . "/" . $line["feed_id"];
258 $inverse = sql_bool_to_bool($line["inverse"]) ?
"inverse" : "";
260 $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
261 htmlspecialchars($line["reg_exp"]),
264 sql_bool_to_bool($line["inverse"]) ?
__("(inverse)") : "") . "</span>";
270 function getfiltertree() {
272 $root['id'] = 'root';
273 $root['name'] = __('Filters');
274 $root['items'] = array();
276 $filter_search = $_SESSION["prefs_filter_search"];
278 $result = $this->dbh
->query("SELECT *,
279 (SELECT action_param FROM ttrss_filters2_actions
280 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
281 (SELECT action_id FROM ttrss_filters2_actions
282 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
283 (SELECT description FROM ttrss_filter_actions
284 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
285 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
286 (SELECT reg_exp FROM ttrss_filters2_rules
287 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
288 FROM ttrss_filters2 WHERE
289 owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
293 $folder['items'] = array();
295 while ($line = $this->dbh
->fetch_assoc($result)) {
297 $name = $this->getFilterName($line["id"]);
300 if ($filter_search) {
301 $rules_result = $this->dbh
->query(
302 "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
304 while ($rule_line = $this->dbh
->fetch_assoc($rules_result)) {
305 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
312 if ($line['action_id'] == 7) {
313 $label_result = $this->dbh
->query("SELECT fg_color, bg_color
314 FROM ttrss_labels2 WHERE caption = '".$this->dbh
->escape_string($line['action_param'])."' AND
315 owner_uid = " . $_SESSION["uid"]);
317 if ($this->dbh
->num_rows($label_result) > 0) {
318 $fg_color = $this->dbh
->fetch_result($label_result, 0, "fg_color");
319 $bg_color = $this->dbh
->fetch_result($label_result, 0, "bg_color");
321 $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>α</span>" . $name[1];
326 $filter['id'] = 'FILTER:' . $line['id'];
327 $filter['bare_id'] = $line['id'];
328 $filter['name'] = $name[0];
329 $filter['param'] = $name[1];
330 $filter['checkbox'] = false;
331 $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
332 $filter['rules'] = $this->getfilterrules_concise($line['id']);
334 if (!$filter_search ||
$match_ok) {
335 array_push($folder['items'], $filter);
339 /* if (count($folder['items']) > 0) {
340 array_push($root['items'], $folder);
343 $root['items'] = $folder['items'];
346 $fl['identifier'] = 'id';
347 $fl['label'] = 'name';
348 $fl['items'] = array($root);
350 print json_encode($fl);
356 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
358 $result = $this->dbh
->query(
359 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
361 $enabled = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "enabled"));
362 $match_any_rule = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "match_any_rule"));
363 $inverse = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "inverse"));
364 $title = htmlspecialchars($this->dbh
->fetch_result($result, 0, "title"));
366 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
368 print_hidden("op", "pref-filters");
369 print_hidden("id", "$filter_id");
370 print_hidden("method", "editSave");
371 print_hidden("csrf_token", $_SESSION['csrf_token']);
373 print "<div class=\"dlgSec\">".__("Caption")."</div>";
375 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
379 print "<div class=\"dlgSec\">".__("Match")."</div>";
381 print "<div dojoType=\"dijit.Toolbar\">";
383 print "<div dojoType=\"dijit.form.DropDownButton\">".
384 "<span>" . __('Select')."</span>";
385 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
386 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
387 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
388 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
389 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
390 print "</div></div>";
392 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
393 __('Add')."</button> ";
395 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
396 __('Delete')."</button> ";
400 print "<ul id='filterDlg_Matches'>";
402 $rules_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
403 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
405 while ($line = $this->dbh
->fetch_assoc($rules_result)) {
406 if ($line["match_on"]) {
407 $line["feed_id"] = json_decode($line["match_on"], true);
409 if (sql_bool_to_bool($line["cat_filter"])) {
410 $feed_id = "CAT:" . (int)$line["cat_id"];
412 $feed_id = (int)$line["feed_id"];
415 $line["feed_id"] = ["" . $feed_id]; // set item type to string for in_array()
418 unset($line["cat_filter"]);
419 unset($line["cat_id"]);
420 unset($line["filter_id"]);
422 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
423 unset($line["match_on"]);
425 $data = htmlspecialchars(json_encode($line));
427 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
428 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
429 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
436 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
438 print "<div dojoType=\"dijit.Toolbar\">";
440 print "<div dojoType=\"dijit.form.DropDownButton\">".
441 "<span>" . __('Select')."</span>";
442 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
443 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
444 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
445 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
446 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
447 print "</div></div>";
449 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
450 __('Add')."</button> ";
452 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
453 __('Delete')."</button> ";
457 print "<ul id='filterDlg_Actions'>";
459 $actions_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
460 WHERE filter_id = '$filter_id' ORDER BY id");
462 while ($line = $this->dbh
->fetch_assoc($actions_result)) {
463 $line["action_param_label"] = $line["action_param"];
465 unset($line["filter_id"]);
468 $data = htmlspecialchars(json_encode($line));
470 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
471 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
472 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
480 $checked = "checked=\"1\"";
485 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
486 <label for=\"enabled\">".__('Enabled')."</label>";
488 if ($match_any_rule) {
489 $checked = "checked=\"1\"";
494 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
495 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
498 $checked = "checked=\"1\"";
503 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
504 <label for=\"inverse\">".__('Inverse matching')."</label>";
508 print "<div class=\"dlgButtons\">";
510 print "<div style=\"float : left\">";
511 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
512 __('Remove')."</button>";
515 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
516 __('Test')."</button> ";
518 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
519 __('Save')."</button> ";
521 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
522 __('Cancel')."</button>";
527 private function getRuleName($rule) {
528 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
530 $feeds = $rule["feed_id"];
533 if (!is_array($feeds)) $feeds = [$feeds];
535 foreach ($feeds as $feed_id) {
537 if (strpos($feed_id, "CAT:") === 0) {
538 $feed_id = (int)substr($feed_id, 4);
539 array_push($feeds_fmt, Feeds
::getCategoryTitle($feed_id));
542 array_push($feeds_fmt, Feeds
::getFeedTitle((int)$feed_id));
544 array_push($feeds_fmt, __("All feeds"));
548 $feed = implode(", ", $feeds_fmt);
550 $result = $this->dbh
->query("SELECT description FROM ttrss_filter_types
551 WHERE id = ".(int)$rule["filter_type"]);
552 $filter_type = $this->dbh
->fetch_result($result, 0, "description");
554 $inverse = isset($rule["inverse"]) ?
"inverse" : "";
556 return "<span class='filterRule $inverse'>" .
557 T_sprintf("%s on %s in %s %s", htmlspecialchars($rule["reg_exp"]),
558 $filter_type, $feed, isset($rule["inverse"]) ?
__("(inverse)") : "") . "</span>";
561 function printRuleName() {
562 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
565 private function getActionName($action) {
566 $result = $this->dbh
->query("SELECT description FROM
567 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
569 $title = __($this->dbh
->fetch_result($result, 0, "description"));
571 if ($action["action_id"] == 4 ||
$action["action_id"] == 6 ||
572 $action["action_id"] == 7)
573 $title .= ": " . $action["action_param"];
575 if ($action["action_id"] == 9) {
576 list ($pfclass, $pfaction) = explode(":", $action["action_param"]);
578 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
580 foreach ($filter_actions as $fclass => $factions) {
581 foreach ($factions as $faction) {
582 if ($pfaction == $faction["action"] && $pfclass == $fclass) {
583 $title .= ": " . $fclass . ": " . $faction["description"];
593 function printActionName() {
594 print $this->getActionName(json_decode($_REQUEST["action"], true));
597 function editSave() {
598 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
599 return $this->testFilter();
602 # print_r($_REQUEST);
604 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
605 $enabled = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["enabled"]));
606 $match_any_rule = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["match_any_rule"]));
607 $inverse = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["inverse"]));
608 $title = $this->dbh
->escape_string($_REQUEST["title"]);
610 $this->dbh
->query("UPDATE ttrss_filters2 SET enabled = $enabled,
611 match_any_rule = $match_any_rule,
614 WHERE id = '$filter_id'
615 AND owner_uid = ". $_SESSION["uid"]);
617 $this->saveRulesAndActions($filter_id);
623 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
625 foreach ($ids as $id) {
626 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
630 private function saveRulesAndActions($filter_id) {
632 $this->dbh
->query("DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
633 $this->dbh
->query("DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
641 foreach ($_REQUEST["rule"] as $rule) {
642 $rule = json_decode($rule, true);
645 if (array_search($rule, $rules) === false) {
646 array_push($rules, $rule);
650 foreach ($_REQUEST["action"] as $action) {
651 $action = json_decode($action, true);
652 unset($action["id"]);
654 if (array_search($action, $actions) === false) {
655 array_push($actions, $action);
659 foreach ($rules as $rule) {
662 $reg_exp = $this->dbh
->escape_string(trim($rule["reg_exp"]), false);
663 $inverse = isset($rule["inverse"]) ?
"true" : "false";
665 $filter_type = (int) $this->dbh
->escape_string(trim($rule["filter_type"]));
666 $match_on = $this->dbh
->escape_string(json_encode($rule["feed_id"]));
668 /*if (strpos($feed_id, "CAT:") === 0) {
670 $cat_filter = bool_to_sql_bool(true);
671 $cat_id = (int) substr($feed_id, 4);
674 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
676 $cat_filter = bool_to_sql_bool(false);
677 $feed_id = (int) $feed_id;
680 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
683 $query = "INSERT INTO ttrss_filters2_rules
684 (filter_id, reg_exp,filter_type,feed_id,cat_id,match_on,inverse) VALUES
685 ('$filter_id', '$reg_exp', '$filter_type', NULL, NULL, '$match_on', $inverse)";
687 $this->dbh
->query($query);
691 foreach ($actions as $action) {
694 $action_id = (int) $this->dbh
->escape_string($action["action_id"]);
695 $action_param = $this->dbh
->escape_string($action["action_param"]);
696 $action_param_label = $this->dbh
->escape_string($action["action_param_label"]);
698 if ($action_id == 7) {
699 $action_param = $action_param_label;
702 if ($action_id == 6) {
703 $action_param = (int) str_replace("+", "", $action_param);
706 $query = "INSERT INTO ttrss_filters2_actions
707 (filter_id, action_id, action_param) VALUES
708 ('$filter_id', '$action_id', '$action_param')";
710 $this->dbh
->query($query);
719 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
720 return $this->testFilter();
723 # print_r($_REQUEST);
725 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
726 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
727 $title = $this->dbh
->escape_string($_REQUEST["title"]);
728 $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
730 $this->dbh
->query("BEGIN");
732 /* create base filter */
734 $result = $this->dbh
->query("INSERT INTO ttrss_filters2
735 (owner_uid, match_any_rule, enabled, title, inverse) VALUES
736 (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title', $inverse)");
738 $result = $this->dbh
->query("SELECT MAX(id) AS id FROM ttrss_filters2
739 WHERE owner_uid = ".$_SESSION["uid"]);
741 $filter_id = $this->dbh
->fetch_result($result, 0, "id");
743 $this->saveRulesAndActions($filter_id);
745 $this->dbh
->query("COMMIT");
750 $sort = $this->dbh
->escape_string($_REQUEST["sort"]);
752 if (!$sort ||
$sort == "undefined") {
756 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
758 if (array_key_exists("search", $_REQUEST)) {
759 $_SESSION["prefs_filter_search"] = $filter_search;
761 $filter_search = $_SESSION["prefs_filter_search"];
764 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
765 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
766 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
768 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
770 if (array_key_exists("search", $_REQUEST)) {
771 $_SESSION["prefs_filter_search"] = $filter_search;
773 $filter_search = $_SESSION["prefs_filter_search"];
776 print "<div style='float : right; padding-right : 4px;'>
777 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
778 value=\"$filter_search\">
779 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
780 __('Search')."</button>
783 print "<div dojoType=\"dijit.form.DropDownButton\">".
784 "<span>" . __('Select')."</span>";
785 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
786 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
787 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
788 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
789 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
790 print "</div></div>";
792 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
793 __('Create filter')."</button> ";
795 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
796 __('Combine')."</button> ";
798 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
799 __('Edit')."</button> ";
801 print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
802 __('Reset sort order')."</button> ";
805 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
806 __('Remove')."</button> ";
808 if (defined('_ENABLE_FEED_DEBUGGING')) {
809 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
810 __('Rescore articles')."</button> ";
813 print "</div>"; # toolbar
814 print "</div>"; # toolbar-frame
815 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
817 print "<div id=\"filterlistLoading\">
818 <img src='images/indicator_tiny.gif'>".
819 __("Loading, please wait...")."</div>";
821 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
822 url=\"backend.php?op=pref-filters&method=getfiltertree\">
824 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
825 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
826 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
828 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
829 dndController=\"dijit.tree.dndSource\"
830 betweenThreshold=\"5\"
831 model=\"filterModel\" openOnClick=\"true\">
832 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
833 Element.hide(\"filterlistLoading\");
835 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
836 var id = String(item.id);
837 var bare_id = id.substr(id.indexOf(':')+1);
839 if (id.match('FILTER:')) {
846 print "</div>"; #pane
848 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB
,
849 "hook_prefs_tab", "prefFilters");
851 print "</div>"; #container
855 function newfilter() {
857 print "<form name='filter_new_form' id='filter_new_form'>";
859 print_hidden("op", "pref-filters");
860 print_hidden("method", "add");
861 print_hidden("csrf_token", $_SESSION['csrf_token']);
863 print "<div class=\"dlgSec\">".__("Caption")."</div>";
865 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
867 print "<div class=\"dlgSec\">".__("Match")."</div>";
869 print "<div dojoType=\"dijit.Toolbar\">";
871 print "<div dojoType=\"dijit.form.DropDownButton\">".
872 "<span>" . __('Select')."</span>";
873 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
874 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
875 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
876 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
877 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
878 print "</div></div>";
880 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
881 __('Add')."</button> ";
883 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
884 __('Delete')."</button> ";
888 print "<ul id='filterDlg_Matches'>";
889 # print "<li>No rules</li>";
894 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
896 print "<div dojoType=\"dijit.Toolbar\">";
898 print "<div dojoType=\"dijit.form.DropDownButton\">".
899 "<span>" . __('Select')."</span>";
900 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
901 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
902 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
903 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
904 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
905 print "</div></div>";
907 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
908 __('Add')."</button> ";
910 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
911 __('Delete')."</button> ";
915 print "<ul id='filterDlg_Actions'>";
916 # print "<li>No actions</li>";
919 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
920 print "<div class=\"dlgSecCont\">"; */
922 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
923 <label for=\"enabled\">".__('Enabled')."</label>";
925 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
926 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
928 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
929 <label for=\"inverse\">".__('Inverse matching')."</label>";
933 print "<div class=\"dlgButtons\">";
935 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
936 __('Test')."</button> ";
938 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
939 __('Create')."</button> ";
941 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
942 __('Cancel')."</button>";
949 $rule = json_decode($_REQUEST["rule"], true);
952 $reg_exp = htmlspecialchars($rule["reg_exp"]);
953 $filter_type = $rule["filter_type"];
954 $feed_id = $rule["feed_id"];
955 $inverse_checked = isset($rule["inverse"]) ?
"checked" : "";
960 $inverse_checked = "";
963 /*if (strpos($feed_id, "CAT:") === 0) {
964 $feed_id = substr($feed_id, 4);
970 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
972 $result = $this->dbh
->query("SELECT id,description
973 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
975 $filter_types = array();
977 while ($line = $this->dbh
->fetch_assoc($result)) {
978 $filter_types[$line["id"]] = __($line["description"]);
981 print "<div class=\"dlgSec\">".__("Match")."</div>";
983 print "<div class=\"dlgSecCont\">";
985 print "<input dojoType=\"dijit.form.ValidationTextBox\"
986 required=\"true\" id=\"filterDlg_regExp\"
987 style=\"font-size : 16px; width : 20em;\"
988 name=\"reg_exp\" value=\"$reg_exp\"/>";
991 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
992 name=\"inverse\" $inverse_checked/>";
993 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
995 print "<hr/>" . __("on field") . " ";
996 print_select_hash("filter_type", $filter_type, $filter_types,
997 'dojoType="dijit.form.Select"');
1001 print __("in") . " ";
1003 print "<span id='filterDlg_feeds'>";
1004 print_feed_multi_select("feed_id",
1006 'dojoType="dijit.form.MultiSelect"');
1011 print "<div class=\"dlgButtons\">";
1013 print "<div style=\"float : left\">
1014 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
1018 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
1019 ($rule ?
__("Save rule") : __('Add rule'))."</button> ";
1021 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
1022 __('Cancel')."</button>";
1029 function newaction() {
1030 $action = json_decode($_REQUEST["action"], true);
1033 $action_param = $this->dbh
->escape_string($action["action_param"]);
1034 $action_id = (int)$action["action_id"];
1040 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
1042 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
1044 print "<div class=\"dlgSecCont\">";
1046 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
1047 onchange=\"filterDlgCheckAction(this)\">";
1049 $result = $this->dbh
->query("SELECT id,description FROM ttrss_filter_actions
1052 while ($line = $this->dbh
->fetch_assoc($result)) {
1053 $is_selected = ($line["id"] == $action_id) ?
"selected='1'" : "";
1054 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
1059 $param_box_hidden = ($action_id == 7 ||
$action_id == 4 ||
$action_id == 6 ||
$action_id == 9) ?
1060 "" : "display : none";
1062 $param_hidden = ($action_id == 4 ||
$action_id == 6) ?
1063 "" : "display : none";
1065 $label_param_hidden = ($action_id == 7) ?
"" : "display : none";
1066 $plugin_param_hidden = ($action_id == 9) ?
"" : "display : none";
1068 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
1070 //print " " . __("with parameters:") . " ";
1071 print "<input dojoType=\"dijit.form.TextBox\"
1072 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
1073 name=\"action_param\" value=\"$action_param\">";
1075 print_label_select("action_param_label", $action_param,
1076 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
1077 dojoType=\"dijit.form.Select\"");
1079 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
1080 $filter_action_hash = array();
1082 foreach ($filter_actions as $fclass => $factions) {
1083 foreach ($factions as $faction) {
1085 $filter_action_hash[$fclass . ":" . $faction["action"]] =
1086 $fclass . ": " . $faction["description"];
1090 if (count($filter_action_hash) == 0) {
1091 $filter_plugin_disabled = "disabled";
1093 $filter_action_hash["no-data"] = __("No actions available");
1096 $filter_plugin_disabled = "";
1099 print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash,
1100 "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled",
1101 "action_param_plugin");
1105 print " "; // tiny layout hack
1109 print "<div class=\"dlgButtons\">";
1111 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
1112 ($action ?
__("Save action") : __('Add action'))."</button> ";
1114 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
1115 __('Cancel')."</button>";
1122 private function getFilterName($id) {
1124 $result = $this->dbh
->query(
1125 "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
1126 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
1127 ON (r.filter_id = f.id)
1128 LEFT JOIN ttrss_filters2_actions AS a
1129 ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title, f.match_any_rule");
1131 $title = $this->dbh
->fetch_result($result, 0, "title");
1132 $num_rules = $this->dbh
->fetch_result($result, 0, "num_rules");
1133 $num_actions = $this->dbh
->fetch_result($result, 0, "num_actions");
1134 $match_any_rule = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "match_any_rule"));
1136 if (!$title) $title = __("[No caption]");
1138 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", (int) $num_rules), $title, $num_rules);
1141 $result = $this->dbh
->query(
1142 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
1146 if ($this->dbh
->num_rows($result) > 0) {
1147 $line = $this->dbh
->fetch_assoc($result);
1148 $actions = $this->getActionName($line);
1153 if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
1155 if ($num_actions > 0)
1156 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", (int) $num_actions), $actions, $num_actions);
1158 return array($title, $actions);
1162 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
1164 if (count($ids) > 1) {
1165 $base_id = array_shift($ids);
1166 $ids_str = join(",", $ids);
1168 $this->dbh
->query("BEGIN");
1169 $this->dbh
->query("UPDATE ttrss_filters2_rules
1170 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1171 $this->dbh
->query("UPDATE ttrss_filters2_actions
1172 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1174 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
1175 $this->dbh
->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
1177 $this->dbh
->query("COMMIT");
1179 $this->optimizeFilter($base_id);
1184 private function optimizeFilter($id) {
1185 $this->dbh
->query("BEGIN");
1186 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
1187 WHERE filter_id = '$id'");
1190 $dupe_ids = array();
1192 while ($line = $this->dbh
->fetch_assoc($result)) {
1196 if (array_search($line, $tmp) === false) {
1197 array_push($tmp, $line);
1199 array_push($dupe_ids, $id);
1203 if (count($dupe_ids) > 0) {
1204 $ids_str = join(",", $dupe_ids);
1205 $this->dbh
->query("DELETE FROM ttrss_filters2_actions
1206 WHERE id IN ($ids_str)");
1209 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
1210 WHERE filter_id = '$id'");
1213 $dupe_ids = array();
1215 while ($line = $this->dbh
->fetch_assoc($result)) {
1219 if (array_search($line, $tmp) === false) {
1220 array_push($tmp, $line);
1222 array_push($dupe_ids, $id);
1226 if (count($dupe_ids) > 0) {
1227 $ids_str = join(",", $dupe_ids);
1228 $this->dbh
->query("DELETE FROM ttrss_filters2_rules
1229 WHERE id IN ($ids_str)");
1232 $this->dbh
->query("COMMIT");