filters.php 33 KB

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