]>
git.wh0rd.org - tt-rss.git/blob - opml.php
2 set_include_path(get_include_path() . PATH_SEPARATOR
.
3 dirname(__FILE__
) . "/include");
5 require_once "functions.php";
6 require_once "sessions.php";
7 require_once "sanity_check.php";
8 require_once "config.php";
10 require_once "db-prefs.php";
12 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
14 if (!init_connection($link)) return;
16 function opml_import_domdoc($link, $owner_uid) {
18 if (is_file($_FILES['opml_file']['tmp_name'])) {
19 $doc = DOMDocument
::load($_FILES['opml_file']['tmp_name']);
21 $result = db_query($link, "SELECT id FROM
22 ttrss_feed_categories WHERE title = 'Imported feeds' AND
23 owner_uid = '$owner_uid' LIMIT 1");
25 if (db_num_rows($result) == 1) {
26 $default_cat_id = db_fetch_result($result, 0, "id");
31 // Keep imported categories in order, after any pre-existing ones.
32 $new_cat_order_id = 0;
33 // Get the highest category order_id in use.
34 $result = db_query($link, "SELECT order_id FROM
35 ttrss_feed_categories WHERE owner_uid = '$owner_uid'
36 ORDER BY order_id DESC LIMIT 1");
37 if (db_num_rows($result) == 1) {
38 $new_cat_order_id = db_fetch_result($result, 0, "order_id");
42 $body = $doc->getElementsByTagName('body');
44 $xpath = new DOMXpath($doc);
45 $query = "/opml/body//outline";
47 $outlines = $xpath->query($query);
49 foreach ($outlines as $outline) {
51 $attributes = $outline->attributes
;
53 $feed_title = db_escape_string($attributes->getNamedItem('text')->nodeValue
);
54 if (!$feed_title) $feed_title = db_escape_string($attributes->getNamedItem('title')->nodeValue
);
56 $cat_title = db_escape_string($attributes->getNamedItem('title')->nodeValue
);
57 if (!$cat_title) $cat_title = db_escape_string($attributes->getNamedItem('text')->nodeValue
);
59 $feed_url = db_escape_string($attributes->getNamedItem('xmlUrl')->nodeValue
);
60 if (!$feed_url) $feed_url = db_escape_string($attributes->getNamedItem('xmlURL')->nodeValue
);
62 $site_url = db_escape_string($attributes->getNamedItem('htmlUrl')->nodeValue
);
64 $pref_name = db_escape_string($attributes->getNamedItem('pref-name')->nodeValue
);
65 $label_name = db_escape_string($attributes->getNamedItem('label-name')->nodeValue
);
66 $filter_name = db_escape_string($attributes->getNamedItem('filter-name')->nodeValue
);
68 if ($cat_title && !$feed_url) {
70 if ($cat_title != "tt-rss-prefs" && $cat_title != 'tt-rss-labels' && $cat_title != 'tt-rss-filters') {
72 db_query($link, "BEGIN");
74 $result = db_query($link, "SELECT id FROM
75 ttrss_feed_categories WHERE title = '$cat_title' AND
76 owner_uid = '$owner_uid' LIMIT 1");
78 if (db_num_rows($result) == 0) {
79 $cat_order_id = ++
$new_cat_order_id;
81 printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title);
83 db_query($link, "INSERT INTO ttrss_feed_categories
84 (title,owner_uid,order_id)
85 VALUES ('$cat_title', '$owner_uid', '$cat_order_id')");
88 db_query($link, "COMMIT");
92 // print "$active_category : $feed_title : $feed_url<br>";
95 $parent_node = $outline->parentNode
;
97 if ($parent_node && $parent_node->nodeName
== "outline") {
98 $cat_check = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
99 if ($cat_check == "tt-rss-prefs") {
100 $pref_value = db_escape_string($outline->attributes
->getNamedItem('value')->nodeValue
);
103 __("Setting preference key %s to %s")."</li>",
104 $pref_name, $pref_value);
106 set_pref($link, $pref_name, $pref_value);
113 $parent_node = $outline->parentNode
;
115 if ($parent_node && $parent_node->nodeName
== "outline") {
116 $cat_check = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
117 if ($cat_check == "tt-rss-labels") {
119 $fg_color = db_escape_string($attributes->getNamedItem('label-fg-color')->nodeValue
);
120 $bg_color = db_escape_string($attributes->getNamedItem('label-bg-color')->nodeValue
);
122 if (!label_find_id($link, $label_name, $_SESSION['uid'])) {
123 printf("<li>".__("Adding label %s")."</li>", htmlspecialchars($label_name));
124 label_create($link, $label_name, $fg_color, $bg_color);
126 printf("<li>".__("Duplicate label: %s")."</li>",
127 htmlspecialchars($label_name));
134 $parent_node = $outline->parentNode
;
136 if ($parent_node && $parent_node->nodeName
== "outline") {
137 $cat_check = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
138 if ($cat_check == "tt-rss-filters") {
139 $filter = json_decode($outline->nodeValue
, true);
142 $reg_exp = db_escape_string($filter['reg_exp']);
143 $filter_type = (int)$filter['filter_type'];
144 $action_id = (int)$filter['action_id'];
146 $result = db_query($link, "SELECT id FROM ttrss_filters WHERE
147 reg_exp = '$reg_exp' AND
148 filter_type = '$filter_type' AND
149 action_id = '$action_id' AND
150 owner_uid = " .$_SESSION['uid']);
152 if (db_num_rows($result) == 0) {
153 $enabled = bool_to_sql_bool($filter['enabled']);
154 $action_param = db_escape_string($filter['action_param']);
155 $inverse = bool_to_sql_bool($filter['inverse']);
156 $filter_param = db_escape_string($filter['filter_param']);
157 $cat_filter = bool_to_sql_bool($filter['cat_filter']);
159 $feed_url = db_escape_string($filter['feed_url']);
160 $cat_title = db_escape_string($filter['cat_title']);
162 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
163 feed_url = '$feed_url' AND owner_uid = ".$_SESSION['uid']);
165 if (db_num_rows($result) != 0) {
166 $feed_id = db_fetch_result($result, 0, "id");
171 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
172 title = '$cat_title' AND owner_uid = ".$_SESSION['uid']);
174 if (db_num_rows($result) != 0) {
175 $cat_id = db_fetch_result($result, 0, "id");
180 printf("<li>".__("Adding filter %s")."</li>", htmlspecialchars($reg_exp));
182 $query = "INSERT INTO ttrss_filters (filter_type, action_id,
183 enabled, inverse, action_param, filter_param,
187 VALUES ($filter_type, $action_id,
188 $enabled, $inverse, '$action_param', '$filter_param',
189 $cat_filter, $feed_id,
190 $cat_id, '$reg_exp', ".
191 $_SESSION['uid'].")";
193 db_query($link, $query);
196 printf("<li>".__("Duplicate filter %s")."</li>", htmlspecialchars($reg_exp));
204 if (!$feed_title ||
!$feed_url) continue;
206 db_query($link, "BEGIN");
210 $parent_node = $outline->parentNode
;
212 if ($parent_node && $parent_node->nodeName
== "outline") {
213 $element_category = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
214 if (!$element_category) $element_category = $parent_node->attributes
->getNamedItem('text')->nodeValue
;
217 $element_category = '';
220 if ($element_category) {
222 $element_category = db_escape_string($element_category);
224 $result = db_query($link, "SELECT id FROM
225 ttrss_feed_categories WHERE title = '$element_category' AND
226 owner_uid = '$owner_uid' LIMIT 1");
228 if (db_num_rows($result) == 1) {
229 $cat_id = db_fetch_result($result, 0, "id");
233 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
234 feed_url = '$feed_url'
235 AND owner_uid = '$owner_uid'");
237 print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b>
238 (<a target='_blank' href=\"$feed_url\">rss</a>) ";
240 if (db_num_rows($result) > 0) {
241 print __('is already imported.');
243 // Get max order_id already in use. Increment.
244 $new_feed_order_id = 0; // these start at zero
245 $cat_id_qpart = $cat_id ?
"cat_id = '$cat_id'" : "cat_id = '$default_cat_id'";
246 $result = db_query($link, "SELECT order_id FROM
247 ttrss_feeds WHERE owner_uid = '$owner_uid' AND $cat_id_qpart
248 ORDER BY order_id DESC LIMIT 1");
249 if (db_num_rows($result) == 1) {
250 $new_feed_order_id = db_fetch_result($result, 0, "order_id");
251 $new_feed_order_id++
;
255 $add_query = "INSERT INTO ttrss_feeds
256 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
257 ('$feed_title', '$feed_url', '$owner_uid',
258 '$cat_id', '$site_url', '$new_feed_order_id')";
261 $add_query = "INSERT INTO ttrss_feeds
262 (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
263 ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
264 '$site_url', '$new_feed_order_id')";
269 db_query($link, $add_query);
276 db_query($link, "COMMIT");
280 print_error(__('Error while parsing document.'));
284 print_error(__('Error: please upload OPML file.'));
290 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
291 if (!$_REQUEST["debug"]) {
292 header("Content-type: application/xml+opml");
293 header("Content-Disposition: attachment; filename=" . $name );
295 header("Content-type: text/xml");
298 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
300 $out .= "<opml version=\"1.0\">";
302 <dateCreated>" . date("r", time()) . "</dateCreated>
303 <title>Tiny Tiny RSS Feed Export</title>
309 $select = "SELECT * ";
310 $where = "WHERE owner_uid = '$owner_uid'";
311 $orderby = "ORDER BY order_id, title";
312 if ($hide_private_feeds){
313 $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
314 auth_login = '' AND auth_pass = ''";
319 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
322 title, feed_url, site_url, order_id,
323 (SELECT order_id FROM ttrss_feed_categories WHERE id = cat_id) AS cat_order_id,
324 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
325 $orderby = "ORDER BY cat_order_id, cat_title, order_id, title";
329 $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
330 $out .= "<!-- feeding cats is not enabled -->";
331 $out .= "<!-- $cat_feed -->";
336 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
340 while ($line = db_fetch_assoc($result)) {
341 $title = htmlspecialchars($line["title"]);
342 $url = htmlspecialchars($line["feed_url"]);
343 $site_url = htmlspecialchars($line["site_url"]);
346 $cat_title = htmlspecialchars($line["cat_title"]);
348 if ($old_cat_title != $cat_title) {
349 if ($old_cat_title) {
350 $out .= "</outline>\n";
354 $out .= "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
357 $old_cat_title = $cat_title;
362 $html_url_qpart = "htmlUrl=\"$site_url\"";
364 $html_url_qpart = "";
367 $out .= "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
370 if ($cat_mode && $old_cat_title) {
371 $out .= "</outline>\n";
374 # export tt-rss settings
376 if ($include_settings) {
377 $out .= "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION
."\">";
379 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
380 profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
382 while ($line = db_fetch_assoc($result)) {
384 $name = $line["pref_name"];
385 $value = htmlspecialchars($line["value"]);
387 $out .= "<outline pref-name=\"$name\" value=\"$value\">";
389 $out .= "</outline>";
393 $out .= "</outline>";
395 $out .= "<outline title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION
."\">";
397 $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
398 owner_uid = " . $_SESSION['uid']);
400 while ($line = db_fetch_assoc($result)) {
401 $name = htmlspecialchars($line['caption']);
402 $fg_color = htmlspecialchars($line['fg_color']);
403 $bg_color = htmlspecialchars($line['bg_color']);
405 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
409 $out .= "</outline>";
411 $out .= "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION
."\">";
413 $result = db_query($link, "SELECT filter_type,
421 ttrss_feeds.feed_url AS feed_url,
422 ttrss_feed_categories.title AS cat_title
424 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
425 LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
427 ttrss_filters.owner_uid = " . $_SESSION['uid']);
429 while ($line = db_fetch_assoc($result)) {
430 $name = htmlspecialchars($line['reg_exp']);
432 foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
433 $line[$b] = sql_bool_to_bool($line[$b]);
436 $filter = json_encode($line);
438 $out .= "<outline filter-name=\"$name\">$filter</outline>";
443 $out .= "</outline>";
446 $out .= "</body></opml>";
449 $doc = new DOMDocument();
450 $doc->formatOutput
= true;
451 $doc->preserveWhiteSpace
= false;
453 $res = $doc->saveXML();
455 // saveXML uses a two-space indent. Change to tabs.
456 $res = preg_replace_callback('/^(?: )+/mu',
459 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
465 // FIXME there are some brackets issues here
467 $op = $_REQUEST["op"];
468 if (!$op) $op = "Export";
470 $output_name = $_REQUEST["filename"];
471 if (!$output_name) $output_name = "TinyTinyRSS.opml";
473 $show_settings = $_REQUEST["settings"];
475 if ($op == "Export") {
477 login_sequence($link);
478 $owner_uid = $_SESSION["uid"];
479 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
482 if ($op == "publish"){
483 $key = db_escape_string($_REQUEST["key"]);
485 $result = db_query($link, "SELECT owner_uid
486 FROM ttrss_access_keys WHERE
487 access_key = '$key' AND feed_id = 'OPML:Publish'");
489 if (db_num_rows($result) == 1) {
490 $owner_uid = db_fetch_result($result, 0, "owner_uid");
491 return opml_export($link, "", $owner_uid, true, false);
493 print "<error>User not found</error>";
497 if ($op == "Import") {
499 login_sequence($link);
500 $owner_uid = $_SESSION["uid"];
502 header('Content-Type: text/html; charset=utf-8');
506 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
507 <title>".__("OPML Utility")."</title>
508 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
511 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
512 <h1>".__('OPML Utility')."</h1>";
514 db_query($link, "BEGIN");
516 /* create Imported feeds category just in case */
518 $result = db_query($link, "SELECT id FROM
519 ttrss_feed_categories WHERE title = 'Imported feeds' AND
520 owner_uid = '$owner_uid' LIMIT 1");
522 if (db_num_rows($result) == 0) {
523 db_query($link, "INSERT INTO ttrss_feed_categories
525 VALUES ('Imported feeds', '$owner_uid')");
528 db_query($link, "COMMIT");
530 print "<p>".__("Importing OPML...")."</p>";
531 opml_import_domdoc($link, $owner_uid);
533 print "<br><form method=\"GET\" action=\"prefs.php\">
534 <input type=\"submit\" value=\"".__("Return to preferences")."\">
537 print "</body></html>";
541 // if ($link) db_close($link);