]>
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> | |
5bbc4bb4 | 30 | <link rel=\"stylesheet\" href=\"css/utility.css\" type=\"text/css\"> |
3f9de6cc AD |
31 | <title>".__("OPML Utility")."</title> |
32 | <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> | |
33 | </head> | |
34 | <body> | |
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 |
c949a928 AD |
78 | (parent_cat = :cat OR (:cat = 0 AND parent_cat IS NULL)) AND |
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 AD |
87 | $fsth = $this->pdo->prepare("select title, feed_url, site_url |
88 | FROM ttrss_feeds WHERE | |
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()) { |
a1495542 | 176 | foreach (array('enabled', 'match_any_rule', 'inverse') as $b) { |
3f9de6cc AD |
177 | $line[$b] = sql_bool_to_bool($line[$b]); |
178 | } | |
179 | ||
fd994f1a AD |
180 | $line["rules"] = array(); |
181 | $line["actions"] = array(); | |
182 | ||
c949a928 AD |
183 | $tmph = $this->pdo->prepare("SELECT * FROM ttrss_filters2_rules |
184 | WHERE filter_id = ?"); | |
185 | $tmph->execute([$line['id']]); | |
fd994f1a | 186 | |
c949a928 | 187 | while ($tmp_line = $tmph->fetch()) { |
fd994f1a AD |
188 | unset($tmp_line["id"]); |
189 | unset($tmp_line["filter_id"]); | |
190 | ||
191 | $cat_filter = sql_bool_to_bool($tmp_line["cat_filter"]); | |
192 | ||
02f3992a AD |
193 | if (!$tmp_line["match_on"]) { |
194 | if ($cat_filter && $tmp_line["cat_id"] || $tmp_line["feed_id"]) { | |
195 | $tmp_line["feed"] = Feeds::getFeedTitle( | |
196 | $cat_filter ? $tmp_line["cat_id"] : $tmp_line["feed_id"], | |
197 | $cat_filter); | |
198 | } else { | |
199 | $tmp_line["feed"] = ""; | |
200 | } | |
201 | } else { | |
202 | $match = []; | |
203 | foreach (json_decode($tmp_line["match_on"], true) as $feed_id) { | |
204 | ||
205 | if (strpos($feed_id, "CAT:") === 0) { | |
206 | $feed_id = (int)substr($feed_id, 4); | |
207 | if ($feed_id) { | |
208 | array_push($match, [Feeds::getCategoryTitle($feed_id), true, false]); | |
209 | } else { | |
210 | array_push($match, [0, true, true]); | |
211 | } | |
212 | } else { | |
213 | if ($feed_id) { | |
214 | array_push($match, [Feeds::getFeedTitle((int)$feed_id), false, false]); | |
215 | } else { | |
216 | array_push($match, [0, false, true]); | |
217 | } | |
218 | } | |
219 | } | |
220 | ||
221 | $tmp_line["match"] = $match; | |
222 | unset($tmp_line["match_on"]); | |
223 | } | |
fd994f1a | 224 | |
8592a8e3 | 225 | $tmp_line["cat_filter"] = sql_bool_to_bool($tmp_line["cat_filter"]); |
afa1a260 | 226 | $tmp_line["inverse"] = sql_bool_to_bool($tmp_line["inverse"]); |
8592a8e3 | 227 | |
fd994f1a AD |
228 | unset($tmp_line["feed_id"]); |
229 | unset($tmp_line["cat_id"]); | |
230 | ||
231 | array_push($line["rules"], $tmp_line); | |
232 | } | |
233 | ||
c949a928 AD |
234 | $tmph = $this->pdo->prepare("SELECT * FROM ttrss_filters2_actions |
235 | WHERE filter_id = ?"); | |
236 | $tmph->execute([$line['id']]); | |
fd994f1a | 237 | |
c949a928 | 238 | while ($tmp_line = $tmph->fetch()) { |
fd994f1a AD |
239 | unset($tmp_line["id"]); |
240 | unset($tmp_line["filter_id"]); | |
241 | ||
242 | array_push($line["actions"], $tmp_line); | |
243 | } | |
244 | ||
245 | unset($line["id"]); | |
246 | unset($line["owner_uid"]); | |
3f9de6cc AD |
247 | $filter = json_encode($line); |
248 | ||
dd0a17b0 | 249 | $out .= "<outline filter-type=\"2\"><![CDATA[$filter]]></outline>"; |
3f9de6cc AD |
250 | |
251 | } | |
252 | ||
253 | ||
fd994f1a | 254 | $out .= "</outline>"; |
3f9de6cc AD |
255 | } |
256 | ||
257 | $out .= "</body></opml>"; | |
258 | ||
259 | // Format output. | |
260 | $doc = new DOMDocument(); | |
261 | $doc->formatOutput = true; | |
262 | $doc->preserveWhiteSpace = false; | |
263 | $doc->loadXML($out); | |
264 | ||
265 | $xpath = new DOMXpath($doc); | |
266 | $outlines = $xpath->query("//outline[@title]"); | |
267 | ||
268 | // cleanup empty categories | |
269 | foreach ($outlines as $node) { | |
270 | if ($node->getElementsByTagName('outline')->length == 0) | |
271 | $node->parentNode->removeChild($node); | |
272 | } | |
273 | ||
274 | $res = $doc->saveXML(); | |
275 | ||
dd0a17b0 | 276 | /* // saveXML uses a two-space indent. Change to tabs. |
3f9de6cc AD |
277 | $res = preg_replace_callback('/^(?: )+/mu', |
278 | create_function( | |
279 | '$matches', | |
280 | 'return str_repeat("\t", intval(strlen($matches[0])/2));'), | |
dd0a17b0 | 281 | $res); */ |
3f9de6cc AD |
282 | |
283 | print $res; | |
284 | } | |
285 | ||
286 | // Import | |
287 | ||
7b55001e | 288 | private function opml_import_feed($node, $cat_id, $owner_uid) { |
3f9de6cc AD |
289 | $attrs = $node->attributes; |
290 | ||
c949a928 AD |
291 | $feed_title = mb_substr($attrs->getNamedItem('text')->nodeValue, 0, 250); |
292 | if (!$feed_title) $feed_title = mb_substr($attrs->getNamedItem('title')->nodeValue, 0, 250); | |
3f9de6cc | 293 | |
c949a928 AD |
294 | $feed_url = $attrs->getNamedItem('xmlUrl')->nodeValue; |
295 | if (!$feed_url) $feed_url = $attrs->getNamedItem('xmlURL')->nodeValue; | |
3f9de6cc | 296 | |
c949a928 | 297 | $site_url = mb_substr($attrs->getNamedItem('htmlUrl')->nodeValue, 0, 250); |
3f9de6cc AD |
298 | |
299 | if ($feed_url && $feed_title) { | |
c949a928 AD |
300 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE |
301 | feed_url = ? AND owner_uid = ?"); | |
302 | $sth->execute([$feed_url, $owner_uid]); | |
3f9de6cc | 303 | |
c949a928 | 304 | if (!$sth->fetch()) { |
3f9de6cc AD |
305 | #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id"); |
306 | $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title)); | |
307 | ||
c949a928 | 308 | if (!$cat_id) $cat_id = null; |
dd0a17b0 | 309 | |
c949a928 | 310 | $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds |
3f9de6cc | 311 | (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES |
c949a928 AD |
312 | (?, ?, ?, ?, ?, 0)"); |
313 | ||
314 | $sth->execute([$feed_title, $feed_url, $owner_uid, $cat_id, $site_url]); | |
3f9de6cc AD |
315 | |
316 | } else { | |
317 | $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title)); | |
318 | } | |
319 | } | |
320 | } | |
321 | ||
7b55001e | 322 | private function opml_import_label($node, $owner_uid) { |
3f9de6cc | 323 | $attrs = $node->attributes; |
c949a928 | 324 | $label_name = $attrs->getNamedItem('label-name')->nodeValue; |
3f9de6cc AD |
325 | |
326 | if ($label_name) { | |
c949a928 AD |
327 | $fg_color = $attrs->getNamedItem('label-fg-color')->nodeValue; |
328 | $bg_color = $attrs->getNamedItem('label-bg-color')->nodeValue; | |
3f9de6cc | 329 | |
7c9b5a3f | 330 | if (!Labels::find_id($label_name, $_SESSION['uid'])) { |
3f9de6cc | 331 | $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name))); |
7c9b5a3f | 332 | Labels::create($label_name, $fg_color, $bg_color, $owner_uid); |
3f9de6cc AD |
333 | } else { |
334 | $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name))); | |
335 | } | |
336 | } | |
337 | } | |
338 | ||
7b55001e | 339 | private function opml_import_preference($node) { |
3f9de6cc | 340 | $attrs = $node->attributes; |
c949a928 | 341 | $pref_name = $attrs->getNamedItem('pref-name')->nodeValue; |
3f9de6cc AD |
342 | |
343 | if ($pref_name) { | |
c949a928 | 344 | $pref_value = $attrs->getNamedItem('value')->nodeValue; |
3f9de6cc AD |
345 | |
346 | $this->opml_notice(T_sprintf("Setting preference key %s to %s", | |
347 | $pref_name, $pref_value)); | |
348 | ||
a42c55f0 | 349 | set_pref($pref_name, $pref_value); |
3f9de6cc AD |
350 | } |
351 | } | |
352 | ||
7b55001e | 353 | private function opml_import_filter($node) { |
3f9de6cc AD |
354 | $attrs = $node->attributes; |
355 | ||
c949a928 | 356 | $filter_type = $attrs->getNamedItem('filter-type')->nodeValue; |
3f9de6cc | 357 | |
3b003f63 AD |
358 | if ($filter_type == '2') { |
359 | $filter = json_decode($node->nodeValue, true); | |
3f9de6cc AD |
360 | |
361 | if ($filter) { | |
3b003f63 AD |
362 | $match_any_rule = bool_to_sql_bool($filter["match_any_rule"]); |
363 | $enabled = bool_to_sql_bool($filter["enabled"]); | |
a1495542 | 364 | $inverse = bool_to_sql_bool($filter["inverse"]); |
c949a928 AD |
365 | $title = $filter["title"]; |
366 | ||
7ccb4e91 AD |
367 | print "F: $title, $inverse, $enabled, $match_any_rule"; |
368 | ||
c949a928 AD |
369 | $sth = $this->pdo->prepare("INSERT INTO ttrss_filters2 (match_any_rule,enabled,inverse,title,owner_uid) |
370 | VALUES (?, ?, ?, ?, ?)"); | |
3f9de6cc | 371 | |
c949a928 | 372 | $sth->execute([$match_any_rule, $enabled, $inverse, $title, $_SESSION['uid']]); |
3f9de6cc | 373 | |
c949a928 AD |
374 | $sth = $this->pdo->prepare("SELECT MAX(id) AS id FROM ttrss_filters2 WHERE |
375 | owner_uid = ?"); | |
376 | $sth->execute([$_SESSION['uid']]); | |
3b003f63 | 377 | |
c949a928 AD |
378 | $row = $sth->fetch(); |
379 | $filter_id = $row['id']; | |
3b003f63 AD |
380 | |
381 | if ($filter_id) { | |
382 | $this->opml_notice(T_sprintf("Adding filter...")); | |
383 | ||
384 | foreach ($filter["rules"] as $rule) { | |
c949a928 AD |
385 | $feed_id = null; |
386 | $cat_id = null; | |
3f9de6cc | 387 | |
02f3992a AD |
388 | if ($rule["match"]) { |
389 | ||
390 | $match_on = []; | |
391 | ||
392 | foreach ($rule["match"] as $match) { | |
393 | list ($name, $is_cat, $is_id) = $match; | |
394 | ||
395 | if ($is_id) { | |
396 | array_push($match_on, ($is_cat ? "CAT:" : "") . $name); | |
397 | } else { | |
398 | ||
399 | $match_id = false; | |
400 | ||
401 | if (!$is_cat) { | |
c949a928 AD |
402 | $tsth = $this->pdo->prepare("SELECT id FROM ttrss_feeds |
403 | WHERE title = ? AND owner_uid = ?"); | |
404 | ||
405 | $tsth->execute([$name, $_SESSION['uid']]); | |
406 | ||
407 | if ($row = $tsth->fetch()) { | |
408 | $match_id = $row['id']; | |
02f3992a AD |
409 | } |
410 | } else { | |
c949a928 AD |
411 | $tsth = $this->pdo->prepare("SELECT id FROM ttrss_feed_categories |
412 | WHERE title = ? AND owner_uid = ?"); | |
413 | $tsth->execute([$name, $_SESSION['uid']]); | |
02f3992a | 414 | |
c949a928 AD |
415 | if ($row = $tsth->fetch()) { |
416 | $match_id = $row['id']; | |
417 | } | |
02f3992a AD |
418 | } |
419 | ||
420 | if ($match_id) array_push($match_on, $match_id); | |
421 | } | |
422 | } | |
423 | ||
c949a928 | 424 | $reg_exp = $rule["reg_exp"]; |
02f3992a AD |
425 | $filter_type = (int)$rule["filter_type"]; |
426 | $inverse = bool_to_sql_bool($rule["inverse"]); | |
c949a928 | 427 | $match_on = json_encode($match_on); |
02f3992a | 428 | |
c949a928 AD |
429 | $usth = $this->pdo->prepare("INSERT INTO ttrss_filters2_rules |
430 | (feed_id,cat_id,match_on,filter_id,filter_type,reg_exp,cat_filter,inverse) | |
431 | VALUES | |
432 | (NULL, NULL, ?, ?, ?, ?, false, ?)"); | |
433 | $usth->execute([$match_on, $filter_id, $filter_type, $reg_exp, $inverse]); | |
02f3992a AD |
434 | |
435 | } else { | |
436 | ||
437 | if (!$rule["cat_filter"]) { | |
c949a928 AD |
438 | $tsth = $this->pdo->prepare("SELECT id FROM ttrss_feeds |
439 | WHERE title = ? AND owner_uid = ?"); | |
440 | ||
441 | $tsth->execute([$rule['feed'], $_SESSION['uid']]); | |
442 | ||
443 | if ($row = $tsth->fetch()) { | |
444 | $feed_id = $row['id']; | |
02f3992a AD |
445 | } |
446 | } else { | |
c949a928 AD |
447 | $tsth = $this->pdo->prepare("SELECT id FROM ttrss_feed_categories |
448 | WHERE title = ? AND owner_uid = ?"); | |
02f3992a | 449 | |
c949a928 AD |
450 | $tsth->execute([$rule['feed'], $_SESSION['uid']]); |
451 | ||
452 | if ($row = $tsth->fetch()) { | |
453 | $feed_id = $row['id']; | |
454 | } | |
02f3992a AD |
455 | } |
456 | ||
457 | $cat_filter = bool_to_sql_bool($rule["cat_filter"]); | |
c949a928 | 458 | $reg_exp = $rule["reg_exp"]; |
02f3992a AD |
459 | $filter_type = (int)$rule["filter_type"]; |
460 | $inverse = bool_to_sql_bool($rule["inverse"]); | |
461 | ||
c949a928 AD |
462 | $usth = $this->pdo->prepare("INSERT INTO ttrss_filters2_rules |
463 | (feed_id,cat_id,filter_id,filter_type,reg_exp,cat_filter,inverse) | |
464 | VALUES | |
465 | (?, ?, ?, ?, ?, ?, ?)"); | |
466 | $usth->execute([$feed_id, $cat_id, $filter_id, $filter_type, $reg_exp, $cat_filter, $inverse]); | |
02f3992a | 467 | } |
3b003f63 | 468 | } |
3f9de6cc | 469 | |
3b003f63 | 470 | foreach ($filter["actions"] as $action) { |
3f9de6cc | 471 | |
3b003f63 | 472 | $action_id = (int)$action["action_id"]; |
c949a928 | 473 | $action_param = $action["action_param"]; |
3f9de6cc | 474 | |
c949a928 AD |
475 | $usth = $this->pdo->prepare("INSERT INTO ttrss_filters2_actions |
476 | (filter_id,action_id,action_param) | |
477 | VALUES | |
478 | (?, ?, ?)"); | |
479 | $usth->execute([$filter_id, $action_id, $action_param]); | |
3b003f63 | 480 | } |
3f9de6cc AD |
481 | } |
482 | } | |
483 | } | |
3b003f63 | 484 | } |
3f9de6cc AD |
485 | |
486 | private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) { | |
e60d5b0a | 487 | $default_cat_id = (int) $this->get_feed_category('Imported feeds', false); |
3f9de6cc AD |
488 | |
489 | if ($root_node) { | |
c949a928 | 490 | $cat_title = mb_substr($root_node->attributes->getNamedItem('text')->nodeValue, 0, 250); |
3f9de6cc | 491 | |
cc616ea1 | 492 | if (!$cat_title) |
c949a928 | 493 | $cat_title = mb_substr($root_node->attributes->getNamedItem('title')->nodeValue, 0, 250); |
cc616ea1 | 494 | |
3f9de6cc | 495 | if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) { |
e60d5b0a | 496 | $cat_id = $this->get_feed_category($cat_title, $parent_id); |
c949a928 | 497 | |
3f9de6cc | 498 | if ($cat_id === false) { |
a42c55f0 | 499 | add_feed_category($cat_title, $parent_id); |
e60d5b0a | 500 | $cat_id = $this->get_feed_category($cat_title, $parent_id); |
3f9de6cc | 501 | } |
c949a928 | 502 | |
3f9de6cc AD |
503 | } else { |
504 | $cat_id = 0; | |
505 | } | |
506 | ||
507 | $outlines = $root_node->childNodes; | |
508 | ||
509 | } else { | |
510 | $xpath = new DOMXpath($doc); | |
511 | $outlines = $xpath->query("//opml/body/outline"); | |
512 | ||
513 | $cat_id = 0; | |
514 | } | |
515 | ||
516 | #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id"); | |
517 | $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized"))); | |
518 | ||
519 | foreach ($outlines as $node) { | |
520 | if ($node->hasAttributes() && strtolower($node->tagName) == "outline") { | |
521 | $attrs = $node->attributes; | |
c949a928 | 522 | $node_cat_title = $attrs->getNamedItem('text')->nodeValue; |
cc616ea1 AD |
523 | |
524 | if (!$node_cat_title) | |
c949a928 | 525 | $node_cat_title = $attrs->getNamedItem('title')->nodeValue; |
cc616ea1 | 526 | |
c949a928 | 527 | $node_feed_url = $attrs->getNamedItem('xmlUrl')->nodeValue; |
3f9de6cc AD |
528 | |
529 | if ($node_cat_title && !$node_feed_url) { | |
530 | $this->opml_import_category($doc, $node, $owner_uid, $cat_id); | |
531 | } else { | |
532 | ||
533 | if (!$cat_id) { | |
534 | $dst_cat_id = $default_cat_id; | |
535 | } else { | |
536 | $dst_cat_id = $cat_id; | |
537 | } | |
538 | ||
539 | switch ($cat_title) { | |
540 | case "tt-rss-prefs": | |
7b55001e | 541 | $this->opml_import_preference($node); |
3f9de6cc AD |
542 | break; |
543 | case "tt-rss-labels": | |
7b55001e | 544 | $this->opml_import_label($node, $owner_uid); |
3f9de6cc AD |
545 | break; |
546 | case "tt-rss-filters": | |
7b55001e | 547 | $this->opml_import_filter($node); |
3f9de6cc AD |
548 | break; |
549 | default: | |
7b55001e | 550 | $this->opml_import_feed($node, $dst_cat_id, $owner_uid); |
3f9de6cc AD |
551 | } |
552 | } | |
553 | } | |
554 | } | |
555 | } | |
556 | ||
557 | function opml_import($owner_uid) { | |
558 | if (!$owner_uid) return; | |
559 | ||
3f9de6cc AD |
560 | $doc = false; |
561 | ||
3306daec AD |
562 | if ($_FILES['opml_file']['error'] != 0) { |
563 | print_error(T_sprintf("Upload failed with error code %d", | |
564 | $_FILES['opml_file']['error'])); | |
565 | return; | |
566 | } | |
567 | ||
3306daec AD |
568 | if (is_uploaded_file($_FILES['opml_file']['tmp_name'])) { |
569 | $tmp_file = tempnam(CACHE_DIR . '/upload', 'opml'); | |
570 | ||
571 | $result = move_uploaded_file($_FILES['opml_file']['tmp_name'], | |
572 | $tmp_file); | |
573 | ||
574 | if (!$result) { | |
575 | print_error(__("Unable to move uploaded file.")); | |
576 | return; | |
577 | } | |
578 | } else { | |
579 | print_error(__('Error: please upload OPML file.')); | |
580 | return; | |
581 | } | |
582 | ||
583 | if (is_file($tmp_file)) { | |
98c39afc | 584 | $doc = new DOMDocument(); |
3457ce7c | 585 | libxml_disable_entity_loader(false); |
3306daec | 586 | $doc->load($tmp_file); |
3457ce7c | 587 | libxml_disable_entity_loader(true); |
3306daec | 588 | unlink($tmp_file); |
3f9de6cc | 589 | } else if (!$doc) { |
3306daec | 590 | print_error(__('Error: unable to find moved OPML file.')); |
3f9de6cc AD |
591 | return; |
592 | } | |
593 | ||
594 | if ($doc) { | |
c949a928 | 595 | $this->pdo->beginTransaction(); |
3f9de6cc | 596 | $this->opml_import_category($doc, false, $owner_uid, false); |
c949a928 | 597 | $this->pdo->commit(); |
3f9de6cc AD |
598 | } else { |
599 | print_error(__('Error while parsing document.')); | |
600 | } | |
601 | } | |
602 | ||
603 | private function opml_notice($msg) { | |
604 | print "$msg<br/>"; | |
605 | } | |
606 | ||
6322ac79 | 607 | static function opml_publish_url(){ |
50832719 AD |
608 | |
609 | $url_path = get_self_url_prefix(); | |
610 | $url_path .= "/opml.php?op=publish&key=" . | |
a42c55f0 | 611 | get_feed_access_key('OPML:Publish', false, $_SESSION["uid"]); |
50832719 AD |
612 | |
613 | return $url_path; | |
614 | } | |
615 | ||
e60d5b0a | 616 | function get_feed_category($feed_cat, $parent_cat_id = false) { |
e60d5b0a | 617 | |
c949a928 | 618 | $parent_cat_id = (int) $parent_cat_id; |
e60d5b0a | 619 | |
c949a928 AD |
620 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_feed_categories |
621 | WHERE title = :title | |
622 | AND (parent_cat = :parent OR (:parent = 0 AND parent_cat IS NULL)) | |
623 | AND owner_uid = :uid"); | |
624 | ||
625 | $sth->execute([':title' => $feed_cat, ':parent' => $parent_cat_id, ':uid' => $_SESSION['uid']]); | |
626 | ||
627 | if ($row = $sth->fetch()) { | |
628 | return $row['id']; | |
e60d5b0a | 629 | } else { |
c949a928 | 630 | return false; |
e60d5b0a AD |
631 | } |
632 | } | |
633 | ||
50832719 | 634 | |
3f9de6cc | 635 | } |