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