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