]>
git.wh0rd.org - tt-rss.git/blob - opml.php
eab0b7b7d453c0e38c747bc755728c7706d5a108
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_feed($link, $doc, $node, $cat_id, $owner_uid) {
17 $attrs = $node->attributes
;
19 $feed_title = db_escape_string($attrs->getNamedItem('text')->nodeValue
);
20 if (!$feed_title) $feed_title = db_escape_string($attrs->getNamedItem('title')->nodeValue
);
22 $feed_url = db_escape_string($attrs->getNamedItem('xmlUrl')->nodeValue
);
23 if (!$feed_url) $feed_url = db_escape_string($attrs->getNamedItem('xmlURL')->nodeValue
);
25 $site_url = db_escape_string($attrs->getNamedItem('htmlUrl')->nodeValue
);
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'");
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));
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);
42 opml_notice(T_sprintf("Duplicate feed: %s", $feed_title));
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
);
52 $fg_color = db_escape_string($attrs->getNamedItem('label-fg-color')->nodeValue
);
53 $bg_color = db_escape_string($attrs->getNamedItem('label-bg-color')->nodeValue
);
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);
59 opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)));
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
);
69 $pref_value = db_escape_string($attrs->getNamedItem('value')->nodeValue
);
71 opml_notice(T_sprintf("Setting preference key %s to %s",
72 $pref_name, $pref_value));
74 set_pref($link, $pref_name, $pref_value);
78 function opml_import_filter($link, $doc, $node, $owner_uid) {
79 $attrs = $node->attributes
;
81 $filter_name = db_escape_string($attrs->getNamedItem('filter-name')->nodeValue
);
85 $filter = json_decode($node->nodeValue
, true);
88 $reg_exp = db_escape_string($filter['reg_exp']);
89 $filter_type = (int)$filter['filter_type'];
90 $action_id = (int)$filter['action_id'];
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']);
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']);
105 $feed_url = db_escape_string($filter['feed_url']);
106 $cat_title = db_escape_string($filter['cat_title']);
108 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
109 feed_url = '$feed_url' AND owner_uid = ".$_SESSION['uid']);
111 if (db_num_rows($result) != 0) {
112 $feed_id = db_fetch_result($result, 0, "id");
117 $result = db_query($link, "SELECT id FROM ttrss_feed_categories WHERE
118 title = '$cat_title' AND owner_uid = ".$_SESSION['uid']);
120 if (db_num_rows($result) != 0) {
121 $cat_id = db_fetch_result($result, 0, "id");
126 opml_notice(T_sprintf("Adding filter %s", htmlspecialchars($reg_exp)));
128 $query = "INSERT INTO ttrss_filters (filter_type, action_id,
129 enabled, inverse, action_param, filter_param,
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'].")";
139 db_query($link, $query);
142 opml_notice(T_sprintf("Duplicate filter %s", htmlspecialchars($reg_exp)));
148 function opml_import_category($link, $doc, $root_node, $owner_uid, $parent_id) {
149 $body = $doc->getElementsByTagName('body');
151 $default_cat_id = (int) get_feed_category($link, 'Imported feeds', false);
154 $cat_title = db_escape_string($root_node->attributes
->getNamedItem('title')->nodeValue
);
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);
163 db_query($link, "COMMIT");
168 $outlines = $root_node->childNodes
;
171 $xpath = new DOMXpath($doc);
172 $outlines = $xpath->query("//opml/body/outline");
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")));
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
);
185 if ($node->hasChildNodes() && $node_cat_title) {
186 opml_import_category($link, $doc, $node, $owner_uid, $cat_id);
190 $dst_cat_id = $default_cat_id;
192 $dst_cat_id = $cat_id;
195 switch ($cat_title) {
197 opml_import_preference($link, $doc, $node, $owner_uid);
199 case "tt-rss-labels":
200 opml_import_label($link, $doc, $node, $owner_uid);
202 case "tt-rss-filters":
203 opml_import_filter($link, $doc, $node, $owner_uid);
206 opml_import_feed($link, $doc, $node, $dst_cat_id, $owner_uid);
213 function opml_import_domdoc($link, $owner_uid) {
215 $debug = isset($_REQUEST["debug"]);
218 #if ($debug) $doc = DOMDocument::load("/tmp/test.opml");
220 if (is_file($_FILES['opml_file']['tmp_name'])) {
221 $doc = DOMDocument
::load($_FILES['opml_file']['tmp_name']);
223 print_error(__('Error: please upload OPML file.'));
228 opml_import_category($link, $doc, false, $owner_uid);
230 print_error(__('Error while parsing document.'));
234 function opml_export_category($link, $owner_uid, $cat_id, $hide_private_feeds=false) {
237 $cat_qpart = "parent_cat = '$cat_id'";
238 $feed_cat_qpart = "cat_id = '$cat_id'";
240 $cat_qpart = "parent_cat IS NULL";
241 $feed_cat_qpart = "cat_id IS NULL";
244 if ($hide_private_feeds)
245 $hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')";
247 $hide_qpart = "true";
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");
257 if ($cat_title) $out .= "<outline title=\"$cat_title\">\n";
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");
263 while ($line = db_fetch_assoc($result)) {
264 $title = htmlspecialchars($line["title"]);
265 $out .= opml_export_category($link, $owner_uid, $line["id"], $hide_private_feeds);
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");
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"]);
278 $html_url_qpart = "htmlUrl=\"$site_url\"";
280 $html_url_qpart = "";
283 $out .= "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
286 if ($cat_title) $out .= "</outline>\n";
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 );
296 header("Content-type: text/xml");
299 $out = "<?xml version=\"1.0\" encoding=\"utf-8\"?".">";
301 $out .= "<opml version=\"1.0\">";
303 <dateCreated>" . date("r", time()) . "</dateCreated>
304 <title>Tiny Tiny RSS Feed Export</title>
308 $out .= opml_export_category($link, $owner_uid, false, $hide_private_feeds);
310 # export tt-rss settings
312 if ($include_settings) {
313 $out .= "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION
."\">";
315 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
316 profile IS NULL AND owner_uid = " . $_SESSION["uid"] . " ORDER BY pref_name");
318 while ($line = db_fetch_assoc($result)) {
320 $name = $line["pref_name"];
321 $value = htmlspecialchars($line["value"]);
323 $out .= "<outline pref-name=\"$name\" value=\"$value\">";
325 $out .= "</outline>";
329 $out .= "</outline>";
331 $out .= "<outline title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION
."\">";
333 $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
334 owner_uid = " . $_SESSION['uid']);
336 while ($line = db_fetch_assoc($result)) {
337 $name = htmlspecialchars($line['caption']);
338 $fg_color = htmlspecialchars($line['fg_color']);
339 $bg_color = htmlspecialchars($line['bg_color']);
341 $out .= "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
345 $out .= "</outline>";
347 $out .= "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION
."\">";
349 $result = db_query($link, "SELECT filter_type,
357 ttrss_feeds.feed_url AS feed_url,
358 ttrss_feed_categories.title AS cat_title
360 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
361 LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
363 ttrss_filters.owner_uid = " . $_SESSION['uid']);
365 while ($line = db_fetch_assoc($result)) {
366 $name = htmlspecialchars($line['reg_exp']);
368 foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
369 $line[$b] = sql_bool_to_bool($line[$b]);
372 $filter = json_encode($line);
374 $out .= "<outline filter-name=\"$name\">$filter</outline>";
379 $out .= "</outline>";
382 $out .= "</body></opml>";
385 $doc = new DOMDocument();
386 $doc->formatOutput
= true;
387 $doc->preserveWhiteSpace
= false;
389 $res = $doc->saveXML();
391 // saveXML uses a two-space indent. Change to tabs.
392 $res = preg_replace_callback('/^(?: )+/mu',
395 'return str_repeat("\t", intval(strlen($matches[0])/2));'),
401 // FIXME there are some brackets issues here
403 $op = $_REQUEST["op"];
404 if (!$op) $op = "Export";
406 $output_name = $_REQUEST["filename"];
407 if (!$output_name) $output_name = "TinyTinyRSS.opml";
409 $show_settings = $_REQUEST["settings"];
411 if ($op == "Export") {
413 login_sequence($link);
414 $owner_uid = $_SESSION["uid"];
415 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
418 if ($op == "publish"){
419 $key = db_escape_string($_REQUEST["key"]);
421 $result = db_query($link, "SELECT owner_uid
422 FROM ttrss_access_keys WHERE
423 access_key = '$key' AND feed_id = 'OPML:Publish'");
425 if (db_num_rows($result) == 1) {
426 $owner_uid = db_fetch_result($result, 0, "owner_uid");
427 return opml_export($link, "", $owner_uid, true, false);
429 print "<error>User not found</error>";
433 if ($op == "Import") {
435 login_sequence($link);
436 $owner_uid = $_SESSION["uid"];
438 header('Content-Type: text/html; charset=utf-8');
442 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
443 <title>".__("OPML Utility")."</title>
444 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
447 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
448 <h1>".__('OPML Utility')."</h1>";
450 db_query($link, "BEGIN");
452 /* create Imported feeds category just in case */
454 $result = db_query($link, "SELECT id FROM
455 ttrss_feed_categories WHERE title = 'Imported feeds' AND
456 owner_uid = '$owner_uid' LIMIT 1");
458 if (db_num_rows($result) == 0) {
459 db_query($link, "INSERT INTO ttrss_feed_categories
461 VALUES ('Imported feeds', '$owner_uid')");
464 db_query($link, "COMMIT");
466 opml_notice(__("Importing OPML..."));
468 opml_import_domdoc($link, $owner_uid);
470 print "<br><form method=\"GET\" action=\"prefs.php\">
471 <input type=\"submit\" value=\"".__("Return to preferences")."\">
474 print "</body></html>";
478 // if ($link) db_close($link);
480 function opml_notice($msg) {