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