]>
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 require_once "include/rssfuncs.php";
49 $offset = (int) db_escape_string($_REQUEST["offset"]);
50 $limit = (int) db_escape_string($_REQUEST["limit"]);
54 $filter["enabled"] = true;
55 $filter["match_any_rule"] = sql_bool_to_bool(
56 checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["match_any_rule"])));
57 $filter["inverse"] = sql_bool_to_bool(
58 checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["inverse"])));
60 $filter["rules"] = array();
61 $filter["actions"] = array("dummy-action");
63 $result = $this->dbh
->query("SELECT id,name FROM ttrss_filter_types");
65 $filter_types = array();
66 while ($line = $this->dbh
->fetch_assoc($result)) {
67 $filter_types[$line["id"]] = $line["name"];
70 $scope_qparts = array();
73 foreach ($_REQUEST["rule"] AS $r) {
74 $rule = json_decode($r, true);
76 if ($rule && $rctr < 5) {
77 $rule["type"] = $filter_types[$rule["filter_type"]];
78 unset($rule["filter_type"]);
80 if (strpos($rule["feed_id"], "CAT:") === 0) {
81 $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
82 unset($rule["feed_id"]);
85 if (isset($rule["feed_id"]) && $rule['feed_id'] > 0) {
86 array_push($scope_qparts, "feed_id = " . $rule["feed_id"]);
87 } else if (isset($rule["cat_id"])) {
88 array_push($scope_qparts, "cat_id = " . $rule["cat_id"]);
90 array_push($scope_qparts, "true");
93 array_push($filter["rules"], $rule);
101 $glue = $filter['match_any_rule'] ?
" OR " : " AND ";
102 $scope_qpart = join($glue, $scope_qparts);
104 if (!$scope_qpart) $scope_qpart = "true";
108 //while ($found < $limit && $offset < $limit * 1000 && time() - $started < ini_get("max_execution_time") * 0.7) {
110 $result = db_query("SELECT ttrss_entries.id,
112 ttrss_feeds.id AS feed_id,
113 ttrss_feeds.title AS feed_title,
114 ttrss_feed_categories.id AS cat_id,
121 ttrss_entries, ttrss_user_entries
122 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
123 LEFT JOIN ttrss_feed_categories ON (ttrss_feeds.cat_id = ttrss_feed_categories.id)
125 ref_id = ttrss_entries.id AND
127 ttrss_user_entries.owner_uid = " . $_SESSION["uid"] . "
128 ORDER BY date_entered DESC LIMIT $limit OFFSET $offset");
130 while ($line = db_fetch_assoc($result)) {
132 $rc = get_article_filters(array($filter), $line['title'], $line['content'], $line['link'],
133 false, $line['author'], explode(",", $line['tag_cache']));
135 if (count($rc) > 0) {
137 $line["content_preview"] = truncate_string(strip_tags($line["content"]), 100, '...');
139 foreach (PluginHost
::getInstance()->get_hooks(PluginHost
::HOOK_QUERY_HEADLINES
) as $p) {
140 $line = $p->hook_query_headlines($line, 100);
143 $content_preview = $line["content_preview"];
145 if ($line["feed_title"]) $feed_title = "(" . $line["feed_title"] . ")";
147 $tmp = "<tr><td width='5%' align='center'><input dojoType=\"dijit.form.CheckBox\"
148 checked=\"1\" disabled=\"1\" type=\"checkbox\"></td><td>";
150 foreach ($filter['rules'] as $rule) {
151 $reg_exp = str_replace('/', '\/', $rule["reg_exp"]);
153 $line["title"] = preg_replace("/($reg_exp)/i",
154 "<span class=\"highlight\">$1</span>", $line["title"]);
156 $content_preview = preg_replace("/($reg_exp)/i",
157 "<span class=\"highlight\">$1</span>", $content_preview);
160 $tmp .= "<strong>" . $line["title"] . "</strong>";
161 $tmp .= "<div class='small' style='float : right'>" . $feed_title . "</div>";
162 $tmp .= "<div class=\"insensitive\">" . $content_preview . "</div>";
163 $tmp .= " " . mb_substr($line["date_entered"], 0, 16);
164 $tmp .= "</td></tr>";
166 array_push($rv, $tmp);
168 /*array_push($rv, array("title" => $line["title"],
169 "content" => $content_preview,
170 "date" => $line["date_entered"],
171 "feed" => $line["feed_title"])); */
180 print "<tr><td align='center'>" .
181 __("No recent articles matching this filter have been found.");
184 print json_encode($rv);
187 function testFilter() {
189 if (isset($_REQUEST["offset"])) return $this->testFilterDo();
191 //print __("Articles matching this filter:");
193 print "<div><img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'> <span id='prefFilterProgressMsg'>Looking for articles...</span></div>";
195 print "<br/><div class=\"filterTestHolder\">";
196 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefFilterTestResultList\">";
197 print "</table></div>";
199 print "<div style='text-align : center'>";
200 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
201 __('Close this window')."</button>";
206 private function getfilterrules_concise($filter_id) {
207 $result = $this->dbh
->query("SELECT reg_exp,
212 ttrss_filter_types.description AS field
214 ttrss_filters2_rules, ttrss_filter_types
216 filter_id = '$filter_id' AND filter_type = ttrss_filter_types.id");
220 while ($line = $this->dbh
->fetch_assoc($result)) {
222 $where = sql_bool_to_bool($line["cat_filter"]) ?
223 getCategoryTitle($line["cat_id"]) :
225 getFeedTitle($line["feed_id"]) : __("All feeds"));
227 # $where = $line["cat_id"] . "/" . $line["feed_id"];
229 $inverse = sql_bool_to_bool($line["inverse"]) ?
"inverse" : "";
231 $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
232 strip_tags($line["reg_exp"]),
235 sql_bool_to_bool($line["inverse"]) ?
__("(inverse)") : "") . "</span>";
241 function getfiltertree() {
243 $root['id'] = 'root';
244 $root['name'] = __('Filters');
245 $root['items'] = array();
247 $filter_search = $_SESSION["prefs_filter_search"];
249 $result = $this->dbh
->query("SELECT *,
250 (SELECT action_param FROM ttrss_filters2_actions
251 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
252 (SELECT action_id FROM ttrss_filters2_actions
253 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
254 (SELECT description FROM ttrss_filter_actions
255 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
256 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
257 (SELECT reg_exp FROM ttrss_filters2_rules
258 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
259 FROM ttrss_filters2 WHERE
260 owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
264 $folder['items'] = array();
266 while ($line = $this->dbh
->fetch_assoc($result)) {
268 $name = $this->getFilterName($line["id"]);
271 if ($filter_search) {
272 $rules_result = $this->dbh
->query(
273 "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
275 while ($rule_line = $this->dbh
->fetch_assoc($rules_result)) {
276 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
283 if ($line['action_id'] == 7) {
284 $label_result = $this->dbh
->query("SELECT fg_color, bg_color
285 FROM ttrss_labels2 WHERE caption = '".$this->dbh
->escape_string($line['action_param'])."' AND
286 owner_uid = " . $_SESSION["uid"]);
288 if ($this->dbh
->num_rows($label_result) > 0) {
289 $fg_color = $this->dbh
->fetch_result($label_result, 0, "fg_color");
290 $bg_color = $this->dbh
->fetch_result($label_result, 0, "bg_color");
292 $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>α</span>" . $name[1];
297 $filter['id'] = 'FILTER:' . $line['id'];
298 $filter['bare_id'] = $line['id'];
299 $filter['name'] = $name[0];
300 $filter['param'] = $name[1];
301 $filter['checkbox'] = false;
302 $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
303 $filter['rules'] = $this->getfilterrules_concise($line['id']);
305 if (!$filter_search ||
$match_ok) {
306 array_push($folder['items'], $filter);
310 /* if (count($folder['items']) > 0) {
311 array_push($root['items'], $folder);
314 $root['items'] = $folder['items'];
317 $fl['identifier'] = 'id';
318 $fl['label'] = 'name';
319 $fl['items'] = array($root);
321 print json_encode($fl);
327 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
329 $result = $this->dbh
->query(
330 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
332 $enabled = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "enabled"));
333 $match_any_rule = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "match_any_rule"));
334 $inverse = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "inverse"));
335 $title = htmlspecialchars($this->dbh
->fetch_result($result, 0, "title"));
337 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
339 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
340 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
341 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
342 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
344 print "<div class=\"dlgSec\">".__("Caption")."</div>";
346 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
350 print "<div class=\"dlgSec\">".__("Match")."</div>";
352 print "<div dojoType=\"dijit.Toolbar\">";
354 print "<div dojoType=\"dijit.form.DropDownButton\">".
355 "<span>" . __('Select')."</span>";
356 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
357 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
358 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
359 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
360 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
361 print "</div></div>";
363 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
364 __('Add')."</button> ";
366 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
367 __('Delete')."</button> ";
371 print "<ul id='filterDlg_Matches'>";
373 $rules_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
374 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
376 while ($line = $this->dbh
->fetch_assoc($rules_result)) {
377 if (sql_bool_to_bool($line["cat_filter"])) {
378 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
381 unset($line["cat_filter"]);
382 unset($line["cat_id"]);
383 unset($line["filter_id"]);
385 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
387 $data = htmlspecialchars(json_encode($line));
389 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
390 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
391 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
398 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
400 print "<div dojoType=\"dijit.Toolbar\">";
402 print "<div dojoType=\"dijit.form.DropDownButton\">".
403 "<span>" . __('Select')."</span>";
404 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
405 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
406 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
407 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
408 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
409 print "</div></div>";
411 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
412 __('Add')."</button> ";
414 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
415 __('Delete')."</button> ";
419 print "<ul id='filterDlg_Actions'>";
421 $actions_result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
422 WHERE filter_id = '$filter_id' ORDER BY id");
424 while ($line = $this->dbh
->fetch_assoc($actions_result)) {
425 $line["action_param_label"] = $line["action_param"];
427 unset($line["filter_id"]);
430 $data = htmlspecialchars(json_encode($line));
432 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
433 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
434 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
442 $checked = "checked=\"1\"";
447 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
448 <label for=\"enabled\">".__('Enabled')."</label>";
450 if ($match_any_rule) {
451 $checked = "checked=\"1\"";
456 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
457 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
460 $checked = "checked=\"1\"";
465 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
466 <label for=\"inverse\">".__('Inverse matching')."</label>";
470 print "<div class=\"dlgButtons\">";
472 print "<div style=\"float : left\">";
473 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
474 __('Remove')."</button>";
477 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
478 __('Test')."</button> ";
480 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
481 __('Save')."</button> ";
483 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
484 __('Cancel')."</button>";
489 private function getRuleName($rule) {
490 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
492 $feed_id = $rule["feed_id"];
494 if (strpos($feed_id, "CAT:") === 0) {
495 $feed_id = (int) substr($feed_id, 4);
496 $feed = getCategoryTitle($feed_id);
498 $feed_id = (int) $feed_id;
500 if ($rule["feed_id"])
501 $feed = getFeedTitle((int)$rule["feed_id"]);
503 $feed = __("All feeds");
506 $result = $this->dbh
->query("SELECT description FROM ttrss_filter_types
507 WHERE id = ".(int)$rule["filter_type"]);
508 $filter_type = $this->dbh
->fetch_result($result, 0, "description");
510 $inverse = isset($rule["inverse"]) ?
"inverse" : "";
512 return "<span class='filterRule $inverse'>" .
513 T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
514 $filter_type, $feed, isset($rule["inverse"]) ?
__("(inverse)") : "") . "</span>";
517 function printRuleName() {
518 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
521 private function getActionName($action) {
522 $result = $this->dbh
->query("SELECT description FROM
523 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
525 $title = __($this->dbh
->fetch_result($result, 0, "description"));
527 if ($action["action_id"] == 4 ||
$action["action_id"] == 6 ||
528 $action["action_id"] == 7)
529 $title .= ": " . $action["action_param"];
531 if ($action["action_id"] == 9) {
532 list ($pfclass, $pfaction) = explode(":", $action["action_param"]);
534 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
536 foreach ($filter_actions as $fclass => $factions) {
537 foreach ($factions as $faction) {
538 if ($pfaction == $faction["action"] && $pfclass == $fclass) {
539 $title .= ": " . $fclass . ": " . $faction["description"];
549 function printActionName() {
550 print $this->getActionName(json_decode($_REQUEST["action"], true));
553 function editSave() {
554 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
555 return $this->testFilter();
558 # print_r($_REQUEST);
560 $filter_id = $this->dbh
->escape_string($_REQUEST["id"]);
561 $enabled = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["enabled"]));
562 $match_any_rule = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["match_any_rule"]));
563 $inverse = checkbox_to_sql_bool($this->dbh
->escape_string($_REQUEST["inverse"]));
564 $title = $this->dbh
->escape_string($_REQUEST["title"]);
566 $this->dbh
->query("UPDATE ttrss_filters2 SET enabled = $enabled,
567 match_any_rule = $match_any_rule,
570 WHERE id = '$filter_id'
571 AND owner_uid = ". $_SESSION["uid"]);
573 $this->saveRulesAndActions($filter_id);
579 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
581 foreach ($ids as $id) {
582 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
586 private function saveRulesAndActions($filter_id) {
588 $this->dbh
->query("DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
589 $this->dbh
->query("DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
597 foreach ($_REQUEST["rule"] as $rule) {
598 $rule = json_decode($rule, true);
601 if (array_search($rule, $rules) === false) {
602 array_push($rules, $rule);
606 foreach ($_REQUEST["action"] as $action) {
607 $action = json_decode($action, true);
608 unset($action["id"]);
610 if (array_search($action, $actions) === false) {
611 array_push($actions, $action);
615 foreach ($rules as $rule) {
618 $reg_exp = strip_tags($this->dbh
->escape_string(trim($rule["reg_exp"])));
619 $inverse = isset($rule["inverse"]) ?
"true" : "false";
621 $filter_type = (int) $this->dbh
->escape_string(trim($rule["filter_type"]));
622 $feed_id = $this->dbh
->escape_string(trim($rule["feed_id"]));
624 if (strpos($feed_id, "CAT:") === 0) {
626 $cat_filter = bool_to_sql_bool(true);
627 $cat_id = (int) substr($feed_id, 4);
630 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
632 $cat_filter = bool_to_sql_bool(false);
633 $feed_id = (int) $feed_id;
636 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
639 $query = "INSERT INTO ttrss_filters2_rules
640 (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
641 ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
643 $this->dbh
->query($query);
647 foreach ($actions as $action) {
650 $action_id = (int) $this->dbh
->escape_string($action["action_id"]);
651 $action_param = $this->dbh
->escape_string($action["action_param"]);
652 $action_param_label = $this->dbh
->escape_string($action["action_param_label"]);
654 if ($action_id == 7) {
655 $action_param = $action_param_label;
658 if ($action_id == 6) {
659 $action_param = (int) str_replace("+", "", $action_param);
662 $query = "INSERT INTO ttrss_filters2_actions
663 (filter_id, action_id, action_param) VALUES
664 ('$filter_id', '$action_id', '$action_param')";
666 $this->dbh
->query($query);
675 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
676 return $this->testFilter();
679 # print_r($_REQUEST);
681 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
682 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
683 $title = $this->dbh
->escape_string($_REQUEST["title"]);
684 $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
686 $this->dbh
->query("BEGIN");
688 /* create base filter */
690 $result = $this->dbh
->query("INSERT INTO ttrss_filters2
691 (owner_uid, match_any_rule, enabled, title, inverse) VALUES
692 (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title', $inverse)");
694 $result = $this->dbh
->query("SELECT MAX(id) AS id FROM ttrss_filters2
695 WHERE owner_uid = ".$_SESSION["uid"]);
697 $filter_id = $this->dbh
->fetch_result($result, 0, "id");
699 $this->saveRulesAndActions($filter_id);
701 $this->dbh
->query("COMMIT");
706 $sort = $this->dbh
->escape_string($_REQUEST["sort"]);
708 if (!$sort ||
$sort == "undefined") {
712 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
714 if (array_key_exists("search", $_REQUEST)) {
715 $_SESSION["prefs_filter_search"] = $filter_search;
717 $filter_search = $_SESSION["prefs_filter_search"];
720 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
721 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
722 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
724 $filter_search = $this->dbh
->escape_string($_REQUEST["search"]);
726 if (array_key_exists("search", $_REQUEST)) {
727 $_SESSION["prefs_filter_search"] = $filter_search;
729 $filter_search = $_SESSION["prefs_filter_search"];
732 print "<div style='float : right; padding-right : 4px;'>
733 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
734 value=\"$filter_search\">
735 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
736 __('Search')."</button>
739 print "<div dojoType=\"dijit.form.DropDownButton\">".
740 "<span>" . __('Select')."</span>";
741 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
742 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
743 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
744 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
745 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
746 print "</div></div>";
748 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
749 __('Create filter')."</button> ";
751 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
752 __('Combine')."</button> ";
754 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
755 __('Edit')."</button> ";
757 print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
758 __('Reset sort order')."</button> ";
761 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
762 __('Remove')."</button> ";
764 if (defined('_ENABLE_FEED_DEBUGGING')) {
765 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
766 __('Rescore articles')."</button> ";
769 print "</div>"; # toolbar
770 print "</div>"; # toolbar-frame
771 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
773 print "<div id=\"filterlistLoading\">
774 <img src='images/indicator_tiny.gif'>".
775 __("Loading, please wait...")."</div>";
777 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
778 url=\"backend.php?op=pref-filters&method=getfiltertree\">
780 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
781 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
782 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
784 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
785 dndController=\"dijit.tree.dndSource\"
786 betweenThreshold=\"5\"
787 model=\"filterModel\" openOnClick=\"true\">
788 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
789 Element.hide(\"filterlistLoading\");
791 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
792 var id = String(item.id);
793 var bare_id = id.substr(id.indexOf(':')+1);
795 if (id.match('FILTER:')) {
802 print "</div>"; #pane
804 PluginHost
::getInstance()->run_hooks(PluginHost
::HOOK_PREFS_TAB
,
805 "hook_prefs_tab", "prefFilters");
807 print "</div>"; #container
811 function newfilter() {
813 print "<form name='filter_new_form' id='filter_new_form'>";
815 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
816 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
817 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
819 print "<div class=\"dlgSec\">".__("Caption")."</div>";
821 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
823 print "<div class=\"dlgSec\">".__("Match")."</div>";
825 print "<div dojoType=\"dijit.Toolbar\">";
827 print "<div dojoType=\"dijit.form.DropDownButton\">".
828 "<span>" . __('Select')."</span>";
829 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
830 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
831 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
832 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
833 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
834 print "</div></div>";
836 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
837 __('Add')."</button> ";
839 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
840 __('Delete')."</button> ";
844 print "<ul id='filterDlg_Matches'>";
845 # print "<li>No rules</li>";
850 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
852 print "<div dojoType=\"dijit.Toolbar\">";
854 print "<div dojoType=\"dijit.form.DropDownButton\">".
855 "<span>" . __('Select')."</span>";
856 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
857 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
858 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
859 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
860 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
861 print "</div></div>";
863 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
864 __('Add')."</button> ";
866 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
867 __('Delete')."</button> ";
871 print "<ul id='filterDlg_Actions'>";
872 # print "<li>No actions</li>";
875 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
876 print "<div class=\"dlgSecCont\">"; */
878 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
879 <label for=\"enabled\">".__('Enabled')."</label>";
881 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
882 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
884 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
885 <label for=\"inverse\">".__('Inverse matching')."</label>";
889 print "<div class=\"dlgButtons\">";
891 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
892 __('Test')."</button> ";
894 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
895 __('Create')."</button> ";
897 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
898 __('Cancel')."</button>";
905 $rule = json_decode($_REQUEST["rule"], true);
908 $reg_exp = htmlspecialchars($rule["reg_exp"]);
909 $filter_type = $rule["filter_type"];
910 $feed_id = $rule["feed_id"];
911 $inverse_checked = isset($rule["inverse"]) ?
"checked" : "";
916 $inverse_checked = "";
919 if (strpos($feed_id, "CAT:") === 0) {
920 $feed_id = substr($feed_id, 4);
927 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
929 $result = $this->dbh
->query("SELECT id,description
930 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
932 $filter_types = array();
934 while ($line = $this->dbh
->fetch_assoc($result)) {
935 $filter_types[$line["id"]] = __($line["description"]);
938 print "<div class=\"dlgSec\">".__("Match")."</div>";
940 print "<div class=\"dlgSecCont\">";
942 print "<input dojoType=\"dijit.form.ValidationTextBox\"
943 required=\"true\" id=\"filterDlg_regExp\"
944 style=\"font-size : 16px; width : 20em;\"
945 name=\"reg_exp\" value=\"$reg_exp\"/>";
948 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
949 name=\"inverse\" $inverse_checked/>";
950 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
952 print "<hr/>" . __("on field") . " ";
953 print_select_hash("filter_type", $filter_type, $filter_types,
954 'dojoType="dijit.form.Select"');
958 print __("in") . " ";
960 print "<span id='filterDlg_feeds'>";
961 print_feed_select("feed_id",
962 $cat_filter ?
"CAT:$feed_id" : $feed_id,
963 'dojoType="dijit.form.FilteringSelect"');
968 print "<div class=\"dlgButtons\">";
970 print "<div style=\"float : left\">
971 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
975 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
976 ($rule ?
__("Save rule") : __('Add rule'))."</button> ";
978 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
979 __('Cancel')."</button>";
986 function newaction() {
987 $action = json_decode($_REQUEST["action"], true);
990 $action_param = $this->dbh
->escape_string($action["action_param"]);
991 $action_id = (int)$action["action_id"];
997 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
999 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
1001 print "<div class=\"dlgSecCont\">";
1003 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
1004 onchange=\"filterDlgCheckAction(this)\">";
1006 $result = $this->dbh
->query("SELECT id,description FROM ttrss_filter_actions
1009 while ($line = $this->dbh
->fetch_assoc($result)) {
1010 $is_selected = ($line["id"] == $action_id) ?
"selected='1'" : "";
1011 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
1016 $param_box_hidden = ($action_id == 7 ||
$action_id == 4 ||
$action_id == 6 ||
$action_id == 9) ?
1017 "" : "display : none";
1019 $param_hidden = ($action_id == 4 ||
$action_id == 6) ?
1020 "" : "display : none";
1022 $label_param_hidden = ($action_id == 7) ?
"" : "display : none";
1023 $plugin_param_hidden = ($action_id == 9) ?
"" : "display : none";
1025 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
1027 //print " " . __("with parameters:") . " ";
1028 print "<input dojoType=\"dijit.form.TextBox\"
1029 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
1030 name=\"action_param\" value=\"$action_param\">";
1032 print_label_select("action_param_label", $action_param,
1033 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
1034 dojoType=\"dijit.form.Select\"");
1036 $filter_actions = PluginHost
::getInstance()->get_filter_actions();
1037 $filter_action_hash = array();
1039 foreach ($filter_actions as $fclass => $factions) {
1040 foreach ($factions as $faction) {
1042 $filter_action_hash[$fclass . ":" . $faction["action"]] =
1043 $fclass . ": " . $faction["description"];
1047 if (count($filter_action_hash) == 0) {
1048 $filter_plugin_disabled = "disabled";
1050 $filter_action_hash["no-data"] = __("No actions available");
1053 $filter_plugin_disabled = "";
1056 print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash,
1057 "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled",
1058 "action_param_plugin");
1062 print " "; // tiny layout hack
1066 print "<div class=\"dlgButtons\">";
1068 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
1069 ($action ?
__("Save action") : __('Add action'))."</button> ";
1071 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
1072 __('Cancel')."</button>";
1079 private function getFilterName($id) {
1081 $result = $this->dbh
->query(
1082 "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
1083 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
1084 ON (r.filter_id = f.id)
1085 LEFT JOIN ttrss_filters2_actions AS a
1086 ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title, f.match_any_rule");
1088 $title = $this->dbh
->fetch_result($result, 0, "title");
1089 $num_rules = $this->dbh
->fetch_result($result, 0, "num_rules");
1090 $num_actions = $this->dbh
->fetch_result($result, 0, "num_actions");
1091 $match_any_rule = sql_bool_to_bool($this->dbh
->fetch_result($result, 0, "match_any_rule"));
1093 if (!$title) $title = __("[No caption]");
1095 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", $num_rules), $title, $num_rules);
1098 $result = $this->dbh
->query(
1099 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
1103 if ($this->dbh
->num_rows($result) > 0) {
1104 $line = $this->dbh
->fetch_assoc($result);
1105 $actions = $this->getActionName($line);
1110 if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
1112 if ($num_actions > 0)
1113 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", $num_actions), $actions, $num_actions);
1115 return array($title, $actions);
1119 $ids = explode(",", $this->dbh
->escape_string($_REQUEST["ids"]));
1121 if (count($ids) > 1) {
1122 $base_id = array_shift($ids);
1123 $ids_str = join(",", $ids);
1125 $this->dbh
->query("BEGIN");
1126 $this->dbh
->query("UPDATE ttrss_filters2_rules
1127 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1128 $this->dbh
->query("UPDATE ttrss_filters2_actions
1129 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1131 $this->dbh
->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
1132 $this->dbh
->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
1134 $this->dbh
->query("COMMIT");
1136 $this->optimizeFilter($base_id);
1141 private function optimizeFilter($id) {
1142 $this->dbh
->query("BEGIN");
1143 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_actions
1144 WHERE filter_id = '$id'");
1147 $dupe_ids = array();
1149 while ($line = $this->dbh
->fetch_assoc($result)) {
1153 if (array_search($line, $tmp) === false) {
1154 array_push($tmp, $line);
1156 array_push($dupe_ids, $id);
1160 if (count($dupe_ids) > 0) {
1161 $ids_str = join(",", $dupe_ids);
1162 $this->dbh
->query("DELETE FROM ttrss_filters2_actions
1163 WHERE id IN ($ids_str)");
1166 $result = $this->dbh
->query("SELECT * FROM ttrss_filters2_rules
1167 WHERE filter_id = '$id'");
1170 $dupe_ids = array();
1172 while ($line = $this->dbh
->fetch_assoc($result)) {
1176 if (array_search($line, $tmp) === false) {
1177 array_push($tmp, $line);
1179 array_push($dupe_ids, $id);
1183 if (count($dupe_ids) > 0) {
1184 $ids_str = join(",", $dupe_ids);
1185 $this->dbh
->query("DELETE FROM ttrss_filters2_rules
1186 WHERE id IN ($ids_str)");
1189 $this->dbh
->query("COMMIT");