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