]> git.wh0rd.org - tt-rss.git/blob - opml.php
include path fix for lighttpd
[tt-rss.git] / opml.php
1 <?php
2 set_include_path(get_include_path() . PATH_SEPARATOR .
3 dirname(__FILE__) . "/include");
4
5 require_once "functions.php";
6 require_once "sessions.php";
7 require_once "sanity_check.php";
8 require_once "config.php";
9 require_once "db.php";
10 require_once "db-prefs.php";
11
12 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
13
14 if (!init_connection($link)) return;
15
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>)&nbsp;";
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
181 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
182 if (!$_REQUEST["debug"]) {
183 header("Content-type: application/xml+opml");
184 } else {
185 header("Content-type: text/xml");
186 }
187 header("Content-Disposition: attachment; filename=" . $name );
188
189 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
190
191 print "<opml version=\"1.0\">";
192 print "<head>
193 <dateCreated>" . date("r", time()) . "</dateCreated>
194 <title>Tiny Tiny RSS Feed Export</title>
195 </head>";
196 print "<body>";
197
198 $cat_mode = false;
199
200 $select = "SELECT * ";
201 $where = "WHERE owner_uid = '$owner_uid'";
202 $orderby = "ORDER BY title";
203 if ($hide_private_feeds){
204 $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
205 auth_login = '' AND auth_pass = ''";
206 }
207
208
209
210 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
211 $cat_mode = true;
212 $select = "SELECT
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
217 }
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
225
226 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
227
228 $old_cat_title = "";
229
230 while ($line = db_fetch_assoc($result)) {
231 $title = htmlspecialchars($line["title"]);
232 $url = htmlspecialchars($line["feed_url"]);
233 $site_url = htmlspecialchars($line["site_url"]);
234
235 if ($cat_mode) {
236 $cat_title = htmlspecialchars($line["cat_title"]);
237
238 if ($old_cat_title != $cat_title) {
239 if ($old_cat_title) {
240 print "</outline>\n";
241 }
242
243 if ($cat_title) {
244 print "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
245 }
246
247 $old_cat_title = $cat_title;
248 }
249 }
250
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";
258 }
259
260 if ($cat_mode && $old_cat_title) {
261 print "</outline>\n";
262 }
263
264 # export tt-rss settings
265
266 if ($include_settings) {
267 print "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
268
269 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
270 profile IS NULL AND owner_uid = " . $_SESSION["uid"]);
271
272 while ($line = db_fetch_assoc($result)) {
273
274 $name = $line["pref_name"];
275 $value = htmlspecialchars($line["value"]);
276
277 print "<outline pref-name=\"$name\" value=\"$value\">";
278
279 print "</outline>";
280
281 }
282
283 print "</outline>";
284 }
285
286 print "</body></opml>";
287 }
288
289 // FIXME there are some brackets issues here
290
291 $op = $_REQUEST["op"];
292 if (!$op) $op = "Export";
293
294 $output_name = $_REQUEST["filename"];
295 if (!$output_name) $output_name = "TinyTinyRSS.opml";
296
297 $show_settings = $_REQUEST["settings"];
298
299 if ($op == "Export") {
300
301 login_sequence($link);
302 $owner_uid = $_SESSION["uid"];
303 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
304 }
305
306 if ($op == "publish"){
307 $key = db_escape_string($_REQUEST["key"]);
308
309 $result = db_query($link, "SELECT owner_uid
310 FROM ttrss_access_keys WHERE
311 access_key = '$key' AND feed_id = 'OPML:Publish'");
312
313 if (db_num_rows($result) == 1) {
314 $owner_uid = db_fetch_result($result, 0, "owner_uid");
315 return opml_export($link, "", $owner_uid, true, false);
316 } else {
317 print "<error>User not found</error>";
318 }
319 }
320
321 if ($op == "Import") {
322
323 login_sequence($link);
324 $owner_uid = $_SESSION["uid"];
325
326 header('Content-Type: text/html; charset=utf-8');
327
328 print "<html>
329 <head>
330 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
331 <title>".__("OPML Utility")."</title>
332 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
333 </head>
334 <body>
335 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
336 <h1>".__('OPML Utility')."</h1>";
337
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
348 (title,owner_uid)
349 VALUES ('Imported feeds', '$owner_uid')");
350 }
351
352 db_query($link, "COMMIT");
353
354 print "<p>".__("Importing OPML...")."</p>";
355 opml_import_domdoc($link, $owner_uid);
356
357 print "<br><form method=\"GET\" action=\"prefs.php\">
358 <input type=\"submit\" value=\"".__("Return to preferences")."\">
359 </form>";
360
361 print "</body></html>";
362
363 }
364
365 // if ($link) db_close($link);
366
367 ?>