]> git.wh0rd.org - tt-rss.git/blob - functions.php
icons in feedlist
[tt-rss.git] / functions.php
1 <?
2 require_once 'config.php';
3
4 function update_all_feeds($link, $fetch) {
5
6 if (WEB_DEMO_MODE) return;
7
8 pg_query("BEGIN");
9
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 }
21
22 while ($line = pg_fetch_assoc($result)) {
23 update_rss_feed($link, $line["feed_url"], $line["id"]);
24 }
25
26 pg_query("COMMIT");
27
28 }
29
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
62 function update_rss_feed($link, $feed_url, $feed) {
63
64 if (WEB_DEMO_MODE) return;
65
66 error_reporting(0);
67 $rss = fetch_rss($feed_url);
68 error_reporting (E_ERROR | E_WARNING | E_PARSE);
69
70 pg_query("BEGIN");
71
72 if ($rss) {
73
74 if (ENABLE_FEED_ICONS) {
75 check_feed_favicon($feed_url, $feed);
76 }
77
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) {
83 $feed_title = $rss->channel["title"];
84 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
85 }
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"];
93
94 if (!$entry_guid) continue;
95
96 $entry_timestamp = "";
97
98 $rss_2_date = $item['pubdate'];
99 $rss_1_date = $item['dc']['date'];
100 $atom_date = $item['issued'];
101
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);
105
106 if ($entry_timestamp == "") {
107 $entry_timestamp = time();
108 $no_orig_date = 'true';
109 } else {
110 $no_orig_date = 'false';
111 }
112
113 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
114
115 $entry_title = $item["title"];
116 $entry_link = $item["link"];
117
118 if (!$entry_title) continue;
119 if (!$entry_link) continue;
120
121 $entry_content = $item["description"];
122 if (!$entry_content) $entry_content = $item["content:escaped"];
123 if (!$entry_content) $entry_content = $item["content"];
124
125 if (!$entry_content) continue;
126
127 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
128
129 $result = pg_query($link, "
130 SELECT
131 id,last_read,no_orig_date,title,feed_id,content_hash,
132 EXTRACT(EPOCH FROM updated) as updated_timestamp
133 FROM
134 ttrss_entries
135 WHERE
136 guid = '$entry_guid'");
137
138 if (pg_num_rows($result) == 0) {
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)
154 VALUES
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
164 $result = pg_query($link, $query);
165
166 } else {
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");
180 $orig_title = pg_fetch_result($result, 0, "title");
181
182 if ($orig_title != $entry_title) {
183 $last_read_qpart = 'last_read = null,';
184 }
185
186 if ($orig_content_hash != $content_hash) {
187 $last_read_qpart = 'last_read = null,';
188 }
189
190 // if ($orig_timestamp < $entry_timestamp) {
191 // $last_read_qpart = 'last_read = null,';
192 // }
193
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
198 $query = "UPDATE ttrss_entries
199 SET
200 $last_read_qpart
201 title = '$entry_title',
202 link = '$entry_link',
203 updated = '$entry_timestamp_fmt',
204 content = '$entry_content',
205 content_hash = '$content_hash'
206 WHERE
207 id = '$orig_entry_id'";
208
209 $result = pg_query($link, $query);
210
211 }
212 }
213
214 if ($result) {
215 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
216 }
217
218 }
219
220 pg_query("COMMIT");
221
222 }
223
224
225 ?>