]> git.wh0rd.org - tt-rss.git/blob - functions.php
support for web-demo mode (restricted rights)
[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 update_rss_feed($link, $feed_url, $feed) {
31
32 if (WEB_DEMO_MODE) return;
33
34 error_reporting(0);
35 $rss = fetch_rss($feed_url);
36 error_reporting (E_ERROR | E_WARNING | E_PARSE);
37
38 pg_query("BEGIN");
39
40 if ($rss) {
41
42 $result = pg_query("SELECT title FROM ttrss_feeds WHERE id = '$feed'");
43
44 $registered_title = pg_fetch_result($result, 0, "title");
45
46 if (!$registered_title) {
47 $feed_title = $rss->channel["title"];
48 pg_query("UPDATE ttrss_feeds SET title = '$feed_title' WHERE id = '$feed'");
49 }
50
51 foreach ($rss->items as $item) {
52
53 $entry_guid = $item["id"];
54
55 if (!$entry_guid) $entry_guid = $item["guid"];
56 if (!$entry_guid) $entry_guid = $item["link"];
57
58 if (!$entry_guid) continue;
59
60 $entry_timestamp = "";
61
62 $rss_2_date = $item['pubdate'];
63 $rss_1_date = $item['dc']['date'];
64 $atom_date = $item['issued'];
65
66 if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date);
67 if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date);
68 if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date);
69
70 if ($entry_timestamp == "") {
71 $entry_timestamp = time();
72 $no_orig_date = 'true';
73 } else {
74 $no_orig_date = 'false';
75 }
76
77 $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp);
78
79 $entry_title = $item["title"];
80 $entry_link = $item["link"];
81
82 if (!$entry_title) continue;
83 if (!$entry_link) continue;
84
85 $entry_content = $item["description"];
86 if (!$entry_content) $entry_content = $item["content:escaped"];
87 if (!$entry_content) $entry_content = $item["content"];
88
89 if (!$entry_content) continue;
90
91 $content_hash = "SHA1:" . sha1(strip_tags($entry_content));
92
93 $result = pg_query($link, "
94 SELECT
95 id,last_read,no_orig_date,title,feed_id,content_hash,
96 EXTRACT(EPOCH FROM updated) as updated_timestamp
97 FROM
98 ttrss_entries
99 WHERE
100 guid = '$entry_guid'");
101
102 if (pg_num_rows($result) == 0) {
103
104 $entry_content = pg_escape_string($entry_content);
105 $entry_title = pg_escape_string($entry_title);
106 $entry_link = pg_escape_string($entry_link);
107
108 $query = "INSERT
109 INTO ttrss_entries
110 (title,
111 guid,
112 link,
113 updated,
114 content,
115 content_hash,
116 feed_id,
117 no_orig_date)
118 VALUES
119 ('$entry_title',
120 '$entry_guid',
121 '$entry_link',
122 '$entry_timestamp_fmt',
123 '$entry_content',
124 '$content_hash',
125 '$feed',
126 $no_orig_date)";
127
128 $result = pg_query($link, $query);
129
130 } else {
131
132 $orig_entry_id = pg_fetch_result($result, 0, "id");
133 $orig_feed_id = pg_fetch_result($result, 0, "feed_id");
134
135 if ($orig_feed_id != $feed) {
136 // print "<p>Update from different feed ($orig_feed_id, $feed): $entry_guid [$entry_title]";
137 continue;
138 }
139
140 $orig_timestamp = pg_fetch_result($result, 0, "updated_timestamp");
141 $orig_content_hash = pg_fetch_result($result, 0, "content_hash");
142 $orig_last_read = pg_fetch_result($result, 0, "last_read");
143 $orig_no_orig_date = pg_fetch_result($result, 0, "no_orig_date");
144 $orig_title = pg_fetch_result($result, 0, "title");
145
146 if ($orig_title != $entry_title) {
147 $last_read_qpart = 'last_read = null,';
148 }
149
150 if ($orig_content_hash != $content_hash) {
151 $last_read_qpart = 'last_read = null,';
152 }
153
154 // if ($orig_timestamp < $entry_timestamp) {
155 // $last_read_qpart = 'last_read = null,';
156 // }
157
158 $entry_content = pg_escape_string($entry_content);
159 $entry_title = pg_escape_string($entry_title);
160 $entry_link = pg_escape_string($entry_link);
161
162 $query = "UPDATE ttrss_entries
163 SET
164 $last_read_qpart
165 title = '$entry_title',
166 link = '$entry_link',
167 updated = '$entry_timestamp_fmt',
168 content = '$entry_content',
169 content_hash = '$content_hash'
170 WHERE
171 id = '$orig_entry_id'";
172
173 $result = pg_query($link, $query);
174
175 }
176 }
177
178 if ($result) {
179 $result = pg_query($link, "UPDATE ttrss_feeds SET last_updated = NOW()");
180 }
181
182 }
183
184 pg_query("COMMIT");
185
186 }
187
188
189
190
191 ?>