filters.php 37 KB

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