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