]> git.wh0rd.org - tt-rss.git/blob - opml.php
support import and export of tt-rss preferences in OPML with DOMDocument OPML path
[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) {
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";
37 }
38
39
40
41 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
42 $cat_mode = true;
43 $select = "SELECT
44 title, feed_url, site_url,
45 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
46 $orderby = "ORDER BY cat_title, title";
47
48 }
49 else{
50 $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
51 print "<!-- feeding cats is not enabled -->";
52 print "<!-- $cat_feed -->";
53
54 }
55
56
57 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
58
59 $old_cat_title = "";
60
61 while ($line = db_fetch_assoc($result)) {
62 $title = htmlspecialchars($line["title"]);
63 $url = htmlspecialchars($line["feed_url"]);
64 $site_url = htmlspecialchars($line["site_url"]);
65
66 if ($cat_mode) {
67 $cat_title = htmlspecialchars($line["cat_title"]);
68
69 if ($old_cat_title != $cat_title) {
70 if ($old_cat_title) {
71 print "</outline>\n";
72 }
73
74 if ($cat_title) {
75 print "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
76 }
77
78 $old_cat_title = $cat_title;
79 }
80 }
81
82 if ($site_url) {
83 $html_url_qpart = "htmlUrl=\"$site_url\"";
84 } else {
85 $html_url_qpart = "";
86 }
87
88 print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
89 }
90
91 if ($cat_mode && $old_cat_title) {
92 print "</outline>\n";
93 }
94
95 # export tt-rss settings
96
97 print "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
98
99 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
100 profile IS NULL AND owner_uid = " . $_SESSION["uid"]);
101
102 while ($line = db_fetch_assoc($result)) {
103
104 $name = $line["pref_name"];
105 $value = htmlspecialchars($line["value"]);
106
107 print "<outline pref-name=\"$name\" value=\"$value\">";
108
109 print "</outline>";
110
111 }
112
113 print "</outline>";
114
115 print "</body></opml>";
116 }
117
118 // FIXME there are some brackets issues here
119
120 $op = $_REQUEST["op"];
121
122 if (!$op) $op = "Export";
123
124 if ($op == "Export") {
125
126 login_sequence($link);
127 $owner_uid = $_SESSION["uid"];
128 return opml_export($link, $owner_uid);
129 }
130 if ($op == "publish"){
131 $key = db_escape_string($_REQUEST["key"]);
132
133 $result = db_query($link, "SELECT login, owner_uid
134 FROM ttrss_user_prefs, ttrss_users WHERE
135 pref_name = '_PREFS_PUBLISH_KEY' AND
136 value = '$key' AND
137 ttrss_users.id = owner_uid");
138
139 if (db_num_rows($result) == 1) {
140 $owner = db_fetch_result($result, 0, "owner_uid");
141 return opml_export($link, $owner, True);
142 } else {
143 print "<error>User not found</error>";
144 }
145 }
146
147 if ($op == "Import") {
148
149 login_sequence($link);
150 $owner_uid = $_SESSION["uid"];
151
152 header('Content-Type: text/html; charset=utf-8');
153
154 print "<html>
155 <head>
156 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
157 <title>".__("OPML Utility")."</title>
158 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
159 </head>
160 <body>
161 <div class=\"floatingLogo\"><img src=\"images/ttrss_logo.png\"></div>
162 <h1>".__('OPML Utility')."</h1>";
163
164 db_query($link, "BEGIN");
165
166 /* create Imported feeds category just in case */
167
168 $result = db_query($link, "SELECT id FROM
169 ttrss_feed_categories WHERE title = 'Imported feeds' AND
170 owner_uid = '$owner_uid' LIMIT 1");
171
172 if (db_num_rows($result) == 0) {
173 db_query($link, "INSERT INTO ttrss_feed_categories
174 (title,owner_uid)
175 VALUES ('Imported feeds', '$owner_uid')");
176 }
177
178 db_query($link, "COMMIT");
179
180 /* Handle OPML import by DOMXML/DOMDocument */
181
182 if (function_exists('domxml_open_file')) {
183 print "<p>".__("Importing OPML (using DOMXML extension)...")."</p>";
184 require_once "modules/opml_domxml.php";
185 opml_import_domxml($link, $owner_uid);
186 } else if (PHP_VERSION >= 5) {
187 print "<p>".__("Importing OPML (using DOMDocument extension)...")."</p>";
188 require_once "modules/opml_domdoc.php";
189 opml_import_domdoc($link, $owner_uid);
190 } else {
191 print_error(__("DOMXML extension is not found. It is required for PHP versions below 5."));
192 }
193
194 print "<br><form method=\"GET\" action=\"prefs.php\">
195 <input type=\"submit\" value=\"".__("Return to preferences")."\">
196 </form>";
197
198 print "</body></html>";
199
200 }
201
202 // if ($link) db_close($link);
203
204 ?>