]>
git.wh0rd.org - tt-rss.git/blob - classes/pref/filters.php
ec0c3862ab4e6e733a22dbb03574daba909a8cee
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 require_once "include/rssfuncs.php";
49 $offset = (int) db_escape_string($_REQUEST["offset"]);
53 $filter["enabled"] = true;
54 $filter["match_any_rule"] = sql_bool_to_bool(
55 checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["match_any_rule"])));
56 $filter["inverse"] = sql_bool_to_bool(
57 checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["inverse"])));
59 $filter["rules"] = array();
60 $filter["actions"] = array("dummy-action");
62 $result = $this->dbh
->query("SELECT id,name FROM ttrss_filter_types");
64 $filter_types = array();
65 while ($line = $this->dbh
->fetch_assoc($result)) {
66 $filter_types[$line["id"]] = $line["name"];
69 $scope_qparts = array();
72 foreach ($_REQUEST["rule"] AS $r) {
73 $rule = json_decode($r, true);
75 if ($rule && $rctr < 5) {
76 $rule["type"] = $filter_types[$rule["filter_type"]];
77 unset($rule["filter_type"]);
79 if (strpos($rule["feed_id"], "CAT:") === 0) {
80 $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
81 unset($rule["feed_id"]);
84 if (isset($rule["feed_id"]) && $rule['feed_id'] > 0) {
85 array_push($scope_qparts, "feed_id = " . $rule["feed_id"]);
86 } else if (isset($rule["cat_id"])) {
87 array_push($scope_qparts, "cat_id = " . $rule["cat_id"]);
89 array_push($scope_qparts, "true");
92 array_push($filter["rules"], $rule);
100 $glue = $filter['match_any_rule'] ?
" OR " : " AND ";
101 $scope_qpart = join($glue, $scope_qparts);
103 if (!$scope_qpart) $scope_qpart = "true";
107 //while ($found < $limit && $offset < $limit * 1000 && time() - $started < ini_get("max_execution_time") * 0.7) {
109 $result = db_query("SELECT ttrss_entries.id,
111 ttrss_feeds.id AS feed_id,
112 ttrss_feeds.title AS feed_title,
113 ttrss_feed_categories.id AS cat_id,
119 ttrss_entries, ttrss_user_entries
120 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
121 LEFT JOIN ttrss_feed_categories ON (ttrss_feeds.cat_id = ttrss_feed_categories.id)
123 ref_id = ttrss_entries.id AND
125 ttrss_user_entries.owner_uid = " . $_SESSION["uid"] . "
126 ORDER BY date_entered DESC LIMIT 30 OFFSET $offset");
128 while ($line = db_fetch_assoc($result)) {
130 $rc = get_article_filters(array($filter), $line['title'], $line['content'], $line['link'],
131 false, $line['author'], explode(",", $line['tag_cache']));
133 if (count($rc) > 0) {
135 $line["content_preview"] = truncate_string(strip_tags($line["content"]), 100, '...');
137 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_QUERY_HEADLINES
) as $p) {
138 $line = $p->hook_query_headlines($line, 100);
141 $content_preview = $line["content_preview"];
143 if ($line["feed_title"]) $feed_title = "(" . $line["feed_title"] . ")";
145 $line = "<td width='5%' align='center'><input dojoType=\"dijit.form.CheckBox\"
146 checked=\"1\" disabled=\"1\" type=\"checkbox\"></td><td>";
148 /*foreach ($filter['rules'] as $rule) {
149 $reg_exp = str_replace('/', '\/', $rule["reg_exp"]);
151 $line["title"] = preg_replace("/($reg_exp)/i",
152 "<span class=\"highlight\">$1</span>", $line["title"]);
154 $content_preview = preg_replace("/($reg_exp)/i",
155 "<span class=\"highlight\">$1</span>", $content_preview);
158 $line .= $line["title"];
159 $line .= "<div class='small' style='float : right'>" . $feed_title . "</div>";
160 $line .= "<div class=\"insensitive\">" . $content_preview . "</div>";
161 $line .= " " . mb_substr($line["date_entered"], 0, 16);
163 $line .= "</td></tr>";
165 array_push($rv, $line);
173 print "<tr><td align='center'>" .
174 __("No recent articles matching this filter have been found.");
177 print json_encode($rv);
180 function testFilter() {
182 if (isset($_REQUEST["offset"])) return $this->testFilterDo();
184 //print __("Articles matching this filter:");
186 print "<div><img src='images/indicator_tiny.gif'> <span id='prefFilterProgressMsg'>Looking for articles...</span></div>";
188 print "<br/><div class=\"filterTestHolder\">";
189 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefFilterTestResultList\">";
190 print "</table></div>";
192 print "<div style='text-align : center'>";
193 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
194 __('Close this window')."</button>";
199 private function getfilterrules_concise($filter_id) {
200 $result = $this->dbh
->query("SELECT reg_exp,
205 ttrss_filter_types.description AS field
207 ttrss_filters2_rules, ttrss_filter_types
209 filter_id = '$filter_id' AND filter_type = ttrss_filter_types.id");
213 while ($line = $this->dbh
->fetch_assoc($result)) {
215 $where = sql_bool_to_bool($line["cat_filter"]) ?
216 getCategoryTitle($line["cat_id"]) :
218 getFeedTitle($line["feed_id"]) : __("All feeds"));
220 # $where = $line["cat_id"] . "/" . $line["feed_id"];
222 $inverse = sql_bool_to_bool($line["inverse"]) ?
"inverse" : "";
224 $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
225 strip_tags($line["reg_exp"]),
228 sql_bool_to_bool($line["inverse"]) ?
__("(inverse)") : "") . "</span>";
234 function getfiltertree() {
236 $root['id'] = 'root';
237 $root['name'] = __('Filters');
238 $root['items'] = array();
240 $filter_search = $_SESSION["prefs_filter_search"];
242 $result = $this->dbh
->query("SELECT *,
243 (SELECT action_param FROM ttrss_filters2_actions
244 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
245 (SELECT action_id FROM ttrss_filters2_actions
246 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
247 (SELECT description FROM ttrss_filter_actions
248 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
249 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
250 (SELECT reg_exp FROM ttrss_filters2_rules
251 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
252 FROM ttrss_filters2 WHERE
253 owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
257 $folder['items'] = array();
259 while ($line = $this->dbh
->fetch_assoc($result)) {
261 $name = $this->getFilterName($line["id"]);
264 if ($filter_search) {
265 $rules_result = $this->dbh
->query(
266 "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
268 while ($rule_line = $this->dbh
->fetch_assoc($rules_result)) {
269 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
276 if ($line['action_id'] == 7) {
277 $label_result = $this->dbh
->query("SELECT fg_color, bg_color
278 FROM ttrss_labels2 WHERE caption = '".$this->dbh
->escape_string($line['action_param'])."' AND
279 owner_uid = " . $_SESSION["uid"]);
281 if ($this->dbh
->num_rows($label_result) > 0) {
282 $fg_color = $this->dbh
->fetch_result($label_result, 0, "fg_color");
283 $bg_color = $this->dbh
->fetch_result($label_result, 0, "bg_color");
285 $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>α</span>" . $name[1];
290 $filter['id'] = 'FILTER:' . $line['id'];
291 $filter['bare_id'] = $line['id'];
292 $filter['name'] = $name[0];
293 $filter['param'] = $name[1];
294 $filter['checkbox'] = false;
295 $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
296 $filter['rules'] = $this->getfilterrules_concise($line['id']);
298 if (!$filter_search ||
$match_ok) {
299 array_push($folder['items'], $filter);
303 /* if (count($folder['items']) > 0) {
304 array_push($root['items'], $folder);
307 $root['items'] = $folder['items'];
310 $fl['identifier'] = 'id';
311 $fl['label'] = 'name';
312 $fl['items'] = array($root);
314 print json_encode($fl);
320 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
322 $result = $this->dbh
->query(
323 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
325 $enabled = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "enabled"));
326 $match_any_rule = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "match_any_rule"));
327 $inverse = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "inverse"));
328 $title = htmlspecialchars($this->dbh
->fetch_result($result, 0, "title"));
330 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
332 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
333 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
334 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
335 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
337 print "<div class=\"dlgSec\">".__("Caption")."</div>";
339 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
343 print "<div class=\"dlgSec\">".__("Match")."</div>";
345 print "<div dojoType=\"dijit.Toolbar\">";
347 print "<div dojoType=\"dijit.form.DropDownButton\">".
348 "<span>" . __('Select')."</span>";
349 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
350 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
351 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
352 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
353 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
354 print "</div></div>";
356 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
357 __('Add')."</button> ";
359 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
360 __('Delete')."</button> ";
364 print "<ul id='filterDlg_Matches'>";
366 $rules_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
367 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
369 while ($line = $this->dbh
->fetch_assoc($rules_result)) {
370 if (sql_bool_to_bool($line["cat_filter"])) {
371 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
374 unset($line["cat_filter"]);
375 unset($line["cat_id"]);
376 unset($line["filter_id"]);
378 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
380 $data = htmlspecialchars(json_encode($line));
382 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
383 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
384 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
391 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
393 print "<div dojoType=\"dijit.Toolbar\">";
395 print "<div dojoType=\"dijit.form.DropDownButton\">".
396 "<span>" . __('Select')."</span>";
397 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
398 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
399 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
400 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
401 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
402 print "</div></div>";
404 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
405 __('Add')."</button> ";
407 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
408 __('Delete')."</button> ";
412 print "<ul id='filterDlg_Actions'>";
414 $actions_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
415 WHERE filter_id = '$filter_id' ORDER BY id");
417 while ($line = $this->dbh
->fetch_assoc($actions_result)) {
418 $line["action_param_label"] = $line["action_param"];
420 unset($line["filter_id"]);
423 $data = htmlspecialchars(json_encode($line));
425 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
426 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
427 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
435 $checked = "checked=\"1\"";
440 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
441 <label for=\"enabled\">".__('Enabled')."</label>";
443 if ($match_any_rule) {
444 $checked = "checked=\"1\"";
449 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
450 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
453 $checked = "checked=\"1\"";
458 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
459 <label for=\"inverse\">".__('Inverse matching')."</label>";
463 print "<div class=\"dlgButtons\">";
465 print "<div style=\"float : left\">";
466 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
467 __('Remove')."</button>";
470 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
471 __('Test')."</button> ";
473 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
474 __('Save')."</button> ";
476 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
477 __('Cancel')."</button>";
482 private function getRuleName($rule) {
483 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
485 $feed_id = $rule["feed_id"];
487 if (strpos($feed_id, "CAT:") === 0) {
488 $feed_id = (int) substr($feed_id, 4);
489 $feed = getCategoryTitle($feed_id);
491 $feed_id = (int) $feed_id;
493 if ($rule["feed_id"])
494 $feed = getFeedTitle((int)$rule["feed_id"]);
496 $feed = __("All feeds");
499 $result = $this->dbh
->query("SELECT description FROM ttrss_filter_types
500 WHERE id = ".(int)$rule["filter_type"]);
501 $filter_type = $this->dbh
->fetch_result($result, 0, "description");
503 $inverse = isset($rule["inverse"]) ?
"inverse" : "";
505 return "<span class='filterRule $inverse'>" .
506 T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
507 $filter_type, $feed, isset($rule["inverse"]) ?
__("(inverse)") : "") . "</span>";
510 function printRuleName() {
511 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
514 private function getActionName($action) {
515 $result = $this->dbh
->query("SELECT description FROM
516 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
518 $title = __($this->dbh
->fetch_result($result, 0, "description"));
520 if ($action["action_id"] == 4 ||
$action["action_id"] == 6 ||
521 $action["action_id"] == 7)
522 $title .= ": " . $action["action_param"];
524 if ($action["action_id"] == 9) {
525 list ($pfclass, $pfaction) = explode(":", $action["action_param"]);
527 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
529 foreach ($filter_actions as $fclass => $factions) {
530 foreach ($factions as $faction) {
531 if ($pfaction == $faction["action"] && $pfclass == $fclass) {
532 $title .= ": " . $fclass . ": " . $faction["description"];
542 function printActionName() {
543 print $this->getActionName(json_decode($_REQUEST["action"], true));
546 function editSave() {
547 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
548 return $this->testFilter();
551 # print_r($_REQUEST);
553 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
554 $enabled = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["enabled"]));
555 $match_any_rule = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["match_any_rule"]));
556 $inverse = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["inverse"]));
557 $title = $this->dbh
->escape_string($_REQUEST["title"]);
559 $this->dbh
->query("UPDATE ttrss_filters2 SET enabled = $enabled,
560 match_any_rule = $match_any_rule,
563 WHERE id = '$filter_id'
564 AND owner_uid = ". $_SESSION["uid"]);
566 $this->saveRulesAndActions($filter_id);
572 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
574 foreach ($ids as $id) {
575 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
579 private function saveRulesAndActions($filter_id) {
581 $this->dbh
->query("DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
582 $this->dbh
->query("DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
590 foreach ($_REQUEST["rule"] as $rule) {
591 $rule = json_decode($rule, true);
594 if (array_search($rule, $rules) === false) {
595 array_push($rules, $rule);
599 foreach ($_REQUEST["action"] as $action) {
600 $action = json_decode($action, true);
601 unset($action["id"]);
603 if (array_search($action, $actions) === false) {
604 array_push($actions, $action);
608 foreach ($rules as $rule) {
611 $reg_exp = strip_tags($this->dbh
->escape_string(trim($rule["reg_exp"])));
612 $inverse = isset($rule["inverse"]) ?
"true" : "false";
614 $filter_type = (int) $this->dbh
->escape_string(trim($rule["filter_type"]));
615 $feed_id = $this->dbh
->escape_string(trim($rule["feed_id"]));
617 if (strpos($feed_id, "CAT:") === 0) {
619 $cat_filter = bool_to_sql_bool(true);
620 $cat_id = (int) substr($feed_id, 4);
623 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
625 $cat_filter = bool_to_sql_bool(false);
626 $feed_id = (int) $feed_id;
629 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
632 $query = "INSERT INTO ttrss_filters2_rules
633 (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
634 ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
636 $this->dbh
->query($query);
640 foreach ($actions as $action) {
643 $action_id = (int) $this->dbh
->escape_string($action["action_id"]);
644 $action_param = $this->dbh
->escape_string($action["action_param"]);
645 $action_param_label = $this->dbh
->escape_string($action["action_param_label"]);
647 if ($action_id == 7) {
648 $action_param = $action_param_label;
651 if ($action_id == 6) {
652 $action_param = (int) str_replace("+", "", $action_param);
655 $query = "INSERT INTO ttrss_filters2_actions
656 (filter_id, action_id, action_param) VALUES
657 ('$filter_id', '$action_id', '$action_param')";
659 $this->dbh
->query($query);
668 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
669 return $this->testFilter();
672 # print_r($_REQUEST);
674 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
675 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
676 $title = $this->dbh
->escape_string($_REQUEST["title"]);
677 $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
679 $this->dbh
->query("BEGIN");
681 /* create base filter */
683 $result = $this->dbh
->query("INSERT INTO ttrss_filters2
684 (owner_uid, match_any_rule, enabled, title, inverse) VALUES
685 (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title', $inverse)");
687 $result = $this->dbh
->query("SELECT MAX(id) AS id FROM ttrss_filters2
688 WHERE owner_uid = ".$_SESSION["uid"]);
690 $filter_id = $this->dbh
->fetch_result($result, 0, "id");
692 $this->saveRulesAndActions($filter_id);
694 $this->dbh
->query("COMMIT");
699 $sort = $this->dbh
->escape_string($_REQUEST["sort"]);
701 if (!$sort ||
$sort == "undefined") {
705 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
707 if (array_key_exists("search", $_REQUEST)) {
708 $_SESSION["prefs_filter_search"] = $filter_search;
710 $filter_search = $_SESSION["prefs_filter_search"];
713 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
714 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
715 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
717 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
719 if (array_key_exists("search", $_REQUEST)) {
720 $_SESSION["prefs_filter_search"] = $filter_search;
722 $filter_search = $_SESSION["prefs_filter_search"];
725 print "<div style='float : right; padding-right : 4px;'>
726 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
727 value=\"$filter_search\">
728 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
729 __('Search')."</button>
732 print "<div dojoType=\"dijit.form.DropDownButton\">".
733 "<span>" . __('Select')."</span>";
734 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
735 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
736 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
737 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
738 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
739 print "</div></div>";
741 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
742 __('Create filter')."</button> ";
744 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
745 __('Combine')."</button> ";
747 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
748 __('Edit')."</button> ";
750 print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
751 __('Reset sort order')."</button> ";
754 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
755 __('Remove')."</button> ";
757 if (defined('_ENABLE_FEED_DEBUGGING')) {
758 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
759 __('Rescore articles')."</button> ";
762 print "</div>"; # toolbar
763 print "</div>"; # toolbar-frame
764 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
766 print "<div id=\"filterlistLoading\">
767 <img src='images/indicator_tiny.gif'>".
768 __("Loading, please wait...")."</div>";
770 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
771 url=\"backend.php?op=pref-filters&method=getfiltertree\">
773 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
774 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
775 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
777 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
778 dndController=\"dijit.tree.dndSource\"
779 betweenThreshold=\"5\"
780 model=\"filterModel\" openOnClick=\"true\">
781 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
782 Element.hide(\"filterlistLoading\");
784 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
785 var id = String(item.id);
786 var bare_id = id.substr(id.indexOf(':')+1);
788 if (id.match('FILTER:')) {
795 print "</div>"; #pane
797 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB
,
798 "hook_prefs_tab", "prefFilters");
800 print "</div>"; #container
804 function newfilter() {
806 print "<form name='filter_new_form' id='filter_new_form'>";
808 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
809 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
810 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
812 print "<div class=\"dlgSec\">".__("Caption")."</div>";
814 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
816 print "<div class=\"dlgSec\">".__("Match")."</div>";
818 print "<div dojoType=\"dijit.Toolbar\">";
820 print "<div dojoType=\"dijit.form.DropDownButton\">".
821 "<span>" . __('Select')."</span>";
822 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
823 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
824 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
825 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
826 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
827 print "</div></div>";
829 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
830 __('Add')."</button> ";
832 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
833 __('Delete')."</button> ";
837 print "<ul id='filterDlg_Matches'>";
838 # print "<li>No rules</li>";
843 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
845 print "<div dojoType=\"dijit.Toolbar\">";
847 print "<div dojoType=\"dijit.form.DropDownButton\">".
848 "<span>" . __('Select')."</span>";
849 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
850 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
851 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
852 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
853 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
854 print "</div></div>";
856 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
857 __('Add')."</button> ";
859 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
860 __('Delete')."</button> ";
864 print "<ul id='filterDlg_Actions'>";
865 # print "<li>No actions</li>";
868 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
869 print "<div class=\"dlgSecCont\">"; */
871 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
872 <label for=\"enabled\">".__('Enabled')."</label>";
874 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
875 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
877 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
878 <label for=\"inverse\">".__('Inverse matching')."</label>";
882 print "<div class=\"dlgButtons\">";
884 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
885 __('Test')."</button> ";
887 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
888 __('Create')."</button> ";
890 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
891 __('Cancel')."</button>";
898 $rule = json_decode($_REQUEST["rule"], true);
901 $reg_exp = htmlspecialchars($rule["reg_exp"]);
902 $filter_type = $rule["filter_type"];
903 $feed_id = $rule["feed_id"];
904 $inverse_checked = isset($rule["inverse"]) ?
"checked" : "";
909 $inverse_checked = "";
912 if (strpos($feed_id, "CAT:") === 0) {
913 $feed_id = substr($feed_id, 4);
920 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
922 $result = $this->dbh
->query("SELECT id,description
923 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
925 $filter_types = array();
927 while ($line = $this->dbh
->fetch_assoc($result)) {
928 $filter_types[$line["id"]] = __($line["description"]);
931 print "<div class=\"dlgSec\">".__("Match")."</div>";
933 print "<div class=\"dlgSecCont\">";
935 print "<input dojoType=\"dijit.form.ValidationTextBox\"
936 required=\"true\" id=\"filterDlg_regExp\"
937 style=\"font-size : 16px; width : 20em;\"
938 name=\"reg_exp\" value=\"$reg_exp\"/>";
941 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
942 name=\"inverse\" $inverse_checked/>";
943 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
945 print "<hr/>" . __("on field") . " ";
946 print_select_hash("filter_type", $filter_type, $filter_types,
947 'dojoType="dijit.form.Select"');
951 print __("in") . " ";
953 print "<span id='filterDlg_feeds'>";
954 print_feed_select("feed_id",
955 $cat_filter ?
"CAT:$feed_id" : $feed_id,
956 'dojoType="dijit.form.FilteringSelect"');
961 print "<div class=\"dlgButtons\">";
963 print "<div style=\"float : left\">
964 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
968 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
969 ($rule ?
__("Save rule") : __('Add rule'))."</button> ";
971 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
972 __('Cancel')."</button>";
979 function newaction() {
980 $action = json_decode($_REQUEST["action"], true);
983 $action_param = $this->dbh
->escape_string($action["action_param"]);
984 $action_id = (int)$action["action_id"];
990 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
992 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
994 print "<div class=\"dlgSecCont\">";
996 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
997 onchange=\"filterDlgCheckAction(this)\">";
999 $result = $this->dbh
->query("SELECT id,description FROM ttrss_filter_actions
1002 while ($line = $this->dbh
->fetch_assoc($result)) {
1003 $is_selected = ($line["id"] == $action_id) ?
"selected='1'" : "";
1004 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
1009 $param_box_hidden = ($action_id == 7 ||
$action_id == 4 ||
$action_id == 6 ||
$action_id == 9) ?
1010 "" : "display : none";
1012 $param_hidden = ($action_id == 4 ||
$action_id == 6) ?
1013 "" : "display : none";
1015 $label_param_hidden = ($action_id == 7) ?
"" : "display : none";
1016 $plugin_param_hidden = ($action_id == 9) ?
"" : "display : none";
1018 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
1020 //print " " . __("with parameters:") . " ";
1021 print "<input dojoType=\"dijit.form.TextBox\"
1022 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
1023 name=\"action_param\" value=\"$action_param\">";
1025 print_label_select("action_param_label", $action_param,
1026 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
1027 dojoType=\"dijit.form.Select\"");
1029 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
1030 $filter_action_hash = array();
1032 foreach ($filter_actions as $fclass => $factions) {
1033 foreach ($factions as $faction) {
1035 $filter_action_hash[$fclass . ":" . $faction["action"]] =
1036 $fclass . ": " . $faction["description"];
1040 if (count($filter_action_hash) == 0) {
1041 $filter_plugin_disabled = "disabled";
1043 $filter_action_hash["no-data"] = __("No actions available");
1046 $filter_plugin_disabled = "";
1049 print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash,
1050 "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled",
1051 "action_param_plugin");
1055 print " "; // tiny layout hack
1059 print "<div class=\"dlgButtons\">";
1061 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
1062 ($action ?
__("Save action") : __('Add action'))."</button> ";
1064 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
1065 __('Cancel')."</button>";
1072 private function getFilterName($id) {
1074 $result = $this->dbh
->query(
1075 "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
1076 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
1077 ON (r.filter_id = f.id)
1078 LEFT JOIN ttrss_filters2_actions AS a
1079 ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title, f.match_any_rule");
1081 $title = $this->dbh
->fetch_result($result, 0, "title");
1082 $num_rules = $this->dbh
->fetch_result($result, 0, "num_rules");
1083 $num_actions = $this->dbh
->fetch_result($result, 0, "num_actions");
1084 $match_any_rule = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "match_any_rule"));
1086 if (!$title) $title = __("[No caption]");
1088 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", $num_rules), $title, $num_rules);
1091 $result = $this->dbh
->query(
1092 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
1096 if ($this->dbh
->num_rows($result) > 0) {
1097 $line = $this->dbh
->fetch_assoc($result);
1098 $actions = $this->getActionName($line);
1103 if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
1105 if ($num_actions > 0)
1106 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", $num_actions), $actions, $num_actions);
1108 return array($title, $actions);
1112 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
1114 if (count($ids) > 1) {
1115 $base_id = array_shift($ids);
1116 $ids_str = join(",", $ids);
1118 $this->dbh
->query("BEGIN");
1119 $this->dbh
->query("UPDATE ttrss_filters2_rules
1120 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1121 $this->dbh
->query("UPDATE ttrss_filters2_actions
1122 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1124 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
1125 $this->dbh
->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
1127 $this->dbh
->query("COMMIT");
1129 $this->optimizeFilter($base_id);
1134 private function optimizeFilter($id) {
1135 $this->dbh
->query("BEGIN");
1136 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
1137 WHERE filter_id = '$id'");
1140 $dupe_ids = array();
1142 while ($line = $this->dbh
->fetch_assoc($result)) {
1146 if (array_search($line, $tmp) === false) {
1147 array_push($tmp, $line);
1149 array_push($dupe_ids, $id);
1153 if (count($dupe_ids) > 0) {
1154 $ids_str = join(",", $dupe_ids);
1155 $this->dbh
->query("DELETE FROM ttrss_filters2_actions
1156 WHERE id IN ($ids_str)");
1159 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
1160 WHERE filter_id = '$id'");
1163 $dupe_ids = array();
1165 while ($line = $this->dbh
->fetch_assoc($result)) {
1169 if (array_search($line, $tmp) === false) {
1170 array_push($tmp, $line);
1172 array_push($dupe_ids, $id);
1176 if (count($dupe_ids) > 0) {
1177 $ids_str = join(",", $dupe_ids);
1178 $this->dbh
->query("DELETE FROM ttrss_filters2_rules
1179 WHERE id IN ($ids_str)");
1182 $this->dbh
->query("COMMIT");