]> git.wh0rd.org - tt-rss.git/blame - utils/xml-export.php
add auth to xml-export if required, bump schema compatibility constants
[tt-rss.git] / utils / xml-export.php
CommitLineData
97aba8ec 1<?
9db0fded
AD
2 session_start();
3
cce5b59a 4 define('MAX_SCHEMA_VERSION', 5);
4f17b5f9 5
97aba8ec
AD
6 require_once "config.php";
7 require_once "functions.php";
8 require_once "db.php";
9
618f424e
AD
10 if ($_GET["export"]) {
11 header("Content-Type: application/xml");
12 }
cce5b59a
AD
13
14 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
15
16 if (!$link) {
17 if (DB_TYPE == "mysql") {
18 print mysql_error();
19 }
20 // PG seems to display its own errors just fine by default.
21 return;
22 }
23
24 if (DB_TYPE == "pgsql") {
25 pg_query("set client_encoding = 'utf-8'");
26 }
27
28 $result = db_query($link, "SELECT schema_version FROM ttrss_version");
29
30 $schema_version = db_fetch_result($result, 0, "schema_version");
31
32 if ($schema_version > 1) login_sequence($link);
33
97aba8ec
AD
34?>
35
618f424e
AD
36<? if (!$_GET["export"]) { ?>
37
38<html>
648cbec8
AD
39<head>
40 <title>XML Export</title>
41 <link rel="stylesheet" href="opml.css" type="text/css">
42</head>
618f424e 43<body>
648cbec8
AD
44<h1><img src="images/ttrss_logo.png"></h1>
45
46<div class="opmlBody">
47 <h2>XML Export</h2>
48 <form method="GET">
49 Limit to: <input type="checkbox" checked name="marked"> starred,
50 <input type="checkbox" name="unread"> unread.<br>
51 <p><input type="submit" class="button" name="export" value="Export"></p>
52 </form>
53</div>
54
618f424e
AD
55</body>
56</html>
57
58<? } else { ?>
59
97aba8ec
AD
60<xmldb>
61
62<?
97aba8ec 63
618f424e 64/* if ($schema_version != SCHEMA_VERSION) {
ebb510ce
AD
65 print "<error>Source database schema is invalid
66 (got version $schema_version; expected ".SCHEMA_VERSION.")</error>";
67 print "</xmldb>";
97aba8ec 68 return;
618f424e 69 } */
97aba8ec
AD
70
71 print "<schema_version>$schema_version</schema_version>";
72
eadcf397 73 if ($schema_version > 1) {
618f424e
AD
74 $owner_uid = $_SESSION["uid"];
75 print "<owner_uid>$owner_uid</owner_uid>";
76 }
97aba8ec 77
618f424e
AD
78 print "<exported>" . time() . "</exported>";
79?>
97aba8ec
AD
80
81<?
618f424e
AD
82 if ($_GET["marked"]) {
83 $marked_qpart = "AND marked = true";
84 }
97aba8ec 85
618f424e
AD
86 if ($_GET["unread"]) {
87 $unread_qpart = "AND unread = true";
88 }
97aba8ec 89
618f424e
AD
90 if ($schema_version == 1) {
91
92 $result = db_query($link, "SELECT
93 ttrss_entries.title AS title,
94 content,
95 marked,
96 unread,
97 updated,
98 guid,
99 link,
fa16f30b
AD
100 SUBSTRING(date_entered,1,16) AS date_entered,
101 SUBSTRING(last_read,1,16) AS last_read,
618f424e
AD
102 comments,
103 ttrss_feeds.feed_url AS feed_url,
104 ttrss_feeds.title AS feed_title
105 FROM
106 ttrss_entries,ttrss_feeds
107 WHERE
fa16f30b
AD
108 feed_id = ttrss_feeds.id $marked_qpart $unread_qpart
109 ORDER BY ttrss_entries.id");
618f424e 110
eadcf397 111 } else if ($schema_version >= 2 && $schema_version <= MAX_SCHEMA_VERSION) {
618f424e
AD
112
113 $result = db_query($link, "SELECT
114 ttrss_entries.title AS title,
115 content,
116 marked,
117 unread,
118 updated,
119 guid,
120 link,
fa16f30b
AD
121 SUBSTRING(date_entered,1,16) AS date_entered,
122 SUBSTRING(last_read,1,16) AS last_read,
618f424e
AD
123 comments,
124 ttrss_feeds.feed_url AS feed_url,
125 ttrss_feeds.title AS feed_title
126 FROM
127 ttrss_entries,ttrss_feeds,ttrss_user_entries
128 WHERE
129 ttrss_user_entries.owner_uid = '$owner_uid' AND
130 ref_id = ttrss_entries.id AND
fa16f30b
AD
131 feed_id = ttrss_feeds.id $marked_qpart $unread_qpart
132 ORDER BY ttrss_entries.id");
618f424e
AD
133
134 } else {
135
136 // BAD SCHEMA, NO COOKIE
137
138 print "<error>Source database schema is invalid
139 (got version $schema_version)</error>";
140 }
141
142 print "<total_articles>" . db_num_rows($result) . "</total_articles>";
143
144?>
145
146<articles>
147
148<?
97aba8ec
AD
149 while ($line = db_fetch_assoc($result)) {
150 print "<article>";
151
152 foreach (array_keys($line) as $key) {
618f424e
AD
153 $line[$key] = str_replace("<![CDATA[", "", $line[$key]);
154 $line[$key] = str_replace("]]>", "", $line[$key]);
155
97aba8ec
AD
156 print "<$key><![CDATA[".$line[$key]."]]></$key>";
157
158 }
159
160 print "</article>";
161 }
162
163?>
164</articles>
165
166</xmldb>
618f424e
AD
167
168<? } ?>