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