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