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