]> git.wh0rd.org - tt-rss.git/blob - functions.php
fixed bug in updated posts marking process
[tt-rss.git] / functions.php
1 <?
2 require_once 'config.php';
3
4 function purge_old_posts() {
5 if (PURGE_OLD_DAYS) {
6 $result = pg_query("DELETE FROM ttrss_entries WHERE
7 date_entered < NOW() - INTERVAL '30 days'");
8 }
9 }
10
11 function update_all_feeds($link, $fetch) {
12
13 if (WEB_DEMO_MODE) return;
14
15 pg_query("BEGIN");
16
17 if (!$fetch) {
18
19 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds WHERE
20 last_updated is null OR title = '' OR
21 EXTRACT(EPOCH FROM NOW()) - EXTRACT(EPOCH FROM last_updated) > " .
22 MIN_UPDATE_TIME);
23
24 } else {
25
26 $result = pg_query($link, "SELECT feed_url,id FROM ttrss_feeds");
27 }
28
29 while ($line = pg_fetch_assoc($result)) {
30 update_rss_feed($link, $line["feed_url"], $line["id"]);
31 }
32
33 purge_old_posts();
34
35 pg_query("COMMIT");
36
37 }
38
39 function check_feed_favicon($feed_url, $feed) {
40 $feed_url = str_replace("http://", "", $feed_url);
41 $feed_url = preg_replace("/\/.*$/", "", $feed_url);
42
43 $icon_url = "http://$feed_url/favicon.ico";
44 $icon_file = ICONS_DIR . "/$feed.ico";
45
46 if (!file_exists($icon_file)) {
47
48 error_reporting(0);
49 $r = fopen($icon_url, "r");
50 error_reporting (E_ERROR | E_WARNING | E_PARSE);
51
52 if ($r) {
53 $tmpfname = tempnam("/tmp", "ttrssicon");
54
55 $t = fopen($tmpfname, "w");
56
57 while (!feof($r)) {
58 $buf = fread($r, 16384);
59 fwrite($t, $buf);
60 }
61
62 fclose($r);
63 fclose($t);
64
65 error_reporting(0);
66 if (!rename($tmpfname, $icon_file)) {
67 unlink($tmpfname);
68 }
69 error_reporting (E_ERROR | E_WARNING | E_PARSE);
70
71 }
72 }
73 }
74
75 function update_rss_feed($link, $feed_url, $feed) {
76
77 if (WEB_DEMO_MODE) return;
78
79 error_reporting(0);
80 $rss = fetch_rss($feed_url);
81 error_reporting (E_ERROR | E_WARNING | E_PARSE);
82
83 pg_query("BEGIN");
84
85 if ($rss) {
86
87 if (ENABLE_FEED_ICONS) {
88 check_feed_favicon($feed_url, $feed);
89 }
90
91 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
92
93 $registered_title = pg_fetch_result($result, 0, "title");
94
95 if (!$registered_title) {
96 $feed_title = $rss->channel["title"];
97 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
98 }
99
100 foreach ($rss->items as $item) {
101
102 $entry_guid = $item["id"];
103
104 if (!$entry_guid) $entry_guid = $item["guid"];
105 if (!$entry_guid) $entry_guid = $item["link"];
106
107 if (!$entry_guid) continue;
108
109 $entry_timestamp = "";
110
111 $rss_2_date = $item['pubdate'];
112 $rss_1_date = $item['dc']['date'];
113 $atom_date = $item['issued'];
114
115 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
116 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
117 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
118
119 if ($entry_timestamp == "") {
120 $entry_timestamp = time();
121 $no_orig_date = 'true';
122 } else {
123 $no_orig_date = 'false';
124 }
125
126 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
127
128 $entry_title = $item["title"];
129 $entry_link = $item["link"];
130
131 if (!$entry_title) continue;
132 if (!$entry_link) continue;
133
134 $entry_content = $item["description"];
135 if (!$entry_content) $entry_content = $item["content:escaped"];
136 if (!$entry_content) $entry_content = $item["content"];
137
138 if (!$entry_content) continue;
139
140 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
141
142 $result = pg_query($link, "
143 SELECT
144 id,last_read,no_orig_date,title,feed_id,content_hash,
145 EXTRACT(EPOCH FROM updated) as updated_timestamp
146 FROM
147 ttrss_entries
148 WHERE
149 guid = '$entry_guid'");
150
151 if (pg_num_rows($result) == 0) {
152
153 $entry_content = pg_escape_string($entry_content);
154 $entry_title = pg_escape_string($entry_title);
155 $entry_link = pg_escape_string($entry_link);
156
157 $query = "INSERT
158 INTO ttrss_entries
159 (title,
160 guid,
161 link,
162 updated,
163 content,
164 content_hash,
165 feed_id,
166 no_orig_date)
167 VALUES
168 ('$entry_title',
169 '$entry_guid',
170 '$entry_link',
171 '$entry_timestamp_fmt',
172 '$entry_content',
173 '$content_hash',
174 '$feed',
175 $no_orig_date)";
176
177 $result = pg_query($link, $query);
178
179 } else {
180
181 $orig_entry_id = pg_fetch_result($result, 0, "id");
182 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
183
184 if ($orig_feed_id != $feed) {
185 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
186 continue;
187 }
188
189 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
190 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
191 $orig_last_read = pg_fetch_result($result, 0, "last_read");
192 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
193 $orig_title = pg_fetch_result($result, 0, "title");
194
195 $last_read_qpart = "";
196
197 // if ("$orig_title" != "$entry_title") {
198 // $last_read_qpart = 'last_read = null,';
199 // }
200
201 if (UPDATE_POST_ON_CHECKSUM_CHANGE &&
202 $orig_content_hash != $content_hash) {
203
204 print "$orig_content_hash : $content_hash";
205
206 $last_read_qpart = 'last_read = null,';
207 }
208
209 // if ($orig_timestamp < $entry_timestamp) {
210 // $last_read_qpart = 'last_read = null,';
211 // }
212
213 $entry_content = pg_escape_string($entry_content);
214 $entry_title = pg_escape_string($entry_title);
215 $entry_link = pg_escape_string($entry_link);
216
217 $query = "UPDATE ttrss_entries
218 SET
219 $last_read_qpart
220 title = '$entry_title',
221 link = '$entry_link',
222 updated = '$entry_timestamp_fmt',
223 content = '$entry_content',
224 content_hash = '$content_hash'
225 WHERE
226 id = '$orig_entry_id'";
227
228 $result = pg_query($link, $query);
229
230 }
231 }
232
233 if ($result) {
234 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
235 }
236
237 }
238
239 pg_query("COMMIT");
240
241 }
242
243
244 ?>