]>
git.wh0rd.org - tt-rss.git/blob - opml.php
2 set_include_path(get_include_path() . PATH_SEPARATOR
.
3 dirname(__FILE__
) . "/include");
5 require_once "functions.php";
6 require_once "sessions.php";
7 require_once "sanity_check.php";
8 require_once "config.php";
10 require_once "db-prefs.php";
12 $link = db_connect(DB_HOST
, DB_USER
, DB_PASS
, DB_NAME
);
14 if (!init_connection($link)) return;
16 function opml_import_domdoc($link, $owner_uid) {
18 if (is_file($_FILES['opml_file']['tmp_name'])) {
19 $doc = DOMDocument
::load($_FILES['opml_file']['tmp_name']);
21 $result = db_query($link, "SELECT id FROM
22 ttrss_feed_categories WHERE title = 'Imported feeds' AND
23 owner_uid = '$owner_uid' LIMIT 1");
25 if (db_num_rows($result) == 1) {
26 $default_cat_id = db_fetch_result($result, 0, "id");
32 $body = $doc->getElementsByTagName('body');
34 $xpath = new DOMXpath($doc);
35 $query = "/opml/body//outline";
37 $outlines = $xpath->query($query);
39 foreach ($outlines as $outline) {
41 $feed_title = db_escape_string($outline->attributes
->getNamedItem('text')->nodeValue
);
44 $feed_title = db_escape_string($outline->attributes
->getNamedItem('title')->nodeValue
);
47 $cat_title = db_escape_string($outline->attributes
->getNamedItem('title')->nodeValue
);
50 $cat_title = db_escape_string($outline->attributes
->getNamedItem('text')->nodeValue
);
53 $feed_url = db_escape_string($outline->attributes
->getNamedItem('xmlUrl')->nodeValue
);
56 $feed_url = db_escape_string($outline->attributes
->getNamedItem('xmlURL')->nodeValue
);
58 $site_url = db_escape_string($outline->attributes
->getNamedItem('htmlUrl')->nodeValue
);
60 $pref_name = db_escape_string($outline->attributes
->getNamedItem('pref-name')->nodeValue
);
62 if ($cat_title && !$feed_url) {
64 if ($cat_title != "tt-rss-prefs") {
66 db_query($link, "BEGIN");
68 $result = db_query($link, "SELECT id FROM
69 ttrss_feed_categories WHERE title = '$cat_title' AND
70 owner_uid = '$owner_uid' LIMIT 1");
72 if (db_num_rows($result) == 0) {
74 printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title);
76 db_query($link, "INSERT INTO ttrss_feed_categories
78 VALUES ('$cat_title', '$owner_uid')");
81 db_query($link, "COMMIT");
85 // print "$active_category : $feed_title : $feed_url<br>";
88 $parent_node = $outline->parentNode
;
90 if ($parent_node && $parent_node->nodeName
== "outline") {
91 $cat_check = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
92 if ($cat_check == "tt-rss-prefs") {
93 $pref_value = db_escape_string($outline->attributes
->getNamedItem('value')->nodeValue
);
96 __("Setting preference key %s to %s")."</li>",
97 $pref_name, $pref_value);
99 set_pref($link, $pref_name, $pref_value);
105 if (!$feed_title ||
!$feed_url) continue;
107 db_query($link, "BEGIN");
111 $parent_node = $outline->parentNode
;
113 if ($parent_node && $parent_node->nodeName
== "outline") {
114 $element_category = $parent_node->attributes
->getNamedItem('title')->nodeValue
;
115 if (!$element_category) $element_category = $parent_node->attributes
->getNamedItem('text')->nodeValue
;
118 $element_category = '';
121 if ($element_category) {
123 $element_category = db_escape_string($element_category);
125 $result = db_query($link, "SELECT id FROM
126 ttrss_feed_categories WHERE title = '$element_category' AND
127 owner_uid = '$owner_uid' LIMIT 1");
129 if (db_num_rows($result) == 1) {
130 $cat_id = db_fetch_result($result, 0, "id");
134 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
135 feed_url = '$feed_url'
136 AND owner_uid = '$owner_uid'");
138 print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b>
139 (<a target='_blank' href=\"$feed_url\">rss</a>) ";
141 if (db_num_rows($result) > 0) {
142 print __('is already imported.');
146 $add_query = "INSERT INTO ttrss_feeds
147 (title, feed_url, owner_uid, cat_id, site_url) VALUES
148 ('$feed_title', '$feed_url', '$owner_uid',
149 '$cat_id', '$site_url')";
152 $add_query = "INSERT INTO ttrss_feeds
153 (title, feed_url, owner_uid, cat_id, site_url) VALUES
154 ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
160 db_query($link, $add_query);
167 db_query($link, "COMMIT");
171 print_error(__('Error while parsing document.'));
175 print_error(__('Error: please upload OPML file.'));
181 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
182 if (!$_REQUEST["debug"]) {
183 header("Content-type: application/xml+opml");
185 header("Content-type: text/xml");
187 header("Content-Disposition: attachment; filename=" . $name );
189 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
191 print "<opml version=\"1.0\">";
193 <dateCreated>" . date("r", time()) . "</dateCreated>
194 <title>Tiny Tiny RSS Feed Export</title>
200 $select = "SELECT * ";
201 $where = "WHERE owner_uid = '$owner_uid'";
202 $orderby = "ORDER BY title";
203 if ($hide_private_feeds){
204 $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
205 auth_login = '' AND auth_pass = ''";
210 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
213 title, feed_url, site_url,
214 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
215 $orderby = "ORDER BY cat_title, title";
219 $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
220 print "<!-- feeding cats is not enabled -->";
221 print "<!-- $cat_feed -->";
226 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
230 while ($line = db_fetch_assoc($result)) {
231 $title = htmlspecialchars($line["title"]);
232 $url = htmlspecialchars($line["feed_url"]);
233 $site_url = htmlspecialchars($line["site_url"]);
236 $cat_title = htmlspecialchars($line["cat_title"]);
238 if ($old_cat_title != $cat_title) {
239 if ($old_cat_title) {
240 print "</outline>\n";
244 print "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
247 $old_cat_title = $cat_title;
252 $html_url_qpart = "htmlUrl=\"$site_url\"";
254 $html_url_qpart = "";
257 print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
260 if ($cat_mode && $old_cat_title) {
261 print "</outline>\n";
264 # export tt-rss settings
266 if ($include_settings) {
267 print "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION
."\">";
269 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
270 profile IS NULL AND owner_uid = " . $_SESSION["uid"]);
272 while ($line = db_fetch_assoc($result)) {
274 $name = $line["pref_name"];
275 $value = htmlspecialchars($line["value"]);
277 print "<outline pref-name=\"$name\" value=\"$value\">";
286 print "</body></opml>";
289 // FIXME there are some brackets issues here
291 $op = $_REQUEST["op"];
292 if (!$op) $op = "Export";
294 $output_name = $_REQUEST["filename"];
295 if (!$output_name) $output_name = "TinyTinyRSS.opml";
297 $show_settings = $_REQUEST["settings"];
299 if ($op == "Export") {
301 login_sequence($link);
302 $owner_uid = $_SESSION["uid"];
303 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
306 if ($op == "publish"){
307 $key = db_escape_string($_REQUEST["key"]);
309 $result = db_query($link, "SELECT owner_uid
310 FROM ttrss_access_keys WHERE
311 access_key = '$key' AND feed_id = 'OPML:Publish'");
313 if (db_num_rows($result) == 1) {
314 $owner_uid = db_fetch_result($result, 0, "owner_uid");
315 return opml_export($link, "", $owner_uid, true, false);
317 print "<error>User not found</error>";
321 if ($op == "Import") {
323 login_sequence($link);
324 $owner_uid = $_SESSION["uid"];
326 header('Content-Type: text/html; charset=utf-8');
330 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
331 <title>".__("OPML Utility")."</title>
332 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
335 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
336 <h1>".__('OPML Utility')."</h1>";
338 db_query($link, "BEGIN");
340 /* create Imported feeds category just in case */
342 $result = db_query($link, "SELECT id FROM
343 ttrss_feed_categories WHERE title = 'Imported feeds' AND
344 owner_uid = '$owner_uid' LIMIT 1");
346 if (db_num_rows($result) == 0) {
347 db_query($link, "INSERT INTO ttrss_feed_categories
349 VALUES ('Imported feeds', '$owner_uid')");
352 db_query($link, "COMMIT");
354 print "<p>".__("Importing OPML...")."</p>";
355 opml_import_domdoc($link, $owner_uid);
357 print "<br><form method=\"GET\" action=\"prefs.php\">
358 <input type=\"submit\" value=\"".__("Return to preferences")."\">
361 print "</body></html>";
365 // if ($link) db_close($link);