]> git.wh0rd.org - tt-rss.git/blob - classes/pref_filters.php
experimental CSRF protection
[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
213 $result = db_query($this->link, "SELECT id,description
214 FROM ttrss_filter_types ORDER BY description");
215
216 $filter_types = array();
217
218 while ($line = db_fetch_assoc($result)) {
219 //array_push($filter_types, $line["description"]);
220 $filter_types[$line["id"]] = __($line["description"]);
221 }
222
223 print "<div class=\"dlgSec\">".__("Match")."</div>";
224
225 print "<div class=\"dlgSecCont\">";
226
227 if ($filter_type != 5) {
228 $date_ops_invisible = 'style="display : none"';
229 }
230
231 print "<span id=\"filterDlg_dateModBox\" $date_ops_invisible>";
232 print __("Date") . " ";
233
234 $filter_params = array(
235 "before" => __("before"),
236 "after" => __("after"));
237
238 print_select_hash("filter_date_modifier", $filter_param,
239 $filter_params, 'dojoType="dijit.form.Select"');
240
241 print "&nbsp;</span>";
242
243 print "<input dojoType=\"dijit.form.ValidationTextBox\"
244 required=\"1\"
245 name=\"reg_exp\" style=\"font-size : 16px;\" value=\"$reg_exp\">";
246
247 print "<span id=\"filterDlg_dateChkBox\" $date_ops_invisible>";
248 print "&nbsp;<button dojoType=\"dijit.form.Button\" onclick=\"return filterDlgCheckDate()\">".
249 __('Check it')."</button>";
250 print "</span>";
251
252 print "<hr/> " . __("on field") . " ";
253 print_select_hash("filter_type", $filter_type, $filter_types,
254 'onchange="filterDlgCheckType(this)" dojoType="dijit.form.Select"');
255
256 print "<hr/>";
257
258 print __("in") . " ";
259 print_feed_select($this->link, "feed_id", $feed_id,
260 'dojoType="dijit.form.FilteringSelect"');
261
262 print "</div>";
263
264 print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
265
266 print "<div class=\"dlgSecCont\">";
267
268 print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
269 onchange=\"filterDlgCheckAction(this)\">";
270
271 $result = db_query($this->link, "SELECT id,description FROM ttrss_filter_actions
272 ORDER BY name");
273
274 while ($line = db_fetch_assoc($result)) {
275 $is_sel = ($line["id"] == $action_id) ? "selected=\"1\"" : "";
276 printf("<option value='%d' $is_sel>%s</option>", $line["id"], __($line["description"]));
277 }
278
279 print "</select>";
280
281 $param_hidden = ($action_id == 4 || $action_id == 6 || $action_id == 7) ? "" : "display : none";
282
283 print "<span id=\"filterDlg_paramBox\" style=\"$param_hidden\">";
284 print " " . __("with parameters:") . " ";
285
286 $param_int_hidden = ($action_id != 7) ? "" : "display : none";
287
288 print "<input style=\"$param_int_hidden\"
289 dojoType=\"dijit.form.TextBox\" id=\"filterDlg_actionParam\"
290 name=\"action_param\" value=\"$action_param\">";
291
292 $param_int_hidden = ($action_id == 7) ? "" : "display : none";
293
294 print_label_select($this->link, "action_param_label", $action_param,
295 "style=\"$param_int_hidden\"" .
296 'id="filterDlg_actionParamLabel" dojoType="dijit.form.Select"');
297
298 print "</span>";
299
300 print "&nbsp;"; // tiny layout hack
301
302 print "</div>";
303
304 print "<div class=\"dlgSec\">".__("Options")."</div>";
305 print "<div class=\"dlgSecCont\">";
306
307 print "<div style=\"line-height : 100%\">";
308
309 if ($enabled) {
310 $checked = "checked=\"1\"";
311 } else {
312 $checked = "";
313 }
314
315 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
316 <label for=\"enabled\">".__('Enabled')."</label><hr/>";
317
318 if ($inverse) {
319 $checked = "checked=\"1\"";
320 } else {
321 $checked = "";
322 }
323
324 print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
325 <label for=\"inverse\">".__('Inverse match')."</label>";
326
327 print "</div>";
328 print "</div>";
329
330 print "<div class=\"dlgButtons\">";
331
332 print "<div style=\"float : left\">";
333 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
334 __('Remove')."</button>";
335 print "</div>";
336
337 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
338 __('Test')."</button> ";
339
340 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
341 __('Save')."</button> ";
342
343 print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
344 __('Cancel')."</button>";
345
346 print "</div>";
347 }
348
349 function editSave() {
350
351 global $memcache;
352
353 if ($memcache) $memcache->flush();
354
355 $savemode = db_escape_string($_REQUEST["savemode"]);
356 $reg_exp = db_escape_string(trim($_REQUEST["reg_exp"]));
357 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
358 $filter_id = db_escape_string($_REQUEST["id"]);
359 $feed_id = db_escape_string($_REQUEST["feed_id"]);
360 $action_id = db_escape_string($_REQUEST["action_id"]);
361 $action_param = db_escape_string($_REQUEST["action_param"]);
362 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
363 $enabled = checkbox_to_sql_bool(db_escape_string($_REQUEST["enabled"]));
364 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
365
366 # for the time being, no other filters use params anyway...
367 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
368
369 if (!$feed_id) {
370 $feed_id = 'NULL';
371 } else {
372 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
373 }
374
375 /* When processing 'assign label' filters, action_param_label dropbox
376 * overrides action_param */
377
378 if ($action_id == 7) {
379 $action_param = $action_param_label;
380 }
381
382 if ($action_id == 6) {
383 $action_param = (int) str_replace("+", "", $action_param);
384 }
385
386 if ($savemode != "test") {
387 $result = db_query($this->link, "UPDATE ttrss_filters SET
388 reg_exp = '$reg_exp',
389 feed_id = $feed_id,
390 action_id = '$action_id',
391 filter_type = '$filter_type',
392 enabled = $enabled,
393 inverse = $inverse,
394 action_param = '$action_param',
395 filter_param = '$filter_param'
396 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
397 } else {
398
399 $this->filter_test($filter_type, $reg_exp,
400 $action_id, $action_param, $filter_param, sql_bool_to_bool($inverse),
401 (int) $_REQUEST["feed_id"]);
402
403 print "<div align='center'>";
404 print "<button dojoType=\"dijit.form.Button\"
405 onclick=\"return dijit.byId('filterTestDlg').hide()\">".
406 __('Close this window')."</button>";
407 print "</div>";
408
409 }
410 }
411
412 function remove() {
413
414 if ($memcache) $memcache->flush();
415
416 $ids = split(",", db_escape_string($_REQUEST["ids"]));
417
418 foreach ($ids as $id) {
419 db_query($this->link, "DELETE FROM ttrss_filters WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
420 }
421 }
422
423 function add() {
424
425 if ($memcache) $memcache->flush();
426
427 $savemode = db_escape_string($_REQUEST["savemode"]);
428 $regexp = db_escape_string(trim($_REQUEST["reg_exp"]));
429 $filter_type = db_escape_string(trim($_REQUEST["filter_type"]));
430 $feed_id = db_escape_string($_REQUEST["feed_id"]);
431 $action_id = db_escape_string($_REQUEST["action_id"]);
432 $action_param = db_escape_string($_REQUEST["action_param"]);
433 $action_param_label = db_escape_string($_REQUEST["action_param_label"]);
434 $inverse = checkbox_to_sql_bool(db_escape_string($_REQUEST["inverse"]));
435
436 # for the time being, no other filters use params anyway...
437 $filter_param = db_escape_string($_REQUEST["filter_date_modifier"]);
438
439 if (!$regexp) return;
440
441 if (!$feed_id) {
442 $feed_id = 'NULL';
443 } else {
444 $feed_id = sprintf("'%s'", db_escape_string($feed_id));
445 }
446
447 /* When processing 'assign label' filters, action_param_label dropbox
448 * overrides action_param */
449
450 if ($action_id == 7) {
451 $action_param = $action_param_label;
452 }
453
454 if ($action_id == 6) {
455 $action_param = (int) str_replace("+", "", $action_param);
456 }
457
458 if ($savemode != "test") {
459 $result = db_query($this->link,
460 "INSERT INTO ttrss_filters (reg_exp,filter_type,owner_uid,feed_id,
461 action_id, action_param, inverse, filter_param)
462 VALUES
463 ('$regexp', '$filter_type','".$_SESSION["uid"]."',
464 $feed_id, '$action_id', '$action_param', $inverse,
465 '$filter_param')");
466
467 if (db_affected_rows($this->link, $result) != 0) {
468 print T_sprintf("Created filter <b>%s</b>", htmlspecialchars($regexp));
469 }
470
471 } else {
472
473 $this->filter_test($filter_type, $regexp,
474 $action_id, $action_param, $filter_param, sql_bool_to_bool($inverse),
475 (int) $_REQUEST["feed_id"]);
476
477 print "<div align='center'>";
478 print "<button dojoType=\"dijit.form.Button\"
479 onclick=\"return dijit.byId('filterTestDlg').hide()\">".
480 __('Close this window')."</button>";
481 print "</div>";
482
483 }
484 }
485
486 function index() {
487
488 $sort = db_escape_string($_REQUEST["sort"]);
489
490 if (!$sort || $sort == "undefined") {
491 $sort = "reg_exp";
492 }
493
494 $result = db_query($this->link, "SELECT id,description
495 FROM ttrss_filter_types ORDER BY description");
496
497 $filter_types = array();
498
499 while ($line = db_fetch_assoc($result)) {
500 //array_push($filter_types, $line["description"]);
501 $filter_types[$line["id"]] = $line["description"];
502 }
503
504
505 $filter_search = db_escape_string($_REQUEST["search"]);
506
507 if (array_key_exists("search", $_REQUEST)) {
508 $_SESSION["prefs_filter_search"] = $filter_search;
509 } else {
510 $filter_search = $_SESSION["prefs_filter_search"];
511 }
512
513 print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
514 print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
515 print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
516
517 print "<div dojoType=\"dijit.form.DropDownButton\">".
518 "<span>" . __('Select')."</span>";
519 print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
520 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
521 dojoType=\"dijit.MenuItem\">".__('All')."</div>";
522 print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
523 dojoType=\"dijit.MenuItem\">".__('None')."</div>";
524 print "</div></div>";
525
526 print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
527 __('Create filter')."</button> ";
528
529 print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
530 __('Edit')."</button> ";
531
532 print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
533 __('Remove')."</button> ";
534
535 if (defined('_ENABLE_FEED_DEBUGGING')) {
536 print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
537 __('Rescore articles')."</button> ";
538 }
539
540 print "</div>"; # toolbar
541 print "</div>"; # toolbar-frame
542 print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
543
544 print "<div id=\"filterlistLoading\">
545 <img src='images/indicator_tiny.gif'>".
546 __("Loading, please wait...")."</div>";
547
548 print "<div dojoType=\"dojo.data.ItemFileWriteStore\" jsId=\"filterStore\"
549 url=\"backend.php?op=pref-filters&method=getfiltertree\">
550 </div>
551 <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
552 query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Feeds\"
553 childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
554 </div>
555 <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
556 model=\"filterModel\" openOnClick=\"true\">
557 <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
558 Element.hide(\"filterlistLoading\");
559 </script>
560 <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
561 var id = String(item.id);
562 var bare_id = id.substr(id.indexOf(':')+1);
563
564 if (id.match('FILTER:')) {
565 editFilter(bare_id);
566 }
567 </script>
568
569 </div>";
570
571 print "</div>"; #pane
572 print "</div>"; #container
573
574 }
575 }
576 ?>