filters.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234
  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. $this->dbh->query("UPDATE ttrss_filters2
  10. SET order_id = 0 WHERE owner_uid = " . $_SESSION["uid"]);
  11. return;
  12. }
  13. function savefilterorder() {
  14. $data = json_decode($_POST['payload'], true);
  15. #file_put_contents("/tmp/saveorder.json", $_POST['payload']);
  16. #$data = json_decode(file_get_contents("/tmp/saveorder.json"), true);
  17. if (!is_array($data['items']))
  18. $data['items'] = json_decode($data['items'], true);
  19. $index = 0;
  20. if (is_array($data) && is_array($data['items'])) {
  21. foreach ($data['items'][0]['items'] as $item) {
  22. $filter_id = (int) str_replace("FILTER:", "", $item['_reference']);
  23. if ($filter_id > 0) {
  24. $this->dbh->query("UPDATE ttrss_filters2 SET
  25. order_id = $index WHERE id = '$filter_id' AND
  26. owner_uid = " .$_SESSION["uid"]);
  27. ++$index;
  28. }
  29. }
  30. }
  31. return;
  32. }
  33. function testFilterDo() {
  34. $offset = (int) db_escape_string($_REQUEST["offset"]);
  35. $limit = (int) db_escape_string($_REQUEST["limit"]);
  36. $filter = array();
  37. $filter["enabled"] = true;
  38. $filter["match_any_rule"] = sql_bool_to_bool(
  39. checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["match_any_rule"])));
  40. $filter["inverse"] = sql_bool_to_bool(
  41. checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["inverse"])));
  42. $filter["rules"] = array();
  43. $filter["actions"] = array("dummy-action");
  44. $result = $this->dbh->query("SELECT id,name FROM ttrss_filter_types");
  45. $filter_types = array();
  46. while ($line = $this->dbh->fetch_assoc($result)) {
  47. $filter_types[$line["id"]] = $line["name"];
  48. }
  49. $scope_qparts = array();
  50. $rctr = 0;
  51. foreach ($_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 = " . $cat_id);
  61. } else if ($feed_id > 0) {
  62. array_push($scope_inner_qparts, "feed_id = " . $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. $result = db_query("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 = " . $_SESSION["uid"] . "
  98. ORDER BY date_entered DESC LIMIT $limit OFFSET $offset");
  99. while ($line = db_fetch_assoc($result)) {
  100. $rc = RSSUtils::get_article_filters(array($filter), $line['title'], $line['content'], $line['link'],
  101. $line['author'], explode(",", $line['tag_cache']));
  102. if (count($rc) > 0) {
  103. $line["content_preview"] = truncate_string(strip_tags($line["content"]), 200, '&hellip;');
  104. foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_QUERY_HEADLINES) as $p) {
  105. $line = $p->hook_query_headlines($line, 100);
  106. }
  107. $content_preview = $line["content_preview"];
  108. $tmp = "<tr style='margin-top : 5px'>";
  109. #$tmp .= "<td width='5%' align='center'><input dojoType=\"dijit.form.CheckBox\"
  110. # checked=\"1\" disabled=\"1\" type=\"checkbox\"></td>";
  111. $id = $line['id'];
  112. $tmp .= "<td width='5%' align='center'><img style='cursor : pointer' title='".__("Preview article")."'
  113. src='images/information.png' onclick='openArticlePopup($id)'></td><td>";
  114. /*foreach ($filter['rules'] as $rule) {
  115. $reg_exp = str_replace('/', '\/', $rule["reg_exp"]);
  116. $line["title"] = preg_replace("/($reg_exp)/i",
  117. "<span class=\"highlight\">$1</span>", $line["title"]);
  118. $content_preview = preg_replace("/($reg_exp)/i",
  119. "<span class=\"highlight\">$1</span>", $content_preview);
  120. }*/
  121. $tmp .= "<strong>" . $line["title"] . "</strong><br/>";
  122. $tmp .= $line['feed_title'] . ", " . mb_substr($line["date_entered"], 0, 16);
  123. $tmp .= "<div class='insensitive'>" . $content_preview . "</div>";
  124. $tmp .= "</td></tr>";
  125. array_push($rv, $tmp);
  126. /*array_push($rv, array("title" => $line["title"],
  127. "content" => $content_preview,
  128. "date" => $line["date_entered"],
  129. "feed" => $line["feed_title"])); */
  130. }
  131. }
  132. //$offset += $limit;
  133. //}
  134. /*if ($found == 0) {
  135. print "<tr><td align='center'>" .
  136. __("No recent articles matching this filter have been found.");
  137. }*/
  138. print json_encode($rv);
  139. }
  140. function testFilter() {
  141. if (isset($_REQUEST["offset"])) return $this->testFilterDo();
  142. //print __("Articles matching this filter:");
  143. print "<div><img id='prefFilterLoadingIndicator' src='images/indicator_tiny.gif'>&nbsp;<span id='prefFilterProgressMsg'>Looking for articles...</span></div>";
  144. print "<br/><div class=\"filterTestHolder\">";
  145. print "<table width=\"100%\" cellspacing=\"0\" id=\"prefFilterTestResultList\">";
  146. print "</table></div>";
  147. print "<div style='text-align : center'>";
  148. print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
  149. __('Close this window')."</button>";
  150. print "</div>";
  151. }
  152. private function getfilterrules_concise($filter_id) {
  153. $result = $this->dbh->query("SELECT reg_exp,
  154. inverse,
  155. match_on,
  156. feed_id,
  157. cat_id,
  158. cat_filter,
  159. ttrss_filter_types.description AS field
  160. FROM
  161. ttrss_filters2_rules, ttrss_filter_types
  162. WHERE
  163. filter_id = '$filter_id' AND filter_type = ttrss_filter_types.id
  164. ORDER BY reg_exp");
  165. $rv = "";
  166. while ($line = $this->dbh->fetch_assoc($result)) {
  167. if ($line["match_on"]) {
  168. $feeds = json_decode($line["match_on"], true);
  169. $feeds_fmt = [];
  170. foreach ($feeds as $feed_id) {
  171. if (strpos($feed_id, "CAT:") === 0) {
  172. $feed_id = (int)substr($feed_id, 4);
  173. array_push($feeds_fmt, Feeds::getCategoryTitle($feed_id));
  174. } else {
  175. if ($feed_id)
  176. array_push($feeds_fmt, Feeds::getFeedTitle((int)$feed_id));
  177. else
  178. array_push($feeds_fmt, __("All feeds"));
  179. }
  180. }
  181. $where = implode(", ", $feeds_fmt);
  182. } else {
  183. $where = sql_bool_to_bool($line["cat_filter"]) ?
  184. Feeds::getCategoryTitle($line["cat_id"]) :
  185. ($line["feed_id"] ?
  186. Feeds::getFeedTitle($line["feed_id"]) : __("All feeds"));
  187. }
  188. # $where = $line["cat_id"] . "/" . $line["feed_id"];
  189. $inverse = sql_bool_to_bool($line["inverse"]) ? "inverse" : "";
  190. $rv .= "<span class='$inverse'>" . T_sprintf("%s on %s in %s %s",
  191. htmlspecialchars($line["reg_exp"]),
  192. $line["field"],
  193. $where,
  194. sql_bool_to_bool($line["inverse"]) ? __("(inverse)") : "") . "</span>";
  195. }
  196. return $rv;
  197. }
  198. function getfiltertree() {
  199. $root = array();
  200. $root['id'] = 'root';
  201. $root['name'] = __('Filters');
  202. $root['items'] = array();
  203. $filter_search = $_SESSION["prefs_filter_search"];
  204. $result = $this->dbh->query("SELECT *,
  205. (SELECT action_param FROM ttrss_filters2_actions
  206. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
  207. (SELECT action_id FROM ttrss_filters2_actions
  208. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
  209. (SELECT description FROM ttrss_filter_actions
  210. WHERE id = (SELECT action_id FROM ttrss_filters2_actions
  211. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
  212. (SELECT reg_exp FROM ttrss_filters2_rules
  213. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
  214. FROM ttrss_filters2 WHERE
  215. owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
  216. $folder = array();
  217. $folder['items'] = array();
  218. while ($line = $this->dbh->fetch_assoc($result)) {
  219. $name = $this->getFilterName($line["id"]);
  220. $match_ok = false;
  221. if ($filter_search) {
  222. $rules_result = $this->dbh->query(
  223. "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
  224. while ($rule_line = $this->dbh->fetch_assoc($rules_result)) {
  225. if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
  226. $match_ok = true;
  227. break;
  228. }
  229. }
  230. }
  231. if ($line['action_id'] == 7) {
  232. $label_result = $this->dbh->query("SELECT fg_color, bg_color
  233. FROM ttrss_labels2 WHERE caption = '".$this->dbh->escape_string($line['action_param'])."' AND
  234. owner_uid = " . $_SESSION["uid"]);
  235. if ($this->dbh->num_rows($label_result) > 0) {
  236. $fg_color = $this->dbh->fetch_result($label_result, 0, "fg_color");
  237. $bg_color = $this->dbh->fetch_result($label_result, 0, "bg_color");
  238. $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>&alpha;</span>" . $name[1];
  239. }
  240. }
  241. $filter = array();
  242. $filter['id'] = 'FILTER:' . $line['id'];
  243. $filter['bare_id'] = $line['id'];
  244. $filter['name'] = $name[0];
  245. $filter['param'] = $name[1];
  246. $filter['checkbox'] = false;
  247. $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
  248. $filter['rules'] = $this->getfilterrules_concise($line['id']);
  249. if (!$filter_search || $match_ok) {
  250. array_push($folder['items'], $filter);
  251. }
  252. }
  253. /* if (count($folder['items']) > 0) {
  254. array_push($root['items'], $folder);
  255. } */
  256. $root['items'] = $folder['items'];
  257. $fl = array();
  258. $fl['identifier'] = 'id';
  259. $fl['label'] = 'name';
  260. $fl['items'] = array($root);
  261. print json_encode($fl);
  262. return;
  263. }
  264. function edit() {
  265. $filter_id = $this->dbh->escape_string($_REQUEST["id"]);
  266. $result = $this->dbh->query(
  267. "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
  268. $enabled = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "enabled"));
  269. $match_any_rule = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "match_any_rule"));
  270. $inverse = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "inverse"));
  271. $title = htmlspecialchars($this->dbh->fetch_result($result, 0, "title"));
  272. print "<form id=\"filter_edit_form\" onsubmit='return false'>";
  273. print_hidden("op", "pref-filters");
  274. print_hidden("id", "$filter_id");
  275. print_hidden("method", "editSave");
  276. print_hidden("csrf_token", $_SESSION['csrf_token']);
  277. print "<div class=\"dlgSec\">".__("Caption")."</div>";
  278. print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
  279. print "</div>";
  280. print "<div class=\"dlgSec\">".__("Match")."</div>";
  281. print "<div dojoType=\"dijit.Toolbar\">";
  282. print "<div dojoType=\"dijit.form.DropDownButton\">".
  283. "<span>" . __('Select')."</span>";
  284. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  285. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
  286. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  287. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
  288. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  289. print "</div></div>";
  290. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
  291. __('Add')."</button> ";
  292. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
  293. __('Delete')."</button> ";
  294. print "</div>";
  295. print "<ul id='filterDlg_Matches'>";
  296. $rules_result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
  297. WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
  298. while ($line = $this->dbh->fetch_assoc($rules_result)) {
  299. if ($line["match_on"]) {
  300. $line["feed_id"] = json_decode($line["match_on"], true);
  301. } else {
  302. if (sql_bool_to_bool($line["cat_filter"])) {
  303. $feed_id = "CAT:" . (int)$line["cat_id"];
  304. } else {
  305. $feed_id = (int)$line["feed_id"];
  306. }
  307. $line["feed_id"] = ["" . $feed_id]; // set item type to string for in_array()
  308. }
  309. unset($line["cat_filter"]);
  310. unset($line["cat_id"]);
  311. unset($line["filter_id"]);
  312. unset($line["id"]);
  313. if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
  314. unset($line["match_on"]);
  315. $data = htmlspecialchars(json_encode($line));
  316. print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
  317. "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
  318. "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
  319. }
  320. print "</ul>";
  321. print "</div>";
  322. print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
  323. print "<div dojoType=\"dijit.Toolbar\">";
  324. print "<div dojoType=\"dijit.form.DropDownButton\">".
  325. "<span>" . __('Select')."</span>";
  326. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  327. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
  328. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  329. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
  330. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  331. print "</div></div>";
  332. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
  333. __('Add')."</button> ";
  334. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
  335. __('Delete')."</button> ";
  336. print "</div>";
  337. print "<ul id='filterDlg_Actions'>";
  338. $actions_result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
  339. WHERE filter_id = '$filter_id' ORDER BY id");
  340. while ($line = $this->dbh->fetch_assoc($actions_result)) {
  341. $line["action_param_label"] = $line["action_param"];
  342. unset($line["filter_id"]);
  343. unset($line["id"]);
  344. $data = htmlspecialchars(json_encode($line));
  345. print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
  346. "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
  347. "<input type='hidden' name='action[]' value=\"$data\"/></li>";
  348. }
  349. print "</ul>";
  350. print "</div>";
  351. if ($enabled) {
  352. $checked = "checked=\"1\"";
  353. } else {
  354. $checked = "";
  355. }
  356. print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
  357. <label for=\"enabled\">".__('Enabled')."</label>";
  358. if ($match_any_rule) {
  359. $checked = "checked=\"1\"";
  360. } else {
  361. $checked = "";
  362. }
  363. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
  364. <label for=\"match_any_rule\">".__('Match any rule')."</label>";
  365. if ($inverse) {
  366. $checked = "checked=\"1\"";
  367. } else {
  368. $checked = "";
  369. }
  370. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
  371. <label for=\"inverse\">".__('Inverse matching')."</label>";
  372. print "<p/>";
  373. print "<div class=\"dlgButtons\">";
  374. print "<div style=\"float : left\">";
  375. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
  376. __('Remove')."</button>";
  377. print "</div>";
  378. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
  379. __('Test')."</button> ";
  380. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
  381. __('Save')."</button> ";
  382. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
  383. __('Cancel')."</button>";
  384. print "</div>";
  385. }
  386. private function getRuleName($rule) {
  387. if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
  388. $feeds = $rule["feed_id"];
  389. $feeds_fmt = [];
  390. if (!is_array($feeds)) $feeds = [$feeds];
  391. foreach ($feeds as $feed_id) {
  392. if (strpos($feed_id, "CAT:") === 0) {
  393. $feed_id = (int)substr($feed_id, 4);
  394. array_push($feeds_fmt, Feeds::getCategoryTitle($feed_id));
  395. } else {
  396. if ($feed_id)
  397. array_push($feeds_fmt, Feeds::getFeedTitle((int)$feed_id));
  398. else
  399. array_push($feeds_fmt, __("All feeds"));
  400. }
  401. }
  402. $feed = implode(", ", $feeds_fmt);
  403. $result = $this->dbh->query("SELECT description FROM ttrss_filter_types
  404. WHERE id = ".(int)$rule["filter_type"]);
  405. $filter_type = $this->dbh->fetch_result($result, 0, "description");
  406. $inverse = isset($rule["inverse"]) ? "inverse" : "";
  407. return "<span class='filterRule $inverse'>" .
  408. T_sprintf("%s on %s in %s %s", htmlspecialchars($rule["reg_exp"]),
  409. $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "") . "</span>";
  410. }
  411. function printRuleName() {
  412. print $this->getRuleName(json_decode($_REQUEST["rule"], true));
  413. }
  414. private function getActionName($action) {
  415. $result = $this->dbh->query("SELECT description FROM
  416. ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
  417. $title = __($this->dbh->fetch_result($result, 0, "description"));
  418. if ($action["action_id"] == 4 || $action["action_id"] == 6 ||
  419. $action["action_id"] == 7)
  420. $title .= ": " . $action["action_param"];
  421. if ($action["action_id"] == 9) {
  422. list ($pfclass, $pfaction) = explode(":", $action["action_param"]);
  423. $filter_actions = PluginHost::getInstance()->get_filter_actions();
  424. foreach ($filter_actions as $fclass => $factions) {
  425. foreach ($factions as $faction) {
  426. if ($pfaction == $faction["action"] && $pfclass == $fclass) {
  427. $title .= ": " . $fclass . ": " . $faction["description"];
  428. break;
  429. }
  430. }
  431. }
  432. }
  433. return $title;
  434. }
  435. function printActionName() {
  436. print $this->getActionName(json_decode($_REQUEST["action"], true));
  437. }
  438. function editSave() {
  439. if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
  440. return $this->testFilter();
  441. }
  442. # print_r($_REQUEST);
  443. $filter_id = $this->dbh->escape_string($_REQUEST["id"]);
  444. $enabled = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["enabled"]));
  445. $match_any_rule = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["match_any_rule"]));
  446. $inverse = checkbox_to_sql_bool($this->dbh->escape_string($_REQUEST["inverse"]));
  447. $title = $this->dbh->escape_string($_REQUEST["title"]);
  448. $this->dbh->query("UPDATE ttrss_filters2 SET enabled = $enabled,
  449. match_any_rule = $match_any_rule,
  450. inverse = $inverse,
  451. title = '$title'
  452. WHERE id = '$filter_id'
  453. AND owner_uid = ". $_SESSION["uid"]);
  454. $this->saveRulesAndActions($filter_id);
  455. }
  456. function remove() {
  457. $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
  458. foreach ($ids as $id) {
  459. $this->dbh->query("DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
  460. }
  461. }
  462. private function saveRulesAndActions($filter_id) {
  463. $this->dbh->query("DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
  464. $this->dbh->query("DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
  465. if ($filter_id) {
  466. /* create rules */
  467. $rules = array();
  468. $actions = array();
  469. foreach ($_REQUEST["rule"] as $rule) {
  470. $rule = json_decode($rule, true);
  471. unset($rule["id"]);
  472. if (array_search($rule, $rules) === false) {
  473. array_push($rules, $rule);
  474. }
  475. }
  476. foreach ($_REQUEST["action"] as $action) {
  477. $action = json_decode($action, true);
  478. unset($action["id"]);
  479. if (array_search($action, $actions) === false) {
  480. array_push($actions, $action);
  481. }
  482. }
  483. foreach ($rules as $rule) {
  484. if ($rule) {
  485. $reg_exp = $this->dbh->escape_string(trim($rule["reg_exp"]), false);
  486. $inverse = isset($rule["inverse"]) ? "true" : "false";
  487. $filter_type = (int) $this->dbh->escape_string(trim($rule["filter_type"]));
  488. $match_on = $this->dbh->escape_string(json_encode($rule["feed_id"]));
  489. /*if (strpos($feed_id, "CAT:") === 0) {
  490. $cat_filter = bool_to_sql_bool(true);
  491. $cat_id = (int) substr($feed_id, 4);
  492. $feed_id = "NULL";
  493. if (!$cat_id) $cat_id = "NULL"; // Uncategorized
  494. } else {
  495. $cat_filter = bool_to_sql_bool(false);
  496. $feed_id = (int) $feed_id;
  497. $cat_id = "NULL";
  498. if (!$feed_id) $feed_id = "NULL"; // Uncategorized
  499. }*/
  500. $query = "INSERT INTO ttrss_filters2_rules
  501. (filter_id, reg_exp,filter_type,feed_id,cat_id,match_on,inverse) VALUES
  502. ('$filter_id', '$reg_exp', '$filter_type', NULL, NULL, '$match_on', $inverse)";
  503. $this->dbh->query($query);
  504. }
  505. }
  506. foreach ($actions as $action) {
  507. if ($action) {
  508. $action_id = (int) $this->dbh->escape_string($action["action_id"]);
  509. $action_param = $this->dbh->escape_string($action["action_param"]);
  510. $action_param_label = $this->dbh->escape_string($action["action_param_label"]);
  511. if ($action_id == 7) {
  512. $action_param = $action_param_label;
  513. }
  514. if ($action_id == 6) {
  515. $action_param = (int) str_replace("+", "", $action_param);
  516. }
  517. $query = "INSERT INTO ttrss_filters2_actions
  518. (filter_id, action_id, action_param) VALUES
  519. ('$filter_id', '$action_id', '$action_param')";
  520. $this->dbh->query($query);
  521. }
  522. }
  523. }
  524. }
  525. function add() {
  526. if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
  527. return $this->testFilter();
  528. }
  529. # print_r($_REQUEST);
  530. $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
  531. $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
  532. $title = $this->dbh->escape_string($_REQUEST["title"]);
  533. $inverse = checkbox_to_sql_bool($_REQUEST["inverse"]);
  534. $this->dbh->query("BEGIN");
  535. /* create base filter */
  536. $result = $this->dbh->query("INSERT INTO ttrss_filters2
  537. (owner_uid, match_any_rule, enabled, title, inverse) VALUES
  538. (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title', $inverse)");
  539. $result = $this->dbh->query("SELECT MAX(id) AS id FROM ttrss_filters2
  540. WHERE owner_uid = ".$_SESSION["uid"]);
  541. $filter_id = $this->dbh->fetch_result($result, 0, "id");
  542. $this->saveRulesAndActions($filter_id);
  543. $this->dbh->query("COMMIT");
  544. }
  545. function index() {
  546. $sort = $this->dbh->escape_string($_REQUEST["sort"]);
  547. if (!$sort || $sort == "undefined") {
  548. $sort = "reg_exp";
  549. }
  550. $filter_search = $this->dbh->escape_string($_REQUEST["search"]);
  551. if (array_key_exists("search", $_REQUEST)) {
  552. $_SESSION["prefs_filter_search"] = $filter_search;
  553. } else {
  554. $filter_search = $_SESSION["prefs_filter_search"];
  555. }
  556. print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
  557. print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
  558. print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
  559. $filter_search = $this->dbh->escape_string($_REQUEST["search"]);
  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=\"updateFilterList()\">".
  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 quickAddFilter()\">".
  580. __('Create filter')."</button> ";
  581. print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
  582. __('Combine')."</button> ";
  583. print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
  584. __('Edit')."</button> ";
  585. print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
  586. __('Reset sort order')."</button> ";
  587. print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
  588. __('Remove')."</button> ";
  589. if (defined('_ENABLE_FEED_DEBUGGING')) {
  590. print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
  591. __('Rescore articles')."</button> ";
  592. }
  593. print "</div>"; # toolbar
  594. print "</div>"; # toolbar-frame
  595. print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
  596. print "<div id=\"filterlistLoading\">
  597. <img src='images/indicator_tiny.gif'>".
  598. __("Loading, please wait...")."</div>";
  599. print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
  600. url=\"backend.php?op=pref-filters&method=getfiltertree\">
  601. </div>
  602. <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
  603. query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
  604. childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
  605. </div>
  606. <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
  607. dndController=\"dijit.tree.dndSource\"
  608. betweenThreshold=\"5\"
  609. model=\"filterModel\" openOnClick=\"true\">
  610. <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
  611. Element.hide(\"filterlistLoading\");
  612. </script>
  613. <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
  614. var id = String(item.id);
  615. var bare_id = id.substr(id.indexOf(':')+1);
  616. if (id.match('FILTER:')) {
  617. editFilter(bare_id);
  618. }
  619. </script>
  620. </div>";
  621. print "</div>"; #pane
  622. PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_TAB,
  623. "hook_prefs_tab", "prefFilters");
  624. print "</div>"; #container
  625. }
  626. function newfilter() {
  627. print "<form name='filter_new_form' id='filter_new_form'>";
  628. print_hidden("op", "pref-filters");
  629. print_hidden("method", "add");
  630. print_hidden("csrf_token", $_SESSION['csrf_token']);
  631. print "<div class=\"dlgSec\">".__("Caption")."</div>";
  632. print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
  633. print "<div class=\"dlgSec\">".__("Match")."</div>";
  634. print "<div dojoType=\"dijit.Toolbar\">";
  635. print "<div dojoType=\"dijit.form.DropDownButton\">".
  636. "<span>" . __('Select')."</span>";
  637. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  638. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
  639. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  640. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
  641. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  642. print "</div></div>";
  643. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
  644. __('Add')."</button> ";
  645. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
  646. __('Delete')."</button> ";
  647. print "</div>";
  648. print "<ul id='filterDlg_Matches'>";
  649. # print "<li>No rules</li>";
  650. print "</ul>";
  651. print "</div>";
  652. print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
  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 "<div class=\"dlgSec\">".__("Options")."</div>";
  671. print "<div class=\"dlgSecCont\">"; */
  672. print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
  673. <label for=\"enabled\">".__('Enabled')."</label>";
  674. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
  675. <label for=\"match_any_rule\">".__('Match any rule')."</label>";
  676. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
  677. <label for=\"inverse\">".__('Inverse matching')."</label>";
  678. // print "</div>";
  679. print "<div class=\"dlgButtons\">";
  680. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
  681. __('Test')."</button> ";
  682. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
  683. __('Create')."</button> ";
  684. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
  685. __('Cancel')."</button>";
  686. print "</div>";
  687. }
  688. function newrule() {
  689. $rule = json_decode($_REQUEST["rule"], true);
  690. if ($rule) {
  691. $reg_exp = htmlspecialchars($rule["reg_exp"]);
  692. $filter_type = $rule["filter_type"];
  693. $feed_id = $rule["feed_id"];
  694. $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
  695. } else {
  696. $reg_exp = "";
  697. $filter_type = 1;
  698. $feed_id = ["0"];
  699. $inverse_checked = "";
  700. }
  701. /*if (strpos($feed_id, "CAT:") === 0) {
  702. $feed_id = substr($feed_id, 4);
  703. $cat_filter = true;
  704. } else {
  705. $cat_filter = false;
  706. }*/
  707. print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
  708. $result = $this->dbh->query("SELECT id,description
  709. FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
  710. $filter_types = array();
  711. while ($line = $this->dbh->fetch_assoc($result)) {
  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($_REQUEST["action"], true);
  748. if ($action) {
  749. $action_param = $this->dbh->escape_string($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. $result = $this->dbh->query("SELECT id,description FROM ttrss_filter_actions
  761. ORDER BY name");
  762. while ($line = $this->dbh->fetch_assoc($result)) {
  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. $result = $this->dbh->query(
  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 = '$id' GROUP BY f.title, f.match_any_rule");
  817. $title = $this->dbh->fetch_result($result, 0, "title");
  818. $num_rules = $this->dbh->fetch_result($result, 0, "num_rules");
  819. $num_actions = $this->dbh->fetch_result($result, 0, "num_actions");
  820. $match_any_rule = sql_bool_to_bool($this->dbh->fetch_result($result, 0, "match_any_rule"));
  821. if (!$title) $title = __("[No caption]");
  822. $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", (int) $num_rules), $title, $num_rules);
  823. $result = $this->dbh->query(
  824. "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
  825. $actions = "";
  826. if ($this->dbh->num_rows($result) > 0) {
  827. $line = $this->dbh->fetch_assoc($result);
  828. $actions = $this->getActionName($line);
  829. $num_actions -= 1;
  830. }
  831. if ($match_any_rule) $title .= " (" . __("matches any rule") . ")";
  832. if ($num_actions > 0)
  833. $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", (int) $num_actions), $actions, $num_actions);
  834. return array($title, $actions);
  835. }
  836. function join() {
  837. $ids = explode(",", $this->dbh->escape_string($_REQUEST["ids"]));
  838. if (count($ids) > 1) {
  839. $base_id = array_shift($ids);
  840. $ids_str = join(",", $ids);
  841. $this->dbh->query("BEGIN");
  842. $this->dbh->query("UPDATE ttrss_filters2_rules
  843. SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
  844. $this->dbh->query("UPDATE ttrss_filters2_actions
  845. SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
  846. $this->dbh->query("DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
  847. $this->dbh->query("UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
  848. $this->dbh->query("COMMIT");
  849. $this->optimizeFilter($base_id);
  850. }
  851. }
  852. private function optimizeFilter($id) {
  853. $this->dbh->query("BEGIN");
  854. $result = $this->dbh->query("SELECT * FROM ttrss_filters2_actions
  855. WHERE filter_id = '$id'");
  856. $tmp = array();
  857. $dupe_ids = array();
  858. while ($line = $this->dbh->fetch_assoc($result)) {
  859. $id = $line["id"];
  860. unset($line["id"]);
  861. if (array_search($line, $tmp) === false) {
  862. array_push($tmp, $line);
  863. } else {
  864. array_push($dupe_ids, $id);
  865. }
  866. }
  867. if (count($dupe_ids) > 0) {
  868. $ids_str = join(",", $dupe_ids);
  869. $this->dbh->query("DELETE FROM ttrss_filters2_actions
  870. WHERE id IN ($ids_str)");
  871. }
  872. $result = $this->dbh->query("SELECT * FROM ttrss_filters2_rules
  873. WHERE filter_id = '$id'");
  874. $tmp = array();
  875. $dupe_ids = array();
  876. while ($line = $this->dbh->fetch_assoc($result)) {
  877. $id = $line["id"];
  878. unset($line["id"]);
  879. if (array_search($line, $tmp) === false) {
  880. array_push($tmp, $line);
  881. } else {
  882. array_push($dupe_ids, $id);
  883. }
  884. }
  885. if (count($dupe_ids) > 0) {
  886. $ids_str = join(",", $dupe_ids);
  887. $this->dbh->query("DELETE FROM ttrss_filters2_rules
  888. WHERE id IN ($ids_str)");
  889. }
  890. $this->dbh->query("COMMIT");
  891. }
  892. }