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