]>
Commit | Line | Data |
---|---|---|
40d13c28 | 1 | <? |
f1a80dae | 2 | |
cce28758 AD |
3 | if ($_GET["debug"]) { |
4 | define('DEFAULT_ERROR_LEVEL', E_ALL); | |
5 | } else { | |
6 | define('DEFAULT_ERROR_LEVEL', E_ERROR | E_WARNING | E_PARSE); | |
7 | } | |
8 | ||
40d13c28 | 9 | require_once 'config.php'; |
b619ff15 | 10 | require_once 'db-prefs.php'; |
5bc0bd27 | 11 | require_once 'compat.php'; |
40d13c28 | 12 | |
387234f3 AD |
13 | require_once 'magpierss/rss_utils.inc'; |
14 | ||
a3ee2a38 AD |
15 | define('MAGPIE_OUTPUT_ENCODING', 'UTF-8'); |
16 | ||
fefa6ca3 | 17 | function purge_feed($link, $feed_id, $purge_interval) { |
4c193675 | 18 | |
fefa6ca3 | 19 | if (DB_TYPE == "pgsql") { |
35d8cf43 | 20 | db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 21 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 AD |
22 | (SELECT date_entered FROM ttrss_entries WHERE |
23 | id = ref_id) < NOW() - INTERVAL '$purge_interval days'"); | |
fefa6ca3 | 24 | } else { |
35d8cf43 | 25 | db_query($link, "DELETE FROM ttrss_user_entries WHERE |
fefa6ca3 | 26 | marked = false AND feed_id = '$feed_id' AND |
35d8cf43 AD |
27 | (SELECT date_entered FROM ttrss_entries WHERE |
28 | id = ref_id) < DATE_SUB(NOW(), INTERVAL $purge_interval DAY)"); | |
fefa6ca3 AD |
29 | } |
30 | } | |
31 | ||
32 | function global_purge_old_posts($link, $do_output = false) { | |
33 | ||
34 | $result = db_query($link, | |
35 | "SELECT id,purge_interval,owner_uid FROM ttrss_feeds"); | |
36 | ||
37 | while ($line = db_fetch_assoc($result)) { | |
38 | ||
39 | $feed_id = $line["id"]; | |
40 | $purge_interval = $line["purge_interval"]; | |
41 | $owner_uid = $line["owner_uid"]; | |
42 | ||
43 | if ($purge_interval == 0) { | |
44 | ||
45 | $tmp_result = db_query($link, | |
46 | "SELECT value FROM ttrss_user_prefs WHERE | |
47 | pref_name = 'PURGE_OLD_DAYS' AND owner_uid = '$owner_uid'"); | |
48 | ||
49 | if (db_num_rows($tmp_result) != 0) { | |
50 | $purge_interval = db_fetch_result($tmp_result, 0, "value"); | |
51 | } | |
52 | } | |
53 | ||
54 | if ($do_output) { | |
55 | print "<feed id='$feed_id' p_intl='$purge_interval'/>"; | |
56 | } | |
57 | ||
58 | if ($purge_interval > 0) { | |
59 | purge_feed($link, $feed_id, $purge_interval); | |
60 | } | |
61 | } | |
62 | ||
71604ca4 AD |
63 | // purge orphaned posts in main content table |
64 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
65 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
66 | ||
fefa6ca3 AD |
67 | } |
68 | ||
b6eefba5 | 69 | function purge_old_posts($link) { |
5d73494a | 70 | |
f1a80dae AD |
71 | $user_id = $_SESSION["uid"]; |
72 | ||
73 | $result = db_query($link, "SELECT id,purge_interval FROM ttrss_feeds | |
74 | WHERE owner_uid = '$user_id'"); | |
5d73494a AD |
75 | |
76 | while ($line = db_fetch_assoc($result)) { | |
77 | ||
78 | $feed_id = $line["id"]; | |
79 | $purge_interval = $line["purge_interval"]; | |
80 | ||
b619ff15 | 81 | if ($purge_interval == 0) $purge_interval = get_pref($link, 'PURGE_OLD_DAYS'); |
5d73494a | 82 | |
140aae81 | 83 | if ($purge_interval > 0) { |
fefa6ca3 | 84 | purge_feed($link, $feed_id, $purge_interval); |
5d73494a AD |
85 | } |
86 | } | |
71604ca4 AD |
87 | |
88 | // purge orphaned posts in main content table | |
89 | db_query($link, "DELETE FROM ttrss_entries WHERE | |
90 | (SELECT COUNT(int_id) FROM ttrss_user_entries WHERE ref_id = id) = 0"); | |
c3a8d71a AD |
91 | } |
92 | ||
1f2b01ed | 93 | function update_all_feeds($link, $fetch, $user_id = false, $force_daemon = false) { |
40d13c28 | 94 | |
4769ddaf | 95 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 96 | |
a2770077 AD |
97 | if (!$user_id) { |
98 | $user_id = $_SESSION["uid"]; | |
99 | purge_old_posts($link); | |
100 | } | |
101 | ||
25af8dad | 102 | // db_query($link, "BEGIN"); |
b82af8c3 | 103 | |
cbd8650d AD |
104 | if (MAX_UPDATE_TIME > 0) { |
105 | if (DB_TYPE == "mysql") { | |
106 | $q_order = "RAND()"; | |
107 | } else { | |
108 | $q_order = "RANDOM()"; | |
109 | } | |
110 | } else { | |
111 | $q_order = "last_updated DESC"; | |
112 | } | |
113 | ||
d148926e | 114 | $result = db_query($link, "SELECT feed_url,id, |
798f722b | 115 | SUBSTRING(last_updated,1,19) AS last_updated, |
5c563acd | 116 | update_interval FROM ttrss_feeds WHERE owner_uid = '$user_id' |
cbd8650d AD |
117 | ORDER BY $q_order"); |
118 | ||
119 | $upd_start = time(); | |
40d13c28 | 120 | |
b6eefba5 | 121 | while ($line = db_fetch_assoc($result)) { |
d148926e AD |
122 | $upd_intl = $line["update_interval"]; |
123 | ||
b619ff15 | 124 | if (!$upd_intl || $upd_intl == 0) { |
a2770077 | 125 | $upd_intl = get_pref($link, 'DEFAULT_UPDATE_INTERVAL', $user_id); |
b619ff15 | 126 | } |
d148926e | 127 | |
93d40f50 AD |
128 | if ($fetch || (!$line["last_updated"] || |
129 | time() - strtotime($line["last_updated"]) > ($upd_intl * 60))) { | |
c5142cca | 130 | |
cbd8650d AD |
131 | // print "<!-- feed: ".$line["feed_url"]." -->"; |
132 | ||
1f2b01ed | 133 | update_rss_feed($link, $line["feed_url"], $line["id"], $force_daemon); |
cbd8650d AD |
134 | |
135 | $upd_elapsed = time() - $upd_start; | |
136 | ||
137 | if (MAX_UPDATE_TIME > 0 && $upd_elapsed > MAX_UPDATE_TIME) { | |
138 | return; | |
139 | } | |
d148926e | 140 | } |
40d13c28 AD |
141 | } |
142 | ||
25af8dad | 143 | // db_query($link, "COMMIT"); |
b82af8c3 | 144 | |
40d13c28 AD |
145 | } |
146 | ||
9e997874 | 147 | function check_feed_favicon($feed_url, $feed, $link) { |
78800912 AD |
148 | $feed_url = str_replace("http://", "", $feed_url); |
149 | $feed_url = preg_replace("/\/.*$/", "", $feed_url); | |
150 | ||
151 | $icon_url = "http://$feed_url/favicon.ico"; | |
273a2f6b | 152 | $icon_file = ICONS_DIR . "/$feed.ico"; |
78800912 AD |
153 | |
154 | if (!file_exists($icon_file)) { | |
e695fdc8 | 155 | |
78800912 AD |
156 | error_reporting(0); |
157 | $r = fopen($icon_url, "r"); | |
cce28758 | 158 | error_reporting (DEFAULT_ERROR_LEVEL); |
78800912 AD |
159 | |
160 | if ($r) { | |
7d7cbaf5 | 161 | $tmpfname = tempnam(TMP_DIRECTORY, "ttrssicon"); |
78800912 AD |
162 | |
163 | $t = fopen($tmpfname, "w"); | |
164 | ||
165 | while (!feof($r)) { | |
166 | $buf = fread($r, 16384); | |
167 | fwrite($t, $buf); | |
168 | } | |
169 | ||
170 | fclose($r); | |
171 | fclose($t); | |
172 | ||
e695fdc8 AD |
173 | error_reporting(0); |
174 | if (!rename($tmpfname, $icon_file)) { | |
175 | unlink($tmpfname); | |
176 | } | |
717f5e64 AD |
177 | |
178 | chmod($icon_file, 0644); | |
179 | ||
cce28758 | 180 | error_reporting (DEFAULT_ERROR_LEVEL); |
78800912 AD |
181 | |
182 | } | |
183 | } | |
184 | } | |
185 | ||
ddb68b81 | 186 | function update_rss_feed($link, $feed_url, $feed, $ignore_daemon = false) { |
40d13c28 | 187 | |
4769ddaf | 188 | if (WEB_DEMO_MODE) return; |
b0b4abcf | 189 | |
ddb68b81 | 190 | if (DAEMON_REFRESH_ONLY && !$_GET["daemon"] && !$ignore_daemon) { |
21cfcdf2 AD |
191 | return; |
192 | } | |
193 | ||
47c6c988 | 194 | $result = db_query($link, "SELECT update_interval,auth_login,auth_pass |
a88c1f36 AD |
195 | FROM ttrss_feeds WHERE id = '$feed'"); |
196 | ||
47c6c988 AD |
197 | $auth_login = db_fetch_result($result, 0, "auth_login"); |
198 | $auth_pass = db_fetch_result($result, 0, "auth_pass"); | |
199 | ||
a88c1f36 AD |
200 | $update_interval = db_fetch_result($result, 0, "update_interval"); |
201 | ||
202 | if ($update_interval < 0) { return; } | |
203 | ||
ab3d0b99 AD |
204 | $feed = db_escape_string($feed); |
205 | ||
47c6c988 AD |
206 | $fetch_url = $feed_url; |
207 | ||
208 | if ($auth_login && $auth_pass) { | |
209 | $url_parts = array(); | |
210 | preg_match("/(^[^:]*):\/\/(.*)/", $fetch_url, $url_parts); | |
211 | ||
212 | if ($url_parts[1] && $url_parts[2]) { | |
213 | $fetch_url = $url_parts[1] . "://$auth_login:$auth_pass@" . $url_parts[2]; | |
214 | } | |
215 | ||
216 | } | |
3ad5aa85 | 217 | error_reporting(0); |
47c6c988 | 218 | $rss = fetch_rss($fetch_url); |
ab3d0b99 | 219 | |
cce28758 | 220 | error_reporting (DEFAULT_ERROR_LEVEL); |
76798ff3 | 221 | |
b6eefba5 | 222 | $feed = db_escape_string($feed); |
dcee8f61 | 223 | |
40d13c28 | 224 | if ($rss) { |
b82af8c3 | 225 | |
dd8c76a9 AD |
226 | db_query($link, "BEGIN"); |
227 | ||
a88c1f36 | 228 | $result = db_query($link, "SELECT title,icon_url,site_url,owner_uid |
f324892e | 229 | FROM ttrss_feeds WHERE id = '$feed'"); |
331900c6 | 230 | |
b6eefba5 AD |
231 | $registered_title = db_fetch_result($result, 0, "title"); |
232 | $orig_icon_url = db_fetch_result($result, 0, "icon_url"); | |
f324892e | 233 | $orig_site_url = db_fetch_result($result, 0, "site_url"); |
331900c6 | 234 | |
7fed1940 AD |
235 | $owner_uid = db_fetch_result($result, 0, "owner_uid"); |
236 | ||
a2770077 AD |
237 | if (get_pref($link, 'ENABLE_FEED_ICONS', $owner_uid)) { |
238 | check_feed_favicon($feed_url, $feed, $link); | |
239 | } | |
240 | ||
746b249f | 241 | if (!$registered_title || $registered_title == "[Unknown]") { |
e1305a97 | 242 | $feed_title = db_escape_string($rss->channel["title"]); |
f324892e AD |
243 | db_query($link, "UPDATE ttrss_feeds SET |
244 | title = '$feed_title' WHERE id = '$feed'"); | |
245 | } | |
246 | ||
147f7691 | 247 | $site_url = $rss->channel["link"]; |
832b7bfc AD |
248 | // weird, weird Magpie |
249 | if (!$site_url) $site_url = db_escape_string($rss->channel["link_"]); | |
147f7691 AD |
250 | |
251 | if ($site_url && $orig_site_url != db_escape_string($site_url)) { | |
f324892e AD |
252 | db_query($link, "UPDATE ttrss_feeds SET |
253 | site_url = '$site_url' WHERE id = '$feed'"); | |
331900c6 | 254 | } |
40d13c28 | 255 | |
b7f4bda2 AD |
256 | // print "I: " . $rss->channel["image"]["url"]; |
257 | ||
258 | $icon_url = $rss->image["url"]; | |
259 | ||
147f7691 | 260 | if ($icon_url && !$orig_icon_url != db_escape_string($icon_url)) { |
b6eefba5 AD |
261 | $icon_url = db_escape_string($icon_url); |
262 | db_query($link, "UPDATE ttrss_feeds SET icon_url = '$icon_url' WHERE id = '$feed'"); | |
b7f4bda2 AD |
263 | } |
264 | ||
e6155a06 AD |
265 | |
266 | $filters = array(); | |
267 | ||
4b3dff6e | 268 | $result = db_query($link, "SELECT reg_exp, |
db42b934 AD |
269 | ttrss_filter_types.name AS name, |
270 | ttrss_filter_actions.name AS action | |
271 | FROM ttrss_filters,ttrss_filter_types,ttrss_filter_actions WHERE | |
272 | owner_uid = $owner_uid AND | |
273 | ttrss_filter_types.id = filter_type AND | |
274 | ttrss_filter_actions.id = action_id AND | |
ead60402 | 275 | (feed_id IS NULL OR feed_id = '$feed')"); |
e6155a06 | 276 | |
b6eefba5 | 277 | while ($line = db_fetch_assoc($result)) { |
e6155a06 | 278 | if (!$filters[$line["name"]]) $filters[$line["name"]] = array(); |
19c9cb11 AD |
279 | |
280 | $filter["reg_exp"] = $line["reg_exp"]; | |
281 | $filter["action"] = $line["action"]; | |
282 | ||
283 | array_push($filters[$line["name"]], $filter); | |
e6155a06 AD |
284 | } |
285 | ||
ddb68b81 AD |
286 | $iterator = $rss->items; |
287 | ||
288 | if (!$iterator) $iterator = $rss->entries; | |
289 | if (!$iterator) $iterator = $rss; | |
290 | ||
291 | foreach ($iterator as $item) { | |
40d13c28 AD |
292 | |
293 | $entry_guid = $item["id"]; | |
294 | ||
295 | if (!$entry_guid) $entry_guid = $item["guid"]; | |
296 | if (!$entry_guid) $entry_guid = $item["link"]; | |
466001c4 AD |
297 | |
298 | if (!$entry_guid) continue; | |
a116f569 | 299 | |
9c9c7e6b | 300 | $entry_timestamp = ""; |
b82af8c3 | 301 | |
9c9c7e6b AD |
302 | $rss_2_date = $item['pubdate']; |
303 | $rss_1_date = $item['dc']['date']; | |
304 | $atom_date = $item['issued']; | |
59ba2c75 | 305 | if (!$atom_date) $atom_date = $item['updated']; |
b197f117 | 306 | |
9c9c7e6b AD |
307 | if ($atom_date != "") $entry_timestamp = parse_w3cdtf($atom_date); |
308 | if ($rss_1_date != "") $entry_timestamp = parse_w3cdtf($rss_1_date); | |
309 | if ($rss_2_date != "") $entry_timestamp = strtotime($rss_2_date); | |
b82af8c3 AD |
310 | |
311 | if ($entry_timestamp == "") { | |
312 | $entry_timestamp = time(); | |
313 | $no_orig_date = 'true'; | |
466001c4 AD |
314 | } else { |
315 | $no_orig_date = 'false'; | |
b82af8c3 | 316 | } |
b197f117 | 317 | |
466001c4 | 318 | $entry_timestamp_fmt = strftime("%Y/%m/%d %H:%M:%S", $entry_timestamp); |
71ad3959 | 319 | |
40d13c28 | 320 | $entry_title = $item["title"]; |
ddb68b81 AD |
321 | |
322 | // strange Magpie workaround | |
323 | $entry_link = $item["link_"]; | |
324 | if (!$entry_link) $entry_link = $item["link"]; | |
71ad3959 AD |
325 | |
326 | if (!$entry_title) continue; | |
327 | if (!$entry_link) continue; | |
328 | ||
1696229f AD |
329 | $entry_content = $item["content:escaped"]; |
330 | ||
331 | if (!$entry_content) $entry_content = $item["content:encoded"]; | |
40d13c28 | 332 | if (!$entry_content) $entry_content = $item["content"]; |
372ced8b | 333 | if (!$entry_content) $entry_content = $item["summary"]; |
1696229f | 334 | if (!$entry_content) $entry_content = $item["description"]; |
a2015351 | 335 | |
a116f569 | 336 | // if (!$entry_content) continue; |
a2015351 | 337 | |
8add756a AD |
338 | // WTF |
339 | if (is_array($entry_content)) { | |
340 | $entry_content = $entry_content["encoded"]; | |
1696229f | 341 | if (!$entry_content) $entry_content = $entry_content["escaped"]; |
8add756a AD |
342 | } |
343 | ||
1696229f | 344 | // print_r($item); |
372ced8b AD |
345 | // print_r(htmlspecialchars($entry_content)); |
346 | // print "<br>"; | |
1696229f | 347 | |
372ced8b | 348 | $entry_content_unescaped = $entry_content; |
466001c4 | 349 | $content_hash = "SHA1:" . sha1(strip_tags($entry_content)); |
cb0bd8bd | 350 | |
a1ea1e12 AD |
351 | $entry_comments = $item["comments"]; |
352 | ||
b6eefba5 | 353 | $entry_guid = db_escape_string($entry_guid); |
2651fc4f | 354 | |
05732aa0 AD |
355 | $result = db_query($link, "SELECT id FROM ttrss_entries |
356 | WHERE guid = '$entry_guid'"); | |
4c193675 | 357 | |
b17fcb1a AD |
358 | $entry_content = db_escape_string($entry_content); |
359 | $entry_title = db_escape_string($entry_title); | |
360 | $entry_link = db_escape_string($entry_link); | |
361 | $entry_comments = db_escape_string($entry_comments); | |
362 | ||
27f089dc | 363 | $num_comments = db_escape_string($item["slash"]["comments"]); |
11b0dce2 | 364 | |
e31073bd AD |
365 | if (!$num_comments) $num_comments = 0; |
366 | ||
4c193675 AD |
367 | if (db_num_rows($result) == 0) { |
368 | ||
369 | // base post entry does not exist, create it | |
370 | ||
4c193675 AD |
371 | $result = db_query($link, |
372 | "INSERT INTO ttrss_entries | |
373 | (title, | |
374 | guid, | |
375 | link, | |
376 | updated, | |
377 | content, | |
378 | content_hash, | |
379 | no_orig_date, | |
380 | date_entered, | |
11b0dce2 AD |
381 | comments, |
382 | num_comments) | |
4c193675 AD |
383 | VALUES |
384 | ('$entry_title', | |
385 | '$entry_guid', | |
386 | '$entry_link', | |
387 | '$entry_timestamp_fmt', | |
388 | '$entry_content', | |
389 | '$content_hash', | |
390 | $no_orig_date, | |
391 | NOW(), | |
11b0dce2 AD |
392 | '$entry_comments', |
393 | '$num_comments')"); | |
8926aab8 AD |
394 | } else { |
395 | // we keep encountering the entry in feeds, so we need to | |
396 | // update date_entered column so that we don't get horrible | |
397 | // dupes when the entry gets purged and reinserted again e.g. | |
398 | // in the case of SLOW SLOW OMG SLOW updating feeds | |
399 | ||
400 | $base_entry_id = db_fetch_result($result, 0, "id"); | |
401 | ||
402 | db_query($link, "UPDATE ttrss_entries SET date_entered = NOW() | |
403 | WHERE id = '$base_entry_id'"); | |
4c193675 AD |
404 | } |
405 | ||
406 | // now it should exist, if not - bad luck then | |
407 | ||
6385315d AD |
408 | $result = db_query($link, "SELECT |
409 | id,content_hash,no_orig_date,title, | |
8926aab8 | 410 | substring(date_entered,1,19) as date_entered, |
11b0dce2 AD |
411 | substring(updated,1,19) as updated, |
412 | num_comments | |
6385315d AD |
413 | FROM |
414 | ttrss_entries | |
415 | WHERE guid = '$entry_guid'"); | |
4c193675 AD |
416 | |
417 | if (db_num_rows($result) == 1) { | |
418 | ||
11b0dce2 AD |
419 | // this will be used below in update handler |
420 | $orig_content_hash = db_fetch_result($result, 0, "content_hash"); | |
421 | $orig_title = db_fetch_result($result, 0, "title"); | |
422 | $orig_num_comments = db_fetch_result($result, 0, "num_comments"); | |
8926aab8 AD |
423 | $orig_date_entered = strtotime(db_fetch_result($result, |
424 | 0, "date_entered")); | |
6385315d | 425 | |
11b0dce2 | 426 | $ref_id = db_fetch_result($result, 0, "id"); |
4c193675 | 427 | |
11b0dce2 | 428 | // check for user post link to main table |
4c193675 | 429 | |
11b0dce2 AD |
430 | // do we allow duplicate posts with same GUID in different feeds? |
431 | if (get_pref($link, "ALLOW_DUPLICATE_POSTS", $owner_uid)) { | |
432 | $dupcheck_qpart = "AND feed_id = '$feed'"; | |
433 | } else { | |
434 | $dupcheck_qpart = ""; | |
435 | } | |
71604ca4 | 436 | |
11b0dce2 | 437 | // error_reporting(0); |
19c9cb11 | 438 | |
11b0dce2 AD |
439 | $filter_name = get_filter_name($entry_title, $entry_content, |
440 | $entry_link, $filters); | |
19c9cb11 | 441 | |
11b0dce2 AD |
442 | if ($filter_name == "filter") { |
443 | continue; | |
444 | } | |
19c9cb11 | 445 | |
11b0dce2 | 446 | // error_reporting (DEFAULT_ERROR_LEVEL); |
3a933f22 | 447 | |
11b0dce2 AD |
448 | $result = db_query($link, |
449 | "SELECT ref_id FROM ttrss_user_entries WHERE | |
450 | ref_id = '$ref_id' AND owner_uid = '$owner_uid' | |
451 | $dupcheck_qpart"); | |
452 | ||
453 | // okay it doesn't exist - create user entry | |
454 | if (db_num_rows($result) == 0) { | |
455 | ||
456 | if ($filter_name != 'catchup') { | |
457 | $unread = 'true'; | |
458 | $last_read_qpart = 'NULL'; | |
459 | } else { | |
460 | $unread = 'false'; | |
461 | $last_read_qpart = 'NOW()'; | |
462 | } | |
19c9cb11 | 463 | |
11b0dce2 AD |
464 | $result = db_query($link, |
465 | "INSERT INTO ttrss_user_entries | |
466 | (ref_id, owner_uid, feed_id, unread, last_read) | |
467 | VALUES ('$ref_id', '$owner_uid', '$feed', $unread, | |
468 | $last_read_qpart)"); | |
469 | } | |
470 | ||
6385315d AD |
471 | $post_needs_update = false; |
472 | ||
a2770077 | 473 | if (get_pref($link, "UPDATE_POST_ON_CHECKSUM_CHANGE", $owner_uid) && |
6385315d AD |
474 | ($content_hash != $orig_content_hash)) { |
475 | $post_needs_update = true; | |
476 | } | |
477 | ||
478 | if ($orig_title != $entry_title) { | |
479 | $post_needs_update = true; | |
480 | } | |
481 | ||
11b0dce2 AD |
482 | if ($orig_num_comments != $num_comments) { |
483 | $post_needs_update = true; | |
484 | } | |
485 | ||
6385315d AD |
486 | // this doesn't seem to be very reliable |
487 | // | |
488 | // if ($orig_timestamp != $entry_timestamp && !$orig_no_orig_date) { | |
489 | // $post_needs_update = true; | |
490 | // } | |
491 | ||
492 | // if post needs update, update it and mark all user entries | |
1c73bc0c | 493 | // linking to this post as updated |
6385315d AD |
494 | if ($post_needs_update) { |
495 | ||
496 | // print "<!-- post $orig_title needs update : $post_needs_update -->"; | |
497 | ||
6385315d | 498 | db_query($link, "UPDATE ttrss_entries |
11b0dce2 AD |
499 | SET title = '$entry_title', content = '$entry_content', |
500 | num_comments = '$num_comments' | |
6385315d AD |
501 | WHERE id = '$ref_id'"); |
502 | ||
503 | db_query($link, "UPDATE ttrss_user_entries | |
504 | SET last_read = null WHERE ref_id = '$ref_id' AND unread = false"); | |
505 | ||
506 | } | |
4c193675 AD |
507 | } |
508 | ||
eb36b4eb AD |
509 | /* taaaags */ |
510 | // <a href="http://technorati.com/tag/Xorg" rel="tag">Xorg</a>, // | |
511 | ||
05732aa0 | 512 | $entry_tags = null; |
eb36b4eb | 513 | |
372ced8b | 514 | preg_match_all("/<a.*?href=.http:\/\/.*?technorati.com\/tag\/([^\"\'>]+)/i", |
ee2c3050 AD |
515 | $entry_content_unescaped, $entry_tags); |
516 | ||
517 | // print "<br>$entry_title : $entry_content_unescaped<br>"; | |
518 | // print_r($entry_tags); | |
372ced8b | 519 | // print "<br>"; |
eb36b4eb AD |
520 | |
521 | $entry_tags = $entry_tags[1]; | |
522 | ||
523 | if (count($entry_tags) > 0) { | |
524 | ||
05732aa0 AD |
525 | $result = db_query($link, "SELECT id,int_id |
526 | FROM ttrss_entries,ttrss_user_entries | |
25da6909 | 527 | WHERE guid = '$entry_guid' |
05732aa0 | 528 | AND feed_id = '$feed' AND ref_id = id |
7fed1940 | 529 | AND owner_uid = '$owner_uid'"); |
eb36b4eb | 530 | |
fe99ab12 | 531 | if (db_num_rows($result) == 1) { |
eb36b4eb | 532 | |
fe99ab12 AD |
533 | $entry_id = db_fetch_result($result, 0, "id"); |
534 | $entry_int_id = db_fetch_result($result, 0, "int_id"); | |
535 | ||
536 | foreach ($entry_tags as $tag) { | |
537 | $tag = db_escape_string(strtolower($tag)); | |
31483fc1 AD |
538 | |
539 | $tag = str_replace("+", " ", $tag); | |
fe99ab12 AD |
540 | $tag = str_replace("technorati tag: ", "", $tag); |
541 | ||
542 | $result = db_query($link, "SELECT id FROM ttrss_tags | |
543 | WHERE tag_name = '$tag' AND post_int_id = '$entry_int_id' AND | |
544 | owner_uid = '$owner_uid' LIMIT 1"); | |
545 | ||
546 | // print db_fetch_result($result, 0, "id"); | |
547 | ||
548 | if ($result && db_num_rows($result) == 0) { | |
549 | ||
550 | // print "tagging $entry_id as $tag<br>"; | |
551 | ||
552 | db_query($link, "INSERT INTO ttrss_tags | |
553 | (owner_uid,tag_name,post_int_id) | |
554 | VALUES ('$owner_uid','$tag', '$entry_int_id')"); | |
555 | } | |
556 | } | |
eb36b4eb | 557 | } |
05732aa0 | 558 | } |
4c193675 | 559 | } |
40d13c28 | 560 | |
ab3d0b99 AD |
561 | db_query($link, "UPDATE ttrss_feeds |
562 | SET last_updated = NOW(), last_error = '' WHERE id = '$feed'"); | |
eb36b4eb | 563 | |
dd8c76a9 AD |
564 | db_query($link, "COMMIT"); |
565 | ||
ab3d0b99 AD |
566 | } else { |
567 | $error_msg = db_escape_string(magpie_error()); | |
568 | db_query($link, | |
aa5f9f5f AD |
569 | "UPDATE ttrss_feeds SET last_error = '$error_msg', |
570 | last_updated = NOW() WHERE id = '$feed'"); | |
40d13c28 AD |
571 | } |
572 | ||
573 | } | |
574 | ||
f175937c AD |
575 | function print_select($id, $default, $values, $attributes = "") { |
576 | print "<select id=\"$id\" $attributes>"; | |
a0d53889 AD |
577 | foreach ($values as $v) { |
578 | if ($v == $default) | |
579 | $sel = " selected"; | |
580 | else | |
581 | $sel = ""; | |
582 | ||
583 | print "<option$sel>$v</option>"; | |
584 | } | |
585 | print "</select>"; | |
586 | } | |
40d13c28 | 587 | |
19c9cb11 | 588 | function get_filter_name($title, $content, $link, $filters) { |
e6155a06 AD |
589 | |
590 | if ($filters["title"]) { | |
19c9cb11 AD |
591 | foreach ($filters["title"] as $filter) { |
592 | $reg_exp = $filter["reg_exp"]; | |
593 | if (preg_match("/$reg_exp/i", $title)) { | |
594 | return $filter["action"]; | |
595 | } | |
e6155a06 AD |
596 | } |
597 | } | |
598 | ||
599 | if ($filters["content"]) { | |
19c9cb11 AD |
600 | foreach ($filters["content"] as $filter) { |
601 | $reg_exp = $filter["reg_exp"]; | |
602 | if (preg_match("/$reg_exp/i", $content)) { | |
603 | return $filter["action"]; | |
604 | } | |
e6155a06 AD |
605 | } |
606 | } | |
607 | ||
608 | if ($filters["both"]) { | |
609 | foreach ($filters["both"] as $filter) { | |
19c9cb11 AD |
610 | $reg_exp = $filter["reg_exp"]; |
611 | if (preg_match("/$reg_exp/i", $title) || | |
612 | preg_match("/$reg_exp/i", $content)) { | |
613 | return $filter["action"]; | |
614 | } | |
e6155a06 AD |
615 | } |
616 | } | |
617 | ||
3a933f22 | 618 | if ($filters["link"]) { |
19c9cb11 AD |
619 | $reg_exp = $filter["reg_exp"]; |
620 | foreach ($filters["link"] as $filter) { | |
621 | $reg_exp = $filter["reg_exp"]; | |
622 | if (preg_match("/$reg_exp/i", $link)) { | |
623 | return $filter["action"]; | |
624 | } | |
3a933f22 AD |
625 | } |
626 | } | |
627 | ||
e6155a06 AD |
628 | return false; |
629 | } | |
630 | ||
4668523d | 631 | function printFeedEntry($feed_id, $class, $feed_title, $unread, $icon_file, $link) { |
254e0e4b AD |
632 | |
633 | if (file_exists($icon_file) && filesize($icon_file) > 0) { | |
023fe037 | 634 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"$icon_file\">"; |
254e0e4b | 635 | } else { |
023fe037 | 636 | $feed_icon = "<img id=\"FIMG-$feed_id\" src=\"images/blank_icon.gif\">"; |
254e0e4b AD |
637 | } |
638 | ||
8143ae1f | 639 | $feed = "<a href=\"javascript:viewfeed('$feed_id', 0);\">$feed_title</a>"; |
254e0e4b AD |
640 | |
641 | print "<li id=\"FEEDR-$feed_id\" class=\"$class\">"; | |
b619ff15 | 642 | if (get_pref($link, 'ENABLE_FEED_ICONS')) { |
254e0e4b AD |
643 | print "$feed_icon"; |
644 | } | |
645 | ||
646 | print "<span id=\"FEEDN-$feed_id\">$feed</span>"; | |
647 | ||
648 | if ($unread != 0) { | |
649 | $fctr_class = ""; | |
650 | } else { | |
651 | $fctr_class = "class=\"invisible\""; | |
652 | } | |
653 | ||
654 | print "<span $fctr_class id=\"FEEDCTR-$feed_id\"> | |
655 | (<span id=\"FEEDU-$feed_id\">$unread</span>)</span>"; | |
656 | ||
657 | print "</li>"; | |
658 | ||
659 | } | |
660 | ||
406d9489 AD |
661 | function getmicrotime() { |
662 | list($usec, $sec) = explode(" ",microtime()); | |
663 | return ((float)$usec + (float)$sec); | |
664 | } | |
665 | ||
77e96719 AD |
666 | function print_radio($id, $default, $values, $attributes = "") { |
667 | foreach ($values as $v) { | |
668 | ||
669 | if ($v == $default) | |
5da169d9 | 670 | $sel = "checked"; |
77e96719 | 671 | else |
5da169d9 AD |
672 | $sel = ""; |
673 | ||
674 | if ($v == "Yes") { | |
675 | $sel .= " value=\"1\""; | |
676 | } else { | |
677 | $sel .= " value=\"0\""; | |
678 | } | |
77e96719 AD |
679 | |
680 | print "<input type=\"radio\" $sel $attributes name=\"$id\"> $v "; | |
681 | ||
682 | } | |
683 | } | |
684 | ||
ff485f1d AD |
685 | function initialize_user_prefs($link, $uid) { |
686 | ||
687 | $uid = db_escape_string($uid); | |
688 | ||
689 | db_query($link, "BEGIN"); | |
690 | ||
691 | $result = db_query($link, "SELECT pref_name,def_value FROM ttrss_prefs"); | |
692 | ||
693 | $u_result = db_query($link, "SELECT pref_name | |
694 | FROM ttrss_user_prefs WHERE owner_uid = '$uid'"); | |
695 | ||
696 | $active_prefs = array(); | |
697 | ||
698 | while ($line = db_fetch_assoc($u_result)) { | |
699 | array_push($active_prefs, $line["pref_name"]); | |
700 | } | |
701 | ||
702 | while ($line = db_fetch_assoc($result)) { | |
703 | if (array_search($line["pref_name"], $active_prefs) === FALSE) { | |
704 | // print "adding " . $line["pref_name"] . "<br>"; | |
705 | ||
706 | db_query($link, "INSERT INTO ttrss_user_prefs | |
707 | (owner_uid,pref_name,value) VALUES | |
708 | ('$uid', '".$line["pref_name"]."','".$line["def_value"]."')"); | |
709 | ||
710 | } | |
711 | } | |
712 | ||
713 | db_query($link, "COMMIT"); | |
714 | ||
715 | } | |
c8437f35 AD |
716 | |
717 | function authenticate_user($link, $login, $password) { | |
718 | ||
719 | $pwd_hash = 'SHA1:' . sha1($password); | |
720 | ||
203b6d25 | 721 | $result = db_query($link, "SELECT id,login,access_level FROM ttrss_users WHERE |
09829e2a AD |
722 | login = '$login' AND ((pwd_hash = '$password' AND '$password' = 'password') |
723 | OR pwd_hash = '$pwd_hash')"); | |
c8437f35 AD |
724 | |
725 | if (db_num_rows($result) == 1) { | |
726 | $_SESSION["uid"] = db_fetch_result($result, 0, "id"); | |
727 | $_SESSION["name"] = db_fetch_result($result, 0, "login"); | |
203b6d25 | 728 | $_SESSION["access_level"] = db_fetch_result($result, 0, "access_level"); |
c8437f35 | 729 | |
f6f32198 AD |
730 | db_query($link, "UPDATE ttrss_users SET last_login = NOW() WHERE id = " . |
731 | $_SESSION["uid"]); | |
732 | ||
503eb349 AD |
733 | $user_theme = get_user_theme_path($link); |
734 | ||
735 | $_SESSION["theme"] = $user_theme; | |
736 | ||
f557cd78 AD |
737 | initialize_user_prefs($link, $_SESSION["uid"]); |
738 | ||
c8437f35 AD |
739 | return true; |
740 | } | |
ff485f1d | 741 | |
c8437f35 AD |
742 | return false; |
743 | ||
744 | } | |
745 | ||
e6cb77a0 AD |
746 | function make_password($length = 8) { |
747 | ||
748 | $password = ""; | |
798f722b AD |
749 | $possible = "0123456789abcdfghjkmnpqrstvwxyzABCDFGHJKMNPQRSTVWXYZ"; |
750 | ||
751 | $i = 0; | |
e6cb77a0 AD |
752 | |
753 | while ($i < $length) { | |
754 | $char = substr($possible, mt_rand(0, strlen($possible)-1), 1); | |
755 | ||
756 | if (!strstr($password, $char)) { | |
757 | $password .= $char; | |
758 | $i++; | |
759 | } | |
760 | } | |
761 | return $password; | |
762 | } | |
763 | ||
764 | // this is called after user is created to initialize default feeds, labels | |
765 | // or whatever else | |
766 | ||
767 | // user preferences are checked on every login, not here | |
768 | ||
769 | function initialize_user($link, $uid) { | |
770 | ||
771 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
772 | values ('$uid','unread = true', 'Unread articles')"); | |
773 | ||
774 | db_query($link, "insert into ttrss_labels (owner_uid,sql_exp,description) | |
775 | values ('$uid','last_read is null and unread = false', 'Updated articles')"); | |
776 | ||
777 | db_query($link, "insert into ttrss_feeds (owner_uid,title,feed_url) | |
74bff337 | 778 | values ('$uid', 'Tiny Tiny RSS: New Releases', |
628fcd2c | 779 | 'http://tt-rss.spb.ru/releases.rss')"); |
3b0feb9b AD |
780 | |
781 | } | |
e6cb77a0 | 782 | |
b8aa49bc | 783 | function logout_user() { |
f557cd78 | 784 | session_destroy(); |
b8aa49bc AD |
785 | } |
786 | ||
75836f33 | 787 | function get_script_urlpath() { |
87a79fa4 | 788 | return preg_replace('/\/[^\/]*$/', "", $_SERVER["REQUEST_URI"]); |
75836f33 AD |
789 | } |
790 | ||
791 | function get_login_redirect() { | |
792 | $server = $_SERVER["SERVER_NAME"]; | |
793 | ||
794 | if (ENABLE_LOGIN_SSL) { | |
795 | $protocol = "https"; | |
796 | } else { | |
797 | $protocol = "http"; | |
798 | } | |
799 | ||
800 | $url_path = get_script_urlpath(); | |
801 | ||
802 | $redirect_uri = "$protocol://$server$url_path/login.php"; | |
803 | ||
804 | return $redirect_uri; | |
805 | } | |
806 | ||
b8aa49bc AD |
807 | function login_sequence($link) { |
808 | if (!SINGLE_USER_MODE) { | |
75836f33 | 809 | |
b8aa49bc AD |
810 | if (!USE_HTTP_AUTH) { |
811 | if (!$_SESSION["uid"]) { | |
75836f33 | 812 | $redirect_uri = get_login_redirect(); |
e31dca14 AD |
813 | $return_to = preg_replace('/.*?\//', '', $_SERVER["REQUEST_URI"]); |
814 | header("Location: $redirect_uri?rt=$return_to"); | |
b8aa49bc AD |
815 | exit; |
816 | } | |
817 | } else { | |
f557cd78 AD |
818 | if (!$_SESSION["uid"]) { |
819 | if (!$_SERVER["PHP_AUTH_USER"]) { | |
820 | ||
821 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
822 | header('HTTP/1.0 401 Unauthorized'); | |
823 | exit; | |
824 | ||
825 | } else { | |
826 | $auth_result = authenticate_user($link, | |
827 | $_SERVER["PHP_AUTH_USER"], $_SERVER["PHP_AUTH_PW"]); | |
828 | ||
829 | if (!$auth_result) { | |
830 | header('WWW-Authenticate: Basic realm="Tiny Tiny RSS"'); | |
831 | header('HTTP/1.0 401 Unauthorized'); | |
832 | exit; | |
833 | } | |
834 | } | |
835 | } | |
b8aa49bc AD |
836 | } |
837 | } else { | |
838 | $_SESSION["uid"] = 1; | |
839 | $_SESSION["name"] = "admin"; | |
c7a03b7a | 840 | initialize_user_prefs($link, 1); |
b8aa49bc AD |
841 | } |
842 | } | |
3547842a AD |
843 | |
844 | function truncate_string($str, $max_len) { | |
12db369c AD |
845 | if (mb_strlen($str, "utf-8") > $max_len - 3) { |
846 | return mb_substr($str, 0, $max_len, "utf-8") . "..."; | |
3547842a AD |
847 | } else { |
848 | return $str; | |
849 | } | |
850 | } | |
54a60e1a AD |
851 | |
852 | function get_user_theme_path($link) { | |
798f722b AD |
853 | $result = db_query($link, "SELECT theme_path |
854 | FROM | |
855 | ttrss_themes,ttrss_users | |
856 | WHERE ttrss_themes.id = theme_id AND ttrss_users.id = " . $_SESSION["uid"]); | |
54a60e1a AD |
857 | if (db_num_rows($result) != 0) { |
858 | return db_fetch_result($result, 0, "theme_path"); | |
859 | } else { | |
860 | return null; | |
861 | } | |
862 | } | |
be773442 AD |
863 | |
864 | function smart_date_time($timestamp) { | |
865 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
866 | return date("G:i", $timestamp); | |
f26450f1 | 867 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
868 | return date("M d, G:i", $timestamp); |
869 | } else { | |
870 | return date("Y/m/d G:i"); | |
871 | } | |
872 | } | |
873 | ||
874 | function smart_date($timestamp) { | |
875 | if (date("Y.m.d", $timestamp) == date("Y.m.d")) { | |
876 | return "Today"; | |
f26450f1 | 877 | } else if (date("Y", $timestamp) == date("Y")) { |
be773442 AD |
878 | return date("D m", $timestamp); |
879 | } else { | |
880 | return date("Y/m/d"); | |
881 | } | |
882 | } | |
a654a595 AD |
883 | |
884 | function sql_bool_to_string($s) { | |
885 | if ($s == "t" || $s == "1") { | |
886 | return "true"; | |
887 | } else { | |
888 | return "false"; | |
889 | } | |
890 | } | |
e3c99f3b AD |
891 | |
892 | function sql_bool_to_bool($s) { | |
893 | if ($s == "t" || $s == "1") { | |
894 | return true; | |
895 | } else { | |
896 | return false; | |
897 | } | |
898 | } | |
0ea4fb50 | 899 | |
e3c99f3b | 900 | |
0ea4fb50 AD |
901 | function toggleEvenOdd($a) { |
902 | if ($a == "even") | |
903 | return "odd"; | |
904 | else | |
905 | return "even"; | |
906 | } | |
6043fb7e AD |
907 | |
908 | function sanity_check($link) { | |
9cbca41f | 909 | |
6043fb7e AD |
910 | $error_code = 0; |
911 | $result = db_query($link, "SELECT schema_version FROM ttrss_version"); | |
912 | $schema_version = db_fetch_result($result, 0, "schema_version"); | |
913 | ||
914 | if ($schema_version != SCHEMA_VERSION) { | |
915 | $error_code = 5; | |
916 | } | |
917 | ||
6043fb7e | 918 | if ($error_code != 0) { |
9cbca41f | 919 | print "<error error-code='$error_code'/>"; |
6043fb7e AD |
920 | return false; |
921 | } else { | |
922 | return true; | |
9cbca41f | 923 | } |
6043fb7e AD |
924 | } |
925 | ||
40d13c28 | 926 | ?> |