]> git.wh0rd.org - tt-rss.git/blob - opml.php
place imported feeds in a separate category unless otherwise specified (closes #162)
[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 if (DB_TYPE == "pgsql") {
14 pg_query($link, "set client_encoding = 'utf-8'");
15 pg_set_client_encoding("UNICODE");
16 } else {
17 if (defined('MYSQL_CHARSET') && MYSQL_CHARSET) {
18 db_query($link, "SET NAMES " . MYSQL_CHARSET);
19 // db_query($link, "SET CHARACTER SET " . MYSQL_CHARSET);
20 }
21 }
22
23 login_sequence($link);
24
25 $owner_uid = $_SESSION["uid"];
26
27 function opml_export($link, $owner_uid) {
28 header("Content-type: application/xml+opml");
29 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
30
31 print "<opml version=\"1.0\">";
32 print "<head>
33 <dateCreated>" . date("r", time()) . "</dateCreated>
34 <title>Tiny Tiny RSS Feed Export</title>
35 </head>";
36 print "<body>";
37
38 $cat_mode = false;
39
40 if (get_pref($link, 'ENABLE_FEED_CATS')) {
41 $cat_mode = true;
42 $result = db_query($link, "SELECT
43 title,feed_url,site_url,
44 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title
45 FROM ttrss_feeds
46 WHERE
47 owner_uid = '$owner_uid'
48 ORDER BY cat_title,title");
49 } else {
50 $result = db_query($link, "SELECT * FROM ttrss_feeds
51 WHERE owner_uid = '$owner_uid' ORDER BY title");
52 }
53
54 $old_cat_title = "";
55
56 while ($line = db_fetch_assoc($result)) {
57 $title = htmlspecialchars($line["title"]);
58 $url = htmlspecialchars($line["feed_url"]);
59 $site_url = htmlspecialchars($line["site_url"]);
60
61 if ($cat_mode) {
62 $cat_title = htmlspecialchars($line["cat_title"]);
63
64 if ($old_cat_title != $cat_title) {
65 if ($old_cat_title) {
66 print "</outline>\n";
67 }
68
69 if ($cat_title) {
70 print "<outline title=\"$cat_title\">\n";
71 }
72
73 $old_cat_title = $cat_title;
74 }
75 }
76
77 if ($site_url) {
78 $html_url_qpart = "htmlUrl=\"$site_url\"";
79 } else {
80 $html_url_qpart = "";
81 }
82
83 print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
84 }
85
86 if ($cat_mode && $old_cat_title) {
87 print "</outline>\n";
88 }
89
90 print "</body></opml>";
91 }
92
93 // FIXME there are some brackets issues here
94
95 $op = $_REQUEST["op"];
96
97 if (!$op) $op = "Export";
98
99 if ($op == "Export") {
100 return opml_export($link, $owner_uid);
101 }
102
103 if ($op == "Import") {
104
105 print "<html>
106 <head>
107 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
108 <title>".__("OPML Utility")."</title>
109 </head>
110 <body>
111 <div class=\"floatingLogo\"><img src=\"images/ttrss_logo.png\"></div>
112 <h1>".__('OPML Utility')."</h1>";
113
114 db_query($link, "BEGIN");
115
116 /* create Imported feeds category just in case */
117
118 $result = db_query($link, "SELECT id FROM
119 ttrss_feed_categories WHERE title = 'Imported feeds' AND
120 owner_uid = '$owner_uid' LIMIT 1");
121
122 if (db_num_rows($result) == 0) {
123 db_query($link, "INSERT INTO ttrss_feed_categories
124 (title,owner_uid)
125 VALUES ('Imported feeds', '$owner_uid')");
126 }
127
128 db_query($link, "COMMIT");
129
130 /* Handle OPML import by DOMXML/DOMDocument */
131
132 if (function_exists('domxml_open_file')) {
133 print "<p>".__("Importing OPML (using DOMXML extension)...")."</p>";
134 require_once "modules/opml_domxml.php";
135 opml_import_domxml($link, $owner_uid);
136 } else if (PHP_VERSION >= 5) {
137 print "<p>".__("Importing OPML (using DOMDocument extension)...")."</p>";
138 require_once "modules/opml_domdoc.php";
139 opml_import_domdoc($link, $owner_uid);
140 } else {
141 print_error(__("DOMXML extension is not found. It is required for PHP versions below 5."));
142 }
143
144 print "<br><form method=\"GET\" action=\"prefs.php\">
145 <input type=\"submit\" value=\"".__("Return to preferences")."\">
146 </form>";
147
148 print "</body></html>";
149
150 }
151
152 // if ($link) db_close($link);
153
154 ?>