]>
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> | |
27 | <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\"> | |
28 | <title>".__("OPML Utility")."</title> | |
29 | <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> | |
30 | </head> | |
31 | <body> | |
32 | <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div> | |
33 | <h1>".__('OPML Utility')."</h1>"; | |
34 | ||
35 | add_feed_category($this->link, "Imported feeds"); | |
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 | ||
44 | print "</body></html>"; | |
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) { | |
69 | $result = db_query($this->link, "SELECT title FROM ttrss_feed_categories WHERE id = '$cat_id' | |
70 | AND owner_uid = '$owner_uid'"); | |
71 | $cat_title = db_fetch_result($result, 0, "title"); | |
72 | } | |
73 | ||
74 | if ($cat_title) $out .= "<outline title=\"$cat_title\">\n"; | |
75 | ||
76 | $result = db_query($this->link, "SELECT id,title | |
77 | FROM ttrss_feed_categories WHERE | |
78 | $cat_qpart AND owner_uid = '$owner_uid' ORDER BY order_id, title"); | |
79 | ||
80 | while ($line = db_fetch_assoc($result)) { | |
81 | $title = htmlspecialchars($line["title"]); | |
82 | $out .= $this->opml_export_category($owner_uid, $line["id"], $hide_private_feeds); | |
83 | } | |
84 | ||
85 | $feeds_result = db_query($this->link, "select title, feed_url, site_url | |
86 | from ttrss_feeds where $feed_cat_qpart AND owner_uid = '$owner_uid' AND $hide_qpart | |
87 | order by order_id, title"); | |
88 | ||
89 | while ($fline = db_fetch_assoc($feeds_result)) { | |
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 | ||
100 | $out .= "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n"; | |
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) { | |
132 | $out .= "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">"; | |
133 | ||
134 | $result = db_query($this->link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE | |
135 | profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name"); | |
136 | ||
137 | while ($line = db_fetch_assoc($result)) { | |
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 | ||
146 | $out .= "<outline title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION."\">"; | |
147 | ||
148 | $result = db_query($this->link, "SELECT * FROM ttrss_labels2 WHERE | |
149 | owner_uid = " . $_SESSION['uid']); | |
150 | ||
151 | while ($line = db_fetch_assoc($result)) { | |
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 | ||
fd994f1a | 162 | $out .= "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">"; |
3f9de6cc | 163 | |
fd994f1a AD |
164 | $result = db_query($this->link, "SELECT * FROM ttrss_filters2 |
165 | WHERE owner_uid = ".$_SESSION["uid"]." ORDER BY id"); | |
3f9de6cc | 166 | |
fd994f1a | 167 | while ($line = db_fetch_assoc($result)) { |
3f9de6cc AD |
168 | foreach (array('enabled', 'inverse', 'cat_filter') as $b) { |
169 | $line[$b] = sql_bool_to_bool($line[$b]); | |
170 | } | |
171 | ||
fd994f1a AD |
172 | $line["rules"] = array(); |
173 | $line["actions"] = array(); | |
174 | ||
175 | $tmp_result = db_query($this->link, "SELECT * FROM ttrss_filters2_rules | |
176 | WHERE filter_id = ".$line["id"]); | |
177 | ||
178 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
179 | unset($tmp_line["id"]); | |
180 | unset($tmp_line["filter_id"]); | |
181 | ||
182 | $cat_filter = sql_bool_to_bool($tmp_line["cat_filter"]); | |
183 | ||
184 | if ($cat_filter && $tmp_line["cat_id"] || $tmp_line["feed_id"]) { | |
185 | $tmp_line["feed"] = getFeedTitle($this->link, | |
186 | $cat_filter ? $tmp_line["cat_id"] : $tmp_line["feed_id"], | |
187 | $cat_filter); | |
188 | } else { | |
189 | $tmp_line["feed"] = ""; | |
190 | } | |
191 | ||
192 | unset($tmp_line["feed_id"]); | |
193 | unset($tmp_line["cat_id"]); | |
194 | ||
195 | array_push($line["rules"], $tmp_line); | |
196 | } | |
197 | ||
198 | $tmp_result = db_query($this->link, "SELECT * FROM ttrss_filters2_actions | |
199 | WHERE filter_id = ".$line["id"]); | |
200 | ||
201 | while ($tmp_line = db_fetch_assoc($tmp_result)) { | |
202 | unset($tmp_line["id"]); | |
203 | unset($tmp_line["filter_id"]); | |
204 | ||
205 | array_push($line["actions"], $tmp_line); | |
206 | } | |
207 | ||
208 | unset($line["id"]); | |
209 | unset($line["owner_uid"]); | |
3f9de6cc AD |
210 | $filter = json_encode($line); |
211 | ||
fd994f1a | 212 | $out .= "<outline filter-type=\"2\">$filter</outline>"; |
3f9de6cc AD |
213 | |
214 | } | |
215 | ||
216 | ||
fd994f1a | 217 | $out .= "</outline>"; |
3f9de6cc AD |
218 | } |
219 | ||
220 | $out .= "</body></opml>"; | |
221 | ||
222 | // Format output. | |
223 | $doc = new DOMDocument(); | |
224 | $doc->formatOutput = true; | |
225 | $doc->preserveWhiteSpace = false; | |
226 | $doc->loadXML($out); | |
227 | ||
228 | $xpath = new DOMXpath($doc); | |
229 | $outlines = $xpath->query("//outline[@title]"); | |
230 | ||
231 | // cleanup empty categories | |
232 | foreach ($outlines as $node) { | |
233 | if ($node->getElementsByTagName('outline')->length == 0) | |
234 | $node->parentNode->removeChild($node); | |
235 | } | |
236 | ||
237 | $res = $doc->saveXML(); | |
238 | ||
239 | // saveXML uses a two-space indent. Change to tabs. | |
240 | $res = preg_replace_callback('/^(?: )+/mu', | |
241 | create_function( | |
242 | '$matches', | |
243 | 'return str_repeat("\t", intval(strlen($matches[0])/2));'), | |
244 | $res); | |
245 | ||
246 | print $res; | |
247 | } | |
248 | ||
249 | // Import | |
250 | ||
251 | private function opml_import_feed($doc, $node, $cat_id, $owner_uid) { | |
252 | $attrs = $node->attributes; | |
253 | ||
254 | $feed_title = db_escape_string($attrs->getNamedItem('text')->nodeValue); | |
255 | if (!$feed_title) $feed_title = db_escape_string($attrs->getNamedItem('title')->nodeValue); | |
256 | ||
257 | $feed_url = db_escape_string($attrs->getNamedItem('xmlUrl')->nodeValue); | |
258 | if (!$feed_url) $feed_url = db_escape_string($attrs->getNamedItem('xmlURL')->nodeValue); | |
259 | ||
260 | $site_url = db_escape_string($attrs->getNamedItem('htmlUrl')->nodeValue); | |
261 | ||
262 | if ($feed_url && $feed_title) { | |
263 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
264 | feed_url = '$feed_url' AND owner_uid = '$owner_uid'"); | |
265 | ||
266 | if (db_num_rows($result) == 0) { | |
267 | #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id"); | |
268 | $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title)); | |
269 | ||
270 | $query = "INSERT INTO ttrss_feeds | |
271 | (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES | |
272 | ('$feed_title', '$feed_url', '$owner_uid', | |
273 | '$cat_id', '$site_url', 0)"; | |
274 | db_query($this->link, $query); | |
275 | ||
276 | } else { | |
277 | $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title)); | |
278 | } | |
279 | } | |
280 | } | |
281 | ||
282 | private function opml_import_label($doc, $node, $owner_uid) { | |
283 | $attrs = $node->attributes; | |
284 | $label_name = db_escape_string($attrs->getNamedItem('label-name')->nodeValue); | |
285 | ||
286 | if ($label_name) { | |
287 | $fg_color = db_escape_string($attrs->getNamedItem('label-fg-color')->nodeValue); | |
288 | $bg_color = db_escape_string($attrs->getNamedItem('label-bg-color')->nodeValue); | |
289 | ||
290 | if (!label_find_id($this->link, $label_name, $_SESSION['uid'])) { | |
291 | $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name))); | |
292 | label_create($this->link, $label_name, $fg_color, $bg_color); | |
293 | } else { | |
294 | $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name))); | |
295 | } | |
296 | } | |
297 | } | |
298 | ||
299 | private function opml_import_preference($doc, $node, $owner_uid) { | |
300 | $attrs = $node->attributes; | |
301 | $pref_name = db_escape_string($attrs->getNamedItem('pref-name')->nodeValue); | |
302 | ||
303 | if ($pref_name) { | |
304 | $pref_value = db_escape_string($attrs->getNamedItem('value')->nodeValue); | |
305 | ||
306 | $this->opml_notice(T_sprintf("Setting preference key %s to %s", | |
307 | $pref_name, $pref_value)); | |
308 | ||
309 | set_pref($this->link, $pref_name, $pref_value); | |
310 | } | |
311 | } | |
312 | ||
6aff7845 | 313 | /* private function opml_import_filter($doc, $node, $owner_uid) { |
3f9de6cc AD |
314 | $attrs = $node->attributes; |
315 | ||
316 | $filter_name = db_escape_string($attrs->getNamedItem('filter-name')->nodeValue); | |
317 | ||
318 | if ($filter_name) { | |
319 | ||
320 | $filter = json_decode($node->nodeValue, true); | |
321 | ||
322 | if ($filter) { | |
323 | $reg_exp = db_escape_string($filter['reg_exp']); | |
324 | $filter_type = (int)$filter['filter_type']; | |
325 | $action_id = (int)$filter['action_id']; | |
326 | ||
327 | $result = db_query($this->link, "SELECT id FROM ttrss_filters WHERE | |
328 | reg_exp = '$reg_exp' AND | |
329 | filter_type = '$filter_type' AND | |
330 | action_id = '$action_id' AND | |
331 | owner_uid = " .$_SESSION['uid']); | |
332 | ||
333 | if (db_num_rows($result) == 0) { | |
334 | $enabled = bool_to_sql_bool($filter['enabled']); | |
335 | $action_param = db_escape_string($filter['action_param']); | |
336 | $inverse = bool_to_sql_bool($filter['inverse']); | |
337 | $filter_param = db_escape_string($filter['filter_param']); | |
338 | $cat_filter = bool_to_sql_bool($filter['cat_filter']); | |
339 | ||
340 | $feed_url = db_escape_string($filter['feed_url']); | |
341 | $cat_title = db_escape_string($filter['cat_title']); | |
342 | ||
343 | $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE | |
344 | feed_url = '$feed_url' AND owner_uid = $owner_uid"); | |
345 | ||
346 | if (db_num_rows($result) != 0) { | |
347 | $feed_id = db_fetch_result($result, 0, "id"); | |
348 | } else { | |
349 | $feed_id = "NULL"; | |
350 | } | |
351 | ||
352 | $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE | |
353 | title = '$cat_title' AND owner_uid = $owner_uid"); | |
354 | ||
355 | if (db_num_rows($result) != 0) { | |
356 | $cat_id = db_fetch_result($result, 0, "id"); | |
357 | } else { | |
358 | $cat_id = "NULL"; | |
359 | } | |
360 | ||
361 | $this->opml_notice(T_sprintf("Adding filter %s", htmlspecialchars($reg_exp))); | |
362 | ||
363 | $query = "INSERT INTO ttrss_filters (filter_type, action_id, | |
364 | enabled, inverse, action_param, filter_param, | |
365 | cat_filter, feed_id, | |
366 | cat_id, reg_exp, | |
367 | owner_uid) | |
368 | VALUES ($filter_type, $action_id, | |
369 | $enabled, $inverse, '$action_param', '$filter_param', | |
370 | $cat_filter, $feed_id, | |
371 | $cat_id, '$reg_exp', ". | |
372 | $_SESSION['uid'].")"; | |
373 | ||
374 | db_query($this->link, $query); | |
375 | ||
376 | } else { | |
377 | $this->opml_notice(T_sprintf("Duplicate filter %s", htmlspecialchars($reg_exp))); | |
378 | } | |
379 | } | |
380 | } | |
6aff7845 | 381 | } */ |
3f9de6cc AD |
382 | |
383 | private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) { | |
384 | $body = $doc->getElementsByTagName('body'); | |
385 | ||
386 | $default_cat_id = (int) get_feed_category($this->link, 'Imported feeds', false); | |
387 | ||
388 | if ($root_node) { | |
389 | $cat_title = db_escape_string($root_node->attributes->getNamedItem('title')->nodeValue); | |
390 | ||
391 | if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) { | |
392 | $cat_id = get_feed_category($this->link, $cat_title, $parent_id); | |
393 | db_query($this->link, "BEGIN"); | |
394 | if ($cat_id === false) { | |
395 | add_feed_category($this->link, $cat_title, $parent_id); | |
396 | $cat_id = get_feed_category($this->link, $cat_title, $parent_id); | |
397 | } | |
398 | db_query($this->link, "COMMIT"); | |
399 | } else { | |
400 | $cat_id = 0; | |
401 | } | |
402 | ||
403 | $outlines = $root_node->childNodes; | |
404 | ||
405 | } else { | |
406 | $xpath = new DOMXpath($doc); | |
407 | $outlines = $xpath->query("//opml/body/outline"); | |
408 | ||
409 | $cat_id = 0; | |
410 | } | |
411 | ||
412 | #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id"); | |
413 | $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized"))); | |
414 | ||
415 | foreach ($outlines as $node) { | |
416 | if ($node->hasAttributes() && strtolower($node->tagName) == "outline") { | |
417 | $attrs = $node->attributes; | |
418 | $node_cat_title = db_escape_string($attrs->getNamedItem('title')->nodeValue); | |
419 | $node_feed_url = db_escape_string($attrs->getNamedItem('xmlUrl')->nodeValue); | |
420 | ||
421 | if ($node_cat_title && !$node_feed_url) { | |
422 | $this->opml_import_category($doc, $node, $owner_uid, $cat_id); | |
423 | } else { | |
424 | ||
425 | if (!$cat_id) { | |
426 | $dst_cat_id = $default_cat_id; | |
427 | } else { | |
428 | $dst_cat_id = $cat_id; | |
429 | } | |
430 | ||
431 | switch ($cat_title) { | |
432 | case "tt-rss-prefs": | |
433 | $this->opml_import_preference($doc, $node, $owner_uid); | |
434 | break; | |
435 | case "tt-rss-labels": | |
436 | $this->opml_import_label($doc, $node, $owner_uid); | |
437 | break; | |
438 | case "tt-rss-filters": | |
6aff7845 | 439 | //$this->opml_import_filter($doc, $node, $owner_uid); |
3f9de6cc AD |
440 | break; |
441 | default: | |
442 | $this->opml_import_feed($doc, $node, $dst_cat_id, $owner_uid); | |
443 | } | |
444 | } | |
445 | } | |
446 | } | |
447 | } | |
448 | ||
449 | function opml_import($owner_uid) { | |
450 | if (!$owner_uid) return; | |
451 | ||
452 | $debug = isset($_REQUEST["debug"]); | |
453 | $doc = false; | |
454 | ||
455 | #if ($debug) $doc = DOMDocument::load("/tmp/test.opml"); | |
456 | ||
457 | if (is_file($_FILES['opml_file']['tmp_name'])) { | |
458 | $doc = DOMDocument::load($_FILES['opml_file']['tmp_name']); | |
459 | } else if (!$doc) { | |
460 | print_error(__('Error: please upload OPML file.')); | |
461 | return; | |
462 | } | |
463 | ||
464 | if ($doc) { | |
465 | $this->opml_import_category($doc, false, $owner_uid, false); | |
466 | } else { | |
467 | print_error(__('Error while parsing document.')); | |
468 | } | |
469 | } | |
470 | ||
471 | private function opml_notice($msg) { | |
472 | print "$msg<br/>"; | |
473 | } | |
474 | ||
475 | } | |
476 | ?> |