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 $attributes = $outline->attributes;
43 $feed_title = db_escape_string($attributes->getNamedItem('text')->nodeValue);
44 if (!$feed_title) $feed_title = db_escape_string($attributes->getNamedItem('title')->nodeValue);
46 $cat_title = db_escape_string($attributes->getNamedItem('title')->nodeValue);
47 if (!$cat_title) $cat_title = db_escape_string($attributes->getNamedItem('text')->nodeValue);
49 $feed_url = db_escape_string($attributes->getNamedItem('xmlUrl')->nodeValue);
50 if (!$feed_url) $feed_url = db_escape_string($attributes->getNamedItem('xmlURL')->nodeValue);
52 $site_url = db_escape_string($attributes->getNamedItem('htmlUrl')->nodeValue);
54 $pref_name = db_escape_string($attributes->getNamedItem('pref-name')->nodeValue);
55 $label_name = db_escape_string($attributes->getNamedItem('label-name')->nodeValue);
56 $filter_name = db_escape_string($attributes->getNamedItem('filter-name')->nodeValue);
58 if ($cat_title && !$feed_url) {
60 if ($cat_title != "tt-rss-prefs" && $cat_title != 'tt-rss-labels' && $cat_title != 'tt-rss-filters') {
62 db_query($link, "BEGIN");
64 $result = db_query($link, "SELECT id FROM
65 ttrss_feed_categories WHERE title = '$cat_title' AND
66 owner_uid = '$owner_uid' LIMIT 1");
68 if (db_num_rows($result) == 0) {
70 printf(__("<li>Adding category <b>%s</b>.</li>"), $cat_title);
72 db_query($link, "INSERT INTO ttrss_feed_categories
74 VALUES ('$cat_title', '$owner_uid')");
77 db_query($link, "COMMIT");
81 // print "$active_category : $feed_title : $feed_url<br>";
84 $parent_node = $outline->parentNode;
86 if ($parent_node && $parent_node->nodeName == "outline") {
87 $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
88 if ($cat_check == "tt-rss-prefs") {
89 $pref_value = db_escape_string($outline->attributes->getNamedItem('value')->nodeValue);
92 __("Setting preference key %s to %s")."</li>",
93 $pref_name, $pref_value);
95 set_pref($link, $pref_name, $pref_value);
102 $parent_node = $outline->parentNode;
104 if ($parent_node && $parent_node->nodeName == "outline") {
105 $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
106 if ($cat_check == "tt-rss-labels") {
108 $fg_color = db_escape_string($attributes->getNamedItem('label-fg-color')->nodeValue);
109 $bg_color = db_escape_string($attributes->getNamedItem('label-bg-color')->nodeValue);
111 if (!label_find_id($link, $label_name, $_SESSION['uid'])) {
112 printf("<li>".__("Adding label %s")."</li>", $label_name);
113 label_create($link, $label_name, $fg_color, $bg_color);
115 printf("<li>".__("Duplicate label: %s")."</li>", $label_name);
122 $parent_node = $outline->parentNode;
124 if ($parent_node && $parent_node->nodeName == "outline") {
125 $cat_check = $parent_node->attributes->getNamedItem('title')->nodeValue;
126 if ($cat_check == "tt-rss-filters") {
127 $filter = json_decode($outline->nodeValue, true);
138 if (!$feed_title || !$feed_url) continue;
140 db_query($link, "BEGIN");
144 $parent_node = $outline->parentNode;
146 if ($parent_node && $parent_node->nodeName == "outline") {
147 $element_category = $parent_node->attributes->getNamedItem('title')->nodeValue;
148 if (!$element_category) $element_category = $parent_node->attributes->getNamedItem('text')->nodeValue;
151 $element_category = '';
154 if ($element_category) {
156 $element_category = db_escape_string($element_category);
158 $result = db_query($link, "SELECT id FROM
159 ttrss_feed_categories WHERE title = '$element_category' AND
160 owner_uid = '$owner_uid' LIMIT 1");
162 if (db_num_rows($result) == 1) {
163 $cat_id = db_fetch_result($result, 0, "id");
167 $result = db_query($link, "SELECT id FROM ttrss_feeds WHERE
168 feed_url = '$feed_url'
169 AND owner_uid = '$owner_uid'");
171 print "<li><a target='_blank' href='$site_url'><b>$feed_title</b></a></b>
172 (<a target='_blank' href=\"$feed_url\">rss</a>) ";
174 if (db_num_rows($result) > 0) {
175 print __('is already imported.');
179 $add_query = "INSERT INTO ttrss_feeds
180 (title, feed_url, owner_uid, cat_id, site_url) VALUES
181 ('$feed_title', '$feed_url', '$owner_uid',
182 '$cat_id', '$site_url')";
185 $add_query = "INSERT INTO ttrss_feeds
186 (title, feed_url, owner_uid, cat_id, site_url) VALUES
187 ('$feed_title', '$feed_url', '$owner_uid', '$default_cat_id',
193 db_query($link, $add_query);
200 db_query($link, "COMMIT");
204 print_error(__('Error while parsing document.'));
208 print_error(__('Error: please upload OPML file.'));
214 function opml_export($link, $name, $owner_uid, $hide_private_feeds=false, $include_settings=true) {
215 if (!$_REQUEST["debug"]) {
216 header("Content-type: application/xml+opml");
217 header("Content-Disposition: attachment; filename=" . $name );
219 header("Content-type: text/xml");
222 print "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
224 print "<opml version=\"1.0\">";
226 <dateCreated>" . date("r", time()) . "</dateCreated>
227 <title>Tiny Tiny RSS Feed Export</title>
233 $select = "SELECT * ";
234 $where = "WHERE owner_uid = '$owner_uid'";
235 $orderby = "ORDER BY title";
236 if ($hide_private_feeds){
237 $where = "WHERE owner_uid = '$owner_uid' AND private IS false AND
238 auth_login = '' AND auth_pass = ''";
243 if (get_pref($link, 'ENABLE_FEED_CATS', $owner_uid) == true) {
246 title, feed_url, site_url,
247 (SELECT title FROM ttrss_feed_categories WHERE id = cat_id) as cat_title";
248 $orderby = "ORDER BY cat_title, title";
252 $cat_feed = get_pref($link, 'ENABLE_FEED_CATS');
253 print "<!-- feeding cats is not enabled -->";
254 print "<!-- $cat_feed -->";
259 $result = db_query($link, $select." FROM ttrss_feeds ".$where." ".$orderby);
263 while ($line = db_fetch_assoc($result)) {
264 $title = htmlspecialchars($line["title"]);
265 $url = htmlspecialchars($line["feed_url"]);
266 $site_url = htmlspecialchars($line["site_url"]);
269 $cat_title = htmlspecialchars($line["cat_title"]);
271 if ($old_cat_title != $cat_title) {
272 if ($old_cat_title) {
273 print "</outline>\n";
277 print "<outline title=\"$cat_title\" text=\"$cat_title\" >\n";
280 $old_cat_title = $cat_title;
285 $html_url_qpart = "htmlUrl=\"$site_url\"";
287 $html_url_qpart = "";
290 print "<outline text=\"$title\" xmlUrl=\"$url\" $html_url_qpart/>\n";
293 if ($cat_mode && $old_cat_title) {
294 print "</outline>\n";
297 # export tt-rss settings
299 if ($include_settings) {
300 print "<outline title=\"tt-rss-prefs\" schema-version=\"".SCHEMA_VERSION."\">";
302 $result = db_query($link, "SELECT pref_name, value FROM ttrss_user_prefs WHERE
303 profile IS NULL AND owner_uid = " . $_SESSION["uid"]);
305 while ($line = db_fetch_assoc($result)) {
307 $name = $line["pref_name"];
308 $value = htmlspecialchars($line["value"]);
310 print "<outline pref-name=\"$name\" value=\"$value\">";
318 print "<outline title=\"tt-rss-labels\" schema-version=\"".SCHEMA_VERSION."\">";
320 $result = db_query($link, "SELECT * FROM ttrss_labels2 WHERE
321 owner_uid = " . $_SESSION['uid']);
323 while ($line = db_fetch_assoc($result)) {
324 $name = htmlspecialchars($line['caption']);
325 $fg_color = htmlspecialchars($line['fg_color']);
326 $bg_color = htmlspecialchars($line['bg_color']);
328 print "<outline label-name=\"$name\" label-fg-color=\"$fg_color\" label-bg-color=\"$bg_color\"/>";
334 print "<outline title=\"tt-rss-filters\" schema-version=\"".SCHEMA_VERSION."\">";
336 $result = db_query($link, "SELECT filter_type,
344 ttrss_feeds.feed_url AS feed_url,
345 ttrss_feed_categories.title AS cat_title
347 LEFT JOIN ttrss_feeds ON (feed_id = ttrss_feeds.id)
348 LEFT JOIN ttrss_feed_categories ON (ttrss_filters.cat_id = ttrss_feed_categories.id)
350 ttrss_filters.owner_uid = " . $_SESSION['uid']);
352 while ($line = db_fetch_assoc($result)) {
353 $name = htmlspecialchars($line['reg_exp']);
355 foreach (array('enabled', 'inverse', 'cat_filter') as $b) {
356 $line[$b] = sql_bool_to_bool($line[$b]);
359 $filter = json_encode($line);
361 print "<outline filter-name=\"$name\">$filter</outline>";
369 print "</body></opml>";
372 // FIXME there are some brackets issues here
374 $op = $_REQUEST["op"];
375 if (!$op) $op = "Export";
377 $output_name = $_REQUEST["filename"];
378 if (!$output_name) $output_name = "TinyTinyRSS.opml";
380 $show_settings = $_REQUEST["settings"];
382 if ($op == "Export") {
384 login_sequence($link);
385 $owner_uid = $_SESSION["uid"];
386 return opml_export($link, $output_name, $owner_uid, false, ($show_settings == 1));
389 if ($op == "publish"){
390 $key = db_escape_string($_REQUEST["key"]);
392 $result = db_query($link, "SELECT owner_uid
393 FROM ttrss_access_keys WHERE
394 access_key = '$key' AND feed_id = 'OPML:Publish'");
396 if (db_num_rows($result) == 1) {
397 $owner_uid = db_fetch_result($result, 0, "owner_uid");
398 return opml_export($link, "", $owner_uid, true, false);
400 print "<error>User not found</error>";
404 if ($op == "Import") {
406 login_sequence($link);
407 $owner_uid = $_SESSION["uid"];
409 header('Content-Type: text/html; charset=utf-8');
413 <link rel=\"stylesheet\" href=\"utility.css\" type=\"text/css\">
414 <title>".__("OPML Utility")."</title>
415 <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/>
418 <div class=\"floatingLogo\"><img src=\"images/logo_wide.png\"></div>
419 <h1>".__('OPML Utility')."</h1>";
421 db_query($link, "BEGIN");
423 /* create Imported feeds category just in case */
425 $result = db_query($link, "SELECT id FROM
426 ttrss_feed_categories WHERE title = 'Imported feeds' AND
427 owner_uid = '$owner_uid' LIMIT 1");
429 if (db_num_rows($result) == 0) {
430 db_query($link, "INSERT INTO ttrss_feed_categories
432 VALUES ('Imported feeds', '$owner_uid')");
435 db_query($link, "COMMIT");
437 print "<p>".__("Importing OPML...")."</p>";
438 opml_import_domdoc($link, $owner_uid);
440 print "<br><form method=\"GET\" action=\"prefs.php\">
441 <input type=\"submit\" value=\"".__("Return to preferences")."\">
444 print "</body></html>";
448 // if ($link) db_close($link);