]> git.wh0rd.org - tt-rss.git/blob - classes/pref/filters.php
cfc20e4192fa7f38c7150a5034e8f67604d21387
[tt-rss.git] / classes / pref / filters.php
1 <?php
2 class Pref_Filters extends Handler_Protected {
3
4 function csrf_ignore($method) {
5 $csrf_ignored = array("index", "getfiltertree", "edit", "newfilter", "newrule",
6 "newaction", "savefilterorder");
7
8 return array_search($method, $csrf_ignored) !== false;
9 }
10
11 function filtersortreset() {
12 $this->dbh->query("UPDATE ttrss_filters2
13 SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
14 return;
15 }
16
17 function savefilterorder() {
18 $data = json_decode($_POST['payload'], true);
19
20 #file_put_contents("/tmp/saveorder.json", $_POST['payload']);
21 #$data = json_decode(file_get_contents("/tmp/saveorder.json"), true);
22
23 if (!is_array($data['items']))
24 $data['items'] = json_decode($data['items'], true);
25
26 $index = 0;
27
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']);
31
32 if ($filter_id > 0) {
33
34 $this->dbh->query("UPDATE ttrss_filters2 SET
35 order_id = $index WHERE id = '$filter_id' AND
36 owner_uid = " .$_SESSION["uid"]);
37
38 ++$index;
39 }
40 }
41 }
42
43 return;
44 }
45
46
47 function testFilter() {
48 $filter = array();
49
50 $filter["enabled"] = true;
51 $filter["match_any_rule"] = sql_bool_to_bool(
52 checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["match_any_rule"])));
53 $filter["inverse"] = sql_bool_to_bool(
54 checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["inverse"])));
55
56 $filter["rules"] = array();
57
58 $result = $this->dbh->query("SELECT id,name FROM ttrss_filter_types");
59
60 $filter_types = array();
61 while ($line = $this->dbh->fetch_assoc($result)) {
62 $filter_types[$line["id"]] = $line["name"];
63 }
64
65 $rctr = 0;
66 foreach ($_REQUEST["rule"] AS $r) {
67 $rule = json_decode($r, true);
68
69 if ($rule && $rctr < 5) {
70 $rule["type"] = $filter_types[$rule["filter_type"]];
71 unset($rule["filter_type"]);
72
73 if (strpos($rule["feed_id"], "CAT:") === 0) {
74 $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
75 unset($rule["feed_id"]);
76 }
77
78 array_push($filter["rules"], $rule);
79
80 ++$rctr;
81 } else {
82 break;
83 }
84 }
85
86 $qfh_ret = queryFeedHeadlines(-4, 30, "", false, false, false,
87 "date_entered DESC", 0, $_SESSION["uid"], $filter);
88
89 $result = $qfh_ret[0];
90
91 $found = 0;
92
93 print __("Articles matching this filter:");
94
95 print "<div class=\"filterTestHolder\">";
96 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">";
97
98 while ($line = $this->dbh->fetch_assoc($result)) {
99 $line["content_preview"] = truncate_string(strip_tags($line["content"]), 100, '...');
100
101 foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
102 $line = $p->hook_query_headlines($line, 100);
103 }
104
105 $content_preview = $line["content_preview"];
106
107 if ($line["feed_title"])
108 $feed_title = $line["feed_title"];
109
110 print "<tr>";
111
112 print "<td width='5%' align='center'><input
113 dojoType=\"dijit.form.CheckBox\" checked=\"1\"
114 disabled=\"1\" type=\"checkbox\"></td>";
115 print "<td>";
116
117 print $line["title"];
118 print "&nbsp;(";
119 print "<b>" . $feed_title . "</b>";
120 print "):&nbsp;";
121 print "<span class=\"insensitive\">" . $content_preview . "</span>";
122 print " " . mb_substr($line["date_entered"], 0, 16);
123
124 print "</td></tr>";
125
126 $found++;
127 }
128
129 if ($found == 0) {
130 print "<tr><td align='center'>" .
131 __("No recent articles matching this filter have been found.");
132
133 print "</td></tr><tr><td class='insensitive' align='center'>";
134
135 print __("Complex expressions might not give results while testing due to issues with database server regexp implementation.");
136
137 print "</td></tr>";
138
139 }
140
141 print "</table></div>";
142
143 print "<div style='text-align : center'>";
144 print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
145 __('Close this window')."</button>";
146 print "</div>";
147
148 }
149
150 private function getfilterrules_concise($filter_id) {
151 $result = $this->dbh->query("SELECT reg_exp,
152 inverse,
153 feed_id,
154 cat_id,
155 cat_filter,
156 ttrss_filter_types.description AS field
157 FROM
158 ttrss_filters2_rules, ttrss_filter_types
159 WHERE
160 filter_id = '$filter_id' AND filter_type = ttrss_filter_types.id");
161
162 $rv = "";
163
164 while ($line = $this->dbh->fetch_assoc($result)) {
165
166 $where = sql_bool_to_bool($line["cat_filter"]) ?
167 getCategoryTitle($line["cat_id"]) : getFeedTitle($line["feed_id"]);
168
169 # $where = $line["cat_id"] . "/" . $line["feed_id"];
170
171 $inverse = sql_bool_to_bool($line["inverse"]) ? "inverse" : "";
172
173 $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
174 strip_tags($line["reg_exp"]),
175 $line["field"],
176 $where,
177 sql_bool_to_bool($line["inverse"]) ? __("(inverse)") : "") . "</span>";
178 }
179
180 return $rv;
181 }
182
183 function getfiltertree() {
184 $root = array();
185 $root['id'] = 'root';
186 $root['name'] = __('Filters');
187 $root['items'] = array();
188
189 $filter_search = $_SESSION["prefs_filter_search"];
190
191 $result = $this->dbh->query("SELECT *,
192 (SELECT action_param FROM ttrss_filters2_actions
193 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
194 (SELECT action_id FROM ttrss_filters2_actions
195 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
196 (SELECT description FROM ttrss_filter_actions
197 WHERE id = (SELECT action_id FROM ttrss_filters2_actions
198 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
199 (SELECT reg_exp FROM ttrss_filters2_rules
200 WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
201 FROM ttrss_filters2 WHERE
202 owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
203
204
205 $folder = array();
206 $folder['items'] = array();
207
208 while ($line = $this->dbh->fetch_assoc($result)) {
209
210 $name = $this->getFilterName($line["id"]);
211
212 $match_ok = false;
213 if ($filter_search) {
214 $rules_result = $this->dbh->query(
215 "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
216
217 while ($rule_line = $this->dbh->fetch_assoc($rules_result)) {
218 if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
219 $match_ok = true;
220 break;
221 }
222 }
223 }
224
225 if ($line['action_id'] == 7) {
226 $label_result = $this->dbh->query("SELECT fg_color, bg_color
227 FROM ttrss_labels2 WHERE caption = '".$this->dbh->escape_string($line['action_param'])."' AND
228 owner_uid = " . $_SESSION["uid"]);
229
230 if ($this->dbh->num_rows($label_result) > 0) {
231 $fg_color = $this->dbh->fetch_result($label_result, 0, "fg_color");
232 $bg_color = $this->dbh->fetch_result($label_result, 0, "bg_color");
233
234 $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>&alpha;</span>" . $name[1];
235 }
236 }
237
238 $filter = array();
239 $filter['id'] = 'FILTER:' . $line['id'];
240 $filter['bare_id'] = $line['id'];
241 $filter['name'] = $name[0];
242 $filter['param'] = $name[1];
243 $filter['checkbox'] = false;
244 $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
245 $filter['rules'] = $this->getfilterrules_concise($line['id']);
246
247 if (!$filter_search || $match_ok) {
248 array_push($folder['items'], $filter);
249 }
250 }
251
252 /* if (count($folder['items']) > 0) {
253 array_push($root['items'], $folder);
254 } */
255
256 $root['items'] = $folder['items'];
257
258 $fl = array();
259 $fl['identifier'] = 'id';
260 $fl['label'] = 'name';
261 $fl['items'] = array($root);
262
263 print json_encode($fl);
264 return;
265 }
266
267 function edit() {
268
269 $filter_id = $this->dbh->escape_string($_REQUEST["id"]);
270
271 $result = $this->dbh->query(
272 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
273
274 $enabled = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "enabled"));
275 $match_any_rule = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "match_any_rule"));
276 $inverse = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "inverse"));
277 $title = htmlspecialchars($this->dbh->fetch_result($result, 0, "title"));
278
279 print "<form id=\"filter_edit_form\" onsubmit='return false'>";
280
281 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
282 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
283 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
284 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
285
286 print "<div class=\"dlgSec\">".__("Caption")."</div>";
287
288 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
289
290 print "</div>";
291
292 print "<div class=\"dlgSec\">".__("Match")."</div>";
293
294 print "<div dojoType=\"dijit.Toolbar\">";
295
296 print "<div dojoType=\"dijit.form.DropDownButton\">".
297 "<span>" . __('Select')."</span>";
298 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
299 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
300 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
301 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
302 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
303 print "</div></div>";
304
305 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
306 __('Add')."</button> ";
307
308 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
309 __('Delete')."</button> ";
310
311 print "</div>";
312
313 print "<ul id='filterDlg_Matches'>";
314
315 $rules_result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
316 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
317
318 while ($line = $this->dbh->fetch_assoc($rules_result)) {
319 if (sql_bool_to_bool($line["cat_filter"])) {
320 $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
321 }
322
323 unset($line["cat_filter"]);
324 unset($line["cat_id"]);
325 unset($line["filter_id"]);
326 unset($line["id"]);
327 if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
328
329 $data = htmlspecialchars(json_encode($line));
330
331 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
332 "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
333 "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
334 }
335
336 print "</ul>";
337
338 print "</div>";
339
340 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
341
342 print "<div dojoType=\"dijit.Toolbar\">";
343
344 print "<div dojoType=\"dijit.form.DropDownButton\">".
345 "<span>" . __('Select')."</span>";
346 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
347 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
348 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
349 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
350 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
351 print "</div></div>";
352
353 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
354 __('Add')."</button> ";
355
356 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
357 __('Delete')."</button> ";
358
359 print "</div>";
360
361 print "<ul id='filterDlg_Actions'>";
362
363 $actions_result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
364 WHERE filter_id = '$filter_id' ORDER BY id");
365
366 while ($line = $this->dbh->fetch_assoc($actions_result)) {
367 $line["action_param_label"] = $line["action_param"];
368
369 unset($line["filter_id"]);
370 unset($line["id"]);
371
372 $data = htmlspecialchars(json_encode($line));
373
374 print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
375 "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
376 "<input type='hidden' name='action[]' value=\"$data\"/></li>";
377 }
378
379 print "</ul>";
380
381 print "</div>";
382
383 if ($enabled) {
384 $checked = "checked=\"1\"";
385 } else {
386 $checked = "";
387 }
388
389 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
390 <label for=\"enabled\">".__('Enabled')."</label>";
391
392 if ($match_any_rule) {
393 $checked = "checked=\"1\"";
394 } else {
395 $checked = "";
396 }
397
398 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
399 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
400
401 if ($inverse) {
402 $checked = "checked=\"1\"";
403 } else {
404 $checked = "";
405 }
406
407 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
408 <label for=\"inverse\">".__('Inverse matching')."</label>";
409
410 print "<p/>";
411
412 print "<div class=\"dlgButtons\">";
413
414 print "<div style=\"float : left\">";
415 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
416 __('Remove')."</button>";
417 print "</div>";
418
419 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
420 __('Test')."</button> ";
421
422 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
423 __('Save')."</button> ";
424
425 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
426 __('Cancel')."</button>";
427
428 print "</div>";
429 }
430
431 private function getRuleName($rule) {
432 if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
433
434 $feed_id = $rule["feed_id"];
435
436 if (strpos($feed_id, "CAT:") === 0) {
437 $feed_id = (int) substr($feed_id, 4);
438 $feed = getCategoryTitle($feed_id);
439 } else {
440 $feed_id = (int) $feed_id;
441
442 if ($rule["feed_id"])
443 $feed = getFeedTitle((int)$rule["feed_id"]);
444 else
445 $feed = __("All feeds");
446 }
447
448 $result = $this->dbh->query("SELECT description FROM ttrss_filter_types
449 WHERE id = ".(int)$rule["filter_type"]);
450 $filter_type = $this->dbh->fetch_result($result, 0, "description");
451
452 $inverse = isset($rule["inverse"]) ? "inverse" : "";
453
454 return "<span class='filterRule $inverse'>" .
455 T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
456 $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "") . "</span>";
457 }
458
459 function printRuleName() {
460 print $this->getRuleName(json_decode($_REQUEST["rule"], true));
461 }
462
463 private function getActionName($action) {
464 $result = $this->dbh->query("SELECT description FROM
465 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
466
467 $title = __($this->dbh->fetch_result($result, 0, "description"));
468
469 if ($action["action_id"] == 4 || $action["action_id"] == 6 ||
470 $action["action_id"] == 7)
471 $title .= ": " . $action["action_param"];
472
473 return $title;
474 }
475
476 function printActionName() {
477 print $this->getActionName(json_decode($_REQUEST["action"], true));
478 }
479
480 function editSave() {
481 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
482 return $this->testFilter();
483 }
484
485 # print_r($_REQUEST);
486
487 $filter_id = $this->dbh->escape_string($_REQUEST["id"]);
488 $enabled = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["enabled"]));
489 $match_any_rule = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["match_any_rule"]));
490 $inverse = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["inverse"]));
491 $title = $this->dbh->escape_string($_REQUEST["title"]);
492
493 $this->dbh->query("UPDATE ttrss_filters2 SET enabled = $enabled,
494 match_any_rule = $match_any_rule,
495 inverse = $inverse,
496 title = '$title'
497 WHERE id = '$filter_id'
498 AND owner_uid = ". $_SESSION["uid"]);
499
500 $this->saveRulesAndActions($filter_id);
501
502 }
503
504 function remove() {
505
506 $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
507
508 foreach ($ids as $id) {
509 $this->dbh->query("DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
510 }
511 }
512
513 private function saveRulesAndActions($filter_id) {
514
515 $this->dbh->query("DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
516 $this->dbh->query("DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
517
518 if ($filter_id) {
519 /* create rules */
520
521 $rules = array();
522 $actions = array();
523
524 foreach ($_REQUEST["rule"] as $rule) {
525 $rule = json_decode($rule, true);
526 unset($rule["id"]);
527
528 if (array_search($rule, $rules) === false) {
529 array_push($rules, $rule);
530 }
531 }
532
533 foreach ($_REQUEST["action"] as $action) {
534 $action = json_decode($action, true);
535 unset($action["id"]);
536
537 if (array_search($action, $actions) === false) {
538 array_push($actions, $action);
539 }
540 }
541
542 foreach ($rules as $rule) {
543 if ($rule) {
544
545 $reg_exp = strip_tags($this->dbh->escape_string(trim($rule["reg_exp"])));
546 $inverse = isset($rule["inverse"]) ? "true" : "false";
547
548 $filter_type = (int) $this->dbh->escape_string(trim($rule["filter_type"]));
549 $feed_id = $this->dbh->escape_string(trim($rule["feed_id"]));
550
551 if (strpos($feed_id, "CAT:") === 0) {
552
553 $cat_filter = bool_to_sql_bool(true);
554 $cat_id = (int) substr($feed_id, 4);
555 $feed_id = "NULL";
556
557 if (!$cat_id) $cat_id = "NULL"; // Uncategorized
558 } else {
559 $cat_filter = bool_to_sql_bool(false);
560 $feed_id = (int) $feed_id;
561 $cat_id = "NULL";
562
563 if (!$feed_id) $feed_id = "NULL"; // Uncategorized
564 }
565
566 $query = "INSERT INTO ttrss_filters2_rules
567 (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
568 ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
569
570 $this->dbh->query($query);
571 }
572 }
573
574 foreach ($actions as $action) {
575 if ($action) {
576
577 $action_id = (int) $this->dbh->escape_string($action["action_id"]);
578 $action_param = $this->dbh->escape_string($action["action_param"]);
579 $action_param_label = $this->dbh->escape_string($action["action_param_label"]);
580
581 if ($action_id == 7) {
582 $action_param = $action_param_label;
583 }
584
585 if ($action_id == 6) {
586 $action_param = (int) str_replace("+", "", $action_param);
587 }
588
589 $query = "INSERT INTO ttrss_filters2_actions
590 (filter_id, action_id, action_param) VALUES
591 ('$filter_id', '$action_id', '$action_param')";
592
593 $this->dbh->query($query);
594 }
595 }
596 }
597
598
599 }
600
601 function add() {
602 if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
603 return $this->testFilter();
604 }
605
606 # print_r($_REQUEST);
607
608 $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
609 $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
610 $title = $this->dbh->escape_string($_REQUEST["title"]);
611 $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
612
613 $this->dbh->query("BEGIN");
614
615 /* create base filter */
616
617 $result = $this->dbh->query("INSERT INTO ttrss_filters2
618 (owner_uid, match_any_rule, enabled, title, inverse) VALUES
619 (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title', $inverse)");
620
621 $result = $this->dbh->query("SELECT MAX(id) AS id FROM ttrss_filters2
622 WHERE owner_uid = ".$_SESSION["uid"]);
623
624 $filter_id = $this->dbh->fetch_result($result, 0, "id");
625
626 $this->saveRulesAndActions($filter_id);
627
628 $this->dbh->query("COMMIT");
629 }
630
631 function index() {
632
633 $sort = $this->dbh->escape_string($_REQUEST["sort"]);
634
635 if (!$sort || $sort == "undefined") {
636 $sort = "reg_exp";
637 }
638
639 $filter_search = $this->dbh->escape_string($_REQUEST["search"]);
640
641 if (array_key_exists("search", $_REQUEST)) {
642 $_SESSION["prefs_filter_search"] = $filter_search;
643 } else {
644 $filter_search = $_SESSION["prefs_filter_search"];
645 }
646
647 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
648 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
649 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
650
651 $filter_search = $this->dbh->escape_string($_REQUEST["search"]);
652
653 if (array_key_exists("search", $_REQUEST)) {
654 $_SESSION["prefs_filter_search"] = $filter_search;
655 } else {
656 $filter_search = $_SESSION["prefs_filter_search"];
657 }
658
659 print "<div style='float : right; padding-right : 4px;'>
660 <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
661 value=\"$filter_search\">
662 <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
663 __('Search')."</button>
664 </div>";
665
666 print "<div dojoType=\"dijit.form.DropDownButton\">".
667 "<span>" . __('Select')."</span>";
668 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
669 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
670 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
671 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
672 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
673 print "</div></div>";
674
675 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
676 __('Create filter')."</button> ";
677
678 print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
679 __('Combine')."</button> ";
680
681 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
682 __('Edit')."</button> ";
683
684 print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
685 __('Reset sort order')."</button> ";
686
687
688 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
689 __('Remove')."</button> ";
690
691 if (defined('_ENABLE_FEED_DEBUGGING')) {
692 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
693 __('Rescore articles')."</button> ";
694 }
695
696 print "</div>"; # toolbar
697 print "</div>"; # toolbar-frame
698 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
699
700 print "<div id=\"filterlistLoading\">
701 <img src='images/indicator_tiny.gif'>".
702 __("Loading, please wait...")."</div>";
703
704 print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
705 url=\"backend.php?op=pref-filters&method=getfiltertree\">
706 </div>
707 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
708 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
709 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
710 </div>
711 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
712 dndController=\"dijit.tree.dndSource\"
713 betweenThreshold=\"5\"
714 model=\"filterModel\" openOnClick=\"true\">
715 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
716 Element.hide(\"filterlistLoading\");
717 </script>
718 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
719 var id = String(item.id);
720 var bare_id = id.substr(id.indexOf(':')+1);
721
722 if (id.match('FILTER:')) {
723 editFilter(bare_id);
724 }
725 </script>
726
727 </div>";
728
729 print "</div>"; #pane
730
731 PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
732 "hook_prefs_tab", "prefFilters");
733
734 print "</div>"; #container
735
736 }
737
738 function newfilter() {
739
740 print "<form name='filter_new_form' id='filter_new_form'>";
741
742 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
743 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
744 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
745
746 print "<div class=\"dlgSec\">".__("Caption")."</div>";
747
748 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
749
750 print "<div class=\"dlgSec\">".__("Match")."</div>";
751
752 print "<div dojoType=\"dijit.Toolbar\">";
753
754 print "<div dojoType=\"dijit.form.DropDownButton\">".
755 "<span>" . __('Select')."</span>";
756 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
757 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
758 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
759 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
760 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
761 print "</div></div>";
762
763 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
764 __('Add')."</button> ";
765
766 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
767 __('Delete')."</button> ";
768
769 print "</div>";
770
771 print "<ul id='filterDlg_Matches'>";
772 # print "<li>No rules</li>";
773 print "</ul>";
774
775 print "</div>";
776
777 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
778
779 print "<div dojoType=\"dijit.Toolbar\">";
780
781 print "<div dojoType=\"dijit.form.DropDownButton\">".
782 "<span>" . __('Select')."</span>";
783 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
784 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
785 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
786 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
787 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
788 print "</div></div>";
789
790 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
791 __('Add')."</button> ";
792
793 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
794 __('Delete')."</button> ";
795
796 print "</div>";
797
798 print "<ul id='filterDlg_Actions'>";
799 # print "<li>No actions</li>";
800 print "</ul>";
801
802 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
803 print "<div class=\"dlgSecCont\">"; */
804
805 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
806 <label for=\"enabled\">".__('Enabled')."</label>";
807
808 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
809 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
810
811 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
812 <label for=\"inverse\">".__('Inverse matching')."</label>";
813
814 // print "</div>";
815
816 print "<div class=\"dlgButtons\">";
817
818 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
819 __('Test')."</button> ";
820
821 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
822 __('Create')."</button> ";
823
824 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
825 __('Cancel')."</button>";
826
827 print "</div>";
828
829 }
830
831 function newrule() {
832 $rule = json_decode($_REQUEST["rule"], true);
833
834 if ($rule) {
835 $reg_exp = htmlspecialchars($rule["reg_exp"]);
836 $filter_type = $rule["filter_type"];
837 $feed_id = $rule["feed_id"];
838 $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
839 } else {
840 $reg_exp = "";
841 $filter_type = 1;
842 $feed_id = 0;
843 $inverse_checked = "";
844 }
845
846 if (strpos($feed_id, "CAT:") === 0) {
847 $feed_id = substr($feed_id, 4);
848 $cat_filter = true;
849 } else {
850 $cat_filter = false;
851 }
852
853
854 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
855
856 $result = $this->dbh->query("SELECT id,description
857 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
858
859 $filter_types = array();
860
861 while ($line = $this->dbh->fetch_assoc($result)) {
862 $filter_types[$line["id"]] = __($line["description"]);
863 }
864
865 print "<div class=\"dlgSec\">".__("Match")."</div>";
866
867 print "<div class=\"dlgSecCont\">";
868
869 print "<input dojoType=\"dijit.form.ValidationTextBox\"
870 required=\"true\" id=\"filterDlg_regExp\"
871 style=\"font-size : 16px; width : 20em;\"
872 name=\"reg_exp\" value=\"$reg_exp\"/>";
873
874 print "<hr/>";
875 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
876 name=\"inverse\" $inverse_checked/>";
877 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
878
879 print "<hr/>" . __("on field") . " ";
880 print_select_hash("filter_type", $filter_type, $filter_types,
881 'dojoType="dijit.form.Select"');
882
883 print "<hr/>";
884
885 print __("in") . " ";
886
887 print "<span id='filterDlg_feeds'>";
888 print_feed_select("feed_id",
889 $cat_filter ? "CAT:$feed_id" : $feed_id,
890 'dojoType="dijit.form.FilteringSelect"');
891 print "</span>";
892
893 print "</div>";
894
895 print "<div class=\"dlgButtons\">";
896
897 print "<div style=\"float : left\">
898 <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
899 </div>";
900
901
902 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
903 ($rule ? __("Save rule") : __('Add rule'))."</button> ";
904
905 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
906 __('Cancel')."</button>";
907
908 print "</div>";
909
910 print "</form>";
911 }
912
913 function newaction() {
914 $action = json_decode($_REQUEST["action"], true);
915
916 if ($action) {
917 $action_param = $this->dbh->escape_string($action["action_param"]);
918 $action_id = (int)$action["action_id"];
919 } else {
920 $action_param = "";
921 $action_id = 0;
922 }
923
924 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
925
926 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
927
928 print "<div class=\"dlgSecCont\">";
929
930 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
931 onchange=\"filterDlgCheckAction(this)\">";
932
933 $result = $this->dbh->query("SELECT id,description FROM ttrss_filter_actions
934 ORDER BY name");
935
936 while ($line = $this->dbh->fetch_assoc($result)) {
937 $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
938 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
939 }
940
941 print "</select>";
942
943 $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6) ?
944 "" : "display : none";
945
946 $param_hidden = ($action_id == 4 || $action_id == 6) ?
947 "" : "display : none";
948
949 $label_param_hidden = ($action_id == 7) ? "" : "display : none";
950
951 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
952 print " " . __("with parameters:") . " ";
953 print "<input dojoType=\"dijit.form.TextBox\"
954 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
955 name=\"action_param\" value=\"$action_param\">";
956
957 print_label_select("action_param_label", $action_param,
958 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
959 dojoType=\"dijit.form.Select\"");
960
961 print "</span>";
962
963 print "&nbsp;"; // tiny layout hack
964
965 print "</div>";
966
967 print "<div class=\"dlgButtons\">";
968
969 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
970 ($action ? __("Save action") : __('Add action'))."</button> ";
971
972 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
973 __('Cancel')."</button>";
974
975 print "</div>";
976
977 print "</form>";
978 }
979
980 private function getFilterName($id) {
981
982 $result = $this->dbh->query(
983 "SELECT title,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
984 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
985 ON (r.filter_id = f.id)
986 LEFT JOIN ttrss_filters2_actions AS a
987 ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title");
988
989 $title = $this->dbh->fetch_result($result, 0, "title");
990 $num_rules = $this->dbh->fetch_result($result, 0, "num_rules");
991 $num_actions = $this->dbh->fetch_result($result, 0, "num_actions");
992
993 if (!$title) $title = __("[No caption]");
994
995 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", $num_rules), $title, $num_rules);
996
997 $result = $this->dbh->query(
998 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
999
1000 $actions = "";
1001
1002 if ($this->dbh->num_rows($result) > 0) {
1003 $line = $this->dbh->fetch_assoc($result);
1004 $actions = $this->getActionName($line);
1005
1006 $num_actions -= 1;
1007 }
1008
1009 if ($num_actions > 0)
1010 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", $num_actions), $actions, $num_actions);
1011
1012 return array($title, $actions);
1013 }
1014
1015 function join() {
1016 $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
1017
1018 if (count($ids) > 1) {
1019 $base_id = array_shift($ids);
1020 $ids_str = join(",", $ids);
1021
1022 $this->dbh->query("BEGIN");
1023 $this->dbh->query("UPDATE ttrss_filters2_rules
1024 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1025 $this->dbh->query("UPDATE ttrss_filters2_actions
1026 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1027
1028 $this->dbh->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
1029 $this->dbh->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
1030
1031 $this->dbh->query("COMMIT");
1032
1033 $this->optimizeFilter($base_id);
1034
1035 }
1036 }
1037
1038 private function optimizeFilter($id) {
1039 $this->dbh->query("BEGIN");
1040 $result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
1041 WHERE filter_id = '$id'");
1042
1043 $tmp = array();
1044 $dupe_ids = array();
1045
1046 while ($line = $this->dbh->fetch_assoc($result)) {
1047 $id = $line["id"];
1048 unset($line["id"]);
1049
1050 if (array_search($line, $tmp) === false) {
1051 array_push($tmp, $line);
1052 } else {
1053 array_push($dupe_ids, $id);
1054 }
1055 }
1056
1057 if (count($dupe_ids) > 0) {
1058 $ids_str = join(",", $dupe_ids);
1059 $this->dbh->query("DELETE FROM ttrss_filters2_actions
1060 WHERE id IN ($ids_str)");
1061 }
1062
1063 $result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
1064 WHERE filter_id = '$id'");
1065
1066 $tmp = array();
1067 $dupe_ids = array();
1068
1069 while ($line = $this->dbh->fetch_assoc($result)) {
1070 $id = $line["id"];
1071 unset($line["id"]);
1072
1073 if (array_search($line, $tmp) === false) {
1074 array_push($tmp, $line);
1075 } else {
1076 array_push($dupe_ids, $id);
1077 }
1078 }
1079
1080 if (count($dupe_ids) > 0) {
1081 $ids_str = join(",", $dupe_ids);
1082 $this->dbh->query("DELETE FROM ttrss_filters2_rules
1083 WHERE id IN ($ids_str)");
1084 }
1085
1086 $this->dbh->query("COMMIT");
1087 }
1088 }
1089 ?>