filters.php 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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. db_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. db_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 testFilter() {
  34. $filter = array();
  35. $filter["enabled"] = true;
  36. $filter["match_any_rule"] = sql_bool_to_bool(
  37. checkbox_to_sql_bool(db_escape_string( $_REQUEST["match_any_rule"])));
  38. $filter["inverse"] = sql_bool_to_bool(
  39. checkbox_to_sql_bool(db_escape_string( $_REQUEST["inverse"])));
  40. $filter["rules"] = array();
  41. $result = db_query( "SELECT id,name FROM ttrss_filter_types");
  42. $filter_types = array();
  43. while ($line = db_fetch_assoc($result)) {
  44. $filter_types[$line["id"]] = $line["name"];
  45. }
  46. $rctr = 0;
  47. foreach ($_REQUEST["rule"] AS $r) {
  48. $rule = json_decode($r, true);
  49. if ($rule && $rctr < 5) {
  50. $rule["type"] = $filter_types[$rule["filter_type"]];
  51. unset($rule["filter_type"]);
  52. if (strpos($rule["feed_id"], "CAT:") === 0) {
  53. $rule["cat_id"] = (int) substr($rule["feed_id"], 4);
  54. unset($rule["feed_id"]);
  55. }
  56. array_push($filter["rules"], $rule);
  57. ++$rctr;
  58. } else {
  59. break;
  60. }
  61. }
  62. $feed_title = getFeedTitle( $feed);
  63. $qfh_ret = queryFeedHeadlines( -4, 30, "", false, false, false,
  64. "date_entered DESC", 0, $_SESSION["uid"], $filter);
  65. $result = $qfh_ret[0];
  66. $articles = array();
  67. $found = 0;
  68. print __("Articles matching this filter:");
  69. print "<div class=\"filterTestHolder\">";
  70. print "<table width=\"100%\" cellspacing=\"0\" id=\"prefErrorFeedList\">";
  71. while ($line = db_fetch_assoc($result)) {
  72. $entry_timestamp = strtotime($line["updated"]);
  73. $entry_tags = get_article_tags( $line["id"], $_SESSION["uid"]);
  74. $content_preview = truncate_string(
  75. strip_tags($line["content_preview"]), 100, '...');
  76. if ($line["feed_title"])
  77. $feed_title = $line["feed_title"];
  78. print "<tr>";
  79. print "<td width='5%' align='center'><input
  80. dojoType=\"dijit.form.CheckBox\" checked=\"1\"
  81. disabled=\"1\" type=\"checkbox\"></td>";
  82. print "<td>";
  83. print $line["title"];
  84. print "&nbsp;(";
  85. print "<b>" . $feed_title . "</b>";
  86. print "):&nbsp;";
  87. print "<span class=\"insensitive\">" . $content_preview . "</span>";
  88. print " " . mb_substr($line["date_entered"], 0, 16);
  89. print "</td></tr>";
  90. $found++;
  91. }
  92. if ($found == 0) {
  93. print "<tr><td align='center'>" .
  94. __("No recent articles matching this filter have been found.");
  95. print "</td></tr><tr><td class='insensitive' align='center'>";
  96. print __("Complex expressions might not give results while testing due to issues with database server regexp implementation.");
  97. print "</td></tr>";
  98. }
  99. print "</table></div>";
  100. print "<div style='text-align : center'>";
  101. print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('filterTestDlg').hide()\">".
  102. __('Close this window')."</button>";
  103. print "</div>";
  104. }
  105. function getfiltertree() {
  106. $root = array();
  107. $root['id'] = 'root';
  108. $root['name'] = __('Filters');
  109. $root['items'] = array();
  110. $filter_search = $_SESSION["prefs_filter_search"];
  111. $result = db_query( "SELECT *,
  112. (SELECT action_param FROM ttrss_filters2_actions
  113. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_param,
  114. (SELECT action_id FROM ttrss_filters2_actions
  115. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS action_id,
  116. (SELECT description FROM ttrss_filter_actions
  117. WHERE id = (SELECT action_id FROM ttrss_filters2_actions
  118. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1)) AS action_name,
  119. (SELECT reg_exp FROM ttrss_filters2_rules
  120. WHERE filter_id = ttrss_filters2.id ORDER BY id LIMIT 1) AS reg_exp
  121. FROM ttrss_filters2 WHERE
  122. owner_uid = ".$_SESSION["uid"]." ORDER BY order_id, title");
  123. $action_id = -1;
  124. $folder = array();
  125. $folder['items'] = array();
  126. while ($line = db_fetch_assoc($result)) {
  127. /* if ($action_id != $line["action_id"]) {
  128. if (count($folder['items']) > 0) {
  129. array_push($root['items'], $folder);
  130. }
  131. $folder = array();
  132. $folder['id'] = $line["action_id"];
  133. $folder['name'] = __($line["action_name"]);
  134. $folder['items'] = array();
  135. $action_id = $line["action_id"];
  136. } */
  137. $name = $this->getFilterName($line["id"]);
  138. $match_ok = false;
  139. if ($filter_search) {
  140. $rules_result = db_query(
  141. "SELECT reg_exp FROM ttrss_filters2_rules WHERE filter_id = ".$line["id"]);
  142. while ($rule_line = db_fetch_assoc($rules_result)) {
  143. if (mb_strpos($rule_line['reg_exp'], $filter_search) !== false) {
  144. $match_ok = true;
  145. break;
  146. }
  147. }
  148. }
  149. if ($line['action_id'] == 7) {
  150. $label_result = db_query( "SELECT fg_color, bg_color
  151. FROM ttrss_labels2 WHERE caption = '".db_escape_string( $line['action_param'])."' AND
  152. owner_uid = " . $_SESSION["uid"]);
  153. if (db_num_rows($label_result) > 0) {
  154. $fg_color = db_fetch_result($label_result, 0, "fg_color");
  155. $bg_color = db_fetch_result($label_result, 0, "bg_color");
  156. $name[1] = "<span class=\"labelColorIndicator\" id=\"label-editor-indicator\" style='color : $fg_color; background-color : $bg_color; margin-right : 4px'>&alpha;</span>" . $name[1];
  157. }
  158. }
  159. $filter = array();
  160. $filter['id'] = 'FILTER:' . $line['id'];
  161. $filter['bare_id'] = $line['id'];
  162. $filter['name'] = $name[0];
  163. $filter['param'] = $name[1];
  164. $filter['checkbox'] = false;
  165. $filter['enabled'] = sql_bool_to_bool($line["enabled"]);
  166. if (!$filter_search || $match_ok) {
  167. array_push($folder['items'], $filter);
  168. }
  169. }
  170. /* if (count($folder['items']) > 0) {
  171. array_push($root['items'], $folder);
  172. } */
  173. $root['items'] = $folder['items'];
  174. $fl = array();
  175. $fl['identifier'] = 'id';
  176. $fl['label'] = 'name';
  177. $fl['items'] = array($root);
  178. print json_encode($fl);
  179. return;
  180. }
  181. function edit() {
  182. $filter_id = db_escape_string( $_REQUEST["id"]);
  183. $result = db_query(
  184. "SELECT * FROM ttrss_filters2 WHERE id = '$filter_id' AND owner_uid = " . $_SESSION["uid"]);
  185. $enabled = sql_bool_to_bool(db_fetch_result($result, 0, "enabled"));
  186. $match_any_rule = sql_bool_to_bool(db_fetch_result($result, 0, "match_any_rule"));
  187. $inverse = sql_bool_to_bool(db_fetch_result($result, 0, "inverse"));
  188. $title = htmlspecialchars(db_fetch_result($result, 0, "title"));
  189. print "<form id=\"filter_edit_form\" onsubmit='return false'>";
  190. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
  191. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"id\" value=\"$filter_id\">";
  192. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"editSave\">";
  193. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
  194. print "<div class=\"dlgSec\">".__("Caption")."</div>";
  195. print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"$title\">";
  196. print "</div>";
  197. print "<div class=\"dlgSec\">".__("Match")."</div>";
  198. print "<div dojoType=\"dijit.Toolbar\">";
  199. print "<div dojoType=\"dijit.form.DropDownButton\">".
  200. "<span>" . __('Select')."</span>";
  201. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  202. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
  203. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  204. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
  205. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  206. print "</div></div>";
  207. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
  208. __('Add')."</button> ";
  209. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
  210. __('Delete')."</button> ";
  211. print "</div>";
  212. print "<ul id='filterDlg_Matches'>";
  213. $rules_result = db_query( "SELECT * FROM ttrss_filters2_rules
  214. WHERE filter_id = '$filter_id' ORDER BY reg_exp, id");
  215. while ($line = db_fetch_assoc($rules_result)) {
  216. if (sql_bool_to_bool($line["cat_filter"])) {
  217. $line["feed_id"] = "CAT:" . (int)$line["cat_id"];
  218. }
  219. unset($line["cat_filter"]);
  220. unset($line["cat_id"]);
  221. unset($line["filter_id"]);
  222. unset($line["id"]);
  223. if (!sql_bool_to_bool($line["inverse"])) unset($line["inverse"]);
  224. $data = htmlspecialchars(json_encode($line));
  225. print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
  226. "<span onclick=\"dijit.byId('filterEditDlg').editRule(this)\">".$this->getRuleName($line)."</span>".
  227. "<input type='hidden' name='rule[]' value=\"$data\"/></li>";
  228. }
  229. print "</ul>";
  230. print "</div>";
  231. print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
  232. print "<div dojoType=\"dijit.Toolbar\">";
  233. print "<div dojoType=\"dijit.form.DropDownButton\">".
  234. "<span>" . __('Select')."</span>";
  235. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  236. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
  237. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  238. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
  239. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  240. print "</div></div>";
  241. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
  242. __('Add')."</button> ";
  243. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
  244. __('Delete')."</button> ";
  245. print "</div>";
  246. print "<ul id='filterDlg_Actions'>";
  247. $actions_result = db_query( "SELECT * FROM ttrss_filters2_actions
  248. WHERE filter_id = '$filter_id' ORDER BY id");
  249. while ($line = db_fetch_assoc($actions_result)) {
  250. $line["action_param_label"] = $line["action_param"];
  251. unset($line["filter_id"]);
  252. unset($line["id"]);
  253. $data = htmlspecialchars(json_encode($line));
  254. print "<li><input dojoType='dijit.form.CheckBox' type='checkbox' onclick='toggleSelectListRow2(this)'>".
  255. "<span onclick=\"dijit.byId('filterEditDlg').editAction(this)\">".$this->getActionName($line)."</span>".
  256. "<input type='hidden' name='action[]' value=\"$data\"/></li>";
  257. }
  258. print "</ul>";
  259. print "</div>";
  260. if ($enabled) {
  261. $checked = "checked=\"1\"";
  262. } else {
  263. $checked = "";
  264. }
  265. print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" $checked>
  266. <label for=\"enabled\">".__('Enabled')."</label>";
  267. if ($match_any_rule) {
  268. $checked = "checked=\"1\"";
  269. } else {
  270. $checked = "";
  271. }
  272. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\" $checked>
  273. <label for=\"match_any_rule\">".__('Match any rule')."</label>";
  274. if ($inverse) {
  275. $checked = "checked=\"1\"";
  276. } else {
  277. $checked = "";
  278. }
  279. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\" $checked>
  280. <label for=\"inverse\">".__('Inverse matching')."</label>";
  281. print "<p/>";
  282. print "<div class=\"dlgButtons\">";
  283. print "<div style=\"float : left\">";
  284. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').removeFilter()\">".
  285. __('Remove')."</button>";
  286. print "</div>";
  287. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
  288. __('Test')."</button> ";
  289. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
  290. __('Save')."</button> ";
  291. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
  292. __('Cancel')."</button>";
  293. print "</div>";
  294. }
  295. private function getRuleName($rule) {
  296. if (!$rule) $rule = json_decode($_REQUEST["rule"], true);
  297. $feed_id = $rule["feed_id"];
  298. if (strpos($feed_id, "CAT:") === 0) {
  299. $feed_id = (int) substr($feed_id, 4);
  300. $feed = getCategoryTitle( $feed_id);
  301. } else {
  302. $feed_id = (int) $feed_id;
  303. if ($rule["feed_id"])
  304. $feed = getFeedTitle( (int)$rule["feed_id"]);
  305. else
  306. $feed = __("All feeds");
  307. }
  308. $result = db_query( "SELECT description FROM ttrss_filter_types
  309. WHERE id = ".(int)$rule["filter_type"]);
  310. $filter_type = db_fetch_result($result, 0, "description");
  311. return T_sprintf("%s on %s in %s %s", strip_tags($rule["reg_exp"]),
  312. $filter_type, $feed, isset($rule["inverse"]) ? __("(inverse)") : "");
  313. }
  314. function printRuleName() {
  315. print $this->getRuleName(json_decode($_REQUEST["rule"], true));
  316. }
  317. private function getActionName($action) {
  318. $result = db_query( "SELECT description FROM
  319. ttrss_filter_actions WHERE id = " .(int)$action["action_id"]);
  320. $title = __(db_fetch_result($result, 0, "description"));
  321. if ($action["action_id"] == 4 || $action["action_id"] == 6 ||
  322. $action["action_id"] == 7)
  323. $title .= ": " . $action["action_param"];
  324. return $title;
  325. }
  326. function printActionName() {
  327. print $this->getActionName(json_decode($_REQUEST["action"], true));
  328. }
  329. function editSave() {
  330. if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
  331. return $this->testFilter();
  332. }
  333. # print_r($_REQUEST);
  334. $filter_id = db_escape_string( $_REQUEST["id"]);
  335. $enabled = checkbox_to_sql_bool(db_escape_string( $_REQUEST["enabled"]));
  336. $match_any_rule = checkbox_to_sql_bool(db_escape_string( $_REQUEST["match_any_rule"]));
  337. $inverse = checkbox_to_sql_bool(db_escape_string( $_REQUEST["inverse"]));
  338. $title = db_escape_string( $_REQUEST["title"]);
  339. $result = db_query( "UPDATE ttrss_filters2 SET enabled = $enabled,
  340. match_any_rule = $match_any_rule,
  341. inverse = $inverse,
  342. title = '$title'
  343. WHERE id = '$filter_id'
  344. AND owner_uid = ". $_SESSION["uid"]);
  345. $this->saveRulesAndActions($filter_id);
  346. }
  347. function remove() {
  348. $ids = explode(",", db_escape_string( $_REQUEST["ids"]));
  349. foreach ($ids as $id) {
  350. db_query( "DELETE FROM ttrss_filters2 WHERE id = '$id' AND owner_uid = ". $_SESSION["uid"]);
  351. }
  352. }
  353. private function saveRulesAndActions($filter_id) {
  354. db_query( "DELETE FROM ttrss_filters2_rules WHERE filter_id = '$filter_id'");
  355. db_query( "DELETE FROM ttrss_filters2_actions WHERE filter_id = '$filter_id'");
  356. if ($filter_id) {
  357. /* create rules */
  358. $rules = array();
  359. $actions = array();
  360. foreach ($_REQUEST["rule"] as $rule) {
  361. $rule = json_decode($rule, true);
  362. unset($rule["id"]);
  363. if (array_search($rule, $rules) === false) {
  364. array_push($rules, $rule);
  365. }
  366. }
  367. foreach ($_REQUEST["action"] as $action) {
  368. $action = json_decode($action, true);
  369. unset($action["id"]);
  370. if (array_search($action, $actions) === false) {
  371. array_push($actions, $action);
  372. }
  373. }
  374. foreach ($rules as $rule) {
  375. if ($rule) {
  376. $reg_exp = strip_tags(db_escape_string( trim($rule["reg_exp"])));
  377. $inverse = isset($rule["inverse"]) ? "true" : "false";
  378. $filter_type = (int) db_escape_string( trim($rule["filter_type"]));
  379. $feed_id = db_escape_string( trim($rule["feed_id"]));
  380. if (strpos($feed_id, "CAT:") === 0) {
  381. $cat_filter = bool_to_sql_bool(true);
  382. $cat_id = (int) substr($feed_id, 4);
  383. $feed_id = "NULL";
  384. if (!$cat_id) $cat_id = "NULL"; // Uncategorized
  385. } else {
  386. $cat_filter = bool_to_sql_bool(false);
  387. $feed_id = (int) $feed_id;
  388. $cat_id = "NULL";
  389. if (!$feed_id) $feed_id = "NULL"; // Uncategorized
  390. }
  391. $query = "INSERT INTO ttrss_filters2_rules
  392. (filter_id, reg_exp,filter_type,feed_id,cat_id,cat_filter,inverse) VALUES
  393. ('$filter_id', '$reg_exp', '$filter_type', $feed_id, $cat_id, $cat_filter, $inverse)";
  394. db_query( $query);
  395. }
  396. }
  397. foreach ($actions as $action) {
  398. if ($action) {
  399. $action_id = (int) db_escape_string( $action["action_id"]);
  400. $action_param = db_escape_string( $action["action_param"]);
  401. $action_param_label = db_escape_string( $action["action_param_label"]);
  402. if ($action_id == 7) {
  403. $action_param = $action_param_label;
  404. }
  405. if ($action_id == 6) {
  406. $action_param = (int) str_replace("+", "", $action_param);
  407. }
  408. $query = "INSERT INTO ttrss_filters2_actions
  409. (filter_id, action_id, action_param) VALUES
  410. ('$filter_id', '$action_id', '$action_param')";
  411. db_query( $query);
  412. }
  413. }
  414. }
  415. }
  416. function add() {
  417. if ($_REQUEST["savemode"] && $_REQUEST["savemode"] == "test") {
  418. return $this->testFilter();
  419. }
  420. # print_r($_REQUEST);
  421. $enabled = checkbox_to_sql_bool($_REQUEST["enabled"]);
  422. $match_any_rule = checkbox_to_sql_bool($_REQUEST["match_any_rule"]);
  423. $title = db_escape_string( $_REQUEST["title"]);
  424. db_query( "BEGIN");
  425. /* create base filter */
  426. $result = db_query( "INSERT INTO ttrss_filters2
  427. (owner_uid, match_any_rule, enabled, title) VALUES
  428. (".$_SESSION["uid"].",$match_any_rule,$enabled, '$title')");
  429. $result = db_query( "SELECT MAX(id) AS id FROM ttrss_filters2
  430. WHERE owner_uid = ".$_SESSION["uid"]);
  431. $filter_id = db_fetch_result($result, 0, "id");
  432. $this->saveRulesAndActions($filter_id);
  433. db_query( "COMMIT");
  434. }
  435. function index() {
  436. $sort = db_escape_string( $_REQUEST["sort"]);
  437. if (!$sort || $sort == "undefined") {
  438. $sort = "reg_exp";
  439. }
  440. $filter_search = db_escape_string( $_REQUEST["search"]);
  441. if (array_key_exists("search", $_REQUEST)) {
  442. $_SESSION["prefs_filter_search"] = $filter_search;
  443. } else {
  444. $filter_search = $_SESSION["prefs_filter_search"];
  445. }
  446. print "<div id=\"pref-filter-wrap\" dojoType=\"dijit.layout.BorderContainer\" gutters=\"false\">";
  447. print "<div id=\"pref-filter-header\" dojoType=\"dijit.layout.ContentPane\" region=\"top\">";
  448. print "<div id=\"pref-filter-toolbar\" dojoType=\"dijit.Toolbar\">";
  449. $filter_search = db_escape_string( $_REQUEST["search"]);
  450. if (array_key_exists("search", $_REQUEST)) {
  451. $_SESSION["prefs_filter_search"] = $filter_search;
  452. } else {
  453. $filter_search = $_SESSION["prefs_filter_search"];
  454. }
  455. print "<div style='float : right; padding-right : 4px;'>
  456. <input dojoType=\"dijit.form.TextBox\" id=\"filter_search\" size=\"20\" type=\"search\"
  457. value=\"$filter_search\">
  458. <button dojoType=\"dijit.form.Button\" onclick=\"updateFilterList()\">".
  459. __('Search')."</button>
  460. </div>";
  461. print "<div dojoType=\"dijit.form.DropDownButton\">".
  462. "<span>" . __('Select')."</span>";
  463. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  464. print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(true)\"
  465. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  466. print "<div onclick=\"dijit.byId('filterTree').model.setAllChecked(false)\"
  467. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  468. print "</div></div>";
  469. print "<button dojoType=\"dijit.form.Button\" onclick=\"return quickAddFilter()\">".
  470. __('Create filter')."</button> ";
  471. print "<button dojoType=\"dijit.form.Button\" onclick=\"return joinSelectedFilters()\">".
  472. __('Combine')."</button> ";
  473. print "<button dojoType=\"dijit.form.Button\" onclick=\"return editSelectedFilter()\">".
  474. __('Edit')."</button> ";
  475. print "<button dojoType=\"dijit.form.Button\" onclick=\"return resetFilterOrder()\">".
  476. __('Reset sort order')."</button> ";
  477. print "<button dojoType=\"dijit.form.Button\" onclick=\"return removeSelectedFilters()\">".
  478. __('Remove')."</button> ";
  479. if (defined('_ENABLE_FEED_DEBUGGING')) {
  480. print "<button dojoType=\"dijit.form.Button\" onclick=\"rescore_all_feeds()\">".
  481. __('Rescore articles')."</button> ";
  482. }
  483. print "</div>"; # toolbar
  484. print "</div>"; # toolbar-frame
  485. print "<div id=\"pref-filter-content\" dojoType=\"dijit.layout.ContentPane\" region=\"center\">";
  486. print "<div id=\"filterlistLoading\">
  487. <img src='images/indicator_tiny.gif'>".
  488. __("Loading, please wait...")."</div>";
  489. print "<div dojoType=\"fox.PrefFilterStore\" jsId=\"filterStore\"
  490. url=\"backend.php?op=pref-filters&method=getfiltertree\">
  491. </div>
  492. <div dojoType=\"lib.CheckBoxStoreModel\" jsId=\"filterModel\" store=\"filterStore\"
  493. query=\"{id:'root'}\" rootId=\"root\" rootLabel=\"Filters\"
  494. childrenAttrs=\"items\" checkboxStrict=\"false\" checkboxAll=\"false\">
  495. </div>
  496. <div dojoType=\"fox.PrefFilterTree\" id=\"filterTree\"
  497. dndController=\"dijit.tree.dndSource\"
  498. betweenThreshold=\"5\"
  499. model=\"filterModel\" openOnClick=\"true\">
  500. <script type=\"dojo/method\" event=\"onLoad\" args=\"item\">
  501. Element.hide(\"filterlistLoading\");
  502. </script>
  503. <script type=\"dojo/method\" event=\"onClick\" args=\"item\">
  504. var id = String(item.id);
  505. var bare_id = id.substr(id.indexOf(':')+1);
  506. if (id.match('FILTER:')) {
  507. editFilter(bare_id);
  508. }
  509. </script>
  510. </div>";
  511. print "</div>"; #pane
  512. global $pluginhost;
  513. $pluginhost->run_hooks($pluginhost::HOOK_PREFS_TAB,
  514. "hook_prefs_tab", "prefFilters");
  515. print "</div>"; #container
  516. }
  517. function newfilter() {
  518. print "<form name='filter_new_form' id='filter_new_form'>";
  519. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pref-filters\">";
  520. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"add\">";
  521. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"csrf_token\" value=\"".$_SESSION['csrf_token']."\">";
  522. print "<div class=\"dlgSec\">".__("Caption")."</div>";
  523. print "<input required=\"true\" dojoType=\"dijit.form.ValidationTextBox\" style=\"width : 20em;\" name=\"title\" value=\"\">";
  524. print "<div class=\"dlgSec\">".__("Match")."</div>";
  525. print "<div dojoType=\"dijit.Toolbar\">";
  526. print "<div dojoType=\"dijit.form.DropDownButton\">".
  527. "<span>" . __('Select')."</span>";
  528. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  529. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(true)\"
  530. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  531. print "<div onclick=\"dijit.byId('filterEditDlg').selectRules(false)\"
  532. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  533. print "</div></div>";
  534. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addRule()\">".
  535. __('Add')."</button> ";
  536. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteRule()\">".
  537. __('Delete')."</button> ";
  538. print "</div>";
  539. print "<ul id='filterDlg_Matches'>";
  540. # print "<li>No rules</li>";
  541. print "</ul>";
  542. print "</div>";
  543. print "<div class=\"dlgSec\">".__("Apply actions")."</div>";
  544. print "<div dojoType=\"dijit.Toolbar\">";
  545. print "<div dojoType=\"dijit.form.DropDownButton\">".
  546. "<span>" . __('Select')."</span>";
  547. print "<div dojoType=\"dijit.Menu\" style=\"display: none;\">";
  548. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(true)\"
  549. dojoType=\"dijit.MenuItem\">".__('All')."</div>";
  550. print "<div onclick=\"dijit.byId('filterEditDlg').selectActions(false)\"
  551. dojoType=\"dijit.MenuItem\">".__('None')."</div>";
  552. print "</div></div>";
  553. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').addAction()\">".
  554. __('Add')."</button> ";
  555. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').deleteAction()\">".
  556. __('Delete')."</button> ";
  557. print "</div>";
  558. print "<ul id='filterDlg_Actions'>";
  559. # print "<li>No actions</li>";
  560. print "</ul>";
  561. /* print "<div class=\"dlgSec\">".__("Options")."</div>";
  562. print "<div class=\"dlgSecCont\">"; */
  563. print "<input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"enabled\" id=\"enabled\" checked=\"1\">
  564. <label for=\"enabled\">".__('Enabled')."</label>";
  565. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"match_any_rule\" id=\"match_any_rule\">
  566. <label for=\"match_any_rule\">".__('Match any rule')."</label>";
  567. print "<br/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" name=\"inverse\" id=\"inverse\">
  568. <label for=\"inverse\">".__('Inverse matching')."</label>";
  569. // print "</div>";
  570. print "<div class=\"dlgButtons\">";
  571. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').test()\">".
  572. __('Test')."</button> ";
  573. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').execute()\">".
  574. __('Create')."</button> ";
  575. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterEditDlg').hide()\">".
  576. __('Cancel')."</button>";
  577. print "</div>";
  578. }
  579. function newrule() {
  580. $rule = json_decode($_REQUEST["rule"], true);
  581. if ($rule) {
  582. $reg_exp = htmlspecialchars($rule["reg_exp"]);
  583. $filter_type = $rule["filter_type"];
  584. $feed_id = $rule["feed_id"];
  585. $inverse_checked = isset($rule["inverse"]) ? "checked" : "";
  586. } else {
  587. $reg_exp = "";
  588. $filter_type = 1;
  589. $feed_id = 0;
  590. $inverse_checked = "";
  591. }
  592. if (strpos($feed_id, "CAT:") === 0) {
  593. $feed_id = substr($feed_id, 4);
  594. $cat_filter = true;
  595. } else {
  596. $cat_filter = false;
  597. }
  598. print "<form name='filter_new_rule_form' id='filter_new_rule_form'>";
  599. $result = db_query( "SELECT id,description
  600. FROM ttrss_filter_types WHERE id != 5 ORDER BY description");
  601. $filter_types = array();
  602. while ($line = db_fetch_assoc($result)) {
  603. $filter_types[$line["id"]] = __($line["description"]);
  604. }
  605. print "<div class=\"dlgSec\">".__("Match")."</div>";
  606. print "<div class=\"dlgSecCont\">";
  607. print "<input dojoType=\"dijit.form.ValidationTextBox\"
  608. required=\"true\" id=\"filterDlg_regExp\"
  609. style=\"font-size : 16px; width : 20em;\"
  610. name=\"reg_exp\" value=\"$reg_exp\"/>";
  611. print "<hr/>";
  612. print "<input id=\"filterDlg_inverse\" dojoType=\"dijit.form.CheckBox\"
  613. name=\"inverse\" $inverse_checked/>";
  614. print "<label for=\"filterDlg_inverse\">".__("Inverse regular expression matching")."</label>";
  615. print "<hr/>" . __("on field") . " ";
  616. print_select_hash("filter_type", $filter_type, $filter_types,
  617. 'dojoType="dijit.form.Select"');
  618. print "<hr/>";
  619. print __("in") . " ";
  620. print "<span id='filterDlg_feeds'>";
  621. print_feed_select( "feed_id",
  622. $cat_filter ? "CAT:$feed_id" : $feed_id,
  623. 'dojoType="dijit.form.FilteringSelect"');
  624. print "</span>";
  625. print "</div>";
  626. print "<div class=\"dlgButtons\">";
  627. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').execute()\">".
  628. ($rule ? __("Save rule") : __('Add rule'))."</button> ";
  629. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewRuleDlg').hide()\">".
  630. __('Cancel')."</button>";
  631. print "</div>";
  632. print "</form>";
  633. }
  634. function newaction() {
  635. $action = json_decode($_REQUEST["action"], true);
  636. if ($action) {
  637. $action_param = db_escape_string( $action["action_param"]);
  638. $action_id = (int)$action["action_id"];
  639. } else {
  640. $action_param = "";
  641. $action_id = 0;
  642. }
  643. print "<form name='filter_new_action_form' id='filter_new_action_form'>";
  644. print "<div class=\"dlgSec\">".__("Perform Action")."</div>";
  645. print "<div class=\"dlgSecCont\">";
  646. print "<select name=\"action_id\" dojoType=\"dijit.form.Select\"
  647. onchange=\"filterDlgCheckAction(this)\">";
  648. $result = db_query( "SELECT id,description FROM ttrss_filter_actions
  649. ORDER BY name");
  650. while ($line = db_fetch_assoc($result)) {
  651. $is_selected = ($line["id"] == $action_id) ? "selected='1'" : "";
  652. printf("<option $is_selected value='%d'>%s</option>", $line["id"], __($line["description"]));
  653. }
  654. print "</select>";
  655. $param_box_hidden = ($action_id == 7 || $action_id == 4 || $action_id == 6) ?
  656. "" : "display : none";
  657. $param_hidden = ($action_id == 4 || $action_id == 6) ?
  658. "" : "display : none";
  659. $label_param_hidden = ($action_id == 7) ? "" : "display : none";
  660. print "<span id=\"filterDlg_paramBox\" style=\"$param_box_hidden\">";
  661. print " " . __("with parameters:") . " ";
  662. print "<input dojoType=\"dijit.form.TextBox\"
  663. id=\"filterDlg_actionParam\" style=\"$param_hidden\"
  664. name=\"action_param\" value=\"$action_param\">";
  665. print_label_select( "action_param_label", $action_param,
  666. "id=\"filterDlg_actionParamLabel\" style=\"$label_param_hidden\"
  667. dojoType=\"dijit.form.Select\"");
  668. print "</span>";
  669. print "&nbsp;"; // tiny layout hack
  670. print "</div>";
  671. print "<div class=\"dlgButtons\">";
  672. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').execute()\">".
  673. ($action ? __("Save action") : __('Add action'))."</button> ";
  674. print "<button dojoType=\"dijit.form.Button\" onclick=\"return dijit.byId('filterNewActionDlg').hide()\">".
  675. __('Cancel')."</button>";
  676. print "</div>";
  677. print "</form>";
  678. }
  679. private function getFilterName($id) {
  680. $result = db_query(
  681. "SELECT title,COUNT(DISTINCT r.id) AS num_rules,COUNT(DISTINCT a.id) AS num_actions
  682. FROM ttrss_filters2 AS f LEFT JOIN ttrss_filters2_rules AS r
  683. ON (r.filter_id = f.id)
  684. LEFT JOIN ttrss_filters2_actions AS a
  685. ON (a.filter_id = f.id) WHERE f.id = '$id' GROUP BY f.title");
  686. $title = db_fetch_result($result, 0, "title");
  687. $num_rules = db_fetch_result($result, 0, "num_rules");
  688. $num_actions = db_fetch_result($result, 0, "num_actions");
  689. if (!$title) $title = __("[No caption]");
  690. $title = sprintf(_ngettext("%s (%d rule)", "%s (%d rules)", $num_rules), $title, $num_rules);
  691. $result = db_query(
  692. "SELECT * FROM ttrss_filters2_actions WHERE filter_id = '$id' ORDER BY id LIMIT 1");
  693. $actions = "";
  694. if (db_num_rows($result) > 0) {
  695. $line = db_fetch_assoc($result);
  696. $actions = $this->getActionName($line);
  697. $num_actions -= 1;
  698. }
  699. if ($num_actions > 0)
  700. $actions = sprintf(_ngettext("%s (+%d action)", "%s (+%d actions)", $num_actions), $actions, $num_actions);
  701. return array($title, $actions);
  702. }
  703. function join() {
  704. $ids = explode(",", db_escape_string( $_REQUEST["ids"]));
  705. if (count($ids) > 1) {
  706. $base_id = array_shift($ids);
  707. $ids_str = join(",", $ids);
  708. db_query( "BEGIN");
  709. db_query( "UPDATE ttrss_filters2_rules
  710. SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
  711. db_query( "UPDATE ttrss_filters2_actions
  712. SET filter_id = '$base_id' WHERE filter_id IN ($ids_str)");
  713. db_query( "DELETE FROM ttrss_filters2 WHERE id IN ($ids_str)");
  714. db_query( "UPDATE ttrss_filters2 SET match_any_rule = true WHERE id = '$base_id'");
  715. db_query( "COMMIT");
  716. $this->optimizeFilter($base_id);
  717. }
  718. }
  719. private function optimizeFilter($id) {
  720. db_query( "BEGIN");
  721. $result = db_query( "SELECT * FROM ttrss_filters2_actions
  722. WHERE filter_id = '$id'");
  723. $tmp = array();
  724. $dupe_ids = array();
  725. while ($line = db_fetch_assoc($result)) {
  726. $id = $line["id"];
  727. unset($line["id"]);
  728. if (array_search($line, $tmp) === false) {
  729. array_push($tmp, $line);
  730. } else {
  731. array_push($dupe_ids, $id);
  732. }
  733. }
  734. if (count($dupe_ids) > 0) {
  735. $ids_str = join(",", $dupe_ids);
  736. db_query( "DELETE FROM ttrss_filters2_actions
  737. WHERE id IN ($ids_str)");
  738. }
  739. $result = db_query( "SELECT * FROM ttrss_filters2_rules
  740. WHERE filter_id = '$id'");
  741. $tmp = array();
  742. $dupe_ids = array();
  743. while ($line = db_fetch_assoc($result)) {
  744. $id = $line["id"];
  745. unset($line["id"]);
  746. if (array_search($line, $tmp) === false) {
  747. array_push($tmp, $line);
  748. } else {
  749. array_push($dupe_ids, $id);
  750. }
  751. }
  752. if (count($dupe_ids) > 0) {
  753. $ids_str = join(",", $dupe_ids);
  754. db_query( "DELETE FROM ttrss_filters2_rules
  755. WHERE id IN ($ids_str)");
  756. }
  757. db_query( "COMMIT");
  758. }
  759. }
  760. ?>