]>
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> | |
86 | <body><h1>Importing OPML...</h1> | |
87 | <div>"; | |
d212ce0e | 88 | |
e98a3f65 AD |
89 | if (WEB_DEMO_MODE) { |
90 | print "OPML import is disabled in demo-mode."; | |
91 | print "<p><a class=\"button\" href=\"prefs.php\"> | |
92 | Return to preferences</a></div></body></html>"; | |
d212ce0e | 93 | |
e98a3f65 AD |
94 | return; |
95 | } | |
9f311df6 | 96 | |
e98a3f65 AD |
97 | if (is_file($_FILES['opml_file']['tmp_name'])) { |
98 | $dom = domxml_open_file($_FILES['opml_file']['tmp_name']); | |
8b7395bb | 99 | |
e98a3f65 AD |
100 | if ($dom) { |
101 | $root = $dom->document_element(); | |
8b7395bb | 102 | |
e98a3f65 | 103 | $body = $root->get_elements_by_tagname('body'); |
8b7395bb | 104 | |
e98a3f65 AD |
105 | if ($body[0]) { |
106 | $body = $body[0]; | |
8b7395bb | 107 | |
e98a3f65 | 108 | $outlines = $body->get_elements_by_tagname('outline'); |
f179a35d | 109 | |
e98a3f65 | 110 | $active_category = ''; |
9f311df6 | 111 | |
e98a3f65 AD |
112 | foreach ($outlines as $outline) { |
113 | $feed_title = $outline->get_attribute('text'); | |
114 | $cat_title = $outline->get_attribute('title'); | |
115 | $feed_url = $outline->get_attribute('xmlUrl'); | |
9f311df6 | 116 | |
e98a3f65 AD |
117 | if ($cat_title) { |
118 | $active_category = $cat_title; | |
9f311df6 | 119 | |
e98a3f65 AD |
120 | db_query($link, "BEGIN"); |
121 | ||
122 | $result = db_query($link, "SELECT id FROM | |
123 | ttrss_feed_categories WHERE title = '$cat_title' AND | |
124 | owner_uid = '$owner_uid' LIMIT 1"); | |
9f311df6 | 125 | |
e98a3f65 | 126 | if (db_num_rows($result) == 0) { |
9f311df6 | 127 | |
e98a3f65 | 128 | print "Adding category <b>$cat_title</b>...<br>"; |
9f311df6 | 129 | |
e98a3f65 AD |
130 | db_query($link, "INSERT INTO ttrss_feed_categories |
131 | (title,owner_uid) VALUES ('$cat_title', '$owner_uid')"); | |
132 | } | |
7118b7ac | 133 | |
e98a3f65 AD |
134 | db_query($link, "COMMIT"); |
135 | } | |
9f311df6 | 136 | |
e98a3f65 | 137 | // print "$active_category : $feed_title : $xmlurl<br>"; |
9f311df6 | 138 | |
e98a3f65 | 139 | if (!$feed_title || !$feed_url) continue; |
9f311df6 | 140 | |
e98a3f65 | 141 | db_query($link, "BEGIN"); |
9f311df6 | 142 | |
e98a3f65 | 143 | $cat_id = null; |
eac7480c | 144 | |
e98a3f65 | 145 | if ($active_category) { |
9f311df6 | 146 | |
e98a3f65 AD |
147 | $result = db_query($link, "SELECT id FROM |
148 | ttrss_feed_categories WHERE title = '$active_category' AND | |
149 | owner_uid = '$owner_uid' LIMIT 1"); | |
eac7480c | 150 | |
e98a3f65 AD |
151 | if (db_num_rows($result) == 1) { |
152 | $cat_id = db_fetch_result($result, 0, "id"); | |
153 | } | |
154 | } | |
eac7480c | 155 | |
e98a3f65 AD |
156 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE |
157 | (title = '$feed_title' OR feed_url = '$feed_url') | |
158 | AND owner_uid = '$owner_uid'"); | |
9f311df6 | 159 | |
e98a3f65 | 160 | print "Feed <b>$feed_title</b> ($feed_url)... "; |
9f311df6 | 161 | |
e98a3f65 AD |
162 | if (db_num_rows($result) > 0) { |
163 | print " Already imported.<br>"; | |
164 | } else { | |
9f311df6 | 165 | |
e98a3f65 AD |
166 | if ($cat_id) { |
167 | $add_query = "INSERT INTO ttrss_feeds | |
168 | (title, feed_url, owner_uid, cat_id) VALUES | |
169 | ('$feed_title', '$feed_url', '$owner_uid', '$cat_id')"; | |
9f311df6 | 170 | |
e98a3f65 AD |
171 | } else { |
172 | $add_query = "INSERT INTO ttrss_feeds | |
173 | (title, feed_url, owner_uid) VALUES | |
174 | ('$feed_title', '$feed_url', '$owner_uid')"; | |
9f311df6 | 175 | |
e98a3f65 AD |
176 | } |
177 | ||
178 | db_query($link, $add_query); | |
179 | ||
180 | print "<b>Done.</b><br>"; | |
181 | } | |
9f311df6 | 182 | |
e98a3f65 | 183 | db_query($link, "COMMIT"); |
9f311df6 | 184 | } |
9f311df6 | 185 | |
e98a3f65 AD |
186 | } else { |
187 | print "Error: can't find body element."; | |
188 | } | |
9f311df6 | 189 | } else { |
e98a3f65 | 190 | print "Error while parsing document."; |
9f311df6 AD |
191 | } |
192 | ||
e98a3f65 | 193 | } else { |
9f311df6 AD |
194 | print "Error: please upload OPML file."; |
195 | } | |
196 | ||
197 | print "<p><a class=\"button\" href=\"prefs.php\"> | |
198 | Return to preferences</a>"; | |
199 | ||
200 | print "</div></body></html>"; | |
201 | ||
202 | } | |
203 | ||
3d477c2c | 204 | // if ($link) db_close($link); |
9f311df6 | 205 | |
9a4506c8 | 206 | ?> |