]> git.wh0rd.org Git - tt-rss.git/blob - opml.php
991f93e0cc00b2b89d160f6afba0e692825f67e6
[tt-rss.git] / opml.php
1 <?php
2         require_once "functions.php";
3         require_once "sessions.php";
4         require_once "sanity_check.php";
5         require_once "config.php";
6         require_once "db.php";
7         require_once "db-prefs.php";
8
9         $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
10
11         init_connection($link);
12
13         function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
14                 if (!$_REQUEST["debug"]) {
15                         header("Content-type: application/xml+opml");
16                 } else {
17                         header("Content-type: text/xml");
18                 }
19         header("Content-Disposition: attachment; filename=" . $name );
20
21                 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
22
23                 print "<opml version=\"1.0\">";
24                 print "<head>
25                         <dateCreated>" . date("r", time()) . "</dateCreated>
26                         <title>Tiny Tiny RSS Feed Export</title>
27                 </head>";
28                 print "<body>";
29
30                 $cat_mode = false;
31
32                 $select = "SELECT * ";
33                 $where = "WHERE owner_uid = '$owner_uid'";
34                 $orderby = "ORDER BY title";
35                 if ($hide_private_feeds){
36                         $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
37                                 auth_login = '' AND auth_pass = ''";
38                 }
39
40
41
42                 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
43                         $cat_mode = true;
44                         $select = "SELECT
45                                 title, feed_url, site_url,
46                                 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
47                         $orderby = "ORDER BY cat_title, title";
48
49                 }
50                 else{
51                         $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
52                         print "<!-- feeding cats is not enabled -->";
53                         print "<!-- $cat_feed -->";
54
55                 }
56
57
58                 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
59
60                 $old_cat_title = "";
61
62                 while ($line = db_fetch_assoc($result)) {
63                         $title = htmlspecialchars($line["title"]);
64                         $url = htmlspecialchars($line["feed_url"]);
65                         $site_url = htmlspecialchars($line["site_url"]);
66
67                         if ($cat_mode) {
68                                 $cat_title = htmlspecialchars($line["cat_title"]);
69
70                                 if ($old_cat_title != $cat_title) {
71                                         if ($old_cat_title) {
72                                                 print "</outline>\n";
73                                         }
74
75                                         if ($cat_title) {
76                                                 print "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
77                                         }
78
79                                         $old_cat_title = $cat_title;
80                                 }
81                         }
82
83                         if ($site_url) {
84                                 $html_url_qpart = "htmlUrl=\"$site_url\"";
85                         } else {
86                                 $html_url_qpart = "";
87                         }
88
89                         print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
90                 }
91
92                 if ($cat_mode && $old_cat_title) {
93                         print "</outline>\n";
94                 }
95
96                 # export tt-rss settings
97
98                 if ($include_settings) {
99                         print "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
100
101                         $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
102                            profile IS NULL AND owner_uid = " . $_SESSION["uid"]);
103
104                         while ($line = db_fetch_assoc($result)) {
105
106                                 $name = $line["pref_name"];
107                                 $value = htmlspecialchars($line["value"]);
108
109                                 print "<outline pref-name=\"$name\" value=\"$value\">";
110
111                                 print "</outline>";
112
113                         }
114
115                         print "</outline>";
116                 }
117
118                 print "</body></opml>";
119         }
120
121         // FIXME there are some brackets issues here
122
123         $op = $_REQUEST["op"];
124     if (!$op) $op = "Export";
125
126     $output_name = $_REQUEST["filename"];
127         if (!$output_name) $output_name = "TinyTinyRSS.opml";
128
129     $show_settings = $_REQUEST["settings"];
130
131         if ($op == "Export") {
132
133                 login_sequence($link);
134                 $owner_uid = $_SESSION["uid"];
135                 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
136         }
137
138         if ($op == "publish"){
139                 $key = db_escape_string($_REQUEST["key"]);
140
141                 $result = db_query($link, "SELECT owner_uid
142                                 FROM ttrss_access_keys WHERE
143                                 access_key = '$key' AND feed_id = 'OPML:Publish'");
144
145                 if (db_num_rows($result) == 1) {
146                         $owner_uid = db_fetch_result($result, 0, "owner_uid");
147                         return opml_export($link, "", $owner_uid, true, false);
148                 } else {
149                         print "<error>User not found</error>";
150                 }
151         }
152
153         if ($op == "Import") {
154
155                 login_sequence($link);
156                 $owner_uid = $_SESSION["uid"];
157
158                 header('Content-Type: text/html; charset=utf-8');
159
160                 print "<html>
161                         <head>
162                                 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
163                                 <title>".__("OPML Utility")."</title>
164                                 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
165                         </head>
166                         <body>
167                         <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
168                         <h1>".__('OPML Utility')."</h1>";
169
170                 db_query($link, "BEGIN");
171
172                 /* create Imported feeds category just in case */
173
174                 $result = db_query($link, "SELECT id FROM
175                         ttrss_feed_categories WHERE title = 'Imported feeds' AND
176                         owner_uid = '$owner_uid' LIMIT 1");
177
178                 if (db_num_rows($result) == 0) {
179                                 db_query($link, "INSERT INTO ttrss_feed_categories
180                                         (title,owner_uid)
181                                                 VALUES ('Imported feeds', '$owner_uid')");
182                 }
183
184                 db_query($link, "COMMIT");
185
186                 print "<p>".__("Importing OPML...")."</p>";
187                 require_once "modules/opml_domdoc.php";
188                 opml_import_domdoc($link, $owner_uid);
189
190                 print "<br><form method=\"GET\" action=\"prefs.php\">
191                         <input type=\"submit\" value=\"".__("Return to preferences")."\">
192                         </form>";
193
194                 print "</body></html>";
195
196         }
197
198 //      if ($link) db_close($link);
199
200 ?>