]>
Commit | Line | Data |
---|---|---|
1d3a17c7 | 1 | <?php |
107d0cf3 AD |
2 | set_include_path(get_include_path() . PATH_SEPARATOR . "include"); |
3 | ||
fb074239 | 4 | require_once "functions.php"; |
1559e374 | 5 | require_once "sessions.php"; |
66581886 | 6 | require_once "sanity_check.php"; |
9a4506c8 | 7 | require_once "config.php"; |
8158c57a | 8 | require_once "db.php"; |
a0111294 | 9 | require_once "db-prefs.php"; |
9a4506c8 | 10 | |
0a13e84e | 11 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); |
9f311df6 | 12 | |
5f0a3741 | 13 | if (!init_connection($link)) return; |
4e9f5c24 | 14 | |
80edb8b4 AD |
15 | function opml_import_domdoc($link, $owner_uid) { |
16 | ||
17 | if (is_file($_FILES['opml_file']['tmp_name'])) { | |
18 | $doc = DOMDocument::load($_FILES['opml_file']['tmp_name']); | |
19 | ||
20 | $result = db_query($link, "SELECT id FROM | |
21 | ttrss_feed_categories WHERE title = 'Imported feeds' AND | |
22 | owner_uid = '$owner_uid' LIMIT 1"); | |
23 | ||
24 | if (db_num_rows($result) == 1) { | |
25 | $default_cat_id = db_fetch_result($result, 0, "id"); | |
26 | } else { | |
27 | $default_cat_id = 0; | |
28 | } | |
29 | ||
30 | if ($doc) { | |
31 | $body = $doc->getElementsByTagName('body'); | |
32 | ||
33 | $xpath = new DOMXpath($doc); | |
34 | $query = "/opml/body//outline"; | |
35 | ||
36 | $outlines = $xpath->query($query); | |
37 | ||
38 | foreach ($outlines as $outline) { | |
39 | ||
40 | $feed_title = db_escape_string($outline->attributes->getNamedItem('text')->nodeValue); | |
41 | ||
42 | if (!$feed_title) { | |
43 | $feed_title = db_escape_string($outline->attributes->getNamedItem('title')->nodeValue); | |
44 | } | |
45 | ||
46 | $cat_title = db_escape_string($outline->attributes->getNamedItem('title')->nodeValue); | |
47 | ||
48 | if (!$cat_title) { | |
49 | $cat_title = db_escape_string($outline->attributes->getNamedItem('text')->nodeValue); | |
50 | } | |
51 | ||
52 | $feed_url = db_escape_string($outline->attributes->getNamedItem('xmlUrl')->nodeValue); | |
53 | ||
54 | if (!$feed_url) | |
55 | $feed_url = db_escape_string($outline->attributes->getNamedItem('xmlURL')->nodeValue); | |
56 | ||
57 | $site_url = db_escape_string($outline->attributes->getNamedItem('htmlUrl')->nodeValue); | |
58 | ||
59 | $pref_name = db_escape_string($outline->attributes->getNamedItem('pref-name')->nodeValue); | |
60 | ||
61 | if ($cat_title && !$feed_url) { | |
62 | ||
63 | if ($cat_title != "tt-rss-prefs") { | |
64 | ||
65 | db_query($link, "BEGIN"); | |
66 | ||
67 | $result = db_query($link, "SELECT id FROM | |
68 | ttrss_feed_categories WHERE title = '$cat_title' AND | |
69 | owner_uid = '$owner_uid' LIMIT 1"); | |
70 | ||
71 | if (db_num_rows($result) == 0) { | |
72 | ||
73 | printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title); | |
74 | ||
75 | db_query($link, "INSERT INTO ttrss_feed_categories | |
76 | (title,owner_uid) | |
77 | VALUES ('$cat_title', '$owner_uid')"); | |
78 | } | |
79 | ||
80 | db_query($link, "COMMIT"); | |
81 | } | |
82 | } | |
83 | ||
84 | // print "$active_category : $feed_title : $feed_url<br>"; | |
85 | ||
86 | if ($pref_name) { | |
87 | $parent_node = $outline->parentNode; | |
88 | ||
89 | if ($parent_node && $parent_node->nodeName == "outline") { | |
90 | $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue; | |
91 | if ($cat_check == "tt-rss-prefs") { | |
92 | $pref_value = db_escape_string($outline->attributes->getNamedItem('value')->nodeValue); | |
93 | ||
94 | printf("<li>". | |
95 | __("Setting preference key %s to %s")."</li>", | |
96 | $pref_name, $pref_value); | |
97 | ||
98 | set_pref($link, $pref_name, $pref_value); | |
99 | ||
100 | } | |
101 | } | |
102 | } | |
103 | ||
104 | if (!$feed_title || !$feed_url) continue; | |
105 | ||
106 | db_query($link, "BEGIN"); | |
107 | ||
108 | $cat_id = null; | |
109 | ||
110 | $parent_node = $outline->parentNode; | |
111 | ||
112 | if ($parent_node && $parent_node->nodeName == "outline") { | |
113 | $element_category = $parent_node->attributes->getNamedItem('title')->nodeValue; | |
114 | if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue; | |
115 | ||
116 | } else { | |
117 | $element_category = ''; | |
118 | } | |
119 | ||
120 | if ($element_category) { | |
121 | ||
122 | $element_category = db_escape_string($element_category); | |
123 | ||
124 | $result = db_query($link, "SELECT id FROM | |
125 | ttrss_feed_categories WHERE title = '$element_category' AND | |
126 | owner_uid = '$owner_uid' LIMIT 1"); | |
127 | ||
128 | if (db_num_rows($result) == 1) { | |
129 | $cat_id = db_fetch_result($result, 0, "id"); | |
130 | } | |
131 | } | |
132 | ||
133 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE | |
134 | feed_url = '$feed_url' | |
135 | AND owner_uid = '$owner_uid'"); | |
136 | ||
137 | print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b> | |
138 | (<a target='_blank' href=\"$feed_url\">rss</a>) "; | |
139 | ||
140 | if (db_num_rows($result) > 0) { | |
141 | print __('is already imported.'); | |
142 | } else { | |
143 | ||
144 | if ($cat_id) { | |
145 | $add_query = "INSERT INTO ttrss_feeds | |
146 | (title, feed_url, owner_uid, cat_id, site_url) VALUES | |
147 | ('$feed_title', '$feed_url', '$owner_uid', | |
148 | '$cat_id', '$site_url')"; | |
149 | ||
150 | } else { | |
151 | $add_query = "INSERT INTO ttrss_feeds | |
152 | (title, feed_url, owner_uid, cat_id, site_url) VALUES | |
153 | ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id', | |
154 | '$site_url')"; | |
155 | ||
156 | } | |
157 | ||
158 | //print $add_query; | |
159 | db_query($link, $add_query); | |
160 | ||
161 | print __('OK'); | |
162 | } | |
163 | ||
164 | print "</li>"; | |
165 | ||
166 | db_query($link, "COMMIT"); | |
167 | } | |
168 | ||
169 | } else { | |
170 | print_error(__('Error while parsing document.')); | |
171 | } | |
172 | ||
173 | } else { | |
174 | print_error(__('Error: please upload OPML file.')); | |
175 | } | |
176 | ||
177 | ||
178 | } | |
179 | ||
08ae2a5b | 180 | function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) { |
e9558345 AD |
181 | if (!$_REQUEST["debug"]) { |
182 | header("Content-type: application/xml+opml"); | |
183 | } else { | |
184 | header("Content-type: text/xml"); | |
185 | } | |
08ae2a5b CM |
186 | header("Content-Disposition: attachment; filename=" . $name ); |
187 | ||
981e8107 | 188 | print "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; |
4e9f5c24 | 189 | |
9a4506c8 | 190 | print "<opml version=\"1.0\">"; |
a90005e6 AD |
191 | print "<head> |
192 | <dateCreated>" . date("r", time()) . "</dateCreated> | |
193 | <title>Tiny Tiny RSS Feed Export</title> | |
0a13e84e | 194 | </head>"; |
9a4506c8 AD |
195 | print "<body>"; |
196 | ||
da49ccf5 | 197 | $cat_mode = false; |
0a13e84e | 198 | |
86e26f1a MK |
199 | $select = "SELECT * "; |
200 | $where = "WHERE owner_uid = '$owner_uid'"; | |
201 | $orderby = "ORDER BY title"; | |
202 | if ($hide_private_feeds){ | |
0a13e84e | 203 | $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND |
c107797c | 204 | auth_login = '' AND auth_pass = ''"; |
86e26f1a | 205 | } |
da49ccf5 | 206 | |
fee909d5 MK |
207 | |
208 | ||
209 | if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) { | |
da49ccf5 | 210 | $cat_mode = true; |
0a13e84e | 211 | $select = "SELECT |
86e26f1a MK |
212 | title, feed_url, site_url, |
213 | (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title"; | |
214 | $orderby = "ORDER BY cat_title, title"; | |
215 | ||
da49ccf5 | 216 | } |
fee909d5 MK |
217 | else{ |
218 | $cat_feed = get_pref($link, 'ENABLE_FEED_CATS'); | |
219 | print "<!-- feeding cats is not enabled -->"; | |
220 | print "<!-- $cat_feed -->"; | |
221 | ||
222 | } | |
223 | ||
da49ccf5 | 224 | |
86e26f1a MK |
225 | $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby); |
226 | ||
da49ccf5 | 227 | $old_cat_title = ""; |
9a4506c8 | 228 | |
8158c57a | 229 | while ($line = db_fetch_assoc($result)) { |
6e0584e9 AD |
230 | $title = htmlspecialchars($line["title"]); |
231 | $url = htmlspecialchars($line["feed_url"]); | |
fa7b8749 | 232 | $site_url = htmlspecialchars($line["site_url"]); |
9a4506c8 | 233 | |
da49ccf5 AD |
234 | if ($cat_mode) { |
235 | $cat_title = htmlspecialchars($line["cat_title"]); | |
236 | ||
237 | if ($old_cat_title != $cat_title) { | |
238 | if ($old_cat_title) { | |
0a13e84e | 239 | print "</outline>\n"; |
da49ccf5 AD |
240 | } |
241 | ||
5b9af2b9 | 242 | if ($cat_title) { |
fee909d5 | 243 | print "<outline title=\"$cat_title\" text=\"$cat_title\" >\n"; |
5b9af2b9 | 244 | } |
da49ccf5 AD |
245 | |
246 | $old_cat_title = $cat_title; | |
247 | } | |
248 | } | |
249 | ||
fa7b8749 AD |
250 | if ($site_url) { |
251 | $html_url_qpart = "htmlUrl=\"$site_url\""; | |
252 | } else { | |
253 | $html_url_qpart = ""; | |
254 | } | |
255 | ||
256 | print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n"; | |
9a4506c8 AD |
257 | } |
258 | ||
da49ccf5 | 259 | if ($cat_mode && $old_cat_title) { |
0a13e84e | 260 | print "</outline>\n"; |
da49ccf5 AD |
261 | } |
262 | ||
e9558345 AD |
263 | # export tt-rss settings |
264 | ||
442f326b AD |
265 | if ($include_settings) { |
266 | print "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">"; | |
e9558345 | 267 | |
442f326b AD |
268 | $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE |
269 | profile IS NULL AND owner_uid = " . $_SESSION["uid"]); | |
e9558345 | 270 | |
442f326b | 271 | while ($line = db_fetch_assoc($result)) { |
e9558345 | 272 | |
442f326b AD |
273 | $name = $line["pref_name"]; |
274 | $value = htmlspecialchars($line["value"]); | |
0a13e84e | 275 | |
442f326b | 276 | print "<outline pref-name=\"$name\" value=\"$value\">"; |
e9558345 | 277 | |
442f326b | 278 | print "</outline>"; |
e9558345 | 279 | |
0a13e84e | 280 | } |
e9558345 | 281 | |
442f326b AD |
282 | print "</outline>"; |
283 | } | |
e9558345 | 284 | |
9a4506c8 AD |
285 | print "</body></opml>"; |
286 | } | |
287 | ||
30f782a2 AD |
288 | // FIXME there are some brackets issues here |
289 | ||
290 | $op = $_REQUEST["op"]; | |
08ae2a5b CM |
291 | if (!$op) $op = "Export"; |
292 | ||
293 | $output_name = $_REQUEST["filename"]; | |
294 | if (!$output_name) $output_name = "TinyTinyRSS.opml"; | |
0a13e84e | 295 | |
08ae2a5b | 296 | $show_settings = $_REQUEST["settings"]; |
0a13e84e | 297 | |
30f782a2 | 298 | if ($op == "Export") { |
0a13e84e | 299 | |
a4234239 MK |
300 | login_sequence($link); |
301 | $owner_uid = $_SESSION["uid"]; | |
08ae2a5b | 302 | return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1)); |
30f782a2 | 303 | } |
0a13e84e | 304 | |
442f326b | 305 | if ($op == "publish"){ |
86e26f1a MK |
306 | $key = db_escape_string($_REQUEST["key"]); |
307 | ||
2e7f046f AD |
308 | $result = db_query($link, "SELECT owner_uid |
309 | FROM ttrss_access_keys WHERE | |
310 | access_key = '$key' AND feed_id = 'OPML:Publish'"); | |
86e26f1a MK |
311 | |
312 | if (db_num_rows($result) == 1) { | |
2e7f046f | 313 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
08ae2a5b | 314 | return opml_export($link, "", $owner_uid, true, false); |
86e26f1a MK |
315 | } else { |
316 | print "<error>User not found</error>"; | |
317 | } | |
318 | } | |
30f782a2 | 319 | |
e98a3f65 | 320 | if ($op == "Import") { |
8158c57a | 321 | |
a4234239 MK |
322 | login_sequence($link); |
323 | $owner_uid = $_SESSION["uid"]; | |
324 | ||
e9558345 AD |
325 | header('Content-Type: text/html; charset=utf-8'); |
326 | ||
e98a3f65 AD |
327 | print "<html> |
328 | <head> | |
ef59e6e8 | 329 | <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\"> |
1025ad87 | 330 | <title>".__("OPML Utility")."</title> |
e9558345 | 331 | <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/> |
e98a3f65 | 332 | </head> |
04f6df27 | 333 | <body> |
0ae2bb2a | 334 | <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div> |
d1db26aa | 335 | <h1>".__('OPML Utility')."</h1>"; |
9f311df6 | 336 | |
c03cf250 AD |
337 | db_query($link, "BEGIN"); |
338 | ||
339 | /* create Imported feeds category just in case */ | |
340 | ||
341 | $result = db_query($link, "SELECT id FROM | |
342 | ttrss_feed_categories WHERE title = 'Imported feeds' AND | |
343 | owner_uid = '$owner_uid' LIMIT 1"); | |
344 | ||
345 | if (db_num_rows($result) == 0) { | |
346 | db_query($link, "INSERT INTO ttrss_feed_categories | |
0a13e84e | 347 | (title,owner_uid) |
c03cf250 AD |
348 | VALUES ('Imported feeds', '$owner_uid')"); |
349 | } | |
350 | ||
351 | db_query($link, "COMMIT"); | |
352 | ||
0a13e84e | 353 | print "<p>".__("Importing OPML...")."</p>"; |
0a13e84e | 354 | opml_import_domdoc($link, $owner_uid); |
9f311df6 | 355 | |
d7c848d9 | 356 | print "<br><form method=\"GET\" action=\"prefs.php\"> |
1025ad87 | 357 | <input type=\"submit\" value=\"".__("Return to preferences")."\"> |
d7c848d9 | 358 | </form>"; |
9f311df6 | 359 | |
30f782a2 | 360 | print "</body></html>"; |
9f311df6 AD |
361 | |
362 | } | |
363 | ||
3d477c2c | 364 | // if ($link) db_close($link); |
9f311df6 | 365 | |
9a4506c8 | 366 | ?> |