]> git.wh0rd.org - tt-rss.git/blame - opml.php
opml: some indenting stuff
[tt-rss.git] / opml.php
CommitLineData
1d3a17c7 1<?php
bbefea90 2 set_include_path(get_include_path() . PATH_SEPARATOR .
f03a795d 3 dirname(__FILE__) . "/include");
107d0cf3 4
fb074239 5 require_once "functions.php";
1559e374 6 require_once "sessions.php";
66581886 7 require_once "sanity_check.php";
9a4506c8 8 require_once "config.php";
8158c57a 9 require_once "db.php";
a0111294 10 require_once "db-prefs.php";
9a4506c8 11
0a13e84e 12 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
9f311df6 13
5f0a3741 14 if (!init_connection($link)) return;
4e9f5c24 15
d2a317e3
AD
16 function opml_import_feed($link, $doc, $node, $cat_id, $owner_uid) {
17 $attrs = $node->attributes;
80edb8b4 18
d2a317e3
AD
19 $feed_title = db_escape_string($attrs->getNamedItem('text')->nodeValue);
20 if (!$feed_title) $feed_title = db_escape_string($attrs->getNamedItem('title')->nodeValue);
80edb8b4 21
d2a317e3
AD
22 $feed_url = db_escape_string($attrs->getNamedItem('xmlUrl')->nodeValue);
23 if (!$feed_url) $feed_url = db_escape_string($attrs->getNamedItem('xmlURL')->nodeValue);
80edb8b4 24
d2a317e3 25 $site_url = db_escape_string($attrs->getNamedItem('htmlUrl')->nodeValue);
80edb8b4 26
d2a317e3
AD
27 if ($feed_url && $feed_title) {
28 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
29 feed_url = '$feed_url' AND owner_uid = '$owner_uid'");
80edb8b4 30
d2a317e3
AD
31 if (db_num_rows($result) == 0) {
32 #opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id");
33 opml_notice(T_sprintf("Adding feed: %s", $feed_title));
80edb8b4 34
d2a317e3
AD
35 $query = "INSERT INTO ttrss_feeds
36 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
37 ('$feed_title', '$feed_url', '$owner_uid',
38 '$cat_id', '$site_url', 0)";
39 db_query($link, $query);
80edb8b4 40
d2a317e3
AD
41 } else {
42 opml_notice(T_sprintf("Duplicate feed: %s", $feed_title));
43 }
44 }
45 }
80edb8b4 46
d2a317e3
AD
47 function opml_import_label($link, $doc, $node, $owner_uid) {
48 $attrs = $node->attributes;
49 $label_name = db_escape_string($attrs->getNamedItem('label-name')->nodeValue);
80edb8b4 50
d2a317e3
AD
51 if ($label_name) {
52 $fg_color = db_escape_string($attrs->getNamedItem('label-fg-color')->nodeValue);
53 $bg_color = db_escape_string($attrs->getNamedItem('label-bg-color')->nodeValue);
80edb8b4 54
d2a317e3
AD
55 if (!label_find_id($link, $label_name, $_SESSION['uid'])) {
56 opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name)));
57 label_create($link, $label_name, $fg_color, $bg_color);
58 } else {
59 opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)));
60 }
61 }
62 }
80edb8b4 63
d2a317e3
AD
64 function opml_import_preference($link, $doc, $node, $owner_uid) {
65 $attrs = $node->attributes;
66 $pref_name = db_escape_string($attrs->getNamedItem('pref-name')->nodeValue);
80edb8b4 67
d2a317e3
AD
68 if ($pref_name) {
69 $pref_value = db_escape_string($attrs->getNamedItem('value')->nodeValue);
80edb8b4 70
d2a317e3
AD
71 opml_notice(T_sprintf("Setting preference key %s to %s",
72 $pref_name, $pref_value));
80edb8b4 73
d2a317e3
AD
74 set_pref($link, $pref_name, $pref_value);
75 }
76 }
80edb8b4 77
d2a317e3
AD
78 function opml_import_filter($link, $doc, $node, $owner_uid) {
79 $attrs = $node->attributes;
80edb8b4 80
d2a317e3 81 $filter_name = db_escape_string($attrs->getNamedItem('filter-name')->nodeValue);
80edb8b4 82
d2a317e3 83 if ($filter_name) {
80edb8b4 84
d2a317e3 85 $filter = json_decode($node->nodeValue, true);
80edb8b4 86
d2a317e3
AD
87 if ($filter) {
88 $reg_exp = db_escape_string($filter['reg_exp']);
89 $filter_type = (int)$filter['filter_type'];
90 $action_id = (int)$filter['action_id'];
80edb8b4 91
d2a317e3
AD
92 $result = db_query($link, "SELECT id FROM ttrss_filters WHERE
93 reg_exp = '$reg_exp' AND
94 filter_type = '$filter_type' AND
95 action_id = '$action_id' AND
96 owner_uid = " .$_SESSION['uid']);
80edb8b4 97
d2a317e3
AD
98 if (db_num_rows($result) == 0) {
99 $enabled = bool_to_sql_bool($filter['enabled']);
100 $action_param = db_escape_string($filter['action_param']);
101 $inverse = bool_to_sql_bool($filter['inverse']);
102 $filter_param = db_escape_string($filter['filter_param']);
103 $cat_filter = bool_to_sql_bool($filter['cat_filter']);
80edb8b4 104
d2a317e3
AD
105 $feed_url = db_escape_string($filter['feed_url']);
106 $cat_title = db_escape_string($filter['cat_title']);
80edb8b4 107
d2a317e3
AD
108 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
109 feed_url = '$feed_url' AND owner_uid = ".$_SESSION['uid']);
80edb8b4 110
d2a317e3
AD
111 if (db_num_rows($result) != 0) {
112 $feed_id = db_fetch_result($result, 0, "id");
113 } else {
114 $feed_id = "NULL";
115 }
80edb8b4 116
d2a317e3
AD
117 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
118 title = '$cat_title' AND owner_uid = ".$_SESSION['uid']);
80edb8b4 119
d2a317e3
AD
120 if (db_num_rows($result) != 0) {
121 $cat_id = db_fetch_result($result, 0, "id");
122 } else {
123 $cat_id = "NULL";
80edb8b4
AD
124 }
125
d2a317e3 126 opml_notice(T_sprintf("Adding filter %s", htmlspecialchars($reg_exp)));
bbefea90 127
d2a317e3
AD
128 $query = "INSERT INTO ttrss_filters (filter_type, action_id,
129 enabled, inverse, action_param, filter_param,
130 cat_filter, feed_id,
131 cat_id, reg_exp,
132 owner_uid)
133 VALUES ($filter_type, $action_id,
134 $enabled, $inverse, '$action_param', '$filter_param',
135 $cat_filter, $feed_id,
136 $cat_id, '$reg_exp', ".
137 $_SESSION['uid'].")";
bbefea90 138
d2a317e3 139 db_query($link, $query);
80edb8b4 140
d2a317e3
AD
141 } else {
142 opml_notice(T_sprintf("Duplicate filter %s", htmlspecialchars($reg_exp)));
143 }
144 }
145 }
146 }
80edb8b4 147
d2a317e3
AD
148 function opml_import_category($link, $doc, $root_node, $owner_uid, $parent_id) {
149 $body = $doc->getElementsByTagName('body');
80edb8b4 150
d2a317e3 151 $default_cat_id = (int) get_feed_category($link, 'Imported feeds', false);
80edb8b4 152
d2a317e3
AD
153 if ($root_node) {
154 $cat_title = db_escape_string($root_node->attributes->getNamedItem('title')->nodeValue);
80edb8b4 155
d2a317e3
AD
156 if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) {
157 $cat_id = get_feed_category($link, $cat_title, $parent_id);
158 db_query($link, "BEGIN");
159 if ($cat_id === false) {
160 add_feed_category($link, $cat_title, $parent_id);
161 $cat_id = get_feed_category($link, $cat_title, $parent_id);
162 }
163 db_query($link, "COMMIT");
164 } else {
165 $cat_id = 0;
166 }
80edb8b4 167
d2a317e3 168 $outlines = $root_node->childNodes;
80edb8b4 169
d2a317e3
AD
170 } else {
171 $xpath = new DOMXpath($doc);
172 $outlines = $xpath->query("//opml/body/outline");
80edb8b4 173
d2a317e3
AD
174 $cat_id = 0;
175 }
80edb8b4 176
d2a317e3
AD
177 #opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id");
178 opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized")));
80edb8b4 179
d2a317e3
AD
180 foreach ($outlines as $node) {
181 if ($node->hasAttributes() && strtolower($node->tagName) == "outline") {
182 $attrs = $node->attributes;
183 $node_cat_title = db_escape_string($attrs->getNamedItem('title')->nodeValue);
80edb8b4 184
d2a317e3
AD
185 if ($node->hasChildNodes() && $node_cat_title) {
186 opml_import_category($link, $doc, $node, $owner_uid, $cat_id);
187 } else {
80edb8b4 188
d2a317e3
AD
189 if (!$cat_id) {
190 $dst_cat_id = $default_cat_id;
80edb8b4 191 } else {
d2a317e3 192 $dst_cat_id = $cat_id;
80edb8b4
AD
193 }
194
d2a317e3
AD
195 switch ($cat_title) {
196 case "tt-rss-prefs":
197 opml_import_preference($link, $doc, $node, $owner_uid);
198 break;
199 case "tt-rss-labels":
200 opml_import_label($link, $doc, $node, $owner_uid);
201 break;
202 case "tt-rss-filters":
203 opml_import_filter($link, $doc, $node, $owner_uid);
204 break;
205 default:
206 opml_import_feed($link, $doc, $node, $dst_cat_id, $owner_uid);
207 }
80edb8b4 208 }
80edb8b4 209 }
d2a317e3
AD
210 }
211 }
80edb8b4 212
d2a317e3
AD
213 function opml_import_domdoc($link, $owner_uid) {
214
215 $debug = isset($_REQUEST["debug"]);
216 $doc = false;
217
d3cface6 218 #if ($debug) $doc = DOMDocument::load("/tmp/test.opml");
d2a317e3
AD
219
220 if (is_file($_FILES['opml_file']['tmp_name'])) {
221 $doc = DOMDocument::load($_FILES['opml_file']['tmp_name']);
222 } else if (!$doc) {
80edb8b4 223 print_error(__('Error: please upload OPML file.'));
d2a317e3 224 return;
80edb8b4
AD
225 }
226
d2a317e3
AD
227 if ($doc) {
228 opml_import_category($link, $doc, false, $owner_uid);
229 } else {
230 print_error(__('Error while parsing document.'));
231 }
80edb8b4
AD
232 }
233
3a9d5c6e 234 function opml_export_category($link, $owner_uid, $cat_id, $hide_private_feeds=false) {
da49ccf5 235
d2a317e3 236 if ($cat_id) {
3a9d5c6e 237 $cat_qpart = "parent_cat = '$cat_id'";
d2a317e3
AD
238 $feed_cat_qpart = "cat_id = '$cat_id'";
239 } else {
3a9d5c6e 240 $cat_qpart = "parent_cat IS NULL";
d2a317e3
AD
241 $feed_cat_qpart = "cat_id IS NULL";
242 }
fee909d5 243
3a9d5c6e
AD
244 if ($hide_private_feeds)
245 $hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')";
246 else
247 $hide_qpart = "true";
fee909d5 248
3a9d5c6e 249 $out = "";
86e26f1a 250
d2a317e3
AD
251 if ($cat_id) {
252 $result = db_query($link, "SELECT title FROM ttrss_feed_categories WHERE id = '$cat_id'
253 AND owner_uid = '$owner_uid'");
254 $cat_title = db_fetch_result($result, 0, "title");
255 }
da49ccf5 256
d2a317e3 257 if ($cat_title) $out .= "<outline title=\"$cat_title\">\n";
86e26f1a 258
d2a317e3
AD
259 $result = db_query($link, "SELECT id,title
260 FROM ttrss_feed_categories WHERE
261 $cat_qpart AND owner_uid = '$owner_uid' ORDER BY order_id, title");
9a4506c8 262
8158c57a 263 while ($line = db_fetch_assoc($result)) {
6e0584e9 264 $title = htmlspecialchars($line["title"]);
d2a317e3
AD
265 $out .= opml_export_category($link, $owner_uid, $line["id"], $hide_private_feeds);
266 }
9a4506c8 267
d2a317e3
AD
268 $feeds_result = db_query($link, "select title, feed_url, site_url
269 from ttrss_feeds where $feed_cat_qpart AND owner_uid = '$owner_uid' AND $hide_qpart
270 order by order_id, title");
da49ccf5 271
ac9b1f07
AD
272 while ($fline = db_fetch_assoc($feeds_result)) {
273 $title = htmlspecialchars($fline["title"]);
274 $url = htmlspecialchars($fline["feed_url"]);
275 $site_url = htmlspecialchars($fline["site_url"]);
da49ccf5 276
ac9b1f07
AD
277 if ($site_url) {
278 $html_url_qpart = "htmlUrl=\"$site_url\"";
279 } else {
280 $html_url_qpart = "";
fa7b8749
AD
281 }
282
ac9b1f07
AD
283 $out .= "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
284 }
285
d2a317e3 286 if ($cat_title) $out .= "</outline>\n";
da49ccf5 287
3a9d5c6e
AD
288 return $out;
289 }
290
291 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
292 if (!isset($_REQUEST["debug"])) {
293 header("Content-type: application/xml+opml");
294 header("Content-Disposition: attachment; filename=" . $name );
295 } else {
296 header("Content-type: text/xml");
297 }
298
299 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
300
301 $out .= "<opml version=\"1.0\">";
302 $out .= "<head>
303 <dateCreated>" . date("r", time()) . "</dateCreated>
304 <title>Tiny Tiny RSS Feed Export</title>
305 </head>";
306 $out .= "<body>";
307
308 $out .= opml_export_category($link, $owner_uid, false, $hide_private_feeds);
309
e9558345
AD
310 # export tt-rss settings
311
442f326b 312 if ($include_settings) {
95562576 313 $out .= "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
e9558345 314
442f326b 315 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
95562576 316 profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
e9558345 317
442f326b 318 while ($line = db_fetch_assoc($result)) {
442f326b
AD
319 $name = $line["pref_name"];
320 $value = htmlspecialchars($line["value"]);
0a13e84e 321
ac9b1f07 322 $out .= "<outline pref-name=\"$name\" value=\"$value\"/>";
0a13e84e 323 }
e9558345 324
95562576 325 $out .= "</outline>";
bbefea90 326
95562576 327 $out .= "<outline title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION."\">";
bbefea90
AD
328
329 $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
330 owner_uid = " . $_SESSION['uid']);
331
332 while ($line = db_fetch_assoc($result)) {
333 $name = htmlspecialchars($line['caption']);
334 $fg_color = htmlspecialchars($line['fg_color']);
335 $bg_color = htmlspecialchars($line['bg_color']);
336
95562576 337 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
bbefea90
AD
338
339 }
340
95562576 341 $out .= "</outline>";
7d926cfc 342
95562576 343 $out .= "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">";
7d926cfc
AD
344
345 $result = db_query($link, "SELECT filter_type,
346 reg_exp,
347 action_id,
348 enabled,
349 action_param,
350 inverse,
351 filter_param,
352 cat_filter,
cfb665c0 353 ttrss_feeds.feed_url AS feed_url,
7d926cfc
AD
354 ttrss_feed_categories.title AS cat_title
355 FROM ttrss_filters
356 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
357 LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
358 WHERE
359 ttrss_filters.owner_uid = " . $_SESSION['uid']);
360
361 while ($line = db_fetch_assoc($result)) {
362 $name = htmlspecialchars($line['reg_exp']);
363
364 foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
365 $line[$b] = sql_bool_to_bool($line[$b]);
366 }
367
368 $filter = json_encode($line);
369
95562576 370 $out .= "<outline filter-name=\"$name\">$filter</outline>";
7d926cfc
AD
371
372 }
373
374
95562576 375 $out .= "</outline>";
442f326b 376 }
e9558345 377
95562576
AD
378 $out .= "</body></opml>";
379
380 // Format output.
381 $doc = new DOMDocument();
382 $doc->formatOutput = true;
383 $doc->preserveWhiteSpace = false;
384 $doc->loadXML($out);
385 $res = $doc->saveXML();
386
387 // saveXML uses a two-space indent. Change to tabs.
388 $res = preg_replace_callback('/^(?: )+/mu',
389 create_function(
390 '$matches',
391 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
392 $res);
393
394 print $res;
9a4506c8
AD
395 }
396
30f782a2
AD
397 // FIXME there are some brackets issues here
398
399 $op = $_REQUEST["op"];
95562576 400 if (!$op) $op = "Export";
08ae2a5b 401
95562576 402 $output_name = $_REQUEST["filename"];
08ae2a5b 403 if (!$output_name) $output_name = "TinyTinyRSS.opml";
0a13e84e 404
95562576 405 $show_settings = $_REQUEST["settings"];
0a13e84e 406
30f782a2 407 if ($op == "Export") {
0a13e84e 408
a4234239
MK
409 login_sequence($link);
410 $owner_uid = $_SESSION["uid"];
08ae2a5b 411 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
30f782a2 412 }
0a13e84e 413
442f326b 414 if ($op == "publish"){
86e26f1a
MK
415 $key = db_escape_string($_REQUEST["key"]);
416
2e7f046f
AD
417 $result = db_query($link, "SELECT owner_uid
418 FROM ttrss_access_keys WHERE
419 access_key = '$key' AND feed_id = 'OPML:Publish'");
86e26f1a
MK
420
421 if (db_num_rows($result) == 1) {
2e7f046f 422 $owner_uid = db_fetch_result($result, 0, "owner_uid");
08ae2a5b 423 return opml_export($link, "", $owner_uid, true, false);
86e26f1a
MK
424 } else {
425 print "<error>User not found</error>";
426 }
427 }
30f782a2 428
e98a3f65 429 if ($op == "Import") {
8158c57a 430
a4234239
MK
431 login_sequence($link);
432 $owner_uid = $_SESSION["uid"];
433
e9558345
AD
434 header('Content-Type: text/html; charset=utf-8');
435
e98a3f65
AD
436 print "<html>
437 <head>
ef59e6e8 438 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
1025ad87 439 <title>".__("OPML Utility")."</title>
e9558345 440 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
e98a3f65 441 </head>
04f6df27 442 <body>
0ae2bb2a 443 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
d1db26aa 444 <h1>".__('OPML Utility')."</h1>";
9f311df6 445
c03cf250
AD
446 db_query($link, "BEGIN");
447
448 /* create Imported feeds category just in case */
449
450 $result = db_query($link, "SELECT id FROM
451 ttrss_feed_categories WHERE title = 'Imported feeds' AND
452 owner_uid = '$owner_uid' LIMIT 1");
453
454 if (db_num_rows($result) == 0) {
455 db_query($link, "INSERT INTO ttrss_feed_categories
0a13e84e 456 (title,owner_uid)
c03cf250
AD
457 VALUES ('Imported feeds', '$owner_uid')");
458 }
459
460 db_query($link, "COMMIT");
461
d2a317e3
AD
462 opml_notice(__("Importing OPML..."));
463
0a13e84e 464 opml_import_domdoc($link, $owner_uid);
9f311df6 465
d7c848d9 466 print "<br><form method=\"GET\" action=\"prefs.php\">
1025ad87 467 <input type=\"submit\" value=\"".__("Return to preferences")."\">
d7c848d9 468 </form>";
9f311df6 469
30f782a2 470 print "</body></html>";
9f311df6
AD
471
472 }
473
3d477c2c 474// if ($link) db_close($link);
9f311df6 475
d2a317e3
AD
476 function opml_notice($msg) {
477 print "$msg<br/>";
478 }
479
9a4506c8 480?>