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