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