]>
Commit | Line | Data |
---|---|---|
8e17d663 | 1 | <?php |
369dbc19 | 2 | class Pref_Filters extends Handler_Protected { |
8e17d663 | 3 | |
8484ce22 | 4 | function csrf_ignore($method) { |
6aff7845 | 5 | $csrf_ignored = array("index", "getfiltertree", "edit", "newfilter", "newrule", |
a86ba0a6 | 6 | "newaction", "savefilterorder"); |
8484ce22 AD |
7 | |
8 | return array_search($method, $csrf_ignored) !== false; | |
9 | } | |
10 | ||
a86ba0a6 | 11 | function filtersortreset() { |
f594717d AD |
12 | $sth = $this->pdo->prepare("UPDATE ttrss_filters2 |
13 | SET order_id = 0 WHERE owner_uid = ?"); | |
14 | $sth->execute([$_SESSION['uid']]); | |
a86ba0a6 AD |
15 | return; |
16 | } | |
17 | ||
18 | function savefilterorder() { | |
67bf38af | 19 | $data = json_decode($_POST['payload'], true); |
a86ba0a6 | 20 | |
e6532439 | 21 | #file_put_contents("/tmp/saveorder.json", clean($_POST['payload'])); |
a86ba0a6 AD |
22 | #$data = json_decode(file_get_contents("/tmp/saveorder.json"), true); |
23 | ||
24 | if (!is_array($data['items'])) | |
25 | $data['items'] = json_decode($data['items'], true); | |
26 | ||
27 | $index = 0; | |
28 | ||
29 | if (is_array($data) && is_array($data['items'])) { | |
f594717d AD |
30 | |
31 | $sth = $this->pdo->prepare("UPDATE ttrss_filters2 SET | |
32 | order_id = ? WHERE id = ? AND | |
33 | owner_uid = ?"); | |
34 | ||
a86ba0a6 AD |
35 | foreach ($data['items'][0]['items'] as $item) { |
36 | $filter_id = (int) str_replace("FILTER:", "", $item['_reference']); | |
37 | ||
38 | if ($filter_id > 0) { | |
f594717d | 39 | $sth->execute([$index, $filter_id, $_SESSION['uid']]); |
a86ba0a6 AD |
40 | ++$index; |
41 | } | |
42 | } | |
43 | } | |
44 | ||
45 | return; | |
46 | } | |
47 | ||
a007c18d | 48 | function testFilterDo() { |
e6532439 AD |
49 | $offset = (int) clean($_REQUEST["offset"]); |
50 | $limit = (int) clean($_REQUEST["limit"]); | |
a86ba0a6 | 51 | |
4e02f582 | 52 | $filter = array(); |
8e17d663 | 53 | |
4e02f582 | 54 | $filter["enabled"] = true; |
e6532439 AD |
55 | $filter["match_any_rule"] = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"])); |
56 | $filter["inverse"] = checkbox_to_sql_bool(clean($_REQUEST["inverse"])); | |
a3a896a1 | 57 | |
4e02f582 | 58 | $filter["rules"] = array(); |
c939dfc3 | 59 | $filter["actions"] = array("dummy-action"); |
8e17d663 | 60 | |
f594717d | 61 | $res = $this->pdo->query("SELECT id,name FROM ttrss_filter_types"); |
8e17d663 | 62 | |
4e02f582 | 63 | $filter_types = array(); |
f594717d | 64 | while ($line = $res->fetch()) { |
4e02f582 AD |
65 | $filter_types[$line["id"]] = $line["name"]; |
66 | } | |
8e17d663 | 67 | |
c939dfc3 AD |
68 | $scope_qparts = array(); |
69 | ||
4e02f582 | 70 | $rctr = 0; |
e6532439 | 71 | foreach (clean($_REQUEST["rule"]) AS $r) { |
4e02f582 | 72 | $rule = json_decode($r, true); |
8e17d663 | 73 | |
4e02f582 AD |
74 | if ($rule && $rctr < 5) { |
75 | $rule["type"] = $filter_types[$rule["filter_type"]]; | |
76 | unset($rule["filter_type"]); | |
8e17d663 | 77 | |
02f3992a AD |
78 | $scope_inner_qparts = []; |
79 | foreach ($rule["feed_id"] as $feed_id) { | |
0bf7e007 | 80 | |
02f3992a AD |
81 | if (strpos($feed_id, "CAT:") === 0) { |
82 | $cat_id = (int) substr($feed_id, 4); | |
f594717d | 83 | array_push($scope_inner_qparts, "cat_id = " . $this->pdo->quote($cat_id)); |
02f3992a | 84 | } else if ($feed_id > 0) { |
f594717d | 85 | array_push($scope_inner_qparts, "feed_id = " . $this->pdo->quote($feed_id)); |
02f3992a AD |
86 | } |
87 | } | |
88 | ||
89 | if (count($scope_inner_qparts) > 0) { | |
90 | array_push($scope_qparts, "(" . implode(" OR ", $scope_inner_qparts) . ")"); | |
91 | } | |
c939dfc3 | 92 | |
4e02f582 | 93 | array_push($filter["rules"], $rule); |
8e17d663 | 94 | |
4e02f582 AD |
95 | ++$rctr; |
96 | } else { | |
97 | break; | |
98 | } | |
99 | } | |
8e17d663 | 100 | |
02f3992a AD |
101 | if (count($scope_qparts) == 0) $scope_qparts = ["true"]; |
102 | ||
314956d4 | 103 | $glue = $filter['match_any_rule'] ? " OR " : " AND "; |
c939dfc3 | 104 | $scope_qpart = join($glue, $scope_qparts); |
74175e6a | 105 | |
9638ec51 AD |
106 | if (!$scope_qpart) $scope_qpart = "true"; |
107 | ||
a007c18d AD |
108 | $rv = array(); |
109 | ||
110 | //while ($found < $limit && $offset < $limit * 1000 && time() - $started < ini_get("max_execution_time") * 0.7) { | |
8e17d663 | 111 | |
248c62a1 AD |
112 | $sth = $this->pdo->prepare("SELECT ttrss_entries.id, |
113 | ttrss_entries.title, | |
114 | ttrss_feeds.id AS feed_id, | |
115 | ttrss_feeds.title AS feed_title, | |
116 | ttrss_feed_categories.id AS cat_id, | |
117 | content, | |
118 | date_entered, | |
119 | link, | |
120 | author, | |
121 | tag_cache | |
122 | FROM | |
123 | ttrss_entries, ttrss_user_entries | |
124 | LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id) | |
125 | LEFT JOIN ttrss_feed_categories ON (ttrss_feeds.cat_id = ttrss_feed_categories.id) | |
126 | WHERE | |
127 | ref_id = ttrss_entries.id AND | |
128 | ($scope_qpart) AND | |
129 | ttrss_user_entries.owner_uid = ? | |
130 | ORDER BY date_entered DESC LIMIT $limit OFFSET $offset"); | |
56fbb82c | 131 | |
248c62a1 | 132 | $sth->execute([$_SESSION['uid']]); |
f594717d | 133 | |
248c62a1 | 134 | while ($line = $sth->fetch()) { |
56fbb82c | 135 | |
248c62a1 AD |
136 | $rc = RSSUtils::get_article_filters(array($filter), $line['title'], $line['content'], $line['link'], |
137 | $line['author'], explode(",", $line['tag_cache'])); | |
56fbb82c | 138 | |
248c62a1 | 139 | if (count($rc) > 0) { |
56fbb82c | 140 | |
248c62a1 | 141 | $line["content_preview"] = truncate_string(strip_tags($line["content"]), 200, '…'); |
56fbb82c | 142 | |
248c62a1 AD |
143 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) { |
144 | $line = $p->hook_query_headlines($line, 100); | |
145 | } | |
56fbb82c | 146 | |
248c62a1 | 147 | $content_preview = $line["content_preview"]; |
8e17d663 | 148 | |
248c62a1 | 149 | $tmp = "<tr style='margin-top : 5px'>"; |
dbeef0ca | 150 | |
248c62a1 AD |
151 | #$tmp .= "<td width='5%' align='center'><input dojoType=\"dijit.form.CheckBox\" |
152 | # checked=\"1\" disabled=\"1\" type=\"checkbox\"></td>"; | |
c939dfc3 | 153 | |
248c62a1 AD |
154 | $id = $line['id']; |
155 | $tmp .= "<td width='5%' align='center'><img style='cursor : pointer' title='".__("Preview article")."' | |
156 | src='images/information.png' onclick='openArticlePopup($id)'></td><td>"; | |
c939dfc3 | 157 | |
248c62a1 AD |
158 | /*foreach ($filter['rules'] as $rule) { |
159 | $reg_exp = str_replace('/', '\/', $rule["reg_exp"]); | |
5cb17306 | 160 | |
248c62a1 AD |
161 | $line["title"] = preg_replace("/($reg_exp)/i", |
162 | "<span class=\"highlight\">$1</span>", $line["title"]); | |
5cb17306 | 163 | |
248c62a1 AD |
164 | $content_preview = preg_replace("/($reg_exp)/i", |
165 | "<span class=\"highlight\">$1</span>", $content_preview); | |
166 | }*/ | |
261226a3 | 167 | |
248c62a1 AD |
168 | $tmp .= "<strong>" . $line["title"] . "</strong><br/>"; |
169 | $tmp .= $line['feed_title'] . ", " . mb_substr($line["date_entered"], 0, 16); | |
170 | $tmp .= "<div class='insensitive'>" . $content_preview . "</div>"; | |
171 | $tmp .= "</td></tr>"; | |
5cb17306 | 172 | |
248c62a1 | 173 | array_push($rv, $tmp); |
5cb17306 | 174 | |
248c62a1 AD |
175 | /*array_push($rv, array("title" => $line["title"], |
176 | "content" => $content_preview, | |
177 | "date" => $line["date_entered"], | |
178 | "feed" => $line["feed_title"])); */ | |
c939dfc3 | 179 | |
c939dfc3 | 180 | } |
248c62a1 | 181 | } |
c939dfc3 | 182 | |
a007c18d AD |
183 | //$offset += $limit; |
184 | //} | |
c939dfc3 | 185 | |
a007c18d | 186 | /*if ($found == 0) { |
c939dfc3 AD |
187 | print "<tr><td align='center'>" . |
188 | __("No recent articles matching this filter have been found."); | |
a007c18d AD |
189 | }*/ |
190 | ||
191 | print json_encode($rv); | |
192 | } | |
193 | ||
194 | function testFilter() { | |
195 | ||
196 | if (isset($_REQUEST["offset"])) return $this->testFilterDo(); | |
197 | ||
198 | //print __("Articles matching this filter:"); | |
199 | ||
261226a3 | 200 | print "<div><img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'> <span id='prefFilterProgressMsg'>Looking for articles...</span></div>"; |
8e17d663 | 201 | |
a007c18d AD |
202 | print "<br/><div class=\"filterTestHolder\">"; |
203 | print "<table width=\"100%\" cellspacing=\"0\" id=\"prefFilterTestResultList\">"; | |
4e02f582 AD |
204 | print "</table></div>"; |
205 | ||
206 | print "<div style='text-align : center'>"; | |
207 | print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">". | |
208 | __('Close this window')."</button>"; | |
8e17d663 AD |
209 | print "</div>"; |
210 | ||
4e02f582 AD |
211 | } |
212 | ||
50e04efd | 213 | private function getfilterrules_concise($filter_id) { |
f594717d | 214 | $sth = $this->pdo->prepare("SELECT reg_exp, |
50e04efd | 215 | inverse, |
02f3992a | 216 | match_on, |
50e04efd AD |
217 | feed_id, |
218 | cat_id, | |
219 | cat_filter, | |
220 | ttrss_filter_types.description AS field | |
221 | FROM | |
222 | ttrss_filters2_rules, ttrss_filter_types | |
223 | WHERE | |
f594717d | 224 | filter_id = ? AND filter_type = ttrss_filter_types.id |
4f100ece | 225 | ORDER BY reg_exp"); |
f594717d | 226 | $sth->execute([$filter_id]); |
50e04efd AD |
227 | |
228 | $rv = ""; | |
229 | ||
f594717d | 230 | while ($line = $sth->fetch()) { |
50e04efd | 231 | |
02f3992a AD |
232 | if ($line["match_on"]) { |
233 | $feeds = json_decode($line["match_on"], true); | |
234 | $feeds_fmt = []; | |
235 | ||
236 | foreach ($feeds as $feed_id) { | |
237 | ||
238 | if (strpos($feed_id, "CAT:") === 0) { | |
239 | $feed_id = (int)substr($feed_id, 4); | |
240 | array_push($feeds_fmt, Feeds::getCategoryTitle($feed_id)); | |
241 | } else { | |
242 | if ($feed_id) | |
243 | array_push($feeds_fmt, Feeds::getFeedTitle((int)$feed_id)); | |
244 | else | |
245 | array_push($feeds_fmt, __("All feeds")); | |
246 | } | |
247 | } | |
248 | ||
249 | $where = implode(", ", $feeds_fmt); | |
250 | ||
251 | } else { | |
252 | ||
8ff3cbb3 | 253 | $where = $line["cat_filter"] ? |
02f3992a AD |
254 | Feeds::getCategoryTitle($line["cat_id"]) : |
255 | ($line["feed_id"] ? | |
256 | Feeds::getFeedTitle($line["feed_id"]) : __("All feeds")); | |
257 | } | |
50e04efd AD |
258 | |
259 | # $where = $line["cat_id"] . "/" . $line["feed_id"]; | |
260 | ||
8ff3cbb3 | 261 | $inverse = $line["inverse"] ? "inverse" : ""; |
50e04efd AD |
262 | |
263 | $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s", | |
154f14d0 | 264 | htmlspecialchars($line["reg_exp"]), |
50e04efd AD |
265 | $line["field"], |
266 | $where, | |
8ff3cbb3 | 267 | $line["inverse"] ? __("(inverse)") : "") . "</span>"; |
50e04efd AD |
268 | } |
269 | ||
270 | return $rv; | |
271 | } | |
8e17d663 AD |
272 | |
273 | function getfiltertree() { | |
274 | $root = array(); | |
275 | $root['id'] = 'root'; | |
276 | $root['name'] = __('Filters'); | |
277 | $root['items'] = array(); | |
278 | ||
69c6e94d AD |
279 | $filter_search = $_SESSION["prefs_filter_search"]; |
280 | ||
f594717d | 281 | $sth = $this->pdo->prepare("SELECT *, |
84d9750e AD |
282 | (SELECT action_param FROM ttrss_filters2_actions |
283 | WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param, | |
6aff7845 AD |
284 | (SELECT action_id FROM ttrss_filters2_actions |
285 | WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id, | |
37f78940 AD |
286 | (SELECT description FROM ttrss_filter_actions |
287 | WHERE id = (SELECT action_id FROM ttrss_filters2_actions | |
288 | WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name, | |
6aff7845 AD |
289 | (SELECT reg_exp FROM ttrss_filters2_rules |
290 | WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp | |
291 | FROM ttrss_filters2 WHERE | |
f594717d AD |
292 | owner_uid = ? ORDER BY order_id, title"); |
293 | $sth->execute([$_SESSION['uid']]); | |
69c6e94d | 294 | |
37f78940 AD |
295 | $folder = array(); |
296 | $folder['items'] = array(); | |
297 | ||
f594717d | 298 | while ($line = $sth->fetch()) { |
8e17d663 | 299 | |
6aff7845 | 300 | $name = $this->getFilterName($line["id"]); |
8e17d663 | 301 | |
69c6e94d AD |
302 | $match_ok = false; |
303 | if ($filter_search) { | |
e794e434 | 304 | $rules_sth = $this->pdo->prepare("SELECT reg_exp |
f594717d AD |
305 | FROM ttrss_filters2_rules WHERE filter_id = ?"); |
306 | $rules_sth->execute([$line['id']]); | |
69c6e94d | 307 | |
f594717d | 308 | while ($rule_line = $rules_sth->fetch()) { |
69c6e94d AD |
309 | if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) { |
310 | $match_ok = true; | |
311 | break; | |
312 | } | |
313 | } | |
314 | } | |
315 | ||
84d9750e | 316 | if ($line['action_id'] == 7) { |
f594717d AD |
317 | $label_sth = $this->pdo->prepare("SELECT fg_color, bg_color |
318 | FROM ttrss_labels2 WHERE caption = ? AND | |
319 | owner_uid = ?"); | |
320 | $label_sth->execute([$line['action_param'], $_SESSION['uid']]); | |
84d9750e | 321 | |
f594717d AD |
322 | if ($label_row = $label_sth->fetch()) { |
323 | $fg_color = $label_row["fg_color"]; | |
324 | $bg_color = $label_row["bg_color"]; | |
84d9750e | 325 | |
da46d21e | 326 | $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>α</span>" . $name[1]; |
84d9750e AD |
327 | } |
328 | } | |
329 | ||
6aff7845 AD |
330 | $filter = array(); |
331 | $filter['id'] = 'FILTER:' . $line['id']; | |
332 | $filter['bare_id'] = $line['id']; | |
333 | $filter['name'] = $name[0]; | |
334 | $filter['param'] = $name[1]; | |
335 | $filter['checkbox'] = false; | |
8ff3cbb3 | 336 | $filter['enabled'] = $line["enabled"]; |
50e04efd | 337 | $filter['rules'] = $this->getfilterrules_concise($line['id']); |
8e17d663 | 338 | |
69c6e94d AD |
339 | if (!$filter_search || $match_ok) { |
340 | array_push($folder['items'], $filter); | |
341 | } | |
8e17d663 AD |
342 | } |
343 | ||
da46d21e | 344 | $root['items'] = $folder['items']; |
e8b70387 | 345 | |
8e17d663 AD |
346 | $fl = array(); |
347 | $fl['identifier'] = 'id'; | |
348 | $fl['label'] = 'name'; | |
349 | $fl['items'] = array($root); | |
350 | ||
351 | print json_encode($fl); | |
352 | return; | |
353 | } | |
354 | ||
355 | function edit() { | |
356 | ||
e6532439 | 357 | $filter_id = clean($_REQUEST["id"]); |
8e17d663 | 358 | |
e794e434 | 359 | $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2 |
f594717d AD |
360 | WHERE id = ? AND owner_uid = ?"); |
361 | $sth->execute([$filter_id, $_SESSION['uid']]); | |
8e17d663 | 362 | |
f594717d | 363 | if ($row = $sth->fetch()) { |
8e17d663 | 364 | |
8ff3cbb3 AD |
365 | $enabled = $row["enabled"]; |
366 | $match_any_rule = $row["match_any_rule"]; | |
367 | $inverse = $row["inverse"]; | |
f594717d | 368 | $title = htmlspecialchars($row["title"]); |
8e17d663 | 369 | |
f594717d | 370 | print "<form id=\"filter_edit_form\" onsubmit='return false'>"; |
8e17d663 | 371 | |
f594717d AD |
372 | print_hidden("op", "pref-filters"); |
373 | print_hidden("id", "$filter_id"); | |
374 | print_hidden("method", "editSave"); | |
375 | print_hidden("csrf_token", $_SESSION['csrf_token']); | |
18ad09af | 376 | |
f594717d | 377 | print "<div class=\"dlgSec\">".__("Caption")."</div>"; |
18ad09af | 378 | |
f594717d | 379 | print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">"; |
18ad09af | 380 | |
f594717d | 381 | print "</div>"; |
8e17d663 | 382 | |
f594717d | 383 | print "<div class=\"dlgSec\">".__("Match")."</div>"; |
8e17d663 | 384 | |
f594717d AD |
385 | print "<div dojoType=\"dijit.Toolbar\">"; |
386 | ||
387 | print "<div dojoType=\"dijit.form.DropDownButton\">". | |
6aff7845 | 388 | "<span>" . __('Select')."</span>"; |
f594717d AD |
389 | print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">"; |
390 | print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\" | |
6aff7845 | 391 | dojoType=\"dijit.MenuItem\">".__('All')."</div>"; |
f594717d | 392 | print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\" |
6aff7845 | 393 | dojoType=\"dijit.MenuItem\">".__('None')."</div>"; |
f594717d | 394 | print "</div></div>"; |
8e17d663 | 395 | |
f594717d AD |
396 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">". |
397 | __('Add')."</button> "; | |
8e17d663 | 398 | |
f594717d AD |
399 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">". |
400 | __('Delete')."</button> "; | |
8e17d663 | 401 | |
f594717d | 402 | print "</div>"; |
8e17d663 | 403 | |
f594717d | 404 | print "<ul id='filterDlg_Matches'>"; |
8e17d663 | 405 | |
f594717d AD |
406 | $rules_sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_rules |
407 | WHERE filter_id = ? ORDER BY reg_exp, id"); | |
408 | $rules_sth->execute([$filter_id]); | |
8e17d663 | 409 | |
f594717d AD |
410 | while ($line = $rules_sth->fetch()) { |
411 | if ($line["match_on"]) { | |
412 | $line["feed_id"] = json_decode($line["match_on"], true); | |
413 | } else { | |
8ff3cbb3 | 414 | if ($line["cat_filter"]) { |
f594717d AD |
415 | $feed_id = "CAT:" . (int)$line["cat_id"]; |
416 | } else { | |
417 | $feed_id = (int)$line["feed_id"]; | |
418 | } | |
02f3992a | 419 | |
f594717d AD |
420 | $line["feed_id"] = ["" . $feed_id]; // set item type to string for in_array() |
421 | } | |
02f3992a | 422 | |
f594717d AD |
423 | unset($line["cat_filter"]); |
424 | unset($line["cat_id"]); | |
425 | unset($line["filter_id"]); | |
426 | unset($line["id"]); | |
8ff3cbb3 | 427 | if (!$line["inverse"]) unset($line["inverse"]); |
f594717d | 428 | unset($line["match_on"]); |
02f3992a | 429 | |
f594717d | 430 | $data = htmlspecialchars(json_encode($line)); |
8e17d663 | 431 | |
f594717d AD |
432 | print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>". |
433 | "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>". | |
434 | "<input type='hidden' name='rule[]' value=\"$data\"/></li>"; | |
435 | } | |
ba975b2e | 436 | |
f594717d | 437 | print "</ul>"; |
ba975b2e | 438 | |
f594717d | 439 | print "</div>"; |
8e17d663 | 440 | |
f594717d | 441 | print "<div class=\"dlgSec\">".__("Apply actions")."</div>"; |
8e17d663 | 442 | |
f594717d | 443 | print "<div dojoType=\"dijit.Toolbar\">"; |
8e17d663 | 444 | |
f594717d | 445 | print "<div dojoType=\"dijit.form.DropDownButton\">". |
6aff7845 | 446 | "<span>" . __('Select')."</span>"; |
f594717d AD |
447 | print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">"; |
448 | print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\" | |
6aff7845 | 449 | dojoType=\"dijit.MenuItem\">".__('All')."</div>"; |
f594717d | 450 | print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\" |
6aff7845 | 451 | dojoType=\"dijit.MenuItem\">".__('None')."</div>"; |
f594717d | 452 | print "</div></div>"; |
8e17d663 | 453 | |
f594717d AD |
454 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">". |
455 | __('Add')."</button> "; | |
8e17d663 | 456 | |
f594717d AD |
457 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">". |
458 | __('Delete')."</button> "; | |
8e17d663 | 459 | |
f594717d | 460 | print "</div>"; |
8e17d663 | 461 | |
f594717d | 462 | print "<ul id='filterDlg_Actions'>"; |
8e17d663 | 463 | |
f594717d AD |
464 | $actions_sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_actions |
465 | WHERE filter_id = ? ORDER BY id"); | |
466 | $actions_sth->execute([$filter_id]); | |
8e17d663 | 467 | |
f594717d AD |
468 | while ($line = $actions_sth->fetch()) { |
469 | $line["action_param_label"] = $line["action_param"]; | |
4e02f582 | 470 | |
f594717d AD |
471 | unset($line["filter_id"]); |
472 | unset($line["id"]); | |
4e02f582 | 473 | |
f594717d | 474 | $data = htmlspecialchars(json_encode($line)); |
8e17d663 | 475 | |
f594717d AD |
476 | print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>". |
477 | "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>". | |
478 | "<input type='hidden' name='action[]' value=\"$data\"/></li>"; | |
479 | } | |
8e17d663 | 480 | |
f594717d | 481 | print "</ul>"; |
8e17d663 | 482 | |
f594717d | 483 | print "</div>"; |
8e17d663 | 484 | |
f594717d AD |
485 | if ($enabled) { |
486 | $checked = "checked=\"1\""; | |
487 | } else { | |
488 | $checked = ""; | |
489 | } | |
8e17d663 | 490 | |
f594717d | 491 | print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked> |
6aff7845 | 492 | <label for=\"enabled\">".__('Enabled')."</label>"; |
8e17d663 | 493 | |
f594717d AD |
494 | if ($match_any_rule) { |
495 | $checked = "checked=\"1\""; | |
496 | } else { | |
497 | $checked = ""; | |
498 | } | |
8e17d663 | 499 | |
f594717d | 500 | print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked> |
6aff7845 | 501 | <label for=\"match_any_rule\">".__('Match any rule')."</label>"; |
8e17d663 | 502 | |
f594717d AD |
503 | if ($inverse) { |
504 | $checked = "checked=\"1\""; | |
505 | } else { | |
506 | $checked = ""; | |
507 | } | |
a3a896a1 | 508 | |
f594717d | 509 | print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked> |
a3a896a1 AD |
510 | <label for=\"inverse\">".__('Inverse matching')."</label>"; |
511 | ||
f594717d | 512 | print "<p/>"; |
8e17d663 | 513 | |
f594717d | 514 | print "<div class=\"dlgButtons\">"; |
8e17d663 | 515 | |
f594717d | 516 | print "<div style=\"float : left\">"; |
6fb5ce5e | 517 | print "<button dojoType=\"dijit.form.Button\" class=\"btn-danger\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">". |
f594717d AD |
518 | __('Remove')."</button>"; |
519 | print "</div>"; | |
8e17d663 | 520 | |
f594717d AD |
521 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">". |
522 | __('Test')."</button> "; | |
8e17d663 | 523 | |
6fb5ce5e | 524 | print "<button dojoType=\"dijit.form.Button\" type=\"submit\" class=\"btn-primary\" onclick=\"return dijit.byId('filterEditDlg').execute()\">". |
f594717d | 525 | __('Save')."</button> "; |
8e17d663 | 526 | |
f594717d AD |
527 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">". |
528 | __('Cancel')."</button>"; | |
8e17d663 | 529 | |
f594717d | 530 | print "</div>"; |
5f7be995 | 531 | print "</form>"; |
e794e434 | 532 | |
f594717d | 533 | } |
8e17d663 AD |
534 | } |
535 | ||
6aff7845 | 536 | private function getRuleName($rule) { |
e6532439 | 537 | if (!$rule) $rule = json_decode(clean($_REQUEST["rule"]), true); |
8e17d663 | 538 | |
02f3992a AD |
539 | $feeds = $rule["feed_id"]; |
540 | $feeds_fmt = []; | |
8e17d663 | 541 | |
02f3992a | 542 | if (!is_array($feeds)) $feeds = [$feeds]; |
0bf7e007 | 543 | |
02f3992a AD |
544 | foreach ($feeds as $feed_id) { |
545 | ||
546 | if (strpos($feed_id, "CAT:") === 0) { | |
547 | $feed_id = (int)substr($feed_id, 4); | |
548 | array_push($feeds_fmt, Feeds::getCategoryTitle($feed_id)); | |
549 | } else { | |
550 | if ($feed_id) | |
551 | array_push($feeds_fmt, Feeds::getFeedTitle((int)$feed_id)); | |
552 | else | |
553 | array_push($feeds_fmt, __("All feeds")); | |
554 | } | |
555 | } | |
556 | ||
557 | $feed = implode(", ", $feeds_fmt); | |
8e17d663 | 558 | |
f594717d AD |
559 | $sth = $this->pdo->prepare("SELECT description FROM ttrss_filter_types |
560 | WHERE id = ?"); | |
561 | $sth->execute([(int)$rule["filter_type"]]); | |
562 | ||
563 | if ($row = $sth->fetch()) { | |
564 | $filter_type = $row["description"]; | |
565 | } else { | |
566 | $filter_type = "?UNKNOWN?"; | |
567 | } | |
4c9d0490 | 568 | |
50e04efd AD |
569 | $inverse = isset($rule["inverse"]) ? "inverse" : ""; |
570 | ||
571 | return "<span class='filterRule $inverse'>" . | |
154f14d0 | 572 | T_sprintf("%s on %s in %s %s", htmlspecialchars($rule["reg_exp"]), |
50e04efd | 573 | $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "") . "</span>"; |
6aff7845 | 574 | } |
ba975b2e | 575 | |
6aff7845 | 576 | function printRuleName() { |
e6532439 | 577 | print $this->getRuleName(json_decode(clean($_REQUEST["rule"]), true)); |
6aff7845 | 578 | } |
8e17d663 | 579 | |
6aff7845 | 580 | private function getActionName($action) { |
f594717d AD |
581 | $sth = $this->pdo->prepare("SELECT description FROM |
582 | ttrss_filter_actions WHERE id = ?"); | |
583 | $sth->execute([(int)$action["action_id"]]); | |
584 | ||
585 | $title = ""; | |
586 | ||
587 | if ($row = $sth->fetch()) { | |
8e17d663 | 588 | |
f594717d | 589 | $title = __($row["description"]); |
8e17d663 | 590 | |
f594717d AD |
591 | if ($action["action_id"] == 4 || $action["action_id"] == 6 || |
592 | $action["action_id"] == 7) | |
6aff7845 | 593 | $title .= ": " . $action["action_param"]; |
8e17d663 | 594 | |
f594717d AD |
595 | if ($action["action_id"] == 9) { |
596 | list ($pfclass, $pfaction) = explode(":", $action["action_param"]); | |
b8774453 | 597 | |
f594717d | 598 | $filter_actions = PluginHost::getInstance()->get_filter_actions(); |
b8774453 | 599 | |
f594717d AD |
600 | foreach ($filter_actions as $fclass => $factions) { |
601 | foreach ($factions as $faction) { | |
602 | if ($pfaction == $faction["action"] && $pfclass == $fclass) { | |
603 | $title .= ": " . $fclass . ": " . $faction["description"]; | |
604 | break; | |
605 | } | |
b8774453 AD |
606 | } |
607 | } | |
608 | } | |
609 | } | |
610 | ||
6aff7845 AD |
611 | return $title; |
612 | } | |
8e17d663 | 613 | |
6aff7845 | 614 | function printActionName() { |
e6532439 | 615 | print $this->getActionName(json_decode(clean($_REQUEST["action"]), true)); |
6aff7845 AD |
616 | } |
617 | ||
618 | function editSave() { | |
e6532439 | 619 | if (clean($_REQUEST["savemode"] && $_REQUEST["savemode"]) == "test") { |
4e02f582 AD |
620 | return $this->testFilter(); |
621 | } | |
6aff7845 | 622 | |
e6532439 AD |
623 | $filter_id = clean($_REQUEST["id"]); |
624 | $enabled = checkbox_to_sql_bool(clean($_REQUEST["enabled"])); | |
625 | $match_any_rule = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"])); | |
626 | $inverse = checkbox_to_sql_bool(clean($_REQUEST["inverse"])); | |
627 | $title = clean($_REQUEST["title"]); | |
6aff7845 | 628 | |
f594717d | 629 | $this->pdo->beginTransaction(); |
6aff7845 | 630 | |
f594717d AD |
631 | $sth = $this->pdo->prepare("UPDATE ttrss_filters2 SET enabled = ?, |
632 | match_any_rule = ?, | |
633 | inverse = ?, | |
634 | title = ? | |
635 | WHERE id = ? AND owner_uid = ?"); | |
636 | ||
637 | $sth->execute([$enabled, $match_any_rule, $inverse, $title, $filter_id, $_SESSION['uid']]); | |
6aff7845 AD |
638 | |
639 | $this->saveRulesAndActions($filter_id); | |
8e17d663 | 640 | |
f594717d | 641 | $this->pdo->commit(); |
8e17d663 AD |
642 | } |
643 | ||
644 | function remove() { | |
645 | ||
e6532439 | 646 | $ids = explode(",", clean($_REQUEST["ids"])); |
f594717d | 647 | $ids_qmarks = arr_qmarks($ids); |
8e17d663 | 648 | |
e794e434 | 649 | $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks) |
f594717d AD |
650 | AND owner_uid = ?"); |
651 | $sth->execute(array_merge($ids, [$_SESSION['uid']])); | |
8e17d663 AD |
652 | } |
653 | ||
f594717d AD |
654 | private function saveRulesAndActions($filter_id) |
655 | { | |
8e17d663 | 656 | |
f594717d AD |
657 | $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2_rules WHERE filter_id = ?"); |
658 | $sth->execute([$filter_id]); | |
8e17d663 | 659 | |
f594717d AD |
660 | $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2_actions WHERE filter_id = ?"); |
661 | $sth->execute([$filter_id]); | |
662 | ||
e6532439 AD |
663 | if (!is_array(clean($_REQUEST["rule"]))) $_REQUEST["rule"] = []; |
664 | if (!is_array(clean($_REQUEST["action"]))) $_REQUEST["action"] = []; | |
e794e434 | 665 | |
6aff7845 AD |
666 | if ($filter_id) { |
667 | /* create rules */ | |
8e17d663 | 668 | |
6aff7845 AD |
669 | $rules = array(); |
670 | $actions = array(); | |
8e17d663 | 671 | |
e6532439 | 672 | foreach (clean($_REQUEST["rule"]) as $rule) { |
6aff7845 AD |
673 | $rule = json_decode($rule, true); |
674 | unset($rule["id"]); | |
8e17d663 | 675 | |
6aff7845 AD |
676 | if (array_search($rule, $rules) === false) { |
677 | array_push($rules, $rule); | |
678 | } | |
679 | } | |
4c9d0490 | 680 | |
e6532439 | 681 | foreach (clean($_REQUEST["action"]) as $action) { |
6aff7845 AD |
682 | $action = json_decode($action, true); |
683 | unset($action["id"]); | |
ba975b2e | 684 | |
6aff7845 AD |
685 | if (array_search($action, $actions) === false) { |
686 | array_push($actions, $action); | |
687 | } | |
688 | } | |
8e17d663 | 689 | |
f594717d AD |
690 | $rsth = $this->pdo->prepare("INSERT INTO ttrss_filters2_rules |
691 | (filter_id, reg_exp,filter_type,feed_id,cat_id,match_on,inverse) VALUES | |
692 | (?, ?, ?, NULL, NULL, ?, ?)"); | |
693 | ||
6aff7845 AD |
694 | foreach ($rules as $rule) { |
695 | if ($rule) { | |
8e17d663 | 696 | |
f594717d | 697 | $reg_exp = trim($rule["reg_exp"]); |
bfccff11 | 698 | $inverse = isset($rule["inverse"]) ? 1 : 0; |
a3a896a1 | 699 | |
f594717d AD |
700 | $filter_type = (int)trim($rule["filter_type"]); |
701 | $match_on = json_encode($rule["feed_id"]); | |
8e17d663 | 702 | |
f594717d | 703 | $rsth->execute([$filter_id, $reg_exp, $filter_type, $match_on, $inverse]); |
6aff7845 | 704 | } |
8e17d663 AD |
705 | } |
706 | ||
f594717d AD |
707 | $asth = $this->pdo->prepare("INSERT INTO ttrss_filters2_actions |
708 | (filter_id, action_id, action_param) VALUES | |
709 | (?, ?, ?)"); | |
710 | ||
6aff7845 AD |
711 | foreach ($actions as $action) { |
712 | if ($action) { | |
713 | ||
f594717d AD |
714 | $action_id = (int)$action["action_id"]; |
715 | $action_param = $action["action_param"]; | |
716 | $action_param_label = $action["action_param_label"]; | |
6aff7845 AD |
717 | |
718 | if ($action_id == 7) { | |
719 | $action_param = $action_param_label; | |
720 | } | |
8e17d663 | 721 | |
6aff7845 | 722 | if ($action_id == 6) { |
f594717d | 723 | $action_param = (int)str_replace("+", "", $action_param); |
6aff7845 | 724 | } |
8e17d663 | 725 | |
f594717d | 726 | $asth->execute([$filter_id, $action_id, $action_param]); |
6aff7845 AD |
727 | } |
728 | } | |
8e17d663 | 729 | } |
6aff7845 AD |
730 | } |
731 | ||
732 | function add() { | |
e6532439 | 733 | if (clean($_REQUEST["savemode"] && $_REQUEST["savemode"]) == "test") { |
4e02f582 AD |
734 | return $this->testFilter(); |
735 | } | |
736 | ||
e6532439 AD |
737 | $enabled = checkbox_to_sql_bool(clean($_REQUEST["enabled"])); |
738 | $match_any_rule = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"])); | |
739 | $title = clean($_REQUEST["title"]); | |
740 | $inverse = checkbox_to_sql_bool(clean($_REQUEST["inverse"])); | |
6aff7845 | 741 | |
f594717d | 742 | $this->pdo->beginTransaction(); |
6aff7845 AD |
743 | |
744 | /* create base filter */ | |
745 | ||
f594717d | 746 | $sth = $this->pdo->prepare("INSERT INTO ttrss_filters2 |
afa1a260 | 747 | (owner_uid, match_any_rule, enabled, title, inverse) VALUES |
f594717d | 748 | (?, ?, ?, ?, ?)"); |
6aff7845 | 749 | |
f594717d | 750 | $sth->execute([$_SESSION['uid'], $match_any_rule, $enabled, $title, $inverse]); |
6aff7845 | 751 | |
f594717d AD |
752 | $sth = $this->pdo->prepare("SELECT MAX(id) AS id FROM ttrss_filters2 |
753 | WHERE owner_uid = ?"); | |
754 | $sth->execute([$_SESSION['uid']]); | |
6aff7845 | 755 | |
f594717d AD |
756 | if ($row = $sth->fetch()) { |
757 | $filter_id = $row['id']; | |
758 | $this->saveRulesAndActions($filter_id); | |
759 | } | |
6aff7845 | 760 | |
f594717d | 761 | $this->pdo->commit(); |
8e17d663 AD |
762 | } |
763 | ||
764 | function index() { | |
765 | ||
e6532439 | 766 | $filter_search = clean($_REQUEST["search"]); |
8e17d663 AD |
767 | |
768 | if (array_key_exists("search", $_REQUEST)) { | |
769 | $_SESSION["prefs_filter_search"] = $filter_search; | |
770 | } else { | |
771 | $filter_search = $_SESSION["prefs_filter_search"]; | |
772 | } | |
773 | ||
774 | print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">"; | |
775 | print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">"; | |
776 | print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">"; | |
777 | ||
310fa2dd AD |
778 | if (array_key_exists("search", $_REQUEST)) { |
779 | $_SESSION["prefs_filter_search"] = $filter_search; | |
780 | } else { | |
781 | $filter_search = $_SESSION["prefs_filter_search"]; | |
782 | } | |
783 | ||
784 | print "<div style='float : right; padding-right : 4px;'> | |
785 | <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\" | |
786 | value=\"$filter_search\"> | |
787 | <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">". | |
788 | __('Search')."</button> | |
789 | </div>"; | |
790 | ||
8e17d663 AD |
791 | print "<div dojoType=\"dijit.form.DropDownButton\">". |
792 | "<span>" . __('Select')."</span>"; | |
793 | print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">"; | |
794 | print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\" | |
795 | dojoType=\"dijit.MenuItem\">".__('All')."</div>"; | |
796 | print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\" | |
797 | dojoType=\"dijit.MenuItem\">".__('None')."</div>"; | |
798 | print "</div></div>"; | |
799 | ||
800 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">". | |
801 | __('Create filter')."</button> "; | |
802 | ||
6aff7845 AD |
803 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">". |
804 | __('Combine')."</button> "; | |
805 | ||
8e17d663 AD |
806 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">". |
807 | __('Edit')."</button> "; | |
808 | ||
a86ba0a6 AD |
809 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">". |
810 | __('Reset sort order')."</button> "; | |
811 | ||
812 | ||
8e17d663 AD |
813 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">". |
814 | __('Remove')."</button> "; | |
815 | ||
8e17d663 AD |
816 | print "</div>"; # toolbar |
817 | print "</div>"; # toolbar-frame | |
818 | print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">"; | |
819 | ||
820 | print "<div id=\"filterlistLoading\"> | |
821 | <img src='images/indicator_tiny.gif'>". | |
822 | __("Loading, please wait...")."</div>"; | |
823 | ||
a86ba0a6 | 824 | print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\" |
8e17d663 AD |
825 | url=\"backend.php?op=pref-filters&method=getfiltertree\"> |
826 | </div> | |
827 | <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\" | |
a86ba0a6 | 828 | query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\" |
8e17d663 AD |
829 | childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\"> |
830 | </div> | |
831 | <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\" | |
a86ba0a6 AD |
832 | dndController=\"dijit.tree.dndSource\" |
833 | betweenThreshold=\"5\" | |
8e17d663 AD |
834 | model=\"filterModel\" openOnClick=\"true\"> |
835 | <script type=\"dojo/method\" event=\"onLoad\" args=\"item\"> | |
836 | Element.hide(\"filterlistLoading\"); | |
837 | </script> | |
838 | <script type=\"dojo/method\" event=\"onClick\" args=\"item\"> | |
839 | var id = String(item.id); | |
840 | var bare_id = id.substr(id.indexOf(':')+1); | |
841 | ||
842 | if (id.match('FILTER:')) { | |
843 | editFilter(bare_id); | |
844 | } | |
845 | </script> | |
846 | ||
847 | </div>"; | |
848 | ||
849 | print "</div>"; #pane | |
6065f3ad | 850 | |
1ffe3391 | 851 | PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB, |
6065f3ad AD |
852 | "hook_prefs_tab", "prefFilters"); |
853 | ||
8e17d663 AD |
854 | print "</div>"; #container |
855 | ||
856 | } | |
6aff7845 AD |
857 | |
858 | function newfilter() { | |
859 | ||
860 | print "<form name='filter_new_form' id='filter_new_form'>"; | |
861 | ||
328118d1 AD |
862 | print_hidden("op", "pref-filters"); |
863 | print_hidden("method", "add"); | |
864 | print_hidden("csrf_token", $_SESSION['csrf_token']); | |
6aff7845 | 865 | |
18ad09af AD |
866 | print "<div class=\"dlgSec\">".__("Caption")."</div>"; |
867 | ||
442bca27 | 868 | print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">"; |
18ad09af | 869 | |
6aff7845 AD |
870 | print "<div class=\"dlgSec\">".__("Match")."</div>"; |
871 | ||
872 | print "<div dojoType=\"dijit.Toolbar\">"; | |
873 | ||
874 | print "<div dojoType=\"dijit.form.DropDownButton\">". | |
875 | "<span>" . __('Select')."</span>"; | |
876 | print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">"; | |
877 | print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\" | |
878 | dojoType=\"dijit.MenuItem\">".__('All')."</div>"; | |
879 | print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\" | |
880 | dojoType=\"dijit.MenuItem\">".__('None')."</div>"; | |
881 | print "</div></div>"; | |
882 | ||
883 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">". | |
884 | __('Add')."</button> "; | |
885 | ||
886 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">". | |
887 | __('Delete')."</button> "; | |
888 | ||
889 | print "</div>"; | |
890 | ||
891 | print "<ul id='filterDlg_Matches'>"; | |
892 | # print "<li>No rules</li>"; | |
893 | print "</ul>"; | |
894 | ||
895 | print "</div>"; | |
896 | ||
897 | print "<div class=\"dlgSec\">".__("Apply actions")."</div>"; | |
898 | ||
899 | print "<div dojoType=\"dijit.Toolbar\">"; | |
900 | ||
901 | print "<div dojoType=\"dijit.form.DropDownButton\">". | |
902 | "<span>" . __('Select')."</span>"; | |
903 | print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">"; | |
904 | print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\" | |
905 | dojoType=\"dijit.MenuItem\">".__('All')."</div>"; | |
906 | print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\" | |
907 | dojoType=\"dijit.MenuItem\">".__('None')."</div>"; | |
908 | print "</div></div>"; | |
909 | ||
910 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">". | |
911 | __('Add')."</button> "; | |
912 | ||
913 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">". | |
914 | __('Delete')."</button> "; | |
915 | ||
916 | print "</div>"; | |
917 | ||
918 | print "<ul id='filterDlg_Actions'>"; | |
919 | # print "<li>No actions</li>"; | |
920 | print "</ul>"; | |
921 | ||
922 | /* print "<div class=\"dlgSec\">".__("Options")."</div>"; | |
923 | print "<div class=\"dlgSecCont\">"; */ | |
924 | ||
925 | print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\"> | |
926 | <label for=\"enabled\">".__('Enabled')."</label>"; | |
927 | ||
928 | print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\"> | |
929 | <label for=\"match_any_rule\">".__('Match any rule')."</label>"; | |
930 | ||
a3a896a1 AD |
931 | print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\"> |
932 | <label for=\"inverse\">".__('Inverse matching')."</label>"; | |
6aff7845 AD |
933 | |
934 | // print "</div>"; | |
935 | ||
936 | print "<div class=\"dlgButtons\">"; | |
937 | ||
4e02f582 AD |
938 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">". |
939 | __('Test')."</button> "; | |
6aff7845 AD |
940 | |
941 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">". | |
942 | __('Create')."</button> "; | |
943 | ||
944 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">". | |
945 | __('Cancel')."</button>"; | |
946 | ||
947 | print "</div>"; | |
948 | ||
949 | } | |
950 | ||
951 | function newrule() { | |
e6532439 | 952 | $rule = json_decode(clean($_REQUEST["rule"]), true); |
6aff7845 AD |
953 | |
954 | if ($rule) { | |
955 | $reg_exp = htmlspecialchars($rule["reg_exp"]); | |
956 | $filter_type = $rule["filter_type"]; | |
957 | $feed_id = $rule["feed_id"]; | |
a3a896a1 | 958 | $inverse_checked = isset($rule["inverse"]) ? "checked" : ""; |
6aff7845 AD |
959 | } else { |
960 | $reg_exp = ""; | |
961 | $filter_type = 1; | |
7b994e4b | 962 | $feed_id = ["0"]; |
a3a896a1 | 963 | $inverse_checked = ""; |
6aff7845 AD |
964 | } |
965 | ||
121ac048 | 966 | print "<form name='filter_new_rule_form' id='filter_new_rule_form' onsubmit='return false;'>"; |
6aff7845 | 967 | |
f594717d | 968 | $res = $this->pdo->query("SELECT id,description |
37f78940 | 969 | FROM ttrss_filter_types WHERE id != 5 ORDER BY description"); |
6aff7845 AD |
970 | |
971 | $filter_types = array(); | |
972 | ||
f594717d | 973 | while ($line = $res->fetch()) { |
6aff7845 AD |
974 | $filter_types[$line["id"]] = __($line["description"]); |
975 | } | |
976 | ||
977 | print "<div class=\"dlgSec\">".__("Match")."</div>"; | |
978 | ||
979 | print "<div class=\"dlgSecCont\">"; | |
980 | ||
981 | print "<input dojoType=\"dijit.form.ValidationTextBox\" | |
982 | required=\"true\" id=\"filterDlg_regExp\" | |
e794e434 | 983 | placeholder=\"match|keywords\" |
ff34bac2 | 984 | style=\"font-size : 16px; width : 20em;\" |
6aff7845 AD |
985 | name=\"reg_exp\" value=\"$reg_exp\"/>"; |
986 | ||
e794e434 AD |
987 | print "<div dojoType=\"dijit.Tooltip\" connectId=\"filterDlg_regExp\" position=\"below\"> |
988 | ".__("Regular expression, without outer delimiters (i.e. slashes)")." | |
989 | </div>"; | |
990 | ||
ec1f8a3d AD |
991 | print "<hr/>"; |
992 | print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\" | |
993 | name=\"inverse\" $inverse_checked/>"; | |
994 | print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>"; | |
995 | ||
6aff7845 AD |
996 | print "<hr/>" . __("on field") . " "; |
997 | print_select_hash("filter_type", $filter_type, $filter_types, | |
998 | 'dojoType="dijit.form.Select"'); | |
999 | ||
1000 | print "<hr/>"; | |
1001 | ||
1002 | print __("in") . " "; | |
1003 | ||
1004 | print "<span id='filterDlg_feeds'>"; | |
02f3992a AD |
1005 | print_feed_multi_select("feed_id", |
1006 | $feed_id, | |
be61f1c4 | 1007 | 'dojoType="dijit.form.MultiSelect"'); |
6aff7845 AD |
1008 | print "</span>"; |
1009 | ||
1010 | print "</div>"; | |
1011 | ||
1012 | print "<div class=\"dlgButtons\">"; | |
1013 | ||
e7a40490 AD |
1014 | print "<div style=\"float : left\"> |
1015 | <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a> | |
1016 | </div>"; | |
1017 | ||
1018 | ||
121ac048 | 1019 | print "<button dojoType=\"dijit.form.Button\" class=\"btn-primary \" type=\"submit\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">". |
6aff7845 AD |
1020 | ($rule ? __("Save rule") : __('Add rule'))."</button> "; |
1021 | ||
1022 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">". | |
1023 | __('Cancel')."</button>"; | |
1024 | ||
1025 | print "</div>"; | |
1026 | ||
1027 | print "</form>"; | |
1028 | } | |
1029 | ||
1030 | function newaction() { | |
e6532439 | 1031 | $action = json_decode(clean($_REQUEST["action"]), true); |
6aff7845 AD |
1032 | |
1033 | if ($action) { | |
f594717d | 1034 | $action_param = $action["action_param"]; |
6aff7845 AD |
1035 | $action_id = (int)$action["action_id"]; |
1036 | } else { | |
1037 | $action_param = ""; | |
1038 | $action_id = 0; | |
1039 | } | |
1040 | ||
121ac048 | 1041 | print "<form name='filter_new_action_form' id='filter_new_action_form' onsubmit='return false;'>"; |
6aff7845 AD |
1042 | |
1043 | print "<div class=\"dlgSec\">".__("Perform Action")."</div>"; | |
1044 | ||
1045 | print "<div class=\"dlgSecCont\">"; | |
1046 | ||
1047 | print "<select name=\"action_id\" dojoType=\"dijit.form.Select\" | |
1048 | onchange=\"filterDlgCheckAction(this)\">"; | |
1049 | ||
f594717d | 1050 | $res = $this->pdo->query("SELECT id,description FROM ttrss_filter_actions |
6aff7845 AD |
1051 | ORDER BY name"); |
1052 | ||
f594717d | 1053 | while ($line = $res->fetch()) { |
6aff7845 AD |
1054 | $is_selected = ($line["id"] == $action_id) ? "selected='1'" : ""; |
1055 | printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"])); | |
1056 | } | |
1057 | ||
1058 | print "</select>"; | |
1059 | ||
b8774453 | 1060 | $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6 || $action_id == 9) ? |
6aff7845 AD |
1061 | "" : "display : none"; |
1062 | ||
1063 | $param_hidden = ($action_id == 4 || $action_id == 6) ? | |
1064 | "" : "display : none"; | |
1065 | ||
1066 | $label_param_hidden = ($action_id == 7) ? "" : "display : none"; | |
b8774453 | 1067 | $plugin_param_hidden = ($action_id == 9) ? "" : "display : none"; |
6aff7845 AD |
1068 | |
1069 | print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">"; | |
b8774453 AD |
1070 | print " "; |
1071 | //print " " . __("with parameters:") . " "; | |
6aff7845 AD |
1072 | print "<input dojoType=\"dijit.form.TextBox\" |
1073 | id=\"filterDlg_actionParam\" style=\"$param_hidden\" | |
1074 | name=\"action_param\" value=\"$action_param\">"; | |
1075 | ||
a42c55f0 | 1076 | print_label_select("action_param_label", $action_param, |
6aff7845 AD |
1077 | "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\" |
1078 | dojoType=\"dijit.form.Select\""); | |
1079 | ||
b8774453 AD |
1080 | $filter_actions = PluginHost::getInstance()->get_filter_actions(); |
1081 | $filter_action_hash = array(); | |
1082 | ||
1083 | foreach ($filter_actions as $fclass => $factions) { | |
1084 | foreach ($factions as $faction) { | |
1085 | ||
1086 | $filter_action_hash[$fclass . ":" . $faction["action"]] = | |
1087 | $fclass . ": " . $faction["description"]; | |
1088 | } | |
b028da0a AD |
1089 | } |
1090 | ||
1091 | if (count($filter_action_hash) == 0) { | |
1092 | $filter_plugin_disabled = "disabled"; | |
b8774453 | 1093 | |
b028da0a AD |
1094 | $filter_action_hash["no-data"] = __("No actions available"); |
1095 | ||
1096 | } else { | |
1097 | $filter_plugin_disabled = ""; | |
b8774453 AD |
1098 | } |
1099 | ||
1100 | print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash, | |
b028da0a | 1101 | "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled", |
b8774453 AD |
1102 | "action_param_plugin"); |
1103 | ||
6aff7845 AD |
1104 | print "</span>"; |
1105 | ||
1106 | print " "; // tiny layout hack | |
1107 | ||
1108 | print "</div>"; | |
1109 | ||
1110 | print "<div class=\"dlgButtons\">"; | |
1111 | ||
121ac048 | 1112 | print "<button dojoType=\"dijit.form.Button\" class=\"btn-primary\" type=\"submit\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">". |
6aff7845 AD |
1113 | ($action ? __("Save action") : __('Add action'))."</button> "; |
1114 | ||
1115 | print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">". | |
1116 | __('Cancel')."</button>"; | |
1117 | ||
1118 | print "</div>"; | |
1119 | ||
1120 | print "</form>"; | |
1121 | } | |
1122 | ||
1123 | private function getFilterName($id) { | |
18ad09af | 1124 | |
f594717d | 1125 | $sth = $this->pdo->prepare( |
e17079dc | 1126 | "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions |
1a293f10 AD |
1127 | FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r |
1128 | ON (r.filter_id = f.id) | |
1129 | LEFT JOIN ttrss_filters2_actions AS a | |
f594717d AD |
1130 | ON (a.filter_id = f.id) WHERE f.id = ? GROUP BY f.title, f.match_any_rule"); |
1131 | $sth->execute([$id]); | |
18ad09af | 1132 | |
f594717d | 1133 | if ($row = $sth->fetch()) { |
18ad09af | 1134 | |
f594717d AD |
1135 | $title = $row["title"]; |
1136 | $num_rules = $row["num_rules"]; | |
1137 | $num_actions = $row["num_actions"]; | |
8ff3cbb3 | 1138 | $match_any_rule = $row["match_any_rule"]; |
18ad09af | 1139 | |
f594717d | 1140 | if (!$title) $title = __("[No caption]"); |
6aff7845 | 1141 | |
f594717d | 1142 | $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", (int) $num_rules), $title, $num_rules); |
e17079dc | 1143 | |
e794e434 | 1144 | $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_actions |
f594717d AD |
1145 | WHERE filter_id = ? ORDER BY id LIMIT 1"); |
1146 | $sth->execute([$id]); | |
bcd4d240 | 1147 | |
f594717d | 1148 | $actions = ""; |
da46d21e | 1149 | |
f594717d AD |
1150 | if ($line = $sth->fetch()) { |
1151 | $actions = $this->getActionName($line); | |
da46d21e | 1152 | |
f594717d AD |
1153 | $num_actions -= 1; |
1154 | } | |
1155 | ||
1156 | if ($match_any_rule) $title .= " (" . __("matches any rule") . ")"; | |
6aff7845 | 1157 | |
f594717d AD |
1158 | if ($num_actions > 0) |
1159 | $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", (int) $num_actions), $actions, $num_actions); | |
e17079dc | 1160 | |
f594717d AD |
1161 | return [$title, $actions]; |
1162 | } | |
6aff7845 | 1163 | |
f594717d | 1164 | return []; |
6aff7845 AD |
1165 | } |
1166 | ||
1167 | function join() { | |
e6532439 | 1168 | $ids = explode(",", clean($_REQUEST["ids"])); |
6aff7845 AD |
1169 | |
1170 | if (count($ids) > 1) { | |
1171 | $base_id = array_shift($ids); | |
f594717d | 1172 | $ids_qmarks = arr_qmarks($ids); |
6aff7845 | 1173 | |
f594717d | 1174 | $this->pdo->beginTransaction(); |
6aff7845 | 1175 | |
f594717d AD |
1176 | $sth = $this->pdo->prepare("UPDATE ttrss_filters2_rules |
1177 | SET filter_id = ? WHERE filter_id IN ($ids_qmarks)"); | |
1178 | $sth->execute(array_merge([$base_id], $ids)); | |
6aff7845 | 1179 | |
f594717d AD |
1180 | $sth = $this->pdo->prepare("UPDATE ttrss_filters2_actions |
1181 | SET filter_id = ? WHERE filter_id IN ($ids_qmarks)"); | |
1182 | $sth->execute(array_merge([$base_id], $ids)); | |
1183 | ||
1184 | $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)"); | |
1185 | $sth->execute($ids); | |
1186 | ||
1187 | $sth = $this->pdo->prepare("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = ?"); | |
1188 | $sth->execute([$base_id]); | |
1189 | ||
1190 | $this->pdo->commit(); | |
6aff7845 AD |
1191 | |
1192 | $this->optimizeFilter($base_id); | |
1193 | ||
1194 | } | |
1195 | } | |
1196 | ||
1197 | private function optimizeFilter($id) { | |
f594717d AD |
1198 | |
1199 | $this->pdo->beginTransaction(); | |
1200 | ||
1201 | $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_actions | |
1202 | WHERE filter_id = ?"); | |
1203 | $sth->execute([$id]); | |
6aff7845 AD |
1204 | |
1205 | $tmp = array(); | |
1206 | $dupe_ids = array(); | |
1207 | ||
f594717d | 1208 | while ($line = $sth->fetch()) { |
6aff7845 AD |
1209 | $id = $line["id"]; |
1210 | unset($line["id"]); | |
1211 | ||
1212 | if (array_search($line, $tmp) === false) { | |
1213 | array_push($tmp, $line); | |
1214 | } else { | |
1215 | array_push($dupe_ids, $id); | |
1216 | } | |
1217 | } | |
1218 | ||
1219 | if (count($dupe_ids) > 0) { | |
1220 | $ids_str = join(",", $dupe_ids); | |
f594717d AD |
1221 | |
1222 | $this->pdo->query("DELETE FROM ttrss_filters2_actions WHERE id IN ($ids_str)"); | |
6aff7845 AD |
1223 | } |
1224 | ||
f594717d AD |
1225 | $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_rules |
1226 | WHERE filter_id = ?"); | |
1227 | $sth->execute([$id]); | |
6aff7845 AD |
1228 | |
1229 | $tmp = array(); | |
1230 | $dupe_ids = array(); | |
1231 | ||
f594717d | 1232 | while ($line = $sth->fetch()) { |
6aff7845 AD |
1233 | $id = $line["id"]; |
1234 | unset($line["id"]); | |
1235 | ||
1236 | if (array_search($line, $tmp) === false) { | |
1237 | array_push($tmp, $line); | |
1238 | } else { | |
1239 | array_push($dupe_ids, $id); | |
1240 | } | |
1241 | } | |
1242 | ||
1243 | if (count($dupe_ids) > 0) { | |
1244 | $ids_str = join(",", $dupe_ids); | |
f594717d AD |
1245 | |
1246 | $this->pdo->query("DELETE FROM ttrss_filters2_rules WHERE id IN ($ids_str)"); | |
6aff7845 AD |
1247 | } |
1248 | ||
f594717d | 1249 | $this->pdo->commit(); |
6aff7845 | 1250 | } |
67bf38af | 1251 | } |