filters.php 37 KB

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