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