]>
Commit | Line | Data |
---|---|---|
9a4506c8 | 1 | <? |
5d3f29da AD |
2 | session_start(); |
3 | ||
66581886 AD |
4 | require_once "sanity_check.php"; |
5 | ||
9a4506c8 AD |
6 | // FIXME there are some brackets issues here |
7 | ||
9f311df6 AD |
8 | $op = $_REQUEST["op"]; |
9 | if ($op == "Export") { | |
9a4506c8 | 10 | header("Content-type: application/xml"); |
eac7480c | 11 | print "<?xml version=\"1.0\"?>"; |
9a4506c8 AD |
12 | } |
13 | ||
14 | require_once "config.php"; | |
8158c57a | 15 | require_once "db.php"; |
a0111294 | 16 | require_once "db-prefs.php"; |
9a4506c8 | 17 | |
e98a3f65 | 18 | $owner_uid = $_SESSION["uid"]; |
5d3f29da | 19 | |
8158c57a | 20 | $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); |
9f311df6 | 21 | |
8158c57a AD |
22 | if (DB_TYPE == "pgsql") { |
23 | pg_query($link, "set client_encoding = 'utf-8'"); | |
24 | } | |
9a4506c8 | 25 | |
9f311df6 | 26 | if ($op == "Export") { |
9a4506c8 AD |
27 | print "<opml version=\"1.0\">"; |
28 | print "<head><dateCreated>" . date("r", time()) . "</dateCreated></head>"; | |
29 | print "<body>"; | |
30 | ||
da49ccf5 AD |
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, | |
18d37445 AD |
38 | (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title |
39 | FROM ttrss_feeds | |
40 | ORDER BY cat_title,title"); | |
da49ccf5 AD |
41 | } else { |
42 | $result = db_query($link, "SELECT * FROM ttrss_feeds | |
43 | ORDER BY title"); | |
44 | } | |
45 | ||
46 | $old_cat_title = ""; | |
9a4506c8 | 47 | |
8158c57a | 48 | while ($line = db_fetch_assoc($result)) { |
6e0584e9 AD |
49 | $title = htmlspecialchars($line["title"]); |
50 | $url = htmlspecialchars($line["feed_url"]); | |
9a4506c8 | 51 | |
da49ccf5 AD |
52 | if ($cat_mode) { |
53 | $cat_title = htmlspecialchars($line["cat_title"]); | |
54 | ||
55 | if ($old_cat_title != $cat_title) { | |
56 | if ($old_cat_title) { | |
57 | print "</outline>"; | |
58 | } | |
59 | ||
d527e4cb | 60 | print "<outline title=\"$cat_title\">"; |
da49ccf5 AD |
61 | |
62 | $old_cat_title = $cat_title; | |
63 | } | |
64 | } | |
65 | ||
9a4506c8 AD |
66 | print "<outline text=\"$title\" xmlUrl=\"$url\"/>"; |
67 | } | |
68 | ||
da49ccf5 AD |
69 | if ($cat_mode && $old_cat_title) { |
70 | print "</outline>"; | |
71 | } | |
72 | ||
9a4506c8 AD |
73 | print "</body></opml>"; |
74 | } | |
75 | ||
e98a3f65 | 76 | if ($op == "Import") { |
8158c57a | 77 | |
e98a3f65 AD |
78 | print "<html> |
79 | <head> | |
80 | <link rel=\"stylesheet\" href=\"opml.css\" type=\"text/css\"> | |
81 | </head> | |
82 | <body><h1>Importing OPML...</h1> | |
83 | <div>"; | |
d212ce0e | 84 | |
e98a3f65 AD |
85 | if (WEB_DEMO_MODE) { |
86 | print "OPML import is disabled in demo-mode."; | |
87 | print "<p><a class=\"button\" href=\"prefs.php\"> | |
88 | Return to preferences</a></div></body></html>"; | |
d212ce0e | 89 | |
e98a3f65 AD |
90 | return; |
91 | } | |
9f311df6 | 92 | |
e98a3f65 AD |
93 | if (is_file($_FILES['opml_file']['tmp_name'])) { |
94 | $dom = domxml_open_file($_FILES['opml_file']['tmp_name']); | |
8b7395bb | 95 | |
e98a3f65 AD |
96 | if ($dom) { |
97 | $root = $dom->document_element(); | |
8b7395bb | 98 | |
e98a3f65 | 99 | $body = $root->get_elements_by_tagname('body'); |
8b7395bb | 100 | |
e98a3f65 AD |
101 | if ($body[0]) { |
102 | $body = $body[0]; | |
8b7395bb | 103 | |
e98a3f65 | 104 | $outlines = $body->get_elements_by_tagname('outline'); |
f179a35d | 105 | |
e98a3f65 | 106 | $active_category = ''; |
9f311df6 | 107 | |
e98a3f65 AD |
108 | foreach ($outlines as $outline) { |
109 | $feed_title = $outline->get_attribute('text'); | |
110 | $cat_title = $outline->get_attribute('title'); | |
111 | $feed_url = $outline->get_attribute('xmlUrl'); | |
9f311df6 | 112 | |
e98a3f65 AD |
113 | if ($cat_title) { |
114 | $active_category = $cat_title; | |
9f311df6 | 115 | |
e98a3f65 AD |
116 | db_query($link, "BEGIN"); |
117 | ||
118 | $result = db_query($link, "SELECT id FROM | |
119 | ttrss_feed_categories WHERE title = '$cat_title' AND | |
120 | owner_uid = '$owner_uid' LIMIT 1"); | |
9f311df6 | 121 | |
e98a3f65 | 122 | if (db_num_rows($result) == 0) { |
9f311df6 | 123 | |
e98a3f65 | 124 | print "Adding category <b>$cat_title</b>...<br>"; |
9f311df6 | 125 | |
e98a3f65 AD |
126 | db_query($link, "INSERT INTO ttrss_feed_categories |
127 | (title,owner_uid) VALUES ('$cat_title', '$owner_uid')"); | |
128 | } | |
7118b7ac | 129 | |
e98a3f65 AD |
130 | db_query($link, "COMMIT"); |
131 | } | |
9f311df6 | 132 | |
e98a3f65 | 133 | // print "$active_category : $feed_title : $xmlurl<br>"; |
9f311df6 | 134 | |
e98a3f65 | 135 | if (!$feed_title || !$feed_url) continue; |
9f311df6 | 136 | |
e98a3f65 | 137 | db_query($link, "BEGIN"); |
9f311df6 | 138 | |
e98a3f65 | 139 | $cat_id = null; |
eac7480c | 140 | |
e98a3f65 | 141 | if ($active_category) { |
9f311df6 | 142 | |
e98a3f65 AD |
143 | $result = db_query($link, "SELECT id FROM |
144 | ttrss_feed_categories WHERE title = '$active_category' AND | |
145 | owner_uid = '$owner_uid' LIMIT 1"); | |
eac7480c | 146 | |
e98a3f65 AD |
147 | if (db_num_rows($result) == 1) { |
148 | $cat_id = db_fetch_result($result, 0, "id"); | |
149 | } | |
150 | } | |
eac7480c | 151 | |
e98a3f65 AD |
152 | $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE |
153 | (title = '$feed_title' OR feed_url = '$feed_url') | |
154 | AND owner_uid = '$owner_uid'"); | |
9f311df6 | 155 | |
e98a3f65 | 156 | print "Feed <b>$feed_title</b> ($feed_url)... "; |
9f311df6 | 157 | |
e98a3f65 AD |
158 | if (db_num_rows($result) > 0) { |
159 | print " Already imported.<br>"; | |
160 | } else { | |
9f311df6 | 161 | |
e98a3f65 AD |
162 | if ($cat_id) { |
163 | $add_query = "INSERT INTO ttrss_feeds | |
164 | (title, feed_url, owner_uid, cat_id) VALUES | |
165 | ('$feed_title', '$feed_url', '$owner_uid', '$cat_id')"; | |
9f311df6 | 166 | |
e98a3f65 AD |
167 | } else { |
168 | $add_query = "INSERT INTO ttrss_feeds | |
169 | (title, feed_url, owner_uid) VALUES | |
170 | ('$feed_title', '$feed_url', '$owner_uid')"; | |
9f311df6 | 171 | |
e98a3f65 AD |
172 | } |
173 | ||
174 | db_query($link, $add_query); | |
175 | ||
176 | print "<b>Done.</b><br>"; | |
177 | } | |
9f311df6 | 178 | |
e98a3f65 | 179 | db_query($link, "COMMIT"); |
9f311df6 | 180 | } |
9f311df6 | 181 | |
e98a3f65 AD |
182 | } else { |
183 | print "Error: can't find body element."; | |
184 | } | |
9f311df6 | 185 | } else { |
e98a3f65 | 186 | print "Error while parsing document."; |
9f311df6 AD |
187 | } |
188 | ||
e98a3f65 | 189 | } else { |
9f311df6 AD |
190 | print "Error: please upload OPML file."; |
191 | } | |
192 | ||
193 | print "<p><a class=\"button\" href=\"prefs.php\"> | |
194 | Return to preferences</a>"; | |
195 | ||
196 | print "</div></body></html>"; | |
197 | ||
198 | } | |
199 | ||
3d477c2c | 200 | // if ($link) db_close($link); |
9f311df6 | 201 | |
9a4506c8 | 202 | ?> |