]> git.wh0rd.org - tt-rss.git/blob - opml.php
category support for OPML export (fix)
[tt-rss.git] / opml.php
1 <?
2 session_start();
3
4 require_once "sanity_check.php";
5
6 // FIXME there are some brackets issues here
7
8 $op = $_REQUEST["op"];
9 if ($op == "Export") {
10 header("Content-type: application/xml");
11 print "<?xml version=\"1.0\"?>";
12 }
13
14 require_once "config.php";
15 require_once "db.php";
16 require_once "db-prefs.php";
17
18 // $_SESSION["uid"] = PLACEHOLDER_UID; // FIXME: placeholder
19
20 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
21
22 if (DB_TYPE == "pgsql") {
23 pg_query($link, "set client_encoding = 'utf-8'");
24 }
25
26 if ($op == "Export") {
27 print "<opml version=\"1.0\">";
28 print "<head><dateCreated>" . date("r", time()) . "</dateCreated></head>";
29 print "<body>";
30
31 $cat_mode = false;
32
33 if (get_pref($link, 'ENABLE_FEED_CATS')) {
34 $cat_mode = true;
35 $result = db_query($link, "SELECT
36 ttrss_feeds.feed_url AS feed_url,
37 ttrss_feeds.title AS title,
38 ttrss_feed_categories.title AS cat_title
39 FROM ttrss_feeds,ttrss_feed_categories
40 WHERE
41 cat_id = ttrss_feed_categories.id
42 ORDER BY ttrss_feed_categories.title,ttrss_feeds.title");
43 } else {
44 $result = db_query($link, "SELECT * FROM ttrss_feeds
45 ORDER BY title");
46 }
47
48 $old_cat_title = "";
49
50 while ($line = db_fetch_assoc($result)) {
51 $title = htmlspecialchars($line["title"]);
52 $url = htmlspecialchars($line["feed_url"]);
53
54 if ($cat_mode) {
55 $cat_title = htmlspecialchars($line["cat_title"]);
56
57 if ($old_cat_title != $cat_title) {
58 if ($old_cat_title) {
59 print "</outline>";
60 }
61
62 print "<outline title=\"$cat_title\">";
63
64 $old_cat_title = $cat_title;
65 }
66 }
67
68 print "<outline text=\"$title\" xmlUrl=\"$url\"/>";
69 }
70
71 if ($cat_mode && $old_cat_title) {
72 print "</outline>";
73 }
74
75 print "</body></opml>";
76 }
77
78 function startElement($parser, $name, $attrs) {
79
80 if ($name == "OUTLINE") {
81 if ($name == "OUTLINE") {
82
83 $title = $attrs["TEXT"];
84 $url = $attrs["XMLURL"];
85
86 if (!$title) {
87 $title = $attrs['TITLE'];
88 }
89 }
90
91 /* this is suboptimal */
92
93 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
94
95 if (!$link) return;
96
97 $title = db_escape_string_2($title, $link);
98 $url = db_escape_string_2($url, $link);
99
100 if (!$title || !$url) return;
101
102 print "Feed <b>$title</b> ($url)... ";
103
104 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
105 (title = '$title' OR feed_url = '$url') AND owner_uid = ".$_SESSION["uid"]);
106
107 if ($result && db_num_rows($result) > 0) {
108
109 print " Already imported.<br>";
110
111 } else {
112
113 $result = db_query($link, "INSERT INTO ttrss_feeds (title, feed_url,owner_uid) VALUES
114 ('$title', '$url', '".$_SESSION["uid"]."')");
115
116 print "<b>Done.</b><br>";
117
118 }
119
120 if ($link) db_close($link);
121
122 }
123 }
124
125 function endElement($parser, $name) {
126
127
128 }
129
130 if ($op == "Import") {
131
132 print "<html>
133 <head>
134 <link rel=\"stylesheet\" href=\"opml.css\" type=\"text/css\">
135 </head>
136 <body><h1>Importing OPML...</h1>
137 <div>";
138
139 if (WEB_DEMO_MODE) {
140 print "OPML import is disabled in demo-mode.";
141 print "<p><a class=\"button\" href=\"prefs.php\">
142 Return to preferences</a></div></body></html>";
143
144 return;
145 }
146
147 if (is_file($_FILES['opml_file']['tmp_name'])) {
148
149 $xml_parser = xml_parser_create();
150
151 xml_set_element_handler($xml_parser, "startElement", "endElement");
152
153 $fp = fopen($_FILES['opml_file']['tmp_name'], "r");
154
155 if ($fp) {
156
157 while ($data = fread($fp, 4096)) {
158
159 if (!xml_parse($xml_parser, $data, feof($fp))) {
160
161 print sprintf("Unable to parse OPML file, XML error: %s at line %d",
162 xml_error_string(xml_get_error_code($xml_parser)),
163 xml_get_current_line_number($xml_parser));
164
165 print "<p><a class=\"button\" href=\"prefs.php\">
166 Return to preferences</a>";
167
168 return;
169
170 }
171 }
172
173 xml_parser_free($xml_parser);
174 fclose($fp);
175
176 } else {
177 print("Error: Could not open OPML input.");
178 }
179
180 } else {
181 print "Error: please upload OPML file.";
182 }
183
184 print "<p><a class=\"button\" href=\"prefs.php\">
185 Return to preferences</a>";
186
187 print "</div></body></html>";
188
189 }
190
191 // if ($link) db_close($link);
192
193 ?>