]> git.wh0rd.org - tt-rss.git/blob - classes/pref/filters.php
remove $link
[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 db_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 db_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(db_escape_string( $_REQUEST["match_any_rule"])));
53 $filter["inverse"] = sql_bool_to_bool(
54 checkbox_to_sql_bool(db_escape_string( $_REQUEST["inverse"])));
55
56 $filter["rules"] = array();
57
58 $result = db_query( "SELECT id,name FROM ttrss_filter_types");
59
60 $filter_types = array();
61 while ($line = db_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 = db_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 = db_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 = db_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 = db_query(
198 "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
199
200 while ($rule_line = db_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 = db_query( "SELECT fg_color, bg_color
210 FROM ttrss_labels2 WHERE caption = '".db_escape_string( $line['action_param'])."' AND
211 owner_uid = " . $_SESSION["uid"]);
212
213 if (db_num_rows($label_result) > 0) {
214 $fg_color = db_fetch_result($label_result, 0, "fg_color");
215 $bg_color = db_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 = db_escape_string( $_REQUEST["id"]);
252
253 $result = db_query(
254 "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
255
256 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
257 $match_any_rule = sql_bool_to_bool(db_fetch_result($result, 0, "match_any_rule"));
258 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
259 $title = htmlspecialchars(db_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 = db_query( "SELECT * FROM ttrss_filters2_rules
298 WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
299
300 while ($line = db_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 = db_query( "SELECT * FROM ttrss_filters2_actions
346 WHERE filter_id = '$filter_id' ORDER BY id");
347
348 while ($line = db_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 = db_query( "SELECT description FROM ttrss_filter_types
431 WHERE id = ".(int)$rule["filter_type"]);
432 $filter_type = db_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 = db_query( "SELECT description FROM
444 ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
445
446 $title = __(db_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 = db_escape_string( $_REQUEST["id"]);
467 $enabled = checkbox_to_sql_bool(db_escape_string( $_REQUEST["enabled"]));
468 $match_any_rule = checkbox_to_sql_bool(db_escape_string( $_REQUEST["match_any_rule"]));
469 $inverse = checkbox_to_sql_bool(db_escape_string( $_REQUEST["inverse"]));
470 $title = db_escape_string( $_REQUEST["title"]);
471
472 $result = db_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(",", db_escape_string( $_REQUEST["ids"]));
486
487 foreach ($ids as $id) {
488 db_query( "DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
489 }
490 }
491
492 private function saveRulesAndActions($filter_id) {
493
494 db_query( "DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
495 db_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(db_escape_string( trim($rule["reg_exp"])));
525 $inverse = isset($rule["inverse"]) ? "true" : "false";
526
527 $filter_type = (int) db_escape_string( trim($rule["filter_type"]));
528 $feed_id = db_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 db_query( $query);
550 }
551 }
552
553 foreach ($actions as $action) {
554 if ($action) {
555
556 $action_id = (int) db_escape_string( $action["action_id"]);
557 $action_param = db_escape_string( $action["action_param"]);
558 $action_param_label = db_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 db_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 = db_escape_string( $_REQUEST["title"]);
590
591 db_query( "BEGIN");
592
593 /* create base filter */
594
595 $result = db_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 = db_query( "SELECT MAX(id) AS id FROM ttrss_filters2
600 WHERE owner_uid = ".$_SESSION["uid"]);
601
602 $filter_id = db_fetch_result($result, 0, "id");
603
604 $this->saveRulesAndActions($filter_id);
605
606 db_query( "COMMIT");
607 }
608
609 function index() {
610
611 $sort = db_escape_string( $_REQUEST["sort"]);
612
613 if (!$sort || $sort == "undefined") {
614 $sort = "reg_exp";
615 }
616
617 $filter_search = db_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 = db_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 global $pluginhost;
710 $pluginhost->run_hooks($pluginhost::HOOK_PREFS_TAB,
711 "hook_prefs_tab", "prefFilters");
712
713 print "</div>"; #container
714
715 }
716
717 function newfilter() {
718
719 print "<form name='filter_new_form' id='filter_new_form'>";
720
721 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
722 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
723 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
724
725 print "<div class=\"dlgSec\">".__("Caption")."</div>";
726
727 print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
728
729 print "<div class=\"dlgSec\">".__("Match")."</div>";
730
731 print "<div dojoType=\"dijit.Toolbar\">";
732
733 print "<div dojoType=\"dijit.form.DropDownButton\">".
734 "<span>" . __('Select')."</span>";
735 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
736 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
737 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
738 print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
739 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
740 print "</div></div>";
741
742 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
743 __('Add')."</button> ";
744
745 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
746 __('Delete')."</button> ";
747
748 print "</div>";
749
750 print "<ul id='filterDlg_Matches'>";
751 # print "<li>No rules</li>";
752 print "</ul>";
753
754 print "</div>";
755
756 print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
757
758 print "<div dojoType=\"dijit.Toolbar\">";
759
760 print "<div dojoType=\"dijit.form.DropDownButton\">".
761 "<span>" . __('Select')."</span>";
762 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
763 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
764 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
765 print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
766 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
767 print "</div></div>";
768
769 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
770 __('Add')."</button> ";
771
772 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
773 __('Delete')."</button> ";
774
775 print "</div>";
776
777 print "<ul id='filterDlg_Actions'>";
778 # print "<li>No actions</li>";
779 print "</ul>";
780
781 /* print "<div class=\"dlgSec\">".__("Options")."</div>";
782 print "<div class=\"dlgSecCont\">"; */
783
784 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
785 <label for=\"enabled\">".__('Enabled')."</label>";
786
787 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
788 <label for=\"match_any_rule\">".__('Match any rule')."</label>";
789
790 print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
791 <label for=\"inverse\">".__('Inverse matching')."</label>";
792
793 // print "</div>";
794
795 print "<div class=\"dlgButtons\">";
796
797 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
798 __('Test')."</button> ";
799
800 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
801 __('Create')."</button> ";
802
803 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
804 __('Cancel')."</button>";
805
806 print "</div>";
807
808 }
809
810 function newrule() {
811 $rule = json_decode($_REQUEST["rule"], true);
812
813 if ($rule) {
814 $reg_exp = htmlspecialchars($rule["reg_exp"]);
815 $filter_type = $rule["filter_type"];
816 $feed_id = $rule["feed_id"];
817 $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
818 } else {
819 $reg_exp = "";
820 $filter_type = 1;
821 $feed_id = 0;
822 $inverse_checked = "";
823 }
824
825 if (strpos($feed_id, "CAT:") === 0) {
826 $feed_id = substr($feed_id, 4);
827 $cat_filter = true;
828 } else {
829 $cat_filter = false;
830 }
831
832
833 print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
834
835 $result = db_query( "SELECT id,description
836 FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
837
838 $filter_types = array();
839
840 while ($line = db_fetch_assoc($result)) {
841 $filter_types[$line["id"]] = __($line["description"]);
842 }
843
844 print "<div class=\"dlgSec\">".__("Match")."</div>";
845
846 print "<div class=\"dlgSecCont\">";
847
848 print "<input dojoType=\"dijit.form.ValidationTextBox\"
849 required=\"true\" id=\"filterDlg_regExp\"
850 style=\"font-size : 16px; width : 20em;\"
851 name=\"reg_exp\" value=\"$reg_exp\"/>";
852
853 print "<hr/>";
854 print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
855 name=\"inverse\" $inverse_checked/>";
856 print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
857
858 print "<hr/>" . __("on field") . " ";
859 print_select_hash("filter_type", $filter_type, $filter_types,
860 'dojoType="dijit.form.Select"');
861
862 print "<hr/>";
863
864 print __("in") . " ";
865
866 print "<span id='filterDlg_feeds'>";
867 print_feed_select( "feed_id",
868 $cat_filter ? "CAT:$feed_id" : $feed_id,
869 'dojoType="dijit.form.FilteringSelect"');
870 print "</span>";
871
872 print "</div>";
873
874 print "<div class=\"dlgButtons\">";
875
876 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
877 ($rule ? __("Save rule") : __('Add rule'))."</button> ";
878
879 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
880 __('Cancel')."</button>";
881
882 print "</div>";
883
884 print "</form>";
885 }
886
887 function newaction() {
888 $action = json_decode($_REQUEST["action"], true);
889
890 if ($action) {
891 $action_param = db_escape_string( $action["action_param"]);
892 $action_id = (int)$action["action_id"];
893 } else {
894 $action_param = "";
895 $action_id = 0;
896 }
897
898 print "<form name='filter_new_action_form' id='filter_new_action_form'>";
899
900 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
901
902 print "<div class=\"dlgSecCont\">";
903
904 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
905 onchange=\"filterDlgCheckAction(this)\">";
906
907 $result = db_query( "SELECT id,description FROM ttrss_filter_actions
908 ORDER BY name");
909
910 while ($line = db_fetch_assoc($result)) {
911 $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
912 printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
913 }
914
915 print "</select>";
916
917 $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6) ?
918 "" : "display : none";
919
920 $param_hidden = ($action_id == 4 || $action_id == 6) ?
921 "" : "display : none";
922
923 $label_param_hidden = ($action_id == 7) ? "" : "display : none";
924
925 print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
926 print " " . __("with parameters:") . " ";
927 print "<input dojoType=\"dijit.form.TextBox\"
928 id=\"filterDlg_actionParam\" style=\"$param_hidden\"
929 name=\"action_param\" value=\"$action_param\">";
930
931 print_label_select( "action_param_label", $action_param,
932 "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
933 dojoType=\"dijit.form.Select\"");
934
935 print "</span>";
936
937 print "&nbsp;"; // tiny layout hack
938
939 print "</div>";
940
941 print "<div class=\"dlgButtons\">";
942
943 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
944 ($action ? __("Save action") : __('Add action'))."</button> ";
945
946 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
947 __('Cancel')."</button>";
948
949 print "</div>";
950
951 print "</form>";
952 }
953
954 private function getFilterName($id) {
955
956 $result = db_query(
957 "SELECT title,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
958 FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
959 ON (r.filter_id = f.id)
960 LEFT JOIN ttrss_filters2_actions AS a
961 ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title");
962
963 $title = db_fetch_result($result, 0, "title");
964 $num_rules = db_fetch_result($result, 0, "num_rules");
965 $num_actions = db_fetch_result($result, 0, "num_actions");
966
967 if (!$title) $title = __("[No caption]");
968
969 $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", $num_rules), $title, $num_rules);
970
971 $result = db_query(
972 "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
973
974 $actions = "";
975
976 if (db_num_rows($result) > 0) {
977 $line = db_fetch_assoc($result);
978 $actions = $this->getActionName($line);
979
980 $num_actions -= 1;
981 }
982
983 if ($num_actions > 0)
984 $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", $num_actions), $actions, $num_actions);
985
986 return array($title, $actions);
987 }
988
989 function join() {
990 $ids = explode(",", db_escape_string( $_REQUEST["ids"]));
991
992 if (count($ids) > 1) {
993 $base_id = array_shift($ids);
994 $ids_str = join(",", $ids);
995
996 db_query( "BEGIN");
997 db_query( "UPDATE ttrss_filters2_rules
998 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
999 db_query( "UPDATE ttrss_filters2_actions
1000 SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
1001
1002 db_query( "DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
1003 db_query( "UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
1004
1005 db_query( "COMMIT");
1006
1007 $this->optimizeFilter($base_id);
1008
1009 }
1010 }
1011
1012 private function optimizeFilter($id) {
1013 db_query( "BEGIN");
1014 $result = db_query( "SELECT * FROM ttrss_filters2_actions
1015 WHERE filter_id = '$id'");
1016
1017 $tmp = array();
1018 $dupe_ids = array();
1019
1020 while ($line = db_fetch_assoc($result)) {
1021 $id = $line["id"];
1022 unset($line["id"]);
1023
1024 if (array_search($line, $tmp) === false) {
1025 array_push($tmp, $line);
1026 } else {
1027 array_push($dupe_ids, $id);
1028 }
1029 }
1030
1031 if (count($dupe_ids) > 0) {
1032 $ids_str = join(",", $dupe_ids);
1033 db_query( "DELETE FROM ttrss_filters2_actions
1034 WHERE id IN ($ids_str)");
1035 }
1036
1037 $result = db_query( "SELECT * FROM ttrss_filters2_rules
1038 WHERE filter_id = '$id'");
1039
1040 $tmp = array();
1041 $dupe_ids = array();
1042
1043 while ($line = db_fetch_assoc($result)) {
1044 $id = $line["id"];
1045 unset($line["id"]);
1046
1047 if (array_search($line, $tmp) === false) {
1048 array_push($tmp, $line);
1049 } else {
1050 array_push($dupe_ids, $id);
1051 }
1052 }
1053
1054 if (count($dupe_ids) > 0) {
1055 $ids_str = join(",", $dupe_ids);
1056 db_query( "DELETE FROM ttrss_filters2_rules
1057 WHERE id IN ($ids_str)");
1058 }
1059
1060 db_query( "COMMIT");
1061 }
1062 }
1063 ?>