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