]> git.wh0rd.org - tt-rss.git/blob - backend.php
functionality updates
[tt-rss.git] / backend.php
1 <?
2 header("Content-Type: application/xml");
3
4 include "config.php";
5
6 require_once('magpierss/rss_fetch.inc');
7
8 $link = pg_connect(DB_CONN);
9
10 pg_query("set client_encoding = 'utf-8'");
11
12 $op = $_GET["op"];
13
14 if ($op == "feeds") {
15
16 $result = pg_query("SELECT *,
17 (SELECT count(id) FROM ttrss_entries
18 WHERE feed_id = ttrss_feeds.id) AS total,
19 (SELECT count(id) FROM ttrss_entries
20 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
21 FROM ttrss_feeds ORDER BY title");
22
23 print "<table width=\"100%\">";
24
25 $lnum = 0;
26
27 while ($line = pg_fetch_assoc($result)) {
28
29 $feed = $line["title"];
30 $feed_id = $line["id"];
31
32 $total = $line["total"];
33 $unread = $line["unread"];
34
35 $class = ($lnum % 2) ? "even" : "odd";
36
37 // if ($lnum == 2 || $lnum == 0) $feed = "<b>$feed</b>";
38
39 if ($unread > 0) $class .= "Unread";
40
41 print "<tr class=\"$class\" id=\"FEEDR-$feed_id\">";
42
43 $feed = "<a href=\"javascript:viewfeed($feed_id, 0);\">$feed</a>";
44 print "<td id=\"FEEDN-$feed_id\">$feed</td>";
45 print "<td id=\"FEEDU-$feed_id\">$unread</td>";
46 print "<td id=\"FEEDT-$feed_id\">$total</td>";
47
48 print "</tr>";
49 ++$lnum;
50 }
51
52 print "</table>";
53
54 }
55
56 if ($op == "view") {
57
58 $id = $_GET["id"];
59
60 $result = pg_query("UPDATE ttrss_entries SET unread = false WHERE id = '$id'");
61
62 $result = pg_query("SELECT title,link,content FROM ttrss_entries
63 WHERE id = '$id'");
64
65 if ($result) {
66
67 $line = pg_fetch_assoc($result);
68
69 print "<table class=\"feedOverview\">";
70 print "<tr><td><b>Title:</b></td><td>".$line["title"]."</td></tr>";
71 print "<tr><td><b>Link:</b></td><td><a href=\"".$line["link"]."\">".$line["link"]."</a></td></tr>";
72
73 print "</table>";
74
75 print $line["content"];
76
77
78 }
79 }
80
81 if ($op == "viewfeed") {
82
83 $feed = $_GET["feed"];
84 $skip = $_GET["skip"];
85 $ext = $_GET["ext"];
86
87 if ($ext == "undefined") $ext = "";
88
89 $result = pg_query("SELECT * FROM ttrss_feeds WHERE id = '$feed'");
90
91 if ($result) {
92
93 $line = pg_fetch_assoc($result);
94
95 if (!$ext) {
96
97 $rss = fetch_rss($line["feed_url"]);
98
99 if ($rss) {
100
101 foreach ($rss->items as $item) {
102
103 $entry_guid = $item["id"];
104
105 if (!$entry_guid) $entry_guid = $item["guid"];
106 if (!$entry_guid) $entry_guid = $item["link"];
107
108 $entry_timestamp = $item["pubdate"];
109 if (!$entry_timestamp) $entry_timestamp = $item["modified"];
110 if (!$entry_timestamp) $entry_timestamp = $item["updated"];
111
112 $entry_timestamp = strtotime($entry_timestamp);
113
114 $entry_title = $item["title"];
115 $entry_link = $item["link"];
116
117 $entry_content = $item["description"];
118 if (!$entry_content) $entry_content = $item["content"];
119
120 $entry_content = pg_escape_string($entry_content);
121 $entry_title = pg_escape_string($entry_title);
122
123 $content_md5 = md5($entry_content);
124
125 $result = pg_query("
126 SELECT
127 id,unread,md5_hash
128 FROM
129 ttrss_entries
130 WHERE
131 guid = '$entry_guid' OR md5_hash = '$content_md5'");
132
133 if (pg_num_rows($result) == 0) {
134
135 $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
136
137 $query = "INSERT INTO ttrss_entries
138 (title, guid, link, updated, content, feed_id, md5_hash)
139 VALUES
140 ('$entry_title', '$entry_guid', '$entry_link',
141 '$entry_timestamp', '$entry_content', '$feed',
142 '$content_md5')";
143
144 pg_query($query);
145
146 } else {
147
148 $entry_id = pg_fetch_result($result, 0, "id");
149 $entry_timestamp = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
150
151 $unread = pg_fetch_result($result, 0, "unread");
152 $md5_hash = pg_fetch_result($result, 0, "md5_hash");
153
154 if ($md5_hash != $content_md5)
155 $unread = "false";
156
157 $query = "UPDATE ttrss_entries
158 SET
159 title ='$entry_title',
160 link = '$entry_link',
161 updated = '$entry_timestamp',
162 content = '$entry_content',
163 md5_hash = '$content_md5',
164 unread = '$unread'
165 WHERE
166 id = '$entry_id'";
167
168 $result = pg_query($query);
169
170 // print "$entry_guid - $entry_timestamp - $entry_title -
171 // $entry_link - $entry_id<br>";
172
173 }
174
175 }
176
177 }
178
179 } else {
180
181 if ($ext == "MarkAllRead") {
182
183 pg_query("UPDATE ttrss_entries SET unread = false
184 WHERE feed_id = '$feed'");
185 }
186
187 }
188 }
189
190 print "<table class=\"headlines\" width=\"100%\">";
191 print "<tr><td colspan=\"2\" class=\"title\">" . $line["title"] . "</td></tr>";
192
193 $result = pg_query("SELECT id,title,updated,unread,feed_id FROM
194 ttrss_entries WHERE
195 feed_id = '$feed' ORDER BY updated LIMIT ".HEADLINES_PER_PAGE." OFFSET $skip");
196
197 $lnum = 0;
198
199 while ($line = pg_fetch_assoc($result)) {
200
201 $class = ($lnum % 2) ? "even" : "odd";
202
203 if ($line["unread"] == "t")
204 $class .= "Unread";
205
206 $content_link = "<a href=\"javascript:view(".$line["id"].",".$line["feed_id"].");\">".$line["title"]."</a>";
207
208 print "<tr class='$class' id='RROW-".$line["id"]."'>";
209 print "<td class='headlineUpdated'>".$line["updated"]."</td>";
210 print "<td class='headlineTitle'>$content_link</td>";
211
212 print "</tr>";
213
214 ++$lnum;
215 }
216
217 print "<tr><td colspan=\"2\" class=\"headlineToolbar\">";
218
219 $next_skip = $skip + HEADLINES_PER_PAGE;
220 $prev_skip = $skip - HEADLINES_PER_PAGE;
221
222 print "<a class=\"button\"
223 href=\"javascript:viewfeed($feed, $prev_skip);\">Previous Page</a>";
224 print "&nbsp;";
225 print "<a class=\"button\"
226 href=\"javascript:viewfeed($feed, $next_skip);\">Next Page</a>";
227 print "&nbsp;&nbsp;&nbsp;";
228
229 print "<a class=\"button\"
230 href=\"javascript:viewfeed($feed, 0, 'MarkAllRead');\">Mark all as read</a>";
231
232 print "</td></tr>";
233 print "</table>";
234
235 $result = pg_query("SELECT id, (SELECT count(id) FROM ttrss_entries
236 WHERE feed_id = ttrss_feeds.id) AS total,
237 (SELECT count(id) FROM ttrss_entries
238 WHERE feed_id = ttrss_feeds.id AND unread = true) as unread
239 FROM ttrss_feeds WHERE id = '$feed'");
240
241 $total = pg_fetch_result($result, 0, "total");
242 $unread = pg_fetch_result($result, 0, "unread");
243
244 print "<div class=\"invisible\" id=\"FACTIVE\">$feed</div>";
245 print "<div class=\"invisible\" id=\"FTOTAL\">$total</div>";
246 print "<div class=\"invisible\" id=\"FUNREAD\">$unread</div>";
247
248 }
249
250
251 ?>