]> git.wh0rd.org Git - tt-rss.git/blob - opml.php
some initial feed-linking work
[tt-rss.git] / opml.php
1 <?
2         session_start();
3
4         require_once "sanity_check.php";
5         require_once "functions.php";
6         require_once "config.php";
7         require_once "db.php";
8         require_once "db-prefs.php";
9
10         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); 
11
12         if (DB_TYPE == "pgsql") {
13                 pg_query($link, "set client_encoding = 'utf-8'");
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");
28                 print "<?xml 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                                 ORDER BY title WHERE owner_uid = '$owner_uid'");
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 (WEB_DEMO_MODE) {
106                         print "OPML import is disabled in demo-mode.";
107                         print "<p><a class=\"button\" href=\"prefs.php\">
108                         Return to preferences</a></div></body></html>";
109
110                         return;
111                 }
112
113                 if (is_file($_FILES['opml_file']['tmp_name'])) {
114                         $dom = domxml_open_file($_FILES['opml_file']['tmp_name']);
115
116                         if ($dom) {
117                                 $root = $dom->document_element();
118
119                                 $body = $root->get_elements_by_tagname('body');
120
121                                 if ($body[0]) {                 
122                                         $body = $body[0];
123
124                                         $outlines = $body->get_elements_by_tagname('outline');
125
126                                         print "<table>";
127
128                                         foreach ($outlines as $outline) {
129
130                                                 $feed_title = db_escape_string($outline->get_attribute('text'));
131
132                                                 if (!$feed_title) {
133                                                         $feed_title = db_escape_string($outline->get_attribute('title'));
134                                                 }
135                                         
136                                                 $cat_title = db_escape_string($outline->get_attribute('title'));
137                                                 $feed_url = db_escape_string($outline->get_attribute('xmlUrl'));
138                                                 $site_url = db_escape_string($outline->get_attribute('htmlUrl'));
139
140                                                 if ($cat_title && !$feed_url) {
141
142                                                         db_query($link, "BEGIN");
143                                                         
144                                                         $result = db_query($link, "SELECT id FROM
145                                                                 ttrss_feed_categories WHERE title = '$cat_title' AND
146                                                                 owner_uid = '$owner_uid' LIMIT 1");
147
148                                                         if (db_num_rows($result) == 0) {
149
150                                                                 print "Adding category <b>$cat_title</b>...<br>";
151
152                                                                 db_query($link, "INSERT INTO ttrss_feed_categories
153                                                                         (title,owner_uid) 
154                                                                 VALUES ('$cat_title', '$owner_uid')");
155                                                         }
156
157                                                         db_query($link, "COMMIT");
158                                                 }
159
160 //                                              print "$active_category : $feed_title : $feed_url<br>";
161
162                                                 if (!$feed_title || !$feed_url) continue;
163
164                                                 db_query($link, "BEGIN");
165
166                                                 $cat_id = null;
167
168                                                 $parent_node = $outline->parent_node();
169
170                                                 if ($parent_node && $parent_node->node_name() == "outline") {
171                                                         $element_category = $parent_node->get_attribute('title');
172                                                 } else {
173                                                         $element_category = '';
174                                                 }
175
176                                                 if ($element_category) {
177
178                                                         $result = db_query($link, "SELECT id FROM
179                                                                         ttrss_feed_categories WHERE title = '$element_category' AND
180                                                                         owner_uid = '$owner_uid' LIMIT 1");                                                             
181
182                                                         if (db_num_rows($result) == 1) {        
183                                                                 $cat_id = db_fetch_result($result, 0, "id");
184                                                         }
185                                                 }                                                               
186
187                                                 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
188                                                         (title = '$feed_title' OR feed_url = '$feed_url') 
189                                                         AND owner_uid = '$owner_uid'");
190
191                                                 print "<tr><td><a href='$site_url'><b>$feed_title</b></a></b> 
192                                                         (<a href=\"$feed_url\">rss</a>)</td>";
193
194                                                 if (db_num_rows($result) > 0) {
195                                                         print "<td>Already imported.</td>";
196                                                 } else {
197
198                                                         if ($cat_id) {
199                                                                 $add_query = "INSERT INTO ttrss_feeds 
200                                                                         (title, feed_url, owner_uid, cat_id, site_url) VALUES
201                                                                         ('$feed_title', '$feed_url', '$owner_uid', 
202                                                                                 '$cat_id', '$site_url')";
203
204                                                         } else {
205                                                                 $add_query = "INSERT INTO ttrss_feeds 
206                                                                         (title, feed_url, owner_uid, site_url) VALUES
207                                                                         ('$feed_title', '$feed_url', '$owner_uid', '$site_url')";
208
209                                                         }
210
211                                                         db_query($link, $add_query);
212                                                         
213                                                         print "<td><b>Done.</b></td>";
214                                                 }
215
216                                                 print "</tr>";
217                                                 
218                                                 db_query($link, "COMMIT");
219                                         }
220
221                                         print "</table>";
222
223                                 } else {
224                                         print "<div class=\"error\">Error: can't find body element.</div>";
225                                 }
226                         } else {
227                                 print "<div class=\"error\">Error while parsing document.</div>";
228                         }
229
230                 } else {
231                         print "<div class=\"error\">Error: please upload OPML file.</div>";
232                 }
233
234                 print "<p><a class=\"button\" href=\"prefs.php\">
235                         Return to preferences</a>";
236
237                 print "</div></body></html>";
238
239         }
240
241 //      if ($link) db_close($link);
242
243 ?>