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