]>
Commit | Line | Data |
---|---|---|
3f9de6cc | 1 | <?php |
369dbc19 | 2 | class Opml extends Handler_Protected { |
3f9de6cc AD |
3 | |
4 | function csrf_ignore($method) { | |
5 | $csrf_ignored = array("export", "import"); | |
6 | ||
7 | return array_search($method, $csrf_ignored) !== false; | |
8 | } | |
9 | ||
10 | function export() { | |
11 | $output_name = $_REQUEST["filename"]; | |
12 | if (!$output_name) $output_name = "TinyTinyRSS.opml"; | |
13 | ||
14 | $show_settings = $_REQUEST["settings"]; | |
15 | ||
16 | $owner_uid = $_SESSION["uid"]; | |
c949a928 AD |
17 | |
18 | $rc = $this->opml_export($output_name, $owner_uid, false, ($show_settings == 1)); | |
19 | ||
20 | return $rc; | |
3f9de6cc AD |
21 | } |
22 | ||
23 | function import() { | |
24 | $owner_uid = $_SESSION["uid"]; | |
25 | ||
26 | header('Content-Type: text/html; charset=utf-8'); | |
27 | ||
28 | print "<html> | |
29 | <head> | |
09bc54c6 | 30 | ".stylesheet_tag("css/default.css")." |
3f9de6cc AD |
31 | <title>".__("OPML Utility")."</title> |
32 | <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> | |
33 | </head> | |
09bc54c6 | 34 | <body class='claro ttrss_utility'> |
884d1650 AD |
35 | <div class=\"floatingLogo\"><img src=\"images/logo_small.png\"></div> |
36 | <h1>".__('OPML Utility')."</h1><div class='content'>"; | |
3f9de6cc | 37 | |
a42c55f0 | 38 | add_feed_category("Imported feeds"); |
3f9de6cc AD |
39 | |
40 | $this->opml_notice(__("Importing OPML...")); | |
c949a928 | 41 | |
3f9de6cc AD |
42 | $this->opml_import($owner_uid); |
43 | ||
44 | print "<br><form method=\"GET\" action=\"prefs.php\"> | |
45 | <input type=\"submit\" value=\"".__("Return to preferences")."\"> | |
46 | </form>"; | |
47 | ||
884d1650 | 48 | print "</div></body></html>"; |
3f9de6cc AD |
49 | |
50 | ||
51 | } | |
52 | ||
53 | // Export | |
54 | ||
55 | private function opml_export_category($owner_uid, $cat_id, $hide_private_feeds=false) { | |
56 | ||
c949a928 | 57 | $cat_id = (int) $cat_id; |
3f9de6cc AD |
58 | |
59 | if ($hide_private_feeds) | |
60 | $hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')"; | |
61 | else | |
62 | $hide_qpart = "true"; | |
63 | ||
64 | $out = ""; | |
65 | ||
66 | if ($cat_id) { | |
c949a928 AD |
67 | $sth = $this->pdo->prepare("SELECT title FROM ttrss_feed_categories WHERE id = ? |
68 | AND owner_uid = ?"); | |
69 | $sth->execute([$cat_id, $owner_uid]); | |
70 | $row = $sth->fetch(); | |
71 | $cat_title = htmlspecialchars($row['title']); | |
3f9de6cc AD |
72 | } |
73 | ||
fd087799 | 74 | if ($cat_title) $out .= "<outline text=\"$cat_title\">\n"; |
3f9de6cc | 75 | |
c949a928 | 76 | $sth = $this->pdo->prepare("SELECT id,title |
3f9de6cc | 77 | FROM ttrss_feed_categories WHERE |
3a029230 | 78 | (parent_cat = :cat OR (:cat = 0 AND parent_cat IS NULL)) AND |
c949a928 AD |
79 | owner_uid = :uid ORDER BY order_id, title"); |
80 | ||
81 | $sth->execute([':cat' => $cat_id, ':uid' => $owner_uid]); | |
3f9de6cc | 82 | |
c949a928 | 83 | while ($line = $sth->fetch()) { |
3f9de6cc AD |
84 | $out .= $this->opml_export_category($owner_uid, $line["id"], $hide_private_feeds); |
85 | } | |
86 | ||
c949a928 | 87 | $fsth = $this->pdo->prepare("select title, feed_url, site_url |
3a029230 | 88 | FROM ttrss_feeds WHERE |
c949a928 AD |
89 | (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)) AND owner_uid = :uid AND $hide_qpart |
90 | ORDER BY order_id, title"); | |
3f9de6cc | 91 | |
c949a928 AD |
92 | $fsth->execute([':cat' => $cat_id, ':uid' => $owner_uid]); |
93 | ||
94 | while ($fline = $fsth->fetch()) { | |
3f9de6cc AD |
95 | $title = htmlspecialchars($fline["title"]); |
96 | $url = htmlspecialchars($fline["feed_url"]); | |
97 | $site_url = htmlspecialchars($fline["site_url"]); | |
98 | ||
99 | if ($site_url) { | |
100 | $html_url_qpart = "htmlUrl=\"$site_url\""; | |
101 | } else { | |
102 | $html_url_qpart = ""; | |
103 | } | |
104 | ||
7bdc1df1 | 105 | $out .= "<outline type=\"rss\" text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n"; |
3f9de6cc AD |
106 | } |
107 | ||
108 | if ($cat_title) $out .= "</outline>\n"; | |
109 | ||
110 | return $out; | |
111 | } | |
112 | ||
113 | function opml_export($name, $owner_uid, $hide_private_feeds=false, $include_settings=true) { | |
114 | if (!$owner_uid) return; | |
115 | ||
116 | if (!isset($_REQUEST["debug"])) { | |
117 | header("Content-type: application/xml+opml"); | |
118 | header("Content-Disposition: attachment; filename=" . $name ); | |
119 | } else { | |
120 | header("Content-type: text/xml"); | |
121 | } | |
122 | ||
123 | $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">"; | |
124 | ||
125 | $out .= "<opml version=\"1.0\">"; | |
126 | $out .= "<head> | |
127 | <dateCreated>" . date("r", time()) . "</dateCreated> | |
128 | <title>Tiny Tiny RSS Feed Export</title> | |
129 | </head>"; | |
130 | $out .= "<body>"; | |
131 | ||
c949a928 | 132 | $out .= $this->opml_export_category($owner_uid, 0, $hide_private_feeds); |
3f9de6cc AD |
133 | |
134 | # export tt-rss settings | |
135 | ||
136 | if ($include_settings) { | |
fd087799 | 137 | $out .= "<outline text=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">"; |
3f9de6cc | 138 | |
c949a928 AD |
139 | $sth = $this->pdo->prepare("SELECT pref_name, value FROM ttrss_user_prefs WHERE |
140 | profile IS NULL AND owner_uid = ? ORDER BY pref_name"); | |
141 | $sth->execute([$owner_uid]); | |
3f9de6cc | 142 | |
c949a928 | 143 | while ($line = $sth->fetch()) { |
3f9de6cc AD |
144 | $name = $line["pref_name"]; |
145 | $value = htmlspecialchars($line["value"]); | |
146 | ||
147 | $out .= "<outline pref-name=\"$name\" value=\"$value\"/>"; | |
148 | } | |
149 | ||
150 | $out .= "</outline>"; | |
151 | ||
fd087799 | 152 | $out .= "<outline text=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION."\">"; |
3f9de6cc | 153 | |
c949a928 AD |
154 | $sth = $this->pdo->prepare("SELECT * FROM ttrss_labels2 WHERE |
155 | owner_uid = ?"); | |
156 | $sth->execute([$owner_uid]); | |
3f9de6cc | 157 | |
c949a928 | 158 | while ($line = $sth->fetch()) { |
3f9de6cc AD |
159 | $name = htmlspecialchars($line['caption']); |
160 | $fg_color = htmlspecialchars($line['fg_color']); | |
161 | $bg_color = htmlspecialchars($line['bg_color']); | |
162 | ||
163 | $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>"; | |
164 | ||
165 | } | |
166 | ||
167 | $out .= "</outline>"; | |
168 | ||
fd087799 | 169 | $out .= "<outline text=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">"; |
3f9de6cc | 170 | |
c949a928 AD |
171 | $sth = $this->pdo->prepare("SELECT * FROM ttrss_filters2 |
172 | WHERE owner_uid = ? ORDER BY id"); | |
173 | $sth->execute([$owner_uid]); | |
3f9de6cc | 174 | |
c949a928 | 175 | while ($line = $sth->fetch()) { |
fd994f1a AD |
176 | $line["rules"] = array(); |
177 | $line["actions"] = array(); | |
178 | ||
c949a928 AD |
179 | $tmph = $this->pdo->prepare("SELECT * FROM ttrss_filters2_rules |
180 | WHERE filter_id = ?"); | |
181 | $tmph->execute([$line['id']]); | |
fd994f1a | 182 | |
c949a928 | 183 | while ($tmp_line = $tmph->fetch()) { |
fd994f1a AD |
184 | unset($tmp_line["id"]); |
185 | unset($tmp_line["filter_id"]); | |
186 | ||
187abfe7 | 187 | $cat_filter = $tmp_line["cat_filter"]; |
fd994f1a | 188 | |
02f3992a AD |
189 | if (!$tmp_line["match_on"]) { |
190 | if ($cat_filter && $tmp_line["cat_id"] || $tmp_line["feed_id"]) { | |
191 | $tmp_line["feed"] = Feeds::getFeedTitle( | |
192 | $cat_filter ? $tmp_line["cat_id"] : $tmp_line["feed_id"], | |
193 | $cat_filter); | |
194 | } else { | |
195 | $tmp_line["feed"] = ""; | |
196 | } | |
197 | } else { | |
198 | $match = []; | |
199 | foreach (json_decode($tmp_line["match_on"], true) as $feed_id) { | |
200 | ||
201 | if (strpos($feed_id, "CAT:") === 0) { | |
202 | $feed_id = (int)substr($feed_id, 4); | |
203 | if ($feed_id) { | |
204 | array_push($match, [Feeds::getCategoryTitle($feed_id), true, false]); | |
205 | } else { | |
206 | array_push($match, [0, true, true]); | |
207 | } | |
208 | } else { | |
209 | if ($feed_id) { | |
210 | array_push($match, [Feeds::getFeedTitle((int)$feed_id), false, false]); | |
211 | } else { | |
212 | array_push($match, [0, false, true]); | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | $tmp_line["match"] = $match; | |
218 | unset($tmp_line["match_on"]); | |
219 | } | |
fd994f1a AD |
220 | |
221 | unset($tmp_line["feed_id"]); | |
222 | unset($tmp_line["cat_id"]); | |
223 | ||
224 | array_push($line["rules"], $tmp_line); | |
225 | } | |
226 | ||
c949a928 AD |
227 | $tmph = $this->pdo->prepare("SELECT * FROM ttrss_filters2_actions |
228 | WHERE filter_id = ?"); | |
229 | $tmph->execute([$line['id']]); | |
fd994f1a | 230 | |
c949a928 | 231 | while ($tmp_line = $tmph->fetch()) { |
fd994f1a AD |
232 | unset($tmp_line["id"]); |
233 | unset($tmp_line["filter_id"]); | |
234 | ||
235 | array_push($line["actions"], $tmp_line); | |
236 | } | |
237 | ||
238 | unset($line["id"]); | |
239 | unset($line["owner_uid"]); | |
3f9de6cc AD |
240 | $filter = json_encode($line); |
241 | ||
dd0a17b0 | 242 | $out .= "<outline filter-type=\"2\"><![CDATA[$filter]]></outline>"; |
3f9de6cc AD |
243 | |
244 | } | |
245 | ||
246 | ||
fd994f1a | 247 | $out .= "</outline>"; |
3f9de6cc AD |
248 | } |
249 | ||
250 | $out .= "</body></opml>"; | |
251 | ||
252 | // Format output. | |
253 | $doc = new DOMDocument(); | |
254 | $doc->formatOutput = true; | |
255 | $doc->preserveWhiteSpace = false; | |
256 | $doc->loadXML($out); | |
257 | ||
258 | $xpath = new DOMXpath($doc); | |
259 | $outlines = $xpath->query("//outline[@title]"); | |
260 | ||
261 | // cleanup empty categories | |
262 | foreach ($outlines as $node) { | |
263 | if ($node->getElementsByTagName('outline')->length == 0) | |
264 | $node->parentNode->removeChild($node); | |
265 | } | |
266 | ||
267 | $res = $doc->saveXML(); | |
268 | ||
dd0a17b0 | 269 | /* // saveXML uses a two-space indent. Change to tabs. |
3f9de6cc AD |
270 | $res = preg_replace_callback('/^(?: )+/mu', |
271 | create_function( | |
272 | '$matches', | |
273 | 'return str_repeat("\t", intval(strlen($matches[0])/2));'), | |
dd0a17b0 | 274 | $res); */ |
3f9de6cc AD |
275 | |
276 | print $res; | |
277 | } | |
278 | ||
279 | // Import | |
280 | ||
7b55001e | 281 | private function opml_import_feed($node, $cat_id, $owner_uid) { |
3f9de6cc AD |
282 | $attrs = $node->attributes; |
283 | ||
c949a928 AD |
284 | $feed_title = mb_substr($attrs->getNamedItem('text')->nodeValue, 0, 250); |
285 | if (!$feed_title) $feed_title = mb_substr($attrs->getNamedItem('title')->nodeValue, 0, 250); | |
3f9de6cc | 286 | |
c949a928 AD |
287 | $feed_url = $attrs->getNamedItem('xmlUrl')->nodeValue; |
288 | if (!$feed_url) $feed_url = $attrs->getNamedItem('xmlURL')->nodeValue; | |
3f9de6cc | 289 | |
c949a928 | 290 | $site_url = mb_substr($attrs->getNamedItem('htmlUrl')->nodeValue, 0, 250); |
3f9de6cc | 291 | |
bbc4d4bf | 292 | if ($feed_url) { |
c949a928 AD |
293 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE |
294 | feed_url = ? AND owner_uid = ?"); | |
295 | $sth->execute([$feed_url, $owner_uid]); | |
3f9de6cc | 296 | |
bbc4d4bf TG |
297 | if (!$feed_title) $feed_title = '[Unknown]'; |
298 | ||
c949a928 | 299 | if (!$sth->fetch()) { |
3f9de6cc | 300 | #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id"); |
bbc4d4bf | 301 | $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title == '[Unknown]' ? $feed_url : $feed_title)); |
3f9de6cc | 302 | |
c949a928 | 303 | if (!$cat_id) $cat_id = null; |
dd0a17b0 | 304 | |
c949a928 | 305 | $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds |
3f9de6cc | 306 | (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES |
c949a928 AD |
307 | (?, ?, ?, ?, ?, 0)"); |
308 | ||
309 | $sth->execute([$feed_title, $feed_url, $owner_uid, $cat_id, $site_url]); | |
3f9de6cc AD |
310 | |
311 | } else { | |
bbc4d4bf | 312 | $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title == '[Unknown]' ? $feed_url : $feed_title)); |
3f9de6cc AD |
313 | } |
314 | } | |
315 | } | |
316 | ||
7b55001e | 317 | private function opml_import_label($node, $owner_uid) { |
3f9de6cc | 318 | $attrs = $node->attributes; |
c949a928 | 319 | $label_name = $attrs->getNamedItem('label-name')->nodeValue; |
3f9de6cc AD |
320 | |
321 | if ($label_name) { | |
c949a928 AD |
322 | $fg_color = $attrs->getNamedItem('label-fg-color')->nodeValue; |
323 | $bg_color = $attrs->getNamedItem('label-bg-color')->nodeValue; | |
3f9de6cc | 324 | |
7c9b5a3f | 325 | if (!Labels::find_id($label_name, $_SESSION['uid'])) { |
3f9de6cc | 326 | $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name))); |
7c9b5a3f | 327 | Labels::create($label_name, $fg_color, $bg_color, $owner_uid); |
3f9de6cc AD |
328 | } else { |
329 | $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name))); | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
7b55001e | 334 | private function opml_import_preference($node) { |
3f9de6cc | 335 | $attrs = $node->attributes; |
c949a928 | 336 | $pref_name = $attrs->getNamedItem('pref-name')->nodeValue; |
3f9de6cc AD |
337 | |
338 | if ($pref_name) { | |
c949a928 | 339 | $pref_value = $attrs->getNamedItem('value')->nodeValue; |
3f9de6cc AD |
340 | |
341 | $this->opml_notice(T_sprintf("Setting preference key %s to %s", | |
342 | $pref_name, $pref_value)); | |
343 | ||
a42c55f0 | 344 | set_pref($pref_name, $pref_value); |
3f9de6cc AD |
345 | } |
346 | } | |
347 | ||
7b55001e | 348 | private function opml_import_filter($node) { |
3f9de6cc AD |
349 | $attrs = $node->attributes; |
350 | ||
c949a928 | 351 | $filter_type = $attrs->getNamedItem('filter-type')->nodeValue; |
3f9de6cc | 352 | |
3b003f63 AD |
353 | if ($filter_type == '2') { |
354 | $filter = json_decode($node->nodeValue, true); | |
3f9de6cc AD |
355 | |
356 | if ($filter) { | |
3b003f63 AD |
357 | $match_any_rule = bool_to_sql_bool($filter["match_any_rule"]); |
358 | $enabled = bool_to_sql_bool($filter["enabled"]); | |
a1495542 | 359 | $inverse = bool_to_sql_bool($filter["inverse"]); |
c949a928 AD |
360 | $title = $filter["title"]; |
361 | ||
99b23dae | 362 | //print "F: $title, $inverse, $enabled, $match_any_rule"; |
7ccb4e91 | 363 | |
c949a928 AD |
364 | $sth = $this->pdo->prepare("INSERT INTO ttrss_filters2 (match_any_rule,enabled,inverse,title,owner_uid) |
365 | VALUES (?, ?, ?, ?, ?)"); | |
3f9de6cc | 366 | |
c949a928 | 367 | $sth->execute([$match_any_rule, $enabled, $inverse, $title, $_SESSION['uid']]); |
3f9de6cc | 368 | |
c949a928 AD |
369 | $sth = $this->pdo->prepare("SELECT MAX(id) AS id FROM ttrss_filters2 WHERE |
370 | owner_uid = ?"); | |
371 | $sth->execute([$_SESSION['uid']]); | |
3b003f63 | 372 | |
c949a928 AD |
373 | $row = $sth->fetch(); |
374 | $filter_id = $row['id']; | |
3b003f63 AD |
375 | |
376 | if ($filter_id) { | |
377 | $this->opml_notice(T_sprintf("Adding filter...")); | |
378 | ||
379 | foreach ($filter["rules"] as $rule) { | |
c949a928 AD |
380 | $feed_id = null; |
381 | $cat_id = null; | |
3f9de6cc | 382 | |
02f3992a AD |
383 | if ($rule["match"]) { |
384 | ||
385 | $match_on = []; | |
386 | ||
387 | foreach ($rule["match"] as $match) { | |
388 | list ($name, $is_cat, $is_id) = $match; | |
389 | ||
390 | if ($is_id) { | |
391 | array_push($match_on, ($is_cat ? "CAT:" : "") . $name); | |
392 | } else { | |
393 | ||
394 | $match_id = false; | |
395 | ||
396 | if (!$is_cat) { | |
c949a928 AD |
397 | $tsth = $this->pdo->prepare("SELECT id FROM ttrss_feeds |
398 | WHERE title = ? AND owner_uid = ?"); | |
399 | ||
400 | $tsth->execute([$name, $_SESSION['uid']]); | |
401 | ||
402 | if ($row = $tsth->fetch()) { | |
403 | $match_id = $row['id']; | |
02f3992a AD |
404 | } |
405 | } else { | |
c949a928 AD |
406 | $tsth = $this->pdo->prepare("SELECT id FROM ttrss_feed_categories |
407 | WHERE title = ? AND owner_uid = ?"); | |
408 | $tsth->execute([$name, $_SESSION['uid']]); | |
02f3992a | 409 | |
c949a928 AD |
410 | if ($row = $tsth->fetch()) { |
411 | $match_id = $row['id']; | |
412 | } | |
02f3992a AD |
413 | } |
414 | ||
415 | if ($match_id) array_push($match_on, $match_id); | |
416 | } | |
417 | } | |
418 | ||
c949a928 | 419 | $reg_exp = $rule["reg_exp"]; |
02f3992a AD |
420 | $filter_type = (int)$rule["filter_type"]; |
421 | $inverse = bool_to_sql_bool($rule["inverse"]); | |
c949a928 | 422 | $match_on = json_encode($match_on); |
02f3992a | 423 | |
3a029230 | 424 | $usth = $this->pdo->prepare("INSERT INTO ttrss_filters2_rules |
c949a928 | 425 | (feed_id,cat_id,match_on,filter_id,filter_type,reg_exp,cat_filter,inverse) |
3a029230 | 426 | VALUES |
c949a928 AD |
427 | (NULL, NULL, ?, ?, ?, ?, false, ?)"); |
428 | $usth->execute([$match_on, $filter_id, $filter_type, $reg_exp, $inverse]); | |
02f3992a AD |
429 | |
430 | } else { | |
431 | ||
432 | if (!$rule["cat_filter"]) { | |
c949a928 AD |
433 | $tsth = $this->pdo->prepare("SELECT id FROM ttrss_feeds |
434 | WHERE title = ? AND owner_uid = ?"); | |
435 | ||
436 | $tsth->execute([$rule['feed'], $_SESSION['uid']]); | |
437 | ||
438 | if ($row = $tsth->fetch()) { | |
439 | $feed_id = $row['id']; | |
02f3992a AD |
440 | } |
441 | } else { | |
c949a928 AD |
442 | $tsth = $this->pdo->prepare("SELECT id FROM ttrss_feed_categories |
443 | WHERE title = ? AND owner_uid = ?"); | |
02f3992a | 444 | |
c949a928 AD |
445 | $tsth->execute([$rule['feed'], $_SESSION['uid']]); |
446 | ||
447 | if ($row = $tsth->fetch()) { | |
448 | $feed_id = $row['id']; | |
449 | } | |
02f3992a AD |
450 | } |
451 | ||
452 | $cat_filter = bool_to_sql_bool($rule["cat_filter"]); | |
c949a928 | 453 | $reg_exp = $rule["reg_exp"]; |
02f3992a AD |
454 | $filter_type = (int)$rule["filter_type"]; |
455 | $inverse = bool_to_sql_bool($rule["inverse"]); | |
456 | ||
3a029230 | 457 | $usth = $this->pdo->prepare("INSERT INTO ttrss_filters2_rules |
c949a928 | 458 | (feed_id,cat_id,filter_id,filter_type,reg_exp,cat_filter,inverse) |
3a029230 | 459 | VALUES |
c949a928 AD |
460 | (?, ?, ?, ?, ?, ?, ?)"); |
461 | $usth->execute([$feed_id, $cat_id, $filter_id, $filter_type, $reg_exp, $cat_filter, $inverse]); | |
02f3992a | 462 | } |
3b003f63 | 463 | } |
3f9de6cc | 464 | |
3b003f63 | 465 | foreach ($filter["actions"] as $action) { |
3f9de6cc | 466 | |
3b003f63 | 467 | $action_id = (int)$action["action_id"]; |
c949a928 | 468 | $action_param = $action["action_param"]; |
3f9de6cc | 469 | |
3a029230 | 470 | $usth = $this->pdo->prepare("INSERT INTO ttrss_filters2_actions |
c949a928 | 471 | (filter_id,action_id,action_param) |
3a029230 | 472 | VALUES |
c949a928 AD |
473 | (?, ?, ?)"); |
474 | $usth->execute([$filter_id, $action_id, $action_param]); | |
3b003f63 | 475 | } |
3f9de6cc AD |
476 | } |
477 | } | |
478 | } | |
3b003f63 | 479 | } |
3f9de6cc AD |
480 | |
481 | private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) { | |
e60d5b0a | 482 | $default_cat_id = (int) $this->get_feed_category('Imported feeds', false); |
3f9de6cc AD |
483 | |
484 | if ($root_node) { | |
c949a928 | 485 | $cat_title = mb_substr($root_node->attributes->getNamedItem('text')->nodeValue, 0, 250); |
3f9de6cc | 486 | |
cc616ea1 | 487 | if (!$cat_title) |
c949a928 | 488 | $cat_title = mb_substr($root_node->attributes->getNamedItem('title')->nodeValue, 0, 250); |
cc616ea1 | 489 | |
3f9de6cc | 490 | if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) { |
e60d5b0a | 491 | $cat_id = $this->get_feed_category($cat_title, $parent_id); |
c949a928 | 492 | |
3f9de6cc | 493 | if ($cat_id === false) { |
a42c55f0 | 494 | add_feed_category($cat_title, $parent_id); |
e60d5b0a | 495 | $cat_id = $this->get_feed_category($cat_title, $parent_id); |
3f9de6cc | 496 | } |
c949a928 | 497 | |
3f9de6cc AD |
498 | } else { |
499 | $cat_id = 0; | |
500 | } | |
501 | ||
502 | $outlines = $root_node->childNodes; | |
503 | ||
504 | } else { | |
505 | $xpath = new DOMXpath($doc); | |
506 | $outlines = $xpath->query("//opml/body/outline"); | |
507 | ||
508 | $cat_id = 0; | |
509 | } | |
510 | ||
511 | #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id"); | |
512 | $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized"))); | |
513 | ||
514 | foreach ($outlines as $node) { | |
515 | if ($node->hasAttributes() && strtolower($node->tagName) == "outline") { | |
516 | $attrs = $node->attributes; | |
c949a928 | 517 | $node_cat_title = $attrs->getNamedItem('text')->nodeValue; |
cc616ea1 AD |
518 | |
519 | if (!$node_cat_title) | |
c949a928 | 520 | $node_cat_title = $attrs->getNamedItem('title')->nodeValue; |
cc616ea1 | 521 | |
c949a928 | 522 | $node_feed_url = $attrs->getNamedItem('xmlUrl')->nodeValue; |
3f9de6cc AD |
523 | |
524 | if ($node_cat_title && !$node_feed_url) { | |
525 | $this->opml_import_category($doc, $node, $owner_uid, $cat_id); | |
526 | } else { | |
527 | ||
528 | if (!$cat_id) { | |
529 | $dst_cat_id = $default_cat_id; | |
530 | } else { | |
531 | $dst_cat_id = $cat_id; | |
532 | } | |
533 | ||
534 | switch ($cat_title) { | |
535 | case "tt-rss-prefs": | |
7b55001e | 536 | $this->opml_import_preference($node); |
3f9de6cc AD |
537 | break; |
538 | case "tt-rss-labels": | |
7b55001e | 539 | $this->opml_import_label($node, $owner_uid); |
3f9de6cc AD |
540 | break; |
541 | case "tt-rss-filters": | |
7b55001e | 542 | $this->opml_import_filter($node); |
3f9de6cc AD |
543 | break; |
544 | default: | |
7b55001e | 545 | $this->opml_import_feed($node, $dst_cat_id, $owner_uid); |
3f9de6cc AD |
546 | } |
547 | } | |
548 | } | |
549 | } | |
550 | } | |
551 | ||
552 | function opml_import($owner_uid) { | |
553 | if (!$owner_uid) return; | |
554 | ||
3f9de6cc AD |
555 | $doc = false; |
556 | ||
3306daec AD |
557 | if ($_FILES['opml_file']['error'] != 0) { |
558 | print_error(T_sprintf("Upload failed with error code %d", | |
559 | $_FILES['opml_file']['error'])); | |
560 | return; | |
561 | } | |
562 | ||
3306daec AD |
563 | if (is_uploaded_file($_FILES['opml_file']['tmp_name'])) { |
564 | $tmp_file = tempnam(CACHE_DIR . '/upload', 'opml'); | |
565 | ||
566 | $result = move_uploaded_file($_FILES['opml_file']['tmp_name'], | |
567 | $tmp_file); | |
568 | ||
569 | if (!$result) { | |
570 | print_error(__("Unable to move uploaded file.")); | |
571 | return; | |
572 | } | |
573 | } else { | |
574 | print_error(__('Error: please upload OPML file.')); | |
575 | return; | |
576 | } | |
577 | ||
578 | if (is_file($tmp_file)) { | |
98c39afc | 579 | $doc = new DOMDocument(); |
3457ce7c | 580 | libxml_disable_entity_loader(false); |
3306daec | 581 | $doc->load($tmp_file); |
3457ce7c | 582 | libxml_disable_entity_loader(true); |
3306daec | 583 | unlink($tmp_file); |
3f9de6cc | 584 | } else if (!$doc) { |
3306daec | 585 | print_error(__('Error: unable to find moved OPML file.')); |
3f9de6cc AD |
586 | return; |
587 | } | |
588 | ||
589 | if ($doc) { | |
c949a928 | 590 | $this->pdo->beginTransaction(); |
3f9de6cc | 591 | $this->opml_import_category($doc, false, $owner_uid, false); |
c949a928 | 592 | $this->pdo->commit(); |
3f9de6cc AD |
593 | } else { |
594 | print_error(__('Error while parsing document.')); | |
595 | } | |
596 | } | |
597 | ||
598 | private function opml_notice($msg) { | |
599 | print "$msg<br/>"; | |
600 | } | |
601 | ||
6322ac79 | 602 | static function opml_publish_url(){ |
50832719 AD |
603 | |
604 | $url_path = get_self_url_prefix(); | |
605 | $url_path .= "/opml.php?op=publish&key=" . | |
a42c55f0 | 606 | get_feed_access_key('OPML:Publish', false, $_SESSION["uid"]); |
50832719 AD |
607 | |
608 | return $url_path; | |
609 | } | |
610 | ||
e60d5b0a | 611 | function get_feed_category($feed_cat, $parent_cat_id = false) { |
e60d5b0a | 612 | |
c949a928 | 613 | $parent_cat_id = (int) $parent_cat_id; |
e60d5b0a | 614 | |
c949a928 | 615 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_feed_categories |
3a029230 | 616 | WHERE title = :title |
c949a928 AD |
617 | AND (parent_cat = :parent OR (:parent = 0 AND parent_cat IS NULL)) |
618 | AND owner_uid = :uid"); | |
619 | ||
620 | $sth->execute([':title' => $feed_cat, ':parent' => $parent_cat_id, ':uid' => $_SESSION['uid']]); | |
621 | ||
622 | if ($row = $sth->fetch()) { | |
623 | return $row['id']; | |
e60d5b0a | 624 | } else { |
c949a928 | 625 | return false; |
e60d5b0a AD |
626 | } |
627 | } | |
628 | ||
50832719 | 629 | |
3f9de6cc | 630 | } |