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