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