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