]>
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 $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 if (strpos($rule["feed_id"], "CAT:") === 0) {
79 $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
80 unset($rule["feed_id"]);
83 if (isset($rule["feed_id"]) && $rule['feed_id'] > 0) {
84 array_push($scope_qparts, "feed_id = " . $rule["feed_id"]);
85 } else if (isset($rule["cat_id"])) {
86 array_push($scope_qparts, "cat_id = " . $rule["cat_id"]);
88 array_push($scope_qparts, "true");
91 array_push($filter["rules"], $rule);
99 $glue = $filter['match_any_rule'] ?
" OR " : " AND ";
100 $scope_qpart = join($glue, $scope_qparts);
102 if (!$scope_qpart) $scope_qpart = "true";
106 //while ($found < $limit && $offset < $limit * 1000 && time() - $started < ini_get("max_execution_time") * 0.7) {
108 $result = db_query("SELECT ttrss_entries.id,
110 ttrss_feeds.id AS feed_id,
111 ttrss_feeds.title AS feed_title,
112 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 $limit OFFSET $offset");
128 while ($line = db_fetch_assoc($result)) {
130 $rc = RSSUtils
::get_article_filters(array($filter), $line['title'], $line['content'], $line['link'],
131 $line['author'], explode(",", $line['tag_cache']));
133 if (count($rc) > 0) {
135 $line["content_preview"] = truncate_string(strip_tags($line["content"]), 200, '…');
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 $tmp = "<tr style='margin-top : 5px'>";
145 #$tmp .= "<td width='5%' align='center'><input dojoType=\"dijit.form.CheckBox\"
146 # checked=\"1\" disabled=\"1\" type=\"checkbox\"></td>";
149 $tmp .= "<td width='5%' align='center'><img style='cursor : pointer' title='".__("Preview article")."'
150 src='images/information.png' onclick='openArticlePopup($id)'></td><td>";
152 /*foreach ($filter['rules'] as $rule) {
153 $reg_exp = str_replace('/', '\/', $rule["reg_exp"]);
155 $line["title"] = preg_replace("/($reg_exp)/i",
156 "<span class=\"highlight\">$1</span>", $line["title"]);
158 $content_preview = preg_replace("/($reg_exp)/i",
159 "<span class=\"highlight\">$1</span>", $content_preview);
162 $tmp .= "<strong>" . $line["title"] . "</strong><br/>";
163 $tmp .= $line['feed_title'] . ", " . mb_substr($line["date_entered"], 0, 16);
164 $tmp .= "<div class='insensitive'>" . $content_preview . "</div>";
165 $tmp .= "</td></tr>";
167 array_push($rv, $tmp);
169 /*array_push($rv, array("title" => $line["title"],
170 "content" => $content_preview,
171 "date" => $line["date_entered"],
172 "feed" => $line["feed_title"])); */
181 print "<tr><td align='center'>" .
182 __("No recent articles matching this filter have been found.");
185 print json_encode($rv);
188 function testFilter() {
190 if (isset($_REQUEST["offset"])) return $this->testFilterDo();
192 //print __("Articles matching this filter:");
194 print "<div><img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'> <span id='prefFilterProgressMsg'>Looking for articles...</span></div>";
196 print "<br/><div class=\"filterTestHolder\">";
197 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefFilterTestResultList\">";
198 print "</table></div>";
200 print "<div style='text-align : center'>";
201 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
202 __('Close this window')."</button>";
207 private function getfilterrules_concise($filter_id) {
208 $result = $this->dbh
->query("SELECT reg_exp,
213 ttrss_filter_types.description AS field
215 ttrss_filters2_rules, ttrss_filter_types
217 filter_id = '$filter_id' AND filter_type = ttrss_filter_types.id
222 while ($line = $this->dbh
->fetch_assoc($result)) {
224 $where = sql_bool_to_bool($line["cat_filter"]) ?
225 Feeds
::getCategoryTitle($line["cat_id"]) :
227 Feeds
::getFeedTitle($line["feed_id"]) : __("All feeds"));
229 # $where = $line["cat_id"] . "/" . $line["feed_id"];
231 $inverse = sql_bool_to_bool($line["inverse"]) ?
"inverse" : "";
233 $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
234 htmlspecialchars($line["reg_exp"]),
237 sql_bool_to_bool($line["inverse"]) ?
__("(inverse)") : "") . "</span>";
243 function getfiltertree() {
245 $root['id'] = 'root';
246 $root['name'] = __('Filters');
247 $root['items'] = array();
249 $filter_search = $_SESSION["prefs_filter_search"];
251 $result = $this->dbh
->query("SELECT *,
252 (SELECT action_param FROM ttrss_filters2_actions
253 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
254 (SELECT action_id FROM ttrss_filters2_actions
255 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
256 (SELECT description FROM ttrss_filter_actions
257 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
258 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
259 (SELECT reg_exp FROM ttrss_filters2_rules
260 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
261 FROM ttrss_filters2 WHERE
262 owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
266 $folder['items'] = array();
268 while ($line = $this->dbh
->fetch_assoc($result)) {
270 $name = $this->getFilterName($line["id"]);
273 if ($filter_search) {
274 $rules_result = $this->dbh
->query(
275 "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
277 while ($rule_line = $this->dbh
->fetch_assoc($rules_result)) {
278 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
285 if ($line['action_id'] == 7) {
286 $label_result = $this->dbh
->query("SELECT fg_color, bg_color
287 FROM ttrss_labels2 WHERE caption = '".$this->dbh
->escape_string($line['action_param'])."' AND
288 owner_uid = " . $_SESSION["uid"]);
290 if ($this->dbh
->num_rows($label_result) > 0) {
291 $fg_color = $this->dbh
->fetch_result($label_result, 0, "fg_color");
292 $bg_color = $this->dbh
->fetch_result($label_result, 0, "bg_color");
294 $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>α</span>" . $name[1];
299 $filter['id'] = 'FILTER:' . $line['id'];
300 $filter['bare_id'] = $line['id'];
301 $filter['name'] = $name[0];
302 $filter['param'] = $name[1];
303 $filter['checkbox'] = false;
304 $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
305 $filter['rules'] = $this->getfilterrules_concise($line['id']);
307 if (!$filter_search ||
$match_ok) {
308 array_push($folder['items'], $filter);
312 /* if (count($folder['items']) > 0) {
313 array_push($root['items'], $folder);
316 $root['items'] = $folder['items'];
319 $fl['identifier'] = 'id';
320 $fl['label'] = 'name';
321 $fl['items'] = array($root);
323 print json_encode($fl);
329 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
331 $result = $this->dbh
->query(
332 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
334 $enabled = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "enabled"));
335 $match_any_rule = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "match_any_rule"));
336 $inverse = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "inverse"));
337 $title = htmlspecialchars($this->dbh
->fetch_result($result, 0, "title"));
339 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
341 print_hidden("op", "pref-filters");
342 print_hidden("id", "$filter_id");
343 print_hidden("method", "editSave");
344 print_hidden("csrf_token", $_SESSION['csrf_token']);
346 print "<div class=\"dlgSec\">".__("Caption")."</div>";
348 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
352 print "<div class=\"dlgSec\">".__("Match")."</div>";
354 print "<div dojoType=\"dijit.Toolbar\">";
356 print "<div dojoType=\"dijit.form.DropDownButton\">".
357 "<span>" . __('Select')."</span>";
358 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
359 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
360 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
361 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
362 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
363 print "</div></div>";
365 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
366 __('Add')."</button> ";
368 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
369 __('Delete')."</button> ";
373 print "<ul id='filterDlg_Matches'>";
375 $rules_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
376 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
378 while ($line = $this->dbh
->fetch_assoc($rules_result)) {
379 if (sql_bool_to_bool($line["cat_filter"])) {
380 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
383 unset($line["cat_filter"]);
384 unset($line["cat_id"]);
385 unset($line["filter_id"]);
387 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
389 $data = htmlspecialchars(json_encode($line));
391 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
392 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
393 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
400 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
402 print "<div dojoType=\"dijit.Toolbar\">";
404 print "<div dojoType=\"dijit.form.DropDownButton\">".
405 "<span>" . __('Select')."</span>";
406 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
407 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
408 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
409 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
410 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
411 print "</div></div>";
413 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
414 __('Add')."</button> ";
416 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
417 __('Delete')."</button> ";
421 print "<ul id='filterDlg_Actions'>";
423 $actions_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
424 WHERE filter_id = '$filter_id' ORDER BY id");
426 while ($line = $this->dbh
->fetch_assoc($actions_result)) {
427 $line["action_param_label"] = $line["action_param"];
429 unset($line["filter_id"]);
432 $data = htmlspecialchars(json_encode($line));
434 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
435 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
436 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
444 $checked = "checked=\"1\"";
449 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
450 <label for=\"enabled\">".__('Enabled')."</label>";
452 if ($match_any_rule) {
453 $checked = "checked=\"1\"";
458 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
459 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
462 $checked = "checked=\"1\"";
467 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
468 <label for=\"inverse\">".__('Inverse matching')."</label>";
472 print "<div class=\"dlgButtons\">";
474 print "<div style=\"float : left\">";
475 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
476 __('Remove')."</button>";
479 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
480 __('Test')."</button> ";
482 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
483 __('Save')."</button> ";
485 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
486 __('Cancel')."</button>";
491 private function getRuleName($rule) {
492 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
494 $feed_id = $rule["feed_id"];
496 if (strpos($feed_id, "CAT:") === 0) {
497 $feed_id = (int) substr($feed_id, 4);
498 $feed = Feeds
::getCategoryTitle($feed_id);
500 $feed_id = (int) $feed_id;
502 if ($rule["feed_id"])
503 $feed = Feeds
::getFeedTitle((int)$rule["feed_id"]);
505 $feed = __("All feeds");
508 $result = $this->dbh
->query("SELECT description FROM ttrss_filter_types
509 WHERE id = ".(int)$rule["filter_type"]);
510 $filter_type = $this->dbh
->fetch_result($result, 0, "description");
512 $inverse = isset($rule["inverse"]) ?
"inverse" : "";
514 return "<span class='filterRule $inverse'>" .
515 T_sprintf("%s on %s in %s %s", htmlspecialchars($rule["reg_exp"]),
516 $filter_type, $feed, isset($rule["inverse"]) ?
__("(inverse)") : "") . "</span>";
519 function printRuleName() {
520 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
523 private function getActionName($action) {
524 $result = $this->dbh
->query("SELECT description FROM
525 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
527 $title = __($this->dbh
->fetch_result($result, 0, "description"));
529 if ($action["action_id"] == 4 ||
$action["action_id"] == 6 ||
530 $action["action_id"] == 7)
531 $title .= ": " . $action["action_param"];
533 if ($action["action_id"] == 9) {
534 list ($pfclass, $pfaction) = explode(":", $action["action_param"]);
536 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
538 foreach ($filter_actions as $fclass => $factions) {
539 foreach ($factions as $faction) {
540 if ($pfaction == $faction["action"] && $pfclass == $fclass) {
541 $title .= ": " . $fclass . ": " . $faction["description"];
551 function printActionName() {
552 print $this->getActionName(json_decode($_REQUEST["action"], true));
555 function editSave() {
556 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
557 return $this->testFilter();
560 # print_r($_REQUEST);
562 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
563 $enabled = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["enabled"]));
564 $match_any_rule = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["match_any_rule"]));
565 $inverse = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["inverse"]));
566 $title = $this->dbh
->escape_string($_REQUEST["title"]);
568 $this->dbh
->query("UPDATE ttrss_filters2 SET enabled = $enabled,
569 match_any_rule = $match_any_rule,
572 WHERE id = '$filter_id'
573 AND owner_uid = ". $_SESSION["uid"]);
575 $this->saveRulesAndActions($filter_id);
581 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
583 foreach ($ids as $id) {
584 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
588 private function saveRulesAndActions($filter_id) {
590 $this->dbh
->query("DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
591 $this->dbh
->query("DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
599 foreach ($_REQUEST["rule"] as $rule) {
600 $rule = json_decode($rule, true);
603 if (array_search($rule, $rules) === false) {
604 array_push($rules, $rule);
608 foreach ($_REQUEST["action"] as $action) {
609 $action = json_decode($action, true);
610 unset($action["id"]);
612 if (array_search($action, $actions) === false) {
613 array_push($actions, $action);
617 foreach ($rules as $rule) {
620 $reg_exp = $this->dbh
->escape_string(trim($rule["reg_exp"]), false);
621 $inverse = isset($rule["inverse"]) ?
"true" : "false";
623 $filter_type = (int) $this->dbh
->escape_string(trim($rule["filter_type"]));
624 $feed_id = $this->dbh
->escape_string(trim($rule["feed_id"]));
626 if (strpos($feed_id, "CAT:") === 0) {
628 $cat_filter = bool_to_sql_bool(true);
629 $cat_id = (int) substr($feed_id, 4);
632 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
634 $cat_filter = bool_to_sql_bool(false);
635 $feed_id = (int) $feed_id;
638 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
641 $query = "INSERT INTO ttrss_filters2_rules
642 (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
643 ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
645 $this->dbh
->query($query);
649 foreach ($actions as $action) {
652 $action_id = (int) $this->dbh
->escape_string($action["action_id"]);
653 $action_param = $this->dbh
->escape_string($action["action_param"]);
654 $action_param_label = $this->dbh
->escape_string($action["action_param_label"]);
656 if ($action_id == 7) {
657 $action_param = $action_param_label;
660 if ($action_id == 6) {
661 $action_param = (int) str_replace("+", "", $action_param);
664 $query = "INSERT INTO ttrss_filters2_actions
665 (filter_id, action_id, action_param) VALUES
666 ('$filter_id', '$action_id', '$action_param')";
668 $this->dbh
->query($query);
677 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
678 return $this->testFilter();
681 # print_r($_REQUEST);
683 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
684 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
685 $title = $this->dbh
->escape_string($_REQUEST["title"]);
686 $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
688 $this->dbh
->query("BEGIN");
690 /* create base filter */
692 $result = $this->dbh
->query("INSERT INTO ttrss_filters2
693 (owner_uid, match_any_rule, enabled, title, inverse) VALUES
694 (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title', $inverse)");
696 $result = $this->dbh
->query("SELECT MAX(id) AS id FROM ttrss_filters2
697 WHERE owner_uid = ".$_SESSION["uid"]);
699 $filter_id = $this->dbh
->fetch_result($result, 0, "id");
701 $this->saveRulesAndActions($filter_id);
703 $this->dbh
->query("COMMIT");
708 $sort = $this->dbh
->escape_string($_REQUEST["sort"]);
710 if (!$sort ||
$sort == "undefined") {
714 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
716 if (array_key_exists("search", $_REQUEST)) {
717 $_SESSION["prefs_filter_search"] = $filter_search;
719 $filter_search = $_SESSION["prefs_filter_search"];
722 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
723 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
724 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
726 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
728 if (array_key_exists("search", $_REQUEST)) {
729 $_SESSION["prefs_filter_search"] = $filter_search;
731 $filter_search = $_SESSION["prefs_filter_search"];
734 print "<div style='float : right; padding-right : 4px;'>
735 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
736 value=\"$filter_search\">
737 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
738 __('Search')."</button>
741 print "<div dojoType=\"dijit.form.DropDownButton\">".
742 "<span>" . __('Select')."</span>";
743 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
744 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
745 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
746 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
747 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
748 print "</div></div>";
750 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
751 __('Create filter')."</button> ";
753 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
754 __('Combine')."</button> ";
756 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
757 __('Edit')."</button> ";
759 print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
760 __('Reset sort order')."</button> ";
763 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
764 __('Remove')."</button> ";
766 if (defined('_ENABLE_FEED_DEBUGGING')) {
767 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
768 __('Rescore articles')."</button> ";
771 print "</div>"; # toolbar
772 print "</div>"; # toolbar-frame
773 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
775 print "<div id=\"filterlistLoading\">
776 <img src='images/indicator_tiny.gif'>".
777 __("Loading, please wait...")."</div>";
779 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
780 url=\"backend.php?op=pref-filters&method=getfiltertree\">
782 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
783 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
784 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
786 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
787 dndController=\"dijit.tree.dndSource\"
788 betweenThreshold=\"5\"
789 model=\"filterModel\" openOnClick=\"true\">
790 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
791 Element.hide(\"filterlistLoading\");
793 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
794 var id = String(item.id);
795 var bare_id = id.substr(id.indexOf(':')+1);
797 if (id.match('FILTER:')) {
804 print "</div>"; #pane
806 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB
,
807 "hook_prefs_tab", "prefFilters");
809 print "</div>"; #container
813 function newfilter() {
815 print "<form name='filter_new_form' id='filter_new_form'>";
817 print_hidden("op", "pref-filters");
818 print_hidden("method", "add");
819 print_hidden("csrf_token", $_SESSION['csrf_token']);
821 print "<div class=\"dlgSec\">".__("Caption")."</div>";
823 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
825 print "<div class=\"dlgSec\">".__("Match")."</div>";
827 print "<div dojoType=\"dijit.Toolbar\">";
829 print "<div dojoType=\"dijit.form.DropDownButton\">".
830 "<span>" . __('Select')."</span>";
831 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
832 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
833 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
834 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
835 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
836 print "</div></div>";
838 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
839 __('Add')."</button> ";
841 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
842 __('Delete')."</button> ";
846 print "<ul id='filterDlg_Matches'>";
847 # print "<li>No rules</li>";
852 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
854 print "<div dojoType=\"dijit.Toolbar\">";
856 print "<div dojoType=\"dijit.form.DropDownButton\">".
857 "<span>" . __('Select')."</span>";
858 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
859 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
860 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
861 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
862 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
863 print "</div></div>";
865 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
866 __('Add')."</button> ";
868 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
869 __('Delete')."</button> ";
873 print "<ul id='filterDlg_Actions'>";
874 # print "<li>No actions</li>";
877 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
878 print "<div class=\"dlgSecCont\">"; */
880 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
881 <label for=\"enabled\">".__('Enabled')."</label>";
883 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
884 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
886 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
887 <label for=\"inverse\">".__('Inverse matching')."</label>";
891 print "<div class=\"dlgButtons\">";
893 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
894 __('Test')."</button> ";
896 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
897 __('Create')."</button> ";
899 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
900 __('Cancel')."</button>";
907 $rule = json_decode($_REQUEST["rule"], true);
910 $reg_exp = htmlspecialchars($rule["reg_exp"]);
911 $filter_type = $rule["filter_type"];
912 $feed_id = $rule["feed_id"];
913 $inverse_checked = isset($rule["inverse"]) ?
"checked" : "";
918 $inverse_checked = "";
921 if (strpos($feed_id, "CAT:") === 0) {
922 $feed_id = substr($feed_id, 4);
929 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
931 $result = $this->dbh
->query("SELECT id,description
932 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
934 $filter_types = array();
936 while ($line = $this->dbh
->fetch_assoc($result)) {
937 $filter_types[$line["id"]] = __($line["description"]);
940 print "<div class=\"dlgSec\">".__("Match")."</div>";
942 print "<div class=\"dlgSecCont\">";
944 print "<input dojoType=\"dijit.form.ValidationTextBox\"
945 required=\"true\" id=\"filterDlg_regExp\"
946 style=\"font-size : 16px; width : 20em;\"
947 name=\"reg_exp\" value=\"$reg_exp\"/>";
950 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
951 name=\"inverse\" $inverse_checked/>";
952 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
954 print "<hr/>" . __("on field") . " ";
955 print_select_hash("filter_type", $filter_type, $filter_types,
956 'dojoType="dijit.form.Select"');
960 print __("in") . " ";
962 print "<span id='filterDlg_feeds'>";
963 print_feed_select("feed_id",
964 $cat_filter ?
"CAT:$feed_id" : $feed_id,
965 'dojoType="dijit.form.FilteringSelect"');
970 print "<div class=\"dlgButtons\">";
972 print "<div style=\"float : left\">
973 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
977 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
978 ($rule ?
__("Save rule") : __('Add rule'))."</button> ";
980 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
981 __('Cancel')."</button>";
988 function newaction() {
989 $action = json_decode($_REQUEST["action"], true);
992 $action_param = $this->dbh
->escape_string($action["action_param"]);
993 $action_id = (int)$action["action_id"];
999 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
1001 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
1003 print "<div class=\"dlgSecCont\">";
1005 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
1006 onchange=\"filterDlgCheckAction(this)\">";
1008 $result = $this->dbh
->query("SELECT id,description FROM ttrss_filter_actions
1011 while ($line = $this->dbh
->fetch_assoc($result)) {
1012 $is_selected = ($line["id"] == $action_id) ?
"selected='1'" : "";
1013 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
1018 $param_box_hidden = ($action_id == 7 ||
$action_id == 4 ||
$action_id == 6 ||
$action_id == 9) ?
1019 "" : "display : none";
1021 $param_hidden = ($action_id == 4 ||
$action_id == 6) ?
1022 "" : "display : none";
1024 $label_param_hidden = ($action_id == 7) ?
"" : "display : none";
1025 $plugin_param_hidden = ($action_id == 9) ?
"" : "display : none";
1027 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
1029 //print " " . __("with parameters:") . " ";
1030 print "<input dojoType=\"dijit.form.TextBox\"
1031 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
1032 name=\"action_param\" value=\"$action_param\">";
1034 print_label_select("action_param_label", $action_param,
1035 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
1036 dojoType=\"dijit.form.Select\"");
1038 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
1039 $filter_action_hash = array();
1041 foreach ($filter_actions as $fclass => $factions) {
1042 foreach ($factions as $faction) {
1044 $filter_action_hash[$fclass . ":" . $faction["action"]] =
1045 $fclass . ": " . $faction["description"];
1049 if (count($filter_action_hash) == 0) {
1050 $filter_plugin_disabled = "disabled";
1052 $filter_action_hash["no-data"] = __("No actions available");
1055 $filter_plugin_disabled = "";
1058 print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash,
1059 "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled",
1060 "action_param_plugin");
1064 print " "; // tiny layout hack
1068 print "<div class=\"dlgButtons\">";
1070 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
1071 ($action ?
__("Save action") : __('Add action'))."</button> ";
1073 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
1074 __('Cancel')."</button>";
1081 private function getFilterName($id) {
1083 $result = $this->dbh
->query(
1084 "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
1085 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
1086 ON (r.filter_id = f.id)
1087 LEFT JOIN ttrss_filters2_actions AS a
1088 ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title, f.match_any_rule");
1090 $title = $this->dbh
->fetch_result($result, 0, "title");
1091 $num_rules = $this->dbh
->fetch_result($result, 0, "num_rules");
1092 $num_actions = $this->dbh
->fetch_result($result, 0, "num_actions");
1093 $match_any_rule = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "match_any_rule"));
1095 if (!$title) $title = __("[No caption]");
1097 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", (int) $num_rules), $title, $num_rules);
1100 $result = $this->dbh
->query(
1101 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
1105 if ($this->dbh
->num_rows($result) > 0) {
1106 $line = $this->dbh
->fetch_assoc($result);
1107 $actions = $this->getActionName($line);
1112 if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
1114 if ($num_actions > 0)
1115 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", (int) $num_actions), $actions, $num_actions);
1117 return array($title, $actions);
1121 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
1123 if (count($ids) > 1) {
1124 $base_id = array_shift($ids);
1125 $ids_str = join(",", $ids);
1127 $this->dbh
->query("BEGIN");
1128 $this->dbh
->query("UPDATE ttrss_filters2_rules
1129 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1130 $this->dbh
->query("UPDATE ttrss_filters2_actions
1131 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1133 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
1134 $this->dbh
->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
1136 $this->dbh
->query("COMMIT");
1138 $this->optimizeFilter($base_id);
1143 private function optimizeFilter($id) {
1144 $this->dbh
->query("BEGIN");
1145 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
1146 WHERE filter_id = '$id'");
1149 $dupe_ids = array();
1151 while ($line = $this->dbh
->fetch_assoc($result)) {
1155 if (array_search($line, $tmp) === false) {
1156 array_push($tmp, $line);
1158 array_push($dupe_ids, $id);
1162 if (count($dupe_ids) > 0) {
1163 $ids_str = join(",", $dupe_ids);
1164 $this->dbh
->query("DELETE FROM ttrss_filters2_actions
1165 WHERE id IN ($ids_str)");
1168 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
1169 WHERE filter_id = '$id'");
1172 $dupe_ids = array();
1174 while ($line = $this->dbh
->fetch_assoc($result)) {
1178 if (array_search($line, $tmp) === false) {
1179 array_push($tmp, $line);
1181 array_push($dupe_ids, $id);
1185 if (count($dupe_ids) > 0) {
1186 $ids_str = join(",", $dupe_ids);
1187 $this->dbh
->query("DELETE FROM ttrss_filters2_rules
1188 WHERE id IN ($ids_str)");
1191 $this->dbh
->query("COMMIT");