]> git.wh0rd.org Git - tt-rss.git/blob - functions.php
images moved to subdirectory, updated TODO
[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                                 error_reporting(0);
57                                 if (!rename($tmpfname, $icon_file)) {
58                                         unlink($tmpfname);
59                                 }
60                                 error_reporting (E_ERROR | E_WARNING | E_PARSE);
61
62                         }       
63                 }
64         }
65
66         function update_rss_feed($link, $feed_url, $feed) {
67
68                 if (WEB_DEMO_MODE) return;
69
70                 error_reporting(0);
71                 $rss = fetch_rss($feed_url);
72                 error_reporting (E_ERROR | E_WARNING | E_PARSE);
73
74                 pg_query("BEGIN");
75         
76                 if ($rss) {
77
78                         if (ENABLE_FEED_ICONS) {        
79                                 check_feed_favicon($feed_url, $feed);
80                         }
81                 
82                         $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
83
84                         $registered_title = pg_fetch_result($result, 0, "title");
85
86                         if (!$registered_title) {
87                                 $feed_title = $rss->channel["title"];
88                                 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
89                         }
90
91                         foreach ($rss->items as $item) {
92         
93                                 $entry_guid = $item["id"];
94         
95                                 if (!$entry_guid) $entry_guid = $item["guid"];
96                                 if (!$entry_guid) $entry_guid = $item["link"];
97
98                                 if (!$entry_guid) continue;
99         
100                                 $entry_timestamp = "";
101
102                                 $rss_2_date = $item['pubdate'];
103                                 $rss_1_date = $item['dc']['date'];
104                                 $atom_date = $item['issued'];
105                         
106                                 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
107                                 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
108                                 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
109                                 
110                                 if ($entry_timestamp == "") {
111                                         $entry_timestamp = time();
112                                         $no_orig_date = 'true';
113                                 } else {
114                                         $no_orig_date = 'false';
115                                 }
116
117                                 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
118
119                                 $entry_title = $item["title"];
120                                 $entry_link = $item["link"];
121
122                                 if (!$entry_title) continue;
123                                 if (!$entry_link) continue;
124
125                                 $entry_content = $item["description"];
126                                 if (!$entry_content) $entry_content = $item["content:escaped"];
127                                 if (!$entry_content) $entry_content = $item["content"];
128
129                                 if (!$entry_content) continue;
130
131                                 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
132
133                                 $result = pg_query($link, "
134                                         SELECT 
135                                                 id,last_read,no_orig_date,title,feed_id,content_hash,
136                                                 EXTRACT(EPOCH FROM updated) as updated_timestamp
137                                         FROM
138                                                 ttrss_entries 
139                                         WHERE
140                                                 guid = '$entry_guid'");
141
142                                 if (pg_num_rows($result) == 0) {
143
144                                         $entry_content = pg_escape_string($entry_content);
145                                         $entry_title = pg_escape_string($entry_title);
146                                         $entry_link = pg_escape_string($entry_link);
147
148                                         $query = "INSERT 
149                                                 INTO ttrss_entries 
150                                                         (title, 
151                                                         guid, 
152                                                         link, 
153                                                         updated, 
154                                                         content, 
155                                                         content_hash,
156                                                         feed_id, 
157                                                         no_orig_date) 
158                                                 VALUES
159                                                         ('$entry_title', 
160                                                         '$entry_guid', 
161                                                         '$entry_link', 
162                                                         '$entry_timestamp_fmt', 
163                                                         '$entry_content', 
164                                                         '$content_hash',
165                                                         '$feed', 
166                                                         $no_orig_date)";
167
168                                         $result = pg_query($link, $query);
169
170                                 } else {
171
172                                         $orig_entry_id = pg_fetch_result($result, 0, "id");                     
173                                         $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
174
175                                         if ($orig_feed_id != $feed) {
176 //                                              print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
177                                                 continue;
178                                         }
179                                         
180                                         $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
181                                         $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
182                                         $orig_last_read = pg_fetch_result($result, 0, "last_read");     
183                                         $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
184                                         $orig_title = pg_fetch_result($result, 0, "title");
185
186                                         if ($orig_title != $entry_title) {
187                                                 $last_read_qpart = 'last_read = null,';
188                                         }
189
190                                         if ($orig_content_hash != $content_hash) {
191                                                 $last_read_qpart = 'last_read = null,';
192                                         }
193
194 //                                      if ($orig_timestamp < $entry_timestamp) {
195 //                                              $last_read_qpart = 'last_read = null,';
196 //                                      }
197
198                                         $entry_content = pg_escape_string($entry_content);
199                                         $entry_title = pg_escape_string($entry_title);
200                                         $entry_link = pg_escape_string($entry_link);
201
202                                         $query = "UPDATE ttrss_entries 
203                                                 SET 
204                                                         $last_read_qpart 
205                                                         title = '$entry_title',
206                                                         link = '$entry_link', 
207                                                         updated = '$entry_timestamp_fmt',
208                                                         content = '$entry_content',
209                                                         content_hash = '$content_hash'
210                                                 WHERE
211                                                         id = '$orig_entry_id'";
212
213                                         $result = pg_query($link, $query);
214
215                                 }
216                         }
217
218                         if ($result) {
219                                 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
220                         }
221
222                 }
223
224                 pg_query("COMMIT");
225
226         }
227
228
229 ?>