]> git.wh0rd.org - tt-rss.git/blob - opml.php
make published OPML use common secret key code
[tt-rss.git] / opml.php
1 <?php
2 error_reporting(E_ERROR | E_WARNING | E_PARSE);
3
4 require_once "sessions.php";
5 require_once "sanity_check.php";
6 require_once "functions.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 init_connection($link);
14
15 function opml_export($link, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
16 if (!$_REQUEST["debug"]) {
17 header("Content-type: application/xml+opml");
18 } else {
19 header("Content-type: text/xml");
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
125 if (!$op) $op = "Export";
126
127 if ($op == "Export") {
128
129 login_sequence($link);
130 $owner_uid = $_SESSION["uid"];
131 return opml_export($link, $owner_uid);
132 }
133
134 if ($op == "publish"){
135 $key = db_escape_string($_REQUEST["key"]);
136
137 $result = db_query($link, "SELECT owner_uid
138 FROM ttrss_access_keys WHERE
139 access_key = '$key' AND feed_id = 'OPML:Publish'");
140
141 if (db_num_rows($result) == 1) {
142 $owner_uid = db_fetch_result($result, 0, "owner_uid");
143 return opml_export($link, $owner_uid, true, false);
144 } else {
145 print "<error>User not found</error>";
146 }
147 }
148
149 if ($op == "Import") {
150
151 login_sequence($link);
152 $owner_uid = $_SESSION["uid"];
153
154 header('Content-Type: text/html; charset=utf-8');
155
156 print "<html>
157 <head>
158 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
159 <title>".__("OPML Utility")."</title>
160 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
161 </head>
162 <body>
163 <div class=\"floatingLogo\"><img src=\"images/ttrss_logo.png\"></div>
164 <h1>".__('OPML Utility')."</h1>";
165
166 db_query($link, "BEGIN");
167
168 /* create Imported feeds category just in case */
169
170 $result = db_query($link, "SELECT id FROM
171 ttrss_feed_categories WHERE title = 'Imported feeds' AND
172 owner_uid = '$owner_uid' LIMIT 1");
173
174 if (db_num_rows($result) == 0) {
175 db_query($link, "INSERT INTO ttrss_feed_categories
176 (title,owner_uid)
177 VALUES ('Imported feeds', '$owner_uid')");
178 }
179
180 db_query($link, "COMMIT");
181
182 /* Handle OPML import by DOMXML/DOMDocument */
183
184 if (function_exists('domxml_open_file')) {
185 print "<p>".__("Importing OPML (using DOMXML extension)...")."</p>";
186 require_once "modules/opml_domxml.php";
187 opml_import_domxml($link, $owner_uid);
188 } else if (PHP_VERSION >= 5) {
189 print "<p>".__("Importing OPML (using DOMDocument extension)...")."</p>";
190 require_once "modules/opml_domdoc.php";
191 opml_import_domdoc($link, $owner_uid);
192 } else {
193 print_error(__("DOMXML extension is not found. It is required for PHP versions below 5."));
194 }
195
196 print "<br><form method=\"GET\" action=\"prefs.php\">
197 <input type=\"submit\" value=\"".__("Return to preferences")."\">
198 </form>";
199
200 print "</body></html>";
201
202 }
203
204 // if ($link) db_close($link);
205
206 ?>