]> git.wh0rd.org - tt-rss.git/blob - opml.php
title in opml head
[tt-rss.git] / opml.php
1 <?
2 session_start();
3
4 require_once "sanity_check.php";
5
6 // FIXME there are some brackets issues here
7
8 $op = $_REQUEST["op"];
9 if ($op == "Export") {
10 header("Content-type: application/xml");
11 print "<?xml version=\"1.0\"?>";
12 }
13
14 require_once "config.php";
15 require_once "db.php";
16 require_once "db-prefs.php";
17
18 $owner_uid = $_SESSION["uid"];
19
20 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
21
22 if (DB_TYPE == "pgsql") {
23 pg_query($link, "set client_encoding = 'utf-8'");
24 }
25
26 if ($op == "Export") {
27 print "<opml version=\"1.0\">";
28 print "<head>
29 <dateCreated>" . date("r", time()) . "</dateCreated>
30 <title>Tiny Tiny RSS Feed Export</title>
31 </head>";
32 print "<body>";
33
34 $cat_mode = false;
35
36 if (get_pref($link, 'ENABLE_FEED_CATS')) {
37 $cat_mode = true;
38 $result = db_query($link, "SELECT
39 ttrss_feeds.feed_url AS feed_url,
40 ttrss_feeds.title AS title,
41 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title
42 FROM ttrss_feeds
43 WHERE
44 owner_uid = '$owner_uid'
45 ORDER BY cat_title,title");
46 } else {
47 $result = db_query($link, "SELECT * FROM ttrss_feeds
48 ORDER BY title WHERE owner_uid = '$owner_uid'");
49 }
50
51 $old_cat_title = "";
52
53 while ($line = db_fetch_assoc($result)) {
54 $title = htmlspecialchars($line["title"]);
55 $url = htmlspecialchars($line["feed_url"]);
56
57 if ($cat_mode) {
58 $cat_title = htmlspecialchars($line["cat_title"]);
59
60 if ($old_cat_title != $cat_title) {
61 if ($old_cat_title) {
62 print "</outline>\n";
63 }
64
65 if ($cat_title) {
66 print "<outline title=\"$cat_title\">\n";
67 }
68
69 $old_cat_title = $cat_title;
70 }
71 }
72
73 print "<outline text=\"$title\" xmlUrl=\"$url\"/>\n";
74 }
75
76 if ($cat_mode && $old_cat_title) {
77 print "</outline>\n";
78 }
79
80 print "</body></opml>";
81 }
82
83 if ($op == "Import") {
84
85 print "<html>
86 <head>
87 <link rel=\"stylesheet\" href=\"opml.css\" type=\"text/css\">
88 </head>
89 <body>
90 <h1>Importing OPML...</h1>
91 <div class=\"opmlBody\">";
92
93 if (WEB_DEMO_MODE) {
94 print "OPML import is disabled in demo-mode.";
95 print "<p><a class=\"button\" href=\"prefs.php\">
96 Return to preferences</a></div></body></html>";
97
98 return;
99 }
100
101 if (is_file($_FILES['opml_file']['tmp_name'])) {
102 $dom = domxml_open_file($_FILES['opml_file']['tmp_name']);
103
104 if ($dom) {
105 $root = $dom->document_element();
106
107 $body = $root->get_elements_by_tagname('body');
108
109 if ($body[0]) {
110 $body = $body[0];
111
112 $outlines = $body->get_elements_by_tagname('outline');
113
114 foreach ($outlines as $outline) {
115 $feed_title = db_escape_string($outline->get_attribute('text'));
116 $cat_title = db_escape_string($outline->get_attribute('title'));
117 $feed_url = db_escape_string($outline->get_attribute('xmlUrl'));
118
119 if ($cat_title) {
120
121 db_query($link, "BEGIN");
122
123 $result = db_query($link, "SELECT id FROM
124 ttrss_feed_categories WHERE title = '$cat_title' AND
125 owner_uid = '$owner_uid' LIMIT 1");
126
127 if (db_num_rows($result) == 0) {
128
129 print "Adding category <b>$cat_title</b>...<br>";
130
131 db_query($link, "INSERT INTO ttrss_feed_categories
132 (title,owner_uid) VALUES ('$cat_title', '$owner_uid')");
133 }
134
135 db_query($link, "COMMIT");
136 }
137
138 // print "$active_category : $feed_title : $xmlurl<br>";
139
140 if (!$feed_title || !$feed_url) continue;
141
142 db_query($link, "BEGIN");
143
144 $cat_id = null;
145
146 $parent_node = $outline->parent_node();
147
148 if ($parent_node && $parent_node->node_name() == "outline") {
149 $element_category = $parent_node->get_attribute('title');
150 } else {
151 $element_category = '';
152 }
153
154 if ($element_category) {
155
156 $result = db_query($link, "SELECT id FROM
157 ttrss_feed_categories WHERE title = '$element_category' AND
158 owner_uid = '$owner_uid' LIMIT 1");
159
160 if (db_num_rows($result) == 1) {
161 $cat_id = db_fetch_result($result, 0, "id");
162 }
163 }
164
165 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
166 (title = '$feed_title' OR feed_url = '$feed_url')
167 AND owner_uid = '$owner_uid'");
168
169 print "Feed <b>$feed_title</b> ($feed_url)... ";
170
171 if (db_num_rows($result) > 0) {
172 print " Already imported.<br>";
173 } else {
174
175 if ($cat_id) {
176 $add_query = "INSERT INTO ttrss_feeds
177 (title, feed_url, owner_uid, cat_id) VALUES
178 ('$feed_title', '$feed_url', '$owner_uid', '$cat_id')";
179
180 } else {
181 $add_query = "INSERT INTO ttrss_feeds
182 (title, feed_url, owner_uid) VALUES
183 ('$feed_title', '$feed_url', '$owner_uid')";
184
185 }
186
187 db_query($link, $add_query);
188
189 print "<b>Done.</b><br>";
190 }
191
192 db_query($link, "COMMIT");
193 }
194
195 } else {
196 print "<div class=\"error\">Error: can't find body element.</div>";
197 }
198 } else {
199 print "<div class=\"error\">Error while parsing document.</div>";
200 }
201
202 } else {
203 print "<div class=\"error\">Error: please upload OPML file.</div>";
204 }
205
206 print "<p><a class=\"button\" href=\"prefs.php\">
207 Return to preferences</a>";
208
209 print "</div></body></html>";
210
211 }
212
213 // if ($link) db_close($link);
214
215 ?>