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