]> git.wh0rd.org - tt-rss.git/blob - classes/pref_filters.php
fix filter test dialog
[tt-rss.git] / classes / pref_filters.php
1 <?php
2 class Pref_Filters extends Protected_Handler {
3
4 function csrf_ignore($method) {
5 $csrf_ignored = array("index", "getfiltertree", "edit");
6
7 return array_search($method, $csrf_ignored) !== false;
8 }
9
10 function filter_test($filter_type, $reg_exp,
11 $action_id, $action_param, $filter_param, $inverse, $feed_id) {
12
13 $result = db_query($this->link, "SELECT name FROM ttrss_filter_types WHERE
14 id = " . $filter_type);
15 $type_name = db_fetch_result($result, 0, "name");
16
17 $result = db_query($this->link, "SELECT name FROM ttrss_filter_actions WHERE
18 id = " . $action_id);
19 $action_name = db_fetch_result($result, 0, "name");
20
21 $filter["reg_exp"] = $reg_exp;
22 $filter["action"] = $action_name;
23 $filter["type"] = $type_name;
24 $filter["action_param"] = $action_param;
25 $filter["filter_param"] = $filter_param;
26 $filter["inverse"] = $inverse;
27
28 $filters[$type_name] = array($filter);
29
30 if ($feed_id)
31 $feed = $feed_id;
32 else
33 $feed = -4;
34
35 $feed_title = getFeedTitle($this->link, $feed);
36
37 $qfh_ret = queryFeedHeadlines($this->link, $feed,
38 30, "", false, false, false,
39 false, "date_entered DESC", 0, $_SESSION["uid"], $filter);
40
41 $result = $qfh_ret[0];
42
43 $articles = array();
44 $found = 0;
45
46 print __("Articles matching this filter:");
47
48 print "<div class=\"inactiveFeedHolder\">";
49 print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">";
50
51 while ($line = db_fetch_assoc($result)) {
52
53 $entry_timestamp = strtotime($line["updated"]);
54 $entry_tags = get_article_tags($this->link, $line["id"], $_SESSION["uid"]);
55
56 $content_preview = truncate_string(
57 strip_tags($line["content_preview"]), 100, '...');
58
59 if ($line["feed_title"])
60 $feed_title = $line["feed_title"];
61
62 print "<tr>";
63
64 print "<td width='5%' align='center'><input
65 dojoType=\"dijit.form.CheckBox\" checked=\"1\"
66 disabled=\"1\" type=\"checkbox\"></td>";
67 print "<td>";
68
69 print $line["title"];
70 print "&nbsp;(";
71 print "<b>" . $feed_title . "</b>";
72 print "):&nbsp;";
73 print "<span class=\"insensitive\">" . $content_preview . "</span>";
74 print " " . mb_substr($line["date_entered"], 0, 16);
75
76 print "</td></tr>";
77
78 $found++;
79 }
80
81 if ($found == 0) {
82 print "<tr><td align='center'>" .
83 __("No articles matching this filter has been found.") . "</td></tr>";
84 }
85
86 print "</table>";
87 print "</div>";
88
89 }
90
91 function getfiltertree() {
92 $root = array();
93 $root['id'] = 'root';
94 $root['name'] = __('Filters');
95 $root['items'] = array();
96
97 $result = db_query($this->link, "SELECT
98 ttrss_filters.id AS id,reg_exp,
99 ttrss_filter_types.name AS filter_type_name,
100 ttrss_filter_types.description AS filter_type_descr,
101 enabled,
102 inverse,
103 feed_id,
104 action_id,
105 filter_param,
106 filter_type,
107 ttrss_filter_actions.description AS action_description,
108 ttrss_feeds.title AS feed_title,
109 ttrss_filter_actions.name AS action_name,
110 ttrss_filters.action_param AS action_param
111 FROM
112 ttrss_filter_types,ttrss_filter_actions,ttrss_filters LEFT JOIN
113 ttrss_feeds ON (ttrss_filters.feed_id = ttrss_feeds.id)
114 WHERE
115 filter_type = ttrss_filter_types.id AND
116 ttrss_filter_actions.id = action_id AND
117 ttrss_filters.owner_uid = ".$_SESSION["uid"]."
118 ORDER by action_description, reg_exp");
119
120 $cat = false;
121 $cur_action_description = "";
122
123 if (db_num_rows($result) > 0) {
124
125 while ($line = db_fetch_assoc($result)) {
126 if ($cur_action_description != $line['action_description']) {
127
128 if ($cat)
129 array_push($root['items'], $cat);
130
131 $cat = array();
132 $cat['id'] = 'ACTION:' . $line['action_id'];
133 $cat['name'] = $line['action_description'];
134 $cat['items'] = array();
135
136 $cur_action_description = $line['action_description'];
137 }
138
139 if (array_search($line["action_name"],
140 array("score", "tag", "label")) === false) {
141
142 $line["action_param"] = '';
143 } else {
144 if ($line['action_name'] == 'label') {
145
146 $tmp_result = db_query($this->link, "SELECT fg_color, bg_color
147 FROM ttrss_labels2 WHERE caption = '".
148 db_escape_string($line["action_param"])."' AND
149 owner_uid = " . $_SESSION["uid"]);
150
151 if (db_num_rows($tmp_result) != 0) {
152 $fg_color = db_fetch_result($tmp_result, 0, "fg_color");
153 $bg_color = db_fetch_result($tmp_result, 0, "bg_color");
154
155 $tmp = "<span class=\"labelColorIndicator\" style='color : $fg_color; background-color : $bg_color'>&alpha;</span> " . $line['action_param'];
156
157 $line['action_param'] = $tmp;
158 }
159 }
160 }
161
162 $filter = array();
163 $filter['id'] = 'FILTER:' . $line['id'];
164 $filter['bare_id'] = $line['id'];
165 $filter['name'] = $line['reg_exp'];
166 $filter['type'] = $line['filter_type'];
167 $filter['enabled'] = sql_bool_to_bool($line['enabled']);
168 $filter['param'] = $line['action_param'];
169 $filter['inverse'] = sql_bool_to_bool($line['inverse']);
170 $filter['checkbox'] = false;
171
172 if ($line['feed_id'])
173 $filter['feed'] = $line['feed_title'];
174
175 array_push($cat['items'], $filter);
176 }
177
178 array_push($root['items'], $cat);
179 }
180
181 $fl = array();
182 $fl['identifier'] = 'id';
183 $fl['label'] = 'name';
184 $fl['items'] = array($root);
185
186 print json_encode($fl);
187 return;
188 }
189
190 function edit() {
191
192 $filter_id = db_escape_string($_REQUEST["id"]);
193
194 $result = db_query($this->link,
195 "SELECT * FROM ttrss_filters WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
196
197 $reg_exp = htmlspecialchars(db_fetch_result($result, 0, "reg_exp"));
198 $filter_type = db_fetch_result($result, 0, "filter_type");
199 $feed_id = db_fetch_result($result, 0, "feed_id");
200 $action_id = db_fetch_result($result, 0, "action_id");
201 $action_param = db_fetch_result($result, 0, "action_param");
202 $filter_param = db_fetch_result($result, 0, "filter_param");
203
204 $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
205 $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
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 $result = db_query($this->link, "SELECT id,description
215 FROM ttrss_filter_types ORDER BY description");
216
217 $filter_types = array();
218
219 while ($line = db_fetch_assoc($result)) {
220 //array_push($filter_types, $line["description"]);
221 $filter_types[$line["id"]] = __($line["description"]);
222 }
223
224 print "<div class=\"dlgSec\">".__("Match")."</div>";
225
226 print "<div class=\"dlgSecCont\">";
227
228 if ($filter_type != 5) {
229 $date_ops_invisible = 'style="display : none"';
230 }
231
232 print "<span id=\"filterDlg_dateModBox\" $date_ops_invisible>";
233 print __("Date") . " ";
234
235 $filter_params = array(
236 "before" => __("before"),
237 "after" => __("after"));
238
239 print_select_hash("filter_date_modifier", $filter_param,
240 $filter_params, 'dojoType="dijit.form.Select"');
241
242 print "&nbsp;</span>";
243
244 print "<input dojoType=\"dijit.form.ValidationTextBox\"
245 required=\"1\"
246 name=\"reg_exp\" style=\"font-size : 16px;\" value=\"$reg_exp\">";
247
248 print "<span id=\"filterDlg_dateChkBox\" $date_ops_invisible>";
249 print "&nbsp;<button dojoType=\"dijit.form.Button\" onclick=\"return filterDlgCheckDate()\">".
250 __('Check it')."</button>";
251 print "</span>";
252
253 print "<hr/> " . __("on field") . " ";
254 print_select_hash("filter_type", $filter_type, $filter_types,
255 'onchange="filterDlgCheckType(this)" dojoType="dijit.form.Select"');
256
257 print "<hr/>";
258
259 print __("in") . " ";
260 print_feed_select($this->link, "feed_id", $feed_id,
261 'dojoType="dijit.form.FilteringSelect"');
262
263 print "</div>";
264
265 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
266
267 print "<div class=\"dlgSecCont\">";
268
269 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
270 onchange=\"filterDlgCheckAction(this)\">";
271
272 $result = db_query($this->link, "SELECT id,description FROM ttrss_filter_actions
273 ORDER BY name");
274
275 while ($line = db_fetch_assoc($result)) {
276 $is_sel = ($line["id"] == $action_id) ? "selected=\"1\"" : "";
277 printf("<option value='%d' $is_sel>%s</option>", $line["id"], __($line["description"]));
278 }
279
280 print "</select>";
281
282 $param_hidden = ($action_id == 4 || $action_id == 6 || $action_id == 7) ? "" : "display : none";
283
284 print "<span id=\"filterDlg_paramBox\" style=\"$param_hidden\">";
285 print " " . __("with parameters:") . " ";
286
287 $param_int_hidden = ($action_id != 7) ? "" : "display : none";
288
289 print "<input style=\"$param_int_hidden\"
290 dojoType=\"dijit.form.TextBox\" id=\"filterDlg_actionParam\"
291 name=\"action_param\" value=\"$action_param\">";
292
293 $param_int_hidden = ($action_id == 7) ? "" : "display : none";
294
295 print_label_select($this->link, "action_param_label", $action_param,
296 "style=\"$param_int_hidden\"" .
297 'id="filterDlg_actionParamLabel" dojoType="dijit.form.Select"');
298
299 print "</span>";
300
301 print "&nbsp;"; // tiny layout hack
302
303 print "</div>";
304
305 print "<div class=\"dlgSec\">".__("Options")."</div>";
306 print "<div class=\"dlgSecCont\">";
307
308 print "<div style=\"line-height : 100%\">";
309
310 if ($enabled) {
311 $checked = "checked=\"1\"";
312 } else {
313 $checked = "";
314 }
315
316 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
317 <label for=\"enabled\">".__('Enabled')."</label><hr/>";
318
319 if ($inverse) {
320 $checked = "checked=\"1\"";
321 } else {
322 $checked = "";
323 }
324
325 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
326 <label for=\"inverse\">".__('Inverse match')."</label>";
327
328 print "</div>";
329 print "</div>";
330
331 print "<div class=\"dlgButtons\">";
332
333 print "<div style=\"float : left\">";
334 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
335 __('Remove')."</button>";
336 print "</div>";
337
338 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
339 __('Test')."</button> ";
340
341 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
342 __('Save')."</button> ";
343
344 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
345 __('Cancel')."</button>";
346
347 print "</div>";
348 }
349
350 function editSave() {
351
352 global $memcache;
353
354 if ($memcache) $memcache->flush();
355
356 $savemode = db_escape_string($_REQUEST["savemode"]);
357 $reg_exp = db_escape_string(trim($_REQUEST["reg_exp"]));
358 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
359 $filter_id = db_escape_string($_REQUEST["id"]);
360 $feed_id = db_escape_string($_REQUEST["feed_id"]);
361 $action_id = db_escape_string($_REQUEST["action_id"]);
362 $action_param = db_escape_string($_REQUEST["action_param"]);
363 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
364 $enabled = checkbox_to_sql_bool(db_escape_string($_REQUEST["enabled"]));
365 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
366
367 # for the time being, no other filters use params anyway...
368 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
369
370 if (!$feed_id) {
371 $feed_id = 'NULL';
372 } else {
373 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
374 }
375
376 /* When processing 'assign label' filters, action_param_label dropbox
377 * overrides action_param */
378
379 if ($action_id == 7) {
380 $action_param = $action_param_label;
381 }
382
383 if ($action_id == 6) {
384 $action_param = (int) str_replace("+", "", $action_param);
385 }
386
387 if ($savemode != "test") {
388 $result = db_query($this->link, "UPDATE ttrss_filters SET
389 reg_exp = '$reg_exp',
390 feed_id = $feed_id,
391 action_id = '$action_id',
392 filter_type = '$filter_type',
393 enabled = $enabled,
394 inverse = $inverse,
395 action_param = '$action_param',
396 filter_param = '$filter_param'
397 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
398 } else {
399
400 $this->filter_test($filter_type, $reg_exp,
401 $action_id, $action_param, $filter_param, sql_bool_to_bool($inverse),
402 (int) $_REQUEST["feed_id"]);
403
404 print "<div align='center'>";
405 print "<button dojoType=\"dijit.form.Button\"
406 onclick=\"return dijit.byId('filterTestDlg').hide()\">".
407 __('Close this window')."</button>";
408 print "</div>";
409
410 }
411 }
412
413 function remove() {
414
415 if ($memcache) $memcache->flush();
416
417 $ids = split(",", db_escape_string($_REQUEST["ids"]));
418
419 foreach ($ids as $id) {
420 db_query($this->link, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
421 }
422 }
423
424 function add() {
425
426 if ($memcache) $memcache->flush();
427
428 $savemode = db_escape_string($_REQUEST["savemode"]);
429 $regexp = db_escape_string(trim($_REQUEST["reg_exp"]));
430 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
431 $feed_id = db_escape_string($_REQUEST["feed_id"]);
432 $action_id = db_escape_string($_REQUEST["action_id"]);
433 $action_param = db_escape_string($_REQUEST["action_param"]);
434 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
435 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
436
437 # for the time being, no other filters use params anyway...
438 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
439
440 if (!$regexp) return;
441
442 if (!$feed_id) {
443 $feed_id = 'NULL';
444 } else {
445 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
446 }
447
448 /* When processing 'assign label' filters, action_param_label dropbox
449 * overrides action_param */
450
451 if ($action_id == 7) {
452 $action_param = $action_param_label;
453 }
454
455 if ($action_id == 6) {
456 $action_param = (int) str_replace("+", "", $action_param);
457 }
458
459 if ($savemode != "test") {
460 $result = db_query($this->link,
461 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
462 action_id, action_param, inverse, filter_param)
463 VALUES
464 ('$regexp', '$filter_type','".$_SESSION["uid"]."',
465 $feed_id, '$action_id', '$action_param', $inverse,
466 '$filter_param')");
467
468 if (db_affected_rows($this->link, $result) != 0) {
469 print T_sprintf("Created filter <b>%s</b>", htmlspecialchars($regexp));
470 }
471
472 } else {
473
474 $this->filter_test($filter_type, $regexp,
475 $action_id, $action_param, $filter_param, sql_bool_to_bool($inverse),
476 (int) $_REQUEST["feed_id"]);
477
478 print "<div align='center'>";
479 print "<button dojoType=\"dijit.form.Button\"
480 onclick=\"return dijit.byId('filterTestDlg').hide()\">".
481 __('Close this window')."</button>";
482 print "</div>";
483
484 }
485 }
486
487 function index() {
488
489 $sort = db_escape_string($_REQUEST["sort"]);
490
491 if (!$sort || $sort == "undefined") {
492 $sort = "reg_exp";
493 }
494
495 $result = db_query($this->link, "SELECT id,description
496 FROM ttrss_filter_types ORDER BY description");
497
498 $filter_types = array();
499
500 while ($line = db_fetch_assoc($result)) {
501 //array_push($filter_types, $line["description"]);
502 $filter_types[$line["id"]] = $line["description"];
503 }
504
505
506 $filter_search = db_escape_string($_REQUEST["search"]);
507
508 if (array_key_exists("search", $_REQUEST)) {
509 $_SESSION["prefs_filter_search"] = $filter_search;
510 } else {
511 $filter_search = $_SESSION["prefs_filter_search"];
512 }
513
514 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
515 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
516 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
517
518 print "<div dojoType=\"dijit.form.DropDownButton\">".
519 "<span>" . __('Select')."</span>";
520 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
521 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
522 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
523 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
524 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
525 print "</div></div>";
526
527 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
528 __('Create filter')."</button> ";
529
530 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
531 __('Edit')."</button> ";
532
533 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
534 __('Remove')."</button> ";
535
536 if (defined('_ENABLE_FEED_DEBUGGING')) {
537 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
538 __('Rescore articles')."</button> ";
539 }
540
541 print "</div>"; # toolbar
542 print "</div>"; # toolbar-frame
543 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
544
545 print "<div id=\"filterlistLoading\">
546 <img src='images/indicator_tiny.gif'>".
547 __("Loading, please wait...")."</div>";
548
549 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"filterStore\"
550 url=\"backend.php?op=pref-filters&method=getfiltertree\">
551 </div>
552 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
553 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
554 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
555 </div>
556 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
557 model=\"filterModel\" openOnClick=\"true\">
558 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
559 Element.hide(\"filterlistLoading\");
560 </script>
561 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
562 var id = String(item.id);
563 var bare_id = id.substr(id.indexOf(':')+1);
564
565 if (id.match('FILTER:')) {
566 editFilter(bare_id);
567 }
568 </script>
569
570 </div>";
571
572 print "</div>"; #pane
573 print "</div>"; #container
574
575 }
576 }
577 ?>