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