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