filters.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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(clean($_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\" 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\" 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. }
  392. }
  393. private function getRuleName($rule) {
  394. if (!$rule) $rule = json_decode(clean($_REQUEST["rule"]), true);
  395. $feeds = $rule["feed_id"];
  396. $feeds_fmt = [];
  397. if (!is_array($feeds)) $feeds = [$feeds];
  398. foreach ($feeds as $feed_id) {
  399. if (strpos($feed_id, "CAT:") === 0) {
  400. $feed_id = (int)substr($feed_id, 4);
  401. array_push($feeds_fmt, Feeds::getCategoryTitle($feed_id));
  402. } else {
  403. if ($feed_id)
  404. array_push($feeds_fmt, Feeds::getFeedTitle((int)$feed_id));
  405. else
  406. array_push($feeds_fmt, __("All feeds"));
  407. }
  408. }
  409. $feed = implode(", ", $feeds_fmt);
  410. $sth = $this->pdo->prepare("SELECT description FROM ttrss_filter_types
  411. WHERE id = ?");
  412. $sth->execute([(int)$rule["filter_type"]]);
  413. if ($row = $sth->fetch()) {
  414. $filter_type = $row["description"];
  415. } else {
  416. $filter_type = "?UNKNOWN?";
  417. }
  418. $inverse = isset($rule["inverse"]) ? "inverse" : "";
  419. return "<span class='filterRule $inverse'>" .
  420. T_sprintf("%s on %s in %s %s", htmlspecialchars($rule["reg_exp"]),
  421. $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "") . "</span>";
  422. }
  423. function printRuleName() {
  424. print $this->getRuleName(json_decode(clean($_REQUEST["rule"]), true));
  425. }
  426. private function getActionName($action) {
  427. $sth = $this->pdo->prepare("SELECT description FROM
  428. ttrss_filter_actions WHERE id = ?");
  429. $sth->execute([(int)$action["action_id"]]);
  430. $title = "";
  431. if ($row = $sth->fetch()) {
  432. $title = __($row["description"]);
  433. if ($action["action_id"] == 4 || $action["action_id"] == 6 ||
  434. $action["action_id"] == 7)
  435. $title .= ": " . $action["action_param"];
  436. if ($action["action_id"] == 9) {
  437. list ($pfclass, $pfaction) = explode(":", $action["action_param"]);
  438. $filter_actions = PluginHost::getInstance()->get_filter_actions();
  439. foreach ($filter_actions as $fclass => $factions) {
  440. foreach ($factions as $faction) {
  441. if ($pfaction == $faction["action"] && $pfclass == $fclass) {
  442. $title .= ": " . $fclass . ": " . $faction["description"];
  443. break;
  444. }
  445. }
  446. }
  447. }
  448. }
  449. return $title;
  450. }
  451. function printActionName() {
  452. print $this->getActionName(json_decode(clean($_REQUEST["action"]), true));
  453. }
  454. function editSave() {
  455. if (clean($_REQUEST["savemode"] && $_REQUEST["savemode"]) == "test") {
  456. return $this->testFilter();
  457. }
  458. $filter_id = clean($_REQUEST["id"]);
  459. $enabled = checkbox_to_sql_bool(clean($_REQUEST["enabled"]));
  460. $match_any_rule = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"]));
  461. $inverse = checkbox_to_sql_bool(clean($_REQUEST["inverse"]));
  462. $title = clean($_REQUEST["title"]);
  463. $this->pdo->beginTransaction();
  464. $sth = $this->pdo->prepare("UPDATE ttrss_filters2 SET enabled = ?,
  465. match_any_rule = ?,
  466. inverse = ?,
  467. title = ?
  468. WHERE id = ? AND owner_uid = ?");
  469. $sth->execute([$enabled, $match_any_rule, $inverse, $title, $filter_id, $_SESSION['uid']]);
  470. $this->saveRulesAndActions($filter_id);
  471. $this->pdo->commit();
  472. }
  473. function remove() {
  474. $ids = explode(",", clean($_REQUEST["ids"]));
  475. $ids_qmarks = arr_qmarks($ids);
  476. $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)
  477. AND owner_uid = ?");
  478. $sth->execute(array_merge($ids, [$_SESSION['uid']]));
  479. }
  480. private function saveRulesAndActions($filter_id)
  481. {
  482. $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2_rules WHERE filter_id = ?");
  483. $sth->execute([$filter_id]);
  484. $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2_actions WHERE filter_id = ?");
  485. $sth->execute([$filter_id]);
  486. if (!is_array(clean($_REQUEST["rule"]))) $_REQUEST["rule"] = [];
  487. if (!is_array(clean($_REQUEST["action"]))) $_REQUEST["action"] = [];
  488. if ($filter_id) {
  489. /* create rules */
  490. $rules = array();
  491. $actions = array();
  492. foreach (clean($_REQUEST["rule"]) as $rule) {
  493. $rule = json_decode($rule, true);
  494. unset($rule["id"]);
  495. if (array_search($rule, $rules) === false) {
  496. array_push($rules, $rule);
  497. }
  498. }
  499. foreach (clean($_REQUEST["action"]) as $action) {
  500. $action = json_decode($action, true);
  501. unset($action["id"]);
  502. if (array_search($action, $actions) === false) {
  503. array_push($actions, $action);
  504. }
  505. }
  506. $rsth = $this->pdo->prepare("INSERT INTO ttrss_filters2_rules
  507. (filter_id, reg_exp,filter_type,feed_id,cat_id,match_on,inverse) VALUES
  508. (?, ?, ?, NULL, NULL, ?, ?)");
  509. foreach ($rules as $rule) {
  510. if ($rule) {
  511. $reg_exp = trim($rule["reg_exp"]);
  512. $inverse = isset($rule["inverse"]) ? 1 : 0;
  513. $filter_type = (int)trim($rule["filter_type"]);
  514. $match_on = json_encode($rule["feed_id"]);
  515. $rsth->execute([$filter_id, $reg_exp, $filter_type, $match_on, $inverse]);
  516. }
  517. }
  518. $asth = $this->pdo->prepare("INSERT INTO ttrss_filters2_actions
  519. (filter_id, action_id, action_param) VALUES
  520. (?, ?, ?)");
  521. foreach ($actions as $action) {
  522. if ($action) {
  523. $action_id = (int)$action["action_id"];
  524. $action_param = $action["action_param"];
  525. $action_param_label = $action["action_param_label"];
  526. if ($action_id == 7) {
  527. $action_param = $action_param_label;
  528. }
  529. if ($action_id == 6) {
  530. $action_param = (int)str_replace("+", "", $action_param);
  531. }
  532. $asth->execute([$filter_id, $action_id, $action_param]);
  533. }
  534. }
  535. }
  536. }
  537. function add() {
  538. if (clean($_REQUEST["savemode"] && $_REQUEST["savemode"]) == "test") {
  539. return $this->testFilter();
  540. }
  541. $enabled = checkbox_to_sql_bool(clean($_REQUEST["enabled"]));
  542. $match_any_rule = checkbox_to_sql_bool(clean($_REQUEST["match_any_rule"]));
  543. $title = clean($_REQUEST["title"]);
  544. $inverse = checkbox_to_sql_bool(clean($_REQUEST["inverse"]));
  545. $this->pdo->beginTransaction();
  546. /* create base filter */
  547. $sth = $this->pdo->prepare("INSERT INTO ttrss_filters2
  548. (owner_uid, match_any_rule, enabled, title, inverse) VALUES
  549. (?, ?, ?, ?, ?)");
  550. $sth->execute([$_SESSION['uid'], $match_any_rule, $enabled, $title, $inverse]);
  551. $sth = $this->pdo->prepare("SELECT MAX(id) AS id FROM ttrss_filters2
  552. WHERE owner_uid = ?");
  553. $sth->execute([$_SESSION['uid']]);
  554. if ($row = $sth->fetch()) {
  555. $filter_id = $row['id'];
  556. $this->saveRulesAndActions($filter_id);
  557. }
  558. $this->pdo->commit();
  559. }
  560. function index() {
  561. $filter_search = clean($_REQUEST["search"]);
  562. if (array_key_exists("search", $_REQUEST)) {
  563. $_SESSION["prefs_filter_search"] = $filter_search;
  564. } else {
  565. $filter_search = $_SESSION["prefs_filter_search"];
  566. }
  567. print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
  568. print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
  569. print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
  570. if (array_key_exists("search", $_REQUEST)) {
  571. $_SESSION["prefs_filter_search"] = $filter_search;
  572. } else {
  573. $filter_search = $_SESSION["prefs_filter_search"];
  574. }
  575. print "<div style='float : right; padding-right : 4px;'>
  576. <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
  577. value=\"$filter_search\">
  578. <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
  579. __('Search')."</button>
  580. </div>";
  581. print "<div dojoType=\"dijit.form.DropDownButton\">".
  582. "<span>" . __('Select')."</span>";
  583. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  584. print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
  585. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  586. print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
  587. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  588. print "</div></div>";
  589. print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
  590. __('Create filter')."</button> ";
  591. print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
  592. __('Combine')."</button> ";
  593. print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
  594. __('Edit')."</button> ";
  595. print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
  596. __('Reset sort order')."</button> ";
  597. print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
  598. __('Remove')."</button> ";
  599. print "</div>"; # toolbar
  600. print "</div>"; # toolbar-frame
  601. print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
  602. print "<div id=\"filterlistLoading\">
  603. <img src='images/indicator_tiny.gif'>".
  604. __("Loading, please wait...")."</div>";
  605. print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
  606. url=\"backend.php?op=pref-filters&method=getfiltertree\">
  607. </div>
  608. <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
  609. query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
  610. childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
  611. </div>
  612. <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
  613. dndController=\"dijit.tree.dndSource\"
  614. betweenThreshold=\"5\"
  615. model=\"filterModel\" openOnClick=\"true\">
  616. <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
  617. Element.hide(\"filterlistLoading\");
  618. </script>
  619. <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
  620. var id = String(item.id);
  621. var bare_id = id.substr(id.indexOf(':')+1);
  622. if (id.match('FILTER:')) {
  623. editFilter(bare_id);
  624. }
  625. </script>
  626. </div>";
  627. print "</div>"; #pane
  628. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
  629. "hook_prefs_tab", "prefFilters");
  630. print "</div>"; #container
  631. }
  632. function newfilter() {
  633. print "<form name='filter_new_form' id='filter_new_form'>";
  634. print_hidden("op", "pref-filters");
  635. print_hidden("method", "add");
  636. print_hidden("csrf_token", $_SESSION['csrf_token']);
  637. print "<div class=\"dlgSec\">".__("Caption")."</div>";
  638. print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
  639. print "<div class=\"dlgSec\">".__("Match")."</div>";
  640. print "<div dojoType=\"dijit.Toolbar\">";
  641. print "<div dojoType=\"dijit.form.DropDownButton\">".
  642. "<span>" . __('Select')."</span>";
  643. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  644. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
  645. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  646. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
  647. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  648. print "</div></div>";
  649. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
  650. __('Add')."</button> ";
  651. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
  652. __('Delete')."</button> ";
  653. print "</div>";
  654. print "<ul id='filterDlg_Matches'>";
  655. # print "<li>No rules</li>";
  656. print "</ul>";
  657. print "</div>";
  658. print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
  659. print "<div dojoType=\"dijit.Toolbar\">";
  660. print "<div dojoType=\"dijit.form.DropDownButton\">".
  661. "<span>" . __('Select')."</span>";
  662. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  663. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
  664. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  665. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
  666. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  667. print "</div></div>";
  668. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
  669. __('Add')."</button> ";
  670. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
  671. __('Delete')."</button> ";
  672. print "</div>";
  673. print "<ul id='filterDlg_Actions'>";
  674. # print "<li>No actions</li>";
  675. print "</ul>";
  676. /* print "<div class=\"dlgSec\">".__("Options")."</div>";
  677. print "<div class=\"dlgSecCont\">"; */
  678. print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
  679. <label for=\"enabled\">".__('Enabled')."</label>";
  680. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
  681. <label for=\"match_any_rule\">".__('Match any rule')."</label>";
  682. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
  683. <label for=\"inverse\">".__('Inverse matching')."</label>";
  684. // print "</div>";
  685. print "<div class=\"dlgButtons\">";
  686. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
  687. __('Test')."</button> ";
  688. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
  689. __('Create')."</button> ";
  690. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
  691. __('Cancel')."</button>";
  692. print "</div>";
  693. }
  694. function newrule() {
  695. $rule = json_decode(clean($_REQUEST["rule"]), true);
  696. if ($rule) {
  697. $reg_exp = htmlspecialchars($rule["reg_exp"]);
  698. $filter_type = $rule["filter_type"];
  699. $feed_id = $rule["feed_id"];
  700. $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
  701. } else {
  702. $reg_exp = "";
  703. $filter_type = 1;
  704. $feed_id = ["0"];
  705. $inverse_checked = "";
  706. }
  707. print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
  708. $res = $this->pdo->query("SELECT id,description
  709. FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
  710. $filter_types = array();
  711. while ($line = $res->fetch()) {
  712. $filter_types[$line["id"]] = __($line["description"]);
  713. }
  714. print "<div class=\"dlgSec\">".__("Match")."</div>";
  715. print "<div class=\"dlgSecCont\">";
  716. print "<input dojoType=\"dijit.form.ValidationTextBox\"
  717. required=\"true\" id=\"filterDlg_regExp\"
  718. style=\"font-size : 16px; width : 20em;\"
  719. name=\"reg_exp\" value=\"$reg_exp\"/>";
  720. print "<hr/>";
  721. print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
  722. name=\"inverse\" $inverse_checked/>";
  723. print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
  724. print "<hr/>" . __("on field") . " ";
  725. print_select_hash("filter_type", $filter_type, $filter_types,
  726. 'dojoType="dijit.form.Select"');
  727. print "<hr/>";
  728. print __("in") . " ";
  729. print "<span id='filterDlg_feeds'>";
  730. print_feed_multi_select("feed_id",
  731. $feed_id,
  732. 'dojoType="dijit.form.MultiSelect"');
  733. print "</span>";
  734. print "</div>";
  735. print "<div class=\"dlgButtons\">";
  736. print "<div style=\"float : left\">
  737. <a class=\"visibleLink\" target=\"_blank\" href=\"http://tt-rss.org/wiki/ContentFilters\">".__("Wiki: Filters")."</a>
  738. </div>";
  739. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
  740. ($rule ? __("Save rule") : __('Add rule'))."</button> ";
  741. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
  742. __('Cancel')."</button>";
  743. print "</div>";
  744. print "</form>";
  745. }
  746. function newaction() {
  747. $action = json_decode(clean($_REQUEST["action"]), true);
  748. if ($action) {
  749. $action_param = $action["action_param"];
  750. $action_id = (int)$action["action_id"];
  751. } else {
  752. $action_param = "";
  753. $action_id = 0;
  754. }
  755. print "<form name='filter_new_action_form' id='filter_new_action_form'>";
  756. print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
  757. print "<div class=\"dlgSecCont\">";
  758. print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
  759. onchange=\"filterDlgCheckAction(this)\">";
  760. $res = $this->pdo->query("SELECT id,description FROM ttrss_filter_actions
  761. ORDER BY name");
  762. while ($line = $res->fetch()) {
  763. $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
  764. printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
  765. }
  766. print "</select>";
  767. $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6 || $action_id == 9) ?
  768. "" : "display : none";
  769. $param_hidden = ($action_id == 4 || $action_id == 6) ?
  770. "" : "display : none";
  771. $label_param_hidden = ($action_id == 7) ? "" : "display : none";
  772. $plugin_param_hidden = ($action_id == 9) ? "" : "display : none";
  773. print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
  774. print " ";
  775. //print " " . __("with parameters:") . " ";
  776. print "<input dojoType=\"dijit.form.TextBox\"
  777. id=\"filterDlg_actionParam\" style=\"$param_hidden\"
  778. name=\"action_param\" value=\"$action_param\">";
  779. print_label_select("action_param_label", $action_param,
  780. "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
  781. dojoType=\"dijit.form.Select\"");
  782. $filter_actions = PluginHost::getInstance()->get_filter_actions();
  783. $filter_action_hash = array();
  784. foreach ($filter_actions as $fclass => $factions) {
  785. foreach ($factions as $faction) {
  786. $filter_action_hash[$fclass . ":" . $faction["action"]] =
  787. $fclass . ": " . $faction["description"];
  788. }
  789. }
  790. if (count($filter_action_hash) == 0) {
  791. $filter_plugin_disabled = "disabled";
  792. $filter_action_hash["no-data"] = __("No actions available");
  793. } else {
  794. $filter_plugin_disabled = "";
  795. }
  796. print_select_hash("filterDlg_actionParamPlugin", $action_param, $filter_action_hash,
  797. "style=\"$plugin_param_hidden\" dojoType=\"dijit.form.Select\" $filter_plugin_disabled",
  798. "action_param_plugin");
  799. print "</span>";
  800. print "&nbsp;"; // tiny layout hack
  801. print "</div>";
  802. print "<div class=\"dlgButtons\">";
  803. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
  804. ($action ? __("Save action") : __('Add action'))."</button> ";
  805. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
  806. __('Cancel')."</button>";
  807. print "</div>";
  808. print "</form>";
  809. }
  810. private function getFilterName($id) {
  811. $sth = $this->pdo->prepare(
  812. "SELECT title,match_any_rule,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
  813. FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
  814. ON (r.filter_id = f.id)
  815. LEFT JOIN ttrss_filters2_actions AS a
  816. ON (a.filter_id = f.id) WHERE f.id = ? GROUP BY f.title, f.match_any_rule");
  817. $sth->execute([$id]);
  818. if ($row = $sth->fetch()) {
  819. $title = $row["title"];
  820. $num_rules = $row["num_rules"];
  821. $num_actions = $row["num_actions"];
  822. $match_any_rule = $row["match_any_rule"];
  823. if (!$title) $title = __("[No caption]");
  824. $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", (int) $num_rules), $title, $num_rules);
  825. $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_actions
  826. WHERE filter_id = ? ORDER BY id LIMIT 1");
  827. $sth->execute([$id]);
  828. $actions = "";
  829. if ($line = $sth->fetch()) {
  830. $actions = $this->getActionName($line);
  831. $num_actions -= 1;
  832. }
  833. if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
  834. if ($num_actions > 0)
  835. $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", (int) $num_actions), $actions, $num_actions);
  836. return [$title, $actions];
  837. }
  838. return [];
  839. }
  840. function join() {
  841. $ids = explode(",", clean($_REQUEST["ids"]));
  842. if (count($ids) > 1) {
  843. $base_id = array_shift($ids);
  844. $ids_qmarks = arr_qmarks($ids);
  845. $this->pdo->beginTransaction();
  846. $sth = $this->pdo->prepare("UPDATE ttrss_filters2_rules
  847. SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
  848. $sth->execute(array_merge([$base_id], $ids));
  849. $sth = $this->pdo->prepare("UPDATE ttrss_filters2_actions
  850. SET filter_id = ? WHERE filter_id IN ($ids_qmarks)");
  851. $sth->execute(array_merge([$base_id], $ids));
  852. $sth = $this->pdo->prepare("DELETE FROM ttrss_filters2 WHERE id IN ($ids_qmarks)");
  853. $sth->execute($ids);
  854. $sth = $this->pdo->prepare("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = ?");
  855. $sth->execute([$base_id]);
  856. $this->pdo->commit();
  857. $this->optimizeFilter($base_id);
  858. }
  859. }
  860. private function optimizeFilter($id) {
  861. $this->pdo->beginTransaction();
  862. $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_actions
  863. WHERE filter_id = ?");
  864. $sth->execute([$id]);
  865. $tmp = array();
  866. $dupe_ids = array();
  867. while ($line = $sth->fetch()) {
  868. $id = $line["id"];
  869. unset($line["id"]);
  870. if (array_search($line, $tmp) === false) {
  871. array_push($tmp, $line);
  872. } else {
  873. array_push($dupe_ids, $id);
  874. }
  875. }
  876. if (count($dupe_ids) > 0) {
  877. $ids_str = join(",", $dupe_ids);
  878. $this->pdo->query("DELETE FROM ttrss_filters2_actions WHERE id IN ($ids_str)");
  879. }
  880. $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2_rules
  881. WHERE filter_id = ?");
  882. $sth->execute([$id]);
  883. $tmp = array();
  884. $dupe_ids = array();
  885. while ($line = $sth->fetch()) {
  886. $id = $line["id"];
  887. unset($line["id"]);
  888. if (array_search($line, $tmp) === false) {
  889. array_push($tmp, $line);
  890. } else {
  891. array_push($dupe_ids, $id);
  892. }
  893. }
  894. if (count($dupe_ids) > 0) {
  895. $ids_str = join(",", $dupe_ids);
  896. $this->pdo->query("DELETE FROM ttrss_filters2_rules WHERE id IN ($ids_str)");
  897. }
  898. $this->pdo->commit();
  899. }
  900. }