filters.php 31 KB

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