]>
git.wh0rd.org - tt-rss.git/blob - opml.php
4 require_once "sanity_check.php";
6 // FIXME there are some brackets issues here
10 header("Content-type: application/xml");
11 print "<?xml version=\"1.0\"?>";
14 require_once "config.php";
15 require_once "db.php";
16 require_once "db-prefs.php";
18 $owner_uid = $_SESSION["uid"];
20 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
22 if (DB_TYPE
== "pgsql") {
23 pg_query($link, "set client_encoding = 'utf-8'");
26 if ($op == "Export") {
27 print "<opml version=\"1.0\">";
28 print "<head><dateCreated>" . date("r", time()) . "</dateCreated></head>";
33 if (get_pref($link, 'ENABLE_FEED_CATS')) {
35 $result = db_query($link, "SELECT
36 ttrss_feeds.feed_url AS feed_url,
37 ttrss_feeds.title AS title,
38 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title
41 owner_uid = '$owner_uid'
42 ORDER BY cat_title,title");
44 $result = db_query($link, "SELECT * FROM ttrss_feeds
45 ORDER BY title WHERE owner_uid = '$owner_uid'");
50 while ($line = db_fetch_assoc($result)) {
51 $title = htmlspecialchars($line["title"]);
52 $url = htmlspecialchars($line["feed_url"]);
55 $cat_title = htmlspecialchars($line["cat_title"]);
57 if ($old_cat_title != $cat_title) {
63 print "<outline title=\"$cat_title\">\n";
66 $old_cat_title = $cat_title;
70 print "<outline text=\"$title\" xmlUrl=\"$url\"/>\n";
73 if ($cat_mode && $old_cat_title) {
77 print "</body></opml>";
80 if ($op == "Import") {
84 <link rel=\"stylesheet\" href=\"opml.css\" type=\"text/css\">
86 <body><h1>Importing OPML...</h1>
90 print "OPML import is disabled in demo-mode.";
91 print "<p><a class=\"button\" href=\"prefs.php\">
92 Return to preferences</a></div></body></html>";
97 if (is_file($_FILES['opml_file']['tmp_name'])) {
98 $dom = domxml_open_file($_FILES['opml_file']['tmp_name']);
101 $root = $dom->document_element();
103 $body = $root->get_elements_by_tagname('body');
108 $outlines = $body->get_elements_by_tagname('outline');
110 $active_category = '';
112 foreach ($outlines as $outline) {
113 $feed_title = $outline->get_attribute('text');
114 $cat_title = $outline->get_attribute('title');
115 $feed_url = $outline->get_attribute('xmlUrl');
118 $active_category = $cat_title;
120 db_query($link, "BEGIN");
122 $result = db_query($link, "SELECT id FROM
123 ttrss_feed_categories WHERE title = '$cat_title' AND
124 owner_uid = '$owner_uid' LIMIT 1");
126 if (db_num_rows($result) == 0) {
128 print "Adding category <b>$cat_title</b>...<br>";
130 db_query($link, "INSERT INTO ttrss_feed_categories
131 (title,owner_uid) VALUES ('$cat_title', '$owner_uid')");
134 db_query($link, "COMMIT");
137 // print "$active_category : $feed_title : $xmlurl<br>";
139 if (!$feed_title ||
!$feed_url) continue;
141 db_query($link, "BEGIN");
145 if ($active_category) {
147 $result = db_query($link, "SELECT id FROM
148 ttrss_feed_categories WHERE title = '$active_category' AND
149 owner_uid = '$owner_uid' LIMIT 1");
151 if (db_num_rows($result) == 1) {
152 $cat_id = db_fetch_result($result, 0, "id");
156 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
157 (title = '$feed_title' OR feed_url = '$feed_url')
158 AND owner_uid = '$owner_uid'");
160 print "Feed <b>$feed_title</b> ($feed_url)... ";
162 if (db_num_rows($result) > 0) {
163 print " Already imported.<br>";
167 $add_query = "INSERT INTO ttrss_feeds
168 (title, feed_url, owner_uid, cat_id) VALUES
169 ('$feed_title', '$feed_url', '$owner_uid', '$cat_id')";
172 $add_query = "INSERT INTO ttrss_feeds
173 (title, feed_url, owner_uid) VALUES
174 ('$feed_title', '$feed_url', '$owner_uid')";
178 db_query($link, $add_query);
180 print "<b>Done.</b><br>";
183 db_query($link, "COMMIT");
187 print "Error: can't find body element.";
190 print "Error while parsing document.";
194 print "Error: please upload OPML file.";
197 print "<p><a class=\"button\" href=\"prefs.php\">
198 Return to preferences</a>";
200 print "</div></body></html>";
204 // if ($link) db_close($link);