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