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