]> git.wh0rd.org - tt-rss.git/blob - classes/opml.php
8683fb73a33a86b7a70fb8c6681d781ae4480a6f
[tt-rss.git] / classes / opml.php
1 <?php
2 class Opml extends Handler_Protected {
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
162 $out .= "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">";
163
164 $result = db_query($this->link, "SELECT filter_type,
165 reg_exp,
166 action_id,
167 enabled,
168 action_param,
169 inverse,
170 filter_param,
171 cat_filter,
172 ttrss_feeds.feed_url AS feed_url,
173 ttrss_feed_categories.title AS cat_title
174 FROM ttrss_filters
175 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
176 LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
177 WHERE
178 ttrss_filters.owner_uid = " . $_SESSION['uid']);
179
180 while ($line = db_fetch_assoc($result)) {
181 $name = htmlspecialchars($line['reg_exp']);
182
183 foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
184 $line[$b] = sql_bool_to_bool($line[$b]);
185 }
186
187 $filter = json_encode($line);
188
189 $out .= "<outline filter-name=\"$name\">$filter</outline>";
190
191 }
192
193
194 $out .= "</outline>";
195 }
196
197 $out .= "</body></opml>";
198
199 // Format output.
200 $doc = new DOMDocument();
201 $doc->formatOutput = true;
202 $doc->preserveWhiteSpace = false;
203 $doc->loadXML($out);
204
205 $xpath = new DOMXpath($doc);
206 $outlines = $xpath->query("//outline[@title]");
207
208 // cleanup empty categories
209 foreach ($outlines as $node) {
210 if ($node->getElementsByTagName('outline')->length == 0)
211 $node->parentNode->removeChild($node);
212 }
213
214 $res = $doc->saveXML();
215
216 // saveXML uses a two-space indent. Change to tabs.
217 $res = preg_replace_callback('/^(?: )+/mu',
218 create_function(
219 '$matches',
220 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
221 $res);
222
223 print $res;
224 }
225
226 // Import
227
228 private function opml_import_feed($doc, $node, $cat_id, $owner_uid) {
229 $attrs = $node->attributes;
230
231 $feed_title = db_escape_string($attrs->getNamedItem('text')->nodeValue);
232 if (!$feed_title) $feed_title = db_escape_string($attrs->getNamedItem('title')->nodeValue);
233
234 $feed_url = db_escape_string($attrs->getNamedItem('xmlUrl')->nodeValue);
235 if (!$feed_url) $feed_url = db_escape_string($attrs->getNamedItem('xmlURL')->nodeValue);
236
237 $site_url = db_escape_string($attrs->getNamedItem('htmlUrl')->nodeValue);
238
239 if ($feed_url && $feed_title) {
240 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
241 feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
242
243 if (db_num_rows($result) == 0) {
244 #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id");
245 $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title));
246
247 $query = "INSERT INTO ttrss_feeds
248 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
249 ('$feed_title', '$feed_url', '$owner_uid',
250 '$cat_id', '$site_url', 0)";
251 db_query($this->link, $query);
252
253 } else {
254 $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title));
255 }
256 }
257 }
258
259 private function opml_import_label($doc, $node, $owner_uid) {
260 $attrs = $node->attributes;
261 $label_name = db_escape_string($attrs->getNamedItem('label-name')->nodeValue);
262
263 if ($label_name) {
264 $fg_color = db_escape_string($attrs->getNamedItem('label-fg-color')->nodeValue);
265 $bg_color = db_escape_string($attrs->getNamedItem('label-bg-color')->nodeValue);
266
267 if (!label_find_id($this->link, $label_name, $_SESSION['uid'])) {
268 $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name)));
269 label_create($this->link, $label_name, $fg_color, $bg_color);
270 } else {
271 $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)));
272 }
273 }
274 }
275
276 private function opml_import_preference($doc, $node, $owner_uid) {
277 $attrs = $node->attributes;
278 $pref_name = db_escape_string($attrs->getNamedItem('pref-name')->nodeValue);
279
280 if ($pref_name) {
281 $pref_value = db_escape_string($attrs->getNamedItem('value')->nodeValue);
282
283 $this->opml_notice(T_sprintf("Setting preference key %s to %s",
284 $pref_name, $pref_value));
285
286 set_pref($this->link, $pref_name, $pref_value);
287 }
288 }
289
290 private function opml_import_filter($doc, $node, $owner_uid) {
291 $attrs = $node->attributes;
292
293 $filter_name = db_escape_string($attrs->getNamedItem('filter-name')->nodeValue);
294
295 if ($filter_name) {
296
297 $filter = json_decode($node->nodeValue, true);
298
299 if ($filter) {
300 $reg_exp = db_escape_string($filter['reg_exp']);
301 $filter_type = (int)$filter['filter_type'];
302 $action_id = (int)$filter['action_id'];
303
304 $result = db_query($this->link, "SELECT id FROM ttrss_filters WHERE
305 reg_exp = '$reg_exp' AND
306 filter_type = '$filter_type' AND
307 action_id = '$action_id' AND
308 owner_uid = " .$_SESSION['uid']);
309
310 if (db_num_rows($result) == 0) {
311 $enabled = bool_to_sql_bool($filter['enabled']);
312 $action_param = db_escape_string($filter['action_param']);
313 $inverse = bool_to_sql_bool($filter['inverse']);
314 $filter_param = db_escape_string($filter['filter_param']);
315 $cat_filter = bool_to_sql_bool($filter['cat_filter']);
316
317 $feed_url = db_escape_string($filter['feed_url']);
318 $cat_title = db_escape_string($filter['cat_title']);
319
320 $result = db_query($this->link, "SELECT id FROM ttrss_feeds WHERE
321 feed_url = '$feed_url' AND owner_uid = $owner_uid");
322
323 if (db_num_rows($result) != 0) {
324 $feed_id = db_fetch_result($result, 0, "id");
325 } else {
326 $feed_id = "NULL";
327 }
328
329 $result = db_query($this->link, "SELECT id FROM ttrss_feed_categories WHERE
330 title = '$cat_title' AND owner_uid = $owner_uid");
331
332 if (db_num_rows($result) != 0) {
333 $cat_id = db_fetch_result($result, 0, "id");
334 } else {
335 $cat_id = "NULL";
336 }
337
338 $this->opml_notice(T_sprintf("Adding filter %s", htmlspecialchars($reg_exp)));
339
340 $query = "INSERT INTO ttrss_filters (filter_type, action_id,
341 enabled, inverse, action_param, filter_param,
342 cat_filter, feed_id,
343 cat_id, reg_exp,
344 owner_uid)
345 VALUES ($filter_type, $action_id,
346 $enabled, $inverse, '$action_param', '$filter_param',
347 $cat_filter, $feed_id,
348 $cat_id, '$reg_exp', ".
349 $_SESSION['uid'].")";
350
351 db_query($this->link, $query);
352
353 } else {
354 $this->opml_notice(T_sprintf("Duplicate filter %s", htmlspecialchars($reg_exp)));
355 }
356 }
357 }
358 }
359
360 private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) {
361 $body = $doc->getElementsByTagName('body');
362
363 $default_cat_id = (int) get_feed_category($this->link, 'Imported feeds', false);
364
365 if ($root_node) {
366 $cat_title = db_escape_string($root_node->attributes->getNamedItem('title')->nodeValue);
367
368 if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) {
369 $cat_id = get_feed_category($this->link, $cat_title, $parent_id);
370 db_query($this->link, "BEGIN");
371 if ($cat_id === false) {
372 add_feed_category($this->link, $cat_title, $parent_id);
373 $cat_id = get_feed_category($this->link, $cat_title, $parent_id);
374 }
375 db_query($this->link, "COMMIT");
376 } else {
377 $cat_id = 0;
378 }
379
380 $outlines = $root_node->childNodes;
381
382 } else {
383 $xpath = new DOMXpath($doc);
384 $outlines = $xpath->query("//opml/body/outline");
385
386 $cat_id = 0;
387 }
388
389 #$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id");
390 $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized")));
391
392 foreach ($outlines as $node) {
393 if ($node->hasAttributes() && strtolower($node->tagName) == "outline") {
394 $attrs = $node->attributes;
395 $node_cat_title = db_escape_string($attrs->getNamedItem('title')->nodeValue);
396 $node_feed_url = db_escape_string($attrs->getNamedItem('xmlUrl')->nodeValue);
397
398 if ($node_cat_title && !$node_feed_url) {
399 $this->opml_import_category($doc, $node, $owner_uid, $cat_id);
400 } else {
401
402 if (!$cat_id) {
403 $dst_cat_id = $default_cat_id;
404 } else {
405 $dst_cat_id = $cat_id;
406 }
407
408 switch ($cat_title) {
409 case "tt-rss-prefs":
410 $this->opml_import_preference($doc, $node, $owner_uid);
411 break;
412 case "tt-rss-labels":
413 $this->opml_import_label($doc, $node, $owner_uid);
414 break;
415 case "tt-rss-filters":
416 $this->opml_import_filter($doc, $node, $owner_uid);
417 break;
418 default:
419 $this->opml_import_feed($doc, $node, $dst_cat_id, $owner_uid);
420 }
421 }
422 }
423 }
424 }
425
426 function opml_import($owner_uid) {
427 if (!$owner_uid) return;
428
429 $debug = isset($_REQUEST["debug"]);
430 $doc = false;
431
432 #if ($debug) $doc = DOMDocument::load("/tmp/test.opml");
433
434 if (is_file($_FILES['opml_file']['tmp_name'])) {
435 $doc = DOMDocument::load($_FILES['opml_file']['tmp_name']);
436 } else if (!$doc) {
437 print_error(__('Error: please upload OPML file.'));
438 return;
439 }
440
441 if ($doc) {
442 $this->opml_import_category($doc, false, $owner_uid, false);
443 } else {
444 print_error(__('Error while parsing document.'));
445 }
446 }
447
448 private function opml_notice($msg) {
449 print "$msg<br/>";
450 }
451
452 }
453 ?>