", htmlspecialchars($reg_exp));
}
}
}
}
}
if (!$feed_title || !$feed_url) continue;
db_query($link, "BEGIN");
$cat_id = null;
$parent_node = $outline->parentNode;
if ($parent_node && $parent_node->nodeName == "outline") {
$element_category = $parent_node->attributes->getNamedItem('title')->nodeValue;
if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue;
} else {
$element_category = '';
}
if ($element_category) {
$element_category = db_escape_string($element_category);
$result = db_query($link, "SELECT id FROM
ttrss_feed_categories WHERE title = '$element_category' AND
owner_uid = '$owner_uid' LIMIT 1");
if (db_num_rows($result) == 1) {
$cat_id = db_fetch_result($result, 0, "id");
}
}
$result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
feed_url = '$feed_url'
AND owner_uid = '$owner_uid'");
print "
$feed_title
(rss) ";
if (db_num_rows($result) > 0) {
print __('is already imported.');
} else {
// Get max order_id already in use. Increment.
$new_feed_order_id = 0; // these start at zero
$cat_id_qpart = $cat_id ? "cat_id = '$cat_id'" : "cat_id = '$default_cat_id'";
$result = db_query($link, "SELECT order_id FROM
ttrss_feeds WHERE owner_uid = '$owner_uid' AND $cat_id_qpart
ORDER BY order_id DESC LIMIT 1");
if (db_num_rows($result) == 1) {
$new_feed_order_id = db_fetch_result($result, 0, "order_id");
$new_feed_order_id++;
}
if ($cat_id) {
$add_query = "INSERT INTO ttrss_feeds
(title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
('$feed_title', '$feed_url', '$owner_uid',
'$cat_id', '$site_url', '$new_feed_order_id')";
} else {
$add_query = "INSERT INTO ttrss_feeds
(title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES
('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
'$site_url', '$new_feed_order_id')";
}
//print $add_query;
db_query($link, $add_query);
print __('OK');
}
print "
";
db_query($link, "COMMIT");
}
} else {
print_error(__('Error while parsing document.'));
}
} else {
print_error(__('Error: please upload OPML file.'));
}
}
function opml_export_category($link, $owner_uid, $cat_id, $hide_private_feeds=false) {
if ($cat_id)
$cat_qpart = "parent_cat = '$cat_id'";
else
$cat_qpart = "parent_cat IS NULL";
if ($hide_private_feeds)
$hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')";
else
$hide_qpart = "true";
$out = "";
$query = "SELECT
ttrss_feeds.title, feed_url, site_url, ttrss_feeds.order_id,
ttrss_feed_categories.id AS cat_id,
ttrss_feed_categories.title AS cat_title,
ttrss_feed_categories.order_id AS cat_order_id
FROM ttrss_feeds LEFT JOIN ttrss_feed_categories ON (ttrss_feed_categories.id = ttrss_feeds.cat_id)
WHERE ttrss_feeds.owner_uid = '$owner_uid' AND $hide_qpart AND $cat_qpart
ORDER BY cat_order_id, cat_title, ttrss_feeds.order_id, title";
#$out .= "";
$result = db_query($link, $query);
$old_cat_title = "";
while ($line = db_fetch_assoc($result)) {
$title = htmlspecialchars($line["title"]);
$url = htmlspecialchars($line["feed_url"]);
$site_url = htmlspecialchars($line["site_url"]);
$cat_title = htmlspecialchars($line["cat_title"]);
if ($old_cat_title != $cat_title) {
if ($old_cat_title) {
$out .= "\n";
}
if ($cat_title) {
$out .= "\n";
}
$old_cat_title = $cat_title;
$cat_id = (int) $line["cat_id"];
if ($cat_id > 0)
$out .= opml_export_category($link, $owner_uid, $cat_id, $hide_private_feeds);
}
if ($site_url) {
$html_url_qpart = "htmlUrl=\"$site_url\"";
} else {
$html_url_qpart = "";
}
$out .= "\n";
}
if ($old_cat_title) {
$out .= "\n";
}
return $out;
}
function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
if (!isset($_REQUEST["debug"])) {
header("Content-type: application/xml+opml");
header("Content-Disposition: attachment; filename=" . $name );
} else {
header("Content-type: text/xml");
}
$out = "";
$out .= "";
$out .= "
" . date("r", time()) . "Tiny Tiny RSS Feed Export
";
$out .= "";
$out .= opml_export_category($link, $owner_uid, false, $hide_private_feeds);
# export tt-rss settings
if ($include_settings) {
$out .= "";
$result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
while ($line = db_fetch_assoc($result)) {
$name = $line["pref_name"];
$value = htmlspecialchars($line["value"]);
$out .= "";
$out .= "";
}
$out .= "";
$out .= "";
$result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
owner_uid = " . $_SESSION['uid']);
while ($line = db_fetch_assoc($result)) {
$name = htmlspecialchars($line['caption']);
$fg_color = htmlspecialchars($line['fg_color']);
$bg_color = htmlspecialchars($line['bg_color']);
$out .= "";
}
$out .= "";
$out .= "";
$result = db_query($link, "SELECT filter_type,
reg_exp,
action_id,
enabled,
action_param,
inverse,
filter_param,
cat_filter,
ttrss_feeds.feed_url AS feed_url,
ttrss_feed_categories.title AS cat_title
FROM ttrss_filters
LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
WHERE
ttrss_filters.owner_uid = " . $_SESSION['uid']);
while ($line = db_fetch_assoc($result)) {
$name = htmlspecialchars($line['reg_exp']);
foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
$line[$b] = sql_bool_to_bool($line[$b]);
}
$filter = json_encode($line);
$out .= "$filter";
}
$out .= "";
}
$out .= "";
// Format output.
$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->preserveWhiteSpace = false;
$doc->loadXML($out);
$res = $doc->saveXML();
// saveXML uses a two-space indent. Change to tabs.
$res = preg_replace_callback('/^(?: )+/mu',
create_function(
'$matches',
'return str_repeat("\t", intval(strlen($matches[0])/2));'),
$res);
print $res;
}
// FIXME there are some brackets issues here
$op = $_REQUEST["op"];
if (!$op) $op = "Export";
$output_name = $_REQUEST["filename"];
if (!$output_name) $output_name = "TinyTinyRSS.opml";
$show_settings = $_REQUEST["settings"];
if ($op == "Export") {
login_sequence($link);
$owner_uid = $_SESSION["uid"];
return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
}
if ($op == "publish"){
$key = db_escape_string($_REQUEST["key"]);
$result = db_query($link, "SELECT owner_uid
FROM ttrss_access_keys WHERE
access_key = '$key' AND feed_id = 'OPML:Publish'");
if (db_num_rows($result) == 1) {
$owner_uid = db_fetch_result($result, 0, "owner_uid");
return opml_export($link, "", $owner_uid, true, false);
} else {
print "User not found";
}
}
if ($op == "Import") {
login_sequence($link);
$owner_uid = $_SESSION["uid"];
header('Content-Type: text/html; charset=utf-8');
print "
".__("OPML Utility")."
".__('OPML Utility')."
";
db_query($link, "BEGIN");
/* create Imported feeds category just in case */
$result = db_query($link, "SELECT id FROM
ttrss_feed_categories WHERE title = 'Imported feeds' AND
owner_uid = '$owner_uid' LIMIT 1");
if (db_num_rows($result) == 0) {
db_query($link, "INSERT INTO ttrss_feed_categories
(title,owner_uid)
VALUES ('Imported feeds', '$owner_uid')");
}
db_query($link, "COMMIT");
print "